781861deda534602b560f138151534da55b91e31
[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         default:
116                 return -1;
117         }
118
119         fprintf(stdout, "json_add_%s '", type);
120         write_key_string(key);
121
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");
127                 break;
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");
132                 break;
133         case json_type_string:
134                 fprintf(stdout, "' '");
135                 add_json_string(json_object_get_string(obj));
136                 fprintf(stdout, "';\n");
137                 break;
138         case json_type_boolean:
139                 fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
140                 break;
141         case json_type_int:
142                 fprintf(stdout, "' %d;\n", json_object_get_int(obj));
143                 break;
144         default:
145                 return -1;
146         }
147
148         return 0;
149 }
150
151 static int jshn_parse(const char *str)
152 {
153         json_object *obj;
154
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");
158                 return 1;
159         }
160         fprintf(stdout, "json_init;\n");
161         add_json_object(obj);
162         fflush(stdout);
163
164         return 0;
165 }
166
167 static char *get_keys(const char *prefix)
168 {
169         char *keys;
170
171         keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1);
172         sprintf(keys, "%sKEYS_%s", var_prefix, prefix);
173         return getenv(keys);
174 }
175
176 static void get_var(const char *prefix, const char **name, char **var, char **type)
177 {
178         char *tmpname, *varname;
179
180         tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_"));
181
182         sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
183         *var = getenv(tmpname);
184
185         sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name);
186         *type = getenv(tmpname);
187
188         sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name);
189         varname = getenv(tmpname);
190         if (varname)
191                 *name = varname;
192 }
193
194 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
195
196 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
197 {
198         json_object *new;
199         char *var, *type;
200
201         get_var(prefix, &name, &var, &type);
202         if (!var || !type)
203                 return;
204
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));
217         } else {
218                 return;
219         }
220
221         if (array)
222                 json_object_array_add(obj, new);
223         else
224                 json_object_object_add(obj, name, new);
225 }
226
227 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
228 {
229         char *keys, *key, *brk;
230
231         keys = get_keys(prefix);
232         if (!keys || !obj)
233                 goto out;
234
235         for (key = strtok_r(keys, " ", &brk); key;
236              key = strtok_r(NULL, " ", &brk)) {
237                 jshn_add_object_var(obj, array, prefix, key);
238         }
239
240 out:
241         return obj;
242 }
243
244 static int jshn_format(bool no_newline)
245 {
246         json_object *obj;
247
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);
253         return 0;
254 }
255
256 static int usage(const char *progname)
257 {
258         fprintf(stderr, "Usage: %s [-n] -r <message>|-w\n", progname);
259         return 2;
260 }
261
262 int main(int argc, char **argv)
263 {
264         bool no_newline = false;
265         int ch;
266
267         while ((ch = getopt(argc, argv, "p:nr:w")) != -1) {
268                 switch(ch) {
269                 case 'p':
270                         var_prefix = optarg;
271                         var_prefix_len = strlen(var_prefix);
272                         break;
273                 case 'r':
274                         return jshn_parse(optarg);
275                 case 'w':
276                         return jshn_format(no_newline);
277                 case 'n':
278                         no_newline = true;
279                         break;
280                 default:
281                         return usage(argv[0]);
282                 }
283         }
284         return usage(argv[0]);
285 }