about summary refs log tree commit diff
diff options
context:
space:
mode:
-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)'