1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-24 17:18:48 -05:00

curl: fixed UTF-8 in current console code page (Windows)

Fixes #3211
Fixes #3175
Closes #3212
This commit is contained in:
Yasuhiro Matsumoto 2018-11-02 01:04:39 +09:00 committed by Daniel Stenberg
parent 29c05ce9c3
commit 5bfaa86ceb
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
2 changed files with 31 additions and 7 deletions

View File

@ -156,6 +156,35 @@ size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
} }
} }
#ifdef _WIN32
if(isatty(fileno(outs->stream))) {
DWORD in_len = (DWORD)(sz * nmemb);
wchar_t* wc_buf;
DWORD wc_len;
/* calculate buffer size for wide characters */
wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len, NULL, 0);
wc_buf = (wchar_t*) malloc(wc_len * sizeof(wchar_t));
if(!wc_buf)
return failure;
/* calculate buffer size for multi-byte characters */
wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len, wc_buf, wc_len);
if(!WriteConsoleW(
(HANDLE) _get_osfhandle(fileno(outs->stream)),
wc_buf,
wc_len,
&wc_len,
NULL)) {
free(wc_buf);
return failure;
}
free(wc_buf);
rc = bytes;
}
else
#endif
rc = fwrite(buffer, sz, nmemb, outs->stream); rc = fwrite(buffer, sz, nmemb, outs->stream);
if(bytes == rc) if(bytes == rc)

View File

@ -241,13 +241,12 @@ static void main_free(struct GlobalConfig *config)
static struct TerminalSettings { static struct TerminalSettings {
HANDLE hStdOut; HANDLE hStdOut;
DWORD dwOutputMode; DWORD dwOutputMode;
UINT nCodepage;
} TerminalSettings; } TerminalSettings;
static void configure_terminal(void) static void configure_terminal(void)
{ {
/* /*
* If we're running Windows, enable VT output & set codepage to UTF-8. * If we're running Windows, enable VT output.
* Note: VT mode flag can be set on any version of Windows, but VT * Note: VT mode flag can be set on any version of Windows, but VT
* processing only performed on Win10 >= Creators Update) * processing only performed on Win10 >= Creators Update)
*/ */
@ -257,10 +256,7 @@ static void configure_terminal(void)
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif #endif
/* Cache current codepage (will restore on exit) & set codepage to UTF-8 */
memset(&TerminalSettings, 0, sizeof(TerminalSettings)); memset(&TerminalSettings, 0, sizeof(TerminalSettings));
TerminalSettings.nCodepage = GetConsoleOutputCP();
SetConsoleOutputCP(65001);
/* Enable VT output */ /* Enable VT output */
TerminalSettings.hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); TerminalSettings.hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
@ -282,7 +278,6 @@ static void restore_terminal(void)
/* Restore Console output mode and codepage to whatever they were /* Restore Console output mode and codepage to whatever they were
* when Curl started */ * when Curl started */
SetConsoleMode(TerminalSettings.hStdOut, TerminalSettings.dwOutputMode); SetConsoleMode(TerminalSettings.hStdOut, TerminalSettings.dwOutputMode);
SetConsoleOutputCP(TerminalSettings.nCodepage);
#endif #endif
} }