2 * Copyright (C) 2013 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 export_value(struct list_head *matches, const char *prefix)
106 struct match_item *item;
108 if (list_empty(matches))
113 printf("export %s=", prefix);
115 list_for_each_entry(item, matches, list)
117 switch (json_object_get_type(item->jsobj))
119 case json_type_object:
120 ; /* a label can only be part of a statement */
121 json_object_object_foreach(item->jsobj, key, val)
134 case json_type_array:
135 for (n = 0, len = json_object_array_length(item->jsobj);
146 case json_type_boolean:
149 printf("%d", json_object_get_boolean(item->jsobj));
155 printf("%d", json_object_get_int(item->jsobj));
158 case json_type_double:
161 printf("%f", json_object_get_double(item->jsobj));
164 case json_type_string:
167 print_string(json_object_get_string(item->jsobj));
181 list_for_each_entry(item, matches, list)
182 printf("%s\n", json_object_to_json_string(item->jsobj));
187 export_type(struct list_head *matches, const char *prefix)
190 struct match_item *item;
191 const char *types[] = {
201 if (list_empty(matches))
205 printf("export %s=", prefix);
207 list_for_each_entry(item, matches, list)
212 printf("%s", types[json_object_get_type(item->jsobj)]);
223 match_cb(struct json_object *res, void *priv)
225 struct list_head *h = priv;
226 struct match_item *i = calloc(1, sizeof(*i));
231 list_add_tail(&i->list, h);
236 filter_json(int opt, struct json_object *jsobj, char *expr)
238 struct jp_state *state;
239 const char *prefix = NULL;
240 struct list_head matches;
241 struct match_item *item, *tmp;
242 struct json_object *res = NULL;
244 state = jp_parse(expr);
246 if (!state || state->error)
248 fprintf(stderr, "In expression '%s': %s\n",
249 expr, state ? state->error : "Out of memory");
254 INIT_LIST_HEAD(&matches);
256 res = jp_match(state->path, jsobj, match_cb, &matches);
257 prefix = (state->path->type == T_LABEL) ? state->path->str : NULL;
262 export_type(&matches, prefix);
266 export_value(&matches, prefix);
270 list_for_each_entry_safe(item, tmp, &matches, list)
280 int main(int argc, char **argv)
284 struct json_object *jsobj = NULL;
285 const char *jserr = NULL, *source = NULL;
287 while ((opt = getopt(argc, argv, "i:s:e:t:q")) != -1)
292 input = fopen(optarg, "r");
296 fprintf(stderr, "Failed to open %s: %s\n",
297 optarg, strerror(errno));
313 jsobj = parse_json(input, source, &jserr);
317 fprintf(stderr, "Failed to parse json data: %s\n",
325 if (!filter_json(opt, jsobj, optarg))
338 json_object_put(jsobj);