eval.c revision 155301
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 155301 2006-02-04 14:37:50Z schweikh $"); 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 771556Srgrimes/* flags in argument to evaltree */ 781556Srgrimes#define EV_EXIT 01 /* exit after evaluating tree */ 791556Srgrimes#define EV_TESTED 02 /* exit status is checked; ignore -e flag */ 801556Srgrimes#define EV_BACKCMD 04 /* command executing within back quotes */ 811556Srgrimes 821556SrgrimesMKINIT int evalskip; /* set if we are skipping commands */ 831556SrgrimesSTATIC int skipcount; /* number of levels to skip */ 841556SrgrimesMKINIT int loopnest; /* current loop nesting level */ 851556Srgrimesint funcnest; /* depth of function calls */ 861556Srgrimes 871556Srgrimes 881556Srgrimeschar *commandname; 891556Srgrimesstruct strlist *cmdenviron; 901556Srgrimesint exitstatus; /* exit status of last command */ 9117987Speterint oexitstatus; /* saved exit status */ 921556Srgrimes 931556Srgrimes 94149933SstefanfSTATIC void evalloop(union node *, int); 95149933SstefanfSTATIC void evalfor(union node *, int); 9690111SimpSTATIC void evalcase(union node *, int); 9790111SimpSTATIC void evalsubshell(union node *, int); 9890111SimpSTATIC void expredir(union node *); 9990111SimpSTATIC void evalpipe(union node *); 10090111SimpSTATIC void evalcommand(union node *, int, struct backcmd *); 10190111SimpSTATIC void prehash(union node *); 1021556Srgrimes 1031556Srgrimes 1041556Srgrimes/* 1051556Srgrimes * Called to reset things after an exception. 1061556Srgrimes */ 1071556Srgrimes 1081556Srgrimes#ifdef mkinit 1091556SrgrimesINCLUDE "eval.h" 1101556Srgrimes 1111556SrgrimesRESET { 1121556Srgrimes evalskip = 0; 1131556Srgrimes loopnest = 0; 1141556Srgrimes funcnest = 0; 1151556Srgrimes} 1161556Srgrimes 1171556SrgrimesSHELLPROC { 1181556Srgrimes exitstatus = 0; 1191556Srgrimes} 1201556Srgrimes#endif 1211556Srgrimes 1221556Srgrimes 1231556Srgrimes 1241556Srgrimes/* 12546684Skris * The eval command. 1261556Srgrimes */ 1271556Srgrimes 12817987Speterint 12990111Simpevalcmd(int argc, char **argv) 1301556Srgrimes{ 1311556Srgrimes char *p; 1321556Srgrimes char *concat; 1331556Srgrimes char **ap; 1341556Srgrimes 1351556Srgrimes if (argc > 1) { 1361556Srgrimes p = argv[1]; 1371556Srgrimes if (argc > 2) { 1381556Srgrimes STARTSTACKSTR(concat); 1391556Srgrimes ap = argv + 2; 1401556Srgrimes for (;;) { 1411556Srgrimes while (*p) 1421556Srgrimes STPUTC(*p++, concat); 1431556Srgrimes if ((p = *ap++) == NULL) 1441556Srgrimes break; 1451556Srgrimes STPUTC(' ', concat); 1461556Srgrimes } 1471556Srgrimes STPUTC('\0', concat); 1481556Srgrimes p = grabstackstr(concat); 1491556Srgrimes } 1501556Srgrimes evalstring(p); 1511556Srgrimes } 1521556Srgrimes return exitstatus; 1531556Srgrimes} 1541556Srgrimes 1551556Srgrimes 1561556Srgrimes/* 1571556Srgrimes * Execute a command or commands contained in a string. 1581556Srgrimes */ 1591556Srgrimes 1601556Srgrimesvoid 16190111Simpevalstring(char *s) 16290111Simp{ 1631556Srgrimes union node *n; 1641556Srgrimes struct stackmark smark; 1651556Srgrimes 1661556Srgrimes setstackmark(&smark); 1671556Srgrimes setinputstring(s, 1); 1681556Srgrimes while ((n = parsecmd(0)) != NEOF) { 1691556Srgrimes evaltree(n, 0); 1701556Srgrimes popstackmark(&smark); 1711556Srgrimes } 1721556Srgrimes popfile(); 1731556Srgrimes popstackmark(&smark); 1741556Srgrimes} 1751556Srgrimes 1761556Srgrimes 1771556Srgrimes 1781556Srgrimes/* 1791556Srgrimes * Evaluate a parse tree. The value is left in the global variable 1801556Srgrimes * exitstatus. 1811556Srgrimes */ 1821556Srgrimes 1831556Srgrimesvoid 18490111Simpevaltree(union node *n, int flags) 18517987Speter{ 186149927Sstefanf int do_etest; 187149927Sstefanf 188149927Sstefanf do_etest = 0; 1891556Srgrimes if (n == NULL) { 1901556Srgrimes TRACE(("evaltree(NULL) called\n")); 1911556Srgrimes exitstatus = 0; 1921556Srgrimes goto out; 1931556Srgrimes } 19417987Speter#ifndef NO_HISTORY 1951556Srgrimes displayhist = 1; /* show history substitutions done with fc */ 19617987Speter#endif 197149802Sstefanf TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type)); 1981556Srgrimes switch (n->type) { 1991556Srgrimes case NSEMI: 200149932Sstefanf evaltree(n->nbinary.ch1, flags & ~EV_EXIT); 2011556Srgrimes if (evalskip) 2021556Srgrimes goto out; 2031556Srgrimes evaltree(n->nbinary.ch2, flags); 2041556Srgrimes break; 2051556Srgrimes case NAND: 2061556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 20718754Ssteve if (evalskip || exitstatus != 0) { 2081556Srgrimes goto out; 20918754Ssteve } 2101556Srgrimes evaltree(n->nbinary.ch2, flags); 2111556Srgrimes break; 2121556Srgrimes case NOR: 2131556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2141556Srgrimes if (evalskip || exitstatus == 0) 2151556Srgrimes goto out; 2161556Srgrimes evaltree(n->nbinary.ch2, flags); 2171556Srgrimes break; 2181556Srgrimes case NREDIR: 2191556Srgrimes expredir(n->nredir.redirect); 2201556Srgrimes redirect(n->nredir.redirect, REDIR_PUSH); 2211556Srgrimes evaltree(n->nredir.n, flags); 2221556Srgrimes popredir(); 2231556Srgrimes break; 2241556Srgrimes case NSUBSHELL: 2251556Srgrimes evalsubshell(n, flags); 226149927Sstefanf do_etest = !(flags & EV_TESTED); 2271556Srgrimes break; 2281556Srgrimes case NBACKGND: 2291556Srgrimes evalsubshell(n, flags); 2301556Srgrimes break; 2311556Srgrimes case NIF: { 2321556Srgrimes evaltree(n->nif.test, EV_TESTED); 2331556Srgrimes if (evalskip) 2341556Srgrimes goto out; 23520425Ssteve if (exitstatus == 0) 2361556Srgrimes evaltree(n->nif.ifpart, flags); 23717987Speter else if (n->nif.elsepart) 2381556Srgrimes evaltree(n->nif.elsepart, flags); 23920425Ssteve else 24020425Ssteve exitstatus = 0; 2411556Srgrimes break; 2421556Srgrimes } 2431556Srgrimes case NWHILE: 2441556Srgrimes case NUNTIL: 245149933Sstefanf evalloop(n, flags & ~EV_EXIT); 2461556Srgrimes break; 2471556Srgrimes case NFOR: 248149933Sstefanf evalfor(n, flags & ~EV_EXIT); 2491556Srgrimes break; 2501556Srgrimes case NCASE: 2511556Srgrimes evalcase(n, flags); 2521556Srgrimes break; 2531556Srgrimes case NDEFUN: 2541556Srgrimes defun(n->narg.text, n->narg.next); 2551556Srgrimes exitstatus = 0; 2561556Srgrimes break; 2571556Srgrimes case NNOT: 2581556Srgrimes evaltree(n->nnot.com, EV_TESTED); 2591556Srgrimes exitstatus = !exitstatus; 2601556Srgrimes break; 2611556Srgrimes 2621556Srgrimes case NPIPE: 2631556Srgrimes evalpipe(n); 264149927Sstefanf do_etest = !(flags & EV_TESTED); 2651556Srgrimes break; 2661556Srgrimes case NCMD: 2671556Srgrimes evalcommand(n, flags, (struct backcmd *)NULL); 268149927Sstefanf do_etest = !(flags & EV_TESTED); 2691556Srgrimes break; 2701556Srgrimes default: 2711556Srgrimes out1fmt("Node type = %d\n", n->type); 2721556Srgrimes flushout(&output); 2731556Srgrimes break; 2741556Srgrimes } 2751556Srgrimesout: 2761556Srgrimes if (pendingsigs) 2771556Srgrimes dotrap(); 278149927Sstefanf if ((flags & EV_EXIT) || (eflag && exitstatus != 0 && do_etest)) 2791556Srgrimes exitshell(exitstatus); 2801556Srgrimes} 2811556Srgrimes 2821556Srgrimes 2831556SrgrimesSTATIC void 284149933Sstefanfevalloop(union node *n, int flags) 28517987Speter{ 2861556Srgrimes int status; 2871556Srgrimes 2881556Srgrimes loopnest++; 2891556Srgrimes status = 0; 2901556Srgrimes for (;;) { 2911556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2921556Srgrimes if (evalskip) { 2931556Srgrimesskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 2941556Srgrimes evalskip = 0; 2951556Srgrimes continue; 2961556Srgrimes } 2971556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 2981556Srgrimes evalskip = 0; 2991556Srgrimes break; 3001556Srgrimes } 3011556Srgrimes if (n->type == NWHILE) { 3021556Srgrimes if (exitstatus != 0) 3031556Srgrimes break; 3041556Srgrimes } else { 3051556Srgrimes if (exitstatus == 0) 3061556Srgrimes break; 3071556Srgrimes } 308149933Sstefanf evaltree(n->nbinary.ch2, flags); 3091556Srgrimes status = exitstatus; 3101556Srgrimes if (evalskip) 3111556Srgrimes goto skipping; 3121556Srgrimes } 3131556Srgrimes loopnest--; 3141556Srgrimes exitstatus = status; 3151556Srgrimes} 3161556Srgrimes 3171556Srgrimes 3181556Srgrimes 3191556SrgrimesSTATIC void 320149933Sstefanfevalfor(union node *n, int flags) 32117987Speter{ 3221556Srgrimes struct arglist arglist; 3231556Srgrimes union node *argp; 3241556Srgrimes struct strlist *sp; 3251556Srgrimes struct stackmark smark; 3261556Srgrimes 3271556Srgrimes setstackmark(&smark); 3281556Srgrimes arglist.lastp = &arglist.list; 3291556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 33017987Speter oexitstatus = exitstatus; 3311556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3321556Srgrimes if (evalskip) 3331556Srgrimes goto out; 3341556Srgrimes } 3351556Srgrimes *arglist.lastp = NULL; 3361556Srgrimes 3371556Srgrimes exitstatus = 0; 3381556Srgrimes loopnest++; 3391556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3401556Srgrimes setvar(n->nfor.var, sp->text, 0); 341149933Sstefanf evaltree(n->nfor.body, flags); 3421556Srgrimes if (evalskip) { 3431556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3441556Srgrimes evalskip = 0; 3451556Srgrimes continue; 3461556Srgrimes } 3471556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3481556Srgrimes evalskip = 0; 3491556Srgrimes break; 3501556Srgrimes } 3511556Srgrimes } 3521556Srgrimes loopnest--; 3531556Srgrimesout: 3541556Srgrimes popstackmark(&smark); 3551556Srgrimes} 3561556Srgrimes 3571556Srgrimes 3581556Srgrimes 3591556SrgrimesSTATIC void 36090111Simpevalcase(union node *n, int flags) 36117987Speter{ 3621556Srgrimes union node *cp; 3631556Srgrimes union node *patp; 3641556Srgrimes struct arglist arglist; 3651556Srgrimes struct stackmark smark; 3661556Srgrimes 3671556Srgrimes setstackmark(&smark); 3681556Srgrimes arglist.lastp = &arglist.list; 36917987Speter oexitstatus = exitstatus; 3701556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3711556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3721556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3731556Srgrimes if (casematch(patp, arglist.list->text)) { 3741556Srgrimes if (evalskip == 0) { 3751556Srgrimes evaltree(cp->nclist.body, flags); 3761556Srgrimes } 3771556Srgrimes goto out; 3781556Srgrimes } 3791556Srgrimes } 3801556Srgrimes } 3811556Srgrimesout: 3821556Srgrimes popstackmark(&smark); 3831556Srgrimes} 3841556Srgrimes 3851556Srgrimes 3861556Srgrimes 3871556Srgrimes/* 3881556Srgrimes * Kick off a subshell to evaluate a tree. 3891556Srgrimes */ 3901556Srgrimes 3911556SrgrimesSTATIC void 39290111Simpevalsubshell(union node *n, int flags) 39317987Speter{ 3941556Srgrimes struct job *jp; 3951556Srgrimes int backgnd = (n->type == NBACKGND); 3961556Srgrimes 3971556Srgrimes expredir(n->nredir.redirect); 3981556Srgrimes jp = makejob(n, 1); 3991556Srgrimes if (forkshell(jp, n, backgnd) == 0) { 4001556Srgrimes if (backgnd) 4011556Srgrimes flags &=~ EV_TESTED; 4021556Srgrimes redirect(n->nredir.redirect, 0); 4031556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 4041556Srgrimes } 4051556Srgrimes if (! backgnd) { 4061556Srgrimes INTOFF; 40745916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4081556Srgrimes INTON; 4091556Srgrimes } 4101556Srgrimes} 4111556Srgrimes 4121556Srgrimes 4131556Srgrimes 4141556Srgrimes/* 4151556Srgrimes * Compute the names of the files in a redirection list. 4161556Srgrimes */ 4171556Srgrimes 4181556SrgrimesSTATIC void 41990111Simpexpredir(union node *n) 42017987Speter{ 42125222Ssteve union node *redir; 4221556Srgrimes 4231556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 42417987Speter struct arglist fn; 42517987Speter fn.lastp = &fn.list; 42617987Speter oexitstatus = exitstatus; 42717987Speter switch (redir->type) { 42817987Speter case NFROM: 42917987Speter case NTO: 43066612Sbrian case NFROMTO: 43117987Speter case NAPPEND: 43296922Stjr case NCLOBBER: 4331556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4341556Srgrimes redir->nfile.expfname = fn.list->text; 43517987Speter break; 43617987Speter case NFROMFD: 43717987Speter case NTOFD: 43817987Speter if (redir->ndup.vname) { 43917987Speter expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); 44017987Speter fixredir(redir, fn.list->text, 1); 44117987Speter } 44217987Speter break; 4431556Srgrimes } 4441556Srgrimes } 4451556Srgrimes} 4461556Srgrimes 4471556Srgrimes 4481556Srgrimes 4491556Srgrimes/* 4501556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 4511556Srgrimes * of the process creating the pipeline. (This differs from some versions 4521556Srgrimes * of the shell, which make the last process in a pipeline the parent 4531556Srgrimes * of all the rest.) 4541556Srgrimes */ 4551556Srgrimes 4561556SrgrimesSTATIC void 45790111Simpevalpipe(union node *n) 45817987Speter{ 4591556Srgrimes struct job *jp; 4601556Srgrimes struct nodelist *lp; 4611556Srgrimes int pipelen; 4621556Srgrimes int prevfd; 4631556Srgrimes int pip[2]; 4641556Srgrimes 465149802Sstefanf TRACE(("evalpipe(%p) called\n", (void *)n)); 4661556Srgrimes pipelen = 0; 4671556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 4681556Srgrimes pipelen++; 4691556Srgrimes INTOFF; 4701556Srgrimes jp = makejob(n, pipelen); 4711556Srgrimes prevfd = -1; 4721556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 4731556Srgrimes prehash(lp->n); 4741556Srgrimes pip[1] = -1; 4751556Srgrimes if (lp->next) { 4761556Srgrimes if (pipe(pip) < 0) { 4771556Srgrimes close(prevfd); 47853891Scracauer error("Pipe call failed: %s", strerror(errno)); 4791556Srgrimes } 4801556Srgrimes } 4811556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 4821556Srgrimes INTON; 4831556Srgrimes if (prevfd > 0) { 484124780Sdes dup2(prevfd, 0); 4851556Srgrimes close(prevfd); 4861556Srgrimes } 4871556Srgrimes if (pip[1] >= 0) { 48853282Scracauer if (!(prevfd >= 0 && pip[0] == 0)) 48952900Scracauer close(pip[0]); 4901556Srgrimes if (pip[1] != 1) { 491124780Sdes dup2(pip[1], 1); 4921556Srgrimes close(pip[1]); 4931556Srgrimes } 4941556Srgrimes } 4951556Srgrimes evaltree(lp->n, EV_EXIT); 4961556Srgrimes } 4971556Srgrimes if (prevfd >= 0) 4981556Srgrimes close(prevfd); 4991556Srgrimes prevfd = pip[0]; 5001556Srgrimes close(pip[1]); 5011556Srgrimes } 5021556Srgrimes INTON; 5031556Srgrimes if (n->npipe.backgnd == 0) { 5041556Srgrimes INTOFF; 50545916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 5061556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5071556Srgrimes INTON; 5081556Srgrimes } 5091556Srgrimes} 5101556Srgrimes 5111556Srgrimes 5121556Srgrimes 5131556Srgrimes/* 5141556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5151556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5161556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5171556Srgrimes * Should be called with interrupts off. 5181556Srgrimes */ 5191556Srgrimes 5201556Srgrimesvoid 52190111Simpevalbackcmd(union node *n, struct backcmd *result) 52217987Speter{ 5231556Srgrimes int pip[2]; 5241556Srgrimes struct job *jp; 5251556Srgrimes struct stackmark smark; /* unnecessary */ 5261556Srgrimes 5271556Srgrimes setstackmark(&smark); 5281556Srgrimes result->fd = -1; 5291556Srgrimes result->buf = NULL; 5301556Srgrimes result->nleft = 0; 5311556Srgrimes result->jp = NULL; 53217987Speter if (n == NULL) { 53317987Speter exitstatus = 0; 5341556Srgrimes goto out; 53517987Speter } 5361556Srgrimes if (n->type == NCMD) { 53717987Speter exitstatus = oexitstatus; 5381556Srgrimes evalcommand(n, EV_BACKCMD, result); 5391556Srgrimes } else { 54017987Speter exitstatus = 0; 5411556Srgrimes if (pipe(pip) < 0) 54253891Scracauer error("Pipe call failed: %s", strerror(errno)); 5431556Srgrimes jp = makejob(n, 1); 5441556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 5451556Srgrimes FORCEINTON; 5461556Srgrimes close(pip[0]); 5471556Srgrimes if (pip[1] != 1) { 548124780Sdes dup2(pip[1], 1); 5491556Srgrimes close(pip[1]); 5501556Srgrimes } 5511556Srgrimes evaltree(n, EV_EXIT); 5521556Srgrimes } 5531556Srgrimes close(pip[1]); 5541556Srgrimes result->fd = pip[0]; 5551556Srgrimes result->jp = jp; 5561556Srgrimes } 5571556Srgrimesout: 5581556Srgrimes popstackmark(&smark); 559109627Stjr TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 5601556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 5611556Srgrimes} 5621556Srgrimes 5631556Srgrimes 5641556Srgrimes 5651556Srgrimes/* 5661556Srgrimes * Execute a simple command. 5671556Srgrimes */ 5681556Srgrimes 5691556SrgrimesSTATIC void 57090111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd) 57117987Speter{ 5721556Srgrimes struct stackmark smark; 5731556Srgrimes union node *argp; 5741556Srgrimes struct arglist arglist; 5751556Srgrimes struct arglist varlist; 5761556Srgrimes char **argv; 5771556Srgrimes int argc; 5781556Srgrimes char **envp; 5791556Srgrimes int varflag; 5801556Srgrimes struct strlist *sp; 5811556Srgrimes int mode; 5821556Srgrimes int pip[2]; 5831556Srgrimes struct cmdentry cmdentry; 5841556Srgrimes struct job *jp; 5851556Srgrimes struct jmploc jmploc; 5861556Srgrimes struct jmploc *volatile savehandler; 5871556Srgrimes char *volatile savecmdname; 5881556Srgrimes volatile struct shparam saveparam; 5891556Srgrimes struct localvar *volatile savelocalvars; 5901556Srgrimes volatile int e; 5911556Srgrimes char *lastarg; 59245916Scracauer int realstatus; 59354884Scracauer int do_clearcmdentry; 59417987Speter#if __GNUC__ 59517987Speter /* Avoid longjmp clobbering */ 59617987Speter (void) &argv; 59717987Speter (void) &argc; 59817987Speter (void) &lastarg; 59917987Speter (void) &flags; 60054884Scracauer (void) &do_clearcmdentry; 60117987Speter#endif 6021556Srgrimes 6031556Srgrimes /* First expand the arguments. */ 604149802Sstefanf TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 6051556Srgrimes setstackmark(&smark); 6061556Srgrimes arglist.lastp = &arglist.list; 6071556Srgrimes varlist.lastp = &varlist.list; 6081556Srgrimes varflag = 1; 60954884Scracauer do_clearcmdentry = 0; 61017987Speter oexitstatus = exitstatus; 61117987Speter exitstatus = 0; 6121556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 61317987Speter char *p = argp->narg.text; 6141556Srgrimes if (varflag && is_name(*p)) { 6151556Srgrimes do { 6161556Srgrimes p++; 6171556Srgrimes } while (is_in_name(*p)); 6181556Srgrimes if (*p == '=') { 6191556Srgrimes expandarg(argp, &varlist, EXP_VARTILDE); 6201556Srgrimes continue; 6211556Srgrimes } 6221556Srgrimes } 6231556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 6241556Srgrimes varflag = 0; 6251556Srgrimes } 6261556Srgrimes *arglist.lastp = NULL; 6271556Srgrimes *varlist.lastp = NULL; 6281556Srgrimes expredir(cmd->ncmd.redirect); 6291556Srgrimes argc = 0; 6301556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 6311556Srgrimes argc++; 6321556Srgrimes argv = stalloc(sizeof (char *) * (argc + 1)); 6331556Srgrimes 6341556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6351556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 6361556Srgrimes *argv++ = sp->text; 6371556Srgrimes } 6381556Srgrimes *argv = NULL; 6391556Srgrimes lastarg = NULL; 6401556Srgrimes if (iflag && funcnest == 0 && argc > 0) 6411556Srgrimes lastarg = argv[-1]; 6421556Srgrimes argv -= argc; 6431556Srgrimes 6441556Srgrimes /* Print the command if xflag is set. */ 6451556Srgrimes if (xflag) { 6461556Srgrimes outc('+', &errout); 6471556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 6481556Srgrimes outc(' ', &errout); 6491556Srgrimes out2str(sp->text); 6501556Srgrimes } 6511556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6521556Srgrimes outc(' ', &errout); 6531556Srgrimes out2str(sp->text); 6541556Srgrimes } 6551556Srgrimes outc('\n', &errout); 6561556Srgrimes flushout(&errout); 6571556Srgrimes } 6581556Srgrimes 6591556Srgrimes /* Now locate the command. */ 6601556Srgrimes if (argc == 0) { 6611556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 6621556Srgrimes cmdentry.u.index = BLTINCMD; 6631556Srgrimes } else { 66417987Speter static const char PATH[] = "PATH="; 66517987Speter char *path = pathval(); 66617987Speter 66717987Speter /* 66817987Speter * Modify the command lookup path, if a PATH= assignment 66917987Speter * is present 67017987Speter */ 67117987Speter for (sp = varlist.list ; sp ; sp = sp->next) 67254884Scracauer if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 67317987Speter path = sp->text + sizeof(PATH) - 1; 674155301Sschweikh /* 67554884Scracauer * On `PATH=... command`, we need to make 67654884Scracauer * sure that the command isn't using the 67754884Scracauer * non-updated hash table of the outer PATH 678155301Sschweikh * setting and we need to make sure that 67954884Scracauer * the hash table isn't filled with items 68054884Scracauer * from the temporary setting. 68154884Scracauer * 682155301Sschweikh * It would be better to forbit using and 68354884Scracauer * updating the table while this command 68454884Scracauer * runs, by the command finding mechanism 68554884Scracauer * is heavily integrated with hash handling, 68654884Scracauer * so we just delete the hash before and after 68754884Scracauer * the command runs. Partly deleting like 68854884Scracauer * changepatch() does doesn't seem worth the 68954884Scracauer * bookinging effort, since most such runs add 690123996Smaxim * directories in front of the new PATH. 69154884Scracauer */ 69254884Scracauer clearcmdentry(0); 69354884Scracauer do_clearcmdentry = 1; 69454884Scracauer } 69517987Speter 69617987Speter find_command(argv[0], &cmdentry, 1, path); 6971556Srgrimes if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */ 69820425Ssteve exitstatus = 127; 6991556Srgrimes flushout(&errout); 7001556Srgrimes return; 7011556Srgrimes } 7021556Srgrimes /* implement the bltin builtin here */ 7031556Srgrimes if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) { 7041556Srgrimes for (;;) { 7051556Srgrimes argv++; 7061556Srgrimes if (--argc == 0) 7071556Srgrimes break; 7081556Srgrimes if ((cmdentry.u.index = find_builtin(*argv)) < 0) { 7091556Srgrimes outfmt(&errout, "%s: not found\n", *argv); 71020425Ssteve exitstatus = 127; 7111556Srgrimes flushout(&errout); 7121556Srgrimes return; 7131556Srgrimes } 7141556Srgrimes if (cmdentry.u.index != BLTINCMD) 7151556Srgrimes break; 7161556Srgrimes } 7171556Srgrimes } 7181556Srgrimes } 7191556Srgrimes 7201556Srgrimes /* Fork off a child process if necessary. */ 7211556Srgrimes if (cmd->ncmd.backgnd 72245221Scracauer || (cmdentry.cmdtype == CMDNORMAL 72345221Scracauer && ((flags & EV_EXIT) == 0 || Tflag)) 72417987Speter || ((flags & EV_BACKCMD) != 0 7251556Srgrimes && (cmdentry.cmdtype != CMDBUILTIN 72648896Ssheldonh || cmdentry.u.index == CDCMD 7271556Srgrimes || cmdentry.u.index == DOTCMD 728100437Stjr || cmdentry.u.index == EVALCMD)) 729100437Stjr || (cmdentry.cmdtype == CMDBUILTIN && 730100437Stjr cmdentry.u.index == COMMANDCMD)) { 7311556Srgrimes jp = makejob(cmd, 1); 7321556Srgrimes mode = cmd->ncmd.backgnd; 7331556Srgrimes if (flags & EV_BACKCMD) { 7341556Srgrimes mode = FORK_NOJOB; 7351556Srgrimes if (pipe(pip) < 0) 73653891Scracauer error("Pipe call failed: %s", strerror(errno)); 7371556Srgrimes } 7381556Srgrimes if (forkshell(jp, cmd, mode) != 0) 7391556Srgrimes goto parent; /* at end of routine */ 7401556Srgrimes if (flags & EV_BACKCMD) { 7411556Srgrimes FORCEINTON; 7421556Srgrimes close(pip[0]); 7431556Srgrimes if (pip[1] != 1) { 744124780Sdes dup2(pip[1], 1); 7451556Srgrimes close(pip[1]); 7461556Srgrimes } 7471556Srgrimes } 7481556Srgrimes flags |= EV_EXIT; 7491556Srgrimes } 7501556Srgrimes 7511556Srgrimes /* This is the child process if a fork occurred. */ 7521556Srgrimes /* Execute the command. */ 7531556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 75420425Ssteve#ifdef DEBUG 7551556Srgrimes trputs("Shell function: "); trargs(argv); 75620425Ssteve#endif 7571556Srgrimes redirect(cmd->ncmd.redirect, REDIR_PUSH); 7581556Srgrimes saveparam = shellparam; 7591556Srgrimes shellparam.malloc = 0; 76020425Ssteve shellparam.reset = 1; 7611556Srgrimes shellparam.nparam = argc - 1; 7621556Srgrimes shellparam.p = argv + 1; 7631556Srgrimes shellparam.optnext = NULL; 7641556Srgrimes INTOFF; 7651556Srgrimes savelocalvars = localvars; 7661556Srgrimes localvars = NULL; 7671556Srgrimes INTON; 7681556Srgrimes if (setjmp(jmploc.loc)) { 7691556Srgrimes if (exception == EXSHELLPROC) 7701556Srgrimes freeparam((struct shparam *)&saveparam); 7711556Srgrimes else { 7721556Srgrimes freeparam(&shellparam); 7731556Srgrimes shellparam = saveparam; 7741556Srgrimes } 7751556Srgrimes poplocalvars(); 7761556Srgrimes localvars = savelocalvars; 7771556Srgrimes handler = savehandler; 7781556Srgrimes longjmp(handler->loc, 1); 7791556Srgrimes } 7801556Srgrimes savehandler = handler; 7811556Srgrimes handler = &jmploc; 7821556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 7831556Srgrimes mklocal(sp->text); 7841556Srgrimes funcnest++; 78535675Scracauer if (flags & EV_TESTED) 78635675Scracauer evaltree(cmdentry.u.func, EV_TESTED); 78735675Scracauer else 78835675Scracauer evaltree(cmdentry.u.func, 0); 7891556Srgrimes funcnest--; 7901556Srgrimes INTOFF; 7911556Srgrimes poplocalvars(); 7921556Srgrimes localvars = savelocalvars; 7931556Srgrimes freeparam(&shellparam); 7941556Srgrimes shellparam = saveparam; 7951556Srgrimes handler = savehandler; 7961556Srgrimes popredir(); 7971556Srgrimes INTON; 7981556Srgrimes if (evalskip == SKIPFUNC) { 7991556Srgrimes evalskip = 0; 8001556Srgrimes skipcount = 0; 8011556Srgrimes } 8021556Srgrimes if (flags & EV_EXIT) 8031556Srgrimes exitshell(exitstatus); 8041556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 80520425Ssteve#ifdef DEBUG 8061556Srgrimes trputs("builtin command: "); trargs(argv); 80720425Ssteve#endif 8081556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 8091556Srgrimes if (flags == EV_BACKCMD) { 8101556Srgrimes memout.nleft = 0; 8111556Srgrimes memout.nextc = memout.buf; 8121556Srgrimes memout.bufsize = 64; 8131556Srgrimes mode |= REDIR_BACKQ; 8141556Srgrimes } 8151556Srgrimes redirect(cmd->ncmd.redirect, mode); 8161556Srgrimes savecmdname = commandname; 8171556Srgrimes cmdenviron = varlist.list; 8181556Srgrimes e = -1; 8191556Srgrimes if (setjmp(jmploc.loc)) { 8201556Srgrimes e = exception; 8211556Srgrimes exitstatus = (e == EXINT)? SIGINT+128 : 2; 8221556Srgrimes goto cmddone; 8231556Srgrimes } 8241556Srgrimes savehandler = handler; 8251556Srgrimes handler = &jmploc; 8261556Srgrimes commandname = argv[0]; 8271556Srgrimes argptr = argv + 1; 8281556Srgrimes optptr = NULL; /* initialize nextopt */ 8291556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 8301556Srgrimes flushall(); 8311556Srgrimescmddone: 83260592Scracauer cmdenviron = NULL; 8331556Srgrimes out1 = &output; 8341556Srgrimes out2 = &errout; 8351556Srgrimes freestdout(); 8361556Srgrimes if (e != EXSHELLPROC) { 8371556Srgrimes commandname = savecmdname; 8381556Srgrimes if (flags & EV_EXIT) { 8391556Srgrimes exitshell(exitstatus); 8401556Srgrimes } 8411556Srgrimes } 8421556Srgrimes handler = savehandler; 8431556Srgrimes if (e != -1) { 84420425Ssteve if ((e != EXERROR && e != EXEXEC) 84520425Ssteve || cmdentry.u.index == BLTINCMD 84620425Ssteve || cmdentry.u.index == DOTCMD 84720425Ssteve || cmdentry.u.index == EVALCMD 84817987Speter#ifndef NO_HISTORY 84920425Ssteve || cmdentry.u.index == HISTCMD 85017987Speter#endif 851100437Stjr || cmdentry.u.index == EXECCMD 852100437Stjr || cmdentry.u.index == COMMANDCMD) 8531556Srgrimes exraise(e); 8541556Srgrimes FORCEINTON; 8551556Srgrimes } 8561556Srgrimes if (cmdentry.u.index != EXECCMD) 8571556Srgrimes popredir(); 8581556Srgrimes if (flags == EV_BACKCMD) { 8591556Srgrimes backcmd->buf = memout.buf; 8601556Srgrimes backcmd->nleft = memout.nextc - memout.buf; 8611556Srgrimes memout.buf = NULL; 8621556Srgrimes } 8631556Srgrimes } else { 86420425Ssteve#ifdef DEBUG 8651556Srgrimes trputs("normal command: "); trargs(argv); 86620425Ssteve#endif 8671556Srgrimes clearredir(); 8681556Srgrimes redirect(cmd->ncmd.redirect, 0); 8691556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8701556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 8711556Srgrimes envp = environment(); 87217987Speter shellexec(argv, envp, pathval(), cmdentry.u.index); 8731556Srgrimes /*NOTREACHED*/ 8741556Srgrimes } 8751556Srgrimes goto out; 8761556Srgrimes 8771556Srgrimesparent: /* parent process gets here (if we forked) */ 8781556Srgrimes if (mode == 0) { /* argument to fork */ 8791556Srgrimes INTOFF; 88045916Scracauer exitstatus = waitforjob(jp, &realstatus); 8811556Srgrimes INTON; 88245916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 88345266Scracauer evalskip = SKIPBREAK; 88445266Scracauer skipcount = loopnest; 88545266Scracauer } 8861556Srgrimes } else if (mode == 2) { 8871556Srgrimes backcmd->fd = pip[0]; 8881556Srgrimes close(pip[1]); 8891556Srgrimes backcmd->jp = jp; 8901556Srgrimes } 8911556Srgrimes 8921556Srgrimesout: 8931556Srgrimes if (lastarg) 8941556Srgrimes setvar("_", lastarg, 0); 89554884Scracauer if (do_clearcmdentry) 89654884Scracauer clearcmdentry(0); 8971556Srgrimes popstackmark(&smark); 8981556Srgrimes} 8991556Srgrimes 9001556Srgrimes 9011556Srgrimes 9021556Srgrimes/* 9031556Srgrimes * Search for a command. This is called before we fork so that the 9041556Srgrimes * location of the command will be available in the parent as well as 9051556Srgrimes * the child. The check for "goodname" is an overly conservative 9061556Srgrimes * check that the name will not be subject to expansion. 9071556Srgrimes */ 9081556Srgrimes 9091556SrgrimesSTATIC void 91090111Simpprehash(union node *n) 91117987Speter{ 9121556Srgrimes struct cmdentry entry; 9131556Srgrimes 91417987Speter if (n->type == NCMD && n->ncmd.args) 91517987Speter if (goodname(n->ncmd.args->narg.text)) 91617987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 91717987Speter pathval()); 9181556Srgrimes} 9191556Srgrimes 9201556Srgrimes 9211556Srgrimes 9221556Srgrimes/* 9231556Srgrimes * Builtin commands. Builtin commands whose functions are closely 9241556Srgrimes * tied to evaluation are implemented here. 9251556Srgrimes */ 9261556Srgrimes 9271556Srgrimes/* 9281556Srgrimes * No command given, or a bltin command with no arguments. Set the 9291556Srgrimes * specified variables. 9301556Srgrimes */ 9311556Srgrimes 93217987Speterint 93390111Simpbltincmd(int argc __unused, char **argv __unused) 93417987Speter{ 9351556Srgrimes listsetvar(cmdenviron); 93620425Ssteve /* 93717987Speter * Preserve exitstatus of a previous possible redirection 93820425Ssteve * as POSIX mandates 93917987Speter */ 9401556Srgrimes return exitstatus; 9411556Srgrimes} 9421556Srgrimes 9431556Srgrimes 9441556Srgrimes/* 9451556Srgrimes * Handle break and continue commands. Break, continue, and return are 9461556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 9471556Srgrimes * above all check this flag, and if it is set they start skipping 9481556Srgrimes * commands rather than executing them. The variable skipcount is 9491556Srgrimes * the number of loops to break/continue, or the number of function 9501556Srgrimes * levels to return. (The latter is always 1.) It should probably 9511556Srgrimes * be an error to break out of more loops than exist, but it isn't 9521556Srgrimes * in the standard shell so we don't make it one here. 9531556Srgrimes */ 9541556Srgrimes 95517987Speterint 95690111Simpbreakcmd(int argc, char **argv) 95717987Speter{ 95820425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 9591556Srgrimes 9601556Srgrimes if (n > loopnest) 9611556Srgrimes n = loopnest; 9621556Srgrimes if (n > 0) { 9631556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 9641556Srgrimes skipcount = n; 9651556Srgrimes } 9661556Srgrimes return 0; 9671556Srgrimes} 9681556Srgrimes 969100437Stjr/* 970100437Stjr * The `command' command. 971100437Stjr */ 972100437Stjrint 973100437Stjrcommandcmd(int argc, char **argv) 974100437Stjr{ 975100437Stjr static char stdpath[] = _PATH_STDPATH; 976100437Stjr struct jmploc loc, *old; 977100437Stjr struct strlist *sp; 978100437Stjr char *path; 979100437Stjr int ch; 980151810Sstefanf int cmd = -1; 9811556Srgrimes 982100437Stjr for (sp = cmdenviron; sp ; sp = sp->next) 983100437Stjr setvareq(sp->text, VEXPORT|VSTACK); 984100437Stjr path = pathval(); 985100437Stjr 986100437Stjr optind = optreset = 1; 987100663Stjr opterr = 0; 988151810Sstefanf while ((ch = getopt(argc, argv, "pvV")) != -1) { 989100437Stjr switch (ch) { 990100437Stjr case 'p': 991100437Stjr path = stdpath; 992100437Stjr break; 993151810Sstefanf case 'v': 994151810Sstefanf cmd = TYPECMD_SMALLV; 995151810Sstefanf break; 996151810Sstefanf case 'V': 997151810Sstefanf cmd = TYPECMD_BIGV; 998151810Sstefanf break; 999100437Stjr case '?': 1000100437Stjr default: 1001100437Stjr error("unknown option: -%c", optopt); 1002100437Stjr } 1003100437Stjr } 1004100437Stjr argc -= optind; 1005100437Stjr argv += optind; 1006100437Stjr 1007151810Sstefanf if (cmd != -1) { 1008151810Sstefanf if (argc != 1) 1009151810Sstefanf error("wrong number of arguments"); 1010151810Sstefanf return typecmd_impl(2, argv - 1, cmd); 1011151810Sstefanf } 1012100437Stjr if (argc != 0) { 1013100437Stjr old = handler; 1014100437Stjr handler = &loc; 1015100437Stjr if (setjmp(handler->loc) == 0) 1016100437Stjr shellexec(argv, environment(), path, 0); 1017100437Stjr handler = old; 1018100437Stjr if (exception == EXEXEC) 1019100437Stjr exit(exerrno); 1020100437Stjr exraise(exception); 1021100437Stjr } 1022100437Stjr 1023100437Stjr /* 1024100437Stjr * Do nothing successfully if no command was specified; 1025100437Stjr * ksh also does this. 1026100437Stjr */ 1027100437Stjr exit(0); 1028100437Stjr} 1029100437Stjr 1030100437Stjr 10311556Srgrimes/* 10321556Srgrimes * The return command. 10331556Srgrimes */ 10341556Srgrimes 103517987Speterint 103690111Simpreturncmd(int argc, char **argv) 103717987Speter{ 103820425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 10391556Srgrimes 10401556Srgrimes if (funcnest) { 10411556Srgrimes evalskip = SKIPFUNC; 10421556Srgrimes skipcount = 1; 104320425Ssteve } else { 104420425Ssteve /* skip the rest of the file */ 104520425Ssteve evalskip = SKIPFILE; 104620425Ssteve skipcount = 1; 10471556Srgrimes } 10481556Srgrimes return ret; 10491556Srgrimes} 10501556Srgrimes 10511556Srgrimes 105217987Speterint 105390111Simpfalsecmd(int argc __unused, char **argv __unused) 105417987Speter{ 105517987Speter return 1; 105617987Speter} 105717987Speter 105817987Speter 105917987Speterint 106090111Simptruecmd(int argc __unused, char **argv __unused) 106117987Speter{ 10621556Srgrimes return 0; 10631556Srgrimes} 10641556Srgrimes 10651556Srgrimes 106617987Speterint 106790111Simpexeccmd(int argc, char **argv) 106817987Speter{ 10691556Srgrimes if (argc > 1) { 107017987Speter struct strlist *sp; 107117987Speter 10721556Srgrimes iflag = 0; /* exit on error */ 10731556Srgrimes mflag = 0; 10741556Srgrimes optschanged(); 107517987Speter for (sp = cmdenviron; sp ; sp = sp->next) 107617987Speter setvareq(sp->text, VEXPORT|VSTACK); 10771556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 10781556Srgrimes 10791556Srgrimes } 10801556Srgrimes return 0; 10811556Srgrimes} 1082153091Sstefanf 1083153091Sstefanf 1084153091Sstefanfint 1085153091Sstefanftimescmd(int argc __unused, char **argv __unused) 1086153091Sstefanf{ 1087153091Sstefanf struct rusage ru; 1088153091Sstefanf long shumins, shsmins, chumins, chsmins; 1089153091Sstefanf double shusecs, shssecs, chusecs, chssecs; 1090153091Sstefanf 1091153091Sstefanf if (getrusage(RUSAGE_SELF, &ru) < 0) 1092153091Sstefanf return 1; 1093153091Sstefanf shumins = ru.ru_utime.tv_sec / 60; 1094153091Sstefanf shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1095153091Sstefanf shsmins = ru.ru_stime.tv_sec / 60; 1096153091Sstefanf shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1097153091Sstefanf if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1098153091Sstefanf return 1; 1099153091Sstefanf chumins = ru.ru_utime.tv_sec / 60; 1100153091Sstefanf chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1101153091Sstefanf chsmins = ru.ru_stime.tv_sec / 60; 1102153091Sstefanf chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1103153091Sstefanf out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1104153091Sstefanf shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1105153091Sstefanf return 0; 1106153091Sstefanf} 1107