about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStephane Chazelas <stephane.chazelas@gmail.com>2016-08-30 13:44:26 +0100
committerPeter Stephenson <pws@zsh.org>2016-08-31 10:21:00 +0100
commit8ce98c75f5330eecfa474e342146b8d057abcefc (patch)
treee6a98cc00046134c2e30c214a8a5282fe193a044
parent70166178bdc7ea149eb1cd29bcdb549a392c46dd (diff)
downloadzsh-8ce98c75f5330eecfa474e342146b8d057abcefc.tar.gz
zsh-8ce98c75f5330eecfa474e342146b8d057abcefc.tar.xz
zsh-8ce98c75f5330eecfa474e342146b8d057abcefc.zip
39125: More care needed decrementing SHLVL on exec.
Not needed in subshell.
-rw-r--r--ChangeLog5
-rw-r--r--Src/exec.c11
-rw-r--r--Test/D04parameter.ztst18
3 files changed, 30 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 2f8f5b7a9..c1ef1044c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2016-08-31  Peter Stephenson  <p.stephenson@samsung.com>
+
+	* Stephane: 39125: Src/exec.c, Test/D04parameter.ztst: More care
+	decrementing SHLVL on exec; not needed in subshells.
+
 2016-08-31  Daniel Shahaf  <d.s@daniel.shahaf.name>
 
 	* 39122: Completion/Unix/Command/_git: __git_recent_branches:
diff --git a/Src/exec.c b/Src/exec.c
index 9b24d388e..2e251b939 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 063007956..75ace5a9b 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)'