147114Sbostic /*-
260717Sbostic * Copyright (c) 1991, 1993
360717Sbostic * The Regents of the University of California. All rights reserved.
447114Sbostic *
547114Sbostic * This code is derived from software contributed to Berkeley by
647114Sbostic * Kenneth Almquist.
747114Sbostic *
860717Sbostic * %sccs.include.redist.c%
947114Sbostic */
1047114Sbostic
1147114Sbostic #ifndef lint
12*69808Schristos static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 06/08/95";
1347114Sbostic #endif /* not lint */
1447114Sbostic
1569272Schristos #include <sys/types.h>
1669272Schristos #include <sys/stat.h>
1769272Schristos #include <unistd.h>
1869272Schristos #include <fcntl.h>
1969272Schristos #include <errno.h>
2069272Schristos #include <stdlib.h>
2169272Schristos
2247114Sbostic /*
2347114Sbostic * When commands are first encountered, they are entered in a hash table.
2447114Sbostic * This ensures that a full path search will not have to be done for them
2547114Sbostic * on each invocation.
2647114Sbostic *
2747114Sbostic * We should investigate converting to a linear search, even though that
2847114Sbostic * would make the command name "hash" a misnomer.
2947114Sbostic */
3047114Sbostic
3147114Sbostic #include "shell.h"
3247114Sbostic #include "main.h"
3347114Sbostic #include "nodes.h"
3447114Sbostic #include "parser.h"
3547114Sbostic #include "redir.h"
3647114Sbostic #include "eval.h"
3747114Sbostic #include "exec.h"
3847114Sbostic #include "builtins.h"
3947114Sbostic #include "var.h"
4047114Sbostic #include "options.h"
4147114Sbostic #include "input.h"
4247114Sbostic #include "output.h"
4347114Sbostic #include "syntax.h"
4447114Sbostic #include "memalloc.h"
4547114Sbostic #include "error.h"
4647114Sbostic #include "init.h"
4747114Sbostic #include "mystring.h"
4869272Schristos #include "show.h"
4955302Smarc #include "jobs.h"
5047114Sbostic
5147114Sbostic
5247114Sbostic #define CMDTABLESIZE 31 /* should be prime */
5347114Sbostic #define ARB 1 /* actual size determined at run time */
5447114Sbostic
5547114Sbostic
5647114Sbostic
5747114Sbostic struct tblentry {
5847114Sbostic struct tblentry *next; /* next entry in hash chain */
5947114Sbostic union param param; /* definition of builtin function */
6047114Sbostic short cmdtype; /* index identifying command */
6147114Sbostic char rehash; /* if set, cd done since entry created */
6247114Sbostic char cmdname[ARB]; /* name of command */
6347114Sbostic };
6447114Sbostic
6547114Sbostic
6647114Sbostic STATIC struct tblentry *cmdtable[CMDTABLESIZE];
6747293Smarc STATIC int builtinloc = -1; /* index in path of %builtin, or -1 */
6847114Sbostic
6947114Sbostic
7069272Schristos STATIC void tryexec __P((char *, char **, char **));
7169272Schristos STATIC void execinterp __P((char **, char **));
7269272Schristos STATIC void printentry __P((struct tblentry *, int));
7369272Schristos STATIC void clearcmdentry __P((int));
7469272Schristos STATIC struct tblentry *cmdlookup __P((char *, int));
7569272Schristos STATIC void delete_cmd_entry __P((void));
7647114Sbostic
7747114Sbostic
7847114Sbostic
7947114Sbostic /*
8047114Sbostic * Exec a program. Never returns. If you change this routine, you may
8147114Sbostic * have to change the find_command routine as well.
8247114Sbostic */
8347114Sbostic
8447114Sbostic void
shellexec(argv,envp,path,index)8547114Sbostic shellexec(argv, envp, path, index)
8647114Sbostic char **argv, **envp;
8747114Sbostic char *path;
8869272Schristos int index;
8969272Schristos {
9047114Sbostic char *cmdname;
9147114Sbostic int e;
9247114Sbostic
9347114Sbostic if (strchr(argv[0], '/') != NULL) {
9447114Sbostic tryexec(argv[0], argv, envp);
9547114Sbostic e = errno;
9647114Sbostic } else {
9747114Sbostic e = ENOENT;
9847114Sbostic while ((cmdname = padvance(&path, argv[0])) != NULL) {
9947114Sbostic if (--index < 0 && pathopt == NULL) {
10047114Sbostic tryexec(cmdname, argv, envp);
10147114Sbostic if (errno != ENOENT && errno != ENOTDIR)
10247114Sbostic e = errno;
10347114Sbostic }
10447114Sbostic stunalloc(cmdname);
10547114Sbostic }
10647114Sbostic }
10747114Sbostic error2(argv[0], errmsg(e, E_EXEC));
10847114Sbostic }
10947114Sbostic
11047114Sbostic
11147114Sbostic STATIC void
tryexec(cmd,argv,envp)11247114Sbostic tryexec(cmd, argv, envp)
11347114Sbostic char *cmd;
11447114Sbostic char **argv;
11547114Sbostic char **envp;
11647114Sbostic {
11747114Sbostic int e;
11869272Schristos #ifndef BSD
11947114Sbostic char *p;
12069272Schristos #endif
12147114Sbostic
12247114Sbostic #ifdef SYSV
12347114Sbostic do {
12447114Sbostic execve(cmd, argv, envp);
12547114Sbostic } while (errno == EINTR);
12647114Sbostic #else
12747114Sbostic execve(cmd, argv, envp);
12847114Sbostic #endif
12947114Sbostic e = errno;
13047114Sbostic if (e == ENOEXEC) {
13147114Sbostic initshellproc();
13247114Sbostic setinputfile(cmd, 0);
13347114Sbostic commandname = arg0 = savestr(argv[0]);
13447114Sbostic #ifndef BSD
13547114Sbostic pgetc(); pungetc(); /* fill up input buffer */
13647114Sbostic p = parsenextc;
13747114Sbostic if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
13847114Sbostic argv[0] = cmd;
13947114Sbostic execinterp(argv, envp);
14047114Sbostic }
14147114Sbostic #endif
14247114Sbostic setparam(argv + 1);
14347114Sbostic exraise(EXSHELLPROC);
14447114Sbostic /*NOTREACHED*/
14547114Sbostic }
14647114Sbostic errno = e;
14747114Sbostic }
14847114Sbostic
14947114Sbostic
15047114Sbostic #ifndef BSD
15147114Sbostic /*
15247114Sbostic * Execute an interpreter introduced by "#!", for systems where this
15347114Sbostic * feature has not been built into the kernel. If the interpreter is
15447114Sbostic * the shell, return (effectively ignoring the "#!"). If the execution
15547114Sbostic * of the interpreter fails, exit.
15647114Sbostic *
15747114Sbostic * This code peeks inside the input buffer in order to avoid actually
15847114Sbostic * reading any input. It would benefit from a rewrite.
15947114Sbostic */
16047114Sbostic
16147114Sbostic #define NEWARGS 5
16247114Sbostic
16347114Sbostic STATIC void
execinterp(argv,envp)16447114Sbostic execinterp(argv, envp)
16547114Sbostic char **argv, **envp;
16647114Sbostic {
16747114Sbostic int n;
16847114Sbostic char *inp;
16947114Sbostic char *outp;
17047114Sbostic char c;
17147114Sbostic char *p;
17247114Sbostic char **ap;
17347114Sbostic char *newargs[NEWARGS];
17447114Sbostic int i;
17547114Sbostic char **ap2;
17647114Sbostic char **new;
17747114Sbostic
17847114Sbostic n = parsenleft - 2;
17947114Sbostic inp = parsenextc + 2;
18047114Sbostic ap = newargs;
18147114Sbostic for (;;) {
18247114Sbostic while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
18347114Sbostic inp++;
18447114Sbostic if (n < 0)
18547114Sbostic goto bad;
18647114Sbostic if ((c = *inp++) == '\n')
18747114Sbostic break;
18847114Sbostic if (ap == &newargs[NEWARGS])
18947114Sbostic bad: error("Bad #! line");
19047114Sbostic STARTSTACKSTR(outp);
19147114Sbostic do {
19247114Sbostic STPUTC(c, outp);
19347114Sbostic } while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n');
19447114Sbostic STPUTC('\0', outp);
19547114Sbostic n++, inp--;
19647114Sbostic *ap++ = grabstackstr(outp);
19747114Sbostic }
19847114Sbostic if (ap == newargs + 1) { /* if no args, maybe no exec is needed */
19947114Sbostic p = newargs[0];
20047114Sbostic for (;;) {
20147114Sbostic if (equal(p, "sh") || equal(p, "ash")) {
20247114Sbostic return;
20347114Sbostic }
20447114Sbostic while (*p != '/') {
20547114Sbostic if (*p == '\0')
20647114Sbostic goto break2;
20747114Sbostic p++;
20847114Sbostic }
20947114Sbostic p++;
21047114Sbostic }
21147114Sbostic break2:;
21247114Sbostic }
21347114Sbostic i = (char *)ap - (char *)newargs; /* size in bytes */
21447114Sbostic if (i == 0)
21547114Sbostic error("Bad #! line");
21647114Sbostic for (ap2 = argv ; *ap2++ != NULL ; );
21747114Sbostic new = ckmalloc(i + ((char *)ap2 - (char *)argv));
21847114Sbostic ap = newargs, ap2 = new;
21947114Sbostic while ((i -= sizeof (char **)) >= 0)
22047114Sbostic *ap2++ = *ap++;
22147114Sbostic ap = argv;
22247114Sbostic while (*ap2++ = *ap++);
22347114Sbostic shellexec(new, envp, pathval(), 0);
22447114Sbostic }
22547114Sbostic #endif
22647114Sbostic
22747114Sbostic
22847114Sbostic
22947114Sbostic /*
23047114Sbostic * Do a path search. The variable path (passed by reference) should be
23147114Sbostic * set to the start of the path before the first call; padvance will update
23247114Sbostic * this value as it proceeds. Successive calls to padvance will return
23347114Sbostic * the possible path expansions in sequence. If an option (indicated by
23447114Sbostic * a percent sign) appears in the path entry then the global variable
23547114Sbostic * pathopt will be set to point to it; otherwise pathopt will be set to
23647114Sbostic * NULL.
23747114Sbostic */
23847114Sbostic
23947114Sbostic char *pathopt;
24047114Sbostic
24147114Sbostic char *
padvance(path,name)24247114Sbostic padvance(path, name)
24347114Sbostic char **path;
24447114Sbostic char *name;
24547114Sbostic {
24647114Sbostic register char *p, *q;
24747114Sbostic char *start;
24847114Sbostic int len;
24947114Sbostic
25047114Sbostic if (*path == NULL)
25147114Sbostic return NULL;
25247114Sbostic start = *path;
25347114Sbostic for (p = start ; *p && *p != ':' && *p != '%' ; p++);
25447114Sbostic len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */
25547114Sbostic while (stackblocksize() < len)
25647114Sbostic growstackblock();
25747114Sbostic q = stackblock();
25847114Sbostic if (p != start) {
25969272Schristos memcpy(q, start, p - start);
26047114Sbostic q += p - start;
26147114Sbostic *q++ = '/';
26247114Sbostic }
26347114Sbostic strcpy(q, name);
26447114Sbostic pathopt = NULL;
26547114Sbostic if (*p == '%') {
26647114Sbostic pathopt = ++p;
26747114Sbostic while (*p && *p != ':') p++;
26847114Sbostic }
26947114Sbostic if (*p == ':')
27047114Sbostic *path = p + 1;
27147114Sbostic else
27247114Sbostic *path = NULL;
27347114Sbostic return stalloc(len);
27447114Sbostic }
27547114Sbostic
27647114Sbostic
27747114Sbostic
27847114Sbostic /*** Command hashing code ***/
27947114Sbostic
28047114Sbostic
28169272Schristos int
hashcmd(argc,argv)28269272Schristos hashcmd(argc, argv)
28369272Schristos int argc;
28469272Schristos char **argv;
28569272Schristos {
28647114Sbostic struct tblentry **pp;
28747114Sbostic struct tblentry *cmdp;
28847114Sbostic int c;
28947114Sbostic int verbose;
29047114Sbostic struct cmdentry entry;
29147114Sbostic char *name;
29247114Sbostic
29347114Sbostic verbose = 0;
29447293Smarc while ((c = nextopt("rv")) != '\0') {
29547114Sbostic if (c == 'r') {
29647114Sbostic clearcmdentry(0);
29747114Sbostic } else if (c == 'v') {
29847114Sbostic verbose++;
29947114Sbostic }
30047114Sbostic }
30155297Smarc if (*argptr == NULL) {
30255297Smarc for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
30355297Smarc for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
30455297Smarc printentry(cmdp, verbose);
30555297Smarc }
30655297Smarc }
30755297Smarc return 0;
30855297Smarc }
30947114Sbostic while ((name = *argptr) != NULL) {
31047114Sbostic if ((cmdp = cmdlookup(name, 0)) != NULL
31147114Sbostic && (cmdp->cmdtype == CMDNORMAL
31269272Schristos || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))
31347114Sbostic delete_cmd_entry();
314*69808Schristos find_command(name, &entry, 1, pathval());
31547114Sbostic if (verbose) {
31647114Sbostic if (entry.cmdtype != CMDUNKNOWN) { /* if no error msg */
31747114Sbostic cmdp = cmdlookup(name, 0);
31855297Smarc printentry(cmdp, verbose);
31947114Sbostic }
32047114Sbostic flushall();
32147114Sbostic }
32247114Sbostic argptr++;
32347114Sbostic }
32447114Sbostic return 0;
32547114Sbostic }
32647114Sbostic
32747114Sbostic
32847114Sbostic STATIC void
printentry(cmdp,verbose)32955297Smarc printentry(cmdp, verbose)
33047114Sbostic struct tblentry *cmdp;
33155297Smarc int verbose;
33247114Sbostic {
33347114Sbostic int index;
33447114Sbostic char *path;
33547114Sbostic char *name;
33647114Sbostic
33747114Sbostic if (cmdp->cmdtype == CMDNORMAL) {
33847114Sbostic index = cmdp->param.index;
33947114Sbostic path = pathval();
34047114Sbostic do {
34147114Sbostic name = padvance(&path, cmdp->cmdname);
34247114Sbostic stunalloc(name);
34347114Sbostic } while (--index >= 0);
34447114Sbostic out1str(name);
34547114Sbostic } else if (cmdp->cmdtype == CMDBUILTIN) {
34647114Sbostic out1fmt("builtin %s", cmdp->cmdname);
34747114Sbostic } else if (cmdp->cmdtype == CMDFUNCTION) {
34847114Sbostic out1fmt("function %s", cmdp->cmdname);
34955297Smarc if (verbose) {
35055297Smarc INTOFF;
35155297Smarc name = commandtext(cmdp->param.func);
35255297Smarc out1c(' ');
35355297Smarc out1str(name);
35455297Smarc ckfree(name);
35555297Smarc INTON;
35655297Smarc }
35747114Sbostic #ifdef DEBUG
35847114Sbostic } else {
35947114Sbostic error("internal error: cmdtype %d", cmdp->cmdtype);
36047114Sbostic #endif
36147114Sbostic }
36247114Sbostic if (cmdp->rehash)
36347114Sbostic out1c('*');
36447114Sbostic out1c('\n');
36547114Sbostic }
36647114Sbostic
36747114Sbostic
36847114Sbostic
36947114Sbostic /*
37047114Sbostic * Resolve a command name. If you change this routine, you may have to
37147114Sbostic * change the shellexec routine as well.
37247114Sbostic */
37347114Sbostic
37447114Sbostic void
find_command(name,entry,printerr,path)375*69808Schristos find_command(name, entry, printerr, path)
37647114Sbostic char *name;
37747114Sbostic struct cmdentry *entry;
37869272Schristos int printerr;
379*69808Schristos char *path;
38069272Schristos {
38147114Sbostic struct tblentry *cmdp;
38247114Sbostic int index;
38347114Sbostic int prev;
38447114Sbostic char *fullname;
38547114Sbostic struct stat statb;
38647114Sbostic int e;
38747114Sbostic int i;
38847114Sbostic
38947114Sbostic /* If name contains a slash, don't use the hash table */
39047114Sbostic if (strchr(name, '/') != NULL) {
39147114Sbostic entry->cmdtype = CMDNORMAL;
39247114Sbostic entry->u.index = 0;
39347114Sbostic return;
39447114Sbostic }
39547114Sbostic
39647114Sbostic /* If name is in the table, and not invalidated by cd, we're done */
39747114Sbostic if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0)
39847114Sbostic goto success;
39947114Sbostic
40047114Sbostic /* If %builtin not in path, check for builtin next */
40147114Sbostic if (builtinloc < 0 && (i = find_builtin(name)) >= 0) {
40247114Sbostic INTOFF;
40347114Sbostic cmdp = cmdlookup(name, 1);
40447114Sbostic cmdp->cmdtype = CMDBUILTIN;
40547114Sbostic cmdp->param.index = i;
40647114Sbostic INTON;
40747114Sbostic goto success;
40847114Sbostic }
40947114Sbostic
41047114Sbostic /* We have to search path. */
41147114Sbostic prev = -1; /* where to start */
41247114Sbostic if (cmdp) { /* doing a rehash */
41347114Sbostic if (cmdp->cmdtype == CMDBUILTIN)
41447114Sbostic prev = builtinloc;
41547114Sbostic else
41647114Sbostic prev = cmdp->param.index;
41747114Sbostic }
41847114Sbostic
41947114Sbostic e = ENOENT;
42047114Sbostic index = -1;
42147114Sbostic loop:
42247114Sbostic while ((fullname = padvance(&path, name)) != NULL) {
42347114Sbostic stunalloc(fullname);
42447114Sbostic index++;
42547114Sbostic if (pathopt) {
42647114Sbostic if (prefix("builtin", pathopt)) {
42747114Sbostic if ((i = find_builtin(name)) < 0)
42847114Sbostic goto loop;
42947114Sbostic INTOFF;
43047114Sbostic cmdp = cmdlookup(name, 1);
43147114Sbostic cmdp->cmdtype = CMDBUILTIN;
43247114Sbostic cmdp->param.index = i;
43347114Sbostic INTON;
43447114Sbostic goto success;
43547114Sbostic } else if (prefix("func", pathopt)) {
43647114Sbostic /* handled below */
43747114Sbostic } else {
43847114Sbostic goto loop; /* ignore unimplemented options */
43947114Sbostic }
44047114Sbostic }
44147114Sbostic /* if rehash, don't redo absolute path names */
44247114Sbostic if (fullname[0] == '/' && index <= prev) {
44347114Sbostic if (index < prev)
44447114Sbostic goto loop;
44547114Sbostic TRACE(("searchexec \"%s\": no change\n", name));
44647114Sbostic goto success;
44747114Sbostic }
44847114Sbostic while (stat(fullname, &statb) < 0) {
44947114Sbostic #ifdef SYSV
45047114Sbostic if (errno == EINTR)
45147114Sbostic continue;
45247114Sbostic #endif
45347114Sbostic if (errno != ENOENT && errno != ENOTDIR)
45447114Sbostic e = errno;
45547114Sbostic goto loop;
45647114Sbostic }
45747114Sbostic e = EACCES; /* if we fail, this will be the error */
45869272Schristos if (!S_ISREG(statb.st_mode))
45947114Sbostic goto loop;
46047114Sbostic if (pathopt) { /* this is a %func directory */
46147114Sbostic stalloc(strlen(fullname) + 1);
46247114Sbostic readcmdfile(fullname);
46347114Sbostic if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
46447114Sbostic error("%s not defined in %s", name, fullname);
46547114Sbostic stunalloc(fullname);
46647114Sbostic goto success;
46747114Sbostic }
46855244Smarc #ifdef notdef
46947114Sbostic if (statb.st_uid == geteuid()) {
47047114Sbostic if ((statb.st_mode & 0100) == 0)
47147114Sbostic goto loop;
47247114Sbostic } else if (statb.st_gid == getegid()) {
47347114Sbostic if ((statb.st_mode & 010) == 0)
47447114Sbostic goto loop;
47547114Sbostic } else {
47647114Sbostic if ((statb.st_mode & 01) == 0)
47747114Sbostic goto loop;
47847114Sbostic }
47955244Smarc #endif
48047114Sbostic TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
48147114Sbostic INTOFF;
48247114Sbostic cmdp = cmdlookup(name, 1);
48347114Sbostic cmdp->cmdtype = CMDNORMAL;
48447114Sbostic cmdp->param.index = index;
48547114Sbostic INTON;
48647114Sbostic goto success;
48747114Sbostic }
48847114Sbostic
48947114Sbostic /* We failed. If there was an entry for this command, delete it */
49047114Sbostic if (cmdp)
49147114Sbostic delete_cmd_entry();
49247114Sbostic if (printerr)
49347114Sbostic outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC));
49447114Sbostic entry->cmdtype = CMDUNKNOWN;
49547114Sbostic return;
49647114Sbostic
49747114Sbostic success:
49847114Sbostic cmdp->rehash = 0;
49947114Sbostic entry->cmdtype = cmdp->cmdtype;
50047114Sbostic entry->u = cmdp->param;
50147114Sbostic }
50247114Sbostic
50347114Sbostic
50447114Sbostic
50547114Sbostic /*
50647114Sbostic * Search the table of builtin commands.
50747114Sbostic */
50847114Sbostic
50947114Sbostic int
find_builtin(name)51047114Sbostic find_builtin(name)
51147114Sbostic char *name;
51269272Schristos {
51369272Schristos register const struct builtincmd *bp;
51447114Sbostic
51547114Sbostic for (bp = builtincmd ; bp->name ; bp++) {
51647114Sbostic if (*bp->name == *name && equal(bp->name, name))
51747114Sbostic return bp->code;
51847114Sbostic }
51947114Sbostic return -1;
52047114Sbostic }
52147114Sbostic
52247114Sbostic
52347114Sbostic
52447114Sbostic /*
52547114Sbostic * Called when a cd is done. Marks all commands so the next time they
52647114Sbostic * are executed they will be rehashed.
52747114Sbostic */
52847114Sbostic
52947114Sbostic void
hashcd()53047114Sbostic hashcd() {
53147114Sbostic struct tblentry **pp;
53247114Sbostic struct tblentry *cmdp;
53347114Sbostic
53447114Sbostic for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
53547114Sbostic for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
53647114Sbostic if (cmdp->cmdtype == CMDNORMAL
53769272Schristos || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
53847114Sbostic cmdp->rehash = 1;
53947114Sbostic }
54047114Sbostic }
54147114Sbostic }
54247114Sbostic
54347114Sbostic
54447114Sbostic
54547114Sbostic /*
54647114Sbostic * Called before PATH is changed. The argument is the new value of PATH;
54747114Sbostic * pathval() still returns the old value at this point. Called with
54847114Sbostic * interrupts off.
54947114Sbostic */
55047114Sbostic
55147114Sbostic void
changepath(newval)55247114Sbostic changepath(newval)
55347114Sbostic char *newval;
55469272Schristos {
55547114Sbostic char *old, *new;
55647114Sbostic int index;
55747114Sbostic int firstchange;
55847114Sbostic int bltin;
55947114Sbostic
56047114Sbostic old = pathval();
56147114Sbostic new = newval;
56247114Sbostic firstchange = 9999; /* assume no change */
56369272Schristos index = 0;
56447114Sbostic bltin = -1;
56547114Sbostic for (;;) {
56647114Sbostic if (*old != *new) {
56747114Sbostic firstchange = index;
56869272Schristos if ((*old == '\0' && *new == ':')
56969272Schristos || (*old == ':' && *new == '\0'))
57047114Sbostic firstchange++;
57147114Sbostic old = new; /* ignore subsequent differences */
57247114Sbostic }
57347114Sbostic if (*new == '\0')
57447114Sbostic break;
57547114Sbostic if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
57647114Sbostic bltin = index;
57747114Sbostic if (*new == ':') {
57847114Sbostic index++;
57947114Sbostic }
58047114Sbostic new++, old++;
58147114Sbostic }
58247114Sbostic if (builtinloc < 0 && bltin >= 0)
58347114Sbostic builtinloc = bltin; /* zap builtins */
58447114Sbostic if (builtinloc >= 0 && bltin < 0)
58547114Sbostic firstchange = 0;
58647114Sbostic clearcmdentry(firstchange);
58747114Sbostic builtinloc = bltin;
58847114Sbostic }
58947114Sbostic
59047114Sbostic
59147114Sbostic /*
59247114Sbostic * Clear out command entries. The argument specifies the first entry in
59347114Sbostic * PATH which has changed.
59447114Sbostic */
59547114Sbostic
59647114Sbostic STATIC void
clearcmdentry(firstchange)59769272Schristos clearcmdentry(firstchange)
59869272Schristos int firstchange;
59969272Schristos {
60047114Sbostic struct tblentry **tblp;
60147114Sbostic struct tblentry **pp;
60247114Sbostic struct tblentry *cmdp;
60347114Sbostic
60447114Sbostic INTOFF;
60547114Sbostic for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
60647114Sbostic pp = tblp;
60747114Sbostic while ((cmdp = *pp) != NULL) {
60869272Schristos if ((cmdp->cmdtype == CMDNORMAL &&
60969272Schristos cmdp->param.index >= firstchange)
61069272Schristos || (cmdp->cmdtype == CMDBUILTIN &&
61169272Schristos builtinloc >= firstchange)) {
61247114Sbostic *pp = cmdp->next;
61347114Sbostic ckfree(cmdp);
61447114Sbostic } else {
61547114Sbostic pp = &cmdp->next;
61647114Sbostic }
61747114Sbostic }
61847114Sbostic }
61947114Sbostic INTON;
62047114Sbostic }
62147114Sbostic
62247114Sbostic
62347114Sbostic /*
62447114Sbostic * Delete all functions.
62547114Sbostic */
62647114Sbostic
62747114Sbostic #ifdef mkinit
62847114Sbostic MKINIT void deletefuncs();
62947114Sbostic
63047114Sbostic SHELLPROC {
63147114Sbostic deletefuncs();
63247114Sbostic }
63347114Sbostic #endif
63447114Sbostic
63547114Sbostic void
deletefuncs()63647114Sbostic deletefuncs() {
63747114Sbostic struct tblentry **tblp;
63847114Sbostic struct tblentry **pp;
63947114Sbostic struct tblentry *cmdp;
64047114Sbostic
64147114Sbostic INTOFF;
64247114Sbostic for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
64347114Sbostic pp = tblp;
64447114Sbostic while ((cmdp = *pp) != NULL) {
64547114Sbostic if (cmdp->cmdtype == CMDFUNCTION) {
64647114Sbostic *pp = cmdp->next;
64747114Sbostic freefunc(cmdp->param.func);
64847114Sbostic ckfree(cmdp);
64947114Sbostic } else {
65047114Sbostic pp = &cmdp->next;
65147114Sbostic }
65247114Sbostic }
65347114Sbostic }
65447114Sbostic INTON;
65547114Sbostic }
65647114Sbostic
65747114Sbostic
65847114Sbostic
65947114Sbostic /*
66047114Sbostic * Locate a command in the command hash table. If "add" is nonzero,
66147114Sbostic * add the command to the table if it is not already present. The
66247114Sbostic * variable "lastcmdentry" is set to point to the address of the link
66347114Sbostic * pointing to the entry, so that delete_cmd_entry can delete the
66447114Sbostic * entry.
66547114Sbostic */
66647114Sbostic
66747114Sbostic struct tblentry **lastcmdentry;
66847114Sbostic
66947114Sbostic
67047114Sbostic STATIC struct tblentry *
cmdlookup(name,add)67147114Sbostic cmdlookup(name, add)
67247114Sbostic char *name;
67369272Schristos int add;
67469272Schristos {
67547114Sbostic int hashval;
67647114Sbostic register char *p;
67747114Sbostic struct tblentry *cmdp;
67847114Sbostic struct tblentry **pp;
67947114Sbostic
68047114Sbostic p = name;
68147114Sbostic hashval = *p << 4;
68247114Sbostic while (*p)
68347114Sbostic hashval += *p++;
68447114Sbostic hashval &= 0x7FFF;
68547114Sbostic pp = &cmdtable[hashval % CMDTABLESIZE];
68647114Sbostic for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
68747114Sbostic if (equal(cmdp->cmdname, name))
68847114Sbostic break;
68947114Sbostic pp = &cmdp->next;
69047114Sbostic }
69147114Sbostic if (add && cmdp == NULL) {
69247114Sbostic INTOFF;
69347114Sbostic cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
69447114Sbostic + strlen(name) + 1);
69547114Sbostic cmdp->next = NULL;
69647114Sbostic cmdp->cmdtype = CMDUNKNOWN;
69747114Sbostic cmdp->rehash = 0;
69847114Sbostic strcpy(cmdp->cmdname, name);
69947114Sbostic INTON;
70047114Sbostic }
70147114Sbostic lastcmdentry = pp;
70247114Sbostic return cmdp;
70347114Sbostic }
70447114Sbostic
70547114Sbostic /*
70647114Sbostic * Delete the command entry returned on the last lookup.
70747114Sbostic */
70847114Sbostic
70947114Sbostic STATIC void
delete_cmd_entry()71047114Sbostic delete_cmd_entry() {
71147114Sbostic struct tblentry *cmdp;
71247114Sbostic
71347114Sbostic INTOFF;
71447114Sbostic cmdp = *lastcmdentry;
71547114Sbostic *lastcmdentry = cmdp->next;
71647114Sbostic ckfree(cmdp);
71747114Sbostic INTON;
71847114Sbostic }
71947114Sbostic
72047114Sbostic
72147114Sbostic
72247114Sbostic #ifdef notdef
72347114Sbostic void
getcmdentry(name,entry)72447114Sbostic getcmdentry(name, entry)
72547114Sbostic char *name;
72647114Sbostic struct cmdentry *entry;
72747114Sbostic {
72847114Sbostic struct tblentry *cmdp = cmdlookup(name, 0);
72947114Sbostic
73047114Sbostic if (cmdp) {
73147114Sbostic entry->u = cmdp->param;
73247114Sbostic entry->cmdtype = cmdp->cmdtype;
73347114Sbostic } else {
73447114Sbostic entry->cmdtype = CMDUNKNOWN;
73547114Sbostic entry->u.index = 0;
73647114Sbostic }
73747114Sbostic }
73847114Sbostic #endif
73947114Sbostic
74047114Sbostic
74147114Sbostic /*
74247114Sbostic * Add a new command entry, replacing any existing command entry for
74347114Sbostic * the same name.
74447114Sbostic */
74547114Sbostic
74647114Sbostic void
addcmdentry(name,entry)74747114Sbostic addcmdentry(name, entry)
74847114Sbostic char *name;
74947114Sbostic struct cmdentry *entry;
75047114Sbostic {
75147114Sbostic struct tblentry *cmdp;
75247114Sbostic
75347114Sbostic INTOFF;
75447114Sbostic cmdp = cmdlookup(name, 1);
75547114Sbostic if (cmdp->cmdtype == CMDFUNCTION) {
75647114Sbostic freefunc(cmdp->param.func);
75747114Sbostic }
75847114Sbostic cmdp->cmdtype = entry->cmdtype;
75947114Sbostic cmdp->param = entry->u;
76047114Sbostic INTON;
76147114Sbostic }
76247114Sbostic
76347114Sbostic
76447114Sbostic /*
76547114Sbostic * Define a shell function.
76647114Sbostic */
76747114Sbostic
76847114Sbostic void
defun(name,func)76947114Sbostic defun(name, func)
77047114Sbostic char *name;
77147114Sbostic union node *func;
77247114Sbostic {
77347114Sbostic struct cmdentry entry;
77447114Sbostic
77547114Sbostic INTOFF;
77647114Sbostic entry.cmdtype = CMDFUNCTION;
77747114Sbostic entry.u.func = copyfunc(func);
77847114Sbostic addcmdentry(name, &entry);
77947114Sbostic INTON;
78047114Sbostic }
78147114Sbostic
78247114Sbostic
78347114Sbostic /*
78447114Sbostic * Delete a function if it exists.
78547114Sbostic */
78647114Sbostic
78754328Smarc int
unsetfunc(name)78847114Sbostic unsetfunc(name)
78947114Sbostic char *name;
79047114Sbostic {
79147114Sbostic struct tblentry *cmdp;
79247114Sbostic
79347114Sbostic if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
79447114Sbostic freefunc(cmdp->param.func);
79547114Sbostic delete_cmd_entry();
79654328Smarc return (0);
79747114Sbostic }
79854328Smarc return (1);
79947114Sbostic }
800