From 505e23124f76ae78a0cb203d3960ec398386c201 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Fri, 26 Jun 2015 21:24:01 -0500 Subject: [PATCH] Fix #830 --- jv_print.c | 25 +------------------------ util.h | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/jv_print.c b/jv_print.c index 0f6d624d..227de1f1 100644 --- a/jv_print.c +++ b/jv_print.c @@ -27,35 +27,12 @@ static const char* const colours[] = COL("0;32"), COL("1;39"), COL("1;39")}; #define FIELD_COLOUR COL("34;1") -/* - * The Windows CRT and console are something else. In order for the - * console to get UTF-8 written to it correctly we have to bypass stdio - * completely. No amount of fflush()ing helps. If the first byte of a - * buffer being written with fwrite() is non-ASCII UTF-8 then the - * console misinterprets the byte sequence. But one must not - * WriteFile() if stdout is a file!1!! - * - * We carry knowledge of whether the FILE * is a tty everywhere we - * output to it just so we can write with WriteFile() if stdout is a - * console on WIN32. - */ - -void priv_fwrite(const char *s, size_t len, FILE *fout, int is_tty) { -#ifdef WIN32 - if (is_tty) - WriteFile((HANDLE)_get_osfhandle(fileno(fout)), s, len, NULL, NULL); - else - fwrite(s, 1, len, fout); -#else - fwrite(s, 1, len, fout); -#endif -} - static void put_buf(const char *s, int len, FILE *fout, jv *strout, int is_tty) { if (strout) { *strout = jv_string_append_buf(*strout, s, len); } else { #ifdef WIN32 + /* See util.h */ if (is_tty) WriteFile((HANDLE)_get_osfhandle(fileno(fout)), s, len, NULL, NULL); else diff --git a/util.h b/util.h index 14eb1353..ceccb087 100644 --- a/util.h +++ b/util.h @@ -11,7 +11,29 @@ jv expand_path(jv); jv get_home(void); jv jq_realpath(jv); -void priv_fwrite(const char *, size_t, FILE *, int); +/* + * The Windows CRT and console are something else. In order for the + * console to get UTF-8 written to it correctly we have to bypass stdio + * completely. No amount of fflush()ing helps. If the first byte of a + * buffer being written with fwrite() is non-ASCII UTF-8 then the + * console misinterprets the byte sequence. But one must not + * WriteFile() if stdout is a file!1!! + * + * We carry knowledge of whether the FILE * is a tty everywhere we + * output to it just so we can write with WriteFile() if stdout is a + * console on WIN32. + */ + +static void priv_fwrite(const char *s, size_t len, FILE *fout, int is_tty) { +#ifdef WIN32 + if (is_tty) + WriteFile((HANDLE)_get_osfhandle(fileno(fout)), s, len, NULL, NULL); + else + fwrite(s, 1, len, fout); +#else + fwrite(s, 1, len, fout); +#endif +} const void *_jq_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);