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