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