From 6b77da2fbfadb69091bbb1339704e7b86aa6a7b7 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sat, 12 Feb 2022 21:27:36 +0000 Subject: [PATCH] Disable broken KEYBD trap for multibyte characters In UTF-8 locales, ksh breaks when a KEYBD trap is active, even a dummy no-op one like 'trap : KEYBD'. Entering multi-byte characters fails (the input is interrupted and a new prompt is displayed) and pasting content with multi-byte characters produces corrupted results. The cause is that the KEYBD trap code is not multibyte-ready. Unfortunately nobody yet understands the edit.c code well enough to implement a proper fix. Pending that, this commit implements a workaround that at least avoids breaking the shell. src/cmd/ksh93/edit/edit.c: ed_getchar(): - When a multi-byte locale is active, do not trigger the the KEYBD trap except for ASCII characters (1-127). Resolves: https://github.com/ksh93/ksh/issues/307 --- NEWS | 6 ++++++ src/cmd/ksh93/edit/edit.c | 5 ++++- src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh.1 | 4 ++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 8877d3975503..fe810fe52ed5 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,12 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2022-02-12: + +- In multibyte locales such as UTF-8, shell input is no longer corrupted when + a KEYBD trap is active. However, the KEYBD trap is not yet multibyte ready + and is now only triggered for ASCII characters (1-127) in a multibyte locale. + 2022-02-11: - On the interactive shell, tab/esc completion is no longer disabled as a side diff --git a/src/cmd/ksh93/edit/edit.c b/src/cmd/ksh93/edit/edit.c index d9933b63bb6a..371f5a9ec8fb 100644 --- a/src/cmd/ksh93/edit/edit.c +++ b/src/cmd/ksh93/edit/edit.c @@ -1122,7 +1122,10 @@ int ed_getchar(register Edit_t *ep,int mode) killpg(getpgrp(),SIGINT); siglongjmp(ep->e_env, UINTR); } - if(mode<=0 && sh.st.trap[SH_KEYTRAP]) + if(mode<=0 && sh.st.trap[SH_KEYTRAP] + /* workaround for : + * do not trigger KEYBD for non-ASCII in multibyte locale */ + && (CC_NATIVE!=CC_ASCII || !mbwide() || c > -128)) { ep->e_keytrap = 1; n=1; diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 5e8abd6eb654..bc51faed1213 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -21,7 +21,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.1.0-alpha" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2022-02-11" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2022-02-12" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/sh.1 b/src/cmd/ksh93/sh.1 index 841c28a43bb8..61f71c2d1698 100644 --- a/src/cmd/ksh93/sh.1 +++ b/src/cmd/ksh93/sh.1 @@ -9062,6 +9062,10 @@ Thus, a trap on .B CHLD won't be executed until the foreground job terminates. .PP +In locales that use a multibyte character set such as UTF-8, the +.B KEYBD +trap is only triggered for ASCII characters (1-127). +.PP It is a good idea to leave a space after the comma operator in arithmetic expressions to prevent the comma from being interpreted as the decimal point character in certain locales.