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