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