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$"); 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 */ 78230212Sdumbbellint skipcount; /* number of levels to skip */ 79253658Sjillesstatic 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); 92230161Sjillesstatic union node *evalcase(union node *); 93213811Sobrienstatic void evalsubshell(union node *, int); 94213811Sobrienstatic void evalredir(union node *, int); 95246288Sjillesstatic void exphere(union node *, struct arglist *); 96213811Sobrienstatic void expredir(union node *); 97213811Sobrienstatic void evalpipe(union node *); 98216778Sjillesstatic int is_valid_fast_cmdsubst(union node *n); 99213811Sobrienstatic void evalcommand(union node *, int, struct backcmd *); 100213811Sobrienstatic void prehash(union node *); 1011556Srgrimes 1021556Srgrimes 1031556Srgrimes/* 1041556Srgrimes * Called to reset things after an exception. 1051556Srgrimes */ 1061556Srgrimes 107253650Sjillesvoid 108253650Sjillesreseteval(void) 109253650Sjilles{ 1101556Srgrimes evalskip = 0; 1111556Srgrimes loopnest = 0; 1121556Srgrimes} 1131556Srgrimes 1141556Srgrimes 1151556Srgrimes/* 11646684Skris * The eval command. 1171556Srgrimes */ 1181556Srgrimes 11917987Speterint 12090111Simpevalcmd(int argc, char **argv) 1211556Srgrimes{ 1221556Srgrimes char *p; 1231556Srgrimes char *concat; 1241556Srgrimes char **ap; 1251556Srgrimes 1261556Srgrimes if (argc > 1) { 1271556Srgrimes p = argv[1]; 1281556Srgrimes if (argc > 2) { 1291556Srgrimes STARTSTACKSTR(concat); 1301556Srgrimes ap = argv + 2; 1311556Srgrimes for (;;) { 132215783Sjilles STPUTS(p, concat); 1331556Srgrimes if ((p = *ap++) == NULL) 1341556Srgrimes break; 1351556Srgrimes STPUTC(' ', concat); 1361556Srgrimes } 1371556Srgrimes STPUTC('\0', concat); 1381556Srgrimes p = grabstackstr(concat); 1391556Srgrimes } 140223163Sjilles evalstring(p, builtin_flags); 141210829Sjilles } else 142210829Sjilles exitstatus = 0; 1431556Srgrimes return exitstatus; 1441556Srgrimes} 1451556Srgrimes 1461556Srgrimes 1471556Srgrimes/* 1481556Srgrimes * Execute a command or commands contained in a string. 1491556Srgrimes */ 1501556Srgrimes 1511556Srgrimesvoid 152193169Sstefanfevalstring(char *s, int flags) 15390111Simp{ 1541556Srgrimes union node *n; 1551556Srgrimes struct stackmark smark; 156194128Sjilles int flags_exit; 157210829Sjilles int any; 1581556Srgrimes 159194128Sjilles flags_exit = flags & EV_EXIT; 160194128Sjilles flags &= ~EV_EXIT; 161210829Sjilles any = 0; 1621556Srgrimes setstackmark(&smark); 1631556Srgrimes setinputstring(s, 1); 1641556Srgrimes while ((n = parsecmd(0)) != NEOF) { 165222676Sjilles if (n != NULL && !nflag) { 166194128Sjilles if (flags_exit && preadateof()) 167194128Sjilles evaltree(n, flags | EV_EXIT); 168194128Sjilles else 169194128Sjilles evaltree(n, flags); 170210829Sjilles any = 1; 171194128Sjilles } 1721556Srgrimes popstackmark(&smark); 173247013Sjilles setstackmark(&smark); 1741556Srgrimes } 1751556Srgrimes popfile(); 1761556Srgrimes popstackmark(&smark); 177210829Sjilles if (!any) 178210829Sjilles exitstatus = 0; 179194128Sjilles if (flags_exit) 180220978Sjilles exraise(EXEXIT); 1811556Srgrimes} 1821556Srgrimes 1831556Srgrimes 1841556Srgrimes/* 1851556Srgrimes * Evaluate a parse tree. The value is left in the global variable 1861556Srgrimes * exitstatus. 1871556Srgrimes */ 1881556Srgrimes 1891556Srgrimesvoid 19090111Simpevaltree(union node *n, int flags) 19117987Speter{ 192149927Sstefanf int do_etest; 193214599Sjilles union node *next; 194245698Sjilles struct stackmark smark; 195149927Sstefanf 196245698Sjilles setstackmark(&smark); 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: 259230161Sjilles next = evalcase(n); 260214600Sjilles break; 261230161Sjilles case NCLIST: 262230161Sjilles next = n->nclist.body; 263230161Sjilles break; 264230161Sjilles case NCLISTFALLTHRU: 265230161Sjilles if (n->nclist.body) { 266230161Sjilles evaltree(n->nclist.body, flags & ~EV_EXIT); 267230161Sjilles if (evalskip) 268230161Sjilles goto out; 269230161Sjilles } 270230161Sjilles next = n->nclist.next; 271230161Sjilles break; 272214600Sjilles case NDEFUN: 273214600Sjilles defun(n->narg.text, n->narg.next); 27420425Ssteve exitstatus = 0; 275214600Sjilles break; 276214600Sjilles case NNOT: 277214600Sjilles evaltree(n->nnot.com, EV_TESTED); 278249407Sjilles if (evalskip) 279249407Sjilles goto out; 280214600Sjilles exitstatus = !exitstatus; 281214600Sjilles break; 2821556Srgrimes 283214600Sjilles case NPIPE: 284214600Sjilles evalpipe(n); 285214600Sjilles do_etest = !(flags & EV_TESTED); 286214600Sjilles break; 287214600Sjilles case NCMD: 288214600Sjilles evalcommand(n, flags, (struct backcmd *)NULL); 289214600Sjilles do_etest = !(flags & EV_TESTED); 290214600Sjilles break; 291214600Sjilles default: 292214600Sjilles out1fmt("Node type = %d\n", n->type); 293214600Sjilles flushout(&output); 294214600Sjilles break; 295214600Sjilles } 296214600Sjilles n = next; 297245698Sjilles popstackmark(&smark); 298247013Sjilles setstackmark(&smark); 299214599Sjilles } while (n != NULL); 3001556Srgrimesout: 301245698Sjilles popstackmark(&smark); 302247206Sjilles if (pendingsig) 3031556Srgrimes dotrap(); 304220978Sjilles if (eflag && exitstatus != 0 && do_etest) 3051556Srgrimes exitshell(exitstatus); 306220978Sjilles if (flags & EV_EXIT) 307220978Sjilles exraise(EXEXIT); 3081556Srgrimes} 3091556Srgrimes 3101556Srgrimes 311213811Sobrienstatic void 312149933Sstefanfevalloop(union node *n, int flags) 31317987Speter{ 3141556Srgrimes int status; 3151556Srgrimes 3161556Srgrimes loopnest++; 3171556Srgrimes status = 0; 3181556Srgrimes for (;;) { 319287750Sjilles if (!evalskip) 320287750Sjilles evaltree(n->nbinary.ch1, EV_TESTED); 3211556Srgrimes if (evalskip) { 322287750Sjilles if (evalskip == SKIPCONT && --skipcount <= 0) { 3231556Srgrimes evalskip = 0; 3241556Srgrimes continue; 3251556Srgrimes } 3261556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3271556Srgrimes evalskip = 0; 328255215Sjilles if (evalskip == SKIPRETURN) 329212475Sjilles status = exitstatus; 3301556Srgrimes break; 3311556Srgrimes } 3321556Srgrimes if (n->type == NWHILE) { 3331556Srgrimes if (exitstatus != 0) 3341556Srgrimes break; 3351556Srgrimes } else { 3361556Srgrimes if (exitstatus == 0) 3371556Srgrimes break; 3381556Srgrimes } 339149933Sstefanf evaltree(n->nbinary.ch2, flags); 3401556Srgrimes status = exitstatus; 3411556Srgrimes } 3421556Srgrimes loopnest--; 3431556Srgrimes exitstatus = status; 3441556Srgrimes} 3451556Srgrimes 3461556Srgrimes 3471556Srgrimes 348213811Sobrienstatic void 349149933Sstefanfevalfor(union node *n, int flags) 35017987Speter{ 3511556Srgrimes struct arglist arglist; 3521556Srgrimes union node *argp; 3531556Srgrimes struct strlist *sp; 354230463Sjilles int status; 3551556Srgrimes 3561556Srgrimes arglist.lastp = &arglist.list; 3571556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 35817987Speter oexitstatus = exitstatus; 3591556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3601556Srgrimes } 3611556Srgrimes *arglist.lastp = NULL; 3621556Srgrimes 3631556Srgrimes loopnest++; 364230463Sjilles status = 0; 3651556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3661556Srgrimes setvar(n->nfor.var, sp->text, 0); 367149933Sstefanf evaltree(n->nfor.body, flags); 368230463Sjilles status = exitstatus; 3691556Srgrimes if (evalskip) { 3701556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3711556Srgrimes evalskip = 0; 3721556Srgrimes continue; 3731556Srgrimes } 3741556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3751556Srgrimes evalskip = 0; 3761556Srgrimes break; 3771556Srgrimes } 3781556Srgrimes } 3791556Srgrimes loopnest--; 380230463Sjilles exitstatus = status; 3811556Srgrimes} 3821556Srgrimes 3831556Srgrimes 384230161Sjilles/* 385230161Sjilles * Evaluate a case statement, returning the selected tree. 386230161Sjilles * 387230161Sjilles * The exit status needs care to get right. 388230161Sjilles */ 3891556Srgrimes 390228013Sjillesstatic union node * 391230161Sjillesevalcase(union node *n) 39217987Speter{ 3931556Srgrimes union node *cp; 3941556Srgrimes union node *patp; 3951556Srgrimes struct arglist arglist; 3961556Srgrimes 3971556Srgrimes arglist.lastp = &arglist.list; 39817987Speter oexitstatus = exitstatus; 3991556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 400228013Sjilles for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) { 4011556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 4021556Srgrimes if (casematch(patp, arglist.list->text)) { 403223186Sjilles while (cp->nclist.next && 404230161Sjilles cp->type == NCLISTFALLTHRU && 405230161Sjilles cp->nclist.body == NULL) 406223186Sjilles cp = cp->nclist.next; 407230161Sjilles if (cp->nclist.next && 408230161Sjilles cp->type == NCLISTFALLTHRU) 409230161Sjilles return (cp); 410230154Sjilles if (cp->nclist.body == NULL) 411230154Sjilles exitstatus = 0; 412228013Sjilles return (cp->nclist.body); 4131556Srgrimes } 4141556Srgrimes } 4151556Srgrimes } 416230154Sjilles exitstatus = 0; 417228013Sjilles return (NULL); 4181556Srgrimes} 4191556Srgrimes 4201556Srgrimes 4211556Srgrimes 4221556Srgrimes/* 4231556Srgrimes * Kick off a subshell to evaluate a tree. 4241556Srgrimes */ 4251556Srgrimes 426213811Sobrienstatic void 42790111Simpevalsubshell(union node *n, int flags) 42817987Speter{ 4291556Srgrimes struct job *jp; 4301556Srgrimes int backgnd = (n->type == NBACKGND); 4311556Srgrimes 432222716Sjilles oexitstatus = exitstatus; 4331556Srgrimes expredir(n->nredir.redirect); 434194774Sjilles if ((!backgnd && flags & EV_EXIT && !have_traps()) || 435194774Sjilles forkshell(jp = makejob(n, 1), n, backgnd) == 0) { 4361556Srgrimes if (backgnd) 4371556Srgrimes flags &=~ EV_TESTED; 4381556Srgrimes redirect(n->nredir.redirect, 0); 4391556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 440201053Sjilles } else if (! backgnd) { 4411556Srgrimes INTOFF; 44245916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4431556Srgrimes INTON; 444221027Sjilles } else 445221027Sjilles exitstatus = 0; 4461556Srgrimes} 4471556Srgrimes 4481556Srgrimes 449205154Sjilles/* 450205154Sjilles * Evaluate a redirected compound command. 451205154Sjilles */ 4521556Srgrimes 453213811Sobrienstatic void 454205154Sjillesevalredir(union node *n, int flags) 455205154Sjilles{ 456205154Sjilles struct jmploc jmploc; 457205154Sjilles struct jmploc *savehandler; 458205154Sjilles volatile int in_redirect = 1; 459205154Sjilles 460222716Sjilles oexitstatus = exitstatus; 461205154Sjilles expredir(n->nredir.redirect); 462205154Sjilles savehandler = handler; 463205154Sjilles if (setjmp(jmploc.loc)) { 464205154Sjilles int e; 465205154Sjilles 466205154Sjilles handler = savehandler; 467205154Sjilles e = exception; 468220978Sjilles popredir(); 469205154Sjilles if (e == EXERROR || e == EXEXEC) { 470205154Sjilles if (in_redirect) { 471205154Sjilles exitstatus = 2; 472205154Sjilles return; 473205154Sjilles } 474205154Sjilles } 475205154Sjilles longjmp(handler->loc, 1); 476205154Sjilles } else { 477205154Sjilles INTOFF; 478205154Sjilles handler = &jmploc; 479205154Sjilles redirect(n->nredir.redirect, REDIR_PUSH); 480205154Sjilles in_redirect = 0; 481205154Sjilles INTON; 482205154Sjilles evaltree(n->nredir.n, flags); 483205154Sjilles } 484205154Sjilles INTOFF; 485205154Sjilles handler = savehandler; 486205154Sjilles popredir(); 487205154Sjilles INTON; 488205154Sjilles} 489205154Sjilles 490205154Sjilles 491246288Sjillesstatic void 492246288Sjillesexphere(union node *redir, struct arglist *fn) 493246288Sjilles{ 494246288Sjilles struct jmploc jmploc; 495246288Sjilles struct jmploc *savehandler; 496246288Sjilles struct localvar *savelocalvars; 497246288Sjilles int need_longjmp = 0; 498246288Sjilles 499246288Sjilles redir->nhere.expdoc = nullstr; 500246288Sjilles savelocalvars = localvars; 501246288Sjilles localvars = NULL; 502246288Sjilles forcelocal++; 503246288Sjilles savehandler = handler; 504246288Sjilles if (setjmp(jmploc.loc)) 505246288Sjilles need_longjmp = exception != EXERROR && exception != EXEXEC; 506246288Sjilles else { 507246288Sjilles handler = &jmploc; 508246288Sjilles expandarg(redir->nhere.doc, fn, 0); 509246288Sjilles redir->nhere.expdoc = fn->list->text; 510246288Sjilles INTOFF; 511246288Sjilles } 512246288Sjilles handler = savehandler; 513246288Sjilles forcelocal--; 514246288Sjilles poplocalvars(); 515246288Sjilles localvars = savelocalvars; 516246288Sjilles if (need_longjmp) 517246288Sjilles longjmp(handler->loc, 1); 518246288Sjilles INTON; 519246288Sjilles} 520246288Sjilles 521246288Sjilles 5221556Srgrimes/* 5231556Srgrimes * Compute the names of the files in a redirection list. 5241556Srgrimes */ 5251556Srgrimes 526213811Sobrienstatic void 52790111Simpexpredir(union node *n) 52817987Speter{ 52925222Ssteve union node *redir; 5301556Srgrimes 5311556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 53217987Speter struct arglist fn; 53317987Speter fn.lastp = &fn.list; 53417987Speter switch (redir->type) { 53517987Speter case NFROM: 53617987Speter case NTO: 53766612Sbrian case NFROMTO: 53817987Speter case NAPPEND: 53996922Stjr case NCLOBBER: 540276365Sjilles expandarg(redir->nfile.fname, &fn, EXP_TILDE); 5411556Srgrimes redir->nfile.expfname = fn.list->text; 54217987Speter break; 54317987Speter case NFROMFD: 54417987Speter case NTOFD: 54517987Speter if (redir->ndup.vname) { 546276365Sjilles expandarg(redir->ndup.vname, &fn, EXP_TILDE); 54717987Speter fixredir(redir, fn.list->text, 1); 54817987Speter } 54917987Speter break; 550246288Sjilles case NXHERE: 551246288Sjilles exphere(redir, &fn); 552246288Sjilles break; 5531556Srgrimes } 5541556Srgrimes } 5551556Srgrimes} 5561556Srgrimes 5571556Srgrimes 5581556Srgrimes 5591556Srgrimes/* 5601556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 5611556Srgrimes * of the process creating the pipeline. (This differs from some versions 5621556Srgrimes * of the shell, which make the last process in a pipeline the parent 5631556Srgrimes * of all the rest.) 5641556Srgrimes */ 5651556Srgrimes 566213811Sobrienstatic void 56790111Simpevalpipe(union node *n) 56817987Speter{ 5691556Srgrimes struct job *jp; 5701556Srgrimes struct nodelist *lp; 5711556Srgrimes int pipelen; 5721556Srgrimes int prevfd; 5731556Srgrimes int pip[2]; 5741556Srgrimes 575149802Sstefanf TRACE(("evalpipe(%p) called\n", (void *)n)); 5761556Srgrimes pipelen = 0; 5771556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 5781556Srgrimes pipelen++; 5791556Srgrimes INTOFF; 5801556Srgrimes jp = makejob(n, pipelen); 5811556Srgrimes prevfd = -1; 5821556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 5831556Srgrimes prehash(lp->n); 5841556Srgrimes pip[1] = -1; 5851556Srgrimes if (lp->next) { 5861556Srgrimes if (pipe(pip) < 0) { 587252359Sjilles if (prevfd >= 0) 588252359Sjilles close(prevfd); 58953891Scracauer error("Pipe call failed: %s", strerror(errno)); 5901556Srgrimes } 5911556Srgrimes } 5921556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 5931556Srgrimes INTON; 5941556Srgrimes if (prevfd > 0) { 595124780Sdes dup2(prevfd, 0); 5961556Srgrimes close(prevfd); 5971556Srgrimes } 5981556Srgrimes if (pip[1] >= 0) { 59953282Scracauer if (!(prevfd >= 0 && pip[0] == 0)) 60052900Scracauer close(pip[0]); 6011556Srgrimes if (pip[1] != 1) { 602124780Sdes dup2(pip[1], 1); 6031556Srgrimes close(pip[1]); 6041556Srgrimes } 6051556Srgrimes } 6061556Srgrimes evaltree(lp->n, EV_EXIT); 6071556Srgrimes } 6081556Srgrimes if (prevfd >= 0) 6091556Srgrimes close(prevfd); 6101556Srgrimes prevfd = pip[0]; 611221970Sjilles if (pip[1] != -1) 612221970Sjilles close(pip[1]); 6131556Srgrimes } 6141556Srgrimes INTON; 6151556Srgrimes if (n->npipe.backgnd == 0) { 6161556Srgrimes INTOFF; 61745916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 6181556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 6191556Srgrimes INTON; 620221027Sjilles } else 621221027Sjilles exitstatus = 0; 6221556Srgrimes} 6231556Srgrimes 6241556Srgrimes 6251556Srgrimes 626216778Sjillesstatic int 627216778Sjillesis_valid_fast_cmdsubst(union node *n) 628216778Sjilles{ 629216778Sjilles 630223024Sjilles return (n->type == NCMD); 631216778Sjilles} 632216778Sjilles 6331556Srgrimes/* 6341556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 6351556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 6361556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 6371556Srgrimes * Should be called with interrupts off. 6381556Srgrimes */ 6391556Srgrimes 6401556Srgrimesvoid 64190111Simpevalbackcmd(union node *n, struct backcmd *result) 64217987Speter{ 6431556Srgrimes int pip[2]; 6441556Srgrimes struct job *jp; 645245698Sjilles struct stackmark smark; 646216761Sjilles struct jmploc jmploc; 647216761Sjilles struct jmploc *savehandler; 648223024Sjilles struct localvar *savelocalvars; 6491556Srgrimes 6501556Srgrimes result->fd = -1; 6511556Srgrimes result->buf = NULL; 6521556Srgrimes result->nleft = 0; 6531556Srgrimes result->jp = NULL; 65417987Speter if (n == NULL) { 65517987Speter exitstatus = 0; 656287750Sjilles return; 65717987Speter } 658287750Sjilles setstackmark(&smark); 659245422Sjilles exitstatus = oexitstatus; 660216778Sjilles if (is_valid_fast_cmdsubst(n)) { 661223024Sjilles savelocalvars = localvars; 662223024Sjilles localvars = NULL; 663223024Sjilles forcelocal++; 664216761Sjilles savehandler = handler; 665216761Sjilles if (setjmp(jmploc.loc)) { 666216761Sjilles if (exception == EXERROR || exception == EXEXEC) 667216761Sjilles exitstatus = 2; 668216761Sjilles else if (exception != 0) { 669216761Sjilles handler = savehandler; 670223024Sjilles forcelocal--; 671223024Sjilles poplocalvars(); 672223024Sjilles localvars = savelocalvars; 673216761Sjilles longjmp(handler->loc, 1); 674216761Sjilles } 675216761Sjilles } else { 676216761Sjilles handler = &jmploc; 677216761Sjilles evalcommand(n, EV_BACKCMD, result); 678216761Sjilles } 679216761Sjilles handler = savehandler; 680223024Sjilles forcelocal--; 681223024Sjilles poplocalvars(); 682223024Sjilles localvars = savelocalvars; 6831556Srgrimes } else { 6841556Srgrimes if (pipe(pip) < 0) 68553891Scracauer error("Pipe call failed: %s", strerror(errno)); 6861556Srgrimes jp = makejob(n, 1); 6871556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 6881556Srgrimes FORCEINTON; 6891556Srgrimes close(pip[0]); 6901556Srgrimes if (pip[1] != 1) { 691124780Sdes dup2(pip[1], 1); 6921556Srgrimes close(pip[1]); 6931556Srgrimes } 6941556Srgrimes evaltree(n, EV_EXIT); 6951556Srgrimes } 6961556Srgrimes close(pip[1]); 6971556Srgrimes result->fd = pip[0]; 6981556Srgrimes result->jp = jp; 6991556Srgrimes } 7001556Srgrimes popstackmark(&smark); 701109627Stjr TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 7021556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 7031556Srgrimes} 7041556Srgrimes 705238468Sjillesstatic int 706238468Sjillesmustexpandto(const char *argtext, const char *mask) 707238468Sjilles{ 708238468Sjilles for (;;) { 709238468Sjilles if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) { 710238468Sjilles argtext++; 711238468Sjilles continue; 712238468Sjilles } 713238468Sjilles if (*argtext == CTLESC) 714238468Sjilles argtext++; 715238468Sjilles else if (BASESYNTAX[(int)*argtext] == CCTL) 716238468Sjilles return (0); 717238468Sjilles if (*argtext != *mask) 718238468Sjilles return (0); 719238468Sjilles if (*argtext == '\0') 720238468Sjilles return (1); 721238468Sjilles argtext++; 722238468Sjilles mask++; 723238468Sjilles } 724238468Sjilles} 725238468Sjilles 726238468Sjillesstatic int 727238468Sjillesisdeclarationcmd(struct narg *arg) 728238468Sjilles{ 729238468Sjilles int have_command = 0; 730238468Sjilles 731238468Sjilles if (arg == NULL) 732238468Sjilles return (0); 733238468Sjilles while (mustexpandto(arg->text, "command")) { 734238468Sjilles have_command = 1; 735238468Sjilles arg = &arg->next->narg; 736238468Sjilles if (arg == NULL) 737238468Sjilles return (0); 738238468Sjilles /* 739238468Sjilles * To also allow "command -p" and "command --" as part of 740238468Sjilles * a declaration command, add code here. 741238468Sjilles * We do not do this, as ksh does not do it either and it 742238468Sjilles * is not required by POSIX. 743238468Sjilles */ 744238468Sjilles } 745238468Sjilles return (mustexpandto(arg->text, "export") || 746238468Sjilles mustexpandto(arg->text, "readonly") || 747238468Sjilles (mustexpandto(arg->text, "local") && 748238468Sjilles (have_command || !isfunc("local")))); 749238468Sjilles} 750238468Sjilles 751262951Sjmmvstatic void 752262951Sjmmvxtracecommand(struct arglist *varlist, struct arglist *arglist) 753262951Sjmmv{ 754262951Sjmmv struct strlist *sp; 755262951Sjmmv char sep = 0; 756262951Sjmmv const char *p, *ps4; 757262951Sjmmv 758262951Sjmmv ps4 = expandstr(ps4val()); 759262951Sjmmv out2str(ps4 != NULL ? ps4 : ps4val()); 760262951Sjmmv for (sp = varlist->list ; sp ; sp = sp->next) { 761262951Sjmmv if (sep != 0) 762262951Sjmmv out2c(' '); 763262951Sjmmv p = strchr(sp->text, '='); 764262951Sjmmv if (p != NULL) { 765262951Sjmmv p++; 766262951Sjmmv outbin(sp->text, p - sp->text, out2); 767262951Sjmmv out2qstr(p); 768262951Sjmmv } else 769262951Sjmmv out2qstr(sp->text); 770262951Sjmmv sep = ' '; 771262951Sjmmv } 772262951Sjmmv for (sp = arglist->list ; sp ; sp = sp->next) { 773262951Sjmmv if (sep != 0) 774262951Sjmmv out2c(' '); 775262951Sjmmv /* Disambiguate command looking like assignment. */ 776262951Sjmmv if (sp == arglist->list && 777262951Sjmmv strchr(sp->text, '=') != NULL && 778262951Sjmmv strchr(sp->text, '\'') == NULL) { 779262951Sjmmv out2c('\''); 780262951Sjmmv out2str(sp->text); 781262951Sjmmv out2c('\''); 782262951Sjmmv } else 783262951Sjmmv out2qstr(sp->text); 784262951Sjmmv sep = ' '; 785262951Sjmmv } 786262951Sjmmv out2c('\n'); 787262951Sjmmv flushout(&errout); 788262951Sjmmv} 789262951Sjmmv 790216826Sjilles/* 791216826Sjilles * Check if a builtin can safely be executed in the same process, 792216826Sjilles * even though it should be in a subshell (command substitution). 793216826Sjilles * Note that jobid, jobs, times and trap can show information not 794216826Sjilles * available in a child process; this is deliberate. 795216826Sjilles * The arguments should already have been expanded. 796216826Sjilles */ 797216826Sjillesstatic int 798216826Sjillessafe_builtin(int idx, int argc, char **argv) 799216826Sjilles{ 800216826Sjilles if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD || 801216826Sjilles idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD || 802216826Sjilles idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD || 803216826Sjilles idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD || 804216826Sjilles idx == TYPECMD) 805216826Sjilles return (1); 806216826Sjilles if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD || 807216826Sjilles idx == UMASKCMD) 808216826Sjilles return (argc <= 1 || (argc == 2 && argv[1][0] == '-')); 809216826Sjilles if (idx == SETCMD) 810216826Sjilles return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' || 811216826Sjilles argv[1][0] == '+') && argv[1][1] == 'o' && 812216826Sjilles argv[1][2] == '\0')); 813216826Sjilles return (0); 814216826Sjilles} 8151556Srgrimes 8161556Srgrimes/* 8171556Srgrimes * Execute a simple command. 818217035Sjilles * Note: This may or may not return if (flags & EV_EXIT). 8191556Srgrimes */ 8201556Srgrimes 821213811Sobrienstatic void 82290111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd) 82317987Speter{ 8241556Srgrimes union node *argp; 8251556Srgrimes struct arglist arglist; 8261556Srgrimes struct arglist varlist; 8271556Srgrimes char **argv; 8281556Srgrimes int argc; 8291556Srgrimes char **envp; 8301556Srgrimes int varflag; 8311556Srgrimes struct strlist *sp; 8321556Srgrimes int mode; 8331556Srgrimes int pip[2]; 8341556Srgrimes struct cmdentry cmdentry; 8351556Srgrimes struct job *jp; 8361556Srgrimes struct jmploc jmploc; 837194765Sjilles struct jmploc *savehandler; 838194765Sjilles char *savecmdname; 839194765Sjilles struct shparam saveparam; 840194765Sjilles struct localvar *savelocalvars; 841199647Sjilles struct parsefile *savetopfile; 8421556Srgrimes volatile int e; 8431556Srgrimes char *lastarg; 84445916Scracauer int realstatus; 84554884Scracauer int do_clearcmdentry; 846211287Sjilles const char *path = pathval(); 8471556Srgrimes 8481556Srgrimes /* First expand the arguments. */ 849149802Sstefanf TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 8501556Srgrimes arglist.lastp = &arglist.list; 8511556Srgrimes varlist.lastp = &varlist.list; 8521556Srgrimes varflag = 1; 853217035Sjilles jp = NULL; 85454884Scracauer do_clearcmdentry = 0; 85517987Speter oexitstatus = exitstatus; 85617987Speter exitstatus = 0; 8571556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 858222165Sjilles if (varflag && isassignment(argp->narg.text)) { 859238468Sjilles expandarg(argp, varflag == 1 ? &varlist : &arglist, 860238468Sjilles EXP_VARTILDE); 861222165Sjilles continue; 862238468Sjilles } else if (varflag == 1) 863238468Sjilles varflag = isdeclarationcmd(&argp->narg) ? 2 : 0; 8641556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 8651556Srgrimes } 8661556Srgrimes *arglist.lastp = NULL; 8671556Srgrimes *varlist.lastp = NULL; 8681556Srgrimes expredir(cmd->ncmd.redirect); 8691556Srgrimes argc = 0; 8701556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 8711556Srgrimes argc++; 872218306Sjilles /* Add one slot at the beginning for tryexec(). */ 873218306Sjilles argv = stalloc(sizeof (char *) * (argc + 2)); 874218306Sjilles argv++; 8751556Srgrimes 8761556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 8771556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 8781556Srgrimes *argv++ = sp->text; 8791556Srgrimes } 8801556Srgrimes *argv = NULL; 8811556Srgrimes lastarg = NULL; 8821556Srgrimes if (iflag && funcnest == 0 && argc > 0) 8831556Srgrimes lastarg = argv[-1]; 8841556Srgrimes argv -= argc; 8851556Srgrimes 8861556Srgrimes /* Print the command if xflag is set. */ 887262951Sjmmv if (xflag) 888262951Sjmmv xtracecommand(&varlist, &arglist); 8891556Srgrimes 8901556Srgrimes /* Now locate the command. */ 8911556Srgrimes if (argc == 0) { 892157601Sstefanf /* Variable assignment(s) without command */ 8931556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 8941556Srgrimes cmdentry.u.index = BLTINCMD; 895205138Sjilles cmdentry.special = 0; 8961556Srgrimes } else { 89717987Speter static const char PATH[] = "PATH="; 898204800Sjilles int cmd_flags = 0, bltinonly = 0; 89917987Speter 90017987Speter /* 90117987Speter * Modify the command lookup path, if a PATH= assignment 90217987Speter * is present 90317987Speter */ 90417987Speter for (sp = varlist.list ; sp ; sp = sp->next) 90554884Scracauer if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 90617987Speter path = sp->text + sizeof(PATH) - 1; 907155301Sschweikh /* 90854884Scracauer * On `PATH=... command`, we need to make 90954884Scracauer * sure that the command isn't using the 91054884Scracauer * non-updated hash table of the outer PATH 911155301Sschweikh * setting and we need to make sure that 91254884Scracauer * the hash table isn't filled with items 91354884Scracauer * from the temporary setting. 91454884Scracauer * 915155301Sschweikh * It would be better to forbit using and 91654884Scracauer * updating the table while this command 91754884Scracauer * runs, by the command finding mechanism 91854884Scracauer * is heavily integrated with hash handling, 91954884Scracauer * so we just delete the hash before and after 92054884Scracauer * the command runs. Partly deleting like 92154884Scracauer * changepatch() does doesn't seem worth the 92254884Scracauer * bookinging effort, since most such runs add 923123996Smaxim * directories in front of the new PATH. 92454884Scracauer */ 925218324Sjilles clearcmdentry(); 92654884Scracauer do_clearcmdentry = 1; 92754884Scracauer } 92817987Speter 929204800Sjilles for (;;) { 930204800Sjilles if (bltinonly) { 931204800Sjilles cmdentry.u.index = find_builtin(*argv, &cmdentry.special); 932204800Sjilles if (cmdentry.u.index < 0) { 933201431Sjilles cmdentry.u.index = BLTINCMD; 934201431Sjilles argv--; 935201431Sjilles argc++; 936201431Sjilles break; 9371556Srgrimes } 938204800Sjilles } else 939204800Sjilles find_command(argv[0], &cmdentry, cmd_flags, path); 940204800Sjilles /* implement the bltin and command builtins here */ 941204800Sjilles if (cmdentry.cmdtype != CMDBUILTIN) 942204800Sjilles break; 943204800Sjilles if (cmdentry.u.index == BLTINCMD) { 944204800Sjilles if (argc == 1) 9451556Srgrimes break; 946204800Sjilles argv++; 947204800Sjilles argc--; 948204800Sjilles bltinonly = 1; 949204800Sjilles } else if (cmdentry.u.index == COMMANDCMD) { 950204800Sjilles if (argc == 1) 951204800Sjilles break; 952204800Sjilles if (!strcmp(argv[1], "-p")) { 953204800Sjilles if (argc == 2) 954204800Sjilles break; 955204800Sjilles if (argv[2][0] == '-') { 956204800Sjilles if (strcmp(argv[2], "--")) 957204800Sjilles break; 958204800Sjilles if (argc == 3) 959204800Sjilles break; 960204800Sjilles argv += 3; 961204800Sjilles argc -= 3; 962204800Sjilles } else { 963204800Sjilles argv += 2; 964204800Sjilles argc -= 2; 965204800Sjilles } 966204800Sjilles path = _PATH_STDPATH; 967218324Sjilles clearcmdentry(); 968204800Sjilles do_clearcmdentry = 1; 969204800Sjilles } else if (!strcmp(argv[1], "--")) { 970204800Sjilles if (argc == 2) 971204800Sjilles break; 972204800Sjilles argv += 2; 973204800Sjilles argc -= 2; 974204800Sjilles } else if (argv[1][0] == '-') 975204800Sjilles break; 976204800Sjilles else { 977204800Sjilles argv++; 978204800Sjilles argc--; 979204800Sjilles } 980204800Sjilles cmd_flags |= DO_NOFUNC; 981204800Sjilles bltinonly = 0; 982204800Sjilles } else 983204800Sjilles break; 9841556Srgrimes } 985204800Sjilles /* 986204800Sjilles * Special builtins lose their special properties when 987204800Sjilles * called via 'command'. 988204800Sjilles */ 989204800Sjilles if (cmd_flags & DO_NOFUNC) 990204800Sjilles cmdentry.special = 0; 9911556Srgrimes } 9921556Srgrimes 9931556Srgrimes /* Fork off a child process if necessary. */ 994223282Sjilles if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 995194127Sjilles && ((flags & EV_EXIT) == 0 || have_traps())) 99617987Speter || ((flags & EV_BACKCMD) != 0 997216826Sjilles && (cmdentry.cmdtype != CMDBUILTIN || 998216826Sjilles !safe_builtin(cmdentry.u.index, argc, argv)))) { 9991556Srgrimes jp = makejob(cmd, 1); 1000223282Sjilles mode = FORK_FG; 10011556Srgrimes if (flags & EV_BACKCMD) { 10021556Srgrimes mode = FORK_NOJOB; 10031556Srgrimes if (pipe(pip) < 0) 100453891Scracauer error("Pipe call failed: %s", strerror(errno)); 10051556Srgrimes } 1006230998Sjilles if (cmdentry.cmdtype == CMDNORMAL && 1007230998Sjilles cmd->ncmd.redirect == NULL && 1008230998Sjilles varlist.list == NULL && 1009230998Sjilles (mode == FORK_FG || mode == FORK_NOJOB) && 1010230998Sjilles !disvforkset() && !iflag && !mflag) { 1011230998Sjilles vforkexecshell(jp, argv, environment(), path, 1012230998Sjilles cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL); 1013230998Sjilles goto parent; 1014230998Sjilles } 10151556Srgrimes if (forkshell(jp, cmd, mode) != 0) 10161556Srgrimes goto parent; /* at end of routine */ 10171556Srgrimes if (flags & EV_BACKCMD) { 10181556Srgrimes FORCEINTON; 10191556Srgrimes close(pip[0]); 10201556Srgrimes if (pip[1] != 1) { 1021124780Sdes dup2(pip[1], 1); 10221556Srgrimes close(pip[1]); 10231556Srgrimes } 1024223163Sjilles flags &= ~EV_BACKCMD; 10251556Srgrimes } 10261556Srgrimes flags |= EV_EXIT; 10271556Srgrimes } 10281556Srgrimes 10291556Srgrimes /* This is the child process if a fork occurred. */ 10301556Srgrimes /* Execute the command. */ 10311556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 103220425Ssteve#ifdef DEBUG 10331556Srgrimes trputs("Shell function: "); trargs(argv); 103420425Ssteve#endif 10351556Srgrimes saveparam = shellparam; 10361556Srgrimes shellparam.malloc = 0; 103720425Ssteve shellparam.reset = 1; 10381556Srgrimes shellparam.nparam = argc - 1; 10391556Srgrimes shellparam.p = argv + 1; 10401556Srgrimes shellparam.optnext = NULL; 10411556Srgrimes INTOFF; 10421556Srgrimes savelocalvars = localvars; 10431556Srgrimes localvars = NULL; 1044196483Sjilles reffunc(cmdentry.u.func); 1045194765Sjilles savehandler = handler; 10461556Srgrimes if (setjmp(jmploc.loc)) { 1047218306Sjilles freeparam(&shellparam); 1048218306Sjilles shellparam = saveparam; 1049220978Sjilles popredir(); 1050196483Sjilles unreffunc(cmdentry.u.func); 10511556Srgrimes poplocalvars(); 10521556Srgrimes localvars = savelocalvars; 1053201283Sjilles funcnest--; 10541556Srgrimes handler = savehandler; 10551556Srgrimes longjmp(handler->loc, 1); 10561556Srgrimes } 10571556Srgrimes handler = &jmploc; 1058201283Sjilles funcnest++; 1059204802Sjilles redirect(cmd->ncmd.redirect, REDIR_PUSH); 1060199660Sjilles INTON; 10611556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 10621556Srgrimes mklocal(sp->text); 1063185231Sstefanf exitstatus = oexitstatus; 1064220978Sjilles evaltree(getfuncnode(cmdentry.u.func), 1065220978Sjilles flags & (EV_TESTED | EV_EXIT)); 10661556Srgrimes INTOFF; 1067196483Sjilles unreffunc(cmdentry.u.func); 10681556Srgrimes poplocalvars(); 10691556Srgrimes localvars = savelocalvars; 10701556Srgrimes freeparam(&shellparam); 10711556Srgrimes shellparam = saveparam; 10721556Srgrimes handler = savehandler; 1073201283Sjilles funcnest--; 10741556Srgrimes popredir(); 10751556Srgrimes INTON; 1076255215Sjilles if (evalskip == SKIPRETURN) { 10771556Srgrimes evalskip = 0; 10781556Srgrimes skipcount = 0; 10791556Srgrimes } 1080217035Sjilles if (jp) 10811556Srgrimes exitshell(exitstatus); 10821556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 108320425Ssteve#ifdef DEBUG 10841556Srgrimes trputs("builtin command: "); trargs(argv); 108520425Ssteve#endif 10861556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 10871556Srgrimes if (flags == EV_BACKCMD) { 10881556Srgrimes memout.nleft = 0; 10891556Srgrimes memout.nextc = memout.buf; 10901556Srgrimes memout.bufsize = 64; 10911556Srgrimes mode |= REDIR_BACKQ; 10921556Srgrimes } 10931556Srgrimes savecmdname = commandname; 1094199647Sjilles savetopfile = getcurrentfile(); 10951556Srgrimes cmdenviron = varlist.list; 10961556Srgrimes e = -1; 1097194765Sjilles savehandler = handler; 10981556Srgrimes if (setjmp(jmploc.loc)) { 10991556Srgrimes e = exception; 1100220978Sjilles if (e == EXINT) 1101220978Sjilles exitstatus = SIGINT+128; 1102220978Sjilles else if (e != EXEXIT) 1103220978Sjilles exitstatus = 2; 11041556Srgrimes goto cmddone; 11051556Srgrimes } 11061556Srgrimes handler = &jmploc; 1107157601Sstefanf redirect(cmd->ncmd.redirect, mode); 1108244162Sjilles outclearerror(out1); 1109205138Sjilles /* 1110205138Sjilles * If there is no command word, redirection errors should 1111205138Sjilles * not be fatal but assignment errors should. 1112205138Sjilles */ 1113228937Sjilles if (argc == 0) 1114205138Sjilles cmdentry.special = 1; 1115216870Sjilles listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET); 1116207678Sjilles if (argc > 0) 1117207678Sjilles bltinsetlocale(); 11181556Srgrimes commandname = argv[0]; 11191556Srgrimes argptr = argv + 1; 1120201053Sjilles nextopt_optptr = NULL; /* initialize nextopt */ 1121193169Sstefanf builtin_flags = flags; 11221556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 11231556Srgrimes flushall(); 1124244162Sjilles if (outiserror(out1)) { 1125244162Sjilles warning("write error on stdout"); 1126244162Sjilles if (exitstatus == 0 || exitstatus == 1) 1127244162Sjilles exitstatus = 2; 1128244162Sjilles } 11291556Srgrimescmddone: 1130207678Sjilles if (argc > 0) 1131207678Sjilles bltinunsetlocale(); 113260592Scracauer cmdenviron = NULL; 11331556Srgrimes out1 = &output; 11341556Srgrimes out2 = &errout; 11351556Srgrimes freestdout(); 1136217035Sjilles handler = savehandler; 1137218306Sjilles commandname = savecmdname; 1138218306Sjilles if (jp) 1139218306Sjilles exitshell(exitstatus); 1140201366Sjilles if (flags == EV_BACKCMD) { 1141201366Sjilles backcmd->buf = memout.buf; 1142201366Sjilles backcmd->nleft = memout.nextc - memout.buf; 1143201366Sjilles memout.buf = NULL; 1144201366Sjilles } 1145220978Sjilles if (cmdentry.u.index != EXECCMD) 1146204801Sjilles popredir(); 11471556Srgrimes if (e != -1) { 114820425Ssteve if ((e != EXERROR && e != EXEXEC) 1149157601Sstefanf || cmdentry.special) 11501556Srgrimes exraise(e); 1151199647Sjilles popfilesupto(savetopfile); 1152201366Sjilles if (flags != EV_BACKCMD) 1153201366Sjilles FORCEINTON; 11541556Srgrimes } 11551556Srgrimes } else { 115620425Ssteve#ifdef DEBUG 11571556Srgrimes trputs("normal command: "); trargs(argv); 115820425Ssteve#endif 11591556Srgrimes redirect(cmd->ncmd.redirect, 0); 11601556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 11611556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 11621556Srgrimes envp = environment(); 1163204800Sjilles shellexec(argv, envp, path, cmdentry.u.index); 11641556Srgrimes /*NOTREACHED*/ 11651556Srgrimes } 11661556Srgrimes goto out; 11671556Srgrimes 11681556Srgrimesparent: /* parent process gets here (if we forked) */ 1169212214Sjilles if (mode == FORK_FG) { /* argument to fork */ 11701556Srgrimes INTOFF; 117145916Scracauer exitstatus = waitforjob(jp, &realstatus); 11721556Srgrimes INTON; 117345916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 117445266Scracauer evalskip = SKIPBREAK; 117545266Scracauer skipcount = loopnest; 117645266Scracauer } 1177212214Sjilles } else if (mode == FORK_NOJOB) { 11781556Srgrimes backcmd->fd = pip[0]; 11791556Srgrimes close(pip[1]); 11801556Srgrimes backcmd->jp = jp; 1181223282Sjilles } 11821556Srgrimes 11831556Srgrimesout: 11841556Srgrimes if (lastarg) 11851556Srgrimes setvar("_", lastarg, 0); 118654884Scracauer if (do_clearcmdentry) 1187218324Sjilles clearcmdentry(); 11881556Srgrimes} 11891556Srgrimes 11901556Srgrimes 11911556Srgrimes 11921556Srgrimes/* 11931556Srgrimes * Search for a command. This is called before we fork so that the 11941556Srgrimes * location of the command will be available in the parent as well as 11951556Srgrimes * the child. The check for "goodname" is an overly conservative 11961556Srgrimes * check that the name will not be subject to expansion. 11971556Srgrimes */ 11981556Srgrimes 1199213811Sobrienstatic void 120090111Simpprehash(union node *n) 120117987Speter{ 12021556Srgrimes struct cmdentry entry; 12031556Srgrimes 1204159633Sstefanf if (n && n->type == NCMD && n->ncmd.args) 120517987Speter if (goodname(n->ncmd.args->narg.text)) 120617987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 120717987Speter pathval()); 12081556Srgrimes} 12091556Srgrimes 12101556Srgrimes 12111556Srgrimes 12121556Srgrimes/* 12131556Srgrimes * Builtin commands. Builtin commands whose functions are closely 12141556Srgrimes * tied to evaluation are implemented here. 12151556Srgrimes */ 12161556Srgrimes 12171556Srgrimes/* 1218201431Sjilles * No command given, a bltin command with no arguments, or a bltin command 1219201431Sjilles * with an invalid name. 12201556Srgrimes */ 12211556Srgrimes 122217987Speterint 1223201431Sjillesbltincmd(int argc, char **argv) 122417987Speter{ 1225201431Sjilles if (argc > 1) { 1226201431Sjilles out2fmt_flush("%s: not found\n", argv[1]); 1227201431Sjilles return 127; 1228201431Sjilles } 122920425Ssteve /* 123017987Speter * Preserve exitstatus of a previous possible redirection 123120425Ssteve * as POSIX mandates 123217987Speter */ 12331556Srgrimes return exitstatus; 12341556Srgrimes} 12351556Srgrimes 12361556Srgrimes 12371556Srgrimes/* 12381556Srgrimes * Handle break and continue commands. Break, continue, and return are 12391556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 12401556Srgrimes * above all check this flag, and if it is set they start skipping 12411556Srgrimes * commands rather than executing them. The variable skipcount is 12421556Srgrimes * the number of loops to break/continue, or the number of function 12431556Srgrimes * levels to return. (The latter is always 1.) It should probably 12441556Srgrimes * be an error to break out of more loops than exist, but it isn't 12451556Srgrimes * in the standard shell so we don't make it one here. 12461556Srgrimes */ 12471556Srgrimes 124817987Speterint 124990111Simpbreakcmd(int argc, char **argv) 125017987Speter{ 125120425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 12521556Srgrimes 12531556Srgrimes if (n > loopnest) 12541556Srgrimes n = loopnest; 12551556Srgrimes if (n > 0) { 12561556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 12571556Srgrimes skipcount = n; 12581556Srgrimes } 12591556Srgrimes return 0; 12601556Srgrimes} 12611556Srgrimes 1262100437Stjr/* 1263100437Stjr * The `command' command. 1264100437Stjr */ 1265100437Stjrint 1266240541Sjillescommandcmd(int argc __unused, char **argv __unused) 1267100437Stjr{ 1268207783Sjilles const char *path; 1269100437Stjr int ch; 1270151810Sstefanf int cmd = -1; 12711556Srgrimes 1272204800Sjilles path = bltinlookup("PATH", 1); 1273100437Stjr 1274240541Sjilles while ((ch = nextopt("pvV")) != '\0') { 1275100437Stjr switch (ch) { 1276100437Stjr case 'p': 1277207783Sjilles path = _PATH_STDPATH; 1278100437Stjr break; 1279151810Sstefanf case 'v': 1280151810Sstefanf cmd = TYPECMD_SMALLV; 1281151810Sstefanf break; 1282151810Sstefanf case 'V': 1283151810Sstefanf cmd = TYPECMD_BIGV; 1284151810Sstefanf break; 1285100437Stjr } 1286100437Stjr } 1287100437Stjr 1288151810Sstefanf if (cmd != -1) { 1289240541Sjilles if (*argptr == NULL || argptr[1] != NULL) 1290151810Sstefanf error("wrong number of arguments"); 1291240541Sjilles return typecmd_impl(2, argptr - 1, cmd, path); 1292151810Sstefanf } 1293240541Sjilles if (*argptr != NULL) 1294214538Sjilles error("commandcmd bad call"); 1295100437Stjr 1296100437Stjr /* 1297100437Stjr * Do nothing successfully if no command was specified; 1298100437Stjr * ksh also does this. 1299100437Stjr */ 1300204800Sjilles return 0; 1301100437Stjr} 1302100437Stjr 1303100437Stjr 13041556Srgrimes/* 13051556Srgrimes * The return command. 13061556Srgrimes */ 13071556Srgrimes 130817987Speterint 130990111Simpreturncmd(int argc, char **argv) 131017987Speter{ 131120425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 13121556Srgrimes 1313255215Sjilles evalskip = SKIPRETURN; 1314255215Sjilles skipcount = 1; 13151556Srgrimes return ret; 13161556Srgrimes} 13171556Srgrimes 13181556Srgrimes 131917987Speterint 132090111Simpfalsecmd(int argc __unused, char **argv __unused) 132117987Speter{ 132217987Speter return 1; 132317987Speter} 132417987Speter 132517987Speter 132617987Speterint 132790111Simptruecmd(int argc __unused, char **argv __unused) 132817987Speter{ 13291556Srgrimes return 0; 13301556Srgrimes} 13311556Srgrimes 13321556Srgrimes 133317987Speterint 133490111Simpexeccmd(int argc, char **argv) 133517987Speter{ 1336208630Sjilles /* 1337208630Sjilles * Because we have historically not supported any options, 1338208630Sjilles * only treat "--" specially. 1339208630Sjilles */ 1340208630Sjilles if (argc > 1 && strcmp(argv[1], "--") == 0) 1341208630Sjilles argc--, argv++; 13421556Srgrimes if (argc > 1) { 134317987Speter struct strlist *sp; 134417987Speter 13451556Srgrimes iflag = 0; /* exit on error */ 13461556Srgrimes mflag = 0; 13471556Srgrimes optschanged(); 134817987Speter for (sp = cmdenviron; sp ; sp = sp->next) 134917987Speter setvareq(sp->text, VEXPORT|VSTACK); 13501556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 13511556Srgrimes 13521556Srgrimes } 13531556Srgrimes return 0; 13541556Srgrimes} 1355153091Sstefanf 1356153091Sstefanf 1357153091Sstefanfint 1358153091Sstefanftimescmd(int argc __unused, char **argv __unused) 1359153091Sstefanf{ 1360153091Sstefanf struct rusage ru; 1361153091Sstefanf long shumins, shsmins, chumins, chsmins; 1362153091Sstefanf double shusecs, shssecs, chusecs, chssecs; 1363153091Sstefanf 1364153091Sstefanf if (getrusage(RUSAGE_SELF, &ru) < 0) 1365153091Sstefanf return 1; 1366153091Sstefanf shumins = ru.ru_utime.tv_sec / 60; 1367153091Sstefanf shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1368153091Sstefanf shsmins = ru.ru_stime.tv_sec / 60; 1369153091Sstefanf shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1370153091Sstefanf if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1371153091Sstefanf return 1; 1372153091Sstefanf chumins = ru.ru_utime.tv_sec / 60; 1373153091Sstefanf chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1374153091Sstefanf chsmins = ru.ru_stime.tv_sec / 60; 1375153091Sstefanf chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1376153091Sstefanf out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1377153091Sstefanf shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1378153091Sstefanf return 0; 1379153091Sstefanf} 1380