From b301d41731945fe0a537277abc43f0410bff1e3f Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 1 Sep 2020 07:16:51 +0100 Subject: [PATCH] -o posix: always recognise octals in "let" builtin Though the "let" builtin is not itself a POSIX standard command, it processes standard shell arithmetic, so it should recognise octals by leading zeros as POSIX requires if the 'posix' option is on. This overrides the setting of the 'letoctal' option. Note that none of this applies to the ((...)) arithmetic command, which has always recognised leading-octal zeros and does not listen to 'letoctal'. So setting the posix mode makes this consistent. src/cmd/ksh93/sh/arith.c: - When running the 'let' builtin, test that both SH_LETOCTAL and SH_POSIX are off before stripping leading zeros to disable octal number recognition. - Cosmetic: fix spurious newline. src/cmd/ksh93/sh.1: - Document the change. src/cmd/ksh93/tests/shtests: - Make sure to disable posix mode by default for regression tests. --- NEWS | 2 ++ src/cmd/ksh93/sh.1 | 9 +++++++-- src/cmd/ksh93/sh/arith.c | 10 ++++++---- src/cmd/ksh93/tests/arith.sh | 5 ++--- src/cmd/ksh93/tests/shtests | 3 ++- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/NEWS b/NEWS index c625356c7..4c3a1caa3 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,8 @@ Any uppercase BUG_* names are modernish shell bug IDs. turned on if ksh is invoked under the name 'sh'. For now, it: * disables the &> redirection shorthand + * causes the 'let' arithmetic command to recognise octal numbers by + leading zeros regardless of the setting of the 'letoctal' option 2020-08-19: diff --git a/src/cmd/ksh93/sh.1 b/src/cmd/ksh93/sh.1 index e8648b3fa..a45891ca6 100644 --- a/src/cmd/ksh93/sh.1 +++ b/src/cmd/ksh93/sh.1 @@ -6321,10 +6321,12 @@ to be evaluated. .B let only recognizes octal constants starting with .B 0 -when the +if one of the .B set -option +options .B letoctal +or +.B posix is on. See .I "Arithmetic Evaluation" @@ -6987,6 +6989,8 @@ 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. .TP 8 .B markdirs All directory names resulting from file name generation have a trailing @@ -7034,6 +7038,7 @@ to fail or zero if no command has failed. .B posix Enable POSIX standard compatibility mode. This option is on by default if ksh is invoked as \fBsh\fR. It +enables octal numbers in \fBlet\fR shell arithmetic (see \fBletoctal\fR), and disables the \fB&>\fR redirection shorthand. .TP 8 .B privileged diff --git a/src/cmd/ksh93/sh/arith.c b/src/cmd/ksh93/sh/arith.c index 2cd406fe0..9ca43f0d3 100644 --- a/src/cmd/ksh93/sh/arith.c +++ b/src/cmd/ksh93/sh/arith.c @@ -390,8 +390,11 @@ 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)) - { + if(shp->bltindata.bnode==SYSLET && !sh_isoption(SH_LETOCTAL) && !sh_isoption(SH_POSIX)) + { /* + * Since we're running the "let" builtin, disable octal number processing by + * skipping all initial zeros, unless the 'letoctal' or 'posix' option is on. + */ while(*val=='0' && isdigit(val[1])) val++; } @@ -415,8 +418,7 @@ static Sfdouble_t arith(const char **ptr, struct lval *lvalue, int type, Sfdoubl c='e'; else c = *str; - if(c==GETDECIMAL(0) || c=='e' || c == 'E' || lastbase == - 16 && (c == 'p' || c == 'P')) + if(c==GETDECIMAL(0) || c=='e' || c == 'E' || lastbase == 16 && (c == 'p' || c == 'P')) { lvalue->isfloat=1; r = strtold(val,&str); diff --git a/src/cmd/ksh93/tests/arith.sh b/src/cmd/ksh93/tests/arith.sh index bfa6f68f8..7327eaffd 100755 --- a/src/cmd/ksh93/tests/arith.sh +++ b/src/cmd/ksh93/tests/arith.sh @@ -680,9 +680,8 @@ exp='typeset -C -a x=((typeset -C -a y=( [0]=(typeset -a -l -i z=([2]=3);));))' unset x let x=010 [[ $x == 10 ]] || err_exit 'let treating 010 as octal' -set -o letoctal -let x=010 -[[ $x == 8 ]] || err_exit 'let not treating 010 as octal with letoctal on' +(set -o letoctal; let x=010; [[ $x == 8 ]]) || err_exit 'let not treating 010 as octal with letoctal on' +(set -o posix 2>/dev/null; let x=010; [[ $x == 8 ]]) || err_exit 'let not treating 010 as octal with posix on' float z=0 integer aa=2 a=1 diff --git a/src/cmd/ksh93/tests/shtests b/src/cmd/ksh93/tests/shtests index b5d413b2d..3fa9dbfae 100755 --- a/src/cmd/ksh93/tests/shtests +++ b/src/cmd/ksh93/tests/shtests @@ -166,7 +166,8 @@ function valxml return $errors } -unset DISPLAY FIGNORE HISTFILE +command set +o posix 2>/dev/null +unset DISPLAY FIGNORE HISTFILE POSIXLY_CORRECT _AST_FEATURES export ENV=/./dev/null trap + PIPE # unadvertized -- set SIGPIPE to SIG_DFL #