]> git.sur5r.net Git - i3/i3/blobdiff - src/commands_parser.c
Merge pull request #1737 from Airblader/feature-xdotool-on-travis
[i3/i3] / src / commands_parser.c
index 57249d2ec31b77e37b857bc6e18a7e135fca164b..9ae75abea21ff28a1fb53501ece991a2037e46d8 100644 (file)
@@ -4,7 +4,7 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
  *
  * commands_parser.c: hand-written parser to parse commands (commands are what
  * you bind on keys and what you can send to i3 using the IPC interface, like
@@ -35,8 +35,8 @@
 #include "all.h"
 
 // Macros to make the YAJL API a bit easier to use.
-#define y(x, ...) (command_output.json_gen != NULL ? yajl_gen_ ## x (command_output.json_gen, ##__VA_ARGS__) : 0)
-#define ystr(str) (command_output.json_gen != NULL ? yajl_gen_string(command_output.json_gen, (unsigned char*)str, strlen(str)) : 0)
+#define y(x, ...) (command_output.json_gen != NULL ? yajl_gen_##x(command_output.json_gen, ##__VA_ARGS__) : 0)
+#define ystr(str) (command_output.json_gen != NULL ? yajl_gen_string(command_output.json_gen, (unsigned char *)str, strlen(str)) : 0)
 
 /*******************************************************************************
  * The data structures used for parsing. Essentially the current state and a
@@ -184,7 +184,6 @@ static struct CommandResultIR command_output;
 
 #include "GENERATED_command_call.h"
 
-
 static void next_state(const cmdp_token *token) {
     if (token->next_state == __CALL) {
         subcommand_output.json_gen = command_output.json_gen;
@@ -205,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
@@ -229,7 +284,7 @@ CommandResult *parse_command(const char *input, yajl_gen gen) {
     const cmdp_token *token;
     bool token_handled;
 
-    // TODO: make this testable
+// TODO: make this testable
 #ifndef TEST_PARSER
     cmd_criteria_init(&current_match, &subcommand_output);
 #endif
@@ -239,7 +294,8 @@ CommandResult *parse_command(const char *input, yajl_gen gen) {
     while ((size_t)(walk - input) <= len) {
         /* skip whitespace and newlines before every token */
         while ((*walk == ' ' || *walk == '\t' ||
-                *walk == '\r' || *walk == '\n') && *walk != '\0')
+                *walk == '\r' || *walk == '\n') &&
+               *walk != '\0')
             walk++;
 
         cmdp_token_ptr *ptr = &(tokens[state]);
@@ -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
@@ -320,19 +336,19 @@ CommandResult *parse_command(const char *input, yajl_gen gen) {
                 if (*walk == '\0' || *walk == ',' || *walk == ';') {
                     next_state(token);
                     token_handled = true;
-                    /* To make sure we start with an appropriate matching
+/* To make sure we start with an appropriate matching
                      * datastructure for commands which do *not* specify any
                      * criteria, we re-initialize the criteria system after
                      * every command. */
-                    // TODO: make this testable
+// TODO: make this testable
 #ifndef TEST_PARSER
                     if (*walk == '\0' || *walk == ';')
                         cmd_criteria_init(&current_match, &subcommand_output);
 #endif
                     walk++;
                     break;
-               }
-           }
+                }
+            }
         }
 
         if (!token_handled) {