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