Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: command -p should enable builtins not in path
- X-seq: zsh-workers 47331
- From: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: command -p should enable builtins not in path
- Date: Fri, 21 Aug 2020 14:03:27 +0100 (BST)
- Archived-at: <https://zsh.org/workers/47331>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2020-08/1194318329.1319677.1598015007835%40mail2.virginmedia.com>
- Authentication-results: zsh.org; iprev=pass (smtpq2.tb.ukmail.iss.as9143.net) smtp.remote-ip=212.54.57.97; dkim=pass header.d=ntlworld.com header.s=meg.feb2017 header.a=rsa-sha256; dmarc=skipped; arc=none
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ntlworld.com; s=meg.feb2017; t=1598015007; bh=SDMh+6f9zJ2940FARViJssHSkXrQrrW1El/8sstbTKo=; h=Date:From:To:In-Reply-To:References:Subject; b=QM+pgrCAGSNmXf4avk5MqRhBnx9tgm+ca5UmCl0ikoMehbGEDigNc1OmOmJhOK5jo 0qS0Ni6oH282qUJ9aGb8YvNhVjCWgGYT4tafRe0CysGmCtnF4HxRz0bmdhg2wvH8es TXZpcEYmId5wDwROLyqpQbWwBoyw9yO5A10LGfbWJw/wRi+po9XJ0Js0PkJytNNGYG HP3Feaq2VAkATLMtyVuavmhq0cCUsF8mTTGDKKG2E7lID22DK3km0c4wYBje3MK54D /1624b5+2s/SNtARx+eeA4rB/WzLwCRiBUM1RPiUglLUHO8q+IOLP6srL+dlHHMVgq PDJNYkGXuUZTQ==
- Importance: Medium
- In-reply-to: <20200820112815.GA216072@zira.vinc17.org>
- List-archive: <http://www.zsh.org/sympa/arc/zsh-workers>
- List-help: <mailto:sympa@zsh.org?subject=help>
- List-id: <zsh-workers.zsh.org>
- List-owner: <mailto:zsh-workers-request@zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-subscribe: <mailto:sympa@zsh.org?subject=subscribe%20zsh-workers>
- List-unsubscribe: <mailto:sympa@zsh.org?subject=unsubscribe%20zsh-workers>
- References: <20200820112815.GA216072@zira.vinc17.org>
- Sender: zsh-workers-request@xxxxxxx
> On 20 August 2020 at 12:28 Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
>
>
> Though zsh isn't meant to conform to POSIX, it should follow its
> requirements when they make sense.
>
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html
>
> says:
>
> The following options shall be supported:
>
> -p
> Perform the command search using a default value for PATH that is
> guaranteed to find all of the standard utilities.
> ^^^
>
> So, in particular, the standard utility "cd" must be found. Note that
> the above sentence does not mean that the utility must be somewhere
> in $PATH, just that the used PATH value allows the shell to find the
> utility; in case of a builtin utility (like "cd"), this will just run
> the utility without needing PATH.
>
> This works with all usual shells, except zsh:
I tend to agree there's not a lot of point in this incompatibility,
given where the "-p" flag originates --- it's a relatively late
addition to zsh.
Here's one possible fix.
pws
diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index 4b91db1fe..17ebba2ac 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -318,10 +318,15 @@ item(tt(command) [ tt(-pvV) ] var(simple command))(
The simple command argument is taken as an external command instead of
a function or builtin and is executed. If the tt(POSIX_BUILTINS) option
is set, builtins will also be executed but certain special properties
-of them are suppressed. The tt(-p) flag causes a default path to be
-searched instead of that in tt($path). With the tt(-v) flag, tt(command)
-is similar to tt(whence) and with tt(-V), it is equivalent to tt(whence
--v).
+of them are suppressed.
+
+The tt(-p) flag causes a default path to be searched instead of that in
+tt($path). The intention of the option is that all standard commands
+may be found, so builtins are also checked, but not other types of
+command such as functions.
+
+With the tt(-v) flag, tt(command) is similar to tt(whence) and with
+tt(-V), it is equivalent to tt(whence -v).
See also ifzman(the section `Precommand Modifiers' in zmanref(zshmisc))\
ifnzman(noderef(Precommand Modifiers)).
diff --git a/Src/exec.c b/Src/exec.c
index ecad923de..f18513703 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3165,8 +3165,19 @@ execcmd_exec(Estate state, Execcmd_params eparams,
}
}
hn = NULL;
- if ((cflags & BINF_COMMAND) && unset(POSIXBUILTINS))
+ if ((cflags & BINF_COMMAND) && unset(POSIXBUILTINS)) {
+ if (use_defpath && nonempty(preargs)) {
+ /*
+ * command -p can find builtins as these are
+ * in a standard location. So check.
+ */
+ if ((hn = builtintab->getnode(builtintab,
+ (char *) peekfirst(preargs)))) {
+ checked = is_builtin = 1;
+ }
+ }
break;
+ }
if (!nonempty(preargs))
execcmd_getargs(preargs, args, eparams->htok);
}
diff --git a/Test/A01grammar.ztst b/Test/A01grammar.ztst
index 35a04e7d5..e62fb531b 100644
--- a/Test/A01grammar.ztst
+++ b/Test/A01grammar.ztst
@@ -200,6 +200,17 @@
>cat is /*/cat
>echo is a shell builtin
+ (mkdir testdir_for_command-p
+ command -p cd testdir_for_command-p
+ print ${PWD##*/})
+0:command -p finds builtins
+>testdir_for_command-p
+
+ func_not_searched() { This was not found; }
+ command -p func_not_searched
+127:command -p does not find functions
+?(eval):2: command not found: func_not_searched
+
cd() { echo Not cd at all; }
builtin cd . && unfunction cd
0:`builtin' precommand modifier
Messages sorted by:
Reverse Date,
Date,
Thread,
Author