util.c (jq-1.5) | : | util.c (jq-1.6) | ||
---|---|---|---|---|
skipping to change at line 49 | skipping to change at line 49 | |||
#include <wchar.h> | #include <wchar.h> | |||
#include <wtypes.h> | #include <wtypes.h> | |||
#endif | #endif | |||
#include "util.h" | #include "util.h" | |||
#include "jq.h" | #include "jq.h" | |||
#include "jv_alloc.h" | #include "jv_alloc.h" | |||
#ifdef WIN32 | #ifdef WIN32 | |||
FILE *fopen(const char *fname, const char *mode) { | FILE *fopen(const char *fname, const char *mode) { | |||
size_t sz = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0); | size_t sz = sizeof(wchar_t) * MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, | |||
wchar_t *wfname = alloca(sz); | 0); | |||
wchar_t *wfname = alloca(sz + 2); // +2 is not needed, but just in case | ||||
MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname, sz); | MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname, sz); | |||
sz = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); | sz = sizeof(wchar_t) * MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); | |||
wchar_t *wmode = alloca(sz); | wchar_t *wmode = alloca(sz + 2); // +2 is not needed, but just in case | |||
MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, sz); | MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, sz); | |||
return _wfopen(wfname, wmode); | return _wfopen(wfname, wmode); | |||
} | } | |||
#endif | #endif | |||
#ifndef HAVE_MKSTEMP | #ifndef HAVE_MKSTEMP | |||
int mkstemp(char *template) { | int mkstemp(char *template) { | |||
size_t len = strlen(template); | size_t len = strlen(template); | |||
int tries=5; | int tries=5; | |||
int fd; | int fd; | |||
skipping to change at line 140 | skipping to change at line 140 | |||
jv jq_realpath(jv path) { | jv jq_realpath(jv path) { | |||
int path_max; | int path_max; | |||
char *buf = NULL; | char *buf = NULL; | |||
#ifdef _PC_PATH_MAX | #ifdef _PC_PATH_MAX | |||
path_max = pathconf(jv_string_value(path),_PC_PATH_MAX); | path_max = pathconf(jv_string_value(path),_PC_PATH_MAX); | |||
#else | #else | |||
path_max = PATH_MAX; | path_max = PATH_MAX; | |||
#endif | #endif | |||
if (path_max > 0) { | if (path_max > 0) { | |||
buf = malloc(sizeof(char) * path_max); | buf = jv_mem_alloc(path_max); | |||
} | } | |||
#ifdef WIN32 | #ifdef WIN32 | |||
char *tmp = _fullpath(buf, jv_string_value(path), path_max); | char *tmp = _fullpath(buf, jv_string_value(path), path_max); | |||
#else | #else | |||
char *tmp = realpath(jv_string_value(path), buf); | char *tmp = realpath(jv_string_value(path), buf); | |||
#endif | #endif | |||
if (tmp == NULL) { | if (tmp == NULL) { | |||
free(buf); | free(buf); | |||
return path; | return path; | |||
} | } | |||
skipping to change at line 410 | skipping to change at line 410 | |||
return jv_invalid_with_msg(jv_string("Unknown input line number")); | return jv_invalid_with_msg(jv_string("Unknown input line number")); | |||
jq_util_input_state *s = (jq_util_input_state *)cb_data; | jq_util_input_state *s = (jq_util_input_state *)cb_data; | |||
jv v = jv_number(s->current_line); | jv v = jv_number(s->current_line); | |||
return v; | return v; | |||
} | } | |||
// Blocks to read one more input from stdin and/or given files | // Blocks to read one more input from stdin and/or given files | |||
// When slurping, it returns just one value | // When slurping, it returns just one value | |||
jv jq_util_input_next_input(jq_util_input_state *state) { | jv jq_util_input_next_input(jq_util_input_state *state) { | |||
int is_last = 0; | int is_last = 0; | |||
int has_more = 0; | ||||
jv value = jv_invalid(); // need more input | jv value = jv_invalid(); // need more input | |||
do { | do { | |||
if (state->parser == NULL) { | if (state->parser == NULL) { | |||
// Raw input | // Raw input | |||
is_last = jq_util_input_read_more(state); | is_last = jq_util_input_read_more(state); | |||
if (state->buf_valid_len == 0) | if (state->buf_valid_len == 0) | |||
continue; | continue; | |||
if (jv_is_valid(state->slurped)) { | if (jv_is_valid(state->slurped)) { | |||
// Slurped raw input | // Slurped raw input | |||
state->slurped = jv_string_concat(state->slurped, jv_string_sized(state- >buf, state->buf_valid_len)); | state->slurped = jv_string_concat(state->slurped, jv_string_sized(state- >buf, state->buf_valid_len)); | |||
skipping to change at line 435 | skipping to change at line 436 | |||
state->buf[state->buf_valid_len-1] = 0; | state->buf[state->buf_valid_len-1] = 0; | |||
return jv_string_concat(value, jv_string_sized(state->buf, state->buf_ valid_len-1)); | return jv_string_concat(value, jv_string_sized(state->buf, state->buf_ valid_len-1)); | |||
} | } | |||
value = jv_string_concat(value, jv_string_sized(state->buf, state->buf_v alid_len)); | value = jv_string_concat(value, jv_string_sized(state->buf, state->buf_v alid_len)); | |||
state->buf[0] = '\0'; | state->buf[0] = '\0'; | |||
state->buf_valid_len = 0; | state->buf_valid_len = 0; | |||
} | } | |||
} else { | } else { | |||
if (jv_parser_remaining(state->parser) == 0) { | if (jv_parser_remaining(state->parser) == 0) { | |||
is_last = jq_util_input_read_more(state); | is_last = jq_util_input_read_more(state); | |||
if (is_last && state->buf_valid_len == 0) { | if (is_last && state->buf_valid_len == 0) | |||
value = jv_invalid(); | value = jv_invalid(); | |||
break; | ||||
} | ||||
jv_parser_set_buf(state->parser, state->buf, state->buf_valid_len, !is_l ast); | jv_parser_set_buf(state->parser, state->buf, state->buf_valid_len, !is_l ast); | |||
} | } | |||
value = jv_parser_next(state->parser); | value = jv_parser_next(state->parser); | |||
if (jv_is_valid(state->slurped)) { | if (jv_is_valid(state->slurped)) { | |||
// When slurping an input that doesn't have a trailing newline, | ||||
// we might have more than one value on the same line, so let's check | ||||
// to see if we have more data to parse. | ||||
has_more = jv_parser_remaining(state->parser); | ||||
if (jv_is_valid(value)) { | if (jv_is_valid(value)) { | |||
state->slurped = jv_array_append(state->slurped, value); | state->slurped = jv_array_append(state->slurped, value); | |||
value = jv_invalid(); | value = jv_invalid(); | |||
} else if (jv_invalid_has_msg(jv_copy(value))) | } else if (jv_invalid_has_msg(jv_copy(value))) | |||
return value; // Not slurped parsed input | return value; // Not slurped parsed input | |||
} else if (jv_is_valid(value) || jv_invalid_has_msg(jv_copy(value))) { | } else if (jv_is_valid(value) || jv_invalid_has_msg(jv_copy(value))) { | |||
return value; | return value; | |||
} | } | |||
} | } | |||
} while (!is_last); | } while (!is_last || has_more); | |||
if (jv_is_valid(state->slurped)) { | if (jv_is_valid(state->slurped)) { | |||
value = state->slurped; | value = state->slurped; | |||
state->slurped = jv_invalid(); | state->slurped = jv_invalid(); | |||
} | } | |||
return value; | return value; | |||
} | } | |||
End of changes. 8 change blocks. | ||||
9 lines changed or deleted | 13 lines changed or added |