jshn: add support for the double datatype
[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 #define MAX_VARLEN 256
31
32 static const char *var_prefix = "";
33 static int var_prefix_len = 0;
34
35 static int add_json_element(const char *key, json_object *obj);
36
37 static int add_json_object(json_object *obj)
38 {
39 int ret = 0;
40
41 json_object_object_foreach(obj, key, val) {
42 ret = add_json_element(key, val);
43 if (ret)
44 break;
45 }
46 return ret;
47 }
48
49 static int add_json_array(struct array_list *a)
50 {
51 char seq[12];
52 int i, len;
53 int ret;
54
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));
58 if (ret)
59 return ret;
60 }
61
62 return 0;
63 }
64
65 static void add_json_string(const char *str)
66 {
67 char *ptr = (char *) str;
68 int len;
69 char *c;
70
71 while ((c = strchr(ptr, '\'')) != NULL) {
72 len = c - ptr;
73 if (len > 0)
74 fwrite(ptr, len, 1, stdout);
75 ptr = c + 1;
76 c = "'\\''";
77 fwrite(c, strlen(c), 1, stdout);
78 }
79 len = strlen(ptr);
80 if (len > 0)
81 fwrite(ptr, len, 1, stdout);
82 }
83
84 static void write_key_string(const char *key)
85 {
86 while (*key) {
87 putc(isalnum(*key) ? *key : '_', stdout);
88 key++;
89 }
90 }
91
92 static int add_json_element(const char *key, json_object *obj)
93 {
94 char *type;
95
96 if (!obj)
97 return -1;
98
99 switch (json_object_get_type(obj)) {
100 case json_type_object:
101 type = "object";
102 break;
103 case json_type_array:
104 type = "array";
105 break;
106 case json_type_string:
107 type = "string";
108 break;
109 case json_type_boolean:
110 type = "boolean";
111 break;
112 case json_type_int:
113 type = "int";
114 break;
115 case json_type_double:
116 type = "double";
117 break;
118 default:
119 return -1;
120 }
121
122 fprintf(stdout, "json_add_%s '", type);
123 write_key_string(key);
124
125 switch (json_object_get_type(obj)) {
126 case json_type_object:
127 fprintf(stdout, "';\n");
128 add_json_object(obj);
129 fprintf(stdout, "json_close_object;\n");
130 break;
131 case json_type_array:
132 fprintf(stdout, "';\n");
133 add_json_array(json_object_get_array(obj));
134 fprintf(stdout, "json_close_array;\n");
135 break;
136 case json_type_string:
137 fprintf(stdout, "' '");
138 add_json_string(json_object_get_string(obj));
139 fprintf(stdout, "';\n");
140 break;
141 case json_type_boolean:
142 fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
143 break;
144 case json_type_int:
145 fprintf(stdout, "' %d;\n", json_object_get_int(obj));
146 break;
147 case json_type_double:
148 fprintf(stdout, "' %lf;\n", json_object_get_double(obj));
149 break;
150 default:
151 return -1;
152 }
153
154 return 0;
155 }
156
157 static int jshn_parse(const char *str)
158 {
159 json_object *obj;
160
161 obj = json_tokener_parse(str);
162 if (is_error(obj) || json_object_get_type(obj) != json_type_object) {
163 fprintf(stderr, "Failed to parse message data\n");
164 return 1;
165 }
166 fprintf(stdout, "json_init;\n");
167 add_json_object(obj);
168 fflush(stdout);
169
170 return 0;
171 }
172
173 static char *get_keys(const char *prefix)
174 {
175 char *keys;
176
177 keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1);
178 sprintf(keys, "%sKEYS_%s", var_prefix, prefix);
179 return getenv(keys);
180 }
181
182 static void get_var(const char *prefix, const char **name, char **var, char **type)
183 {
184 char *tmpname, *varname;
185
186 tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_"));
187
188 sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
189 *var = getenv(tmpname);
190
191 sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name);
192 *type = getenv(tmpname);
193
194 sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name);
195 varname = getenv(tmpname);
196 if (varname)
197 *name = varname;
198 }
199
200 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
201
202 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
203 {
204 json_object *new;
205 char *var, *type;
206
207 get_var(prefix, &name, &var, &type);
208 if (!var || !type)
209 return;
210
211 if (!strcmp(type, "array")) {
212 new = json_object_new_array();
213 jshn_add_objects(new, var, true);
214 } else if (!strcmp(type, "object")) {
215 new = json_object_new_object();
216 jshn_add_objects(new, var, false);
217 } else if (!strcmp(type, "string")) {
218 new = json_object_new_string(var);
219 } else if (!strcmp(type, "int")) {
220 new = json_object_new_int(atoi(var));
221 } else if (!strcmp(type, "double")) {
222 new = json_object_new_double(strtod(var, NULL));
223 } else if (!strcmp(type, "boolean")) {
224 new = json_object_new_boolean(!!atoi(var));
225 } else {
226 return;
227 }
228
229 if (array)
230 json_object_array_add(obj, new);
231 else
232 json_object_object_add(obj, name, new);
233 }
234
235 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
236 {
237 char *keys, *key, *brk;
238
239 keys = get_keys(prefix);
240 if (!keys || !obj)
241 goto out;
242
243 for (key = strtok_r(keys, " ", &brk); key;
244 key = strtok_r(NULL, " ", &brk)) {
245 jshn_add_object_var(obj, array, prefix, key);
246 }
247
248 out:
249 return obj;
250 }
251
252 static int jshn_format(bool no_newline)
253 {
254 json_object *obj;
255
256 obj = json_object_new_object();
257 jshn_add_objects(obj, "JSON_VAR", false);
258 fprintf(stdout, "%s%s", json_object_to_json_string(obj),
259 no_newline ? "" : "\n");
260 json_object_put(obj);
261 return 0;
262 }
263
264 static int usage(const char *progname)
265 {
266 fprintf(stderr, "Usage: %s [-n] -r <message>|-w\n", progname);
267 return 2;
268 }
269
270 int main(int argc, char **argv)
271 {
272 bool no_newline = false;
273 int ch;
274
275 while ((ch = getopt(argc, argv, "p:nr:w")) != -1) {
276 switch(ch) {
277 case 'p':
278 var_prefix = optarg;
279 var_prefix_len = strlen(var_prefix);
280 break;
281 case 'r':
282 return jshn_parse(optarg);
283 case 'w':
284 return jshn_format(no_newline);
285 case 'n':
286 no_newline = true;
287 break;
288 default:
289 return usage(argv[0]);
290 }
291 }
292 return usage(argv[0]);
293 }