- if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0)
- errx(EXIT_FAILURE, "invalid magic in reply");
-
- walk += strlen(I3_IPC_MAGIC);
- *reply_length = *((uint32_t*)walk);
- walk += sizeof(uint32_t);
- if (*((uint32_t*)walk) != message_type)
- errx(EXIT_FAILURE, "unexpected reply type (got %d, expected %d)", *((uint32_t*)walk), message_type);
- walk += sizeof(uint32_t);
-
- *reply = malloc(*reply_length);
- if ((*reply) == NULL)
- err(EXIT_FAILURE, "malloc() failed");
-
- to_read = *reply_length;
- read_bytes = 0;
- while (read_bytes < to_read) {
- int n = read(sockfd, *reply + read_bytes, to_read);
- if (n == -1)
- err(EXIT_FAILURE, "read() failed");
-
- read_bytes += n;
- to_read -= n;
+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);