eval.c revision 36150
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 * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836150Scharnier#if 0 3936150Scharnierstatic char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; 4036150Scharnier#endif 4136150Scharnierstatic const char rcsid[] = 4236150Scharnier "$Id$"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 4517987Speter#include <signal.h> 4617987Speter#include <unistd.h> 4717987Speter 481556Srgrimes/* 491556Srgrimes * Evaluate a command. 501556Srgrimes */ 511556Srgrimes 521556Srgrimes#include "shell.h" 531556Srgrimes#include "nodes.h" 541556Srgrimes#include "syntax.h" 551556Srgrimes#include "expand.h" 561556Srgrimes#include "parser.h" 571556Srgrimes#include "jobs.h" 581556Srgrimes#include "eval.h" 591556Srgrimes#include "builtins.h" 601556Srgrimes#include "options.h" 611556Srgrimes#include "exec.h" 621556Srgrimes#include "redir.h" 631556Srgrimes#include "input.h" 641556Srgrimes#include "output.h" 651556Srgrimes#include "trap.h" 661556Srgrimes#include "var.h" 671556Srgrimes#include "memalloc.h" 681556Srgrimes#include "error.h" 6917987Speter#include "show.h" 701556Srgrimes#include "mystring.h" 7117987Speter#ifndef NO_HISTORY 721556Srgrimes#include "myhistedit.h" 7317987Speter#endif 741556Srgrimes 751556Srgrimes 761556Srgrimes/* flags in argument to evaltree */ 771556Srgrimes#define EV_EXIT 01 /* exit after evaluating tree */ 781556Srgrimes#define EV_TESTED 02 /* exit status is checked; ignore -e flag */ 791556Srgrimes#define EV_BACKCMD 04 /* command executing within back quotes */ 801556Srgrimes 811556SrgrimesMKINIT int evalskip; /* set if we are skipping commands */ 821556SrgrimesSTATIC int skipcount; /* number of levels to skip */ 831556SrgrimesMKINIT int loopnest; /* current loop nesting level */ 841556Srgrimesint funcnest; /* depth of function calls */ 851556Srgrimes 861556Srgrimes 871556Srgrimeschar *commandname; 881556Srgrimesstruct strlist *cmdenviron; 891556Srgrimesint exitstatus; /* exit status of last command */ 9017987Speterint oexitstatus; /* saved exit status */ 911556Srgrimes 921556Srgrimes 9317987SpeterSTATIC void evalloop __P((union node *)); 9417987SpeterSTATIC void evalfor __P((union node *)); 9517987SpeterSTATIC void evalcase __P((union node *, int)); 9617987SpeterSTATIC void evalsubshell __P((union node *, int)); 9717987SpeterSTATIC void expredir __P((union node *)); 9817987SpeterSTATIC void evalpipe __P((union node *)); 9917987SpeterSTATIC void evalcommand __P((union node *, int, struct backcmd *)); 10017987SpeterSTATIC void prehash __P((union node *)); 1011556Srgrimes 1021556Srgrimes 1031556Srgrimes/* 1041556Srgrimes * Called to reset things after an exception. 1051556Srgrimes */ 1061556Srgrimes 1071556Srgrimes#ifdef mkinit 1081556SrgrimesINCLUDE "eval.h" 1091556Srgrimes 1101556SrgrimesRESET { 1111556Srgrimes evalskip = 0; 1121556Srgrimes loopnest = 0; 1131556Srgrimes funcnest = 0; 1141556Srgrimes} 1151556Srgrimes 1161556SrgrimesSHELLPROC { 1171556Srgrimes exitstatus = 0; 1181556Srgrimes} 1191556Srgrimes#endif 1201556Srgrimes 1211556Srgrimes 1221556Srgrimes 1231556Srgrimes/* 1241556Srgrimes * The eval commmand. 1251556Srgrimes */ 1261556Srgrimes 12717987Speterint 12820425Ssteveevalcmd(argc, argv) 12917987Speter int argc; 13020425Ssteve char **argv; 1311556Srgrimes{ 1321556Srgrimes char *p; 1331556Srgrimes char *concat; 1341556Srgrimes char **ap; 1351556Srgrimes 1361556Srgrimes if (argc > 1) { 1371556Srgrimes p = argv[1]; 1381556Srgrimes if (argc > 2) { 1391556Srgrimes STARTSTACKSTR(concat); 1401556Srgrimes ap = argv + 2; 1411556Srgrimes for (;;) { 1421556Srgrimes while (*p) 1431556Srgrimes STPUTC(*p++, concat); 1441556Srgrimes if ((p = *ap++) == NULL) 1451556Srgrimes break; 1461556Srgrimes STPUTC(' ', concat); 1471556Srgrimes } 1481556Srgrimes STPUTC('\0', concat); 1491556Srgrimes p = grabstackstr(concat); 1501556Srgrimes } 1511556Srgrimes evalstring(p); 1521556Srgrimes } 1531556Srgrimes return exitstatus; 1541556Srgrimes} 1551556Srgrimes 1561556Srgrimes 1571556Srgrimes/* 1581556Srgrimes * Execute a command or commands contained in a string. 1591556Srgrimes */ 1601556Srgrimes 1611556Srgrimesvoid 1621556Srgrimesevalstring(s) 1631556Srgrimes char *s; 1641556Srgrimes { 1651556Srgrimes union node *n; 1661556Srgrimes struct stackmark smark; 1671556Srgrimes 1681556Srgrimes setstackmark(&smark); 1691556Srgrimes setinputstring(s, 1); 1701556Srgrimes while ((n = parsecmd(0)) != NEOF) { 1711556Srgrimes evaltree(n, 0); 1721556Srgrimes popstackmark(&smark); 1731556Srgrimes } 1741556Srgrimes popfile(); 1751556Srgrimes popstackmark(&smark); 1761556Srgrimes} 1771556Srgrimes 1781556Srgrimes 1791556Srgrimes 1801556Srgrimes/* 1811556Srgrimes * Evaluate a parse tree. The value is left in the global variable 1821556Srgrimes * exitstatus. 1831556Srgrimes */ 1841556Srgrimes 1851556Srgrimesvoid 1861556Srgrimesevaltree(n, flags) 1871556Srgrimes union node *n; 18817987Speter int flags; 18917987Speter{ 1901556Srgrimes if (n == NULL) { 1911556Srgrimes TRACE(("evaltree(NULL) called\n")); 1921556Srgrimes exitstatus = 0; 1931556Srgrimes goto out; 1941556Srgrimes } 19517987Speter#ifndef NO_HISTORY 1961556Srgrimes displayhist = 1; /* show history substitutions done with fc */ 19717987Speter#endif 19817987Speter TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type)); 1991556Srgrimes switch (n->type) { 2001556Srgrimes case NSEMI: 2011556Srgrimes evaltree(n->nbinary.ch1, 0); 2021556Srgrimes if (evalskip) 2031556Srgrimes goto out; 2041556Srgrimes evaltree(n->nbinary.ch2, flags); 2051556Srgrimes break; 2061556Srgrimes case NAND: 2071556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 20818754Ssteve if (evalskip || exitstatus != 0) { 20918754Ssteve flags |= EV_TESTED; 2101556Srgrimes goto out; 21118754Ssteve } 2121556Srgrimes evaltree(n->nbinary.ch2, flags); 2131556Srgrimes break; 2141556Srgrimes case NOR: 2151556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2161556Srgrimes if (evalskip || exitstatus == 0) 2171556Srgrimes goto out; 2181556Srgrimes evaltree(n->nbinary.ch2, flags); 2191556Srgrimes break; 2201556Srgrimes case NREDIR: 2211556Srgrimes expredir(n->nredir.redirect); 2221556Srgrimes redirect(n->nredir.redirect, REDIR_PUSH); 2231556Srgrimes evaltree(n->nredir.n, flags); 2241556Srgrimes popredir(); 2251556Srgrimes break; 2261556Srgrimes case NSUBSHELL: 2271556Srgrimes evalsubshell(n, flags); 2281556Srgrimes break; 2291556Srgrimes case NBACKGND: 2301556Srgrimes evalsubshell(n, flags); 2311556Srgrimes break; 2321556Srgrimes case NIF: { 2331556Srgrimes evaltree(n->nif.test, EV_TESTED); 2341556Srgrimes if (evalskip) 2351556Srgrimes goto out; 23620425Ssteve if (exitstatus == 0) 2371556Srgrimes evaltree(n->nif.ifpart, flags); 23817987Speter else if (n->nif.elsepart) 2391556Srgrimes evaltree(n->nif.elsepart, flags); 24020425Ssteve else 24120425Ssteve exitstatus = 0; 2421556Srgrimes break; 2431556Srgrimes } 2441556Srgrimes case NWHILE: 2451556Srgrimes case NUNTIL: 2461556Srgrimes evalloop(n); 2471556Srgrimes break; 2481556Srgrimes case NFOR: 2491556Srgrimes evalfor(n); 2501556Srgrimes break; 2511556Srgrimes case NCASE: 2521556Srgrimes evalcase(n, flags); 2531556Srgrimes break; 2541556Srgrimes case NDEFUN: 2551556Srgrimes defun(n->narg.text, n->narg.next); 2561556Srgrimes exitstatus = 0; 2571556Srgrimes break; 2581556Srgrimes case NNOT: 2591556Srgrimes evaltree(n->nnot.com, EV_TESTED); 2601556Srgrimes exitstatus = !exitstatus; 2611556Srgrimes break; 2621556Srgrimes 2631556Srgrimes case NPIPE: 2641556Srgrimes evalpipe(n); 2651556Srgrimes break; 2661556Srgrimes case NCMD: 2671556Srgrimes evalcommand(n, flags, (struct backcmd *)NULL); 2681556Srgrimes break; 2691556Srgrimes default: 2701556Srgrimes out1fmt("Node type = %d\n", n->type); 2711556Srgrimes flushout(&output); 2721556Srgrimes break; 2731556Srgrimes } 2741556Srgrimesout: 2751556Srgrimes if (pendingsigs) 2761556Srgrimes dotrap(); 2771556Srgrimes if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED))) 2781556Srgrimes exitshell(exitstatus); 2791556Srgrimes} 2801556Srgrimes 2811556Srgrimes 2821556SrgrimesSTATIC void 2831556Srgrimesevalloop(n) 2841556Srgrimes union node *n; 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 } 3081556Srgrimes evaltree(n->nbinary.ch2, 0); 3091556Srgrimes status = exitstatus; 3101556Srgrimes if (evalskip) 3111556Srgrimes goto skipping; 3121556Srgrimes } 3131556Srgrimes loopnest--; 3141556Srgrimes exitstatus = status; 3151556Srgrimes} 3161556Srgrimes 3171556Srgrimes 3181556Srgrimes 3191556SrgrimesSTATIC void 3201556Srgrimesevalfor(n) 32117987Speter union node *n; 32217987Speter{ 3231556Srgrimes struct arglist arglist; 3241556Srgrimes union node *argp; 3251556Srgrimes struct strlist *sp; 3261556Srgrimes struct stackmark smark; 3271556Srgrimes 3281556Srgrimes setstackmark(&smark); 3291556Srgrimes arglist.lastp = &arglist.list; 3301556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 33117987Speter oexitstatus = exitstatus; 3321556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3331556Srgrimes if (evalskip) 3341556Srgrimes goto out; 3351556Srgrimes } 3361556Srgrimes *arglist.lastp = NULL; 3371556Srgrimes 3381556Srgrimes exitstatus = 0; 3391556Srgrimes loopnest++; 3401556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3411556Srgrimes setvar(n->nfor.var, sp->text, 0); 3421556Srgrimes evaltree(n->nfor.body, 0); 3431556Srgrimes if (evalskip) { 3441556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3451556Srgrimes evalskip = 0; 3461556Srgrimes continue; 3471556Srgrimes } 3481556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3491556Srgrimes evalskip = 0; 3501556Srgrimes break; 3511556Srgrimes } 3521556Srgrimes } 3531556Srgrimes loopnest--; 3541556Srgrimesout: 3551556Srgrimes popstackmark(&smark); 3561556Srgrimes} 3571556Srgrimes 3581556Srgrimes 3591556Srgrimes 3601556SrgrimesSTATIC void 3611556Srgrimesevalcase(n, flags) 3621556Srgrimes union node *n; 36317987Speter 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; 3731556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3741556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3751556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3761556Srgrimes if (casematch(patp, arglist.list->text)) { 3771556Srgrimes if (evalskip == 0) { 3781556Srgrimes evaltree(cp->nclist.body, flags); 3791556Srgrimes } 3801556Srgrimes goto out; 3811556Srgrimes } 3821556Srgrimes } 3831556Srgrimes } 3841556Srgrimesout: 3851556Srgrimes popstackmark(&smark); 3861556Srgrimes} 3871556Srgrimes 3881556Srgrimes 3891556Srgrimes 3901556Srgrimes/* 3911556Srgrimes * Kick off a subshell to evaluate a tree. 3921556Srgrimes */ 3931556Srgrimes 3941556SrgrimesSTATIC void 3951556Srgrimesevalsubshell(n, flags) 3961556Srgrimes union node *n; 39717987Speter int flags; 39817987Speter{ 3991556Srgrimes struct job *jp; 4001556Srgrimes int backgnd = (n->type == NBACKGND); 4011556Srgrimes 4021556Srgrimes expredir(n->nredir.redirect); 4031556Srgrimes jp = makejob(n, 1); 4041556Srgrimes if (forkshell(jp, n, backgnd) == 0) { 4051556Srgrimes if (backgnd) 4061556Srgrimes flags &=~ EV_TESTED; 4071556Srgrimes redirect(n->nredir.redirect, 0); 4081556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 4091556Srgrimes } 4101556Srgrimes if (! backgnd) { 4111556Srgrimes INTOFF; 4121556Srgrimes exitstatus = waitforjob(jp); 4131556Srgrimes INTON; 4141556Srgrimes } 4151556Srgrimes} 4161556Srgrimes 4171556Srgrimes 4181556Srgrimes 4191556Srgrimes/* 4201556Srgrimes * Compute the names of the files in a redirection list. 4211556Srgrimes */ 4221556Srgrimes 4231556SrgrimesSTATIC void 4241556Srgrimesexpredir(n) 4251556Srgrimes union node *n; 42617987Speter{ 42725222Ssteve union node *redir; 4281556Srgrimes 4291556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 43017987Speter struct arglist fn; 43117987Speter fn.lastp = &fn.list; 43217987Speter oexitstatus = exitstatus; 43317987Speter switch (redir->type) { 43417987Speter case NFROM: 43517987Speter case NTO: 43617987Speter case NAPPEND: 4371556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4381556Srgrimes redir->nfile.expfname = fn.list->text; 43917987Speter break; 44017987Speter case NFROMFD: 44117987Speter case NTOFD: 44217987Speter if (redir->ndup.vname) { 44317987Speter expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); 44417987Speter fixredir(redir, fn.list->text, 1); 44517987Speter } 44617987Speter break; 4471556Srgrimes } 4481556Srgrimes } 4491556Srgrimes} 4501556Srgrimes 4511556Srgrimes 4521556Srgrimes 4531556Srgrimes/* 4541556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 4551556Srgrimes * of the process creating the pipeline. (This differs from some versions 4561556Srgrimes * of the shell, which make the last process in a pipeline the parent 4571556Srgrimes * of all the rest.) 4581556Srgrimes */ 4591556Srgrimes 4601556SrgrimesSTATIC void 4611556Srgrimesevalpipe(n) 4621556Srgrimes union node *n; 46317987Speter{ 4641556Srgrimes struct job *jp; 4651556Srgrimes struct nodelist *lp; 4661556Srgrimes int pipelen; 4671556Srgrimes int prevfd; 4681556Srgrimes int pip[2]; 4691556Srgrimes 47017987Speter TRACE(("evalpipe(0x%lx) called\n", (long)n)); 4711556Srgrimes pipelen = 0; 4721556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 4731556Srgrimes pipelen++; 4741556Srgrimes INTOFF; 4751556Srgrimes jp = makejob(n, pipelen); 4761556Srgrimes prevfd = -1; 4771556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 4781556Srgrimes prehash(lp->n); 4791556Srgrimes pip[1] = -1; 4801556Srgrimes if (lp->next) { 4811556Srgrimes if (pipe(pip) < 0) { 4821556Srgrimes close(prevfd); 4831556Srgrimes error("Pipe call failed"); 4841556Srgrimes } 4851556Srgrimes } 4861556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 4871556Srgrimes INTON; 4881556Srgrimes if (prevfd > 0) { 4891556Srgrimes close(0); 4901556Srgrimes copyfd(prevfd, 0); 4911556Srgrimes close(prevfd); 4921556Srgrimes } 4931556Srgrimes if (pip[1] >= 0) { 4941556Srgrimes close(pip[0]); 4951556Srgrimes if (pip[1] != 1) { 4961556Srgrimes close(1); 4971556Srgrimes copyfd(pip[1], 1); 4981556Srgrimes close(pip[1]); 4991556Srgrimes } 5001556Srgrimes } 5011556Srgrimes evaltree(lp->n, EV_EXIT); 5021556Srgrimes } 5031556Srgrimes if (prevfd >= 0) 5041556Srgrimes close(prevfd); 5051556Srgrimes prevfd = pip[0]; 5061556Srgrimes close(pip[1]); 5071556Srgrimes } 5081556Srgrimes INTON; 5091556Srgrimes if (n->npipe.backgnd == 0) { 5101556Srgrimes INTOFF; 5111556Srgrimes exitstatus = waitforjob(jp); 5121556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5131556Srgrimes INTON; 5141556Srgrimes } 5151556Srgrimes} 5161556Srgrimes 5171556Srgrimes 5181556Srgrimes 5191556Srgrimes/* 5201556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5211556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5221556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5231556Srgrimes * Should be called with interrupts off. 5241556Srgrimes */ 5251556Srgrimes 5261556Srgrimesvoid 5271556Srgrimesevalbackcmd(n, result) 5281556Srgrimes union node *n; 5291556Srgrimes struct backcmd *result; 53017987Speter{ 5311556Srgrimes int pip[2]; 5321556Srgrimes struct job *jp; 5331556Srgrimes struct stackmark smark; /* unnecessary */ 5341556Srgrimes 5351556Srgrimes setstackmark(&smark); 5361556Srgrimes result->fd = -1; 5371556Srgrimes result->buf = NULL; 5381556Srgrimes result->nleft = 0; 5391556Srgrimes result->jp = NULL; 54017987Speter if (n == NULL) { 54117987Speter exitstatus = 0; 5421556Srgrimes goto out; 54317987Speter } 5441556Srgrimes if (n->type == NCMD) { 54517987Speter exitstatus = oexitstatus; 5461556Srgrimes evalcommand(n, EV_BACKCMD, result); 5471556Srgrimes } else { 54817987Speter exitstatus = 0; 5491556Srgrimes if (pipe(pip) < 0) 5501556Srgrimes error("Pipe call failed"); 5511556Srgrimes jp = makejob(n, 1); 5521556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 5531556Srgrimes FORCEINTON; 5541556Srgrimes close(pip[0]); 5551556Srgrimes if (pip[1] != 1) { 5561556Srgrimes close(1); 5571556Srgrimes copyfd(pip[1], 1); 5581556Srgrimes close(pip[1]); 5591556Srgrimes } 5601556Srgrimes evaltree(n, EV_EXIT); 5611556Srgrimes } 5621556Srgrimes close(pip[1]); 5631556Srgrimes result->fd = pip[0]; 5641556Srgrimes result->jp = jp; 5651556Srgrimes } 5661556Srgrimesout: 5671556Srgrimes popstackmark(&smark); 5681556Srgrimes TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", 5691556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 5701556Srgrimes} 5711556Srgrimes 5721556Srgrimes 5731556Srgrimes 5741556Srgrimes/* 5751556Srgrimes * Execute a simple command. 5761556Srgrimes */ 5771556Srgrimes 5781556SrgrimesSTATIC void 5791556Srgrimesevalcommand(cmd, flags, backcmd) 5801556Srgrimes union node *cmd; 58117987Speter int flags; 5821556Srgrimes struct backcmd *backcmd; 58317987Speter{ 5841556Srgrimes struct stackmark smark; 5851556Srgrimes union node *argp; 5861556Srgrimes struct arglist arglist; 5871556Srgrimes struct arglist varlist; 5881556Srgrimes char **argv; 5891556Srgrimes int argc; 5901556Srgrimes char **envp; 5911556Srgrimes int varflag; 5921556Srgrimes struct strlist *sp; 5931556Srgrimes int mode; 5941556Srgrimes int pip[2]; 5951556Srgrimes struct cmdentry cmdentry; 5961556Srgrimes struct job *jp; 5971556Srgrimes struct jmploc jmploc; 5981556Srgrimes struct jmploc *volatile savehandler; 5991556Srgrimes char *volatile savecmdname; 6001556Srgrimes volatile struct shparam saveparam; 6011556Srgrimes struct localvar *volatile savelocalvars; 6021556Srgrimes volatile int e; 6031556Srgrimes char *lastarg; 60417987Speter#if __GNUC__ 60517987Speter /* Avoid longjmp clobbering */ 60617987Speter (void) &argv; 60717987Speter (void) &argc; 60817987Speter (void) &lastarg; 60917987Speter (void) &flags; 61017987Speter#endif 6111556Srgrimes 6121556Srgrimes /* First expand the arguments. */ 61317987Speter TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); 6141556Srgrimes setstackmark(&smark); 6151556Srgrimes arglist.lastp = &arglist.list; 6161556Srgrimes varlist.lastp = &varlist.list; 6171556Srgrimes varflag = 1; 61817987Speter oexitstatus = exitstatus; 61917987Speter exitstatus = 0; 6201556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 62117987Speter char *p = argp->narg.text; 6221556Srgrimes if (varflag && is_name(*p)) { 6231556Srgrimes do { 6241556Srgrimes p++; 6251556Srgrimes } while (is_in_name(*p)); 6261556Srgrimes if (*p == '=') { 6271556Srgrimes expandarg(argp, &varlist, EXP_VARTILDE); 6281556Srgrimes continue; 6291556Srgrimes } 6301556Srgrimes } 6311556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 6321556Srgrimes varflag = 0; 6331556Srgrimes } 6341556Srgrimes *arglist.lastp = NULL; 6351556Srgrimes *varlist.lastp = NULL; 6361556Srgrimes expredir(cmd->ncmd.redirect); 6371556Srgrimes argc = 0; 6381556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 6391556Srgrimes argc++; 6401556Srgrimes argv = stalloc(sizeof (char *) * (argc + 1)); 6411556Srgrimes 6421556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6431556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 6441556Srgrimes *argv++ = sp->text; 6451556Srgrimes } 6461556Srgrimes *argv = NULL; 6471556Srgrimes lastarg = NULL; 6481556Srgrimes if (iflag && funcnest == 0 && argc > 0) 6491556Srgrimes lastarg = argv[-1]; 6501556Srgrimes argv -= argc; 6511556Srgrimes 6521556Srgrimes /* Print the command if xflag is set. */ 6531556Srgrimes if (xflag) { 6541556Srgrimes outc('+', &errout); 6551556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 6561556Srgrimes outc(' ', &errout); 6571556Srgrimes out2str(sp->text); 6581556Srgrimes } 6591556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6601556Srgrimes outc(' ', &errout); 6611556Srgrimes out2str(sp->text); 6621556Srgrimes } 6631556Srgrimes outc('\n', &errout); 6641556Srgrimes flushout(&errout); 6651556Srgrimes } 6661556Srgrimes 6671556Srgrimes /* Now locate the command. */ 6681556Srgrimes if (argc == 0) { 6691556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 6701556Srgrimes cmdentry.u.index = BLTINCMD; 6711556Srgrimes } else { 67217987Speter static const char PATH[] = "PATH="; 67317987Speter char *path = pathval(); 67417987Speter 67517987Speter /* 67617987Speter * Modify the command lookup path, if a PATH= assignment 67717987Speter * is present 67817987Speter */ 67917987Speter for (sp = varlist.list ; sp ; sp = sp->next) 68017987Speter if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) 68117987Speter path = sp->text + sizeof(PATH) - 1; 68217987Speter 68317987Speter find_command(argv[0], &cmdentry, 1, path); 6841556Srgrimes if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */ 68520425Ssteve exitstatus = 127; 6861556Srgrimes flushout(&errout); 6871556Srgrimes return; 6881556Srgrimes } 6891556Srgrimes /* implement the bltin builtin here */ 6901556Srgrimes if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) { 6911556Srgrimes for (;;) { 6921556Srgrimes argv++; 6931556Srgrimes if (--argc == 0) 6941556Srgrimes break; 6951556Srgrimes if ((cmdentry.u.index = find_builtin(*argv)) < 0) { 6961556Srgrimes outfmt(&errout, "%s: not found\n", *argv); 69720425Ssteve exitstatus = 127; 6981556Srgrimes flushout(&errout); 6991556Srgrimes return; 7001556Srgrimes } 7011556Srgrimes if (cmdentry.u.index != BLTINCMD) 7021556Srgrimes break; 7031556Srgrimes } 7041556Srgrimes } 7051556Srgrimes } 7061556Srgrimes 7071556Srgrimes /* Fork off a child process if necessary. */ 7081556Srgrimes if (cmd->ncmd.backgnd 70917987Speter || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0) 71017987Speter || ((flags & EV_BACKCMD) != 0 7111556Srgrimes && (cmdentry.cmdtype != CMDBUILTIN 7121556Srgrimes || cmdentry.u.index == DOTCMD 71317987Speter || cmdentry.u.index == EVALCMD))) { 7141556Srgrimes jp = makejob(cmd, 1); 7151556Srgrimes mode = cmd->ncmd.backgnd; 7161556Srgrimes if (flags & EV_BACKCMD) { 7171556Srgrimes mode = FORK_NOJOB; 7181556Srgrimes if (pipe(pip) < 0) 7191556Srgrimes error("Pipe call failed"); 7201556Srgrimes } 7211556Srgrimes if (forkshell(jp, cmd, mode) != 0) 7221556Srgrimes goto parent; /* at end of routine */ 7231556Srgrimes if (flags & EV_BACKCMD) { 7241556Srgrimes FORCEINTON; 7251556Srgrimes close(pip[0]); 7261556Srgrimes if (pip[1] != 1) { 7271556Srgrimes close(1); 7281556Srgrimes copyfd(pip[1], 1); 7291556Srgrimes close(pip[1]); 7301556Srgrimes } 7311556Srgrimes } 7321556Srgrimes flags |= EV_EXIT; 7331556Srgrimes } 7341556Srgrimes 7351556Srgrimes /* This is the child process if a fork occurred. */ 7361556Srgrimes /* Execute the command. */ 7371556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 73820425Ssteve#ifdef DEBUG 7391556Srgrimes trputs("Shell function: "); trargs(argv); 74020425Ssteve#endif 7411556Srgrimes redirect(cmd->ncmd.redirect, REDIR_PUSH); 7421556Srgrimes saveparam = shellparam; 7431556Srgrimes shellparam.malloc = 0; 74420425Ssteve shellparam.reset = 1; 7451556Srgrimes shellparam.nparam = argc - 1; 7461556Srgrimes shellparam.p = argv + 1; 7471556Srgrimes shellparam.optnext = NULL; 7481556Srgrimes INTOFF; 7491556Srgrimes savelocalvars = localvars; 7501556Srgrimes localvars = NULL; 7511556Srgrimes INTON; 7521556Srgrimes if (setjmp(jmploc.loc)) { 7531556Srgrimes if (exception == EXSHELLPROC) 7541556Srgrimes freeparam((struct shparam *)&saveparam); 7551556Srgrimes else { 7561556Srgrimes freeparam(&shellparam); 7571556Srgrimes shellparam = saveparam; 7581556Srgrimes } 7591556Srgrimes poplocalvars(); 7601556Srgrimes localvars = savelocalvars; 7611556Srgrimes handler = savehandler; 7621556Srgrimes longjmp(handler->loc, 1); 7631556Srgrimes } 7641556Srgrimes savehandler = handler; 7651556Srgrimes handler = &jmploc; 7661556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 7671556Srgrimes mklocal(sp->text); 7681556Srgrimes funcnest++; 76935675Scracauer if (flags & EV_TESTED) 77035675Scracauer evaltree(cmdentry.u.func, EV_TESTED); 77135675Scracauer else 77235675Scracauer evaltree(cmdentry.u.func, 0); 7731556Srgrimes funcnest--; 7741556Srgrimes INTOFF; 7751556Srgrimes poplocalvars(); 7761556Srgrimes localvars = savelocalvars; 7771556Srgrimes freeparam(&shellparam); 7781556Srgrimes shellparam = saveparam; 7791556Srgrimes handler = savehandler; 7801556Srgrimes popredir(); 7811556Srgrimes INTON; 7821556Srgrimes if (evalskip == SKIPFUNC) { 7831556Srgrimes evalskip = 0; 7841556Srgrimes skipcount = 0; 7851556Srgrimes } 7861556Srgrimes if (flags & EV_EXIT) 7871556Srgrimes exitshell(exitstatus); 7881556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 78920425Ssteve#ifdef DEBUG 7901556Srgrimes trputs("builtin command: "); trargs(argv); 79120425Ssteve#endif 7921556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 7931556Srgrimes if (flags == EV_BACKCMD) { 7941556Srgrimes memout.nleft = 0; 7951556Srgrimes memout.nextc = memout.buf; 7961556Srgrimes memout.bufsize = 64; 7971556Srgrimes mode |= REDIR_BACKQ; 7981556Srgrimes } 7991556Srgrimes redirect(cmd->ncmd.redirect, mode); 8001556Srgrimes savecmdname = commandname; 8011556Srgrimes cmdenviron = varlist.list; 8021556Srgrimes e = -1; 8031556Srgrimes if (setjmp(jmploc.loc)) { 8041556Srgrimes e = exception; 8051556Srgrimes exitstatus = (e == EXINT)? SIGINT+128 : 2; 8061556Srgrimes goto cmddone; 8071556Srgrimes } 8081556Srgrimes savehandler = handler; 8091556Srgrimes handler = &jmploc; 8101556Srgrimes commandname = argv[0]; 8111556Srgrimes argptr = argv + 1; 8121556Srgrimes optptr = NULL; /* initialize nextopt */ 8131556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 8141556Srgrimes flushall(); 8151556Srgrimescmddone: 8161556Srgrimes out1 = &output; 8171556Srgrimes out2 = &errout; 8181556Srgrimes freestdout(); 8191556Srgrimes if (e != EXSHELLPROC) { 8201556Srgrimes commandname = savecmdname; 8211556Srgrimes if (flags & EV_EXIT) { 8221556Srgrimes exitshell(exitstatus); 8231556Srgrimes } 8241556Srgrimes } 8251556Srgrimes handler = savehandler; 8261556Srgrimes if (e != -1) { 82720425Ssteve if ((e != EXERROR && e != EXEXEC) 82820425Ssteve || cmdentry.u.index == BLTINCMD 82920425Ssteve || cmdentry.u.index == DOTCMD 83020425Ssteve || cmdentry.u.index == EVALCMD 83117987Speter#ifndef NO_HISTORY 83220425Ssteve || cmdentry.u.index == HISTCMD 83317987Speter#endif 83420425Ssteve || cmdentry.u.index == EXECCMD) 8351556Srgrimes exraise(e); 8361556Srgrimes FORCEINTON; 8371556Srgrimes } 8381556Srgrimes if (cmdentry.u.index != EXECCMD) 8391556Srgrimes popredir(); 8401556Srgrimes if (flags == EV_BACKCMD) { 8411556Srgrimes backcmd->buf = memout.buf; 8421556Srgrimes backcmd->nleft = memout.nextc - memout.buf; 8431556Srgrimes memout.buf = NULL; 8441556Srgrimes } 8451556Srgrimes } else { 84620425Ssteve#ifdef DEBUG 8471556Srgrimes trputs("normal command: "); trargs(argv); 84820425Ssteve#endif 8491556Srgrimes clearredir(); 8501556Srgrimes redirect(cmd->ncmd.redirect, 0); 8511556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8521556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 8531556Srgrimes envp = environment(); 85417987Speter shellexec(argv, envp, pathval(), cmdentry.u.index); 8551556Srgrimes /*NOTREACHED*/ 8561556Srgrimes } 8571556Srgrimes goto out; 8581556Srgrimes 8591556Srgrimesparent: /* parent process gets here (if we forked) */ 8601556Srgrimes if (mode == 0) { /* argument to fork */ 8611556Srgrimes INTOFF; 86219683Speter exitstatus = waitforjob(jp); 8631556Srgrimes INTON; 8641556Srgrimes } else if (mode == 2) { 8651556Srgrimes backcmd->fd = pip[0]; 8661556Srgrimes close(pip[1]); 8671556Srgrimes backcmd->jp = jp; 8681556Srgrimes } 8691556Srgrimes 8701556Srgrimesout: 8711556Srgrimes if (lastarg) 8721556Srgrimes setvar("_", lastarg, 0); 8731556Srgrimes popstackmark(&smark); 8741556Srgrimes} 8751556Srgrimes 8761556Srgrimes 8771556Srgrimes 8781556Srgrimes/* 8791556Srgrimes * Search for a command. This is called before we fork so that the 8801556Srgrimes * location of the command will be available in the parent as well as 8811556Srgrimes * the child. The check for "goodname" is an overly conservative 8821556Srgrimes * check that the name will not be subject to expansion. 8831556Srgrimes */ 8841556Srgrimes 8851556SrgrimesSTATIC void 8861556Srgrimesprehash(n) 8871556Srgrimes union node *n; 88817987Speter{ 8891556Srgrimes struct cmdentry entry; 8901556Srgrimes 89117987Speter if (n->type == NCMD && n->ncmd.args) 89217987Speter if (goodname(n->ncmd.args->narg.text)) 89317987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 89417987Speter pathval()); 8951556Srgrimes} 8961556Srgrimes 8971556Srgrimes 8981556Srgrimes 8991556Srgrimes/* 9001556Srgrimes * Builtin commands. Builtin commands whose functions are closely 9011556Srgrimes * tied to evaluation are implemented here. 9021556Srgrimes */ 9031556Srgrimes 9041556Srgrimes/* 9051556Srgrimes * No command given, or a bltin command with no arguments. Set the 9061556Srgrimes * specified variables. 9071556Srgrimes */ 9081556Srgrimes 90917987Speterint 91017987Speterbltincmd(argc, argv) 91125905Ssteve int argc __unused; 91225905Ssteve char **argv __unused; 91317987Speter{ 9141556Srgrimes listsetvar(cmdenviron); 91520425Ssteve /* 91617987Speter * Preserve exitstatus of a previous possible redirection 91720425Ssteve * as POSIX mandates 91817987Speter */ 9191556Srgrimes return exitstatus; 9201556Srgrimes} 9211556Srgrimes 9221556Srgrimes 9231556Srgrimes/* 9241556Srgrimes * Handle break and continue commands. Break, continue, and return are 9251556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 9261556Srgrimes * above all check this flag, and if it is set they start skipping 9271556Srgrimes * commands rather than executing them. The variable skipcount is 9281556Srgrimes * the number of loops to break/continue, or the number of function 9291556Srgrimes * levels to return. (The latter is always 1.) It should probably 9301556Srgrimes * be an error to break out of more loops than exist, but it isn't 9311556Srgrimes * in the standard shell so we don't make it one here. 9321556Srgrimes */ 9331556Srgrimes 93417987Speterint 93517987Speterbreakcmd(argc, argv) 93617987Speter int argc; 93720425Ssteve char **argv; 93817987Speter{ 93920425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 9401556Srgrimes 9411556Srgrimes if (n > loopnest) 9421556Srgrimes n = loopnest; 9431556Srgrimes if (n > 0) { 9441556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 9451556Srgrimes skipcount = n; 9461556Srgrimes } 9471556Srgrimes return 0; 9481556Srgrimes} 9491556Srgrimes 9501556Srgrimes 9511556Srgrimes/* 9521556Srgrimes * The return command. 9531556Srgrimes */ 9541556Srgrimes 95517987Speterint 95620425Sstevereturncmd(argc, argv) 95717987Speter int argc; 95820425Ssteve char **argv; 95917987Speter{ 96020425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 9611556Srgrimes 9621556Srgrimes if (funcnest) { 9631556Srgrimes evalskip = SKIPFUNC; 9641556Srgrimes skipcount = 1; 96520425Ssteve } else { 96620425Ssteve /* skip the rest of the file */ 96720425Ssteve evalskip = SKIPFILE; 96820425Ssteve skipcount = 1; 9691556Srgrimes } 9701556Srgrimes return ret; 9711556Srgrimes} 9721556Srgrimes 9731556Srgrimes 97417987Speterint 97520425Sstevefalsecmd(argc, argv) 97625905Ssteve int argc __unused; 97725905Ssteve char **argv __unused; 97817987Speter{ 97917987Speter return 1; 98017987Speter} 98117987Speter 98217987Speter 98317987Speterint 98420425Sstevetruecmd(argc, argv) 98525905Ssteve int argc __unused; 98625905Ssteve char **argv __unused; 98717987Speter{ 9881556Srgrimes return 0; 9891556Srgrimes} 9901556Srgrimes 9911556Srgrimes 99217987Speterint 99320425Ssteveexeccmd(argc, argv) 99417987Speter int argc; 99520425Ssteve char **argv; 99617987Speter{ 9971556Srgrimes if (argc > 1) { 99817987Speter struct strlist *sp; 99917987Speter 10001556Srgrimes iflag = 0; /* exit on error */ 10011556Srgrimes mflag = 0; 10021556Srgrimes optschanged(); 100317987Speter for (sp = cmdenviron; sp ; sp = sp->next) 100417987Speter setvareq(sp->text, VEXPORT|VSTACK); 10051556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 10061556Srgrimes 10071556Srgrimes } 10081556Srgrimes return 0; 10091556Srgrimes} 1010