Update copyright
[project/jsonpath.git] / main.c
1 /*
2 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
3 *
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.
7 *
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.
15 */
16
17 #include <stdio.h>
18 #include <stdbool.h>
19 #include <unistd.h>
20 #include <errno.h>
21
22 #ifdef JSONC
23 #include <json.h>
24 #else
25 #include <json-c/json.h>
26 #endif
27
28 #include <libubox/list.h>
29
30 #include "lexer.h"
31 #include "parser.h"
32 #include "matcher.h"
33
34
35 struct match_item {
36 struct json_object *jsobj;
37 struct list_head list;
38 };
39
40 static struct json_object *
41 parse_json(FILE *fd, const char *source, const char **error)
42 {
43 int len;
44 char buf[256];
45 struct json_object *obj = NULL;
46 struct json_tokener *tok = json_tokener_new();
47 enum json_tokener_error err = json_tokener_continue;
48
49 if (!tok)
50 return NULL;
51
52 if (source)
53 {
54 obj = json_tokener_parse_ex(tok, source, strlen(source));
55 err = json_tokener_get_error(tok);
56 }
57 else
58 {
59 while ((len = fread(buf, 1, sizeof(buf), fd)) > 0)
60 {
61 obj = json_tokener_parse_ex(tok, buf, len);
62 err = json_tokener_get_error(tok);
63
64 if (!err || err != json_tokener_continue)
65 break;
66 }
67 }
68
69 json_tokener_free(tok);
70
71 if (err)
72 {
73 if (err == json_tokener_continue)
74 err = json_tokener_error_parse_eof;
75
76 *error = json_tokener_error_desc(err);
77 return NULL;
78 }
79
80 return obj;
81 }
82
83 static void
84 print_string(const char *s)
85 {
86 const char *p;
87
88 printf("'");
89
90 for (p = s; *p; p++)
91 {
92 if (*p == '\'')
93 printf("'\"'\"'");
94 else
95 printf("%c", *p);
96 }
97
98 printf("'");
99 }
100
101 static void
102 export_value(struct list_head *matches, const char *prefix)
103 {
104 int n, len;
105 bool first = true;
106 struct match_item *item;
107
108 if (list_empty(matches))
109 return;
110
111 if (prefix)
112 {
113 printf("export %s=", prefix);
114
115 list_for_each_entry(item, matches, list)
116 {
117 switch (json_object_get_type(item->jsobj))
118 {
119 case json_type_object:
120 ; /* a label can only be part of a statement */
121 json_object_object_foreach(item->jsobj, key, val)
122 {
123 if (!val)
124 continue;
125
126 if (!first)
127 printf("\\ ");
128
129 print_string(key);
130 first = false;
131 }
132 break;
133
134 case json_type_array:
135 for (n = 0, len = json_object_array_length(item->jsobj);
136 n < len; n++)
137 {
138 if (!first)
139 printf("\\ ");
140
141 printf("%d", n);
142 first = false;
143 }
144 break;
145
146 case json_type_boolean:
147 if (!first)
148 printf("\\ ");
149 printf("%d", json_object_get_boolean(item->jsobj));
150 break;
151
152 case json_type_int:
153 if (!first)
154 printf("\\ ");
155 printf("%d", json_object_get_int(item->jsobj));
156 break;
157
158 case json_type_double:
159 if (!first)
160 printf("\\ ");
161 printf("%f", json_object_get_double(item->jsobj));
162 break;
163
164 case json_type_string:
165 if (!first)
166 printf("\\ ");
167 print_string(json_object_get_string(item->jsobj));
168 break;
169
170 case json_type_null:
171 break;
172 }
173
174 first = false;
175 }
176
177 printf("; ");
178 }
179 else
180 {
181 list_for_each_entry(item, matches, list)
182 printf("%s\n", json_object_to_json_string(item->jsobj));
183 }
184 }
185
186 static void
187 export_type(struct list_head *matches, const char *prefix)
188 {
189 bool first = true;
190 struct match_item *item;
191 const char *types[] = {
192 "null",
193 "boolean",
194 "double",
195 "int",
196 "object",
197 "array",
198 "string"
199 };
200
201 if (list_empty(matches))
202 return;
203
204 if (prefix)
205 printf("export %s=", prefix);
206
207 list_for_each_entry(item, matches, list)
208 {
209 if (!first)
210 printf("\\ ");
211
212 printf("%s", types[json_object_get_type(item->jsobj)]);
213 first = false;
214 }
215
216 if (prefix)
217 printf("; ");
218 else
219 printf("\n");
220 }
221
222 static void
223 match_cb(struct json_object *res, void *priv)
224 {
225 struct list_head *h = priv;
226 struct match_item *i = calloc(1, sizeof(*i));
227
228 if (i)
229 {
230 i->jsobj = res;
231 list_add_tail(&i->list, h);
232 }
233 }
234
235 static bool
236 filter_json(int opt, struct json_object *jsobj, char *expr)
237 {
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;
243
244 state = jp_parse(expr);
245
246 if (!state || state->error)
247 {
248 fprintf(stderr, "In expression '%s': %s\n",
249 expr, state ? state->error : "Out of memory");
250
251 goto out;
252 }
253
254 INIT_LIST_HEAD(&matches);
255
256 res = jp_match(state->path, jsobj, match_cb, &matches);
257 prefix = (state->path->type == T_LABEL) ? state->path->str : NULL;
258
259 switch (opt)
260 {
261 case 't':
262 export_type(&matches, prefix);
263 break;
264
265 default:
266 export_value(&matches, prefix);
267 break;
268 }
269
270 list_for_each_entry_safe(item, tmp, &matches, list)
271 free(item);
272
273 out:
274 if (state)
275 jp_free(state);
276
277 return !!res;
278 }
279
280 int main(int argc, char **argv)
281 {
282 int opt, rv = 0;
283 FILE *input = stdin;
284 struct json_object *jsobj = NULL;
285 const char *jserr = NULL, *source = NULL;
286
287 while ((opt = getopt(argc, argv, "i:s:e:t:q")) != -1)
288 {
289 switch (opt)
290 {
291 case 'i':
292 input = fopen(optarg, "r");
293
294 if (!input)
295 {
296 fprintf(stderr, "Failed to open %s: %s\n",
297 optarg, strerror(errno));
298
299 rv = 125;
300 goto out;
301 }
302
303 break;
304
305 case 's':
306 source = optarg;
307 break;
308
309 case 't':
310 case 'e':
311 if (!jsobj)
312 {
313 jsobj = parse_json(input, source, &jserr);
314
315 if (!jsobj)
316 {
317 fprintf(stderr, "Failed to parse json data: %s\n",
318 jserr);
319
320 rv = 126;
321 goto out;
322 }
323 }
324
325 if (!filter_json(opt, jsobj, optarg))
326 rv = 1;
327
328 break;
329
330 case 'q':
331 fclose(stderr);
332 break;
333 }
334 }
335
336 out:
337 if (jsobj)
338 json_object_put(jsobj);
339
340 if (input != stdin)
341 fclose(input);
342
343 return rv;
344 }