You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_parse.c 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <getopt.h>
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include "apps_config.h"
  11. /* XXX for a regular program, these should be <json-c/foo.h>
  12. * but that's inconvenient when building in the json-c source tree.
  13. */
  14. #include "json_object.h"
  15. #include "json_tokener.h"
  16. #include "json_util.h"
  17. #ifdef HAVE_SYS_RESOURCE_H
  18. #include <sys/resource.h>
  19. #include <sys/time.h>
  20. #endif
  21. #ifndef JSON_NORETURN
  22. #if defined(_MSC_VER)
  23. #define JSON_NORETURN __declspec(noreturn)
  24. #elif defined(__OS400__)
  25. #define JSON_NORETURN
  26. #else
  27. /* 'cold' attribute is for optimization, telling the computer this code
  28. * path is unlikely.
  29. */
  30. #define JSON_NORETURN __attribute__((noreturn, cold))
  31. #endif
  32. #endif
  33. static int formatted_output = JSON_C_TO_STRING_SPACED;
  34. static int show_output = 1;
  35. static int strict_mode = 0;
  36. static int validate_utf8 = 0;
  37. static int tokener_flags = 0;
  38. static int color = 0;
  39. static const char *fname = NULL;
  40. #ifndef HAVE_JSON_TOKENER_GET_PARSE_END
  41. #define json_tokener_get_parse_end(tok) ((tok)->char_offset)
  42. #endif
  43. JSON_NORETURN static void usage(const char *argv0, int exitval, const char *errmsg);
  44. static void showmem(void);
  45. static int parseit(int fd, int (*callback)(struct json_object *));
  46. static int showobj(struct json_object *new_obj);
  47. static void showmem(void)
  48. {
  49. #ifdef HAVE_GETRUSAGE
  50. struct rusage rusage;
  51. memset(&rusage, 0, sizeof(rusage));
  52. getrusage(RUSAGE_SELF, &rusage);
  53. fprintf(stderr, "maxrss: %ld KB\n", rusage.ru_maxrss);
  54. #endif
  55. }
  56. static int parseit(int fd, int (*callback)(struct json_object *))
  57. {
  58. struct json_object *obj;
  59. char buf[32768];
  60. ssize_t ret;
  61. int depth = JSON_TOKENER_DEFAULT_DEPTH;
  62. json_tokener *tok;
  63. tok = json_tokener_new_ex(depth);
  64. if (!tok)
  65. {
  66. fprintf(stderr, "unable to allocate json_tokener: %s\n", strerror(errno));
  67. return 1;
  68. }
  69. if (strict_mode)
  70. {
  71. tokener_flags |= JSON_TOKENER_STRICT;
  72. #ifdef JSON_TOKENER_ALLOW_TRAILING_CHARS
  73. tokener_flags |= JSON_TOKENER_ALLOW_TRAILING_CHARS;
  74. #endif
  75. }
  76. if (validate_utf8)
  77. {
  78. tokener_flags |= JSON_TOKENER_VALIDATE_UTF8;
  79. }
  80. if (tokener_flags)
  81. {
  82. json_tokener_set_flags(tok, tokener_flags);
  83. }
  84. // XXX push this into some kind of json_tokener_parse_fd API?
  85. // json_object_from_fd isn't flexible enough, and mirroring
  86. // everything you can do with a tokener into json_util.c seems
  87. // like the wrong approach.
  88. size_t total_read = 0;
  89. while ((ret = read(fd, buf, sizeof(buf))) > 0)
  90. {
  91. size_t retu = (size_t)ret; // We know it's positive
  92. total_read += retu;
  93. size_t start_pos = 0;
  94. while (start_pos != retu)
  95. {
  96. obj = json_tokener_parse_ex(tok, &buf[start_pos], retu - start_pos);
  97. enum json_tokener_error jerr = json_tokener_get_error(tok);
  98. size_t parse_end = json_tokener_get_parse_end(tok);
  99. if (obj == NULL && jerr != json_tokener_continue)
  100. {
  101. const char *aterr = (start_pos + parse_end < (int)sizeof(buf)) ?
  102. &buf[start_pos + parse_end] : "";
  103. fflush(stdout);
  104. size_t fail_offset = total_read - retu + start_pos + parse_end;
  105. fprintf(stderr, "Failed at offset %lu: %s %c\n", (unsigned long)fail_offset,
  106. json_tokener_error_desc(jerr), aterr[0]);
  107. json_tokener_free(tok);
  108. return 1;
  109. }
  110. if (obj != NULL)
  111. {
  112. int cb_ret = callback(obj);
  113. json_object_put(obj);
  114. if (cb_ret != 0)
  115. {
  116. json_tokener_free(tok);
  117. return 1;
  118. }
  119. }
  120. start_pos += json_tokener_get_parse_end(tok);
  121. assert(start_pos <= retu);
  122. }
  123. }
  124. if (ret < 0)
  125. {
  126. fprintf(stderr, "error reading fd %d: %s\n", fd, strerror(errno));
  127. }
  128. json_tokener_free(tok);
  129. return 0;
  130. }
  131. static int showobj(struct json_object *new_obj)
  132. {
  133. if (new_obj == NULL)
  134. {
  135. fprintf(stderr, "%s: Failed to parse\n", fname);
  136. return 1;
  137. }
  138. fprintf(stderr, "Successfully parsed object from %s\n", fname);
  139. if (show_output)
  140. {
  141. const char *output;
  142. output = json_object_to_json_string_ext(new_obj, formatted_output | color);
  143. printf("%s\n", output);
  144. }
  145. showmem();
  146. return 0;
  147. }
  148. static void usage(const char *argv0, int exitval, const char *errmsg)
  149. {
  150. FILE *fp = stdout;
  151. if (exitval != 0)
  152. fp = stderr;
  153. if (errmsg != NULL)
  154. fprintf(fp, "ERROR: %s\n\n", errmsg);
  155. fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s] [-u] [filename]\n", argv0);
  156. fprintf(fp, " -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
  157. fprintf(fp, " -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
  158. fprintf(fp, " -n - No output\n");
  159. fprintf(fp, " -c - Set JSON_C_TO_STRING_COLOR to colorize the output\n");
  160. fprintf(fp, " -P - Initialize tokener flags to the given value\n");
  161. fprintf(fp, " -s - Parse in strict mode, add flags:\n");
  162. fprintf(fp, " JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
  163. fprintf(fp, " -u - Add the JSON_TOKENER_VALIDATE_UTF8 flag when parsing\n");
  164. fprintf(fp, " Diagnostic information will be emitted to stderr\n");
  165. fprintf(fp, "\nWARNING WARNING WARNING\n");
  166. fprintf(fp, "This is a prototype, it may change or be removed at any time!\n");
  167. exit(exitval);
  168. }
  169. int main(int argc, char **argv)
  170. {
  171. int opt;
  172. while ((opt = getopt(argc, argv, "cfF:hnP:su")) != -1)
  173. {
  174. switch (opt)
  175. {
  176. case 'c': color = JSON_C_TO_STRING_COLOR; break;
  177. case 'f': formatted_output = JSON_C_TO_STRING_PRETTY; break;
  178. case 'F': formatted_output = atoi(optarg); break;
  179. case 'n': show_output = 0; break;
  180. case 'P': tokener_flags = atoi(optarg); break;
  181. case 's': strict_mode = 1; break;
  182. case 'u': validate_utf8 = 1; break;
  183. case 'h': usage(argv[0], 0, NULL);
  184. default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
  185. }
  186. }
  187. int fd = STDIN_FILENO;
  188. fname = "stdin";
  189. if (argc > optind && strcmp(argv[optind], "-") != 0)
  190. {
  191. fname = argv[optind];
  192. fd = open(fname, O_RDONLY, 0);
  193. }
  194. showmem();
  195. if (parseit(fd, showobj) != 0)
  196. exit(EXIT_FAILURE);
  197. showmem();
  198. exit(EXIT_SUCCESS);
  199. }