diff options
author | Ron Yorston <rmy@pobox.com> | 2023-02-21 14:15:03 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-02-21 14:15:03 +0000 |
commit | 3cf3b5d29e68d5f85034b6fdc8d1fc2151048197 (patch) | |
tree | 7284315ecba914b94f0e9149d62e1ac72944147b | |
parent | 665f3ac4ad7e5f32b88860f413fdb9fafd0eb3e5 (diff) | |
download | busybox-w32-3cf3b5d29e68d5f85034b6fdc8d1fc2151048197.tar.gz busybox-w32-3cf3b5d29e68d5f85034b6fdc8d1fc2151048197.tar.bz2 busybox-w32-3cf3b5d29e68d5f85034b6fdc8d1fc2151048197.zip |
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)
-rw-r--r-- | win32/winansi.c | 18 |
1 files 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) | |||
76 | static int skip = -1; | 76 | static int skip = -1; |
77 | 77 | ||
78 | if (skip < 0 || reset) { | 78 | if (skip < 0 || reset) { |
79 | HANDLE h; | ||
80 | DWORD oldmode, newmode; | ||
79 | const char *var = getenv(BB_SKIP_ANSI_EMULATION); | 81 | const char *var = getenv(BB_SKIP_ANSI_EMULATION); |
82 | |||
80 | if (var) { | 83 | if (var) { |
81 | skip = atoi(var); | 84 | skip = atoi(var); |
82 | if (skip < 0 || skip > 2) | 85 | if (skip < 0 || skip > 2) |
@@ -87,11 +90,10 @@ int skip_ansi_emulation(int reset) | |||
87 | } | 90 | } |
88 | 91 | ||
89 | if (is_console(STDOUT_FILENO)) { | 92 | if (is_console(STDOUT_FILENO)) { |
90 | HANDLE h = get_console(); | 93 | h = get_console(); |
91 | DWORD oldmode, newmode; | ||
92 | |||
93 | if (GetConsoleMode(h, &oldmode)) { | 94 | if (GetConsoleMode(h, &oldmode)) { |
94 | newmode = oldmode; | 95 | // Try to recover from mode 0 induced by SSH. |
96 | newmode = oldmode == 0 ? 3 : oldmode; | ||
95 | if (skip) | 97 | if (skip) |
96 | newmode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; | 98 | newmode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; |
97 | else | 99 | else |
@@ -103,6 +105,14 @@ int skip_ansi_emulation(int reset) | |||
103 | } | 105 | } |
104 | } | 106 | } |
105 | } | 107 | } |
108 | |||
109 | // Try to recover from mode 0 induced by SSH. | ||
110 | if (reset && is_console_in(STDIN_FILENO)) { | ||
111 | h = GetStdHandle(STD_INPUT_HANDLE); | ||
112 | if (GetConsoleMode(h, &oldmode) && oldmode == 0) { | ||
113 | SetConsoleMode(h, 0x1f7); | ||
114 | } | ||
115 | } | ||
106 | } | 116 | } |
107 | 117 | ||
108 | return skip; | 118 | return skip; |