]> git.sur5r.net Git - i3/i3/commitdiff
i3-msg: parse command replies and display errors nicely if there were errors
authorMichael Stapelberg <michael@stapelberg.de>
Sat, 26 Jan 2013 16:56:43 +0000 (17:56 +0100)
committerMichael Stapelberg <michael@stapelberg.de>
Sat, 26 Jan 2013 16:56:43 +0000 (17:56 +0100)
fixes #737

i3-msg/i3-msg.mk
i3-msg/main.c

index fef9581d785c30b641024205455c5834d09063fa..b1ba7b4d647c0d4bbee5c03a10c8620a34e5df5c 100644 (file)
@@ -5,7 +5,7 @@ CLEAN_TARGETS += clean-i3-msg
 i3_msg_SOURCES := $(wildcard i3-msg/*.c)
 i3_msg_HEADERS := $(wildcard i3-msg/*.h)
 i3_msg_CFLAGS   = $(XCB_CFLAGS) $(PANGO_CFLAGS)
-i3_msg_LIBS     = $(XCB_LIBS)
+i3_msg_LIBS     = $(XCB_LIBS) $(YAJL_LIBS)
 
 i3_msg_OBJECTS := $(i3_msg_SOURCES:.c=.o)
 
index 3259043c4393a8e428fe3141c7c515271ac0c593..a1428fb8f5f89aac9e35f68420f983b808ee7303 100644 (file)
@@ -28,6 +28,9 @@
 #include <getopt.h>
 #include <limits.h>
 
+#include <yajl/yajl_parse.h>
+#include <yajl/yajl_version.h>
+
 #include <xcb/xcb.h>
 #include <xcb/xcb_aux.h>
 
@@ -56,6 +59,79 @@ void errorlog(char *fmt, ...) {
     va_end(args);
 }
 
+static char *last_key = NULL;
+
+typedef struct reply_t {
+    bool success;
+    char *error;
+    char *input;
+    char *errorposition;
+} reply_t;
+
+static reply_t last_reply;
+
+static int reply_boolean_cb(void *params, int val) {
+    if (strcmp(last_key, "success") == 0)
+        last_reply.success = val;
+    return 1;
+}
+
+#if YAJL_MAJOR >= 2
+static int reply_string_cb(void *params, const unsigned char *val, size_t len) {
+#else
+static int reply_string_cb(void *params, const unsigned char *val, unsigned int len) {
+#endif
+    char *str = scalloc(len + 1);
+    strncpy(str, (const char*)val, len);
+    if (strcmp(last_key, "error") == 0)
+        last_reply.error = str;
+    else if (strcmp(last_key, "input") == 0)
+        last_reply.input = str;
+    else if (strcmp(last_key, "errorposition") == 0)
+        last_reply.errorposition = str;
+    else free(str);
+    return 1;
+}
+
+static int reply_start_map_cb(void *params) {
+    return 1;
+}
+
+static int reply_end_map_cb(void *params) {
+    if (!last_reply.success) {
+        fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
+        fprintf(stderr, "ERROR:               %s\n", last_reply.errorposition);
+        fprintf(stderr, "ERROR: %s\n", last_reply.error);
+    }
+    return 1;
+}
+
+
+#if YAJL_MAJOR >= 2
+static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
+#else
+static int reply_map_key_cb(void *params, const unsigned char *keyVal, unsigned keyLen) {
+#endif
+    free(last_key);
+    last_key = scalloc(keyLen + 1);
+    strncpy(last_key, (const char*)keyVal, keyLen);
+    return 1;
+}
+
+yajl_callbacks reply_callbacks = {
+    NULL,
+    &reply_boolean_cb,
+    NULL,
+    NULL,
+    NULL,
+    &reply_string_cb,
+    &reply_start_map_cb,
+    &reply_map_key_cb,
+    &reply_end_map_cb,
+    NULL,
+    NULL
+};
+
 int main(int argc, char *argv[]) {
     socket_path = getenv("I3SOCK");
     int o, option_index = 0;
@@ -165,6 +241,32 @@ int main(int argc, char *argv[]) {
     }
     if (reply_type != message_type)
         errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
+    /* For the reply of commands, have a look if that command was successful.
+     * If not, nicely format the error message. */
+    if (reply_type == I3_IPC_MESSAGE_TYPE_COMMAND) {
+        yajl_handle handle;
+#if YAJL_MAJOR < 2
+        yajl_parser_config parse_conf = { 0, 0 };
+
+        handle = yajl_alloc(&reply_callbacks, &parse_conf, NULL, NULL);
+#else
+        handle = yajl_alloc(&reply_callbacks, NULL, NULL);
+#endif
+        yajl_status state = yajl_parse(handle, (const unsigned char*)reply, reply_length);
+        switch (state) {
+            case yajl_status_ok:
+                break;
+            case yajl_status_client_canceled:
+#if YAJL_MAJOR < 2
+            case yajl_status_insufficient_data:
+#endif
+            case yajl_status_error:
+                errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
+        }
+
+        /* NB: We still fall-through and print the reply, because even if one
+         * command failed, that doesn’t mean that all commands failed. */
+    }
     printf("%.*s\n", reply_length, reply);
     free(reply);