From 9b45f2ccbecf88e1e237a2c118b3d8ffe84efba0 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sun, 23 Aug 2020 23:41:31 +0100 Subject: [PATCH] build system: modernise shell compatibility checks All changed files: - Put the shell in POSIX mode if it has an '-o posix' option. - Remove nonsense disabling 'set -x' on bash. It's not broken. bin/package, src/cmd/INIT/package.sh: - Add check blocking native zsh mode (e.g., "$path" conflicts). Using a 'sh -> zsh' symlink works, so recommend that. - Remove old ksh93 version check for a supposed conflict with libcmd. It was broken; it would revert to /bin/sh, but on illumos distributions, /bin/sh is a ksh93 of a version that is supposedly affected. It builds fine anyway. - Rewrite checksh() to incorporate the shell compatibility checks that were previously in two different places in 'package'. bin/ignore, src/cmd/INIT/ignore.sh, bin/silent, src/cmd/INIT/silent.sh: - Change bad check for a full POSIX 'export' command (no, $RANDOM has nothing to do with that) with a proper feature test. --- bin/ignore | 11 +++-- bin/mamprobe | 4 +- bin/package | 69 ++++++++++-------------------- bin/silent | 11 +++-- src/cmd/INIT/crossexec.sh | 2 + src/cmd/INIT/execrate.sh | 2 + src/cmd/INIT/filter.sh | 2 + src/cmd/INIT/hurl.sh | 2 + src/cmd/INIT/iffe.sh | 4 +- src/cmd/INIT/ignore.sh | 11 +++-- src/cmd/INIT/mamprobe.sh | 4 +- src/cmd/INIT/mkdir.sh | 5 ++- src/cmd/INIT/package.sh | 69 ++++++++++-------------------- src/cmd/INIT/silent.sh | 11 +++-- src/cmd/ksh93/features/math.sh | 5 +++ src/lib/libast/comp/conf.sh | 5 ++- src/lib/libast/features/libpath.sh | 6 +++ src/lib/libast/features/param.sh | 6 +++ src/lib/libast/features/preroot.sh | 6 +++ src/lib/libast/features/sig.sh | 6 +++ 20 files changed, 113 insertions(+), 128 deletions(-) diff --git a/bin/ignore b/bin/ignore index 299964501..88e31093e 100755 --- a/bin/ignore +++ b/bin/ignore @@ -20,18 +20,17 @@ # non-ksh script for the nmake ignore prefix # @(#)ignore (AT&T Research) 1992-08-11 -case $-:$BASH_VERSION in -*x*:[0123456789]*) : bash set -x is broken :; set +ex ;; -esac +(set -o posix) 2>/dev/null && set -o posix +modern_export=`v=; export v=ok 2>/dev/null; echo "$v"` while : do case $# in 0) exit 0 ;; esac case $1 in - *=*) case $RANDOM in - $RANDOM)`echo $1 | sed "s/\\([^=]*\\)=\\(.*\\)/eval \\1='\\2'; export \\1/"` ;; - *) export "$1" ;; + *=*) case $modern_export in + ok) export "$1" ;; + *) `echo $1 | sed "s/\\([^=]*\\)=\\(.*\\)/eval \\1='\\2'; export \\1/"` ;; esac shift ;; diff --git a/bin/mamprobe b/bin/mamprobe index 5988b8975..e5166f47d 100755 --- a/bin/mamprobe +++ b/bin/mamprobe @@ -21,9 +21,7 @@ # mamprobe - generate MAM cc probe info # Glenn Fowler -case $-:$BASH_VERSION in -*x*:[0123456789]*) : bash set -x is broken :; set +ex ;; -esac +(set -o posix) 2>/dev/null && set -o posix command=mamprobe diff --git a/bin/package b/bin/package index 0b0420a11..4ff05cf22 100755 --- a/bin/package +++ b/bin/package @@ -24,37 +24,33 @@ USAGE_LICENSE="[-author?Glenn Fowler ][-copyright?Copyrigh command=package -case $-:$BASH_VERSION in -*x*:[0123456789]*) : bash set -x is broken :; set +ex ;; +(set -o posix) 2>/dev/null && set -o posix +path=Bad +case $PATH in +Bad*) echo "Cannot be run by zsh in native mode; use a sh symlink to zsh" >&2 + exit 1 ;; esac -# ksh checks -- ksh between 2007-11-05 and 2011-11-11 conflict with new -lcmd -- wea culpa +# shell checks checksh() { - egrep 'Version.*(88|1993)' $1 >/dev/null 2>&1 || - PATH=/dev/null $1 -c '(( 2 + 2 ))' >/dev/null 2>&1 && - $1 -c '(( .sh.version >= 20111111 ))' >/dev/null 2>&1 -} - -case $_AST_BIN_PACKAGE_:$SHELL:$0 in -1:*:*|*:/bin/sh:*) - ;; -*:*/*:*/*) - _AST_BIN_PACKAGE_=1 # prevent non-interactive sh .rc referencing bin/package recursion # - export _AST_BIN_PACKAGE_ - if checksh $SHELL - then : no -lcmd conflict : - else case " $* " in - *" debug "*|*" DEBUG "*|*" show "*) - echo $command: $SHELL: warning: possible -lcmd conflict -- falling back to /bin/sh >&2 - ;; + "$1" -ec ' + # reject csh + case 1 in + 1) ;; esac - SHELL=/bin/sh - export SHELL - exec $SHELL "$0" "$@" - fi - ;; -esac + # reject special use of $path (to use zsh, use a "sh -> zsh" symlink, which disables this) + path=Bad + case $PATH in + Bad*) exit 1 ;; + esac + # catch (our own) pipe/socket configuration mismatches + date | "$1" -c "read x" || exit 1 + # check Bourne/POSIX compatible trap exit status (should exit with status 0) + trap "exit 0" 0 + exit 1 + ' x "$1" 2>/dev/null || return 1 +} LC_ALL=C export LC_ALL @@ -3103,26 +3099,7 @@ cat $INITROOT/$i.sh ;; esac ;; - esac - - # $SHELL must be /bin/sh compatible - - case $SHELL in - /bin/sh);; - '') SHELL=/bin/sh - ;; - *) $SHELL -c 'trap "exit 0" 0; exit 1' 2>/dev/null - case $? in - 1) SHELL=/bin/sh - ;; - *) # catch (our own) pipe/socket configuration mismatches - $SHELL -c "date | $SHELL -c 'read x'" - case $? in - 0) ;; - *) SHELL=/bin/sh ;; - esac - ;; - esac + *) checksh $SHELL || SHELL=/bin/sh ;; esac export SHELL diff --git a/bin/silent b/bin/silent index be99aa69a..e3c148eed 100755 --- a/bin/silent +++ b/bin/silent @@ -20,18 +20,17 @@ # non-ksh stub for the nmake silent prefix # @(#)silent (AT&T Research) 1992-08-11 -case $-:$BASH_VERSION in -*x*:[01234567899]*) : bash set -x is broken :; set +ex ;; -esac +(set -o posix) 2>/dev/null && set -o posix +modern_export=`v=; export v=ok 2>/dev/null; echo "$v"` while : do case $# in 0) exit 0 ;; esac case $1 in - *=*) case $RANDOM in - $RANDOM)`echo $1 | sed "s/\\([^=]*\\)=\\(.*\\)/eval \\1='\\2'; export \\1/"` ;; - *) export "$1" ;; + *=*) case $modern_export in + ok) export "$1" ;; + *) `echo $1 | sed "s/\\([^=]*\\)=\\(.*\\)/eval \\1='\\2'; export \\1/"` ;; esac shift ;; diff --git a/src/cmd/INIT/crossexec.sh b/src/cmd/INIT/crossexec.sh index 9d73ee2a7..3fab299c4 100644 --- a/src/cmd/INIT/crossexec.sh +++ b/src/cmd/INIT/crossexec.sh @@ -19,6 +19,8 @@ ######################################################################## : cross compiler a.out execution +(set -o posix) 2>/dev/null && set -o posix + command=crossexec tmp=/tmp/cross$$ diff --git a/src/cmd/INIT/execrate.sh b/src/cmd/INIT/execrate.sh index e175aac72..ec142b6ef 100644 --- a/src/cmd/INIT/execrate.sh +++ b/src/cmd/INIT/execrate.sh @@ -19,6 +19,8 @@ ######################################################################## : wrapper for .exe challenged win32 systems/commands +(set -o posix) 2>/dev/null && set -o posix + command=execrate bins=` diff --git a/src/cmd/INIT/filter.sh b/src/cmd/INIT/filter.sh index e2ad93680..f1f524dca 100644 --- a/src/cmd/INIT/filter.sh +++ b/src/cmd/INIT/filter.sh @@ -19,6 +19,8 @@ ######################################################################## : convert command that operates on file args to pipeline filter +(set -o posix) 2>/dev/null && set -o posix + command=filter TMPDIR=${TMPDIR:-/tmp} diff --git a/src/cmd/INIT/hurl.sh b/src/cmd/INIT/hurl.sh index 60e8a6bac..1290a4b83 100644 --- a/src/cmd/INIT/hurl.sh +++ b/src/cmd/INIT/hurl.sh @@ -19,6 +19,8 @@ ######################################################################## : copy http url data +(set -o posix) 2>/dev/null && set -o posix + command=hurl agent="$command/2009-01-20 (AT&T Research)" authorize= diff --git a/src/cmd/INIT/iffe.sh b/src/cmd/INIT/iffe.sh index 0586fbfec..de8d0bb56 100644 --- a/src/cmd/INIT/iffe.sh +++ b/src/cmd/INIT/iffe.sh @@ -25,9 +25,7 @@ # # NOTE: .exe a.out suffix and [\\/] in path patterns for dos/nt -case $-:$BASH_VERSION in -*x*:[0123456789]*) : bash set -x is broken :; set +ex ;; -esac +(set -o posix) 2>/dev/null && set -o posix command=iffe version=2012-07-17 # update in USAGE too # diff --git a/src/cmd/INIT/ignore.sh b/src/cmd/INIT/ignore.sh index 299964501..88e31093e 100644 --- a/src/cmd/INIT/ignore.sh +++ b/src/cmd/INIT/ignore.sh @@ -20,18 +20,17 @@ # non-ksh script for the nmake ignore prefix # @(#)ignore (AT&T Research) 1992-08-11 -case $-:$BASH_VERSION in -*x*:[0123456789]*) : bash set -x is broken :; set +ex ;; -esac +(set -o posix) 2>/dev/null && set -o posix +modern_export=`v=; export v=ok 2>/dev/null; echo "$v"` while : do case $# in 0) exit 0 ;; esac case $1 in - *=*) case $RANDOM in - $RANDOM)`echo $1 | sed "s/\\([^=]*\\)=\\(.*\\)/eval \\1='\\2'; export \\1/"` ;; - *) export "$1" ;; + *=*) case $modern_export in + ok) export "$1" ;; + *) `echo $1 | sed "s/\\([^=]*\\)=\\(.*\\)/eval \\1='\\2'; export \\1/"` ;; esac shift ;; diff --git a/src/cmd/INIT/mamprobe.sh b/src/cmd/INIT/mamprobe.sh index 5988b8975..e5166f47d 100644 --- a/src/cmd/INIT/mamprobe.sh +++ b/src/cmd/INIT/mamprobe.sh @@ -21,9 +21,7 @@ # mamprobe - generate MAM cc probe info # Glenn Fowler -case $-:$BASH_VERSION in -*x*:[0123456789]*) : bash set -x is broken :; set +ex ;; -esac +(set -o posix) 2>/dev/null && set -o posix command=mamprobe diff --git a/src/cmd/INIT/mkdir.sh b/src/cmd/INIT/mkdir.sh index c929e9517..e6fe0cf34 100644 --- a/src/cmd/INIT/mkdir.sh +++ b/src/cmd/INIT/mkdir.sh @@ -19,7 +19,10 @@ # # ######################################################################## : mkdir for systems that do not support -p : 2002-09-01 : -MKDIR=/bin/mkdir + +(set -o posix) 2>/dev/null && set -o posix + +MKDIR=mkdir CHMOD=chmod mode= parents= diff --git a/src/cmd/INIT/package.sh b/src/cmd/INIT/package.sh index c30391ccc..18d02e608 100644 --- a/src/cmd/INIT/package.sh +++ b/src/cmd/INIT/package.sh @@ -23,37 +23,33 @@ command=package -case $-:$BASH_VERSION in -*x*:[0123456789]*) : bash set -x is broken :; set +ex ;; +(set -o posix) 2>/dev/null && set -o posix +path=Bad +case $PATH in +Bad*) echo "Cannot be run by zsh in native mode; use a sh symlink to zsh" >&2 + exit 1 ;; esac -# ksh checks -- ksh between 2007-11-05 and 2011-11-11 conflict with new -lcmd -- wea culpa +# shell checks checksh() { - egrep 'Version.*(88|1993)' $1 >/dev/null 2>&1 || - PATH=/dev/null $1 -c '(( 2 + 2 ))' >/dev/null 2>&1 && - $1 -c '(( .sh.version >= 20111111 ))' >/dev/null 2>&1 -} - -case $_AST_BIN_PACKAGE_:$SHELL:$0 in -1:*:*|*:/bin/sh:*) - ;; -*:*/*:*/*) - _AST_BIN_PACKAGE_=1 # prevent non-interactive sh .rc referencing bin/package recursion # - export _AST_BIN_PACKAGE_ - if checksh $SHELL - then : no -lcmd conflict : - else case " $* " in - *" debug "*|*" DEBUG "*|*" show "*) - echo $command: $SHELL: warning: possible -lcmd conflict -- falling back to /bin/sh >&2 - ;; + "$1" -ec ' + # reject csh + case 1 in + 1) ;; esac - SHELL=/bin/sh - export SHELL - exec $SHELL "$0" "$@" - fi - ;; -esac + # reject special use of $path (to use zsh, use a "sh -> zsh" symlink, which disables this) + path=Bad + case $PATH in + Bad*) exit 1 ;; + esac + # catch (our own) pipe/socket configuration mismatches + date | "$1" -c "read x" || exit 1 + # check Bourne/POSIX compatible trap exit status (should exit with status 0) + trap "exit 0" 0 + exit 1 + ' x "$1" 2>/dev/null || return 1 +} LC_ALL=C export LC_ALL @@ -3102,26 +3098,7 @@ cat $INITROOT/$i.sh ;; esac ;; - esac - - # $SHELL must be /bin/sh compatible - - case $SHELL in - /bin/sh);; - '') SHELL=/bin/sh - ;; - *) $SHELL -c 'trap "exit 0" 0; exit 1' 2>/dev/null - case $? in - 1) SHELL=/bin/sh - ;; - *) # catch (our own) pipe/socket configuration mismatches - $SHELL -c "date | $SHELL -c 'read x'" - case $? in - 0) ;; - *) SHELL=/bin/sh ;; - esac - ;; - esac + *) checksh $SHELL || SHELL=/bin/sh ;; esac export SHELL diff --git a/src/cmd/INIT/silent.sh b/src/cmd/INIT/silent.sh index be99aa69a..e3c148eed 100644 --- a/src/cmd/INIT/silent.sh +++ b/src/cmd/INIT/silent.sh @@ -20,18 +20,17 @@ # non-ksh stub for the nmake silent prefix # @(#)silent (AT&T Research) 1992-08-11 -case $-:$BASH_VERSION in -*x*:[01234567899]*) : bash set -x is broken :; set +ex ;; -esac +(set -o posix) 2>/dev/null && set -o posix +modern_export=`v=; export v=ok 2>/dev/null; echo "$v"` while : do case $# in 0) exit 0 ;; esac case $1 in - *=*) case $RANDOM in - $RANDOM)`echo $1 | sed "s/\\([^=]*\\)=\\(.*\\)/eval \\1='\\2'; export \\1/"` ;; - *) export "$1" ;; + *=*) case $modern_export in + ok) export "$1" ;; + *) `echo $1 | sed "s/\\([^=]*\\)=\\(.*\\)/eval \\1='\\2'; export \\1/"` ;; esac shift ;; diff --git a/src/cmd/ksh93/features/math.sh b/src/cmd/ksh93/features/math.sh index c0a1875ff..2f95d0fbc 100644 --- a/src/cmd/ksh93/features/math.sh +++ b/src/cmd/ksh93/features/math.sh @@ -22,6 +22,11 @@ # @(#)math.sh (AT&T Research) 2012-06-13 +case $ZSH_VERSION in +?*) emulate ksh ;; +*) (set -o posix) 2>/dev/null && set -o posix ;; +esac + command=$0 iffeflags="-n -v" iffehdrs="math.h" diff --git a/src/lib/libast/comp/conf.sh b/src/lib/libast/comp/conf.sh index cc15f7930..b87eae05a 100644 --- a/src/lib/libast/comp/conf.sh +++ b/src/lib/libast/comp/conf.sh @@ -34,8 +34,9 @@ # but you shall be confused anyway # -case $-:$BASH_VERSION in -*x*:[0123456789]*) : bash set -x is broken :; set +ex ;; +case $ZSH_VERSION in +?*) emulate ksh ;; +*) (set -o posix) 2>/dev/null && set -o posix ;; esac LC_ALL=C diff --git a/src/lib/libast/features/libpath.sh b/src/lib/libast/features/libpath.sh index b3809fda7..dc29fca49 100644 --- a/src/lib/libast/features/libpath.sh +++ b/src/lib/libast/features/libpath.sh @@ -19,6 +19,12 @@ # Phong Vo # # # ######################################################################## + +case $ZSH_VERSION in +?*) emulate ksh ;; +*) (set -o posix) 2>/dev/null && set -o posix ;; +esac + ok=0 for i in \ -x /lib/ld.so /lib/ld-*.so /usr/lib/ld.so /lib/rld \ diff --git a/src/lib/libast/features/param.sh b/src/lib/libast/features/param.sh index 7d4a856fd..83878f352 100644 --- a/src/lib/libast/features/param.sh +++ b/src/lib/libast/features/param.sh @@ -20,6 +20,12 @@ # # ######################################################################## : generate " + + " include sequence + +case $ZSH_VERSION in +?*) emulate ksh ;; +*) (set -o posix) 2>/dev/null && set -o posix ;; +esac + case $# in 0) ;; *) eval $1 diff --git a/src/lib/libast/features/preroot.sh b/src/lib/libast/features/preroot.sh index 40ac2fc14..247eb4303 100644 --- a/src/lib/libast/features/preroot.sh +++ b/src/lib/libast/features/preroot.sh @@ -20,6 +20,12 @@ # # ######################################################################## : generate preroot features + +case $ZSH_VERSION in +?*) emulate ksh ;; +*) (set -o posix) 2>/dev/null && set -o posix ;; +esac + case $# in 0) ;; *) eval $1 diff --git a/src/lib/libast/features/sig.sh b/src/lib/libast/features/sig.sh index 706372b53..93e33ef9e 100644 --- a/src/lib/libast/features/sig.sh +++ b/src/lib/libast/features/sig.sh @@ -20,6 +20,12 @@ # # ######################################################################## : generate sig features + +case $ZSH_VERSION in +?*) emulate ksh ;; +*) (set -o posix) 2>/dev/null && set -o posix ;; +esac + case $# in 0) ;; *) eval $1