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