eval.c revision 207783
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 207783 2010-05-08 14:00:01Z jilles $"); 401556Srgrimes 41100437Stjr#include <paths.h> 4217987Speter#include <signal.h> 43102576Skeramida#include <stdlib.h> 4417987Speter#include <unistd.h> 45153091Sstefanf#include <sys/resource.h> 4645266Scracauer#include <sys/wait.h> /* For WIFSIGNALED(status) */ 4753891Scracauer#include <errno.h> 4817987Speter 491556Srgrimes/* 501556Srgrimes * Evaluate a command. 511556Srgrimes */ 521556Srgrimes 531556Srgrimes#include "shell.h" 541556Srgrimes#include "nodes.h" 551556Srgrimes#include "syntax.h" 561556Srgrimes#include "expand.h" 571556Srgrimes#include "parser.h" 581556Srgrimes#include "jobs.h" 591556Srgrimes#include "eval.h" 601556Srgrimes#include "builtins.h" 611556Srgrimes#include "options.h" 621556Srgrimes#include "exec.h" 631556Srgrimes#include "redir.h" 641556Srgrimes#include "input.h" 651556Srgrimes#include "output.h" 661556Srgrimes#include "trap.h" 671556Srgrimes#include "var.h" 681556Srgrimes#include "memalloc.h" 691556Srgrimes#include "error.h" 7017987Speter#include "show.h" 711556Srgrimes#include "mystring.h" 7217987Speter#ifndef NO_HISTORY 731556Srgrimes#include "myhistedit.h" 7417987Speter#endif 751556Srgrimes 761556Srgrimes 77201053Sjillesint evalskip; /* set if we are skipping commands */ 781556SrgrimesSTATIC int skipcount; /* number of levels to skip */ 791556SrgrimesMKINIT int loopnest; /* current loop nesting level */ 801556Srgrimesint funcnest; /* depth of function calls */ 81193169SstefanfSTATIC 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 90149933SstefanfSTATIC void evalloop(union node *, int); 91149933SstefanfSTATIC void evalfor(union node *, int); 9290111SimpSTATIC void evalcase(union node *, int); 9390111SimpSTATIC void evalsubshell(union node *, int); 94205154SjillesSTATIC void evalredir(union node *, int); 9590111SimpSTATIC void expredir(union node *); 9690111SimpSTATIC void evalpipe(union node *); 9790111SimpSTATIC void evalcommand(union node *, int, struct backcmd *); 9890111SimpSTATIC 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); 1481556Srgrimes } 1491556Srgrimes return exitstatus; 1501556Srgrimes} 1511556Srgrimes 1521556Srgrimes 1531556Srgrimes/* 1541556Srgrimes * Execute a command or commands contained in a string. 1551556Srgrimes */ 1561556Srgrimes 1571556Srgrimesvoid 158193169Sstefanfevalstring(char *s, int flags) 15990111Simp{ 1601556Srgrimes union node *n; 1611556Srgrimes struct stackmark smark; 162194128Sjilles int flags_exit; 1631556Srgrimes 164194128Sjilles flags_exit = flags & EV_EXIT; 165194128Sjilles flags &= ~EV_EXIT; 1661556Srgrimes setstackmark(&smark); 1671556Srgrimes setinputstring(s, 1); 1681556Srgrimes while ((n = parsecmd(0)) != NEOF) { 169194128Sjilles if (n != NULL) { 170194128Sjilles if (flags_exit && preadateof()) 171194128Sjilles evaltree(n, flags | EV_EXIT); 172194128Sjilles else 173194128Sjilles evaltree(n, flags); 174194128Sjilles } 1751556Srgrimes popstackmark(&smark); 1761556Srgrimes } 1771556Srgrimes popfile(); 1781556Srgrimes popstackmark(&smark); 179194128Sjilles if (flags_exit) 180194128Sjilles exitshell(exitstatus); 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; 193149927Sstefanf 194149927Sstefanf do_etest = 0; 1951556Srgrimes if (n == NULL) { 1961556Srgrimes TRACE(("evaltree(NULL) called\n")); 1971556Srgrimes exitstatus = 0; 1981556Srgrimes goto out; 1991556Srgrimes } 20017987Speter#ifndef NO_HISTORY 2011556Srgrimes displayhist = 1; /* show history substitutions done with fc */ 20217987Speter#endif 203149802Sstefanf TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type)); 2041556Srgrimes switch (n->type) { 2051556Srgrimes case NSEMI: 206149932Sstefanf evaltree(n->nbinary.ch1, flags & ~EV_EXIT); 2071556Srgrimes if (evalskip) 2081556Srgrimes goto out; 2091556Srgrimes evaltree(n->nbinary.ch2, flags); 2101556Srgrimes break; 2111556Srgrimes case NAND: 2121556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 21318754Ssteve if (evalskip || exitstatus != 0) { 2141556Srgrimes goto out; 21518754Ssteve } 2161556Srgrimes evaltree(n->nbinary.ch2, flags); 2171556Srgrimes break; 2181556Srgrimes case NOR: 2191556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2201556Srgrimes if (evalskip || exitstatus == 0) 2211556Srgrimes goto out; 2221556Srgrimes evaltree(n->nbinary.ch2, flags); 2231556Srgrimes break; 2241556Srgrimes case NREDIR: 225205154Sjilles evalredir(n, flags); 2261556Srgrimes break; 2271556Srgrimes case NSUBSHELL: 2281556Srgrimes evalsubshell(n, flags); 229149927Sstefanf do_etest = !(flags & EV_TESTED); 2301556Srgrimes break; 2311556Srgrimes case NBACKGND: 2321556Srgrimes evalsubshell(n, flags); 2331556Srgrimes break; 2341556Srgrimes case NIF: { 2351556Srgrimes evaltree(n->nif.test, EV_TESTED); 2361556Srgrimes if (evalskip) 2371556Srgrimes goto out; 23820425Ssteve if (exitstatus == 0) 2391556Srgrimes evaltree(n->nif.ifpart, flags); 24017987Speter else if (n->nif.elsepart) 2411556Srgrimes evaltree(n->nif.elsepart, flags); 24220425Ssteve else 24320425Ssteve exitstatus = 0; 2441556Srgrimes break; 2451556Srgrimes } 2461556Srgrimes case NWHILE: 2471556Srgrimes case NUNTIL: 248149933Sstefanf evalloop(n, flags & ~EV_EXIT); 2491556Srgrimes break; 2501556Srgrimes case NFOR: 251149933Sstefanf evalfor(n, flags & ~EV_EXIT); 2521556Srgrimes break; 2531556Srgrimes case NCASE: 2541556Srgrimes evalcase(n, flags); 2551556Srgrimes break; 2561556Srgrimes case NDEFUN: 2571556Srgrimes defun(n->narg.text, n->narg.next); 2581556Srgrimes exitstatus = 0; 2591556Srgrimes break; 2601556Srgrimes case NNOT: 2611556Srgrimes evaltree(n->nnot.com, EV_TESTED); 2621556Srgrimes exitstatus = !exitstatus; 2631556Srgrimes break; 2641556Srgrimes 2651556Srgrimes case NPIPE: 2661556Srgrimes evalpipe(n); 267149927Sstefanf do_etest = !(flags & EV_TESTED); 2681556Srgrimes break; 2691556Srgrimes case NCMD: 2701556Srgrimes evalcommand(n, flags, (struct backcmd *)NULL); 271149927Sstefanf do_etest = !(flags & EV_TESTED); 2721556Srgrimes break; 2731556Srgrimes default: 2741556Srgrimes out1fmt("Node type = %d\n", n->type); 2751556Srgrimes flushout(&output); 2761556Srgrimes break; 2771556Srgrimes } 2781556Srgrimesout: 2791556Srgrimes if (pendingsigs) 2801556Srgrimes dotrap(); 281149927Sstefanf if ((flags & EV_EXIT) || (eflag && exitstatus != 0 && do_etest)) 2821556Srgrimes exitshell(exitstatus); 2831556Srgrimes} 2841556Srgrimes 2851556Srgrimes 2861556SrgrimesSTATIC void 287149933Sstefanfevalloop(union node *n, int flags) 28817987Speter{ 2891556Srgrimes int status; 2901556Srgrimes 2911556Srgrimes loopnest++; 2921556Srgrimes status = 0; 2931556Srgrimes for (;;) { 2941556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2951556Srgrimes if (evalskip) { 2961556Srgrimesskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 2971556Srgrimes evalskip = 0; 2981556Srgrimes continue; 2991556Srgrimes } 3001556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3011556Srgrimes evalskip = 0; 3021556Srgrimes break; 3031556Srgrimes } 3041556Srgrimes if (n->type == NWHILE) { 3051556Srgrimes if (exitstatus != 0) 3061556Srgrimes break; 3071556Srgrimes } else { 3081556Srgrimes if (exitstatus == 0) 3091556Srgrimes break; 3101556Srgrimes } 311149933Sstefanf evaltree(n->nbinary.ch2, flags); 3121556Srgrimes status = exitstatus; 3131556Srgrimes if (evalskip) 3141556Srgrimes goto skipping; 3151556Srgrimes } 3161556Srgrimes loopnest--; 3171556Srgrimes exitstatus = status; 3181556Srgrimes} 3191556Srgrimes 3201556Srgrimes 3211556Srgrimes 3221556SrgrimesSTATIC void 323149933Sstefanfevalfor(union node *n, int flags) 32417987Speter{ 3251556Srgrimes struct arglist arglist; 3261556Srgrimes union node *argp; 3271556Srgrimes struct strlist *sp; 3281556Srgrimes struct stackmark smark; 3291556Srgrimes 3301556Srgrimes setstackmark(&smark); 3311556Srgrimes arglist.lastp = &arglist.list; 3321556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 33317987Speter oexitstatus = exitstatus; 3341556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3351556Srgrimes if (evalskip) 3361556Srgrimes goto out; 3371556Srgrimes } 3381556Srgrimes *arglist.lastp = NULL; 3391556Srgrimes 3401556Srgrimes exitstatus = 0; 3411556Srgrimes loopnest++; 3421556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3431556Srgrimes setvar(n->nfor.var, sp->text, 0); 344149933Sstefanf evaltree(n->nfor.body, flags); 3451556Srgrimes if (evalskip) { 3461556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3471556Srgrimes evalskip = 0; 3481556Srgrimes continue; 3491556Srgrimes } 3501556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3511556Srgrimes evalskip = 0; 3521556Srgrimes break; 3531556Srgrimes } 3541556Srgrimes } 3551556Srgrimes loopnest--; 3561556Srgrimesout: 3571556Srgrimes popstackmark(&smark); 3581556Srgrimes} 3591556Srgrimes 3601556Srgrimes 3611556Srgrimes 3621556SrgrimesSTATIC void 36390111Simpevalcase(union node *n, int flags) 36417987Speter{ 3651556Srgrimes union node *cp; 3661556Srgrimes union node *patp; 3671556Srgrimes struct arglist arglist; 3681556Srgrimes struct stackmark smark; 3691556Srgrimes 3701556Srgrimes setstackmark(&smark); 3711556Srgrimes arglist.lastp = &arglist.list; 37217987Speter oexitstatus = exitstatus; 373172440Sstefanf exitstatus = 0; 3741556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3751556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3761556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3771556Srgrimes if (casematch(patp, arglist.list->text)) { 3781556Srgrimes if (evalskip == 0) { 3791556Srgrimes evaltree(cp->nclist.body, flags); 3801556Srgrimes } 3811556Srgrimes goto out; 3821556Srgrimes } 3831556Srgrimes } 3841556Srgrimes } 3851556Srgrimesout: 3861556Srgrimes popstackmark(&smark); 3871556Srgrimes} 3881556Srgrimes 3891556Srgrimes 3901556Srgrimes 3911556Srgrimes/* 3921556Srgrimes * Kick off a subshell to evaluate a tree. 3931556Srgrimes */ 3941556Srgrimes 3951556SrgrimesSTATIC void 39690111Simpevalsubshell(union node *n, int flags) 39717987Speter{ 3981556Srgrimes struct job *jp; 3991556Srgrimes int backgnd = (n->type == NBACKGND); 4001556Srgrimes 4011556Srgrimes expredir(n->nredir.redirect); 402194774Sjilles if ((!backgnd && flags & EV_EXIT && !have_traps()) || 403194774Sjilles forkshell(jp = makejob(n, 1), n, backgnd) == 0) { 4041556Srgrimes if (backgnd) 4051556Srgrimes flags &=~ EV_TESTED; 4061556Srgrimes redirect(n->nredir.redirect, 0); 4071556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 408201053Sjilles } else if (! backgnd) { 4091556Srgrimes INTOFF; 41045916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4111556Srgrimes INTON; 4121556Srgrimes } 4131556Srgrimes} 4141556Srgrimes 4151556Srgrimes 416205154Sjilles/* 417205154Sjilles * Evaluate a redirected compound command. 418205154Sjilles */ 4191556Srgrimes 420205154SjillesSTATIC void 421205154Sjillesevalredir(union node *n, int flags) 422205154Sjilles{ 423205154Sjilles struct jmploc jmploc; 424205154Sjilles struct jmploc *savehandler; 425205154Sjilles volatile int in_redirect = 1; 426205154Sjilles 427205154Sjilles expredir(n->nredir.redirect); 428205154Sjilles savehandler = handler; 429205154Sjilles if (setjmp(jmploc.loc)) { 430205154Sjilles int e; 431205154Sjilles 432205154Sjilles handler = savehandler; 433205154Sjilles e = exception; 434205154Sjilles if (e == EXERROR || e == EXEXEC) { 435205154Sjilles popredir(); 436205154Sjilles if (in_redirect) { 437205154Sjilles exitstatus = 2; 438205154Sjilles return; 439205154Sjilles } 440205154Sjilles } 441205154Sjilles longjmp(handler->loc, 1); 442205154Sjilles } else { 443205154Sjilles INTOFF; 444205154Sjilles handler = &jmploc; 445205154Sjilles redirect(n->nredir.redirect, REDIR_PUSH); 446205154Sjilles in_redirect = 0; 447205154Sjilles INTON; 448205154Sjilles evaltree(n->nredir.n, flags); 449205154Sjilles } 450205154Sjilles INTOFF; 451205154Sjilles handler = savehandler; 452205154Sjilles popredir(); 453205154Sjilles INTON; 454205154Sjilles} 455205154Sjilles 456205154Sjilles 4571556Srgrimes/* 4581556Srgrimes * Compute the names of the files in a redirection list. 4591556Srgrimes */ 4601556Srgrimes 4611556SrgrimesSTATIC void 46290111Simpexpredir(union node *n) 46317987Speter{ 46425222Ssteve union node *redir; 4651556Srgrimes 4661556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 46717987Speter struct arglist fn; 46817987Speter fn.lastp = &fn.list; 46917987Speter oexitstatus = exitstatus; 47017987Speter switch (redir->type) { 47117987Speter case NFROM: 47217987Speter case NTO: 47366612Sbrian case NFROMTO: 47417987Speter case NAPPEND: 47596922Stjr case NCLOBBER: 4761556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4771556Srgrimes redir->nfile.expfname = fn.list->text; 47817987Speter break; 47917987Speter case NFROMFD: 48017987Speter case NTOFD: 48117987Speter if (redir->ndup.vname) { 482181017Sstefanf expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); 48317987Speter fixredir(redir, fn.list->text, 1); 48417987Speter } 48517987Speter break; 4861556Srgrimes } 4871556Srgrimes } 4881556Srgrimes} 4891556Srgrimes 4901556Srgrimes 4911556Srgrimes 4921556Srgrimes/* 4931556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 4941556Srgrimes * of the process creating the pipeline. (This differs from some versions 4951556Srgrimes * of the shell, which make the last process in a pipeline the parent 4961556Srgrimes * of all the rest.) 4971556Srgrimes */ 4981556Srgrimes 4991556SrgrimesSTATIC void 50090111Simpevalpipe(union node *n) 50117987Speter{ 5021556Srgrimes struct job *jp; 5031556Srgrimes struct nodelist *lp; 5041556Srgrimes int pipelen; 5051556Srgrimes int prevfd; 5061556Srgrimes int pip[2]; 5071556Srgrimes 508149802Sstefanf TRACE(("evalpipe(%p) called\n", (void *)n)); 5091556Srgrimes pipelen = 0; 5101556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 5111556Srgrimes pipelen++; 5121556Srgrimes INTOFF; 5131556Srgrimes jp = makejob(n, pipelen); 5141556Srgrimes prevfd = -1; 5151556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 5161556Srgrimes prehash(lp->n); 5171556Srgrimes pip[1] = -1; 5181556Srgrimes if (lp->next) { 5191556Srgrimes if (pipe(pip) < 0) { 5201556Srgrimes close(prevfd); 52153891Scracauer error("Pipe call failed: %s", strerror(errno)); 5221556Srgrimes } 5231556Srgrimes } 5241556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 5251556Srgrimes INTON; 5261556Srgrimes if (prevfd > 0) { 527124780Sdes dup2(prevfd, 0); 5281556Srgrimes close(prevfd); 5291556Srgrimes } 5301556Srgrimes if (pip[1] >= 0) { 53153282Scracauer if (!(prevfd >= 0 && pip[0] == 0)) 53252900Scracauer close(pip[0]); 5331556Srgrimes if (pip[1] != 1) { 534124780Sdes dup2(pip[1], 1); 5351556Srgrimes close(pip[1]); 5361556Srgrimes } 5371556Srgrimes } 5381556Srgrimes evaltree(lp->n, EV_EXIT); 5391556Srgrimes } 5401556Srgrimes if (prevfd >= 0) 5411556Srgrimes close(prevfd); 5421556Srgrimes prevfd = pip[0]; 5431556Srgrimes close(pip[1]); 5441556Srgrimes } 5451556Srgrimes INTON; 5461556Srgrimes if (n->npipe.backgnd == 0) { 5471556Srgrimes INTOFF; 54845916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 5491556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5501556Srgrimes INTON; 5511556Srgrimes } 5521556Srgrimes} 5531556Srgrimes 5541556Srgrimes 5551556Srgrimes 5561556Srgrimes/* 5571556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5581556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5591556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5601556Srgrimes * Should be called with interrupts off. 5611556Srgrimes */ 5621556Srgrimes 5631556Srgrimesvoid 56490111Simpevalbackcmd(union node *n, struct backcmd *result) 56517987Speter{ 5661556Srgrimes int pip[2]; 5671556Srgrimes struct job *jp; 5681556Srgrimes struct stackmark smark; /* unnecessary */ 5691556Srgrimes 5701556Srgrimes setstackmark(&smark); 5711556Srgrimes result->fd = -1; 5721556Srgrimes result->buf = NULL; 5731556Srgrimes result->nleft = 0; 5741556Srgrimes result->jp = NULL; 57517987Speter if (n == NULL) { 57617987Speter exitstatus = 0; 5771556Srgrimes goto out; 57817987Speter } 5791556Srgrimes if (n->type == NCMD) { 58017987Speter exitstatus = oexitstatus; 5811556Srgrimes evalcommand(n, EV_BACKCMD, result); 5821556Srgrimes } else { 58317987Speter exitstatus = 0; 5841556Srgrimes if (pipe(pip) < 0) 58553891Scracauer error("Pipe call failed: %s", strerror(errno)); 5861556Srgrimes jp = makejob(n, 1); 5871556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 5881556Srgrimes FORCEINTON; 5891556Srgrimes close(pip[0]); 5901556Srgrimes if (pip[1] != 1) { 591124780Sdes dup2(pip[1], 1); 5921556Srgrimes close(pip[1]); 5931556Srgrimes } 5941556Srgrimes evaltree(n, EV_EXIT); 5951556Srgrimes } 5961556Srgrimes close(pip[1]); 5971556Srgrimes result->fd = pip[0]; 5981556Srgrimes result->jp = jp; 5991556Srgrimes } 6001556Srgrimesout: 6011556Srgrimes popstackmark(&smark); 602109627Stjr TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n", 6031556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 6041556Srgrimes} 6051556Srgrimes 6061556Srgrimes 6071556Srgrimes 6081556Srgrimes/* 6091556Srgrimes * Execute a simple command. 6101556Srgrimes */ 6111556Srgrimes 6121556SrgrimesSTATIC void 61390111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd) 61417987Speter{ 6151556Srgrimes struct stackmark smark; 6161556Srgrimes union node *argp; 6171556Srgrimes struct arglist arglist; 6181556Srgrimes struct arglist varlist; 6191556Srgrimes char **argv; 6201556Srgrimes int argc; 6211556Srgrimes char **envp; 6221556Srgrimes int varflag; 6231556Srgrimes struct strlist *sp; 6241556Srgrimes int mode; 6251556Srgrimes int pip[2]; 6261556Srgrimes struct cmdentry cmdentry; 6271556Srgrimes struct job *jp; 6281556Srgrimes struct jmploc jmploc; 629194765Sjilles struct jmploc *savehandler; 630194765Sjilles char *savecmdname; 631194765Sjilles struct shparam saveparam; 632194765Sjilles struct localvar *savelocalvars; 633199647Sjilles struct parsefile *savetopfile; 6341556Srgrimes volatile int e; 6351556Srgrimes char *lastarg; 63645916Scracauer int realstatus; 63754884Scracauer int do_clearcmdentry; 638204800Sjilles char *path = pathval(); 6391556Srgrimes 6401556Srgrimes /* First expand the arguments. */ 641149802Sstefanf TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags)); 6421556Srgrimes setstackmark(&smark); 6431556Srgrimes arglist.lastp = &arglist.list; 6441556Srgrimes varlist.lastp = &varlist.list; 6451556Srgrimes varflag = 1; 64654884Scracauer do_clearcmdentry = 0; 64717987Speter oexitstatus = exitstatus; 64817987Speter exitstatus = 0; 6491556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 65017987Speter char *p = argp->narg.text; 6511556Srgrimes if (varflag && is_name(*p)) { 6521556Srgrimes do { 6531556Srgrimes p++; 6541556Srgrimes } while (is_in_name(*p)); 6551556Srgrimes if (*p == '=') { 6561556Srgrimes expandarg(argp, &varlist, EXP_VARTILDE); 6571556Srgrimes continue; 6581556Srgrimes } 6591556Srgrimes } 6601556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 6611556Srgrimes varflag = 0; 6621556Srgrimes } 6631556Srgrimes *arglist.lastp = NULL; 6641556Srgrimes *varlist.lastp = NULL; 6651556Srgrimes expredir(cmd->ncmd.redirect); 6661556Srgrimes argc = 0; 6671556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 6681556Srgrimes argc++; 6691556Srgrimes argv = stalloc(sizeof (char *) * (argc + 1)); 6701556Srgrimes 6711556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6721556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 6731556Srgrimes *argv++ = sp->text; 6741556Srgrimes } 6751556Srgrimes *argv = NULL; 6761556Srgrimes lastarg = NULL; 6771556Srgrimes if (iflag && funcnest == 0 && argc > 0) 6781556Srgrimes lastarg = argv[-1]; 6791556Srgrimes argv -= argc; 6801556Srgrimes 6811556Srgrimes /* Print the command if xflag is set. */ 6821556Srgrimes if (xflag) { 683159632Sstefanf char sep = 0; 684194786Sjilles const char *p; 685159632Sstefanf out2str(ps4val()); 6861556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 687159632Sstefanf if (sep != 0) 688201366Sjilles out2c(' '); 689194786Sjilles p = sp->text; 690194786Sjilles while (*p != '=' && *p != '\0') 691194786Sjilles out2c(*p++); 692194786Sjilles if (*p != '\0') { 693194786Sjilles out2c(*p++); 694194786Sjilles out2qstr(p); 695194786Sjilles } 696159632Sstefanf sep = ' '; 6971556Srgrimes } 6981556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 699159632Sstefanf if (sep != 0) 700201366Sjilles out2c(' '); 701194786Sjilles /* Disambiguate command looking like assignment. */ 702194786Sjilles if (sp == arglist.list && 703194786Sjilles strchr(sp->text, '=') != NULL && 704194786Sjilles strchr(sp->text, '\'') == NULL) { 705194786Sjilles out2c('\''); 706194786Sjilles out2str(sp->text); 707194786Sjilles out2c('\''); 708194786Sjilles } else 709194786Sjilles out2qstr(sp->text); 710159632Sstefanf sep = ' '; 7111556Srgrimes } 712201366Sjilles out2c('\n'); 7131556Srgrimes flushout(&errout); 7141556Srgrimes } 7151556Srgrimes 7161556Srgrimes /* Now locate the command. */ 7171556Srgrimes if (argc == 0) { 718157601Sstefanf /* Variable assignment(s) without command */ 7191556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 7201556Srgrimes cmdentry.u.index = BLTINCMD; 721205138Sjilles cmdentry.special = 0; 7221556Srgrimes } else { 72317987Speter static const char PATH[] = "PATH="; 724204800Sjilles int cmd_flags = 0, bltinonly = 0; 72517987Speter 72617987Speter /* 72717987Speter * Modify the command lookup path, if a PATH= assignment 72817987Speter * is present 72917987Speter */ 73017987Speter for (sp = varlist.list ; sp ; sp = sp->next) 73154884Scracauer if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 73217987Speter path = sp->text + sizeof(PATH) - 1; 733155301Sschweikh /* 73454884Scracauer * On `PATH=... command`, we need to make 73554884Scracauer * sure that the command isn't using the 73654884Scracauer * non-updated hash table of the outer PATH 737155301Sschweikh * setting and we need to make sure that 73854884Scracauer * the hash table isn't filled with items 73954884Scracauer * from the temporary setting. 74054884Scracauer * 741155301Sschweikh * It would be better to forbit using and 74254884Scracauer * updating the table while this command 74354884Scracauer * runs, by the command finding mechanism 74454884Scracauer * is heavily integrated with hash handling, 74554884Scracauer * so we just delete the hash before and after 74654884Scracauer * the command runs. Partly deleting like 74754884Scracauer * changepatch() does doesn't seem worth the 74854884Scracauer * bookinging effort, since most such runs add 749123996Smaxim * directories in front of the new PATH. 75054884Scracauer */ 75154884Scracauer clearcmdentry(0); 75254884Scracauer do_clearcmdentry = 1; 75354884Scracauer } 75417987Speter 755204800Sjilles for (;;) { 756204800Sjilles if (bltinonly) { 757204800Sjilles cmdentry.u.index = find_builtin(*argv, &cmdentry.special); 758204800Sjilles if (cmdentry.u.index < 0) { 759201431Sjilles cmdentry.u.index = BLTINCMD; 760201431Sjilles argv--; 761201431Sjilles argc++; 762201431Sjilles break; 7631556Srgrimes } 764204800Sjilles } else 765204800Sjilles find_command(argv[0], &cmdentry, cmd_flags, path); 766204800Sjilles /* implement the bltin and command builtins here */ 767204800Sjilles if (cmdentry.cmdtype != CMDBUILTIN) 768204800Sjilles break; 769204800Sjilles if (cmdentry.u.index == BLTINCMD) { 770204800Sjilles if (argc == 1) 7711556Srgrimes break; 772204800Sjilles argv++; 773204800Sjilles argc--; 774204800Sjilles bltinonly = 1; 775204800Sjilles } else if (cmdentry.u.index == COMMANDCMD) { 776204800Sjilles if (argc == 1) 777204800Sjilles break; 778204800Sjilles if (!strcmp(argv[1], "-p")) { 779204800Sjilles if (argc == 2) 780204800Sjilles break; 781204800Sjilles if (argv[2][0] == '-') { 782204800Sjilles if (strcmp(argv[2], "--")) 783204800Sjilles break; 784204800Sjilles if (argc == 3) 785204800Sjilles break; 786204800Sjilles argv += 3; 787204800Sjilles argc -= 3; 788204800Sjilles } else { 789204800Sjilles argv += 2; 790204800Sjilles argc -= 2; 791204800Sjilles } 792204800Sjilles path = _PATH_STDPATH; 793204800Sjilles clearcmdentry(0); 794204800Sjilles do_clearcmdentry = 1; 795204800Sjilles } else if (!strcmp(argv[1], "--")) { 796204800Sjilles if (argc == 2) 797204800Sjilles break; 798204800Sjilles argv += 2; 799204800Sjilles argc -= 2; 800204800Sjilles } else if (argv[1][0] == '-') 801204800Sjilles break; 802204800Sjilles else { 803204800Sjilles argv++; 804204800Sjilles argc--; 805204800Sjilles } 806204800Sjilles cmd_flags |= DO_NOFUNC; 807204800Sjilles bltinonly = 0; 808204800Sjilles } else 809204800Sjilles break; 8101556Srgrimes } 811204800Sjilles /* 812204800Sjilles * Special builtins lose their special properties when 813204800Sjilles * called via 'command'. 814204800Sjilles */ 815204800Sjilles if (cmd_flags & DO_NOFUNC) 816204800Sjilles cmdentry.special = 0; 8171556Srgrimes } 8181556Srgrimes 8191556Srgrimes /* Fork off a child process if necessary. */ 8201556Srgrimes if (cmd->ncmd.backgnd 821197820Sjilles || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) 822194127Sjilles && ((flags & EV_EXIT) == 0 || have_traps())) 82317987Speter || ((flags & EV_BACKCMD) != 0 8241556Srgrimes && (cmdentry.cmdtype != CMDBUILTIN 82548896Ssheldonh || cmdentry.u.index == CDCMD 8261556Srgrimes || cmdentry.u.index == DOTCMD 827204800Sjilles || cmdentry.u.index == EVALCMD))) { 8281556Srgrimes jp = makejob(cmd, 1); 8291556Srgrimes mode = cmd->ncmd.backgnd; 8301556Srgrimes if (flags & EV_BACKCMD) { 8311556Srgrimes mode = FORK_NOJOB; 8321556Srgrimes if (pipe(pip) < 0) 83353891Scracauer error("Pipe call failed: %s", strerror(errno)); 8341556Srgrimes } 8351556Srgrimes if (forkshell(jp, cmd, mode) != 0) 8361556Srgrimes goto parent; /* at end of routine */ 8371556Srgrimes if (flags & EV_BACKCMD) { 8381556Srgrimes FORCEINTON; 8391556Srgrimes close(pip[0]); 8401556Srgrimes if (pip[1] != 1) { 841124780Sdes dup2(pip[1], 1); 8421556Srgrimes close(pip[1]); 8431556Srgrimes } 8441556Srgrimes } 8451556Srgrimes flags |= EV_EXIT; 8461556Srgrimes } 8471556Srgrimes 8481556Srgrimes /* This is the child process if a fork occurred. */ 8491556Srgrimes /* Execute the command. */ 8501556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 85120425Ssteve#ifdef DEBUG 8521556Srgrimes trputs("Shell function: "); trargs(argv); 85320425Ssteve#endif 8541556Srgrimes saveparam = shellparam; 8551556Srgrimes shellparam.malloc = 0; 85620425Ssteve shellparam.reset = 1; 8571556Srgrimes shellparam.nparam = argc - 1; 8581556Srgrimes shellparam.p = argv + 1; 8591556Srgrimes shellparam.optnext = NULL; 8601556Srgrimes INTOFF; 8611556Srgrimes savelocalvars = localvars; 8621556Srgrimes localvars = NULL; 863196483Sjilles reffunc(cmdentry.u.func); 864194765Sjilles savehandler = handler; 8651556Srgrimes if (setjmp(jmploc.loc)) { 8661556Srgrimes if (exception == EXSHELLPROC) 867194765Sjilles freeparam(&saveparam); 8681556Srgrimes else { 8691556Srgrimes freeparam(&shellparam); 8701556Srgrimes shellparam = saveparam; 871204802Sjilles if (exception == EXERROR || exception == EXEXEC) 872204802Sjilles popredir(); 8731556Srgrimes } 874196483Sjilles unreffunc(cmdentry.u.func); 8751556Srgrimes poplocalvars(); 8761556Srgrimes localvars = savelocalvars; 877201283Sjilles funcnest--; 8781556Srgrimes handler = savehandler; 8791556Srgrimes longjmp(handler->loc, 1); 8801556Srgrimes } 8811556Srgrimes handler = &jmploc; 882201283Sjilles funcnest++; 883204802Sjilles redirect(cmd->ncmd.redirect, REDIR_PUSH); 884199660Sjilles INTON; 8851556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8861556Srgrimes mklocal(sp->text); 887185231Sstefanf exitstatus = oexitstatus; 88835675Scracauer if (flags & EV_TESTED) 889196634Sjilles evaltree(getfuncnode(cmdentry.u.func), EV_TESTED); 89035675Scracauer else 891196634Sjilles evaltree(getfuncnode(cmdentry.u.func), 0); 8921556Srgrimes INTOFF; 893196483Sjilles unreffunc(cmdentry.u.func); 8941556Srgrimes poplocalvars(); 8951556Srgrimes localvars = savelocalvars; 8961556Srgrimes freeparam(&shellparam); 8971556Srgrimes shellparam = saveparam; 8981556Srgrimes handler = savehandler; 899201283Sjilles funcnest--; 9001556Srgrimes popredir(); 9011556Srgrimes INTON; 9021556Srgrimes if (evalskip == SKIPFUNC) { 9031556Srgrimes evalskip = 0; 9041556Srgrimes skipcount = 0; 9051556Srgrimes } 9061556Srgrimes if (flags & EV_EXIT) 9071556Srgrimes exitshell(exitstatus); 9081556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 90920425Ssteve#ifdef DEBUG 9101556Srgrimes trputs("builtin command: "); trargs(argv); 91120425Ssteve#endif 9121556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 9131556Srgrimes if (flags == EV_BACKCMD) { 9141556Srgrimes memout.nleft = 0; 9151556Srgrimes memout.nextc = memout.buf; 9161556Srgrimes memout.bufsize = 64; 9171556Srgrimes mode |= REDIR_BACKQ; 918201366Sjilles cmdentry.special = 0; 9191556Srgrimes } 9201556Srgrimes savecmdname = commandname; 921199647Sjilles savetopfile = getcurrentfile(); 9221556Srgrimes cmdenviron = varlist.list; 9231556Srgrimes e = -1; 924194765Sjilles savehandler = handler; 9251556Srgrimes if (setjmp(jmploc.loc)) { 9261556Srgrimes e = exception; 9271556Srgrimes exitstatus = (e == EXINT)? SIGINT+128 : 2; 9281556Srgrimes goto cmddone; 9291556Srgrimes } 9301556Srgrimes handler = &jmploc; 931157601Sstefanf redirect(cmd->ncmd.redirect, mode); 932205138Sjilles /* 933205138Sjilles * If there is no command word, redirection errors should 934205138Sjilles * not be fatal but assignment errors should. 935205138Sjilles */ 936205138Sjilles if (argc == 0 && !(flags & EV_BACKCMD)) 937205138Sjilles cmdentry.special = 1; 938157601Sstefanf if (cmdentry.special) 939157601Sstefanf listsetvar(cmdenviron); 940207678Sjilles if (argc > 0) 941207678Sjilles bltinsetlocale(); 9421556Srgrimes commandname = argv[0]; 9431556Srgrimes argptr = argv + 1; 944201053Sjilles nextopt_optptr = NULL; /* initialize nextopt */ 945193169Sstefanf builtin_flags = flags; 9461556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 9471556Srgrimes flushall(); 9481556Srgrimescmddone: 949207678Sjilles if (argc > 0) 950207678Sjilles bltinunsetlocale(); 95160592Scracauer cmdenviron = NULL; 9521556Srgrimes out1 = &output; 9531556Srgrimes out2 = &errout; 9541556Srgrimes freestdout(); 9551556Srgrimes if (e != EXSHELLPROC) { 9561556Srgrimes commandname = savecmdname; 9571556Srgrimes if (flags & EV_EXIT) { 9581556Srgrimes exitshell(exitstatus); 9591556Srgrimes } 9601556Srgrimes } 9611556Srgrimes handler = savehandler; 962201366Sjilles if (flags == EV_BACKCMD) { 963201366Sjilles backcmd->buf = memout.buf; 964201366Sjilles backcmd->nleft = memout.nextc - memout.buf; 965201366Sjilles memout.buf = NULL; 966201366Sjilles } 967204801Sjilles if (cmdentry.u.index != EXECCMD && 968204801Sjilles (e == -1 || e == EXERROR || e == EXEXEC)) 969204801Sjilles popredir(); 9701556Srgrimes if (e != -1) { 97120425Ssteve if ((e != EXERROR && e != EXEXEC) 972157601Sstefanf || cmdentry.special) 9731556Srgrimes exraise(e); 974199647Sjilles popfilesupto(savetopfile); 975201366Sjilles if (flags != EV_BACKCMD) 976201366Sjilles FORCEINTON; 9771556Srgrimes } 9781556Srgrimes } else { 97920425Ssteve#ifdef DEBUG 9801556Srgrimes trputs("normal command: "); trargs(argv); 98120425Ssteve#endif 9821556Srgrimes redirect(cmd->ncmd.redirect, 0); 9831556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 9841556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 9851556Srgrimes envp = environment(); 986204800Sjilles shellexec(argv, envp, path, cmdentry.u.index); 9871556Srgrimes /*NOTREACHED*/ 9881556Srgrimes } 9891556Srgrimes goto out; 9901556Srgrimes 9911556Srgrimesparent: /* parent process gets here (if we forked) */ 9921556Srgrimes if (mode == 0) { /* argument to fork */ 9931556Srgrimes INTOFF; 99445916Scracauer exitstatus = waitforjob(jp, &realstatus); 9951556Srgrimes INTON; 99645916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 99745266Scracauer evalskip = SKIPBREAK; 99845266Scracauer skipcount = loopnest; 99945266Scracauer } 10001556Srgrimes } else if (mode == 2) { 10011556Srgrimes backcmd->fd = pip[0]; 10021556Srgrimes close(pip[1]); 10031556Srgrimes backcmd->jp = jp; 10041556Srgrimes } 10051556Srgrimes 10061556Srgrimesout: 10071556Srgrimes if (lastarg) 10081556Srgrimes setvar("_", lastarg, 0); 100954884Scracauer if (do_clearcmdentry) 101054884Scracauer clearcmdentry(0); 10111556Srgrimes popstackmark(&smark); 10121556Srgrimes} 10131556Srgrimes 10141556Srgrimes 10151556Srgrimes 10161556Srgrimes/* 10171556Srgrimes * Search for a command. This is called before we fork so that the 10181556Srgrimes * location of the command will be available in the parent as well as 10191556Srgrimes * the child. The check for "goodname" is an overly conservative 10201556Srgrimes * check that the name will not be subject to expansion. 10211556Srgrimes */ 10221556Srgrimes 10231556SrgrimesSTATIC void 102490111Simpprehash(union node *n) 102517987Speter{ 10261556Srgrimes struct cmdentry entry; 10271556Srgrimes 1028159633Sstefanf if (n && n->type == NCMD && n->ncmd.args) 102917987Speter if (goodname(n->ncmd.args->narg.text)) 103017987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 103117987Speter pathval()); 10321556Srgrimes} 10331556Srgrimes 10341556Srgrimes 10351556Srgrimes 10361556Srgrimes/* 10371556Srgrimes * Builtin commands. Builtin commands whose functions are closely 10381556Srgrimes * tied to evaluation are implemented here. 10391556Srgrimes */ 10401556Srgrimes 10411556Srgrimes/* 1042201431Sjilles * No command given, a bltin command with no arguments, or a bltin command 1043201431Sjilles * with an invalid name. 10441556Srgrimes */ 10451556Srgrimes 104617987Speterint 1047201431Sjillesbltincmd(int argc, char **argv) 104817987Speter{ 1049201431Sjilles if (argc > 1) { 1050201431Sjilles out2fmt_flush("%s: not found\n", argv[1]); 1051201431Sjilles return 127; 1052201431Sjilles } 105320425Ssteve /* 105417987Speter * Preserve exitstatus of a previous possible redirection 105520425Ssteve * as POSIX mandates 105617987Speter */ 10571556Srgrimes return exitstatus; 10581556Srgrimes} 10591556Srgrimes 10601556Srgrimes 10611556Srgrimes/* 10621556Srgrimes * Handle break and continue commands. Break, continue, and return are 10631556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 10641556Srgrimes * above all check this flag, and if it is set they start skipping 10651556Srgrimes * commands rather than executing them. The variable skipcount is 10661556Srgrimes * the number of loops to break/continue, or the number of function 10671556Srgrimes * levels to return. (The latter is always 1.) It should probably 10681556Srgrimes * be an error to break out of more loops than exist, but it isn't 10691556Srgrimes * in the standard shell so we don't make it one here. 10701556Srgrimes */ 10711556Srgrimes 107217987Speterint 107390111Simpbreakcmd(int argc, char **argv) 107417987Speter{ 107520425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 10761556Srgrimes 10771556Srgrimes if (n > loopnest) 10781556Srgrimes n = loopnest; 10791556Srgrimes if (n > 0) { 10801556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 10811556Srgrimes skipcount = n; 10821556Srgrimes } 10831556Srgrimes return 0; 10841556Srgrimes} 10851556Srgrimes 1086100437Stjr/* 1087100437Stjr * The `command' command. 1088100437Stjr */ 1089100437Stjrint 1090100437Stjrcommandcmd(int argc, char **argv) 1091100437Stjr{ 1092207783Sjilles const char *path; 1093100437Stjr int ch; 1094151810Sstefanf int cmd = -1; 10951556Srgrimes 1096204800Sjilles path = bltinlookup("PATH", 1); 1097100437Stjr 1098100437Stjr optind = optreset = 1; 1099100663Stjr opterr = 0; 1100151810Sstefanf while ((ch = getopt(argc, argv, "pvV")) != -1) { 1101100437Stjr switch (ch) { 1102100437Stjr case 'p': 1103207783Sjilles path = _PATH_STDPATH; 1104100437Stjr break; 1105151810Sstefanf case 'v': 1106151810Sstefanf cmd = TYPECMD_SMALLV; 1107151810Sstefanf break; 1108151810Sstefanf case 'V': 1109151810Sstefanf cmd = TYPECMD_BIGV; 1110151810Sstefanf break; 1111100437Stjr case '?': 1112100437Stjr default: 1113100437Stjr error("unknown option: -%c", optopt); 1114100437Stjr } 1115100437Stjr } 1116100437Stjr argc -= optind; 1117100437Stjr argv += optind; 1118100437Stjr 1119151810Sstefanf if (cmd != -1) { 1120151810Sstefanf if (argc != 1) 1121151810Sstefanf error("wrong number of arguments"); 1122201343Sjilles return typecmd_impl(2, argv - 1, cmd, path); 1123151810Sstefanf } 1124204800Sjilles if (argc != 0) 1125204800Sjilles error("commandcmd() called while it should not be"); 1126100437Stjr 1127100437Stjr /* 1128100437Stjr * Do nothing successfully if no command was specified; 1129100437Stjr * ksh also does this. 1130100437Stjr */ 1131204800Sjilles return 0; 1132100437Stjr} 1133100437Stjr 1134100437Stjr 11351556Srgrimes/* 11361556Srgrimes * The return command. 11371556Srgrimes */ 11381556Srgrimes 113917987Speterint 114090111Simpreturncmd(int argc, char **argv) 114117987Speter{ 114220425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 11431556Srgrimes 11441556Srgrimes if (funcnest) { 11451556Srgrimes evalskip = SKIPFUNC; 11461556Srgrimes skipcount = 1; 114720425Ssteve } else { 114820425Ssteve /* skip the rest of the file */ 114920425Ssteve evalskip = SKIPFILE; 115020425Ssteve skipcount = 1; 11511556Srgrimes } 11521556Srgrimes return ret; 11531556Srgrimes} 11541556Srgrimes 11551556Srgrimes 115617987Speterint 115790111Simpfalsecmd(int argc __unused, char **argv __unused) 115817987Speter{ 115917987Speter return 1; 116017987Speter} 116117987Speter 116217987Speter 116317987Speterint 116490111Simptruecmd(int argc __unused, char **argv __unused) 116517987Speter{ 11661556Srgrimes return 0; 11671556Srgrimes} 11681556Srgrimes 11691556Srgrimes 117017987Speterint 117190111Simpexeccmd(int argc, char **argv) 117217987Speter{ 11731556Srgrimes if (argc > 1) { 117417987Speter struct strlist *sp; 117517987Speter 11761556Srgrimes iflag = 0; /* exit on error */ 11771556Srgrimes mflag = 0; 11781556Srgrimes optschanged(); 117917987Speter for (sp = cmdenviron; sp ; sp = sp->next) 118017987Speter setvareq(sp->text, VEXPORT|VSTACK); 11811556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 11821556Srgrimes 11831556Srgrimes } 11841556Srgrimes return 0; 11851556Srgrimes} 1186153091Sstefanf 1187153091Sstefanf 1188153091Sstefanfint 1189153091Sstefanftimescmd(int argc __unused, char **argv __unused) 1190153091Sstefanf{ 1191153091Sstefanf struct rusage ru; 1192153091Sstefanf long shumins, shsmins, chumins, chsmins; 1193153091Sstefanf double shusecs, shssecs, chusecs, chssecs; 1194153091Sstefanf 1195153091Sstefanf if (getrusage(RUSAGE_SELF, &ru) < 0) 1196153091Sstefanf return 1; 1197153091Sstefanf shumins = ru.ru_utime.tv_sec / 60; 1198153091Sstefanf shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1199153091Sstefanf shsmins = ru.ru_stime.tv_sec / 60; 1200153091Sstefanf shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1201153091Sstefanf if (getrusage(RUSAGE_CHILDREN, &ru) < 0) 1202153091Sstefanf return 1; 1203153091Sstefanf chumins = ru.ru_utime.tv_sec / 60; 1204153091Sstefanf chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; 1205153091Sstefanf chsmins = ru.ru_stime.tv_sec / 60; 1206153091Sstefanf chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; 1207153091Sstefanf out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, 1208153091Sstefanf shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); 1209153091Sstefanf return 0; 1210153091Sstefanf} 1211