1 /*- 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; 36 #endif 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <paths.h> 42 #include <signal.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 #include <sys/resource.h> 46 #include <sys/wait.h> /* For WIFSIGNALED(status) */ 47 #include <errno.h> 48 49 /* 50 * Evaluate a command. 51 */ 52 53 #include "shell.h" 54 #include "nodes.h" 55 #include "syntax.h" 56 #include "expand.h" 57 #include "parser.h" 58 #include "jobs.h" 59 #include "eval.h" 60 #include "builtins.h" 61 #include "options.h" 62 #include "exec.h" 63 #include "redir.h" 64 #include "input.h" 65 #include "output.h" 66 #include "trap.h" 67 #include "var.h" 68 #include "memalloc.h" 69 #include "error.h" 70 #include "show.h" 71 #include "mystring.h" 72 #ifndef NO_HISTORY 73 #include "myhistedit.h" 74 #endif 75 76 77 int evalskip; /* set if we are skipping commands */ 78 static int skipcount; /* number of levels to skip */ 79 MKINIT int loopnest; /* current loop nesting level */ 80 int funcnest; /* depth of function calls */ 81 static int builtin_flags; /* evalcommand flags for builtins */ 82 83 84 char *commandname; 85 struct strlist *cmdenviron; 86 int exitstatus; /* exit status of last command */ 87 int oexitstatus; /* saved exit status */ 88 89 90 static void evalloop(union node *, int); 91 static void evalfor(union node *, int); 92 static void evalcase(union node *, int); 93 static void evalsubshell(union node *, int); 94 static void evalredir(union node *, int); 95 static void expredir(union node *); 96 static void evalpipe(union node *); 97 static int is_valid_fast_cmdsubst(union node *n); 98 static void evalcommand(union node *, int, struct backcmd *); 99 static void prehash(union node *); 100 101 102 /* 103 * Called to reset things after an exception. 104 */ 105 106 #ifdef mkinit 107 INCLUDE "eval.h" 108 109 RESET { 110 evalskip = 0; 111 loopnest = 0; 112 funcnest = 0; 113 } 114 #endif 115 116 117 118 /* 119 * The eval command. 120 */ 121 122 int 123 evalcmd(int argc, char **argv) 124 { 125 char *p; 126 char *concat; 127 char **ap; 128 129 if (argc > 1) { 130 p = argv[1]; 131 if (argc > 2) { 132 STARTSTACKSTR(concat); 133 ap = argv + 2; 134 for (;;) { 135 STPUTS(p, concat); 136 if ((p = *ap++) == NULL) 137 break; 138 STPUTC(' ', concat); 139 } 140 STPUTC('\0', concat); 141 p = grabstackstr(concat); 142 } 143 evalstring(p, builtin_flags & EV_TESTED); 144 } else 145 exitstatus = 0; 146 return exitstatus; 147 } 148 149 150 /* 151 * Execute a command or commands contained in a string. 152 */ 153 154 void 155 evalstring(char *s, int flags) 156 { 157 union node *n; 158 struct stackmark smark; 159 int flags_exit; 160 int any; 161 162 flags_exit = flags & EV_EXIT; 163 flags &= ~EV_EXIT; 164 any = 0; 165 setstackmark(&smark); 166 setinputstring(s, 1); 167 while ((n = parsecmd(0)) != NEOF) { 168 if (n != NULL) { 169 if (flags_exit && preadateof()) 170 evaltree(n, flags | EV_EXIT); 171 else 172 evaltree(n, flags); 173 any = 1; 174 } 175 popstackmark(&smark); 176 } 177 popfile(); 178 popstackmark(&smark); 179 if (!any) 180 exitstatus = 0; 181 if (flags_exit) 182 exitshell(exitstatus); 183 } 184 185 186 /* 187 * Evaluate a parse tree. The value is left in the global variable 188 * exitstatus. 189 */ 190 191 void 192 evaltree(union node *n, int flags) 193 { 194 int do_etest; 195 union node *next; 196 197 do_etest = 0; 198 if (n == NULL) { 199 TRACE(("evaltree(NULL) called\n")); 200 exitstatus = 0; 201 goto out; 202 } 203 do { 204 next = NULL; 205 #ifndef NO_HISTORY 206 displayhist = 1; /* show history substitutions done with fc */ 207 #endif 208 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type)); 209 switch (n->type) { 210 case NSEMI: 211 evaltree(n->nbinary.ch1, flags & ~EV_EXIT); 212 if (evalskip) 213 goto out; 214 next = n->nbinary.ch2; 215 break; 216 case NAND: 217 evaltree(n->nbinary.ch1, EV_TESTED); 218 if (evalskip || exitstatus != 0) { 219 goto out; 220 } 221 next = n->nbinary.ch2; 222 break; 223 case NOR: 224 evaltree(n->nbinary.ch1, EV_TESTED); 225 if (evalskip || exitstatus == 0) 226 goto out; 227 next = n->nbinary.ch2; 228 break; 229 case NREDIR: 230 evalredir(n, flags); 231 break; 232 case NSUBSHELL: 233 evalsubshell(n, flags); 234 do_etest = !(flags & EV_TESTED); 235 break; 236 case NBACKGND: 237 evalsubshell(n, flags); 238 break; 239 case NIF: { 240 evaltree(n->nif.test, EV_TESTED); 241 if (evalskip) 242 goto out; 243 if (exitstatus == 0) 244 next = n->nif.ifpart; 245 else if (n->nif.elsepart) 246 next = n->nif.elsepart; 247 else 248 exitstatus = 0; 249 break; 250 } 251 case NWHILE: 252 case NUNTIL: 253 evalloop(n, flags & ~EV_EXIT); 254 break; 255 case NFOR: 256 evalfor(n, flags & ~EV_EXIT); 257 break; 258 case NCASE: 259 evalcase(n, flags); 260 break; 261 case NDEFUN: 262 defun(n->narg.text, n->narg.next); 263 exitstatus = 0; 264 break; 265 case NNOT: 266 evaltree(n->nnot.com, EV_TESTED); 267 exitstatus = !exitstatus; 268 break; 269 270 case NPIPE: 271 evalpipe(n); 272 do_etest = !(flags & EV_TESTED); 273 break; 274 case NCMD: 275 evalcommand(n, flags, (struct backcmd *)NULL); 276 do_etest = !(flags & EV_TESTED); 277 break; 278 default: 279 out1fmt("Node type = %d\n", n->type); 280 flushout(&output); 281 break; 282 } 283 n = next; 284 } while (n != NULL); 285 out: 286 if (pendingsigs) 287 dotrap(); 288 if ((flags & EV_EXIT) || (eflag && exitstatus != 0 && do_etest)) 289 exitshell(exitstatus); 290 } 291 292 293 static void 294 evalloop(union node *n, int flags) 295 { 296 int status; 297 298 loopnest++; 299 status = 0; 300 for (;;) { 301 evaltree(n->nbinary.ch1, EV_TESTED); 302 if (evalskip) { 303 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 304 evalskip = 0; 305 continue; 306 } 307 if (evalskip == SKIPBREAK && --skipcount <= 0) 308 evalskip = 0; 309 if (evalskip == SKIPFUNC || evalskip == SKIPFILE) 310 status = exitstatus; 311 break; 312 } 313 if (n->type == NWHILE) { 314 if (exitstatus != 0) 315 break; 316 } else { 317 if (exitstatus == 0) 318 break; 319 } 320 evaltree(n->nbinary.ch2, flags); 321 status = exitstatus; 322 if (evalskip) 323 goto skipping; 324 } 325 loopnest--; 326 exitstatus = status; 327 } 328 329 330 331 static void 332 evalfor(union node *n, int flags) 333 { 334 struct arglist arglist; 335 union node *argp; 336 struct strlist *sp; 337 struct stackmark smark; 338 339 setstackmark(&smark); 340 arglist.lastp = &arglist.list; 341 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 342 oexitstatus = exitstatus; 343 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 344 if (evalskip) 345 goto out; 346 } 347 *arglist.lastp = NULL; 348 349 exitstatus = 0; 350 loopnest++; 351 for (sp = arglist.list ; sp ; sp = sp->next) { 352 setvar(n->nfor.var, sp->text, 0); 353 evaltree(n->nfor.body, flags); 354 if (evalskip) { 355 if (evalskip == SKIPCONT && --skipcount <= 0) { 356 evalskip = 0; 357 continue; 358 } 359 if (evalskip == SKIPBREAK && --skipcount <= 0) 360 evalskip = 0; 361 break; 362 } 363 } 364 loopnest--; 365 out: 366 popstackmark(&smark); 367 } 368 369 370 371 static void 372 evalcase(union node *n, int flags) 373 { 374 union node *cp; 375 union node *patp; 376 struct arglist arglist; 377 struct stackmark smark; 378 379 setstackmark(&smark); 380 arglist.lastp = &arglist.list; 381 oexitstatus = exitstatus; 382 exitstatus = 0; 383 expandarg(n->ncase.expr, &arglist, EXP_TILDE); 384 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 385 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 386 if (casematch(patp, arglist.list->text)) { 387 if (evalskip == 0) { 388 evaltree(cp->nclist.body, flags); 389 } 390 goto out; 391 } 392 } 393 } 394 out: 395 popstackmark(&smark); 396 } 397 398 399 400 /* 401 * Kick off a subshell to evaluate a tree. 402 */ 403 404 static void 405 evalsubshell(union node *n, int flags) 406 { 407 struct job *jp; 408 int backgnd = (n->type == NBACKGND); 409 410 expredir(n->nredir.redirect); 411 if ((!backgnd && flags & EV_EXIT && !have_traps()) || 412 forkshell(jp = makejob(n, 1), n, backgnd) == 0) { 413 if (backgnd) 414 flags &=~ EV_TESTED; 415 redirect(n->nredir.redirect, 0); 416 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 417 } else if (! backgnd) { 418 INTOFF; 419 exitstatus = waitforjob(jp, (int *)NULL); 420 INTON; 421 } 422 } 423 424 425 /* 426 * Evaluate a redirected compound command. 427 */ 428 429 static void 430 evalredir(union node *n, int flags) 431 { 432 struct jmploc jmploc; 433 struct jmploc *savehandler; 434 volatile int in_redirect = 1; 435 436 expredir(n->nredir.redirect); 437 savehandler = handler; 438 if (setjmp(jmploc.loc)) { 439 int e; 440 441 handler = savehandler; 442 e = exception; 443 if (e == EXERROR || e == EXEXEC) { 444 popredir(); 445 if (in_redirect) { 446 exitstatus = 2; 447 return; 448 } 449 } 450 longjmp(handler->loc, 1); 451 } else { 452 INTOFF; 453 handler = &jmploc; 454 redirect(n->nredir.redirect, REDIR_PUSH); 455 in_redirect = 0; 456 INTON; 457 evaltree(n->nredir.n, flags); 458 } 459 INTOFF; 460 handler = savehandler; 461 popredir(); 462 INTON; 463 } 464 465 466 /* 467 * Compute the names of the files in a redirection list. 468 */ 469 470 static void 471 expredir(union node *n) 472 { 473 union node *redir; 474 475 for (redir = n ; redir ; redir = redir->nfile.next) { 476 struct arglist fn; 477 fn.lastp = &fn.list; 478 oexitstatus = exitstatus; 479 switch (redir->type) { 480 case NFROM: 481 case NTO: 482 case NFROMTO: 483 case NAPPEND: 484 case NCLOBBER: 485 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 486 redir->nfile.expfname = fn.list->text; 487 break; 488 case NFROMFD: 489 case NTOFD: 490 if (redir->ndup.vname) { 491 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); 492 fixredir(redir, fn.list->text, 1); 493 } 494 break; 495 } 496 } 497 } 498 499 500 501 /* 502 * Evaluate a pipeline. All the processes in the pipeline are children 503 * of the process creating the pipeline. (This differs from some versions 504 * of the shell, which make the last process in a pipeline the parent 505 * of all the rest.) 506 */ 507 508 static void 509 evalpipe(union node *n) 510 { 511 struct job *jp; 512 struct nodelist *lp; 513 int pipelen; 514 int prevfd; 515 int pip[2]; 516 517 TRACE(("evalpipe(%p) called\n", (void *)n)); 518 pipelen = 0; 519 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 520 pipelen++; 521 INTOFF; 522 jp = makejob(n, pipelen); 523 prevfd = -1; 524 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 525 prehash(lp->n); 526 pip[1] = -1; 527 if (lp->next) { 528 if (pipe(pip) < 0) { 529 close(prevfd); 530 error("Pipe call failed: %s", strerror(errno)); 531 } 532 } 533 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 534 INTON; 535 if (prevfd > 0) { 536 dup2(prevfd, 0); 537 close(prevfd); 538 } 539 if (pip[1] >= 0) { 540 if (!(prevfd >= 0 && pip[0] == 0)) 541 close(pip[0]); 542 if (pip[1] != 1) { 543 dup2(pip[1], 1); 544 close(pip[1]); 545 } 546 } 547 evaltree(lp->n, EV_EXIT); 548 } 549 if (prevfd >= 0) 550 close(prevfd); 551 prevfd = pip[0]; 552 close(pip[1]); 553 } 554 INTON; 555 if (n->npipe.backgnd == 0) { 556 INTOFF; 557 exitstatus = waitforjob(jp, (int *)NULL); 558 TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 559 INTON; 560 } 561 } 562 563 564 565 static int 566 is_valid_fast_cmdsubst(union node *n) 567 { 568 union node *argp; 569 570 if (n->type != NCMD) 571 return 0; 572 for (argp = n->ncmd.args ; argp ; argp = argp->narg.next) 573 if (expandhassideeffects(argp->narg.text)) 574 return 0; 575 return 1; 576 } 577 578 /* 579 * Execute a command inside back quotes. If it's a builtin command, we 580 * want to save its output in a block obtained from malloc. Otherwise 581 * we fork off a subprocess and get the output of the command via a pipe. 582 * Should be called with interrupts off. 583 */ 584 585 void 586 evalbackcmd(union node *n, struct backcmd *result) 587 { 588 int pip[2]; 589 struct job *jp; 590 struct stackmark smark; /* unnecessary */ 591 struct jmploc jmploc; 592 struct jmploc *savehandler; 593 594 setstackmark(&smark); 595 result->fd = -1; 596 result->buf = NULL; 597 result->nleft = 0; 598 result->jp = NULL; 599 if (n == NULL) { 600 exitstatus = 0; 601 goto out; 602 } 603 if (is_valid_fast_cmdsubst(n)) { 604 exitstatus = oexitstatus; 605 savehandler = handler; 606 if (setjmp(jmploc.loc)) { 607 if (exception == EXERROR || exception == EXEXEC) 608 exitstatus = 2; 609 else if (exception != 0) { 610 handler = savehandler; 611 longjmp(handler->loc, 1); 612 } 613 } else { 614 handler = &jmploc; 615 evalcommand(n, EV_BACKCMD, result); 616 } 617 handler = savehandler; 618 } else { 619 exitstatus = 0; 620 if (pipe(pip) < 0) 621 error("Pipe call failed: %s", strerror(errno)); 622 jp = makejob(n, 1); 623 if (forkshell(jp, n, FORK_NOJOB) == 0) { 624 FORCEINTON; 625 close(pip[0]); 626 if (pip[1] != 1) { 627 dup2(pip[1], 1); 628 close(pip[1]); 629 } 630 evaltree(n, EV_EXIT); 631 } 632 close(pip[1]); 633 result->fd = pip[0]; 634 result->jp = jp; 635 } 636 out: 637 popstackmark(&smark); 638 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 639 result->fd, result->buf, result->nleft, result->jp)); 640 } 641 642 /* 643 * Check if a builtin can safely be executed in the same process, 644 * even though it should be in a subshell (command substitution). 645 * Note that jobid, jobs, times and trap can show information not 646 * available in a child process; this is deliberate. 647 * The arguments should already have been expanded. 648 */ 649 static int 650 safe_builtin(int idx, int argc, char **argv) 651 { 652 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD || 653 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD || 654 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD || 655 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD || 656 idx == TYPECMD) 657 return (1); 658 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD || 659 idx == UMASKCMD) 660 return (argc <= 1 || (argc == 2 && argv[1][0] == '-')); 661 if (idx == SETCMD) 662 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' || 663 argv[1][0] == '+') && argv[1][1] == 'o' && 664 argv[1][2] == '\0')); 665 return (0); 666 } 667 668 /* 669 * Execute a simple command. 670 * Note: This may or may not return if (flags & EV_EXIT). 671 */ 672 673 static void 674 evalcommand(union node *cmd, int flags, struct backcmd *backcmd) 675 { 676 struct stackmark smark; 677 union node *argp; 678 struct arglist arglist; 679 struct arglist varlist; 680 char **argv; 681 int argc; 682 char **envp; 683 int varflag; 684 struct strlist *sp; 685 int mode; 686 int pip[2]; 687 struct cmdentry cmdentry; 688 struct job *jp; 689 struct jmploc jmploc; 690 struct jmploc *savehandler; 691 char *savecmdname; 692 struct shparam saveparam; 693 struct localvar *savelocalvars; 694 struct parsefile *savetopfile; 695 volatile int e; 696 char *lastarg; 697 int realstatus; 698 int do_clearcmdentry; 699 const char *path = pathval(); 700 701 /* First expand the arguments. */ 702 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 703 setstackmark(&smark); 704 arglist.lastp = &arglist.list; 705 varlist.lastp = &varlist.list; 706 varflag = 1; 707 jp = NULL; 708 do_clearcmdentry = 0; 709 oexitstatus = exitstatus; 710 exitstatus = 0; 711 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 712 char *p = argp->narg.text; 713 if (varflag && is_name(*p)) { 714 do { 715 p++; 716 } while (is_in_name(*p)); 717 if (*p == '=') { 718 expandarg(argp, &varlist, EXP_VARTILDE); 719 continue; 720 } 721 } 722 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 723 varflag = 0; 724 } 725 *arglist.lastp = NULL; 726 *varlist.lastp = NULL; 727 expredir(cmd->ncmd.redirect); 728 argc = 0; 729 for (sp = arglist.list ; sp ; sp = sp->next) 730 argc++; 731 /* Add one slot at the beginning for tryexec(). */ 732 argv = stalloc(sizeof (char *) * (argc + 2)); 733 argv++; 734 735 for (sp = arglist.list ; sp ; sp = sp->next) { 736 TRACE(("evalcommand arg: %s\n", sp->text)); 737 *argv++ = sp->text; 738 } 739 *argv = NULL; 740 lastarg = NULL; 741 if (iflag && funcnest == 0 && argc > 0) 742 lastarg = argv[-1]; 743 argv -= argc; 744 745 /* Print the command if xflag is set. */ 746 if (xflag) { 747 char sep = 0; 748 const char *p; 749 out2str(ps4val()); 750 for (sp = varlist.list ; sp ; sp = sp->next) { 751 if (sep != 0) 752 out2c(' '); 753 p = strchr(sp->text, '='); 754 if (p != NULL) { 755 p++; 756 outbin(sp->text, p - sp->text, out2); 757 out2qstr(p); 758 } else 759 out2qstr(sp->text); 760 sep = ' '; 761 } 762 for (sp = arglist.list ; sp ; sp = sp->next) { 763 if (sep != 0) 764 out2c(' '); 765 /* Disambiguate command looking like assignment. */ 766 if (sp == arglist.list && 767 strchr(sp->text, '=') != NULL && 768 strchr(sp->text, '\'') == NULL) { 769 out2c('\''); 770 out2str(sp->text); 771 out2c('\''); 772 } else 773 out2qstr(sp->text); 774 sep = ' '; 775 } 776 out2c('\n'); 777 flushout(&errout); 778 } 779 780 /* Now locate the command. */ 781 if (argc == 0) { 782 /* Variable assignment(s) without command */ 783 cmdentry.cmdtype = CMDBUILTIN; 784 cmdentry.u.index = BLTINCMD; 785 cmdentry.special = 0; 786 } else { 787 static const char PATH[] = "PATH="; 788 int cmd_flags = 0, bltinonly = 0; 789 790 /* 791 * Modify the command lookup path, if a PATH= assignment 792 * is present 793 */ 794 for (sp = varlist.list ; sp ; sp = sp->next) 795 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 796 path = sp->text + sizeof(PATH) - 1; 797 /* 798 * On `PATH=... command`, we need to make 799 * sure that the command isn't using the 800 * non-updated hash table of the outer PATH 801 * setting and we need to make sure that 802 * the hash table isn't filled with items 803 * from the temporary setting. 804 * 805 * It would be better to forbit using and 806 * updating the table while this command 807 * runs, by the command finding mechanism 808 * is heavily integrated with hash handling, 809 * so we just delete the hash before and after 810 * the command runs. Partly deleting like 811 * changepatch() does doesn't seem worth the 812 * bookinging effort, since most such runs add 813 * directories in front of the new PATH. 814 */ 815 clearcmdentry(); 816 do_clearcmdentry = 1; 817 } 818 819 for (;;) { 820 if (bltinonly) { 821 cmdentry.u.index = find_builtin(*argv, &cmdentry.special); 822 if (cmdentry.u.index < 0) { 823 cmdentry.u.index = BLTINCMD; 824 argv--; 825 argc++; 826 break; 827 } 828 } else 829 find_command(argv[0], &cmdentry, cmd_flags, path); 830 /* implement the bltin and command builtins here */ 831 if (cmdentry.cmdtype != CMDBUILTIN) 832 break; 833 if (cmdentry.u.index == BLTINCMD) { 834 if (argc == 1) 835 break; 836 argv++; 837 argc--; 838 bltinonly = 1; 839 } else if (cmdentry.u.index == COMMANDCMD) { 840 if (argc == 1) 841 break; 842 if (!strcmp(argv[1], "-p")) { 843 if (argc == 2) 844 break; 845 if (argv[2][0] == '-') { 846 if (strcmp(argv[2], "--")) 847 break; 848 if (argc == 3) 849 break; 850 argv += 3; 851 argc -= 3; 852 } else { 853 argv += 2; 854 argc -= 2; 855 } 856 path = _PATH_STDPATH; 857 clearcmdentry(); 858 do_clearcmdentry = 1; 859 } else if (!strcmp(argv[1], "--")) { 860 if (argc == 2) 861 break; 862 argv += 2; 863 argc -= 2; 864 } else if (argv[1][0] == '-') 865 break; 866 else { 867 argv++; 868 argc--; 869 } 870 cmd_flags |= DO_NOFUNC; 871 bltinonly = 0; 872 } else 873 break; 874 } 875 /* 876 * Special builtins lose their special properties when 877 * called via 'command'. 878 */ 879 if (cmd_flags & DO_NOFUNC) 880 cmdentry.special = 0; 881 } 882 883 /* Fork off a child process if necessary. */ 884 if (cmd->ncmd.backgnd 885 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 886 && ((flags & EV_EXIT) == 0 || have_traps())) 887 || ((flags & EV_BACKCMD) != 0 888 && (cmdentry.cmdtype != CMDBUILTIN || 889 !safe_builtin(cmdentry.u.index, argc, argv)))) { 890 jp = makejob(cmd, 1); 891 mode = cmd->ncmd.backgnd; 892 if (flags & EV_BACKCMD) { 893 mode = FORK_NOJOB; 894 if (pipe(pip) < 0) 895 error("Pipe call failed: %s", strerror(errno)); 896 } 897 if (forkshell(jp, cmd, mode) != 0) 898 goto parent; /* at end of routine */ 899 if (flags & EV_BACKCMD) { 900 FORCEINTON; 901 close(pip[0]); 902 if (pip[1] != 1) { 903 dup2(pip[1], 1); 904 close(pip[1]); 905 } 906 } 907 flags |= EV_EXIT; 908 } 909 910 /* This is the child process if a fork occurred. */ 911 /* Execute the command. */ 912 if (cmdentry.cmdtype == CMDFUNCTION) { 913 #ifdef DEBUG 914 trputs("Shell function: "); trargs(argv); 915 #endif 916 saveparam = shellparam; 917 shellparam.malloc = 0; 918 shellparam.reset = 1; 919 shellparam.nparam = argc - 1; 920 shellparam.p = argv + 1; 921 shellparam.optnext = NULL; 922 INTOFF; 923 savelocalvars = localvars; 924 localvars = NULL; 925 reffunc(cmdentry.u.func); 926 savehandler = handler; 927 if (setjmp(jmploc.loc)) { 928 freeparam(&shellparam); 929 shellparam = saveparam; 930 if (exception == EXERROR || exception == EXEXEC) 931 popredir(); 932 unreffunc(cmdentry.u.func); 933 poplocalvars(); 934 localvars = savelocalvars; 935 funcnest--; 936 handler = savehandler; 937 longjmp(handler->loc, 1); 938 } 939 handler = &jmploc; 940 funcnest++; 941 redirect(cmd->ncmd.redirect, REDIR_PUSH); 942 INTON; 943 for (sp = varlist.list ; sp ; sp = sp->next) 944 mklocal(sp->text); 945 exitstatus = oexitstatus; 946 if (flags & EV_TESTED) 947 evaltree(getfuncnode(cmdentry.u.func), EV_TESTED); 948 else 949 evaltree(getfuncnode(cmdentry.u.func), 0); 950 INTOFF; 951 unreffunc(cmdentry.u.func); 952 poplocalvars(); 953 localvars = savelocalvars; 954 freeparam(&shellparam); 955 shellparam = saveparam; 956 handler = savehandler; 957 funcnest--; 958 popredir(); 959 INTON; 960 if (evalskip == SKIPFUNC) { 961 evalskip = 0; 962 skipcount = 0; 963 } 964 if (jp) 965 exitshell(exitstatus); 966 } else if (cmdentry.cmdtype == CMDBUILTIN) { 967 #ifdef DEBUG 968 trputs("builtin command: "); trargs(argv); 969 #endif 970 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 971 if (flags == EV_BACKCMD) { 972 memout.nleft = 0; 973 memout.nextc = memout.buf; 974 memout.bufsize = 64; 975 mode |= REDIR_BACKQ; 976 cmdentry.special = 0; 977 } 978 savecmdname = commandname; 979 savetopfile = getcurrentfile(); 980 cmdenviron = varlist.list; 981 e = -1; 982 savehandler = handler; 983 if (setjmp(jmploc.loc)) { 984 e = exception; 985 exitstatus = (e == EXINT)? SIGINT+128 : 2; 986 goto cmddone; 987 } 988 handler = &jmploc; 989 redirect(cmd->ncmd.redirect, mode); 990 /* 991 * If there is no command word, redirection errors should 992 * not be fatal but assignment errors should. 993 */ 994 if (argc == 0 && !(flags & EV_BACKCMD)) 995 cmdentry.special = 1; 996 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET); 997 if (argc > 0) 998 bltinsetlocale(); 999 commandname = argv[0]; 1000 argptr = argv + 1; 1001 nextopt_optptr = NULL; /* initialize nextopt */ 1002 builtin_flags = flags; 1003 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 1004 flushall(); 1005 cmddone: 1006 if (argc > 0) 1007 bltinunsetlocale(); 1008 cmdenviron = NULL; 1009 out1 = &output; 1010 out2 = &errout; 1011 freestdout(); 1012 handler = savehandler; 1013 commandname = savecmdname; 1014 if (jp) 1015 exitshell(exitstatus); 1016 if (flags == EV_BACKCMD) { 1017 backcmd->buf = memout.buf; 1018 backcmd->nleft = memout.nextc - memout.buf; 1019 memout.buf = NULL; 1020 } 1021 if (cmdentry.u.index != EXECCMD && 1022 (e == -1 || e == EXERROR || e == EXEXEC)) 1023 popredir(); 1024 if (e != -1) { 1025 if ((e != EXERROR && e != EXEXEC) 1026 || cmdentry.special) 1027 exraise(e); 1028 popfilesupto(savetopfile); 1029 if (flags != EV_BACKCMD) 1030 FORCEINTON; 1031 } 1032 } else { 1033 #ifdef DEBUG 1034 trputs("normal command: "); trargs(argv); 1035 #endif 1036 redirect(cmd->ncmd.redirect, 0); 1037 for (sp = varlist.list ; sp ; sp = sp->next) 1038 setvareq(sp->text, VEXPORT|VSTACK); 1039 envp = environment(); 1040 shellexec(argv, envp, path, cmdentry.u.index); 1041 /*NOTREACHED*/ 1042 } 1043 goto out; 1044 1045 parent: /* parent process gets here (if we forked) */ 1046 if (mode == FORK_FG) { /* argument to fork */ 1047 INTOFF; 1048 exitstatus = waitforjob(jp, &realstatus); 1049 INTON; 1050 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 1051 evalskip = SKIPBREAK; 1052 skipcount = loopnest; 1053 } 1054 } else if (mode == FORK_NOJOB) { 1055 backcmd->fd = pip[0]; 1056 close(pip[1]); 1057 backcmd->jp = jp; 1058 } 1059 1060 out: 1061 if (lastarg) 1062 setvar("_", lastarg, 0); 1063 if (do_clearcmdentry) 1064 clearcmdentry(); 1065 popstackmark(&smark); 1066 } 1067 1068 1069 1070 /* 1071 * Search for a command. This is called before we fork so that the 1072 * location of the command will be available in the parent as well as 1073 * the child. The check for "goodname" is an overly conservative 1074 * check that the name will not be subject to expansion. 1075 */ 1076 1077 static void 1078 prehash(union node *n) 1079 { 1080 struct cmdentry entry; 1081 1082 if (n && n->type == NCMD && n->ncmd.args) 1083 if (goodname(n->ncmd.args->narg.text)) 1084 find_command(n->ncmd.args->narg.text, &entry, 0, 1085 pathval()); 1086 } 1087 1088 1089 1090 /* 1091 * Builtin commands. Builtin commands whose functions are closely 1092 * tied to evaluation are implemented here. 1093 */ 1094 1095 /* 1096 * No command given, a bltin command with no arguments, or a bltin command 1097 * with an invalid name. 1098 */ 1099 1100 int 1101 bltincmd(int argc, char **argv) 1102 { 1103 if (argc > 1) { 1104 out2fmt_flush("%s: not found\n", argv[1]); 1105 return 127; 1106 } 1107 /* 1108 * Preserve exitstatus of a previous possible redirection 1109 * as POSIX mandates 1110 */ 1111 return exitstatus; 1112 } 1113 1114 1115 /* 1116 * Handle break and continue commands. Break, continue, and return are 1117 * all handled by setting the evalskip flag. The evaluation routines 1118 * above all check this flag, and if it is set they start skipping 1119 * commands rather than executing them. The variable skipcount is 1120 * the number of loops to break/continue, or the number of function 1121 * levels to return. (The latter is always 1.) It should probably 1122 * be an error to break out of more loops than exist, but it isn't 1123 * in the standard shell so we don't make it one here. 1124 */ 1125 1126 int 1127 breakcmd(int argc, char **argv) 1128 { 1129 int n = argc > 1 ? number(argv[1]) : 1; 1130 1131 if (n > loopnest) 1132 n = loopnest; 1133 if (n > 0) { 1134 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 1135 skipcount = n; 1136 } 1137 return 0; 1138 } 1139 1140 /* 1141 * The `command' command. 1142 */ 1143 int 1144 commandcmd(int argc, char **argv) 1145 { 1146 const char *path; 1147 int ch; 1148 int cmd = -1; 1149 1150 path = bltinlookup("PATH", 1); 1151 1152 optind = optreset = 1; 1153 opterr = 0; 1154 while ((ch = getopt(argc, argv, "pvV")) != -1) { 1155 switch (ch) { 1156 case 'p': 1157 path = _PATH_STDPATH; 1158 break; 1159 case 'v': 1160 cmd = TYPECMD_SMALLV; 1161 break; 1162 case 'V': 1163 cmd = TYPECMD_BIGV; 1164 break; 1165 case '?': 1166 default: 1167 error("unknown option: -%c", optopt); 1168 } 1169 } 1170 argc -= optind; 1171 argv += optind; 1172 1173 if (cmd != -1) { 1174 if (argc != 1) 1175 error("wrong number of arguments"); 1176 return typecmd_impl(2, argv - 1, cmd, path); 1177 } 1178 if (argc != 0) 1179 error("commandcmd bad call"); 1180 1181 /* 1182 * Do nothing successfully if no command was specified; 1183 * ksh also does this. 1184 */ 1185 return 0; 1186 } 1187 1188 1189 /* 1190 * The return command. 1191 */ 1192 1193 int 1194 returncmd(int argc, char **argv) 1195 { 1196 int ret = argc > 1 ? number(argv[1]) : oexitstatus; 1197 1198 if (funcnest) { 1199 evalskip = SKIPFUNC; 1200 skipcount = 1; 1201 } else { 1202 /* skip the rest of the file */ 1203 evalskip = SKIPFILE; 1204 skipcount = 1; 1205 } 1206 return ret; 1207 } 1208 1209 1210 int 1211 falsecmd(int argc __unused, char **argv __unused) 1212 { 1213 return 1; 1214 } 1215 1216 1217 int 1218 truecmd(int argc __unused, char **argv __unused) 1219 { 1220 return 0; 1221 } 1222 1223 1224 int 1225 execcmd(int argc, char **argv) 1226 { 1227 /* 1228 * Because we have historically not supported any options, 1229 * only treat "--" specially. 1230 */ 1231 if (argc > 1 && strcmp(argv[1], "--") == 0) 1232 argc--, argv++; 1233 if (argc > 1) { 1234 struct strlist *sp; 1235 1236 iflag = 0; /* exit on error */ 1237 mflag = 0; 1238 optschanged(); 1239 for (sp = cmdenviron; sp ; sp = sp->next) 1240 setvareq(sp->text, VEXPORT|VSTACK); 1241 shellexec(argv + 1, environment(), pathval(), 0); 1242 1243 } 1244 return 0; 1245 } 1246 1247 1248 int 1249 timescmd(int argc __unused, char **argv __unused) 1250 { 1251 struct rusage ru; 1252 long shumins, shsmins, chumins, chsmins; 1253 double shusecs, shssecs, chusecs, chssecs; 1254 1255 if (getrusage(RUSAGE_SELF, &ru) < 0) 1256 return 1; 1257 shumins = ru.ru_utime.tv_sec / 60; 1258 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1259 shsmins = ru.ru_stime.tv_sec / 60; 1260 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1261 if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1262 return 1; 1263 chumins = ru.ru_utime.tv_sec / 60; 1264 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1265 chsmins = ru.ru_stime.tv_sec / 60; 1266 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1267 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1268 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1269 return 0; 1270 } 1271