2 * Copyright (C) 2011-2013 Felix Fietkau <nbd@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.
19 #include <json/json.h>
35 #include "blobmsg_json.h"
37 #define MAX_VARLEN 256
39 static struct avl_tree env_vars;
40 static struct blob_buf b = { 0 };
42 static const char *var_prefix = "";
43 static int var_prefix_len = 0;
45 static int add_json_element(const char *key, json_object *obj);
52 static int add_json_object(json_object *obj)
56 json_object_object_foreach(obj, key, val) {
57 ret = add_json_element(key, val);
64 static int add_json_array(struct array_list *a)
70 for (i = 0, len = array_list_length(a); i < len; i++) {
71 snprintf(seq, sizeof(seq), "%d", i);
72 ret = add_json_element(seq, array_list_get_idx(a, i));
80 static void add_json_string(const char *str)
82 char *ptr = (char *) str;
86 while ((c = strchr(ptr, '\'')) != NULL) {
89 fwrite(ptr, len, 1, stdout);
92 fwrite(c, strlen(c), 1, stdout);
96 fwrite(ptr, len, 1, stdout);
99 static void write_key_string(const char *key)
102 putc(isalnum(*key) ? *key : '_', stdout);
107 static int add_json_element(const char *key, json_object *obj)
111 switch (json_object_get_type(obj)) {
112 case json_type_object:
115 case json_type_array:
118 case json_type_string:
121 case json_type_boolean:
127 case json_type_double:
137 fprintf(stdout, "json_add_%s '", type);
138 write_key_string(key);
140 switch (json_object_get_type(obj)) {
141 case json_type_object:
142 fprintf(stdout, "';\n");
143 add_json_object(obj);
144 fprintf(stdout, "json_close_object;\n");
146 case json_type_array:
147 fprintf(stdout, "';\n");
148 add_json_array(json_object_get_array(obj));
149 fprintf(stdout, "json_close_array;\n");
151 case json_type_string:
152 fprintf(stdout, "' '");
153 add_json_string(json_object_get_string(obj));
154 fprintf(stdout, "';\n");
156 case json_type_boolean:
157 fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
160 fprintf(stdout, "' %"PRId64";\n", json_object_get_int64(obj));
162 case json_type_double:
163 fprintf(stdout, "' %lf;\n", json_object_get_double(obj));
166 fprintf(stdout, "';\n");
175 static int jshn_parse(const char *str)
179 obj = json_tokener_parse(str);
180 if (!obj || json_object_get_type(obj) != json_type_object) {
182 json_object_put(obj);
183 fprintf(stderr, "Failed to parse message data\n");
186 fprintf(stdout, "json_init;\n");
187 add_json_object(obj);
189 json_object_put(obj);
194 static char *getenv_avl(const char *key)
196 struct env_var *var = avl_find_element(&env_vars, key, var, avl);
197 return var ? var->val : NULL;
200 static char *get_keys(const char *prefix)
203 size_t len = var_prefix_len + strlen(prefix) + sizeof("K_") + 1;
206 snprintf(keys, len, "%sK_%s", var_prefix, prefix);
207 return getenv_avl(keys);
210 static void get_var(const char *prefix, const char **name, char **var, char **type)
212 char *tmpname, *varname;
213 size_t len = var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("T_");
215 tmpname = alloca(len);
217 snprintf(tmpname, len, "%s%s_%s", var_prefix, prefix, *name);
218 *var = getenv_avl(tmpname);
220 snprintf(tmpname, len, "%sT_%s_%s", var_prefix, prefix, *name);
221 *type = getenv_avl(tmpname);
223 snprintf(tmpname, len, "%sN_%s_%s", var_prefix, prefix, *name);
224 varname = getenv_avl(tmpname);
229 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
231 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
236 get_var(prefix, &name, &var, &type);
240 if (!strcmp(type, "array")) {
241 new = json_object_new_array();
242 jshn_add_objects(new, var, true);
243 } else if (!strcmp(type, "object")) {
244 new = json_object_new_object();
245 jshn_add_objects(new, var, false);
246 } else if (!strcmp(type, "string")) {
247 new = json_object_new_string(var);
248 } else if (!strcmp(type, "int")) {
249 new = json_object_new_int64(atoll(var));
250 } else if (!strcmp(type, "double")) {
251 new = json_object_new_double(strtod(var, NULL));
252 } else if (!strcmp(type, "boolean")) {
253 new = json_object_new_boolean(!!atoi(var));
254 } else if (!strcmp(type, "null")) {
261 json_object_array_add(obj, new);
263 json_object_object_add(obj, name, new);
266 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
268 char *keys, *key, *brk;
270 keys = get_keys(prefix);
274 for (key = strtok_r(keys, " ", &brk); key;
275 key = strtok_r(NULL, " ", &brk)) {
276 jshn_add_object_var(obj, array, prefix, key);
283 static int jshn_format(bool no_newline, bool indent, FILE *stream)
287 char *blobmsg_output = NULL;
290 if (!(obj = json_object_new_object()))
293 jshn_add_objects(obj, "J_V", false);
294 if (!(output = json_object_to_json_string(obj)))
298 blob_buf_init(&b, 0);
299 if (!blobmsg_add_json_from_string(&b, output))
301 if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0)))
303 output = blobmsg_output;
305 fprintf(stream, "%s%s", output, no_newline ? "" : "\n");
306 free(blobmsg_output);
310 json_object_put(obj);
314 static int usage(const char *progname)
316 fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-R <file>|-o <file>|-p <prefix>|-w\n", progname);
320 static int avl_strcmp_var(const void *k1, const void *k2, void *ptr)
326 while (*s1 && *s1 == *s2) {
341 static int jshn_parse_file(const char *path)
348 if ((fd = open(path, O_RDONLY)) == -1) {
349 fprintf(stderr, "Error opening %s\n", path);
353 if (fstat(fd, &sb) == -1) {
354 fprintf(stderr, "Error getting size of %s\n", path);
359 if (!(fbuf = calloc(1, sb.st_size+1))) {
360 fprintf(stderr, "Error allocating memory for %s\n", path);
365 if (read(fd, fbuf, sb.st_size) != sb.st_size) {
366 fprintf(stderr, "Error reading %s\n", path);
372 ret = jshn_parse(fbuf);
379 static int jshn_format_file(const char *path, bool no_newline, bool indent)
384 fp = fopen(path, "w");
386 fprintf(stderr, "Error opening %s\n", path);
390 ret = jshn_format(no_newline, indent, fp);
396 int main(int argc, char **argv)
398 extern char **environ;
399 bool no_newline = false;
401 struct env_var *vars;
406 avl_init(&env_vars, avl_strcmp_var, false, NULL);
407 for (i = 0; environ[i]; i++);
409 vars = calloc(i, sizeof(*vars));
411 fprintf(stderr, "%m\n");
414 for (i = 0; environ[i]; i++) {
417 vars[i].avl.key = environ[i];
418 c = strchr(environ[i], '=');
423 avl_insert(&env_vars, &vars[i].avl);
426 while ((ch = getopt(argc, argv, "p:nir:R:o:w")) != -1) {
430 var_prefix_len = strlen(var_prefix);
433 ret = jshn_parse(optarg);
436 ret = jshn_parse_file(optarg);
439 ret = jshn_format(no_newline, indent, stdout);
442 ret = jshn_format_file(optarg, no_newline, indent);
452 return usage(argv[0]);
457 return usage(argv[0]);