14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 174b88c807SRodney W. Grimes * must display the following acknowledgement: 184b88c807SRodney W. Grimes * This product includes software developed by the University of 194b88c807SRodney W. Grimes * California, Berkeley and its contributors. 204b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 214b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 224b88c807SRodney W. Grimes * without specific prior written permission. 234b88c807SRodney W. Grimes * 244b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 254b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 264b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 274b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 284b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 294b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 304b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 314b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 324b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 334b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 344b88c807SRodney W. Grimes * SUCH DAMAGE. 3589730b29SDavid Greenman * 3676ad65f7SSteve Price * $Id: exec.c,v 1.9 1997/02/22 13:58:25 peter Exp $ 374b88c807SRodney W. Grimes */ 384b88c807SRodney W. Grimes 394b88c807SRodney W. Grimes #ifndef lint 40ab0a2172SSteve Price static char const sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95"; 414b88c807SRodney W. Grimes #endif /* not lint */ 424b88c807SRodney W. Grimes 43aa9caaf6SPeter Wemm #include <sys/types.h> 44aa9caaf6SPeter Wemm #include <sys/stat.h> 45aa9caaf6SPeter Wemm #include <unistd.h> 46aa9caaf6SPeter Wemm #include <fcntl.h> 47aa9caaf6SPeter Wemm #include <errno.h> 48aa9caaf6SPeter Wemm #include <stdlib.h> 49aa9caaf6SPeter Wemm 504b88c807SRodney W. Grimes /* 514b88c807SRodney W. Grimes * When commands are first encountered, they are entered in a hash table. 524b88c807SRodney W. Grimes * This ensures that a full path search will not have to be done for them 534b88c807SRodney W. Grimes * on each invocation. 544b88c807SRodney W. Grimes * 554b88c807SRodney W. Grimes * We should investigate converting to a linear search, even though that 564b88c807SRodney W. Grimes * would make the command name "hash" a misnomer. 574b88c807SRodney W. Grimes */ 584b88c807SRodney W. Grimes 594b88c807SRodney W. Grimes #include "shell.h" 604b88c807SRodney W. Grimes #include "main.h" 614b88c807SRodney W. Grimes #include "nodes.h" 624b88c807SRodney W. Grimes #include "parser.h" 634b88c807SRodney W. Grimes #include "redir.h" 644b88c807SRodney W. Grimes #include "eval.h" 654b88c807SRodney W. Grimes #include "exec.h" 664b88c807SRodney W. Grimes #include "builtins.h" 674b88c807SRodney W. Grimes #include "var.h" 684b88c807SRodney W. Grimes #include "options.h" 694b88c807SRodney W. Grimes #include "input.h" 704b88c807SRodney W. Grimes #include "output.h" 714b88c807SRodney W. Grimes #include "syntax.h" 724b88c807SRodney W. Grimes #include "memalloc.h" 734b88c807SRodney W. Grimes #include "error.h" 744b88c807SRodney W. Grimes #include "init.h" 754b88c807SRodney W. Grimes #include "mystring.h" 76aa9caaf6SPeter Wemm #include "show.h" 774b88c807SRodney W. Grimes #include "jobs.h" 7876ad65f7SSteve Price #include "alias.h" 794b88c807SRodney W. Grimes 804b88c807SRodney W. Grimes 814b88c807SRodney W. Grimes #define CMDTABLESIZE 31 /* should be prime */ 824b88c807SRodney W. Grimes #define ARB 1 /* actual size determined at run time */ 834b88c807SRodney W. Grimes 844b88c807SRodney W. Grimes 854b88c807SRodney W. Grimes 864b88c807SRodney W. Grimes struct tblentry { 874b88c807SRodney W. Grimes struct tblentry *next; /* next entry in hash chain */ 884b88c807SRodney W. Grimes union param param; /* definition of builtin function */ 894b88c807SRodney W. Grimes short cmdtype; /* index identifying command */ 904b88c807SRodney W. Grimes char rehash; /* if set, cd done since entry created */ 914b88c807SRodney W. Grimes char cmdname[ARB]; /* name of command */ 924b88c807SRodney W. Grimes }; 934b88c807SRodney W. Grimes 944b88c807SRodney W. Grimes 954b88c807SRodney W. Grimes STATIC struct tblentry *cmdtable[CMDTABLESIZE]; 964b88c807SRodney W. Grimes STATIC int builtinloc = -1; /* index in path of %builtin, or -1 */ 97ab0a2172SSteve Price int exerrno = 0; /* Last exec error */ 984b88c807SRodney W. Grimes 994b88c807SRodney W. Grimes 100aa9caaf6SPeter Wemm STATIC void tryexec __P((char *, char **, char **)); 101ab0a2172SSteve Price #ifndef BSD 102aa9caaf6SPeter Wemm STATIC void execinterp __P((char **, char **)); 103ab0a2172SSteve Price #endif 104aa9caaf6SPeter Wemm STATIC void printentry __P((struct tblentry *, int)); 105aa9caaf6SPeter Wemm STATIC void clearcmdentry __P((int)); 106aa9caaf6SPeter Wemm STATIC struct tblentry *cmdlookup __P((char *, int)); 107aa9caaf6SPeter Wemm STATIC void delete_cmd_entry __P((void)); 1084b88c807SRodney W. Grimes 1094b88c807SRodney W. Grimes 1104b88c807SRodney W. Grimes 1114b88c807SRodney W. Grimes /* 1124b88c807SRodney W. Grimes * Exec a program. Never returns. If you change this routine, you may 1134b88c807SRodney W. Grimes * have to change the find_command routine as well. 1144b88c807SRodney W. Grimes */ 1154b88c807SRodney W. Grimes 1164b88c807SRodney W. Grimes void 1174b88c807SRodney W. Grimes shellexec(argv, envp, path, index) 1184b88c807SRodney W. Grimes char **argv, **envp; 1194b88c807SRodney W. Grimes char *path; 120aa9caaf6SPeter Wemm int index; 1214b88c807SRodney W. Grimes { 1224b88c807SRodney W. Grimes char *cmdname; 1234b88c807SRodney W. Grimes int e; 1244b88c807SRodney W. Grimes 1254b88c807SRodney W. Grimes if (strchr(argv[0], '/') != NULL) { 1264b88c807SRodney W. Grimes tryexec(argv[0], argv, envp); 1274b88c807SRodney W. Grimes e = errno; 1284b88c807SRodney W. Grimes } else { 1294b88c807SRodney W. Grimes e = ENOENT; 1304b88c807SRodney W. Grimes while ((cmdname = padvance(&path, argv[0])) != NULL) { 1314b88c807SRodney W. Grimes if (--index < 0 && pathopt == NULL) { 1324b88c807SRodney W. Grimes tryexec(cmdname, argv, envp); 1334b88c807SRodney W. Grimes if (errno != ENOENT && errno != ENOTDIR) 1344b88c807SRodney W. Grimes e = errno; 1354b88c807SRodney W. Grimes } 1364b88c807SRodney W. Grimes stunalloc(cmdname); 1374b88c807SRodney W. Grimes } 1384b88c807SRodney W. Grimes } 139ab0a2172SSteve Price 140ab0a2172SSteve Price /* Map to POSIX errors */ 141ab0a2172SSteve Price switch (e) { 142ab0a2172SSteve Price case EACCES: 143ab0a2172SSteve Price exerrno = 126; 144ab0a2172SSteve Price break; 145ab0a2172SSteve Price case ENOENT: 146ab0a2172SSteve Price exerrno = 127; 147ab0a2172SSteve Price break; 148ab0a2172SSteve Price default: 149ab0a2172SSteve Price exerrno = 2; 150ab0a2172SSteve Price break; 151ab0a2172SSteve Price } 152ab0a2172SSteve Price exerror(EXEXEC, "%s: %s", argv[0], errmsg(e, E_EXEC)); 1534b88c807SRodney W. Grimes } 1544b88c807SRodney W. Grimes 1554b88c807SRodney W. Grimes 1564b88c807SRodney W. Grimes STATIC void 1574b88c807SRodney W. Grimes tryexec(cmd, argv, envp) 1584b88c807SRodney W. Grimes char *cmd; 1594b88c807SRodney W. Grimes char **argv; 1604b88c807SRodney W. Grimes char **envp; 1614b88c807SRodney W. Grimes { 1624b88c807SRodney W. Grimes int e; 163aa9caaf6SPeter Wemm #ifndef BSD 1644b88c807SRodney W. Grimes char *p; 165aa9caaf6SPeter Wemm #endif 1664b88c807SRodney W. Grimes 1674b88c807SRodney W. Grimes #ifdef SYSV 1684b88c807SRodney W. Grimes do { 1694b88c807SRodney W. Grimes execve(cmd, argv, envp); 1704b88c807SRodney W. Grimes } while (errno == EINTR); 1714b88c807SRodney W. Grimes #else 1724b88c807SRodney W. Grimes execve(cmd, argv, envp); 1734b88c807SRodney W. Grimes #endif 1744b88c807SRodney W. Grimes e = errno; 1754b88c807SRodney W. Grimes if (e == ENOEXEC) { 1764b88c807SRodney W. Grimes initshellproc(); 1774b88c807SRodney W. Grimes setinputfile(cmd, 0); 1784b88c807SRodney W. Grimes commandname = arg0 = savestr(argv[0]); 1794b88c807SRodney W. Grimes #ifndef BSD 1804b88c807SRodney W. Grimes pgetc(); pungetc(); /* fill up input buffer */ 1814b88c807SRodney W. Grimes p = parsenextc; 1824b88c807SRodney W. Grimes if (parsenleft > 2 && p[0] == '#' && p[1] == '!') { 1834b88c807SRodney W. Grimes argv[0] = cmd; 1844b88c807SRodney W. Grimes execinterp(argv, envp); 1854b88c807SRodney W. Grimes } 1864b88c807SRodney W. Grimes #endif 1874b88c807SRodney W. Grimes setparam(argv + 1); 1884b88c807SRodney W. Grimes exraise(EXSHELLPROC); 1894b88c807SRodney W. Grimes /*NOTREACHED*/ 1904b88c807SRodney W. Grimes } 1914b88c807SRodney W. Grimes errno = e; 1924b88c807SRodney W. Grimes } 1934b88c807SRodney W. Grimes 1944b88c807SRodney W. Grimes 1954b88c807SRodney W. Grimes #ifndef BSD 1964b88c807SRodney W. Grimes /* 1974b88c807SRodney W. Grimes * Execute an interpreter introduced by "#!", for systems where this 1984b88c807SRodney W. Grimes * feature has not been built into the kernel. If the interpreter is 1994b88c807SRodney W. Grimes * the shell, return (effectively ignoring the "#!"). If the execution 2004b88c807SRodney W. Grimes * of the interpreter fails, exit. 2014b88c807SRodney W. Grimes * 2024b88c807SRodney W. Grimes * This code peeks inside the input buffer in order to avoid actually 2034b88c807SRodney W. Grimes * reading any input. It would benefit from a rewrite. 2044b88c807SRodney W. Grimes */ 2054b88c807SRodney W. Grimes 2064b88c807SRodney W. Grimes #define NEWARGS 5 2074b88c807SRodney W. Grimes 2084b88c807SRodney W. Grimes STATIC void 2094b88c807SRodney W. Grimes execinterp(argv, envp) 2104b88c807SRodney W. Grimes char **argv, **envp; 2114b88c807SRodney W. Grimes { 2124b88c807SRodney W. Grimes int n; 2134b88c807SRodney W. Grimes char *inp; 2144b88c807SRodney W. Grimes char *outp; 2154b88c807SRodney W. Grimes char c; 2164b88c807SRodney W. Grimes char *p; 2174b88c807SRodney W. Grimes char **ap; 2184b88c807SRodney W. Grimes char *newargs[NEWARGS]; 2194b88c807SRodney W. Grimes int i; 2204b88c807SRodney W. Grimes char **ap2; 2214b88c807SRodney W. Grimes char **new; 2224b88c807SRodney W. Grimes 2234b88c807SRodney W. Grimes n = parsenleft - 2; 2244b88c807SRodney W. Grimes inp = parsenextc + 2; 2254b88c807SRodney W. Grimes ap = newargs; 2264b88c807SRodney W. Grimes for (;;) { 2274b88c807SRodney W. Grimes while (--n >= 0 && (*inp == ' ' || *inp == '\t')) 2284b88c807SRodney W. Grimes inp++; 2294b88c807SRodney W. Grimes if (n < 0) 2304b88c807SRodney W. Grimes goto bad; 2314b88c807SRodney W. Grimes if ((c = *inp++) == '\n') 2324b88c807SRodney W. Grimes break; 2334b88c807SRodney W. Grimes if (ap == &newargs[NEWARGS]) 2344b88c807SRodney W. Grimes bad: error("Bad #! line"); 2354b88c807SRodney W. Grimes STARTSTACKSTR(outp); 2364b88c807SRodney W. Grimes do { 2374b88c807SRodney W. Grimes STPUTC(c, outp); 2384b88c807SRodney W. Grimes } while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n'); 2394b88c807SRodney W. Grimes STPUTC('\0', outp); 2404b88c807SRodney W. Grimes n++, inp--; 2414b88c807SRodney W. Grimes *ap++ = grabstackstr(outp); 2424b88c807SRodney W. Grimes } 2434b88c807SRodney W. Grimes if (ap == newargs + 1) { /* if no args, maybe no exec is needed */ 2444b88c807SRodney W. Grimes p = newargs[0]; 2454b88c807SRodney W. Grimes for (;;) { 2464b88c807SRodney W. Grimes if (equal(p, "sh") || equal(p, "ash")) { 2474b88c807SRodney W. Grimes return; 2484b88c807SRodney W. Grimes } 2494b88c807SRodney W. Grimes while (*p != '/') { 2504b88c807SRodney W. Grimes if (*p == '\0') 2514b88c807SRodney W. Grimes goto break2; 2524b88c807SRodney W. Grimes p++; 2534b88c807SRodney W. Grimes } 2544b88c807SRodney W. Grimes p++; 2554b88c807SRodney W. Grimes } 2564b88c807SRodney W. Grimes break2:; 2574b88c807SRodney W. Grimes } 2584b88c807SRodney W. Grimes i = (char *)ap - (char *)newargs; /* size in bytes */ 2594b88c807SRodney W. Grimes if (i == 0) 2604b88c807SRodney W. Grimes error("Bad #! line"); 2614b88c807SRodney W. Grimes for (ap2 = argv ; *ap2++ != NULL ; ); 2624b88c807SRodney W. Grimes new = ckmalloc(i + ((char *)ap2 - (char *)argv)); 2634b88c807SRodney W. Grimes ap = newargs, ap2 = new; 2644b88c807SRodney W. Grimes while ((i -= sizeof (char **)) >= 0) 2654b88c807SRodney W. Grimes *ap2++ = *ap++; 2664b88c807SRodney W. Grimes ap = argv; 2674b88c807SRodney W. Grimes while (*ap2++ = *ap++); 2684b88c807SRodney W. Grimes shellexec(new, envp, pathval(), 0); 2694b88c807SRodney W. Grimes } 2704b88c807SRodney W. Grimes #endif 2714b88c807SRodney W. Grimes 2724b88c807SRodney W. Grimes 2734b88c807SRodney W. Grimes 2744b88c807SRodney W. Grimes /* 2754b88c807SRodney W. Grimes * Do a path search. The variable path (passed by reference) should be 2764b88c807SRodney W. Grimes * set to the start of the path before the first call; padvance will update 2774b88c807SRodney W. Grimes * this value as it proceeds. Successive calls to padvance will return 2784b88c807SRodney W. Grimes * the possible path expansions in sequence. If an option (indicated by 2794b88c807SRodney W. Grimes * a percent sign) appears in the path entry then the global variable 2804b88c807SRodney W. Grimes * pathopt will be set to point to it; otherwise pathopt will be set to 2814b88c807SRodney W. Grimes * NULL. 2824b88c807SRodney W. Grimes */ 2834b88c807SRodney W. Grimes 2844b88c807SRodney W. Grimes char *pathopt; 2854b88c807SRodney W. Grimes 2864b88c807SRodney W. Grimes char * 2874b88c807SRodney W. Grimes padvance(path, name) 2884b88c807SRodney W. Grimes char **path; 2894b88c807SRodney W. Grimes char *name; 2904b88c807SRodney W. Grimes { 29176ad65f7SSteve Price char *p, *q; 2924b88c807SRodney W. Grimes char *start; 2934b88c807SRodney W. Grimes int len; 2944b88c807SRodney W. Grimes 2954b88c807SRodney W. Grimes if (*path == NULL) 2964b88c807SRodney W. Grimes return NULL; 2974b88c807SRodney W. Grimes start = *path; 2984b88c807SRodney W. Grimes for (p = start ; *p && *p != ':' && *p != '%' ; p++); 2994b88c807SRodney W. Grimes len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ 3004b88c807SRodney W. Grimes while (stackblocksize() < len) 3014b88c807SRodney W. Grimes growstackblock(); 3024b88c807SRodney W. Grimes q = stackblock(); 3034b88c807SRodney W. Grimes if (p != start) { 304aa9caaf6SPeter Wemm memcpy(q, start, p - start); 3054b88c807SRodney W. Grimes q += p - start; 3064b88c807SRodney W. Grimes *q++ = '/'; 3074b88c807SRodney W. Grimes } 3084b88c807SRodney W. Grimes strcpy(q, name); 3094b88c807SRodney W. Grimes pathopt = NULL; 3104b88c807SRodney W. Grimes if (*p == '%') { 3114b88c807SRodney W. Grimes pathopt = ++p; 3124b88c807SRodney W. Grimes while (*p && *p != ':') p++; 3134b88c807SRodney W. Grimes } 3144b88c807SRodney W. Grimes if (*p == ':') 3154b88c807SRodney W. Grimes *path = p + 1; 3164b88c807SRodney W. Grimes else 3174b88c807SRodney W. Grimes *path = NULL; 3184b88c807SRodney W. Grimes return stalloc(len); 3194b88c807SRodney W. Grimes } 3204b88c807SRodney W. Grimes 3214b88c807SRodney W. Grimes 3224b88c807SRodney W. Grimes 3234b88c807SRodney W. Grimes /*** Command hashing code ***/ 3244b88c807SRodney W. Grimes 3254b88c807SRodney W. Grimes 326aa9caaf6SPeter Wemm int 327aa9caaf6SPeter Wemm hashcmd(argc, argv) 328aa9caaf6SPeter Wemm int argc; 329aa9caaf6SPeter Wemm char **argv; 330aa9caaf6SPeter Wemm { 3314b88c807SRodney W. Grimes struct tblentry **pp; 3324b88c807SRodney W. Grimes struct tblentry *cmdp; 3334b88c807SRodney W. Grimes int c; 3344b88c807SRodney W. Grimes int verbose; 3354b88c807SRodney W. Grimes struct cmdentry entry; 3364b88c807SRodney W. Grimes char *name; 3374b88c807SRodney W. Grimes 3384b88c807SRodney W. Grimes verbose = 0; 3394b88c807SRodney W. Grimes while ((c = nextopt("rv")) != '\0') { 3404b88c807SRodney W. Grimes if (c == 'r') { 3414b88c807SRodney W. Grimes clearcmdentry(0); 3424b88c807SRodney W. Grimes } else if (c == 'v') { 3434b88c807SRodney W. Grimes verbose++; 3444b88c807SRodney W. Grimes } 3454b88c807SRodney W. Grimes } 3464b88c807SRodney W. Grimes if (*argptr == NULL) { 3474b88c807SRodney W. Grimes for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) { 3484b88c807SRodney W. Grimes for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { 3494b88c807SRodney W. Grimes printentry(cmdp, verbose); 3504b88c807SRodney W. Grimes } 3514b88c807SRodney W. Grimes } 3524b88c807SRodney W. Grimes return 0; 3534b88c807SRodney W. Grimes } 3544b88c807SRodney W. Grimes while ((name = *argptr) != NULL) { 3554b88c807SRodney W. Grimes if ((cmdp = cmdlookup(name, 0)) != NULL 3564b88c807SRodney W. Grimes && (cmdp->cmdtype == CMDNORMAL 357aa9caaf6SPeter Wemm || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))) 3584b88c807SRodney W. Grimes delete_cmd_entry(); 359aa9caaf6SPeter Wemm find_command(name, &entry, 1, pathval()); 3604b88c807SRodney W. Grimes if (verbose) { 3614b88c807SRodney W. Grimes if (entry.cmdtype != CMDUNKNOWN) { /* if no error msg */ 3624b88c807SRodney W. Grimes cmdp = cmdlookup(name, 0); 3634b88c807SRodney W. Grimes printentry(cmdp, verbose); 3644b88c807SRodney W. Grimes } 3654b88c807SRodney W. Grimes flushall(); 3664b88c807SRodney W. Grimes } 3674b88c807SRodney W. Grimes argptr++; 3684b88c807SRodney W. Grimes } 3694b88c807SRodney W. Grimes return 0; 3704b88c807SRodney W. Grimes } 3714b88c807SRodney W. Grimes 3724b88c807SRodney W. Grimes 3734b88c807SRodney W. Grimes STATIC void 3744b88c807SRodney W. Grimes printentry(cmdp, verbose) 3754b88c807SRodney W. Grimes struct tblentry *cmdp; 3764b88c807SRodney W. Grimes int verbose; 3774b88c807SRodney W. Grimes { 3784b88c807SRodney W. Grimes int index; 3794b88c807SRodney W. Grimes char *path; 3804b88c807SRodney W. Grimes char *name; 3814b88c807SRodney W. Grimes 3824b88c807SRodney W. Grimes if (cmdp->cmdtype == CMDNORMAL) { 3834b88c807SRodney W. Grimes index = cmdp->param.index; 3844b88c807SRodney W. Grimes path = pathval(); 3854b88c807SRodney W. Grimes do { 3864b88c807SRodney W. Grimes name = padvance(&path, cmdp->cmdname); 3874b88c807SRodney W. Grimes stunalloc(name); 3884b88c807SRodney W. Grimes } while (--index >= 0); 3894b88c807SRodney W. Grimes out1str(name); 3904b88c807SRodney W. Grimes } else if (cmdp->cmdtype == CMDBUILTIN) { 3914b88c807SRodney W. Grimes out1fmt("builtin %s", cmdp->cmdname); 3924b88c807SRodney W. Grimes } else if (cmdp->cmdtype == CMDFUNCTION) { 3934b88c807SRodney W. Grimes out1fmt("function %s", cmdp->cmdname); 3944b88c807SRodney W. Grimes if (verbose) { 3954b88c807SRodney W. Grimes INTOFF; 3964b88c807SRodney W. Grimes name = commandtext(cmdp->param.func); 3974b88c807SRodney W. Grimes out1c(' '); 3984b88c807SRodney W. Grimes out1str(name); 3994b88c807SRodney W. Grimes ckfree(name); 4004b88c807SRodney W. Grimes INTON; 4014b88c807SRodney W. Grimes } 4024b88c807SRodney W. Grimes #ifdef DEBUG 4034b88c807SRodney W. Grimes } else { 4044b88c807SRodney W. Grimes error("internal error: cmdtype %d", cmdp->cmdtype); 4054b88c807SRodney W. Grimes #endif 4064b88c807SRodney W. Grimes } 4074b88c807SRodney W. Grimes if (cmdp->rehash) 4084b88c807SRodney W. Grimes out1c('*'); 4094b88c807SRodney W. Grimes out1c('\n'); 4104b88c807SRodney W. Grimes } 4114b88c807SRodney W. Grimes 4124b88c807SRodney W. Grimes 4134b88c807SRodney W. Grimes 4144b88c807SRodney W. Grimes /* 4154b88c807SRodney W. Grimes * Resolve a command name. If you change this routine, you may have to 4164b88c807SRodney W. Grimes * change the shellexec routine as well. 4174b88c807SRodney W. Grimes */ 4184b88c807SRodney W. Grimes 4194b88c807SRodney W. Grimes void 420aa9caaf6SPeter Wemm find_command(name, entry, printerr, path) 4214b88c807SRodney W. Grimes char *name; 4224b88c807SRodney W. Grimes struct cmdentry *entry; 423aa9caaf6SPeter Wemm int printerr; 424aa9caaf6SPeter Wemm char *path; 4254b88c807SRodney W. Grimes { 4264b88c807SRodney W. Grimes struct tblentry *cmdp; 4274b88c807SRodney W. Grimes int index; 4284b88c807SRodney W. Grimes int prev; 4294b88c807SRodney W. Grimes char *fullname; 4304b88c807SRodney W. Grimes struct stat statb; 4314b88c807SRodney W. Grimes int e; 4324b88c807SRodney W. Grimes int i; 4334b88c807SRodney W. Grimes 4344b88c807SRodney W. Grimes /* If name contains a slash, don't use the hash table */ 4354b88c807SRodney W. Grimes if (strchr(name, '/') != NULL) { 4364b88c807SRodney W. Grimes entry->cmdtype = CMDNORMAL; 4374b88c807SRodney W. Grimes entry->u.index = 0; 4384b88c807SRodney W. Grimes return; 4394b88c807SRodney W. Grimes } 4404b88c807SRodney W. Grimes 4414b88c807SRodney W. Grimes /* If name is in the table, and not invalidated by cd, we're done */ 4424b88c807SRodney W. Grimes if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0) 4434b88c807SRodney W. Grimes goto success; 4444b88c807SRodney W. Grimes 4454b88c807SRodney W. Grimes /* If %builtin not in path, check for builtin next */ 4464b88c807SRodney W. Grimes if (builtinloc < 0 && (i = find_builtin(name)) >= 0) { 4474b88c807SRodney W. Grimes INTOFF; 4484b88c807SRodney W. Grimes cmdp = cmdlookup(name, 1); 4494b88c807SRodney W. Grimes cmdp->cmdtype = CMDBUILTIN; 4504b88c807SRodney W. Grimes cmdp->param.index = i; 4514b88c807SRodney W. Grimes INTON; 4524b88c807SRodney W. Grimes goto success; 4534b88c807SRodney W. Grimes } 4544b88c807SRodney W. Grimes 4554b88c807SRodney W. Grimes /* We have to search path. */ 4564b88c807SRodney W. Grimes prev = -1; /* where to start */ 4574b88c807SRodney W. Grimes if (cmdp) { /* doing a rehash */ 4584b88c807SRodney W. Grimes if (cmdp->cmdtype == CMDBUILTIN) 4594b88c807SRodney W. Grimes prev = builtinloc; 4604b88c807SRodney W. Grimes else 4614b88c807SRodney W. Grimes prev = cmdp->param.index; 4624b88c807SRodney W. Grimes } 4634b88c807SRodney W. Grimes 4644b88c807SRodney W. Grimes e = ENOENT; 4654b88c807SRodney W. Grimes index = -1; 4664b88c807SRodney W. Grimes loop: 4674b88c807SRodney W. Grimes while ((fullname = padvance(&path, name)) != NULL) { 4684b88c807SRodney W. Grimes stunalloc(fullname); 4694b88c807SRodney W. Grimes index++; 4704b88c807SRodney W. Grimes if (pathopt) { 4714b88c807SRodney W. Grimes if (prefix("builtin", pathopt)) { 4724b88c807SRodney W. Grimes if ((i = find_builtin(name)) < 0) 4734b88c807SRodney W. Grimes goto loop; 4744b88c807SRodney W. Grimes INTOFF; 4754b88c807SRodney W. Grimes cmdp = cmdlookup(name, 1); 4764b88c807SRodney W. Grimes cmdp->cmdtype = CMDBUILTIN; 4774b88c807SRodney W. Grimes cmdp->param.index = i; 4784b88c807SRodney W. Grimes INTON; 4794b88c807SRodney W. Grimes goto success; 4804b88c807SRodney W. Grimes } else if (prefix("func", pathopt)) { 4814b88c807SRodney W. Grimes /* handled below */ 4824b88c807SRodney W. Grimes } else { 4834b88c807SRodney W. Grimes goto loop; /* ignore unimplemented options */ 4844b88c807SRodney W. Grimes } 4854b88c807SRodney W. Grimes } 4864b88c807SRodney W. Grimes /* if rehash, don't redo absolute path names */ 4874b88c807SRodney W. Grimes if (fullname[0] == '/' && index <= prev) { 4884b88c807SRodney W. Grimes if (index < prev) 4894b88c807SRodney W. Grimes goto loop; 4904b88c807SRodney W. Grimes TRACE(("searchexec \"%s\": no change\n", name)); 4914b88c807SRodney W. Grimes goto success; 4924b88c807SRodney W. Grimes } 4934b88c807SRodney W. Grimes while (stat(fullname, &statb) < 0) { 4944b88c807SRodney W. Grimes #ifdef SYSV 4954b88c807SRodney W. Grimes if (errno == EINTR) 4964b88c807SRodney W. Grimes continue; 4974b88c807SRodney W. Grimes #endif 4984b88c807SRodney W. Grimes if (errno != ENOENT && errno != ENOTDIR) 4994b88c807SRodney W. Grimes e = errno; 5004b88c807SRodney W. Grimes goto loop; 5014b88c807SRodney W. Grimes } 5024b88c807SRodney W. Grimes e = EACCES; /* if we fail, this will be the error */ 503aa9caaf6SPeter Wemm if (!S_ISREG(statb.st_mode)) 5044b88c807SRodney W. Grimes goto loop; 5054b88c807SRodney W. Grimes if (pathopt) { /* this is a %func directory */ 5064b88c807SRodney W. Grimes stalloc(strlen(fullname) + 1); 5074b88c807SRodney W. Grimes readcmdfile(fullname); 5084b88c807SRodney W. Grimes if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION) 5094b88c807SRodney W. Grimes error("%s not defined in %s", name, fullname); 5104b88c807SRodney W. Grimes stunalloc(fullname); 5114b88c807SRodney W. Grimes goto success; 5124b88c807SRodney W. Grimes } 5134b88c807SRodney W. Grimes #ifdef notdef 5144b88c807SRodney W. Grimes if (statb.st_uid == geteuid()) { 5154b88c807SRodney W. Grimes if ((statb.st_mode & 0100) == 0) 5164b88c807SRodney W. Grimes goto loop; 5174b88c807SRodney W. Grimes } else if (statb.st_gid == getegid()) { 5184b88c807SRodney W. Grimes if ((statb.st_mode & 010) == 0) 5194b88c807SRodney W. Grimes goto loop; 5204b88c807SRodney W. Grimes } else { 5214b88c807SRodney W. Grimes if ((statb.st_mode & 01) == 0) 5224b88c807SRodney W. Grimes goto loop; 5234b88c807SRodney W. Grimes } 5244b88c807SRodney W. Grimes #endif 5254b88c807SRodney W. Grimes TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); 5264b88c807SRodney W. Grimes INTOFF; 5274b88c807SRodney W. Grimes cmdp = cmdlookup(name, 1); 5284b88c807SRodney W. Grimes cmdp->cmdtype = CMDNORMAL; 5294b88c807SRodney W. Grimes cmdp->param.index = index; 5304b88c807SRodney W. Grimes INTON; 5314b88c807SRodney W. Grimes goto success; 5324b88c807SRodney W. Grimes } 5334b88c807SRodney W. Grimes 5344b88c807SRodney W. Grimes /* We failed. If there was an entry for this command, delete it */ 5354b88c807SRodney W. Grimes if (cmdp) 5364b88c807SRodney W. Grimes delete_cmd_entry(); 5374b88c807SRodney W. Grimes if (printerr) 5384b88c807SRodney W. Grimes outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC)); 5394b88c807SRodney W. Grimes entry->cmdtype = CMDUNKNOWN; 5404b88c807SRodney W. Grimes return; 5414b88c807SRodney W. Grimes 5424b88c807SRodney W. Grimes success: 5434b88c807SRodney W. Grimes cmdp->rehash = 0; 5444b88c807SRodney W. Grimes entry->cmdtype = cmdp->cmdtype; 5454b88c807SRodney W. Grimes entry->u = cmdp->param; 5464b88c807SRodney W. Grimes } 5474b88c807SRodney W. Grimes 5484b88c807SRodney W. Grimes 5494b88c807SRodney W. Grimes 5504b88c807SRodney W. Grimes /* 5514b88c807SRodney W. Grimes * Search the table of builtin commands. 5524b88c807SRodney W. Grimes */ 5534b88c807SRodney W. Grimes 5544b88c807SRodney W. Grimes int 5554b88c807SRodney W. Grimes find_builtin(name) 5564b88c807SRodney W. Grimes char *name; 5574b88c807SRodney W. Grimes { 55876ad65f7SSteve Price const struct builtincmd *bp; 5594b88c807SRodney W. Grimes 5604b88c807SRodney W. Grimes for (bp = builtincmd ; bp->name ; bp++) { 5614b88c807SRodney W. Grimes if (*bp->name == *name && equal(bp->name, name)) 5624b88c807SRodney W. Grimes return bp->code; 5634b88c807SRodney W. Grimes } 5644b88c807SRodney W. Grimes return -1; 5654b88c807SRodney W. Grimes } 5664b88c807SRodney W. Grimes 5674b88c807SRodney W. Grimes 5684b88c807SRodney W. Grimes 5694b88c807SRodney W. Grimes /* 5704b88c807SRodney W. Grimes * Called when a cd is done. Marks all commands so the next time they 5714b88c807SRodney W. Grimes * are executed they will be rehashed. 5724b88c807SRodney W. Grimes */ 5734b88c807SRodney W. Grimes 5744b88c807SRodney W. Grimes void 5754b88c807SRodney W. Grimes hashcd() { 5764b88c807SRodney W. Grimes struct tblentry **pp; 5774b88c807SRodney W. Grimes struct tblentry *cmdp; 5784b88c807SRodney W. Grimes 5794b88c807SRodney W. Grimes for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) { 5804b88c807SRodney W. Grimes for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { 5814b88c807SRodney W. Grimes if (cmdp->cmdtype == CMDNORMAL 582aa9caaf6SPeter Wemm || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) 5834b88c807SRodney W. Grimes cmdp->rehash = 1; 5844b88c807SRodney W. Grimes } 5854b88c807SRodney W. Grimes } 5864b88c807SRodney W. Grimes } 5874b88c807SRodney W. Grimes 5884b88c807SRodney W. Grimes 5894b88c807SRodney W. Grimes 5904b88c807SRodney W. Grimes /* 5914b88c807SRodney W. Grimes * Called before PATH is changed. The argument is the new value of PATH; 5924b88c807SRodney W. Grimes * pathval() still returns the old value at this point. Called with 5934b88c807SRodney W. Grimes * interrupts off. 5944b88c807SRodney W. Grimes */ 5954b88c807SRodney W. Grimes 5964b88c807SRodney W. Grimes void 5974b88c807SRodney W. Grimes changepath(newval) 598ab0a2172SSteve Price const char *newval; 5994b88c807SRodney W. Grimes { 600ab0a2172SSteve Price const char *old, *new; 6014b88c807SRodney W. Grimes int index; 6024b88c807SRodney W. Grimes int firstchange; 6034b88c807SRodney W. Grimes int bltin; 6044b88c807SRodney W. Grimes 6054b88c807SRodney W. Grimes old = pathval(); 6064b88c807SRodney W. Grimes new = newval; 6074b88c807SRodney W. Grimes firstchange = 9999; /* assume no change */ 608fa074287SDavid Greenman index = 0; 6094b88c807SRodney W. Grimes bltin = -1; 6104b88c807SRodney W. Grimes for (;;) { 6114b88c807SRodney W. Grimes if (*old != *new) { 6124b88c807SRodney W. Grimes firstchange = index; 613aa9caaf6SPeter Wemm if ((*old == '\0' && *new == ':') 614aa9caaf6SPeter Wemm || (*old == ':' && *new == '\0')) 6154b88c807SRodney W. Grimes firstchange++; 6164b88c807SRodney W. Grimes old = new; /* ignore subsequent differences */ 6174b88c807SRodney W. Grimes } 6184b88c807SRodney W. Grimes if (*new == '\0') 6194b88c807SRodney W. Grimes break; 6204b88c807SRodney W. Grimes if (*new == '%' && bltin < 0 && prefix("builtin", new + 1)) 6214b88c807SRodney W. Grimes bltin = index; 6224b88c807SRodney W. Grimes if (*new == ':') { 6234b88c807SRodney W. Grimes index++; 6244b88c807SRodney W. Grimes } 6254b88c807SRodney W. Grimes new++, old++; 6264b88c807SRodney W. Grimes } 6274b88c807SRodney W. Grimes if (builtinloc < 0 && bltin >= 0) 6284b88c807SRodney W. Grimes builtinloc = bltin; /* zap builtins */ 6294b88c807SRodney W. Grimes if (builtinloc >= 0 && bltin < 0) 6304b88c807SRodney W. Grimes firstchange = 0; 6314b88c807SRodney W. Grimes clearcmdentry(firstchange); 6324b88c807SRodney W. Grimes builtinloc = bltin; 6334b88c807SRodney W. Grimes } 6344b88c807SRodney W. Grimes 6354b88c807SRodney W. Grimes 6364b88c807SRodney W. Grimes /* 6374b88c807SRodney W. Grimes * Clear out command entries. The argument specifies the first entry in 6384b88c807SRodney W. Grimes * PATH which has changed. 6394b88c807SRodney W. Grimes */ 6404b88c807SRodney W. Grimes 6414b88c807SRodney W. Grimes STATIC void 642aa9caaf6SPeter Wemm clearcmdentry(firstchange) 643aa9caaf6SPeter Wemm int firstchange; 644aa9caaf6SPeter Wemm { 6454b88c807SRodney W. Grimes struct tblentry **tblp; 6464b88c807SRodney W. Grimes struct tblentry **pp; 6474b88c807SRodney W. Grimes struct tblentry *cmdp; 6484b88c807SRodney W. Grimes 6494b88c807SRodney W. Grimes INTOFF; 6504b88c807SRodney W. Grimes for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) { 6514b88c807SRodney W. Grimes pp = tblp; 6524b88c807SRodney W. Grimes while ((cmdp = *pp) != NULL) { 653aa9caaf6SPeter Wemm if ((cmdp->cmdtype == CMDNORMAL && 654aa9caaf6SPeter Wemm cmdp->param.index >= firstchange) 655aa9caaf6SPeter Wemm || (cmdp->cmdtype == CMDBUILTIN && 656aa9caaf6SPeter Wemm builtinloc >= firstchange)) { 6574b88c807SRodney W. Grimes *pp = cmdp->next; 6584b88c807SRodney W. Grimes ckfree(cmdp); 6594b88c807SRodney W. Grimes } else { 6604b88c807SRodney W. Grimes pp = &cmdp->next; 6614b88c807SRodney W. Grimes } 6624b88c807SRodney W. Grimes } 6634b88c807SRodney W. Grimes } 6644b88c807SRodney W. Grimes INTON; 6654b88c807SRodney W. Grimes } 6664b88c807SRodney W. Grimes 6674b88c807SRodney W. Grimes 6684b88c807SRodney W. Grimes /* 6694b88c807SRodney W. Grimes * Delete all functions. 6704b88c807SRodney W. Grimes */ 6714b88c807SRodney W. Grimes 6724b88c807SRodney W. Grimes #ifdef mkinit 6734b88c807SRodney W. Grimes MKINIT void deletefuncs(); 6744b88c807SRodney W. Grimes 6754b88c807SRodney W. Grimes SHELLPROC { 6764b88c807SRodney W. Grimes deletefuncs(); 6774b88c807SRodney W. Grimes } 6784b88c807SRodney W. Grimes #endif 6794b88c807SRodney W. Grimes 6804b88c807SRodney W. Grimes void 6814b88c807SRodney W. Grimes deletefuncs() { 6824b88c807SRodney W. Grimes struct tblentry **tblp; 6834b88c807SRodney W. Grimes struct tblentry **pp; 6844b88c807SRodney W. Grimes struct tblentry *cmdp; 6854b88c807SRodney W. Grimes 6864b88c807SRodney W. Grimes INTOFF; 6874b88c807SRodney W. Grimes for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) { 6884b88c807SRodney W. Grimes pp = tblp; 6894b88c807SRodney W. Grimes while ((cmdp = *pp) != NULL) { 6904b88c807SRodney W. Grimes if (cmdp->cmdtype == CMDFUNCTION) { 6914b88c807SRodney W. Grimes *pp = cmdp->next; 6924b88c807SRodney W. Grimes freefunc(cmdp->param.func); 6934b88c807SRodney W. Grimes ckfree(cmdp); 6944b88c807SRodney W. Grimes } else { 6954b88c807SRodney W. Grimes pp = &cmdp->next; 6964b88c807SRodney W. Grimes } 6974b88c807SRodney W. Grimes } 6984b88c807SRodney W. Grimes } 6994b88c807SRodney W. Grimes INTON; 7004b88c807SRodney W. Grimes } 7014b88c807SRodney W. Grimes 7024b88c807SRodney W. Grimes 7034b88c807SRodney W. Grimes 7044b88c807SRodney W. Grimes /* 7054b88c807SRodney W. Grimes * Locate a command in the command hash table. If "add" is nonzero, 7064b88c807SRodney W. Grimes * add the command to the table if it is not already present. The 7074b88c807SRodney W. Grimes * variable "lastcmdentry" is set to point to the address of the link 7084b88c807SRodney W. Grimes * pointing to the entry, so that delete_cmd_entry can delete the 7094b88c807SRodney W. Grimes * entry. 7104b88c807SRodney W. Grimes */ 7114b88c807SRodney W. Grimes 7124b88c807SRodney W. Grimes struct tblentry **lastcmdentry; 7134b88c807SRodney W. Grimes 7144b88c807SRodney W. Grimes 7154b88c807SRodney W. Grimes STATIC struct tblentry * 7164b88c807SRodney W. Grimes cmdlookup(name, add) 7174b88c807SRodney W. Grimes char *name; 718aa9caaf6SPeter Wemm int add; 7194b88c807SRodney W. Grimes { 7204b88c807SRodney W. Grimes int hashval; 72176ad65f7SSteve Price char *p; 7224b88c807SRodney W. Grimes struct tblentry *cmdp; 7234b88c807SRodney W. Grimes struct tblentry **pp; 7244b88c807SRodney W. Grimes 7254b88c807SRodney W. Grimes p = name; 7264b88c807SRodney W. Grimes hashval = *p << 4; 7274b88c807SRodney W. Grimes while (*p) 7284b88c807SRodney W. Grimes hashval += *p++; 7294b88c807SRodney W. Grimes hashval &= 0x7FFF; 7304b88c807SRodney W. Grimes pp = &cmdtable[hashval % CMDTABLESIZE]; 7314b88c807SRodney W. Grimes for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { 7324b88c807SRodney W. Grimes if (equal(cmdp->cmdname, name)) 7334b88c807SRodney W. Grimes break; 7344b88c807SRodney W. Grimes pp = &cmdp->next; 7354b88c807SRodney W. Grimes } 7364b88c807SRodney W. Grimes if (add && cmdp == NULL) { 7374b88c807SRodney W. Grimes INTOFF; 7384b88c807SRodney W. Grimes cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB 7394b88c807SRodney W. Grimes + strlen(name) + 1); 7404b88c807SRodney W. Grimes cmdp->next = NULL; 7414b88c807SRodney W. Grimes cmdp->cmdtype = CMDUNKNOWN; 7424b88c807SRodney W. Grimes cmdp->rehash = 0; 7434b88c807SRodney W. Grimes strcpy(cmdp->cmdname, name); 7444b88c807SRodney W. Grimes INTON; 7454b88c807SRodney W. Grimes } 7464b88c807SRodney W. Grimes lastcmdentry = pp; 7474b88c807SRodney W. Grimes return cmdp; 7484b88c807SRodney W. Grimes } 7494b88c807SRodney W. Grimes 7504b88c807SRodney W. Grimes /* 7514b88c807SRodney W. Grimes * Delete the command entry returned on the last lookup. 7524b88c807SRodney W. Grimes */ 7534b88c807SRodney W. Grimes 7544b88c807SRodney W. Grimes STATIC void 7554b88c807SRodney W. Grimes delete_cmd_entry() { 7564b88c807SRodney W. Grimes struct tblentry *cmdp; 7574b88c807SRodney W. Grimes 7584b88c807SRodney W. Grimes INTOFF; 7594b88c807SRodney W. Grimes cmdp = *lastcmdentry; 7604b88c807SRodney W. Grimes *lastcmdentry = cmdp->next; 7614b88c807SRodney W. Grimes ckfree(cmdp); 7624b88c807SRodney W. Grimes INTON; 7634b88c807SRodney W. Grimes } 7644b88c807SRodney W. Grimes 7654b88c807SRodney W. Grimes 7664b88c807SRodney W. Grimes 7674b88c807SRodney W. Grimes #ifdef notdef 7684b88c807SRodney W. Grimes void 7694b88c807SRodney W. Grimes getcmdentry(name, entry) 7704b88c807SRodney W. Grimes char *name; 7714b88c807SRodney W. Grimes struct cmdentry *entry; 7724b88c807SRodney W. Grimes { 7734b88c807SRodney W. Grimes struct tblentry *cmdp = cmdlookup(name, 0); 7744b88c807SRodney W. Grimes 7754b88c807SRodney W. Grimes if (cmdp) { 7764b88c807SRodney W. Grimes entry->u = cmdp->param; 7774b88c807SRodney W. Grimes entry->cmdtype = cmdp->cmdtype; 7784b88c807SRodney W. Grimes } else { 7794b88c807SRodney W. Grimes entry->cmdtype = CMDUNKNOWN; 7804b88c807SRodney W. Grimes entry->u.index = 0; 7814b88c807SRodney W. Grimes } 7824b88c807SRodney W. Grimes } 7834b88c807SRodney W. Grimes #endif 7844b88c807SRodney W. Grimes 7854b88c807SRodney W. Grimes 7864b88c807SRodney W. Grimes /* 7874b88c807SRodney W. Grimes * Add a new command entry, replacing any existing command entry for 7884b88c807SRodney W. Grimes * the same name. 7894b88c807SRodney W. Grimes */ 7904b88c807SRodney W. Grimes 7914b88c807SRodney W. Grimes void 7924b88c807SRodney W. Grimes addcmdentry(name, entry) 7934b88c807SRodney W. Grimes char *name; 7944b88c807SRodney W. Grimes struct cmdentry *entry; 7954b88c807SRodney W. Grimes { 7964b88c807SRodney W. Grimes struct tblentry *cmdp; 7974b88c807SRodney W. Grimes 7984b88c807SRodney W. Grimes INTOFF; 7994b88c807SRodney W. Grimes cmdp = cmdlookup(name, 1); 8004b88c807SRodney W. Grimes if (cmdp->cmdtype == CMDFUNCTION) { 8014b88c807SRodney W. Grimes freefunc(cmdp->param.func); 8024b88c807SRodney W. Grimes } 8034b88c807SRodney W. Grimes cmdp->cmdtype = entry->cmdtype; 8044b88c807SRodney W. Grimes cmdp->param = entry->u; 8054b88c807SRodney W. Grimes INTON; 8064b88c807SRodney W. Grimes } 8074b88c807SRodney W. Grimes 8084b88c807SRodney W. Grimes 8094b88c807SRodney W. Grimes /* 8104b88c807SRodney W. Grimes * Define a shell function. 8114b88c807SRodney W. Grimes */ 8124b88c807SRodney W. Grimes 8134b88c807SRodney W. Grimes void 8144b88c807SRodney W. Grimes defun(name, func) 8154b88c807SRodney W. Grimes char *name; 8164b88c807SRodney W. Grimes union node *func; 8174b88c807SRodney W. Grimes { 8184b88c807SRodney W. Grimes struct cmdentry entry; 8194b88c807SRodney W. Grimes 8204b88c807SRodney W. Grimes INTOFF; 8214b88c807SRodney W. Grimes entry.cmdtype = CMDFUNCTION; 8224b88c807SRodney W. Grimes entry.u.func = copyfunc(func); 8234b88c807SRodney W. Grimes addcmdentry(name, &entry); 8244b88c807SRodney W. Grimes INTON; 8254b88c807SRodney W. Grimes } 8264b88c807SRodney W. Grimes 8274b88c807SRodney W. Grimes 8284b88c807SRodney W. Grimes /* 8294b88c807SRodney W. Grimes * Delete a function if it exists. 8304b88c807SRodney W. Grimes */ 8314b88c807SRodney W. Grimes 8324b88c807SRodney W. Grimes int 8334b88c807SRodney W. Grimes unsetfunc(name) 8344b88c807SRodney W. Grimes char *name; 8354b88c807SRodney W. Grimes { 8364b88c807SRodney W. Grimes struct tblentry *cmdp; 8374b88c807SRodney W. Grimes 8384b88c807SRodney W. Grimes if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) { 8394b88c807SRodney W. Grimes freefunc(cmdp->param.func); 8404b88c807SRodney W. Grimes delete_cmd_entry(); 8414b88c807SRodney W. Grimes return (0); 8424b88c807SRodney W. Grimes } 8434b88c807SRodney W. Grimes return (1); 8444b88c807SRodney W. Grimes } 84576ad65f7SSteve Price 84676ad65f7SSteve Price /* 84776ad65f7SSteve Price * Locate and print what a word is... 84876ad65f7SSteve Price */ 84976ad65f7SSteve Price 85076ad65f7SSteve Price int 85176ad65f7SSteve Price typecmd(argc, argv) 85276ad65f7SSteve Price int argc; 85376ad65f7SSteve Price char **argv; 85476ad65f7SSteve Price { 85576ad65f7SSteve Price struct cmdentry entry; 85676ad65f7SSteve Price struct tblentry *cmdp; 85776ad65f7SSteve Price char **pp; 85876ad65f7SSteve Price struct alias *ap; 85976ad65f7SSteve Price int i; 86076ad65f7SSteve Price int error = 0; 86176ad65f7SSteve Price extern char *const parsekwd[]; 86276ad65f7SSteve Price 86376ad65f7SSteve Price for (i = 1; i < argc; i++) { 86476ad65f7SSteve Price out1str(argv[i]); 86576ad65f7SSteve Price /* First look at the keywords */ 86676ad65f7SSteve Price for (pp = (char **)parsekwd; *pp; pp++) 86776ad65f7SSteve Price if (**pp == *argv[i] && equal(*pp, argv[i])) 86876ad65f7SSteve Price break; 86976ad65f7SSteve Price 87076ad65f7SSteve Price if (*pp) { 87176ad65f7SSteve Price out1str(" is a shell keyword\n"); 87276ad65f7SSteve Price continue; 87376ad65f7SSteve Price } 87476ad65f7SSteve Price 87576ad65f7SSteve Price /* Then look at the aliases */ 87676ad65f7SSteve Price if ((ap = lookupalias(argv[i], 1)) != NULL) { 87776ad65f7SSteve Price out1fmt(" is an alias for %s\n", ap->val); 87876ad65f7SSteve Price continue; 87976ad65f7SSteve Price } 88076ad65f7SSteve Price 88176ad65f7SSteve Price /* Then check if it is a tracked alias */ 88276ad65f7SSteve Price if ((cmdp = cmdlookup(argv[i], 0)) != NULL) { 88376ad65f7SSteve Price entry.cmdtype = cmdp->cmdtype; 88476ad65f7SSteve Price entry.u = cmdp->param; 88576ad65f7SSteve Price } 88676ad65f7SSteve Price else { 88776ad65f7SSteve Price /* Finally use brute force */ 88876ad65f7SSteve Price find_command(argv[i], &entry, 0, pathval()); 88976ad65f7SSteve Price } 89076ad65f7SSteve Price 89176ad65f7SSteve Price switch (entry.cmdtype) { 89276ad65f7SSteve Price case CMDNORMAL: { 89376ad65f7SSteve Price int j = entry.u.index; 89476ad65f7SSteve Price char *path = pathval(), *name; 89576ad65f7SSteve Price do { 89676ad65f7SSteve Price name = padvance(&path, argv[i]); 89776ad65f7SSteve Price stunalloc(name); 89876ad65f7SSteve Price } while (--j >= 0); 89976ad65f7SSteve Price out1fmt(" is%s %s\n", 90076ad65f7SSteve Price cmdp ? " a tracked alias for" : "", name); 90176ad65f7SSteve Price break; 90276ad65f7SSteve Price } 90376ad65f7SSteve Price case CMDFUNCTION: 90476ad65f7SSteve Price out1str(" is a shell function\n"); 90576ad65f7SSteve Price break; 90676ad65f7SSteve Price 90776ad65f7SSteve Price case CMDBUILTIN: 90876ad65f7SSteve Price out1str(" is a shell builtin\n"); 90976ad65f7SSteve Price break; 91076ad65f7SSteve Price 91176ad65f7SSteve Price default: 91276ad65f7SSteve Price out1str(" not found\n"); 91376ad65f7SSteve Price error |= 127; 91476ad65f7SSteve Price break; 91576ad65f7SSteve Price } 91676ad65f7SSteve Price } 91776ad65f7SSteve Price return error; 91876ad65f7SSteve Price } 919