zsh/Src
brian m. carlson f7a417388c 47794: exec: run final pipeline command in a subshell in sh mode
zsh typically runs the final command in a pipeline in the main shell
instead of a subshell.  However, POSIX specifies that all commands in a
pipeline run in a subshell, but permits zsh's behavior as an extension.
The default /bin/sh implementations on various Linux distros and the
BSDs always use a subshell for all components of a pipeline.

Since zsh may be used as /bin/sh in some cases (such as macOS Catalina),
it makes sense to have the common sh 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.

From the comment above execpline(), we know the following:

 last1 is a flag that this command is the last command in a shell that
 is about to exit, so we can exec instead of forking.  It gets passed
 all the way down to execcmd() which actually makes the decision.  A 0
 is always passed if the command is not the last in the pipeline. […]
 If last1 is zero but the command is at the end of a pipeline, we pass
 2 down to execcmd().

So there are three cases to consider in this code:

• last1 is 0, which means we are not at the end of a pipeline, in which
 case we should not change behavior.
• last1 is 1, which means we are effectively running in a subshell,
 because nothing that happens due to the exec is going to affect the
 actual shell, since it will have been replaced.  So there is nothing
 to do here.
• last1 is 2, which means our command is at the end of the pipeline, so
 in sh mode we should create a subshell by forking.

input is nonzero if the input to this process is a pipe that we've
opened.  At the end of a multi-stage pipeline, it will necessarily be
nonzero.

Note that several of the tests may appear bizarre, since most developers
do not place useless variable assignments directly at the end of a
pipeline.  However, as the function tests demonstrate, there are cases
where assignments may occur when a shell function is used at the end of
a command.  The remaining assignment tests simply test additional cases,
such as the use of local, that would otherwise be untested.
2021-04-10 17:56:39 -05:00
..
Builtins unposted: add a comment about how to add a new resource 2020-05-03 18:38:36 +09:00
Modules 48432 and enable test: fix quotiing of zstyle -L for zstyle -e 2021-04-09 21:01:37 +01:00
Zle 48391: fix display problem in menu-complete 2021-04-06 23:38:52 +09:00
.cvsignore unposted: .cvsignore patchlevel.h 2009-02-18 17:11:16 +00:00
.distfiles 33047: use git to simplify massively source distribution file selection 2014-08-23 21:01:58 +01:00
.exrc Initial revision 1999-04-15 18:05:38 +00:00
.indent.pro Initial revision 1999-10-17 18:01:49 +00:00
builtin.c users/26509: fix for r -L 2021-02-18 21:37:08 +00:00
compat.c 45708: zsh/system: Enable sub-second timeout in zsystem flock 2020-04-20 12:10:01 -05:00
cond.c 42031 + 42048: Make [[ -o invalidoption ]] a normal(ish) false value, rather than a syntax error. 2017-11-24 22:10:27 +00:00
context.c 35642: signal queueing in zcontext_save_partial() 2015-06-27 15:53:21 -07:00
exec.c 47794: exec: run final pipeline command in a subshell in sh mode 2021-04-10 17:56:39 -05:00
glob.c 45291: A glob with a trailing slash will now match unreadable/unexecutable directories. 2020-01-15 19:20:33 -06:00
hashnameddir.c 29195: Separate nameddir hash to avoid clash of 2011-05-09 10:38:02 +00:00
hashtable.c 46275: eliminate undefined behavior caused by redefinition of hashtable 2020-07-29 08:27:15 +02:00
hashtable.h 43616: Various parameter setting and display fixes. 2018-10-08 10:10:42 +01:00
hist.c 45583/0003: chrealpath: Let caller decide how the return value should be allocated. 2020-03-25 23:49:01 +00:00
init.c 45660: Fix crash setting vi or emacs mode on command line. 2020-04-09 21:46:20 +01:00
input.c 43511: Initialiase alias entry on input stack. 2018-09-21 16:29:07 +01:00
jobs.c 46060: Fix spurious actions on exit status 130 or 131. 2020-06-18 09:27:05 +01:00
lex.c 45900: Fix issues with escaped newline in $-substitution. 2020-05-23 18:01:26 +01:00
linklist.c 45729: internal: Add a second parameter to zlinklist2array(), analogously to hlinklist2array(). 2020-05-03 01:27:36 +00:00
loop.c 47296 (+ unposted additional tests and comments): 'repeat' loops: Let the repeat count use $?. 2020-08-08 06:11:00 +00:00
main.c 45004: Fix typos in comments 2019-12-11 02:37:39 +00:00
Makefile.in 45065: Make 'make -s' print nothing when it does nothing. 2019-12-18 06:01:49 +00:00
Makemod.in.in 29214: use -I rather than explicit path to find headers 2011-05-10 16:40:27 +00:00
makepro.awk 44637: = doesn't need quoting in awk regexp. 2019-08-03 10:57:02 +01:00
math.c 48389: getkeystring() should not return ptr to local var 2021-04-06 23:05:03 +09:00
mem.c 47785: remove deprecated autoconf functions 2021-04-09 21:45:11 +02:00
mkbltnmlst.sh 27648, 267650/1, unposted README change: 2010-02-03 18:34:32 +00:00
mkmakemod.sh 36663: replace obsolete "autobins" commentary with "autofeatures" 2015-09-27 12:43:25 -07:00
modentry.c 23571 + unposted; AIX compilation fixes 2007-06-19 20:21:52 +00:00
module.c 45004: Fix typos in comments 2019-12-11 02:37:39 +00:00
openssh_bsd_setres_id.c Improve PRIVILEGED fixes 2020-02-14 16:06:57 -06:00
options.c 47913: implement CASE_PATHS option to make NO_CASE_GLOB more sensible 2021-04-10 14:26:46 -07:00
params.c 45772: Restore locale on parameter scope end. 2020-05-05 20:28:31 +01:00
parse.c 47300: Document the EC_NODUP, EC_DUP, EC_DUPTOK triplet. 2020-08-08 06:01:32 +00:00
pattern.c 47913: implement CASE_PATHS option to make NO_CASE_GLOB more sensible 2021-04-10 14:26:46 -07:00
prompt.c 47510: drop code that avoided termcap for named colours 2021-04-03 00:58:45 +02:00
prototypes.h 32737, 32736 (32741), 32735, 32734, 32733, 32732 (32739): Strict compilation 2014-06-07 13:48:40 -07:00
signals.c users/24710: Fix job control problem with sudo. 2020-02-27 20:42:16 +00:00
signals.h 433029: Testing signal return type is no longer needed 2018-06-18 10:18:29 +01:00
signames1.awk Initial revision 1999-04-15 18:05:38 +00:00
signames2.awk 20566++: prefer SIGCHLD to SIGCLD and SIGPOLL to SIGIO 2004-11-19 09:57:22 +00:00
sort.c users/24971: ${(-)var} sorts on signed integers 2020-07-03 21:05:46 +01:00
string.c 41096: Don't assume null termination copying string. 2017-05-11 17:33:30 +01:00
subst.c 48379: Make the parameter expansion subscript flags parse error message, "error in flags", identify the location of the parse error. 2021-04-08 02:19:39 +00:00
text.c 45583/0007: WC_FUNCDEF: Add a placeholder element. 2020-03-22 02:23:53 +00:00
utils.c 48389: getkeystring() should not return ptr to local var 2021-04-06 23:05:03 +09:00
watch.c 45004: Fix typos in comments 2019-12-11 02:37:39 +00:00
wcwidth9.h 41090: Replace iswprint() if unicode9 is enabled. 2017-05-12 12:10:13 +09:00
zsh.h 47913: implement CASE_PATHS option to make NO_CASE_GLOB more sensible 2021-04-10 14:26:46 -07:00
zsh.ico pws/Andrej: 12594, 12661: Windows resources and icon 2000-08-16 13:23:37 +00:00
zsh.mdd Improve PRIVILEGED fixes 2020-02-14 16:06:57 -06:00
zsh.rc pws/Andrej: 12594, 12661: Windows resources and icon 2000-08-16 13:23:37 +00:00
zsh_system.h 47785: remove deprecated autoconf functions 2021-04-09 21:45:11 +02:00
ztype.h 45269: Fix misspellings in completions and elsewhere. 2020-01-09 13:42:02 +00:00