Fix EOF handling; fix #656
This commit is contained in:
1 parent
e0f9b6a5cd
commit
a55085b662
1 file changed
+19
-21
@@ -153,36 +153,33 @@ FILE* current_input;
|
|||||||
const char** input_filenames = NULL;
|
const char** input_filenames = NULL;
|
||||||
int ninput_files;
|
int ninput_files;
|
||||||
int next_input_idx;
|
int next_input_idx;
|
||||||
static int read_more(char* buf, size_t size, int* islast) {
|
static int read_more(char* buf, size_t size) {
|
||||||
while (!current_input || feof(current_input)) {
|
while (!current_input || feof(current_input)) {
|
||||||
if (current_input) {
|
if (current_input) {
|
||||||
if (current_input == stdin) {
|
if (current_input == stdin) {
|
||||||
clearerr(stdin); // perhaps we can read again
|
clearerr(stdin); // perhaps we can read again; anyways, we don't fclose(stdin)
|
||||||
} else {
|
} else {
|
||||||
fclose(current_input);
|
fclose(current_input);
|
||||||
}
|
}
|
||||||
current_input = 0;
|
current_input = 0;
|
||||||
}
|
}
|
||||||
if (next_input_idx == ninput_files) {
|
if (next_input_idx < ninput_files) {
|
||||||
assert(*islast == 1);
|
if (!strcmp(input_filenames[next_input_idx], "-")) {
|
||||||
return 0;
|
current_input = stdin;
|
||||||
|
} else {
|
||||||
|
current_input = fopen(input_filenames[next_input_idx], "r");
|
||||||
|
if (!current_input)
|
||||||
|
fprintf(stderr, "%s: %s: %s\n", progname, input_filenames[next_input_idx], strerror(errno));
|
||||||
|
}
|
||||||
|
next_input_idx++;
|
||||||
}
|
}
|
||||||
*islast = 0;
|
|
||||||
if (!strcmp(input_filenames[next_input_idx], "-")) {
|
|
||||||
current_input = stdin;
|
|
||||||
} else {
|
|
||||||
current_input = fopen(input_filenames[next_input_idx], "r");
|
|
||||||
}
|
|
||||||
if (!current_input) {
|
|
||||||
fprintf(stderr, "%s: %s: %s\n", progname, input_filenames[next_input_idx], strerror(errno));
|
|
||||||
}
|
|
||||||
next_input_idx++;
|
|
||||||
*islast = (next_input_idx == ninput_files);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fgets(buf, size, current_input)) buf[0] = 0;
|
if (current_input) {
|
||||||
if (!current_input || feof(current_input)) *islast = 1;
|
if (!fgets(buf, size, current_input))
|
||||||
return 1;
|
buf[0] = 0;
|
||||||
|
}
|
||||||
|
return next_input_idx == ninput_files && (!current_input || feof(current_input));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
@@ -493,7 +490,8 @@ int main(int argc, char* argv[]) {
|
|||||||
struct jv_parser* parser = jv_parser_new((options & SEQ) ? JV_PARSE_SEQ : 0);
|
struct jv_parser* parser = jv_parser_new((options & SEQ) ? JV_PARSE_SEQ : 0);
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
int is_last = 0;
|
int is_last = 0;
|
||||||
while (read_more(buf, sizeof(buf), &is_last)) {
|
do {
|
||||||
|
is_last = read_more(buf, sizeof(buf));
|
||||||
if (options & RAW_INPUT) {
|
if (options & RAW_INPUT) {
|
||||||
int len = strlen(buf);
|
int len = strlen(buf);
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
@@ -530,7 +528,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} while (!is_last);
|
||||||
jv_parser_free(parser);
|
jv_parser_free(parser);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
goto out;
|
goto out;
|
||||||
|
|||||||
Reference in new issue
Block a user