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