From 8152e92d3abfff0386a672421e164ce163678dcf Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 8 Feb 2020 16:48:40 +0000 Subject: ash: fixes to handling of ctrl-C in read builtin Consider this script: while read -r; do echo $REPLY; done echo hello There are currently two problems with this when the read is interrupted by ctrl-C: - The error return code is 0 when it should be 130; - The echo command is executed. Fix these issues by propagating the control event to the process that would have caught it if the read builtin hadn't grabbed keyboard input. --- shell/ash.c | 15 +++++++++++++++ shell/shell_common.c | 9 +++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/shell/ash.c b/shell/ash.c index 2a7941702..f23d63b21 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -14701,6 +14701,21 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) goto again; } +#if ENABLE_PLATFORM_MINGW32 + if ((uintptr_t)r == 2) { + /* ^C pressed, propagate event */ + if (iflag) { + write(STDOUT_FILENO, "^C", 2); + raise_interrupt(); + } + else { + GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); + exitshell(); + } + return (uintptr_t)r; + } +#endif + if ((uintptr_t)r > 1) ash_msg_and_raise_error(r); diff --git a/shell/shell_common.c b/shell/shell_common.c index 320cd88fd..9092a8e2b 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -235,8 +235,13 @@ shell_builtin_read(struct builtin_read_params *params) int64_t key; key = read_key(fd, NULL, timeout); - if (key == 0x03 || key == -1 || (key == 0x1a && bufpos == 0)) { - /* ^C, timeout or ^Z at start of buffer */ + if (key == 0x03) { + /* ^C pressed */ + retval = (const char *)(uintptr_t)2; + goto ret; + } + else if (key == -1 || (key == 0x1a && bufpos == 0)) { + /* timeout or ^Z at start of buffer */ retval = (const char *)(uintptr_t)1; goto ret; } -- cgit v1.2.3-55-g6feb