zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH 0/1] Run final pipeline command in a subshell in sh mode
@ 2019-11-24 23:41 brian m. carlson
  2019-11-24 23:41 ` [PATCH 1/1] exec: run " brian m. carlson
  2019-12-07 19:23 ` [PATCH 0/1] Run " brian m. carlson
  0 siblings, 2 replies; 3+ messages in thread
From: brian m. carlson @ 2019-11-24 23:41 UTC (permalink / raw)
  To: zsh-workers

POSIX sh implementations run each command in a pipeline in a subshell,
although zsh (and AT&T ksh) do not: instead, they run the final command
in the main shell.

zsh is starting to be used in some cases as /bin/sh, such as on macOS
Catalina.  Whether this is a good idea or not, it makes sense to emulate
the POSIX behavior as much as possible when emulating sh, since that's
the least surprising behavior.  This patch does exactly that.

With this patch, using "zsh --emulate sh" passes all but one test of the
Git testsuite.  The remaining failure is due to zsh preserving NUL bytes
in the output of command substitutions, which is permitted by POSIX; I
will be sending a patch to fix that bug in the Git testsuite.

I will admit that some of the tests included look bizarre, such as
piping to an assignment, but I felt it was important to hit as many
cases as possible.

I'm not subscribed to the list, so please CC me with any comments, and
I'll try to address them promptly.  If you'd like me to include a
changelog entry, please say so, and I'm happy to include one.

brian m. carlson (1):
  exec: run final pipeline command in a subshell in sh mode

 Src/exec.c           | 10 ++++++----
 Test/B07emulate.ztst | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/1] exec: run final pipeline command in a subshell in sh mode
  2019-11-24 23:41 [PATCH 0/1] Run final pipeline command in a subshell in sh mode brian m. carlson
@ 2019-11-24 23:41 ` brian m. carlson
  2019-12-07 19:23 ` [PATCH 0/1] Run " brian m. carlson
  1 sibling, 0 replies; 3+ messages in thread
From: brian m. carlson @ 2019-11-24 23:41 UTC (permalink / raw)
  To: zsh-workers

zsh typically runs the final command in a pipeline in the main shell
instead of a subshell.  However, POSIX requires that all commands in a
pipeline run in a subshell, but permits zsh's behavior as an extension.

Since zsh may be used as /bin/sh in some cases (such as macOS Catalina),
it makes sense to have the POSIX behavior when emulating sh, so do that
by checking for being the final item of a multi-item pipeline and
creating a subshell in that case.
---
 Src/exec.c           | 10 ++++++----
 Test/B07emulate.ztst | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/Src/exec.c b/Src/exec.c
index 6014ec9a5..66dc8db2d 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -2861,11 +2861,13 @@ execcmd_exec(Estate state, Execcmd_params eparams,
 	    pushnode(args, dupstring("fg"));
     }
 
-    if ((how & Z_ASYNC) || output) {
+    if ((how & Z_ASYNC) || output ||
+	(last1 == 2 && input && EMULATION(EMULATE_SH))) {
 	/*
-	 * If running in the background, or not the last command in a
-	 * pipeline, we don't need any of the rest of this function to
-	 * affect the state in the main shell, so fork immediately.
+	 * If running in the background, not the last command in a
+	 * pipeline, or the last command in a multi-stage pipeline
+	 * in sh mode, we don't need any of the rest of this function
+	 * to affect the state in the main shell, so fork immediately.
 	 *
 	 * In other cases we may need to process the command line
 	 * a bit further before we make the decision.
diff --git a/Test/B07emulate.ztst b/Test/B07emulate.ztst
index 7b1592fa9..45c39b51d 100644
--- a/Test/B07emulate.ztst
+++ b/Test/B07emulate.ztst
@@ -276,3 +276,25 @@ F:Some reserved tokens are handled in alias expansion
 0:--emulate followed by other options
 >yes
 >no
+
+  emulate sh -c '
+  foo () {
+    VAR=foo &&
+    echo $VAR | bar &&
+    echo "$VAR"
+  }
+  bar () {
+    tr f b &&
+    VAR="$(echo bar | tr r z)" &&
+    echo "$VAR"
+  }
+  foo
+  '
+  emulate sh -c 'func() { echo | local def="abc"; echo $def;}; func'
+  emulate sh -c 'abc="def"; echo | abc="ghi"; echo $abc'
+0:emulate sh uses subshell for last pipe entry
+>boo
+>baz
+>foo
+>
+>def

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 0/1] Run final pipeline command in a subshell in sh mode
  2019-11-24 23:41 [PATCH 0/1] Run final pipeline command in a subshell in sh mode brian m. carlson
  2019-11-24 23:41 ` [PATCH 1/1] exec: run " brian m. carlson
@ 2019-12-07 19:23 ` brian m. carlson
  1 sibling, 0 replies; 3+ messages in thread
From: brian m. carlson @ 2019-12-07 19:23 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 907 bytes --]

> POSIX sh implementations run each command in a pipeline in a subshell,
> although zsh (and AT&T ksh) do not: instead, they run the final command
> in the main shell.
> 
> zsh is starting to be used in some cases as /bin/sh, such as on macOS
> Catalina.  Whether this is a good idea or not, it makes sense to emulate
> the POSIX behavior as much as possible when emulating sh, since that's
> the least surprising behavior.  This patch does exactly that.
> 
> With this patch, using "zsh --emulate sh" passes all but one test of the
> Git testsuite.  The remaining failure is due to zsh preserving NUL bytes
> in the output of command substitutions, which is permitted by POSIX; I
> will be sending a patch to fix that bug in the Git testsuite.

Gentle ping.  Is there any feedback that folks have on this patch?
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 868 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-12-07 19:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-24 23:41 [PATCH 0/1] Run final pipeline command in a subshell in sh mode brian m. carlson
2019-11-24 23:41 ` [PATCH 1/1] exec: run " brian m. carlson
2019-12-07 19:23 ` [PATCH 0/1] Run " brian m. carlson

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).