From ce3cc66d5859aa1ce96d8d65c4bb8e487248999d Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 21 Jul 2022 22:58:53 +0200 Subject: [PATCH] Next round of minor tweaks and cleanups Notable changes: - Tie up some loose ends re: 3de4da5a and 7ba2c685. - comp/omitted.c: Header include fix for Cygwin. - misc/optget.c: - args(): When printing options for the uage line, use a local pointer for the 'if' block instead of reusing the 'b' pointer. That variable is used to output blanks later. - The above fix allows re-enabling the AST translation-aware macros and deleting the astsa fallback without causing usage message corruption in multibyte locales. Maybe someday we'll make ksh actually translatable. - Remove code to reinitialise _error_infop_ and _opt_info_ 'because these are not initialised by all DLLs'. In 2022, hopefully the buggy dynamic linkers are fixed. If not, we're not going to find out by keeping the workaround. I suspect that those bugs may have been triggered by the Microsoft/Cygwin import/export obfuscation removed in 3de4da5a. - ksh93: - Remove unused sh.st.var_local variable. This was a leftover of a 93v- attempt to implement the bash 'local' command. It used static scoping, so it's not actually compatible. - Add a few regression tests for miscellaneous breakage that I caused in experiments (the breakage never made it to git; the tests are just to keep it that way). --- src/cmd/INIT/mamake.c | 16 ++++++++--- src/cmd/ksh93/bltins/misc.c | 2 +- src/cmd/ksh93/include/argnod.h | 1 - src/cmd/ksh93/include/shell.h | 1 - src/cmd/ksh93/sh/xec.c | 24 ++++++++--------- src/cmd/ksh93/tests/arith.sh | 6 +++++ src/cmd/ksh93/tests/attributes.sh | 3 ++- src/cmd/ksh93/tests/subshell.sh | 11 ++++---- src/cmd/ksh93/tests/variables.sh | 7 +++++ src/lib/libast/Mamfile | 1 + src/lib/libast/comp/omitted.c | 2 ++ src/lib/libast/misc/error.c | 12 ++------- src/lib/libast/misc/optget.c | 44 +++---------------------------- src/lib/libast/string/chresc.c | 4 +-- src/lib/libast/string/strerror.c | 10 ------- 15 files changed, 55 insertions(+), 89 deletions(-) diff --git a/src/cmd/INIT/mamake.c b/src/cmd/INIT/mamake.c index 5d03e718e..4c2c2ecb2 100644 --- a/src/cmd/INIT/mamake.c +++ b/src/cmd/INIT/mamake.c @@ -97,7 +97,7 @@ static const char usage[] = #define elementsof(x) (sizeof(x)/sizeof(x[0])) #define newof(p,t,n,x) ((p)?(t*)realloc((char*)(p),sizeof(t)*(n)+(x)):(t*)calloc(1,sizeof(t)*(n)+(x))) -#define NiL ((char*)0) +#define NiL 0 #endif @@ -267,7 +267,15 @@ extern char** environ; static void usage() { - fprintf(stderr, "Usage: %s [-iknFKNV] [-f mamfile] [-r pattern] [-C directory] [-D level] [target ...] [name=value ...]\n", state.id); + fprintf(stderr, "Usage: %s" + " [-iknFKNV]" + " [-f Mamfile]" + " [-r pattern]" + " [-C directory]" + " [-D level]" + " [target ...]" + " [name=value ...]" + "\n", state.id); exit(2); } @@ -1834,7 +1842,7 @@ update(register Rule_t* r) } /* - * scan makefile prereqs + * scan Mamfile prereqs */ static int @@ -2123,7 +2131,7 @@ recurse(char* pattern) search(state.vars, "MAMAKEARGS", duplicate(use(state.opt) + 1)); /* - * scan the makefile and descend + * scan the Mamfile and descend */ walk(state.rules, scan, NiL); diff --git a/src/cmd/ksh93/bltins/misc.c b/src/cmd/ksh93/bltins/misc.c index d8574dfb9..302e6cf9c 100644 --- a/src/cmd/ksh93/bltins/misc.c +++ b/src/cmd/ksh93/bltins/misc.c @@ -287,7 +287,7 @@ int b_dot_cmd(register int n,char *argv[],Shbltin_t *context) } *prevscope = sh.st; sh.st.lineno = np?((struct functnod*)nv_funtree(np))->functline:1; - sh.st.var_local = sh.st.save_tree = sh.var_tree; + sh.st.save_tree = sh.var_tree; if(filename) { sh.st.filename = filename; diff --git a/src/cmd/ksh93/include/argnod.h b/src/cmd/ksh93/include/argnod.h index 5f2b6610d..a7d8ce5f6 100644 --- a/src/cmd/ksh93/include/argnod.h +++ b/src/cmd/ksh93/include/argnod.h @@ -113,7 +113,6 @@ struct argnod /* legal argument flags */ #define ARG_RAW 0x1 /* string needs no processing */ #define ARG_MAKE 0x2 /* bit set during argument expansion */ -#define ARG_COMSUB 0x2 /* command sub */ #define ARG_MAC 0x4 /* string needs macro expansion */ #define ARG_EXP 0x8 /* string needs file expansion */ #define ARG_ASSIGN 0x10 /* argument is an assignment */ diff --git a/src/cmd/ksh93/include/shell.h b/src/cmd/ksh93/include/shell.h index e047746a0..a1084698f 100644 --- a/src/cmd/ksh93/include/shell.h +++ b/src/cmd/ksh93/include/shell.h @@ -195,7 +195,6 @@ struct sh_scoped int lineno; Dt_t *save_tree; /* var_tree for calling function */ struct sh_scoped *self; /* pointer to copy of this scope */ - Dt_t *var_local; /* local level variables for name() */ struct slnod *staklist; /* link list of function stacks */ int states; /* shell state bits used by sh_isstate(), etc. */ int breakcnt; /* number of levels to 'break'/'continue' (negative if 'continue') */ diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 596a237c9..a344fe05b 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -2804,18 +2804,6 @@ int sh_trace(register char *argv[], register int nl) return(0); } -/* - * This routine creates a subshell by calling fork() or vfork() - * If ((flags&COMMSK)==TCOM), then vfork() is permitted - * If fork fails, the shell sleeps for exponentially longer periods - * and tries again until a limit is reached. - * SH_FORKLIM is the max period between forks - power of 2 usually. - * Currently shell tries after 2,4,8,16, and 32 seconds and then quits - * Failures cause the routine to error exit. - * Parent links to here-documents are removed by the child - * Traps are reset by the child - * The process-id of the child is returned to the parent, 0 to the child. - */ static void timed_out(void *handle) { NOT_USED(handle); @@ -2954,6 +2942,17 @@ pid_t _sh_fork(register pid_t parent,int flags,int *jobid) return(0); } +/* + * This routine creates a subshell by calling fork(2). + * If fork fails, the shell sleeps for exponentially longer periods + * and tries again until a limit is reached. + * SH_FORKLIM is the max period between forks - power of 2 usually. + * Currently, the shell tries after 2, 4, 8, 16 and 32 seconds, and then quits. + * Failures cause the routine to error exit. + * Parent links to here-documents are removed by the child. + * Traps are reset by the child. + * The process-id of the child is returned to the parent, 0 to the child. + */ pid_t sh_fork(int flags, int *jobid) { register pid_t parent; @@ -3109,7 +3108,6 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg) sh_pushcontext(buffp,SH_JMPFUN); errorpush(&buffp->err,0); error_info.id = argv[0]; - sh.st.var_local = sh.var_tree; if(!fun) { if(fp->node->nvalue.rp) diff --git a/src/cmd/ksh93/tests/arith.sh b/src/cmd/ksh93/tests/arith.sh index 630b8140c..d2d14bc39 100755 --- a/src/cmd/ksh93/tests/arith.sh +++ b/src/cmd/ksh93/tests/arith.sh @@ -983,5 +983,11 @@ float x exit 0 EOF +# ====== +got=$(set +x; eval ': $((1 << 2))' 2>&1) \ +|| err_exit "bitwise left shift operator fails to parse (got $(printf %q "$got"))" +got=$(set +x; eval 'got=$( ((y=1<<4)); echo $y )' 2>&1; echo $got) \ +|| err_exit "bitwise left shift operator fails to parse in comsub (got $(printf %q "$got"))" + # ====== exit $((Errors<125?Errors:125)) diff --git a/src/cmd/ksh93/tests/attributes.sh b/src/cmd/ksh93/tests/attributes.sh index f48a166d3..eda4b1df4 100755 --- a/src/cmd/ksh93/tests/attributes.sh +++ b/src/cmd/ksh93/tests/attributes.sh @@ -385,7 +385,8 @@ done unset v typeset -H v=/dev/null -[[ $v == *nul* ]] || err_exit 'typeset -H for /dev/null not working' +# on cygwin, this is \\.\GLOBALROOT\Device\Null +[[ $v == *[Nn]ul* ]] || err_exit "typeset -H for /dev/null not working (got $(printf %q "$v"))" unset x (typeset +C x) 2> /dev/null && err_exit 'typeset +C should be an error' diff --git a/src/cmd/ksh93/tests/subshell.sh b/src/cmd/ksh93/tests/subshell.sh index c90feecfe..1a8da97cc 100755 --- a/src/cmd/ksh93/tests/subshell.sh +++ b/src/cmd/ksh93/tests/subshell.sh @@ -790,16 +790,17 @@ hash -r # ====== # Variables set in functions inside of a virtual subshell should not affect the -# outside environment. This regression test must be run from the disk. -testvars=$tmp/testvars.sh -cat >| "$testvars" << 'EOF' +# outside environment. This regression test must be run as a separate script. +got=$("$SHELL" -c ' c=0 function set_ac { a=1; c=1; } function set_abc { ( set_ac ; b=1 ) } set_abc echo "a=$a b=$b c=$c" -EOF -v=$($SHELL $testvars) && [[ "$v" == "a= b= c=0" ]] || err_exit 'variables set in subshells are not confined to the subshell' +') +exp='a= b= c=0' +[[ $got == "$exp" ]] || err_exit 'variables set in subshells are not confined to the subshell' \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" # ====== got=$("$SHELL" -c ' diff --git a/src/cmd/ksh93/tests/variables.sh b/src/cmd/ksh93/tests/variables.sh index 21005b752..eef45c049 100755 --- a/src/cmd/ksh93/tests/variables.sh +++ b/src/cmd/ksh93/tests/variables.sh @@ -1424,5 +1424,12 @@ do done done +# ====== +var1.get() { .sh.value=one; : $var2; } +var2.get() { .sh.value=two; } +got=$var1 +unset var1 var2 +[[ $got == one ]] || err_exit ".sh.value not restored after second .get discipline call (got $(printf %q "$got"))" + # ====== exit $((Errors<125?Errors:125)) diff --git a/src/lib/libast/Mamfile b/src/lib/libast/Mamfile index 3a13656c4..a7cf1d341 100644 --- a/src/lib/libast/Mamfile +++ b/src/lib/libast/Mamfile @@ -2832,6 +2832,7 @@ make install done FEATURE/omitted generated prev include/tm.h implicit prev include/error.h implicit + prev ast_stdio.h implicit prev include/ast.h implicit prev FEATURE/float implicit done comp/omitted.c diff --git a/src/lib/libast/comp/omitted.c b/src/lib/libast/comp/omitted.c index f80ae1bd0..d77020968 100644 --- a/src/lib/libast/comp/omitted.c +++ b/src/lib/libast/comp/omitted.c @@ -7,6 +7,7 @@ #define utimes ______utimes #include +#include #include #include @@ -24,6 +25,7 @@ #if __CYGWIN__ #include +#include #if _win32_botch_execve || _lib_spawn_mode #define CONVERT 1 #endif diff --git a/src/lib/libast/misc/error.c b/src/lib/libast/misc/error.c index 8c0ad86c7..0f5b2f9f6 100644 --- a/src/lib/libast/misc/error.c +++ b/src/lib/libast/misc/error.c @@ -47,12 +47,9 @@ /* * 2007-03-19 move error_info from _error_info_ to (*_error_infop_) * to allow future Error_info_t growth - * by 2009 _error_info_ can be static */ -extern Error_info_t _error_info_; - -Error_info_t _error_info_ = +static Error_info_t _error_info_ = { 2, exit, write, 0,0,0,0,0,0,0,0, @@ -64,8 +61,7 @@ Error_info_t _error_info_ = translate, 0 /* catalog */ }; - -extern Error_info_t* _error_infop_ = &_error_info_; +Error_info_t* _error_infop_ = &_error_info_; /* * these should probably be in error_info @@ -356,10 +352,8 @@ errorv(const char* id, int level, va_list ap) int line; char* file; -#if !_PACKAGE_astsa unsigned long d; struct tms us; -#endif if (!error_info.init) { @@ -466,14 +460,12 @@ errorv(const char* id, int level, va_list ap) sfprintf(stkstd, "%s %d: ", ERROR_translate(NiL, NiL, ast.id, "line"), error_info.line); } } -#if !_PACKAGE_astsa if (error_info.time) { if ((d = times(&us)) < error_info.time || error_info.time == 1) error_info.time = d; sfprintf(stkstd, " %05lu.%05lu.%05lu ", d - error_info.time, (unsigned long)us.tms_utime, (unsigned long)us.tms_stime); } -#endif switch (level) { case 0: diff --git a/src/lib/libast/misc/optget.c b/src/lib/libast/misc/optget.c index 9ade62feb..ce24bbfc9 100644 --- a/src/lib/libast/misc/optget.c +++ b/src/lib/libast/misc/optget.c @@ -176,8 +176,6 @@ static unsigned char map[UCHAR_MAX]; static Optstate_t state; -#if 0 /* #if !_PACKAGE_astsa // this somehow corrupts "Or:" usage messages, e.g. in 'typeset -\?' */ - #define ID ast.id #define C(s) ERROR_catalog(s) @@ -200,18 +198,6 @@ translate(const char* cmd, const char* cat, const char* msg) return errorx(NiL, cmd, cat, msg); } -#else - -static char ID[] = "ast"; - -#define C(s) s -#define D(s) (state.msgdict && dtmatch(state.msgdict, (s))) -#define T(i,c,m) m -#define X(c) 0 -#define Z(x) C(x),sizeof(x)-1 - -#endif - static const List_t help_head[] = { '-', 0, @@ -337,17 +323,9 @@ static Msg_t C_LC_MESSAGES_libast[] = /* * 2007-03-19 move opt_info from _opt_info_ to (*_opt_data_) * to allow future Opt_t growth - * by 2009 _opt_info_ can be static */ -extern Opt_t _opt_info_; - -Opt_t _opt_info_ = { 0,0,0,0,0,0,0,{0},{0},0,0,0,{0},{0},&state }; - -extern Opt_t _opt_info_; - -extern Opt_t* _opt_infop_; - +static Opt_t _opt_info_ = { 0,0,0,0,0,0,0,{0},{0},0,0,0,{0},{0},&state }; Opt_t* _opt_infop_ = &_opt_info_; Optstate_t* @@ -925,10 +903,8 @@ init(register char* s, Optpass_t* p) if (!state.localized) { state.localized = 1; -#if !_PACKAGE_astsa if (!ast.locale.serial) setlocale(LC_ALL, ""); -#endif state.xp = sfstropen(); if (!map[OPT_FLAGS[0]]) for (n = 0, t = OPT_FLAGS; *t; t++) @@ -1650,9 +1626,10 @@ args(register Sfio_t* sp, register char* p, register int n, int flags, int style sfputr(sp, b, -1); if (X(catalog)) { + char *cp; sfwrite(ip, p, i); - if (b = sfstruse(ip)) - sfputr(sp, T(id, catalog, b), -1); + if (cp = sfstruse(ip)) + sfputr(sp, T(id, catalog, cp), -1); else sfwrite(sp, p, i); } @@ -4283,19 +4260,6 @@ optget(register char** argv, const char* oopts) Optcache_t* pcache; Optpass_t* pass; -#if !_PACKAGE_astsa && !_YOU_FIGURED_OUT_HOW_TO_GET_ALL_DLLS_TO_DO_THIS_ - /* - * these are not initialized by all dlls! - */ - - extern Error_info_t _error_info_; - extern Opt_t _opt_info_; - - if (!_error_infop_) - _error_infop_ = &_error_info_; - if (!_opt_infop_) - _opt_infop_ = &_opt_info_; -#endif if (!oopts) return 0; state.pindex = opt_info.index; diff --git a/src/lib/libast/string/chresc.c b/src/lib/libast/string/chresc.c index 7abff8614..530fddd36 100644 --- a/src/lib/libast/string/chresc.c +++ b/src/lib/libast/string/chresc.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1985-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2021 Contributors to ksh 93u+m * +* Copyright (c) 2020-2022 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 1.0 * * by AT&T Intellectual Property * @@ -34,9 +34,7 @@ #include #include -#if !_PACKAGE_astsa #include -#endif int chrexp(register const char* s, char** p, int* m, register int flags) diff --git a/src/lib/libast/string/strerror.c b/src/lib/libast/string/strerror.c index 31dd96ceb..dc7a55bf4 100644 --- a/src/lib/libast/string/strerror.c +++ b/src/lib/libast/string/strerror.c @@ -56,14 +56,6 @@ int sys_nerr = 0; extern char* strerror(int); #endif -#if _PACKAGE_astsa - -#define fmtbuf(n) ((n),tmp) - -static char tmp[32]; - -#endif - char* _ast_strerror(int err) { @@ -82,7 +74,6 @@ _ast_strerror(int err) #endif if (msg) { -#if !_PACKAGE_astsa if (ERROR_translating()) { #if _lib_strerror @@ -125,7 +116,6 @@ _ast_strerror(int err) #endif return ERROR_translate(NiL, NiL, "errlist", msg); } -#endif return msg; } msg = fmtbuf(z = 32);