From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13220 invoked by alias); 26 Jul 2017 09:24:07 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 41464 Received: (qmail 5725 invoked from network); 26 Jul 2017 09:24:07 -0000 X-Qmail-Scanner-Diagnostics: from mailout2.w1.samsung.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(210.118.77.12):SA:0(-5.0/5.0):. Processed in 3.235415 secs); 26 Jul 2017 09:24:07 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-5.0 required=5.0 tests=RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: p.stephenson@samsung.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.samsung.com designates 210.118.77.12 as permitted sender) X-AuditID: cbfec7f2-f797e6d000004438-0c-59785fac384d Date: Wed, 26 Jul 2017 10:23:53 +0100 From: Peter Stephenson To: Zsh hackers list Subject: Re: [BUG] 'exec' runs shell functions and builtins Message-id: <20170726102353.68c3d866@pwslap01u.europe.root.pri> In-reply-to: Organization: Samsung Cambridge Solution Centre X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrLIsWRmVeSWpSXmKPExsWy7djP87pr4isiDZ7O1LE42PyQyYHRY9XB D0wBjFFcNimpOZllqUX6dglcGU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Lm8BsNgFDiambZjOC 2CICWhI7Tp4E2yEsYC0xYfdrdhCbV8Be4vWtuUD1HBycAjYS06YWgoSFBCYySqx4Wwpi8wvo S1z9+wnqPHuJmVfOMEK0Ckr8mHwPbBUz0PjN25pYIWx5ic1r3jJDzFGXuHF3N/sERuFZSFpm IWmZhaRlASPzKkaR1NLi3PTcYkO94sTc4tK8dL3k/NxNjMAo2nbs5+YdjJc2Bh9iFOBgVOLh NfAqjxRiTSwrrsw9xCjBwawkwssKjEEh3pTEyqrUovz4otKc1OJDjKbAcJnILCWanA+M8LyS eEMTQ3NLQyNjCwtzIyMlcd6SD1fChQTSE0tSs1NTC1KLYPqYODilGhh72yx+L+xY/qJZ5JmW h+rhsx7ztwfHRb+cbsW48krPbhU9/euGf3uZ3m4SMOIP+9uw1UD6xMtLPE5VT4OvuixkLra5 xNOXsP6FxqHH3yO5LVsyux2dOZS7xQ5dn5p4iNVywQrG5MvSc0WS3EWESq17Nxr7x59KmlJn d3e9ieiLX5qHvOfUpymxFGckGmoxFxUnAgDD4vmYuAIAAA== X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170726092356eucas1p2655c1b0e6dbbb8c94566fd3774adcfd0 X-Msg-Generator: CA X-Sender-IP: 182.198.249.179 X-Local-Sender: =?UTF-8?B?UGV0ZXIgU3RlcGhlbnNvbhtTQ1NDLURhdGEgUGxhbmUb?= =?UTF-8?B?7IK87ISx7KCE7J6QG1ByaW5jaXBhbCBFbmdpbmVlciwgU29mdHdhcmU=?= X-Global-Sender: =?UTF-8?B?UGV0ZXIgU3RlcGhlbnNvbhtTQ1NDLURhdGEgUGxhbmUbU2Ft?= =?UTF-8?B?c3VuZyBFbGVjdHJvbmljcxtQcmluY2lwYWwgRW5naW5lZXIsIFNvZnR3YXJl?= X-Sender-Code: =?UTF-8?B?QzEwG0VIURtDMTBDRDA1Q0QwNTAwNTg=?= CMS-TYPE: 201P X-HopCount: 7 X-CMS-RootMailID: 20170725221050epcas4p131de822f47289e279c7de12de0d6c127 X-RootMTR: 20170725221050epcas4p131de822f47289e279c7de12de0d6c127 References: On Tue, 25 Jul 2017 22:49:33 +0100 Martijn Dekker wrote: > In zsh, 'exec' looks up shell functions and builtins before external > commands, and if it finds one it appears to do the equivalent of running > the function or builtin followed by 'exit'. This is different from most > other shells and turns out[1] to be a bug in POSIX terms; 'exec' is > supposed to launch a program that overlays the current shell[2], > implying the program launched by 'exec' is always external to the shell. Prior art suggests we can get away with adding this behaviour to the POSIX_BUILTINS option. (I'd like to hope people don't actually set the POSIX options separately anyway, but feel free not to tell me.) pws diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo index cc6ae2a..edf43d4 100644 --- a/Doc/Zsh/options.yo +++ b/Doc/Zsh/options.yo @@ -2140,6 +2140,10 @@ In addition, various error conditions associated with the above builtins or tt(exec) cause a non-interactive shell to exit and an interactive shell to return to its top-level processing. +Furthermore, functions ahd shell builtins are not executed after +an tt(exec) prefix; the command to be executed must be an external +command found in the path. + Furthermore, the tt(getopts) builtin behaves in a POSIX-compatible fashion in that the associated variable tt(OPTIND) is not made local to functions. diff --git a/Src/exec.c b/Src/exec.c index 0a96879..f339dd6 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -2778,6 +2778,12 @@ execcmd_exec(Estate state, Execcmd_params eparams, * Reserved words take precedence over shell functions. */ checked = 1; + } else if (isset(POSIXBUILTINS) && (cflags & BINF_EXEC)) { + /* + * POSIX doesn't allow "exec" to operate on builtins + * or shell functions. + */ + break; } else { if (!(cflags & (BINF_BUILTIN | BINF_COMMAND)) && (hn = shfunctab->getnode(shfunctab, cmdarg))) { @@ -3123,10 +3129,14 @@ execcmd_exec(Estate state, Execcmd_params eparams, * - we have determined there are options which would * require us to use the "command" builtin); or * - we aren't using POSIX and so BINF_COMMAND indicates a zsh - * precommand modifier is being used in place of the builtin + * precommand modifier is being used in place of the + * builtin + * - we are using POSIX and this is an EXEC, so we can't + * execute a builtin or function. */ if (errflag || checked || is_builtin || - (unset(POSIXBUILTINS) && (cflags & BINF_COMMAND))) + (isset(POSIXBUILTINS) ? + (cflags & BINF_EXEC) : (cflags & BINF_COMMAND))) break; cmdarg = (char *) peekfirst(args); diff --git a/Test/E01options.ztst b/Test/E01options.ztst index dac9430..f01d835 100644 --- a/Test/E01options.ztst +++ b/Test/E01options.ztst @@ -831,6 +831,20 @@ >val2 >val2 + print "Contents of file" >cat_arg + ( + cat() { print Function with argument $1 } + print Without + (exec cat cat_arg; print Not reached) + print With + (setopt posixbuiltins; exec cat cat_arg; print Not reached) + ) +0:POSIX_BUILTINS and exec +>Without +>Function with argument cat_arg +>With +>Contents of file + # PRINTEXITVALUE only works if shell input is coming from standard input. # Goodness only knows why. $ZTST_testdir/../Src/zsh -f <<<'