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