Home | History | Annotate | Line # | Download | only in sh
exec.c revision 1.16
      1 /*	$NetBSD: exec.c,v 1.16 1995/05/11 21:29:02 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)exec.c	8.3 (Berkeley) 5/4/95";
     42 #else
     43 static char rcsid[] = "$NetBSD: exec.c,v 1.16 1995/05/11 21:29:02 christos Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <sys/types.h>
     48 #include <sys/stat.h>
     49 #include <unistd.h>
     50 #include <fcntl.h>
     51 #include <errno.h>
     52 #include <stdlib.h>
     53 
     54 /*
     55  * When commands are first encountered, they are entered in a hash table.
     56  * This ensures that a full path search will not have to be done for them
     57  * on each invocation.
     58  *
     59  * We should investigate converting to a linear search, even though that
     60  * would make the command name "hash" a misnomer.
     61  */
     62 
     63 #include "shell.h"
     64 #include "main.h"
     65 #include "nodes.h"
     66 #include "parser.h"
     67 #include "redir.h"
     68 #include "eval.h"
     69 #include "exec.h"
     70 #include "builtins.h"
     71 #include "var.h"
     72 #include "options.h"
     73 #include "input.h"
     74 #include "output.h"
     75 #include "syntax.h"
     76 #include "memalloc.h"
     77 #include "error.h"
     78 #include "init.h"
     79 #include "mystring.h"
     80 #include "show.h"
     81 #include "jobs.h"
     82 
     83 
     84 #define CMDTABLESIZE 31		/* should be prime */
     85 #define ARB 1			/* actual size determined at run time */
     86 
     87 
     88 
     89 struct tblentry {
     90 	struct tblentry *next;	/* next entry in hash chain */
     91 	union param param;	/* definition of builtin function */
     92 	short cmdtype;		/* index identifying command */
     93 	char rehash;		/* if set, cd done since entry created */
     94 	char cmdname[ARB];	/* name of command */
     95 };
     96 
     97 
     98 STATIC struct tblentry *cmdtable[CMDTABLESIZE];
     99 STATIC int builtinloc = -1;		/* index in path of %builtin, or -1 */
    100 
    101 
    102 STATIC void tryexec __P((char *, char **, char **));
    103 STATIC void execinterp __P((char **, char **));
    104 STATIC void printentry __P((struct tblentry *, int));
    105 STATIC void clearcmdentry __P((int));
    106 STATIC struct tblentry *cmdlookup __P((char *, int));
    107 STATIC void delete_cmd_entry __P((void));
    108 
    109 
    110 
    111 /*
    112  * Exec a program.  Never returns.  If you change this routine, you may
    113  * have to change the find_command routine as well.
    114  */
    115 
    116 void
    117 shellexec(argv, envp, path, index)
    118 	char **argv, **envp;
    119 	char *path;
    120 	int index;
    121 {
    122 	char *cmdname;
    123 	int e;
    124 
    125 	if (strchr(argv[0], '/') != NULL) {
    126 		tryexec(argv[0], argv, envp);
    127 		e = errno;
    128 	} else {
    129 		e = ENOENT;
    130 		while ((cmdname = padvance(&path, argv[0])) != NULL) {
    131 			if (--index < 0 && pathopt == NULL) {
    132 				tryexec(cmdname, argv, envp);
    133 				if (errno != ENOENT && errno != ENOTDIR)
    134 					e = errno;
    135 			}
    136 			stunalloc(cmdname);
    137 		}
    138 	}
    139 	error2(argv[0], errmsg(e, E_EXEC));
    140 }
    141 
    142 
    143 STATIC void
    144 tryexec(cmd, argv, envp)
    145 	char *cmd;
    146 	char **argv;
    147 	char **envp;
    148 	{
    149 	int e;
    150 #ifndef BSD
    151 	char *p;
    152 #endif
    153 
    154 #ifdef SYSV
    155 	do {
    156 		execve(cmd, argv, envp);
    157 	} while (errno == EINTR);
    158 #else
    159 	execve(cmd, argv, envp);
    160 #endif
    161 	e = errno;
    162 	if (e == ENOEXEC) {
    163 		initshellproc();
    164 		setinputfile(cmd, 0);
    165 		commandname = arg0 = savestr(argv[0]);
    166 #ifndef BSD
    167 		pgetc(); pungetc();		/* fill up input buffer */
    168 		p = parsenextc;
    169 		if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
    170 			argv[0] = cmd;
    171 			execinterp(argv, envp);
    172 		}
    173 #endif
    174 		setparam(argv + 1);
    175 		exraise(EXSHELLPROC);
    176 		/*NOTREACHED*/
    177 	}
    178 	errno = e;
    179 }
    180 
    181 
    182 #ifndef BSD
    183 /*
    184  * Execute an interpreter introduced by "#!", for systems where this
    185  * feature has not been built into the kernel.  If the interpreter is
    186  * the shell, return (effectively ignoring the "#!").  If the execution
    187  * of the interpreter fails, exit.
    188  *
    189  * This code peeks inside the input buffer in order to avoid actually
    190  * reading any input.  It would benefit from a rewrite.
    191  */
    192 
    193 #define NEWARGS 5
    194 
    195 STATIC void
    196 execinterp(argv, envp)
    197 	char **argv, **envp;
    198 	{
    199 	int n;
    200 	char *inp;
    201 	char *outp;
    202 	char c;
    203 	char *p;
    204 	char **ap;
    205 	char *newargs[NEWARGS];
    206 	int i;
    207 	char **ap2;
    208 	char **new;
    209 
    210 	n = parsenleft - 2;
    211 	inp = parsenextc + 2;
    212 	ap = newargs;
    213 	for (;;) {
    214 		while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
    215 			inp++;
    216 		if (n < 0)
    217 			goto bad;
    218 		if ((c = *inp++) == '\n')
    219 			break;
    220 		if (ap == &newargs[NEWARGS])
    221 bad:		  error("Bad #! line");
    222 		STARTSTACKSTR(outp);
    223 		do {
    224 			STPUTC(c, outp);
    225 		} while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n');
    226 		STPUTC('\0', outp);
    227 		n++, inp--;
    228 		*ap++ = grabstackstr(outp);
    229 	}
    230 	if (ap == newargs + 1) {	/* if no args, maybe no exec is needed */
    231 		p = newargs[0];
    232 		for (;;) {
    233 			if (equal(p, "sh") || equal(p, "ash")) {
    234 				return;
    235 			}
    236 			while (*p != '/') {
    237 				if (*p == '\0')
    238 					goto break2;
    239 				p++;
    240 			}
    241 			p++;
    242 		}
    243 break2:;
    244 	}
    245 	i = (char *)ap - (char *)newargs;		/* size in bytes */
    246 	if (i == 0)
    247 		error("Bad #! line");
    248 	for (ap2 = argv ; *ap2++ != NULL ; );
    249 	new = ckmalloc(i + ((char *)ap2 - (char *)argv));
    250 	ap = newargs, ap2 = new;
    251 	while ((i -= sizeof (char **)) >= 0)
    252 		*ap2++ = *ap++;
    253 	ap = argv;
    254 	while (*ap2++ = *ap++);
    255 	shellexec(new, envp, pathval(), 0);
    256 }
    257 #endif
    258 
    259 
    260 
    261 /*
    262  * Do a path search.  The variable path (passed by reference) should be
    263  * set to the start of the path before the first call; padvance will update
    264  * this value as it proceeds.  Successive calls to padvance will return
    265  * the possible path expansions in sequence.  If an option (indicated by
    266  * a percent sign) appears in the path entry then the global variable
    267  * pathopt will be set to point to it; otherwise pathopt will be set to
    268  * NULL.
    269  */
    270 
    271 char *pathopt;
    272 
    273 char *
    274 padvance(path, name)
    275 	char **path;
    276 	char *name;
    277 	{
    278 	register char *p, *q;
    279 	char *start;
    280 	int len;
    281 
    282 	if (*path == NULL)
    283 		return NULL;
    284 	start = *path;
    285 	for (p = start ; *p && *p != ':' && *p != '%' ; p++);
    286 	len = p - start + strlen(name) + 2;	/* "2" is for '/' and '\0' */
    287 	while (stackblocksize() < len)
    288 		growstackblock();
    289 	q = stackblock();
    290 	if (p != start) {
    291 		memcpy(q, start, p - start);
    292 		q += p - start;
    293 		*q++ = '/';
    294 	}
    295 	strcpy(q, name);
    296 	pathopt = NULL;
    297 	if (*p == '%') {
    298 		pathopt = ++p;
    299 		while (*p && *p != ':')  p++;
    300 	}
    301 	if (*p == ':')
    302 		*path = p + 1;
    303 	else
    304 		*path = NULL;
    305 	return stalloc(len);
    306 }
    307 
    308 
    309 
    310 /*** Command hashing code ***/
    311 
    312 
    313 int
    314 hashcmd(argc, argv)
    315 	int argc;
    316 	char **argv;
    317 {
    318 	struct tblentry **pp;
    319 	struct tblentry *cmdp;
    320 	int c;
    321 	int verbose;
    322 	struct cmdentry entry;
    323 	char *name;
    324 
    325 	verbose = 0;
    326 	while ((c = nextopt("rv")) != '\0') {
    327 		if (c == 'r') {
    328 			clearcmdentry(0);
    329 		} else if (c == 'v') {
    330 			verbose++;
    331 		}
    332 	}
    333 	if (*argptr == NULL) {
    334 		for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
    335 			for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
    336 				printentry(cmdp, verbose);
    337 			}
    338 		}
    339 		return 0;
    340 	}
    341 	while ((name = *argptr) != NULL) {
    342 		if ((cmdp = cmdlookup(name, 0)) != NULL
    343 		 && (cmdp->cmdtype == CMDNORMAL
    344 		     || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))
    345 			delete_cmd_entry();
    346 		find_command(name, &entry, 1);
    347 		if (verbose) {
    348 			if (entry.cmdtype != CMDUNKNOWN) {	/* if no error msg */
    349 				cmdp = cmdlookup(name, 0);
    350 				printentry(cmdp, verbose);
    351 			}
    352 			flushall();
    353 		}
    354 		argptr++;
    355 	}
    356 	return 0;
    357 }
    358 
    359 
    360 STATIC void
    361 printentry(cmdp, verbose)
    362 	struct tblentry *cmdp;
    363 	int verbose;
    364 	{
    365 	int index;
    366 	char *path;
    367 	char *name;
    368 
    369 	if (cmdp->cmdtype == CMDNORMAL) {
    370 		index = cmdp->param.index;
    371 		path = pathval();
    372 		do {
    373 			name = padvance(&path, cmdp->cmdname);
    374 			stunalloc(name);
    375 		} while (--index >= 0);
    376 		out1str(name);
    377 	} else if (cmdp->cmdtype == CMDBUILTIN) {
    378 		out1fmt("builtin %s", cmdp->cmdname);
    379 	} else if (cmdp->cmdtype == CMDFUNCTION) {
    380 		out1fmt("function %s", cmdp->cmdname);
    381 		if (verbose) {
    382 			INTOFF;
    383 			name = commandtext(cmdp->param.func);
    384 			out1c(' ');
    385 			out1str(name);
    386 			ckfree(name);
    387 			INTON;
    388 		}
    389 #ifdef DEBUG
    390 	} else {
    391 		error("internal error: cmdtype %d", cmdp->cmdtype);
    392 #endif
    393 	}
    394 	if (cmdp->rehash)
    395 		out1c('*');
    396 	out1c('\n');
    397 }
    398 
    399 
    400 
    401 /*
    402  * Resolve a command name.  If you change this routine, you may have to
    403  * change the shellexec routine as well.
    404  */
    405 
    406 void
    407 find_command(name, entry, printerr)
    408 	char *name;
    409 	struct cmdentry *entry;
    410 	int printerr;
    411 {
    412 	struct tblentry *cmdp;
    413 	int index;
    414 	int prev;
    415 	char *path;
    416 	char *fullname;
    417 	struct stat statb;
    418 	int e;
    419 	int i;
    420 
    421 	/* If name contains a slash, don't use the hash table */
    422 	if (strchr(name, '/') != NULL) {
    423 		entry->cmdtype = CMDNORMAL;
    424 		entry->u.index = 0;
    425 		return;
    426 	}
    427 
    428 	/* If name is in the table, and not invalidated by cd, we're done */
    429 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0)
    430 		goto success;
    431 
    432 	/* If %builtin not in path, check for builtin next */
    433 	if (builtinloc < 0 && (i = find_builtin(name)) >= 0) {
    434 		INTOFF;
    435 		cmdp = cmdlookup(name, 1);
    436 		cmdp->cmdtype = CMDBUILTIN;
    437 		cmdp->param.index = i;
    438 		INTON;
    439 		goto success;
    440 	}
    441 
    442 	/* We have to search path. */
    443 	prev = -1;		/* where to start */
    444 	if (cmdp) {		/* doing a rehash */
    445 		if (cmdp->cmdtype == CMDBUILTIN)
    446 			prev = builtinloc;
    447 		else
    448 			prev = cmdp->param.index;
    449 	}
    450 
    451 	path = pathval();
    452 	e = ENOENT;
    453 	index = -1;
    454 loop:
    455 	while ((fullname = padvance(&path, name)) != NULL) {
    456 		stunalloc(fullname);
    457 		index++;
    458 		if (pathopt) {
    459 			if (prefix("builtin", pathopt)) {
    460 				if ((i = find_builtin(name)) < 0)
    461 					goto loop;
    462 				INTOFF;
    463 				cmdp = cmdlookup(name, 1);
    464 				cmdp->cmdtype = CMDBUILTIN;
    465 				cmdp->param.index = i;
    466 				INTON;
    467 				goto success;
    468 			} else if (prefix("func", pathopt)) {
    469 				/* handled below */
    470 			} else {
    471 				goto loop;	/* ignore unimplemented options */
    472 			}
    473 		}
    474 		/* if rehash, don't redo absolute path names */
    475 		if (fullname[0] == '/' && index <= prev) {
    476 			if (index < prev)
    477 				goto loop;
    478 			TRACE(("searchexec \"%s\": no change\n", name));
    479 			goto success;
    480 		}
    481 		while (stat(fullname, &statb) < 0) {
    482 #ifdef SYSV
    483 			if (errno == EINTR)
    484 				continue;
    485 #endif
    486 			if (errno != ENOENT && errno != ENOTDIR)
    487 				e = errno;
    488 			goto loop;
    489 		}
    490 		e = EACCES;	/* if we fail, this will be the error */
    491 		if (!S_ISREG(statb.st_mode))
    492 			goto loop;
    493 		if (pathopt) {		/* this is a %func directory */
    494 			stalloc(strlen(fullname) + 1);
    495 			readcmdfile(fullname);
    496 			if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
    497 				error("%s not defined in %s", name, fullname);
    498 			stunalloc(fullname);
    499 			goto success;
    500 		}
    501 #ifdef notdef
    502 		if (statb.st_uid == geteuid()) {
    503 			if ((statb.st_mode & 0100) == 0)
    504 				goto loop;
    505 		} else if (statb.st_gid == getegid()) {
    506 			if ((statb.st_mode & 010) == 0)
    507 				goto loop;
    508 		} else {
    509 			if ((statb.st_mode & 01) == 0)
    510 				goto loop;
    511 		}
    512 #endif
    513 		TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
    514 		INTOFF;
    515 		cmdp = cmdlookup(name, 1);
    516 		cmdp->cmdtype = CMDNORMAL;
    517 		cmdp->param.index = index;
    518 		INTON;
    519 		goto success;
    520 	}
    521 
    522 	/* We failed.  If there was an entry for this command, delete it */
    523 	if (cmdp)
    524 		delete_cmd_entry();
    525 	if (printerr)
    526 		outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC));
    527 	entry->cmdtype = CMDUNKNOWN;
    528 	return;
    529 
    530 success:
    531 	cmdp->rehash = 0;
    532 	entry->cmdtype = cmdp->cmdtype;
    533 	entry->u = cmdp->param;
    534 }
    535 
    536 
    537 
    538 /*
    539  * Search the table of builtin commands.
    540  */
    541 
    542 int
    543 find_builtin(name)
    544 	char *name;
    545 {
    546 	register const struct builtincmd *bp;
    547 
    548 	for (bp = builtincmd ; bp->name ; bp++) {
    549 		if (*bp->name == *name && equal(bp->name, name))
    550 			return bp->code;
    551 	}
    552 	return -1;
    553 }
    554 
    555 
    556 
    557 /*
    558  * Called when a cd is done.  Marks all commands so the next time they
    559  * are executed they will be rehashed.
    560  */
    561 
    562 void
    563 hashcd() {
    564 	struct tblentry **pp;
    565 	struct tblentry *cmdp;
    566 
    567 	for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
    568 		for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
    569 			if (cmdp->cmdtype == CMDNORMAL
    570 			 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
    571 				cmdp->rehash = 1;
    572 		}
    573 	}
    574 }
    575 
    576 
    577 
    578 /*
    579  * Called before PATH is changed.  The argument is the new value of PATH;
    580  * pathval() still returns the old value at this point.  Called with
    581  * interrupts off.
    582  */
    583 
    584 void
    585 changepath(newval)
    586 	char *newval;
    587 {
    588 	char *old, *new;
    589 	int index;
    590 	int firstchange;
    591 	int bltin;
    592 
    593 	old = pathval();
    594 	new = newval;
    595 	firstchange = 9999;	/* assume no change */
    596 	index = 0;
    597 	bltin = -1;
    598 	for (;;) {
    599 		if (*old != *new) {
    600 			firstchange = index;
    601 			if ((*old == '\0' && *new == ':')
    602 			 || (*old == ':' && *new == '\0'))
    603 				firstchange++;
    604 			old = new;	/* ignore subsequent differences */
    605 		}
    606 		if (*new == '\0')
    607 			break;
    608 		if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
    609 			bltin = index;
    610 		if (*new == ':') {
    611 			index++;
    612 		}
    613 		new++, old++;
    614 	}
    615 	if (builtinloc < 0 && bltin >= 0)
    616 		builtinloc = bltin;		/* zap builtins */
    617 	if (builtinloc >= 0 && bltin < 0)
    618 		firstchange = 0;
    619 	clearcmdentry(firstchange);
    620 	builtinloc = bltin;
    621 }
    622 
    623 
    624 /*
    625  * Clear out command entries.  The argument specifies the first entry in
    626  * PATH which has changed.
    627  */
    628 
    629 STATIC void
    630 clearcmdentry(firstchange)
    631 	int firstchange;
    632 {
    633 	struct tblentry **tblp;
    634 	struct tblentry **pp;
    635 	struct tblentry *cmdp;
    636 
    637 	INTOFF;
    638 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
    639 		pp = tblp;
    640 		while ((cmdp = *pp) != NULL) {
    641 			if ((cmdp->cmdtype == CMDNORMAL &&
    642 			     cmdp->param.index >= firstchange)
    643 			 || (cmdp->cmdtype == CMDBUILTIN &&
    644 			     builtinloc >= firstchange)) {
    645 				*pp = cmdp->next;
    646 				ckfree(cmdp);
    647 			} else {
    648 				pp = &cmdp->next;
    649 			}
    650 		}
    651 	}
    652 	INTON;
    653 }
    654 
    655 
    656 /*
    657  * Delete all functions.
    658  */
    659 
    660 #ifdef mkinit
    661 MKINIT void deletefuncs();
    662 
    663 SHELLPROC {
    664 	deletefuncs();
    665 }
    666 #endif
    667 
    668 void
    669 deletefuncs() {
    670 	struct tblentry **tblp;
    671 	struct tblentry **pp;
    672 	struct tblentry *cmdp;
    673 
    674 	INTOFF;
    675 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
    676 		pp = tblp;
    677 		while ((cmdp = *pp) != NULL) {
    678 			if (cmdp->cmdtype == CMDFUNCTION) {
    679 				*pp = cmdp->next;
    680 				freefunc(cmdp->param.func);
    681 				ckfree(cmdp);
    682 			} else {
    683 				pp = &cmdp->next;
    684 			}
    685 		}
    686 	}
    687 	INTON;
    688 }
    689 
    690 
    691 
    692 /*
    693  * Locate a command in the command hash table.  If "add" is nonzero,
    694  * add the command to the table if it is not already present.  The
    695  * variable "lastcmdentry" is set to point to the address of the link
    696  * pointing to the entry, so that delete_cmd_entry can delete the
    697  * entry.
    698  */
    699 
    700 struct tblentry **lastcmdentry;
    701 
    702 
    703 STATIC struct tblentry *
    704 cmdlookup(name, add)
    705 	char *name;
    706 	int add;
    707 {
    708 	int hashval;
    709 	register char *p;
    710 	struct tblentry *cmdp;
    711 	struct tblentry **pp;
    712 
    713 	p = name;
    714 	hashval = *p << 4;
    715 	while (*p)
    716 		hashval += *p++;
    717 	hashval &= 0x7FFF;
    718 	pp = &cmdtable[hashval % CMDTABLESIZE];
    719 	for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
    720 		if (equal(cmdp->cmdname, name))
    721 			break;
    722 		pp = &cmdp->next;
    723 	}
    724 	if (add && cmdp == NULL) {
    725 		INTOFF;
    726 		cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
    727 					+ strlen(name) + 1);
    728 		cmdp->next = NULL;
    729 		cmdp->cmdtype = CMDUNKNOWN;
    730 		cmdp->rehash = 0;
    731 		strcpy(cmdp->cmdname, name);
    732 		INTON;
    733 	}
    734 	lastcmdentry = pp;
    735 	return cmdp;
    736 }
    737 
    738 /*
    739  * Delete the command entry returned on the last lookup.
    740  */
    741 
    742 STATIC void
    743 delete_cmd_entry() {
    744 	struct tblentry *cmdp;
    745 
    746 	INTOFF;
    747 	cmdp = *lastcmdentry;
    748 	*lastcmdentry = cmdp->next;
    749 	ckfree(cmdp);
    750 	INTON;
    751 }
    752 
    753 
    754 
    755 #ifdef notdef
    756 void
    757 getcmdentry(name, entry)
    758 	char *name;
    759 	struct cmdentry *entry;
    760 	{
    761 	struct tblentry *cmdp = cmdlookup(name, 0);
    762 
    763 	if (cmdp) {
    764 		entry->u = cmdp->param;
    765 		entry->cmdtype = cmdp->cmdtype;
    766 	} else {
    767 		entry->cmdtype = CMDUNKNOWN;
    768 		entry->u.index = 0;
    769 	}
    770 }
    771 #endif
    772 
    773 
    774 /*
    775  * Add a new command entry, replacing any existing command entry for
    776  * the same name.
    777  */
    778 
    779 void
    780 addcmdentry(name, entry)
    781 	char *name;
    782 	struct cmdentry *entry;
    783 	{
    784 	struct tblentry *cmdp;
    785 
    786 	INTOFF;
    787 	cmdp = cmdlookup(name, 1);
    788 	if (cmdp->cmdtype == CMDFUNCTION) {
    789 		freefunc(cmdp->param.func);
    790 	}
    791 	cmdp->cmdtype = entry->cmdtype;
    792 	cmdp->param = entry->u;
    793 	INTON;
    794 }
    795 
    796 
    797 /*
    798  * Define a shell function.
    799  */
    800 
    801 void
    802 defun(name, func)
    803 	char *name;
    804 	union node *func;
    805 	{
    806 	struct cmdentry entry;
    807 
    808 	INTOFF;
    809 	entry.cmdtype = CMDFUNCTION;
    810 	entry.u.func = copyfunc(func);
    811 	addcmdentry(name, &entry);
    812 	INTON;
    813 }
    814 
    815 
    816 /*
    817  * Delete a function if it exists.
    818  */
    819 
    820 int
    821 unsetfunc(name)
    822 	char *name;
    823 	{
    824 	struct tblentry *cmdp;
    825 
    826 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
    827 		freefunc(cmdp->param.func);
    828 		delete_cmd_entry();
    829 		return (0);
    830 	}
    831 	return (1);
    832 }
    833