2 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include <json-c/json.h>
28 #include <libubox/list.h>
36 struct json_object *jsobj;
37 struct list_head list;
40 static struct json_object *
41 parse_json(FILE *fd, const char *source, const char **error)
45 struct json_object *obj = NULL;
46 struct json_tokener *tok = json_tokener_new();
47 enum json_tokener_error err = json_tokener_continue;
54 obj = json_tokener_parse_ex(tok, source, strlen(source));
55 err = json_tokener_get_error(tok);
59 while ((len = fread(buf, 1, sizeof(buf), fd)) > 0)
61 obj = json_tokener_parse_ex(tok, buf, len);
62 err = json_tokener_get_error(tok);
64 if (!err || err != json_tokener_continue)
69 json_tokener_free(tok);
73 if (err == json_tokener_continue)
74 err = json_tokener_error_parse_eof;
76 *error = json_tokener_error_desc(err);
84 print_string(const char *s)
102 print_separator(const char *sep, int *sc, int sl)
106 switch (sep[(*sc - 1) % sl])
121 printf("%c", sep[(*sc - 1) % sl]);
129 export_value(struct list_head *matches, const char *prefix, const char *sep,
133 int sc = 0, sl = strlen(sep);
134 struct match_item *item;
136 if (list_empty(matches))
141 printf("export %s=", prefix);
143 list_for_each_entry(item, matches, list)
148 switch (json_object_get_type(item->jsobj))
150 case json_type_object:
151 ; /* a label can only be part of a statement */
152 json_object_object_foreach(item->jsobj, key, val)
157 print_separator(sep, &sc, sl);
162 case json_type_array:
163 for (n = 0, len = json_object_array_length(item->jsobj);
166 print_separator(sep, &sc, sl);
171 case json_type_boolean:
172 print_separator(sep, &sc, sl);
173 printf("%d", json_object_get_boolean(item->jsobj));
177 print_separator(sep, &sc, sl);
178 printf("%d", json_object_get_int(item->jsobj));
181 case json_type_double:
182 print_separator(sep, &sc, sl);
183 printf("%f", json_object_get_double(item->jsobj));
186 case json_type_string:
187 print_separator(sep, &sc, sl);
188 print_string(json_object_get_string(item->jsobj));
200 list_for_each_entry(item, matches, list)
205 switch (json_object_get_type(item->jsobj))
207 case json_type_object:
208 case json_type_array:
209 case json_type_boolean:
211 case json_type_double:
212 printf("%s\n", json_object_to_json_string(item->jsobj));
215 case json_type_string:
216 printf("%s\n", json_object_get_string(item->jsobj));
227 export_type(struct list_head *matches, const char *prefix, int limit)
230 struct match_item *item;
231 const char *types[] = {
241 if (list_empty(matches))
245 printf("export %s=", prefix);
247 list_for_each_entry(item, matches, list)
255 printf("%s", types[json_object_get_type(item->jsobj)]);
266 match_cb(struct json_object *res, void *priv)
268 struct list_head *h = priv;
269 struct match_item *i = calloc(1, sizeof(*i));
274 list_add_tail(&i->list, h);
279 print_error(struct jp_state *state, char *expr)
284 fprintf(stderr, "Syntax error: ");
286 switch (state->error_code)
289 fprintf(stderr, "Unexpected character\n");
293 fprintf(stderr, "String or label literal too long\n");
297 fprintf(stderr, "Invalid escape sequence\n");
301 fprintf(stderr, "Unterminated string\n");
305 for (i = 0; i < sizeof(state->error_code) * 8; i++)
307 if (state->error_code & (1 << i))
310 first ? "Expecting %s" : " or %s", tokennames[i]);
316 fprintf(stderr, "\n");
320 fprintf(stderr, "In expression %s\n", expr);
321 fprintf(stderr, "Near here ----");
323 for (i = 0; i < state->error_pos; i++)
324 fprintf(stderr, "-");
326 fprintf(stderr, "^\n");
330 filter_json(int opt, struct json_object *jsobj, char *expr, const char *sep,
333 struct jp_state *state;
334 const char *prefix = NULL;
335 struct list_head matches;
336 struct match_item *item, *tmp;
337 struct json_object *res = NULL;
339 state = jp_parse(expr);
343 fprintf(stderr, "Out of memory\n");
346 else if (state->error_code)
348 print_error(state, expr);
352 INIT_LIST_HEAD(&matches);
354 res = jp_match(state->path, jsobj, match_cb, &matches);
355 prefix = (state->path->type == T_LABEL) ? state->path->str : NULL;
360 export_type(&matches, prefix, limit);
364 export_value(&matches, prefix, sep, limit);
368 list_for_each_entry_safe(item, tmp, &matches, list)
378 int main(int argc, char **argv)
380 int opt, rv = 0, limit = 0x7FFFFFFF;
382 struct json_object *jsobj = NULL;
383 const char *jserr = NULL, *source = NULL, *separator = " ";
385 while ((opt = getopt(argc, argv, "i:s:e:t:F:l:q")) != -1)
390 input = fopen(optarg, "r");
394 fprintf(stderr, "Failed to open %s: %s\n",
395 optarg, strerror(errno));
408 if (optarg && *optarg)
413 limit = atoi(optarg);
420 jsobj = parse_json(input, source, &jserr);
424 fprintf(stderr, "Failed to parse json data: %s\n",
432 if (!filter_json(opt, jsobj, optarg, separator, limit))
445 json_object_put(jsobj);
447 if (input && input != stdin)