aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-10-01 03:02:25 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2016-10-01 03:02:25 +0200
commitcac4d002e78339272f75952959b4ce78b9239e7f (patch)
tree27a8c3db3659ca3f5e43cb43169eeaf390af0f62
parente627ac95bef4d61c2d264fc87d767cb7a948c38f (diff)
downloadbusybox-w32-cac4d002e78339272f75952959b4ce78b9239e7f.tar.gz
busybox-w32-cac4d002e78339272f75952959b4ce78b9239e7f.tar.bz2
busybox-w32-cac4d002e78339272f75952959b4ce78b9239e7f.zip
ash: explain how "command" is handled, and shrink it a bit
function old new delta getoptscmd 641 632 -9 commandcmd 79 69 -10 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-19) Total: -19 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--shell/ash.c88
1 files changed, 48 insertions, 40 deletions
diff --git a/shell/ash.c b/shell/ash.c
index a7c03bbc2..4adf92ac2 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -8019,9 +8019,45 @@ typecmd(int argc UNUSED_PARAM, char **argv)
8019} 8019}
8020 8020
8021#if ENABLE_ASH_CMDCMD 8021#if ENABLE_ASH_CMDCMD
8022/* Is it "command [-p] PROG ARGS" bltin, no other opts? Return ptr to "PROG" if yes */
8023static char **
8024parse_command_args(char **argv, const char **path)
8025{
8026 char *cp, c;
8027
8028 for (;;) {
8029 cp = *++argv;
8030 if (!cp)
8031 return NULL;
8032 if (*cp++ != '-')
8033 break;
8034 c = *cp++;
8035 if (!c)
8036 break;
8037 if (c == '-' && !*cp) {
8038 if (!*++argv)
8039 return NULL;
8040 break;
8041 }
8042 do {
8043 switch (c) {
8044 case 'p':
8045 *path = bb_default_path;
8046 break;
8047 default:
8048 /* run 'typecmd' for other options */
8049 return NULL;
8050 }
8051 c = *cp++;
8052 } while (c);
8053 }
8054 return argv;
8055}
8056
8022static int FAST_FUNC 8057static int FAST_FUNC
8023commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) 8058commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8024{ 8059{
8060 char *cmd;
8025 int c; 8061 int c;
8026 enum { 8062 enum {
8027 VERIFY_BRIEF = 1, 8063 VERIFY_BRIEF = 1,
@@ -8029,21 +8065,26 @@ commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8029 } verify = 0; 8065 } verify = 0;
8030 const char *path = NULL; 8066 const char *path = NULL;
8031 8067
8068 /* "command [-p] PROG ARGS" (that is, without -V or -v)
8069 * never reaches this function.
8070 */
8071
8032 while ((c = nextopt("pvV")) != '\0') 8072 while ((c = nextopt("pvV")) != '\0')
8033 if (c == 'V') 8073 if (c == 'V')
8034 verify |= VERIFY_VERBOSE; 8074 verify |= VERIFY_VERBOSE;
8035 else if (c == 'v') 8075 else if (c == 'v')
8036 verify |= VERIFY_BRIEF; 8076 /*verify |= VERIFY_BRIEF*/;
8037#if DEBUG 8077#if DEBUG
8038 else if (c != 'p') 8078 else if (c != 'p')
8039 abort(); 8079 abort();
8040#endif 8080#endif
8041 else 8081 else
8042 path = bb_default_path; 8082 path = bb_default_path;
8083
8043 /* Mimic bash: just "command -v" doesn't complain, it's a nop */ 8084 /* Mimic bash: just "command -v" doesn't complain, it's a nop */
8044 if (verify && (*argptr != NULL)) { 8085 cmd = *argptr;
8045 return describe_command(*argptr, path, verify - VERIFY_BRIEF); 8086 if (/*verify && */ cmd)
8046 } 8087 return describe_command(cmd, path, verify /* - VERIFY_BRIEF*/);
8047 8088
8048 return 0; 8089 return 0;
8049} 8090}
@@ -8976,42 +9017,6 @@ evalfun(struct funcnode *func, int argc, char **argv, int flags)
8976 return e; 9017 return e;
8977} 9018}
8978 9019
8979#if ENABLE_ASH_CMDCMD
8980static char **
8981parse_command_args(char **argv, const char **path)
8982{
8983 char *cp, c;
8984
8985 for (;;) {
8986 cp = *++argv;
8987 if (!cp)
8988 return NULL;
8989 if (*cp++ != '-')
8990 break;
8991 c = *cp++;
8992 if (!c)
8993 break;
8994 if (c == '-' && !*cp) {
8995 if (!*++argv)
8996 return NULL;
8997 break;
8998 }
8999 do {
9000 switch (c) {
9001 case 'p':
9002 *path = bb_default_path;
9003 break;
9004 default:
9005 /* run 'typecmd' for other options */
9006 return NULL;
9007 }
9008 c = *cp++;
9009 } while (c);
9010 }
9011 return argv;
9012}
9013#endif
9014
9015/* 9020/*
9016 * Make a variable a local variable. When a variable is made local, it's 9021 * Make a variable a local variable. When a variable is made local, it's
9017 * value and flags are saved in a localvar structure. The saved values 9022 * value and flags are saved in a localvar structure. The saved values
@@ -9463,6 +9468,9 @@ evalcommand(union node *cmd, int flags)
9463 nargv = parse_command_args(argv, &path); 9468 nargv = parse_command_args(argv, &path);
9464 if (!nargv) 9469 if (!nargv)
9465 break; 9470 break;
9471 /* It's "command [-p] PROG ARGS" (that is, no -Vv).
9472 * nargv => "PROG". path is updated if -p.
9473 */
9466 argc -= nargv - argv; 9474 argc -= nargv - argv;
9467 argv = nargv; 9475 argv = nargv;
9468 cmd_flag |= DO_NOFUNC; 9476 cmd_flag |= DO_NOFUNC;