1 /* $NetBSD: eval.c,v 1.21 1995/05/11 21:28:56 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)eval.c 8.4 (Berkeley) 5/4/95"; 42 #else 43 static char rcsid[] = "$NetBSD: eval.c,v 1.21 1995/05/11 21:28:56 christos Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include <signal.h> 48 #include <unistd.h> 49 50 /* 51 * Evaluate a command. 52 */ 53 54 #include "shell.h" 55 #include "nodes.h" 56 #include "syntax.h" 57 #include "expand.h" 58 #include "parser.h" 59 #include "jobs.h" 60 #include "eval.h" 61 #include "builtins.h" 62 #include "options.h" 63 #include "exec.h" 64 #include "redir.h" 65 #include "input.h" 66 #include "output.h" 67 #include "trap.h" 68 #include "var.h" 69 #include "memalloc.h" 70 #include "error.h" 71 #include "show.h" 72 #include "mystring.h" 73 #ifndef NO_HISTORY 74 #include "myhistedit.h" 75 #endif 76 77 78 /* flags in argument to evaltree */ 79 #define EV_EXIT 01 /* exit after evaluating tree */ 80 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ 81 #define EV_BACKCMD 04 /* command executing within back quotes */ 82 83 84 /* reasons for skipping commands (see comment on breakcmd routine) */ 85 #define SKIPBREAK 1 86 #define SKIPCONT 2 87 #define SKIPFUNC 3 88 89 MKINIT int evalskip; /* set if we are skipping commands */ 90 STATIC int skipcount; /* number of levels to skip */ 91 MKINIT int loopnest; /* current loop nesting level */ 92 int funcnest; /* depth of function calls */ 93 94 95 char *commandname; 96 struct strlist *cmdenviron; 97 int exitstatus; /* exit status of last command */ 98 99 100 STATIC void evalloop __P((union node *)); 101 STATIC void evalfor __P((union node *)); 102 STATIC void evalcase __P((union node *, int)); 103 STATIC void evalsubshell __P((union node *, int)); 104 STATIC void expredir __P((union node *)); 105 STATIC void evalpipe __P((union node *)); 106 STATIC void evalcommand __P((union node *, int, struct backcmd *)); 107 STATIC void prehash __P((union node *)); 108 109 110 /* 111 * Called to reset things after an exception. 112 */ 113 114 #ifdef mkinit 115 INCLUDE "eval.h" 116 117 RESET { 118 evalskip = 0; 119 loopnest = 0; 120 funcnest = 0; 121 } 122 123 SHELLPROC { 124 exitstatus = 0; 125 } 126 #endif 127 128 129 130 /* 131 * The eval commmand. 132 */ 133 134 int 135 evalcmd(argc, argv) 136 int argc; 137 char **argv; 138 { 139 char *p; 140 char *concat; 141 char **ap; 142 143 if (argc > 1) { 144 p = argv[1]; 145 if (argc > 2) { 146 STARTSTACKSTR(concat); 147 ap = argv + 2; 148 for (;;) { 149 while (*p) 150 STPUTC(*p++, concat); 151 if ((p = *ap++) == NULL) 152 break; 153 STPUTC(' ', concat); 154 } 155 STPUTC('\0', concat); 156 p = grabstackstr(concat); 157 } 158 evalstring(p); 159 } 160 return exitstatus; 161 } 162 163 164 /* 165 * Execute a command or commands contained in a string. 166 */ 167 168 void 169 evalstring(s) 170 char *s; 171 { 172 union node *n; 173 struct stackmark smark; 174 175 setstackmark(&smark); 176 setinputstring(s, 1); 177 while ((n = parsecmd(0)) != NEOF) { 178 evaltree(n, 0); 179 popstackmark(&smark); 180 } 181 popfile(); 182 popstackmark(&smark); 183 } 184 185 186 187 /* 188 * Evaluate a parse tree. The value is left in the global variable 189 * exitstatus. 190 */ 191 192 void 193 evaltree(n, flags) 194 union node *n; 195 int flags; 196 { 197 if (n == NULL) { 198 TRACE(("evaltree(NULL) called\n")); 199 exitstatus = 0; 200 goto out; 201 } 202 #ifndef NO_HISTORY 203 displayhist = 1; /* show history substitutions done with fc */ 204 #endif 205 TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type)); 206 switch (n->type) { 207 case NSEMI: 208 evaltree(n->nbinary.ch1, 0); 209 if (evalskip) 210 goto out; 211 evaltree(n->nbinary.ch2, flags); 212 break; 213 case NAND: 214 evaltree(n->nbinary.ch1, EV_TESTED); 215 if (evalskip || exitstatus != 0) 216 goto out; 217 evaltree(n->nbinary.ch2, flags); 218 break; 219 case NOR: 220 evaltree(n->nbinary.ch1, EV_TESTED); 221 if (evalskip || exitstatus == 0) 222 goto out; 223 evaltree(n->nbinary.ch2, flags); 224 break; 225 case NREDIR: 226 expredir(n->nredir.redirect); 227 redirect(n->nredir.redirect, REDIR_PUSH); 228 evaltree(n->nredir.n, flags); 229 popredir(); 230 break; 231 case NSUBSHELL: 232 evalsubshell(n, flags); 233 break; 234 case NBACKGND: 235 evalsubshell(n, flags); 236 break; 237 case NIF: { 238 int status = 0; 239 240 evaltree(n->nif.test, EV_TESTED); 241 if (evalskip) 242 goto out; 243 if (exitstatus == 0) { 244 evaltree(n->nif.ifpart, flags); 245 status = exitstatus; 246 } else if (n->nif.elsepart) { 247 evaltree(n->nif.elsepart, flags); 248 status = exitstatus; 249 } 250 exitstatus = status; 251 break; 252 } 253 case NWHILE: 254 case NUNTIL: 255 evalloop(n); 256 break; 257 case NFOR: 258 evalfor(n); 259 break; 260 case NCASE: 261 evalcase(n, flags); 262 break; 263 case NDEFUN: 264 defun(n->narg.text, n->narg.next); 265 exitstatus = 0; 266 break; 267 case NNOT: 268 evaltree(n->nnot.com, EV_TESTED); 269 exitstatus = !exitstatus; 270 break; 271 272 case NPIPE: 273 evalpipe(n); 274 break; 275 case NCMD: 276 evalcommand(n, flags, (struct backcmd *)NULL); 277 break; 278 default: 279 out1fmt("Node type = %d\n", n->type); 280 flushout(&output); 281 break; 282 } 283 out: 284 if (pendingsigs) 285 dotrap(); 286 if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED))) 287 exitshell(exitstatus); 288 } 289 290 291 STATIC void 292 evalloop(n) 293 union node *n; 294 { 295 int status; 296 297 loopnest++; 298 status = 0; 299 for (;;) { 300 evaltree(n->nbinary.ch1, EV_TESTED); 301 if (evalskip) { 302 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 303 evalskip = 0; 304 continue; 305 } 306 if (evalskip == SKIPBREAK && --skipcount <= 0) 307 evalskip = 0; 308 break; 309 } 310 if (n->type == NWHILE) { 311 if (exitstatus != 0) 312 break; 313 } else { 314 if (exitstatus == 0) 315 break; 316 } 317 evaltree(n->nbinary.ch2, 0); 318 status = exitstatus; 319 if (evalskip) 320 goto skipping; 321 } 322 loopnest--; 323 exitstatus = status; 324 } 325 326 327 328 STATIC void 329 evalfor(n) 330 union node *n; 331 { 332 struct arglist arglist; 333 union node *argp; 334 struct strlist *sp; 335 struct stackmark smark; 336 337 setstackmark(&smark); 338 arglist.lastp = &arglist.list; 339 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 340 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 341 if (evalskip) 342 goto out; 343 } 344 *arglist.lastp = NULL; 345 346 exitstatus = 0; 347 loopnest++; 348 for (sp = arglist.list ; sp ; sp = sp->next) { 349 setvar(n->nfor.var, sp->text, 0); 350 evaltree(n->nfor.body, 0); 351 if (evalskip) { 352 if (evalskip == SKIPCONT && --skipcount <= 0) { 353 evalskip = 0; 354 continue; 355 } 356 if (evalskip == SKIPBREAK && --skipcount <= 0) 357 evalskip = 0; 358 break; 359 } 360 } 361 loopnest--; 362 out: 363 popstackmark(&smark); 364 } 365 366 367 368 STATIC void 369 evalcase(n, flags) 370 union node *n; 371 int flags; 372 { 373 union node *cp; 374 union node *patp; 375 struct arglist arglist; 376 struct stackmark smark; 377 378 setstackmark(&smark); 379 arglist.lastp = &arglist.list; 380 expandarg(n->ncase.expr, &arglist, EXP_TILDE); 381 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 382 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 383 if (casematch(patp, arglist.list->text)) { 384 if (evalskip == 0) { 385 evaltree(cp->nclist.body, flags); 386 } 387 goto out; 388 } 389 } 390 } 391 out: 392 popstackmark(&smark); 393 } 394 395 396 397 /* 398 * Kick off a subshell to evaluate a tree. 399 */ 400 401 STATIC void 402 evalsubshell(n, flags) 403 union node *n; 404 int flags; 405 { 406 struct job *jp; 407 int backgnd = (n->type == NBACKGND); 408 409 expredir(n->nredir.redirect); 410 jp = makejob(n, 1); 411 if (forkshell(jp, n, backgnd) == 0) { 412 if (backgnd) 413 flags &=~ EV_TESTED; 414 redirect(n->nredir.redirect, 0); 415 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 416 } 417 if (! backgnd) { 418 INTOFF; 419 exitstatus = waitforjob(jp); 420 INTON; 421 } 422 } 423 424 425 426 /* 427 * Compute the names of the files in a redirection list. 428 */ 429 430 STATIC void 431 expredir(n) 432 union node *n; 433 { 434 register union node *redir; 435 436 for (redir = n ; redir ; redir = redir->nfile.next) { 437 struct arglist fn; 438 fn.lastp = &fn.list; 439 switch (redir->type) { 440 case NFROM: 441 case NTO: 442 case NAPPEND: 443 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 444 redir->nfile.expfname = fn.list->text; 445 break; 446 case NFROMFD: 447 case NTOFD: 448 if (redir->ndup.vname) { 449 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); 450 fixredir(redir, fn.list->text, 1); 451 } 452 break; 453 } 454 } 455 } 456 457 458 459 /* 460 * Evaluate a pipeline. All the processes in the pipeline are children 461 * of the process creating the pipeline. (This differs from some versions 462 * of the shell, which make the last process in a pipeline the parent 463 * of all the rest.) 464 */ 465 466 STATIC void 467 evalpipe(n) 468 union node *n; 469 { 470 struct job *jp; 471 struct nodelist *lp; 472 int pipelen; 473 int prevfd; 474 int pip[2]; 475 476 TRACE(("evalpipe(0x%lx) called\n", (long)n)); 477 pipelen = 0; 478 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 479 pipelen++; 480 INTOFF; 481 jp = makejob(n, pipelen); 482 prevfd = -1; 483 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 484 prehash(lp->n); 485 pip[1] = -1; 486 if (lp->next) { 487 if (pipe(pip) < 0) { 488 close(prevfd); 489 error("Pipe call failed"); 490 } 491 } 492 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 493 INTON; 494 if (prevfd > 0) { 495 close(0); 496 copyfd(prevfd, 0); 497 close(prevfd); 498 } 499 if (pip[1] >= 0) { 500 close(pip[0]); 501 if (pip[1] != 1) { 502 close(1); 503 copyfd(pip[1], 1); 504 close(pip[1]); 505 } 506 } 507 evaltree(lp->n, EV_EXIT); 508 } 509 if (prevfd >= 0) 510 close(prevfd); 511 prevfd = pip[0]; 512 close(pip[1]); 513 } 514 INTON; 515 if (n->npipe.backgnd == 0) { 516 INTOFF; 517 exitstatus = waitforjob(jp); 518 TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 519 INTON; 520 } 521 } 522 523 524 525 /* 526 * Execute a command inside back quotes. If it's a builtin command, we 527 * want to save its output in a block obtained from malloc. Otherwise 528 * we fork off a subprocess and get the output of the command via a pipe. 529 * Should be called with interrupts off. 530 */ 531 532 void 533 evalbackcmd(n, result) 534 union node *n; 535 struct backcmd *result; 536 { 537 int pip[2]; 538 struct job *jp; 539 struct stackmark smark; /* unnecessary */ 540 541 setstackmark(&smark); 542 result->fd = -1; 543 result->buf = NULL; 544 result->nleft = 0; 545 result->jp = NULL; 546 exitstatus = 0; 547 if (n == NULL) 548 goto out; 549 if (n->type == NCMD) { 550 evalcommand(n, EV_BACKCMD, result); 551 } else { 552 if (pipe(pip) < 0) 553 error("Pipe call failed"); 554 jp = makejob(n, 1); 555 if (forkshell(jp, n, FORK_NOJOB) == 0) { 556 FORCEINTON; 557 close(pip[0]); 558 if (pip[1] != 1) { 559 close(1); 560 copyfd(pip[1], 1); 561 close(pip[1]); 562 } 563 evaltree(n, EV_EXIT); 564 } 565 close(pip[1]); 566 result->fd = pip[0]; 567 result->jp = jp; 568 } 569 out: 570 popstackmark(&smark); 571 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", 572 result->fd, result->buf, result->nleft, result->jp)); 573 } 574 575 576 577 /* 578 * Execute a simple command. 579 */ 580 581 STATIC void 582 evalcommand(cmd, flags, backcmd) 583 union node *cmd; 584 int flags; 585 struct backcmd *backcmd; 586 { 587 struct stackmark smark; 588 union node *argp; 589 struct arglist arglist; 590 struct arglist varlist; 591 char **argv; 592 int argc; 593 char **envp; 594 int varflag; 595 struct strlist *sp; 596 int mode; 597 int pip[2]; 598 struct cmdentry cmdentry; 599 struct job *jp; 600 struct jmploc jmploc; 601 struct jmploc *volatile savehandler; 602 char *volatile savecmdname; 603 volatile struct shparam saveparam; 604 struct localvar *volatile savelocalvars; 605 volatile int e; 606 char *lastarg; 607 #if __GNUC__ 608 /* Avoid longjmp clobbering */ 609 (void) &argv; 610 (void) &argc; 611 (void) &lastarg; 612 (void) &flags; 613 #endif 614 615 /* First expand the arguments. */ 616 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); 617 setstackmark(&smark); 618 arglist.lastp = &arglist.list; 619 varlist.lastp = &varlist.list; 620 varflag = 1; 621 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 622 char *p = argp->narg.text; 623 if (varflag && is_name(*p)) { 624 do { 625 p++; 626 } while (is_in_name(*p)); 627 if (*p == '=') { 628 expandarg(argp, &varlist, EXP_VARTILDE); 629 continue; 630 } 631 } 632 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 633 varflag = 0; 634 } 635 *arglist.lastp = NULL; 636 *varlist.lastp = NULL; 637 expredir(cmd->ncmd.redirect); 638 argc = 0; 639 for (sp = arglist.list ; sp ; sp = sp->next) 640 argc++; 641 argv = stalloc(sizeof (char *) * (argc + 1)); 642 643 for (sp = arglist.list ; sp ; sp = sp->next) { 644 TRACE(("evalcommand arg: %s\n", sp->text)); 645 *argv++ = sp->text; 646 } 647 *argv = NULL; 648 lastarg = NULL; 649 if (iflag && funcnest == 0 && argc > 0) 650 lastarg = argv[-1]; 651 argv -= argc; 652 653 /* Print the command if xflag is set. */ 654 if (xflag) { 655 outc('+', &errout); 656 for (sp = varlist.list ; sp ; sp = sp->next) { 657 outc(' ', &errout); 658 out2str(sp->text); 659 } 660 for (sp = arglist.list ; sp ; sp = sp->next) { 661 outc(' ', &errout); 662 out2str(sp->text); 663 } 664 outc('\n', &errout); 665 flushout(&errout); 666 } 667 668 /* Now locate the command. */ 669 if (argc == 0) { 670 cmdentry.cmdtype = CMDBUILTIN; 671 cmdentry.u.index = BLTINCMD; 672 } else { 673 find_command(argv[0], &cmdentry, 1); 674 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */ 675 exitstatus = 2; 676 flushout(&errout); 677 return; 678 } 679 /* implement the bltin builtin here */ 680 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) { 681 for (;;) { 682 argv++; 683 if (--argc == 0) 684 break; 685 if ((cmdentry.u.index = find_builtin(*argv)) < 0) { 686 outfmt(&errout, "%s: not found\n", *argv); 687 exitstatus = 2; 688 flushout(&errout); 689 return; 690 } 691 if (cmdentry.u.index != BLTINCMD) 692 break; 693 } 694 } 695 } 696 697 /* Fork off a child process if necessary. */ 698 if (cmd->ncmd.backgnd 699 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0) 700 || ((flags & EV_BACKCMD) != 0 701 && (cmdentry.cmdtype != CMDBUILTIN 702 || cmdentry.u.index == DOTCMD 703 || cmdentry.u.index == EVALCMD))) { 704 jp = makejob(cmd, 1); 705 mode = cmd->ncmd.backgnd; 706 if (flags & EV_BACKCMD) { 707 mode = FORK_NOJOB; 708 if (pipe(pip) < 0) 709 error("Pipe call failed"); 710 } 711 if (forkshell(jp, cmd, mode) != 0) 712 goto parent; /* at end of routine */ 713 if (flags & EV_BACKCMD) { 714 FORCEINTON; 715 close(pip[0]); 716 if (pip[1] != 1) { 717 close(1); 718 copyfd(pip[1], 1); 719 close(pip[1]); 720 } 721 } 722 flags |= EV_EXIT; 723 } 724 725 /* This is the child process if a fork occurred. */ 726 /* Execute the command. */ 727 if (cmdentry.cmdtype == CMDFUNCTION) { 728 trputs("Shell function: "); trargs(argv); 729 redirect(cmd->ncmd.redirect, REDIR_PUSH); 730 saveparam = shellparam; 731 shellparam.malloc = 0; 732 shellparam.nparam = argc - 1; 733 shellparam.p = argv + 1; 734 shellparam.optnext = NULL; 735 INTOFF; 736 savelocalvars = localvars; 737 localvars = NULL; 738 INTON; 739 if (setjmp(jmploc.loc)) { 740 if (exception == EXSHELLPROC) 741 freeparam((struct shparam *)&saveparam); 742 else { 743 freeparam(&shellparam); 744 shellparam = saveparam; 745 } 746 poplocalvars(); 747 localvars = savelocalvars; 748 handler = savehandler; 749 longjmp(handler->loc, 1); 750 } 751 savehandler = handler; 752 handler = &jmploc; 753 for (sp = varlist.list ; sp ; sp = sp->next) 754 mklocal(sp->text); 755 funcnest++; 756 evaltree(cmdentry.u.func, 0); 757 funcnest--; 758 INTOFF; 759 poplocalvars(); 760 localvars = savelocalvars; 761 freeparam(&shellparam); 762 shellparam = saveparam; 763 handler = savehandler; 764 popredir(); 765 INTON; 766 if (evalskip == SKIPFUNC) { 767 evalskip = 0; 768 skipcount = 0; 769 } 770 if (flags & EV_EXIT) 771 exitshell(exitstatus); 772 } else if (cmdentry.cmdtype == CMDBUILTIN) { 773 trputs("builtin command: "); trargs(argv); 774 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 775 if (flags == EV_BACKCMD) { 776 memout.nleft = 0; 777 memout.nextc = memout.buf; 778 memout.bufsize = 64; 779 mode |= REDIR_BACKQ; 780 } 781 redirect(cmd->ncmd.redirect, mode); 782 savecmdname = commandname; 783 cmdenviron = varlist.list; 784 e = -1; 785 if (setjmp(jmploc.loc)) { 786 e = exception; 787 exitstatus = (e == EXINT)? SIGINT+128 : 2; 788 goto cmddone; 789 } 790 savehandler = handler; 791 handler = &jmploc; 792 commandname = argv[0]; 793 argptr = argv + 1; 794 optptr = NULL; /* initialize nextopt */ 795 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 796 flushall(); 797 cmddone: 798 out1 = &output; 799 out2 = &errout; 800 freestdout(); 801 if (e != EXSHELLPROC) { 802 commandname = savecmdname; 803 if (flags & EV_EXIT) { 804 exitshell(exitstatus); 805 } 806 } 807 handler = savehandler; 808 if (e != -1) { 809 if (e != EXERROR || cmdentry.u.index == BLTINCMD 810 || cmdentry.u.index == DOTCMD 811 || cmdentry.u.index == EVALCMD 812 #ifndef NO_HISTORY 813 || cmdentry.u.index == HISTCMD 814 #endif 815 || cmdentry.u.index == EXECCMD) 816 exraise(e); 817 FORCEINTON; 818 } 819 if (cmdentry.u.index != EXECCMD) 820 popredir(); 821 if (flags == EV_BACKCMD) { 822 backcmd->buf = memout.buf; 823 backcmd->nleft = memout.nextc - memout.buf; 824 memout.buf = NULL; 825 } 826 } else { 827 trputs("normal command: "); trargs(argv); 828 clearredir(); 829 redirect(cmd->ncmd.redirect, 0); 830 for (sp = varlist.list ; sp ; sp = sp->next) 831 setvareq(sp->text, VEXPORT|VSTACK); 832 envp = environment(); 833 shellexec(argv, envp, pathval(), cmdentry.u.index); 834 /*NOTREACHED*/ 835 } 836 goto out; 837 838 parent: /* parent process gets here (if we forked) */ 839 if (mode == 0) { /* argument to fork */ 840 INTOFF; 841 exitstatus = waitforjob(jp); 842 INTON; 843 } else if (mode == 2) { 844 backcmd->fd = pip[0]; 845 close(pip[1]); 846 backcmd->jp = jp; 847 } 848 849 out: 850 if (lastarg) 851 setvar("_", lastarg, 0); 852 popstackmark(&smark); 853 } 854 855 856 857 /* 858 * Search for a command. This is called before we fork so that the 859 * location of the command will be available in the parent as well as 860 * the child. The check for "goodname" is an overly conservative 861 * check that the name will not be subject to expansion. 862 */ 863 864 STATIC void 865 prehash(n) 866 union node *n; 867 { 868 struct cmdentry entry; 869 870 if (n->type == NCMD && n->ncmd.args) 871 if (goodname(n->ncmd.args->narg.text)) 872 find_command(n->ncmd.args->narg.text, &entry, 0); 873 } 874 875 876 877 /* 878 * Builtin commands. Builtin commands whose functions are closely 879 * tied to evaluation are implemented here. 880 */ 881 882 /* 883 * No command given, or a bltin command with no arguments. Set the 884 * specified variables. 885 */ 886 887 int 888 bltincmd(argc, argv) 889 int argc; 890 char **argv; 891 { 892 listsetvar(cmdenviron); 893 /* Preserve the exitstatus as POSIX.2 mandates */ 894 return exitstatus; 895 } 896 897 898 /* 899 * Handle break and continue commands. Break, continue, and return are 900 * all handled by setting the evalskip flag. The evaluation routines 901 * above all check this flag, and if it is set they start skipping 902 * commands rather than executing them. The variable skipcount is 903 * the number of loops to break/continue, or the number of function 904 * levels to return. (The latter is always 1.) It should probably 905 * be an error to break out of more loops than exist, but it isn't 906 * in the standard shell so we don't make it one here. 907 */ 908 909 int 910 breakcmd(argc, argv) 911 int argc; 912 char **argv; 913 { 914 int n; 915 916 n = 1; 917 if (argc > 1) 918 n = number(argv[1]); 919 if (n > loopnest) 920 n = loopnest; 921 if (n > 0) { 922 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 923 skipcount = n; 924 } 925 return 0; 926 } 927 928 929 /* 930 * The return command. 931 */ 932 933 int 934 returncmd(argc, argv) 935 int argc; 936 char **argv; 937 { 938 int ret; 939 940 ret = exitstatus; 941 if (argc > 1) 942 ret = number(argv[1]); 943 if (funcnest) { 944 evalskip = SKIPFUNC; 945 skipcount = 1; 946 } 947 return ret; 948 } 949 950 951 int 952 falsecmd(argc, argv) 953 int argc; 954 char **argv; 955 { 956 return 1; 957 } 958 959 960 int 961 truecmd(argc, argv) 962 int argc; 963 char **argv; 964 { 965 return 0; 966 } 967 968 969 int 970 execcmd(argc, argv) 971 int argc; 972 char **argv; 973 { 974 if (argc > 1) { 975 struct strlist *sp; 976 977 iflag = 0; /* exit on error */ 978 mflag = 0; 979 optschanged(); 980 for (sp = cmdenviron; sp ; sp = sp->next) 981 setvareq(sp->text, VEXPORT|VSTACK); 982 shellexec(argv + 1, environment(), pathval(), 0); 983 984 } 985 return 0; 986 } 987