Bugfix: use the command parser to properly extract workspace names

fixes #1377
This commit is contained in:
Michael Stapelberg 2014-10-01 22:50:48 +02:00
parent 7fe55090ec
commit ea2552e852
4 changed files with 90 additions and 49 deletions

View File

@ -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

View File

@ -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

View File

@ -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 —

View File

@ -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 = <<EOT;
# i3 config file (v4)
font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
bindsym Mod1+1 workspace 3; exec foo
EOT
$pid = launch_with_config($config);
@names = @{get_workspace_names()};
is_deeply(\@names, [ '3' ], 'i3 starts on workspace 3 without ;exec foo');
exit_gracefully($pid);
done_testing;