jshn: add copyright info
[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 #include <json/json.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <ctype.h>
22 #include <getopt.h>
23 #include "list.h"
24
25 #define MAX_VARLEN      256
26
27 static const char *var_prefix = "";
28 static int var_prefix_len = 0;
29
30 static int add_json_element(const char *key, json_object *obj);
31
32 static int add_json_object(json_object *obj)
33 {
34         int ret = 0;
35
36         json_object_object_foreach(obj, key, val) {
37                 ret = add_json_element(key, val);
38                 if (ret)
39                         break;
40         }
41         return ret;
42 }
43
44 static int add_json_array(struct array_list *a)
45 {
46         char seq[12];
47         int i, len;
48         int ret;
49
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));
53                 if (ret)
54                         return ret;
55         }
56
57         return 0;
58 }
59
60 static void add_json_string(const char *str)
61 {
62         char *ptr = (char *) str;
63         int len;
64         char *c;
65
66         while ((c = strchr(ptr, '\'')) != NULL) {
67                 len = c - ptr;
68                 if (len > 0)
69                         fwrite(ptr, len, 1, stdout);
70                 ptr = c + 1;
71                 c = "'\\''";
72                 fwrite(c, strlen(c), 1, stdout);
73         }
74         len = strlen(ptr);
75         if (len > 0)
76                 fwrite(ptr, len, 1, stdout);
77 }
78
79 static void write_key_string(const char *key)
80 {
81         while (*key) {
82                 putc(isalnum(*key) ? *key : '_', stdout);
83                 key++;
84         }
85 }
86
87 static int add_json_element(const char *key, json_object *obj)
88 {
89         char *type;
90
91         if (!obj)
92                 return -1;
93
94         switch (json_object_get_type(obj)) {
95         case json_type_object:
96                 type = "object";
97                 break;
98         case json_type_array:
99                 type = "array";
100                 break;
101         case json_type_string:
102                 type = "string";
103                 break;
104         case json_type_boolean:
105                 type = "boolean";
106                 break;
107         case json_type_int:
108                 type = "int";
109                 break;
110         default:
111                 return -1;
112         }
113
114         fprintf(stdout, "json_add_%s '", type);
115         write_key_string(key);
116
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");
122                 break;
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");
127                 break;
128         case json_type_string:
129                 fprintf(stdout, "' '");
130                 add_json_string(json_object_get_string(obj));
131                 fprintf(stdout, "';\n");
132                 break;
133         case json_type_boolean:
134                 fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
135                 break;
136         case json_type_int:
137                 fprintf(stdout, "' %d;\n", json_object_get_int(obj));
138                 break;
139         default:
140                 return -1;
141         }
142
143         return 0;
144 }
145
146 static int jshn_parse(const char *str)
147 {
148         json_object *obj;
149
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");
153                 return 1;
154         }
155         fprintf(stdout, "json_init;\n");
156         add_json_object(obj);
157         fflush(stdout);
158
159         return 0;
160 }
161
162 static char *get_keys(const char *prefix)
163 {
164         char *keys;
165
166         keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1);
167         sprintf(keys, "%sKEYS_%s", var_prefix, prefix);
168         return getenv(keys);
169 }
170
171 static void get_var(const char *prefix, const char **name, char **var, char **type)
172 {
173         char *tmpname, *varname;
174
175         tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_"));
176
177         sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
178         *var = getenv(tmpname);
179
180         sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name);
181         *type = getenv(tmpname);
182
183         sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name);
184         varname = getenv(tmpname);
185         if (varname)
186                 *name = varname;
187 }
188
189 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
190
191 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
192 {
193         json_object *new;
194         char *var, *type;
195
196         get_var(prefix, &name, &var, &type);
197         if (!var || !type)
198                 return;
199
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));
212         } else {
213                 return;
214         }
215
216         if (array)
217                 json_object_array_add(obj, new);
218         else
219                 json_object_object_add(obj, name, new);
220 }
221
222 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
223 {
224         char *keys, *key, *brk;
225
226         keys = get_keys(prefix);
227         if (!keys || !obj)
228                 goto out;
229
230         for (key = strtok_r(keys, " ", &brk); key;
231              key = strtok_r(NULL, " ", &brk)) {
232                 jshn_add_object_var(obj, array, prefix, key);
233         }
234
235 out:
236         return obj;
237 }
238
239 static int jshn_format(bool no_newline)
240 {
241         json_object *obj;
242
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);
248         return 0;
249 }
250
251 static int usage(const char *progname)
252 {
253         fprintf(stderr, "Usage: %s [-n] -r <message>|-w\n", progname);
254         return 2;
255 }
256
257 int main(int argc, char **argv)
258 {
259         bool no_newline = false;
260         int ch;
261
262         while ((ch = getopt(argc, argv, "p:nr:w")) != -1) {
263                 switch(ch) {
264                 case 'p':
265                         var_prefix = optarg;
266                         var_prefix_len = strlen(var_prefix);
267                         break;
268                 case 'r':
269                         return jshn_parse(optarg);
270                 case 'w':
271                         return jshn_format(no_newline);
272                 case 'n':
273                         no_newline = true;
274                         break;
275                 default:
276                         return usage(argv[0]);
277                 }
278         }
279         return usage(argv[0]);
280 }