Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCHv2] [bug] $SHLVL decremented for the last command of a subshell
- X-seq: zsh-workers 39125
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Subject: [PATCHv2] [bug] $SHLVL decremented for the last command of a subshell
- Date: Tue, 30 Aug 2016 13:44:26 +0100
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=kPoQzF00qQS/y/fplfR2FeT6vfj0Zqro11Ao11WWJwY=; b=CG7yEeGR5Srrg/qOZClLIS8J0ZCm3CY8d2988Xot7zpUUowpvbrHtA9nqcPr4RAwyp LhxbfXBzjXG8MnBuVEkg+Ic+D8/Pe/5ym9hl5uPa6zRDVR9Yoe5G4fSEUyrzZiURcyM3 8F5wg2Q5wykoLDnbyCwoyHx5YX3OwgeF/0p4DXrPCOjlmO3gJriZIh9kPuk9cwl/PKlX abNLr016tiEtv4nFsMkgljhLkUngUXmq3nBmzaDzw1pUW8g+ox3laIlPGnx5mdMk4q4+ P96l53NwNT+grjU+2cY/HmSpkGaUeNTlU/+kRIe9wwGghtK8B6J+rzj6km/zsLqoffvQ d2sA==
- In-reply-to: <160829185505.ZM8699@torch.brasslantern.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mail-followup-to: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <20160822061723.GA7308@chaz.gmail.com> <20160829155156.GB17700@chaz.gmail.com> <160829185505.ZM8699@torch.brasslantern.com>
2016-08-29 18:55:05 -0700, Bart Schaefer:
> On Aug 29, 4:51pm, Stephane Chazelas wrote:
> }
> } I think the change below should be enough (also condensed the 2
> } "if"s below (unrelated)):
>
> This looks fine, although I think we generally keep the braces on the
> "if" body whenever there is more than one line following -- even if
> some of those lines are comments.
>
> Either that or move the comment above the "if".
How about this (factorised the !subsh some more comments and
added some test cases):
diff --git a/Src/exec.c b/Src/exec.c
index 9b24d38..2e251b9 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3694,12 +3694,15 @@ execcmd(Estate state, int input, int output, int how, int last1)
restore_params(restorelist, removelist);
} else {
- if (!forked)
- setiparam("SHLVL", --shlvl);
- if (do_exec) {
+ if (!subsh) {
+ /* for either implicit or explicit "exec", decrease $SHLVL
+ * as we're now done as a shell */
+ if (!forked)
+ setiparam("SHLVL", --shlvl);
+
/* If we are exec'ing a command, and we are not *
* in a subshell, then save the history file. */
- if (!subsh && isset(RCS) && interact && !nohistsave)
+ if (do_exec && isset(RCS) && interact && !nohistsave)
savehistfile(NULL, 1, HFILE_USE_OPTIONS);
}
if (type == WC_SIMPLE || type == WC_TYPESET) {
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index 0630079..75ace5a 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1718,6 +1718,24 @@
>2
>2
+ SHLVL=1
+ $ZTST_testdir/../Src/zsh -fc 'sh -c "echo \$SHLVL"'
+ $ZTST_testdir/../Src/zsh -fc '(sh -c "echo \$SHLVL")'
+ $ZTST_testdir/../Src/zsh -fc '( (sh -c "echo \$SHLVL"))'
+0:SHLVL decremented upon implicit exec optimisation
+>1
+>1
+>1
+
+ SHLVL=1
+ $ZTST_testdir/../Src/zsh -fc '(sh -c "echo \$SHLVL"); exit'
+ $ZTST_testdir/../Src/zsh -fc '(exec sh -c "echo \$SHLVL"); exit'
+ $ZTST_testdir/../Src/zsh -fc '( (sh -c "echo \$SHLVL"); exit)'
+0:SHLVL not decremented upon exec in subshells
+>2
+>2
+>2
+
# The following tests the return behaviour of parsestr/parsestrnoerr
alias param-test-alias='print $'\''\x45xpanded in substitution'\'
param='$(param-test-alias)'
Messages sorted by:
Reverse Date,
Date,
Thread,
Author