eval.c revision 223163
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 223163 2011-06-16 21:50:28Z 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); 92213811Sobrienstatic void 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: 259214600Sjilles 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 if (evalskip) 3471556Srgrimes goto out; 3481556Srgrimes } 3491556Srgrimes *arglist.lastp = NULL; 3501556Srgrimes 3511556Srgrimes exitstatus = 0; 3521556Srgrimes loopnest++; 3531556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3541556Srgrimes setvar(n->nfor.var, sp->text, 0); 355149933Sstefanf evaltree(n->nfor.body, flags); 3561556Srgrimes if (evalskip) { 3571556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3581556Srgrimes evalskip = 0; 3591556Srgrimes continue; 3601556Srgrimes } 3611556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3621556Srgrimes evalskip = 0; 3631556Srgrimes break; 3641556Srgrimes } 3651556Srgrimes } 3661556Srgrimes loopnest--; 3671556Srgrimesout: 3681556Srgrimes popstackmark(&smark); 3691556Srgrimes} 3701556Srgrimes 3711556Srgrimes 3721556Srgrimes 373213811Sobrienstatic void 37490111Simpevalcase(union node *n, int flags) 37517987Speter{ 3761556Srgrimes union node *cp; 3771556Srgrimes union node *patp; 3781556Srgrimes struct arglist arglist; 3791556Srgrimes struct stackmark smark; 3801556Srgrimes 3811556Srgrimes setstackmark(&smark); 3821556Srgrimes arglist.lastp = &arglist.list; 38317987Speter oexitstatus = exitstatus; 384172440Sstefanf exitstatus = 0; 3851556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3861556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3871556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3881556Srgrimes if (casematch(patp, arglist.list->text)) { 3891556Srgrimes if (evalskip == 0) { 3901556Srgrimes evaltree(cp->nclist.body, flags); 3911556Srgrimes } 3921556Srgrimes goto out; 3931556Srgrimes } 3941556Srgrimes } 3951556Srgrimes } 3961556Srgrimesout: 3971556Srgrimes popstackmark(&smark); 3981556Srgrimes} 3991556Srgrimes 4001556Srgrimes 4011556Srgrimes 4021556Srgrimes/* 4031556Srgrimes * Kick off a subshell to evaluate a tree. 4041556Srgrimes */ 4051556Srgrimes 406213811Sobrienstatic void 40790111Simpevalsubshell(union node *n, int flags) 40817987Speter{ 4091556Srgrimes struct job *jp; 4101556Srgrimes int backgnd = (n->type == NBACKGND); 4111556Srgrimes 412222716Sjilles oexitstatus = exitstatus; 4131556Srgrimes expredir(n->nredir.redirect); 414194774Sjilles if ((!backgnd && flags & EV_EXIT && !have_traps()) || 415194774Sjilles forkshell(jp = makejob(n, 1), n, backgnd) == 0) { 4161556Srgrimes if (backgnd) 4171556Srgrimes flags &=~ EV_TESTED; 4181556Srgrimes redirect(n->nredir.redirect, 0); 4191556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 420201053Sjilles } else if (! backgnd) { 4211556Srgrimes INTOFF; 42245916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4231556Srgrimes INTON; 424221027Sjilles } else 425221027Sjilles exitstatus = 0; 4261556Srgrimes} 4271556Srgrimes 4281556Srgrimes 429205154Sjilles/* 430205154Sjilles * Evaluate a redirected compound command. 431205154Sjilles */ 4321556Srgrimes 433213811Sobrienstatic void 434205154Sjillesevalredir(union node *n, int flags) 435205154Sjilles{ 436205154Sjilles struct jmploc jmploc; 437205154Sjilles struct jmploc *savehandler; 438205154Sjilles volatile int in_redirect = 1; 439205154Sjilles 440222716Sjilles oexitstatus = exitstatus; 441205154Sjilles expredir(n->nredir.redirect); 442205154Sjilles savehandler = handler; 443205154Sjilles if (setjmp(jmploc.loc)) { 444205154Sjilles int e; 445205154Sjilles 446205154Sjilles handler = savehandler; 447205154Sjilles e = exception; 448220978Sjilles popredir(); 449205154Sjilles if (e == EXERROR || e == EXEXEC) { 450205154Sjilles if (in_redirect) { 451205154Sjilles exitstatus = 2; 452205154Sjilles return; 453205154Sjilles } 454205154Sjilles } 455205154Sjilles longjmp(handler->loc, 1); 456205154Sjilles } else { 457205154Sjilles INTOFF; 458205154Sjilles handler = &jmploc; 459205154Sjilles redirect(n->nredir.redirect, REDIR_PUSH); 460205154Sjilles in_redirect = 0; 461205154Sjilles INTON; 462205154Sjilles evaltree(n->nredir.n, flags); 463205154Sjilles } 464205154Sjilles INTOFF; 465205154Sjilles handler = savehandler; 466205154Sjilles popredir(); 467205154Sjilles INTON; 468205154Sjilles} 469205154Sjilles 470205154Sjilles 4711556Srgrimes/* 4721556Srgrimes * Compute the names of the files in a redirection list. 4731556Srgrimes */ 4741556Srgrimes 475213811Sobrienstatic void 47690111Simpexpredir(union node *n) 47717987Speter{ 47825222Ssteve union node *redir; 4791556Srgrimes 4801556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 48117987Speter struct arglist fn; 48217987Speter fn.lastp = &fn.list; 48317987Speter switch (redir->type) { 48417987Speter case NFROM: 48517987Speter case NTO: 48666612Sbrian case NFROMTO: 48717987Speter case NAPPEND: 48896922Stjr case NCLOBBER: 4891556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4901556Srgrimes redir->nfile.expfname = fn.list->text; 49117987Speter break; 49217987Speter case NFROMFD: 49317987Speter case NTOFD: 49417987Speter if (redir->ndup.vname) { 495181017Sstefanf expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); 49617987Speter fixredir(redir, fn.list->text, 1); 49717987Speter } 49817987Speter break; 4991556Srgrimes } 5001556Srgrimes } 5011556Srgrimes} 5021556Srgrimes 5031556Srgrimes 5041556Srgrimes 5051556Srgrimes/* 5061556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 5071556Srgrimes * of the process creating the pipeline. (This differs from some versions 5081556Srgrimes * of the shell, which make the last process in a pipeline the parent 5091556Srgrimes * of all the rest.) 5101556Srgrimes */ 5111556Srgrimes 512213811Sobrienstatic void 51390111Simpevalpipe(union node *n) 51417987Speter{ 5151556Srgrimes struct job *jp; 5161556Srgrimes struct nodelist *lp; 5171556Srgrimes int pipelen; 5181556Srgrimes int prevfd; 5191556Srgrimes int pip[2]; 5201556Srgrimes 521149802Sstefanf TRACE(("evalpipe(%p) called\n", (void *)n)); 5221556Srgrimes pipelen = 0; 5231556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 5241556Srgrimes pipelen++; 5251556Srgrimes INTOFF; 5261556Srgrimes jp = makejob(n, pipelen); 5271556Srgrimes prevfd = -1; 5281556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 5291556Srgrimes prehash(lp->n); 5301556Srgrimes pip[1] = -1; 5311556Srgrimes if (lp->next) { 5321556Srgrimes if (pipe(pip) < 0) { 5331556Srgrimes close(prevfd); 53453891Scracauer error("Pipe call failed: %s", strerror(errno)); 5351556Srgrimes } 5361556Srgrimes } 5371556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 5381556Srgrimes INTON; 5391556Srgrimes if (prevfd > 0) { 540124780Sdes dup2(prevfd, 0); 5411556Srgrimes close(prevfd); 5421556Srgrimes } 5431556Srgrimes if (pip[1] >= 0) { 54453282Scracauer if (!(prevfd >= 0 && pip[0] == 0)) 54552900Scracauer close(pip[0]); 5461556Srgrimes if (pip[1] != 1) { 547124780Sdes dup2(pip[1], 1); 5481556Srgrimes close(pip[1]); 5491556Srgrimes } 5501556Srgrimes } 5511556Srgrimes evaltree(lp->n, EV_EXIT); 5521556Srgrimes } 5531556Srgrimes if (prevfd >= 0) 5541556Srgrimes close(prevfd); 5551556Srgrimes prevfd = pip[0]; 556221970Sjilles if (pip[1] != -1) 557221970Sjilles close(pip[1]); 5581556Srgrimes } 5591556Srgrimes INTON; 5601556Srgrimes if (n->npipe.backgnd == 0) { 5611556Srgrimes INTOFF; 56245916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 5631556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5641556Srgrimes INTON; 565221027Sjilles } else 566221027Sjilles exitstatus = 0; 5671556Srgrimes} 5681556Srgrimes 5691556Srgrimes 5701556Srgrimes 571216778Sjillesstatic int 572216778Sjillesis_valid_fast_cmdsubst(union node *n) 573216778Sjilles{ 574216778Sjilles 575223024Sjilles return (n->type == NCMD); 576216778Sjilles} 577216778Sjilles 5781556Srgrimes/* 5791556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5801556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5811556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5821556Srgrimes * Should be called with interrupts off. 5831556Srgrimes */ 5841556Srgrimes 5851556Srgrimesvoid 58690111Simpevalbackcmd(union node *n, struct backcmd *result) 58717987Speter{ 5881556Srgrimes int pip[2]; 5891556Srgrimes struct job *jp; 5901556Srgrimes struct stackmark smark; /* unnecessary */ 591216761Sjilles struct jmploc jmploc; 592216761Sjilles struct jmploc *savehandler; 593223024Sjilles struct localvar *savelocalvars; 5941556Srgrimes 5951556Srgrimes setstackmark(&smark); 5961556Srgrimes result->fd = -1; 5971556Srgrimes result->buf = NULL; 5981556Srgrimes result->nleft = 0; 5991556Srgrimes result->jp = NULL; 60017987Speter if (n == NULL) { 60117987Speter exitstatus = 0; 6021556Srgrimes goto out; 60317987Speter } 604216778Sjilles if (is_valid_fast_cmdsubst(n)) { 60517987Speter exitstatus = oexitstatus; 606223024Sjilles savelocalvars = localvars; 607223024Sjilles localvars = NULL; 608223024Sjilles forcelocal++; 609216761Sjilles savehandler = handler; 610216761Sjilles if (setjmp(jmploc.loc)) { 611216761Sjilles if (exception == EXERROR || exception == EXEXEC) 612216761Sjilles exitstatus = 2; 613216761Sjilles else if (exception != 0) { 614216761Sjilles handler = savehandler; 615223024Sjilles forcelocal--; 616223024Sjilles poplocalvars(); 617223024Sjilles localvars = savelocalvars; 618216761Sjilles longjmp(handler->loc, 1); 619216761Sjilles } 620216761Sjilles } else { 621216761Sjilles handler = &jmploc; 622216761Sjilles evalcommand(n, EV_BACKCMD, result); 623216761Sjilles } 624216761Sjilles handler = savehandler; 625223024Sjilles forcelocal--; 626223024Sjilles poplocalvars(); 627223024Sjilles localvars = savelocalvars; 6281556Srgrimes } else { 62917987Speter exitstatus = 0; 6301556Srgrimes if (pipe(pip) < 0) 63153891Scracauer error("Pipe call failed: %s", strerror(errno)); 6321556Srgrimes jp = makejob(n, 1); 6331556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 6341556Srgrimes FORCEINTON; 6351556Srgrimes close(pip[0]); 6361556Srgrimes if (pip[1] != 1) { 637124780Sdes dup2(pip[1], 1); 6381556Srgrimes close(pip[1]); 6391556Srgrimes } 6401556Srgrimes evaltree(n, EV_EXIT); 6411556Srgrimes } 6421556Srgrimes close(pip[1]); 6431556Srgrimes result->fd = pip[0]; 6441556Srgrimes result->jp = jp; 6451556Srgrimes } 6461556Srgrimesout: 6471556Srgrimes popstackmark(&smark); 648109627Stjr TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 6491556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 6501556Srgrimes} 6511556Srgrimes 652216826Sjilles/* 653216826Sjilles * Check if a builtin can safely be executed in the same process, 654216826Sjilles * even though it should be in a subshell (command substitution). 655216826Sjilles * Note that jobid, jobs, times and trap can show information not 656216826Sjilles * available in a child process; this is deliberate. 657216826Sjilles * The arguments should already have been expanded. 658216826Sjilles */ 659216826Sjillesstatic int 660216826Sjillessafe_builtin(int idx, int argc, char **argv) 661216826Sjilles{ 662216826Sjilles if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD || 663216826Sjilles idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD || 664216826Sjilles idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD || 665216826Sjilles idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD || 666216826Sjilles idx == TYPECMD) 667216826Sjilles return (1); 668216826Sjilles if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD || 669216826Sjilles idx == UMASKCMD) 670216826Sjilles return (argc <= 1 || (argc == 2 && argv[1][0] == '-')); 671216826Sjilles if (idx == SETCMD) 672216826Sjilles return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' || 673216826Sjilles argv[1][0] == '+') && argv[1][1] == 'o' && 674216826Sjilles argv[1][2] == '\0')); 675216826Sjilles return (0); 676216826Sjilles} 6771556Srgrimes 6781556Srgrimes/* 6791556Srgrimes * Execute a simple command. 680217035Sjilles * Note: This may or may not return if (flags & EV_EXIT). 6811556Srgrimes */ 6821556Srgrimes 683213811Sobrienstatic void 68490111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd) 68517987Speter{ 6861556Srgrimes struct stackmark smark; 6871556Srgrimes union node *argp; 6881556Srgrimes struct arglist arglist; 6891556Srgrimes struct arglist varlist; 6901556Srgrimes char **argv; 6911556Srgrimes int argc; 6921556Srgrimes char **envp; 6931556Srgrimes int varflag; 6941556Srgrimes struct strlist *sp; 6951556Srgrimes int mode; 6961556Srgrimes int pip[2]; 6971556Srgrimes struct cmdentry cmdentry; 6981556Srgrimes struct job *jp; 6991556Srgrimes struct jmploc jmploc; 700194765Sjilles struct jmploc *savehandler; 701194765Sjilles char *savecmdname; 702194765Sjilles struct shparam saveparam; 703194765Sjilles struct localvar *savelocalvars; 704199647Sjilles struct parsefile *savetopfile; 7051556Srgrimes volatile int e; 7061556Srgrimes char *lastarg; 70745916Scracauer int realstatus; 70854884Scracauer int do_clearcmdentry; 709211287Sjilles const char *path = pathval(); 7101556Srgrimes 7111556Srgrimes /* First expand the arguments. */ 712149802Sstefanf TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 7131556Srgrimes setstackmark(&smark); 7141556Srgrimes arglist.lastp = &arglist.list; 7151556Srgrimes varlist.lastp = &varlist.list; 7161556Srgrimes varflag = 1; 717217035Sjilles jp = NULL; 71854884Scracauer do_clearcmdentry = 0; 71917987Speter oexitstatus = exitstatus; 72017987Speter exitstatus = 0; 7211556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 722222165Sjilles if (varflag && isassignment(argp->narg.text)) { 723222165Sjilles expandarg(argp, &varlist, EXP_VARTILDE); 724222165Sjilles continue; 7251556Srgrimes } 7261556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 7271556Srgrimes varflag = 0; 7281556Srgrimes } 7291556Srgrimes *arglist.lastp = NULL; 7301556Srgrimes *varlist.lastp = NULL; 7311556Srgrimes expredir(cmd->ncmd.redirect); 7321556Srgrimes argc = 0; 7331556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 7341556Srgrimes argc++; 735218306Sjilles /* Add one slot at the beginning for tryexec(). */ 736218306Sjilles argv = stalloc(sizeof (char *) * (argc + 2)); 737218306Sjilles argv++; 7381556Srgrimes 7391556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 7401556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 7411556Srgrimes *argv++ = sp->text; 7421556Srgrimes } 7431556Srgrimes *argv = NULL; 7441556Srgrimes lastarg = NULL; 7451556Srgrimes if (iflag && funcnest == 0 && argc > 0) 7461556Srgrimes lastarg = argv[-1]; 7471556Srgrimes argv -= argc; 7481556Srgrimes 7491556Srgrimes /* Print the command if xflag is set. */ 7501556Srgrimes if (xflag) { 751159632Sstefanf char sep = 0; 752222907Sjilles const char *p, *ps4; 753222907Sjilles ps4 = expandstr(ps4val()); 754222907Sjilles out2str(ps4 != NULL ? ps4 : ps4val()); 7551556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 756159632Sstefanf if (sep != 0) 757201366Sjilles out2c(' '); 758215567Sjilles p = strchr(sp->text, '='); 759215567Sjilles if (p != NULL) { 760215567Sjilles p++; 761215567Sjilles outbin(sp->text, p - sp->text, out2); 762194786Sjilles out2qstr(p); 763215567Sjilles } else 764215567Sjilles out2qstr(sp->text); 765159632Sstefanf sep = ' '; 7661556Srgrimes } 7671556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 768159632Sstefanf if (sep != 0) 769201366Sjilles out2c(' '); 770194786Sjilles /* Disambiguate command looking like assignment. */ 771194786Sjilles if (sp == arglist.list && 772194786Sjilles strchr(sp->text, '=') != NULL && 773194786Sjilles strchr(sp->text, '\'') == NULL) { 774194786Sjilles out2c('\''); 775194786Sjilles out2str(sp->text); 776194786Sjilles out2c('\''); 777194786Sjilles } else 778194786Sjilles out2qstr(sp->text); 779159632Sstefanf sep = ' '; 7801556Srgrimes } 781201366Sjilles out2c('\n'); 7821556Srgrimes flushout(&errout); 7831556Srgrimes } 7841556Srgrimes 7851556Srgrimes /* Now locate the command. */ 7861556Srgrimes if (argc == 0) { 787157601Sstefanf /* Variable assignment(s) without command */ 7881556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 7891556Srgrimes cmdentry.u.index = BLTINCMD; 790205138Sjilles cmdentry.special = 0; 7911556Srgrimes } else { 79217987Speter static const char PATH[] = "PATH="; 793204800Sjilles int cmd_flags = 0, bltinonly = 0; 79417987Speter 79517987Speter /* 79617987Speter * Modify the command lookup path, if a PATH= assignment 79717987Speter * is present 79817987Speter */ 79917987Speter for (sp = varlist.list ; sp ; sp = sp->next) 80054884Scracauer if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 80117987Speter path = sp->text + sizeof(PATH) - 1; 802155301Sschweikh /* 80354884Scracauer * On `PATH=... command`, we need to make 80454884Scracauer * sure that the command isn't using the 80554884Scracauer * non-updated hash table of the outer PATH 806155301Sschweikh * setting and we need to make sure that 80754884Scracauer * the hash table isn't filled with items 80854884Scracauer * from the temporary setting. 80954884Scracauer * 810155301Sschweikh * It would be better to forbit using and 81154884Scracauer * updating the table while this command 81254884Scracauer * runs, by the command finding mechanism 81354884Scracauer * is heavily integrated with hash handling, 81454884Scracauer * so we just delete the hash before and after 81554884Scracauer * the command runs. Partly deleting like 81654884Scracauer * changepatch() does doesn't seem worth the 81754884Scracauer * bookinging effort, since most such runs add 818123996Smaxim * directories in front of the new PATH. 81954884Scracauer */ 820218324Sjilles clearcmdentry(); 82154884Scracauer do_clearcmdentry = 1; 82254884Scracauer } 82317987Speter 824204800Sjilles for (;;) { 825204800Sjilles if (bltinonly) { 826204800Sjilles cmdentry.u.index = find_builtin(*argv, &cmdentry.special); 827204800Sjilles if (cmdentry.u.index < 0) { 828201431Sjilles cmdentry.u.index = BLTINCMD; 829201431Sjilles argv--; 830201431Sjilles argc++; 831201431Sjilles break; 8321556Srgrimes } 833204800Sjilles } else 834204800Sjilles find_command(argv[0], &cmdentry, cmd_flags, path); 835204800Sjilles /* implement the bltin and command builtins here */ 836204800Sjilles if (cmdentry.cmdtype != CMDBUILTIN) 837204800Sjilles break; 838204800Sjilles if (cmdentry.u.index == BLTINCMD) { 839204800Sjilles if (argc == 1) 8401556Srgrimes break; 841204800Sjilles argv++; 842204800Sjilles argc--; 843204800Sjilles bltinonly = 1; 844204800Sjilles } else if (cmdentry.u.index == COMMANDCMD) { 845204800Sjilles if (argc == 1) 846204800Sjilles break; 847204800Sjilles if (!strcmp(argv[1], "-p")) { 848204800Sjilles if (argc == 2) 849204800Sjilles break; 850204800Sjilles if (argv[2][0] == '-') { 851204800Sjilles if (strcmp(argv[2], "--")) 852204800Sjilles break; 853204800Sjilles if (argc == 3) 854204800Sjilles break; 855204800Sjilles argv += 3; 856204800Sjilles argc -= 3; 857204800Sjilles } else { 858204800Sjilles argv += 2; 859204800Sjilles argc -= 2; 860204800Sjilles } 861204800Sjilles path = _PATH_STDPATH; 862218324Sjilles clearcmdentry(); 863204800Sjilles do_clearcmdentry = 1; 864204800Sjilles } else if (!strcmp(argv[1], "--")) { 865204800Sjilles if (argc == 2) 866204800Sjilles break; 867204800Sjilles argv += 2; 868204800Sjilles argc -= 2; 869204800Sjilles } else if (argv[1][0] == '-') 870204800Sjilles break; 871204800Sjilles else { 872204800Sjilles argv++; 873204800Sjilles argc--; 874204800Sjilles } 875204800Sjilles cmd_flags |= DO_NOFUNC; 876204800Sjilles bltinonly = 0; 877204800Sjilles } else 878204800Sjilles break; 8791556Srgrimes } 880204800Sjilles /* 881204800Sjilles * Special builtins lose their special properties when 882204800Sjilles * called via 'command'. 883204800Sjilles */ 884204800Sjilles if (cmd_flags & DO_NOFUNC) 885204800Sjilles cmdentry.special = 0; 8861556Srgrimes } 8871556Srgrimes 8881556Srgrimes /* Fork off a child process if necessary. */ 8891556Srgrimes if (cmd->ncmd.backgnd 890197820Sjilles || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 891194127Sjilles && ((flags & EV_EXIT) == 0 || have_traps())) 89217987Speter || ((flags & EV_BACKCMD) != 0 893216826Sjilles && (cmdentry.cmdtype != CMDBUILTIN || 894216826Sjilles !safe_builtin(cmdentry.u.index, argc, argv)))) { 8951556Srgrimes jp = makejob(cmd, 1); 8961556Srgrimes mode = cmd->ncmd.backgnd; 8971556Srgrimes if (flags & EV_BACKCMD) { 8981556Srgrimes mode = FORK_NOJOB; 8991556Srgrimes if (pipe(pip) < 0) 90053891Scracauer error("Pipe call failed: %s", strerror(errno)); 9011556Srgrimes } 9021556Srgrimes if (forkshell(jp, cmd, mode) != 0) 9031556Srgrimes goto parent; /* at end of routine */ 9041556Srgrimes if (flags & EV_BACKCMD) { 9051556Srgrimes FORCEINTON; 9061556Srgrimes close(pip[0]); 9071556Srgrimes if (pip[1] != 1) { 908124780Sdes dup2(pip[1], 1); 9091556Srgrimes close(pip[1]); 9101556Srgrimes } 911223163Sjilles flags &= ~EV_BACKCMD; 9121556Srgrimes } 9131556Srgrimes flags |= EV_EXIT; 9141556Srgrimes } 9151556Srgrimes 9161556Srgrimes /* This is the child process if a fork occurred. */ 9171556Srgrimes /* Execute the command. */ 9181556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 91920425Ssteve#ifdef DEBUG 9201556Srgrimes trputs("Shell function: "); trargs(argv); 92120425Ssteve#endif 9221556Srgrimes saveparam = shellparam; 9231556Srgrimes shellparam.malloc = 0; 92420425Ssteve shellparam.reset = 1; 9251556Srgrimes shellparam.nparam = argc - 1; 9261556Srgrimes shellparam.p = argv + 1; 9271556Srgrimes shellparam.optnext = NULL; 9281556Srgrimes INTOFF; 9291556Srgrimes savelocalvars = localvars; 9301556Srgrimes localvars = NULL; 931196483Sjilles reffunc(cmdentry.u.func); 932194765Sjilles savehandler = handler; 9331556Srgrimes if (setjmp(jmploc.loc)) { 934218306Sjilles freeparam(&shellparam); 935218306Sjilles shellparam = saveparam; 936220978Sjilles popredir(); 937196483Sjilles unreffunc(cmdentry.u.func); 9381556Srgrimes poplocalvars(); 9391556Srgrimes localvars = savelocalvars; 940201283Sjilles funcnest--; 9411556Srgrimes handler = savehandler; 9421556Srgrimes longjmp(handler->loc, 1); 9431556Srgrimes } 9441556Srgrimes handler = &jmploc; 945201283Sjilles funcnest++; 946204802Sjilles redirect(cmd->ncmd.redirect, REDIR_PUSH); 947199660Sjilles INTON; 9481556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 9491556Srgrimes mklocal(sp->text); 950185231Sstefanf exitstatus = oexitstatus; 951220978Sjilles evaltree(getfuncnode(cmdentry.u.func), 952220978Sjilles flags & (EV_TESTED | EV_EXIT)); 9531556Srgrimes INTOFF; 954196483Sjilles unreffunc(cmdentry.u.func); 9551556Srgrimes poplocalvars(); 9561556Srgrimes localvars = savelocalvars; 9571556Srgrimes freeparam(&shellparam); 9581556Srgrimes shellparam = saveparam; 9591556Srgrimes handler = savehandler; 960201283Sjilles funcnest--; 9611556Srgrimes popredir(); 9621556Srgrimes INTON; 9631556Srgrimes if (evalskip == SKIPFUNC) { 9641556Srgrimes evalskip = 0; 9651556Srgrimes skipcount = 0; 9661556Srgrimes } 967217035Sjilles if (jp) 9681556Srgrimes exitshell(exitstatus); 9691556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 97020425Ssteve#ifdef DEBUG 9711556Srgrimes trputs("builtin command: "); trargs(argv); 97220425Ssteve#endif 9731556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 9741556Srgrimes if (flags == EV_BACKCMD) { 9751556Srgrimes memout.nleft = 0; 9761556Srgrimes memout.nextc = memout.buf; 9771556Srgrimes memout.bufsize = 64; 9781556Srgrimes mode |= REDIR_BACKQ; 979201366Sjilles cmdentry.special = 0; 9801556Srgrimes } 9811556Srgrimes savecmdname = commandname; 982199647Sjilles savetopfile = getcurrentfile(); 9831556Srgrimes cmdenviron = varlist.list; 9841556Srgrimes e = -1; 985194765Sjilles savehandler = handler; 9861556Srgrimes if (setjmp(jmploc.loc)) { 9871556Srgrimes e = exception; 988220978Sjilles if (e == EXINT) 989220978Sjilles exitstatus = SIGINT+128; 990220978Sjilles else if (e != EXEXIT) 991220978Sjilles exitstatus = 2; 9921556Srgrimes goto cmddone; 9931556Srgrimes } 9941556Srgrimes handler = &jmploc; 995157601Sstefanf redirect(cmd->ncmd.redirect, mode); 996205138Sjilles /* 997205138Sjilles * If there is no command word, redirection errors should 998205138Sjilles * not be fatal but assignment errors should. 999205138Sjilles */ 1000205138Sjilles if (argc == 0 && !(flags & EV_BACKCMD)) 1001205138Sjilles cmdentry.special = 1; 1002216870Sjilles listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET); 1003207678Sjilles if (argc > 0) 1004207678Sjilles bltinsetlocale(); 10051556Srgrimes commandname = argv[0]; 10061556Srgrimes argptr = argv + 1; 1007201053Sjilles nextopt_optptr = NULL; /* initialize nextopt */ 1008193169Sstefanf builtin_flags = flags; 10091556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 10101556Srgrimes flushall(); 10111556Srgrimescmddone: 1012207678Sjilles if (argc > 0) 1013207678Sjilles bltinunsetlocale(); 101460592Scracauer cmdenviron = NULL; 10151556Srgrimes out1 = &output; 10161556Srgrimes out2 = &errout; 10171556Srgrimes freestdout(); 1018217035Sjilles handler = savehandler; 1019218306Sjilles commandname = savecmdname; 1020218306Sjilles if (jp) 1021218306Sjilles exitshell(exitstatus); 1022201366Sjilles if (flags == EV_BACKCMD) { 1023201366Sjilles backcmd->buf = memout.buf; 1024201366Sjilles backcmd->nleft = memout.nextc - memout.buf; 1025201366Sjilles memout.buf = NULL; 1026201366Sjilles } 1027220978Sjilles if (cmdentry.u.index != EXECCMD) 1028204801Sjilles popredir(); 10291556Srgrimes if (e != -1) { 103020425Ssteve if ((e != EXERROR && e != EXEXEC) 1031157601Sstefanf || cmdentry.special) 10321556Srgrimes exraise(e); 1033199647Sjilles popfilesupto(savetopfile); 1034201366Sjilles if (flags != EV_BACKCMD) 1035201366Sjilles FORCEINTON; 10361556Srgrimes } 10371556Srgrimes } else { 103820425Ssteve#ifdef DEBUG 10391556Srgrimes trputs("normal command: "); trargs(argv); 104020425Ssteve#endif 10411556Srgrimes redirect(cmd->ncmd.redirect, 0); 10421556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 10431556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 10441556Srgrimes envp = environment(); 1045204800Sjilles shellexec(argv, envp, path, cmdentry.u.index); 10461556Srgrimes /*NOTREACHED*/ 10471556Srgrimes } 10481556Srgrimes goto out; 10491556Srgrimes 10501556Srgrimesparent: /* parent process gets here (if we forked) */ 1051212214Sjilles if (mode == FORK_FG) { /* argument to fork */ 10521556Srgrimes INTOFF; 105345916Scracauer exitstatus = waitforjob(jp, &realstatus); 10541556Srgrimes INTON; 105545916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 105645266Scracauer evalskip = SKIPBREAK; 105745266Scracauer skipcount = loopnest; 105845266Scracauer } 1059212214Sjilles } else if (mode == FORK_NOJOB) { 10601556Srgrimes backcmd->fd = pip[0]; 10611556Srgrimes close(pip[1]); 10621556Srgrimes backcmd->jp = jp; 1063221027Sjilles } else 1064221027Sjilles exitstatus = 0; 10651556Srgrimes 10661556Srgrimesout: 10671556Srgrimes if (lastarg) 10681556Srgrimes setvar("_", lastarg, 0); 106954884Scracauer if (do_clearcmdentry) 1070218324Sjilles clearcmdentry(); 10711556Srgrimes popstackmark(&smark); 10721556Srgrimes} 10731556Srgrimes 10741556Srgrimes 10751556Srgrimes 10761556Srgrimes/* 10771556Srgrimes * Search for a command. This is called before we fork so that the 10781556Srgrimes * location of the command will be available in the parent as well as 10791556Srgrimes * the child. The check for "goodname" is an overly conservative 10801556Srgrimes * check that the name will not be subject to expansion. 10811556Srgrimes */ 10821556Srgrimes 1083213811Sobrienstatic void 108490111Simpprehash(union node *n) 108517987Speter{ 10861556Srgrimes struct cmdentry entry; 10871556Srgrimes 1088159633Sstefanf if (n && n->type == NCMD && n->ncmd.args) 108917987Speter if (goodname(n->ncmd.args->narg.text)) 109017987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 109117987Speter pathval()); 10921556Srgrimes} 10931556Srgrimes 10941556Srgrimes 10951556Srgrimes 10961556Srgrimes/* 10971556Srgrimes * Builtin commands. Builtin commands whose functions are closely 10981556Srgrimes * tied to evaluation are implemented here. 10991556Srgrimes */ 11001556Srgrimes 11011556Srgrimes/* 1102201431Sjilles * No command given, a bltin command with no arguments, or a bltin command 1103201431Sjilles * with an invalid name. 11041556Srgrimes */ 11051556Srgrimes 110617987Speterint 1107201431Sjillesbltincmd(int argc, char **argv) 110817987Speter{ 1109201431Sjilles if (argc > 1) { 1110201431Sjilles out2fmt_flush("%s: not found\n", argv[1]); 1111201431Sjilles return 127; 1112201431Sjilles } 111320425Ssteve /* 111417987Speter * Preserve exitstatus of a previous possible redirection 111520425Ssteve * as POSIX mandates 111617987Speter */ 11171556Srgrimes return exitstatus; 11181556Srgrimes} 11191556Srgrimes 11201556Srgrimes 11211556Srgrimes/* 11221556Srgrimes * Handle break and continue commands. Break, continue, and return are 11231556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 11241556Srgrimes * above all check this flag, and if it is set they start skipping 11251556Srgrimes * commands rather than executing them. The variable skipcount is 11261556Srgrimes * the number of loops to break/continue, or the number of function 11271556Srgrimes * levels to return. (The latter is always 1.) It should probably 11281556Srgrimes * be an error to break out of more loops than exist, but it isn't 11291556Srgrimes * in the standard shell so we don't make it one here. 11301556Srgrimes */ 11311556Srgrimes 113217987Speterint 113390111Simpbreakcmd(int argc, char **argv) 113417987Speter{ 113520425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 11361556Srgrimes 11371556Srgrimes if (n > loopnest) 11381556Srgrimes n = loopnest; 11391556Srgrimes if (n > 0) { 11401556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 11411556Srgrimes skipcount = n; 11421556Srgrimes } 11431556Srgrimes return 0; 11441556Srgrimes} 11451556Srgrimes 1146100437Stjr/* 1147100437Stjr * The `command' command. 1148100437Stjr */ 1149100437Stjrint 1150100437Stjrcommandcmd(int argc, char **argv) 1151100437Stjr{ 1152207783Sjilles const char *path; 1153100437Stjr int ch; 1154151810Sstefanf int cmd = -1; 11551556Srgrimes 1156204800Sjilles path = bltinlookup("PATH", 1); 1157100437Stjr 1158100437Stjr optind = optreset = 1; 1159100663Stjr opterr = 0; 1160151810Sstefanf while ((ch = getopt(argc, argv, "pvV")) != -1) { 1161100437Stjr switch (ch) { 1162100437Stjr case 'p': 1163207783Sjilles path = _PATH_STDPATH; 1164100437Stjr break; 1165151810Sstefanf case 'v': 1166151810Sstefanf cmd = TYPECMD_SMALLV; 1167151810Sstefanf break; 1168151810Sstefanf case 'V': 1169151810Sstefanf cmd = TYPECMD_BIGV; 1170151810Sstefanf break; 1171100437Stjr case '?': 1172100437Stjr default: 1173100437Stjr error("unknown option: -%c", optopt); 1174100437Stjr } 1175100437Stjr } 1176100437Stjr argc -= optind; 1177100437Stjr argv += optind; 1178100437Stjr 1179151810Sstefanf if (cmd != -1) { 1180151810Sstefanf if (argc != 1) 1181151810Sstefanf error("wrong number of arguments"); 1182201343Sjilles return typecmd_impl(2, argv - 1, cmd, path); 1183151810Sstefanf } 1184204800Sjilles if (argc != 0) 1185214538Sjilles error("commandcmd bad call"); 1186100437Stjr 1187100437Stjr /* 1188100437Stjr * Do nothing successfully if no command was specified; 1189100437Stjr * ksh also does this. 1190100437Stjr */ 1191204800Sjilles return 0; 1192100437Stjr} 1193100437Stjr 1194100437Stjr 11951556Srgrimes/* 11961556Srgrimes * The return command. 11971556Srgrimes */ 11981556Srgrimes 119917987Speterint 120090111Simpreturncmd(int argc, char **argv) 120117987Speter{ 120220425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 12031556Srgrimes 12041556Srgrimes if (funcnest) { 12051556Srgrimes evalskip = SKIPFUNC; 12061556Srgrimes skipcount = 1; 120720425Ssteve } else { 120820425Ssteve /* skip the rest of the file */ 120920425Ssteve evalskip = SKIPFILE; 121020425Ssteve skipcount = 1; 12111556Srgrimes } 12121556Srgrimes return ret; 12131556Srgrimes} 12141556Srgrimes 12151556Srgrimes 121617987Speterint 121790111Simpfalsecmd(int argc __unused, char **argv __unused) 121817987Speter{ 121917987Speter return 1; 122017987Speter} 122117987Speter 122217987Speter 122317987Speterint 122490111Simptruecmd(int argc __unused, char **argv __unused) 122517987Speter{ 12261556Srgrimes return 0; 12271556Srgrimes} 12281556Srgrimes 12291556Srgrimes 123017987Speterint 123190111Simpexeccmd(int argc, char **argv) 123217987Speter{ 1233208630Sjilles /* 1234208630Sjilles * Because we have historically not supported any options, 1235208630Sjilles * only treat "--" specially. 1236208630Sjilles */ 1237208630Sjilles if (argc > 1 && strcmp(argv[1], "--") == 0) 1238208630Sjilles argc--, argv++; 12391556Srgrimes if (argc > 1) { 124017987Speter struct strlist *sp; 124117987Speter 12421556Srgrimes iflag = 0; /* exit on error */ 12431556Srgrimes mflag = 0; 12441556Srgrimes optschanged(); 124517987Speter for (sp = cmdenviron; sp ; sp = sp->next) 124617987Speter setvareq(sp->text, VEXPORT|VSTACK); 12471556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 12481556Srgrimes 12491556Srgrimes } 12501556Srgrimes return 0; 12511556Srgrimes} 1252153091Sstefanf 1253153091Sstefanf 1254153091Sstefanfint 1255153091Sstefanftimescmd(int argc __unused, char **argv __unused) 1256153091Sstefanf{ 1257153091Sstefanf struct rusage ru; 1258153091Sstefanf long shumins, shsmins, chumins, chsmins; 1259153091Sstefanf double shusecs, shssecs, chusecs, chssecs; 1260153091Sstefanf 1261153091Sstefanf if (getrusage(RUSAGE_SELF, &ru) < 0) 1262153091Sstefanf return 1; 1263153091Sstefanf shumins = ru.ru_utime.tv_sec / 60; 1264153091Sstefanf shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1265153091Sstefanf shsmins = ru.ru_stime.tv_sec / 60; 1266153091Sstefanf shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1267153091Sstefanf if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1268153091Sstefanf return 1; 1269153091Sstefanf chumins = ru.ru_utime.tv_sec / 60; 1270153091Sstefanf chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1271153091Sstefanf chsmins = ru.ru_stime.tv_sec / 60; 1272153091Sstefanf chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1273153091Sstefanf out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1274153091Sstefanf shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1275153091Sstefanf return 0; 1276153091Sstefanf} 1277