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>
34 #include "blobmsg_json.h"
36 #define MAX_VARLEN 256
38 static struct avl_tree env_vars;
39 static struct blob_buf b = { 0 };
41 static const char *var_prefix = "";
42 static int var_prefix_len = 0;
44 static int add_json_element(const char *key, json_object *obj);
51 static int add_json_object(json_object *obj)
55 json_object_object_foreach(obj, key, val) {
56 ret = add_json_element(key, val);
63 static int add_json_array(struct array_list *a)
69 for (i = 0, len = array_list_length(a); i < len; i++) {
70 sprintf(seq, "%d", i);
71 ret = add_json_element(seq, array_list_get_idx(a, i));
79 static void add_json_string(const char *str)
81 char *ptr = (char *) str;
85 while ((c = strchr(ptr, '\'')) != NULL) {
88 fwrite(ptr, len, 1, stdout);
91 fwrite(c, strlen(c), 1, stdout);
95 fwrite(ptr, len, 1, stdout);
98 static void write_key_string(const char *key)
101 putc(isalnum(*key) ? *key : '_', stdout);
106 static int add_json_element(const char *key, json_object *obj)
110 switch (json_object_get_type(obj)) {
111 case json_type_object:
114 case json_type_array:
117 case json_type_string:
120 case json_type_boolean:
126 case json_type_double:
136 fprintf(stdout, "json_add_%s '", type);
137 write_key_string(key);
139 switch (json_object_get_type(obj)) {
140 case json_type_object:
141 fprintf(stdout, "';\n");
142 add_json_object(obj);
143 fprintf(stdout, "json_close_object;\n");
145 case json_type_array:
146 fprintf(stdout, "';\n");
147 add_json_array(json_object_get_array(obj));
148 fprintf(stdout, "json_close_array;\n");
150 case json_type_string:
151 fprintf(stdout, "' '");
152 add_json_string(json_object_get_string(obj));
153 fprintf(stdout, "';\n");
155 case json_type_boolean:
156 fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
159 fprintf(stdout, "' %jd;\n", json_object_get_int64(obj));
161 case json_type_double:
162 fprintf(stdout, "' %lf;\n", json_object_get_double(obj));
165 fprintf(stdout, "';\n");
174 static int jshn_parse(const char *str)
178 obj = json_tokener_parse(str);
179 if (!obj || json_object_get_type(obj) != json_type_object) {
180 fprintf(stderr, "Failed to parse message data\n");
183 fprintf(stdout, "json_init;\n");
184 add_json_object(obj);
190 static char *getenv_avl(const char *key)
192 struct env_var *var = avl_find_element(&env_vars, key, var, avl);
193 return var ? var->val : NULL;
196 static char *get_keys(const char *prefix)
200 keys = alloca(var_prefix_len + strlen(prefix) + sizeof("K_") + 1);
201 sprintf(keys, "%sK_%s", var_prefix, prefix);
202 return getenv_avl(keys);
205 static void get_var(const char *prefix, const char **name, char **var, char **type)
207 char *tmpname, *varname;
209 tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("T_"));
211 sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
212 *var = getenv_avl(tmpname);
214 sprintf(tmpname, "%sT_%s_%s", var_prefix, prefix, *name);
215 *type = getenv_avl(tmpname);
217 sprintf(tmpname, "%sN_%s_%s", var_prefix, prefix, *name);
218 varname = getenv_avl(tmpname);
223 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
225 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
230 get_var(prefix, &name, &var, &type);
234 if (!strcmp(type, "array")) {
235 new = json_object_new_array();
236 jshn_add_objects(new, var, true);
237 } else if (!strcmp(type, "object")) {
238 new = json_object_new_object();
239 jshn_add_objects(new, var, false);
240 } else if (!strcmp(type, "string")) {
241 new = json_object_new_string(var);
242 } else if (!strcmp(type, "int")) {
243 new = json_object_new_int64(atoll(var));
244 } else if (!strcmp(type, "double")) {
245 new = json_object_new_double(strtod(var, NULL));
246 } else if (!strcmp(type, "boolean")) {
247 new = json_object_new_boolean(!!atoi(var));
248 } else if (!strcmp(type, "null")) {
255 json_object_array_add(obj, new);
257 json_object_object_add(obj, name, new);
260 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
262 char *keys, *key, *brk;
264 keys = get_keys(prefix);
268 for (key = strtok_r(keys, " ", &brk); key;
269 key = strtok_r(NULL, " ", &brk)) {
270 jshn_add_object_var(obj, array, prefix, key);
277 static int jshn_format(bool no_newline, bool indent)
281 char *blobmsg_output = NULL;
284 if (!(obj = json_object_new_object()))
287 jshn_add_objects(obj, "J_V", false);
288 if (!(output = json_object_to_json_string(obj)))
292 blob_buf_init(&b, 0);
293 if (!blobmsg_add_json_from_string(&b, output))
295 if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0)))
297 output = blobmsg_output;
299 fprintf(stdout, "%s%s", output, no_newline ? "" : "\n");
300 free(blobmsg_output);
304 json_object_put(obj);
308 static int usage(const char *progname)
310 fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-R <file>|-w\n", progname);
314 static int avl_strcmp_var(const void *k1, const void *k2, void *ptr)
320 while (*s1 && *s1 == *s2) {
335 int main(int argc, char **argv)
337 extern char **environ;
338 bool no_newline = false;
340 struct env_var *vars;
348 avl_init(&env_vars, avl_strcmp_var, false, NULL);
349 for (i = 0; environ[i]; i++);
351 vars = calloc(i, sizeof(*vars));
353 fprintf(stderr, "%m\n");
356 for (i = 0; environ[i]; i++) {
359 vars[i].avl.key = environ[i];
360 c = strchr(environ[i], '=');
365 avl_insert(&env_vars, &vars[i].avl);
368 while ((ch = getopt(argc, argv, "p:nir:R:w")) != -1) {
372 var_prefix_len = strlen(var_prefix);
375 return jshn_parse(optarg);
377 if ((fd = open(optarg, O_RDONLY)) == -1) {
378 fprintf(stderr, "Error opening %s\n", optarg);
381 if (fstat(fd, &sb) == -1) {
382 fprintf(stderr, "Error getting size of %s\n", optarg);
386 if (!(fbuf = malloc(sb.st_size))) {
387 fprintf(stderr, "Error allocating memory for %s\n", optarg);
391 if (read(fd, fbuf, sb.st_size) != sb.st_size) {
392 fprintf(stderr, "Error reading %s\n", optarg);
397 ret = jshn_parse(fbuf);
402 return jshn_format(no_newline, indent);
410 return usage(argv[0]);
413 return usage(argv[0]);