69cb06f91f75058fecfc9da92c05646398baad0d
[project/libubox.git] / jshn.c
1 /*
2 * Copyright (C) 2011-2013 Felix Fietkau <nbd@openwrt.org>
3 *
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.
7 *
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.
15 */
16 #ifdef JSONC
17 #include <json.h>
18 #else
19 #include <json/json.h>
20 #endif
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <ctype.h>
27 #include <getopt.h>
28 #include "list.h"
29
30 #include "blob.h"
31 #include "blobmsg_json.h"
32
33 #define MAX_VARLEN 256
34
35 static struct blob_buf b = { 0 };
36
37 static const char *var_prefix = "";
38 static int var_prefix_len = 0;
39
40 static int add_json_element(const char *key, json_object *obj);
41
42 static int add_json_object(json_object *obj)
43 {
44 int ret = 0;
45
46 json_object_object_foreach(obj, key, val) {
47 ret = add_json_element(key, val);
48 if (ret)
49 break;
50 }
51 return ret;
52 }
53
54 static int add_json_array(struct array_list *a)
55 {
56 char seq[12];
57 int i, len;
58 int ret;
59
60 for (i = 0, len = array_list_length(a); i < len; i++) {
61 sprintf(seq, "%d", i);
62 ret = add_json_element(seq, array_list_get_idx(a, i));
63 if (ret)
64 return ret;
65 }
66
67 return 0;
68 }
69
70 static void add_json_string(const char *str)
71 {
72 char *ptr = (char *) str;
73 int len;
74 char *c;
75
76 while ((c = strchr(ptr, '\'')) != NULL) {
77 len = c - ptr;
78 if (len > 0)
79 fwrite(ptr, len, 1, stdout);
80 ptr = c + 1;
81 c = "'\\''";
82 fwrite(c, strlen(c), 1, stdout);
83 }
84 len = strlen(ptr);
85 if (len > 0)
86 fwrite(ptr, len, 1, stdout);
87 }
88
89 static void write_key_string(const char *key)
90 {
91 while (*key) {
92 putc(isalnum(*key) ? *key : '_', stdout);
93 key++;
94 }
95 }
96
97 static int add_json_element(const char *key, json_object *obj)
98 {
99 char *type;
100
101 if (!obj)
102 return -1;
103
104 switch (json_object_get_type(obj)) {
105 case json_type_object:
106 type = "object";
107 break;
108 case json_type_array:
109 type = "array";
110 break;
111 case json_type_string:
112 type = "string";
113 break;
114 case json_type_boolean:
115 type = "boolean";
116 break;
117 case json_type_int:
118 type = "int";
119 break;
120 case json_type_double:
121 type = "double";
122 break;
123 default:
124 return -1;
125 }
126
127 fprintf(stdout, "json_add_%s '", type);
128 write_key_string(key);
129
130 switch (json_object_get_type(obj)) {
131 case json_type_object:
132 fprintf(stdout, "';\n");
133 add_json_object(obj);
134 fprintf(stdout, "json_close_object;\n");
135 break;
136 case json_type_array:
137 fprintf(stdout, "';\n");
138 add_json_array(json_object_get_array(obj));
139 fprintf(stdout, "json_close_array;\n");
140 break;
141 case json_type_string:
142 fprintf(stdout, "' '");
143 add_json_string(json_object_get_string(obj));
144 fprintf(stdout, "';\n");
145 break;
146 case json_type_boolean:
147 fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
148 break;
149 case json_type_int:
150 fprintf(stdout, "' %d;\n", json_object_get_int(obj));
151 break;
152 case json_type_double:
153 fprintf(stdout, "' %lf;\n", json_object_get_double(obj));
154 break;
155 default:
156 return -1;
157 }
158
159 return 0;
160 }
161
162 static int jshn_parse(const char *str)
163 {
164 json_object *obj;
165
166 obj = json_tokener_parse(str);
167 if (!obj || json_object_get_type(obj) != json_type_object) {
168 fprintf(stderr, "Failed to parse message data\n");
169 return 1;
170 }
171 fprintf(stdout, "json_init;\n");
172 add_json_object(obj);
173 fflush(stdout);
174
175 return 0;
176 }
177
178 static char *get_keys(const char *prefix)
179 {
180 char *keys;
181
182 keys = alloca(var_prefix_len + strlen(prefix) + sizeof("K_") + 1);
183 sprintf(keys, "%sK_%s", var_prefix, prefix);
184 return getenv(keys);
185 }
186
187 static void get_var(const char *prefix, const char **name, char **var, char **type)
188 {
189 char *tmpname, *varname;
190
191 tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("T_"));
192
193 sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
194 *var = getenv(tmpname);
195
196 sprintf(tmpname, "%sT_%s_%s", var_prefix, prefix, *name);
197 *type = getenv(tmpname);
198
199 sprintf(tmpname, "%sN_%s_%s", var_prefix, prefix, *name);
200 varname = getenv(tmpname);
201 if (varname)
202 *name = varname;
203 }
204
205 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
206
207 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
208 {
209 json_object *new;
210 char *var, *type;
211
212 get_var(prefix, &name, &var, &type);
213 if (!var || !type)
214 return;
215
216 if (!strcmp(type, "array")) {
217 new = json_object_new_array();
218 jshn_add_objects(new, var, true);
219 } else if (!strcmp(type, "object")) {
220 new = json_object_new_object();
221 jshn_add_objects(new, var, false);
222 } else if (!strcmp(type, "string")) {
223 new = json_object_new_string(var);
224 } else if (!strcmp(type, "int")) {
225 new = json_object_new_int(atoi(var));
226 } else if (!strcmp(type, "double")) {
227 new = json_object_new_double(strtod(var, NULL));
228 } else if (!strcmp(type, "boolean")) {
229 new = json_object_new_boolean(!!atoi(var));
230 } else {
231 return;
232 }
233
234 if (array)
235 json_object_array_add(obj, new);
236 else
237 json_object_object_add(obj, name, new);
238 }
239
240 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
241 {
242 char *keys, *key, *brk;
243
244 keys = get_keys(prefix);
245 if (!keys || !obj)
246 goto out;
247
248 for (key = strtok_r(keys, " ", &brk); key;
249 key = strtok_r(NULL, " ", &brk)) {
250 jshn_add_object_var(obj, array, prefix, key);
251 }
252
253 out:
254 return obj;
255 }
256
257 static int jshn_format(bool no_newline, bool indent)
258 {
259 json_object *obj;
260 const char *output;
261 char *blobmsg_output = NULL;
262 int ret = -1;
263
264 if (!(obj = json_object_new_object()))
265 return -1;
266
267 jshn_add_objects(obj, "J_V", false);
268 if (!(output = json_object_to_json_string(obj)))
269 goto out;
270
271 if (indent) {
272 blob_buf_init(&b, 0);
273 if (!blobmsg_add_json_from_string(&b, output))
274 goto out;
275 if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0)))
276 goto out;
277 output = blobmsg_output;
278 }
279 fprintf(stdout, "%s%s", output, no_newline ? "" : "\n");
280 free(blobmsg_output);
281 ret = 0;
282
283 out:
284 json_object_put(obj);
285 return ret;
286 }
287
288 static int usage(const char *progname)
289 {
290 fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-w\n", progname);
291 return 2;
292 }
293
294 int main(int argc, char **argv)
295 {
296 bool no_newline = false;
297 bool indent = false;
298 int ch;
299
300 while ((ch = getopt(argc, argv, "p:nir:w")) != -1) {
301 switch(ch) {
302 case 'p':
303 var_prefix = optarg;
304 var_prefix_len = strlen(var_prefix);
305 break;
306 case 'r':
307 return jshn_parse(optarg);
308 case 'w':
309 return jshn_format(no_newline, indent);
310 case 'n':
311 no_newline = true;
312 break;
313 case 'i':
314 indent = true;
315 break;
316 default:
317 return usage(argv[0]);
318 }
319 }
320 return usage(argv[0]);
321 }