10 #define MAX_VARLEN 256
12 static const char *var_prefix = "";
13 static int var_prefix_len = 0;
15 static int add_json_element(const char *key, json_object *obj);
17 static int add_json_object(json_object *obj)
21 json_object_object_foreach(obj, key, val) {
22 ret = add_json_element(key, val);
29 static int add_json_array(struct array_list *a)
35 for (i = 0, len = array_list_length(a); i < len; i++) {
36 sprintf(seq, "%d", i);
37 ret = add_json_element(seq, array_list_get_idx(a, i));
45 static void add_json_string(const char *str)
47 char *ptr = (char *) str;
51 while ((c = strchr(ptr, '\'')) != NULL) {
54 fwrite(ptr, len, 1, stdout);
57 fwrite(c, strlen(c), 1, stdout);
61 fwrite(ptr, len, 1, stdout);
64 static void write_key_string(const char *key)
67 putc(isalnum(*key) ? *key : '_', stdout);
72 static int add_json_element(const char *key, json_object *obj)
79 switch (json_object_get_type(obj)) {
80 case json_type_object:
86 case json_type_string:
89 case json_type_boolean:
99 fprintf(stdout, "json_add_%s '", type);
100 write_key_string(key);
102 switch (json_object_get_type(obj)) {
103 case json_type_object:
104 fprintf(stdout, "';\n");
105 add_json_object(obj);
106 fprintf(stdout, "json_close_object;\n");
108 case json_type_array:
109 fprintf(stdout, "';\n");
110 add_json_array(json_object_get_array(obj));
111 fprintf(stdout, "json_close_array;\n");
113 case json_type_string:
114 fprintf(stdout, "' '");
115 add_json_string(json_object_get_string(obj));
116 fprintf(stdout, "';\n");
118 case json_type_boolean:
119 fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
122 fprintf(stdout, "' %d;\n", json_object_get_int(obj));
131 static int jshn_parse(const char *str)
135 obj = json_tokener_parse(str);
136 if (is_error(obj) || json_object_get_type(obj) != json_type_object) {
137 fprintf(stderr, "Failed to parse message data\n");
140 fprintf(stdout, "json_init;\n");
141 add_json_object(obj);
147 static char *get_keys(const char *prefix)
151 keys = alloca(var_prefix_len + strlen(prefix) + sizeof("KEYS_") + 1);
152 sprintf(keys, "%sKEYS_%s", var_prefix, prefix);
156 static void get_var(const char *prefix, const char **name, char **var, char **type)
158 char *tmpname, *varname;
160 tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("TYPE_"));
162 sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
163 *var = getenv(tmpname);
165 sprintf(tmpname, "%sTYPE_%s_%s", var_prefix, prefix, *name);
166 *type = getenv(tmpname);
168 sprintf(tmpname, "%sNAME_%s_%s", var_prefix, prefix, *name);
169 varname = getenv(tmpname);
174 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
176 static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
181 get_var(prefix, &name, &var, &type);
185 if (!strcmp(type, "array")) {
186 new = json_object_new_array();
187 jshn_add_objects(new, var, true);
188 } else if (!strcmp(type, "object")) {
189 new = json_object_new_object();
190 jshn_add_objects(new, var, false);
191 } else if (!strcmp(type, "string")) {
192 new = json_object_new_string(var);
193 } else if (!strcmp(type, "int")) {
194 new = json_object_new_int(atoi(var));
195 } else if (!strcmp(type, "boolean")) {
196 new = json_object_new_boolean(!!atoi(var));
202 json_object_array_add(obj, new);
204 json_object_object_add(obj, name, new);
207 static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
209 char *keys, *key, *brk;
211 keys = get_keys(prefix);
215 for (key = strtok_r(keys, " ", &brk); key;
216 key = strtok_r(NULL, " ", &brk)) {
217 jshn_add_object_var(obj, array, prefix, key);
224 static int jshn_format(bool no_newline)
228 obj = json_object_new_object();
229 jshn_add_objects(obj, "JSON_VAR", false);
230 fprintf(stdout, "%s%s", json_object_to_json_string(obj),
231 no_newline ? "" : "\n");
232 json_object_put(obj);
236 static int usage(const char *progname)
238 fprintf(stderr, "Usage: %s [-n] -r <message>|-w\n", progname);
242 int main(int argc, char **argv)
244 bool no_newline = false;
247 while ((ch = getopt(argc, argv, "p:nr:w")) != -1) {
251 var_prefix_len = strlen(var_prefix);
254 return jshn_parse(optarg);
256 return jshn_format(no_newline);
261 return usage(argv[0]);
264 return usage(argv[0]);