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 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = 0;
  34. static int show_output = 1;
  35. static int strict_mode = 0;
  36. static const char *fname = NULL;
  37. #ifndef HAVE_JSON_TOKENER_GET_PARSE_END
  38. #define json_tokener_get_parse_end(tok) ((tok)->char_offset)
  39. #endif
  40. JSON_NORETURN static void usage(const char *argv0, int exitval, const char *errmsg);
  41. static void showmem(void);
  42. static int parseit(int fd, int (*callback)(struct json_object *));
  43. static int showobj(struct json_object *new_obj);
  44. static void showmem(void)
  45. {
  46. #ifdef HAVE_GETRUSAGE
  47. struct rusage rusage;
  48. memset(&rusage, 0, sizeof(rusage));
  49. getrusage(RUSAGE_SELF, &rusage);
  50. printf("maxrss: %ld KB\n", rusage.ru_maxrss);
  51. #endif
  52. }
  53. static int parseit(int fd, int (*callback)(struct json_object *))
  54. {
  55. struct json_object *obj;
  56. char buf[32768];
  57. ssize_t ret;
  58. int depth = JSON_TOKENER_DEFAULT_DEPTH;
  59. json_tokener *tok;
  60. tok = json_tokener_new_ex(depth);
  61. if (!tok)
  62. {
  63. fprintf(stderr, "unable to allocate json_tokener: %s\n", strerror(errno));
  64. return 1;
  65. }
  66. json_tokener_set_flags(tok, JSON_TOKENER_STRICT
  67. #ifdef JSON_TOKENER_ALLOW_TRAILING_CHARS
  68. | JSON_TOKENER_ALLOW_TRAILING_CHARS
  69. #endif
  70. );
  71. // XXX push this into some kind of json_tokener_parse_fd API?
  72. // json_object_from_fd isn't flexible enough, and mirroring
  73. // everything you can do with a tokener into json_util.c seems
  74. // like the wrong approach.
  75. size_t total_read = 0;
  76. while ((ret = read(fd, buf, sizeof(buf))) > 0)
  77. {
  78. size_t retu = (size_t)ret; // We know it's positive
  79. total_read += retu;
  80. size_t start_pos = 0;
  81. while (start_pos != retu)
  82. {
  83. obj = json_tokener_parse_ex(tok, &buf[start_pos], retu - start_pos);
  84. enum json_tokener_error jerr = json_tokener_get_error(tok);
  85. size_t parse_end = json_tokener_get_parse_end(tok);
  86. if (obj == NULL && jerr != json_tokener_continue)
  87. {
  88. const char *aterr = (start_pos + parse_end < (int)sizeof(buf)) ?
  89. &buf[start_pos + parse_end] : "";
  90. fflush(stdout);
  91. size_t fail_offset = total_read - retu + start_pos + parse_end;
  92. fprintf(stderr, "Failed at offset %lu: %s %c\n", (unsigned long)fail_offset,
  93. json_tokener_error_desc(jerr), aterr[0]);
  94. json_tokener_free(tok);
  95. return 1;
  96. }
  97. if (obj != NULL)
  98. {
  99. int cb_ret = callback(obj);
  100. json_object_put(obj);
  101. if (cb_ret != 0)
  102. {
  103. json_tokener_free(tok);
  104. return 1;
  105. }
  106. }
  107. start_pos += json_tokener_get_parse_end(tok);
  108. assert(start_pos <= retu);
  109. }
  110. }
  111. if (ret < 0)
  112. {
  113. fprintf(stderr, "error reading fd %d: %s\n", fd, strerror(errno));
  114. }
  115. json_tokener_free(tok);
  116. return 0;
  117. }
  118. static int showobj(struct json_object *new_obj)
  119. {
  120. if (new_obj == NULL)
  121. {
  122. fprintf(stderr, "%s: Failed to parse\n", fname);
  123. return 1;
  124. }
  125. printf("Successfully parsed object from %s\n", fname);
  126. if (show_output)
  127. {
  128. const char *output;
  129. if (formatted_output)
  130. output = json_object_to_json_string(new_obj);
  131. else
  132. output = json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_PRETTY);
  133. printf("%s\n", output);
  134. }
  135. showmem();
  136. return 0;
  137. }
  138. static void usage(const char *argv0, int exitval, const char *errmsg)
  139. {
  140. FILE *fp = stdout;
  141. if (exitval != 0)
  142. fp = stderr;
  143. if (errmsg != NULL)
  144. fprintf(fp, "ERROR: %s\n\n", errmsg);
  145. fprintf(fp, "Usage: %s [-f] [-n] [-s]\n", argv0);
  146. fprintf(fp, " -f - Format the output with JSON_C_TO_STRING_PRETTY\n");
  147. fprintf(fp, " -n - No output\n");
  148. fprintf(fp, " -s - Parse in strict mode, flags:\n");
  149. fprintf(fp, " JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
  150. fprintf(fp, "\nWARNING WARNING WARNING\n");
  151. fprintf(fp, "This is a prototype, it may change or be removed at any time!\n");
  152. exit(exitval);
  153. }
  154. int main(int argc, char **argv)
  155. {
  156. int opt;
  157. while ((opt = getopt(argc, argv, "fhns")) != -1)
  158. {
  159. switch (opt)
  160. {
  161. case 'f': formatted_output = 1; break;
  162. case 'n': show_output = 0; break;
  163. case 's': strict_mode = 1; break;
  164. case 'h': usage(argv[0], 0, NULL);
  165. default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
  166. }
  167. }
  168. if (optind >= argc)
  169. {
  170. usage(argv[0], EXIT_FAILURE, "Expected argument after options");
  171. }
  172. fname = argv[optind];
  173. int fd = open(argv[optind], O_RDONLY, 0);
  174. showmem();
  175. if (parseit(fd, showobj) != 0)
  176. exit(EXIT_FAILURE);
  177. showmem();
  178. exit(EXIT_SUCCESS);
  179. }