Skip to content

Commit

Permalink
-o posix: always recognise octals in "let" builtin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
McDutchie committed Sep 1, 2020
1 parent 921bbca commit b301d41
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
9 changes: 7 additions & 2 deletions src/cmd/ksh93/sh.1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/cmd/ksh93/sh/arith.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand All @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/ksh93/tests/arith.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/ksh93/tests/shtests
Original file line number Diff line number Diff line change
Expand Up @@ -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 #

Expand Down

0 comments on commit b301d41

Please sign in to comment.