From f45a0f16504f58b3dbd7647794761ab771ec2c40 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Fri, 18 Sep 2020 20:32:34 +0200 Subject: [PATCH] -o posix: inverse-sync braceexpand; properly sync letoctal {Brace,expansion} is potentially incompatible with POSIX scripts, because in POSIX those are simple literal strings with no special meaning. So the POSIX option should really turn that off. As of b301d417, the 'posix' option was also forcing 'letoctal' behaviour on, without actually setting that option. I've since found that to be a botch; 'let' may recognise octals without that option being set, and that looks like a bug. So as of this commit, the '-o posix' option actually toggles both of these options off/on and on/of, respectively. 'set +o posix' toggles them inversely. However, it is now possible to control both options (and their associated behaviour) independently in between 'set -o posix' and 'set +o posix'. Much better. src/cmd/ksh93/sh/main.c: sh_main(): - If SH_POSIX was set on init, turn on SH_LETOCTAL by default instead of SH_BRACEEXPAND. src/cmd/ksh93/sh/args.c: sh_applyopts(): - Turn off SH_BRACEEXPAND and turn on SH_LETOCTAL when SH_POSIX is turned on (but not if it was already on). - Turn on SH_BRACEEXPAND and turn off SH_LETOCTAL when SH_POSIX is turned off (but not if it was already off). src/cmd/ksh93/sh/arith.c: arith(): - Revert to pre-b301d417 and only check SH_LETOCTAL option when deciding whether 'let' should skip initial zeros. src/cmd/ksh93/tests/options.sh: - Update $- test to allow '-o posix' to switch B = braceexpand. src/cmd/ksh93/sh.1: - Update. - Edit for clarity. --- NEWS | 8 +++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh.1 | 53 ++++++++++++++++++++++----------- src/cmd/ksh93/sh/args.c | 19 ++++++++++-- src/cmd/ksh93/sh/arith.c | 4 +-- src/cmd/ksh93/sh/main.c | 5 +++- src/cmd/ksh93/tests/options.sh | 3 +- 7 files changed, 69 insertions(+), 25 deletions(-) diff --git a/NEWS b/NEWS index e21d9ece6..afbbe6a83 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,14 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2020-09-18: + +- Setting the 'posix' option now turns off the 'braceexpand' option, as brace + expansion is not specified by POSIX and potentially incompatible with sh + scripts. In addition, 'set -o posix' now turns on the 'letoctal' option + instead of controlling that behaviour directly. 'set +o posix' does the + reverse of these. + 2020-09-17: - In the vi and emacs line editors, repeat count parameters can now also be diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 337d4a483..656a5ac9f 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -17,4 +17,4 @@ * David Korn * * * ***********************************************************************/ -#define SH_RELEASE "93u+m 2020-09-17" +#define SH_RELEASE "93u+m 2020-09-18" diff --git a/src/cmd/ksh93/sh.1 b/src/cmd/ksh93/sh.1 index b2166b943..ef222296c 100644 --- a/src/cmd/ksh93/sh.1 +++ b/src/cmd/ksh93/sh.1 @@ -6370,12 +6370,10 @@ to be evaluated. .B let only recognizes octal constants starting with .B 0 -if one of the +when the .B set -options +option .B letoctal -or -.B posix is on. See .I "Arithmetic Evaluation" @@ -6906,7 +6904,8 @@ is used, the variable is not unset first. .TP 8 .B \-B -Enable brace group expansion. On by default. +Enable brace group expansion. On by default, except +if ksh is invoked as \fBsh\fR, \fBrsh\fR, or \fBpfsh\fR. .TP 8 .B \-C Prevents redirection @@ -7034,8 +7033,7 @@ The .B let command allows octal constants starting with .BR 0 . -If the \fBposix\fR shell option is active, -octals are recognized regardless of this option. +On by default if ksh is invoked as \fBsh\fR, \fBrsh\fR, or \fBpfsh\fR. .TP 8 .B markdirs All directory names resulting from file name generation have a trailing @@ -7081,16 +7079,37 @@ will be the value of the last non-zero command to fail or zero if no command has failed. .TP 8 .B posix -Enable full POSIX standard compliance mode. This option -is on by default if ksh is invoked as \fBsh\fR, \fBrsh\fR, or \fBpfsh\fR. It -disables passing exported variables' attributes (such as integer or readonly) to a new ksh process through the environment, -causes file descriptors > 2 to be left open when invoking another program, -makes the \fB<>\fR redirection operator default to standard input, -causes the \fBcommand\fR utility to disable declaration command properties of any declaration commands it invokes, -skips the initialization of preset aliases upon invoking an interactive shell, -disables a hack that makes \fBtest -t\fR (\fB[ -t ]\fR) equivalent to \fBtest -t 1\fR (\fB[ -t 1 ]\fR), -enables octal numbers in \fBlet\fR shell arithmetic (see \fBletoctal\fR), and -disables the \fB&>\fR redirection shorthand. +Enables the POSIX standard mode for maximum compatibility with other compliant +shells. At the moment that the \fBposix\fR option is turned on, it also turns +on \fBletoctal\fR and turns off \fB\-B\fR/\fBbraceexpand\fR; the reverse is +done when \fBposix\fR is turned back off. (These options can still be +controlled independently in between.) Furthermore, the \fBposix\fR option +is automaticaly turned on upon invocation if ksh is invoked as \fBsh\fR, +\fBrsh\fR, or \fBpfsh\fR. In that case, or if the option is turned on by +specifying \fB-o posix\fR on the invocation command line, the invoked shell +will not set the preset aliases even if interactive, and will not import type +attributes for variables (such as integer or readonly) from the environment. +.RS 8 +.PP +In addition, while on, the \fBposix\fR option +.IP \[bu] 3 +disables exporting variable type attributes to the environment for other ksh +processes to import; +.IP \[bu] +causes file descriptors > 2 to be left open when invoking another program; +.IP \[bu] +disables the \fB&>\fR redirection shorthand; +.IP \[bu] +makes the \fB<>\fR redirection operator default to redirecting standard input +if no file descriptor number precedes it; +.IP \[bu] +causes the \fBcommand\fR utility to disable declaration command properties of +any declaration commands it invokes; +and +.IP \[bu] +disables a hack that makes \fBtest -t\fR (\fB[ -t ]\fR) equivalent to +\fBtest -t 1\fR (\fB[ -t 1 ]\fR). +.RE .TP 8 .B privileged Same as diff --git a/src/cmd/ksh93/sh/args.c b/src/cmd/ksh93/sh/args.c index cb619abac..96220d3e7 100644 --- a/src/cmd/ksh93/sh/args.c +++ b/src/cmd/ksh93/sh/args.c @@ -289,9 +289,7 @@ int sh_argopts(int argc,register char *argv[], void *context) } argc--; } - /* handling SH_INTERACTIVE and SH_PRIVILEGED has been moved to - * sh_applyopts(), so that the code can be reused from b_shopt(), too - */ + /* SH_INTERACTIVE and SH_PRIVILEGED are handled in sh_applyopts() */ sh_applyopts(ap->sh,newflags); #if SHOPT_KIA if(ap->kiafile) @@ -343,6 +341,21 @@ void sh_applyopts(Shell_t* shp,Shopt_t newflags) (shp->gd->userid==shp->gd->euserid && shp->gd->groupid==shp->gd->egroupid)) off_option(&newflags,SH_PRIVILEGED); } + /* -o posix also switches -o braceexpand and -o letoctal */ + if(!sh_isoption(SH_POSIX) && is_option(&newflags,SH_POSIX)) + { +#if SHOPT_BRACEPAT + off_option(&newflags,SH_BRACEEXPAND); +#endif + on_option(&newflags,SH_LETOCTAL); + } + else if(sh_isoption(SH_POSIX) && !is_option(&newflags,SH_POSIX)) + { +#if SHOPT_BRACEPAT + on_option(&newflags,SH_BRACEEXPAND); +#endif + off_option(&newflags,SH_LETOCTAL); + } shp->options = newflags; } diff --git a/src/cmd/ksh93/sh/arith.c b/src/cmd/ksh93/sh/arith.c index 9ca43f0d3..cea06248a 100644 --- a/src/cmd/ksh93/sh/arith.c +++ b/src/cmd/ksh93/sh/arith.c @@ -390,10 +390,10 @@ static Sfdouble_t arith(const char **ptr, struct lval *lvalue, int type, Sfdoubl char lastbase=0, *val = xp, oerrno = errno; lvalue->eflag = 0; errno = 0; - if(shp->bltindata.bnode==SYSLET && !sh_isoption(SH_LETOCTAL) && !sh_isoption(SH_POSIX)) + if(shp->bltindata.bnode==SYSLET && !sh_isoption(SH_LETOCTAL)) { /* * Since we're running the "let" builtin, disable octal number processing by - * skipping all initial zeros, unless the 'letoctal' or 'posix' option is on. + * skipping all initial zeros, unless the 'letoctal' option is on. */ while(*val=='0' && isdigit(val[1])) val++; diff --git a/src/cmd/ksh93/sh/main.c b/src/cmd/ksh93/sh/main.c index e1849a701..322a2b5ce 100644 --- a/src/cmd/ksh93/sh/main.c +++ b/src/cmd/ksh93/sh/main.c @@ -157,8 +157,11 @@ int sh_main(int ac, char *av[], Shinit_f userinit) nv_putval(PS4NOD,e_traceprompt,NV_RDONLY); path_pwd(shp,1); iop = (Sfio_t*)0; + if(sh_isoption(SH_POSIX)) + sh_onoption(SH_LETOCTAL); #if SHOPT_BRACEPAT - sh_onoption(SH_BRACEEXPAND); + else + sh_onoption(SH_BRACEEXPAND); #endif if((beenhere++)==0) { diff --git a/src/cmd/ksh93/tests/options.sh b/src/cmd/ksh93/tests/options.sh index 1e5dcf3bc..f0399a47c 100755 --- a/src/cmd/ksh93/tests/options.sh +++ b/src/cmd/ksh93/tests/options.sh @@ -533,9 +533,10 @@ print $'alias print=:\nprint foobar' > dotfile # ====== # test that '-o posix' option (not having a letter) does not affect "$-" expansion +# other than B = braceexpand ( command set +o posix 2>/dev/null - opt1=$- + opt1=${-/B/} command set -o posix 2>/dev/null opt2=$- [[ $opt1 == "$opt2" ]]