2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
7 * parse_json_header.c: Parse the JSON protocol header to determine
8 * protocol version and features.
13 #include <sys/types.h>
24 #include <yajl/yajl_common.h>
25 #include <yajl/yajl_parse.h>
26 #include <yajl/yajl_version.h>
38 static int header_integer(void *ctx, long long val) {
40 static int header_integer(void *ctx, long val) {
42 i3bar_child *child = ctx;
44 switch (current_key) {
49 child->stop_signal = val;
52 child->cont_signal = val;
60 #define CHECK_KEY(name) (stringlen == strlen(name) && \
61 STARTS_WITH((const char*)stringval, stringlen, name))
64 static int header_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) {
66 static int header_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) {
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;
78 static yajl_callbacks version_callbacks = {
88 NULL, /* start_array */
92 static void child_init(i3bar_child *child) {
94 child->stop_signal = SIGSTOP;
95 child->cont_signal = SIGCONT;
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).
106 void parse_json_header(i3bar_child *child, const unsigned char *buffer, int length, unsigned int *consumed) {
109 current_key = NO_KEY;
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);
117 yajl_parser_config parse_conf = { 0, 0 };
119 yajl_handle handle = yajl_alloc(&version_callbacks, &parse_conf, NULL, child);
122 yajl_status state = yajl_parse(handle, buffer, length);
123 if (state != yajl_status_ok) {
125 if (consumed != NULL)
128 if (consumed != NULL)
129 *consumed = yajl_get_bytes_consumed(handle);