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