]> git.sur5r.net Git - i3/i3/blobdiff - src/commands_parser.c
Merge branch 'fix-dump-log-errmsg'
[i3/i3] / src / commands_parser.c
index cdfafee2b9aaca0bfc5f2fc7f2fa65f5a3ea9cb1..73a14565ff3201ff0f8b741fcfdadf8ee51cd3a6 100644 (file)
 
 #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))
+
 /*******************************************************************************
  * The data structures used for parsing. Essentially the current state and a
  * list of tokens for that state.
@@ -91,7 +95,9 @@ 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);
 }
 
@@ -108,7 +114,7 @@ static char *get_string(const char *identifier) {
     return NULL;
 }
 
-static void clear_stack() {
+static void clear_stack(void) {
     DLOG("clearing stack.\n");
     for (int c = 0; c < 10; c++) {
         if (stack[c].str != NULL)
@@ -173,7 +179,8 @@ 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"
 
@@ -182,7 +189,13 @@ 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,10 +207,19 @@ static void next_state(const cmdp_token *token) {
 }
 
 /* TODO: Return parsing errors via JSON. */
-char *parse_command(const char *input) {
+struct CommandResult *parse_command(const char *input) {
     DLOG("new parser handling: %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);
@@ -207,7 +229,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
@@ -312,7 +334,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;
@@ -371,14 +393,29 @@ char *parse_command(const char *input) {
             printf("Your command: %s\n", input);
             printf("              %s\n", position);
 
+            /* Format this error message as a JSON reply. */
+            y(map_open);
+            ystr("success");
+            y(bool, false);
+            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);
+
+    DLOG("command_output.needs_tree_render = %d\n", command_output.needs_tree_render);
+    return &command_output;
 }
 
 /*******************************************************************************