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