From 36b405c633c7ea0ca02cebea0e1189b14f9245a3 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 8 May 2017 19:40:27 -0400 Subject: [PATCH] bt_common_shell_quote(): accept new parameter `with_single_quotes` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This parameter indicates if the function must put the initial and final `'` character itself or if it's the caller's job. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- common/common.c | 15 +++++++++++---- include/babeltrace/common-internal.h | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/common/common.c b/common/common.c index 8d202ea5..a987e83e 100644 --- a/common/common.c +++ b/common/common.c @@ -398,7 +398,7 @@ end: } BT_HIDDEN -GString *bt_common_shell_quote(const char *input) +GString *bt_common_shell_quote(const char *input, bool with_single_quotes) { GString *output = g_string_new(NULL); const char *ch; @@ -409,7 +409,10 @@ GString *bt_common_shell_quote(const char *input) } if (strlen(input) == 0) { - g_string_assign(output, "''"); + if (with_single_quotes) { + g_string_assign(output, "''"); + } + goto end; } @@ -430,7 +433,9 @@ GString *bt_common_shell_quote(const char *input) goto end; } - g_string_assign(output, "'"); + if (with_single_quotes) { + g_string_assign(output, "'"); + } for (ch = input; *ch != '\0'; ch++) { if (*ch == '\'') { @@ -440,7 +445,9 @@ GString *bt_common_shell_quote(const char *input) } } - g_string_append_c(output, '\''); + if (with_single_quotes) { + g_string_append_c(output, '\''); + } end: return output; diff --git a/include/babeltrace/common-internal.h b/include/babeltrace/common-internal.h index b65a9d0c..13c4b79a 100644 --- a/include/babeltrace/common-internal.h +++ b/include/babeltrace/common-internal.h @@ -106,7 +106,7 @@ GString *bt_common_string_until(const char *input, const char *escapable_chars, const char *end_chars, size_t *end_pos); BT_HIDDEN -GString *bt_common_shell_quote(const char *input); +GString *bt_common_shell_quote(const char *input, bool with_single_quotes); BT_HIDDEN bool bt_common_string_is_printable(const char *input); -- 2.34.1