summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortb <tb@openbsd.org>2016-03-21 13:35:00 +0000
committertb <tb@openbsd.org>2016-03-21 13:35:00 +0000
commitf5230a3a326c78e0803569fb78c8693d79f2bb70 (patch)
tree8740d8c51356219fb6613e964f66e994142a8608
parentThe common part of rtwn(4) needs help from attachment drivers to determine (diff)
downloadwireguard-openbsd-f5230a3a326c78e0803569fb78c8693d79f2bb70.tar.xz
wireguard-openbsd-f5230a3a326c78e0803569fb78c8693d79f2bb70.zip
More ksh POSIX compliance fixes by Martijn Dekker:
This simple patch makes the 'command' builtin POSIX-compliant and consistent with other current shells. It fixes two things: a) 'command -v' does not find shell reserved words (a.k.a. keywords). For instance, 'command -v select' outputs nothing but should output 'select'. b) 'command -pv' always outputs the path of an external command, even if 'command -p' would execute a builtin. For instance, 'command -p kill' executes the 'kill' builtin, as expected, but 'command -pv kill' outputs '/bin/kill'. The '-v' option is supposed to reflect what would actually be executed, so 'command -pv kill' should output 'kill'. The -p option sets the PATH to a default system value before doing the search, but that has no bearing on the fact that builtins take precedence over external commands. The patch fixes both issues for 'command' without affecting the behaviour of the ksh-specific builtin 'whence', which is handled by the same C function. Regression test added to obsd-regress.t. Issues found and fixed by Martijn Dekker, ok millert@
-rw-r--r--bin/ksh/c_ksh.c18
-rw-r--r--bin/ksh/ksh.110
-rw-r--r--bin/ksh/sh.17
-rw-r--r--regress/bin/ksh/obsd-regress.t83
4 files changed, 102 insertions, 16 deletions
diff --git a/bin/ksh/c_ksh.c b/bin/ksh/c_ksh.c
index 3d85d09ac38..f29a3e10ff8 100644
--- a/bin/ksh/c_ksh.c
+++ b/bin/ksh/c_ksh.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: c_ksh.c,v 1.49 2016/01/15 17:55:45 mmcc Exp $ */
+/* $OpenBSD: c_ksh.c,v 1.50 2016/03/21 13:35:00 tb Exp $ */
/*
* built-in Korn commands: c_*
@@ -433,23 +433,23 @@ c_whence(char **wp)
fcflags = FC_BI | FC_PATH | FC_FUNC;
if (!iam_whence) {
- /* Note that -p on its own is deal with in comexec() */
+ /* Note that -p on its own is dealt with in comexec() */
if (pflag)
fcflags |= FC_DEFPATH;
- /* Convert command options to whence options - note that
- * command -pV uses a different path search than whence -v
- * or whence -pv. This should be considered a feature.
+ /* Convert command options to whence options. Note that
+ * command -pV and command -pv use a different path search
+ * than whence -v or whence -pv. This should be considered
+ * a feature.
*/
vflag = Vflag;
- }
- if (pflag)
+ } else if (pflag)
fcflags &= ~(FC_BI | FC_FUNC);
while ((vflag || ret == 0) && (id = *wp++) != NULL) {
tp = NULL;
- if ((iam_whence || vflag) && !pflag)
+ if (!iam_whence || !pflag)
tp = ktsearch(&keywords, id, hash(id));
- if (!tp && !pflag) {
+ if (!tp && (!iam_whence || !pflag)) {
tp = ktsearch(&aliases, id, hash(id));
if (tp && !(tp->flag & ISSET))
tp = NULL;
diff --git a/bin/ksh/ksh.1 b/bin/ksh/ksh.1
index a710217a7e7..d460db9acd1 100644
--- a/bin/ksh/ksh.1
+++ b/bin/ksh/ksh.1
@@ -1,8 +1,8 @@
-.\" $OpenBSD: ksh.1,v 1.177 2016/03/06 14:20:04 jmc Exp $
+.\" $OpenBSD: ksh.1,v 1.178 2016/03/21 13:35:00 tb Exp $
.\"
.\" Public Domain
.\"
-.Dd $Mdocdate: March 6 2016 $
+.Dd $Mdocdate: March 21 2016 $
.Dt KSH 1
.Os
.Sh NAME
@@ -2969,7 +2969,7 @@ is executed exactly as if
had not been specified, with two exceptions:
firstly,
.Ar cmd
-cannot be a shell function;
+cannot be an alias or a shell function;
and secondly, special built-in commands lose their specialness
(i.e. redirection and utility errors do not cause the shell to
exit, and command assignments are not permanent).
@@ -2981,6 +2981,8 @@ option is given, a default search path is used instead of the current value of
(the actual value of the default path is system dependent: on
POSIX-ish systems, it is the value returned by
.Ic getconf PATH ) .
+Nevertheless, reserved words, aliases, shell functions, and
+builtin commands are still found before external commands.
.Pp
If the
.Fl v
@@ -4437,7 +4439,7 @@ is similar to
.Ic command Fl v
except that
.Ic whence
-will find reserved words and won't print aliases as alias commands.
+won't print aliases as alias commands.
With the
.Fl v
option,
diff --git a/bin/ksh/sh.1 b/bin/ksh/sh.1
index cb5a0c07d33..86a340dc86a 100644
--- a/bin/ksh/sh.1
+++ b/bin/ksh/sh.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: sh.1,v 1.130 2015/10/12 12:34:42 jmc Exp $
+.\" $OpenBSD: sh.1,v 1.131 2016/03/21 13:35:00 tb Exp $
.\"
.\" Copyright (c) 2015 Jason McIntyre <jmc@openbsd.org>
.\"
@@ -14,7 +14,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: October 12 2015 $
+.Dd $Mdocdate: March 21 2016 $
.Dt SH 1
.Os
.Sh NAME
@@ -329,6 +329,9 @@ but identify how the shell will interpret it
Do not invoke
.Ar command ,
but identify the pathname the shell will use to run it.
+For aliases, a command to define that alias is printed.
+For shell reserved words, shell functions, and built-in utilities,
+just the name is printed.
.El
.Pp
The exit status is that of
diff --git a/regress/bin/ksh/obsd-regress.t b/regress/bin/ksh/obsd-regress.t
index 26759c89cbb..8952ab0e91d 100644
--- a/regress/bin/ksh/obsd-regress.t
+++ b/regress/bin/ksh/obsd-regress.t
@@ -1,4 +1,4 @@
-# $OpenBSD: obsd-regress.t,v 1.2 2016/03/05 12:30:17 czarkoff Exp $
+# $OpenBSD: obsd-regress.t,v 1.3 2016/03/21 13:35:00 tb Exp $
#
# ksh regression tests from OpenBSD
@@ -274,6 +274,87 @@ stdin:
---
+name: command-pvV-posix-priorities
+description:
+ For POSIX compatibility, command -v should find aliases and reserved
+ words, and command -p[vV] should find aliases, reserved words, and
+ builtins over external commands.
+stdin:
+ PATH=$(command -p getconf PATH) || PATH=/bin:/usr/bin
+ alias foo="bar baz"
+ bar() { :; }
+ for word in 'if' 'foo' 'bar' 'set' 'true' 'ls'; do
+ command -v "$word"
+ command -pv "$word"
+ command -V "$word"
+ command -pV "$word"
+ done
+expected-stdout-pattern:
+ /^if
+ if
+ if is a reserved word
+ if is a reserved word
+ alias foo='bar baz'
+ alias foo='bar baz'
+ foo is an alias for 'bar baz'
+ foo is an alias for 'bar baz'
+ bar
+ bar
+ bar is a function
+ bar is a function
+ set
+ set
+ set is a special shell builtin
+ set is a special shell builtin
+ true
+ true
+ true is a shell builtin
+ true is a shell builtin
+ .*\/ls.*
+ .*\/ls.*
+ ls is a tracked alias for .*\/ls.*
+ ls is .*\/ls.*$/
+---
+
+name: whence-preserve-tradition
+description:
+ POSIX 'command' and ksh88/pdksh-specific 'whence' are handled by the
+ same c_whence() function. This regression test is to ensure that
+ the POSIX compatibility changes for 'command' (see previous test) do
+ not affect traditional 'whence' behaviour.
+stdin:
+ PATH=$(command -p getconf PATH) || PATH=/bin:/usr/bin
+ alias foo="bar baz"
+ bar() { :; }
+ for word in 'if' 'foo' 'bar' 'set' 'true' 'ls'; do
+ whence "$word"
+ whence -p "$word"
+ whence -v "$word"
+ whence -pv "$word"
+ done
+expected-stdout-pattern:
+ /^if
+ if is a reserved word
+ if not found
+ 'bar baz'
+ foo is an alias for 'bar baz'
+ foo not found
+ bar
+ bar is a function
+ bar not found
+ set
+ set is a special shell builtin
+ set not found
+ true
+ .*\/true.*
+ true is a shell builtin
+ true is a tracked alias for .*\/true.*
+ .*\/ls.*
+ .*\/ls.*
+ ls is a tracked alias for .*\/ls.*
+ ls is a tracked alias for .*\/ls.*$/
+---
+
name: shellopt-u-1
description:
Check that "$@" and "$*" are exempt from 'set -u' (nounset)