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