zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCHv2] [bug] $SHLVL decremented for the last command of a subshell
Date: Tue, 30 Aug 2016 13:44:26 +0100	[thread overview]
Message-ID: <20160830124426.GA15615@chaz.gmail.com> (raw)
In-Reply-To: <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)'


  reply	other threads:[~2016-08-30 12:51 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-22  6:17 Stephane Chazelas
2016-08-29 15:51 ` [PATCH] " Stephane Chazelas
2016-08-30  1:55   ` Bart Schaefer
2016-08-30 12:44     ` Stephane Chazelas [this message]
2016-08-30 13:04       ` [PATCHv2] " Peter Stephenson
2016-08-30 16:46         ` Stephane Chazelas
2016-08-30 17:00           ` Peter Stephenson
2016-08-30 18:34             ` Mikael Magnusson
2016-08-31  9:02               ` Peter Stephenson
2016-08-30 19:54             ` Stephane Chazelas
2016-08-30 20:10               ` Stephane Chazelas
2016-08-31 10:06       ` Peter Stephenson
2016-09-04  5:32         ` Stephane Chazelas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160830124426.GA15615@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).