zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [BUG] 'exec' runs shell functions and builtins
Date: Wed, 26 Jul 2017 10:23:53 +0100	[thread overview]
Message-ID: <20170726102353.68c3d866@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <fd2a215e-5832-f135-c69d-a1f4e1a6c99e@inlv.org>

On Tue, 25 Jul 2017 22:49:33 +0100
Martijn Dekker <martijn@inlv.org> 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 <<<'


  reply	other threads:[~2017-07-26  9:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20170725221050epcas4p131de822f47289e279c7de12de0d6c127@epcas4p1.samsung.com>
2017-07-25 21:49 ` Martijn Dekker
2017-07-26  9:23   ` Peter Stephenson [this message]
2017-07-26  9:27     ` Peter Stephenson
2017-07-26 12:53   ` Peter Stephenson
2017-08-04 15:34     ` Stephane Chazelas
2017-07-26 17:46   ` Bart Schaefer
2017-07-26 17:48     ` Bart Schaefer
2017-07-27  9:02     ` Peter Stephenson
2017-07-28 17:58       ` Bart Schaefer
2017-08-11 15:10         ` Kamil Dudka
2017-08-11 15:25           ` Peter Stephenson
2017-08-14  7:19             ` Kamil Dudka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170726102353.68c3d866@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).