exec.c revision 1.56 1 1.56 rillig /* $NetBSD: exec.c,v 1.56 2021/10/10 08:19:02 rillig Exp $ */
2 1.15 cgd
3 1.1 cgd /*-
4 1.8 jtc * Copyright (c) 1991, 1993
5 1.8 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.37 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.23 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.15 cgd #if 0
38 1.17 christos static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95";
39 1.15 cgd #else
40 1.56 rillig __RCSID("$NetBSD: exec.c,v 1.56 2021/10/10 08:19:02 rillig Exp $");
41 1.15 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.16 christos #include <sys/types.h>
45 1.16 christos #include <sys/stat.h>
46 1.33 christos #include <sys/wait.h>
47 1.16 christos #include <unistd.h>
48 1.16 christos #include <fcntl.h>
49 1.16 christos #include <errno.h>
50 1.33 christos #include <stdio.h>
51 1.16 christos #include <stdlib.h>
52 1.16 christos
53 1.1 cgd /*
54 1.1 cgd * When commands are first encountered, they are entered in a hash table.
55 1.1 cgd * This ensures that a full path search will not have to be done for them
56 1.1 cgd * on each invocation.
57 1.1 cgd *
58 1.1 cgd * We should investigate converting to a linear search, even though that
59 1.1 cgd * would make the command name "hash" a misnomer.
60 1.1 cgd */
61 1.1 cgd
62 1.1 cgd #include "shell.h"
63 1.1 cgd #include "main.h"
64 1.1 cgd #include "nodes.h"
65 1.1 cgd #include "parser.h"
66 1.1 cgd #include "redir.h"
67 1.1 cgd #include "eval.h"
68 1.1 cgd #include "exec.h"
69 1.1 cgd #include "builtins.h"
70 1.1 cgd #include "var.h"
71 1.1 cgd #include "options.h"
72 1.1 cgd #include "input.h"
73 1.1 cgd #include "output.h"
74 1.1 cgd #include "syntax.h"
75 1.1 cgd #include "memalloc.h"
76 1.1 cgd #include "error.h"
77 1.1 cgd #include "init.h"
78 1.1 cgd #include "mystring.h"
79 1.16 christos #include "show.h"
80 1.8 jtc #include "jobs.h"
81 1.22 christos #include "alias.h"
82 1.1 cgd
83 1.1 cgd
84 1.1 cgd #define CMDTABLESIZE 31 /* should be prime */
85 1.1 cgd #define ARB 1 /* actual size determined at run time */
86 1.1 cgd
87 1.1 cgd
88 1.1 cgd
89 1.1 cgd struct tblentry {
90 1.1 cgd struct tblentry *next; /* next entry in hash chain */
91 1.1 cgd union param param; /* definition of builtin function */
92 1.1 cgd short cmdtype; /* index identifying command */
93 1.1 cgd char rehash; /* if set, cd done since entry created */
94 1.49 kre char fn_ln1; /* for functions, LINENO from 1 */
95 1.49 kre int lineno; /* for functions abs LINENO of definition */
96 1.1 cgd char cmdname[ARB]; /* name of command */
97 1.1 cgd };
98 1.1 cgd
99 1.1 cgd
100 1.1 cgd STATIC struct tblentry *cmdtable[CMDTABLESIZE];
101 1.1 cgd STATIC int builtinloc = -1; /* index in path of %builtin, or -1 */
102 1.19 christos int exerrno = 0; /* Last exec error */
103 1.1 cgd
104 1.1 cgd
105 1.34 christos STATIC void tryexec(char *, char **, char **, int);
106 1.34 christos STATIC void printentry(struct tblentry *, int);
107 1.46 christos STATIC void addcmdentry(char *, struct cmdentry *);
108 1.34 christos STATIC void clearcmdentry(int);
109 1.34 christos STATIC struct tblentry *cmdlookup(const char *, int);
110 1.34 christos STATIC void delete_cmd_entry(void);
111 1.1 cgd
112 1.42 dholland #ifndef BSD
113 1.42 dholland STATIC void execinterp(char **, char **);
114 1.42 dholland #endif
115 1.42 dholland
116 1.1 cgd
117 1.41 matt extern const char *const parsekwd[];
118 1.1 cgd
119 1.1 cgd /*
120 1.1 cgd * Exec a program. Never returns. If you change this routine, you may
121 1.1 cgd * have to change the find_command routine as well.
122 1.1 cgd */
123 1.1 cgd
124 1.1 cgd void
125 1.34 christos shellexec(char **argv, char **envp, const char *path, int idx, int vforked)
126 1.12 cgd {
127 1.1 cgd char *cmdname;
128 1.1 cgd int e;
129 1.1 cgd
130 1.1 cgd if (strchr(argv[0], '/') != NULL) {
131 1.33 christos tryexec(argv[0], argv, envp, vforked);
132 1.1 cgd e = errno;
133 1.1 cgd } else {
134 1.1 cgd e = ENOENT;
135 1.48 kre while ((cmdname = padvance(&path, argv[0], 1)) != NULL) {
136 1.27 christos if (--idx < 0 && pathopt == NULL) {
137 1.33 christos tryexec(cmdname, argv, envp, vforked);
138 1.1 cgd if (errno != ENOENT && errno != ENOTDIR)
139 1.1 cgd e = errno;
140 1.1 cgd }
141 1.1 cgd stunalloc(cmdname);
142 1.1 cgd }
143 1.1 cgd }
144 1.19 christos
145 1.19 christos /* Map to POSIX errors */
146 1.19 christos switch (e) {
147 1.51 kre case EACCES: /* particularly this (unless no search perm) */
148 1.51 kre /*
149 1.51 kre * should perhaps check if this EACCES is an exec()
150 1.51 kre * EACESS or a namei() EACESS - the latter should be 127
151 1.51 kre * - but not today
152 1.51 kre */
153 1.51 kre case EINVAL: /* also explicitly these */
154 1.51 kre case ENOEXEC:
155 1.51 kre default: /* and anything else */
156 1.19 christos exerrno = 126;
157 1.19 christos break;
158 1.51 kre
159 1.51 kre case ENOENT: /* these are the "pathname lookup failed" errors */
160 1.51 kre case ELOOP:
161 1.51 kre case ENOTDIR:
162 1.51 kre case ENAMETOOLONG:
163 1.19 christos exerrno = 127;
164 1.19 christos break;
165 1.19 christos }
166 1.51 kre CTRACE(DBG_ERRS|DBG_CMDS|DBG_EVAL,
167 1.51 kre ("shellexec failed for %s, errno %d, vforked %d, suppressint %d\n",
168 1.51 kre argv[0], e, vforked, suppressint));
169 1.19 christos exerror(EXEXEC, "%s: %s", argv[0], errmsg(e, E_EXEC));
170 1.26 mycroft /* NOTREACHED */
171 1.1 cgd }
172 1.1 cgd
173 1.1 cgd
174 1.1 cgd STATIC void
175 1.34 christos tryexec(char *cmd, char **argv, char **envp, int vforked)
176 1.34 christos {
177 1.1 cgd int e;
178 1.16 christos #ifndef BSD
179 1.1 cgd char *p;
180 1.16 christos #endif
181 1.1 cgd
182 1.1 cgd #ifdef SYSV
183 1.1 cgd do {
184 1.1 cgd execve(cmd, argv, envp);
185 1.1 cgd } while (errno == EINTR);
186 1.1 cgd #else
187 1.1 cgd execve(cmd, argv, envp);
188 1.1 cgd #endif
189 1.1 cgd e = errno;
190 1.1 cgd if (e == ENOEXEC) {
191 1.33 christos if (vforked) {
192 1.33 christos /* We are currently vfork(2)ed, so raise an
193 1.33 christos * exception, and evalcommand will try again
194 1.33 christos * with a normal fork(2).
195 1.33 christos */
196 1.33 christos exraise(EXSHELLPROC);
197 1.33 christos }
198 1.40 christos #ifdef DEBUG
199 1.51 kre VTRACE(DBG_CMDS, ("execve(cmd=%s) returned ENOEXEC\n", cmd));
200 1.40 christos #endif
201 1.1 cgd initshellproc();
202 1.1 cgd setinputfile(cmd, 0);
203 1.1 cgd commandname = arg0 = savestr(argv[0]);
204 1.1 cgd #ifndef BSD
205 1.1 cgd pgetc(); pungetc(); /* fill up input buffer */
206 1.1 cgd p = parsenextc;
207 1.1 cgd if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
208 1.1 cgd argv[0] = cmd;
209 1.1 cgd execinterp(argv, envp);
210 1.1 cgd }
211 1.1 cgd #endif
212 1.1 cgd setparam(argv + 1);
213 1.1 cgd exraise(EXSHELLPROC);
214 1.1 cgd }
215 1.1 cgd errno = e;
216 1.1 cgd }
217 1.1 cgd
218 1.1 cgd
219 1.1 cgd #ifndef BSD
220 1.1 cgd /*
221 1.1 cgd * Execute an interpreter introduced by "#!", for systems where this
222 1.1 cgd * feature has not been built into the kernel. If the interpreter is
223 1.1 cgd * the shell, return (effectively ignoring the "#!"). If the execution
224 1.1 cgd * of the interpreter fails, exit.
225 1.1 cgd *
226 1.1 cgd * This code peeks inside the input buffer in order to avoid actually
227 1.1 cgd * reading any input. It would benefit from a rewrite.
228 1.1 cgd */
229 1.1 cgd
230 1.1 cgd #define NEWARGS 5
231 1.1 cgd
232 1.1 cgd STATIC void
233 1.34 christos execinterp(char **argv, char **envp)
234 1.34 christos {
235 1.1 cgd int n;
236 1.1 cgd char *inp;
237 1.1 cgd char *outp;
238 1.1 cgd char c;
239 1.1 cgd char *p;
240 1.1 cgd char **ap;
241 1.1 cgd char *newargs[NEWARGS];
242 1.1 cgd int i;
243 1.1 cgd char **ap2;
244 1.1 cgd char **new;
245 1.1 cgd
246 1.1 cgd n = parsenleft - 2;
247 1.1 cgd inp = parsenextc + 2;
248 1.1 cgd ap = newargs;
249 1.1 cgd for (;;) {
250 1.1 cgd while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
251 1.1 cgd inp++;
252 1.1 cgd if (n < 0)
253 1.1 cgd goto bad;
254 1.1 cgd if ((c = *inp++) == '\n')
255 1.1 cgd break;
256 1.1 cgd if (ap == &newargs[NEWARGS])
257 1.1 cgd bad: error("Bad #! line");
258 1.1 cgd STARTSTACKSTR(outp);
259 1.1 cgd do {
260 1.1 cgd STPUTC(c, outp);
261 1.1 cgd } while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n');
262 1.1 cgd STPUTC('\0', outp);
263 1.1 cgd n++, inp--;
264 1.1 cgd *ap++ = grabstackstr(outp);
265 1.1 cgd }
266 1.1 cgd if (ap == newargs + 1) { /* if no args, maybe no exec is needed */
267 1.1 cgd p = newargs[0];
268 1.1 cgd for (;;) {
269 1.1 cgd if (equal(p, "sh") || equal(p, "ash")) {
270 1.1 cgd return;
271 1.1 cgd }
272 1.1 cgd while (*p != '/') {
273 1.1 cgd if (*p == '\0')
274 1.1 cgd goto break2;
275 1.1 cgd p++;
276 1.1 cgd }
277 1.1 cgd p++;
278 1.1 cgd }
279 1.1 cgd break2:;
280 1.1 cgd }
281 1.1 cgd i = (char *)ap - (char *)newargs; /* size in bytes */
282 1.1 cgd if (i == 0)
283 1.1 cgd error("Bad #! line");
284 1.1 cgd for (ap2 = argv ; *ap2++ != NULL ; );
285 1.1 cgd new = ckmalloc(i + ((char *)ap2 - (char *)argv));
286 1.1 cgd ap = newargs, ap2 = new;
287 1.1 cgd while ((i -= sizeof (char **)) >= 0)
288 1.1 cgd *ap2++ = *ap++;
289 1.1 cgd ap = argv;
290 1.1 cgd while (*ap2++ = *ap++);
291 1.1 cgd shellexec(new, envp, pathval(), 0);
292 1.26 mycroft /* NOTREACHED */
293 1.1 cgd }
294 1.1 cgd #endif
295 1.1 cgd
296 1.1 cgd
297 1.1 cgd
298 1.1 cgd /*
299 1.1 cgd * Do a path search. The variable path (passed by reference) should be
300 1.1 cgd * set to the start of the path before the first call; padvance will update
301 1.1 cgd * this value as it proceeds. Successive calls to padvance will return
302 1.1 cgd * the possible path expansions in sequence. If an option (indicated by
303 1.1 cgd * a percent sign) appears in the path entry then the global variable
304 1.1 cgd * pathopt will be set to point to it; otherwise pathopt will be set to
305 1.1 cgd * NULL.
306 1.1 cgd */
307 1.1 cgd
308 1.27 christos const char *pathopt;
309 1.1 cgd
310 1.1 cgd char *
311 1.48 kre padvance(const char **path, const char *name, int magic_percent)
312 1.34 christos {
313 1.27 christos const char *p;
314 1.27 christos char *q;
315 1.27 christos const char *start;
316 1.1 cgd int len;
317 1.1 cgd
318 1.1 cgd if (*path == NULL)
319 1.1 cgd return NULL;
320 1.48 kre if (magic_percent)
321 1.48 kre magic_percent = '%';
322 1.48 kre
323 1.1 cgd start = *path;
324 1.48 kre for (p = start ; *p && *p != ':' && *p != magic_percent ; p++)
325 1.48 kre ;
326 1.1 cgd len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */
327 1.1 cgd while (stackblocksize() < len)
328 1.1 cgd growstackblock();
329 1.1 cgd q = stackblock();
330 1.1 cgd if (p != start) {
331 1.11 mycroft memcpy(q, start, p - start);
332 1.1 cgd q += p - start;
333 1.48 kre if (q[-1] != '/')
334 1.48 kre *q++ = '/';
335 1.1 cgd }
336 1.1 cgd strcpy(q, name);
337 1.1 cgd pathopt = NULL;
338 1.48 kre if (*p == magic_percent) {
339 1.1 cgd pathopt = ++p;
340 1.48 kre while (*p && *p != ':')
341 1.48 kre p++;
342 1.1 cgd }
343 1.1 cgd if (*p == ':')
344 1.1 cgd *path = p + 1;
345 1.1 cgd else
346 1.1 cgd *path = NULL;
347 1.50 kre return grabstackstr(q + strlen(name) + 1);
348 1.1 cgd }
349 1.1 cgd
350 1.1 cgd
351 1.1 cgd /*** Command hashing code ***/
352 1.1 cgd
353 1.1 cgd
354 1.12 cgd int
355 1.34 christos hashcmd(int argc, char **argv)
356 1.12 cgd {
357 1.1 cgd struct tblentry **pp;
358 1.1 cgd struct tblentry *cmdp;
359 1.1 cgd int c;
360 1.1 cgd struct cmdentry entry;
361 1.1 cgd char *name;
362 1.47 kre int allopt=0, bopt=0, fopt=0, ropt=0, sopt=0, uopt=0, verbose=0;
363 1.1 cgd
364 1.47 kre while ((c = nextopt("bcfrsuv")) != '\0')
365 1.47 kre switch (c) {
366 1.47 kre case 'b': bopt = 1; break;
367 1.47 kre case 'c': uopt = 1; break; /* c == u */
368 1.47 kre case 'f': fopt = 1; break;
369 1.47 kre case 'r': ropt = 1; break;
370 1.47 kre case 's': sopt = 1; break;
371 1.47 kre case 'u': uopt = 1; break;
372 1.47 kre case 'v': verbose = 1; break;
373 1.1 cgd }
374 1.47 kre
375 1.47 kre if (ropt)
376 1.47 kre clearcmdentry(0);
377 1.47 kre
378 1.47 kre if (bopt == 0 && fopt == 0 && sopt == 0 && uopt == 0)
379 1.47 kre allopt = bopt = fopt = sopt = uopt = 1;
380 1.47 kre
381 1.8 jtc if (*argptr == NULL) {
382 1.8 jtc for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
383 1.8 jtc for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
384 1.47 kre switch (cmdp->cmdtype) {
385 1.47 kre case CMDNORMAL:
386 1.47 kre if (!uopt)
387 1.47 kre continue;
388 1.47 kre break;
389 1.47 kre case CMDBUILTIN:
390 1.47 kre if (!bopt)
391 1.47 kre continue;
392 1.47 kre break;
393 1.47 kre case CMDSPLBLTIN:
394 1.47 kre if (!sopt)
395 1.47 kre continue;
396 1.47 kre break;
397 1.47 kre case CMDFUNCTION:
398 1.47 kre if (!fopt)
399 1.47 kre continue;
400 1.47 kre break;
401 1.47 kre default: /* never happens */
402 1.47 kre continue;
403 1.47 kre }
404 1.47 kre if (!allopt || verbose ||
405 1.47 kre cmdp->cmdtype == CMDNORMAL)
406 1.36 dsl printentry(cmdp, verbose);
407 1.8 jtc }
408 1.8 jtc }
409 1.8 jtc return 0;
410 1.8 jtc }
411 1.47 kre
412 1.47 kre while ((name = *argptr++) != NULL) {
413 1.47 kre if ((cmdp = cmdlookup(name, 0)) != NULL) {
414 1.47 kre switch (cmdp->cmdtype) {
415 1.47 kre case CMDNORMAL:
416 1.47 kre if (!uopt)
417 1.47 kre continue;
418 1.47 kre delete_cmd_entry();
419 1.47 kre break;
420 1.47 kre case CMDBUILTIN:
421 1.47 kre if (!bopt)
422 1.47 kre continue;
423 1.47 kre if (builtinloc >= 0)
424 1.47 kre delete_cmd_entry();
425 1.47 kre break;
426 1.47 kre case CMDSPLBLTIN:
427 1.47 kre if (!sopt)
428 1.47 kre continue;
429 1.47 kre break;
430 1.47 kre case CMDFUNCTION:
431 1.47 kre if (!fopt)
432 1.47 kre continue;
433 1.47 kre break;
434 1.47 kre }
435 1.47 kre }
436 1.24 christos find_command(name, &entry, DO_ERR, pathval());
437 1.1 cgd if (verbose) {
438 1.1 cgd if (entry.cmdtype != CMDUNKNOWN) { /* if no error msg */
439 1.1 cgd cmdp = cmdlookup(name, 0);
440 1.38 christos if (cmdp != NULL)
441 1.38 christos printentry(cmdp, verbose);
442 1.1 cgd }
443 1.1 cgd flushall();
444 1.1 cgd }
445 1.1 cgd }
446 1.1 cgd return 0;
447 1.1 cgd }
448 1.1 cgd
449 1.1 cgd STATIC void
450 1.34 christos printentry(struct tblentry *cmdp, int verbose)
451 1.34 christos {
452 1.27 christos int idx;
453 1.27 christos const char *path;
454 1.1 cgd char *name;
455 1.1 cgd
456 1.34 christos switch (cmdp->cmdtype) {
457 1.34 christos case CMDNORMAL:
458 1.27 christos idx = cmdp->param.index;
459 1.1 cgd path = pathval();
460 1.1 cgd do {
461 1.48 kre name = padvance(&path, cmdp->cmdname, 1);
462 1.1 cgd stunalloc(name);
463 1.27 christos } while (--idx >= 0);
464 1.47 kre if (verbose)
465 1.47 kre out1fmt("Command from PATH[%d]: ",
466 1.47 kre cmdp->param.index);
467 1.1 cgd out1str(name);
468 1.34 christos break;
469 1.34 christos case CMDSPLBLTIN:
470 1.47 kre if (verbose)
471 1.47 kre out1str("special ");
472 1.47 kre /* FALLTHROUGH */
473 1.34 christos case CMDBUILTIN:
474 1.47 kre if (verbose)
475 1.47 kre out1str("builtin ");
476 1.47 kre out1fmt("%s", cmdp->cmdname);
477 1.34 christos break;
478 1.34 christos case CMDFUNCTION:
479 1.47 kre if (verbose)
480 1.47 kre out1str("function ");
481 1.47 kre out1fmt("%s", cmdp->cmdname);
482 1.8 jtc if (verbose) {
483 1.34 christos struct procstat ps;
484 1.52 kre
485 1.8 jtc INTOFF;
486 1.52 kre commandtext(&ps, getfuncnode(cmdp->param.func));
487 1.36 dsl INTON;
488 1.36 dsl out1str("() { ");
489 1.34 christos out1str(ps.cmd);
490 1.36 dsl out1str("; }");
491 1.8 jtc }
492 1.34 christos break;
493 1.34 christos default:
494 1.47 kre error("internal error: %s cmdtype %d",
495 1.47 kre cmdp->cmdname, cmdp->cmdtype);
496 1.1 cgd }
497 1.1 cgd if (cmdp->rehash)
498 1.1 cgd out1c('*');
499 1.1 cgd out1c('\n');
500 1.1 cgd }
501 1.1 cgd
502 1.1 cgd
503 1.1 cgd
504 1.1 cgd /*
505 1.1 cgd * Resolve a command name. If you change this routine, you may have to
506 1.1 cgd * change the shellexec routine as well.
507 1.1 cgd */
508 1.1 cgd
509 1.1 cgd void
510 1.34 christos find_command(char *name, struct cmdentry *entry, int act, const char *path)
511 1.12 cgd {
512 1.35 dsl struct tblentry *cmdp, loc_cmd;
513 1.27 christos int idx;
514 1.1 cgd int prev;
515 1.1 cgd char *fullname;
516 1.1 cgd struct stat statb;
517 1.1 cgd int e;
518 1.34 christos int (*bltin)(int,char **);
519 1.1 cgd
520 1.35 dsl /* If name contains a slash, don't use PATH or hash table */
521 1.1 cgd if (strchr(name, '/') != NULL) {
522 1.24 christos if (act & DO_ABS) {
523 1.24 christos while (stat(name, &statb) < 0) {
524 1.34 christos #ifdef SYSV
525 1.24 christos if (errno == EINTR)
526 1.24 christos continue;
527 1.34 christos #endif
528 1.24 christos if (errno != ENOENT && errno != ENOTDIR)
529 1.24 christos e = errno;
530 1.24 christos entry->cmdtype = CMDUNKNOWN;
531 1.24 christos entry->u.index = -1;
532 1.24 christos return;
533 1.24 christos }
534 1.24 christos entry->cmdtype = CMDNORMAL;
535 1.24 christos entry->u.index = -1;
536 1.24 christos return;
537 1.24 christos }
538 1.1 cgd entry->cmdtype = CMDNORMAL;
539 1.1 cgd entry->u.index = 0;
540 1.1 cgd return;
541 1.1 cgd }
542 1.1 cgd
543 1.35 dsl if (path != pathval())
544 1.35 dsl act |= DO_ALTPATH;
545 1.35 dsl
546 1.35 dsl if (act & DO_ALTPATH && strstr(path, "%builtin") != NULL)
547 1.35 dsl act |= DO_ALTBLTIN;
548 1.35 dsl
549 1.35 dsl /* If name is in the table, check answer will be ok */
550 1.35 dsl if ((cmdp = cmdlookup(name, 0)) != NULL) {
551 1.56 rillig switch (cmdp->cmdtype) {
552 1.56 rillig case CMDNORMAL:
553 1.56 rillig if (act & DO_ALTPATH)
554 1.56 rillig cmdp = NULL;
555 1.56 rillig break;
556 1.56 rillig case CMDFUNCTION:
557 1.56 rillig if (act & DO_NOFUNC)
558 1.56 rillig cmdp = NULL;
559 1.56 rillig break;
560 1.56 rillig case CMDBUILTIN:
561 1.56 rillig if ((act & DO_ALTBLTIN) || builtinloc >= 0)
562 1.56 rillig cmdp = NULL;
563 1.56 rillig break;
564 1.56 rillig }
565 1.56 rillig /* if not invalidated by cd, we're done */
566 1.56 rillig if (cmdp != NULL && cmdp->rehash == 0)
567 1.56 rillig goto success;
568 1.35 dsl }
569 1.1 cgd
570 1.1 cgd /* If %builtin not in path, check for builtin next */
571 1.35 dsl if ((act & DO_ALTPATH ? !(act & DO_ALTBLTIN) : builtinloc < 0) &&
572 1.35 dsl (bltin = find_builtin(name)) != 0)
573 1.35 dsl goto builtin_success;
574 1.1 cgd
575 1.1 cgd /* We have to search path. */
576 1.1 cgd prev = -1; /* where to start */
577 1.1 cgd if (cmdp) { /* doing a rehash */
578 1.1 cgd if (cmdp->cmdtype == CMDBUILTIN)
579 1.1 cgd prev = builtinloc;
580 1.1 cgd else
581 1.1 cgd prev = cmdp->param.index;
582 1.1 cgd }
583 1.1 cgd
584 1.1 cgd e = ENOENT;
585 1.27 christos idx = -1;
586 1.1 cgd loop:
587 1.48 kre while ((fullname = padvance(&path, name, 1)) != NULL) {
588 1.1 cgd stunalloc(fullname);
589 1.27 christos idx++;
590 1.1 cgd if (pathopt) {
591 1.1 cgd if (prefix("builtin", pathopt)) {
592 1.34 christos if ((bltin = find_builtin(name)) == 0)
593 1.1 cgd goto loop;
594 1.35 dsl goto builtin_success;
595 1.1 cgd } else if (prefix("func", pathopt)) {
596 1.1 cgd /* handled below */
597 1.1 cgd } else {
598 1.35 dsl /* ignore unimplemented options */
599 1.35 dsl goto loop;
600 1.1 cgd }
601 1.1 cgd }
602 1.1 cgd /* if rehash, don't redo absolute path names */
603 1.27 christos if (fullname[0] == '/' && idx <= prev) {
604 1.27 christos if (idx < prev)
605 1.1 cgd goto loop;
606 1.51 kre VTRACE(DBG_CMDS, ("searchexec \"%s\": no change\n",
607 1.51 kre name));
608 1.1 cgd goto success;
609 1.1 cgd }
610 1.1 cgd while (stat(fullname, &statb) < 0) {
611 1.1 cgd #ifdef SYSV
612 1.1 cgd if (errno == EINTR)
613 1.1 cgd continue;
614 1.1 cgd #endif
615 1.1 cgd if (errno != ENOENT && errno != ENOTDIR)
616 1.1 cgd e = errno;
617 1.1 cgd goto loop;
618 1.1 cgd }
619 1.1 cgd e = EACCES; /* if we fail, this will be the error */
620 1.14 mycroft if (!S_ISREG(statb.st_mode))
621 1.1 cgd goto loop;
622 1.1 cgd if (pathopt) { /* this is a %func directory */
623 1.50 kre char *endname;
624 1.50 kre
625 1.35 dsl if (act & DO_NOFUNC)
626 1.35 dsl goto loop;
627 1.50 kre endname = fullname + strlen(fullname) + 1;
628 1.50 kre grabstackstr(endname);
629 1.1 cgd readcmdfile(fullname);
630 1.35 dsl if ((cmdp = cmdlookup(name, 0)) == NULL ||
631 1.35 dsl cmdp->cmdtype != CMDFUNCTION)
632 1.1 cgd error("%s not defined in %s", name, fullname);
633 1.50 kre ungrabstackstr(fullname, endname);
634 1.1 cgd goto success;
635 1.1 cgd }
636 1.8 jtc #ifdef notdef
637 1.35 dsl /* XXX this code stops root executing stuff, and is buggy
638 1.35 dsl if you need a group from the group list. */
639 1.8 jtc if (statb.st_uid == geteuid()) {
640 1.1 cgd if ((statb.st_mode & 0100) == 0)
641 1.1 cgd goto loop;
642 1.1 cgd } else if (statb.st_gid == getegid()) {
643 1.1 cgd if ((statb.st_mode & 010) == 0)
644 1.1 cgd goto loop;
645 1.1 cgd } else {
646 1.8 jtc if ((statb.st_mode & 01) == 0)
647 1.1 cgd goto loop;
648 1.1 cgd }
649 1.4 cgd #endif
650 1.51 kre VTRACE(DBG_CMDS, ("searchexec \"%s\" returns \"%s\"\n", name,
651 1.51 kre fullname));
652 1.1 cgd INTOFF;
653 1.35 dsl if (act & DO_ALTPATH) {
654 1.50 kre /*
655 1.50 kre * this should be a grabstackstr() but is not needed:
656 1.50 kre * fullname is no longer needed for anything
657 1.35 dsl stalloc(strlen(fullname) + 1);
658 1.50 kre */
659 1.35 dsl cmdp = &loc_cmd;
660 1.35 dsl } else
661 1.35 dsl cmdp = cmdlookup(name, 1);
662 1.54 kre
663 1.54 kre if (cmdp->cmdtype == CMDFUNCTION)
664 1.54 kre cmdp = &loc_cmd;
665 1.54 kre
666 1.1 cgd cmdp->cmdtype = CMDNORMAL;
667 1.27 christos cmdp->param.index = idx;
668 1.1 cgd INTON;
669 1.1 cgd goto success;
670 1.1 cgd }
671 1.1 cgd
672 1.1 cgd /* We failed. If there was an entry for this command, delete it */
673 1.1 cgd if (cmdp)
674 1.1 cgd delete_cmd_entry();
675 1.24 christos if (act & DO_ERR)
676 1.20 abrown outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC));
677 1.1 cgd entry->cmdtype = CMDUNKNOWN;
678 1.55 kre entry->u.index = idx + 1;
679 1.1 cgd return;
680 1.1 cgd
681 1.35 dsl builtin_success:
682 1.35 dsl INTOFF;
683 1.35 dsl if (act & DO_ALTPATH)
684 1.35 dsl cmdp = &loc_cmd;
685 1.35 dsl else
686 1.35 dsl cmdp = cmdlookup(name, 1);
687 1.36 dsl if (cmdp->cmdtype == CMDFUNCTION)
688 1.36 dsl /* DO_NOFUNC must have been set */
689 1.36 dsl cmdp = &loc_cmd;
690 1.35 dsl cmdp->cmdtype = CMDBUILTIN;
691 1.35 dsl cmdp->param.bltin = bltin;
692 1.35 dsl INTON;
693 1.1 cgd success:
694 1.39 christos if (cmdp) {
695 1.39 christos cmdp->rehash = 0;
696 1.39 christos entry->cmdtype = cmdp->cmdtype;
697 1.49 kre entry->lineno = cmdp->lineno;
698 1.49 kre entry->lno_frel = cmdp->fn_ln1;
699 1.39 christos entry->u = cmdp->param;
700 1.55 kre } else {
701 1.39 christos entry->cmdtype = CMDUNKNOWN;
702 1.55 kre entry->u.index = -1;
703 1.55 kre }
704 1.1 cgd }
705 1.1 cgd
706 1.1 cgd
707 1.1 cgd
708 1.1 cgd /*
709 1.1 cgd * Search the table of builtin commands.
710 1.1 cgd */
711 1.1 cgd
712 1.1 cgd int
713 1.43 matt (*find_builtin(char *name))(int, char **)
714 1.12 cgd {
715 1.21 tls const struct builtincmd *bp;
716 1.1 cgd
717 1.1 cgd for (bp = builtincmd ; bp->name ; bp++) {
718 1.44 dsl if (*bp->name == *name
719 1.44 dsl && (*name == '%' || equal(bp->name, name)))
720 1.34 christos return bp->builtin;
721 1.34 christos }
722 1.34 christos return 0;
723 1.34 christos }
724 1.34 christos
725 1.34 christos int
726 1.43 matt (*find_splbltin(char *name))(int, char **)
727 1.34 christos {
728 1.34 christos const struct builtincmd *bp;
729 1.34 christos
730 1.34 christos for (bp = splbltincmd ; bp->name ; bp++) {
731 1.34 christos if (*bp->name == *name && equal(bp->name, name))
732 1.34 christos return bp->builtin;
733 1.34 christos }
734 1.34 christos return 0;
735 1.34 christos }
736 1.34 christos
737 1.34 christos /*
738 1.34 christos * At shell startup put special builtins into hash table.
739 1.34 christos * ensures they are executed first (see posix).
740 1.34 christos * We stop functions being added with the same name
741 1.34 christos * (as they are impossible to call)
742 1.34 christos */
743 1.34 christos
744 1.34 christos void
745 1.34 christos hash_special_builtins(void)
746 1.34 christos {
747 1.34 christos const struct builtincmd *bp;
748 1.34 christos struct tblentry *cmdp;
749 1.34 christos
750 1.34 christos for (bp = splbltincmd ; bp->name ; bp++) {
751 1.34 christos cmdp = cmdlookup(bp->name, 1);
752 1.34 christos cmdp->cmdtype = CMDSPLBLTIN;
753 1.34 christos cmdp->param.bltin = bp->builtin;
754 1.1 cgd }
755 1.1 cgd }
756 1.1 cgd
757 1.1 cgd
758 1.1 cgd
759 1.1 cgd /*
760 1.1 cgd * Called when a cd is done. Marks all commands so the next time they
761 1.1 cgd * are executed they will be rehashed.
762 1.1 cgd */
763 1.1 cgd
764 1.1 cgd void
765 1.34 christos hashcd(void)
766 1.34 christos {
767 1.1 cgd struct tblentry **pp;
768 1.1 cgd struct tblentry *cmdp;
769 1.1 cgd
770 1.1 cgd for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
771 1.1 cgd for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
772 1.1 cgd if (cmdp->cmdtype == CMDNORMAL
773 1.16 christos || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
774 1.1 cgd cmdp->rehash = 1;
775 1.1 cgd }
776 1.1 cgd }
777 1.1 cgd }
778 1.1 cgd
779 1.1 cgd
780 1.1 cgd
781 1.1 cgd /*
782 1.35 dsl * Fix command hash table when PATH changed.
783 1.1 cgd * Called before PATH is changed. The argument is the new value of PATH;
784 1.35 dsl * pathval() still returns the old value at this point.
785 1.35 dsl * Called with interrupts off.
786 1.1 cgd */
787 1.1 cgd
788 1.1 cgd void
789 1.34 christos changepath(const char *newval)
790 1.13 mycroft {
791 1.18 christos const char *old, *new;
792 1.27 christos int idx;
793 1.1 cgd int firstchange;
794 1.1 cgd int bltin;
795 1.1 cgd
796 1.1 cgd old = pathval();
797 1.1 cgd new = newval;
798 1.1 cgd firstchange = 9999; /* assume no change */
799 1.27 christos idx = 0;
800 1.1 cgd bltin = -1;
801 1.1 cgd for (;;) {
802 1.1 cgd if (*old != *new) {
803 1.27 christos firstchange = idx;
804 1.16 christos if ((*old == '\0' && *new == ':')
805 1.16 christos || (*old == ':' && *new == '\0'))
806 1.1 cgd firstchange++;
807 1.1 cgd old = new; /* ignore subsequent differences */
808 1.1 cgd }
809 1.1 cgd if (*new == '\0')
810 1.1 cgd break;
811 1.1 cgd if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
812 1.27 christos bltin = idx;
813 1.1 cgd if (*new == ':') {
814 1.27 christos idx++;
815 1.1 cgd }
816 1.1 cgd new++, old++;
817 1.1 cgd }
818 1.1 cgd if (builtinloc < 0 && bltin >= 0)
819 1.1 cgd builtinloc = bltin; /* zap builtins */
820 1.1 cgd if (builtinloc >= 0 && bltin < 0)
821 1.1 cgd firstchange = 0;
822 1.1 cgd clearcmdentry(firstchange);
823 1.1 cgd builtinloc = bltin;
824 1.1 cgd }
825 1.1 cgd
826 1.1 cgd
827 1.1 cgd /*
828 1.1 cgd * Clear out command entries. The argument specifies the first entry in
829 1.1 cgd * PATH which has changed.
830 1.1 cgd */
831 1.1 cgd
832 1.1 cgd STATIC void
833 1.34 christos clearcmdentry(int firstchange)
834 1.12 cgd {
835 1.1 cgd struct tblentry **tblp;
836 1.1 cgd struct tblentry **pp;
837 1.1 cgd struct tblentry *cmdp;
838 1.1 cgd
839 1.1 cgd INTOFF;
840 1.1 cgd for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
841 1.1 cgd pp = tblp;
842 1.1 cgd while ((cmdp = *pp) != NULL) {
843 1.16 christos if ((cmdp->cmdtype == CMDNORMAL &&
844 1.16 christos cmdp->param.index >= firstchange)
845 1.16 christos || (cmdp->cmdtype == CMDBUILTIN &&
846 1.16 christos builtinloc >= firstchange)) {
847 1.1 cgd *pp = cmdp->next;
848 1.1 cgd ckfree(cmdp);
849 1.1 cgd } else {
850 1.1 cgd pp = &cmdp->next;
851 1.1 cgd }
852 1.1 cgd }
853 1.1 cgd }
854 1.1 cgd INTON;
855 1.1 cgd }
856 1.1 cgd
857 1.1 cgd
858 1.1 cgd /*
859 1.1 cgd * Delete all functions.
860 1.1 cgd */
861 1.1 cgd
862 1.1 cgd #ifdef mkinit
863 1.34 christos MKINIT void deletefuncs(void);
864 1.34 christos MKINIT void hash_special_builtins(void);
865 1.34 christos
866 1.34 christos INIT {
867 1.34 christos hash_special_builtins();
868 1.34 christos }
869 1.1 cgd
870 1.1 cgd SHELLPROC {
871 1.1 cgd deletefuncs();
872 1.1 cgd }
873 1.1 cgd #endif
874 1.1 cgd
875 1.1 cgd void
876 1.34 christos deletefuncs(void)
877 1.34 christos {
878 1.1 cgd struct tblentry **tblp;
879 1.1 cgd struct tblentry **pp;
880 1.1 cgd struct tblentry *cmdp;
881 1.1 cgd
882 1.1 cgd INTOFF;
883 1.1 cgd for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
884 1.1 cgd pp = tblp;
885 1.1 cgd while ((cmdp = *pp) != NULL) {
886 1.1 cgd if (cmdp->cmdtype == CMDFUNCTION) {
887 1.1 cgd *pp = cmdp->next;
888 1.1 cgd freefunc(cmdp->param.func);
889 1.1 cgd ckfree(cmdp);
890 1.1 cgd } else {
891 1.1 cgd pp = &cmdp->next;
892 1.1 cgd }
893 1.1 cgd }
894 1.1 cgd }
895 1.1 cgd INTON;
896 1.1 cgd }
897 1.1 cgd
898 1.1 cgd
899 1.1 cgd
900 1.1 cgd /*
901 1.1 cgd * Locate a command in the command hash table. If "add" is nonzero,
902 1.1 cgd * add the command to the table if it is not already present. The
903 1.1 cgd * variable "lastcmdentry" is set to point to the address of the link
904 1.1 cgd * pointing to the entry, so that delete_cmd_entry can delete the
905 1.1 cgd * entry.
906 1.1 cgd */
907 1.1 cgd
908 1.1 cgd struct tblentry **lastcmdentry;
909 1.1 cgd
910 1.1 cgd
911 1.1 cgd STATIC struct tblentry *
912 1.34 christos cmdlookup(const char *name, int add)
913 1.12 cgd {
914 1.1 cgd int hashval;
915 1.34 christos const char *p;
916 1.1 cgd struct tblentry *cmdp;
917 1.1 cgd struct tblentry **pp;
918 1.1 cgd
919 1.1 cgd p = name;
920 1.1 cgd hashval = *p << 4;
921 1.1 cgd while (*p)
922 1.1 cgd hashval += *p++;
923 1.1 cgd hashval &= 0x7FFF;
924 1.1 cgd pp = &cmdtable[hashval % CMDTABLESIZE];
925 1.1 cgd for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
926 1.1 cgd if (equal(cmdp->cmdname, name))
927 1.1 cgd break;
928 1.1 cgd pp = &cmdp->next;
929 1.1 cgd }
930 1.1 cgd if (add && cmdp == NULL) {
931 1.1 cgd INTOFF;
932 1.1 cgd cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
933 1.1 cgd + strlen(name) + 1);
934 1.1 cgd cmdp->next = NULL;
935 1.1 cgd cmdp->cmdtype = CMDUNKNOWN;
936 1.1 cgd cmdp->rehash = 0;
937 1.1 cgd strcpy(cmdp->cmdname, name);
938 1.1 cgd INTON;
939 1.1 cgd }
940 1.1 cgd lastcmdentry = pp;
941 1.1 cgd return cmdp;
942 1.1 cgd }
943 1.1 cgd
944 1.1 cgd /*
945 1.1 cgd * Delete the command entry returned on the last lookup.
946 1.1 cgd */
947 1.1 cgd
948 1.1 cgd STATIC void
949 1.34 christos delete_cmd_entry(void)
950 1.34 christos {
951 1.1 cgd struct tblentry *cmdp;
952 1.1 cgd
953 1.1 cgd INTOFF;
954 1.1 cgd cmdp = *lastcmdentry;
955 1.1 cgd *lastcmdentry = cmdp->next;
956 1.1 cgd ckfree(cmdp);
957 1.1 cgd INTON;
958 1.1 cgd }
959 1.1 cgd
960 1.1 cgd
961 1.1 cgd
962 1.1 cgd #ifdef notdef
963 1.1 cgd void
964 1.34 christos getcmdentry(char *name, struct cmdentry *entry)
965 1.34 christos {
966 1.1 cgd struct tblentry *cmdp = cmdlookup(name, 0);
967 1.1 cgd
968 1.1 cgd if (cmdp) {
969 1.1 cgd entry->u = cmdp->param;
970 1.1 cgd entry->cmdtype = cmdp->cmdtype;
971 1.1 cgd } else {
972 1.1 cgd entry->cmdtype = CMDUNKNOWN;
973 1.1 cgd entry->u.index = 0;
974 1.1 cgd }
975 1.1 cgd }
976 1.1 cgd #endif
977 1.1 cgd
978 1.1 cgd
979 1.1 cgd /*
980 1.1 cgd * Add a new command entry, replacing any existing command entry for
981 1.34 christos * the same name - except special builtins.
982 1.1 cgd */
983 1.1 cgd
984 1.35 dsl STATIC void
985 1.34 christos addcmdentry(char *name, struct cmdentry *entry)
986 1.34 christos {
987 1.1 cgd struct tblentry *cmdp;
988 1.1 cgd
989 1.1 cgd INTOFF;
990 1.1 cgd cmdp = cmdlookup(name, 1);
991 1.34 christos if (cmdp->cmdtype != CMDSPLBLTIN) {
992 1.52 kre if (cmdp->cmdtype == CMDFUNCTION)
993 1.52 kre unreffunc(cmdp->param.func);
994 1.34 christos cmdp->cmdtype = entry->cmdtype;
995 1.49 kre cmdp->lineno = entry->lineno;
996 1.49 kre cmdp->fn_ln1 = entry->lno_frel;
997 1.34 christos cmdp->param = entry->u;
998 1.1 cgd }
999 1.1 cgd INTON;
1000 1.1 cgd }
1001 1.1 cgd
1002 1.1 cgd
1003 1.1 cgd /*
1004 1.1 cgd * Define a shell function.
1005 1.1 cgd */
1006 1.1 cgd
1007 1.1 cgd void
1008 1.49 kre defun(char *name, union node *func, int lineno)
1009 1.34 christos {
1010 1.1 cgd struct cmdentry entry;
1011 1.1 cgd
1012 1.1 cgd INTOFF;
1013 1.1 cgd entry.cmdtype = CMDFUNCTION;
1014 1.49 kre entry.lineno = lineno;
1015 1.49 kre entry.lno_frel = fnline1;
1016 1.1 cgd entry.u.func = copyfunc(func);
1017 1.1 cgd addcmdentry(name, &entry);
1018 1.1 cgd INTON;
1019 1.1 cgd }
1020 1.1 cgd
1021 1.1 cgd
1022 1.1 cgd /*
1023 1.1 cgd * Delete a function if it exists.
1024 1.1 cgd */
1025 1.1 cgd
1026 1.8 jtc int
1027 1.34 christos unsetfunc(char *name)
1028 1.34 christos {
1029 1.1 cgd struct tblentry *cmdp;
1030 1.1 cgd
1031 1.35 dsl if ((cmdp = cmdlookup(name, 0)) != NULL &&
1032 1.35 dsl cmdp->cmdtype == CMDFUNCTION) {
1033 1.52 kre unreffunc(cmdp->param.func);
1034 1.1 cgd delete_cmd_entry();
1035 1.1 cgd }
1036 1.45 christos return 0;
1037 1.22 christos }
1038 1.22 christos
1039 1.22 christos /*
1040 1.22 christos * Locate and print what a word is...
1041 1.35 dsl * also used for 'command -[v|V]'
1042 1.22 christos */
1043 1.22 christos
1044 1.22 christos int
1045 1.34 christos typecmd(int argc, char **argv)
1046 1.22 christos {
1047 1.22 christos struct cmdentry entry;
1048 1.22 christos struct tblentry *cmdp;
1049 1.41 matt const char * const *pp;
1050 1.22 christos struct alias *ap;
1051 1.27 christos int err = 0;
1052 1.35 dsl char *arg;
1053 1.35 dsl int c;
1054 1.35 dsl int V_flag = 0;
1055 1.35 dsl int v_flag = 0;
1056 1.35 dsl int p_flag = 0;
1057 1.22 christos
1058 1.35 dsl while ((c = nextopt("vVp")) != 0) {
1059 1.35 dsl switch (c) {
1060 1.35 dsl case 'v': v_flag = 1; break;
1061 1.35 dsl case 'V': V_flag = 1; break;
1062 1.35 dsl case 'p': p_flag = 1; break;
1063 1.35 dsl }
1064 1.35 dsl }
1065 1.35 dsl
1066 1.53 kre if (argv[0][0] != 'c' && v_flag | V_flag | p_flag)
1067 1.53 kre error("usage: %s name...", argv[0]);
1068 1.53 kre
1069 1.53 kre if (v_flag && V_flag)
1070 1.53 kre error("-v and -V cannot both be specified");
1071 1.53 kre
1072 1.53 kre if (*argptr == NULL)
1073 1.53 kre error("usage: %s%s name ...", argv[0],
1074 1.53 kre argv[0][0] == 'c' ? " [-p] [-v|-V]" : "");
1075 1.35 dsl
1076 1.35 dsl while ((arg = *argptr++)) {
1077 1.35 dsl if (!v_flag)
1078 1.35 dsl out1str(arg);
1079 1.22 christos /* First look at the keywords */
1080 1.30 matt for (pp = parsekwd; *pp; pp++)
1081 1.35 dsl if (**pp == *arg && equal(*pp, arg))
1082 1.22 christos break;
1083 1.22 christos
1084 1.22 christos if (*pp) {
1085 1.35 dsl if (v_flag)
1086 1.53 kre out1fmt("%s\n", arg);
1087 1.35 dsl else
1088 1.35 dsl out1str(" is a shell keyword\n");
1089 1.22 christos continue;
1090 1.22 christos }
1091 1.22 christos
1092 1.22 christos /* Then look at the aliases */
1093 1.35 dsl if ((ap = lookupalias(arg, 1)) != NULL) {
1094 1.53 kre int ml = 0;
1095 1.53 kre
1096 1.53 kre if (!v_flag) {
1097 1.53 kre out1str(" is an alias ");
1098 1.53 kre if (strchr(ap->val, '\n')) {
1099 1.53 kre out1str("(multiline)...\n");
1100 1.53 kre ml = 1;
1101 1.53 kre } else
1102 1.53 kre out1str("for: ");
1103 1.53 kre }
1104 1.35 dsl out1fmt("%s\n", ap->val);
1105 1.53 kre if (ml && *argptr != NULL)
1106 1.53 kre out1c('\n');
1107 1.22 christos continue;
1108 1.22 christos }
1109 1.22 christos
1110 1.22 christos /* Then check if it is a tracked alias */
1111 1.53 kre if (!p_flag && (cmdp = cmdlookup(arg, 0)) != NULL) {
1112 1.22 christos entry.cmdtype = cmdp->cmdtype;
1113 1.22 christos entry.u = cmdp->param;
1114 1.35 dsl } else {
1115 1.53 kre cmdp = NULL;
1116 1.22 christos /* Finally use brute force */
1117 1.53 kre find_command(arg, &entry, DO_ABS,
1118 1.53 kre p_flag ? syspath() + 5 : pathval());
1119 1.22 christos }
1120 1.22 christos
1121 1.22 christos switch (entry.cmdtype) {
1122 1.22 christos case CMDNORMAL: {
1123 1.35 dsl if (strchr(arg, '/') == NULL) {
1124 1.53 kre const char *path;
1125 1.31 christos char *name;
1126 1.31 christos int j = entry.u.index;
1127 1.53 kre
1128 1.53 kre path = p_flag ? syspath() + 5 : pathval();
1129 1.53 kre
1130 1.31 christos do {
1131 1.48 kre name = padvance(&path, arg, 1);
1132 1.24 christos stunalloc(name);
1133 1.24 christos } while (--j >= 0);
1134 1.35 dsl if (!v_flag)
1135 1.35 dsl out1fmt(" is%s ",
1136 1.35 dsl cmdp ? " a tracked alias for" : "");
1137 1.35 dsl out1fmt("%s\n", name);
1138 1.31 christos } else {
1139 1.35 dsl if (access(arg, X_OK) == 0) {
1140 1.35 dsl if (!v_flag)
1141 1.35 dsl out1fmt(" is ");
1142 1.35 dsl out1fmt("%s\n", arg);
1143 1.35 dsl } else {
1144 1.35 dsl if (!v_flag)
1145 1.35 dsl out1fmt(": %s\n",
1146 1.35 dsl strerror(errno));
1147 1.35 dsl else
1148 1.35 dsl err = 126;
1149 1.35 dsl }
1150 1.24 christos }
1151 1.31 christos break;
1152 1.22 christos }
1153 1.22 christos case CMDFUNCTION:
1154 1.35 dsl if (!v_flag)
1155 1.35 dsl out1str(" is a shell function\n");
1156 1.35 dsl else
1157 1.35 dsl out1fmt("%s\n", arg);
1158 1.22 christos break;
1159 1.22 christos
1160 1.22 christos case CMDBUILTIN:
1161 1.35 dsl if (!v_flag)
1162 1.35 dsl out1str(" is a shell builtin\n");
1163 1.35 dsl else
1164 1.35 dsl out1fmt("%s\n", arg);
1165 1.34 christos break;
1166 1.34 christos
1167 1.34 christos case CMDSPLBLTIN:
1168 1.35 dsl if (!v_flag)
1169 1.35 dsl out1str(" is a special shell builtin\n");
1170 1.35 dsl else
1171 1.35 dsl out1fmt("%s\n", arg);
1172 1.22 christos break;
1173 1.22 christos
1174 1.22 christos default:
1175 1.35 dsl if (!v_flag)
1176 1.35 dsl out1str(": not found\n");
1177 1.35 dsl err = 127;
1178 1.22 christos break;
1179 1.22 christos }
1180 1.22 christos }
1181 1.27 christos return err;
1182 1.1 cgd }
1183