xref: /freebsd/bin/sh/exec.c (revision d8f32e72878cc01f2bf93fb164756f14db521661)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
343d7b5b93SPhilippe Charnier #if 0
353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)exec.c	8.4 (Berkeley) 6/8/95";
363d7b5b93SPhilippe Charnier #endif
374b88c807SRodney W. Grimes #endif /* not lint */
382749b141SDavid E. O'Brien #include <sys/cdefs.h>
392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
41aa9caaf6SPeter Wemm #include <sys/types.h>
42aa9caaf6SPeter Wemm #include <sys/stat.h>
43aa9caaf6SPeter Wemm #include <unistd.h>
44aa9caaf6SPeter Wemm #include <fcntl.h>
45aa9caaf6SPeter Wemm #include <errno.h>
46aa9caaf6SPeter Wemm #include <stdlib.h>
47aa9caaf6SPeter Wemm 
484b88c807SRodney W. Grimes /*
494b88c807SRodney W. Grimes  * When commands are first encountered, they are entered in a hash table.
504b88c807SRodney W. Grimes  * This ensures that a full path search will not have to be done for them
514b88c807SRodney W. Grimes  * on each invocation.
524b88c807SRodney W. Grimes  *
534b88c807SRodney W. Grimes  * We should investigate converting to a linear search, even though that
544b88c807SRodney W. Grimes  * would make the command name "hash" a misnomer.
554b88c807SRodney W. Grimes  */
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes #include "shell.h"
584b88c807SRodney W. Grimes #include "main.h"
594b88c807SRodney W. Grimes #include "nodes.h"
604b88c807SRodney W. Grimes #include "parser.h"
614b88c807SRodney W. Grimes #include "redir.h"
624b88c807SRodney W. Grimes #include "eval.h"
634b88c807SRodney W. Grimes #include "exec.h"
644b88c807SRodney W. Grimes #include "builtins.h"
654b88c807SRodney W. Grimes #include "var.h"
664b88c807SRodney W. Grimes #include "options.h"
674b88c807SRodney W. Grimes #include "input.h"
684b88c807SRodney W. Grimes #include "output.h"
694b88c807SRodney W. Grimes #include "syntax.h"
704b88c807SRodney W. Grimes #include "memalloc.h"
714b88c807SRodney W. Grimes #include "error.h"
724b88c807SRodney W. Grimes #include "init.h"
734b88c807SRodney W. Grimes #include "mystring.h"
74aa9caaf6SPeter Wemm #include "show.h"
754b88c807SRodney W. Grimes #include "jobs.h"
7676ad65f7SSteve Price #include "alias.h"
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes 
794b88c807SRodney W. Grimes #define CMDTABLESIZE 31		/* should be prime */
804b88c807SRodney W. Grimes #define ARB 1			/* actual size determined at run time */
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes 
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes struct tblentry {
854b88c807SRodney W. Grimes 	struct tblentry *next;	/* next entry in hash chain */
864b88c807SRodney W. Grimes 	union param param;	/* definition of builtin function */
8785170a4aSStefan Farfeleder 	int special;		/* flag for special builtin commands */
884b88c807SRodney W. Grimes 	short cmdtype;		/* index identifying command */
894b88c807SRodney W. Grimes 	char rehash;		/* if set, cd done since entry created */
904b88c807SRodney W. Grimes 	char cmdname[ARB];	/* name of command */
914b88c807SRodney W. Grimes };
924b88c807SRodney W. Grimes 
934b88c807SRodney W. Grimes 
94aa7b6f82SDavid E. O'Brien static struct tblentry *cmdtable[CMDTABLESIZE];
95aa7b6f82SDavid E. O'Brien static int builtinloc = -1;		/* index in path of %builtin, or -1 */
96ab0a2172SSteve Price int exerrno = 0;			/* Last exec error */
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes 
9988328642SDavid E. O'Brien static void tryexec(char *, char **, char **);
10088328642SDavid E. O'Brien static void printentry(struct tblentry *, int);
10188328642SDavid E. O'Brien static struct tblentry *cmdlookup(const char *, int);
10288328642SDavid E. O'Brien static void delete_cmd_entry(void);
1034b88c807SRodney W. Grimes 
1044b88c807SRodney W. Grimes 
1054b88c807SRodney W. Grimes 
1064b88c807SRodney W. Grimes /*
1074b88c807SRodney W. Grimes  * Exec a program.  Never returns.  If you change this routine, you may
1084b88c807SRodney W. Grimes  * have to change the find_command routine as well.
1094b88c807SRodney W. Grimes  */
1104b88c807SRodney W. Grimes 
1114b88c807SRodney W. Grimes void
112384aedabSJilles Tjoelker shellexec(char **argv, char **envp, const char *path, int idx)
1134b88c807SRodney W. Grimes {
1144b88c807SRodney W. Grimes 	char *cmdname;
1154b88c807SRodney W. Grimes 	int e;
1164b88c807SRodney W. Grimes 
1174b88c807SRodney W. Grimes 	if (strchr(argv[0], '/') != NULL) {
1184b88c807SRodney W. Grimes 		tryexec(argv[0], argv, envp);
1194b88c807SRodney W. Grimes 		e = errno;
1204b88c807SRodney W. Grimes 	} else {
1214b88c807SRodney W. Grimes 		e = ENOENT;
1224b88c807SRodney W. Grimes 		while ((cmdname = padvance(&path, argv[0])) != NULL) {
123384aedabSJilles Tjoelker 			if (--idx < 0 && pathopt == NULL) {
1244b88c807SRodney W. Grimes 				tryexec(cmdname, argv, envp);
1254b88c807SRodney W. Grimes 				if (errno != ENOENT && errno != ENOTDIR)
1264b88c807SRodney W. Grimes 					e = errno;
1274b88c807SRodney W. Grimes 			}
1284b88c807SRodney W. Grimes 			stunalloc(cmdname);
1294b88c807SRodney W. Grimes 		}
1304b88c807SRodney W. Grimes 	}
131ab0a2172SSteve Price 
132ab0a2172SSteve Price 	/* Map to POSIX errors */
133ab0a2172SSteve Price 	switch (e) {
134ab0a2172SSteve Price 	case EACCES:
135ab0a2172SSteve Price 		exerrno = 126;
136ab0a2172SSteve Price 		break;
137ab0a2172SSteve Price 	case ENOENT:
138ab0a2172SSteve Price 		exerrno = 127;
139ab0a2172SSteve Price 		break;
140ab0a2172SSteve Price 	default:
141ab0a2172SSteve Price 		exerrno = 2;
142ab0a2172SSteve Price 		break;
143ab0a2172SSteve Price 	}
1448c395729STim J. Robbins 	if (e == ENOENT || e == ENOTDIR)
1458c395729STim J. Robbins 		exerror(EXEXEC, "%s: not found", argv[0]);
1461c59560dSTim J. Robbins 	exerror(EXEXEC, "%s: %s", argv[0], strerror(e));
1474b88c807SRodney W. Grimes }
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes 
15088328642SDavid E. O'Brien static void
1515134c3f7SWarner Losh tryexec(char *cmd, char **argv, char **envp)
1524b88c807SRodney W. Grimes {
1534b88c807SRodney W. Grimes 	int e;
1544b88c807SRodney W. Grimes 
1554b88c807SRodney W. Grimes 	execve(cmd, argv, envp);
1564b88c807SRodney W. Grimes 	e = errno;
1574b88c807SRodney W. Grimes 	if (e == ENOEXEC) {
1584b88c807SRodney W. Grimes 		initshellproc();
1594b88c807SRodney W. Grimes 		setinputfile(cmd, 0);
1604b88c807SRodney W. Grimes 		commandname = arg0 = savestr(argv[0]);
1614b88c807SRodney W. Grimes 		setparam(argv + 1);
1624b88c807SRodney W. Grimes 		exraise(EXSHELLPROC);
1634b88c807SRodney W. Grimes 		/*NOTREACHED*/
1644b88c807SRodney W. Grimes 	}
1654b88c807SRodney W. Grimes 	errno = e;
1664b88c807SRodney W. Grimes }
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes /*
1694b88c807SRodney W. Grimes  * Do a path search.  The variable path (passed by reference) should be
1704b88c807SRodney W. Grimes  * set to the start of the path before the first call; padvance will update
1714b88c807SRodney W. Grimes  * this value as it proceeds.  Successive calls to padvance will return
1724b88c807SRodney W. Grimes  * the possible path expansions in sequence.  If an option (indicated by
1734b88c807SRodney W. Grimes  * a percent sign) appears in the path entry then the global variable
1744b88c807SRodney W. Grimes  * pathopt will be set to point to it; otherwise pathopt will be set to
1754b88c807SRodney W. Grimes  * NULL.
1764b88c807SRodney W. Grimes  */
1774b88c807SRodney W. Grimes 
1782cac6e36SJilles Tjoelker const char *pathopt;
1794b88c807SRodney W. Grimes 
1804b88c807SRodney W. Grimes char *
1812cac6e36SJilles Tjoelker padvance(const char **path, const char *name)
1824b88c807SRodney W. Grimes {
1832cac6e36SJilles Tjoelker 	const char *p, *start;
1842cac6e36SJilles Tjoelker 	char *q;
1854b88c807SRodney W. Grimes 	int len;
1864b88c807SRodney W. Grimes 
1874b88c807SRodney W. Grimes 	if (*path == NULL)
1884b88c807SRodney W. Grimes 		return NULL;
1894b88c807SRodney W. Grimes 	start = *path;
19035f2d3b6SRalf S. Engelschall 	for (p = start; *p && *p != ':' && *p != '%'; p++)
19135f2d3b6SRalf S. Engelschall 		; /* nothing */
1924b88c807SRodney W. Grimes 	len = p - start + strlen(name) + 2;	/* "2" is for '/' and '\0' */
193*d8f32e72SJilles Tjoelker 	STARTSTACKSTR(q);
194*d8f32e72SJilles Tjoelker 	CHECKSTRSPACE(len, q);
1954b88c807SRodney W. Grimes 	if (p != start) {
196aa9caaf6SPeter Wemm 		memcpy(q, start, p - start);
1974b88c807SRodney W. Grimes 		q += p - start;
1984b88c807SRodney W. Grimes 		*q++ = '/';
1994b88c807SRodney W. Grimes 	}
2004b88c807SRodney W. Grimes 	strcpy(q, name);
2014b88c807SRodney W. Grimes 	pathopt = NULL;
2024b88c807SRodney W. Grimes 	if (*p == '%') {
2034b88c807SRodney W. Grimes 		pathopt = ++p;
2044b88c807SRodney W. Grimes 		while (*p && *p != ':')  p++;
2054b88c807SRodney W. Grimes 	}
2064b88c807SRodney W. Grimes 	if (*p == ':')
2074b88c807SRodney W. Grimes 		*path = p + 1;
2084b88c807SRodney W. Grimes 	else
2094b88c807SRodney W. Grimes 		*path = NULL;
2104b88c807SRodney W. Grimes 	return stalloc(len);
2114b88c807SRodney W. Grimes }
2124b88c807SRodney W. Grimes 
2134b88c807SRodney W. Grimes 
2144b88c807SRodney W. Grimes 
2154b88c807SRodney W. Grimes /*** Command hashing code ***/
2164b88c807SRodney W. Grimes 
2174b88c807SRodney W. Grimes 
218aa9caaf6SPeter Wemm int
2195134c3f7SWarner Losh hashcmd(int argc __unused, char **argv __unused)
220aa9caaf6SPeter Wemm {
2214b88c807SRodney W. Grimes 	struct tblentry **pp;
2224b88c807SRodney W. Grimes 	struct tblentry *cmdp;
2234b88c807SRodney W. Grimes 	int c;
2244b88c807SRodney W. Grimes 	int verbose;
2254b88c807SRodney W. Grimes 	struct cmdentry entry;
2264b88c807SRodney W. Grimes 	char *name;
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes 	verbose = 0;
2294b88c807SRodney W. Grimes 	while ((c = nextopt("rv")) != '\0') {
2304b88c807SRodney W. Grimes 		if (c == 'r') {
2314b88c807SRodney W. Grimes 			clearcmdentry(0);
2324b88c807SRodney W. Grimes 		} else if (c == 'v') {
2334b88c807SRodney W. Grimes 			verbose++;
2344b88c807SRodney W. Grimes 		}
2354b88c807SRodney W. Grimes 	}
2364b88c807SRodney W. Grimes 	if (*argptr == NULL) {
2374b88c807SRodney W. Grimes 		for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
2384b88c807SRodney W. Grimes 			for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
23983952a11STim J. Robbins 				if (cmdp->cmdtype == CMDNORMAL)
2404b88c807SRodney W. Grimes 					printentry(cmdp, verbose);
2414b88c807SRodney W. Grimes 			}
2424b88c807SRodney W. Grimes 		}
2434b88c807SRodney W. Grimes 		return 0;
2444b88c807SRodney W. Grimes 	}
2454b88c807SRodney W. Grimes 	while ((name = *argptr) != NULL) {
2464b88c807SRodney W. Grimes 		if ((cmdp = cmdlookup(name, 0)) != NULL
2474b88c807SRodney W. Grimes 		 && (cmdp->cmdtype == CMDNORMAL
248aa9caaf6SPeter Wemm 		     || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))
2494b88c807SRodney W. Grimes 			delete_cmd_entry();
250c848bc18SJilles Tjoelker 		find_command(name, &entry, DO_ERR, pathval());
2514b88c807SRodney W. Grimes 		if (verbose) {
2524b88c807SRodney W. Grimes 			if (entry.cmdtype != CMDUNKNOWN) {	/* if no error msg */
2534b88c807SRodney W. Grimes 				cmdp = cmdlookup(name, 0);
2544c7e4a54SGeorge C A Reid 				if (cmdp != NULL)
2554b88c807SRodney W. Grimes 					printentry(cmdp, verbose);
2564c7e4a54SGeorge C A Reid 				else
257f7cc73afSJilles Tjoelker 					outfmt(out2, "%s: not found\n", name);
2584b88c807SRodney W. Grimes 			}
2594b88c807SRodney W. Grimes 			flushall();
2604b88c807SRodney W. Grimes 		}
2614b88c807SRodney W. Grimes 		argptr++;
2624b88c807SRodney W. Grimes 	}
2634b88c807SRodney W. Grimes 	return 0;
2644b88c807SRodney W. Grimes }
2654b88c807SRodney W. Grimes 
2664b88c807SRodney W. Grimes 
26788328642SDavid E. O'Brien static void
2685134c3f7SWarner Losh printentry(struct tblentry *cmdp, int verbose)
2694b88c807SRodney W. Grimes {
270384aedabSJilles Tjoelker 	int idx;
2712cac6e36SJilles Tjoelker 	const char *path;
2724b88c807SRodney W. Grimes 	char *name;
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes 	if (cmdp->cmdtype == CMDNORMAL) {
275384aedabSJilles Tjoelker 		idx = cmdp->param.index;
2764b88c807SRodney W. Grimes 		path = pathval();
2774b88c807SRodney W. Grimes 		do {
2784b88c807SRodney W. Grimes 			name = padvance(&path, cmdp->cmdname);
2794b88c807SRodney W. Grimes 			stunalloc(name);
280384aedabSJilles Tjoelker 		} while (--idx >= 0);
2814b88c807SRodney W. Grimes 		out1str(name);
2824b88c807SRodney W. Grimes 	} else if (cmdp->cmdtype == CMDBUILTIN) {
2834b88c807SRodney W. Grimes 		out1fmt("builtin %s", cmdp->cmdname);
2844b88c807SRodney W. Grimes 	} else if (cmdp->cmdtype == CMDFUNCTION) {
2854b88c807SRodney W. Grimes 		out1fmt("function %s", cmdp->cmdname);
2864b88c807SRodney W. Grimes 		if (verbose) {
2874b88c807SRodney W. Grimes 			INTOFF;
288e16947f8SJilles Tjoelker 			name = commandtext(getfuncnode(cmdp->param.func));
2894b88c807SRodney W. Grimes 			out1c(' ');
2904b88c807SRodney W. Grimes 			out1str(name);
2914b88c807SRodney W. Grimes 			ckfree(name);
2924b88c807SRodney W. Grimes 			INTON;
2934b88c807SRodney W. Grimes 		}
2944b88c807SRodney W. Grimes #ifdef DEBUG
2954b88c807SRodney W. Grimes 	} else {
2964b88c807SRodney W. Grimes 		error("internal error: cmdtype %d", cmdp->cmdtype);
2974b88c807SRodney W. Grimes #endif
2984b88c807SRodney W. Grimes 	}
2994b88c807SRodney W. Grimes 	if (cmdp->rehash)
3004b88c807SRodney W. Grimes 		out1c('*');
3014b88c807SRodney W. Grimes 	out1c('\n');
3024b88c807SRodney W. Grimes }
3034b88c807SRodney W. Grimes 
3044b88c807SRodney W. Grimes 
3054b88c807SRodney W. Grimes 
3064b88c807SRodney W. Grimes /*
3074b88c807SRodney W. Grimes  * Resolve a command name.  If you change this routine, you may have to
3084b88c807SRodney W. Grimes  * change the shellexec routine as well.
3094b88c807SRodney W. Grimes  */
3104b88c807SRodney W. Grimes 
3114b88c807SRodney W. Grimes void
312c848bc18SJilles Tjoelker find_command(const char *name, struct cmdentry *entry, int act,
3132cac6e36SJilles Tjoelker     const char *path)
3144b88c807SRodney W. Grimes {
315c848bc18SJilles Tjoelker 	struct tblentry *cmdp, loc_cmd;
316384aedabSJilles Tjoelker 	int idx;
3174b88c807SRodney W. Grimes 	int prev;
3184b88c807SRodney W. Grimes 	char *fullname;
3194b88c807SRodney W. Grimes 	struct stat statb;
3204b88c807SRodney W. Grimes 	int e;
3214b88c807SRodney W. Grimes 	int i;
32285170a4aSStefan Farfeleder 	int spec;
3234b88c807SRodney W. Grimes 
3244b88c807SRodney W. Grimes 	/* If name contains a slash, don't use the hash table */
3254b88c807SRodney W. Grimes 	if (strchr(name, '/') != NULL) {
3264b88c807SRodney W. Grimes 		entry->cmdtype = CMDNORMAL;
3274b88c807SRodney W. Grimes 		entry->u.index = 0;
3284b88c807SRodney W. Grimes 		return;
3294b88c807SRodney W. Grimes 	}
3304b88c807SRodney W. Grimes 
3314b88c807SRodney W. Grimes 	/* If name is in the table, and not invalidated by cd, we're done */
332c848bc18SJilles Tjoelker 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0) {
333c848bc18SJilles Tjoelker 		if (cmdp->cmdtype == CMDFUNCTION && act & DO_NOFUNC)
334c848bc18SJilles Tjoelker 			cmdp = NULL;
335c848bc18SJilles Tjoelker 		else
3364b88c807SRodney W. Grimes 			goto success;
337c848bc18SJilles Tjoelker 	}
3384b88c807SRodney W. Grimes 
3394b88c807SRodney W. Grimes 	/* If %builtin not in path, check for builtin next */
34085170a4aSStefan Farfeleder 	if (builtinloc < 0 && (i = find_builtin(name, &spec)) >= 0) {
3414b88c807SRodney W. Grimes 		INTOFF;
3424b88c807SRodney W. Grimes 		cmdp = cmdlookup(name, 1);
343c848bc18SJilles Tjoelker 		if (cmdp->cmdtype == CMDFUNCTION)
344c848bc18SJilles Tjoelker 			cmdp = &loc_cmd;
3454b88c807SRodney W. Grimes 		cmdp->cmdtype = CMDBUILTIN;
3464b88c807SRodney W. Grimes 		cmdp->param.index = i;
34785170a4aSStefan Farfeleder 		cmdp->special = spec;
3484b88c807SRodney W. Grimes 		INTON;
3494b88c807SRodney W. Grimes 		goto success;
3504b88c807SRodney W. Grimes 	}
3514b88c807SRodney W. Grimes 
3524b88c807SRodney W. Grimes 	/* We have to search path. */
3534b88c807SRodney W. Grimes 	prev = -1;		/* where to start */
3544b88c807SRodney W. Grimes 	if (cmdp) {		/* doing a rehash */
3554b88c807SRodney W. Grimes 		if (cmdp->cmdtype == CMDBUILTIN)
3564b88c807SRodney W. Grimes 			prev = builtinloc;
3574b88c807SRodney W. Grimes 		else
3584b88c807SRodney W. Grimes 			prev = cmdp->param.index;
3594b88c807SRodney W. Grimes 	}
3604b88c807SRodney W. Grimes 
3614b88c807SRodney W. Grimes 	e = ENOENT;
362384aedabSJilles Tjoelker 	idx = -1;
3634b88c807SRodney W. Grimes loop:
3644b88c807SRodney W. Grimes 	while ((fullname = padvance(&path, name)) != NULL) {
3654b88c807SRodney W. Grimes 		stunalloc(fullname);
366384aedabSJilles Tjoelker 		idx++;
3674b88c807SRodney W. Grimes 		if (pathopt) {
3684b88c807SRodney W. Grimes 			if (prefix("builtin", pathopt)) {
36985170a4aSStefan Farfeleder 				if ((i = find_builtin(name, &spec)) < 0)
3704b88c807SRodney W. Grimes 					goto loop;
3714b88c807SRodney W. Grimes 				INTOFF;
3724b88c807SRodney W. Grimes 				cmdp = cmdlookup(name, 1);
373c848bc18SJilles Tjoelker 				if (cmdp->cmdtype == CMDFUNCTION)
374c848bc18SJilles Tjoelker 					cmdp = &loc_cmd;
3754b88c807SRodney W. Grimes 				cmdp->cmdtype = CMDBUILTIN;
3764b88c807SRodney W. Grimes 				cmdp->param.index = i;
37785170a4aSStefan Farfeleder 				cmdp->special = spec;
3784b88c807SRodney W. Grimes 				INTON;
3794b88c807SRodney W. Grimes 				goto success;
3804b88c807SRodney W. Grimes 			} else if (prefix("func", pathopt)) {
3814b88c807SRodney W. Grimes 				/* handled below */
3824b88c807SRodney W. Grimes 			} else {
3834b88c807SRodney W. Grimes 				goto loop;	/* ignore unimplemented options */
3844b88c807SRodney W. Grimes 			}
3854b88c807SRodney W. Grimes 		}
3864b88c807SRodney W. Grimes 		/* if rehash, don't redo absolute path names */
387384aedabSJilles Tjoelker 		if (fullname[0] == '/' && idx <= prev) {
388384aedabSJilles Tjoelker 			if (idx < prev)
3894b88c807SRodney W. Grimes 				goto loop;
3904b88c807SRodney W. Grimes 			TRACE(("searchexec \"%s\": no change\n", name));
3914b88c807SRodney W. Grimes 			goto success;
3924b88c807SRodney W. Grimes 		}
3932628ebdbSTim J. Robbins 		if (stat(fullname, &statb) < 0) {
3944b88c807SRodney W. Grimes 			if (errno != ENOENT && errno != ENOTDIR)
3954b88c807SRodney W. Grimes 				e = errno;
3964b88c807SRodney W. Grimes 			goto loop;
3974b88c807SRodney W. Grimes 		}
3984b88c807SRodney W. Grimes 		e = EACCES;	/* if we fail, this will be the error */
399aa9caaf6SPeter Wemm 		if (!S_ISREG(statb.st_mode))
4004b88c807SRodney W. Grimes 			goto loop;
4014b88c807SRodney W. Grimes 		if (pathopt) {		/* this is a %func directory */
4024b88c807SRodney W. Grimes 			stalloc(strlen(fullname) + 1);
4034b88c807SRodney W. Grimes 			readcmdfile(fullname);
4044b88c807SRodney W. Grimes 			if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
4054b88c807SRodney W. Grimes 				error("%s not defined in %s", name, fullname);
4064b88c807SRodney W. Grimes 			stunalloc(fullname);
4074b88c807SRodney W. Grimes 			goto success;
4084b88c807SRodney W. Grimes 		}
4094b88c807SRodney W. Grimes #ifdef notdef
4104b88c807SRodney W. Grimes 		if (statb.st_uid == geteuid()) {
4114b88c807SRodney W. Grimes 			if ((statb.st_mode & 0100) == 0)
4124b88c807SRodney W. Grimes 				goto loop;
4134b88c807SRodney W. Grimes 		} else if (statb.st_gid == getegid()) {
4144b88c807SRodney W. Grimes 			if ((statb.st_mode & 010) == 0)
4154b88c807SRodney W. Grimes 				goto loop;
4164b88c807SRodney W. Grimes 		} else {
4174b88c807SRodney W. Grimes 			if ((statb.st_mode & 01) == 0)
4184b88c807SRodney W. Grimes 				goto loop;
4194b88c807SRodney W. Grimes 		}
4204b88c807SRodney W. Grimes #endif
4214b88c807SRodney W. Grimes 		TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
4224b88c807SRodney W. Grimes 		INTOFF;
4234b88c807SRodney W. Grimes 		cmdp = cmdlookup(name, 1);
424c848bc18SJilles Tjoelker 		if (cmdp->cmdtype == CMDFUNCTION)
425c848bc18SJilles Tjoelker 			cmdp = &loc_cmd;
4264b88c807SRodney W. Grimes 		cmdp->cmdtype = CMDNORMAL;
427384aedabSJilles Tjoelker 		cmdp->param.index = idx;
4284b88c807SRodney W. Grimes 		INTON;
4294b88c807SRodney W. Grimes 		goto success;
4304b88c807SRodney W. Grimes 	}
4314b88c807SRodney W. Grimes 
4324b88c807SRodney W. Grimes 	/* We failed.  If there was an entry for this command, delete it */
433c848bc18SJilles Tjoelker 	if (cmdp && cmdp->cmdtype != CMDFUNCTION)
4344b88c807SRodney W. Grimes 		delete_cmd_entry();
435c848bc18SJilles Tjoelker 	if (act & DO_ERR) {
4368c395729STim J. Robbins 		if (e == ENOENT || e == ENOTDIR)
4378c395729STim J. Robbins 			outfmt(out2, "%s: not found\n", name);
4388c395729STim J. Robbins 		else
4391c59560dSTim J. Robbins 			outfmt(out2, "%s: %s\n", name, strerror(e));
4408c395729STim J. Robbins 	}
4414b88c807SRodney W. Grimes 	entry->cmdtype = CMDUNKNOWN;
442640b70e4SJilles Tjoelker 	entry->u.index = 0;
4434b88c807SRodney W. Grimes 	return;
4444b88c807SRodney W. Grimes 
4454b88c807SRodney W. Grimes success:
4464b88c807SRodney W. Grimes 	cmdp->rehash = 0;
4474b88c807SRodney W. Grimes 	entry->cmdtype = cmdp->cmdtype;
4484b88c807SRodney W. Grimes 	entry->u = cmdp->param;
44985170a4aSStefan Farfeleder 	entry->special = cmdp->special;
4504b88c807SRodney W. Grimes }
4514b88c807SRodney W. Grimes 
4524b88c807SRodney W. Grimes 
4534b88c807SRodney W. Grimes 
4544b88c807SRodney W. Grimes /*
4554b88c807SRodney W. Grimes  * Search the table of builtin commands.
4564b88c807SRodney W. Grimes  */
4574b88c807SRodney W. Grimes 
4584b88c807SRodney W. Grimes int
4592cac6e36SJilles Tjoelker find_builtin(const char *name, int *special)
4604b88c807SRodney W. Grimes {
46176ad65f7SSteve Price 	const struct builtincmd *bp;
4624b88c807SRodney W. Grimes 
4634b88c807SRodney W. Grimes 	for (bp = builtincmd ; bp->name ; bp++) {
46485170a4aSStefan Farfeleder 		if (*bp->name == *name && equal(bp->name, name)) {
46585170a4aSStefan Farfeleder 			*special = bp->special;
4664b88c807SRodney W. Grimes 			return bp->code;
4674b88c807SRodney W. Grimes 		}
46885170a4aSStefan Farfeleder 	}
4694b88c807SRodney W. Grimes 	return -1;
4704b88c807SRodney W. Grimes }
4714b88c807SRodney W. Grimes 
4724b88c807SRodney W. Grimes 
4734b88c807SRodney W. Grimes 
4744b88c807SRodney W. Grimes /*
4754b88c807SRodney W. Grimes  * Called when a cd is done.  Marks all commands so the next time they
4764b88c807SRodney W. Grimes  * are executed they will be rehashed.
4774b88c807SRodney W. Grimes  */
4784b88c807SRodney W. Grimes 
4794b88c807SRodney W. Grimes void
4805134c3f7SWarner Losh hashcd(void)
4815134c3f7SWarner Losh {
4824b88c807SRodney W. Grimes 	struct tblentry **pp;
4834b88c807SRodney W. Grimes 	struct tblentry *cmdp;
4844b88c807SRodney W. Grimes 
4854b88c807SRodney W. Grimes 	for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
4864b88c807SRodney W. Grimes 		for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
4874b88c807SRodney W. Grimes 			if (cmdp->cmdtype == CMDNORMAL
488aa9caaf6SPeter Wemm 			 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
4894b88c807SRodney W. Grimes 				cmdp->rehash = 1;
4904b88c807SRodney W. Grimes 		}
4914b88c807SRodney W. Grimes 	}
4924b88c807SRodney W. Grimes }
4934b88c807SRodney W. Grimes 
4944b88c807SRodney W. Grimes 
4954b88c807SRodney W. Grimes 
4964b88c807SRodney W. Grimes /*
4974b88c807SRodney W. Grimes  * Called before PATH is changed.  The argument is the new value of PATH;
4984b88c807SRodney W. Grimes  * pathval() still returns the old value at this point.  Called with
4994b88c807SRodney W. Grimes  * interrupts off.
5004b88c807SRodney W. Grimes  */
5014b88c807SRodney W. Grimes 
5024b88c807SRodney W. Grimes void
5035134c3f7SWarner Losh changepath(const char *newval)
5044b88c807SRodney W. Grimes {
505ab0a2172SSteve Price 	const char *old, *new;
506384aedabSJilles Tjoelker 	int idx;
5074b88c807SRodney W. Grimes 	int firstchange;
5084b88c807SRodney W. Grimes 	int bltin;
5094b88c807SRodney W. Grimes 
5104b88c807SRodney W. Grimes 	old = pathval();
5114b88c807SRodney W. Grimes 	new = newval;
5124b88c807SRodney W. Grimes 	firstchange = 9999;	/* assume no change */
513384aedabSJilles Tjoelker 	idx = 0;
5144b88c807SRodney W. Grimes 	bltin = -1;
5154b88c807SRodney W. Grimes 	for (;;) {
5164b88c807SRodney W. Grimes 		if (*old != *new) {
517384aedabSJilles Tjoelker 			firstchange = idx;
518aa9caaf6SPeter Wemm 			if ((*old == '\0' && *new == ':')
519aa9caaf6SPeter Wemm 			 || (*old == ':' && *new == '\0'))
5204b88c807SRodney W. Grimes 				firstchange++;
5214b88c807SRodney W. Grimes 			old = new;	/* ignore subsequent differences */
5224b88c807SRodney W. Grimes 		}
5234b88c807SRodney W. Grimes 		if (*new == '\0')
5244b88c807SRodney W. Grimes 			break;
5254b88c807SRodney W. Grimes 		if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
526384aedabSJilles Tjoelker 			bltin = idx;
5274b88c807SRodney W. Grimes 		if (*new == ':') {
528384aedabSJilles Tjoelker 			idx++;
5294b88c807SRodney W. Grimes 		}
5304b88c807SRodney W. Grimes 		new++, old++;
5314b88c807SRodney W. Grimes 	}
5324b88c807SRodney W. Grimes 	if (builtinloc < 0 && bltin >= 0)
5334b88c807SRodney W. Grimes 		builtinloc = bltin;		/* zap builtins */
5344b88c807SRodney W. Grimes 	if (builtinloc >= 0 && bltin < 0)
5354b88c807SRodney W. Grimes 		firstchange = 0;
5364b88c807SRodney W. Grimes 	clearcmdentry(firstchange);
5374b88c807SRodney W. Grimes 	builtinloc = bltin;
5384b88c807SRodney W. Grimes }
5394b88c807SRodney W. Grimes 
5404b88c807SRodney W. Grimes 
5414b88c807SRodney W. Grimes /*
5424b88c807SRodney W. Grimes  * Clear out command entries.  The argument specifies the first entry in
5434b88c807SRodney W. Grimes  * PATH which has changed.
5444b88c807SRodney W. Grimes  */
5454b88c807SRodney W. Grimes 
546a436dc79SMartin Cracauer void
5475134c3f7SWarner Losh clearcmdentry(int firstchange)
548aa9caaf6SPeter Wemm {
5494b88c807SRodney W. Grimes 	struct tblentry **tblp;
5504b88c807SRodney W. Grimes 	struct tblentry **pp;
5514b88c807SRodney W. Grimes 	struct tblentry *cmdp;
5524b88c807SRodney W. Grimes 
5534b88c807SRodney W. Grimes 	INTOFF;
5544b88c807SRodney W. Grimes 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
5554b88c807SRodney W. Grimes 		pp = tblp;
5564b88c807SRodney W. Grimes 		while ((cmdp = *pp) != NULL) {
557aa9caaf6SPeter Wemm 			if ((cmdp->cmdtype == CMDNORMAL &&
558aa9caaf6SPeter Wemm 			     cmdp->param.index >= firstchange)
559aa9caaf6SPeter Wemm 			 || (cmdp->cmdtype == CMDBUILTIN &&
560aa9caaf6SPeter Wemm 			     builtinloc >= firstchange)) {
5614b88c807SRodney W. Grimes 				*pp = cmdp->next;
5624b88c807SRodney W. Grimes 				ckfree(cmdp);
5634b88c807SRodney W. Grimes 			} else {
5644b88c807SRodney W. Grimes 				pp = &cmdp->next;
5654b88c807SRodney W. Grimes 			}
5664b88c807SRodney W. Grimes 		}
5674b88c807SRodney W. Grimes 	}
5684b88c807SRodney W. Grimes 	INTON;
5694b88c807SRodney W. Grimes }
5704b88c807SRodney W. Grimes 
5714b88c807SRodney W. Grimes 
5724b88c807SRodney W. Grimes /*
5734b88c807SRodney W. Grimes  * Delete all functions.
5744b88c807SRodney W. Grimes  */
5754b88c807SRodney W. Grimes 
5764b88c807SRodney W. Grimes #ifdef mkinit
577c7893739SStefan Farfeleder MKINIT void deletefuncs(void);
5784b88c807SRodney W. Grimes 
5794b88c807SRodney W. Grimes SHELLPROC {
5804b88c807SRodney W. Grimes 	deletefuncs();
5814b88c807SRodney W. Grimes }
5824b88c807SRodney W. Grimes #endif
5834b88c807SRodney W. Grimes 
5844b88c807SRodney W. Grimes void
5855134c3f7SWarner Losh deletefuncs(void)
5865134c3f7SWarner Losh {
5874b88c807SRodney W. Grimes 	struct tblentry **tblp;
5884b88c807SRodney W. Grimes 	struct tblentry **pp;
5894b88c807SRodney W. Grimes 	struct tblentry *cmdp;
5904b88c807SRodney W. Grimes 
5914b88c807SRodney W. Grimes 	INTOFF;
5924b88c807SRodney W. Grimes 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
5934b88c807SRodney W. Grimes 		pp = tblp;
5944b88c807SRodney W. Grimes 		while ((cmdp = *pp) != NULL) {
5954b88c807SRodney W. Grimes 			if (cmdp->cmdtype == CMDFUNCTION) {
5964b88c807SRodney W. Grimes 				*pp = cmdp->next;
597eb33e843SJilles Tjoelker 				unreffunc(cmdp->param.func);
5984b88c807SRodney W. Grimes 				ckfree(cmdp);
5994b88c807SRodney W. Grimes 			} else {
6004b88c807SRodney W. Grimes 				pp = &cmdp->next;
6014b88c807SRodney W. Grimes 			}
6024b88c807SRodney W. Grimes 		}
6034b88c807SRodney W. Grimes 	}
6044b88c807SRodney W. Grimes 	INTON;
6054b88c807SRodney W. Grimes }
6064b88c807SRodney W. Grimes 
6074b88c807SRodney W. Grimes 
6084b88c807SRodney W. Grimes 
6094b88c807SRodney W. Grimes /*
6104b88c807SRodney W. Grimes  * Locate a command in the command hash table.  If "add" is nonzero,
6114b88c807SRodney W. Grimes  * add the command to the table if it is not already present.  The
6124b88c807SRodney W. Grimes  * variable "lastcmdentry" is set to point to the address of the link
6134b88c807SRodney W. Grimes  * pointing to the entry, so that delete_cmd_entry can delete the
6144b88c807SRodney W. Grimes  * entry.
6154b88c807SRodney W. Grimes  */
6164b88c807SRodney W. Grimes 
617aa7b6f82SDavid E. O'Brien static struct tblentry **lastcmdentry;
6184b88c807SRodney W. Grimes 
6194b88c807SRodney W. Grimes 
62088328642SDavid E. O'Brien static struct tblentry *
6212cac6e36SJilles Tjoelker cmdlookup(const char *name, int add)
6224b88c807SRodney W. Grimes {
6234b88c807SRodney W. Grimes 	int hashval;
6242cac6e36SJilles Tjoelker 	const char *p;
6254b88c807SRodney W. Grimes 	struct tblentry *cmdp;
6264b88c807SRodney W. Grimes 	struct tblentry **pp;
6274b88c807SRodney W. Grimes 
6284b88c807SRodney W. Grimes 	p = name;
6294b88c807SRodney W. Grimes 	hashval = *p << 4;
6304b88c807SRodney W. Grimes 	while (*p)
6314b88c807SRodney W. Grimes 		hashval += *p++;
6324b88c807SRodney W. Grimes 	hashval &= 0x7FFF;
6334b88c807SRodney W. Grimes 	pp = &cmdtable[hashval % CMDTABLESIZE];
6344b88c807SRodney W. Grimes 	for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
6354b88c807SRodney W. Grimes 		if (equal(cmdp->cmdname, name))
6364b88c807SRodney W. Grimes 			break;
6374b88c807SRodney W. Grimes 		pp = &cmdp->next;
6384b88c807SRodney W. Grimes 	}
6394b88c807SRodney W. Grimes 	if (add && cmdp == NULL) {
6404b88c807SRodney W. Grimes 		INTOFF;
6414b88c807SRodney W. Grimes 		cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
6424b88c807SRodney W. Grimes 					+ strlen(name) + 1);
6434b88c807SRodney W. Grimes 		cmdp->next = NULL;
6444b88c807SRodney W. Grimes 		cmdp->cmdtype = CMDUNKNOWN;
6454b88c807SRodney W. Grimes 		cmdp->rehash = 0;
6464b88c807SRodney W. Grimes 		strcpy(cmdp->cmdname, name);
6474b88c807SRodney W. Grimes 		INTON;
6484b88c807SRodney W. Grimes 	}
6494b88c807SRodney W. Grimes 	lastcmdentry = pp;
6504b88c807SRodney W. Grimes 	return cmdp;
6514b88c807SRodney W. Grimes }
6524b88c807SRodney W. Grimes 
6534b88c807SRodney W. Grimes /*
6544b88c807SRodney W. Grimes  * Delete the command entry returned on the last lookup.
6554b88c807SRodney W. Grimes  */
6564b88c807SRodney W. Grimes 
65788328642SDavid E. O'Brien static void
6585134c3f7SWarner Losh delete_cmd_entry(void)
6595134c3f7SWarner Losh {
6604b88c807SRodney W. Grimes 	struct tblentry *cmdp;
6614b88c807SRodney W. Grimes 
6624b88c807SRodney W. Grimes 	INTOFF;
6634b88c807SRodney W. Grimes 	cmdp = *lastcmdentry;
6644b88c807SRodney W. Grimes 	*lastcmdentry = cmdp->next;
6654b88c807SRodney W. Grimes 	ckfree(cmdp);
6664b88c807SRodney W. Grimes 	INTON;
6674b88c807SRodney W. Grimes }
6684b88c807SRodney W. Grimes 
6694b88c807SRodney W. Grimes 
6704b88c807SRodney W. Grimes 
6714b88c807SRodney W. Grimes /*
6724b88c807SRodney W. Grimes  * Add a new command entry, replacing any existing command entry for
6734b88c807SRodney W. Grimes  * the same name.
6744b88c807SRodney W. Grimes  */
6754b88c807SRodney W. Grimes 
6764b88c807SRodney W. Grimes void
6772cac6e36SJilles Tjoelker addcmdentry(const char *name, struct cmdentry *entry)
6784b88c807SRodney W. Grimes {
6794b88c807SRodney W. Grimes 	struct tblentry *cmdp;
6804b88c807SRodney W. Grimes 
6814b88c807SRodney W. Grimes 	INTOFF;
6824b88c807SRodney W. Grimes 	cmdp = cmdlookup(name, 1);
6834b88c807SRodney W. Grimes 	if (cmdp->cmdtype == CMDFUNCTION) {
684eb33e843SJilles Tjoelker 		unreffunc(cmdp->param.func);
6854b88c807SRodney W. Grimes 	}
6864b88c807SRodney W. Grimes 	cmdp->cmdtype = entry->cmdtype;
6874b88c807SRodney W. Grimes 	cmdp->param = entry->u;
6884b88c807SRodney W. Grimes 	INTON;
6894b88c807SRodney W. Grimes }
6904b88c807SRodney W. Grimes 
6914b88c807SRodney W. Grimes 
6924b88c807SRodney W. Grimes /*
6934b88c807SRodney W. Grimes  * Define a shell function.
6944b88c807SRodney W. Grimes  */
6954b88c807SRodney W. Grimes 
6964b88c807SRodney W. Grimes void
6972cac6e36SJilles Tjoelker defun(const char *name, union node *func)
6984b88c807SRodney W. Grimes {
6994b88c807SRodney W. Grimes 	struct cmdentry entry;
7004b88c807SRodney W. Grimes 
7014b88c807SRodney W. Grimes 	INTOFF;
7024b88c807SRodney W. Grimes 	entry.cmdtype = CMDFUNCTION;
7034b88c807SRodney W. Grimes 	entry.u.func = copyfunc(func);
7044b88c807SRodney W. Grimes 	addcmdentry(name, &entry);
7054b88c807SRodney W. Grimes 	INTON;
7064b88c807SRodney W. Grimes }
7074b88c807SRodney W. Grimes 
7084b88c807SRodney W. Grimes 
7094b88c807SRodney W. Grimes /*
7104b88c807SRodney W. Grimes  * Delete a function if it exists.
7114b88c807SRodney W. Grimes  */
7124b88c807SRodney W. Grimes 
7134b88c807SRodney W. Grimes int
7142cac6e36SJilles Tjoelker unsetfunc(const char *name)
7154b88c807SRodney W. Grimes {
7164b88c807SRodney W. Grimes 	struct tblentry *cmdp;
7174b88c807SRodney W. Grimes 
7184b88c807SRodney W. Grimes 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
719eb33e843SJilles Tjoelker 		unreffunc(cmdp->param.func);
7204b88c807SRodney W. Grimes 		delete_cmd_entry();
7214b88c807SRodney W. Grimes 		return (0);
7224b88c807SRodney W. Grimes 	}
723bd766773SDag-Erling Smørgrav 	return (0);
7244b88c807SRodney W. Grimes }
72576ad65f7SSteve Price 
72676ad65f7SSteve Price /*
727b2f153feSStefan Farfeleder  * Shared code for the following builtin commands:
728b2f153feSStefan Farfeleder  *    type, command -v, command -V
72976ad65f7SSteve Price  */
73076ad65f7SSteve Price 
73176ad65f7SSteve Price int
73206a8a57fSJilles Tjoelker typecmd_impl(int argc, char **argv, int cmd, const char *path)
73376ad65f7SSteve Price {
73476ad65f7SSteve Price 	struct cmdentry entry;
73576ad65f7SSteve Price 	struct tblentry *cmdp;
736384aedabSJilles Tjoelker 	const char *const *pp;
73776ad65f7SSteve Price 	struct alias *ap;
73876ad65f7SSteve Price 	int i;
739384aedabSJilles Tjoelker 	int error1 = 0;
74076ad65f7SSteve Price 
74106a8a57fSJilles Tjoelker 	if (path != pathval())
74206a8a57fSJilles Tjoelker 		clearcmdentry(0);
74306a8a57fSJilles Tjoelker 
74476ad65f7SSteve Price 	for (i = 1; i < argc; i++) {
74576ad65f7SSteve Price 		/* First look at the keywords */
746384aedabSJilles Tjoelker 		for (pp = parsekwd; *pp; pp++)
74776ad65f7SSteve Price 			if (**pp == *argv[i] && equal(*pp, argv[i]))
74876ad65f7SSteve Price 				break;
74976ad65f7SSteve Price 
75076ad65f7SSteve Price 		if (*pp) {
751b2f153feSStefan Farfeleder 			if (cmd == TYPECMD_SMALLV)
752b2f153feSStefan Farfeleder 				out1fmt("%s\n", argv[i]);
753b2f153feSStefan Farfeleder 			else
754f7bbf3ffSStefan Farfeleder 				out1fmt("%s is a shell keyword\n", argv[i]);
75576ad65f7SSteve Price 			continue;
75676ad65f7SSteve Price 		}
75776ad65f7SSteve Price 
75876ad65f7SSteve Price 		/* Then look at the aliases */
75976ad65f7SSteve Price 		if ((ap = lookupalias(argv[i], 1)) != NULL) {
760b2f153feSStefan Farfeleder 			if (cmd == TYPECMD_SMALLV)
761b2f153feSStefan Farfeleder 				out1fmt("alias %s='%s'\n", argv[i], ap->val);
762b2f153feSStefan Farfeleder 			else
763f7bbf3ffSStefan Farfeleder 				out1fmt("%s is an alias for %s\n", argv[i],
764f7bbf3ffSStefan Farfeleder 				    ap->val);
76576ad65f7SSteve Price 			continue;
76676ad65f7SSteve Price 		}
76776ad65f7SSteve Price 
76876ad65f7SSteve Price 		/* Then check if it is a tracked alias */
76976ad65f7SSteve Price 		if ((cmdp = cmdlookup(argv[i], 0)) != NULL) {
77076ad65f7SSteve Price 			entry.cmdtype = cmdp->cmdtype;
77176ad65f7SSteve Price 			entry.u = cmdp->param;
77230268dfaSJilles Tjoelker 			entry.special = cmdp->special;
77376ad65f7SSteve Price 		}
77476ad65f7SSteve Price 		else {
77576ad65f7SSteve Price 			/* Finally use brute force */
77606a8a57fSJilles Tjoelker 			find_command(argv[i], &entry, 0, path);
77776ad65f7SSteve Price 		}
77876ad65f7SSteve Price 
77976ad65f7SSteve Price 		switch (entry.cmdtype) {
78076ad65f7SSteve Price 		case CMDNORMAL: {
781d753a425SMartin Cracauer 			if (strchr(argv[i], '/') == NULL) {
78206a8a57fSJilles Tjoelker 				const char *path2 = path;
7832cac6e36SJilles Tjoelker 				char *name;
784d753a425SMartin Cracauer 				int j = entry.u.index;
78576ad65f7SSteve Price 				do {
78606a8a57fSJilles Tjoelker 					name = padvance(&path2, argv[i]);
78776ad65f7SSteve Price 					stunalloc(name);
78876ad65f7SSteve Price 				} while (--j >= 0);
789b2f153feSStefan Farfeleder 				if (cmd == TYPECMD_SMALLV)
790b2f153feSStefan Farfeleder 					out1fmt("%s\n", name);
791b2f153feSStefan Farfeleder 				else
792f7bbf3ffSStefan Farfeleder 					out1fmt("%s is%s %s\n", argv[i],
793b2f153feSStefan Farfeleder 					    (cmdp && cmd == TYPECMD_TYPE) ?
794b2f153feSStefan Farfeleder 						" a tracked alias for" : "",
795b2f153feSStefan Farfeleder 					    name);
796d753a425SMartin Cracauer 			} else {
797d92e35fdSStefan Farfeleder 				if (eaccess(argv[i], X_OK) == 0) {
798b2f153feSStefan Farfeleder 					if (cmd == TYPECMD_SMALLV)
799b2f153feSStefan Farfeleder 						out1fmt("%s\n", argv[i]);
800b2f153feSStefan Farfeleder 					else
801f7bbf3ffSStefan Farfeleder 						out1fmt("%s is %s\n", argv[i],
802f7bbf3ffSStefan Farfeleder 						    argv[i]);
803f30842baSStefan Farfeleder 				} else {
804f30842baSStefan Farfeleder 					if (cmd != TYPECMD_SMALLV)
805f7bbf3ffSStefan Farfeleder 						outfmt(out2, "%s: %s\n",
806f7bbf3ffSStefan Farfeleder 						    argv[i], strerror(errno));
807384aedabSJilles Tjoelker 					error1 |= 127;
808b2f153feSStefan Farfeleder 				}
809d753a425SMartin Cracauer 			}
81076ad65f7SSteve Price 			break;
81176ad65f7SSteve Price 		}
81276ad65f7SSteve Price 		case CMDFUNCTION:
813b2f153feSStefan Farfeleder 			if (cmd == TYPECMD_SMALLV)
814b2f153feSStefan Farfeleder 				out1fmt("%s\n", argv[i]);
815b2f153feSStefan Farfeleder 			else
816f7bbf3ffSStefan Farfeleder 				out1fmt("%s is a shell function\n", argv[i]);
81776ad65f7SSteve Price 			break;
81876ad65f7SSteve Price 
81976ad65f7SSteve Price 		case CMDBUILTIN:
820b2f153feSStefan Farfeleder 			if (cmd == TYPECMD_SMALLV)
821b2f153feSStefan Farfeleder 				out1fmt("%s\n", argv[i]);
82230268dfaSJilles Tjoelker 			else if (entry.special)
82330268dfaSJilles Tjoelker 				out1fmt("%s is a special shell builtin\n",
82430268dfaSJilles Tjoelker 				    argv[i]);
825b2f153feSStefan Farfeleder 			else
826f7bbf3ffSStefan Farfeleder 				out1fmt("%s is a shell builtin\n", argv[i]);
82776ad65f7SSteve Price 			break;
82876ad65f7SSteve Price 
82976ad65f7SSteve Price 		default:
830b2f153feSStefan Farfeleder 			if (cmd != TYPECMD_SMALLV)
831f7bbf3ffSStefan Farfeleder 				outfmt(out2, "%s: not found\n", argv[i]);
832384aedabSJilles Tjoelker 			error1 |= 127;
83376ad65f7SSteve Price 			break;
83476ad65f7SSteve Price 		}
83576ad65f7SSteve Price 	}
83606a8a57fSJilles Tjoelker 
83706a8a57fSJilles Tjoelker 	if (path != pathval())
83806a8a57fSJilles Tjoelker 		clearcmdentry(0);
83906a8a57fSJilles Tjoelker 
840384aedabSJilles Tjoelker 	return error1;
84176ad65f7SSteve Price }
842b2f153feSStefan Farfeleder 
843b2f153feSStefan Farfeleder /*
844b2f153feSStefan Farfeleder  * Locate and print what a word is...
845b2f153feSStefan Farfeleder  */
846b2f153feSStefan Farfeleder 
847b2f153feSStefan Farfeleder int
848b2f153feSStefan Farfeleder typecmd(int argc, char **argv)
849b2f153feSStefan Farfeleder {
8500fb60646SJilles Tjoelker 	return typecmd_impl(argc, argv, TYPECMD_TYPE, bltinlookup("PATH", 1));
851b2f153feSStefan Farfeleder }
852