| 1 |
/* |
| 2 |
* vim:ts=4:sw=4:expandtab |
| 3 |
* |
| 4 |
* i3bar - an xcb-based status- and ws-bar for i3 |
| 5 |
* © 2010 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 "common.h" |
| 12 |
|
| 13 |
#include <signal.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <yajl/yajl_parse.h> |
| 17 |
|
| 18 |
static enum { |
| 19 |
KEY_VERSION, |
| 20 |
KEY_STOP_SIGNAL, |
| 21 |
KEY_CONT_SIGNAL, |
| 22 |
KEY_CLICK_EVENTS, |
| 23 |
NO_KEY |
| 24 |
} current_key; |
| 25 |
|
| 26 |
static int header_integer(void *ctx, long long val) { |
| 27 |
i3bar_child *child = ctx; |
| 28 |
|
| 29 |
switch (current_key) { |
| 30 |
case KEY_VERSION: |
| 31 |
child->version = val; |
| 32 |
break; |
| 33 |
case KEY_STOP_SIGNAL: |
| 34 |
child->stop_signal = val; |
| 35 |
break; |
| 36 |
case KEY_CONT_SIGNAL: |
| 37 |
child->cont_signal = val; |
| 38 |
break; |
| 39 |
default: |
| 40 |
break; |
| 41 |
} |
| 42 |
|
| 43 |
return 1; |
| 44 |
} |
| 45 |
|
| 46 |
static int header_boolean(void *ctx, int val) { |
| 47 |
i3bar_child *child = ctx; |
| 48 |
|
| 49 |
switch (current_key) { |
| 50 |
case KEY_CLICK_EVENTS: |
| 51 |
child->click_events = val; |
| 52 |
break; |
| 53 |
default: |
| 54 |
break; |
| 55 |
} |
| 56 |
|
| 57 |
return 1; |
| 58 |
} |
| 59 |
|
| 60 |
#define CHECK_KEY(name) (stringlen == strlen(name) && \ |
| 61 |
STARTS_WITH((const char *)stringval, stringlen, name)) |
| 62 |
|
| 63 |
static int header_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) { |
| 64 |
if (CHECK_KEY("version")) { |
| 65 |
current_key = KEY_VERSION; |
| 66 |
} else if (CHECK_KEY("stop_signal")) { |
| 67 |
current_key = KEY_STOP_SIGNAL; |
| 68 |
} else if (CHECK_KEY("cont_signal")) { |
| 69 |
current_key = KEY_CONT_SIGNAL; |
| 70 |
} else if (CHECK_KEY("click_events")) { |
| 71 |
current_key = KEY_CLICK_EVENTS; |
| 72 |
} |
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
static void child_init(i3bar_child *child) { |
| 77 |
child->version = 0; |
| 78 |
child->stop_signal = SIGSTOP; |
| 79 |
child->cont_signal = SIGCONT; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* Parse the JSON protocol header to determine protocol version and features. |
| 84 |
* In case the buffer does not contain a valid header (invalid JSON, or no |
| 85 |
* version field found), the 'correct' field of the returned header is set to |
| 86 |
* false. The amount of bytes consumed by parsing the header is returned in |
| 87 |
* *consumed (if non-NULL). |
| 88 |
* |
| 89 |
*/ |
| 90 |
void parse_json_header(i3bar_child *child, const unsigned char *buffer, int length, unsigned int *consumed) { |
| 91 |
static yajl_callbacks version_callbacks = { |
| 92 |
.yajl_boolean = header_boolean, |
| 93 |
.yajl_integer = header_integer, |
| 94 |
.yajl_map_key = &header_map_key, |
| 95 |
}; |
| 96 |
|
| 97 |
child_init(child); |
| 98 |
|
| 99 |
current_key = NO_KEY; |
| 100 |
|
| 101 |
yajl_handle handle = yajl_alloc(&version_callbacks, NULL, child); |
| 102 |
/* Allow trailing garbage. yajl 1 always behaves that way anyways, but for |
| 103 |
* yajl 2, we need to be explicit. */ |
| 104 |
yajl_config(handle, yajl_allow_trailing_garbage, 1); |
| 105 |
|
| 106 |
yajl_status state = yajl_parse(handle, buffer, length); |
| 107 |
if (state != yajl_status_ok) { |
| 108 |
child_init(child); |
| 109 |
if (consumed != NULL) { |
| 110 |
*consumed = 0; |
| 111 |
} |
| 112 |
} else { |
| 113 |
if (consumed != NULL) { |
| 114 |
*consumed = yajl_get_bytes_consumed(handle); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
yajl_free(handle); |
| 119 |
} |