eval.c revision 213811
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 213811 2010-10-13 22:18:03Z obrien $"); 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 *); 97213811Sobrienstatic void evalcommand(union node *, int, struct backcmd *); 98213811Sobrienstatic void prehash(union node *); 991556Srgrimes 1001556Srgrimes 1011556Srgrimes/* 1021556Srgrimes * Called to reset things after an exception. 1031556Srgrimes */ 1041556Srgrimes 1051556Srgrimes#ifdef mkinit 1061556SrgrimesINCLUDE "eval.h" 1071556Srgrimes 1081556SrgrimesRESET { 1091556Srgrimes evalskip = 0; 1101556Srgrimes loopnest = 0; 1111556Srgrimes funcnest = 0; 1121556Srgrimes} 1131556Srgrimes 1141556SrgrimesSHELLPROC { 1151556Srgrimes exitstatus = 0; 1161556Srgrimes} 1171556Srgrimes#endif 1181556Srgrimes 1191556Srgrimes 1201556Srgrimes 1211556Srgrimes/* 12246684Skris * The eval command. 1231556Srgrimes */ 1241556Srgrimes 12517987Speterint 12690111Simpevalcmd(int argc, char **argv) 1271556Srgrimes{ 1281556Srgrimes char *p; 1291556Srgrimes char *concat; 1301556Srgrimes char **ap; 1311556Srgrimes 1321556Srgrimes if (argc > 1) { 1331556Srgrimes p = argv[1]; 1341556Srgrimes if (argc > 2) { 1351556Srgrimes STARTSTACKSTR(concat); 1361556Srgrimes ap = argv + 2; 1371556Srgrimes for (;;) { 1381556Srgrimes while (*p) 1391556Srgrimes STPUTC(*p++, concat); 1401556Srgrimes if ((p = *ap++) == NULL) 1411556Srgrimes break; 1421556Srgrimes STPUTC(' ', concat); 1431556Srgrimes } 1441556Srgrimes STPUTC('\0', concat); 1451556Srgrimes p = grabstackstr(concat); 1461556Srgrimes } 147193169Sstefanf evalstring(p, builtin_flags & EV_TESTED); 148210829Sjilles } else 149210829Sjilles exitstatus = 0; 1501556Srgrimes return exitstatus; 1511556Srgrimes} 1521556Srgrimes 1531556Srgrimes 1541556Srgrimes/* 1551556Srgrimes * Execute a command or commands contained in a string. 1561556Srgrimes */ 1571556Srgrimes 1581556Srgrimesvoid 159193169Sstefanfevalstring(char *s, int flags) 16090111Simp{ 1611556Srgrimes union node *n; 1621556Srgrimes struct stackmark smark; 163194128Sjilles int flags_exit; 164210829Sjilles int any; 1651556Srgrimes 166194128Sjilles flags_exit = flags & EV_EXIT; 167194128Sjilles flags &= ~EV_EXIT; 168210829Sjilles any = 0; 1691556Srgrimes setstackmark(&smark); 1701556Srgrimes setinputstring(s, 1); 1711556Srgrimes while ((n = parsecmd(0)) != NEOF) { 172194128Sjilles if (n != NULL) { 173194128Sjilles if (flags_exit && preadateof()) 174194128Sjilles evaltree(n, flags | EV_EXIT); 175194128Sjilles else 176194128Sjilles evaltree(n, flags); 177210829Sjilles any = 1; 178194128Sjilles } 1791556Srgrimes popstackmark(&smark); 1801556Srgrimes } 1811556Srgrimes popfile(); 1821556Srgrimes popstackmark(&smark); 183210829Sjilles if (!any) 184210829Sjilles exitstatus = 0; 185194128Sjilles if (flags_exit) 186194128Sjilles exitshell(exitstatus); 1871556Srgrimes} 1881556Srgrimes 1891556Srgrimes 1901556Srgrimes/* 1911556Srgrimes * Evaluate a parse tree. The value is left in the global variable 1921556Srgrimes * exitstatus. 1931556Srgrimes */ 1941556Srgrimes 1951556Srgrimesvoid 19690111Simpevaltree(union node *n, int flags) 19717987Speter{ 198149927Sstefanf int do_etest; 199149927Sstefanf 200149927Sstefanf do_etest = 0; 2011556Srgrimes if (n == NULL) { 2021556Srgrimes TRACE(("evaltree(NULL) called\n")); 2031556Srgrimes exitstatus = 0; 2041556Srgrimes goto out; 2051556Srgrimes } 20617987Speter#ifndef NO_HISTORY 2071556Srgrimes displayhist = 1; /* show history substitutions done with fc */ 20817987Speter#endif 209149802Sstefanf TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type)); 2101556Srgrimes switch (n->type) { 2111556Srgrimes case NSEMI: 212149932Sstefanf evaltree(n->nbinary.ch1, flags & ~EV_EXIT); 2131556Srgrimes if (evalskip) 2141556Srgrimes goto out; 2151556Srgrimes evaltree(n->nbinary.ch2, flags); 2161556Srgrimes break; 2171556Srgrimes case NAND: 2181556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 21918754Ssteve if (evalskip || exitstatus != 0) { 2201556Srgrimes goto out; 22118754Ssteve } 2221556Srgrimes evaltree(n->nbinary.ch2, flags); 2231556Srgrimes break; 2241556Srgrimes case NOR: 2251556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2261556Srgrimes if (evalskip || exitstatus == 0) 2271556Srgrimes goto out; 2281556Srgrimes evaltree(n->nbinary.ch2, flags); 2291556Srgrimes break; 2301556Srgrimes case NREDIR: 231205154Sjilles evalredir(n, flags); 2321556Srgrimes break; 2331556Srgrimes case NSUBSHELL: 2341556Srgrimes evalsubshell(n, flags); 235149927Sstefanf do_etest = !(flags & EV_TESTED); 2361556Srgrimes break; 2371556Srgrimes case NBACKGND: 2381556Srgrimes evalsubshell(n, flags); 2391556Srgrimes break; 2401556Srgrimes case NIF: { 2411556Srgrimes evaltree(n->nif.test, EV_TESTED); 2421556Srgrimes if (evalskip) 2431556Srgrimes goto out; 24420425Ssteve if (exitstatus == 0) 2451556Srgrimes evaltree(n->nif.ifpart, flags); 24617987Speter else if (n->nif.elsepart) 2471556Srgrimes evaltree(n->nif.elsepart, flags); 24820425Ssteve else 24920425Ssteve exitstatus = 0; 2501556Srgrimes break; 2511556Srgrimes } 2521556Srgrimes case NWHILE: 2531556Srgrimes case NUNTIL: 254149933Sstefanf evalloop(n, flags & ~EV_EXIT); 2551556Srgrimes break; 2561556Srgrimes case NFOR: 257149933Sstefanf evalfor(n, flags & ~EV_EXIT); 2581556Srgrimes break; 2591556Srgrimes case NCASE: 2601556Srgrimes evalcase(n, flags); 2611556Srgrimes break; 2621556Srgrimes case NDEFUN: 2631556Srgrimes defun(n->narg.text, n->narg.next); 2641556Srgrimes exitstatus = 0; 2651556Srgrimes break; 2661556Srgrimes case NNOT: 2671556Srgrimes evaltree(n->nnot.com, EV_TESTED); 2681556Srgrimes exitstatus = !exitstatus; 2691556Srgrimes break; 2701556Srgrimes 2711556Srgrimes case NPIPE: 2721556Srgrimes evalpipe(n); 273149927Sstefanf do_etest = !(flags & EV_TESTED); 2741556Srgrimes break; 2751556Srgrimes case NCMD: 2761556Srgrimes evalcommand(n, flags, (struct backcmd *)NULL); 277149927Sstefanf do_etest = !(flags & EV_TESTED); 2781556Srgrimes break; 2791556Srgrimes default: 2801556Srgrimes out1fmt("Node type = %d\n", n->type); 2811556Srgrimes flushout(&output); 2821556Srgrimes break; 2831556Srgrimes } 2841556Srgrimesout: 2851556Srgrimes if (pendingsigs) 2861556Srgrimes dotrap(); 287149927Sstefanf if ((flags & EV_EXIT) || (eflag && exitstatus != 0 && do_etest)) 2881556Srgrimes exitshell(exitstatus); 2891556Srgrimes} 2901556Srgrimes 2911556Srgrimes 292213811Sobrienstatic void 293149933Sstefanfevalloop(union node *n, int flags) 29417987Speter{ 2951556Srgrimes int status; 2961556Srgrimes 2971556Srgrimes loopnest++; 2981556Srgrimes status = 0; 2991556Srgrimes for (;;) { 3001556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 3011556Srgrimes if (evalskip) { 3021556Srgrimesskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 3031556Srgrimes evalskip = 0; 3041556Srgrimes continue; 3051556Srgrimes } 3061556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3071556Srgrimes evalskip = 0; 308212475Sjilles if (evalskip == SKIPFUNC || evalskip == SKIPFILE) 309212475Sjilles status = exitstatus; 3101556Srgrimes break; 3111556Srgrimes } 3121556Srgrimes if (n->type == NWHILE) { 3131556Srgrimes if (exitstatus != 0) 3141556Srgrimes break; 3151556Srgrimes } else { 3161556Srgrimes if (exitstatus == 0) 3171556Srgrimes break; 3181556Srgrimes } 319149933Sstefanf evaltree(n->nbinary.ch2, flags); 3201556Srgrimes status = exitstatus; 3211556Srgrimes if (evalskip) 3221556Srgrimes goto skipping; 3231556Srgrimes } 3241556Srgrimes loopnest--; 3251556Srgrimes exitstatus = status; 3261556Srgrimes} 3271556Srgrimes 3281556Srgrimes 3291556Srgrimes 330213811Sobrienstatic void 331149933Sstefanfevalfor(union node *n, int flags) 33217987Speter{ 3331556Srgrimes struct arglist arglist; 3341556Srgrimes union node *argp; 3351556Srgrimes struct strlist *sp; 3361556Srgrimes struct stackmark smark; 3371556Srgrimes 3381556Srgrimes setstackmark(&smark); 3391556Srgrimes arglist.lastp = &arglist.list; 3401556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 34117987Speter oexitstatus = exitstatus; 3421556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3431556Srgrimes if (evalskip) 3441556Srgrimes goto out; 3451556Srgrimes } 3461556Srgrimes *arglist.lastp = NULL; 3471556Srgrimes 3481556Srgrimes exitstatus = 0; 3491556Srgrimes loopnest++; 3501556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3511556Srgrimes setvar(n->nfor.var, sp->text, 0); 352149933Sstefanf evaltree(n->nfor.body, flags); 3531556Srgrimes if (evalskip) { 3541556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3551556Srgrimes evalskip = 0; 3561556Srgrimes continue; 3571556Srgrimes } 3581556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3591556Srgrimes evalskip = 0; 3601556Srgrimes break; 3611556Srgrimes } 3621556Srgrimes } 3631556Srgrimes loopnest--; 3641556Srgrimesout: 3651556Srgrimes popstackmark(&smark); 3661556Srgrimes} 3671556Srgrimes 3681556Srgrimes 3691556Srgrimes 370213811Sobrienstatic void 37190111Simpevalcase(union node *n, int flags) 37217987Speter{ 3731556Srgrimes union node *cp; 3741556Srgrimes union node *patp; 3751556Srgrimes struct arglist arglist; 3761556Srgrimes struct stackmark smark; 3771556Srgrimes 3781556Srgrimes setstackmark(&smark); 3791556Srgrimes arglist.lastp = &arglist.list; 38017987Speter oexitstatus = exitstatus; 381172440Sstefanf exitstatus = 0; 3821556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3831556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3841556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3851556Srgrimes if (casematch(patp, arglist.list->text)) { 3861556Srgrimes if (evalskip == 0) { 3871556Srgrimes evaltree(cp->nclist.body, flags); 3881556Srgrimes } 3891556Srgrimes goto out; 3901556Srgrimes } 3911556Srgrimes } 3921556Srgrimes } 3931556Srgrimesout: 3941556Srgrimes popstackmark(&smark); 3951556Srgrimes} 3961556Srgrimes 3971556Srgrimes 3981556Srgrimes 3991556Srgrimes/* 4001556Srgrimes * Kick off a subshell to evaluate a tree. 4011556Srgrimes */ 4021556Srgrimes 403213811Sobrienstatic void 40490111Simpevalsubshell(union node *n, int flags) 40517987Speter{ 4061556Srgrimes struct job *jp; 4071556Srgrimes int backgnd = (n->type == NBACKGND); 4081556Srgrimes 4091556Srgrimes expredir(n->nredir.redirect); 410194774Sjilles if ((!backgnd && flags & EV_EXIT && !have_traps()) || 411194774Sjilles forkshell(jp = makejob(n, 1), n, backgnd) == 0) { 4121556Srgrimes if (backgnd) 4131556Srgrimes flags &=~ EV_TESTED; 4141556Srgrimes redirect(n->nredir.redirect, 0); 4151556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 416201053Sjilles } else if (! backgnd) { 4171556Srgrimes INTOFF; 41845916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4191556Srgrimes INTON; 4201556Srgrimes } 4211556Srgrimes} 4221556Srgrimes 4231556Srgrimes 424205154Sjilles/* 425205154Sjilles * Evaluate a redirected compound command. 426205154Sjilles */ 4271556Srgrimes 428213811Sobrienstatic void 429205154Sjillesevalredir(union node *n, int flags) 430205154Sjilles{ 431205154Sjilles struct jmploc jmploc; 432205154Sjilles struct jmploc *savehandler; 433205154Sjilles volatile int in_redirect = 1; 434205154Sjilles 435205154Sjilles expredir(n->nredir.redirect); 436205154Sjilles savehandler = handler; 437205154Sjilles if (setjmp(jmploc.loc)) { 438205154Sjilles int e; 439205154Sjilles 440205154Sjilles handler = savehandler; 441205154Sjilles e = exception; 442205154Sjilles if (e == EXERROR || e == EXEXEC) { 443205154Sjilles popredir(); 444205154Sjilles if (in_redirect) { 445205154Sjilles exitstatus = 2; 446205154Sjilles return; 447205154Sjilles } 448205154Sjilles } 449205154Sjilles longjmp(handler->loc, 1); 450205154Sjilles } else { 451205154Sjilles INTOFF; 452205154Sjilles handler = &jmploc; 453205154Sjilles redirect(n->nredir.redirect, REDIR_PUSH); 454205154Sjilles in_redirect = 0; 455205154Sjilles INTON; 456205154Sjilles evaltree(n->nredir.n, flags); 457205154Sjilles } 458205154Sjilles INTOFF; 459205154Sjilles handler = savehandler; 460205154Sjilles popredir(); 461205154Sjilles INTON; 462205154Sjilles} 463205154Sjilles 464205154Sjilles 4651556Srgrimes/* 4661556Srgrimes * Compute the names of the files in a redirection list. 4671556Srgrimes */ 4681556Srgrimes 469213811Sobrienstatic void 47090111Simpexpredir(union node *n) 47117987Speter{ 47225222Ssteve union node *redir; 4731556Srgrimes 4741556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 47517987Speter struct arglist fn; 47617987Speter fn.lastp = &fn.list; 47717987Speter oexitstatus = exitstatus; 47817987Speter switch (redir->type) { 47917987Speter case NFROM: 48017987Speter case NTO: 48166612Sbrian case NFROMTO: 48217987Speter case NAPPEND: 48396922Stjr case NCLOBBER: 4841556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4851556Srgrimes redir->nfile.expfname = fn.list->text; 48617987Speter break; 48717987Speter case NFROMFD: 48817987Speter case NTOFD: 48917987Speter if (redir->ndup.vname) { 490181017Sstefanf expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); 49117987Speter fixredir(redir, fn.list->text, 1); 49217987Speter } 49317987Speter break; 4941556Srgrimes } 4951556Srgrimes } 4961556Srgrimes} 4971556Srgrimes 4981556Srgrimes 4991556Srgrimes 5001556Srgrimes/* 5011556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 5021556Srgrimes * of the process creating the pipeline. (This differs from some versions 5031556Srgrimes * of the shell, which make the last process in a pipeline the parent 5041556Srgrimes * of all the rest.) 5051556Srgrimes */ 5061556Srgrimes 507213811Sobrienstatic void 50890111Simpevalpipe(union node *n) 50917987Speter{ 5101556Srgrimes struct job *jp; 5111556Srgrimes struct nodelist *lp; 5121556Srgrimes int pipelen; 5131556Srgrimes int prevfd; 5141556Srgrimes int pip[2]; 5151556Srgrimes 516149802Sstefanf TRACE(("evalpipe(%p) called\n", (void *)n)); 5171556Srgrimes pipelen = 0; 5181556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 5191556Srgrimes pipelen++; 5201556Srgrimes INTOFF; 5211556Srgrimes jp = makejob(n, pipelen); 5221556Srgrimes prevfd = -1; 5231556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 5241556Srgrimes prehash(lp->n); 5251556Srgrimes pip[1] = -1; 5261556Srgrimes if (lp->next) { 5271556Srgrimes if (pipe(pip) < 0) { 5281556Srgrimes close(prevfd); 52953891Scracauer error("Pipe call failed: %s", strerror(errno)); 5301556Srgrimes } 5311556Srgrimes } 5321556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 5331556Srgrimes INTON; 5341556Srgrimes if (prevfd > 0) { 535124780Sdes dup2(prevfd, 0); 5361556Srgrimes close(prevfd); 5371556Srgrimes } 5381556Srgrimes if (pip[1] >= 0) { 53953282Scracauer if (!(prevfd >= 0 && pip[0] == 0)) 54052900Scracauer close(pip[0]); 5411556Srgrimes if (pip[1] != 1) { 542124780Sdes dup2(pip[1], 1); 5431556Srgrimes close(pip[1]); 5441556Srgrimes } 5451556Srgrimes } 5461556Srgrimes evaltree(lp->n, EV_EXIT); 5471556Srgrimes } 5481556Srgrimes if (prevfd >= 0) 5491556Srgrimes close(prevfd); 5501556Srgrimes prevfd = pip[0]; 5511556Srgrimes close(pip[1]); 5521556Srgrimes } 5531556Srgrimes INTON; 5541556Srgrimes if (n->npipe.backgnd == 0) { 5551556Srgrimes INTOFF; 55645916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 5571556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5581556Srgrimes INTON; 5591556Srgrimes } 5601556Srgrimes} 5611556Srgrimes 5621556Srgrimes 5631556Srgrimes 5641556Srgrimes/* 5651556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5661556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5671556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5681556Srgrimes * Should be called with interrupts off. 5691556Srgrimes */ 5701556Srgrimes 5711556Srgrimesvoid 57290111Simpevalbackcmd(union node *n, struct backcmd *result) 57317987Speter{ 5741556Srgrimes int pip[2]; 5751556Srgrimes struct job *jp; 5761556Srgrimes struct stackmark smark; /* unnecessary */ 5771556Srgrimes 5781556Srgrimes setstackmark(&smark); 5791556Srgrimes result->fd = -1; 5801556Srgrimes result->buf = NULL; 5811556Srgrimes result->nleft = 0; 5821556Srgrimes result->jp = NULL; 58317987Speter if (n == NULL) { 58417987Speter exitstatus = 0; 5851556Srgrimes goto out; 58617987Speter } 5871556Srgrimes if (n->type == NCMD) { 58817987Speter exitstatus = oexitstatus; 5891556Srgrimes evalcommand(n, EV_BACKCMD, result); 5901556Srgrimes } else { 59117987Speter exitstatus = 0; 5921556Srgrimes if (pipe(pip) < 0) 59353891Scracauer error("Pipe call failed: %s", strerror(errno)); 5941556Srgrimes jp = makejob(n, 1); 5951556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 5961556Srgrimes FORCEINTON; 5971556Srgrimes close(pip[0]); 5981556Srgrimes if (pip[1] != 1) { 599124780Sdes dup2(pip[1], 1); 6001556Srgrimes close(pip[1]); 6011556Srgrimes } 6021556Srgrimes evaltree(n, EV_EXIT); 6031556Srgrimes } 6041556Srgrimes close(pip[1]); 6051556Srgrimes result->fd = pip[0]; 6061556Srgrimes result->jp = jp; 6071556Srgrimes } 6081556Srgrimesout: 6091556Srgrimes popstackmark(&smark); 610109627Stjr TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 6111556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 6121556Srgrimes} 6131556Srgrimes 6141556Srgrimes 6151556Srgrimes 6161556Srgrimes/* 6171556Srgrimes * Execute a simple command. 6181556Srgrimes */ 6191556Srgrimes 620213811Sobrienstatic void 62190111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd) 62217987Speter{ 6231556Srgrimes struct stackmark smark; 6241556Srgrimes union node *argp; 6251556Srgrimes struct arglist arglist; 6261556Srgrimes struct arglist varlist; 6271556Srgrimes char **argv; 6281556Srgrimes int argc; 6291556Srgrimes char **envp; 6301556Srgrimes int varflag; 6311556Srgrimes struct strlist *sp; 6321556Srgrimes int mode; 6331556Srgrimes int pip[2]; 6341556Srgrimes struct cmdentry cmdentry; 6351556Srgrimes struct job *jp; 6361556Srgrimes struct jmploc jmploc; 637194765Sjilles struct jmploc *savehandler; 638194765Sjilles char *savecmdname; 639194765Sjilles struct shparam saveparam; 640194765Sjilles struct localvar *savelocalvars; 641199647Sjilles struct parsefile *savetopfile; 6421556Srgrimes volatile int e; 6431556Srgrimes char *lastarg; 64445916Scracauer int realstatus; 64554884Scracauer int do_clearcmdentry; 646211287Sjilles const char *path = pathval(); 6471556Srgrimes 6481556Srgrimes /* First expand the arguments. */ 649149802Sstefanf TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 6501556Srgrimes setstackmark(&smark); 6511556Srgrimes arglist.lastp = &arglist.list; 6521556Srgrimes varlist.lastp = &varlist.list; 6531556Srgrimes varflag = 1; 65454884Scracauer do_clearcmdentry = 0; 65517987Speter oexitstatus = exitstatus; 65617987Speter exitstatus = 0; 6571556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 65817987Speter char *p = argp->narg.text; 6591556Srgrimes if (varflag && is_name(*p)) { 6601556Srgrimes do { 6611556Srgrimes p++; 6621556Srgrimes } while (is_in_name(*p)); 6631556Srgrimes if (*p == '=') { 6641556Srgrimes expandarg(argp, &varlist, EXP_VARTILDE); 6651556Srgrimes continue; 6661556Srgrimes } 6671556Srgrimes } 6681556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 6691556Srgrimes varflag = 0; 6701556Srgrimes } 6711556Srgrimes *arglist.lastp = NULL; 6721556Srgrimes *varlist.lastp = NULL; 6731556Srgrimes expredir(cmd->ncmd.redirect); 6741556Srgrimes argc = 0; 6751556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 6761556Srgrimes argc++; 6771556Srgrimes argv = stalloc(sizeof (char *) * (argc + 1)); 6781556Srgrimes 6791556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6801556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 6811556Srgrimes *argv++ = sp->text; 6821556Srgrimes } 6831556Srgrimes *argv = NULL; 6841556Srgrimes lastarg = NULL; 6851556Srgrimes if (iflag && funcnest == 0 && argc > 0) 6861556Srgrimes lastarg = argv[-1]; 6871556Srgrimes argv -= argc; 6881556Srgrimes 6891556Srgrimes /* Print the command if xflag is set. */ 6901556Srgrimes if (xflag) { 691159632Sstefanf char sep = 0; 692194786Sjilles const char *p; 693159632Sstefanf out2str(ps4val()); 6941556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 695159632Sstefanf if (sep != 0) 696201366Sjilles out2c(' '); 697194786Sjilles p = sp->text; 698194786Sjilles while (*p != '=' && *p != '\0') 699194786Sjilles out2c(*p++); 700194786Sjilles if (*p != '\0') { 701194786Sjilles out2c(*p++); 702194786Sjilles out2qstr(p); 703194786Sjilles } 704159632Sstefanf sep = ' '; 7051556Srgrimes } 7061556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 707159632Sstefanf if (sep != 0) 708201366Sjilles out2c(' '); 709194786Sjilles /* Disambiguate command looking like assignment. */ 710194786Sjilles if (sp == arglist.list && 711194786Sjilles strchr(sp->text, '=') != NULL && 712194786Sjilles strchr(sp->text, '\'') == NULL) { 713194786Sjilles out2c('\''); 714194786Sjilles out2str(sp->text); 715194786Sjilles out2c('\''); 716194786Sjilles } else 717194786Sjilles out2qstr(sp->text); 718159632Sstefanf sep = ' '; 7191556Srgrimes } 720201366Sjilles out2c('\n'); 7211556Srgrimes flushout(&errout); 7221556Srgrimes } 7231556Srgrimes 7241556Srgrimes /* Now locate the command. */ 7251556Srgrimes if (argc == 0) { 726157601Sstefanf /* Variable assignment(s) without command */ 7271556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 7281556Srgrimes cmdentry.u.index = BLTINCMD; 729205138Sjilles cmdentry.special = 0; 7301556Srgrimes } else { 73117987Speter static const char PATH[] = "PATH="; 732204800Sjilles int cmd_flags = 0, bltinonly = 0; 73317987Speter 73417987Speter /* 73517987Speter * Modify the command lookup path, if a PATH= assignment 73617987Speter * is present 73717987Speter */ 73817987Speter for (sp = varlist.list ; sp ; sp = sp->next) 73954884Scracauer if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 74017987Speter path = sp->text + sizeof(PATH) - 1; 741155301Sschweikh /* 74254884Scracauer * On `PATH=... command`, we need to make 74354884Scracauer * sure that the command isn't using the 74454884Scracauer * non-updated hash table of the outer PATH 745155301Sschweikh * setting and we need to make sure that 74654884Scracauer * the hash table isn't filled with items 74754884Scracauer * from the temporary setting. 74854884Scracauer * 749155301Sschweikh * It would be better to forbit using and 75054884Scracauer * updating the table while this command 75154884Scracauer * runs, by the command finding mechanism 75254884Scracauer * is heavily integrated with hash handling, 75354884Scracauer * so we just delete the hash before and after 75454884Scracauer * the command runs. Partly deleting like 75554884Scracauer * changepatch() does doesn't seem worth the 75654884Scracauer * bookinging effort, since most such runs add 757123996Smaxim * directories in front of the new PATH. 75854884Scracauer */ 75954884Scracauer clearcmdentry(0); 76054884Scracauer do_clearcmdentry = 1; 76154884Scracauer } 76217987Speter 763204800Sjilles for (;;) { 764204800Sjilles if (bltinonly) { 765204800Sjilles cmdentry.u.index = find_builtin(*argv, &cmdentry.special); 766204800Sjilles if (cmdentry.u.index < 0) { 767201431Sjilles cmdentry.u.index = BLTINCMD; 768201431Sjilles argv--; 769201431Sjilles argc++; 770201431Sjilles break; 7711556Srgrimes } 772204800Sjilles } else 773204800Sjilles find_command(argv[0], &cmdentry, cmd_flags, path); 774204800Sjilles /* implement the bltin and command builtins here */ 775204800Sjilles if (cmdentry.cmdtype != CMDBUILTIN) 776204800Sjilles break; 777204800Sjilles if (cmdentry.u.index == BLTINCMD) { 778204800Sjilles if (argc == 1) 7791556Srgrimes break; 780204800Sjilles argv++; 781204800Sjilles argc--; 782204800Sjilles bltinonly = 1; 783204800Sjilles } else if (cmdentry.u.index == COMMANDCMD) { 784204800Sjilles if (argc == 1) 785204800Sjilles break; 786204800Sjilles if (!strcmp(argv[1], "-p")) { 787204800Sjilles if (argc == 2) 788204800Sjilles break; 789204800Sjilles if (argv[2][0] == '-') { 790204800Sjilles if (strcmp(argv[2], "--")) 791204800Sjilles break; 792204800Sjilles if (argc == 3) 793204800Sjilles break; 794204800Sjilles argv += 3; 795204800Sjilles argc -= 3; 796204800Sjilles } else { 797204800Sjilles argv += 2; 798204800Sjilles argc -= 2; 799204800Sjilles } 800204800Sjilles path = _PATH_STDPATH; 801204800Sjilles clearcmdentry(0); 802204800Sjilles do_clearcmdentry = 1; 803204800Sjilles } else if (!strcmp(argv[1], "--")) { 804204800Sjilles if (argc == 2) 805204800Sjilles break; 806204800Sjilles argv += 2; 807204800Sjilles argc -= 2; 808204800Sjilles } else if (argv[1][0] == '-') 809204800Sjilles break; 810204800Sjilles else { 811204800Sjilles argv++; 812204800Sjilles argc--; 813204800Sjilles } 814204800Sjilles cmd_flags |= DO_NOFUNC; 815204800Sjilles bltinonly = 0; 816204800Sjilles } else 817204800Sjilles break; 8181556Srgrimes } 819204800Sjilles /* 820204800Sjilles * Special builtins lose their special properties when 821204800Sjilles * called via 'command'. 822204800Sjilles */ 823204800Sjilles if (cmd_flags & DO_NOFUNC) 824204800Sjilles cmdentry.special = 0; 8251556Srgrimes } 8261556Srgrimes 8271556Srgrimes /* Fork off a child process if necessary. */ 8281556Srgrimes if (cmd->ncmd.backgnd 829197820Sjilles || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 830194127Sjilles && ((flags & EV_EXIT) == 0 || have_traps())) 83117987Speter || ((flags & EV_BACKCMD) != 0 8321556Srgrimes && (cmdentry.cmdtype != CMDBUILTIN 83348896Ssheldonh || cmdentry.u.index == CDCMD 8341556Srgrimes || cmdentry.u.index == DOTCMD 835204800Sjilles || cmdentry.u.index == EVALCMD))) { 8361556Srgrimes jp = makejob(cmd, 1); 8371556Srgrimes mode = cmd->ncmd.backgnd; 8381556Srgrimes if (flags & EV_BACKCMD) { 8391556Srgrimes mode = FORK_NOJOB; 8401556Srgrimes if (pipe(pip) < 0) 84153891Scracauer error("Pipe call failed: %s", strerror(errno)); 8421556Srgrimes } 8431556Srgrimes if (forkshell(jp, cmd, mode) != 0) 8441556Srgrimes goto parent; /* at end of routine */ 8451556Srgrimes if (flags & EV_BACKCMD) { 8461556Srgrimes FORCEINTON; 8471556Srgrimes close(pip[0]); 8481556Srgrimes if (pip[1] != 1) { 849124780Sdes dup2(pip[1], 1); 8501556Srgrimes close(pip[1]); 8511556Srgrimes } 8521556Srgrimes } 8531556Srgrimes flags |= EV_EXIT; 8541556Srgrimes } 8551556Srgrimes 8561556Srgrimes /* This is the child process if a fork occurred. */ 8571556Srgrimes /* Execute the command. */ 8581556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 85920425Ssteve#ifdef DEBUG 8601556Srgrimes trputs("Shell function: "); trargs(argv); 86120425Ssteve#endif 8621556Srgrimes saveparam = shellparam; 8631556Srgrimes shellparam.malloc = 0; 86420425Ssteve shellparam.reset = 1; 8651556Srgrimes shellparam.nparam = argc - 1; 8661556Srgrimes shellparam.p = argv + 1; 8671556Srgrimes shellparam.optnext = NULL; 8681556Srgrimes INTOFF; 8691556Srgrimes savelocalvars = localvars; 8701556Srgrimes localvars = NULL; 871196483Sjilles reffunc(cmdentry.u.func); 872194765Sjilles savehandler = handler; 8731556Srgrimes if (setjmp(jmploc.loc)) { 8741556Srgrimes if (exception == EXSHELLPROC) 875194765Sjilles freeparam(&saveparam); 8761556Srgrimes else { 8771556Srgrimes freeparam(&shellparam); 8781556Srgrimes shellparam = saveparam; 879204802Sjilles if (exception == EXERROR || exception == EXEXEC) 880204802Sjilles popredir(); 8811556Srgrimes } 882196483Sjilles unreffunc(cmdentry.u.func); 8831556Srgrimes poplocalvars(); 8841556Srgrimes localvars = savelocalvars; 885201283Sjilles funcnest--; 8861556Srgrimes handler = savehandler; 8871556Srgrimes longjmp(handler->loc, 1); 8881556Srgrimes } 8891556Srgrimes handler = &jmploc; 890201283Sjilles funcnest++; 891204802Sjilles redirect(cmd->ncmd.redirect, REDIR_PUSH); 892199660Sjilles INTON; 8931556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8941556Srgrimes mklocal(sp->text); 895185231Sstefanf exitstatus = oexitstatus; 89635675Scracauer if (flags & EV_TESTED) 897196634Sjilles evaltree(getfuncnode(cmdentry.u.func), EV_TESTED); 89835675Scracauer else 899196634Sjilles evaltree(getfuncnode(cmdentry.u.func), 0); 9001556Srgrimes INTOFF; 901196483Sjilles unreffunc(cmdentry.u.func); 9021556Srgrimes poplocalvars(); 9031556Srgrimes localvars = savelocalvars; 9041556Srgrimes freeparam(&shellparam); 9051556Srgrimes shellparam = saveparam; 9061556Srgrimes handler = savehandler; 907201283Sjilles funcnest--; 9081556Srgrimes popredir(); 9091556Srgrimes INTON; 9101556Srgrimes if (evalskip == SKIPFUNC) { 9111556Srgrimes evalskip = 0; 9121556Srgrimes skipcount = 0; 9131556Srgrimes } 9141556Srgrimes if (flags & EV_EXIT) 9151556Srgrimes exitshell(exitstatus); 9161556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 91720425Ssteve#ifdef DEBUG 9181556Srgrimes trputs("builtin command: "); trargs(argv); 91920425Ssteve#endif 9201556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 9211556Srgrimes if (flags == EV_BACKCMD) { 9221556Srgrimes memout.nleft = 0; 9231556Srgrimes memout.nextc = memout.buf; 9241556Srgrimes memout.bufsize = 64; 9251556Srgrimes mode |= REDIR_BACKQ; 926201366Sjilles cmdentry.special = 0; 9271556Srgrimes } 9281556Srgrimes savecmdname = commandname; 929199647Sjilles savetopfile = getcurrentfile(); 9301556Srgrimes cmdenviron = varlist.list; 9311556Srgrimes e = -1; 932194765Sjilles savehandler = handler; 9331556Srgrimes if (setjmp(jmploc.loc)) { 9341556Srgrimes e = exception; 9351556Srgrimes exitstatus = (e == EXINT)? SIGINT+128 : 2; 9361556Srgrimes goto cmddone; 9371556Srgrimes } 9381556Srgrimes handler = &jmploc; 939157601Sstefanf redirect(cmd->ncmd.redirect, mode); 940205138Sjilles /* 941205138Sjilles * If there is no command word, redirection errors should 942205138Sjilles * not be fatal but assignment errors should. 943205138Sjilles */ 944205138Sjilles if (argc == 0 && !(flags & EV_BACKCMD)) 945205138Sjilles cmdentry.special = 1; 946157601Sstefanf if (cmdentry.special) 947157601Sstefanf listsetvar(cmdenviron); 948207678Sjilles if (argc > 0) 949207678Sjilles bltinsetlocale(); 9501556Srgrimes commandname = argv[0]; 9511556Srgrimes argptr = argv + 1; 952201053Sjilles nextopt_optptr = NULL; /* initialize nextopt */ 953193169Sstefanf builtin_flags = flags; 9541556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 9551556Srgrimes flushall(); 9561556Srgrimescmddone: 957207678Sjilles if (argc > 0) 958207678Sjilles bltinunsetlocale(); 95960592Scracauer cmdenviron = NULL; 9601556Srgrimes out1 = &output; 9611556Srgrimes out2 = &errout; 9621556Srgrimes freestdout(); 9631556Srgrimes if (e != EXSHELLPROC) { 9641556Srgrimes commandname = savecmdname; 9651556Srgrimes if (flags & EV_EXIT) { 9661556Srgrimes exitshell(exitstatus); 9671556Srgrimes } 9681556Srgrimes } 9691556Srgrimes handler = savehandler; 970201366Sjilles if (flags == EV_BACKCMD) { 971201366Sjilles backcmd->buf = memout.buf; 972201366Sjilles backcmd->nleft = memout.nextc - memout.buf; 973201366Sjilles memout.buf = NULL; 974201366Sjilles } 975204801Sjilles if (cmdentry.u.index != EXECCMD && 976204801Sjilles (e == -1 || e == EXERROR || e == EXEXEC)) 977204801Sjilles popredir(); 9781556Srgrimes if (e != -1) { 97920425Ssteve if ((e != EXERROR && e != EXEXEC) 980157601Sstefanf || cmdentry.special) 9811556Srgrimes exraise(e); 982199647Sjilles popfilesupto(savetopfile); 983201366Sjilles if (flags != EV_BACKCMD) 984201366Sjilles FORCEINTON; 9851556Srgrimes } 9861556Srgrimes } else { 98720425Ssteve#ifdef DEBUG 9881556Srgrimes trputs("normal command: "); trargs(argv); 98920425Ssteve#endif 9901556Srgrimes redirect(cmd->ncmd.redirect, 0); 9911556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 9921556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 9931556Srgrimes envp = environment(); 994204800Sjilles shellexec(argv, envp, path, cmdentry.u.index); 9951556Srgrimes /*NOTREACHED*/ 9961556Srgrimes } 9971556Srgrimes goto out; 9981556Srgrimes 9991556Srgrimesparent: /* parent process gets here (if we forked) */ 1000212214Sjilles if (mode == FORK_FG) { /* argument to fork */ 10011556Srgrimes INTOFF; 100245916Scracauer exitstatus = waitforjob(jp, &realstatus); 10031556Srgrimes INTON; 100445916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 100545266Scracauer evalskip = SKIPBREAK; 100645266Scracauer skipcount = loopnest; 100745266Scracauer } 1008212214Sjilles } else if (mode == FORK_NOJOB) { 10091556Srgrimes backcmd->fd = pip[0]; 10101556Srgrimes close(pip[1]); 10111556Srgrimes backcmd->jp = jp; 10121556Srgrimes } 10131556Srgrimes 10141556Srgrimesout: 10151556Srgrimes if (lastarg) 10161556Srgrimes setvar("_", lastarg, 0); 101754884Scracauer if (do_clearcmdentry) 101854884Scracauer clearcmdentry(0); 10191556Srgrimes popstackmark(&smark); 10201556Srgrimes} 10211556Srgrimes 10221556Srgrimes 10231556Srgrimes 10241556Srgrimes/* 10251556Srgrimes * Search for a command. This is called before we fork so that the 10261556Srgrimes * location of the command will be available in the parent as well as 10271556Srgrimes * the child. The check for "goodname" is an overly conservative 10281556Srgrimes * check that the name will not be subject to expansion. 10291556Srgrimes */ 10301556Srgrimes 1031213811Sobrienstatic void 103290111Simpprehash(union node *n) 103317987Speter{ 10341556Srgrimes struct cmdentry entry; 10351556Srgrimes 1036159633Sstefanf if (n && n->type == NCMD && n->ncmd.args) 103717987Speter if (goodname(n->ncmd.args->narg.text)) 103817987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 103917987Speter pathval()); 10401556Srgrimes} 10411556Srgrimes 10421556Srgrimes 10431556Srgrimes 10441556Srgrimes/* 10451556Srgrimes * Builtin commands. Builtin commands whose functions are closely 10461556Srgrimes * tied to evaluation are implemented here. 10471556Srgrimes */ 10481556Srgrimes 10491556Srgrimes/* 1050201431Sjilles * No command given, a bltin command with no arguments, or a bltin command 1051201431Sjilles * with an invalid name. 10521556Srgrimes */ 10531556Srgrimes 105417987Speterint 1055201431Sjillesbltincmd(int argc, char **argv) 105617987Speter{ 1057201431Sjilles if (argc > 1) { 1058201431Sjilles out2fmt_flush("%s: not found\n", argv[1]); 1059201431Sjilles return 127; 1060201431Sjilles } 106120425Ssteve /* 106217987Speter * Preserve exitstatus of a previous possible redirection 106320425Ssteve * as POSIX mandates 106417987Speter */ 10651556Srgrimes return exitstatus; 10661556Srgrimes} 10671556Srgrimes 10681556Srgrimes 10691556Srgrimes/* 10701556Srgrimes * Handle break and continue commands. Break, continue, and return are 10711556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 10721556Srgrimes * above all check this flag, and if it is set they start skipping 10731556Srgrimes * commands rather than executing them. The variable skipcount is 10741556Srgrimes * the number of loops to break/continue, or the number of function 10751556Srgrimes * levels to return. (The latter is always 1.) It should probably 10761556Srgrimes * be an error to break out of more loops than exist, but it isn't 10771556Srgrimes * in the standard shell so we don't make it one here. 10781556Srgrimes */ 10791556Srgrimes 108017987Speterint 108190111Simpbreakcmd(int argc, char **argv) 108217987Speter{ 108320425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 10841556Srgrimes 10851556Srgrimes if (n > loopnest) 10861556Srgrimes n = loopnest; 10871556Srgrimes if (n > 0) { 10881556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 10891556Srgrimes skipcount = n; 10901556Srgrimes } 10911556Srgrimes return 0; 10921556Srgrimes} 10931556Srgrimes 1094100437Stjr/* 1095100437Stjr * The `command' command. 1096100437Stjr */ 1097100437Stjrint 1098100437Stjrcommandcmd(int argc, char **argv) 1099100437Stjr{ 1100207783Sjilles const char *path; 1101100437Stjr int ch; 1102151810Sstefanf int cmd = -1; 11031556Srgrimes 1104204800Sjilles path = bltinlookup("PATH", 1); 1105100437Stjr 1106100437Stjr optind = optreset = 1; 1107100663Stjr opterr = 0; 1108151810Sstefanf while ((ch = getopt(argc, argv, "pvV")) != -1) { 1109100437Stjr switch (ch) { 1110100437Stjr case 'p': 1111207783Sjilles path = _PATH_STDPATH; 1112100437Stjr break; 1113151810Sstefanf case 'v': 1114151810Sstefanf cmd = TYPECMD_SMALLV; 1115151810Sstefanf break; 1116151810Sstefanf case 'V': 1117151810Sstefanf cmd = TYPECMD_BIGV; 1118151810Sstefanf break; 1119100437Stjr case '?': 1120100437Stjr default: 1121100437Stjr error("unknown option: -%c", optopt); 1122100437Stjr } 1123100437Stjr } 1124100437Stjr argc -= optind; 1125100437Stjr argv += optind; 1126100437Stjr 1127151810Sstefanf if (cmd != -1) { 1128151810Sstefanf if (argc != 1) 1129151810Sstefanf error("wrong number of arguments"); 1130201343Sjilles return typecmd_impl(2, argv - 1, cmd, path); 1131151810Sstefanf } 1132204800Sjilles if (argc != 0) 1133204800Sjilles error("commandcmd() called while it should not be"); 1134100437Stjr 1135100437Stjr /* 1136100437Stjr * Do nothing successfully if no command was specified; 1137100437Stjr * ksh also does this. 1138100437Stjr */ 1139204800Sjilles return 0; 1140100437Stjr} 1141100437Stjr 1142100437Stjr 11431556Srgrimes/* 11441556Srgrimes * The return command. 11451556Srgrimes */ 11461556Srgrimes 114717987Speterint 114890111Simpreturncmd(int argc, char **argv) 114917987Speter{ 115020425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 11511556Srgrimes 11521556Srgrimes if (funcnest) { 11531556Srgrimes evalskip = SKIPFUNC; 11541556Srgrimes skipcount = 1; 115520425Ssteve } else { 115620425Ssteve /* skip the rest of the file */ 115720425Ssteve evalskip = SKIPFILE; 115820425Ssteve skipcount = 1; 11591556Srgrimes } 11601556Srgrimes return ret; 11611556Srgrimes} 11621556Srgrimes 11631556Srgrimes 116417987Speterint 116590111Simpfalsecmd(int argc __unused, char **argv __unused) 116617987Speter{ 116717987Speter return 1; 116817987Speter} 116917987Speter 117017987Speter 117117987Speterint 117290111Simptruecmd(int argc __unused, char **argv __unused) 117317987Speter{ 11741556Srgrimes return 0; 11751556Srgrimes} 11761556Srgrimes 11771556Srgrimes 117817987Speterint 117990111Simpexeccmd(int argc, char **argv) 118017987Speter{ 1181208630Sjilles /* 1182208630Sjilles * Because we have historically not supported any options, 1183208630Sjilles * only treat "--" specially. 1184208630Sjilles */ 1185208630Sjilles if (argc > 1 && strcmp(argv[1], "--") == 0) 1186208630Sjilles argc--, argv++; 11871556Srgrimes if (argc > 1) { 118817987Speter struct strlist *sp; 118917987Speter 11901556Srgrimes iflag = 0; /* exit on error */ 11911556Srgrimes mflag = 0; 11921556Srgrimes optschanged(); 119317987Speter for (sp = cmdenviron; sp ; sp = sp->next) 119417987Speter setvareq(sp->text, VEXPORT|VSTACK); 11951556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 11961556Srgrimes 11971556Srgrimes } 11981556Srgrimes return 0; 11991556Srgrimes} 1200153091Sstefanf 1201153091Sstefanf 1202153091Sstefanfint 1203153091Sstefanftimescmd(int argc __unused, char **argv __unused) 1204153091Sstefanf{ 1205153091Sstefanf struct rusage ru; 1206153091Sstefanf long shumins, shsmins, chumins, chsmins; 1207153091Sstefanf double shusecs, shssecs, chusecs, chssecs; 1208153091Sstefanf 1209153091Sstefanf if (getrusage(RUSAGE_SELF, &ru) < 0) 1210153091Sstefanf return 1; 1211153091Sstefanf shumins = ru.ru_utime.tv_sec / 60; 1212153091Sstefanf shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1213153091Sstefanf shsmins = ru.ru_stime.tv_sec / 60; 1214153091Sstefanf shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1215153091Sstefanf if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1216153091Sstefanf return 1; 1217153091Sstefanf chumins = ru.ru_utime.tv_sec / 60; 1218153091Sstefanf chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1219153091Sstefanf chsmins = ru.ru_stime.tv_sec / 60; 1220153091Sstefanf chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1221153091Sstefanf out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1222153091Sstefanf shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1223153091Sstefanf return 0; 1224153091Sstefanf} 1225