From cb50586aedc9725fb14dbb25263bba1598726037 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 17 Mar 2023 13:20:53 +0000 Subject: win32: delay adjusting code page Commit 93a63809f9 (win32: add support for the euro currency symbol) caused all invocations of busybox-w32 to change code page 850 to 858. This has been known to cause problems with fonts in PowerShell (GitHub issue #207). Delay changing the code page until an i/o operation is imminent. Instances of PowerShell started by the `drop` applet during ssh login thus no longer have their code page adjusted. --- include/mingw.h | 1 - libbb/appletlib.c | 4 +--- win32/winansi.c | 13 ++++++++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index 0ccced2a3..a94d90767 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -554,7 +554,6 @@ void qsort_string_vector_case(char **sv, unsigned count) FAST_FUNC; const char *get_busybox_exec_path(void); void init_winsock(void); -void init_codepage(void); int has_bat_suffix(const char *p); int has_exe_suffix(const char *p); diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 504c1f634..dacfbdd22 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -1290,9 +1290,7 @@ int main(int argc UNUSED_PARAM, char **argv) break; } } -# if ENABLE_FEATURE_EURO - init_codepage(); -# endif + /* Ignore critical errors, such as calling GetVolumeInformation() on * a floppy or CDROM drive with no media. */ SetErrorMode(SEM_FAILCRITICALERRORS); diff --git a/win32/winansi.c b/win32/winansi.c index de6db08e2..dbdba9626 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -29,6 +29,10 @@ static WORD plain_attr = 0xffff; static WORD current_attr; +#if ENABLE_FEATURE_EURO +static void init_codepage(void); +#endif + static HANDLE get_console(void) { return GetStdHandle(STD_OUTPUT_HANDLE); @@ -75,6 +79,11 @@ int terminal_mode(int reset) { static int mode = -1; +#if ENABLE_FEATURE_EURO + if (mode < 0) + init_codepage(); +#endif + if (mode < 0 || reset) { HANDLE h; DWORD oldmode, newmode; @@ -699,7 +708,7 @@ static char *process_escape(char *pos) } #if ENABLE_FEATURE_EURO -void init_codepage(void) +static void init_codepage(void) { if (GetConsoleCP() == 850 && GetConsoleOutputCP() == 850) { SetConsoleCP(858); @@ -715,6 +724,7 @@ static BOOL winansi_CharToOemBuff(LPCSTR s, LPSTR d, DWORD len) if (!s || !d) return FALSE; + terminal_mode(FALSE); buf = xmalloc(len*sizeof(WCHAR)); MultiByteToWideChar(CP_ACP, 0, s, len, buf, len); WideCharToMultiByte(CP_OEMCP, 0, buf, len, d, len, NULL, NULL); @@ -744,6 +754,7 @@ static BOOL winansi_OemToCharBuff(LPCSTR s, LPSTR d, DWORD len) if (!s || !d) return FALSE; + terminal_mode(FALSE); buf = xmalloc(len*sizeof(WCHAR)); MultiByteToWideChar(CP_OEMCP, 0, s, len, buf, len); WideCharToMultiByte(CP_ACP, 0, buf, len, d, len, NULL, NULL); -- cgit v1.2.3-55-g6feb