jshn.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <stdbool.h>
  25. #include <ctype.h>
  26. #include <getopt.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <inttypes.h>
  30. #include "list.h"
  31. #include "avl.h"
  32. #include "blob.h"
  33. #include "blobmsg_json.h"
  34. #define MAX_VARLEN 256
  35. static struct avl_tree env_vars;
  36. static struct blob_buf b = { 0 };
  37. static const char *var_prefix = "";
  38. static int var_prefix_len = 0;
  39. static int add_json_element(const char *key, json_object *obj);
  40. struct env_var {
  41. struct avl_node avl;
  42. char *val;
  43. };
  44. static int add_json_object(json_object *obj)
  45. {
  46. int ret = 0;
  47. json_object_object_foreach(obj, key, val) {
  48. ret = add_json_element(key, val);
  49. if (ret)
  50. break;
  51. }
  52. return ret;
  53. }
  54. static int add_json_array(struct array_list *a)
  55. {
  56. char seq[12];
  57. int i, len;
  58. int ret;
  59. for (i = 0, len = array_list_length(a); i < len; i++) {
  60. sprintf(seq, "%d", i);
  61. ret = add_json_element(seq, array_list_get_idx(a, i));
  62. if (ret)
  63. return ret;
  64. }
  65. return 0;
  66. }
  67. static void add_json_string(const char *str)
  68. {
  69. char *ptr = (char *) str;
  70. int len;
  71. char *c;
  72. while ((c = strchr(ptr, '\'')) != NULL) {
  73. len = c - ptr;
  74. if (len > 0)
  75. fwrite(ptr, len, 1, stdout);
  76. ptr = c + 1;
  77. c = "'\\''";
  78. fwrite(c, strlen(c), 1, stdout);
  79. }
  80. len = strlen(ptr);
  81. if (len > 0)
  82. fwrite(ptr, len, 1, stdout);
  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. static int add_json_element(const char *key, json_object *obj)
  92. {
  93. char *type;
  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. case json_type_double:
  111. type = "double";
  112. break;
  113. case json_type_null:
  114. type = "null";
  115. break;
  116. default:
  117. return -1;
  118. }
  119. fprintf(stdout, "json_add_%s '", type);
  120. write_key_string(key);
  121. switch (json_object_get_type(obj)) {
  122. case json_type_object:
  123. fprintf(stdout, "';\n");
  124. add_json_object(obj);
  125. fprintf(stdout, "json_close_object;\n");
  126. break;
  127. case json_type_array:
  128. fprintf(stdout, "';\n");
  129. add_json_array(json_object_get_array(obj));
  130. fprintf(stdout, "json_close_array;\n");
  131. break;
  132. case json_type_string:
  133. fprintf(stdout, "' '");
  134. add_json_string(json_object_get_string(obj));
  135. fprintf(stdout, "';\n");
  136. break;
  137. case json_type_boolean:
  138. fprintf(stdout, "' %d;\n", json_object_get_boolean(obj));
  139. break;
  140. case json_type_int:
  141. fprintf(stdout, "' %"PRId64";\n", json_object_get_int64(obj));
  142. break;
  143. case json_type_double:
  144. fprintf(stdout, "' %lf;\n", json_object_get_double(obj));
  145. break;
  146. case json_type_null:
  147. fprintf(stdout, "';\n");
  148. break;
  149. default:
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. static int jshn_parse(const char *str)
  155. {
  156. json_object *obj;
  157. obj = json_tokener_parse(str);
  158. if (!obj || json_object_get_type(obj) != json_type_object) {
  159. if (obj)
  160. json_object_put(obj);
  161. fprintf(stderr, "Failed to parse message data\n");
  162. return 1;
  163. }
  164. fprintf(stdout, "json_init;\n");
  165. add_json_object(obj);
  166. fflush(stdout);
  167. json_object_put(obj);
  168. return 0;
  169. }
  170. static char *getenv_avl(const char *key)
  171. {
  172. struct env_var *var = avl_find_element(&env_vars, key, var, avl);
  173. return var ? var->val : NULL;
  174. }
  175. static char *get_keys(const char *prefix)
  176. {
  177. char *keys;
  178. keys = alloca(var_prefix_len + strlen(prefix) + sizeof("K_") + 1);
  179. sprintf(keys, "%sK_%s", var_prefix, prefix);
  180. return getenv_avl(keys);
  181. }
  182. static void get_var(const char *prefix, const char **name, char **var, char **type)
  183. {
  184. char *tmpname, *varname;
  185. tmpname = alloca(var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("T_"));
  186. sprintf(tmpname, "%s%s_%s", var_prefix, prefix, *name);
  187. *var = getenv_avl(tmpname);
  188. sprintf(tmpname, "%sT_%s_%s", var_prefix, prefix, *name);
  189. *type = getenv_avl(tmpname);
  190. sprintf(tmpname, "%sN_%s_%s", var_prefix, prefix, *name);
  191. varname = getenv_avl(tmpname);
  192. if (varname)
  193. *name = varname;
  194. }
  195. static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
  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. get_var(prefix, &name, &var, &type);
  201. if (!var || !type)
  202. return;
  203. if (!strcmp(type, "array")) {
  204. new = json_object_new_array();
  205. jshn_add_objects(new, var, true);
  206. } else if (!strcmp(type, "object")) {
  207. new = json_object_new_object();
  208. jshn_add_objects(new, var, false);
  209. } else if (!strcmp(type, "string")) {
  210. new = json_object_new_string(var);
  211. } else if (!strcmp(type, "int")) {
  212. new = json_object_new_int64(atoll(var));
  213. } else if (!strcmp(type, "double")) {
  214. new = json_object_new_double(strtod(var, NULL));
  215. } else if (!strcmp(type, "boolean")) {
  216. new = json_object_new_boolean(!!atoi(var));
  217. } else if (!strcmp(type, "null")) {
  218. new = NULL;
  219. } else {
  220. return;
  221. }
  222. if (array)
  223. json_object_array_add(obj, new);
  224. else
  225. json_object_object_add(obj, name, new);
  226. }
  227. static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
  228. {
  229. char *keys, *key, *brk;
  230. keys = get_keys(prefix);
  231. if (!keys || !obj)
  232. goto out;
  233. for (key = strtok_r(keys, " ", &brk); key;
  234. key = strtok_r(NULL, " ", &brk)) {
  235. jshn_add_object_var(obj, array, prefix, key);
  236. }
  237. out:
  238. return obj;
  239. }
  240. static int jshn_format(bool no_newline, bool indent, FILE *stream)
  241. {
  242. json_object *obj;
  243. const char *output;
  244. char *blobmsg_output = NULL;
  245. int ret = -1;
  246. if (!(obj = json_object_new_object()))
  247. return -1;
  248. jshn_add_objects(obj, "J_V", false);
  249. if (!(output = json_object_to_json_string(obj)))
  250. goto out;
  251. if (indent) {
  252. blob_buf_init(&b, 0);
  253. if (!blobmsg_add_json_from_string(&b, output))
  254. goto out;
  255. if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0)))
  256. goto out;
  257. output = blobmsg_output;
  258. }
  259. fprintf(stream, "%s%s", output, no_newline ? "" : "\n");
  260. free(blobmsg_output);
  261. ret = 0;
  262. out:
  263. json_object_put(obj);
  264. return ret;
  265. }
  266. static int usage(const char *progname)
  267. {
  268. fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-R <file>|-o <file>|-p <prefix>|-w\n", progname);
  269. return 2;
  270. }
  271. static int avl_strcmp_var(const void *k1, const void *k2, void *ptr)
  272. {
  273. const char *s1 = k1;
  274. const char *s2 = k2;
  275. char c1, c2;
  276. while (*s1 && *s1 == *s2) {
  277. s1++;
  278. s2++;
  279. }
  280. c1 = *s1;
  281. c2 = *s2;
  282. if (c1 == '=')
  283. c1 = 0;
  284. if (c2 == '=')
  285. c2 = 0;
  286. return c1 - c2;
  287. }
  288. static int jshn_parse_file(const char *path)
  289. {
  290. struct stat sb;
  291. int ret = 0;
  292. char *fbuf;
  293. int fd;
  294. if ((fd = open(path, O_RDONLY)) == -1) {
  295. fprintf(stderr, "Error opening %s\n", path);
  296. return 3;
  297. }
  298. if (fstat(fd, &sb) == -1) {
  299. fprintf(stderr, "Error getting size of %s\n", path);
  300. close(fd);
  301. return 3;
  302. }
  303. if (!(fbuf = calloc(1, sb.st_size+1))) {
  304. fprintf(stderr, "Error allocating memory for %s\n", path);
  305. close(fd);
  306. return 3;
  307. }
  308. if (read(fd, fbuf, sb.st_size) != sb.st_size) {
  309. fprintf(stderr, "Error reading %s\n", path);
  310. free(fbuf);
  311. close(fd);
  312. return 3;
  313. }
  314. ret = jshn_parse(fbuf);
  315. free(fbuf);
  316. close(fd);
  317. return ret;
  318. }
  319. static int jshn_format_file(const char *path, bool no_newline, bool indent)
  320. {
  321. FILE *fp = NULL;
  322. int ret = 0;
  323. fp = fopen(path, "w");
  324. if (!fp) {
  325. fprintf(stderr, "Error opening %s\n", path);
  326. return 3;
  327. }
  328. ret = jshn_format(no_newline, indent, fp);
  329. fclose(fp);
  330. return ret;
  331. }
  332. int main(int argc, char **argv)
  333. {
  334. extern char **environ;
  335. bool no_newline = false;
  336. bool indent = false;
  337. struct env_var *vars;
  338. int i;
  339. int ret = 0;
  340. int ch;
  341. avl_init(&env_vars, avl_strcmp_var, false, NULL);
  342. for (i = 0; environ[i]; i++);
  343. vars = calloc(i, sizeof(*vars));
  344. if (!vars) {
  345. fprintf(stderr, "%m\n");
  346. return -1;
  347. }
  348. for (i = 0; environ[i]; i++) {
  349. char *c;
  350. vars[i].avl.key = environ[i];
  351. c = strchr(environ[i], '=');
  352. if (!c)
  353. continue;
  354. vars[i].val = c + 1;
  355. avl_insert(&env_vars, &vars[i].avl);
  356. }
  357. while ((ch = getopt(argc, argv, "p:nir:R:o:w")) != -1) {
  358. switch(ch) {
  359. case 'p':
  360. var_prefix = optarg;
  361. var_prefix_len = strlen(var_prefix);
  362. break;
  363. case 'r':
  364. ret = jshn_parse(optarg);
  365. goto exit;
  366. case 'R':
  367. ret = jshn_parse_file(optarg);
  368. goto exit;
  369. case 'w':
  370. ret = jshn_format(no_newline, indent, stdout);
  371. goto exit;
  372. case 'o':
  373. ret = jshn_format_file(optarg, no_newline, indent);
  374. goto exit;
  375. case 'n':
  376. no_newline = true;
  377. break;
  378. case 'i':
  379. indent = true;
  380. break;
  381. default:
  382. free(vars);
  383. return usage(argv[0]);
  384. }
  385. }
  386. free(vars);
  387. return usage(argv[0]);
  388. exit:
  389. free(vars);
  390. return ret;
  391. }