Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/bin/sh Fix several bugs in the command / type builtin ( incl...
details: https://anonhg.NetBSD.org/src/rev/4887ba4b8838
branches: trunk
changeset: 324874:4887ba4b8838
user: kre <kre%NetBSD.org@localhost>
date: Wed Jul 25 14:42:50 2018 +0000
description:
Fix several bugs in the command / type builtin ( including PR bin/48499 )
1. Make command -pv (and -pV) work (which is not as easy as the PR
suggests it might be (the "check and cause error" was there because
it did not work, not in order to prevent it from working).
2. Stop -v and -V being both used (that makes no sense).
3. Stop the "type" builtin inheriting the args (-pvV) that "command" has
(which it did, as when -v -or -V is used with command, it and type are
implemented using the same code).
4. make "command -v word" DTRT for sh keywords (was treating them as an error).
5. Require at least one arg for "command -[vV]" or "type" else usage & error.
Strictly this should also apply to "command" and "command -p" (no -v)
but that's handled elsewhere, so perhaps some other time. Perhaps
"command -v" (and -V) should be limited to 1 command name (where "type"
can have many) as in the POSIX definitions, but I don't think that matters.
6. With "command -V alias", (or "type alias" which is the same thing),
(but not "command -v alias") alter the output format, so we get
ll is an alias for: ls -al
instead of the old
ll is an alias for
ls -al
(and note there was a space, for some reason, after "for")
That is, unless the alias value contains any \n characters, in which
case (something approximating) the old multi-line format is retained.
Also note: that if code wants to parse/use the value of an alias, it
should be using the output of "alias name", not command or type.
Note that none of the above affects "command [-p] cmd" (no -v or -V options)
only "command -[vV]" and "type".
Note also that the changes to eval.[ch] are merely to make syspath()
visible in exec.c rather than static in eval.c
diffstat:
bin/sh/eval.c | 6 +++---
bin/sh/eval.h | 4 +++-
bin/sh/exec.c | 42 ++++++++++++++++++++++++++++++++----------
3 files changed, 38 insertions(+), 14 deletions(-)
diffs (138 lines):
diff -r c0d12b6cc06c -r 4887ba4b8838 bin/sh/eval.c
--- a/bin/sh/eval.c Wed Jul 25 14:41:52 2018 +0000
+++ b/bin/sh/eval.c Wed Jul 25 14:42:50 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: eval.c,v 1.155 2018/06/22 11:04:55 kre Exp $ */
+/* $NetBSD: eval.c,v 1.156 2018/07/25 14:42:50 kre Exp $ */
/*-
* Copyright (c) 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
#else
-__RCSID("$NetBSD: eval.c,v 1.155 2018/06/22 11:04:55 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.156 2018/07/25 14:42:50 kre Exp $");
#endif
#endif /* not lint */
@@ -703,7 +703,7 @@
result->fd, result->buf, result->nleft, result->jp));
}
-static const char *
+const char *
syspath(void)
{
static char *sys_path = NULL;
diff -r c0d12b6cc06c -r 4887ba4b8838 bin/sh/eval.h
--- a/bin/sh/eval.h Wed Jul 25 14:41:52 2018 +0000
+++ b/bin/sh/eval.h Wed Jul 25 14:42:50 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: eval.h,v 1.19 2016/05/09 21:03:10 kre Exp $ */
+/* $NetBSD: eval.h,v 1.20 2018/07/25 14:42:50 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -52,6 +52,8 @@
void evaltree(union node *, int);
void evalbackcmd(union node *, struct backcmd *);
+const char *syspath(void);
+
/* in_function returns nonzero if we are currently evaluating a function */
int in_function(void); /* return non-zero, if evaluating a function */
diff -r c0d12b6cc06c -r 4887ba4b8838 bin/sh/exec.c
--- a/bin/sh/exec.c Wed Jul 25 14:41:52 2018 +0000
+++ b/bin/sh/exec.c Wed Jul 25 14:42:50 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: exec.c,v 1.52 2018/06/22 11:04:55 kre Exp $ */
+/* $NetBSD: exec.c,v 1.53 2018/07/25 14:42:50 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95";
#else
-__RCSID("$NetBSD: exec.c,v 1.52 2018/06/22 11:04:55 kre Exp $");
+__RCSID("$NetBSD: exec.c,v 1.53 2018/07/25 14:42:50 kre Exp $");
#endif
#endif /* not lint */
@@ -1064,8 +1064,15 @@
}
}
- if (p_flag && (v_flag || V_flag))
- error("cannot specify -p with -v or -V");
+ if (argv[0][0] != 'c' && v_flag | V_flag | p_flag)
+ error("usage: %s name...", argv[0]);
+
+ if (v_flag && V_flag)
+ error("-v and -V cannot both be specified");
+
+ if (*argptr == NULL)
+ error("usage: %s%s name ...", argv[0],
+ argv[0][0] == 'c' ? " [-p] [-v|-V]" : "");
while ((arg = *argptr++)) {
if (!v_flag)
@@ -1077,7 +1084,7 @@
if (*pp) {
if (v_flag)
- err = 1;
+ out1fmt("%s\n", arg);
else
out1str(" is a shell keyword\n");
continue;
@@ -1085,27 +1092,42 @@
/* Then look at the aliases */
if ((ap = lookupalias(arg, 1)) != NULL) {
- if (!v_flag)
- out1fmt(" is an alias for \n");
+ int ml = 0;
+
+ if (!v_flag) {
+ out1str(" is an alias ");
+ if (strchr(ap->val, '\n')) {
+ out1str("(multiline)...\n");
+ ml = 1;
+ } else
+ out1str("for: ");
+ }
out1fmt("%s\n", ap->val);
+ if (ml && *argptr != NULL)
+ out1c('\n');
continue;
}
/* Then check if it is a tracked alias */
- if ((cmdp = cmdlookup(arg, 0)) != NULL) {
+ if (!p_flag && (cmdp = cmdlookup(arg, 0)) != NULL) {
entry.cmdtype = cmdp->cmdtype;
entry.u = cmdp->param;
} else {
+ cmdp = NULL;
/* Finally use brute force */
- find_command(arg, &entry, DO_ABS, pathval());
+ find_command(arg, &entry, DO_ABS,
+ p_flag ? syspath() + 5 : pathval());
}
switch (entry.cmdtype) {
case CMDNORMAL: {
if (strchr(arg, '/') == NULL) {
- const char *path = pathval();
+ const char *path;
char *name;
int j = entry.u.index;
+
+ path = p_flag ? syspath() + 5 : pathval();
+
do {
name = padvance(&path, arg, 1);
stunalloc(name);
Home |
Main Index |
Thread Index |
Old Index