From ea2552e8524fd2064d4ed26d89ddeba49af3d717 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 1 Oct 2014 22:50:48 +0200 Subject: [PATCH 1/1] Bugfix: use the command parser to properly extract workspace names fixes #1377 --- include/commands_parser.h | 8 +++ src/commands_parser.c | 99 +++++++++++++++++------------ src/workspace.c | 14 ++-- testcases/t/172-start-on-named-ws.t | 18 ++++++ 4 files changed, 90 insertions(+), 49 deletions(-) diff --git a/include/commands_parser.h b/include/commands_parser.h index 6e531e9b..cfa44dd5 100644 --- a/include/commands_parser.h +++ b/include/commands_parser.h @@ -44,6 +44,14 @@ struct CommandResult { bool needs_tree_render; }; +/** + * Parses a string (or word, if as_word is true). Extracted out of + * parse_command so that it can be used in src/workspace.c for interpreting + * workspace commands. + * + */ +char *parse_string(const char **walk, bool as_word); + /** * Parses and executes the given command. If a caller-allocated yajl_gen is * passed, a json reply will be generated in the format specified by the ipc diff --git a/src/commands_parser.c b/src/commands_parser.c index 31729676..f325a048 100644 --- a/src/commands_parser.c +++ b/src/commands_parser.c @@ -204,6 +204,61 @@ static void next_state(const cmdp_token *token) { } } +/* + * Parses a string (or word, if as_word is true). Extracted out of + * parse_command so that it can be used in src/workspace.c for interpreting + * workspace commands. + * + */ +char *parse_string(const char **walk, bool as_word) { + const char *beginning = *walk; + /* Handle quoted strings (or words). */ + if (**walk == '"') { + beginning++; + (*walk)++; + while (**walk != '\0' && (**walk != '"' || *(*walk - 1) == '\\')) + (*walk)++; + } else { + if (!as_word) { + /* For a string (starting with 's'), the delimiters are + * comma (,) and semicolon (;) which introduce a new + * operation or command, respectively. Also, newlines + * end a command. */ + while (**walk != ';' && **walk != ',' && + **walk != '\0' && **walk != '\r' && + **walk != '\n') + (*walk)++; + } else { + /* For a word, the delimiters are white space (' ' or + * '\t'), closing square bracket (]), comma (,) and + * semicolon (;). */ + while (**walk != ' ' && **walk != '\t' && + **walk != ']' && **walk != ',' && + **walk != ';' && **walk != '\r' && + **walk != '\n' && **walk != '\0') + (*walk)++; + } + } + if (*walk == beginning) + return NULL; + + char *str = scalloc(*walk - beginning + 1); + /* We copy manually to handle escaping of characters. */ + int inpos, outpos; + for (inpos = 0, outpos = 0; + inpos < (*walk - beginning); + inpos++, outpos++) { + /* We only handle escaped double quotes to not break + * backwards compatibility with people using \w in + * regular expressions etc. */ + if (beginning[inpos] == '\\' && beginning[inpos + 1] == '"') + inpos++; + str[outpos] = beginning[inpos]; + } + + return str; +} + /* * Parses and executes the given command. If a caller-allocated yajl_gen is * passed, a json reply will be generated in the format specified by the ipc @@ -262,48 +317,8 @@ CommandResult *parse_command(const char *input, yajl_gen gen) { if (strcmp(token->name, "string") == 0 || strcmp(token->name, "word") == 0) { - const char *beginning = walk; - /* Handle quoted strings (or words). */ - if (*walk == '"') { - beginning++; - walk++; - while (*walk != '\0' && (*walk != '"' || *(walk - 1) == '\\')) - walk++; - } else { - if (token->name[0] == 's') { - /* For a string (starting with 's'), the delimiters are - * comma (,) and semicolon (;) which introduce a new - * operation or command, respectively. Also, newlines - * end a command. */ - while (*walk != ';' && *walk != ',' && - *walk != '\0' && *walk != '\r' && - *walk != '\n') - walk++; - } else { - /* For a word, the delimiters are white space (' ' or - * '\t'), closing square bracket (]), comma (,) and - * semicolon (;). */ - while (*walk != ' ' && *walk != '\t' && - *walk != ']' && *walk != ',' && - *walk != ';' && *walk != '\r' && - *walk != '\n' && *walk != '\0') - walk++; - } - } - if (walk != beginning) { - char *str = scalloc(walk - beginning + 1); - /* We copy manually to handle escaping of characters. */ - int inpos, outpos; - for (inpos = 0, outpos = 0; - inpos < (walk - beginning); - inpos++, outpos++) { - /* We only handle escaped double quotes to not break - * backwards compatibility with people using \w in - * regular expressions etc. */ - if (beginning[inpos] == '\\' && beginning[inpos + 1] == '"') - inpos++; - str[outpos] = beginning[inpos]; - } + char *str = parse_string(&walk, (token->name[0] != 's')); + if (str != NULL) { if (token->identifier) push_string(token->identifier, str); /* If we are at the end of a quoted string, skip the ending diff --git a/src/workspace.c b/src/workspace.c index 9ec664d7..1bb619c3 100644 --- a/src/workspace.c +++ b/src/workspace.c @@ -126,7 +126,7 @@ Con *create_workspace_on_output(Output *output, Con *content) { strncasecmp(bind->command, "workspace", strlen("workspace")) != 0) continue; DLOG("relevant command = %s\n", bind->command); - char *target = bind->command + strlen("workspace "); + const char *target = bind->command + strlen("workspace "); while ((*target == ' ' || *target == '\t') && target != '\0') target++; /* We check if this is the workspace @@ -142,16 +142,16 @@ Con *create_workspace_on_output(Output *output, Con *content) { strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 || strncasecmp(target, "current", strlen("current")) == 0) continue; - if (*target == '"') - target++; - if (strncasecmp(target, "__", strlen("__")) == 0) { + char *target_name = parse_string(&target, false); + if (target_name == NULL) + continue; + if (strncasecmp(target_name, "__", strlen("__")) == 0) { LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target); + free(target_name); continue; } FREE(ws->name); - ws->name = strdup(target); - if (ws->name[strlen(ws->name) - 1] == '"') - ws->name[strlen(ws->name) - 1] = '\0'; + ws->name = target_name; DLOG("trying name *%s*\n", ws->name); /* Ensure that this workspace is not assigned to a different output — diff --git a/testcases/t/172-start-on-named-ws.t b/testcases/t/172-start-on-named-ws.t index 09b708be..8b00abac 100644 --- a/testcases/t/172-start-on-named-ws.t +++ b/testcases/t/172-start-on-named-ws.t @@ -88,4 +88,22 @@ is_deeply(\@names, [ '3' ], 'i3 starts on workspace 3 without whitespace'); exit_gracefully($pid); +################################################################################ +# 5: now with a binding that contains multiple commands +################################################################################ + +$config = <