summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--Doc/Zsh/params.yo7
-rw-r--r--Src/exec.c7
-rw-r--r--Src/params.c4
-rw-r--r--Test/D04parameter.ztst15
5 files changed, 37 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index dc1a78ce1..21f556465 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2008-09-05  Peter Stephenson  <p.w.stephenson@ntlworld.com>
+
+	* 25627: Doc/Zsh/params.yo, Src/exec.c, Src/params.c,
+	Test/D04parameter.ztst: add $ZSH_SUBSHELL.
+
 2008-09-05  Peter Stephenson  <pws@csr.com>
 
 	* 25615: Doc/Zsh/builtins.yo, Doc/Zsh/func.yo, Src/exec.c,
diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index bf12b5ded..bc8a79450 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -731,6 +731,13 @@ item(tt(zsh_scheduled_events))(
 See ifzman(the section `The zsh/sched Module' in zmanref(zshmodules))\
 ifnzman(noderef(The zsh/sched Module)).
 )
+vindex(ZSH_SUBSHELL <S>)
+item(tt(ZSH_SUBSHELL))(
+Readonly integer.  Initially zero, incremented each time the shell forks
+to create a subshell for executing code.  Hence `tt((print $ZSH_SUBSHELL))'
+and `tt(print $(print $ZSH_SUBSHELL))' output 1, while
+`tt(( (print $ZSH_SUBSHELL) ))' outputs 2.
+)
 vindex(ZSH_VERSION)
 item(tt(ZSH_VERSION))(
 The version number of this zsh.
diff --git a/Src/exec.c b/Src/exec.c
index a147dacd5..e638b9f6e 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -910,6 +910,13 @@ entersubsh(int flags)
     }
     if (!(flags & ESUB_FAKE))
 	subsh = 1;
+    /*
+     * Increment the visible parameter ZSH_SUBSHELL even if this
+     * is a fake subshell because we are exec'ing at the end.
+     * Logically this should be equivalent to a real subshell so
+     * we don't hang out the dirty washing.
+     */
+    zsh_subshell++;
     if ((flags & ESUB_REVERTPGRP) && getpid() == mypgrp)
 	release_pgrp();
     if (SHTTY != -1) {
diff --git a/Src/params.c b/Src/params.c
index 0bd909905..39bc8515a 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -85,7 +85,8 @@ zlong lastval,		/* $?           */
      lastpid,		/* $!           */
      columns,		/* $COLUMNS     */
      lines,		/* $LINES       */
-     ppid;		/* $PPID        */
+     ppid,		/* $PPID        */
+     zsh_subshell;	/* $ZSH_SUBSHELL */
 /**/
 zlong lineno,		/* $LINENO      */
      zoptind,		/* $OPTIND      */
@@ -291,6 +292,7 @@ IPDEF4("?", &lastval),
 IPDEF4("HISTCMD", &curhist),
 IPDEF4("LINENO", &lineno),
 IPDEF4("PPID", &ppid),
+IPDEF4("ZSH_SUBSHELL", &zsh_subshell),
 
 #define IPDEF5(A,B,F) {{NULL,A,PM_INTEGER|PM_SPECIAL},BR((void *)B),GSU(varinteger_gsu),10,0,NULL,NULL,NULL,0}
 IPDEF5("COLUMNS", &columns, zlevar_gsu),
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index 433f7743b..721396e73 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1047,3 +1047,18 @@
 >and !that!
 >of *this*
 >or (the|other)
+
+  print $ZSH_SUBSHELL
+  (print $ZSH_SUBSHELL)
+  ( (print $ZSH_SUBSHELL) )
+  ( (print $ZSH_SUBSHELL); print $ZSH_SUBSHELL )
+  print $(print $ZSH_SUBSHELL)
+  cat =(print $ZSH_SUBSHELL)
+0:ZSH_SUBSHELL
+>0
+>1
+>2
+>2
+>1
+>1
+>1