eval.c revision 230154
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/eval.c 230154 2012-01-15 20:04:05Z jilles $"); 401556Srgrimes 41100437Stjr#include <paths.h> 4217987Speter#include <signal.h> 43102576Skeramida#include <stdlib.h> 4417987Speter#include <unistd.h> 45153091Sstefanf#include <sys/resource.h> 4645266Scracauer#include <sys/wait.h> /* For WIFSIGNALED(status) */ 4753891Scracauer#include <errno.h> 4817987Speter 491556Srgrimes/* 501556Srgrimes * Evaluate a command. 511556Srgrimes */ 521556Srgrimes 531556Srgrimes#include "shell.h" 541556Srgrimes#include "nodes.h" 551556Srgrimes#include "syntax.h" 561556Srgrimes#include "expand.h" 571556Srgrimes#include "parser.h" 581556Srgrimes#include "jobs.h" 591556Srgrimes#include "eval.h" 601556Srgrimes#include "builtins.h" 611556Srgrimes#include "options.h" 621556Srgrimes#include "exec.h" 631556Srgrimes#include "redir.h" 641556Srgrimes#include "input.h" 651556Srgrimes#include "output.h" 661556Srgrimes#include "trap.h" 671556Srgrimes#include "var.h" 681556Srgrimes#include "memalloc.h" 691556Srgrimes#include "error.h" 7017987Speter#include "show.h" 711556Srgrimes#include "mystring.h" 7217987Speter#ifndef NO_HISTORY 731556Srgrimes#include "myhistedit.h" 7417987Speter#endif 751556Srgrimes 761556Srgrimes 77201053Sjillesint evalskip; /* set if we are skipping commands */ 78213760Sobrienstatic int skipcount; /* number of levels to skip */ 791556SrgrimesMKINIT int loopnest; /* current loop nesting level */ 801556Srgrimesint funcnest; /* depth of function calls */ 81213760Sobrienstatic int builtin_flags; /* evalcommand flags for builtins */ 821556Srgrimes 831556Srgrimes 841556Srgrimeschar *commandname; 851556Srgrimesstruct strlist *cmdenviron; 861556Srgrimesint exitstatus; /* exit status of last command */ 8717987Speterint oexitstatus; /* saved exit status */ 881556Srgrimes 891556Srgrimes 90213811Sobrienstatic void evalloop(union node *, int); 91213811Sobrienstatic void evalfor(union node *, int); 92228013Sjillesstatic union node *evalcase(union node *, int); 93213811Sobrienstatic void evalsubshell(union node *, int); 94213811Sobrienstatic void evalredir(union node *, int); 95213811Sobrienstatic void expredir(union node *); 96213811Sobrienstatic void evalpipe(union node *); 97216778Sjillesstatic int is_valid_fast_cmdsubst(union node *n); 98213811Sobrienstatic void evalcommand(union node *, int, struct backcmd *); 99213811Sobrienstatic void prehash(union node *); 1001556Srgrimes 1011556Srgrimes 1021556Srgrimes/* 1031556Srgrimes * Called to reset things after an exception. 1041556Srgrimes */ 1051556Srgrimes 1061556Srgrimes#ifdef mkinit 1071556SrgrimesINCLUDE "eval.h" 1081556Srgrimes 1091556SrgrimesRESET { 1101556Srgrimes evalskip = 0; 1111556Srgrimes loopnest = 0; 1121556Srgrimes funcnest = 0; 1131556Srgrimes} 1141556Srgrimes#endif 1151556Srgrimes 1161556Srgrimes 1171556Srgrimes 1181556Srgrimes/* 11946684Skris * The eval command. 1201556Srgrimes */ 1211556Srgrimes 12217987Speterint 12390111Simpevalcmd(int argc, char **argv) 1241556Srgrimes{ 1251556Srgrimes char *p; 1261556Srgrimes char *concat; 1271556Srgrimes char **ap; 1281556Srgrimes 1291556Srgrimes if (argc > 1) { 1301556Srgrimes p = argv[1]; 1311556Srgrimes if (argc > 2) { 1321556Srgrimes STARTSTACKSTR(concat); 1331556Srgrimes ap = argv + 2; 1341556Srgrimes for (;;) { 135215783Sjilles STPUTS(p, concat); 1361556Srgrimes if ((p = *ap++) == NULL) 1371556Srgrimes break; 1381556Srgrimes STPUTC(' ', concat); 1391556Srgrimes } 1401556Srgrimes STPUTC('\0', concat); 1411556Srgrimes p = grabstackstr(concat); 1421556Srgrimes } 143223163Sjilles evalstring(p, builtin_flags); 144210829Sjilles } else 145210829Sjilles exitstatus = 0; 1461556Srgrimes return exitstatus; 1471556Srgrimes} 1481556Srgrimes 1491556Srgrimes 1501556Srgrimes/* 1511556Srgrimes * Execute a command or commands contained in a string. 1521556Srgrimes */ 1531556Srgrimes 1541556Srgrimesvoid 155193169Sstefanfevalstring(char *s, int flags) 15690111Simp{ 1571556Srgrimes union node *n; 1581556Srgrimes struct stackmark smark; 159194128Sjilles int flags_exit; 160210829Sjilles int any; 1611556Srgrimes 162194128Sjilles flags_exit = flags & EV_EXIT; 163194128Sjilles flags &= ~EV_EXIT; 164210829Sjilles any = 0; 1651556Srgrimes setstackmark(&smark); 1661556Srgrimes setinputstring(s, 1); 1671556Srgrimes while ((n = parsecmd(0)) != NEOF) { 168222676Sjilles if (n != NULL && !nflag) { 169194128Sjilles if (flags_exit && preadateof()) 170194128Sjilles evaltree(n, flags | EV_EXIT); 171194128Sjilles else 172194128Sjilles evaltree(n, flags); 173210829Sjilles any = 1; 174194128Sjilles } 1751556Srgrimes popstackmark(&smark); 1761556Srgrimes } 1771556Srgrimes popfile(); 1781556Srgrimes popstackmark(&smark); 179210829Sjilles if (!any) 180210829Sjilles exitstatus = 0; 181194128Sjilles if (flags_exit) 182220978Sjilles exraise(EXEXIT); 1831556Srgrimes} 1841556Srgrimes 1851556Srgrimes 1861556Srgrimes/* 1871556Srgrimes * Evaluate a parse tree. The value is left in the global variable 1881556Srgrimes * exitstatus. 1891556Srgrimes */ 1901556Srgrimes 1911556Srgrimesvoid 19290111Simpevaltree(union node *n, int flags) 19317987Speter{ 194149927Sstefanf int do_etest; 195214599Sjilles union node *next; 196149927Sstefanf 197149927Sstefanf do_etest = 0; 1981556Srgrimes if (n == NULL) { 1991556Srgrimes TRACE(("evaltree(NULL) called\n")); 2001556Srgrimes exitstatus = 0; 2011556Srgrimes goto out; 2021556Srgrimes } 203214599Sjilles do { 204214600Sjilles next = NULL; 20517987Speter#ifndef NO_HISTORY 206214600Sjilles displayhist = 1; /* show history substitutions done with fc */ 20717987Speter#endif 208214600Sjilles TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type)); 209214600Sjilles switch (n->type) { 210214600Sjilles case NSEMI: 211214600Sjilles evaltree(n->nbinary.ch1, flags & ~EV_EXIT); 212214600Sjilles if (evalskip) 213214600Sjilles goto out; 214214600Sjilles next = n->nbinary.ch2; 215214600Sjilles break; 216214600Sjilles case NAND: 217214600Sjilles evaltree(n->nbinary.ch1, EV_TESTED); 218214600Sjilles if (evalskip || exitstatus != 0) { 219214600Sjilles goto out; 220214600Sjilles } 221214600Sjilles next = n->nbinary.ch2; 222214600Sjilles break; 223214600Sjilles case NOR: 224214600Sjilles evaltree(n->nbinary.ch1, EV_TESTED); 225214600Sjilles if (evalskip || exitstatus == 0) 226214600Sjilles goto out; 227214600Sjilles next = n->nbinary.ch2; 228214600Sjilles break; 229214600Sjilles case NREDIR: 230214600Sjilles evalredir(n, flags); 231214600Sjilles break; 232214600Sjilles case NSUBSHELL: 233214600Sjilles evalsubshell(n, flags); 234214600Sjilles do_etest = !(flags & EV_TESTED); 235214600Sjilles break; 236214600Sjilles case NBACKGND: 237214600Sjilles evalsubshell(n, flags); 238214600Sjilles break; 239214600Sjilles case NIF: { 240214600Sjilles evaltree(n->nif.test, EV_TESTED); 241214600Sjilles if (evalskip) 242214600Sjilles goto out; 243214600Sjilles if (exitstatus == 0) 244214600Sjilles next = n->nif.ifpart; 245214600Sjilles else if (n->nif.elsepart) 246214600Sjilles next = n->nif.elsepart; 247214600Sjilles else 248214600Sjilles exitstatus = 0; 249214600Sjilles break; 25018754Ssteve } 251214600Sjilles case NWHILE: 252214600Sjilles case NUNTIL: 253214600Sjilles evalloop(n, flags & ~EV_EXIT); 254214600Sjilles break; 255214600Sjilles case NFOR: 256214600Sjilles evalfor(n, flags & ~EV_EXIT); 257214600Sjilles break; 258214600Sjilles case NCASE: 259228013Sjilles next = evalcase(n, flags); 260214600Sjilles break; 261214600Sjilles case NDEFUN: 262214600Sjilles defun(n->narg.text, n->narg.next); 26320425Ssteve exitstatus = 0; 264214600Sjilles break; 265214600Sjilles case NNOT: 266214600Sjilles evaltree(n->nnot.com, EV_TESTED); 267214600Sjilles exitstatus = !exitstatus; 268214600Sjilles break; 2691556Srgrimes 270214600Sjilles case NPIPE: 271214600Sjilles evalpipe(n); 272214600Sjilles do_etest = !(flags & EV_TESTED); 273214600Sjilles break; 274214600Sjilles case NCMD: 275214600Sjilles evalcommand(n, flags, (struct backcmd *)NULL); 276214600Sjilles do_etest = !(flags & EV_TESTED); 277214600Sjilles break; 278214600Sjilles default: 279214600Sjilles out1fmt("Node type = %d\n", n->type); 280214600Sjilles flushout(&output); 281214600Sjilles break; 282214600Sjilles } 283214600Sjilles n = next; 284214599Sjilles } while (n != NULL); 2851556Srgrimesout: 2861556Srgrimes if (pendingsigs) 2871556Srgrimes dotrap(); 288220978Sjilles if (eflag && exitstatus != 0 && do_etest) 2891556Srgrimes exitshell(exitstatus); 290220978Sjilles if (flags & EV_EXIT) 291220978Sjilles exraise(EXEXIT); 2921556Srgrimes} 2931556Srgrimes 2941556Srgrimes 295213811Sobrienstatic void 296149933Sstefanfevalloop(union node *n, int flags) 29717987Speter{ 2981556Srgrimes int status; 2991556Srgrimes 3001556Srgrimes loopnest++; 3011556Srgrimes status = 0; 3021556Srgrimes for (;;) { 3031556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 3041556Srgrimes if (evalskip) { 3051556Srgrimesskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 3061556Srgrimes evalskip = 0; 3071556Srgrimes continue; 3081556Srgrimes } 3091556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3101556Srgrimes evalskip = 0; 311212475Sjilles if (evalskip == SKIPFUNC || evalskip == SKIPFILE) 312212475Sjilles status = exitstatus; 3131556Srgrimes break; 3141556Srgrimes } 3151556Srgrimes if (n->type == NWHILE) { 3161556Srgrimes if (exitstatus != 0) 3171556Srgrimes break; 3181556Srgrimes } else { 3191556Srgrimes if (exitstatus == 0) 3201556Srgrimes break; 3211556Srgrimes } 322149933Sstefanf evaltree(n->nbinary.ch2, flags); 3231556Srgrimes status = exitstatus; 3241556Srgrimes if (evalskip) 3251556Srgrimes goto skipping; 3261556Srgrimes } 3271556Srgrimes loopnest--; 3281556Srgrimes exitstatus = status; 3291556Srgrimes} 3301556Srgrimes 3311556Srgrimes 3321556Srgrimes 333213811Sobrienstatic void 334149933Sstefanfevalfor(union node *n, int flags) 33517987Speter{ 3361556Srgrimes struct arglist arglist; 3371556Srgrimes union node *argp; 3381556Srgrimes struct strlist *sp; 3391556Srgrimes struct stackmark smark; 3401556Srgrimes 3411556Srgrimes setstackmark(&smark); 3421556Srgrimes arglist.lastp = &arglist.list; 3431556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 34417987Speter oexitstatus = exitstatus; 3451556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3461556Srgrimes } 3471556Srgrimes *arglist.lastp = NULL; 3481556Srgrimes 3491556Srgrimes exitstatus = 0; 3501556Srgrimes loopnest++; 3511556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3521556Srgrimes setvar(n->nfor.var, sp->text, 0); 353149933Sstefanf evaltree(n->nfor.body, flags); 3541556Srgrimes if (evalskip) { 3551556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3561556Srgrimes evalskip = 0; 3571556Srgrimes continue; 3581556Srgrimes } 3591556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3601556Srgrimes evalskip = 0; 3611556Srgrimes break; 3621556Srgrimes } 3631556Srgrimes } 3641556Srgrimes loopnest--; 3651556Srgrimes popstackmark(&smark); 3661556Srgrimes} 3671556Srgrimes 3681556Srgrimes 3691556Srgrimes 370228013Sjillesstatic union node * 37190111Simpevalcase(union node *n, int flags) 37217987Speter{ 3731556Srgrimes union node *cp; 3741556Srgrimes union node *patp; 3751556Srgrimes struct arglist arglist; 3761556Srgrimes struct stackmark smark; 3771556Srgrimes 3781556Srgrimes setstackmark(&smark); 3791556Srgrimes arglist.lastp = &arglist.list; 38017987Speter oexitstatus = exitstatus; 3811556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 382228013Sjilles for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) { 3831556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3841556Srgrimes if (casematch(patp, arglist.list->text)) { 385228013Sjilles popstackmark(&smark); 386223186Sjilles while (cp->nclist.next && 387223186Sjilles cp->type == NCLISTFALLTHRU) { 388223186Sjilles evaltree(cp->nclist.body, 389223186Sjilles flags & ~EV_EXIT); 390228013Sjilles if (evalskip != 0) 391228013Sjilles return (NULL); 392223186Sjilles cp = cp->nclist.next; 393223186Sjilles } 394230154Sjilles if (cp->nclist.body == NULL) 395230154Sjilles exitstatus = 0; 396228013Sjilles return (cp->nclist.body); 3971556Srgrimes } 3981556Srgrimes } 3991556Srgrimes } 4001556Srgrimes popstackmark(&smark); 401230154Sjilles exitstatus = 0; 402228013Sjilles return (NULL); 4031556Srgrimes} 4041556Srgrimes 4051556Srgrimes 4061556Srgrimes 4071556Srgrimes/* 4081556Srgrimes * Kick off a subshell to evaluate a tree. 4091556Srgrimes */ 4101556Srgrimes 411213811Sobrienstatic void 41290111Simpevalsubshell(union node *n, int flags) 41317987Speter{ 4141556Srgrimes struct job *jp; 4151556Srgrimes int backgnd = (n->type == NBACKGND); 4161556Srgrimes 417222716Sjilles oexitstatus = exitstatus; 4181556Srgrimes expredir(n->nredir.redirect); 419194774Sjilles if ((!backgnd && flags & EV_EXIT && !have_traps()) || 420194774Sjilles forkshell(jp = makejob(n, 1), n, backgnd) == 0) { 4211556Srgrimes if (backgnd) 4221556Srgrimes flags &=~ EV_TESTED; 4231556Srgrimes redirect(n->nredir.redirect, 0); 4241556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 425201053Sjilles } else if (! backgnd) { 4261556Srgrimes INTOFF; 42745916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4281556Srgrimes INTON; 429221027Sjilles } else 430221027Sjilles exitstatus = 0; 4311556Srgrimes} 4321556Srgrimes 4331556Srgrimes 434205154Sjilles/* 435205154Sjilles * Evaluate a redirected compound command. 436205154Sjilles */ 4371556Srgrimes 438213811Sobrienstatic void 439205154Sjillesevalredir(union node *n, int flags) 440205154Sjilles{ 441205154Sjilles struct jmploc jmploc; 442205154Sjilles struct jmploc *savehandler; 443205154Sjilles volatile int in_redirect = 1; 444205154Sjilles 445222716Sjilles oexitstatus = exitstatus; 446205154Sjilles expredir(n->nredir.redirect); 447205154Sjilles savehandler = handler; 448205154Sjilles if (setjmp(jmploc.loc)) { 449205154Sjilles int e; 450205154Sjilles 451205154Sjilles handler = savehandler; 452205154Sjilles e = exception; 453220978Sjilles popredir(); 454205154Sjilles if (e == EXERROR || e == EXEXEC) { 455205154Sjilles if (in_redirect) { 456205154Sjilles exitstatus = 2; 457205154Sjilles return; 458205154Sjilles } 459205154Sjilles } 460205154Sjilles longjmp(handler->loc, 1); 461205154Sjilles } else { 462205154Sjilles INTOFF; 463205154Sjilles handler = &jmploc; 464205154Sjilles redirect(n->nredir.redirect, REDIR_PUSH); 465205154Sjilles in_redirect = 0; 466205154Sjilles INTON; 467205154Sjilles evaltree(n->nredir.n, flags); 468205154Sjilles } 469205154Sjilles INTOFF; 470205154Sjilles handler = savehandler; 471205154Sjilles popredir(); 472205154Sjilles INTON; 473205154Sjilles} 474205154Sjilles 475205154Sjilles 4761556Srgrimes/* 4771556Srgrimes * Compute the names of the files in a redirection list. 4781556Srgrimes */ 4791556Srgrimes 480213811Sobrienstatic void 48190111Simpexpredir(union node *n) 48217987Speter{ 48325222Ssteve union node *redir; 4841556Srgrimes 4851556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 48617987Speter struct arglist fn; 48717987Speter fn.lastp = &fn.list; 48817987Speter switch (redir->type) { 48917987Speter case NFROM: 49017987Speter case NTO: 49166612Sbrian case NFROMTO: 49217987Speter case NAPPEND: 49396922Stjr case NCLOBBER: 4941556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4951556Srgrimes redir->nfile.expfname = fn.list->text; 49617987Speter break; 49717987Speter case NFROMFD: 49817987Speter case NTOFD: 49917987Speter if (redir->ndup.vname) { 500181017Sstefanf expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); 50117987Speter fixredir(redir, fn.list->text, 1); 50217987Speter } 50317987Speter break; 5041556Srgrimes } 5051556Srgrimes } 5061556Srgrimes} 5071556Srgrimes 5081556Srgrimes 5091556Srgrimes 5101556Srgrimes/* 5111556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 5121556Srgrimes * of the process creating the pipeline. (This differs from some versions 5131556Srgrimes * of the shell, which make the last process in a pipeline the parent 5141556Srgrimes * of all the rest.) 5151556Srgrimes */ 5161556Srgrimes 517213811Sobrienstatic void 51890111Simpevalpipe(union node *n) 51917987Speter{ 5201556Srgrimes struct job *jp; 5211556Srgrimes struct nodelist *lp; 5221556Srgrimes int pipelen; 5231556Srgrimes int prevfd; 5241556Srgrimes int pip[2]; 5251556Srgrimes 526149802Sstefanf TRACE(("evalpipe(%p) called\n", (void *)n)); 5271556Srgrimes pipelen = 0; 5281556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 5291556Srgrimes pipelen++; 5301556Srgrimes INTOFF; 5311556Srgrimes jp = makejob(n, pipelen); 5321556Srgrimes prevfd = -1; 5331556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 5341556Srgrimes prehash(lp->n); 5351556Srgrimes pip[1] = -1; 5361556Srgrimes if (lp->next) { 5371556Srgrimes if (pipe(pip) < 0) { 5381556Srgrimes close(prevfd); 53953891Scracauer error("Pipe call failed: %s", strerror(errno)); 5401556Srgrimes } 5411556Srgrimes } 5421556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 5431556Srgrimes INTON; 5441556Srgrimes if (prevfd > 0) { 545124780Sdes dup2(prevfd, 0); 5461556Srgrimes close(prevfd); 5471556Srgrimes } 5481556Srgrimes if (pip[1] >= 0) { 54953282Scracauer if (!(prevfd >= 0 && pip[0] == 0)) 55052900Scracauer close(pip[0]); 5511556Srgrimes if (pip[1] != 1) { 552124780Sdes dup2(pip[1], 1); 5531556Srgrimes close(pip[1]); 5541556Srgrimes } 5551556Srgrimes } 5561556Srgrimes evaltree(lp->n, EV_EXIT); 5571556Srgrimes } 5581556Srgrimes if (prevfd >= 0) 5591556Srgrimes close(prevfd); 5601556Srgrimes prevfd = pip[0]; 561221970Sjilles if (pip[1] != -1) 562221970Sjilles close(pip[1]); 5631556Srgrimes } 5641556Srgrimes INTON; 5651556Srgrimes if (n->npipe.backgnd == 0) { 5661556Srgrimes INTOFF; 56745916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 5681556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5691556Srgrimes INTON; 570221027Sjilles } else 571221027Sjilles exitstatus = 0; 5721556Srgrimes} 5731556Srgrimes 5741556Srgrimes 5751556Srgrimes 576216778Sjillesstatic int 577216778Sjillesis_valid_fast_cmdsubst(union node *n) 578216778Sjilles{ 579216778Sjilles 580223024Sjilles return (n->type == NCMD); 581216778Sjilles} 582216778Sjilles 5831556Srgrimes/* 5841556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5851556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5861556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5871556Srgrimes * Should be called with interrupts off. 5881556Srgrimes */ 5891556Srgrimes 5901556Srgrimesvoid 59190111Simpevalbackcmd(union node *n, struct backcmd *result) 59217987Speter{ 5931556Srgrimes int pip[2]; 5941556Srgrimes struct job *jp; 5951556Srgrimes struct stackmark smark; /* unnecessary */ 596216761Sjilles struct jmploc jmploc; 597216761Sjilles struct jmploc *savehandler; 598223024Sjilles struct localvar *savelocalvars; 5991556Srgrimes 6001556Srgrimes setstackmark(&smark); 6011556Srgrimes result->fd = -1; 6021556Srgrimes result->buf = NULL; 6031556Srgrimes result->nleft = 0; 6041556Srgrimes result->jp = NULL; 60517987Speter if (n == NULL) { 60617987Speter exitstatus = 0; 6071556Srgrimes goto out; 60817987Speter } 609216778Sjilles if (is_valid_fast_cmdsubst(n)) { 61017987Speter exitstatus = oexitstatus; 611223024Sjilles savelocalvars = localvars; 612223024Sjilles localvars = NULL; 613223024Sjilles forcelocal++; 614216761Sjilles savehandler = handler; 615216761Sjilles if (setjmp(jmploc.loc)) { 616216761Sjilles if (exception == EXERROR || exception == EXEXEC) 617216761Sjilles exitstatus = 2; 618216761Sjilles else if (exception != 0) { 619216761Sjilles handler = savehandler; 620223024Sjilles forcelocal--; 621223024Sjilles poplocalvars(); 622223024Sjilles localvars = savelocalvars; 623216761Sjilles longjmp(handler->loc, 1); 624216761Sjilles } 625216761Sjilles } else { 626216761Sjilles handler = &jmploc; 627216761Sjilles evalcommand(n, EV_BACKCMD, result); 628216761Sjilles } 629216761Sjilles handler = savehandler; 630223024Sjilles forcelocal--; 631223024Sjilles poplocalvars(); 632223024Sjilles localvars = savelocalvars; 6331556Srgrimes } else { 63417987Speter exitstatus = 0; 6351556Srgrimes if (pipe(pip) < 0) 63653891Scracauer error("Pipe call failed: %s", strerror(errno)); 6371556Srgrimes jp = makejob(n, 1); 6381556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 6391556Srgrimes FORCEINTON; 6401556Srgrimes close(pip[0]); 6411556Srgrimes if (pip[1] != 1) { 642124780Sdes dup2(pip[1], 1); 6431556Srgrimes close(pip[1]); 6441556Srgrimes } 6451556Srgrimes evaltree(n, EV_EXIT); 6461556Srgrimes } 6471556Srgrimes close(pip[1]); 6481556Srgrimes result->fd = pip[0]; 6491556Srgrimes result->jp = jp; 6501556Srgrimes } 6511556Srgrimesout: 6521556Srgrimes popstackmark(&smark); 653109627Stjr TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 6541556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 6551556Srgrimes} 6561556Srgrimes 657216826Sjilles/* 658216826Sjilles * Check if a builtin can safely be executed in the same process, 659216826Sjilles * even though it should be in a subshell (command substitution). 660216826Sjilles * Note that jobid, jobs, times and trap can show information not 661216826Sjilles * available in a child process; this is deliberate. 662216826Sjilles * The arguments should already have been expanded. 663216826Sjilles */ 664216826Sjillesstatic int 665216826Sjillessafe_builtin(int idx, int argc, char **argv) 666216826Sjilles{ 667216826Sjilles if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD || 668216826Sjilles idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD || 669216826Sjilles idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD || 670216826Sjilles idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD || 671216826Sjilles idx == TYPECMD) 672216826Sjilles return (1); 673216826Sjilles if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD || 674216826Sjilles idx == UMASKCMD) 675216826Sjilles return (argc <= 1 || (argc == 2 && argv[1][0] == '-')); 676216826Sjilles if (idx == SETCMD) 677216826Sjilles return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' || 678216826Sjilles argv[1][0] == '+') && argv[1][1] == 'o' && 679216826Sjilles argv[1][2] == '\0')); 680216826Sjilles return (0); 681216826Sjilles} 6821556Srgrimes 6831556Srgrimes/* 6841556Srgrimes * Execute a simple command. 685217035Sjilles * Note: This may or may not return if (flags & EV_EXIT). 6861556Srgrimes */ 6871556Srgrimes 688213811Sobrienstatic void 68990111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd) 69017987Speter{ 6911556Srgrimes struct stackmark smark; 6921556Srgrimes union node *argp; 6931556Srgrimes struct arglist arglist; 6941556Srgrimes struct arglist varlist; 6951556Srgrimes char **argv; 6961556Srgrimes int argc; 6971556Srgrimes char **envp; 6981556Srgrimes int varflag; 6991556Srgrimes struct strlist *sp; 7001556Srgrimes int mode; 7011556Srgrimes int pip[2]; 7021556Srgrimes struct cmdentry cmdentry; 7031556Srgrimes struct job *jp; 7041556Srgrimes struct jmploc jmploc; 705194765Sjilles struct jmploc *savehandler; 706194765Sjilles char *savecmdname; 707194765Sjilles struct shparam saveparam; 708194765Sjilles struct localvar *savelocalvars; 709199647Sjilles struct parsefile *savetopfile; 7101556Srgrimes volatile int e; 7111556Srgrimes char *lastarg; 71245916Scracauer int realstatus; 71354884Scracauer int do_clearcmdentry; 714211287Sjilles const char *path = pathval(); 7151556Srgrimes 7161556Srgrimes /* First expand the arguments. */ 717149802Sstefanf TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 7181556Srgrimes setstackmark(&smark); 7191556Srgrimes arglist.lastp = &arglist.list; 7201556Srgrimes varlist.lastp = &varlist.list; 7211556Srgrimes varflag = 1; 722217035Sjilles jp = NULL; 72354884Scracauer do_clearcmdentry = 0; 72417987Speter oexitstatus = exitstatus; 72517987Speter exitstatus = 0; 7261556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 727222165Sjilles if (varflag && isassignment(argp->narg.text)) { 728222165Sjilles expandarg(argp, &varlist, EXP_VARTILDE); 729222165Sjilles continue; 7301556Srgrimes } 7311556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 7321556Srgrimes varflag = 0; 7331556Srgrimes } 7341556Srgrimes *arglist.lastp = NULL; 7351556Srgrimes *varlist.lastp = NULL; 7361556Srgrimes expredir(cmd->ncmd.redirect); 7371556Srgrimes argc = 0; 7381556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 7391556Srgrimes argc++; 740218306Sjilles /* Add one slot at the beginning for tryexec(). */ 741218306Sjilles argv = stalloc(sizeof (char *) * (argc + 2)); 742218306Sjilles argv++; 7431556Srgrimes 7441556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 7451556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 7461556Srgrimes *argv++ = sp->text; 7471556Srgrimes } 7481556Srgrimes *argv = NULL; 7491556Srgrimes lastarg = NULL; 7501556Srgrimes if (iflag && funcnest == 0 && argc > 0) 7511556Srgrimes lastarg = argv[-1]; 7521556Srgrimes argv -= argc; 7531556Srgrimes 7541556Srgrimes /* Print the command if xflag is set. */ 7551556Srgrimes if (xflag) { 756159632Sstefanf char sep = 0; 757222907Sjilles const char *p, *ps4; 758222907Sjilles ps4 = expandstr(ps4val()); 759222907Sjilles out2str(ps4 != NULL ? ps4 : ps4val()); 7601556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 761159632Sstefanf if (sep != 0) 762201366Sjilles out2c(' '); 763215567Sjilles p = strchr(sp->text, '='); 764215567Sjilles if (p != NULL) { 765215567Sjilles p++; 766215567Sjilles outbin(sp->text, p - sp->text, out2); 767194786Sjilles out2qstr(p); 768215567Sjilles } else 769215567Sjilles out2qstr(sp->text); 770159632Sstefanf sep = ' '; 7711556Srgrimes } 7721556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 773159632Sstefanf if (sep != 0) 774201366Sjilles out2c(' '); 775194786Sjilles /* Disambiguate command looking like assignment. */ 776194786Sjilles if (sp == arglist.list && 777194786Sjilles strchr(sp->text, '=') != NULL && 778194786Sjilles strchr(sp->text, '\'') == NULL) { 779194786Sjilles out2c('\''); 780194786Sjilles out2str(sp->text); 781194786Sjilles out2c('\''); 782194786Sjilles } else 783194786Sjilles out2qstr(sp->text); 784159632Sstefanf sep = ' '; 7851556Srgrimes } 786201366Sjilles out2c('\n'); 7871556Srgrimes flushout(&errout); 7881556Srgrimes } 7891556Srgrimes 7901556Srgrimes /* Now locate the command. */ 7911556Srgrimes if (argc == 0) { 792157601Sstefanf /* Variable assignment(s) without command */ 7931556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 7941556Srgrimes cmdentry.u.index = BLTINCMD; 795205138Sjilles cmdentry.special = 0; 7961556Srgrimes } else { 79717987Speter static const char PATH[] = "PATH="; 798204800Sjilles int cmd_flags = 0, bltinonly = 0; 79917987Speter 80017987Speter /* 80117987Speter * Modify the command lookup path, if a PATH= assignment 80217987Speter * is present 80317987Speter */ 80417987Speter for (sp = varlist.list ; sp ; sp = sp->next) 80554884Scracauer if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 80617987Speter path = sp->text + sizeof(PATH) - 1; 807155301Sschweikh /* 80854884Scracauer * On `PATH=... command`, we need to make 80954884Scracauer * sure that the command isn't using the 81054884Scracauer * non-updated hash table of the outer PATH 811155301Sschweikh * setting and we need to make sure that 81254884Scracauer * the hash table isn't filled with items 81354884Scracauer * from the temporary setting. 81454884Scracauer * 815155301Sschweikh * It would be better to forbit using and 81654884Scracauer * updating the table while this command 81754884Scracauer * runs, by the command finding mechanism 81854884Scracauer * is heavily integrated with hash handling, 81954884Scracauer * so we just delete the hash before and after 82054884Scracauer * the command runs. Partly deleting like 82154884Scracauer * changepatch() does doesn't seem worth the 82254884Scracauer * bookinging effort, since most such runs add 823123996Smaxim * directories in front of the new PATH. 82454884Scracauer */ 825218324Sjilles clearcmdentry(); 82654884Scracauer do_clearcmdentry = 1; 82754884Scracauer } 82817987Speter 829204800Sjilles for (;;) { 830204800Sjilles if (bltinonly) { 831204800Sjilles cmdentry.u.index = find_builtin(*argv, &cmdentry.special); 832204800Sjilles if (cmdentry.u.index < 0) { 833201431Sjilles cmdentry.u.index = BLTINCMD; 834201431Sjilles argv--; 835201431Sjilles argc++; 836201431Sjilles break; 8371556Srgrimes } 838204800Sjilles } else 839204800Sjilles find_command(argv[0], &cmdentry, cmd_flags, path); 840204800Sjilles /* implement the bltin and command builtins here */ 841204800Sjilles if (cmdentry.cmdtype != CMDBUILTIN) 842204800Sjilles break; 843204800Sjilles if (cmdentry.u.index == BLTINCMD) { 844204800Sjilles if (argc == 1) 8451556Srgrimes break; 846204800Sjilles argv++; 847204800Sjilles argc--; 848204800Sjilles bltinonly = 1; 849204800Sjilles } else if (cmdentry.u.index == COMMANDCMD) { 850204800Sjilles if (argc == 1) 851204800Sjilles break; 852204800Sjilles if (!strcmp(argv[1], "-p")) { 853204800Sjilles if (argc == 2) 854204800Sjilles break; 855204800Sjilles if (argv[2][0] == '-') { 856204800Sjilles if (strcmp(argv[2], "--")) 857204800Sjilles break; 858204800Sjilles if (argc == 3) 859204800Sjilles break; 860204800Sjilles argv += 3; 861204800Sjilles argc -= 3; 862204800Sjilles } else { 863204800Sjilles argv += 2; 864204800Sjilles argc -= 2; 865204800Sjilles } 866204800Sjilles path = _PATH_STDPATH; 867218324Sjilles clearcmdentry(); 868204800Sjilles do_clearcmdentry = 1; 869204800Sjilles } else if (!strcmp(argv[1], "--")) { 870204800Sjilles if (argc == 2) 871204800Sjilles break; 872204800Sjilles argv += 2; 873204800Sjilles argc -= 2; 874204800Sjilles } else if (argv[1][0] == '-') 875204800Sjilles break; 876204800Sjilles else { 877204800Sjilles argv++; 878204800Sjilles argc--; 879204800Sjilles } 880204800Sjilles cmd_flags |= DO_NOFUNC; 881204800Sjilles bltinonly = 0; 882204800Sjilles } else 883204800Sjilles break; 8841556Srgrimes } 885204800Sjilles /* 886204800Sjilles * Special builtins lose their special properties when 887204800Sjilles * called via 'command'. 888204800Sjilles */ 889204800Sjilles if (cmd_flags & DO_NOFUNC) 890204800Sjilles cmdentry.special = 0; 8911556Srgrimes } 8921556Srgrimes 8931556Srgrimes /* Fork off a child process if necessary. */ 894223282Sjilles if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 895194127Sjilles && ((flags & EV_EXIT) == 0 || have_traps())) 89617987Speter || ((flags & EV_BACKCMD) != 0 897216826Sjilles && (cmdentry.cmdtype != CMDBUILTIN || 898216826Sjilles !safe_builtin(cmdentry.u.index, argc, argv)))) { 8991556Srgrimes jp = makejob(cmd, 1); 900223282Sjilles mode = FORK_FG; 9011556Srgrimes if (flags & EV_BACKCMD) { 9021556Srgrimes mode = FORK_NOJOB; 9031556Srgrimes if (pipe(pip) < 0) 90453891Scracauer error("Pipe call failed: %s", strerror(errno)); 9051556Srgrimes } 9061556Srgrimes if (forkshell(jp, cmd, mode) != 0) 9071556Srgrimes goto parent; /* at end of routine */ 9081556Srgrimes if (flags & EV_BACKCMD) { 9091556Srgrimes FORCEINTON; 9101556Srgrimes close(pip[0]); 9111556Srgrimes if (pip[1] != 1) { 912124780Sdes dup2(pip[1], 1); 9131556Srgrimes close(pip[1]); 9141556Srgrimes } 915223163Sjilles flags &= ~EV_BACKCMD; 9161556Srgrimes } 9171556Srgrimes flags |= EV_EXIT; 9181556Srgrimes } 9191556Srgrimes 9201556Srgrimes /* This is the child process if a fork occurred. */ 9211556Srgrimes /* Execute the command. */ 9221556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 92320425Ssteve#ifdef DEBUG 9241556Srgrimes trputs("Shell function: "); trargs(argv); 92520425Ssteve#endif 9261556Srgrimes saveparam = shellparam; 9271556Srgrimes shellparam.malloc = 0; 92820425Ssteve shellparam.reset = 1; 9291556Srgrimes shellparam.nparam = argc - 1; 9301556Srgrimes shellparam.p = argv + 1; 9311556Srgrimes shellparam.optnext = NULL; 9321556Srgrimes INTOFF; 9331556Srgrimes savelocalvars = localvars; 9341556Srgrimes localvars = NULL; 935196483Sjilles reffunc(cmdentry.u.func); 936194765Sjilles savehandler = handler; 9371556Srgrimes if (setjmp(jmploc.loc)) { 938218306Sjilles freeparam(&shellparam); 939218306Sjilles shellparam = saveparam; 940220978Sjilles popredir(); 941196483Sjilles unreffunc(cmdentry.u.func); 9421556Srgrimes poplocalvars(); 9431556Srgrimes localvars = savelocalvars; 944201283Sjilles funcnest--; 9451556Srgrimes handler = savehandler; 9461556Srgrimes longjmp(handler->loc, 1); 9471556Srgrimes } 9481556Srgrimes handler = &jmploc; 949201283Sjilles funcnest++; 950204802Sjilles redirect(cmd->ncmd.redirect, REDIR_PUSH); 951199660Sjilles INTON; 9521556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 9531556Srgrimes mklocal(sp->text); 954185231Sstefanf exitstatus = oexitstatus; 955220978Sjilles evaltree(getfuncnode(cmdentry.u.func), 956220978Sjilles flags & (EV_TESTED | EV_EXIT)); 9571556Srgrimes INTOFF; 958196483Sjilles unreffunc(cmdentry.u.func); 9591556Srgrimes poplocalvars(); 9601556Srgrimes localvars = savelocalvars; 9611556Srgrimes freeparam(&shellparam); 9621556Srgrimes shellparam = saveparam; 9631556Srgrimes handler = savehandler; 964201283Sjilles funcnest--; 9651556Srgrimes popredir(); 9661556Srgrimes INTON; 9671556Srgrimes if (evalskip == SKIPFUNC) { 9681556Srgrimes evalskip = 0; 9691556Srgrimes skipcount = 0; 9701556Srgrimes } 971217035Sjilles if (jp) 9721556Srgrimes exitshell(exitstatus); 9731556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 97420425Ssteve#ifdef DEBUG 9751556Srgrimes trputs("builtin command: "); trargs(argv); 97620425Ssteve#endif 9771556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 9781556Srgrimes if (flags == EV_BACKCMD) { 9791556Srgrimes memout.nleft = 0; 9801556Srgrimes memout.nextc = memout.buf; 9811556Srgrimes memout.bufsize = 64; 9821556Srgrimes mode |= REDIR_BACKQ; 9831556Srgrimes } 9841556Srgrimes savecmdname = commandname; 985199647Sjilles savetopfile = getcurrentfile(); 9861556Srgrimes cmdenviron = varlist.list; 9871556Srgrimes e = -1; 988194765Sjilles savehandler = handler; 9891556Srgrimes if (setjmp(jmploc.loc)) { 9901556Srgrimes e = exception; 991220978Sjilles if (e == EXINT) 992220978Sjilles exitstatus = SIGINT+128; 993220978Sjilles else if (e != EXEXIT) 994220978Sjilles exitstatus = 2; 9951556Srgrimes goto cmddone; 9961556Srgrimes } 9971556Srgrimes handler = &jmploc; 998157601Sstefanf redirect(cmd->ncmd.redirect, mode); 999205138Sjilles /* 1000205138Sjilles * If there is no command word, redirection errors should 1001205138Sjilles * not be fatal but assignment errors should. 1002205138Sjilles */ 1003228937Sjilles if (argc == 0) 1004205138Sjilles cmdentry.special = 1; 1005216870Sjilles listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET); 1006207678Sjilles if (argc > 0) 1007207678Sjilles bltinsetlocale(); 10081556Srgrimes commandname = argv[0]; 10091556Srgrimes argptr = argv + 1; 1010201053Sjilles nextopt_optptr = NULL; /* initialize nextopt */ 1011193169Sstefanf builtin_flags = flags; 10121556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 10131556Srgrimes flushall(); 10141556Srgrimescmddone: 1015207678Sjilles if (argc > 0) 1016207678Sjilles bltinunsetlocale(); 101760592Scracauer cmdenviron = NULL; 10181556Srgrimes out1 = &output; 10191556Srgrimes out2 = &errout; 10201556Srgrimes freestdout(); 1021217035Sjilles handler = savehandler; 1022218306Sjilles commandname = savecmdname; 1023218306Sjilles if (jp) 1024218306Sjilles exitshell(exitstatus); 1025201366Sjilles if (flags == EV_BACKCMD) { 1026201366Sjilles backcmd->buf = memout.buf; 1027201366Sjilles backcmd->nleft = memout.nextc - memout.buf; 1028201366Sjilles memout.buf = NULL; 1029201366Sjilles } 1030220978Sjilles if (cmdentry.u.index != EXECCMD) 1031204801Sjilles popredir(); 10321556Srgrimes if (e != -1) { 103320425Ssteve if ((e != EXERROR && e != EXEXEC) 1034157601Sstefanf || cmdentry.special) 10351556Srgrimes exraise(e); 1036199647Sjilles popfilesupto(savetopfile); 1037201366Sjilles if (flags != EV_BACKCMD) 1038201366Sjilles FORCEINTON; 10391556Srgrimes } 10401556Srgrimes } else { 104120425Ssteve#ifdef DEBUG 10421556Srgrimes trputs("normal command: "); trargs(argv); 104320425Ssteve#endif 10441556Srgrimes redirect(cmd->ncmd.redirect, 0); 10451556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 10461556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 10471556Srgrimes envp = environment(); 1048204800Sjilles shellexec(argv, envp, path, cmdentry.u.index); 10491556Srgrimes /*NOTREACHED*/ 10501556Srgrimes } 10511556Srgrimes goto out; 10521556Srgrimes 10531556Srgrimesparent: /* parent process gets here (if we forked) */ 1054212214Sjilles if (mode == FORK_FG) { /* argument to fork */ 10551556Srgrimes INTOFF; 105645916Scracauer exitstatus = waitforjob(jp, &realstatus); 10571556Srgrimes INTON; 105845916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 105945266Scracauer evalskip = SKIPBREAK; 106045266Scracauer skipcount = loopnest; 106145266Scracauer } 1062212214Sjilles } else if (mode == FORK_NOJOB) { 10631556Srgrimes backcmd->fd = pip[0]; 10641556Srgrimes close(pip[1]); 10651556Srgrimes backcmd->jp = jp; 1066223282Sjilles } 10671556Srgrimes 10681556Srgrimesout: 10691556Srgrimes if (lastarg) 10701556Srgrimes setvar("_", lastarg, 0); 107154884Scracauer if (do_clearcmdentry) 1072218324Sjilles clearcmdentry(); 10731556Srgrimes popstackmark(&smark); 10741556Srgrimes} 10751556Srgrimes 10761556Srgrimes 10771556Srgrimes 10781556Srgrimes/* 10791556Srgrimes * Search for a command. This is called before we fork so that the 10801556Srgrimes * location of the command will be available in the parent as well as 10811556Srgrimes * the child. The check for "goodname" is an overly conservative 10821556Srgrimes * check that the name will not be subject to expansion. 10831556Srgrimes */ 10841556Srgrimes 1085213811Sobrienstatic void 108690111Simpprehash(union node *n) 108717987Speter{ 10881556Srgrimes struct cmdentry entry; 10891556Srgrimes 1090159633Sstefanf if (n && n->type == NCMD && n->ncmd.args) 109117987Speter if (goodname(n->ncmd.args->narg.text)) 109217987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 109317987Speter pathval()); 10941556Srgrimes} 10951556Srgrimes 10961556Srgrimes 10971556Srgrimes 10981556Srgrimes/* 10991556Srgrimes * Builtin commands. Builtin commands whose functions are closely 11001556Srgrimes * tied to evaluation are implemented here. 11011556Srgrimes */ 11021556Srgrimes 11031556Srgrimes/* 1104201431Sjilles * No command given, a bltin command with no arguments, or a bltin command 1105201431Sjilles * with an invalid name. 11061556Srgrimes */ 11071556Srgrimes 110817987Speterint 1109201431Sjillesbltincmd(int argc, char **argv) 111017987Speter{ 1111201431Sjilles if (argc > 1) { 1112201431Sjilles out2fmt_flush("%s: not found\n", argv[1]); 1113201431Sjilles return 127; 1114201431Sjilles } 111520425Ssteve /* 111617987Speter * Preserve exitstatus of a previous possible redirection 111720425Ssteve * as POSIX mandates 111817987Speter */ 11191556Srgrimes return exitstatus; 11201556Srgrimes} 11211556Srgrimes 11221556Srgrimes 11231556Srgrimes/* 11241556Srgrimes * Handle break and continue commands. Break, continue, and return are 11251556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 11261556Srgrimes * above all check this flag, and if it is set they start skipping 11271556Srgrimes * commands rather than executing them. The variable skipcount is 11281556Srgrimes * the number of loops to break/continue, or the number of function 11291556Srgrimes * levels to return. (The latter is always 1.) It should probably 11301556Srgrimes * be an error to break out of more loops than exist, but it isn't 11311556Srgrimes * in the standard shell so we don't make it one here. 11321556Srgrimes */ 11331556Srgrimes 113417987Speterint 113590111Simpbreakcmd(int argc, char **argv) 113617987Speter{ 113720425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 11381556Srgrimes 11391556Srgrimes if (n > loopnest) 11401556Srgrimes n = loopnest; 11411556Srgrimes if (n > 0) { 11421556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 11431556Srgrimes skipcount = n; 11441556Srgrimes } 11451556Srgrimes return 0; 11461556Srgrimes} 11471556Srgrimes 1148100437Stjr/* 1149100437Stjr * The `command' command. 1150100437Stjr */ 1151100437Stjrint 1152100437Stjrcommandcmd(int argc, char **argv) 1153100437Stjr{ 1154207783Sjilles const char *path; 1155100437Stjr int ch; 1156151810Sstefanf int cmd = -1; 11571556Srgrimes 1158204800Sjilles path = bltinlookup("PATH", 1); 1159100437Stjr 1160100437Stjr optind = optreset = 1; 1161100663Stjr opterr = 0; 1162151810Sstefanf while ((ch = getopt(argc, argv, "pvV")) != -1) { 1163100437Stjr switch (ch) { 1164100437Stjr case 'p': 1165207783Sjilles path = _PATH_STDPATH; 1166100437Stjr break; 1167151810Sstefanf case 'v': 1168151810Sstefanf cmd = TYPECMD_SMALLV; 1169151810Sstefanf break; 1170151810Sstefanf case 'V': 1171151810Sstefanf cmd = TYPECMD_BIGV; 1172151810Sstefanf break; 1173100437Stjr case '?': 1174100437Stjr default: 1175100437Stjr error("unknown option: -%c", optopt); 1176100437Stjr } 1177100437Stjr } 1178100437Stjr argc -= optind; 1179100437Stjr argv += optind; 1180100437Stjr 1181151810Sstefanf if (cmd != -1) { 1182151810Sstefanf if (argc != 1) 1183151810Sstefanf error("wrong number of arguments"); 1184201343Sjilles return typecmd_impl(2, argv - 1, cmd, path); 1185151810Sstefanf } 1186204800Sjilles if (argc != 0) 1187214538Sjilles error("commandcmd bad call"); 1188100437Stjr 1189100437Stjr /* 1190100437Stjr * Do nothing successfully if no command was specified; 1191100437Stjr * ksh also does this. 1192100437Stjr */ 1193204800Sjilles return 0; 1194100437Stjr} 1195100437Stjr 1196100437Stjr 11971556Srgrimes/* 11981556Srgrimes * The return command. 11991556Srgrimes */ 12001556Srgrimes 120117987Speterint 120290111Simpreturncmd(int argc, char **argv) 120317987Speter{ 120420425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 12051556Srgrimes 12061556Srgrimes if (funcnest) { 12071556Srgrimes evalskip = SKIPFUNC; 12081556Srgrimes skipcount = 1; 120920425Ssteve } else { 121020425Ssteve /* skip the rest of the file */ 121120425Ssteve evalskip = SKIPFILE; 121220425Ssteve skipcount = 1; 12131556Srgrimes } 12141556Srgrimes return ret; 12151556Srgrimes} 12161556Srgrimes 12171556Srgrimes 121817987Speterint 121990111Simpfalsecmd(int argc __unused, char **argv __unused) 122017987Speter{ 122117987Speter return 1; 122217987Speter} 122317987Speter 122417987Speter 122517987Speterint 122690111Simptruecmd(int argc __unused, char **argv __unused) 122717987Speter{ 12281556Srgrimes return 0; 12291556Srgrimes} 12301556Srgrimes 12311556Srgrimes 123217987Speterint 123390111Simpexeccmd(int argc, char **argv) 123417987Speter{ 1235208630Sjilles /* 1236208630Sjilles * Because we have historically not supported any options, 1237208630Sjilles * only treat "--" specially. 1238208630Sjilles */ 1239208630Sjilles if (argc > 1 && strcmp(argv[1], "--") == 0) 1240208630Sjilles argc--, argv++; 12411556Srgrimes if (argc > 1) { 124217987Speter struct strlist *sp; 124317987Speter 12441556Srgrimes iflag = 0; /* exit on error */ 12451556Srgrimes mflag = 0; 12461556Srgrimes optschanged(); 124717987Speter for (sp = cmdenviron; sp ; sp = sp->next) 124817987Speter setvareq(sp->text, VEXPORT|VSTACK); 12491556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 12501556Srgrimes 12511556Srgrimes } 12521556Srgrimes return 0; 12531556Srgrimes} 1254153091Sstefanf 1255153091Sstefanf 1256153091Sstefanfint 1257153091Sstefanftimescmd(int argc __unused, char **argv __unused) 1258153091Sstefanf{ 1259153091Sstefanf struct rusage ru; 1260153091Sstefanf long shumins, shsmins, chumins, chsmins; 1261153091Sstefanf double shusecs, shssecs, chusecs, chssecs; 1262153091Sstefanf 1263153091Sstefanf if (getrusage(RUSAGE_SELF, &ru) < 0) 1264153091Sstefanf return 1; 1265153091Sstefanf shumins = ru.ru_utime.tv_sec / 60; 1266153091Sstefanf shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1267153091Sstefanf shsmins = ru.ru_stime.tv_sec / 60; 1268153091Sstefanf shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1269153091Sstefanf if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1270153091Sstefanf return 1; 1271153091Sstefanf chumins = ru.ru_utime.tv_sec / 60; 1272153091Sstefanf chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1273153091Sstefanf chsmins = ru.ru_stime.tv_sec / 60; 1274153091Sstefanf chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1275153091Sstefanf out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1276153091Sstefanf shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1277153091Sstefanf return 0; 1278153091Sstefanf} 1279