X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcommands_parser.c;h=fa4c2360101513e4796490c9720ff9b63533915f;hb=93adcf8bdcc09ab78ed265b3392900be552596fa;hp=31729676a383f86b95e53863f4e02e6569b148b9;hpb=34f6f185bfe9476801e6ef45114197becefb0b21;p=i3%2Fi3 diff --git a/src/commands_parser.c b/src/commands_parser.c index 31729676..fa4c2360 100644 --- a/src/commands_parser.c +++ b/src/commands_parser.c @@ -204,6 +204,62 @@ 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)++; + for (; **walk != '\0' && **walk != '"'; (*walk)++) + if (**walk == '\\' && *(*walk + 1) != '\0') + (*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 and backslashes to not break + * backwards compatibility with people using \w in regular expressions + * etc. */ + if (beginning[inpos] == '\\' && (beginning[inpos + 1] == '"' || 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 +318,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