diff --git a/NEWS b/NEWS index 8785c5300..ebb10b1ad 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,13 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2021-03-16: + +- Fixed a bug in interactive shells: if a variable used by the shell called + a discipline function (such as PS1.get() or COLUMNS.set()), the value of $? + was set to the exit status of the discipline function instead of the last + command run. + 2021-03-15: - If the HOME variable is unset, the bare tilde ~ now expands to the current diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index b5dd5224d..21edffc0f 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -20,7 +20,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-alpha" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2021-03-15" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2021-03-16" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2021 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/sh/nvdisc.c b/src/cmd/ksh93/sh/nvdisc.c index 69ab3b92e..983db9313 100644 --- a/src/cmd/ksh93/sh/nvdisc.c +++ b/src/cmd/ksh93/sh/nvdisc.c @@ -289,7 +289,7 @@ static void assign(Namval_t *np,const char* val,int flags,Namfun_t *handle) nq = vp->disc[type=UNASSIGN]; if(nq && !isblocked(bp,type)) { - int bflag=0; + int bflag=0, savexit=sh.savexit; block(bp,type); if (type==APPEND && (bflag= !isblocked(bp,LOOKUPS))) block(bp,LOOKUPS); @@ -299,6 +299,7 @@ static void assign(Namval_t *np,const char* val,int flags,Namfun_t *handle) unblock(bp,LOOKUPS); if(!vp->disc[type]) chktfree(np,vp); + sh.savexit = savexit; /* avoid influencing $? */ } if(nv_isarray(np)) np->nvalue.up = up; @@ -381,6 +382,7 @@ static char* lookup(Namval_t *np, int type, Sfdouble_t *dp,Namfun_t *handle) union Value *up = np->nvalue.up; if(nq && !isblocked(bp,type)) { + int savexit = sh.savexit; node = *SH_VALNOD; if(!nv_isnull(SH_VALNOD)) { @@ -410,6 +412,7 @@ static char* lookup(Namval_t *np, int type, Sfdouble_t *dp,Namfun_t *handle) /* restore everything but the nvlink field */ memcpy(&SH_VALNOD->nvname, &node.nvname, sizeof(node)-sizeof(node.nvlink)); } + sh.savexit = savexit; /* avoid influencing $? */ } if(nv_isarray(np)) np->nvalue.up = up; diff --git a/src/cmd/ksh93/tests/pty.sh b/src/cmd/ksh93/tests/pty.sh index 7d686b19e..d35f8d14e 100755 --- a/src/cmd/ksh93/tests/pty.sh +++ b/src/cmd/ksh93/tests/pty.sh @@ -709,5 +709,29 @@ r ^:test-2: fc -lN1\r\n$ r \tdo something\r\n$ ! +# err_exit # +tst $LINENO <<"!" +L value of $? after the shell uses a variable with a discipline function + +w PS1.get() { true; }; PS2.get() { true; }; false +u PS1.get\(\) \{ true; \}; PS2.get\(\) \{ true; \}; false +w echo "Exit status is: $?" +u Exit status is: 1 +w LINES.set() { return 13; } +u LINES.set\(\) \{ return 13; \} +w echo "Exit status is: $?" +u Exit status is: 0 + +# It's worth noting that the test below will always fail in ksh93u+ and ksh2020, +# even when $PS2 lacks a discipline function (see https://github.com/ksh93/ksh/issues/117). +# After that bug was fixed the test below could still fail if PS2.get() existed. +w false +w ( +w exit +w ) +w echo "Exit status is: $?" +u Exit status is: 1 +! + # ====== exit $((Errors<125?Errors:125)) diff --git a/src/cmd/ksh93/tests/return.sh b/src/cmd/ksh93/tests/return.sh index e5564cf3f..c322a60c6 100755 --- a/src/cmd/ksh93/tests/return.sh +++ b/src/cmd/ksh93/tests/return.sh @@ -237,5 +237,8 @@ foo && err_exit "'exit' within 'for' with redirection does not preserve exit sta foo() ( false; { exit; } 2>&1 ) foo && err_exit "'exit' within { block; } with redirection does not preserve exit status" +foo() { false; (exit); } +foo && err_exit "'exit' within subshell does not preserve exit status" + # ====== exit $((Errors<125?Errors:125))