From 3cf3b5d29e68d5f85034b6fdc8d1fc2151048197 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 21 Feb 2023 14:15:03 +0000 Subject: win32: try to recover from strange console mode Execute a remote command on a Windows server using ssh: ~$ ssh localhost 'echo hello' rmy@localhost's password: hello If such a command is run from a busybox-w32 shell the console enters a strange mode when the command completes. Attempt to recover by setting the console back to a default mode. - This doesn't work in a legacy console. - The new mode may not be _exactly_ correct, but it's better than how ssh leaves it. Costs 80-96 bytes. (GitHub issue #288) --- win32/winansi.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/win32/winansi.c b/win32/winansi.c index 15f443f4b..ef566684e 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -76,7 +76,10 @@ int skip_ansi_emulation(int reset) static int skip = -1; if (skip < 0 || reset) { + HANDLE h; + DWORD oldmode, newmode; const char *var = getenv(BB_SKIP_ANSI_EMULATION); + if (var) { skip = atoi(var); if (skip < 0 || skip > 2) @@ -87,11 +90,10 @@ int skip_ansi_emulation(int reset) } if (is_console(STDOUT_FILENO)) { - HANDLE h = get_console(); - DWORD oldmode, newmode; - + h = get_console(); if (GetConsoleMode(h, &oldmode)) { - newmode = oldmode; + // Try to recover from mode 0 induced by SSH. + newmode = oldmode == 0 ? 3 : oldmode; if (skip) newmode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; else @@ -103,6 +105,14 @@ int skip_ansi_emulation(int reset) } } } + + // Try to recover from mode 0 induced by SSH. + if (reset && is_console_in(STDIN_FILENO)) { + h = GetStdHandle(STD_INPUT_HANDLE); + if (GetConsoleMode(h, &oldmode) && oldmode == 0) { + SetConsoleMode(h, 0x1f7); + } + } } return skip; -- cgit v1.2.3-55-g6feb