]> git.sur5r.net Git - i3/i3/blobdiff - src/commands_parser.c
generate-command-parser: make input/output configurable
[i3/i3] / src / commands_parser.c
index 1bb9f0c2ba8ac5fb65a77af0ecaa6c1d2453729c..bbba2c44eb5cbc72a69eb5e073266690b401a892 100644 (file)
@@ -1,3 +1,5 @@
+#undef I3__FILE__
+#define I3__FILE__ "commands_parser.c"
 /*
  * vim:ts=4:sw=4:expandtab
  *
 #include <stdint.h>
 
 #include "all.h"
-#include "queue.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))
 
 /*******************************************************************************
  * The data structures used for parsing. Essentially the current state and a
@@ -41,7 +46,7 @@
  * input parser-specs/commands.spec.
  ******************************************************************************/
 
-#include "GENERATED_enums.h"
+#include "GENERATED_commands_enums.h"
 
 typedef struct token {
     char *name;
@@ -58,7 +63,7 @@ typedef struct tokenptr {
     int n;
 } cmdp_token_ptr;
 
-#include "GENERATED_tokens.h"
+#include "GENERATED_commands_tokens.h"
 
 /*******************************************************************************
  * The (small) stack where identified literals are stored during the parsing
@@ -92,14 +97,15 @@ static void push_string(const char *identifier, char *str) {
     /* 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. */
-    printf("argh! stack full\n");
+    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);
 }
 
 // XXX: ideally, this would be const char. need to check if that works with all
 // called functions.
 static char *get_string(const char *identifier) {
-    DLOG("Getting string %s from stack...\n", identifier);
     for (int c = 0; c < 10; c++) {
         if (stack[c].identifier == NULL)
             break;
@@ -109,8 +115,7 @@ static char *get_string(const char *identifier) {
     return NULL;
 }
 
-static void clear_stack() {
-    DLOG("clearing stack.\n");
+static void clear_stack(void) {
     for (int c = 0; c < 10; c++) {
         if (stack[c].str != NULL)
             free(stack[c].str);
@@ -174,16 +179,21 @@ static cmdp_state state;
 #ifndef TEST_PARSER
 static Match current_match;
 #endif
-static char *json_output;
+static struct CommandResult subcommand_output;
+static struct CommandResult command_output;
 
-#include "GENERATED_call.h"
+#include "GENERATED_commands_call.h"
 
 
 static void next_state(const cmdp_token *token) {
     if (token->next_state == __CALL) {
-        DLOG("should call stuff, yay. call_id = %d\n",
-                token->extra.call_identifier);
-        json_output = GENERATED_call(token->extra.call_identifier);
+        subcommand_output.json_gen = command_output.json_gen;
+        subcommand_output.needs_tree_render = false;
+        GENERATED_call(token->extra.call_identifier, &subcommand_output);
+        /* If any subcommand requires a tree_render(), we need to make the
+         * whole parser result request a tree_render(). */
+        if (subcommand_output.needs_tree_render)
+            command_output.needs_tree_render = true;
         clear_stack();
         return;
     }
@@ -194,11 +204,19 @@ static void next_state(const cmdp_token *token) {
     }
 }
 
-/* TODO: Return parsing errors via JSON. */
-char *parse_command(const char *input) {
-    DLOG("new parser handling: %s\n", input);
+struct CommandResult *parse_command(const char *input) {
+    DLOG("COMMAND: *%s*\n", input);
     state = INITIAL;
-    json_output = NULL;
+
+/* A YAJL JSON generator used for formatting replies. */
+#if YAJL_MAJOR >= 2
+    command_output.json_gen = yajl_gen_alloc(NULL);
+#else
+    command_output.json_gen = yajl_gen_alloc(NULL, NULL);
+#endif
+
+    y(array_open);
+    command_output.needs_tree_render = false;
 
     const char *walk = input;
     const size_t len = strlen(input);
@@ -208,7 +226,7 @@ char *parse_command(const char *input) {
 
     // TODO: make this testable
 #ifndef TEST_PARSER
-    cmd_criteria_init(&current_match);
+    cmd_criteria_init(&current_match, &subcommand_output);
 #endif
 
     /* The "<=" operator is intentional: We also handle the terminating 0-byte
@@ -219,21 +237,16 @@ char *parse_command(const char *input) {
                 *walk == '\r' || *walk == '\n') && *walk != '\0')
             walk++;
 
-        DLOG("remaining input = %s\n", walk);
-
         cmdp_token_ptr *ptr = &(tokens[state]);
         token_handled = false;
         for (c = 0; c < ptr->n; c++) {
             token = &(ptr->array[c]);
-            DLOG("trying token %d = %s\n", c, token->name);
 
             /* A literal. */
             if (token->name[0] == '\'') {
-                DLOG("literal\n");
                 if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) {
-                    DLOG("found literal, moving to next state\n");
                     if (token->identifier != NULL)
-                        push_string(token->identifier, strdup(token->name + 1));
+                        push_string(token->identifier, sstrdup(token->name + 1));
                     walk += strlen(token->name) - 1;
                     next_state(token);
                     token_handled = true;
@@ -244,13 +257,12 @@ char *parse_command(const char *input) {
 
             if (strcmp(token->name, "string") == 0 ||
                 strcmp(token->name, "word") == 0) {
-                DLOG("parsing this as a string\n");
                 const char *beginning = walk;
                 /* Handle quoted strings (or words). */
                 if (*walk == '"') {
                     beginning++;
                     walk++;
-                    while (*walk != '"' || *(walk-1) == '\\')
+                    while (*walk != '\0' && (*walk != '"' || *(walk-1) == '\\'))
                         walk++;
                 } else {
                     if (token->name[0] == 's') {
@@ -274,11 +286,21 @@ char *parse_command(const char *input) {
                     }
                 }
                 if (walk != beginning) {
-                    char *str = calloc(walk-beginning + 1, 1);
-                    strncpy(str, beginning, 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];
+                    }
                     if (token->identifier)
                         push_string(token->identifier, str);
-                    DLOG("str is \"%s\"\n", str);
                     /* If we are at the end of a quoted string, skip the ending
                      * double quote. */
                     if (*walk == '"')
@@ -290,9 +312,7 @@ char *parse_command(const char *input) {
             }
 
             if (strcmp(token->name, "end") == 0) {
-                DLOG("checking for the end token.\n");
                 if (*walk == '\0' || *walk == ',' || *walk == ';') {
-                    DLOG("yes, indeed. end\n");
                     next_state(token);
                     token_handled = true;
                     /* To make sure we start with an appropriate matching
@@ -302,7 +322,7 @@ char *parse_command(const char *input) {
                     // TODO: make this testable
 #ifndef TEST_PARSER
                     if (*walk == '\0' || *walk == ';')
-                        cmd_criteria_init(&current_match);
+                        cmd_criteria_init(&current_match, &subcommand_output);
 #endif
                     walk++;
                     break;
@@ -321,7 +341,7 @@ char *parse_command(const char *input) {
              * full input, and underline the position where the parser
              * currently is. */
             char *errormessage;
-            char *possible_tokens = malloc(tokenlen + 1);
+            char *possible_tokens = smalloc(tokenlen + 1);
             char *tokenwalk = possible_tokens;
             for (c = 0; c < ptr->n; c++) {
                 token = &(ptr->array[c]);
@@ -346,29 +366,48 @@ char *parse_command(const char *input) {
                 }
             }
             *tokenwalk = '\0';
-            asprintf(&errormessage, "Expected one of these tokens: %s",
-                     possible_tokens);
+            sasprintf(&errormessage, "Expected one of these tokens: %s",
+                      possible_tokens);
             free(possible_tokens);
 
             /* Contains the same amount of characters as 'input' has, but with
              * the unparseable part highlighted using ^ characters. */
-            char *position = malloc(len + 1);
+            char *position = smalloc(len + 1);
             for (const char *copywalk = input; *copywalk != '\0'; copywalk++)
                 position[(copywalk - input)] = (copywalk >= walk ? '^' : ' ');
             position[len] = '\0';
 
-            printf("%s\n", errormessage);
-            printf("Your command: %s\n", input);
-            printf("              %s\n", position);
+            ELOG("%s\n", errormessage);
+            ELOG("Your command: %s\n", input);
+            ELOG("              %s\n", position);
+
+            /* Format this error message as a JSON reply. */
+            y(map_open);
+            ystr("success");
+            y(bool, false);
+            /* We set parse_error to true to distinguish this from other
+             * errors. i3-nagbar is spawned upon keypresses only for parser
+             * errors. */
+            ystr("parse_error");
+            y(bool, true);
+            ystr("error");
+            ystr(errormessage);
+            ystr("input");
+            ystr(input);
+            ystr("errorposition");
+            ystr(position);
+            y(map_close);
 
             free(position);
             free(errormessage);
+            clear_stack();
             break;
         }
     }
 
-    DLOG("json_output = %s\n", json_output);
-    return json_output;
+    y(array_close);
+
+    return &command_output;
 }
 
 /*******************************************************************************
@@ -380,15 +419,23 @@ char *parse_command(const char *input) {
 
 /*
  * Logs the given message to stdout while prefixing the current time to it,
- * but only if the corresponding debug loglevel was activated.
+ * but only if debug logging was activated.
  * This is to be called by DLOG() which includes filename/linenumber
  *
  */
-void debuglog(uint64_t lev, char *fmt, ...) {
+void debuglog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    fprintf(stdout, "# ");
+    vfprintf(stdout, fmt, args);
+    va_end(args);
+}
+
+void errorlog(char *fmt, ...) {
     va_list args;
 
     va_start(args, fmt);
-    fprintf(stderr, "# ");
     vfprintf(stderr, fmt, args);
     va_end(args);
 }