eval.c revision 17987
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. 353044Sdg * 3617987Speter * $Id: eval.c,v 1.4 1995/09/20 08:30:56 davidg Exp $ 371556Srgrimes */ 381556Srgrimes 391556Srgrimes#ifndef lint 4017987Speterstatic char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; 411556Srgrimes#endif /* not lint */ 421556Srgrimes 4317987Speter#include <signal.h> 4417987Speter#include <unistd.h> 4517987Speter 461556Srgrimes/* 471556Srgrimes * Evaluate a command. 481556Srgrimes */ 491556Srgrimes 501556Srgrimes#include "shell.h" 511556Srgrimes#include "nodes.h" 521556Srgrimes#include "syntax.h" 531556Srgrimes#include "expand.h" 541556Srgrimes#include "parser.h" 551556Srgrimes#include "jobs.h" 561556Srgrimes#include "eval.h" 571556Srgrimes#include "builtins.h" 581556Srgrimes#include "options.h" 591556Srgrimes#include "exec.h" 601556Srgrimes#include "redir.h" 611556Srgrimes#include "input.h" 621556Srgrimes#include "output.h" 631556Srgrimes#include "trap.h" 641556Srgrimes#include "var.h" 651556Srgrimes#include "memalloc.h" 661556Srgrimes#include "error.h" 6717987Speter#include "show.h" 681556Srgrimes#include "mystring.h" 6917987Speter#ifndef NO_HISTORY 701556Srgrimes#include "myhistedit.h" 7117987Speter#endif 721556Srgrimes 731556Srgrimes 741556Srgrimes/* flags in argument to evaltree */ 751556Srgrimes#define EV_EXIT 01 /* exit after evaluating tree */ 761556Srgrimes#define EV_TESTED 02 /* exit status is checked; ignore -e flag */ 771556Srgrimes#define EV_BACKCMD 04 /* command executing within back quotes */ 781556Srgrimes 791556Srgrimes 801556Srgrimes/* reasons for skipping commands (see comment on breakcmd routine) */ 811556Srgrimes#define SKIPBREAK 1 821556Srgrimes#define SKIPCONT 2 831556Srgrimes#define SKIPFUNC 3 841556Srgrimes 851556SrgrimesMKINIT int evalskip; /* set if we are skipping commands */ 861556SrgrimesSTATIC int skipcount; /* number of levels to skip */ 871556SrgrimesMKINIT int loopnest; /* current loop nesting level */ 881556Srgrimesint funcnest; /* depth of function calls */ 891556Srgrimes 901556Srgrimes 911556Srgrimeschar *commandname; 921556Srgrimesstruct strlist *cmdenviron; 931556Srgrimesint exitstatus; /* exit status of last command */ 9417987Speterint oexitstatus; /* saved exit status */ 951556Srgrimes 961556Srgrimes 9717987SpeterSTATIC void evalloop __P((union node *)); 9817987SpeterSTATIC void evalfor __P((union node *)); 9917987SpeterSTATIC void evalcase __P((union node *, int)); 10017987SpeterSTATIC void evalsubshell __P((union node *, int)); 10117987SpeterSTATIC void expredir __P((union node *)); 10217987SpeterSTATIC void evalpipe __P((union node *)); 10317987SpeterSTATIC void evalcommand __P((union node *, int, struct backcmd *)); 10417987SpeterSTATIC void prehash __P((union node *)); 1051556Srgrimes 1061556Srgrimes 1071556Srgrimes/* 1081556Srgrimes * Called to reset things after an exception. 1091556Srgrimes */ 1101556Srgrimes 1111556Srgrimes#ifdef mkinit 1121556SrgrimesINCLUDE "eval.h" 1131556Srgrimes 1141556SrgrimesRESET { 1151556Srgrimes evalskip = 0; 1161556Srgrimes loopnest = 0; 1171556Srgrimes funcnest = 0; 1181556Srgrimes} 1191556Srgrimes 1201556SrgrimesSHELLPROC { 1211556Srgrimes exitstatus = 0; 1221556Srgrimes} 1231556Srgrimes#endif 1241556Srgrimes 1251556Srgrimes 1261556Srgrimes 1271556Srgrimes/* 1281556Srgrimes * The eval commmand. 1291556Srgrimes */ 1301556Srgrimes 13117987Speterint 13217987Speterevalcmd(argc, argv) 13317987Speter int argc; 13417987Speter char **argv; 1351556Srgrimes{ 1361556Srgrimes char *p; 1371556Srgrimes char *concat; 1381556Srgrimes char **ap; 1391556Srgrimes 1401556Srgrimes if (argc > 1) { 1411556Srgrimes p = argv[1]; 1421556Srgrimes if (argc > 2) { 1431556Srgrimes STARTSTACKSTR(concat); 1441556Srgrimes ap = argv + 2; 1451556Srgrimes for (;;) { 1461556Srgrimes while (*p) 1471556Srgrimes STPUTC(*p++, concat); 1481556Srgrimes if ((p = *ap++) == NULL) 1491556Srgrimes break; 1501556Srgrimes STPUTC(' ', concat); 1511556Srgrimes } 1521556Srgrimes STPUTC('\0', concat); 1531556Srgrimes p = grabstackstr(concat); 1541556Srgrimes } 1551556Srgrimes evalstring(p); 1561556Srgrimes } 1571556Srgrimes return exitstatus; 1581556Srgrimes} 1591556Srgrimes 1601556Srgrimes 1611556Srgrimes/* 1621556Srgrimes * Execute a command or commands contained in a string. 1631556Srgrimes */ 1641556Srgrimes 1651556Srgrimesvoid 1661556Srgrimesevalstring(s) 1671556Srgrimes char *s; 1681556Srgrimes { 1691556Srgrimes union node *n; 1701556Srgrimes struct stackmark smark; 1711556Srgrimes 1721556Srgrimes setstackmark(&smark); 1731556Srgrimes setinputstring(s, 1); 1741556Srgrimes while ((n = parsecmd(0)) != NEOF) { 1751556Srgrimes evaltree(n, 0); 1761556Srgrimes popstackmark(&smark); 1771556Srgrimes } 1781556Srgrimes popfile(); 1791556Srgrimes popstackmark(&smark); 1801556Srgrimes} 1811556Srgrimes 1821556Srgrimes 1831556Srgrimes 1841556Srgrimes/* 1851556Srgrimes * Evaluate a parse tree. The value is left in the global variable 1861556Srgrimes * exitstatus. 1871556Srgrimes */ 1881556Srgrimes 1891556Srgrimesvoid 1901556Srgrimesevaltree(n, flags) 1911556Srgrimes union node *n; 19217987Speter int flags; 19317987Speter{ 1941556Srgrimes if (n == NULL) { 1951556Srgrimes TRACE(("evaltree(NULL) called\n")); 1961556Srgrimes exitstatus = 0; 1971556Srgrimes goto out; 1981556Srgrimes } 19917987Speter#ifndef NO_HISTORY 2001556Srgrimes displayhist = 1; /* show history substitutions done with fc */ 20117987Speter#endif 20217987Speter TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type)); 2031556Srgrimes switch (n->type) { 2041556Srgrimes case NSEMI: 2051556Srgrimes evaltree(n->nbinary.ch1, 0); 2061556Srgrimes if (evalskip) 2071556Srgrimes goto out; 2081556Srgrimes evaltree(n->nbinary.ch2, flags); 2091556Srgrimes break; 2101556Srgrimes case NAND: 2111556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2121556Srgrimes if (evalskip || exitstatus != 0) 2131556Srgrimes goto out; 2141556Srgrimes evaltree(n->nbinary.ch2, flags); 2151556Srgrimes break; 2161556Srgrimes case NOR: 2171556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2181556Srgrimes if (evalskip || exitstatus == 0) 2191556Srgrimes goto out; 2201556Srgrimes evaltree(n->nbinary.ch2, flags); 2211556Srgrimes break; 2221556Srgrimes case NREDIR: 2231556Srgrimes expredir(n->nredir.redirect); 2241556Srgrimes redirect(n->nredir.redirect, REDIR_PUSH); 2251556Srgrimes evaltree(n->nredir.n, flags); 2261556Srgrimes popredir(); 2271556Srgrimes break; 2281556Srgrimes case NSUBSHELL: 2291556Srgrimes evalsubshell(n, flags); 2301556Srgrimes break; 2311556Srgrimes case NBACKGND: 2321556Srgrimes evalsubshell(n, flags); 2331556Srgrimes break; 2341556Srgrimes case NIF: { 23517987Speter int status; 2361556Srgrimes 2371556Srgrimes evaltree(n->nif.test, EV_TESTED); 23817987Speter status = exitstatus; 23917987Speter exitstatus = 0; 2401556Srgrimes if (evalskip) 2411556Srgrimes goto out; 24217987Speter if (status == 0) 2431556Srgrimes evaltree(n->nif.ifpart, flags); 24417987Speter else if (n->nif.elsepart) 2451556Srgrimes evaltree(n->nif.elsepart, flags); 2461556Srgrimes break; 2471556Srgrimes } 2481556Srgrimes case NWHILE: 2491556Srgrimes case NUNTIL: 2501556Srgrimes evalloop(n); 2511556Srgrimes break; 2521556Srgrimes case NFOR: 2531556Srgrimes evalfor(n); 2541556Srgrimes break; 2551556Srgrimes case NCASE: 2561556Srgrimes evalcase(n, flags); 2571556Srgrimes break; 2581556Srgrimes case NDEFUN: 2591556Srgrimes defun(n->narg.text, n->narg.next); 2601556Srgrimes exitstatus = 0; 2611556Srgrimes break; 2621556Srgrimes case NNOT: 2631556Srgrimes evaltree(n->nnot.com, EV_TESTED); 2641556Srgrimes exitstatus = !exitstatus; 2651556Srgrimes break; 2661556Srgrimes 2671556Srgrimes case NPIPE: 2681556Srgrimes evalpipe(n); 2691556Srgrimes break; 2701556Srgrimes case NCMD: 2711556Srgrimes evalcommand(n, flags, (struct backcmd *)NULL); 2721556Srgrimes break; 2731556Srgrimes default: 2741556Srgrimes out1fmt("Node type = %d\n", n->type); 2751556Srgrimes flushout(&output); 2761556Srgrimes break; 2771556Srgrimes } 2781556Srgrimesout: 2791556Srgrimes if (pendingsigs) 2801556Srgrimes dotrap(); 2811556Srgrimes if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED))) 2821556Srgrimes exitshell(exitstatus); 2831556Srgrimes} 2841556Srgrimes 2851556Srgrimes 2861556SrgrimesSTATIC void 2871556Srgrimesevalloop(n) 2881556Srgrimes union node *n; 28917987Speter{ 2901556Srgrimes int status; 2911556Srgrimes 2921556Srgrimes loopnest++; 2931556Srgrimes status = 0; 2941556Srgrimes for (;;) { 2951556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2961556Srgrimes if (evalskip) { 2971556Srgrimesskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 2981556Srgrimes evalskip = 0; 2991556Srgrimes continue; 3001556Srgrimes } 3011556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3021556Srgrimes evalskip = 0; 3031556Srgrimes break; 3041556Srgrimes } 3051556Srgrimes if (n->type == NWHILE) { 3061556Srgrimes if (exitstatus != 0) 3071556Srgrimes break; 3081556Srgrimes } else { 3091556Srgrimes if (exitstatus == 0) 3101556Srgrimes break; 3111556Srgrimes } 3121556Srgrimes evaltree(n->nbinary.ch2, 0); 3131556Srgrimes status = exitstatus; 3141556Srgrimes if (evalskip) 3151556Srgrimes goto skipping; 3161556Srgrimes } 3171556Srgrimes loopnest--; 3181556Srgrimes exitstatus = status; 3191556Srgrimes} 3201556Srgrimes 3211556Srgrimes 3221556Srgrimes 3231556SrgrimesSTATIC void 3241556Srgrimesevalfor(n) 32517987Speter union node *n; 32617987Speter{ 3271556Srgrimes struct arglist arglist; 3281556Srgrimes union node *argp; 3291556Srgrimes struct strlist *sp; 3301556Srgrimes struct stackmark smark; 3311556Srgrimes 3321556Srgrimes setstackmark(&smark); 3331556Srgrimes arglist.lastp = &arglist.list; 3341556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 33517987Speter oexitstatus = exitstatus; 3361556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3371556Srgrimes if (evalskip) 3381556Srgrimes goto out; 3391556Srgrimes } 3401556Srgrimes *arglist.lastp = NULL; 3411556Srgrimes 3421556Srgrimes exitstatus = 0; 3431556Srgrimes loopnest++; 3441556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3451556Srgrimes setvar(n->nfor.var, sp->text, 0); 3461556Srgrimes evaltree(n->nfor.body, 0); 3471556Srgrimes if (evalskip) { 3481556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3491556Srgrimes evalskip = 0; 3501556Srgrimes continue; 3511556Srgrimes } 3521556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3531556Srgrimes evalskip = 0; 3541556Srgrimes break; 3551556Srgrimes } 3561556Srgrimes } 3571556Srgrimes loopnest--; 3581556Srgrimesout: 3591556Srgrimes popstackmark(&smark); 3601556Srgrimes} 3611556Srgrimes 3621556Srgrimes 3631556Srgrimes 3641556SrgrimesSTATIC void 3651556Srgrimesevalcase(n, flags) 3661556Srgrimes union node *n; 36717987Speter int flags; 36817987Speter{ 3691556Srgrimes union node *cp; 3701556Srgrimes union node *patp; 3711556Srgrimes struct arglist arglist; 3721556Srgrimes struct stackmark smark; 3731556Srgrimes 3741556Srgrimes setstackmark(&smark); 3751556Srgrimes arglist.lastp = &arglist.list; 37617987Speter oexitstatus = exitstatus; 3771556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3781556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3791556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3801556Srgrimes if (casematch(patp, arglist.list->text)) { 3811556Srgrimes if (evalskip == 0) { 3821556Srgrimes evaltree(cp->nclist.body, flags); 3831556Srgrimes } 3841556Srgrimes goto out; 3851556Srgrimes } 3861556Srgrimes } 3871556Srgrimes } 3881556Srgrimesout: 3891556Srgrimes popstackmark(&smark); 3901556Srgrimes} 3911556Srgrimes 3921556Srgrimes 3931556Srgrimes 3941556Srgrimes/* 3951556Srgrimes * Kick off a subshell to evaluate a tree. 3961556Srgrimes */ 3971556Srgrimes 3981556SrgrimesSTATIC void 3991556Srgrimesevalsubshell(n, flags) 4001556Srgrimes union node *n; 40117987Speter int flags; 40217987Speter{ 4031556Srgrimes struct job *jp; 4041556Srgrimes int backgnd = (n->type == NBACKGND); 4051556Srgrimes 4061556Srgrimes expredir(n->nredir.redirect); 4071556Srgrimes jp = makejob(n, 1); 4081556Srgrimes if (forkshell(jp, n, backgnd) == 0) { 4091556Srgrimes if (backgnd) 4101556Srgrimes flags &=~ EV_TESTED; 4111556Srgrimes redirect(n->nredir.redirect, 0); 4121556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 4131556Srgrimes } 4141556Srgrimes if (! backgnd) { 4151556Srgrimes INTOFF; 4161556Srgrimes exitstatus = waitforjob(jp); 4171556Srgrimes INTON; 4181556Srgrimes } 4191556Srgrimes} 4201556Srgrimes 4211556Srgrimes 4221556Srgrimes 4231556Srgrimes/* 4241556Srgrimes * Compute the names of the files in a redirection list. 4251556Srgrimes */ 4261556Srgrimes 4271556SrgrimesSTATIC void 4281556Srgrimesexpredir(n) 4291556Srgrimes union node *n; 43017987Speter{ 4311556Srgrimes register union node *redir; 4321556Srgrimes 4331556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 43417987Speter struct arglist fn; 43517987Speter fn.lastp = &fn.list; 43617987Speter oexitstatus = exitstatus; 43717987Speter switch (redir->type) { 43817987Speter case NFROM: 43917987Speter case NTO: 44017987Speter case NAPPEND: 4411556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4421556Srgrimes redir->nfile.expfname = fn.list->text; 44317987Speter break; 44417987Speter case NFROMFD: 44517987Speter case NTOFD: 44617987Speter if (redir->ndup.vname) { 44717987Speter expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); 44817987Speter fixredir(redir, fn.list->text, 1); 44917987Speter } 45017987Speter break; 4511556Srgrimes } 4521556Srgrimes } 4531556Srgrimes} 4541556Srgrimes 4551556Srgrimes 4561556Srgrimes 4571556Srgrimes/* 4581556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 4591556Srgrimes * of the process creating the pipeline. (This differs from some versions 4601556Srgrimes * of the shell, which make the last process in a pipeline the parent 4611556Srgrimes * of all the rest.) 4621556Srgrimes */ 4631556Srgrimes 4641556SrgrimesSTATIC void 4651556Srgrimesevalpipe(n) 4661556Srgrimes union node *n; 46717987Speter{ 4681556Srgrimes struct job *jp; 4691556Srgrimes struct nodelist *lp; 4701556Srgrimes int pipelen; 4711556Srgrimes int prevfd; 4721556Srgrimes int pip[2]; 4731556Srgrimes 47417987Speter TRACE(("evalpipe(0x%lx) called\n", (long)n)); 4751556Srgrimes pipelen = 0; 4761556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 4771556Srgrimes pipelen++; 4781556Srgrimes INTOFF; 4791556Srgrimes jp = makejob(n, pipelen); 4801556Srgrimes prevfd = -1; 4811556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 4821556Srgrimes prehash(lp->n); 4831556Srgrimes pip[1] = -1; 4841556Srgrimes if (lp->next) { 4851556Srgrimes if (pipe(pip) < 0) { 4861556Srgrimes close(prevfd); 4871556Srgrimes error("Pipe call failed"); 4881556Srgrimes } 4891556Srgrimes } 4901556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 4911556Srgrimes INTON; 4921556Srgrimes if (prevfd > 0) { 4931556Srgrimes close(0); 4941556Srgrimes copyfd(prevfd, 0); 4951556Srgrimes close(prevfd); 4961556Srgrimes } 4971556Srgrimes if (pip[1] >= 0) { 4981556Srgrimes close(pip[0]); 4991556Srgrimes if (pip[1] != 1) { 5001556Srgrimes close(1); 5011556Srgrimes copyfd(pip[1], 1); 5021556Srgrimes close(pip[1]); 5031556Srgrimes } 5041556Srgrimes } 5051556Srgrimes evaltree(lp->n, EV_EXIT); 5061556Srgrimes } 5071556Srgrimes if (prevfd >= 0) 5081556Srgrimes close(prevfd); 5091556Srgrimes prevfd = pip[0]; 5101556Srgrimes close(pip[1]); 5111556Srgrimes } 5121556Srgrimes INTON; 5131556Srgrimes if (n->npipe.backgnd == 0) { 5141556Srgrimes INTOFF; 5151556Srgrimes exitstatus = waitforjob(jp); 5161556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5171556Srgrimes INTON; 5181556Srgrimes } 5191556Srgrimes} 5201556Srgrimes 5211556Srgrimes 5221556Srgrimes 5231556Srgrimes/* 5241556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5251556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5261556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5271556Srgrimes * Should be called with interrupts off. 5281556Srgrimes */ 5291556Srgrimes 5301556Srgrimesvoid 5311556Srgrimesevalbackcmd(n, result) 5321556Srgrimes union node *n; 5331556Srgrimes struct backcmd *result; 53417987Speter{ 5351556Srgrimes int pip[2]; 5361556Srgrimes struct job *jp; 5371556Srgrimes struct stackmark smark; /* unnecessary */ 5381556Srgrimes 5391556Srgrimes setstackmark(&smark); 5401556Srgrimes result->fd = -1; 5411556Srgrimes result->buf = NULL; 5421556Srgrimes result->nleft = 0; 5431556Srgrimes result->jp = NULL; 54417987Speter if (n == NULL) { 54517987Speter exitstatus = 0; 5461556Srgrimes goto out; 54717987Speter } 5481556Srgrimes if (n->type == NCMD) { 54917987Speter exitstatus = oexitstatus; 5501556Srgrimes evalcommand(n, EV_BACKCMD, result); 5511556Srgrimes } else { 55217987Speter exitstatus = 0; 5531556Srgrimes if (pipe(pip) < 0) 5541556Srgrimes error("Pipe call failed"); 5551556Srgrimes jp = makejob(n, 1); 5561556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 5571556Srgrimes FORCEINTON; 5581556Srgrimes close(pip[0]); 5591556Srgrimes if (pip[1] != 1) { 5601556Srgrimes close(1); 5611556Srgrimes copyfd(pip[1], 1); 5621556Srgrimes close(pip[1]); 5631556Srgrimes } 5641556Srgrimes evaltree(n, EV_EXIT); 5651556Srgrimes } 5661556Srgrimes close(pip[1]); 5671556Srgrimes result->fd = pip[0]; 5681556Srgrimes result->jp = jp; 5691556Srgrimes } 5701556Srgrimesout: 5711556Srgrimes popstackmark(&smark); 5721556Srgrimes TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", 5731556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 5741556Srgrimes} 5751556Srgrimes 5761556Srgrimes 5771556Srgrimes 5781556Srgrimes/* 5791556Srgrimes * Execute a simple command. 5801556Srgrimes */ 5811556Srgrimes 5821556SrgrimesSTATIC void 5831556Srgrimesevalcommand(cmd, flags, backcmd) 5841556Srgrimes union node *cmd; 58517987Speter int flags; 5861556Srgrimes struct backcmd *backcmd; 58717987Speter{ 5881556Srgrimes struct stackmark smark; 5891556Srgrimes union node *argp; 5901556Srgrimes struct arglist arglist; 5911556Srgrimes struct arglist varlist; 5921556Srgrimes char **argv; 5931556Srgrimes int argc; 5941556Srgrimes char **envp; 5951556Srgrimes int varflag; 5961556Srgrimes struct strlist *sp; 5971556Srgrimes int mode; 5981556Srgrimes int pip[2]; 5991556Srgrimes struct cmdentry cmdentry; 6001556Srgrimes struct job *jp; 6011556Srgrimes struct jmploc jmploc; 6021556Srgrimes struct jmploc *volatile savehandler; 6031556Srgrimes char *volatile savecmdname; 6041556Srgrimes volatile struct shparam saveparam; 6051556Srgrimes struct localvar *volatile savelocalvars; 6061556Srgrimes volatile int e; 6071556Srgrimes char *lastarg; 60817987Speter#if __GNUC__ 60917987Speter /* Avoid longjmp clobbering */ 61017987Speter (void) &argv; 61117987Speter (void) &argc; 61217987Speter (void) &lastarg; 61317987Speter (void) &flags; 61417987Speter#endif 6151556Srgrimes 6161556Srgrimes /* First expand the arguments. */ 61717987Speter TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); 6181556Srgrimes setstackmark(&smark); 6191556Srgrimes arglist.lastp = &arglist.list; 6201556Srgrimes varlist.lastp = &varlist.list; 6211556Srgrimes varflag = 1; 62217987Speter oexitstatus = exitstatus; 62317987Speter exitstatus = 0; 6241556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 62517987Speter char *p = argp->narg.text; 6261556Srgrimes if (varflag && is_name(*p)) { 6271556Srgrimes do { 6281556Srgrimes p++; 6291556Srgrimes } while (is_in_name(*p)); 6301556Srgrimes if (*p == '=') { 6311556Srgrimes expandarg(argp, &varlist, EXP_VARTILDE); 6321556Srgrimes continue; 6331556Srgrimes } 6341556Srgrimes } 6351556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 6361556Srgrimes varflag = 0; 6371556Srgrimes } 6381556Srgrimes *arglist.lastp = NULL; 6391556Srgrimes *varlist.lastp = NULL; 6401556Srgrimes expredir(cmd->ncmd.redirect); 6411556Srgrimes argc = 0; 6421556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 6431556Srgrimes argc++; 6441556Srgrimes argv = stalloc(sizeof (char *) * (argc + 1)); 6451556Srgrimes 6461556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6471556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 6481556Srgrimes *argv++ = sp->text; 6491556Srgrimes } 6501556Srgrimes *argv = NULL; 6511556Srgrimes lastarg = NULL; 6521556Srgrimes if (iflag && funcnest == 0 && argc > 0) 6531556Srgrimes lastarg = argv[-1]; 6541556Srgrimes argv -= argc; 6551556Srgrimes 6561556Srgrimes /* Print the command if xflag is set. */ 6571556Srgrimes if (xflag) { 6581556Srgrimes outc('+', &errout); 6591556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 6601556Srgrimes outc(' ', &errout); 6611556Srgrimes out2str(sp->text); 6621556Srgrimes } 6631556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6641556Srgrimes outc(' ', &errout); 6651556Srgrimes out2str(sp->text); 6661556Srgrimes } 6671556Srgrimes outc('\n', &errout); 6681556Srgrimes flushout(&errout); 6691556Srgrimes } 6701556Srgrimes 6711556Srgrimes /* Now locate the command. */ 6721556Srgrimes if (argc == 0) { 6731556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 6741556Srgrimes cmdentry.u.index = BLTINCMD; 6751556Srgrimes } else { 67617987Speter static const char PATH[] = "PATH="; 67717987Speter char *path = pathval(); 67817987Speter 67917987Speter /* 68017987Speter * Modify the command lookup path, if a PATH= assignment 68117987Speter * is present 68217987Speter */ 68317987Speter for (sp = varlist.list ; sp ; sp = sp->next) 68417987Speter if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) 68517987Speter path = sp->text + sizeof(PATH) - 1; 68617987Speter 68717987Speter find_command(argv[0], &cmdentry, 1, path); 6881556Srgrimes if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */ 68917987Speter exitstatus = 1; 6901556Srgrimes flushout(&errout); 6911556Srgrimes return; 6921556Srgrimes } 6931556Srgrimes /* implement the bltin builtin here */ 6941556Srgrimes if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) { 6951556Srgrimes for (;;) { 6961556Srgrimes argv++; 6971556Srgrimes if (--argc == 0) 6981556Srgrimes break; 6991556Srgrimes if ((cmdentry.u.index = find_builtin(*argv)) < 0) { 7001556Srgrimes outfmt(&errout, "%s: not found\n", *argv); 70117987Speter exitstatus = 1; 7021556Srgrimes flushout(&errout); 7031556Srgrimes return; 7041556Srgrimes } 7051556Srgrimes if (cmdentry.u.index != BLTINCMD) 7061556Srgrimes break; 7071556Srgrimes } 7081556Srgrimes } 7091556Srgrimes } 7101556Srgrimes 7111556Srgrimes /* Fork off a child process if necessary. */ 7121556Srgrimes if (cmd->ncmd.backgnd 71317987Speter || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0) 71417987Speter || ((flags & EV_BACKCMD) != 0 7151556Srgrimes && (cmdentry.cmdtype != CMDBUILTIN 7161556Srgrimes || cmdentry.u.index == DOTCMD 71717987Speter || cmdentry.u.index == EVALCMD))) { 7181556Srgrimes jp = makejob(cmd, 1); 7191556Srgrimes mode = cmd->ncmd.backgnd; 7201556Srgrimes if (flags & EV_BACKCMD) { 7211556Srgrimes mode = FORK_NOJOB; 7221556Srgrimes if (pipe(pip) < 0) 7231556Srgrimes error("Pipe call failed"); 7241556Srgrimes } 7251556Srgrimes if (forkshell(jp, cmd, mode) != 0) 7261556Srgrimes goto parent; /* at end of routine */ 7271556Srgrimes if (flags & EV_BACKCMD) { 7281556Srgrimes FORCEINTON; 7291556Srgrimes close(pip[0]); 7301556Srgrimes if (pip[1] != 1) { 7311556Srgrimes close(1); 7321556Srgrimes copyfd(pip[1], 1); 7331556Srgrimes close(pip[1]); 7341556Srgrimes } 7351556Srgrimes } 7361556Srgrimes flags |= EV_EXIT; 7371556Srgrimes } 7381556Srgrimes 7391556Srgrimes /* This is the child process if a fork occurred. */ 7401556Srgrimes /* Execute the command. */ 7411556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 7421556Srgrimes trputs("Shell function: "); trargs(argv); 7431556Srgrimes redirect(cmd->ncmd.redirect, REDIR_PUSH); 7441556Srgrimes saveparam = shellparam; 7451556Srgrimes shellparam.malloc = 0; 7461556Srgrimes shellparam.nparam = argc - 1; 7471556Srgrimes shellparam.p = argv + 1; 7481556Srgrimes shellparam.optnext = NULL; 7491556Srgrimes INTOFF; 7501556Srgrimes savelocalvars = localvars; 7511556Srgrimes localvars = NULL; 7521556Srgrimes INTON; 7531556Srgrimes if (setjmp(jmploc.loc)) { 7541556Srgrimes if (exception == EXSHELLPROC) 7551556Srgrimes freeparam((struct shparam *)&saveparam); 7561556Srgrimes else { 7571556Srgrimes freeparam(&shellparam); 7581556Srgrimes shellparam = saveparam; 7591556Srgrimes } 7601556Srgrimes poplocalvars(); 7611556Srgrimes localvars = savelocalvars; 7621556Srgrimes handler = savehandler; 7631556Srgrimes longjmp(handler->loc, 1); 7641556Srgrimes } 7651556Srgrimes savehandler = handler; 7661556Srgrimes handler = &jmploc; 7671556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 7681556Srgrimes mklocal(sp->text); 7691556Srgrimes funcnest++; 7701556Srgrimes evaltree(cmdentry.u.func, 0); 7711556Srgrimes funcnest--; 7721556Srgrimes INTOFF; 7731556Srgrimes poplocalvars(); 7741556Srgrimes localvars = savelocalvars; 7751556Srgrimes freeparam(&shellparam); 7761556Srgrimes shellparam = saveparam; 7771556Srgrimes handler = savehandler; 7781556Srgrimes popredir(); 7791556Srgrimes INTON; 7801556Srgrimes if (evalskip == SKIPFUNC) { 7811556Srgrimes evalskip = 0; 7821556Srgrimes skipcount = 0; 7831556Srgrimes } 7841556Srgrimes if (flags & EV_EXIT) 7851556Srgrimes exitshell(exitstatus); 7861556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 7871556Srgrimes trputs("builtin command: "); trargs(argv); 7881556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 7891556Srgrimes if (flags == EV_BACKCMD) { 7901556Srgrimes memout.nleft = 0; 7911556Srgrimes memout.nextc = memout.buf; 7921556Srgrimes memout.bufsize = 64; 7931556Srgrimes mode |= REDIR_BACKQ; 7941556Srgrimes } 7951556Srgrimes redirect(cmd->ncmd.redirect, mode); 7961556Srgrimes savecmdname = commandname; 7971556Srgrimes cmdenviron = varlist.list; 7981556Srgrimes e = -1; 7991556Srgrimes if (setjmp(jmploc.loc)) { 8001556Srgrimes e = exception; 8011556Srgrimes exitstatus = (e == EXINT)? SIGINT+128 : 2; 8021556Srgrimes goto cmddone; 8031556Srgrimes } 8041556Srgrimes savehandler = handler; 8051556Srgrimes handler = &jmploc; 8061556Srgrimes commandname = argv[0]; 8071556Srgrimes argptr = argv + 1; 8081556Srgrimes optptr = NULL; /* initialize nextopt */ 8091556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 8101556Srgrimes flushall(); 8111556Srgrimescmddone: 8121556Srgrimes out1 = &output; 8131556Srgrimes out2 = &errout; 8141556Srgrimes freestdout(); 8151556Srgrimes if (e != EXSHELLPROC) { 8161556Srgrimes commandname = savecmdname; 8171556Srgrimes if (flags & EV_EXIT) { 8181556Srgrimes exitshell(exitstatus); 8191556Srgrimes } 8201556Srgrimes } 8211556Srgrimes handler = savehandler; 8221556Srgrimes if (e != -1) { 8231556Srgrimes if (e != EXERROR || cmdentry.u.index == BLTINCMD 8241556Srgrimes || cmdentry.u.index == DOTCMD 8251556Srgrimes || cmdentry.u.index == EVALCMD 82617987Speter#ifndef NO_HISTORY 8271556Srgrimes || cmdentry.u.index == HISTCMD 82817987Speter#endif 8291556Srgrimes || cmdentry.u.index == EXECCMD) 8301556Srgrimes exraise(e); 8311556Srgrimes FORCEINTON; 8321556Srgrimes } 8331556Srgrimes if (cmdentry.u.index != EXECCMD) 8341556Srgrimes popredir(); 8351556Srgrimes if (flags == EV_BACKCMD) { 8361556Srgrimes backcmd->buf = memout.buf; 8371556Srgrimes backcmd->nleft = memout.nextc - memout.buf; 8381556Srgrimes memout.buf = NULL; 8391556Srgrimes } 8401556Srgrimes } else { 8411556Srgrimes trputs("normal command: "); trargs(argv); 8421556Srgrimes clearredir(); 8431556Srgrimes redirect(cmd->ncmd.redirect, 0); 8441556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8451556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 8461556Srgrimes envp = environment(); 84717987Speter shellexec(argv, envp, pathval(), cmdentry.u.index); 8481556Srgrimes /*NOTREACHED*/ 8491556Srgrimes } 8501556Srgrimes goto out; 8511556Srgrimes 8521556Srgrimesparent: /* parent process gets here (if we forked) */ 8531556Srgrimes if (mode == 0) { /* argument to fork */ 8541556Srgrimes INTOFF; 8551556Srgrimes exitstatus = waitforjob(jp); 8561556Srgrimes INTON; 8571556Srgrimes } else if (mode == 2) { 8581556Srgrimes backcmd->fd = pip[0]; 8591556Srgrimes close(pip[1]); 8601556Srgrimes backcmd->jp = jp; 8611556Srgrimes } 8621556Srgrimes 8631556Srgrimesout: 8641556Srgrimes if (lastarg) 8651556Srgrimes setvar("_", lastarg, 0); 8661556Srgrimes popstackmark(&smark); 8671556Srgrimes} 8681556Srgrimes 8691556Srgrimes 8701556Srgrimes 8711556Srgrimes/* 8721556Srgrimes * Search for a command. This is called before we fork so that the 8731556Srgrimes * location of the command will be available in the parent as well as 8741556Srgrimes * the child. The check for "goodname" is an overly conservative 8751556Srgrimes * check that the name will not be subject to expansion. 8761556Srgrimes */ 8771556Srgrimes 8781556SrgrimesSTATIC void 8791556Srgrimesprehash(n) 8801556Srgrimes union node *n; 88117987Speter{ 8821556Srgrimes struct cmdentry entry; 8831556Srgrimes 88417987Speter if (n->type == NCMD && n->ncmd.args) 88517987Speter if (goodname(n->ncmd.args->narg.text)) 88617987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 88717987Speter pathval()); 8881556Srgrimes} 8891556Srgrimes 8901556Srgrimes 8911556Srgrimes 8921556Srgrimes/* 8931556Srgrimes * Builtin commands. Builtin commands whose functions are closely 8941556Srgrimes * tied to evaluation are implemented here. 8951556Srgrimes */ 8961556Srgrimes 8971556Srgrimes/* 8981556Srgrimes * No command given, or a bltin command with no arguments. Set the 8991556Srgrimes * specified variables. 9001556Srgrimes */ 9011556Srgrimes 90217987Speterint 90317987Speterbltincmd(argc, argv) 90417987Speter int argc; 90517987Speter char **argv; 90617987Speter{ 9071556Srgrimes listsetvar(cmdenviron); 90817987Speter /* 90917987Speter * Preserve exitstatus of a previous possible redirection 91017987Speter * as POSIX mandates 91117987Speter */ 9121556Srgrimes return exitstatus; 9131556Srgrimes} 9141556Srgrimes 9151556Srgrimes 9161556Srgrimes/* 9171556Srgrimes * Handle break and continue commands. Break, continue, and return are 9181556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 9191556Srgrimes * above all check this flag, and if it is set they start skipping 9201556Srgrimes * commands rather than executing them. The variable skipcount is 9211556Srgrimes * the number of loops to break/continue, or the number of function 9221556Srgrimes * levels to return. (The latter is always 1.) It should probably 9231556Srgrimes * be an error to break out of more loops than exist, but it isn't 9241556Srgrimes * in the standard shell so we don't make it one here. 9251556Srgrimes */ 9261556Srgrimes 92717987Speterint 92817987Speterbreakcmd(argc, argv) 92917987Speter int argc; 93017987Speter char **argv; 93117987Speter{ 9321556Srgrimes int n; 9331556Srgrimes 9341556Srgrimes n = 1; 9351556Srgrimes if (argc > 1) 9361556Srgrimes n = number(argv[1]); 9371556Srgrimes if (n > loopnest) 9381556Srgrimes n = loopnest; 9391556Srgrimes if (n > 0) { 9401556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 9411556Srgrimes skipcount = n; 9421556Srgrimes } 9431556Srgrimes return 0; 9441556Srgrimes} 9451556Srgrimes 9461556Srgrimes 9471556Srgrimes/* 9481556Srgrimes * The return command. 9491556Srgrimes */ 9501556Srgrimes 95117987Speterint 95217987Speterreturncmd(argc, argv) 95317987Speter int argc; 95417987Speter char **argv; 95517987Speter{ 9561556Srgrimes int ret; 9571556Srgrimes 9581556Srgrimes ret = exitstatus; 9591556Srgrimes if (argc > 1) 9601556Srgrimes ret = number(argv[1]); 9611556Srgrimes if (funcnest) { 9621556Srgrimes evalskip = SKIPFUNC; 9631556Srgrimes skipcount = 1; 9641556Srgrimes } 9651556Srgrimes return ret; 9661556Srgrimes} 9671556Srgrimes 9681556Srgrimes 96917987Speterint 97017987Speterfalsecmd(argc, argv) 97117987Speter int argc; 97217987Speter char **argv; 97317987Speter{ 97417987Speter return 1; 97517987Speter} 97617987Speter 97717987Speter 97817987Speterint 97917987Spetertruecmd(argc, argv) 98017987Speter int argc; 98117987Speter char **argv; 98217987Speter{ 9831556Srgrimes return 0; 9841556Srgrimes} 9851556Srgrimes 9861556Srgrimes 98717987Speterint 98817987Speterexeccmd(argc, argv) 98917987Speter int argc; 99017987Speter char **argv; 99117987Speter{ 9921556Srgrimes if (argc > 1) { 99317987Speter struct strlist *sp; 99417987Speter 9951556Srgrimes iflag = 0; /* exit on error */ 9961556Srgrimes mflag = 0; 9971556Srgrimes optschanged(); 99817987Speter for (sp = cmdenviron; sp ; sp = sp->next) 99917987Speter setvareq(sp->text, VEXPORT|VSTACK); 10001556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 10011556Srgrimes 10021556Srgrimes } 10031556Srgrimes return 0; 10041556Srgrimes} 1005