Skip to content
Myr

Use WriteConsoleW() in priv_fwrite() too (#3607)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

jq dumps JSON to the Windows console as UTF-16 with WriteConsoleW(), but priv_fwrite() still writes the raw UTF-8 bytes with WriteFile(), which the console decodes with its output code page. So `jq .k` prints non-ASCII correctly while `jq -r .k` prints mojibake, and the same goes for -j, --raw-output0, --seq, halt_error and the `stderr` builtin. The tty flag is computed once, from stdout, and passed to writes aimed at stderr too, so ask the handle instead: with stdout on a console and stderr redirected, WriteConsoleW() failed on the file handle and the output was dropped. One byte-level change: with stdout on a console and stderr redirected, halt_error and the `stderr` builtin now go through the CRT, so bare LF becomes CRLF and an existing CRLF becomes CR CRLF. Use -b for byte-exact output on that stream.

authored by Bumsu Hyeon · committed by GitHub · parent 7422e8c · Browse files · 2 files+39 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+39 added−40 removed

  • other:+39 added−40 removed

2 files+39 added−40 removed

modified:src/jv_print.c+1 added-24 removed
1 unmodified line
2#include <stdio.h>3#include <float.h>4#include <string.h>56#ifdef WIN327#include <windows.h>8#include <io.h>9#include <fileapi.h>10#endif116#include "jv.h"7#include "jv_dtoa.h"8#include "jv_dtoa_tsd.h"9#include "jv_unicode.h"
82 unmodified lines
92static void put_buf(const char *s, int len, FILE *fout, jv *strout, int is_tty) {93 if (strout) {94 *strout = jv_string_append_buf(*strout, s, len);95 } else {102#ifdef WIN32103 /* See util.h */104 if (is_tty) {105 wchar_t *ws;106 size_t wl;96 if (len == -1)97 len = strlen(s);109 wl = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);110 ws = jv_mem_calloc(wl + 1, sizeof(*ws));111 if (!ws)112 return;113 wl = MultiByteToWideChar(CP_UTF8, 0, s, len, ws, wl + 1);114 ws[wl] = 0;115 WriteConsoleW((HANDLE)_get_osfhandle(fileno(fout)), ws, wl, NULL, NULL);116 free(ws);117 } else118 fwrite(s, 1, len, fout);119#else120 fwrite(s, 1, len, fout);121#endif98 priv_fwrite(s, len, fout, is_tty); /* see util.h */99 }100}101102static void put_char(char c, FILE* fout, jv* strout, int T) {
318 unmodified lines
modified:src/util.h+38 added-16 removed
1#ifndef UTIL_H2#define UTIL_H34#ifdef WIN325/* For WriteFile() below */5/* For WriteConsoleW() below */6#include <windows.h>7#include <io.h>8#include <limits.h>9#include <processenv.h>10#include <shellapi.h>11#include <stdlib.h>12#include <wchar.h>13#include <wtypes.h>14#endif15
3 unmodified lines
19jv get_home(void);20jv jq_realpath(jv);2122/*21 * The Windows CRT and console are something else. In order for the22 * console to get UTF-8 written to it correctly we have to bypass stdio23 * completely. No amount of fflush()ing helps. If the first byte of a24 * buffer being written with fwrite() is non-ASCII UTF-8 then the25 * console misinterprets the byte sequence. But one must not26 * WriteFile() if stdout is a file!1!!23 * The Windows console does not do UTF-8. Bytes written to it, with24 * fwrite() or with WriteFile() alike, are decoded with the console's25 * output code page, so anything but ASCII comes out as mojibake unless26 * the user happened to run `chcp 65001` first. The wide character27 * console API is code page independent, so convert to UTF-16 and use28 * WriteConsoleW() instead. We must not call SetConsoleOutputCP(): the29 * code page belongs to the console rather than to us, so changing it30 * also changes the shell we were started from (see #1121 and #1184).31 *28 * We carry knowledge of whether the FILE * is a tty everywhere we29 * output to it just so we can write with WriteFile() if stdout is a30 * console on WIN32.32 * Only a console needs this; a file or a pipe wants the UTF-8 bytes.33 * The is_tty flag alone cannot tell the two apart, as it is computed34 * once, from stdout, and passed along even for writes to stderr, so35 * ask the handle we are about to write to.36 */3738static void priv_fwrite(const char *s, size_t len, FILE *fout, int is_tty) {39#ifdef WIN3235 if (is_tty)36 WriteFile((HANDLE)_get_osfhandle(fileno(fout)), s, len, NULL, NULL);37 else38 fwrite(s, 1, len, fout);39#else40 fwrite(s, 1, len, fout);40 if (is_tty && len > 0 && len <= INT_MAX / sizeof(wchar_t)) {41 HANDLE h = (HANDLE)_get_osfhandle(fileno(fout));42 DWORD mode;43 if (GetConsoleMode(h, &mode)) {44 int wlen = MultiByteToWideChar(CP_UTF8, 0, s, (int)len, NULL, 0);45 wchar_t *ws = wlen > 0 ? malloc(wlen * sizeof(*ws)) : NULL;46 if (ws != NULL) {47 wlen = MultiByteToWideChar(CP_UTF8, 0, s, (int)len, ws, wlen);48 fflush(fout); /* keep our order with stdio writes to this stream */49 for (int done = 0; done < wlen;) {50 DWORD written;51 /* WriteConsoleW() may write fewer characters than asked for. */52 if (!WriteConsoleW(h, ws + done, wlen - done, &written, NULL) ||53 written == 0)54 break;55 done += written;56 }57 free(ws);58 return;59 }60 }61 }62#endif63 fwrite(s, 1, len, fout);64}6566const void *_jq_memmem(const void *haystack, size_t haystacklen,67 const void *needle, size_t needlelen);
21 unmodified lines