From b50470de96320425adbed82129bdb7f7f5263ddb Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 6 Mar 2023 12:25:39 +0000 Subject: win32: virtual terminal input fixes - Disable ENABLE_PROCESSED_INPUT in raw mode. Otherwise ^C isn't immediately detected during shell command line editing with virtual terminal input enabled. - Switch read_key()/unix_readkey() to windows_read_key()/read_key(). This allows the shell `read` builtin to use windows_read_key(). Without this change `read` fails when virtual terminal input is enabled. --- include/libbb.h | 2 +- libbb/read_key.c | 9 +++++---- libbb/xfuncs.c | 3 ++- shell/shell_common.c | 2 +- win32/termios.c | 5 +---- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index c1d4d11c1..4276bae61 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1976,7 +1976,7 @@ enum { */ int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC; #if ENABLE_PLATFORM_MINGW32 -int64_t unix_read_key(int fd, char *buffer, int timeout) FAST_FUNC; +int64_t windows_read_key(int fd, char *buffer, int timeout) FAST_FUNC; #endif /* This version loops on EINTR: */ int64_t safe_read_key(int fd, char *buffer, int timeout) FAST_FUNC; diff --git a/libbb/read_key.c b/libbb/read_key.c index c34770028..54886cc9c 100644 --- a/libbb/read_key.c +++ b/libbb/read_key.c @@ -9,11 +9,7 @@ */ #include "libbb.h" -#if ENABLE_PLATFORM_MINGW32 -int64_t FAST_FUNC unix_read_key(int fd, char *buffer, int timeout) -#else int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout) -#endif { struct pollfd pfd; const char *seq; @@ -116,6 +112,11 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout) 0 }; +#if ENABLE_PLATFORM_MINGW32 + if (!(terminal_mode(FALSE) & VT_INPUT)) + return windows_read_key(fd, buffer, timeout); +#endif + pfd.fd = fd; pfd.events = POLLIN; diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index bf15f4c75..cbeb24701 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -326,7 +326,8 @@ int FAST_FUNC get_termios_and_make_raw(int fd, struct termios *newterm, struct t *newterm = *oldterm; #if ENABLE_PLATFORM_MINGW32 - newterm->imode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT); + newterm->imode &= + ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); #else /* Turn off buffered input (ICANON) * Turn off echoing (ECHO) diff --git a/shell/shell_common.c b/shell/shell_common.c index 9d74dcb7a..efe8828e4 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -223,7 +223,7 @@ shell_builtin_read(struct builtin_read_params *params) if (isatty(fd)) { int64_t key; - key = read_key(fd, NULL, timeout); + key = windows_read_key(fd, NULL, timeout); if (key == 0x03) { /* ^C pressed */ retval = (const char *)(uintptr_t)2; diff --git a/win32/termios.c b/win32/termios.c index 20ec4dcc5..9fa97685d 100644 --- a/win32/termios.c +++ b/win32/termios.c @@ -28,7 +28,7 @@ int tcgetattr(int fd, struct termios *t) return 0; } -int64_t FAST_FUNC read_key(int fd, char *buf, int timeout) +int64_t FAST_FUNC windows_read_key(int fd, char *buf UNUSED_PARAM, int timeout) { HANDLE cin = GetStdHandle(STD_INPUT_HANDLE); INPUT_RECORD record; @@ -41,9 +41,6 @@ int64_t FAST_FUNC read_key(int fd, char *buf, int timeout) DWORD alt_pressed = FALSE; DWORD state; - if (terminal_mode(FALSE) & VT_INPUT) - return unix_read_key(fd, buf, timeout); - if (fd != 0) bb_error_msg_and_die("read_key only works on stdin"); if (cin == INVALID_HANDLE_VALUE) -- cgit v1.2.3-55-g6feb