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)
155 char *tmpname, *varname;
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);
162 memcpy(tmpname, "NAME", 4);
163 varname = getenv(tmpname);
168 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
170 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
175 get_var(prefix, &name, &var, &type);
179 if (!strcmp(type, "array")) {
180 new = json_object_new_array();
181 jshn_add_objects(new, var, true);
182 } else if (!strcmp(type, "object")) {
183 new = json_object_new_object();
184 jshn_add_objects(new, var, false);
185 } else if (!strcmp(type, "string")) {
186 new = json_object_new_string(var);
187 } else if (!strcmp(type, "int")) {
188 new = json_object_new_int(atoi(var));
189 } else if (!strcmp(type, "boolean")) {
190 new = json_object_new_boolean(!!atoi(var));
196 json_object_array_add(obj, new);
198 json_object_object_add(obj, name, new);
201 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
203 char *keys, *key, *brk;
205 keys = get_keys(prefix);
209 for (key = strtok_r(keys, " ", &brk); key;
210 key = strtok_r(NULL, " ", &brk)) {
211 jshn_add_object_var(obj, array, prefix, key);
218 static int jshn_format(bool no_newline)
222 obj = json_object_new_object();
223 jshn_add_objects(obj, "JSON_VAR", false);
224 fprintf(stdout, "%s%s", json_object_to_json_string(obj),
225 no_newline ? "" : "\n");
226 json_object_put(obj);
230 static int usage(const char *progname)
232 fprintf(stderr, "Usage: %s [-n] -r <message>|-w\n", progname);
236 int main(int argc, char **argv)
238 bool no_newline = false;
241 while ((ch = getopt(argc, argv, "nr:w")) != -1) {
244 return jshn_parse(optarg);
246 return jshn_format(no_newline);
251 return usage(argv[0]);
254 return usage(argv[0]);