]> git.sur5r.net Git - i3/i3/blob - i3bar/src/parse_json_header.c
Merge branch 'master' into next
[i3/i3] / i3bar / src / parse_json_header.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3bar - an xcb-based status- and ws-bar for i3
5  * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
6  *
7  * parse_json_header.c: Parse the JSON protocol header to determine
8  *                      protocol version and features.
9  *
10  */
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <signal.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <err.h>
21 #include <ev.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <yajl/yajl_common.h>
25 #include <yajl/yajl_parse.h>
26 #include <yajl/yajl_version.h>
27
28 #include "common.h"
29
30 static enum {
31     KEY_VERSION,
32     KEY_STOP_SIGNAL,
33     KEY_CONT_SIGNAL,
34     NO_KEY
35 } current_key;
36
37 #if YAJL_MAJOR >= 2
38 static int header_integer(void *ctx, long long val) {
39 #else
40 static int header_integer(void *ctx, long val) {
41 #endif
42     i3bar_child *child = ctx;
43
44     switch (current_key) {
45         case KEY_VERSION:
46             child->version = val;
47             break;
48         case KEY_STOP_SIGNAL:
49             child->stop_signal = val;
50             break;
51         case KEY_CONT_SIGNAL:
52             child->cont_signal = val;
53             break;
54         default:
55             break;
56     }
57     return 1;
58 }
59
60 #define CHECK_KEY(name) (stringlen == strlen(name) && \
61                          STARTS_WITH((const char*)stringval, stringlen, name))
62
63 #if YAJL_MAJOR >= 2
64 static int header_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
65 #else
66 static int header_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) {
67 #endif
68     if (CHECK_KEY("version")) {
69         current_key = KEY_VERSION;
70     } else if (CHECK_KEY("stop_signal")) {
71         current_key = KEY_STOP_SIGNAL;
72     } else if (CHECK_KEY("cont_signal")) {
73         current_key = KEY_CONT_SIGNAL;
74     }
75     return 1;
76 }
77
78 static yajl_callbacks version_callbacks = {
79     NULL, /* null */
80     NULL, /* boolean */
81     &header_integer,
82     NULL, /* double */
83     NULL, /* number */
84     NULL, /* string */
85     NULL, /* start_map */
86     &header_map_key,
87     NULL, /* end_map */
88     NULL, /* start_array */
89     NULL /* end_array */
90 };
91
92 static void child_init(i3bar_child *child) {
93     child->version = 0;
94     child->stop_signal = SIGSTOP;
95     child->cont_signal = SIGCONT;
96 }
97
98 /*
99  * Parse the JSON protocol header to determine protocol version and features.
100  * In case the buffer does not contain a valid header (invalid JSON, or no
101  * version field found), the 'correct' field of the returned header is set to
102  * false. The amount of bytes consumed by parsing the header is returned in
103  * *consumed (if non-NULL).
104  *
105  */
106 void parse_json_header(i3bar_child *child, const unsigned char *buffer, int length, unsigned int *consumed) {
107     child_init(child);
108
109     current_key = NO_KEY;
110
111 #if YAJL_MAJOR >= 2
112     yajl_handle handle = yajl_alloc(&version_callbacks, NULL, child);
113     /* Allow trailing garbage. yajl 1 always behaves that way anyways, but for
114      * yajl 2, we need to be explicit. */
115     yajl_config(handle, yajl_allow_trailing_garbage, 1);
116 #else
117     yajl_parser_config parse_conf = { 0, 0 };
118
119     yajl_handle handle = yajl_alloc(&version_callbacks, &parse_conf, NULL, child);
120 #endif
121
122     yajl_status state = yajl_parse(handle, buffer, length);
123     if (state != yajl_status_ok) {
124         child_init(child);
125         if (consumed != NULL)
126             *consumed = 0;
127     } else {
128         if (consumed != NULL)
129             *consumed = yajl_get_bytes_consumed(handle);
130     }
131
132     yajl_free(handle);
133 }