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.
16 #include <json/json.h>
25 #define MAX_VARLEN 256
27 static const char *var_prefix
= "";
28 static int var_prefix_len
= 0;
30 static int add_json_element(const char *key
, json_object
*obj
);
32 static int add_json_object(json_object
*obj
)
36 json_object_object_foreach(obj
, key
, val
) {
37 ret
= add_json_element(key
, val
);
44 static int add_json_array(struct array_list
*a
)
50 for (i
= 0, len
= array_list_length(a
); i
< len
; i
++) {
51 sprintf(seq
, "%d", i
);
52 ret
= add_json_element(seq
, array_list_get_idx(a
, i
));
60 static void add_json_string(const char *str
)
62 char *ptr
= (char *) str
;
66 while ((c
= strchr(ptr
, '\'')) != NULL
) {
69 fwrite(ptr
, len
, 1, stdout
);
72 fwrite(c
, strlen(c
), 1, stdout
);
76 fwrite(ptr
, len
, 1, stdout
);
79 static void write_key_string(const char *key
)
82 putc(isalnum(*key
) ? *key
: '_', stdout
);
87 static int add_json_element(const char *key
, json_object
*obj
)
94 switch (json_object_get_type(obj
)) {
95 case json_type_object
:
101 case json_type_string
:
104 case json_type_boolean
:
114 fprintf(stdout
, "json_add_%s '", type
);
115 write_key_string(key
);
117 switch (json_object_get_type(obj
)) {
118 case json_type_object
:
119 fprintf(stdout
, "';\n");
120 add_json_object(obj
);
121 fprintf(stdout
, "json_close_object;\n");
123 case json_type_array
:
124 fprintf(stdout
, "';\n");
125 add_json_array(json_object_get_array(obj
));
126 fprintf(stdout
, "json_close_array;\n");
128 case json_type_string
:
129 fprintf(stdout
, "' '");
130 add_json_string(json_object_get_string(obj
));
131 fprintf(stdout
, "';\n");
133 case json_type_boolean
:
134 fprintf(stdout
, "' %d;\n", json_object_get_boolean(obj
));
137 fprintf(stdout
, "' %d;\n", json_object_get_int(obj
));
146 static int jshn_parse(const char *str
)
150 obj
= json_tokener_parse(str
);
151 if (is_error(obj
) || json_object_get_type(obj
) != json_type_object
) {
152 fprintf(stderr
, "Failed to parse message data\n");
155 fprintf(stdout
, "json_init;\n");
156 add_json_object(obj
);
162 static char *get_keys(const char *prefix
)
166 keys
= alloca(var_prefix_len
+ strlen(prefix
) + sizeof("KEYS_") + 1);
167 sprintf(keys
, "%sKEYS_%s", var_prefix
, prefix
);
171 static void get_var(const char *prefix
, const char **name
, char **var
, char **type
)
173 char *tmpname
, *varname
;
175 tmpname
= alloca(var_prefix_len
+ strlen(prefix
) + 1 + strlen(*name
) + 1 + sizeof("TYPE_"));
177 sprintf(tmpname
, "%s%s_%s", var_prefix
, prefix
, *name
);
178 *var
= getenv(tmpname
);
180 sprintf(tmpname
, "%sTYPE_%s_%s", var_prefix
, prefix
, *name
);
181 *type
= getenv(tmpname
);
183 sprintf(tmpname
, "%sNAME_%s_%s", var_prefix
, prefix
, *name
);
184 varname
= getenv(tmpname
);
189 static json_object
*jshn_add_objects(json_object
*obj
, const char *prefix
, bool array
);
191 static void jshn_add_object_var(json_object
*obj
, bool array
, const char *prefix
, const char *name
)
196 get_var(prefix
, &name
, &var
, &type
);
200 if (!strcmp(type
, "array")) {
201 new = json_object_new_array();
202 jshn_add_objects(new, var
, true);
203 } else if (!strcmp(type
, "object")) {
204 new = json_object_new_object();
205 jshn_add_objects(new, var
, false);
206 } else if (!strcmp(type
, "string")) {
207 new = json_object_new_string(var
);
208 } else if (!strcmp(type
, "int")) {
209 new = json_object_new_int(atoi(var
));
210 } else if (!strcmp(type
, "boolean")) {
211 new = json_object_new_boolean(!!atoi(var
));
217 json_object_array_add(obj
, new);
219 json_object_object_add(obj
, name
, new);
222 static json_object
*jshn_add_objects(json_object
*obj
, const char *prefix
, bool array
)
224 char *keys
, *key
, *brk
;
226 keys
= get_keys(prefix
);
230 for (key
= strtok_r(keys
, " ", &brk
); key
;
231 key
= strtok_r(NULL
, " ", &brk
)) {
232 jshn_add_object_var(obj
, array
, prefix
, key
);
239 static int jshn_format(bool no_newline
)
243 obj
= json_object_new_object();
244 jshn_add_objects(obj
, "JSON_VAR", false);
245 fprintf(stdout
, "%s%s", json_object_to_json_string(obj
),
246 no_newline
? "" : "\n");
247 json_object_put(obj
);
251 static int usage(const char *progname
)
253 fprintf(stderr
, "Usage: %s [-n] -r <message>|-w\n", progname
);
257 int main(int argc
, char **argv
)
259 bool no_newline
= false;
262 while ((ch
= getopt(argc
, argv
, "p:nr:w")) != -1) {
266 var_prefix_len
= strlen(var_prefix
);
269 return jshn_parse(optarg
);
271 return jshn_format(no_newline
);
276 return usage(argv
[0]);
279 return usage(argv
[0]);