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>
30 #define MAX_VARLEN 256
32 static const char *var_prefix = "";
33 static int var_prefix_len = 0;
35 static int add_json_element(const char *key, json_object *obj);
37 static int add_json_object(json_object *obj)
41 json_object_object_foreach(obj, key, val) {
42 ret = add_json_element(key, val);
49 static int add_json_array(struct array_list *a)
55 for (i = 0, len = array_list_length(a); i < len; i++) {
56 sprintf(seq, "%d", i);
57 ret = add_json_element(seq, array_list_get_idx(a, i));
65 static void add_json_string(const char *str)
67 char *ptr = (char *) str;
71 while ((c = strchr(ptr, '\'')) != NULL) {
74 fwrite(ptr, len, 1, stdout);
77 fwrite(c, strlen(c), 1, stdout);
81 fwrite(ptr, len, 1, stdout);
84 static void write_key_string(const char *key)
87 putc(isalnum(*key) ? *key : '_', stdout);
92 static int add_json_element(const char *key, json_object *obj)
99 switch (json_object_get_type(obj)) {
100 case json_type_object:
103 case json_type_array:
106 case json_type_string:
109 case json_type_boolean:
119 fprintf(stdout, "json_add_%s '", type);
120 write_key_string(key);
122 switch (json_object_get_type(obj)) {
123 case json_type_object:
124 fprintf(stdout, "';\n");
125 add_json_object(obj);
126 fprintf(stdout, "json_close_object;\n");
128 case json_type_array:
129 fprintf(stdout, "';\n");
130 add_json_array(json_object_get_array(obj));
131 fprintf(stdout, "json_close_array;\n");
133 case json_type_string:
134 fprintf(stdout, "' '");
135 add_json_string(json_object_get_string(obj));
136 fprintf(stdout, "';\n");
138 case json_type_boolean:
139 fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
142 fprintf(stdout, "' %d;\n", json_object_get_int(obj));
151 static int jshn_parse(const char *str)
155 obj = json_tokener_parse(str);
156 if (is_error(obj) || json_object_get_type(obj) != json_type_object) {
157 fprintf(stderr, "Failed to parse message data\n");
160 fprintf(stdout, "json_init;\n");
161 add_json_object(obj);
167 static char *get_keys(const char *prefix)
171 keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1);
172 sprintf(keys, "%sKEYS_%s", var_prefix, prefix);
176 static void get_var(const char *prefix, const char **name, char **var, char **type)
178 char *tmpname, *varname;
180 tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_"));
182 sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
183 *var = getenv(tmpname);
185 sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name);
186 *type = getenv(tmpname);
188 sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name);
189 varname = getenv(tmpname);
194 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
196 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
201 get_var(prefix, &name, &var, &type);
205 if (!strcmp(type, "array")) {
206 new = json_object_new_array();
207 jshn_add_objects(new, var, true);
208 } else if (!strcmp(type, "object")) {
209 new = json_object_new_object();
210 jshn_add_objects(new, var, false);
211 } else if (!strcmp(type, "string")) {
212 new = json_object_new_string(var);
213 } else if (!strcmp(type, "int")) {
214 new = json_object_new_int(atoi(var));
215 } else if (!strcmp(type, "boolean")) {
216 new = json_object_new_boolean(!!atoi(var));
222 json_object_array_add(obj, new);
224 json_object_object_add(obj, name, new);
227 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
229 char *keys, *key, *brk;
231 keys = get_keys(prefix);
235 for (key = strtok_r(keys, " ", &brk); key;
236 key = strtok_r(NULL, " ", &brk)) {
237 jshn_add_object_var(obj, array, prefix, key);
244 static int jshn_format(bool no_newline)
248 obj = json_object_new_object();
249 jshn_add_objects(obj, "JSON_VAR", false);
250 fprintf(stdout, "%s%s", json_object_to_json_string(obj),
251 no_newline ? "" : "\n");
252 json_object_put(obj);
256 static int usage(const char *progname)
258 fprintf(stderr, "Usage: %s [-n] -r <message>|-w\n", progname);
262 int main(int argc, char **argv)
264 bool no_newline = false;
267 while ((ch = getopt(argc, argv, "p:nr:w")) != -1) {
271 var_prefix_len = strlen(var_prefix);
274 return jshn_parse(optarg);
276 return jshn_format(no_newline);
281 return usage(argv[0]);
284 return usage(argv[0]);