2 #include <libubox/list.h>
10 #define MAX_VARLEN 256
12 static int add_json_element(const char *key
, json_object
*obj
);
14 static int add_json_object(json_object
*obj
)
18 json_object_object_foreach(obj
, key
, val
) {
19 ret
= add_json_element(key
, val
);
26 static int add_json_array(struct array_list
*a
)
32 for (i
= 0, len
= array_list_length(a
); i
< len
; i
++) {
33 sprintf(seq
, "%d", i
);
34 ret
= add_json_element(seq
, array_list_get_idx(a
, i
));
42 static void add_json_string(const char *str
)
44 char *ptr
= (char *) str
;
48 while ((c
= strchr(ptr
, '\'')) != NULL
) {
51 fwrite(ptr
, len
, 1, stdout
);
54 fwrite(c
, strlen(c
), 1, stdout
);
58 fwrite(ptr
, len
, 1, stdout
);
61 static void write_key_string(const char *key
)
64 putc(isalnum(*key
) ? *key
: '_', stdout
);
69 static int add_json_element(const char *key
, json_object
*obj
)
76 switch (json_object_get_type(obj
)) {
77 case json_type_object
:
83 case json_type_string
:
86 case json_type_boolean
:
96 fprintf(stdout
, "json_add_%s '", type
);
97 write_key_string(key
);
99 switch (json_object_get_type(obj
)) {
100 case json_type_object
:
101 fprintf(stdout
, "';\n");
102 add_json_object(obj
);
103 fprintf(stdout
, "json_close_object;\n");
105 case json_type_array
:
106 fprintf(stdout
, "';\n");
107 add_json_array(json_object_get_array(obj
));
108 fprintf(stdout
, "json_close_array;\n");
110 case json_type_string
:
111 fprintf(stdout
, "' '");
112 add_json_string(json_object_get_string(obj
));
113 fprintf(stdout
, "';\n");
115 case json_type_boolean
:
116 fprintf(stdout
, "' %d;\n", json_object_get_boolean(obj
));
119 fprintf(stdout
, "' %d;\n", json_object_get_int(obj
));
128 static int jshn_parse(const char *str
)
132 obj
= json_tokener_parse(str
);
133 if (is_error(obj
) || json_object_get_type(obj
) != json_type_object
) {
134 fprintf(stderr
, "Failed to parse message data\n");
137 fprintf(stdout
, "json_init;\n");
138 add_json_object(obj
);
144 static char *get_keys(const char *prefix
)
148 keys
= alloca(strlen(prefix
) + sizeof("KEYS_") + 1);
149 sprintf(keys
, "KEYS_%s", prefix
);
153 static void get_var(const char *prefix
, const char *name
, char **var
, char **type
)
157 tmpname
= alloca(strlen(prefix
) + 1 + strlen(name
) + 1 + sizeof("TYPE_"));
158 sprintf(tmpname
, "TYPE_%s_%s", prefix
, name
);
159 *var
= getenv(tmpname
+ 5);
160 *type
= getenv(tmpname
);
163 static json_object
*jshn_add_objects(json_object
*obj
, const char *prefix
, bool array
);
165 static void jshn_add_object_var(json_object
*obj
, bool array
, const char *prefix
, const char *name
)
170 get_var(prefix
, name
, &var
, &type
);
174 if (!strcmp(type
, "array")) {
175 new = json_object_new_array();
176 jshn_add_objects(new, var
, true);
177 } else if (!strcmp(type
, "object")) {
178 new = json_object_new_object();
179 jshn_add_objects(new, var
, false);
180 } else if (!strcmp(type
, "string")) {
181 new = json_object_new_string(var
);
182 } else if (!strcmp(type
, "int")) {
183 new = json_object_new_int(atoi(var
));
184 } else if (!strcmp(type
, "boolean")) {
185 new = json_object_new_boolean(!!atoi(var
));
191 json_object_array_add(obj
, new);
193 json_object_object_add(obj
, name
, new);
196 static json_object
*jshn_add_objects(json_object
*obj
, const char *prefix
, bool array
)
198 char *keys
, *key
, *brk
;
200 keys
= get_keys(prefix
);
204 for (key
= strtok_r(keys
, " ", &brk
); key
;
205 key
= strtok_r(NULL
, " ", &brk
)) {
206 jshn_add_object_var(obj
, array
, prefix
, key
);
213 static int jshn_format(void)
217 obj
= json_object_new_object();
218 jshn_add_objects(obj
, "JSON_VAR", false);
219 fprintf(stdout
, "%s\n", json_object_to_json_string(obj
));
220 json_object_put(obj
);
224 static int usage(const char *progname
)
226 fprintf(stderr
, "Usage: %s -r <message>|-w\n", progname
);
230 int main(int argc
, char **argv
)
234 while ((ch
= getopt(argc
, argv
, "r:w")) != -1) {
237 return jshn_parse(optarg
);
239 return jshn_format();
241 return usage(argv
[0]);
244 return usage(argv
[0]);
This page took 0.052494 seconds and 5 git commands to generate.