Home | History | Annotate | Line # | Download | only in sh
exec.c revision 1.35
      1  1.35       dsl /*	$NetBSD: exec.c,v 1.35 2003/01/22 20:36:04 dsl Exp $	*/
      2  1.15       cgd 
      3   1.1       cgd /*-
      4   1.8       jtc  * Copyright (c) 1991, 1993
      5   1.8       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39  1.23  christos #include <sys/cdefs.h>
     40   1.1       cgd #ifndef lint
     41  1.15       cgd #if 0
     42  1.17  christos static char sccsid[] = "@(#)exec.c	8.4 (Berkeley) 6/8/95";
     43  1.15       cgd #else
     44  1.35       dsl __RCSID("$NetBSD: exec.c,v 1.35 2003/01/22 20:36:04 dsl Exp $");
     45  1.15       cgd #endif
     46   1.1       cgd #endif /* not lint */
     47   1.1       cgd 
     48  1.16  christos #include <sys/types.h>
     49  1.16  christos #include <sys/stat.h>
     50  1.33  christos #include <sys/wait.h>
     51  1.16  christos #include <unistd.h>
     52  1.16  christos #include <fcntl.h>
     53  1.16  christos #include <errno.h>
     54  1.33  christos #include <stdio.h>
     55  1.16  christos #include <stdlib.h>
     56  1.16  christos 
     57   1.1       cgd /*
     58   1.1       cgd  * When commands are first encountered, they are entered in a hash table.
     59   1.1       cgd  * This ensures that a full path search will not have to be done for them
     60   1.1       cgd  * on each invocation.
     61   1.1       cgd  *
     62   1.1       cgd  * We should investigate converting to a linear search, even though that
     63   1.1       cgd  * would make the command name "hash" a misnomer.
     64   1.1       cgd  */
     65   1.1       cgd 
     66   1.1       cgd #include "shell.h"
     67   1.1       cgd #include "main.h"
     68   1.1       cgd #include "nodes.h"
     69   1.1       cgd #include "parser.h"
     70   1.1       cgd #include "redir.h"
     71   1.1       cgd #include "eval.h"
     72   1.1       cgd #include "exec.h"
     73   1.1       cgd #include "builtins.h"
     74   1.1       cgd #include "var.h"
     75   1.1       cgd #include "options.h"
     76   1.1       cgd #include "input.h"
     77   1.1       cgd #include "output.h"
     78   1.1       cgd #include "syntax.h"
     79   1.1       cgd #include "memalloc.h"
     80   1.1       cgd #include "error.h"
     81   1.1       cgd #include "init.h"
     82   1.1       cgd #include "mystring.h"
     83  1.16  christos #include "show.h"
     84   1.8       jtc #include "jobs.h"
     85  1.22  christos #include "alias.h"
     86   1.1       cgd 
     87   1.1       cgd 
     88   1.1       cgd #define CMDTABLESIZE 31		/* should be prime */
     89   1.1       cgd #define ARB 1			/* actual size determined at run time */
     90   1.1       cgd 
     91   1.1       cgd 
     92   1.1       cgd 
     93   1.1       cgd struct tblentry {
     94   1.1       cgd 	struct tblentry *next;	/* next entry in hash chain */
     95   1.1       cgd 	union param param;	/* definition of builtin function */
     96   1.1       cgd 	short cmdtype;		/* index identifying command */
     97   1.1       cgd 	char rehash;		/* if set, cd done since entry created */
     98   1.1       cgd 	char cmdname[ARB];	/* name of command */
     99   1.1       cgd };
    100   1.1       cgd 
    101   1.1       cgd 
    102   1.1       cgd STATIC struct tblentry *cmdtable[CMDTABLESIZE];
    103   1.1       cgd STATIC int builtinloc = -1;		/* index in path of %builtin, or -1 */
    104  1.19  christos int exerrno = 0;			/* Last exec error */
    105   1.1       cgd 
    106   1.1       cgd 
    107  1.34  christos STATIC void tryexec(char *, char **, char **, int);
    108  1.34  christos STATIC void execinterp(char **, char **);
    109  1.34  christos STATIC void printentry(struct tblentry *, int);
    110  1.34  christos STATIC void clearcmdentry(int);
    111  1.34  christos STATIC struct tblentry *cmdlookup(const char *, int);
    112  1.34  christos STATIC void delete_cmd_entry(void);
    113   1.1       cgd 
    114   1.1       cgd 
    115  1.32  christos extern char *const parsekwd[];
    116   1.1       cgd 
    117   1.1       cgd /*
    118   1.1       cgd  * Exec a program.  Never returns.  If you change this routine, you may
    119   1.1       cgd  * have to change the find_command routine as well.
    120   1.1       cgd  */
    121   1.1       cgd 
    122   1.1       cgd void
    123  1.34  christos shellexec(char **argv, char **envp, const char *path, int idx, int vforked)
    124  1.12       cgd {
    125   1.1       cgd 	char *cmdname;
    126   1.1       cgd 	int e;
    127   1.1       cgd 
    128   1.1       cgd 	if (strchr(argv[0], '/') != NULL) {
    129  1.33  christos 		tryexec(argv[0], argv, envp, vforked);
    130   1.1       cgd 		e = errno;
    131   1.1       cgd 	} else {
    132   1.1       cgd 		e = ENOENT;
    133   1.1       cgd 		while ((cmdname = padvance(&path, argv[0])) != NULL) {
    134  1.27  christos 			if (--idx < 0 && pathopt == NULL) {
    135  1.33  christos 				tryexec(cmdname, argv, envp, vforked);
    136   1.1       cgd 				if (errno != ENOENT && errno != ENOTDIR)
    137   1.1       cgd 					e = errno;
    138   1.1       cgd 			}
    139   1.1       cgd 			stunalloc(cmdname);
    140   1.1       cgd 		}
    141   1.1       cgd 	}
    142  1.19  christos 
    143  1.19  christos 	/* Map to POSIX errors */
    144  1.19  christos 	switch (e) {
    145  1.19  christos 	case EACCES:
    146  1.19  christos 		exerrno = 126;
    147  1.19  christos 		break;
    148  1.19  christos 	case ENOENT:
    149  1.19  christos 		exerrno = 127;
    150  1.19  christos 		break;
    151  1.19  christos 	default:
    152  1.19  christos 		exerrno = 2;
    153  1.19  christos 		break;
    154  1.19  christos 	}
    155  1.34  christos 	TRACE(("shellexec failed for %s, errno %d, vforked %d, suppressint %d\n",
    156  1.34  christos 		argv[0], e, vforked, suppressint ));
    157  1.19  christos 	exerror(EXEXEC, "%s: %s", argv[0], errmsg(e, E_EXEC));
    158  1.26   mycroft 	/* NOTREACHED */
    159   1.1       cgd }
    160   1.1       cgd 
    161   1.1       cgd 
    162   1.1       cgd STATIC void
    163  1.34  christos tryexec(char *cmd, char **argv, char **envp, int vforked)
    164  1.34  christos {
    165   1.1       cgd 	int e;
    166  1.16  christos #ifndef BSD
    167   1.1       cgd 	char *p;
    168  1.16  christos #endif
    169   1.1       cgd 
    170   1.1       cgd #ifdef SYSV
    171   1.1       cgd 	do {
    172   1.1       cgd 		execve(cmd, argv, envp);
    173   1.1       cgd 	} while (errno == EINTR);
    174   1.1       cgd #else
    175   1.1       cgd 	execve(cmd, argv, envp);
    176   1.1       cgd #endif
    177   1.1       cgd 	e = errno;
    178   1.1       cgd 	if (e == ENOEXEC) {
    179  1.33  christos 		if (vforked) {
    180  1.33  christos 			/* We are currently vfork(2)ed, so raise an
    181  1.33  christos 			 * exception, and evalcommand will try again
    182  1.33  christos 			 * with a normal fork(2).
    183  1.33  christos 			 */
    184  1.33  christos 			exraise(EXSHELLPROC);
    185  1.33  christos 		}
    186   1.1       cgd 		initshellproc();
    187   1.1       cgd 		setinputfile(cmd, 0);
    188   1.1       cgd 		commandname = arg0 = savestr(argv[0]);
    189   1.1       cgd #ifndef BSD
    190   1.1       cgd 		pgetc(); pungetc();		/* fill up input buffer */
    191   1.1       cgd 		p = parsenextc;
    192   1.1       cgd 		if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
    193   1.1       cgd 			argv[0] = cmd;
    194   1.1       cgd 			execinterp(argv, envp);
    195   1.1       cgd 		}
    196   1.1       cgd #endif
    197   1.1       cgd 		setparam(argv + 1);
    198   1.1       cgd 		exraise(EXSHELLPROC);
    199   1.1       cgd 	}
    200   1.1       cgd 	errno = e;
    201   1.1       cgd }
    202   1.1       cgd 
    203   1.1       cgd 
    204   1.1       cgd #ifndef BSD
    205   1.1       cgd /*
    206   1.1       cgd  * Execute an interpreter introduced by "#!", for systems where this
    207   1.1       cgd  * feature has not been built into the kernel.  If the interpreter is
    208   1.1       cgd  * the shell, return (effectively ignoring the "#!").  If the execution
    209   1.1       cgd  * of the interpreter fails, exit.
    210   1.1       cgd  *
    211   1.1       cgd  * This code peeks inside the input buffer in order to avoid actually
    212   1.1       cgd  * reading any input.  It would benefit from a rewrite.
    213   1.1       cgd  */
    214   1.1       cgd 
    215   1.1       cgd #define NEWARGS 5
    216   1.1       cgd 
    217   1.1       cgd STATIC void
    218  1.34  christos execinterp(char **argv, char **envp)
    219  1.34  christos {
    220   1.1       cgd 	int n;
    221   1.1       cgd 	char *inp;
    222   1.1       cgd 	char *outp;
    223   1.1       cgd 	char c;
    224   1.1       cgd 	char *p;
    225   1.1       cgd 	char **ap;
    226   1.1       cgd 	char *newargs[NEWARGS];
    227   1.1       cgd 	int i;
    228   1.1       cgd 	char **ap2;
    229   1.1       cgd 	char **new;
    230   1.1       cgd 
    231   1.1       cgd 	n = parsenleft - 2;
    232   1.1       cgd 	inp = parsenextc + 2;
    233   1.1       cgd 	ap = newargs;
    234   1.1       cgd 	for (;;) {
    235   1.1       cgd 		while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
    236   1.1       cgd 			inp++;
    237   1.1       cgd 		if (n < 0)
    238   1.1       cgd 			goto bad;
    239   1.1       cgd 		if ((c = *inp++) == '\n')
    240   1.1       cgd 			break;
    241   1.1       cgd 		if (ap == &newargs[NEWARGS])
    242   1.1       cgd bad:		  error("Bad #! line");
    243   1.1       cgd 		STARTSTACKSTR(outp);
    244   1.1       cgd 		do {
    245   1.1       cgd 			STPUTC(c, outp);
    246   1.1       cgd 		} while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n');
    247   1.1       cgd 		STPUTC('\0', outp);
    248   1.1       cgd 		n++, inp--;
    249   1.1       cgd 		*ap++ = grabstackstr(outp);
    250   1.1       cgd 	}
    251   1.1       cgd 	if (ap == newargs + 1) {	/* if no args, maybe no exec is needed */
    252   1.1       cgd 		p = newargs[0];
    253   1.1       cgd 		for (;;) {
    254   1.1       cgd 			if (equal(p, "sh") || equal(p, "ash")) {
    255   1.1       cgd 				return;
    256   1.1       cgd 			}
    257   1.1       cgd 			while (*p != '/') {
    258   1.1       cgd 				if (*p == '\0')
    259   1.1       cgd 					goto break2;
    260   1.1       cgd 				p++;
    261   1.1       cgd 			}
    262   1.1       cgd 			p++;
    263   1.1       cgd 		}
    264   1.1       cgd break2:;
    265   1.1       cgd 	}
    266   1.1       cgd 	i = (char *)ap - (char *)newargs;		/* size in bytes */
    267   1.1       cgd 	if (i == 0)
    268   1.1       cgd 		error("Bad #! line");
    269   1.1       cgd 	for (ap2 = argv ; *ap2++ != NULL ; );
    270   1.1       cgd 	new = ckmalloc(i + ((char *)ap2 - (char *)argv));
    271   1.1       cgd 	ap = newargs, ap2 = new;
    272   1.1       cgd 	while ((i -= sizeof (char **)) >= 0)
    273   1.1       cgd 		*ap2++ = *ap++;
    274   1.1       cgd 	ap = argv;
    275   1.1       cgd 	while (*ap2++ = *ap++);
    276   1.1       cgd 	shellexec(new, envp, pathval(), 0);
    277  1.26   mycroft 	/* NOTREACHED */
    278   1.1       cgd }
    279   1.1       cgd #endif
    280   1.1       cgd 
    281   1.1       cgd 
    282   1.1       cgd 
    283   1.1       cgd /*
    284   1.1       cgd  * Do a path search.  The variable path (passed by reference) should be
    285   1.1       cgd  * set to the start of the path before the first call; padvance will update
    286   1.1       cgd  * this value as it proceeds.  Successive calls to padvance will return
    287   1.1       cgd  * the possible path expansions in sequence.  If an option (indicated by
    288   1.1       cgd  * a percent sign) appears in the path entry then the global variable
    289   1.1       cgd  * pathopt will be set to point to it; otherwise pathopt will be set to
    290   1.1       cgd  * NULL.
    291   1.1       cgd  */
    292   1.1       cgd 
    293  1.27  christos const char *pathopt;
    294   1.1       cgd 
    295   1.1       cgd char *
    296  1.34  christos padvance(const char **path, const char *name)
    297  1.34  christos {
    298  1.27  christos 	const char *p;
    299  1.27  christos 	char *q;
    300  1.27  christos 	const char *start;
    301   1.1       cgd 	int len;
    302   1.1       cgd 
    303   1.1       cgd 	if (*path == NULL)
    304   1.1       cgd 		return NULL;
    305   1.1       cgd 	start = *path;
    306   1.1       cgd 	for (p = start ; *p && *p != ':' && *p != '%' ; p++);
    307   1.1       cgd 	len = p - start + strlen(name) + 2;	/* "2" is for '/' and '\0' */
    308   1.1       cgd 	while (stackblocksize() < len)
    309   1.1       cgd 		growstackblock();
    310   1.1       cgd 	q = stackblock();
    311   1.1       cgd 	if (p != start) {
    312  1.11   mycroft 		memcpy(q, start, p - start);
    313   1.1       cgd 		q += p - start;
    314   1.1       cgd 		*q++ = '/';
    315   1.1       cgd 	}
    316   1.1       cgd 	strcpy(q, name);
    317   1.1       cgd 	pathopt = NULL;
    318   1.1       cgd 	if (*p == '%') {
    319   1.1       cgd 		pathopt = ++p;
    320   1.1       cgd 		while (*p && *p != ':')  p++;
    321   1.1       cgd 	}
    322   1.1       cgd 	if (*p == ':')
    323   1.1       cgd 		*path = p + 1;
    324   1.1       cgd 	else
    325   1.1       cgd 		*path = NULL;
    326   1.1       cgd 	return stalloc(len);
    327   1.1       cgd }
    328   1.1       cgd 
    329   1.1       cgd 
    330   1.1       cgd 
    331   1.1       cgd /*** Command hashing code ***/
    332   1.1       cgd 
    333   1.1       cgd 
    334  1.12       cgd int
    335  1.34  christos hashcmd(int argc, char **argv)
    336  1.12       cgd {
    337   1.1       cgd 	struct tblentry **pp;
    338   1.1       cgd 	struct tblentry *cmdp;
    339   1.1       cgd 	int c;
    340   1.1       cgd 	int verbose;
    341   1.1       cgd 	struct cmdentry entry;
    342   1.1       cgd 	char *name;
    343   1.1       cgd 
    344   1.1       cgd 	verbose = 0;
    345   1.1       cgd 	while ((c = nextopt("rv")) != '\0') {
    346   1.1       cgd 		if (c == 'r') {
    347   1.1       cgd 			clearcmdentry(0);
    348   1.1       cgd 		} else if (c == 'v') {
    349   1.1       cgd 			verbose++;
    350   1.1       cgd 		}
    351   1.1       cgd 	}
    352   1.8       jtc 	if (*argptr == NULL) {
    353   1.8       jtc 		for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
    354   1.8       jtc 			for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
    355   1.8       jtc 				printentry(cmdp, verbose);
    356   1.8       jtc 			}
    357   1.8       jtc 		}
    358   1.8       jtc 		return 0;
    359   1.8       jtc 	}
    360   1.1       cgd 	while ((name = *argptr) != NULL) {
    361   1.1       cgd 		if ((cmdp = cmdlookup(name, 0)) != NULL
    362   1.1       cgd 		 && (cmdp->cmdtype == CMDNORMAL
    363  1.16  christos 		     || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))
    364   1.1       cgd 			delete_cmd_entry();
    365  1.24  christos 		find_command(name, &entry, DO_ERR, pathval());
    366   1.1       cgd 		if (verbose) {
    367   1.1       cgd 			if (entry.cmdtype != CMDUNKNOWN) {	/* if no error msg */
    368   1.1       cgd 				cmdp = cmdlookup(name, 0);
    369   1.8       jtc 				printentry(cmdp, verbose);
    370   1.1       cgd 			}
    371   1.1       cgd 			flushall();
    372   1.1       cgd 		}
    373   1.1       cgd 		argptr++;
    374   1.1       cgd 	}
    375   1.1       cgd 	return 0;
    376   1.1       cgd }
    377   1.1       cgd 
    378   1.1       cgd 
    379   1.1       cgd STATIC void
    380  1.34  christos printentry(struct tblentry *cmdp, int verbose)
    381  1.34  christos {
    382  1.27  christos 	int idx;
    383  1.27  christos 	const char *path;
    384   1.1       cgd 	char *name;
    385   1.1       cgd 
    386  1.34  christos 	switch (cmdp->cmdtype) {
    387  1.34  christos 	case CMDNORMAL:
    388  1.27  christos 		idx = cmdp->param.index;
    389   1.1       cgd 		path = pathval();
    390   1.1       cgd 		do {
    391   1.1       cgd 			name = padvance(&path, cmdp->cmdname);
    392   1.1       cgd 			stunalloc(name);
    393  1.27  christos 		} while (--idx >= 0);
    394   1.1       cgd 		out1str(name);
    395  1.34  christos 		break;
    396  1.34  christos 	case CMDSPLBLTIN:
    397  1.34  christos 		out1fmt("special builtin %s", cmdp->cmdname);
    398  1.34  christos 		break;
    399  1.34  christos 	case CMDBUILTIN:
    400   1.1       cgd 		out1fmt("builtin %s", cmdp->cmdname);
    401  1.34  christos 		break;
    402  1.34  christos 	case CMDFUNCTION:
    403   1.1       cgd 		out1fmt("function %s", cmdp->cmdname);
    404   1.8       jtc 		if (verbose) {
    405  1.34  christos 			struct procstat ps;
    406   1.8       jtc 			INTOFF;
    407  1.34  christos 			commandtext(&ps, cmdp->param.func);
    408   1.8       jtc 			out1c(' ');
    409  1.34  christos 			out1str(ps.cmd);
    410   1.8       jtc 			INTON;
    411   1.8       jtc 		}
    412  1.34  christos 		break;
    413  1.34  christos 	default:
    414  1.34  christos 		error("internal error: %s cmdtype %d", cmdp->cmdname, cmdp->cmdtype);
    415   1.1       cgd 	}
    416   1.1       cgd 	if (cmdp->rehash)
    417   1.1       cgd 		out1c('*');
    418   1.1       cgd 	out1c('\n');
    419   1.1       cgd }
    420   1.1       cgd 
    421   1.1       cgd 
    422   1.1       cgd 
    423   1.1       cgd /*
    424   1.1       cgd  * Resolve a command name.  If you change this routine, you may have to
    425   1.1       cgd  * change the shellexec routine as well.
    426   1.1       cgd  */
    427   1.1       cgd 
    428   1.1       cgd void
    429  1.34  christos find_command(char *name, struct cmdentry *entry, int act, const char *path)
    430  1.12       cgd {
    431  1.35       dsl 	struct tblentry *cmdp, loc_cmd;
    432  1.27  christos 	int idx;
    433   1.1       cgd 	int prev;
    434   1.1       cgd 	char *fullname;
    435   1.1       cgd 	struct stat statb;
    436   1.1       cgd 	int e;
    437  1.34  christos 	int (*bltin)(int,char **);
    438   1.1       cgd 
    439  1.35       dsl 	/* If name contains a slash, don't use PATH or hash table */
    440   1.1       cgd 	if (strchr(name, '/') != NULL) {
    441  1.24  christos 		if (act & DO_ABS) {
    442  1.24  christos 			while (stat(name, &statb) < 0) {
    443  1.34  christos #ifdef SYSV
    444  1.24  christos 				if (errno == EINTR)
    445  1.24  christos 					continue;
    446  1.34  christos #endif
    447  1.24  christos 				if (errno != ENOENT && errno != ENOTDIR)
    448  1.24  christos 					e = errno;
    449  1.24  christos 				entry->cmdtype = CMDUNKNOWN;
    450  1.24  christos 				entry->u.index = -1;
    451  1.24  christos 				return;
    452  1.24  christos 			}
    453  1.24  christos 			entry->cmdtype = CMDNORMAL;
    454  1.24  christos 			entry->u.index = -1;
    455  1.24  christos 			return;
    456  1.24  christos 		}
    457   1.1       cgd 		entry->cmdtype = CMDNORMAL;
    458   1.1       cgd 		entry->u.index = 0;
    459   1.1       cgd 		return;
    460   1.1       cgd 	}
    461   1.1       cgd 
    462  1.35       dsl 	if (path != pathval())
    463  1.35       dsl 		act |= DO_ALTPATH;
    464  1.35       dsl 
    465  1.35       dsl 	if (act & DO_ALTPATH && strstr(path, "%builtin") != NULL)
    466  1.35       dsl 		act |= DO_ALTBLTIN;
    467  1.35       dsl 
    468  1.35       dsl 	/* If name is in the table, check answer will be ok */
    469  1.35       dsl 	if ((cmdp = cmdlookup(name, 0)) != NULL) {
    470  1.35       dsl 		do {
    471  1.35       dsl 			switch (cmdp->cmdtype) {
    472  1.35       dsl 			case CMDNORMAL:
    473  1.35       dsl 				if (act & DO_ALTPATH) {
    474  1.35       dsl 					cmdp = NULL;
    475  1.35       dsl 					continue;
    476  1.35       dsl 				}
    477  1.35       dsl 				break;
    478  1.35       dsl 			case CMDFUNCTION:
    479  1.35       dsl 				if (act & DO_NOFUNC) {
    480  1.35       dsl 					cmdp = NULL;
    481  1.35       dsl 					continue;
    482  1.35       dsl 				}
    483  1.35       dsl 				break;
    484  1.35       dsl 			case CMDBUILTIN:
    485  1.35       dsl 				if ((act & DO_ALTBLTIN) || builtinloc >= 0) {
    486  1.35       dsl 					cmdp = NULL;
    487  1.35       dsl 					continue;
    488  1.35       dsl 				}
    489  1.35       dsl 				break;
    490  1.35       dsl 			}
    491  1.35       dsl 			/* if not invalidated by cd, we're done */
    492  1.35       dsl 			if (cmdp->rehash == 0)
    493  1.35       dsl 				goto success;
    494  1.35       dsl 		} while (0);
    495  1.35       dsl 	}
    496   1.1       cgd 
    497   1.1       cgd 	/* If %builtin not in path, check for builtin next */
    498  1.35       dsl 	if ((act & DO_ALTPATH ? !(act & DO_ALTBLTIN) : builtinloc < 0) &&
    499  1.35       dsl 	    (bltin = find_builtin(name)) != 0)
    500  1.35       dsl 		goto builtin_success;
    501   1.1       cgd 
    502   1.1       cgd 	/* We have to search path. */
    503   1.1       cgd 	prev = -1;		/* where to start */
    504   1.1       cgd 	if (cmdp) {		/* doing a rehash */
    505   1.1       cgd 		if (cmdp->cmdtype == CMDBUILTIN)
    506   1.1       cgd 			prev = builtinloc;
    507   1.1       cgd 		else
    508   1.1       cgd 			prev = cmdp->param.index;
    509   1.1       cgd 	}
    510   1.1       cgd 
    511   1.1       cgd 	e = ENOENT;
    512  1.27  christos 	idx = -1;
    513   1.1       cgd loop:
    514   1.1       cgd 	while ((fullname = padvance(&path, name)) != NULL) {
    515   1.1       cgd 		stunalloc(fullname);
    516  1.27  christos 		idx++;
    517   1.1       cgd 		if (pathopt) {
    518   1.1       cgd 			if (prefix("builtin", pathopt)) {
    519  1.34  christos 				if ((bltin = find_builtin(name)) == 0)
    520   1.1       cgd 					goto loop;
    521  1.35       dsl 				goto builtin_success;
    522   1.1       cgd 			} else if (prefix("func", pathopt)) {
    523   1.1       cgd 				/* handled below */
    524   1.1       cgd 			} else {
    525  1.35       dsl 				/* ignore unimplemented options */
    526  1.35       dsl 				goto loop;
    527   1.1       cgd 			}
    528   1.1       cgd 		}
    529   1.1       cgd 		/* if rehash, don't redo absolute path names */
    530  1.27  christos 		if (fullname[0] == '/' && idx <= prev) {
    531  1.27  christos 			if (idx < prev)
    532   1.1       cgd 				goto loop;
    533   1.1       cgd 			TRACE(("searchexec \"%s\": no change\n", name));
    534   1.1       cgd 			goto success;
    535   1.1       cgd 		}
    536   1.1       cgd 		while (stat(fullname, &statb) < 0) {
    537   1.1       cgd #ifdef SYSV
    538   1.1       cgd 			if (errno == EINTR)
    539   1.1       cgd 				continue;
    540   1.1       cgd #endif
    541   1.1       cgd 			if (errno != ENOENT && errno != ENOTDIR)
    542   1.1       cgd 				e = errno;
    543   1.1       cgd 			goto loop;
    544   1.1       cgd 		}
    545   1.1       cgd 		e = EACCES;	/* if we fail, this will be the error */
    546  1.14   mycroft 		if (!S_ISREG(statb.st_mode))
    547   1.1       cgd 			goto loop;
    548   1.1       cgd 		if (pathopt) {		/* this is a %func directory */
    549  1.35       dsl 			if (act & DO_NOFUNC)
    550  1.35       dsl 				goto loop;
    551   1.1       cgd 			stalloc(strlen(fullname) + 1);
    552   1.1       cgd 			readcmdfile(fullname);
    553  1.35       dsl 			if ((cmdp = cmdlookup(name, 0)) == NULL ||
    554  1.35       dsl 			    cmdp->cmdtype != CMDFUNCTION)
    555   1.1       cgd 				error("%s not defined in %s", name, fullname);
    556   1.1       cgd 			stunalloc(fullname);
    557   1.1       cgd 			goto success;
    558   1.1       cgd 		}
    559   1.8       jtc #ifdef notdef
    560  1.35       dsl 		/* XXX this code stops root executing stuff, and is buggy
    561  1.35       dsl 		   if you need a group from the group list. */
    562   1.8       jtc 		if (statb.st_uid == geteuid()) {
    563   1.1       cgd 			if ((statb.st_mode & 0100) == 0)
    564   1.1       cgd 				goto loop;
    565   1.1       cgd 		} else if (statb.st_gid == getegid()) {
    566   1.1       cgd 			if ((statb.st_mode & 010) == 0)
    567   1.1       cgd 				goto loop;
    568   1.1       cgd 		} else {
    569   1.8       jtc 			if ((statb.st_mode & 01) == 0)
    570   1.1       cgd 				goto loop;
    571   1.1       cgd 		}
    572   1.4       cgd #endif
    573   1.1       cgd 		TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
    574   1.1       cgd 		INTOFF;
    575  1.35       dsl 		if (act & DO_ALTPATH) {
    576  1.35       dsl 			stalloc(strlen(fullname) + 1);
    577  1.35       dsl 			cmdp = &loc_cmd;
    578  1.35       dsl 		} else
    579  1.35       dsl 			cmdp = cmdlookup(name, 1);
    580   1.1       cgd 		cmdp->cmdtype = CMDNORMAL;
    581  1.27  christos 		cmdp->param.index = idx;
    582   1.1       cgd 		INTON;
    583   1.1       cgd 		goto success;
    584   1.1       cgd 	}
    585   1.1       cgd 
    586   1.1       cgd 	/* We failed.  If there was an entry for this command, delete it */
    587   1.1       cgd 	if (cmdp)
    588   1.1       cgd 		delete_cmd_entry();
    589  1.24  christos 	if (act & DO_ERR)
    590  1.20    abrown 		outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC));
    591   1.1       cgd 	entry->cmdtype = CMDUNKNOWN;
    592   1.1       cgd 	return;
    593   1.1       cgd 
    594  1.35       dsl builtin_success:
    595  1.35       dsl 	INTOFF;
    596  1.35       dsl 	if (act & DO_ALTPATH)
    597  1.35       dsl 		cmdp = &loc_cmd;
    598  1.35       dsl 	else
    599  1.35       dsl 		cmdp = cmdlookup(name, 1);
    600  1.35       dsl 	cmdp->cmdtype = CMDBUILTIN;
    601  1.35       dsl 	cmdp->param.bltin = bltin;
    602  1.35       dsl 	INTON;
    603   1.1       cgd success:
    604   1.1       cgd 	cmdp->rehash = 0;
    605   1.1       cgd 	entry->cmdtype = cmdp->cmdtype;
    606   1.1       cgd 	entry->u = cmdp->param;
    607   1.1       cgd }
    608   1.1       cgd 
    609   1.1       cgd 
    610   1.1       cgd 
    611   1.1       cgd /*
    612   1.1       cgd  * Search the table of builtin commands.
    613   1.1       cgd  */
    614   1.1       cgd 
    615   1.1       cgd int
    616  1.34  christos (*find_builtin(name))(int, char **)
    617   1.1       cgd 	char *name;
    618  1.12       cgd {
    619  1.21       tls 	const struct builtincmd *bp;
    620   1.1       cgd 
    621   1.1       cgd 	for (bp = builtincmd ; bp->name ; bp++) {
    622   1.1       cgd 		if (*bp->name == *name && equal(bp->name, name))
    623  1.34  christos 			return bp->builtin;
    624  1.34  christos 	}
    625  1.34  christos 	return 0;
    626  1.34  christos }
    627  1.34  christos 
    628  1.34  christos int
    629  1.34  christos (*find_splbltin(name))(int, char **)
    630  1.34  christos 	char *name;
    631  1.34  christos {
    632  1.34  christos 	const struct builtincmd *bp;
    633  1.34  christos 
    634  1.34  christos 	for (bp = splbltincmd ; bp->name ; bp++) {
    635  1.34  christos 		if (*bp->name == *name && equal(bp->name, name))
    636  1.34  christos 			return bp->builtin;
    637  1.34  christos 	}
    638  1.34  christos 	return 0;
    639  1.34  christos }
    640  1.34  christos 
    641  1.34  christos /*
    642  1.34  christos  * At shell startup put special builtins into hash table.
    643  1.34  christos  * ensures they are executed first (see posix).
    644  1.34  christos  * We stop functions being added with the same name
    645  1.34  christos  * (as they are impossible to call)
    646  1.34  christos  */
    647  1.34  christos 
    648  1.34  christos void
    649  1.34  christos hash_special_builtins(void)
    650  1.34  christos {
    651  1.34  christos 	const struct builtincmd *bp;
    652  1.34  christos 	struct tblentry *cmdp;
    653  1.34  christos 
    654  1.34  christos 	for (bp = splbltincmd ; bp->name ; bp++) {
    655  1.34  christos 		cmdp = cmdlookup(bp->name, 1);
    656  1.34  christos 		cmdp->cmdtype = CMDSPLBLTIN;
    657  1.34  christos 		cmdp->param.bltin = bp->builtin;
    658   1.1       cgd 	}
    659   1.1       cgd }
    660   1.1       cgd 
    661   1.1       cgd 
    662   1.1       cgd 
    663   1.1       cgd /*
    664   1.1       cgd  * Called when a cd is done.  Marks all commands so the next time they
    665   1.1       cgd  * are executed they will be rehashed.
    666   1.1       cgd  */
    667   1.1       cgd 
    668   1.1       cgd void
    669  1.34  christos hashcd(void)
    670  1.34  christos {
    671   1.1       cgd 	struct tblentry **pp;
    672   1.1       cgd 	struct tblentry *cmdp;
    673   1.1       cgd 
    674   1.1       cgd 	for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
    675   1.1       cgd 		for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
    676   1.1       cgd 			if (cmdp->cmdtype == CMDNORMAL
    677  1.16  christos 			 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
    678   1.1       cgd 				cmdp->rehash = 1;
    679   1.1       cgd 		}
    680   1.1       cgd 	}
    681   1.1       cgd }
    682   1.1       cgd 
    683   1.1       cgd 
    684   1.1       cgd 
    685   1.1       cgd /*
    686  1.35       dsl  * Fix command hash table when PATH changed.
    687   1.1       cgd  * Called before PATH is changed.  The argument is the new value of PATH;
    688  1.35       dsl  * pathval() still returns the old value at this point.
    689  1.35       dsl  * Called with interrupts off.
    690   1.1       cgd  */
    691   1.1       cgd 
    692   1.1       cgd void
    693  1.34  christos changepath(const char *newval)
    694  1.13   mycroft {
    695  1.18  christos 	const char *old, *new;
    696  1.27  christos 	int idx;
    697   1.1       cgd 	int firstchange;
    698   1.1       cgd 	int bltin;
    699   1.1       cgd 
    700   1.1       cgd 	old = pathval();
    701   1.1       cgd 	new = newval;
    702   1.1       cgd 	firstchange = 9999;	/* assume no change */
    703  1.27  christos 	idx = 0;
    704   1.1       cgd 	bltin = -1;
    705   1.1       cgd 	for (;;) {
    706   1.1       cgd 		if (*old != *new) {
    707  1.27  christos 			firstchange = idx;
    708  1.16  christos 			if ((*old == '\0' && *new == ':')
    709  1.16  christos 			 || (*old == ':' && *new == '\0'))
    710   1.1       cgd 				firstchange++;
    711   1.1       cgd 			old = new;	/* ignore subsequent differences */
    712   1.1       cgd 		}
    713   1.1       cgd 		if (*new == '\0')
    714   1.1       cgd 			break;
    715   1.1       cgd 		if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
    716  1.27  christos 			bltin = idx;
    717   1.1       cgd 		if (*new == ':') {
    718  1.27  christos 			idx++;
    719   1.1       cgd 		}
    720   1.1       cgd 		new++, old++;
    721   1.1       cgd 	}
    722   1.1       cgd 	if (builtinloc < 0 && bltin >= 0)
    723   1.1       cgd 		builtinloc = bltin;		/* zap builtins */
    724   1.1       cgd 	if (builtinloc >= 0 && bltin < 0)
    725   1.1       cgd 		firstchange = 0;
    726   1.1       cgd 	clearcmdentry(firstchange);
    727   1.1       cgd 	builtinloc = bltin;
    728   1.1       cgd }
    729   1.1       cgd 
    730   1.1       cgd 
    731   1.1       cgd /*
    732   1.1       cgd  * Clear out command entries.  The argument specifies the first entry in
    733   1.1       cgd  * PATH which has changed.
    734   1.1       cgd  */
    735   1.1       cgd 
    736   1.1       cgd STATIC void
    737  1.34  christos clearcmdentry(int firstchange)
    738  1.12       cgd {
    739   1.1       cgd 	struct tblentry **tblp;
    740   1.1       cgd 	struct tblentry **pp;
    741   1.1       cgd 	struct tblentry *cmdp;
    742   1.1       cgd 
    743   1.1       cgd 	INTOFF;
    744   1.1       cgd 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
    745   1.1       cgd 		pp = tblp;
    746   1.1       cgd 		while ((cmdp = *pp) != NULL) {
    747  1.16  christos 			if ((cmdp->cmdtype == CMDNORMAL &&
    748  1.16  christos 			     cmdp->param.index >= firstchange)
    749  1.16  christos 			 || (cmdp->cmdtype == CMDBUILTIN &&
    750  1.16  christos 			     builtinloc >= firstchange)) {
    751   1.1       cgd 				*pp = cmdp->next;
    752   1.1       cgd 				ckfree(cmdp);
    753   1.1       cgd 			} else {
    754   1.1       cgd 				pp = &cmdp->next;
    755   1.1       cgd 			}
    756   1.1       cgd 		}
    757   1.1       cgd 	}
    758   1.1       cgd 	INTON;
    759   1.1       cgd }
    760   1.1       cgd 
    761   1.1       cgd 
    762   1.1       cgd /*
    763   1.1       cgd  * Delete all functions.
    764   1.1       cgd  */
    765   1.1       cgd 
    766   1.1       cgd #ifdef mkinit
    767  1.34  christos MKINIT void deletefuncs(void);
    768  1.34  christos MKINIT void hash_special_builtins(void);
    769  1.34  christos 
    770  1.34  christos INIT {
    771  1.34  christos 	hash_special_builtins();
    772  1.34  christos }
    773   1.1       cgd 
    774   1.1       cgd SHELLPROC {
    775   1.1       cgd 	deletefuncs();
    776   1.1       cgd }
    777   1.1       cgd #endif
    778   1.1       cgd 
    779   1.1       cgd void
    780  1.34  christos deletefuncs(void)
    781  1.34  christos {
    782   1.1       cgd 	struct tblentry **tblp;
    783   1.1       cgd 	struct tblentry **pp;
    784   1.1       cgd 	struct tblentry *cmdp;
    785   1.1       cgd 
    786   1.1       cgd 	INTOFF;
    787   1.1       cgd 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
    788   1.1       cgd 		pp = tblp;
    789   1.1       cgd 		while ((cmdp = *pp) != NULL) {
    790   1.1       cgd 			if (cmdp->cmdtype == CMDFUNCTION) {
    791   1.1       cgd 				*pp = cmdp->next;
    792   1.1       cgd 				freefunc(cmdp->param.func);
    793   1.1       cgd 				ckfree(cmdp);
    794   1.1       cgd 			} else {
    795   1.1       cgd 				pp = &cmdp->next;
    796   1.1       cgd 			}
    797   1.1       cgd 		}
    798   1.1       cgd 	}
    799   1.1       cgd 	INTON;
    800   1.1       cgd }
    801   1.1       cgd 
    802   1.1       cgd 
    803   1.1       cgd 
    804   1.1       cgd /*
    805   1.1       cgd  * Locate a command in the command hash table.  If "add" is nonzero,
    806   1.1       cgd  * add the command to the table if it is not already present.  The
    807   1.1       cgd  * variable "lastcmdentry" is set to point to the address of the link
    808   1.1       cgd  * pointing to the entry, so that delete_cmd_entry can delete the
    809   1.1       cgd  * entry.
    810   1.1       cgd  */
    811   1.1       cgd 
    812   1.1       cgd struct tblentry **lastcmdentry;
    813   1.1       cgd 
    814   1.1       cgd 
    815   1.1       cgd STATIC struct tblentry *
    816  1.34  christos cmdlookup(const char *name, int add)
    817  1.12       cgd {
    818   1.1       cgd 	int hashval;
    819  1.34  christos 	const char *p;
    820   1.1       cgd 	struct tblentry *cmdp;
    821   1.1       cgd 	struct tblentry **pp;
    822   1.1       cgd 
    823   1.1       cgd 	p = name;
    824   1.1       cgd 	hashval = *p << 4;
    825   1.1       cgd 	while (*p)
    826   1.1       cgd 		hashval += *p++;
    827   1.1       cgd 	hashval &= 0x7FFF;
    828   1.1       cgd 	pp = &cmdtable[hashval % CMDTABLESIZE];
    829   1.1       cgd 	for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
    830   1.1       cgd 		if (equal(cmdp->cmdname, name))
    831   1.1       cgd 			break;
    832   1.1       cgd 		pp = &cmdp->next;
    833   1.1       cgd 	}
    834   1.1       cgd 	if (add && cmdp == NULL) {
    835   1.1       cgd 		INTOFF;
    836   1.1       cgd 		cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
    837   1.1       cgd 					+ strlen(name) + 1);
    838   1.1       cgd 		cmdp->next = NULL;
    839   1.1       cgd 		cmdp->cmdtype = CMDUNKNOWN;
    840   1.1       cgd 		cmdp->rehash = 0;
    841   1.1       cgd 		strcpy(cmdp->cmdname, name);
    842   1.1       cgd 		INTON;
    843   1.1       cgd 	}
    844   1.1       cgd 	lastcmdentry = pp;
    845   1.1       cgd 	return cmdp;
    846   1.1       cgd }
    847   1.1       cgd 
    848   1.1       cgd /*
    849   1.1       cgd  * Delete the command entry returned on the last lookup.
    850   1.1       cgd  */
    851   1.1       cgd 
    852   1.1       cgd STATIC void
    853  1.34  christos delete_cmd_entry(void)
    854  1.34  christos {
    855   1.1       cgd 	struct tblentry *cmdp;
    856   1.1       cgd 
    857   1.1       cgd 	INTOFF;
    858   1.1       cgd 	cmdp = *lastcmdentry;
    859   1.1       cgd 	*lastcmdentry = cmdp->next;
    860   1.1       cgd 	ckfree(cmdp);
    861   1.1       cgd 	INTON;
    862   1.1       cgd }
    863   1.1       cgd 
    864   1.1       cgd 
    865   1.1       cgd 
    866   1.1       cgd #ifdef notdef
    867   1.1       cgd void
    868  1.34  christos getcmdentry(char *name, struct cmdentry *entry)
    869  1.34  christos {
    870   1.1       cgd 	struct tblentry *cmdp = cmdlookup(name, 0);
    871   1.1       cgd 
    872   1.1       cgd 	if (cmdp) {
    873   1.1       cgd 		entry->u = cmdp->param;
    874   1.1       cgd 		entry->cmdtype = cmdp->cmdtype;
    875   1.1       cgd 	} else {
    876   1.1       cgd 		entry->cmdtype = CMDUNKNOWN;
    877   1.1       cgd 		entry->u.index = 0;
    878   1.1       cgd 	}
    879   1.1       cgd }
    880   1.1       cgd #endif
    881   1.1       cgd 
    882   1.1       cgd 
    883   1.1       cgd /*
    884   1.1       cgd  * Add a new command entry, replacing any existing command entry for
    885  1.34  christos  * the same name - except special builtins.
    886   1.1       cgd  */
    887   1.1       cgd 
    888  1.35       dsl STATIC void
    889  1.34  christos addcmdentry(char *name, struct cmdentry *entry)
    890  1.34  christos {
    891   1.1       cgd 	struct tblentry *cmdp;
    892   1.1       cgd 
    893   1.1       cgd 	INTOFF;
    894   1.1       cgd 	cmdp = cmdlookup(name, 1);
    895  1.34  christos 	if (cmdp->cmdtype != CMDSPLBLTIN) {
    896  1.34  christos 		if (cmdp->cmdtype == CMDFUNCTION) {
    897  1.34  christos 			freefunc(cmdp->param.func);
    898  1.34  christos 		}
    899  1.34  christos 		cmdp->cmdtype = entry->cmdtype;
    900  1.34  christos 		cmdp->param = entry->u;
    901   1.1       cgd 	}
    902   1.1       cgd 	INTON;
    903   1.1       cgd }
    904   1.1       cgd 
    905   1.1       cgd 
    906   1.1       cgd /*
    907   1.1       cgd  * Define a shell function.
    908   1.1       cgd  */
    909   1.1       cgd 
    910   1.1       cgd void
    911  1.34  christos defun(char *name, union node *func)
    912  1.34  christos {
    913   1.1       cgd 	struct cmdentry entry;
    914   1.1       cgd 
    915   1.1       cgd 	INTOFF;
    916   1.1       cgd 	entry.cmdtype = CMDFUNCTION;
    917   1.1       cgd 	entry.u.func = copyfunc(func);
    918   1.1       cgd 	addcmdentry(name, &entry);
    919   1.1       cgd 	INTON;
    920   1.1       cgd }
    921   1.1       cgd 
    922   1.1       cgd 
    923   1.1       cgd /*
    924   1.1       cgd  * Delete a function if it exists.
    925   1.1       cgd  */
    926   1.1       cgd 
    927   1.8       jtc int
    928  1.34  christos unsetfunc(char *name)
    929  1.34  christos {
    930   1.1       cgd 	struct tblentry *cmdp;
    931   1.1       cgd 
    932  1.35       dsl 	if ((cmdp = cmdlookup(name, 0)) != NULL &&
    933  1.35       dsl 	    cmdp->cmdtype == CMDFUNCTION) {
    934   1.1       cgd 		freefunc(cmdp->param.func);
    935   1.1       cgd 		delete_cmd_entry();
    936   1.8       jtc 		return (0);
    937   1.1       cgd 	}
    938   1.8       jtc 	return (1);
    939  1.22  christos }
    940  1.22  christos 
    941  1.22  christos /*
    942  1.22  christos  * Locate and print what a word is...
    943  1.35       dsl  * also used for 'command -[v|V]'
    944  1.22  christos  */
    945  1.22  christos 
    946  1.22  christos int
    947  1.34  christos typecmd(int argc, char **argv)
    948  1.22  christos {
    949  1.22  christos 	struct cmdentry entry;
    950  1.22  christos 	struct tblentry *cmdp;
    951  1.30      matt 	char * const *pp;
    952  1.22  christos 	struct alias *ap;
    953  1.27  christos 	int err = 0;
    954  1.35       dsl 	char *arg;
    955  1.35       dsl 	int c;
    956  1.35       dsl 	int V_flag = 0;
    957  1.35       dsl 	int v_flag = 0;
    958  1.35       dsl 	int p_flag = 0;
    959  1.22  christos 
    960  1.35       dsl 	while ((c = nextopt("vVp")) != 0) {
    961  1.35       dsl 		switch (c) {
    962  1.35       dsl 		case 'v': v_flag = 1; break;
    963  1.35       dsl 		case 'V': V_flag = 1; break;
    964  1.35       dsl 		case 'p': p_flag = 1; break;
    965  1.35       dsl 		}
    966  1.35       dsl 	}
    967  1.35       dsl 
    968  1.35       dsl 	if (p_flag && (v_flag || V_flag))
    969  1.35       dsl 		error("cannot specify -p with -v or -V");
    970  1.35       dsl 
    971  1.35       dsl 	while ((arg = *argptr++)) {
    972  1.35       dsl 		if (!v_flag)
    973  1.35       dsl 			out1str(arg);
    974  1.22  christos 		/* First look at the keywords */
    975  1.30      matt 		for (pp = parsekwd; *pp; pp++)
    976  1.35       dsl 			if (**pp == *arg && equal(*pp, arg))
    977  1.22  christos 				break;
    978  1.22  christos 
    979  1.22  christos 		if (*pp) {
    980  1.35       dsl 			if (v_flag)
    981  1.35       dsl 				err = 1;
    982  1.35       dsl 			else
    983  1.35       dsl 				out1str(" is a shell keyword\n");
    984  1.22  christos 			continue;
    985  1.22  christos 		}
    986  1.22  christos 
    987  1.22  christos 		/* Then look at the aliases */
    988  1.35       dsl 		if ((ap = lookupalias(arg, 1)) != NULL) {
    989  1.35       dsl 			if (!v_flag)
    990  1.35       dsl 				out1fmt(" is an alias for \n");
    991  1.35       dsl 			out1fmt("%s\n", ap->val);
    992  1.22  christos 			continue;
    993  1.22  christos 		}
    994  1.22  christos 
    995  1.22  christos 		/* Then check if it is a tracked alias */
    996  1.35       dsl 		if ((cmdp = cmdlookup(arg, 0)) != NULL) {
    997  1.22  christos 			entry.cmdtype = cmdp->cmdtype;
    998  1.22  christos 			entry.u = cmdp->param;
    999  1.35       dsl 		} else {
   1000  1.22  christos 			/* Finally use brute force */
   1001  1.35       dsl 			find_command(arg, &entry, DO_ABS, pathval());
   1002  1.22  christos 		}
   1003  1.22  christos 
   1004  1.22  christos 		switch (entry.cmdtype) {
   1005  1.22  christos 		case CMDNORMAL: {
   1006  1.35       dsl 			if (strchr(arg, '/') == NULL) {
   1007  1.31  christos 				const char *path = pathval();
   1008  1.31  christos 				char *name;
   1009  1.31  christos 				int j = entry.u.index;
   1010  1.31  christos 				do {
   1011  1.35       dsl 					name = padvance(&path, arg);
   1012  1.24  christos 					stunalloc(name);
   1013  1.24  christos 				} while (--j >= 0);
   1014  1.35       dsl 				if (!v_flag)
   1015  1.35       dsl 					out1fmt(" is%s ",
   1016  1.35       dsl 					    cmdp ? " a tracked alias for" : "");
   1017  1.35       dsl 				out1fmt("%s\n", name);
   1018  1.31  christos 			} else {
   1019  1.35       dsl 				if (access(arg, X_OK) == 0) {
   1020  1.35       dsl 					if (!v_flag)
   1021  1.35       dsl 						out1fmt(" is ");
   1022  1.35       dsl 					out1fmt("%s\n", arg);
   1023  1.35       dsl 				} else {
   1024  1.35       dsl 					if (!v_flag)
   1025  1.35       dsl 						out1fmt(": %s\n",
   1026  1.35       dsl 						    strerror(errno));
   1027  1.35       dsl 					else
   1028  1.35       dsl 						err = 126;
   1029  1.35       dsl 				}
   1030  1.24  christos 			}
   1031  1.31  christos  			break;
   1032  1.22  christos 		}
   1033  1.22  christos 		case CMDFUNCTION:
   1034  1.35       dsl 			if (!v_flag)
   1035  1.35       dsl 				out1str(" is a shell function\n");
   1036  1.35       dsl 			else
   1037  1.35       dsl 				out1fmt("%s\n", arg);
   1038  1.22  christos 			break;
   1039  1.22  christos 
   1040  1.22  christos 		case CMDBUILTIN:
   1041  1.35       dsl 			if (!v_flag)
   1042  1.35       dsl 				out1str(" is a shell builtin\n");
   1043  1.35       dsl 			else
   1044  1.35       dsl 				out1fmt("%s\n", arg);
   1045  1.34  christos 			break;
   1046  1.34  christos 
   1047  1.34  christos 		case CMDSPLBLTIN:
   1048  1.35       dsl 			if (!v_flag)
   1049  1.35       dsl 				out1str(" is a special shell builtin\n");
   1050  1.35       dsl 			else
   1051  1.35       dsl 				out1fmt("%s\n", arg);
   1052  1.22  christos 			break;
   1053  1.22  christos 
   1054  1.22  christos 		default:
   1055  1.35       dsl 			if (!v_flag)
   1056  1.35       dsl 				out1str(": not found\n");
   1057  1.35       dsl 			err = 127;
   1058  1.22  christos 			break;
   1059  1.22  christos 		}
   1060  1.22  christos 	}
   1061  1.27  christos 	return err;
   1062   1.1       cgd }
   1063