diff options
author | William Langford <wlangfor@gmail.com> | 2014-08-28 21:51:33 -0400 |
---|---|---|
committer | William Langford <wlangfor@gmail.com> | 2014-08-28 21:52:45 -0400 |
commit | d177944b75fc7fd116c09d90d71e6fcbd1c2ed52 (patch) | |
tree | 1395d789d398867fc96c1c0a5dd431145f464e41 | |
parent | 9bd55487ea6dca1f4c263ce4c3348c496a2179af (diff) |
Properly handle incomplete json when input is file
Fix #562
-rw-r--r-- | main.c | 8 |
1 files changed, 5 insertions, 3 deletions
@@ -148,7 +148,7 @@ FILE* current_input; const char** input_filenames = NULL; int ninput_files; int next_input_idx; -static int read_more(char* buf, size_t size) { +static int read_more(char* buf, size_t size, int* islast) { while (!current_input || feof(current_input)) { if (current_input) { fclose(current_input); @@ -168,6 +168,7 @@ static int read_more(char* buf, size_t size) { next_input_idx++; } + if (next_input_idx == ninput_files) *islast = 1; if (!fgets(buf, size, current_input)) buf[0] = 0; return 1; } @@ -439,7 +440,8 @@ int main(int argc, char* argv[]) { } struct jv_parser* parser = jv_parser_new(0); char buf[4096]; - while (read_more(buf, sizeof(buf))) { + int is_last = 0; + while (read_more(buf, sizeof(buf), &is_last)) { if (options & RAW_INPUT) { int len = strlen(buf); if (len > 0) { @@ -451,7 +453,7 @@ int main(int argc, char* argv[]) { } } } else { - jv_parser_set_buf(parser, buf, strlen(buf), !feof(stdin)); + jv_parser_set_buf(parser, buf, strlen(buf), !(is_last || feof(stdin))); jv value; while (jv_is_valid((value = jv_parser_next(parser)))) { if (options & SLURP) { |