i3-msg: parse command replies and display errors nicely if there were errors

fixes #737
next
Michael Stapelberg 2013-01-26 17:56:43 +01:00
parent 62b0df0640
commit c300d216ec
2 changed files with 103 additions and 1 deletions

View 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)

View 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 doesnt mean that all commands failed. */
}
printf("%.*s\n", reply_length, reply);
free(reply);