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.
16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes * without specific prior written permission.
194b88c807SRodney W. Grimes *
204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes * SUCH DAMAGE.
314b88c807SRodney W. Grimes */
324b88c807SRodney W. Grimes
33aa9caaf6SPeter Wemm #include <sys/types.h>
34aa9caaf6SPeter Wemm #include <sys/stat.h>
35aa9caaf6SPeter Wemm #include <unistd.h>
36aa9caaf6SPeter Wemm #include <fcntl.h>
37aa9caaf6SPeter Wemm #include <errno.h>
383835f47cSJilles Tjoelker #include <paths.h>
39e0f5c138SJilles Tjoelker #include <stdbool.h>
40aa9caaf6SPeter Wemm #include <stdlib.h>
41aa9caaf6SPeter Wemm
424b88c807SRodney W. Grimes /*
434b88c807SRodney W. Grimes * When commands are first encountered, they are entered in a hash table.
444b88c807SRodney W. Grimes * This ensures that a full path search will not have to be done for them
454b88c807SRodney W. Grimes * on each invocation.
464b88c807SRodney W. Grimes *
474b88c807SRodney W. Grimes * We should investigate converting to a linear search, even though that
484b88c807SRodney W. Grimes * would make the command name "hash" a misnomer.
494b88c807SRodney W. Grimes */
504b88c807SRodney W. Grimes
514b88c807SRodney W. Grimes #include "shell.h"
524b88c807SRodney W. Grimes #include "main.h"
534b88c807SRodney W. Grimes #include "nodes.h"
544b88c807SRodney W. Grimes #include "parser.h"
554b88c807SRodney W. Grimes #include "redir.h"
564b88c807SRodney W. Grimes #include "eval.h"
574b88c807SRodney W. Grimes #include "exec.h"
584b88c807SRodney W. Grimes #include "builtins.h"
594b88c807SRodney W. Grimes #include "var.h"
604b88c807SRodney W. Grimes #include "options.h"
614b88c807SRodney W. Grimes #include "input.h"
624b88c807SRodney W. Grimes #include "output.h"
634b88c807SRodney W. Grimes #include "syntax.h"
644b88c807SRodney W. Grimes #include "memalloc.h"
654b88c807SRodney W. Grimes #include "error.h"
664b88c807SRodney W. Grimes #include "mystring.h"
67aa9caaf6SPeter Wemm #include "show.h"
684b88c807SRodney W. Grimes #include "jobs.h"
6976ad65f7SSteve Price #include "alias.h"
704b88c807SRodney W. Grimes
714b88c807SRodney W. Grimes
724b88c807SRodney W. Grimes #define CMDTABLESIZE 31 /* should be prime */
734b88c807SRodney W. Grimes
744b88c807SRodney W. Grimes
754b88c807SRodney W. Grimes
764b88c807SRodney W. Grimes struct tblentry {
774b88c807SRodney W. Grimes struct tblentry *next; /* next entry in hash chain */
784b88c807SRodney W. Grimes union param param; /* definition of builtin function */
7985170a4aSStefan Farfeleder int special; /* flag for special builtin commands */
80d172408cSJilles Tjoelker signed char cmdtype; /* index identifying command */
81422c281cSJilles Tjoelker char cmdname[]; /* name of command */
824b88c807SRodney W. Grimes };
834b88c807SRodney W. Grimes
844b88c807SRodney W. Grimes
85aa7b6f82SDavid E. O'Brien static struct tblentry *cmdtable[CMDTABLESIZE];
86523646eeSJilles Tjoelker static int cmdtable_cd = 0; /* cmdtable contains cd-dependent entries */
874b88c807SRodney W. Grimes
884b88c807SRodney W. Grimes
8988328642SDavid E. O'Brien static void tryexec(char *, char **, char **);
9088328642SDavid E. O'Brien static void printentry(struct tblentry *, int);
9188328642SDavid E. O'Brien static struct tblentry *cmdlookup(const char *, int);
9288328642SDavid E. O'Brien static void delete_cmd_entry(void);
93260fc3f4SJilles Tjoelker static void addcmdentry(const char *, struct cmdentry *);
944b88c807SRodney W. Grimes
954b88c807SRodney W. Grimes
964b88c807SRodney W. Grimes
974b88c807SRodney W. Grimes /*
984b88c807SRodney W. Grimes * Exec a program. Never returns. If you change this routine, you may
994b88c807SRodney W. Grimes * have to change the find_command routine as well.
1003835f47cSJilles Tjoelker *
1013835f47cSJilles Tjoelker * The argv array may be changed and element argv[-1] should be writable.
1024b88c807SRodney W. Grimes */
1034b88c807SRodney W. Grimes
1044b88c807SRodney W. Grimes void
shellexec(char ** argv,char ** envp,const char * path,int idx)105384aedabSJilles Tjoelker shellexec(char **argv, char **envp, const char *path, int idx)
1064b88c807SRodney W. Grimes {
1074b88c807SRodney W. Grimes char *cmdname;
1084600b569SJilles Tjoelker const char *opt;
1094b88c807SRodney W. Grimes int e;
1104b88c807SRodney W. Grimes
1114b88c807SRodney W. Grimes if (strchr(argv[0], '/') != NULL) {
1124b88c807SRodney W. Grimes tryexec(argv[0], argv, envp);
1134b88c807SRodney W. Grimes e = errno;
1144b88c807SRodney W. Grimes } else {
1154b88c807SRodney W. Grimes e = ENOENT;
1164600b569SJilles Tjoelker while ((cmdname = padvance(&path, &opt, argv[0])) != NULL) {
1174600b569SJilles Tjoelker if (--idx < 0 && opt == NULL) {
1184b88c807SRodney W. Grimes tryexec(cmdname, argv, envp);
1194b88c807SRodney W. Grimes if (errno != ENOENT && errno != ENOTDIR)
1204b88c807SRodney W. Grimes e = errno;
121604e8224SJilles Tjoelker if (e == ENOEXEC)
122604e8224SJilles Tjoelker break;
1234b88c807SRodney W. Grimes }
1244b88c807SRodney W. Grimes stunalloc(cmdname);
1254b88c807SRodney W. Grimes }
1264b88c807SRodney W. Grimes }
127ab0a2172SSteve Price
128ab0a2172SSteve Price /* Map to POSIX errors */
129bb324af6SJilles Tjoelker if (e == ENOENT || e == ENOTDIR)
130bb324af6SJilles Tjoelker errorwithstatus(127, "%s: not found", argv[0]);
131bb324af6SJilles Tjoelker else
132bb324af6SJilles Tjoelker errorwithstatus(126, "%s: %s", argv[0], strerror(e));
133834d160bSJilles Tjoelker }
1344b88c807SRodney W. Grimes
1354b88c807SRodney W. Grimes
136e0f5c138SJilles Tjoelker static bool
isbinary(const char * data,size_t len)137e0f5c138SJilles Tjoelker isbinary(const char *data, size_t len)
138e0f5c138SJilles Tjoelker {
139e0f5c138SJilles Tjoelker const char *nul, *p;
140e0f5c138SJilles Tjoelker bool hasletter;
141e0f5c138SJilles Tjoelker
142e0f5c138SJilles Tjoelker nul = memchr(data, '\0', len);
143e0f5c138SJilles Tjoelker if (nul == NULL)
144e0f5c138SJilles Tjoelker return false;
145e0f5c138SJilles Tjoelker /*
146e0f5c138SJilles Tjoelker * POSIX says we shall allow execution if the initial part intended
147e0f5c138SJilles Tjoelker * to be parsed by the shell consists of characters and does not
148e0f5c138SJilles Tjoelker * contain the NUL character. This allows concatenating a shell
149e0f5c138SJilles Tjoelker * script (ending with exec or exit) and a binary payload.
150e0f5c138SJilles Tjoelker *
151e0f5c138SJilles Tjoelker * In order to reject common binary files such as PNG images, check
152e0f5c138SJilles Tjoelker * that there is a lowercase letter or expansion before the last
153e0f5c138SJilles Tjoelker * newline before the NUL character, in addition to the check for
154e0f5c138SJilles Tjoelker * the newline character suggested by POSIX.
155e0f5c138SJilles Tjoelker */
156e0f5c138SJilles Tjoelker hasletter = false;
157e0f5c138SJilles Tjoelker for (p = data; *p != '\0'; p++) {
158e0f5c138SJilles Tjoelker if ((*p >= 'a' && *p <= 'z') || *p == '$' || *p == '`')
159e0f5c138SJilles Tjoelker hasletter = true;
160e0f5c138SJilles Tjoelker if (hasletter && *p == '\n')
161e0f5c138SJilles Tjoelker return false;
162e0f5c138SJilles Tjoelker }
163e0f5c138SJilles Tjoelker return true;
164e0f5c138SJilles Tjoelker }
165e0f5c138SJilles Tjoelker
166e0f5c138SJilles Tjoelker
16788328642SDavid E. O'Brien static void
tryexec(char * cmd,char ** argv,char ** envp)1685134c3f7SWarner Losh tryexec(char *cmd, char **argv, char **envp)
1694b88c807SRodney W. Grimes {
170604e8224SJilles Tjoelker int e, in;
171604e8224SJilles Tjoelker ssize_t n;
172604e8224SJilles Tjoelker char buf[256];
1734b88c807SRodney W. Grimes
1744b88c807SRodney W. Grimes execve(cmd, argv, envp);
1754b88c807SRodney W. Grimes e = errno;
1764b88c807SRodney W. Grimes if (e == ENOEXEC) {
177604e8224SJilles Tjoelker INTOFF;
178604e8224SJilles Tjoelker in = open(cmd, O_RDONLY | O_NONBLOCK);
179604e8224SJilles Tjoelker if (in != -1) {
180604e8224SJilles Tjoelker n = pread(in, buf, sizeof buf, 0);
181604e8224SJilles Tjoelker close(in);
182e0f5c138SJilles Tjoelker if (n > 0 && isbinary(buf, n)) {
183604e8224SJilles Tjoelker errno = ENOEXEC;
184604e8224SJilles Tjoelker return;
185604e8224SJilles Tjoelker }
186604e8224SJilles Tjoelker }
1873835f47cSJilles Tjoelker *argv = cmd;
18846c6b52dSJilles Tjoelker *--argv = __DECONST(char *, _PATH_BSHELL);
1893835f47cSJilles Tjoelker execve(_PATH_BSHELL, argv, envp);
1904b88c807SRodney W. Grimes }
1914b88c807SRodney W. Grimes errno = e;
1924b88c807SRodney W. Grimes }
1934b88c807SRodney W. Grimes
1944b88c807SRodney W. Grimes /*
1954b88c807SRodney W. Grimes * Do a path search. The variable path (passed by reference) should be
1964b88c807SRodney W. Grimes * set to the start of the path before the first call; padvance will update
1974b88c807SRodney W. Grimes * this value as it proceeds. Successive calls to padvance will return
1984600b569SJilles Tjoelker * the possible path expansions in sequence. If popt is not NULL, options
1994600b569SJilles Tjoelker * are processed: if an option (indicated by a percent sign) appears in
2004600b569SJilles Tjoelker * the path entry then *popt will be set to point to it; else *popt will be
2014600b569SJilles Tjoelker * set to NULL. If popt is NULL, percent signs are not special.
2024b88c807SRodney W. Grimes */
2034b88c807SRodney W. Grimes
2044b88c807SRodney W. Grimes char *
padvance(const char ** path,const char ** popt,const char * name)2054600b569SJilles Tjoelker padvance(const char **path, const char **popt, const char *name)
2064b88c807SRodney W. Grimes {
2072cac6e36SJilles Tjoelker const char *p, *start;
2082cac6e36SJilles Tjoelker char *q;
209670dd3f0SJilles Tjoelker size_t len, namelen;
2104b88c807SRodney W. Grimes
2114b88c807SRodney W. Grimes if (*path == NULL)
2124b88c807SRodney W. Grimes return NULL;
2134b88c807SRodney W. Grimes start = *path;
2144600b569SJilles Tjoelker if (popt != NULL)
21535f2d3b6SRalf S. Engelschall for (p = start; *p && *p != ':' && *p != '%'; p++)
21635f2d3b6SRalf S. Engelschall ; /* nothing */
2174600b569SJilles Tjoelker else
2184600b569SJilles Tjoelker for (p = start; *p && *p != ':'; p++)
2194600b569SJilles Tjoelker ; /* nothing */
220670dd3f0SJilles Tjoelker namelen = strlen(name);
221670dd3f0SJilles Tjoelker len = p - start + namelen + 2; /* "2" is for '/' and '\0' */
222d8f32e72SJilles Tjoelker STARTSTACKSTR(q);
223d8f32e72SJilles Tjoelker CHECKSTRSPACE(len, q);
2244b88c807SRodney W. Grimes if (p != start) {
225aa9caaf6SPeter Wemm memcpy(q, start, p - start);
2264b88c807SRodney W. Grimes q += p - start;
2274b88c807SRodney W. Grimes *q++ = '/';
2284b88c807SRodney W. Grimes }
229670dd3f0SJilles Tjoelker memcpy(q, name, namelen + 1);
2304600b569SJilles Tjoelker if (popt != NULL) {
2314b88c807SRodney W. Grimes if (*p == '%') {
2324600b569SJilles Tjoelker *popt = ++p;
2334b88c807SRodney W. Grimes while (*p && *p != ':') p++;
2344600b569SJilles Tjoelker } else
2354600b569SJilles Tjoelker *popt = NULL;
2364b88c807SRodney W. Grimes }
2374b88c807SRodney W. Grimes if (*p == ':')
2384b88c807SRodney W. Grimes *path = p + 1;
2394b88c807SRodney W. Grimes else
2404b88c807SRodney W. Grimes *path = NULL;
2414b88c807SRodney W. Grimes return stalloc(len);
2424b88c807SRodney W. Grimes }
2434b88c807SRodney W. Grimes
2444b88c807SRodney W. Grimes
2454b88c807SRodney W. Grimes
2464b88c807SRodney W. Grimes /*** Command hashing code ***/
2474b88c807SRodney W. Grimes
2484b88c807SRodney W. Grimes
249aa9caaf6SPeter Wemm int
hashcmd(int argc __unused,char ** argv __unused)2505134c3f7SWarner Losh hashcmd(int argc __unused, char **argv __unused)
251aa9caaf6SPeter Wemm {
2524b88c807SRodney W. Grimes struct tblentry **pp;
2534b88c807SRodney W. Grimes struct tblentry *cmdp;
2544b88c807SRodney W. Grimes int c;
2554b88c807SRodney W. Grimes int verbose;
2564b88c807SRodney W. Grimes struct cmdentry entry;
2574b88c807SRodney W. Grimes char *name;
258c0b3cf06SJilles Tjoelker int errors;
2594b88c807SRodney W. Grimes
260c0b3cf06SJilles Tjoelker errors = 0;
2614b88c807SRodney W. Grimes verbose = 0;
2624b88c807SRodney W. Grimes while ((c = nextopt("rv")) != '\0') {
2634b88c807SRodney W. Grimes if (c == 'r') {
264c059d822SJilles Tjoelker clearcmdentry();
2654b88c807SRodney W. Grimes } else if (c == 'v') {
2664b88c807SRodney W. Grimes verbose++;
2674b88c807SRodney W. Grimes }
2684b88c807SRodney W. Grimes }
2694b88c807SRodney W. Grimes if (*argptr == NULL) {
2704b88c807SRodney W. Grimes for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
2714b88c807SRodney W. Grimes for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
27283952a11STim J. Robbins if (cmdp->cmdtype == CMDNORMAL)
2734b88c807SRodney W. Grimes printentry(cmdp, verbose);
2744b88c807SRodney W. Grimes }
2754b88c807SRodney W. Grimes }
2764b88c807SRodney W. Grimes return 0;
2774b88c807SRodney W. Grimes }
2784b88c807SRodney W. Grimes while ((name = *argptr) != NULL) {
2794b88c807SRodney W. Grimes if ((cmdp = cmdlookup(name, 0)) != NULL
2804b45b49aSJilles Tjoelker && cmdp->cmdtype == CMDNORMAL)
2814b88c807SRodney W. Grimes delete_cmd_entry();
282c848bc18SJilles Tjoelker find_command(name, &entry, DO_ERR, pathval());
283c0b3cf06SJilles Tjoelker if (entry.cmdtype == CMDUNKNOWN)
284c0b3cf06SJilles Tjoelker errors = 1;
285c0b3cf06SJilles Tjoelker else if (verbose) {
2864b88c807SRodney W. Grimes cmdp = cmdlookup(name, 0);
2874c7e4a54SGeorge C A Reid if (cmdp != NULL)
2884b88c807SRodney W. Grimes printentry(cmdp, verbose);
289c0b3cf06SJilles Tjoelker else {
290f7cc73afSJilles Tjoelker outfmt(out2, "%s: not found\n", name);
291c0b3cf06SJilles Tjoelker errors = 1;
2924b88c807SRodney W. Grimes }
2934b88c807SRodney W. Grimes flushall();
2944b88c807SRodney W. Grimes }
2954b88c807SRodney W. Grimes argptr++;
2964b88c807SRodney W. Grimes }
297c0b3cf06SJilles Tjoelker return errors;
2984b88c807SRodney W. Grimes }
2994b88c807SRodney W. Grimes
3004b88c807SRodney W. Grimes
30188328642SDavid E. O'Brien static void
printentry(struct tblentry * cmdp,int verbose)3025134c3f7SWarner Losh printentry(struct tblentry *cmdp, int verbose)
3034b88c807SRodney W. Grimes {
304384aedabSJilles Tjoelker int idx;
3054600b569SJilles Tjoelker const char *path, *opt;
3064b88c807SRodney W. Grimes char *name;
3074b88c807SRodney W. Grimes
3084b88c807SRodney W. Grimes if (cmdp->cmdtype == CMDNORMAL) {
309384aedabSJilles Tjoelker idx = cmdp->param.index;
3104b88c807SRodney W. Grimes path = pathval();
3114b88c807SRodney W. Grimes do {
3124600b569SJilles Tjoelker name = padvance(&path, &opt, cmdp->cmdname);
3134b88c807SRodney W. Grimes stunalloc(name);
314384aedabSJilles Tjoelker } while (--idx >= 0);
3154b88c807SRodney W. Grimes out1str(name);
3164b88c807SRodney W. Grimes } else if (cmdp->cmdtype == CMDBUILTIN) {
3174b88c807SRodney W. Grimes out1fmt("builtin %s", cmdp->cmdname);
3184b88c807SRodney W. Grimes } else if (cmdp->cmdtype == CMDFUNCTION) {
3194b88c807SRodney W. Grimes out1fmt("function %s", cmdp->cmdname);
3204b88c807SRodney W. Grimes if (verbose) {
3214b88c807SRodney W. Grimes INTOFF;
322e16947f8SJilles Tjoelker name = commandtext(getfuncnode(cmdp->param.func));
3234b88c807SRodney W. Grimes out1c(' ');
3244b88c807SRodney W. Grimes out1str(name);
3254b88c807SRodney W. Grimes ckfree(name);
3264b88c807SRodney W. Grimes INTON;
3274b88c807SRodney W. Grimes }
3284b88c807SRodney W. Grimes #ifdef DEBUG
3294b88c807SRodney W. Grimes } else {
3304b88c807SRodney W. Grimes error("internal error: cmdtype %d", cmdp->cmdtype);
3314b88c807SRodney W. Grimes #endif
3324b88c807SRodney W. Grimes }
3334b88c807SRodney W. Grimes out1c('\n');
3344b88c807SRodney W. Grimes }
3354b88c807SRodney W. Grimes
3364b88c807SRodney W. Grimes
3374b88c807SRodney W. Grimes
3384b88c807SRodney W. Grimes /*
3394b88c807SRodney W. Grimes * Resolve a command name. If you change this routine, you may have to
3404b88c807SRodney W. Grimes * change the shellexec routine as well.
3414b88c807SRodney W. Grimes */
3424b88c807SRodney W. Grimes
3434b88c807SRodney W. Grimes void
find_command(const char * name,struct cmdentry * entry,int act,const char * path)344c848bc18SJilles Tjoelker find_command(const char *name, struct cmdentry *entry, int act,
3452cac6e36SJilles Tjoelker const char *path)
3464b88c807SRodney W. Grimes {
347c848bc18SJilles Tjoelker struct tblentry *cmdp, loc_cmd;
348384aedabSJilles Tjoelker int idx;
3494600b569SJilles Tjoelker const char *opt;
3504b88c807SRodney W. Grimes char *fullname;
3514b88c807SRodney W. Grimes struct stat statb;
3524b88c807SRodney W. Grimes int e;
3534b88c807SRodney W. Grimes int i;
35485170a4aSStefan Farfeleder int spec;
355523646eeSJilles Tjoelker int cd;
3564b88c807SRodney W. Grimes
3574b88c807SRodney W. Grimes /* If name contains a slash, don't use the hash table */
3584b88c807SRodney W. Grimes if (strchr(name, '/') != NULL) {
3594b88c807SRodney W. Grimes entry->cmdtype = CMDNORMAL;
3604b88c807SRodney W. Grimes entry->u.index = 0;
3617d980385SJilles Tjoelker entry->special = 0;
3624b88c807SRodney W. Grimes return;
3634b88c807SRodney W. Grimes }
3644b88c807SRodney W. Grimes
365523646eeSJilles Tjoelker cd = 0;
366523646eeSJilles Tjoelker
367b9807277SJilles Tjoelker /* If name is in the table, we're done */
368523646eeSJilles Tjoelker if ((cmdp = cmdlookup(name, 0)) != NULL) {
369c848bc18SJilles Tjoelker if (cmdp->cmdtype == CMDFUNCTION && act & DO_NOFUNC)
370c848bc18SJilles Tjoelker cmdp = NULL;
371c848bc18SJilles Tjoelker else
3724b88c807SRodney W. Grimes goto success;
373c848bc18SJilles Tjoelker }
3744b88c807SRodney W. Grimes
3754b45b49aSJilles Tjoelker /* Check for builtin next */
3764b45b49aSJilles Tjoelker if ((i = find_builtin(name, &spec)) >= 0) {
3774b88c807SRodney W. Grimes INTOFF;
3784b88c807SRodney W. Grimes cmdp = cmdlookup(name, 1);
379c848bc18SJilles Tjoelker if (cmdp->cmdtype == CMDFUNCTION)
380c848bc18SJilles Tjoelker cmdp = &loc_cmd;
3814b88c807SRodney W. Grimes cmdp->cmdtype = CMDBUILTIN;
3824b88c807SRodney W. Grimes cmdp->param.index = i;
38385170a4aSStefan Farfeleder cmdp->special = spec;
3844b88c807SRodney W. Grimes INTON;
3854b88c807SRodney W. Grimes goto success;
3864b88c807SRodney W. Grimes }
3874b88c807SRodney W. Grimes
3884b88c807SRodney W. Grimes /* We have to search path. */
3894b88c807SRodney W. Grimes
3904b88c807SRodney W. Grimes e = ENOENT;
391384aedabSJilles Tjoelker idx = -1;
3924600b569SJilles Tjoelker for (;(fullname = padvance(&path, &opt, name)) != NULL;
3934600b569SJilles Tjoelker stunalloc(fullname)) {
394384aedabSJilles Tjoelker idx++;
3954600b569SJilles Tjoelker if (opt) {
3964600b569SJilles Tjoelker if (strncmp(opt, "func", 4) == 0) {
3974b88c807SRodney W. Grimes /* handled below */
3984b88c807SRodney W. Grimes } else {
3992ceda702SJilles Tjoelker continue; /* ignore unimplemented options */
4004b88c807SRodney W. Grimes }
4014b88c807SRodney W. Grimes }
402523646eeSJilles Tjoelker if (fullname[0] != '/')
403523646eeSJilles Tjoelker cd = 1;
4042628ebdbSTim J. Robbins if (stat(fullname, &statb) < 0) {
4054b88c807SRodney W. Grimes if (errno != ENOENT && errno != ENOTDIR)
4064b88c807SRodney W. Grimes e = errno;
4072ceda702SJilles Tjoelker continue;
4084b88c807SRodney W. Grimes }
4094b88c807SRodney W. Grimes e = EACCES; /* if we fail, this will be the error */
410aa9caaf6SPeter Wemm if (!S_ISREG(statb.st_mode))
4112ceda702SJilles Tjoelker continue;
4124600b569SJilles Tjoelker if (opt) { /* this is a %func directory */
413d2c23317SStephane Rochoy readcmdfile(fullname, -1 /* verify */);
4144b88c807SRodney W. Grimes if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
4154b88c807SRodney W. Grimes error("%s not defined in %s", name, fullname);
4164b88c807SRodney W. Grimes stunalloc(fullname);
4174b88c807SRodney W. Grimes goto success;
4184b88c807SRodney W. Grimes }
4194b88c807SRodney W. Grimes #ifdef notdef
4204b88c807SRodney W. Grimes if (statb.st_uid == geteuid()) {
4214b88c807SRodney W. Grimes if ((statb.st_mode & 0100) == 0)
4224b88c807SRodney W. Grimes goto loop;
4234b88c807SRodney W. Grimes } else if (statb.st_gid == getegid()) {
4244b88c807SRodney W. Grimes if ((statb.st_mode & 010) == 0)
4254b88c807SRodney W. Grimes goto loop;
4264b88c807SRodney W. Grimes } else {
4274b88c807SRodney W. Grimes if ((statb.st_mode & 01) == 0)
4284b88c807SRodney W. Grimes goto loop;
4294b88c807SRodney W. Grimes }
4304b88c807SRodney W. Grimes #endif
4314b88c807SRodney W. Grimes TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
4324b88c807SRodney W. Grimes INTOFF;
4332ceda702SJilles Tjoelker stunalloc(fullname);
4344b88c807SRodney W. Grimes cmdp = cmdlookup(name, 1);
435c848bc18SJilles Tjoelker if (cmdp->cmdtype == CMDFUNCTION)
436c848bc18SJilles Tjoelker cmdp = &loc_cmd;
4374b88c807SRodney W. Grimes cmdp->cmdtype = CMDNORMAL;
438384aedabSJilles Tjoelker cmdp->param.index = idx;
4397d980385SJilles Tjoelker cmdp->special = 0;
4404b88c807SRodney W. Grimes INTON;
4414b88c807SRodney W. Grimes goto success;
4424b88c807SRodney W. Grimes }
4434b88c807SRodney W. Grimes
444c848bc18SJilles Tjoelker if (act & DO_ERR) {
4458c395729STim J. Robbins if (e == ENOENT || e == ENOTDIR)
4468c395729STim J. Robbins outfmt(out2, "%s: not found\n", name);
4478c395729STim J. Robbins else
4481c59560dSTim J. Robbins outfmt(out2, "%s: %s\n", name, strerror(e));
4498c395729STim J. Robbins }
4504b88c807SRodney W. Grimes entry->cmdtype = CMDUNKNOWN;
451640b70e4SJilles Tjoelker entry->u.index = 0;
4527d980385SJilles Tjoelker entry->special = 0;
4534b88c807SRodney W. Grimes return;
4544b88c807SRodney W. Grimes
4554b88c807SRodney W. Grimes success:
456523646eeSJilles Tjoelker if (cd)
457523646eeSJilles Tjoelker cmdtable_cd = 1;
4584b88c807SRodney W. Grimes entry->cmdtype = cmdp->cmdtype;
4594b88c807SRodney W. Grimes entry->u = cmdp->param;
46085170a4aSStefan Farfeleder entry->special = cmdp->special;
4614b88c807SRodney W. Grimes }
4624b88c807SRodney W. Grimes
4634b88c807SRodney W. Grimes
4644b88c807SRodney W. Grimes
4654b88c807SRodney W. Grimes /*
4664b88c807SRodney W. Grimes * Search the table of builtin commands.
4674b88c807SRodney W. Grimes */
4684b88c807SRodney W. Grimes
4694b88c807SRodney W. Grimes int
find_builtin(const char * name,int * special)4702cac6e36SJilles Tjoelker find_builtin(const char *name, int *special)
4714b88c807SRodney W. Grimes {
472d3fa2c78SJilles Tjoelker const unsigned char *bp;
473d3fa2c78SJilles Tjoelker size_t len;
4744b88c807SRodney W. Grimes
475d3fa2c78SJilles Tjoelker len = strlen(name);
476d3fa2c78SJilles Tjoelker for (bp = builtincmd ; *bp ; bp += 2 + bp[0]) {
477d3fa2c78SJilles Tjoelker if (bp[0] == len && memcmp(bp + 2, name, len) == 0) {
478d3fa2c78SJilles Tjoelker *special = (bp[1] & BUILTIN_SPECIAL) != 0;
479d3fa2c78SJilles Tjoelker return bp[1] & ~BUILTIN_SPECIAL;
4804b88c807SRodney W. Grimes }
48185170a4aSStefan Farfeleder }
4824b88c807SRodney W. Grimes return -1;
4834b88c807SRodney W. Grimes }
4844b88c807SRodney W. Grimes
4854b88c807SRodney W. Grimes
4864b88c807SRodney W. Grimes
4874b88c807SRodney W. Grimes /*
488523646eeSJilles Tjoelker * Called when a cd is done. If any entry in cmdtable depends on the current
489523646eeSJilles Tjoelker * directory, simply clear cmdtable completely.
4904b88c807SRodney W. Grimes */
4914b88c807SRodney W. Grimes
4924b88c807SRodney W. Grimes void
hashcd(void)4935134c3f7SWarner Losh hashcd(void)
4945134c3f7SWarner Losh {
495523646eeSJilles Tjoelker if (cmdtable_cd)
496523646eeSJilles Tjoelker clearcmdentry();
4974b88c807SRodney W. Grimes }
4984b88c807SRodney W. Grimes
4994b88c807SRodney W. Grimes
5004b88c807SRodney W. Grimes
5014b88c807SRodney W. Grimes /*
5024b88c807SRodney W. Grimes * Called before PATH is changed. The argument is the new value of PATH;
5034b88c807SRodney W. Grimes * pathval() still returns the old value at this point. Called with
5044b88c807SRodney W. Grimes * interrupts off.
5054b88c807SRodney W. Grimes */
5064b88c807SRodney W. Grimes
5074b88c807SRodney W. Grimes void
changepath(const char * newval __unused)5082fae4c3dSPhilippe Charnier changepath(const char *newval __unused)
5094b88c807SRodney W. Grimes {
510c059d822SJilles Tjoelker clearcmdentry();
5114b88c807SRodney W. Grimes }
5124b88c807SRodney W. Grimes
5134b88c807SRodney W. Grimes
5144b88c807SRodney W. Grimes /*
515b9807277SJilles Tjoelker * Clear out cached utility locations.
5164b88c807SRodney W. Grimes */
5174b88c807SRodney W. Grimes
518a436dc79SMartin Cracauer void
clearcmdentry(void)519c059d822SJilles Tjoelker clearcmdentry(void)
520aa9caaf6SPeter Wemm {
5214b88c807SRodney W. Grimes struct tblentry **tblp;
5224b88c807SRodney W. Grimes struct tblentry **pp;
5234b88c807SRodney W. Grimes struct tblentry *cmdp;
5244b88c807SRodney W. Grimes
5254b88c807SRodney W. Grimes INTOFF;
5264b88c807SRodney W. Grimes for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
5274b88c807SRodney W. Grimes pp = tblp;
5284b88c807SRodney W. Grimes while ((cmdp = *pp) != NULL) {
529c059d822SJilles Tjoelker if (cmdp->cmdtype == CMDNORMAL) {
5304b88c807SRodney W. Grimes *pp = cmdp->next;
5314b88c807SRodney W. Grimes ckfree(cmdp);
5324b88c807SRodney W. Grimes } else {
5334b88c807SRodney W. Grimes pp = &cmdp->next;
5344b88c807SRodney W. Grimes }
5354b88c807SRodney W. Grimes }
5364b88c807SRodney W. Grimes }
537523646eeSJilles Tjoelker cmdtable_cd = 0;
5384b88c807SRodney W. Grimes INTON;
5394b88c807SRodney W. Grimes }
5404b88c807SRodney W. Grimes
5414b88c807SRodney W. Grimes
542*bec7b9a2SPiotr Pawel Stefaniak static unsigned int
hashname(const char * p)543*bec7b9a2SPiotr Pawel Stefaniak hashname(const char *p)
544*bec7b9a2SPiotr Pawel Stefaniak {
545*bec7b9a2SPiotr Pawel Stefaniak unsigned int hashval;
546*bec7b9a2SPiotr Pawel Stefaniak
547*bec7b9a2SPiotr Pawel Stefaniak hashval = (unsigned char)*p << 4;
548*bec7b9a2SPiotr Pawel Stefaniak while (*p)
549*bec7b9a2SPiotr Pawel Stefaniak hashval += *p++;
550*bec7b9a2SPiotr Pawel Stefaniak
551*bec7b9a2SPiotr Pawel Stefaniak return (hashval % CMDTABLESIZE);
552*bec7b9a2SPiotr Pawel Stefaniak }
553*bec7b9a2SPiotr Pawel Stefaniak
554*bec7b9a2SPiotr Pawel Stefaniak
5554b88c807SRodney W. Grimes /*
5564b88c807SRodney W. Grimes * Locate a command in the command hash table. If "add" is nonzero,
5574b88c807SRodney W. Grimes * add the command to the table if it is not already present. The
5584b88c807SRodney W. Grimes * variable "lastcmdentry" is set to point to the address of the link
5594b88c807SRodney W. Grimes * pointing to the entry, so that delete_cmd_entry can delete the
5604b88c807SRodney W. Grimes * entry.
5614b88c807SRodney W. Grimes */
5624b88c807SRodney W. Grimes
563aa7b6f82SDavid E. O'Brien static struct tblentry **lastcmdentry;
5644b88c807SRodney W. Grimes
5654b88c807SRodney W. Grimes
56688328642SDavid E. O'Brien static struct tblentry *
cmdlookup(const char * name,int add)5672cac6e36SJilles Tjoelker cmdlookup(const char *name, int add)
5684b88c807SRodney W. Grimes {
5694b88c807SRodney W. Grimes struct tblentry *cmdp;
5704b88c807SRodney W. Grimes struct tblentry **pp;
571670dd3f0SJilles Tjoelker size_t len;
5724b88c807SRodney W. Grimes
573*bec7b9a2SPiotr Pawel Stefaniak pp = &cmdtable[hashname(name)];
5744b88c807SRodney W. Grimes for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
5754b88c807SRodney W. Grimes if (equal(cmdp->cmdname, name))
5764b88c807SRodney W. Grimes break;
5774b88c807SRodney W. Grimes pp = &cmdp->next;
5784b88c807SRodney W. Grimes }
5794b88c807SRodney W. Grimes if (add && cmdp == NULL) {
5804b88c807SRodney W. Grimes INTOFF;
581670dd3f0SJilles Tjoelker len = strlen(name);
582670dd3f0SJilles Tjoelker cmdp = *pp = ckmalloc(sizeof (struct tblentry) + len + 1);
5834b88c807SRodney W. Grimes cmdp->next = NULL;
5844b88c807SRodney W. Grimes cmdp->cmdtype = CMDUNKNOWN;
585670dd3f0SJilles Tjoelker memcpy(cmdp->cmdname, name, len + 1);
5864b88c807SRodney W. Grimes INTON;
5874b88c807SRodney W. Grimes }
5884b88c807SRodney W. Grimes lastcmdentry = pp;
5894b88c807SRodney W. Grimes return cmdp;
5904b88c807SRodney W. Grimes }
5914b88c807SRodney W. Grimes
592*bec7b9a2SPiotr Pawel Stefaniak const void *
itercmd(const void * entry,struct cmdentry * result)593*bec7b9a2SPiotr Pawel Stefaniak itercmd(const void *entry, struct cmdentry *result)
594*bec7b9a2SPiotr Pawel Stefaniak {
595*bec7b9a2SPiotr Pawel Stefaniak const struct tblentry *e = entry;
596*bec7b9a2SPiotr Pawel Stefaniak size_t i = 0;
597*bec7b9a2SPiotr Pawel Stefaniak
598*bec7b9a2SPiotr Pawel Stefaniak if (e != NULL) {
599*bec7b9a2SPiotr Pawel Stefaniak if (e->next != NULL) {
600*bec7b9a2SPiotr Pawel Stefaniak e = e->next;
601*bec7b9a2SPiotr Pawel Stefaniak goto success;
602*bec7b9a2SPiotr Pawel Stefaniak }
603*bec7b9a2SPiotr Pawel Stefaniak i = hashname(e->cmdname) + 1;
604*bec7b9a2SPiotr Pawel Stefaniak }
605*bec7b9a2SPiotr Pawel Stefaniak for (; i < CMDTABLESIZE; i++)
606*bec7b9a2SPiotr Pawel Stefaniak if ((e = cmdtable[i]) != NULL)
607*bec7b9a2SPiotr Pawel Stefaniak goto success;
608*bec7b9a2SPiotr Pawel Stefaniak
609*bec7b9a2SPiotr Pawel Stefaniak return (NULL);
610*bec7b9a2SPiotr Pawel Stefaniak success:
611*bec7b9a2SPiotr Pawel Stefaniak result->cmdtype = e->cmdtype;
612*bec7b9a2SPiotr Pawel Stefaniak result->cmdname = e->cmdname;
613*bec7b9a2SPiotr Pawel Stefaniak
614*bec7b9a2SPiotr Pawel Stefaniak return (e);
615*bec7b9a2SPiotr Pawel Stefaniak }
616*bec7b9a2SPiotr Pawel Stefaniak
6174b88c807SRodney W. Grimes /*
6184b88c807SRodney W. Grimes * Delete the command entry returned on the last lookup.
6194b88c807SRodney W. Grimes */
6204b88c807SRodney W. Grimes
62188328642SDavid E. O'Brien static void
delete_cmd_entry(void)6225134c3f7SWarner Losh delete_cmd_entry(void)
6235134c3f7SWarner Losh {
6244b88c807SRodney W. Grimes struct tblentry *cmdp;
6254b88c807SRodney W. Grimes
6264b88c807SRodney W. Grimes INTOFF;
6274b88c807SRodney W. Grimes cmdp = *lastcmdentry;
6284b88c807SRodney W. Grimes *lastcmdentry = cmdp->next;
6294b88c807SRodney W. Grimes ckfree(cmdp);
6304b88c807SRodney W. Grimes INTON;
6314b88c807SRodney W. Grimes }
6324b88c807SRodney W. Grimes
6334b88c807SRodney W. Grimes
6344b88c807SRodney W. Grimes
6354b88c807SRodney W. Grimes /*
6364b88c807SRodney W. Grimes * Add a new command entry, replacing any existing command entry for
6374b88c807SRodney W. Grimes * the same name.
6384b88c807SRodney W. Grimes */
6394b88c807SRodney W. Grimes
640260fc3f4SJilles Tjoelker static void
addcmdentry(const char * name,struct cmdentry * entry)6412cac6e36SJilles Tjoelker addcmdentry(const char *name, struct cmdentry *entry)
6424b88c807SRodney W. Grimes {
6434b88c807SRodney W. Grimes struct tblentry *cmdp;
6444b88c807SRodney W. Grimes
6454b88c807SRodney W. Grimes INTOFF;
6464b88c807SRodney W. Grimes cmdp = cmdlookup(name, 1);
6474b88c807SRodney W. Grimes if (cmdp->cmdtype == CMDFUNCTION) {
648eb33e843SJilles Tjoelker unreffunc(cmdp->param.func);
6494b88c807SRodney W. Grimes }
6504b88c807SRodney W. Grimes cmdp->cmdtype = entry->cmdtype;
6514b88c807SRodney W. Grimes cmdp->param = entry->u;
6527d980385SJilles Tjoelker cmdp->special = entry->special;
6534b88c807SRodney W. Grimes INTON;
6544b88c807SRodney W. Grimes }
6554b88c807SRodney W. Grimes
6564b88c807SRodney W. Grimes
6574b88c807SRodney W. Grimes /*
6584b88c807SRodney W. Grimes * Define a shell function.
6594b88c807SRodney W. Grimes */
6604b88c807SRodney W. Grimes
6614b88c807SRodney W. Grimes void
defun(const char * name,union node * func)6622cac6e36SJilles Tjoelker defun(const char *name, union node *func)
6634b88c807SRodney W. Grimes {
6644b88c807SRodney W. Grimes struct cmdentry entry;
6654b88c807SRodney W. Grimes
6664b88c807SRodney W. Grimes INTOFF;
6674b88c807SRodney W. Grimes entry.cmdtype = CMDFUNCTION;
6684b88c807SRodney W. Grimes entry.u.func = copyfunc(func);
6697d980385SJilles Tjoelker entry.special = 0;
6704b88c807SRodney W. Grimes addcmdentry(name, &entry);
6714b88c807SRodney W. Grimes INTON;
6724b88c807SRodney W. Grimes }
6734b88c807SRodney W. Grimes
6744b88c807SRodney W. Grimes
6754b88c807SRodney W. Grimes /*
6764b88c807SRodney W. Grimes * Delete a function if it exists.
6771632bf1aSJilles Tjoelker * Called with interrupts off.
6784b88c807SRodney W. Grimes */
6794b88c807SRodney W. Grimes
6804b88c807SRodney W. Grimes int
unsetfunc(const char * name)6812cac6e36SJilles Tjoelker unsetfunc(const char *name)
6824b88c807SRodney W. Grimes {
6834b88c807SRodney W. Grimes struct tblentry *cmdp;
6844b88c807SRodney W. Grimes
6854b88c807SRodney W. Grimes if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
686eb33e843SJilles Tjoelker unreffunc(cmdp->param.func);
6874b88c807SRodney W. Grimes delete_cmd_entry();
6884b88c807SRodney W. Grimes return (0);
6894b88c807SRodney W. Grimes }
690bd766773SDag-Erling Smørgrav return (0);
6914b88c807SRodney W. Grimes }
69276ad65f7SSteve Price
69384fbdd8cSJilles Tjoelker
69484fbdd8cSJilles Tjoelker /*
69584fbdd8cSJilles Tjoelker * Check if a function by a certain name exists.
69684fbdd8cSJilles Tjoelker */
69784fbdd8cSJilles Tjoelker int
isfunc(const char * name)69884fbdd8cSJilles Tjoelker isfunc(const char *name)
69984fbdd8cSJilles Tjoelker {
70084fbdd8cSJilles Tjoelker struct tblentry *cmdp;
70184fbdd8cSJilles Tjoelker cmdp = cmdlookup(name, 0);
70284fbdd8cSJilles Tjoelker return (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION);
70384fbdd8cSJilles Tjoelker }
70484fbdd8cSJilles Tjoelker
70584fbdd8cSJilles Tjoelker
706ccd0a51fSJilles Tjoelker static void
print_absolute_path(const char * name)707ccd0a51fSJilles Tjoelker print_absolute_path(const char *name)
708ccd0a51fSJilles Tjoelker {
709ccd0a51fSJilles Tjoelker const char *pwd;
710ccd0a51fSJilles Tjoelker
711ccd0a51fSJilles Tjoelker if (*name != '/' && (pwd = lookupvar("PWD")) != NULL && *pwd != '\0') {
712ccd0a51fSJilles Tjoelker out1str(pwd);
713ccd0a51fSJilles Tjoelker if (strcmp(pwd, "/") != 0)
714ccd0a51fSJilles Tjoelker outcslow('/', out1);
715ccd0a51fSJilles Tjoelker }
716ccd0a51fSJilles Tjoelker out1str(name);
717ccd0a51fSJilles Tjoelker outcslow('\n', out1);
718ccd0a51fSJilles Tjoelker }
719ccd0a51fSJilles Tjoelker
720ccd0a51fSJilles Tjoelker
72176ad65f7SSteve Price /*
722b2f153feSStefan Farfeleder * Shared code for the following builtin commands:
723b2f153feSStefan Farfeleder * type, command -v, command -V
72476ad65f7SSteve Price */
72576ad65f7SSteve Price
72676ad65f7SSteve Price int
typecmd_impl(int argc,char ** argv,int cmd,const char * path)72706a8a57fSJilles Tjoelker typecmd_impl(int argc, char **argv, int cmd, const char *path)
72876ad65f7SSteve Price {
72976ad65f7SSteve Price struct cmdentry entry;
73076ad65f7SSteve Price struct tblentry *cmdp;
731384aedabSJilles Tjoelker const char *const *pp;
73276ad65f7SSteve Price struct alias *ap;
73376ad65f7SSteve Price int i;
734384aedabSJilles Tjoelker int error1 = 0;
73576ad65f7SSteve Price
73606a8a57fSJilles Tjoelker if (path != pathval())
737c059d822SJilles Tjoelker clearcmdentry();
73806a8a57fSJilles Tjoelker
73976ad65f7SSteve Price for (i = 1; i < argc; i++) {
74076ad65f7SSteve Price /* First look at the keywords */
741384aedabSJilles Tjoelker for (pp = parsekwd; *pp; pp++)
74276ad65f7SSteve Price if (**pp == *argv[i] && equal(*pp, argv[i]))
74376ad65f7SSteve Price break;
74476ad65f7SSteve Price
74576ad65f7SSteve Price if (*pp) {
746b2f153feSStefan Farfeleder if (cmd == TYPECMD_SMALLV)
747b2f153feSStefan Farfeleder out1fmt("%s\n", argv[i]);
748b2f153feSStefan Farfeleder else
749f7bbf3ffSStefan Farfeleder out1fmt("%s is a shell keyword\n", argv[i]);
75076ad65f7SSteve Price continue;
75176ad65f7SSteve Price }
75276ad65f7SSteve Price
75376ad65f7SSteve Price /* Then look at the aliases */
75476ad65f7SSteve Price if ((ap = lookupalias(argv[i], 1)) != NULL) {
7555d4d10e3SJilles Tjoelker if (cmd == TYPECMD_SMALLV) {
7565d4d10e3SJilles Tjoelker out1fmt("alias %s=", argv[i]);
7575d4d10e3SJilles Tjoelker out1qstr(ap->val);
7585d4d10e3SJilles Tjoelker outcslow('\n', out1);
7595d4d10e3SJilles Tjoelker } else
760f7bbf3ffSStefan Farfeleder out1fmt("%s is an alias for %s\n", argv[i],
761f7bbf3ffSStefan Farfeleder ap->val);
76276ad65f7SSteve Price continue;
76376ad65f7SSteve Price }
76476ad65f7SSteve Price
76576ad65f7SSteve Price /* Then check if it is a tracked alias */
76676ad65f7SSteve Price if ((cmdp = cmdlookup(argv[i], 0)) != NULL) {
76776ad65f7SSteve Price entry.cmdtype = cmdp->cmdtype;
76876ad65f7SSteve Price entry.u = cmdp->param;
76930268dfaSJilles Tjoelker entry.special = cmdp->special;
77076ad65f7SSteve Price }
77176ad65f7SSteve Price else {
77276ad65f7SSteve Price /* Finally use brute force */
77306a8a57fSJilles Tjoelker find_command(argv[i], &entry, 0, path);
77476ad65f7SSteve Price }
77576ad65f7SSteve Price
77676ad65f7SSteve Price switch (entry.cmdtype) {
77776ad65f7SSteve Price case CMDNORMAL: {
778d753a425SMartin Cracauer if (strchr(argv[i], '/') == NULL) {
77906a8a57fSJilles Tjoelker const char *path2 = path;
7804600b569SJilles Tjoelker const char *opt2;
7812cac6e36SJilles Tjoelker char *name;
782d753a425SMartin Cracauer int j = entry.u.index;
78376ad65f7SSteve Price do {
7844600b569SJilles Tjoelker name = padvance(&path2, &opt2, argv[i]);
78576ad65f7SSteve Price stunalloc(name);
78676ad65f7SSteve Price } while (--j >= 0);
787ccd0a51fSJilles Tjoelker if (cmd != TYPECMD_SMALLV)
788ccd0a51fSJilles Tjoelker out1fmt("%s is%s ", argv[i],
789b2f153feSStefan Farfeleder (cmdp && cmd == TYPECMD_TYPE) ?
790ccd0a51fSJilles Tjoelker " a tracked alias for" : "");
791ccd0a51fSJilles Tjoelker print_absolute_path(name);
792d753a425SMartin Cracauer } else {
793d92e35fdSStefan Farfeleder if (eaccess(argv[i], X_OK) == 0) {
794ccd0a51fSJilles Tjoelker if (cmd != TYPECMD_SMALLV)
795ccd0a51fSJilles Tjoelker out1fmt("%s is ", argv[i]);
796ccd0a51fSJilles Tjoelker print_absolute_path(argv[i]);
797f30842baSStefan Farfeleder } else {
798f30842baSStefan Farfeleder if (cmd != TYPECMD_SMALLV)
799f7bbf3ffSStefan Farfeleder outfmt(out2, "%s: %s\n",
800f7bbf3ffSStefan Farfeleder argv[i], strerror(errno));
801384aedabSJilles Tjoelker error1 |= 127;
802b2f153feSStefan Farfeleder }
803d753a425SMartin Cracauer }
80476ad65f7SSteve Price break;
80576ad65f7SSteve Price }
80676ad65f7SSteve Price case CMDFUNCTION:
807b2f153feSStefan Farfeleder if (cmd == TYPECMD_SMALLV)
808b2f153feSStefan Farfeleder out1fmt("%s\n", argv[i]);
809b2f153feSStefan Farfeleder else
810f7bbf3ffSStefan Farfeleder out1fmt("%s is a shell function\n", argv[i]);
81176ad65f7SSteve Price break;
81276ad65f7SSteve Price
81376ad65f7SSteve Price case CMDBUILTIN:
814b2f153feSStefan Farfeleder if (cmd == TYPECMD_SMALLV)
815b2f153feSStefan Farfeleder out1fmt("%s\n", argv[i]);
81630268dfaSJilles Tjoelker else if (entry.special)
81730268dfaSJilles Tjoelker out1fmt("%s is a special shell builtin\n",
81830268dfaSJilles Tjoelker argv[i]);
819b2f153feSStefan Farfeleder else
820f7bbf3ffSStefan Farfeleder out1fmt("%s is a shell builtin\n", argv[i]);
82176ad65f7SSteve Price break;
82276ad65f7SSteve Price
82376ad65f7SSteve Price default:
824b2f153feSStefan Farfeleder if (cmd != TYPECMD_SMALLV)
825f7bbf3ffSStefan Farfeleder outfmt(out2, "%s: not found\n", argv[i]);
826384aedabSJilles Tjoelker error1 |= 127;
82776ad65f7SSteve Price break;
82876ad65f7SSteve Price }
82976ad65f7SSteve Price }
83006a8a57fSJilles Tjoelker
83106a8a57fSJilles Tjoelker if (path != pathval())
832c059d822SJilles Tjoelker clearcmdentry();
83306a8a57fSJilles Tjoelker
834384aedabSJilles Tjoelker return error1;
83576ad65f7SSteve Price }
836b2f153feSStefan Farfeleder
837b2f153feSStefan Farfeleder /*
838b2f153feSStefan Farfeleder * Locate and print what a word is...
839b2f153feSStefan Farfeleder */
840b2f153feSStefan Farfeleder
841b2f153feSStefan Farfeleder int
typecmd(int argc,char ** argv)842b2f153feSStefan Farfeleder typecmd(int argc, char **argv)
843b2f153feSStefan Farfeleder {
84465519ccbSJilles Tjoelker if (argc > 2 && strcmp(argv[1], "--") == 0)
84565519ccbSJilles Tjoelker argc--, argv++;
8460fb60646SJilles Tjoelker return typecmd_impl(argc, argv, TYPECMD_TYPE, bltinlookup("PATH", 1));
847b2f153feSStefan Farfeleder }
848