From 9aef4d4d298987437e33bf25bcddd16175148d45 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 5 Mar 2023 12:33:01 +0000 Subject: win32: enable Unix read_key() for virtual terminal Until now busybox-w32 has used a native Windows implementation of read_key(). Build the upstream Unix implementation and use it instead of the native version when virtual terminal input mode is enabled. --- include/libbb.h | 5 ++--- libbb/Kbuild.src | 2 +- libbb/read_key.c | 4 ++++ win32/termios.c | 5 ++++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 4197a5e83..c1d4d11c1 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1976,11 +1976,10 @@ enum { */ int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC; #if ENABLE_PLATFORM_MINGW32 -#define safe_read_key(f, b, t) read_key(f, b, t) -#else +int64_t unix_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; -#endif void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC; diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index 191984c9d..425bfe1c6 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src @@ -64,6 +64,7 @@ lib-y += process_escape_sequence.o lib-y += procps.o lib-y += ptr_to_globals.o lib-y += read.o +lib-y += read_key.o lib-y += read_printf.o lib-y += recursive_action.o lib-y += remove_file.o @@ -114,7 +115,6 @@ lib-$(CONFIG_PLATFORM_POSIX) += perror_nomsg.o lib-$(CONFIG_PLATFORM_POSIX) += pidfile.o lib-$(CONFIG_PLATFORM_POSIX) += print_flags.o lib-$(CONFIG_PLATFORM_POSIX) += progress.o -lib-$(CONFIG_PLATFORM_POSIX) += read_key.o lib-$(CONFIG_PLATFORM_POSIX) += setup_environment.o lib-$(CONFIG_PLATFORM_POSIX) += signals.o lib-$(CONFIG_PLATFORM_POSIX) += speed_table.o diff --git a/libbb/read_key.c b/libbb/read_key.c index cf8ed411e..c34770028 100644 --- a/libbb/read_key.c +++ b/libbb/read_key.c @@ -9,7 +9,11 @@ */ #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; diff --git a/win32/termios.c b/win32/termios.c index 4b3588826..20ec4dcc5 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 UNUSED_PARAM, int timeout) +int64_t FAST_FUNC read_key(int fd, char *buf, int timeout) { HANDLE cin = GetStdHandle(STD_INPUT_HANDLE); INPUT_RECORD record; @@ -41,6 +41,9 @@ int64_t FAST_FUNC read_key(int fd, char *buf UNUSED_PARAM, 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