From c607c48c84dc29b994d397bc31c8bd6fe6a9201b Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 1 Sep 2020 08:36:28 +0100 Subject: [PATCH] Revert <> redir FD except in posix mode (re: eeee77ed, 60516872) eeee77ed implemented a POSIX compliance fix that caused a potential incompatibility with existing ksh scripts; it made the (rarely used) read/write redirection operator, <>, default to file descriptor 0 (standard input) as POSIX specified, instead of 1 (standard output) which is traditional ksh93 behaviour. So ksh scripts needed to change all <> to 1<> to override the new default. This commit reverts that change, except in the new posix mode. src/cmd/ksh93/sh/lex.c: - Make FD for <> default to 0 in POSIX mode, 1 otherwise. src/cmd/ksh93/tests/io.sh: - Revert <> regression test changes from 60516872; we no longer need 1<> instead of <> in ksh code. --- NEWS | 3 +++ src/cmd/ksh93/sh.1 | 4 +++- src/cmd/ksh93/sh/lex.c | 2 +- src/cmd/ksh93/tests/io.sh | 6 +++--- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 003ea1148..81f04f079 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,9 @@ Any uppercase BUG_* names are modernish shell bug IDs. * causes the 'let' arithmetic command to recognise octal numbers by leading zeros regardless of the setting of the 'letoctal' option * causes file descriptors > 2 to be left open when invoking another program + * makes the <> redirection operator default to stdin instead of stdout + (this keeps the 2020-05-13 BUG_REDIRIO fix for the POSIX mode while + restoring traditional ksh93 behaviour for backwards compatibility) 2020-08-19: diff --git a/src/cmd/ksh93/sh.1 b/src/cmd/ksh93/sh.1 index 9e5bfcd3a..31e1f1406 100644 --- a/src/cmd/ksh93/sh.1 +++ b/src/cmd/ksh93/sh.1 @@ -3423,7 +3423,8 @@ otherwise, the file is created. Open file .I word\^ for reading and writing -as standard input. +as standard output. +If the \fBposix\fR option is active, it defaults to standard input instead. .TP .BI <>; word The same as @@ -7040,6 +7041,7 @@ to fail or zero if no command has failed. Enable POSIX standard compatibility mode. This option is on by default if ksh is invoked as \fBsh\fR. It causes file descriptors > 2 to be left open when invoking another program, +makes the \fB<>\fR redirection operator default to standard input, enables octal numbers in \fBlet\fR shell arithmetic (see \fBletoctal\fR), and disables the \fB&>\fR redirection shorthand. .TP 8 diff --git a/src/cmd/ksh93/sh/lex.c b/src/cmd/ksh93/sh/lex.c index 0988802d8..54507541c 100644 --- a/src/cmd/ksh93/sh/lex.c +++ b/src/cmd/ksh93/sh/lex.c @@ -601,7 +601,7 @@ int sh_lex(Lex_t* lp) c |= SYMPIPE; else if(c=='<' && n=='>') { - lp->digits = 0; + lp->digits = sh_isoption(SH_POSIX) ? 0 : 1; c = IORDWRSYM; fcgetc(n); if(fcgetc(n)==';') diff --git a/src/cmd/ksh93/tests/io.sh b/src/cmd/ksh93/tests/io.sh index f703e2c4a..5aab5dc36 100755 --- a/src/cmd/ksh93/tests/io.sh +++ b/src/cmd/ksh93/tests/io.sh @@ -304,14 +304,14 @@ fi $SHELL -c 'exec 3<>; /dev/null' 2> /dev/null && err_exit '<>; with exec should be an error' $SHELL -c ': 3<>; /dev/null' 2> /dev/null || err_exit '<>; not working with at all' print $'hello\nworld' > $tmp/1 -if ! $SHELL -c "false 1<>; $tmp/1" 2> /dev/null +if ! $SHELL -c "false <>; $tmp/1" 2> /dev/null then [[ $(<$tmp/1) == $'hello\nworld' ]] || err_exit '<>; not preserving file on failure' fi -if ! $SHELL -c "head -1 $tmp/1" 1<>; $tmp/1 2> /dev/null +if ! $SHELL -c "head -1 $tmp/1" <>; $tmp/1 2> /dev/null then [[ $(<$tmp/1) == hello ]] || err_exit '<>; not truncating file on success of head' fi print $'hello\nworld' > $tmp/1 -if ! $SHELL -c head < $tmp/1 <#((6)) 1<>; $tmp/1 2> /dev/null +if ! $SHELL -c head < $tmp/1 <#((6)) <>; $tmp/1 2> /dev/null then [[ $(<$tmp/1) == world ]] || err_exit '<>; not truncating file on success of behead' fi