jshn.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. snprintf(seq, sizeof(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. size_t len = var_prefix_len + strlen(prefix) + sizeof("K_") + 1;
  179. keys = alloca(len);
  180. snprintf(keys, len, "%sK_%s", var_prefix, prefix);
  181. return getenv_avl(keys);
  182. }
  183. static void get_var(const char *prefix, const char **name, char **var, char **type)
  184. {
  185. char *tmpname, *varname;
  186. size_t len = var_prefix_len + strlen(prefix) + 1 + strlen(*name) + 1 + sizeof("T_");
  187. tmpname = alloca(len);
  188. snprintf(tmpname, len, "%s%s_%s", var_prefix, prefix, *name);
  189. *var = getenv_avl(tmpname);
  190. snprintf(tmpname, len, "%sT_%s_%s", var_prefix, prefix, *name);
  191. *type = getenv_avl(tmpname);
  192. snprintf(tmpname, len, "%sN_%s_%s", var_prefix, prefix, *name);
  193. varname = getenv_avl(tmpname);
  194. if (varname)
  195. *name = varname;
  196. }
  197. static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array);
  198. static void jshn_add_object_var(json_object *obj, bool array, const char *prefix, const char *name)
  199. {
  200. json_object *new;
  201. char *var, *type;
  202. get_var(prefix, &name, &var, &type);
  203. if (!var || !type)
  204. return;
  205. if (!strcmp(type, "array")) {
  206. new = json_object_new_array();
  207. jshn_add_objects(new, var, true);
  208. } else if (!strcmp(type, "object")) {
  209. new = json_object_new_object();
  210. jshn_add_objects(new, var, false);
  211. } else if (!strcmp(type, "string")) {
  212. new = json_object_new_string(var);
  213. } else if (!strcmp(type, "int")) {
  214. new = json_object_new_int64(atoll(var));
  215. } else if (!strcmp(type, "double")) {
  216. new = json_object_new_double(strtod(var, NULL));
  217. } else if (!strcmp(type, "boolean")) {
  218. new = json_object_new_boolean(!!atoi(var));
  219. } else if (!strcmp(type, "null")) {
  220. new = NULL;
  221. } else {
  222. return;
  223. }
  224. if (array)
  225. json_object_array_add(obj, new);
  226. else
  227. json_object_object_add(obj, name, new);
  228. }
  229. static json_object *jshn_add_objects(json_object *obj, const char *prefix, bool array)
  230. {
  231. char *keys, *key, *brk;
  232. keys = get_keys(prefix);
  233. if (!keys || !obj)
  234. goto out;
  235. for (key = strtok_r(keys, " ", &brk); key;
  236. key = strtok_r(NULL, " ", &brk)) {
  237. jshn_add_object_var(obj, array, prefix, key);
  238. }
  239. out:
  240. return obj;
  241. }
  242. static int jshn_format(bool no_newline, bool indent, FILE *stream)
  243. {
  244. json_object *obj;
  245. const char *output;
  246. char *blobmsg_output = NULL;
  247. int ret = -1;
  248. if (!(obj = json_object_new_object()))
  249. return -1;
  250. jshn_add_objects(obj, "J_V", false);
  251. if (!(output = json_object_to_json_string(obj)))
  252. goto out;
  253. if (indent) {
  254. blob_buf_init(&b, 0);
  255. if (!blobmsg_add_json_from_string(&b, output))
  256. goto out;
  257. if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0)))
  258. goto out;
  259. output = blobmsg_output;
  260. }
  261. fprintf(stream, "%s%s", output, no_newline ? "" : "\n");
  262. free(blobmsg_output);
  263. ret = 0;
  264. out:
  265. json_object_put(obj);
  266. return ret;
  267. }
  268. static int usage(const char *progname)
  269. {
  270. fprintf(stderr, "Usage: %s [-n] [-i] -r <message>|-R <file>|-o <file>|-p <prefix>|-w\n", progname);
  271. return 2;
  272. }
  273. static int avl_strcmp_var(const void *k1, const void *k2, void *ptr)
  274. {
  275. const char *s1 = k1;
  276. const char *s2 = k2;
  277. char c1, c2;
  278. while (*s1 && *s1 == *s2) {
  279. s1++;
  280. s2++;
  281. }
  282. c1 = *s1;
  283. c2 = *s2;
  284. if (c1 == '=')
  285. c1 = 0;
  286. if (c2 == '=')
  287. c2 = 0;
  288. return c1 - c2;
  289. }
  290. static int jshn_parse_file(const char *path)
  291. {
  292. struct stat sb;
  293. int ret = 0;
  294. char *fbuf;
  295. int fd;
  296. if ((fd = open(path, O_RDONLY)) == -1) {
  297. fprintf(stderr, "Error opening %s\n", path);
  298. return 3;
  299. }
  300. if (fstat(fd, &sb) == -1) {
  301. fprintf(stderr, "Error getting size of %s\n", path);
  302. close(fd);
  303. return 3;
  304. }
  305. if (!(fbuf = calloc(1, sb.st_size+1))) {
  306. fprintf(stderr, "Error allocating memory for %s\n", path);
  307. close(fd);
  308. return 3;
  309. }
  310. if (read(fd, fbuf, sb.st_size) != sb.st_size) {
  311. fprintf(stderr, "Error reading %s\n", path);
  312. free(fbuf);
  313. close(fd);
  314. return 3;
  315. }
  316. ret = jshn_parse(fbuf);
  317. free(fbuf);
  318. close(fd);
  319. return ret;
  320. }
  321. static int jshn_format_file(const char *path, bool no_newline, bool indent)
  322. {
  323. FILE *fp = NULL;
  324. int ret = 0;
  325. fp = fopen(path, "w");
  326. if (!fp) {
  327. fprintf(stderr, "Error opening %s\n", path);
  328. return 3;
  329. }
  330. ret = jshn_format(no_newline, indent, fp);
  331. fclose(fp);
  332. return ret;
  333. }
  334. int main(int argc, char **argv)
  335. {
  336. extern char **environ;
  337. bool no_newline = false;
  338. bool indent = false;
  339. struct env_var *vars;
  340. int i;
  341. int ret = 0;
  342. int ch;
  343. avl_init(&env_vars, avl_strcmp_var, false, NULL);
  344. for (i = 0; environ[i]; i++);
  345. vars = calloc(i, sizeof(*vars));
  346. if (!vars) {
  347. fprintf(stderr, "%m\n");
  348. return -1;
  349. }
  350. for (i = 0; environ[i]; i++) {
  351. char *c;
  352. vars[i].avl.key = environ[i];
  353. c = strchr(environ[i], '=');
  354. if (!c)
  355. continue;
  356. vars[i].val = c + 1;
  357. avl_insert(&env_vars, &vars[i].avl);
  358. }
  359. while ((ch = getopt(argc, argv, "p:nir:R:o:w")) != -1) {
  360. switch(ch) {
  361. case 'p':
  362. var_prefix = optarg;
  363. var_prefix_len = strlen(var_prefix);
  364. break;
  365. case 'r':
  366. ret = jshn_parse(optarg);
  367. goto exit;
  368. case 'R':
  369. ret = jshn_parse_file(optarg);
  370. goto exit;
  371. case 'w':
  372. ret = jshn_format(no_newline, indent, stdout);
  373. goto exit;
  374. case 'o':
  375. ret = jshn_format_file(optarg, no_newline, indent);
  376. goto exit;
  377. case 'n':
  378. no_newline = true;
  379. break;
  380. case 'i':
  381. indent = true;
  382. break;
  383. default:
  384. free(vars);
  385. return usage(argv[0]);
  386. }
  387. }
  388. free(vars);
  389. return usage(argv[0]);
  390. exit:
  391. free(vars);
  392. return ret;
  393. }