From c828ea8d0d4c358583eb4293d2055c3b69896663 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 18 May 2021 12:39:24 +0200 Subject: [PATCH] Fix typeset -u/-l on NetBSD On NetBSD, for some reason, the wctrans(3) and towctrans(3) C library functions exist, but have no effect; the "toupper" and "tolower" maps don't even translate case for ASCII, never mind wide characters. This kills 'typeset -u' and 'typeset -l' on ksh, which was the cause of most of the regression test failures on NetBSD. Fallback versions for these functions are provided in init.c, but were not being used on NetBSD because the feature test detected the presence of these functions in the C library. src/cmd/ksh93/features/locale: - Replace the simple test for the presence of wctrans(3), towctrans(3), and the wctrans_t type by an actual feature test that checks that these functions not only compile, but are also capable of changing an ASCII 'q' to upper case and back again. src/cmd/ksh93/sh/init.c: towctrans(): - Add wide character support to the fallback function, for whatever good that may do; on NetBSD, the wide-character towupper(3) and towlower(3) functions only change case for ASCII. --- TODO | 2 -- src/cmd/ksh93/README | 3 ++- src/cmd/ksh93/features/locale | 22 ++++++++++++++++++++-- src/cmd/ksh93/sh/init.c | 10 ++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 3a53c6b09..2a642ee96 100644 --- a/TODO +++ b/TODO @@ -68,8 +68,6 @@ Enhancements to do: ______ Fix regression test failures: -- There are many regression test failures on NetBSD. - - There are some serious regression test failures on OpenBSD when ksh is compiled with AST vmalloc disabled, so the system standard malloc(3) is used. These probably represent real ksh93 bugs exposed by OpenBSD's diff --git a/src/cmd/ksh93/README b/src/cmd/ksh93/README index c6fa7897c..06831e6a6 100644 --- a/src/cmd/ksh93/README +++ b/src/cmd/ksh93/README @@ -254,7 +254,8 @@ failures (crashes, and/or important functionality does not work). * illumos: OmniOS 2020-08-19 (gcc) on x86_64 macOS 10.13.6 (High Sierra) on x86_64 macOS 10.14.6 (Mojave) on x86_64 -*** NetBSD 8.1 and 9.0 on x86_64 +* NetBSD 8.1 on x86_64 +* NetBSD 9.2 on x86_64 * OpenBSD 6.8 on x86_64 ** QNX 6.5.0 on i386 * Solaris 11.4 (gcc) on x86_64 diff --git a/src/cmd/ksh93/features/locale b/src/cmd/ksh93/features/locale index 4155b5307..8221ef7e4 100644 --- a/src/cmd/ksh93/features/locale +++ b/src/cmd/ksh93/features/locale @@ -1,7 +1,25 @@ hdr locale,wchar,wctype lib locale,localeconv,wctype,iswctype,iswblank -lib wctrans,towctrans wctype.h -typ wctrans_t wctype.h + +tst note{ do wctrans/towctrans work }end output{ + /* + * On NetBSD, these functions exist, but simply have no effect for some reason. + * So instead of simple lib/typ tests, this custom test checks if they actually work. + */ + #include + #include + #include + int main() + { + wctrans_t toupper = wctrans("toupper"), tolower = wctrans("tolower"); + int r = towctrans('q',toupper) == 'Q' && towctrans('Q',tolower) == 'q'; + printf("#define _lib_wctrans\t%d\n",r); + printf("#define _lib_towctrans\t%d\n",r); + printf("#define _typ_wctrans_t\t%d\n",r); + return !r; + } +}end + cat{ #if _PACKAGE_ast # undef _hdr_locale diff --git a/src/cmd/ksh93/sh/init.c b/src/cmd/ksh93/sh/init.c index 5b7dcdcff..1a7f686fe 100644 --- a/src/cmd/ksh93/sh/init.c +++ b/src/cmd/ksh93/sh/init.c @@ -76,6 +76,16 @@ static wctrans_t wctrans(const char *name) #define towctrans sh_towctrans static int towctrans(int c, wctrans_t t) { +#if _lib_towupper && _lib_towlower + if(mbwide()) + { + if(t==1 && iswupper((wint_t)c)) + c = (int)towlower((wint_t)c); + else if(t==2 && iswlower((wint_t)c)) + c = (int)towupper((wint_t)c); + } + else +#endif if(t==1 && isupper(c)) c = tolower(c); else if(t==2 && islower(c))