eval.c revision 222716
1219019Sgabor/*- 2219019Sgabor * Copyright (c) 1993 3219019Sgabor * The Regents of the University of California. All rights reserved. 4219019Sgabor * 5219019Sgabor * This code is derived from software contributed to Berkeley by 6219019Sgabor * Kenneth Almquist. 7219019Sgabor * 8219019Sgabor * Redistribution and use in source and binary forms, with or without 9219019Sgabor * modification, are permitted provided that the following conditions 10219019Sgabor * are met: 11219019Sgabor * 1. Redistributions of source code must retain the above copyright 12219019Sgabor * notice, this list of conditions and the following disclaimer. 13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer in the 15219019Sgabor * documentation and/or other materials provided with the distribution. 16219019Sgabor * 4. Neither the name of the University nor the names of its contributors 17219019Sgabor * may be used to endorse or promote products derived from this software 18219019Sgabor * without specific prior written permission. 19219019Sgabor * 20219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30219019Sgabor * SUCH DAMAGE. 31219019Sgabor */ 32219019Sgabor 33219019Sgabor#ifndef lint 34219019Sgabor#if 0 35219019Sgaborstatic char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; 36219019Sgabor#endif 37219019Sgabor#endif /* not lint */ 38219019Sgabor#include <sys/cdefs.h> 39219019Sgabor__FBSDID("$FreeBSD: head/bin/sh/eval.c 222716 2011-06-05 14:13:15Z jilles $"); 40219019Sgabor 41219019Sgabor#include <paths.h> 42219019Sgabor#include <signal.h> 43219019Sgabor#include <stdlib.h> 44219019Sgabor#include <unistd.h> 45219019Sgabor#include <sys/resource.h> 46219019Sgabor#include <sys/wait.h> /* For WIFSIGNALED(status) */ 47219019Sgabor#include <errno.h> 48219019Sgabor 49219019Sgabor/* 50219019Sgabor * Evaluate a command. 51219019Sgabor */ 52219019Sgabor 53219019Sgabor#include "shell.h" 54219019Sgabor#include "nodes.h" 55219019Sgabor#include "syntax.h" 56219019Sgabor#include "expand.h" 57219019Sgabor#include "parser.h" 58219019Sgabor#include "jobs.h" 59219019Sgabor#include "eval.h" 60219019Sgabor#include "builtins.h" 61219019Sgabor#include "options.h" 62219019Sgabor#include "exec.h" 63219019Sgabor#include "redir.h" 64219019Sgabor#include "input.h" 65219019Sgabor#include "output.h" 66219019Sgabor#include "trap.h" 67219019Sgabor#include "var.h" 68219019Sgabor#include "memalloc.h" 69219019Sgabor#include "error.h" 70219019Sgabor#include "show.h" 71219019Sgabor#include "mystring.h" 72219019Sgabor#ifndef NO_HISTORY 73219019Sgabor#include "myhistedit.h" 74219019Sgabor#endif 75219019Sgabor 76219019Sgabor 77219019Sgaborint evalskip; /* set if we are skipping commands */ 78219019Sgaborstatic int skipcount; /* number of levels to skip */ 79219019SgaborMKINIT int loopnest; /* current loop nesting level */ 80219019Sgaborint funcnest; /* depth of function calls */ 81219019Sgaborstatic int builtin_flags; /* evalcommand flags for builtins */ 82219019Sgabor 83219019Sgabor 84219019Sgaborchar *commandname; 85219019Sgaborstruct strlist *cmdenviron; 86219019Sgaborint exitstatus; /* exit status of last command */ 87219019Sgaborint oexitstatus; /* saved exit status */ 88219019Sgabor 89219019Sgabor 90219019Sgaborstatic void evalloop(union node *, int); 91219019Sgaborstatic void evalfor(union node *, int); 92219019Sgaborstatic void evalcase(union node *, int); 93219019Sgaborstatic void evalsubshell(union node *, int); 94219019Sgaborstatic void evalredir(union node *, int); 95219019Sgaborstatic void expredir(union node *); 96219019Sgaborstatic void evalpipe(union node *); 97219019Sgaborstatic int is_valid_fast_cmdsubst(union node *n); 98219019Sgaborstatic void evalcommand(union node *, int, struct backcmd *); 99219019Sgaborstatic void prehash(union node *); 100219019Sgabor 101219019Sgabor 102219019Sgabor/* 103219019Sgabor * Called to reset things after an exception. 104219019Sgabor */ 105219019Sgabor 106219019Sgabor#ifdef mkinit 107219019SgaborINCLUDE "eval.h" 108219019Sgabor 109219019SgaborRESET { 110219019Sgabor evalskip = 0; 111219019Sgabor loopnest = 0; 112219019Sgabor funcnest = 0; 113219019Sgabor} 114219019Sgabor#endif 115219019Sgabor 116219019Sgabor 117250984Sed 118219019Sgabor/* 119219019Sgabor * The eval command. 120219019Sgabor */ 121219019Sgabor 122219019Sgaborint 123219019Sgaborevalcmd(int argc, char **argv) 124219019Sgabor{ 125219019Sgabor char *p; 126219019Sgabor char *concat; 127219019Sgabor char **ap; 128219019Sgabor 129219019Sgabor if (argc > 1) { 130219019Sgabor p = argv[1]; 131219019Sgabor if (argc > 2) { 132219019Sgabor STARTSTACKSTR(concat); 133219019Sgabor ap = argv + 2; 134219019Sgabor for (;;) { 135219019Sgabor STPUTS(p, concat); 136219019Sgabor if ((p = *ap++) == NULL) 137219019Sgabor break; 138219019Sgabor STPUTC(' ', concat); 139219019Sgabor } 140219019Sgabor STPUTC('\0', concat); 141219019Sgabor p = grabstackstr(concat); 142219019Sgabor } 143219019Sgabor evalstring(p, builtin_flags & EV_TESTED); 144219019Sgabor } else 145219019Sgabor exitstatus = 0; 146219019Sgabor return exitstatus; 147219019Sgabor} 148219019Sgabor 149219019Sgabor 150219019Sgabor/* 151219019Sgabor * Execute a command or commands contained in a string. 152219019Sgabor */ 153219019Sgabor 154219019Sgaborvoid 155219019Sgaborevalstring(char *s, int flags) 156219019Sgabor{ 157219019Sgabor union node *n; 158219019Sgabor struct stackmark smark; 159219019Sgabor int flags_exit; 160219019Sgabor int any; 161219019Sgabor 162219019Sgabor flags_exit = flags & EV_EXIT; 163219019Sgabor flags &= ~EV_EXIT; 164219019Sgabor any = 0; 165219019Sgabor setstackmark(&smark); 166219019Sgabor setinputstring(s, 1); 167219019Sgabor while ((n = parsecmd(0)) != NEOF) { 168219019Sgabor if (n != NULL && !nflag) { 169219019Sgabor if (flags_exit && preadateof()) 170219019Sgabor evaltree(n, flags | EV_EXIT); 171219019Sgabor else 172219019Sgabor evaltree(n, flags); 173219019Sgabor any = 1; 174219019Sgabor } 175219019Sgabor popstackmark(&smark); 176219019Sgabor } 177219019Sgabor popfile(); 178219019Sgabor popstackmark(&smark); 179219019Sgabor if (!any) 180219019Sgabor exitstatus = 0; 181219019Sgabor if (flags_exit) 182219019Sgabor exraise(EXEXIT); 183219019Sgabor} 184219019Sgabor 185219019Sgabor 186219019Sgabor/* 187219019Sgabor * Evaluate a parse tree. The value is left in the global variable 188219019Sgabor * exitstatus. 189219019Sgabor */ 190219019Sgabor 191219019Sgaborvoid 192219019Sgaborevaltree(union node *n, int flags) 193219019Sgabor{ 194219019Sgabor int do_etest; 195219019Sgabor union node *next; 196219019Sgabor 197219019Sgabor do_etest = 0; 198219019Sgabor if (n == NULL) { 199219019Sgabor TRACE(("evaltree(NULL) called\n")); 200219019Sgabor exitstatus = 0; 201219019Sgabor goto out; 202219019Sgabor } 203219019Sgabor do { 204219019Sgabor next = NULL; 205219019Sgabor#ifndef NO_HISTORY 206219019Sgabor displayhist = 1; /* show history substitutions done with fc */ 207219019Sgabor#endif 208219019Sgabor TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type)); 209219019Sgabor switch (n->type) { 210219019Sgabor case NSEMI: 211219019Sgabor evaltree(n->nbinary.ch1, flags & ~EV_EXIT); 212219019Sgabor if (evalskip) 213219019Sgabor goto out; 214219019Sgabor next = n->nbinary.ch2; 215219019Sgabor break; 216219019Sgabor case NAND: 217219019Sgabor evaltree(n->nbinary.ch1, EV_TESTED); 218219019Sgabor if (evalskip || exitstatus != 0) { 219219019Sgabor goto out; 220219019Sgabor } 221219019Sgabor next = n->nbinary.ch2; 222219019Sgabor break; 223219019Sgabor case NOR: 224219019Sgabor evaltree(n->nbinary.ch1, EV_TESTED); 225219019Sgabor if (evalskip || exitstatus == 0) 226219019Sgabor goto out; 227219019Sgabor next = n->nbinary.ch2; 228219019Sgabor break; 229219019Sgabor case NREDIR: 230219019Sgabor evalredir(n, flags); 231219019Sgabor break; 232219019Sgabor case NSUBSHELL: 233219019Sgabor evalsubshell(n, flags); 234219019Sgabor do_etest = !(flags & EV_TESTED); 235219019Sgabor break; 236219019Sgabor case NBACKGND: 237219019Sgabor evalsubshell(n, flags); 238219019Sgabor break; 239219019Sgabor case NIF: { 240219019Sgabor evaltree(n->nif.test, EV_TESTED); 241219019Sgabor if (evalskip) 242219019Sgabor goto out; 243219019Sgabor if (exitstatus == 0) 244219019Sgabor next = n->nif.ifpart; 245219019Sgabor else if (n->nif.elsepart) 246219019Sgabor next = n->nif.elsepart; 247219019Sgabor else 248219019Sgabor exitstatus = 0; 249219019Sgabor break; 250219019Sgabor } 251219019Sgabor case NWHILE: 252219019Sgabor case NUNTIL: 253219019Sgabor evalloop(n, flags & ~EV_EXIT); 254219019Sgabor break; 255219019Sgabor case NFOR: 256219019Sgabor evalfor(n, flags & ~EV_EXIT); 257219019Sgabor break; 258219019Sgabor case NCASE: 259219019Sgabor evalcase(n, flags); 260219019Sgabor break; 261219019Sgabor case NDEFUN: 262219019Sgabor defun(n->narg.text, n->narg.next); 263219019Sgabor exitstatus = 0; 264219019Sgabor break; 265219019Sgabor case NNOT: 266219019Sgabor evaltree(n->nnot.com, EV_TESTED); 267219019Sgabor exitstatus = !exitstatus; 268219019Sgabor break; 269219019Sgabor 270219019Sgabor case NPIPE: 271219019Sgabor evalpipe(n); 272219019Sgabor do_etest = !(flags & EV_TESTED); 273219019Sgabor break; 274219019Sgabor case NCMD: 275219019Sgabor evalcommand(n, flags, (struct backcmd *)NULL); 276219019Sgabor do_etest = !(flags & EV_TESTED); 277219019Sgabor break; 278219019Sgabor default: 279219019Sgabor out1fmt("Node type = %d\n", n->type); 280219019Sgabor flushout(&output); 281219019Sgabor break; 282219019Sgabor } 283219019Sgabor n = next; 284219019Sgabor } while (n != NULL); 285219019Sgaborout: 286219019Sgabor if (pendingsigs) 287219019Sgabor dotrap(); 288219019Sgabor if (eflag && exitstatus != 0 && do_etest) 289219019Sgabor exitshell(exitstatus); 290219019Sgabor if (flags & EV_EXIT) 291219019Sgabor exraise(EXEXIT); 292219019Sgabor} 293219019Sgabor 294219019Sgabor 295219019Sgaborstatic void 296219019Sgaborevalloop(union node *n, int flags) 297219019Sgabor{ 298219019Sgabor int status; 299219019Sgabor 300219019Sgabor loopnest++; 301219019Sgabor status = 0; 302219019Sgabor for (;;) { 303219019Sgabor evaltree(n->nbinary.ch1, EV_TESTED); 304219019Sgabor if (evalskip) { 305219019Sgaborskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 306219019Sgabor evalskip = 0; 307219019Sgabor continue; 308219019Sgabor } 309219019Sgabor if (evalskip == SKIPBREAK && --skipcount <= 0) 310219019Sgabor evalskip = 0; 311219019Sgabor if (evalskip == SKIPFUNC || evalskip == SKIPFILE) 312219019Sgabor status = exitstatus; 313219019Sgabor break; 314219019Sgabor } 315219019Sgabor if (n->type == NWHILE) { 316219019Sgabor if (exitstatus != 0) 317219019Sgabor break; 318219019Sgabor } else { 319219019Sgabor if (exitstatus == 0) 320219019Sgabor break; 321219019Sgabor } 322219019Sgabor evaltree(n->nbinary.ch2, flags); 323219019Sgabor status = exitstatus; 324219019Sgabor if (evalskip) 325219019Sgabor goto skipping; 326219019Sgabor } 327219019Sgabor loopnest--; 328219019Sgabor exitstatus = status; 329219019Sgabor} 330219019Sgabor 331219019Sgabor 332219019Sgabor 333static void 334evalfor(union node *n, int flags) 335{ 336 struct arglist arglist; 337 union node *argp; 338 struct strlist *sp; 339 struct stackmark smark; 340 341 setstackmark(&smark); 342 arglist.lastp = &arglist.list; 343 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 344 oexitstatus = exitstatus; 345 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 346 if (evalskip) 347 goto out; 348 } 349 *arglist.lastp = NULL; 350 351 exitstatus = 0; 352 loopnest++; 353 for (sp = arglist.list ; sp ; sp = sp->next) { 354 setvar(n->nfor.var, sp->text, 0); 355 evaltree(n->nfor.body, flags); 356 if (evalskip) { 357 if (evalskip == SKIPCONT && --skipcount <= 0) { 358 evalskip = 0; 359 continue; 360 } 361 if (evalskip == SKIPBREAK && --skipcount <= 0) 362 evalskip = 0; 363 break; 364 } 365 } 366 loopnest--; 367out: 368 popstackmark(&smark); 369} 370 371 372 373static void 374evalcase(union node *n, int flags) 375{ 376 union node *cp; 377 union node *patp; 378 struct arglist arglist; 379 struct stackmark smark; 380 381 setstackmark(&smark); 382 arglist.lastp = &arglist.list; 383 oexitstatus = exitstatus; 384 exitstatus = 0; 385 expandarg(n->ncase.expr, &arglist, EXP_TILDE); 386 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 387 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 388 if (casematch(patp, arglist.list->text)) { 389 if (evalskip == 0) { 390 evaltree(cp->nclist.body, flags); 391 } 392 goto out; 393 } 394 } 395 } 396out: 397 popstackmark(&smark); 398} 399 400 401 402/* 403 * Kick off a subshell to evaluate a tree. 404 */ 405 406static void 407evalsubshell(union node *n, int flags) 408{ 409 struct job *jp; 410 int backgnd = (n->type == NBACKGND); 411 412 oexitstatus = exitstatus; 413 expredir(n->nredir.redirect); 414 if ((!backgnd && flags & EV_EXIT && !have_traps()) || 415 forkshell(jp = makejob(n, 1), n, backgnd) == 0) { 416 if (backgnd) 417 flags &=~ EV_TESTED; 418 redirect(n->nredir.redirect, 0); 419 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 420 } else if (! backgnd) { 421 INTOFF; 422 exitstatus = waitforjob(jp, (int *)NULL); 423 INTON; 424 } else 425 exitstatus = 0; 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 oexitstatus = exitstatus; 441 expredir(n->nredir.redirect); 442 savehandler = handler; 443 if (setjmp(jmploc.loc)) { 444 int e; 445 446 handler = savehandler; 447 e = exception; 448 popredir(); 449 if (e == EXERROR || e == EXEXEC) { 450 if (in_redirect) { 451 exitstatus = 2; 452 return; 453 } 454 } 455 longjmp(handler->loc, 1); 456 } else { 457 INTOFF; 458 handler = &jmploc; 459 redirect(n->nredir.redirect, REDIR_PUSH); 460 in_redirect = 0; 461 INTON; 462 evaltree(n->nredir.n, flags); 463 } 464 INTOFF; 465 handler = savehandler; 466 popredir(); 467 INTON; 468} 469 470 471/* 472 * Compute the names of the files in a redirection list. 473 */ 474 475static void 476expredir(union node *n) 477{ 478 union node *redir; 479 480 for (redir = n ; redir ; redir = redir->nfile.next) { 481 struct arglist fn; 482 fn.lastp = &fn.list; 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 if (pip[1] != -1) 557 close(pip[1]); 558 } 559 INTON; 560 if (n->npipe.backgnd == 0) { 561 INTOFF; 562 exitstatus = waitforjob(jp, (int *)NULL); 563 TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 564 INTON; 565 } else 566 exitstatus = 0; 567} 568 569 570 571static int 572is_valid_fast_cmdsubst(union node *n) 573{ 574 union node *argp; 575 576 if (n->type != NCMD) 577 return 0; 578 for (argp = n->ncmd.args ; argp ; argp = argp->narg.next) 579 if (expandhassideeffects(argp->narg.text)) 580 return 0; 581 return 1; 582} 583 584/* 585 * Execute a command inside back quotes. If it's a builtin command, we 586 * want to save its output in a block obtained from malloc. Otherwise 587 * we fork off a subprocess and get the output of the command via a pipe. 588 * Should be called with interrupts off. 589 */ 590 591void 592evalbackcmd(union node *n, struct backcmd *result) 593{ 594 int pip[2]; 595 struct job *jp; 596 struct stackmark smark; /* unnecessary */ 597 struct jmploc jmploc; 598 struct jmploc *savehandler; 599 600 setstackmark(&smark); 601 result->fd = -1; 602 result->buf = NULL; 603 result->nleft = 0; 604 result->jp = NULL; 605 if (n == NULL) { 606 exitstatus = 0; 607 goto out; 608 } 609 if (is_valid_fast_cmdsubst(n)) { 610 exitstatus = oexitstatus; 611 savehandler = handler; 612 if (setjmp(jmploc.loc)) { 613 if (exception == EXERROR || exception == EXEXEC) 614 exitstatus = 2; 615 else if (exception != 0) { 616 handler = savehandler; 617 longjmp(handler->loc, 1); 618 } 619 } else { 620 handler = &jmploc; 621 evalcommand(n, EV_BACKCMD, result); 622 } 623 handler = savehandler; 624 } else { 625 exitstatus = 0; 626 if (pipe(pip) < 0) 627 error("Pipe call failed: %s", strerror(errno)); 628 jp = makejob(n, 1); 629 if (forkshell(jp, n, FORK_NOJOB) == 0) { 630 FORCEINTON; 631 close(pip[0]); 632 if (pip[1] != 1) { 633 dup2(pip[1], 1); 634 close(pip[1]); 635 } 636 evaltree(n, EV_EXIT); 637 } 638 close(pip[1]); 639 result->fd = pip[0]; 640 result->jp = jp; 641 } 642out: 643 popstackmark(&smark); 644 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 645 result->fd, result->buf, result->nleft, result->jp)); 646} 647 648/* 649 * Check if a builtin can safely be executed in the same process, 650 * even though it should be in a subshell (command substitution). 651 * Note that jobid, jobs, times and trap can show information not 652 * available in a child process; this is deliberate. 653 * The arguments should already have been expanded. 654 */ 655static int 656safe_builtin(int idx, int argc, char **argv) 657{ 658 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD || 659 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD || 660 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD || 661 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD || 662 idx == TYPECMD) 663 return (1); 664 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD || 665 idx == UMASKCMD) 666 return (argc <= 1 || (argc == 2 && argv[1][0] == '-')); 667 if (idx == SETCMD) 668 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' || 669 argv[1][0] == '+') && argv[1][1] == 'o' && 670 argv[1][2] == '\0')); 671 return (0); 672} 673 674/* 675 * Execute a simple command. 676 * Note: This may or may not return if (flags & EV_EXIT). 677 */ 678 679static void 680evalcommand(union node *cmd, int flags, struct backcmd *backcmd) 681{ 682 struct stackmark smark; 683 union node *argp; 684 struct arglist arglist; 685 struct arglist varlist; 686 char **argv; 687 int argc; 688 char **envp; 689 int varflag; 690 struct strlist *sp; 691 int mode; 692 int pip[2]; 693 struct cmdentry cmdentry; 694 struct job *jp; 695 struct jmploc jmploc; 696 struct jmploc *savehandler; 697 char *savecmdname; 698 struct shparam saveparam; 699 struct localvar *savelocalvars; 700 struct parsefile *savetopfile; 701 volatile int e; 702 char *lastarg; 703 int realstatus; 704 int do_clearcmdentry; 705 const char *path = pathval(); 706 707 /* First expand the arguments. */ 708 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 709 setstackmark(&smark); 710 arglist.lastp = &arglist.list; 711 varlist.lastp = &varlist.list; 712 varflag = 1; 713 jp = NULL; 714 do_clearcmdentry = 0; 715 oexitstatus = exitstatus; 716 exitstatus = 0; 717 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 718 if (varflag && isassignment(argp->narg.text)) { 719 expandarg(argp, &varlist, EXP_VARTILDE); 720 continue; 721 } 722 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 723 varflag = 0; 724 } 725 *arglist.lastp = NULL; 726 *varlist.lastp = NULL; 727 expredir(cmd->ncmd.redirect); 728 argc = 0; 729 for (sp = arglist.list ; sp ; sp = sp->next) 730 argc++; 731 /* Add one slot at the beginning for tryexec(). */ 732 argv = stalloc(sizeof (char *) * (argc + 2)); 733 argv++; 734 735 for (sp = arglist.list ; sp ; sp = sp->next) { 736 TRACE(("evalcommand arg: %s\n", sp->text)); 737 *argv++ = sp->text; 738 } 739 *argv = NULL; 740 lastarg = NULL; 741 if (iflag && funcnest == 0 && argc > 0) 742 lastarg = argv[-1]; 743 argv -= argc; 744 745 /* Print the command if xflag is set. */ 746 if (xflag) { 747 char sep = 0; 748 const char *p; 749 out2str(ps4val()); 750 for (sp = varlist.list ; sp ; sp = sp->next) { 751 if (sep != 0) 752 out2c(' '); 753 p = strchr(sp->text, '='); 754 if (p != NULL) { 755 p++; 756 outbin(sp->text, p - sp->text, out2); 757 out2qstr(p); 758 } else 759 out2qstr(sp->text); 760 sep = ' '; 761 } 762 for (sp = arglist.list ; sp ; sp = sp->next) { 763 if (sep != 0) 764 out2c(' '); 765 /* Disambiguate command looking like assignment. */ 766 if (sp == arglist.list && 767 strchr(sp->text, '=') != NULL && 768 strchr(sp->text, '\'') == NULL) { 769 out2c('\''); 770 out2str(sp->text); 771 out2c('\''); 772 } else 773 out2qstr(sp->text); 774 sep = ' '; 775 } 776 out2c('\n'); 777 flushout(&errout); 778 } 779 780 /* Now locate the command. */ 781 if (argc == 0) { 782 /* Variable assignment(s) without command */ 783 cmdentry.cmdtype = CMDBUILTIN; 784 cmdentry.u.index = BLTINCMD; 785 cmdentry.special = 0; 786 } else { 787 static const char PATH[] = "PATH="; 788 int cmd_flags = 0, bltinonly = 0; 789 790 /* 791 * Modify the command lookup path, if a PATH= assignment 792 * is present 793 */ 794 for (sp = varlist.list ; sp ; sp = sp->next) 795 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 796 path = sp->text + sizeof(PATH) - 1; 797 /* 798 * On `PATH=... command`, we need to make 799 * sure that the command isn't using the 800 * non-updated hash table of the outer PATH 801 * setting and we need to make sure that 802 * the hash table isn't filled with items 803 * from the temporary setting. 804 * 805 * It would be better to forbit using and 806 * updating the table while this command 807 * runs, by the command finding mechanism 808 * is heavily integrated with hash handling, 809 * so we just delete the hash before and after 810 * the command runs. Partly deleting like 811 * changepatch() does doesn't seem worth the 812 * bookinging effort, since most such runs add 813 * directories in front of the new PATH. 814 */ 815 clearcmdentry(); 816 do_clearcmdentry = 1; 817 } 818 819 for (;;) { 820 if (bltinonly) { 821 cmdentry.u.index = find_builtin(*argv, &cmdentry.special); 822 if (cmdentry.u.index < 0) { 823 cmdentry.u.index = BLTINCMD; 824 argv--; 825 argc++; 826 break; 827 } 828 } else 829 find_command(argv[0], &cmdentry, cmd_flags, path); 830 /* implement the bltin and command builtins here */ 831 if (cmdentry.cmdtype != CMDBUILTIN) 832 break; 833 if (cmdentry.u.index == BLTINCMD) { 834 if (argc == 1) 835 break; 836 argv++; 837 argc--; 838 bltinonly = 1; 839 } else if (cmdentry.u.index == COMMANDCMD) { 840 if (argc == 1) 841 break; 842 if (!strcmp(argv[1], "-p")) { 843 if (argc == 2) 844 break; 845 if (argv[2][0] == '-') { 846 if (strcmp(argv[2], "--")) 847 break; 848 if (argc == 3) 849 break; 850 argv += 3; 851 argc -= 3; 852 } else { 853 argv += 2; 854 argc -= 2; 855 } 856 path = _PATH_STDPATH; 857 clearcmdentry(); 858 do_clearcmdentry = 1; 859 } else if (!strcmp(argv[1], "--")) { 860 if (argc == 2) 861 break; 862 argv += 2; 863 argc -= 2; 864 } else if (argv[1][0] == '-') 865 break; 866 else { 867 argv++; 868 argc--; 869 } 870 cmd_flags |= DO_NOFUNC; 871 bltinonly = 0; 872 } else 873 break; 874 } 875 /* 876 * Special builtins lose their special properties when 877 * called via 'command'. 878 */ 879 if (cmd_flags & DO_NOFUNC) 880 cmdentry.special = 0; 881 } 882 883 /* Fork off a child process if necessary. */ 884 if (cmd->ncmd.backgnd 885 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 886 && ((flags & EV_EXIT) == 0 || have_traps())) 887 || ((flags & EV_BACKCMD) != 0 888 && (cmdentry.cmdtype != CMDBUILTIN || 889 !safe_builtin(cmdentry.u.index, argc, argv)))) { 890 jp = makejob(cmd, 1); 891 mode = cmd->ncmd.backgnd; 892 if (flags & EV_BACKCMD) { 893 mode = FORK_NOJOB; 894 if (pipe(pip) < 0) 895 error("Pipe call failed: %s", strerror(errno)); 896 } 897 if (forkshell(jp, cmd, mode) != 0) 898 goto parent; /* at end of routine */ 899 if (flags & EV_BACKCMD) { 900 FORCEINTON; 901 close(pip[0]); 902 if (pip[1] != 1) { 903 dup2(pip[1], 1); 904 close(pip[1]); 905 } 906 } 907 flags |= EV_EXIT; 908 } 909 910 /* This is the child process if a fork occurred. */ 911 /* Execute the command. */ 912 if (cmdentry.cmdtype == CMDFUNCTION) { 913#ifdef DEBUG 914 trputs("Shell function: "); trargs(argv); 915#endif 916 saveparam = shellparam; 917 shellparam.malloc = 0; 918 shellparam.reset = 1; 919 shellparam.nparam = argc - 1; 920 shellparam.p = argv + 1; 921 shellparam.optnext = NULL; 922 INTOFF; 923 savelocalvars = localvars; 924 localvars = NULL; 925 reffunc(cmdentry.u.func); 926 savehandler = handler; 927 if (setjmp(jmploc.loc)) { 928 freeparam(&shellparam); 929 shellparam = saveparam; 930 popredir(); 931 unreffunc(cmdentry.u.func); 932 poplocalvars(); 933 localvars = savelocalvars; 934 funcnest--; 935 handler = savehandler; 936 longjmp(handler->loc, 1); 937 } 938 handler = &jmploc; 939 funcnest++; 940 redirect(cmd->ncmd.redirect, REDIR_PUSH); 941 INTON; 942 for (sp = varlist.list ; sp ; sp = sp->next) 943 mklocal(sp->text); 944 exitstatus = oexitstatus; 945 evaltree(getfuncnode(cmdentry.u.func), 946 flags & (EV_TESTED | EV_EXIT)); 947 INTOFF; 948 unreffunc(cmdentry.u.func); 949 poplocalvars(); 950 localvars = savelocalvars; 951 freeparam(&shellparam); 952 shellparam = saveparam; 953 handler = savehandler; 954 funcnest--; 955 popredir(); 956 INTON; 957 if (evalskip == SKIPFUNC) { 958 evalskip = 0; 959 skipcount = 0; 960 } 961 if (jp) 962 exitshell(exitstatus); 963 } else if (cmdentry.cmdtype == CMDBUILTIN) { 964#ifdef DEBUG 965 trputs("builtin command: "); trargs(argv); 966#endif 967 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 968 if (flags == EV_BACKCMD) { 969 memout.nleft = 0; 970 memout.nextc = memout.buf; 971 memout.bufsize = 64; 972 mode |= REDIR_BACKQ; 973 cmdentry.special = 0; 974 } 975 savecmdname = commandname; 976 savetopfile = getcurrentfile(); 977 cmdenviron = varlist.list; 978 e = -1; 979 savehandler = handler; 980 if (setjmp(jmploc.loc)) { 981 e = exception; 982 if (e == EXINT) 983 exitstatus = SIGINT+128; 984 else if (e != EXEXIT) 985 exitstatus = 2; 986 goto cmddone; 987 } 988 handler = &jmploc; 989 redirect(cmd->ncmd.redirect, mode); 990 /* 991 * If there is no command word, redirection errors should 992 * not be fatal but assignment errors should. 993 */ 994 if (argc == 0 && !(flags & EV_BACKCMD)) 995 cmdentry.special = 1; 996 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET); 997 if (argc > 0) 998 bltinsetlocale(); 999 commandname = argv[0]; 1000 argptr = argv + 1; 1001 nextopt_optptr = NULL; /* initialize nextopt */ 1002 builtin_flags = flags; 1003 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 1004 flushall(); 1005cmddone: 1006 if (argc > 0) 1007 bltinunsetlocale(); 1008 cmdenviron = NULL; 1009 out1 = &output; 1010 out2 = &errout; 1011 freestdout(); 1012 handler = savehandler; 1013 commandname = savecmdname; 1014 if (jp) 1015 exitshell(exitstatus); 1016 if (flags == EV_BACKCMD) { 1017 backcmd->buf = memout.buf; 1018 backcmd->nleft = memout.nextc - memout.buf; 1019 memout.buf = NULL; 1020 } 1021 if (cmdentry.u.index != EXECCMD) 1022 popredir(); 1023 if (e != -1) { 1024 if ((e != EXERROR && e != EXEXEC) 1025 || cmdentry.special) 1026 exraise(e); 1027 popfilesupto(savetopfile); 1028 if (flags != EV_BACKCMD) 1029 FORCEINTON; 1030 } 1031 } else { 1032#ifdef DEBUG 1033 trputs("normal command: "); trargs(argv); 1034#endif 1035 redirect(cmd->ncmd.redirect, 0); 1036 for (sp = varlist.list ; sp ; sp = sp->next) 1037 setvareq(sp->text, VEXPORT|VSTACK); 1038 envp = environment(); 1039 shellexec(argv, envp, path, cmdentry.u.index); 1040 /*NOTREACHED*/ 1041 } 1042 goto out; 1043 1044parent: /* parent process gets here (if we forked) */ 1045 if (mode == FORK_FG) { /* argument to fork */ 1046 INTOFF; 1047 exitstatus = waitforjob(jp, &realstatus); 1048 INTON; 1049 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 1050 evalskip = SKIPBREAK; 1051 skipcount = loopnest; 1052 } 1053 } else if (mode == FORK_NOJOB) { 1054 backcmd->fd = pip[0]; 1055 close(pip[1]); 1056 backcmd->jp = jp; 1057 } else 1058 exitstatus = 0; 1059 1060out: 1061 if (lastarg) 1062 setvar("_", lastarg, 0); 1063 if (do_clearcmdentry) 1064 clearcmdentry(); 1065 popstackmark(&smark); 1066} 1067 1068 1069 1070/* 1071 * Search for a command. This is called before we fork so that the 1072 * location of the command will be available in the parent as well as 1073 * the child. The check for "goodname" is an overly conservative 1074 * check that the name will not be subject to expansion. 1075 */ 1076 1077static void 1078prehash(union node *n) 1079{ 1080 struct cmdentry entry; 1081 1082 if (n && n->type == NCMD && n->ncmd.args) 1083 if (goodname(n->ncmd.args->narg.text)) 1084 find_command(n->ncmd.args->narg.text, &entry, 0, 1085 pathval()); 1086} 1087 1088 1089 1090/* 1091 * Builtin commands. Builtin commands whose functions are closely 1092 * tied to evaluation are implemented here. 1093 */ 1094 1095/* 1096 * No command given, a bltin command with no arguments, or a bltin command 1097 * with an invalid name. 1098 */ 1099 1100int 1101bltincmd(int argc, char **argv) 1102{ 1103 if (argc > 1) { 1104 out2fmt_flush("%s: not found\n", argv[1]); 1105 return 127; 1106 } 1107 /* 1108 * Preserve exitstatus of a previous possible redirection 1109 * as POSIX mandates 1110 */ 1111 return exitstatus; 1112} 1113 1114 1115/* 1116 * Handle break and continue commands. Break, continue, and return are 1117 * all handled by setting the evalskip flag. The evaluation routines 1118 * above all check this flag, and if it is set they start skipping 1119 * commands rather than executing them. The variable skipcount is 1120 * the number of loops to break/continue, or the number of function 1121 * levels to return. (The latter is always 1.) It should probably 1122 * be an error to break out of more loops than exist, but it isn't 1123 * in the standard shell so we don't make it one here. 1124 */ 1125 1126int 1127breakcmd(int argc, char **argv) 1128{ 1129 int n = argc > 1 ? number(argv[1]) : 1; 1130 1131 if (n > loopnest) 1132 n = loopnest; 1133 if (n > 0) { 1134 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 1135 skipcount = n; 1136 } 1137 return 0; 1138} 1139 1140/* 1141 * The `command' command. 1142 */ 1143int 1144commandcmd(int argc, char **argv) 1145{ 1146 const char *path; 1147 int ch; 1148 int cmd = -1; 1149 1150 path = bltinlookup("PATH", 1); 1151 1152 optind = optreset = 1; 1153 opterr = 0; 1154 while ((ch = getopt(argc, argv, "pvV")) != -1) { 1155 switch (ch) { 1156 case 'p': 1157 path = _PATH_STDPATH; 1158 break; 1159 case 'v': 1160 cmd = TYPECMD_SMALLV; 1161 break; 1162 case 'V': 1163 cmd = TYPECMD_BIGV; 1164 break; 1165 case '?': 1166 default: 1167 error("unknown option: -%c", optopt); 1168 } 1169 } 1170 argc -= optind; 1171 argv += optind; 1172 1173 if (cmd != -1) { 1174 if (argc != 1) 1175 error("wrong number of arguments"); 1176 return typecmd_impl(2, argv - 1, cmd, path); 1177 } 1178 if (argc != 0) 1179 error("commandcmd bad call"); 1180 1181 /* 1182 * Do nothing successfully if no command was specified; 1183 * ksh also does this. 1184 */ 1185 return 0; 1186} 1187 1188 1189/* 1190 * The return command. 1191 */ 1192 1193int 1194returncmd(int argc, char **argv) 1195{ 1196 int ret = argc > 1 ? number(argv[1]) : oexitstatus; 1197 1198 if (funcnest) { 1199 evalskip = SKIPFUNC; 1200 skipcount = 1; 1201 } else { 1202 /* skip the rest of the file */ 1203 evalskip = SKIPFILE; 1204 skipcount = 1; 1205 } 1206 return ret; 1207} 1208 1209 1210int 1211falsecmd(int argc __unused, char **argv __unused) 1212{ 1213 return 1; 1214} 1215 1216 1217int 1218truecmd(int argc __unused, char **argv __unused) 1219{ 1220 return 0; 1221} 1222 1223 1224int 1225execcmd(int argc, char **argv) 1226{ 1227 /* 1228 * Because we have historically not supported any options, 1229 * only treat "--" specially. 1230 */ 1231 if (argc > 1 && strcmp(argv[1], "--") == 0) 1232 argc--, argv++; 1233 if (argc > 1) { 1234 struct strlist *sp; 1235 1236 iflag = 0; /* exit on error */ 1237 mflag = 0; 1238 optschanged(); 1239 for (sp = cmdenviron; sp ; sp = sp->next) 1240 setvareq(sp->text, VEXPORT|VSTACK); 1241 shellexec(argv + 1, environment(), pathval(), 0); 1242 1243 } 1244 return 0; 1245} 1246 1247 1248int 1249timescmd(int argc __unused, char **argv __unused) 1250{ 1251 struct rusage ru; 1252 long shumins, shsmins, chumins, chsmins; 1253 double shusecs, shssecs, chusecs, chssecs; 1254 1255 if (getrusage(RUSAGE_SELF, &ru) < 0) 1256 return 1; 1257 shumins = ru.ru_utime.tv_sec / 60; 1258 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1259 shsmins = ru.ru_stime.tv_sec / 60; 1260 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1261 if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1262 return 1; 1263 chumins = ru.ru_utime.tv_sec / 60; 1264 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1265 chsmins = ru.ru_stime.tv_sec / 60; 1266 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1267 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1268 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1269 return 0; 1270} 1271