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