aboutsummaryrefslogtreecommitdiff
path: root/tokenize.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2020-07-27 14:43:41 +0000
committerDaniel Eklöf <daniel@ekloef.se>2020-07-27 14:43:41 +0000
commit50bd51c4d43931418c5b8c706841b46b67c1a947 (patch)
tree3786a820f415c0b9354bdd3614033d87b64fe4e5 /tokenize.c
parentMerge branch 'tokenizer-reset-argv-on-error' (diff)
tokenize: handle escaped quotes
We only support escaping quotes inside a quote, and only of the same type as the enclosing quote. I.e. "double quote \"escaped double quote\"" But not "double quote \'invalid escaped single quote\'"
Diffstat (limited to 'tokenize.c')
-rw-r--r--tokenize.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/tokenize.c b/tokenize.c
index 2671e23b..35d52345 100644
--- a/tokenize.c
+++ b/tokenize.c
@@ -38,10 +38,11 @@ tokenize_cmdline(char *cmdline, char ***argv)
char delim = first_token_is_quoted ? cmdline[0] : ' ';
char *p = first_token_is_quoted ? &cmdline[1] : &cmdline[0];
+ char *search_start = p;
size_t idx = 0;
while (*p != '\0') {
- char *end = strchr(p, delim);
+ char *end = strchr(search_start, delim);
if (end == NULL) {
if (delim != ' ') {
LOG_ERR("unterminated %s quote", delim == '"' ? "double" : "single");
@@ -58,6 +59,15 @@ tokenize_cmdline(char *cmdline, char ***argv)
return true;
}
+ if (end > p && *(end - 1) == '\\') {
+ /* Escaped quote, remove one level of escaping and
+ * continue searching for "our" closing quote */
+ memmove(end - 1, end, strlen(end));
+ end[strlen(end) - 1] = '\0';
+ search_start = end;
+ continue;
+ }
+
*end = '\0';
if (!push_argv(argv, &argv_size, p, &idx))
@@ -75,6 +85,7 @@ tokenize_cmdline(char *cmdline, char ***argv)
p++;
} else
delim = ' ';
+ search_start = p;
}
if (!push_argv(argv, &argv_size, NULL, &idx))