]> git.sur5r.net Git - i3/i3/blobdiff - src/commands_parser.c
Merge branch 'release-4.16.1'
[i3/i3] / src / commands_parser.c
index 870260487358cd8dccb9ff498e302662c64e6d78..4299c008335570ced9c8daebc12bdaa1cefd74e9 100644 (file)
@@ -1,10 +1,8 @@
-#undef I3__FILE__
-#define I3__FILE__ "commands_parser.c"
 /*
  * 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
@@ -25,6 +23,8 @@
  * instead of actually calling any function).
  *
  */
+#include "all.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
 #include <stdint.h>
 
-#include "all.h"
-
 // Macros to make the YAJL API a bit easier to use.
-#define y(x, ...) yajl_gen_ ## x (command_output.json_gen, ##__VA_ARGS__)
-#define ystr(str) yajl_gen_string(command_output.json_gen, (unsigned char*)str, strlen(str))
+#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
@@ -73,7 +71,14 @@ typedef struct tokenptr {
 struct stack_entry {
     /* Just a pointer, not dynamically allocated. */
     const char *identifier;
-    char *str;
+    enum {
+        STACK_STR = 0,
+        STACK_LONG = 1,
+    } type;
+    union {
+        char *str;
+        long num;
+    } val;
 };
 
 /* 10 entries should be enough for everybody. */
@@ -90,7 +95,30 @@ static void push_string(const char *identifier, char *str) {
             continue;
         /* Found a free slot, let’s store it here. */
         stack[c].identifier = identifier;
-        stack[c].str = str;
+        stack[c].val.str = str;
+        stack[c].type = STACK_STR;
+        return;
+    }
+
+    /* When we arrive here, the stack is full. This should not happen and
+     * means there’s either a bug in this parser or the specification
+     * contains a command with more than 10 identified tokens. */
+    fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
+                    "in the code, or a new command which contains more than "
+                    "10 identified tokens.\n");
+    exit(1);
+}
+
+// TODO move to a common util
+static void push_long(const char *identifier, long num) {
+    for (int c = 0; c < 10; c++) {
+        if (stack[c].identifier != NULL) {
+            continue;
+        }
+
+        stack[c].identifier = identifier;
+        stack[c].val.num = num;
+        stack[c].type = STACK_LONG;
         return;
     }
 
@@ -103,73 +131,39 @@ static void push_string(const char *identifier, char *str) {
     exit(1);
 }
 
-// XXX: ideally, this would be const char. need to check if that works with all
-// called functions.
-static char *get_string(const char *identifier) {
+// TODO move to a common util
+static const char *get_string(const char *identifier) {
     for (int c = 0; c < 10; c++) {
         if (stack[c].identifier == NULL)
             break;
         if (strcmp(identifier, stack[c].identifier) == 0)
-            return stack[c].str;
+            return stack[c].val.str;
     }
     return NULL;
 }
 
-static void clear_stack(void) {
+// TODO move to a common util
+static long get_long(const char *identifier) {
     for (int c = 0; c < 10; c++) {
-        if (stack[c].str != NULL)
-            free(stack[c].str);
-        stack[c].identifier = NULL;
-        stack[c].str = NULL;
+        if (stack[c].identifier == NULL)
+            break;
+        if (strcmp(identifier, stack[c].identifier) == 0)
+            return stack[c].val.num;
     }
-}
 
-// TODO: remove this if it turns out we don’t need it for testing.
-#if 0
-/*******************************************************************************
- * A dynamically growing linked list which holds the criteria for the current
- * command.
- ******************************************************************************/
-
-typedef struct criterion {
-    char *type;
-    char *value;
-
-    TAILQ_ENTRY(criterion) criteria;
-} criterion;
-
-static TAILQ_HEAD(criteria_head, criterion) criteria =
-  TAILQ_HEAD_INITIALIZER(criteria);
-
-/*
- * Stores the given type/value in the list of criteria.
- * Accepts a pointer as first argument, since it is 'call'ed by the parser.
- *
- */
-static void push_criterion(void *unused_criteria, const char *type,
-                           const char *value) {
-    struct criterion *criterion = malloc(sizeof(struct criterion));
-    criterion->type = strdup(type);
-    criterion->value = strdup(value);
-    TAILQ_INSERT_TAIL(&criteria, criterion, criteria);
+    return 0;
 }
 
-/*
- * Clears the criteria linked list.
- * Accepts a pointer as first argument, since it is 'call'ed by the parser.
- *
- */
-static void clear_criteria(void *unused_criteria) {
-    struct criterion *criterion;
-    while (!TAILQ_EMPTY(&criteria)) {
-        criterion = TAILQ_FIRST(&criteria);
-        free(criterion->type);
-        free(criterion->value);
-        TAILQ_REMOVE(&criteria, criterion, criteria);
-        free(criterion);
+// TODO move to a common util
+static void clear_stack(void) {
+    for (int c = 0; c < 10; c++) {
+        if (stack[c].type == STACK_STR)
+            free(stack[c].val.str);
+        stack[c].identifier = NULL;
+        stack[c].val.str = NULL;
+        stack[c].val.num = 0;
     }
 }
-#endif
 
 /*******************************************************************************
  * The parser itself.
@@ -179,12 +173,11 @@ static cmdp_state state;
 #ifndef TEST_PARSER
 static Match current_match;
 #endif
-static struct CommandResult subcommand_output;
-static struct CommandResult command_output;
+static struct CommandResultIR subcommand_output;
+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,12 +198,76 @@ static void next_state(const cmdp_token *token) {
     }
 }
 
-struct CommandResult *parse_command(const char *input) {
+/*
+ * 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, 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
+ * protocol. Pass NULL if no json reply is required.
+ *
+ * Free the returned CommandResult with command_result_free().
+ */
+CommandResult *parse_command(const char *input, yajl_gen gen) {
     DLOG("COMMAND: *%s*\n", input);
     state = INITIAL;
+    CommandResult *result = scalloc(1, sizeof(CommandResult));
 
     /* A YAJL JSON generator used for formatting replies. */
-    command_output.json_gen = yajl_gen_alloc(NULL);
+    command_output.json_gen = gen;
 
     y(array_open);
     command_output.needs_tree_render = false;
@@ -221,7 +278,7 @@ struct CommandResult *parse_command(const char *input) {
     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
@@ -231,7 +288,8 @@ struct CommandResult *parse_command(const char *input) {
     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]);
@@ -252,50 +310,33 @@ struct CommandResult *parse_command(const char *input) {
                 continue;
             }
 
+            if (strcmp(token->name, "number") == 0) {
+                /* Handle numbers. We only accept decimal numbers for now. */
+                char *end = NULL;
+                errno = 0;
+                long int num = strtol(walk, &end, 10);
+                if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
+                    (errno != 0 && num == 0))
+                    continue;
+
+                /* No valid numbers found */
+                if (end == walk)
+                    continue;
+
+                if (token->identifier != NULL)
+                    push_long(token->identifier, num);
+
+                /* Set walk to the first non-number character */
+                walk = end;
+                next_state(token);
+                token_handled = true;
+                break;
+            }
+
             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
@@ -312,19 +353,19 @@ struct CommandResult *parse_command(const char *input) {
                 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) {
@@ -378,6 +419,9 @@ struct CommandResult *parse_command(const char *input) {
             ELOG("Your command: %s\n", input);
             ELOG("              %s\n", position);
 
+            result->parse_error = true;
+            result->error_message = errormessage;
+
             /* Format this error message as a JSON reply. */
             y(map_open);
             ystr("success");
@@ -396,7 +440,6 @@ struct CommandResult *parse_command(const char *input) {
             y(map_close);
 
             free(position);
-            free(errormessage);
             clear_stack();
             break;
         }
@@ -404,7 +447,19 @@ struct CommandResult *parse_command(const char *input) {
 
     y(array_close);
 
-    return &command_output;
+    result->needs_tree_render = command_output.needs_tree_render;
+    return result;
+}
+
+/*
+ * Frees a CommandResult
+ */
+void command_result_free(CommandResult *result) {
+    if (result == NULL)
+        return;
+
+    FREE(result->error_message);
+    FREE(result);
 }
 
 /*******************************************************************************
@@ -442,6 +497,12 @@ int main(int argc, char *argv[]) {
         fprintf(stderr, "Syntax: %s <command>\n", argv[0]);
         return 1;
     }
-    parse_command(argv[1]);
+    yajl_gen gen = yajl_gen_alloc(NULL);
+
+    CommandResult *result = parse_command(argv[1], gen);
+
+    command_result_free(result);
+
+    yajl_gen_free(gen);
 }
 #endif