From e5e1d4b53ed60d26c90e87441243dfb39b7ceb57 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Wed, 19 May 2021 00:02:49 +0200 Subject: [PATCH] Decrease SHLVL before doing 'exec' from main shell Problem: $ exec ksh $ echo $SHLVL 2 $ exec ksh $ echo $SHLVL 3 $ exec ksh $ echo $SHLVL 4 ...etc. SHLVL is supposed to acount the number of shell processes that you need to exit before you get logged out. Since ksh was replacing itself with a new shell in the same process using 'exec', SHLVL should not increase. src/cmd/ksh93/bltins/misc.c: b_exec(): - When about to replace the shell and we're not in a subshell, decrease SHLVL to cancel out a subsequent increase by the replacing shell. Bash and zsh also do this. --- NEWS | 4 ++++ src/cmd/ksh93/bltins/misc.c | 3 +++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh.1 | 9 +++++++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 87e19b1cb..e0d06810e 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,10 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2021-05-18: + +- Fixed SHLVL so that replacing ksh by itself (exec ksh) will not increase it. + 2021-05-13: - Fixed a bug with 'test -t 1' that was introduced on 2021-04-26: diff --git a/src/cmd/ksh93/bltins/misc.c b/src/cmd/ksh93/bltins/misc.c index 852ff7da6..a517100b2 100644 --- a/src/cmd/ksh93/bltins/misc.c +++ b/src/cmd/ksh93/bltins/misc.c @@ -161,6 +161,9 @@ int b_exec(int argc,char *argv[], Shbltin_t *context) if(job_close(logdata.sh) < 0) return(1); #endif /* JOBS */ + /* if the main shell is about to be replaced, decrease SHLVL to cancel out a subsequent increase */ + if(!shgd->realsubshell) + (*SHLVL->nvalue.ip)--; /* force bad exec to terminate shell */ pp = (struct checkpt*)logdata.sh->jmplist; pp->mode = SH_JMPEXIT; diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index fa0e53090..420bc1910 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -21,7 +21,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2021-05-13" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2021-05-18" /* 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.1 b/src/cmd/ksh93/sh.1 index c07a014b9..3c5ece6e2 100644 --- a/src/cmd/ksh93/sh.1 +++ b/src/cmd/ksh93/sh.1 @@ -1802,8 +1802,8 @@ be the value that was assigned plus the number of seconds since the assignment. .TP .SM .B SHLVL -An integer variable the is incremented each time the shell -is invoked and is exported. +An integer variable that is incremented and exported +each time the shell is invoked. If .SM .B SHLVL @@ -6028,6 +6028,11 @@ is given, the command specified by the arguments is executed in place of this shell without creating a new process. +The value of the +.SM +.B SHLVL +environment variable is decreased by one, +unless the shell replaced is a subshell. The .B \-c option causes the environment to be cleared before applying