eval.c revision 204800
155682Smarkm/*- 2233294Sstas * Copyright (c) 1993 3233294Sstas * The Regents of the University of California. All rights reserved. 4233294Sstas * 555682Smarkm * This code is derived from software contributed to Berkeley by 6233294Sstas * Kenneth Almquist. 7233294Sstas * 8233294Sstas * Redistribution and use in source and binary forms, with or without 955682Smarkm * modification, are permitted provided that the following conditions 10233294Sstas * are met: 11233294Sstas * 1. Redistributions of source code must retain the above copyright 1255682Smarkm * notice, this list of conditions and the following disclaimer. 13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 14233294Sstas * notice, this list of conditions and the following disclaimer in the 15233294Sstas * documentation and/or other materials provided with the distribution. 1655682Smarkm * 4. Neither the name of the University nor the names of its contributors 17233294Sstas * may be used to endorse or promote products derived from this software 18233294Sstas * without specific prior written permission. 19233294Sstas * 2055682Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30233294Sstas * SUCH DAMAGE. 31233294Sstas */ 3255682Smarkm 3355682Smarkm#ifndef lint 3455682Smarkm#if 0 3555682Smarkmstatic char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; 36233294Sstas#endif 3755682Smarkm#endif /* not lint */ 38178825Sdfr#include <sys/cdefs.h> 39233294Sstas__FBSDID("$FreeBSD: head/bin/sh/eval.c 204800 2010-03-06 16:57:53Z jilles $"); 40178825Sdfr 41178825Sdfr#include <paths.h> 42178825Sdfr#include <signal.h> 43178825Sdfr#include <stdlib.h> 44178825Sdfr#include <unistd.h> 45178825Sdfr#include <sys/resource.h> 46178825Sdfr#include <sys/wait.h> /* For WIFSIGNALED(status) */ 47178825Sdfr#include <errno.h> 48178825Sdfr 49178825Sdfr/* 50178825Sdfr * Evaluate a command. 51233294Sstas */ 52178825Sdfr 53178825Sdfr#include "shell.h" 54178825Sdfr#include "nodes.h" 55178825Sdfr#include "syntax.h" 56178825Sdfr#include "expand.h" 57178825Sdfr#include "parser.h" 58178825Sdfr#include "jobs.h" 59178825Sdfr#include "eval.h" 60178825Sdfr#include "builtins.h" 61178825Sdfr#include "options.h" 62178825Sdfr#include "exec.h" 63178825Sdfr#include "redir.h" 64233294Sstas#include "input.h" 65178825Sdfr#include "output.h" 66178825Sdfr#include "trap.h" 6755682Smarkm#include "var.h" 68233294Sstas#include "memalloc.h" 69233294Sstas#include "error.h" 70233294Sstas#include "show.h" 71178825Sdfr#include "mystring.h" 7255682Smarkm#ifndef NO_HISTORY 7355682Smarkm#include "myhistedit.h" 7455682Smarkm#endif 75178825Sdfr 76233294Sstas 77178825Sdfrint evalskip; /* set if we are skipping commands */ 78178825SdfrSTATIC int skipcount; /* number of levels to skip */ 7955682SmarkmMKINIT int loopnest; /* current loop nesting level */ 8055682Smarkmint funcnest; /* depth of function calls */ 81233294SstasSTATIC int builtin_flags; /* evalcommand flags for builtins */ 82233294Sstas 83178825Sdfr 8455682Smarkmchar *commandname; 8555682Smarkmstruct strlist *cmdenviron; 8655682Smarkmint exitstatus; /* exit status of last command */ 8755682Smarkmint oexitstatus; /* saved exit status */ 8855682Smarkm 89233294Sstas 9055682SmarkmSTATIC void evalloop(union node *, int); 9155682SmarkmSTATIC void evalfor(union node *, int); 9255682SmarkmSTATIC void evalcase(union node *, int); 93178825SdfrSTATIC void evalsubshell(union node *, int); 94178825SdfrSTATIC void expredir(union node *); 95178825SdfrSTATIC void evalpipe(union node *); 96178825SdfrSTATIC void evalcommand(union node *, int, struct backcmd *); 9755682SmarkmSTATIC void prehash(union node *); 98178825Sdfr 9955682Smarkm 100178825Sdfr/* 101178825Sdfr * Called to reset things after an exception. 102178825Sdfr */ 103178825Sdfr 104178825Sdfr#ifdef mkinit 105178825SdfrINCLUDE "eval.h" 106178825Sdfr 107178825SdfrRESET { 108178825Sdfr evalskip = 0; 109178825Sdfr loopnest = 0; 110178825Sdfr funcnest = 0; 111178825Sdfr} 112178825Sdfr 11355682SmarkmSHELLPROC { 11472445Sassar exitstatus = 0; 115178825Sdfr} 116178825Sdfr#endif 11772445Sassar 11872445Sassar 11972445Sassar 12055682Smarkm/* 121178825Sdfr * The eval command. 122178825Sdfr */ 12355682Smarkm 124178825Sdfrint 12555682Smarkmevalcmd(int argc, char **argv) 12655682Smarkm{ 127178825Sdfr char *p; 128178825Sdfr char *concat; 129233294Sstas char **ap; 130178825Sdfr 13155682Smarkm if (argc > 1) { 132178825Sdfr p = argv[1]; 133233294Sstas if (argc > 2) { 134178825Sdfr STARTSTACKSTR(concat); 13555682Smarkm ap = argv + 2; 13672445Sassar for (;;) { 13772445Sassar while (*p) 13855682Smarkm STPUTC(*p++, concat); 13955682Smarkm if ((p = *ap++) == NULL) 14055682Smarkm break; 14155682Smarkm STPUTC(' ', concat); 14255682Smarkm } 143178825Sdfr STPUTC('\0', concat); 14455682Smarkm p = grabstackstr(concat); 145233294Sstas } 14655682Smarkm evalstring(p, builtin_flags & EV_TESTED); 147178825Sdfr } 148178825Sdfr return exitstatus; 149178825Sdfr} 15055682Smarkm 15155682Smarkm 15255682Smarkm/* 153233294Sstas * Execute a command or commands contained in a string. 15455682Smarkm */ 155233294Sstas 156233294Sstasvoid 157233294Sstasevalstring(char *s, int flags) 158233294Sstas{ 159233294Sstas union node *n; 160233294Sstas struct stackmark smark; 161233294Sstas int flags_exit; 162233294Sstas 16355682Smarkm flags_exit = flags & EV_EXIT; 16455682Smarkm flags &= ~EV_EXIT; 16572445Sassar setstackmark(&smark); 166178825Sdfr setinputstring(s, 1); 167178825Sdfr while ((n = parsecmd(0)) != NEOF) { 16872445Sassar if (n != NULL) { 16972445Sassar if (flags_exit && preadateof()) 17072445Sassar evaltree(n, flags | EV_EXIT); 17155682Smarkm else 172233294Sstas evaltree(n, flags); 17355682Smarkm } 17455682Smarkm popstackmark(&smark); 17555682Smarkm } 17655682Smarkm popfile(); 177178825Sdfr popstackmark(&smark); 178178825Sdfr if (flags_exit) 179233294Sstas exitshell(exitstatus); 180178825Sdfr} 181178825Sdfr 182178825Sdfr 183178825Sdfr/* 184178825Sdfr * Evaluate a parse tree. The value is left in the global variable 18555682Smarkm * exitstatus. 18655682Smarkm */ 187178825Sdfr 18855682Smarkmvoid 18955682Smarkmevaltree(union node *n, int flags) 19055682Smarkm{ 19155682Smarkm int do_etest; 19272445Sassar 19355682Smarkm do_etest = 0; 19455682Smarkm if (n == NULL) { 19555682Smarkm TRACE(("evaltree(NULL) called\n")); 196233294Sstas exitstatus = 0; 19755682Smarkm goto out; 19855682Smarkm } 19955682Smarkm#ifndef NO_HISTORY 200233294Sstas displayhist = 1; /* show history substitutions done with fc */ 20155682Smarkm#endif 20255682Smarkm TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type)); 20355682Smarkm switch (n->type) { 20455682Smarkm case NSEMI: 20555682Smarkm evaltree(n->nbinary.ch1, flags & ~EV_EXIT); 20655682Smarkm if (evalskip) 20755682Smarkm goto out; 20855682Smarkm evaltree(n->nbinary.ch2, flags); 20955682Smarkm break; 21055682Smarkm case NAND: 21155682Smarkm evaltree(n->nbinary.ch1, EV_TESTED); 21255682Smarkm if (evalskip || exitstatus != 0) { 21355682Smarkm goto out; 21455682Smarkm } 21555682Smarkm evaltree(n->nbinary.ch2, flags); 21655682Smarkm break; 21755682Smarkm case NOR: 21855682Smarkm evaltree(n->nbinary.ch1, EV_TESTED); 21955682Smarkm if (evalskip || exitstatus == 0) 22055682Smarkm goto out; 22155682Smarkm evaltree(n->nbinary.ch2, flags); 22255682Smarkm break; 22355682Smarkm case NREDIR: 224178825Sdfr expredir(n->nredir.redirect); 225178825Sdfr redirect(n->nredir.redirect, REDIR_PUSH); 226233294Sstas evaltree(n->nredir.n, flags); 227178825Sdfr popredir(); 228178825Sdfr break; 229178825Sdfr case NSUBSHELL: 230178825Sdfr evalsubshell(n, flags); 231178825Sdfr do_etest = !(flags & EV_TESTED); 232178825Sdfr break; 233178825Sdfr case NBACKGND: 234178825Sdfr evalsubshell(n, flags); 235178825Sdfr break; 236178825Sdfr case NIF: { 237178825Sdfr evaltree(n->nif.test, EV_TESTED); 238178825Sdfr if (evalskip) 239233294Sstas goto out; 240178825Sdfr if (exitstatus == 0) 241178825Sdfr evaltree(n->nif.ifpart, flags); 242178825Sdfr else if (n->nif.elsepart) 243178825Sdfr evaltree(n->nif.elsepart, flags); 244178825Sdfr else 245178825Sdfr exitstatus = 0; 246233294Sstas break; 247178825Sdfr } 248178825Sdfr case NWHILE: 249178825Sdfr case NUNTIL: 250178825Sdfr evalloop(n, flags & ~EV_EXIT); 251178825Sdfr break; 252233294Sstas case NFOR: 253178825Sdfr evalfor(n, flags & ~EV_EXIT); 254178825Sdfr break; 255233294Sstas case NCASE: 256233294Sstas evalcase(n, flags); 257233294Sstas break; 258233294Sstas case NDEFUN: 259233294Sstas defun(n->narg.text, n->narg.next); 260233294Sstas exitstatus = 0; 261233294Sstas break; 262233294Sstas case NNOT: 263233294Sstas evaltree(n->nnot.com, EV_TESTED); 264233294Sstas exitstatus = !exitstatus; 265233294Sstas break; 266233294Sstas 267233294Sstas case NPIPE: 268233294Sstas evalpipe(n); 269233294Sstas do_etest = !(flags & EV_TESTED); 270233294Sstas break; 271233294Sstas case NCMD: 272233294Sstas evalcommand(n, flags, (struct backcmd *)NULL); 273233294Sstas do_etest = !(flags & EV_TESTED); 274233294Sstas break; 275233294Sstas default: 276233294Sstas out1fmt("Node type = %d\n", n->type); 277233294Sstas flushout(&output); 278233294Sstas break; 279233294Sstas } 280233294Sstasout: 281178825Sdfr if (pendingsigs) 282178825Sdfr dotrap(); 283178825Sdfr if ((flags & EV_EXIT) || (eflag && exitstatus != 0 && do_etest)) 284178825Sdfr exitshell(exitstatus); 285178825Sdfr} 286178825Sdfr 287178825Sdfr 288178825SdfrSTATIC void 289178825Sdfrevalloop(union node *n, int flags) 290178825Sdfr{ 291178825Sdfr int status; 292178825Sdfr 293178825Sdfr loopnest++; 294178825Sdfr status = 0; 295178825Sdfr for (;;) { 296178825Sdfr evaltree(n->nbinary.ch1, EV_TESTED); 297178825Sdfr if (evalskip) { 298178825Sdfrskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 299178825Sdfr evalskip = 0; 300178825Sdfr continue; 301178825Sdfr } 302178825Sdfr if (evalskip == SKIPBREAK && --skipcount <= 0) 303178825Sdfr evalskip = 0; 304178825Sdfr break; 305178825Sdfr } 306178825Sdfr if (n->type == NWHILE) { 307178825Sdfr if (exitstatus != 0) 30855682Smarkm break; 30955682Smarkm } else { 31055682Smarkm if (exitstatus == 0) 31155682Smarkm break; 31255682Smarkm } 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 char *path = pathval(); 601 602 /* First expand the arguments. */ 603 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 604 setstackmark(&smark); 605 arglist.lastp = &arglist.list; 606 varlist.lastp = &varlist.list; 607 varflag = 1; 608 do_clearcmdentry = 0; 609 oexitstatus = exitstatus; 610 exitstatus = 0; 611 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 612 char *p = argp->narg.text; 613 if (varflag && is_name(*p)) { 614 do { 615 p++; 616 } while (is_in_name(*p)); 617 if (*p == '=') { 618 expandarg(argp, &varlist, EXP_VARTILDE); 619 continue; 620 } 621 } 622 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 623 varflag = 0; 624 } 625 *arglist.lastp = NULL; 626 *varlist.lastp = NULL; 627 expredir(cmd->ncmd.redirect); 628 argc = 0; 629 for (sp = arglist.list ; sp ; sp = sp->next) 630 argc++; 631 argv = stalloc(sizeof (char *) * (argc + 1)); 632 633 for (sp = arglist.list ; sp ; sp = sp->next) { 634 TRACE(("evalcommand arg: %s\n", sp->text)); 635 *argv++ = sp->text; 636 } 637 *argv = NULL; 638 lastarg = NULL; 639 if (iflag && funcnest == 0 && argc > 0) 640 lastarg = argv[-1]; 641 argv -= argc; 642 643 /* Print the command if xflag is set. */ 644 if (xflag) { 645 char sep = 0; 646 const char *p; 647 out2str(ps4val()); 648 for (sp = varlist.list ; sp ; sp = sp->next) { 649 if (sep != 0) 650 out2c(' '); 651 p = sp->text; 652 while (*p != '=' && *p != '\0') 653 out2c(*p++); 654 if (*p != '\0') { 655 out2c(*p++); 656 out2qstr(p); 657 } 658 sep = ' '; 659 } 660 for (sp = arglist.list ; sp ; sp = sp->next) { 661 if (sep != 0) 662 out2c(' '); 663 /* Disambiguate command looking like assignment. */ 664 if (sp == arglist.list && 665 strchr(sp->text, '=') != NULL && 666 strchr(sp->text, '\'') == NULL) { 667 out2c('\''); 668 out2str(sp->text); 669 out2c('\''); 670 } else 671 out2qstr(sp->text); 672 sep = ' '; 673 } 674 out2c('\n'); 675 flushout(&errout); 676 } 677 678 /* Now locate the command. */ 679 if (argc == 0) { 680 /* Variable assignment(s) without command */ 681 cmdentry.cmdtype = CMDBUILTIN; 682 cmdentry.u.index = BLTINCMD; 683 cmdentry.special = 1; 684 } else { 685 static const char PATH[] = "PATH="; 686 int cmd_flags = 0, bltinonly = 0; 687 688 /* 689 * Modify the command lookup path, if a PATH= assignment 690 * is present 691 */ 692 for (sp = varlist.list ; sp ; sp = sp->next) 693 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 694 path = sp->text + sizeof(PATH) - 1; 695 /* 696 * On `PATH=... command`, we need to make 697 * sure that the command isn't using the 698 * non-updated hash table of the outer PATH 699 * setting and we need to make sure that 700 * the hash table isn't filled with items 701 * from the temporary setting. 702 * 703 * It would be better to forbit using and 704 * updating the table while this command 705 * runs, by the command finding mechanism 706 * is heavily integrated with hash handling, 707 * so we just delete the hash before and after 708 * the command runs. Partly deleting like 709 * changepatch() does doesn't seem worth the 710 * bookinging effort, since most such runs add 711 * directories in front of the new PATH. 712 */ 713 clearcmdentry(0); 714 do_clearcmdentry = 1; 715 } 716 717 for (;;) { 718 if (bltinonly) { 719 cmdentry.u.index = find_builtin(*argv, &cmdentry.special); 720 if (cmdentry.u.index < 0) { 721 cmdentry.u.index = BLTINCMD; 722 argv--; 723 argc++; 724 break; 725 } 726 } else 727 find_command(argv[0], &cmdentry, cmd_flags, path); 728 /* implement the bltin and command builtins here */ 729 if (cmdentry.cmdtype != CMDBUILTIN) 730 break; 731 if (cmdentry.u.index == BLTINCMD) { 732 if (argc == 1) 733 break; 734 argv++; 735 argc--; 736 bltinonly = 1; 737 } else if (cmdentry.u.index == COMMANDCMD) { 738 if (argc == 1) 739 break; 740 if (!strcmp(argv[1], "-p")) { 741 if (argc == 2) 742 break; 743 if (argv[2][0] == '-') { 744 if (strcmp(argv[2], "--")) 745 break; 746 if (argc == 3) 747 break; 748 argv += 3; 749 argc -= 3; 750 } else { 751 argv += 2; 752 argc -= 2; 753 } 754 path = _PATH_STDPATH; 755 clearcmdentry(0); 756 do_clearcmdentry = 1; 757 } else if (!strcmp(argv[1], "--")) { 758 if (argc == 2) 759 break; 760 argv += 2; 761 argc -= 2; 762 } else if (argv[1][0] == '-') 763 break; 764 else { 765 argv++; 766 argc--; 767 } 768 cmd_flags |= DO_NOFUNC; 769 bltinonly = 0; 770 } else 771 break; 772 } 773 /* 774 * Special builtins lose their special properties when 775 * called via 'command'. 776 */ 777 if (cmd_flags & DO_NOFUNC) 778 cmdentry.special = 0; 779 } 780 781 /* Fork off a child process if necessary. */ 782 if (cmd->ncmd.backgnd 783 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 784 && ((flags & EV_EXIT) == 0 || have_traps())) 785 || ((flags & EV_BACKCMD) != 0 786 && (cmdentry.cmdtype != CMDBUILTIN 787 || cmdentry.u.index == CDCMD 788 || cmdentry.u.index == DOTCMD 789 || cmdentry.u.index == EVALCMD))) { 790 jp = makejob(cmd, 1); 791 mode = cmd->ncmd.backgnd; 792 if (flags & EV_BACKCMD) { 793 mode = FORK_NOJOB; 794 if (pipe(pip) < 0) 795 error("Pipe call failed: %s", strerror(errno)); 796 } 797 if (forkshell(jp, cmd, mode) != 0) 798 goto parent; /* at end of routine */ 799 if (flags & EV_BACKCMD) { 800 FORCEINTON; 801 close(pip[0]); 802 if (pip[1] != 1) { 803 dup2(pip[1], 1); 804 close(pip[1]); 805 } 806 } 807 flags |= EV_EXIT; 808 } 809 810 /* This is the child process if a fork occurred. */ 811 /* Execute the command. */ 812 if (cmdentry.cmdtype == CMDFUNCTION) { 813#ifdef DEBUG 814 trputs("Shell function: "); trargs(argv); 815#endif 816 redirect(cmd->ncmd.redirect, REDIR_PUSH); 817 saveparam = shellparam; 818 shellparam.malloc = 0; 819 shellparam.reset = 1; 820 shellparam.nparam = argc - 1; 821 shellparam.p = argv + 1; 822 shellparam.optnext = NULL; 823 INTOFF; 824 savelocalvars = localvars; 825 localvars = NULL; 826 reffunc(cmdentry.u.func); 827 savehandler = handler; 828 if (setjmp(jmploc.loc)) { 829 if (exception == EXSHELLPROC) 830 freeparam(&saveparam); 831 else { 832 freeparam(&shellparam); 833 shellparam = saveparam; 834 } 835 unreffunc(cmdentry.u.func); 836 poplocalvars(); 837 localvars = savelocalvars; 838 funcnest--; 839 handler = savehandler; 840 longjmp(handler->loc, 1); 841 } 842 handler = &jmploc; 843 funcnest++; 844 INTON; 845 for (sp = varlist.list ; sp ; sp = sp->next) 846 mklocal(sp->text); 847 exitstatus = oexitstatus; 848 if (flags & EV_TESTED) 849 evaltree(getfuncnode(cmdentry.u.func), EV_TESTED); 850 else 851 evaltree(getfuncnode(cmdentry.u.func), 0); 852 INTOFF; 853 unreffunc(cmdentry.u.func); 854 poplocalvars(); 855 localvars = savelocalvars; 856 freeparam(&shellparam); 857 shellparam = saveparam; 858 handler = savehandler; 859 funcnest--; 860 popredir(); 861 INTON; 862 if (evalskip == SKIPFUNC) { 863 evalskip = 0; 864 skipcount = 0; 865 } 866 if (flags & EV_EXIT) 867 exitshell(exitstatus); 868 } else if (cmdentry.cmdtype == CMDBUILTIN) { 869#ifdef DEBUG 870 trputs("builtin command: "); trargs(argv); 871#endif 872 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 873 if (flags == EV_BACKCMD) { 874 memout.nleft = 0; 875 memout.nextc = memout.buf; 876 memout.bufsize = 64; 877 mode |= REDIR_BACKQ; 878 cmdentry.special = 0; 879 } 880 savecmdname = commandname; 881 savetopfile = getcurrentfile(); 882 cmdenviron = varlist.list; 883 e = -1; 884 savehandler = handler; 885 if (setjmp(jmploc.loc)) { 886 e = exception; 887 exitstatus = (e == EXINT)? SIGINT+128 : 2; 888 goto cmddone; 889 } 890 handler = &jmploc; 891 redirect(cmd->ncmd.redirect, mode); 892 if (cmdentry.special) 893 listsetvar(cmdenviron); 894 commandname = argv[0]; 895 argptr = argv + 1; 896 nextopt_optptr = NULL; /* initialize nextopt */ 897 builtin_flags = flags; 898 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 899 flushall(); 900cmddone: 901 cmdenviron = NULL; 902 out1 = &output; 903 out2 = &errout; 904 freestdout(); 905 if (e != EXSHELLPROC) { 906 commandname = savecmdname; 907 if (flags & EV_EXIT) { 908 exitshell(exitstatus); 909 } 910 } 911 handler = savehandler; 912 if (flags == EV_BACKCMD) { 913 backcmd->buf = memout.buf; 914 backcmd->nleft = memout.nextc - memout.buf; 915 memout.buf = NULL; 916 } 917 if (e != -1) { 918 if ((e != EXERROR && e != EXEXEC) 919 || cmdentry.special) 920 exraise(e); 921 popfilesupto(savetopfile); 922 if (flags != EV_BACKCMD) 923 FORCEINTON; 924 } 925 if (cmdentry.u.index != EXECCMD) 926 popredir(); 927 } else { 928#ifdef DEBUG 929 trputs("normal command: "); trargs(argv); 930#endif 931 redirect(cmd->ncmd.redirect, 0); 932 for (sp = varlist.list ; sp ; sp = sp->next) 933 setvareq(sp->text, VEXPORT|VSTACK); 934 envp = environment(); 935 shellexec(argv, envp, path, cmdentry.u.index); 936 /*NOTREACHED*/ 937 } 938 goto out; 939 940parent: /* parent process gets here (if we forked) */ 941 if (mode == 0) { /* argument to fork */ 942 INTOFF; 943 exitstatus = waitforjob(jp, &realstatus); 944 INTON; 945 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 946 evalskip = SKIPBREAK; 947 skipcount = loopnest; 948 } 949 } else if (mode == 2) { 950 backcmd->fd = pip[0]; 951 close(pip[1]); 952 backcmd->jp = jp; 953 } 954 955out: 956 if (lastarg) 957 setvar("_", lastarg, 0); 958 if (do_clearcmdentry) 959 clearcmdentry(0); 960 popstackmark(&smark); 961} 962 963 964 965/* 966 * Search for a command. This is called before we fork so that the 967 * location of the command will be available in the parent as well as 968 * the child. The check for "goodname" is an overly conservative 969 * check that the name will not be subject to expansion. 970 */ 971 972STATIC void 973prehash(union node *n) 974{ 975 struct cmdentry entry; 976 977 if (n && n->type == NCMD && n->ncmd.args) 978 if (goodname(n->ncmd.args->narg.text)) 979 find_command(n->ncmd.args->narg.text, &entry, 0, 980 pathval()); 981} 982 983 984 985/* 986 * Builtin commands. Builtin commands whose functions are closely 987 * tied to evaluation are implemented here. 988 */ 989 990/* 991 * No command given, a bltin command with no arguments, or a bltin command 992 * with an invalid name. 993 */ 994 995int 996bltincmd(int argc, char **argv) 997{ 998 if (argc > 1) { 999 out2fmt_flush("%s: not found\n", argv[1]); 1000 return 127; 1001 } 1002 /* 1003 * Preserve exitstatus of a previous possible redirection 1004 * as POSIX mandates 1005 */ 1006 return exitstatus; 1007} 1008 1009 1010/* 1011 * Handle break and continue commands. Break, continue, and return are 1012 * all handled by setting the evalskip flag. The evaluation routines 1013 * above all check this flag, and if it is set they start skipping 1014 * commands rather than executing them. The variable skipcount is 1015 * the number of loops to break/continue, or the number of function 1016 * levels to return. (The latter is always 1.) It should probably 1017 * be an error to break out of more loops than exist, but it isn't 1018 * in the standard shell so we don't make it one here. 1019 */ 1020 1021int 1022breakcmd(int argc, char **argv) 1023{ 1024 int n = argc > 1 ? number(argv[1]) : 1; 1025 1026 if (n > loopnest) 1027 n = loopnest; 1028 if (n > 0) { 1029 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 1030 skipcount = n; 1031 } 1032 return 0; 1033} 1034 1035/* 1036 * The `command' command. 1037 */ 1038int 1039commandcmd(int argc, char **argv) 1040{ 1041 static char stdpath[] = _PATH_STDPATH; 1042 char *path; 1043 int ch; 1044 int cmd = -1; 1045 1046 path = bltinlookup("PATH", 1); 1047 1048 optind = optreset = 1; 1049 opterr = 0; 1050 while ((ch = getopt(argc, argv, "pvV")) != -1) { 1051 switch (ch) { 1052 case 'p': 1053 path = stdpath; 1054 break; 1055 case 'v': 1056 cmd = TYPECMD_SMALLV; 1057 break; 1058 case 'V': 1059 cmd = TYPECMD_BIGV; 1060 break; 1061 case '?': 1062 default: 1063 error("unknown option: -%c", optopt); 1064 } 1065 } 1066 argc -= optind; 1067 argv += optind; 1068 1069 if (cmd != -1) { 1070 if (argc != 1) 1071 error("wrong number of arguments"); 1072 return typecmd_impl(2, argv - 1, cmd, path); 1073 } 1074 if (argc != 0) 1075 error("commandcmd() called while it should not be"); 1076 1077 /* 1078 * Do nothing successfully if no command was specified; 1079 * ksh also does this. 1080 */ 1081 return 0; 1082} 1083 1084 1085/* 1086 * The return command. 1087 */ 1088 1089int 1090returncmd(int argc, char **argv) 1091{ 1092 int ret = argc > 1 ? number(argv[1]) : oexitstatus; 1093 1094 if (funcnest) { 1095 evalskip = SKIPFUNC; 1096 skipcount = 1; 1097 } else { 1098 /* skip the rest of the file */ 1099 evalskip = SKIPFILE; 1100 skipcount = 1; 1101 } 1102 return ret; 1103} 1104 1105 1106int 1107falsecmd(int argc __unused, char **argv __unused) 1108{ 1109 return 1; 1110} 1111 1112 1113int 1114truecmd(int argc __unused, char **argv __unused) 1115{ 1116 return 0; 1117} 1118 1119 1120int 1121execcmd(int argc, char **argv) 1122{ 1123 if (argc > 1) { 1124 struct strlist *sp; 1125 1126 iflag = 0; /* exit on error */ 1127 mflag = 0; 1128 optschanged(); 1129 for (sp = cmdenviron; sp ; sp = sp->next) 1130 setvareq(sp->text, VEXPORT|VSTACK); 1131 shellexec(argv + 1, environment(), pathval(), 0); 1132 1133 } 1134 return 0; 1135} 1136 1137 1138int 1139timescmd(int argc __unused, char **argv __unused) 1140{ 1141 struct rusage ru; 1142 long shumins, shsmins, chumins, chsmins; 1143 double shusecs, shssecs, chusecs, chssecs; 1144 1145 if (getrusage(RUSAGE_SELF, &ru) < 0) 1146 return 1; 1147 shumins = ru.ru_utime.tv_sec / 60; 1148 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1149 shsmins = ru.ru_stime.tv_sec / 60; 1150 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1151 if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1152 return 1; 1153 chumins = ru.ru_utime.tv_sec / 60; 1154 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1155 chsmins = ru.ru_stime.tv_sec / 60; 1156 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1157 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1158 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1159 return 0; 1160} 1161