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 MKINIT 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 expredir(union node *); 95 STATIC void evalpipe(union node *); 96 STATIC void evalcommand(union node *, int, struct backcmd *); 97 STATIC void prehash(union node *); 98 99 100 /* 101 * Called to reset things after an exception. 102 */ 103 104 #ifdef mkinit 105 INCLUDE "eval.h" 106 107 RESET { 108 evalskip = 0; 109 loopnest = 0; 110 funcnest = 0; 111 } 112 113 SHELLPROC { 114 exitstatus = 0; 115 } 116 #endif 117 118 119 120 /* 121 * The eval command. 122 */ 123 124 int 125 evalcmd(int argc, char **argv) 126 { 127 char *p; 128 char *concat; 129 char **ap; 130 131 if (argc > 1) { 132 p = argv[1]; 133 if (argc > 2) { 134 STARTSTACKSTR(concat); 135 ap = argv + 2; 136 for (;;) { 137 while (*p) 138 STPUTC(*p++, concat); 139 if ((p = *ap++) == NULL) 140 break; 141 STPUTC(' ', concat); 142 } 143 STPUTC('\0', concat); 144 p = grabstackstr(concat); 145 } 146 evalstring(p, builtin_flags & EV_TESTED); 147 } 148 return exitstatus; 149 } 150 151 152 /* 153 * Execute a command or commands contained in a string. 154 */ 155 156 void 157 evalstring(char *s, int flags) 158 { 159 union node *n; 160 struct stackmark smark; 161 int flags_exit; 162 163 flags_exit = flags & EV_EXIT; 164 flags &= ~EV_EXIT; 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 } 174 popstackmark(&smark); 175 } 176 popfile(); 177 popstackmark(&smark); 178 if (flags_exit) 179 exitshell(exitstatus); 180 } 181 182 183 /* 184 * Evaluate a parse tree. The value is left in the global variable 185 * exitstatus. 186 */ 187 188 void 189 evaltree(union node *n, int flags) 190 { 191 int do_etest; 192 193 do_etest = 0; 194 if (n == NULL) { 195 TRACE(("evaltree(NULL) called\n")); 196 exitstatus = 0; 197 goto out; 198 } 199 #ifndef NO_HISTORY 200 displayhist = 1; /* show history substitutions done with fc */ 201 #endif 202 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type)); 203 switch (n->type) { 204 case NSEMI: 205 evaltree(n->nbinary.ch1, flags & ~EV_EXIT); 206 if (evalskip) 207 goto out; 208 evaltree(n->nbinary.ch2, flags); 209 break; 210 case NAND: 211 evaltree(n->nbinary.ch1, EV_TESTED); 212 if (evalskip || exitstatus != 0) { 213 goto out; 214 } 215 evaltree(n->nbinary.ch2, flags); 216 break; 217 case NOR: 218 evaltree(n->nbinary.ch1, EV_TESTED); 219 if (evalskip || exitstatus == 0) 220 goto out; 221 evaltree(n->nbinary.ch2, flags); 222 break; 223 case NREDIR: 224 expredir(n->nredir.redirect); 225 redirect(n->nredir.redirect, REDIR_PUSH); 226 evaltree(n->nredir.n, flags); 227 popredir(); 228 break; 229 case NSUBSHELL: 230 evalsubshell(n, flags); 231 do_etest = !(flags & EV_TESTED); 232 break; 233 case NBACKGND: 234 evalsubshell(n, flags); 235 break; 236 case NIF: { 237 evaltree(n->nif.test, EV_TESTED); 238 if (evalskip) 239 goto out; 240 if (exitstatus == 0) 241 evaltree(n->nif.ifpart, flags); 242 else if (n->nif.elsepart) 243 evaltree(n->nif.elsepart, flags); 244 else 245 exitstatus = 0; 246 break; 247 } 248 case NWHILE: 249 case NUNTIL: 250 evalloop(n, flags & ~EV_EXIT); 251 break; 252 case NFOR: 253 evalfor(n, flags & ~EV_EXIT); 254 break; 255 case NCASE: 256 evalcase(n, flags); 257 break; 258 case NDEFUN: 259 defun(n->narg.text, n->narg.next); 260 exitstatus = 0; 261 break; 262 case NNOT: 263 evaltree(n->nnot.com, EV_TESTED); 264 exitstatus = !exitstatus; 265 break; 266 267 case NPIPE: 268 evalpipe(n); 269 do_etest = !(flags & EV_TESTED); 270 break; 271 case NCMD: 272 evalcommand(n, flags, (struct backcmd *)NULL); 273 do_etest = !(flags & EV_TESTED); 274 break; 275 default: 276 out1fmt("Node type = %d\n", n->type); 277 flushout(&output); 278 break; 279 } 280 out: 281 if (pendingsigs) 282 dotrap(); 283 if ((flags & EV_EXIT) || (eflag && exitstatus != 0 && do_etest)) 284 exitshell(exitstatus); 285 } 286 287 288 STATIC void 289 evalloop(union node *n, int flags) 290 { 291 int status; 292 293 loopnest++; 294 status = 0; 295 for (;;) { 296 evaltree(n->nbinary.ch1, EV_TESTED); 297 if (evalskip) { 298 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 299 evalskip = 0; 300 continue; 301 } 302 if (evalskip == SKIPBREAK && --skipcount <= 0) 303 evalskip = 0; 304 break; 305 } 306 if (n->type == NWHILE) { 307 if (exitstatus != 0) 308 break; 309 } else { 310 if (exitstatus == 0) 311 break; 312 } 313 evaltree(n->nbinary.ch2, flags); 314 status = exitstatus; 315 if (evalskip) 316 goto skipping; 317 } 318 loopnest--; 319 exitstatus = status; 320 } 321 322 323 324 STATIC void 325 evalfor(union node *n, int flags) 326 { 327 struct arglist arglist; 328 union node *argp; 329 struct strlist *sp; 330 struct stackmark smark; 331 332 setstackmark(&smark); 333 arglist.lastp = &arglist.list; 334 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 335 oexitstatus = exitstatus; 336 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 337 if (evalskip) 338 goto out; 339 } 340 *arglist.lastp = NULL; 341 342 exitstatus = 0; 343 loopnest++; 344 for (sp = arglist.list ; sp ; sp = sp->next) { 345 setvar(n->nfor.var, sp->text, 0); 346 evaltree(n->nfor.body, flags); 347 if (evalskip) { 348 if (evalskip == SKIPCONT && --skipcount <= 0) { 349 evalskip = 0; 350 continue; 351 } 352 if (evalskip == SKIPBREAK && --skipcount <= 0) 353 evalskip = 0; 354 break; 355 } 356 } 357 loopnest--; 358 out: 359 popstackmark(&smark); 360 } 361 362 363 364 STATIC void 365 evalcase(union node *n, int flags) 366 { 367 union node *cp; 368 union node *patp; 369 struct arglist arglist; 370 struct stackmark smark; 371 372 setstackmark(&smark); 373 arglist.lastp = &arglist.list; 374 oexitstatus = exitstatus; 375 exitstatus = 0; 376 expandarg(n->ncase.expr, &arglist, EXP_TILDE); 377 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 378 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 379 if (casematch(patp, arglist.list->text)) { 380 if (evalskip == 0) { 381 evaltree(cp->nclist.body, flags); 382 } 383 goto out; 384 } 385 } 386 } 387 out: 388 popstackmark(&smark); 389 } 390 391 392 393 /* 394 * Kick off a subshell to evaluate a tree. 395 */ 396 397 STATIC void 398 evalsubshell(union node *n, int flags) 399 { 400 struct job *jp; 401 int backgnd = (n->type == NBACKGND); 402 403 expredir(n->nredir.redirect); 404 if ((!backgnd && flags & EV_EXIT && !have_traps()) || 405 forkshell(jp = makejob(n, 1), n, backgnd) == 0) { 406 if (backgnd) 407 flags &=~ EV_TESTED; 408 redirect(n->nredir.redirect, 0); 409 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 410 } 411 if (! backgnd) { 412 INTOFF; 413 exitstatus = waitforjob(jp, (int *)NULL); 414 INTON; 415 } 416 } 417 418 419 420 /* 421 * Compute the names of the files in a redirection list. 422 */ 423 424 STATIC void 425 expredir(union node *n) 426 { 427 union node *redir; 428 429 for (redir = n ; redir ; redir = redir->nfile.next) { 430 struct arglist fn; 431 fn.lastp = &fn.list; 432 oexitstatus = exitstatus; 433 switch (redir->type) { 434 case NFROM: 435 case NTO: 436 case NFROMTO: 437 case NAPPEND: 438 case NCLOBBER: 439 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 440 redir->nfile.expfname = fn.list->text; 441 break; 442 case NFROMFD: 443 case NTOFD: 444 if (redir->ndup.vname) { 445 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); 446 fixredir(redir, fn.list->text, 1); 447 } 448 break; 449 } 450 } 451 } 452 453 454 455 /* 456 * Evaluate a pipeline. All the processes in the pipeline are children 457 * of the process creating the pipeline. (This differs from some versions 458 * of the shell, which make the last process in a pipeline the parent 459 * of all the rest.) 460 */ 461 462 STATIC void 463 evalpipe(union node *n) 464 { 465 struct job *jp; 466 struct nodelist *lp; 467 int pipelen; 468 int prevfd; 469 int pip[2]; 470 471 TRACE(("evalpipe(%p) called\n", (void *)n)); 472 pipelen = 0; 473 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 474 pipelen++; 475 INTOFF; 476 jp = makejob(n, pipelen); 477 prevfd = -1; 478 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 479 prehash(lp->n); 480 pip[1] = -1; 481 if (lp->next) { 482 if (pipe(pip) < 0) { 483 close(prevfd); 484 error("Pipe call failed: %s", strerror(errno)); 485 } 486 } 487 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 488 INTON; 489 if (prevfd > 0) { 490 dup2(prevfd, 0); 491 close(prevfd); 492 } 493 if (pip[1] >= 0) { 494 if (!(prevfd >= 0 && pip[0] == 0)) 495 close(pip[0]); 496 if (pip[1] != 1) { 497 dup2(pip[1], 1); 498 close(pip[1]); 499 } 500 } 501 evaltree(lp->n, EV_EXIT); 502 } 503 if (prevfd >= 0) 504 close(prevfd); 505 prevfd = pip[0]; 506 close(pip[1]); 507 } 508 INTON; 509 if (n->npipe.backgnd == 0) { 510 INTOFF; 511 exitstatus = waitforjob(jp, (int *)NULL); 512 TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 513 INTON; 514 } 515 } 516 517 518 519 /* 520 * Execute a command inside back quotes. If it's a builtin command, we 521 * want to save its output in a block obtained from malloc. Otherwise 522 * we fork off a subprocess and get the output of the command via a pipe. 523 * Should be called with interrupts off. 524 */ 525 526 void 527 evalbackcmd(union node *n, struct backcmd *result) 528 { 529 int pip[2]; 530 struct job *jp; 531 struct stackmark smark; /* unnecessary */ 532 533 setstackmark(&smark); 534 result->fd = -1; 535 result->buf = NULL; 536 result->nleft = 0; 537 result->jp = NULL; 538 if (n == NULL) { 539 exitstatus = 0; 540 goto out; 541 } 542 if (n->type == NCMD) { 543 exitstatus = oexitstatus; 544 evalcommand(n, EV_BACKCMD, result); 545 } else { 546 exitstatus = 0; 547 if (pipe(pip) < 0) 548 error("Pipe call failed: %s", strerror(errno)); 549 jp = makejob(n, 1); 550 if (forkshell(jp, n, FORK_NOJOB) == 0) { 551 FORCEINTON; 552 close(pip[0]); 553 if (pip[1] != 1) { 554 dup2(pip[1], 1); 555 close(pip[1]); 556 } 557 evaltree(n, EV_EXIT); 558 } 559 close(pip[1]); 560 result->fd = pip[0]; 561 result->jp = jp; 562 } 563 out: 564 popstackmark(&smark); 565 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 566 result->fd, result->buf, result->nleft, result->jp)); 567 } 568 569 570 571 /* 572 * Execute a simple command. 573 */ 574 575 STATIC void 576 evalcommand(union node *cmd, int flags, struct backcmd *backcmd) 577 { 578 struct stackmark smark; 579 union node *argp; 580 struct arglist arglist; 581 struct arglist varlist; 582 char **argv; 583 int argc; 584 char **envp; 585 int varflag; 586 struct strlist *sp; 587 int mode; 588 int pip[2]; 589 struct cmdentry cmdentry; 590 struct job *jp; 591 struct jmploc jmploc; 592 struct jmploc *savehandler; 593 char *savecmdname; 594 struct shparam saveparam; 595 struct localvar *savelocalvars; 596 struct parsefile *savetopfile; 597 volatile int e; 598 char *lastarg; 599 int realstatus; 600 int do_clearcmdentry; 601 602 /* First expand the arguments. */ 603 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 604 setstackmark(&smark); 605 arglist.lastp = &arglist.list; 606 varlist.lastp = &varlist.list; 607 varflag = 1; 608 do_clearcmdentry = 0; 609 oexitstatus = exitstatus; 610 exitstatus = 0; 611 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 612 char *p = argp->narg.text; 613 if (varflag && is_name(*p)) { 614 do { 615 p++; 616 } while (is_in_name(*p)); 617 if (*p == '=') { 618 expandarg(argp, &varlist, EXP_VARTILDE); 619 continue; 620 } 621 } 622 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 623 varflag = 0; 624 } 625 *arglist.lastp = NULL; 626 *varlist.lastp = NULL; 627 expredir(cmd->ncmd.redirect); 628 argc = 0; 629 for (sp = arglist.list ; sp ; sp = sp->next) 630 argc++; 631 argv = stalloc(sizeof (char *) * (argc + 1)); 632 633 for (sp = arglist.list ; sp ; sp = sp->next) { 634 TRACE(("evalcommand arg: %s\n", sp->text)); 635 *argv++ = sp->text; 636 } 637 *argv = NULL; 638 lastarg = NULL; 639 if (iflag && funcnest == 0 && argc > 0) 640 lastarg = argv[-1]; 641 argv -= argc; 642 643 /* Print the command if xflag is set. */ 644 if (xflag) { 645 char sep = 0; 646 const char *p; 647 out2str(ps4val()); 648 for (sp = varlist.list ; sp ; sp = sp->next) { 649 if (sep != 0) 650 outc(' ', &errout); 651 p = sp->text; 652 while (*p != '=' && *p != '\0') 653 out2c(*p++); 654 if (*p != '\0') { 655 out2c(*p++); 656 out2qstr(p); 657 } 658 sep = ' '; 659 } 660 for (sp = arglist.list ; sp ; sp = sp->next) { 661 if (sep != 0) 662 outc(' ', &errout); 663 /* Disambiguate command looking like assignment. */ 664 if (sp == arglist.list && 665 strchr(sp->text, '=') != NULL && 666 strchr(sp->text, '\'') == NULL) { 667 out2c('\''); 668 out2str(sp->text); 669 out2c('\''); 670 } else 671 out2qstr(sp->text); 672 sep = ' '; 673 } 674 outc('\n', &errout); 675 flushout(&errout); 676 } 677 678 /* Now locate the command. */ 679 if (argc == 0) { 680 /* Variable assignment(s) without command */ 681 cmdentry.cmdtype = CMDBUILTIN; 682 cmdentry.u.index = BLTINCMD; 683 cmdentry.special = 1; 684 } else { 685 static const char PATH[] = "PATH="; 686 char *path = pathval(); 687 688 /* 689 * Modify the command lookup path, if a PATH= assignment 690 * is present 691 */ 692 for (sp = varlist.list ; sp ; sp = sp->next) 693 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 694 path = sp->text + sizeof(PATH) - 1; 695 /* 696 * On `PATH=... command`, we need to make 697 * sure that the command isn't using the 698 * non-updated hash table of the outer PATH 699 * setting and we need to make sure that 700 * the hash table isn't filled with items 701 * from the temporary setting. 702 * 703 * It would be better to forbit using and 704 * updating the table while this command 705 * runs, by the command finding mechanism 706 * is heavily integrated with hash handling, 707 * so we just delete the hash before and after 708 * the command runs. Partly deleting like 709 * changepatch() does doesn't seem worth the 710 * bookinging effort, since most such runs add 711 * directories in front of the new PATH. 712 */ 713 clearcmdentry(0); 714 do_clearcmdentry = 1; 715 } 716 717 find_command(argv[0], &cmdentry, 0, path); 718 /* implement the bltin builtin here */ 719 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) { 720 for (;;) { 721 argv++; 722 if (--argc == 0) 723 break; 724 if ((cmdentry.u.index = find_builtin(*argv, 725 &cmdentry.special)) < 0) { 726 outfmt(&errout, "%s: not found\n", *argv); 727 exitstatus = 127; 728 flushout(&errout); 729 return; 730 } 731 if (cmdentry.u.index != BLTINCMD) 732 break; 733 } 734 } 735 } 736 737 /* Fork off a child process if necessary. */ 738 if (cmd->ncmd.backgnd 739 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 740 && ((flags & EV_EXIT) == 0 || have_traps())) 741 || ((flags & EV_BACKCMD) != 0 742 && (cmdentry.cmdtype != CMDBUILTIN 743 || cmdentry.u.index == CDCMD 744 || cmdentry.u.index == DOTCMD 745 || cmdentry.u.index == EVALCMD)) 746 || (cmdentry.cmdtype == CMDBUILTIN && 747 cmdentry.u.index == COMMANDCMD)) { 748 jp = makejob(cmd, 1); 749 mode = cmd->ncmd.backgnd; 750 if (flags & EV_BACKCMD) { 751 mode = FORK_NOJOB; 752 if (pipe(pip) < 0) 753 error("Pipe call failed: %s", strerror(errno)); 754 } 755 if (forkshell(jp, cmd, mode) != 0) 756 goto parent; /* at end of routine */ 757 if (flags & EV_BACKCMD) { 758 FORCEINTON; 759 close(pip[0]); 760 if (pip[1] != 1) { 761 dup2(pip[1], 1); 762 close(pip[1]); 763 } 764 } 765 flags |= EV_EXIT; 766 } 767 768 /* This is the child process if a fork occurred. */ 769 /* Execute the command. */ 770 if (cmdentry.cmdtype == CMDFUNCTION) { 771 #ifdef DEBUG 772 trputs("Shell function: "); trargs(argv); 773 #endif 774 redirect(cmd->ncmd.redirect, REDIR_PUSH); 775 saveparam = shellparam; 776 shellparam.malloc = 0; 777 shellparam.reset = 1; 778 shellparam.nparam = argc - 1; 779 shellparam.p = argv + 1; 780 shellparam.optnext = NULL; 781 INTOFF; 782 savelocalvars = localvars; 783 localvars = NULL; 784 reffunc(cmdentry.u.func); 785 savehandler = handler; 786 if (setjmp(jmploc.loc)) { 787 if (exception == EXSHELLPROC) 788 freeparam(&saveparam); 789 else { 790 freeparam(&shellparam); 791 shellparam = saveparam; 792 } 793 unreffunc(cmdentry.u.func); 794 poplocalvars(); 795 localvars = savelocalvars; 796 handler = savehandler; 797 longjmp(handler->loc, 1); 798 } 799 handler = &jmploc; 800 INTON; 801 for (sp = varlist.list ; sp ; sp = sp->next) 802 mklocal(sp->text); 803 funcnest++; 804 exitstatus = oexitstatus; 805 if (flags & EV_TESTED) 806 evaltree(getfuncnode(cmdentry.u.func), EV_TESTED); 807 else 808 evaltree(getfuncnode(cmdentry.u.func), 0); 809 funcnest--; 810 INTOFF; 811 unreffunc(cmdentry.u.func); 812 poplocalvars(); 813 localvars = savelocalvars; 814 freeparam(&shellparam); 815 shellparam = saveparam; 816 handler = savehandler; 817 popredir(); 818 INTON; 819 if (evalskip == SKIPFUNC) { 820 evalskip = 0; 821 skipcount = 0; 822 } 823 if (flags & EV_EXIT) 824 exitshell(exitstatus); 825 } else if (cmdentry.cmdtype == CMDBUILTIN) { 826 #ifdef DEBUG 827 trputs("builtin command: "); trargs(argv); 828 #endif 829 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 830 if (flags == EV_BACKCMD) { 831 memout.nleft = 0; 832 memout.nextc = memout.buf; 833 memout.bufsize = 64; 834 mode |= REDIR_BACKQ; 835 } 836 savecmdname = commandname; 837 savetopfile = getcurrentfile(); 838 cmdenviron = varlist.list; 839 e = -1; 840 savehandler = handler; 841 if (setjmp(jmploc.loc)) { 842 e = exception; 843 exitstatus = (e == EXINT)? SIGINT+128 : 2; 844 goto cmddone; 845 } 846 handler = &jmploc; 847 redirect(cmd->ncmd.redirect, mode); 848 if (cmdentry.special) 849 listsetvar(cmdenviron); 850 commandname = argv[0]; 851 argptr = argv + 1; 852 optptr = NULL; /* initialize nextopt */ 853 builtin_flags = flags; 854 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 855 flushall(); 856 cmddone: 857 cmdenviron = NULL; 858 out1 = &output; 859 out2 = &errout; 860 freestdout(); 861 if (e != EXSHELLPROC) { 862 commandname = savecmdname; 863 if (flags & EV_EXIT) { 864 exitshell(exitstatus); 865 } 866 } 867 handler = savehandler; 868 if (e != -1) { 869 if ((e != EXERROR && e != EXEXEC) 870 || cmdentry.special) 871 exraise(e); 872 popfilesupto(savetopfile); 873 FORCEINTON; 874 } 875 if (cmdentry.u.index != EXECCMD) 876 popredir(); 877 if (flags == EV_BACKCMD) { 878 backcmd->buf = memout.buf; 879 backcmd->nleft = memout.nextc - memout.buf; 880 memout.buf = NULL; 881 } 882 } else { 883 #ifdef DEBUG 884 trputs("normal command: "); trargs(argv); 885 #endif 886 redirect(cmd->ncmd.redirect, 0); 887 for (sp = varlist.list ; sp ; sp = sp->next) 888 setvareq(sp->text, VEXPORT|VSTACK); 889 envp = environment(); 890 shellexec(argv, envp, pathval(), cmdentry.u.index); 891 /*NOTREACHED*/ 892 } 893 goto out; 894 895 parent: /* parent process gets here (if we forked) */ 896 if (mode == 0) { /* argument to fork */ 897 INTOFF; 898 exitstatus = waitforjob(jp, &realstatus); 899 INTON; 900 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 901 evalskip = SKIPBREAK; 902 skipcount = loopnest; 903 } 904 } else if (mode == 2) { 905 backcmd->fd = pip[0]; 906 close(pip[1]); 907 backcmd->jp = jp; 908 } 909 910 out: 911 if (lastarg) 912 setvar("_", lastarg, 0); 913 if (do_clearcmdentry) 914 clearcmdentry(0); 915 popstackmark(&smark); 916 } 917 918 919 920 /* 921 * Search for a command. This is called before we fork so that the 922 * location of the command will be available in the parent as well as 923 * the child. The check for "goodname" is an overly conservative 924 * check that the name will not be subject to expansion. 925 */ 926 927 STATIC void 928 prehash(union node *n) 929 { 930 struct cmdentry entry; 931 932 if (n && n->type == NCMD && n->ncmd.args) 933 if (goodname(n->ncmd.args->narg.text)) 934 find_command(n->ncmd.args->narg.text, &entry, 0, 935 pathval()); 936 } 937 938 939 940 /* 941 * Builtin commands. Builtin commands whose functions are closely 942 * tied to evaluation are implemented here. 943 */ 944 945 /* 946 * No command given, or a bltin command with no arguments. 947 */ 948 949 int 950 bltincmd(int argc __unused, char **argv __unused) 951 { 952 /* 953 * Preserve exitstatus of a previous possible redirection 954 * as POSIX mandates 955 */ 956 return exitstatus; 957 } 958 959 960 /* 961 * Handle break and continue commands. Break, continue, and return are 962 * all handled by setting the evalskip flag. The evaluation routines 963 * above all check this flag, and if it is set they start skipping 964 * commands rather than executing them. The variable skipcount is 965 * the number of loops to break/continue, or the number of function 966 * levels to return. (The latter is always 1.) It should probably 967 * be an error to break out of more loops than exist, but it isn't 968 * in the standard shell so we don't make it one here. 969 */ 970 971 int 972 breakcmd(int argc, char **argv) 973 { 974 int n = argc > 1 ? number(argv[1]) : 1; 975 976 if (n > loopnest) 977 n = loopnest; 978 if (n > 0) { 979 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 980 skipcount = n; 981 } 982 return 0; 983 } 984 985 /* 986 * The `command' command. 987 */ 988 int 989 commandcmd(int argc, char **argv) 990 { 991 static char stdpath[] = _PATH_STDPATH; 992 struct jmploc loc, *old; 993 struct strlist *sp; 994 char *path; 995 int ch; 996 int cmd = -1; 997 998 for (sp = cmdenviron; sp ; sp = sp->next) 999 setvareq(sp->text, VEXPORT|VSTACK); 1000 path = pathval(); 1001 1002 optind = optreset = 1; 1003 opterr = 0; 1004 while ((ch = getopt(argc, argv, "pvV")) != -1) { 1005 switch (ch) { 1006 case 'p': 1007 path = stdpath; 1008 break; 1009 case 'v': 1010 cmd = TYPECMD_SMALLV; 1011 break; 1012 case 'V': 1013 cmd = TYPECMD_BIGV; 1014 break; 1015 case '?': 1016 default: 1017 error("unknown option: -%c", optopt); 1018 } 1019 } 1020 argc -= optind; 1021 argv += optind; 1022 1023 if (cmd != -1) { 1024 if (argc != 1) 1025 error("wrong number of arguments"); 1026 return typecmd_impl(2, argv - 1, cmd); 1027 } 1028 if (argc != 0) { 1029 old = handler; 1030 handler = &loc; 1031 if (setjmp(handler->loc) == 0) 1032 shellexec(argv, environment(), path, 0); 1033 handler = old; 1034 if (exception == EXEXEC) 1035 exit(exerrno); 1036 exraise(exception); 1037 } 1038 1039 /* 1040 * Do nothing successfully if no command was specified; 1041 * ksh also does this. 1042 */ 1043 exit(0); 1044 } 1045 1046 1047 /* 1048 * The return command. 1049 */ 1050 1051 int 1052 returncmd(int argc, char **argv) 1053 { 1054 int ret = argc > 1 ? number(argv[1]) : oexitstatus; 1055 1056 if (funcnest) { 1057 evalskip = SKIPFUNC; 1058 skipcount = 1; 1059 } else { 1060 /* skip the rest of the file */ 1061 evalskip = SKIPFILE; 1062 skipcount = 1; 1063 } 1064 return ret; 1065 } 1066 1067 1068 int 1069 falsecmd(int argc __unused, char **argv __unused) 1070 { 1071 return 1; 1072 } 1073 1074 1075 int 1076 truecmd(int argc __unused, char **argv __unused) 1077 { 1078 return 0; 1079 } 1080 1081 1082 int 1083 execcmd(int argc, char **argv) 1084 { 1085 if (argc > 1) { 1086 struct strlist *sp; 1087 1088 iflag = 0; /* exit on error */ 1089 mflag = 0; 1090 optschanged(); 1091 for (sp = cmdenviron; sp ; sp = sp->next) 1092 setvareq(sp->text, VEXPORT|VSTACK); 1093 shellexec(argv + 1, environment(), pathval(), 0); 1094 1095 } 1096 return 0; 1097 } 1098 1099 1100 int 1101 timescmd(int argc __unused, char **argv __unused) 1102 { 1103 struct rusage ru; 1104 long shumins, shsmins, chumins, chsmins; 1105 double shusecs, shssecs, chusecs, chssecs; 1106 1107 if (getrusage(RUSAGE_SELF, &ru) < 0) 1108 return 1; 1109 shumins = ru.ru_utime.tv_sec / 60; 1110 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1111 shsmins = ru.ru_stime.tv_sec / 60; 1112 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1113 if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1114 return 1; 1115 chumins = ru.ru_utime.tv_sec / 60; 1116 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1117 chsmins = ru.ru_stime.tv_sec / 60; 1118 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1119 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1120 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1121 return 0; 1122 } 1123