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>
32 static struct json_object
*
33 parse_json(FILE *fd
, const char **error
)
37 struct json_object
*obj
= NULL
;
38 struct json_tokener
*tok
= json_tokener_new();
39 enum json_tokener_error err
= json_tokener_continue
;
44 while ((len
= fread(buf
, 1, sizeof(buf
), fd
)) > 0)
46 obj
= json_tokener_parse_ex(tok
, buf
, len
);
47 err
= json_tokener_get_error(tok
);
49 if (!err
|| err
!= json_tokener_continue
)
53 json_tokener_free(tok
);
57 if (err
== json_tokener_continue
)
58 err
= json_tokener_error_parse_eof
;
60 *error
= json_tokener_error_desc(err
);
68 print_string(const char *s
)
86 export_value(struct json_object
*jsobj
, const char *prefix
)
93 switch (json_object_get_type(jsobj
))
95 case json_type_object
:
96 printf("export %s=", prefix
);
97 json_object_object_foreach(jsobj
, key
, val
)
111 case json_type_array
:
112 printf("export %s=", prefix
);
113 for (n
= 0, len
= json_object_array_length(jsobj
); n
< len
; n
++)
124 case json_type_boolean
:
125 printf("export %s=%d; ", prefix
, json_object_get_boolean(jsobj
));
129 printf("export %s=%d; ", prefix
, json_object_get_int(jsobj
));
132 case json_type_double
:
133 printf("export %s=%f; ", prefix
, json_object_get_double(jsobj
));
136 case json_type_string
:
137 printf("export %s=", prefix
);
138 print_string(json_object_get_string(jsobj
));
148 printf("%s\n", json_object_to_json_string(jsobj
));
153 export_type(struct json_object
*jsobj
, const char *prefix
)
155 const char *types
[] = {
166 printf("export %s=%s; ", prefix
, types
[json_object_get_type(jsobj
)]);
168 printf("%s\n", types
[json_object_get_type(jsobj
)]);
172 filter_json(int opt
, struct json_object
*jsobj
, char *expr
)
174 struct jp_state
*state
;
175 struct json_object
*res
= NULL
;
176 const char *prefix
= NULL
;
178 state
= jp_parse(expr
);
180 if (!state
|| state
->error
)
182 fprintf(stderr
, "In expression '%s': %s\n",
183 expr
, state
? state
->error
: "Out of memory");
188 res
= jp_match(state
->path
, jsobj
);
192 prefix
= (state
->path
->type
== T_LABEL
) ? state
->path
->str
: NULL
;
197 export_type(res
, prefix
);
201 export_value(res
, prefix
);
213 int main(int argc
, char **argv
)
217 struct json_object
*jsobj
= NULL
;
218 const char *jserr
= NULL
;
220 while ((opt
= getopt(argc
, argv
, "i:e:t:q")) != -1)
225 input
= fopen(optarg
, "r");
229 fprintf(stderr
, "Failed to open %s: %s\n",
230 optarg
, strerror(errno
));
242 jsobj
= parse_json(input
, &jserr
);
246 fprintf(stderr
, "Failed to parse json data: %s\n",
254 if (!filter_json(opt
, jsobj
, optarg
))
267 json_object_put(jsobj
);