eval.c revision 52900
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[] = 4250471Speter "$FreeBSD: head/bin/sh/eval.c 52900 1999-11-05 12:06:30Z cracauer $"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 4517987Speter#include <signal.h> 4617987Speter#include <unistd.h> 4745266Scracauer#include <sys/wait.h> /* For WIFSIGNALED(status) */ 4817987Speter 491556Srgrimes/* 501556Srgrimes * Evaluate a command. 511556Srgrimes */ 521556Srgrimes 531556Srgrimes#include "shell.h" 541556Srgrimes#include "nodes.h" 551556Srgrimes#include "syntax.h" 561556Srgrimes#include "expand.h" 571556Srgrimes#include "parser.h" 581556Srgrimes#include "jobs.h" 591556Srgrimes#include "eval.h" 601556Srgrimes#include "builtins.h" 611556Srgrimes#include "options.h" 621556Srgrimes#include "exec.h" 631556Srgrimes#include "redir.h" 641556Srgrimes#include "input.h" 651556Srgrimes#include "output.h" 661556Srgrimes#include "trap.h" 671556Srgrimes#include "var.h" 681556Srgrimes#include "memalloc.h" 691556Srgrimes#include "error.h" 7017987Speter#include "show.h" 711556Srgrimes#include "mystring.h" 7217987Speter#ifndef NO_HISTORY 731556Srgrimes#include "myhistedit.h" 7417987Speter#endif 751556Srgrimes 761556Srgrimes 771556Srgrimes/* flags in argument to evaltree */ 781556Srgrimes#define EV_EXIT 01 /* exit after evaluating tree */ 791556Srgrimes#define EV_TESTED 02 /* exit status is checked; ignore -e flag */ 801556Srgrimes#define EV_BACKCMD 04 /* command executing within back quotes */ 811556Srgrimes 821556SrgrimesMKINIT int evalskip; /* set if we are skipping commands */ 831556SrgrimesSTATIC int skipcount; /* number of levels to skip */ 841556SrgrimesMKINIT int loopnest; /* current loop nesting level */ 851556Srgrimesint funcnest; /* depth of function calls */ 861556Srgrimes 871556Srgrimes 881556Srgrimeschar *commandname; 891556Srgrimesstruct strlist *cmdenviron; 901556Srgrimesint exitstatus; /* exit status of last command */ 9117987Speterint oexitstatus; /* saved exit status */ 921556Srgrimes 931556Srgrimes 9417987SpeterSTATIC void evalloop __P((union node *)); 9517987SpeterSTATIC void evalfor __P((union node *)); 9617987SpeterSTATIC void evalcase __P((union node *, int)); 9717987SpeterSTATIC void evalsubshell __P((union node *, int)); 9817987SpeterSTATIC void expredir __P((union node *)); 9917987SpeterSTATIC void evalpipe __P((union node *)); 10017987SpeterSTATIC void evalcommand __P((union node *, int, struct backcmd *)); 10117987SpeterSTATIC void prehash __P((union node *)); 1021556Srgrimes 1031556Srgrimes 1041556Srgrimes/* 1051556Srgrimes * Called to reset things after an exception. 1061556Srgrimes */ 1071556Srgrimes 1081556Srgrimes#ifdef mkinit 1091556SrgrimesINCLUDE "eval.h" 1101556Srgrimes 1111556SrgrimesRESET { 1121556Srgrimes evalskip = 0; 1131556Srgrimes loopnest = 0; 1141556Srgrimes funcnest = 0; 1151556Srgrimes} 1161556Srgrimes 1171556SrgrimesSHELLPROC { 1181556Srgrimes exitstatus = 0; 1191556Srgrimes} 1201556Srgrimes#endif 1211556Srgrimes 1221556Srgrimes 1231556Srgrimes 1241556Srgrimes/* 12546684Skris * The eval command. 1261556Srgrimes */ 1271556Srgrimes 12817987Speterint 12920425Ssteveevalcmd(argc, argv) 13017987Speter int argc; 13120425Ssteve char **argv; 1321556Srgrimes{ 1331556Srgrimes char *p; 1341556Srgrimes char *concat; 1351556Srgrimes char **ap; 1361556Srgrimes 1371556Srgrimes if (argc > 1) { 1381556Srgrimes p = argv[1]; 1391556Srgrimes if (argc > 2) { 1401556Srgrimes STARTSTACKSTR(concat); 1411556Srgrimes ap = argv + 2; 1421556Srgrimes for (;;) { 1431556Srgrimes while (*p) 1441556Srgrimes STPUTC(*p++, concat); 1451556Srgrimes if ((p = *ap++) == NULL) 1461556Srgrimes break; 1471556Srgrimes STPUTC(' ', concat); 1481556Srgrimes } 1491556Srgrimes STPUTC('\0', concat); 1501556Srgrimes p = grabstackstr(concat); 1511556Srgrimes } 1521556Srgrimes evalstring(p); 1531556Srgrimes } 1541556Srgrimes return exitstatus; 1551556Srgrimes} 1561556Srgrimes 1571556Srgrimes 1581556Srgrimes/* 1591556Srgrimes * Execute a command or commands contained in a string. 1601556Srgrimes */ 1611556Srgrimes 1621556Srgrimesvoid 1631556Srgrimesevalstring(s) 1641556Srgrimes char *s; 1651556Srgrimes { 1661556Srgrimes union node *n; 1671556Srgrimes struct stackmark smark; 1681556Srgrimes 1691556Srgrimes setstackmark(&smark); 1701556Srgrimes setinputstring(s, 1); 1711556Srgrimes while ((n = parsecmd(0)) != NEOF) { 1721556Srgrimes evaltree(n, 0); 1731556Srgrimes popstackmark(&smark); 1741556Srgrimes } 1751556Srgrimes popfile(); 1761556Srgrimes popstackmark(&smark); 1771556Srgrimes} 1781556Srgrimes 1791556Srgrimes 1801556Srgrimes 1811556Srgrimes/* 1821556Srgrimes * Evaluate a parse tree. The value is left in the global variable 1831556Srgrimes * exitstatus. 1841556Srgrimes */ 1851556Srgrimes 1861556Srgrimesvoid 1871556Srgrimesevaltree(n, flags) 1881556Srgrimes union node *n; 18917987Speter int flags; 19017987Speter{ 1911556Srgrimes if (n == NULL) { 1921556Srgrimes TRACE(("evaltree(NULL) called\n")); 1931556Srgrimes exitstatus = 0; 1941556Srgrimes goto out; 1951556Srgrimes } 19617987Speter#ifndef NO_HISTORY 1971556Srgrimes displayhist = 1; /* show history substitutions done with fc */ 19817987Speter#endif 19917987Speter TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type)); 2001556Srgrimes switch (n->type) { 2011556Srgrimes case NSEMI: 2021556Srgrimes evaltree(n->nbinary.ch1, 0); 2031556Srgrimes if (evalskip) 2041556Srgrimes goto out; 2051556Srgrimes evaltree(n->nbinary.ch2, flags); 2061556Srgrimes break; 2071556Srgrimes case NAND: 2081556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 20918754Ssteve if (evalskip || exitstatus != 0) { 21018754Ssteve flags |= EV_TESTED; 2111556Srgrimes goto out; 21218754Ssteve } 2131556Srgrimes evaltree(n->nbinary.ch2, flags); 2141556Srgrimes break; 2151556Srgrimes case NOR: 2161556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2171556Srgrimes if (evalskip || exitstatus == 0) 2181556Srgrimes goto out; 2191556Srgrimes evaltree(n->nbinary.ch2, flags); 2201556Srgrimes break; 2211556Srgrimes case NREDIR: 2221556Srgrimes expredir(n->nredir.redirect); 2231556Srgrimes redirect(n->nredir.redirect, REDIR_PUSH); 2241556Srgrimes evaltree(n->nredir.n, flags); 2251556Srgrimes popredir(); 2261556Srgrimes break; 2271556Srgrimes case NSUBSHELL: 2281556Srgrimes evalsubshell(n, flags); 2291556Srgrimes break; 2301556Srgrimes case NBACKGND: 2311556Srgrimes evalsubshell(n, flags); 2321556Srgrimes break; 2331556Srgrimes case NIF: { 2341556Srgrimes evaltree(n->nif.test, EV_TESTED); 2351556Srgrimes if (evalskip) 2361556Srgrimes goto out; 23720425Ssteve if (exitstatus == 0) 2381556Srgrimes evaltree(n->nif.ifpart, flags); 23917987Speter else if (n->nif.elsepart) 2401556Srgrimes evaltree(n->nif.elsepart, flags); 24120425Ssteve else 24220425Ssteve exitstatus = 0; 2431556Srgrimes break; 2441556Srgrimes } 2451556Srgrimes case NWHILE: 2461556Srgrimes case NUNTIL: 2471556Srgrimes evalloop(n); 2481556Srgrimes break; 2491556Srgrimes case NFOR: 2501556Srgrimes evalfor(n); 2511556Srgrimes break; 2521556Srgrimes case NCASE: 2531556Srgrimes evalcase(n, flags); 2541556Srgrimes break; 2551556Srgrimes case NDEFUN: 2561556Srgrimes defun(n->narg.text, n->narg.next); 2571556Srgrimes exitstatus = 0; 2581556Srgrimes break; 2591556Srgrimes case NNOT: 2601556Srgrimes evaltree(n->nnot.com, EV_TESTED); 2611556Srgrimes exitstatus = !exitstatus; 2621556Srgrimes break; 2631556Srgrimes 2641556Srgrimes case NPIPE: 2651556Srgrimes evalpipe(n); 2661556Srgrimes break; 2671556Srgrimes case NCMD: 2681556Srgrimes evalcommand(n, flags, (struct backcmd *)NULL); 2691556Srgrimes break; 2701556Srgrimes default: 2711556Srgrimes out1fmt("Node type = %d\n", n->type); 2721556Srgrimes flushout(&output); 2731556Srgrimes break; 2741556Srgrimes } 2751556Srgrimesout: 2761556Srgrimes if (pendingsigs) 2771556Srgrimes dotrap(); 27852526Scracauer /* 27952526Scracauer * XXX - Like "!(n->type == NSEMI)", more types will probably 28052526Scracauer * need to be excluded from this test. It's probably better 28152526Scracauer * to set or unset EV_TESTED in the loop above than to bloat 28252526Scracauer * the conditional here. 28352526Scracauer */ 28452526Scracauer if ((flags & EV_EXIT) || (eflag && exitstatus 28552526Scracauer && !(flags & EV_TESTED) && !(n->type == NSEMI))) 2861556Srgrimes exitshell(exitstatus); 2871556Srgrimes} 2881556Srgrimes 2891556Srgrimes 2901556SrgrimesSTATIC void 2911556Srgrimesevalloop(n) 2921556Srgrimes union node *n; 29317987Speter{ 2941556Srgrimes int status; 2951556Srgrimes 2961556Srgrimes loopnest++; 2971556Srgrimes status = 0; 2981556Srgrimes for (;;) { 2991556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 3001556Srgrimes if (evalskip) { 3011556Srgrimesskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 3021556Srgrimes evalskip = 0; 3031556Srgrimes continue; 3041556Srgrimes } 3051556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3061556Srgrimes evalskip = 0; 3071556Srgrimes break; 3081556Srgrimes } 3091556Srgrimes if (n->type == NWHILE) { 3101556Srgrimes if (exitstatus != 0) 3111556Srgrimes break; 3121556Srgrimes } else { 3131556Srgrimes if (exitstatus == 0) 3141556Srgrimes break; 3151556Srgrimes } 3161556Srgrimes evaltree(n->nbinary.ch2, 0); 3171556Srgrimes status = exitstatus; 3181556Srgrimes if (evalskip) 3191556Srgrimes goto skipping; 3201556Srgrimes } 3211556Srgrimes loopnest--; 3221556Srgrimes exitstatus = status; 3231556Srgrimes} 3241556Srgrimes 3251556Srgrimes 3261556Srgrimes 3271556SrgrimesSTATIC void 3281556Srgrimesevalfor(n) 32917987Speter union node *n; 33017987Speter{ 3311556Srgrimes struct arglist arglist; 3321556Srgrimes union node *argp; 3331556Srgrimes struct strlist *sp; 3341556Srgrimes struct stackmark smark; 3351556Srgrimes 3361556Srgrimes setstackmark(&smark); 3371556Srgrimes arglist.lastp = &arglist.list; 3381556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 33917987Speter oexitstatus = exitstatus; 3401556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3411556Srgrimes if (evalskip) 3421556Srgrimes goto out; 3431556Srgrimes } 3441556Srgrimes *arglist.lastp = NULL; 3451556Srgrimes 3461556Srgrimes exitstatus = 0; 3471556Srgrimes loopnest++; 3481556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3491556Srgrimes setvar(n->nfor.var, sp->text, 0); 3501556Srgrimes evaltree(n->nfor.body, 0); 3511556Srgrimes if (evalskip) { 3521556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3531556Srgrimes evalskip = 0; 3541556Srgrimes continue; 3551556Srgrimes } 3561556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3571556Srgrimes evalskip = 0; 3581556Srgrimes break; 3591556Srgrimes } 3601556Srgrimes } 3611556Srgrimes loopnest--; 3621556Srgrimesout: 3631556Srgrimes popstackmark(&smark); 3641556Srgrimes} 3651556Srgrimes 3661556Srgrimes 3671556Srgrimes 3681556SrgrimesSTATIC void 3691556Srgrimesevalcase(n, flags) 3701556Srgrimes union node *n; 37117987Speter int flags; 37217987Speter{ 3731556Srgrimes union node *cp; 3741556Srgrimes union node *patp; 3751556Srgrimes struct arglist arglist; 3761556Srgrimes struct stackmark smark; 3771556Srgrimes 3781556Srgrimes setstackmark(&smark); 3791556Srgrimes arglist.lastp = &arglist.list; 38017987Speter oexitstatus = exitstatus; 3811556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3821556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3831556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3841556Srgrimes if (casematch(patp, arglist.list->text)) { 3851556Srgrimes if (evalskip == 0) { 3861556Srgrimes evaltree(cp->nclist.body, flags); 3871556Srgrimes } 3881556Srgrimes goto out; 3891556Srgrimes } 3901556Srgrimes } 3911556Srgrimes } 3921556Srgrimesout: 3931556Srgrimes popstackmark(&smark); 3941556Srgrimes} 3951556Srgrimes 3961556Srgrimes 3971556Srgrimes 3981556Srgrimes/* 3991556Srgrimes * Kick off a subshell to evaluate a tree. 4001556Srgrimes */ 4011556Srgrimes 4021556SrgrimesSTATIC void 4031556Srgrimesevalsubshell(n, flags) 4041556Srgrimes union node *n; 40517987Speter int flags; 40617987Speter{ 4071556Srgrimes struct job *jp; 4081556Srgrimes int backgnd = (n->type == NBACKGND); 4091556Srgrimes 4101556Srgrimes expredir(n->nredir.redirect); 4111556Srgrimes jp = makejob(n, 1); 4121556Srgrimes if (forkshell(jp, n, backgnd) == 0) { 4131556Srgrimes if (backgnd) 4141556Srgrimes flags &=~ EV_TESTED; 4151556Srgrimes redirect(n->nredir.redirect, 0); 4161556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 4171556Srgrimes } 4181556Srgrimes if (! backgnd) { 4191556Srgrimes INTOFF; 42045916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4211556Srgrimes INTON; 4221556Srgrimes } 4231556Srgrimes} 4241556Srgrimes 4251556Srgrimes 4261556Srgrimes 4271556Srgrimes/* 4281556Srgrimes * Compute the names of the files in a redirection list. 4291556Srgrimes */ 4301556Srgrimes 4311556SrgrimesSTATIC void 4321556Srgrimesexpredir(n) 4331556Srgrimes union node *n; 43417987Speter{ 43525222Ssteve union node *redir; 4361556Srgrimes 4371556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 43817987Speter struct arglist fn; 43917987Speter fn.lastp = &fn.list; 44017987Speter oexitstatus = exitstatus; 44117987Speter switch (redir->type) { 44217987Speter case NFROM: 44317987Speter case NTO: 44417987Speter case NAPPEND: 4451556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4461556Srgrimes redir->nfile.expfname = fn.list->text; 44717987Speter break; 44817987Speter case NFROMFD: 44917987Speter case NTOFD: 45017987Speter if (redir->ndup.vname) { 45117987Speter expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); 45217987Speter fixredir(redir, fn.list->text, 1); 45317987Speter } 45417987Speter break; 4551556Srgrimes } 4561556Srgrimes } 4571556Srgrimes} 4581556Srgrimes 4591556Srgrimes 4601556Srgrimes 4611556Srgrimes/* 4621556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 4631556Srgrimes * of the process creating the pipeline. (This differs from some versions 4641556Srgrimes * of the shell, which make the last process in a pipeline the parent 4651556Srgrimes * of all the rest.) 4661556Srgrimes */ 4671556Srgrimes 4681556SrgrimesSTATIC void 4691556Srgrimesevalpipe(n) 4701556Srgrimes union node *n; 47117987Speter{ 4721556Srgrimes struct job *jp; 4731556Srgrimes struct nodelist *lp; 4741556Srgrimes int pipelen; 4751556Srgrimes int prevfd; 4761556Srgrimes int pip[2]; 4771556Srgrimes 47817987Speter TRACE(("evalpipe(0x%lx) called\n", (long)n)); 4791556Srgrimes pipelen = 0; 4801556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 4811556Srgrimes pipelen++; 4821556Srgrimes INTOFF; 4831556Srgrimes jp = makejob(n, pipelen); 4841556Srgrimes prevfd = -1; 4851556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 4861556Srgrimes prehash(lp->n); 4871556Srgrimes pip[1] = -1; 4881556Srgrimes if (lp->next) { 4891556Srgrimes if (pipe(pip) < 0) { 4901556Srgrimes close(prevfd); 4911556Srgrimes error("Pipe call failed"); 4921556Srgrimes } 4931556Srgrimes } 4941556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 4951556Srgrimes INTON; 4961556Srgrimes if (prevfd > 0) { 4971556Srgrimes close(0); 4981556Srgrimes copyfd(prevfd, 0); 4991556Srgrimes close(prevfd); 5001556Srgrimes } 5011556Srgrimes if (pip[1] >= 0) { 50252900Scracauer if (!prevfd > 0) 50352900Scracauer close(pip[0]); 5041556Srgrimes if (pip[1] != 1) { 5051556Srgrimes close(1); 5061556Srgrimes copyfd(pip[1], 1); 5071556Srgrimes close(pip[1]); 5081556Srgrimes } 5091556Srgrimes } 5101556Srgrimes evaltree(lp->n, EV_EXIT); 5111556Srgrimes } 5121556Srgrimes if (prevfd >= 0) 5131556Srgrimes close(prevfd); 5141556Srgrimes prevfd = pip[0]; 5151556Srgrimes close(pip[1]); 5161556Srgrimes } 5171556Srgrimes INTON; 5181556Srgrimes if (n->npipe.backgnd == 0) { 5191556Srgrimes INTOFF; 52045916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 5211556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5221556Srgrimes INTON; 5231556Srgrimes } 5241556Srgrimes} 5251556Srgrimes 5261556Srgrimes 5271556Srgrimes 5281556Srgrimes/* 5291556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5301556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5311556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5321556Srgrimes * Should be called with interrupts off. 5331556Srgrimes */ 5341556Srgrimes 5351556Srgrimesvoid 5361556Srgrimesevalbackcmd(n, result) 5371556Srgrimes union node *n; 5381556Srgrimes struct backcmd *result; 53917987Speter{ 5401556Srgrimes int pip[2]; 5411556Srgrimes struct job *jp; 5421556Srgrimes struct stackmark smark; /* unnecessary */ 5431556Srgrimes 5441556Srgrimes setstackmark(&smark); 5451556Srgrimes result->fd = -1; 5461556Srgrimes result->buf = NULL; 5471556Srgrimes result->nleft = 0; 5481556Srgrimes result->jp = NULL; 54917987Speter if (n == NULL) { 55017987Speter exitstatus = 0; 5511556Srgrimes goto out; 55217987Speter } 5531556Srgrimes if (n->type == NCMD) { 55417987Speter exitstatus = oexitstatus; 5551556Srgrimes evalcommand(n, EV_BACKCMD, result); 5561556Srgrimes } else { 55717987Speter exitstatus = 0; 5581556Srgrimes if (pipe(pip) < 0) 5591556Srgrimes error("Pipe call failed"); 5601556Srgrimes jp = makejob(n, 1); 5611556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 5621556Srgrimes FORCEINTON; 5631556Srgrimes close(pip[0]); 5641556Srgrimes if (pip[1] != 1) { 5651556Srgrimes close(1); 5661556Srgrimes copyfd(pip[1], 1); 5671556Srgrimes close(pip[1]); 5681556Srgrimes } 5691556Srgrimes evaltree(n, EV_EXIT); 5701556Srgrimes } 5711556Srgrimes close(pip[1]); 5721556Srgrimes result->fd = pip[0]; 5731556Srgrimes result->jp = jp; 5741556Srgrimes } 5751556Srgrimesout: 5761556Srgrimes popstackmark(&smark); 5771556Srgrimes TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", 5781556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 5791556Srgrimes} 5801556Srgrimes 5811556Srgrimes 5821556Srgrimes 5831556Srgrimes/* 5841556Srgrimes * Execute a simple command. 5851556Srgrimes */ 5861556Srgrimes 5871556SrgrimesSTATIC void 5881556Srgrimesevalcommand(cmd, flags, backcmd) 5891556Srgrimes union node *cmd; 59017987Speter int flags; 5911556Srgrimes struct backcmd *backcmd; 59217987Speter{ 5931556Srgrimes struct stackmark smark; 5941556Srgrimes union node *argp; 5951556Srgrimes struct arglist arglist; 5961556Srgrimes struct arglist varlist; 5971556Srgrimes char **argv; 5981556Srgrimes int argc; 5991556Srgrimes char **envp; 6001556Srgrimes int varflag; 6011556Srgrimes struct strlist *sp; 6021556Srgrimes int mode; 6031556Srgrimes int pip[2]; 6041556Srgrimes struct cmdentry cmdentry; 6051556Srgrimes struct job *jp; 6061556Srgrimes struct jmploc jmploc; 6071556Srgrimes struct jmploc *volatile savehandler; 6081556Srgrimes char *volatile savecmdname; 6091556Srgrimes volatile struct shparam saveparam; 6101556Srgrimes struct localvar *volatile savelocalvars; 6111556Srgrimes volatile int e; 6121556Srgrimes char *lastarg; 61345916Scracauer int realstatus; 61417987Speter#if __GNUC__ 61517987Speter /* Avoid longjmp clobbering */ 61617987Speter (void) &argv; 61717987Speter (void) &argc; 61817987Speter (void) &lastarg; 61917987Speter (void) &flags; 62017987Speter#endif 6211556Srgrimes 6221556Srgrimes /* First expand the arguments. */ 62317987Speter TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); 6241556Srgrimes setstackmark(&smark); 6251556Srgrimes arglist.lastp = &arglist.list; 6261556Srgrimes varlist.lastp = &varlist.list; 6271556Srgrimes varflag = 1; 62817987Speter oexitstatus = exitstatus; 62917987Speter exitstatus = 0; 6301556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 63117987Speter char *p = argp->narg.text; 6321556Srgrimes if (varflag && is_name(*p)) { 6331556Srgrimes do { 6341556Srgrimes p++; 6351556Srgrimes } while (is_in_name(*p)); 6361556Srgrimes if (*p == '=') { 6371556Srgrimes expandarg(argp, &varlist, EXP_VARTILDE); 6381556Srgrimes continue; 6391556Srgrimes } 6401556Srgrimes } 6411556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 6421556Srgrimes varflag = 0; 6431556Srgrimes } 6441556Srgrimes *arglist.lastp = NULL; 6451556Srgrimes *varlist.lastp = NULL; 6461556Srgrimes expredir(cmd->ncmd.redirect); 6471556Srgrimes argc = 0; 6481556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 6491556Srgrimes argc++; 6501556Srgrimes argv = stalloc(sizeof (char *) * (argc + 1)); 6511556Srgrimes 6521556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6531556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 6541556Srgrimes *argv++ = sp->text; 6551556Srgrimes } 6561556Srgrimes *argv = NULL; 6571556Srgrimes lastarg = NULL; 6581556Srgrimes if (iflag && funcnest == 0 && argc > 0) 6591556Srgrimes lastarg = argv[-1]; 6601556Srgrimes argv -= argc; 6611556Srgrimes 6621556Srgrimes /* Print the command if xflag is set. */ 6631556Srgrimes if (xflag) { 6641556Srgrimes outc('+', &errout); 6651556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 6661556Srgrimes outc(' ', &errout); 6671556Srgrimes out2str(sp->text); 6681556Srgrimes } 6691556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6701556Srgrimes outc(' ', &errout); 6711556Srgrimes out2str(sp->text); 6721556Srgrimes } 6731556Srgrimes outc('\n', &errout); 6741556Srgrimes flushout(&errout); 6751556Srgrimes } 6761556Srgrimes 6771556Srgrimes /* Now locate the command. */ 6781556Srgrimes if (argc == 0) { 6791556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 6801556Srgrimes cmdentry.u.index = BLTINCMD; 6811556Srgrimes } else { 68217987Speter static const char PATH[] = "PATH="; 68317987Speter char *path = pathval(); 68417987Speter 68517987Speter /* 68617987Speter * Modify the command lookup path, if a PATH= assignment 68717987Speter * is present 68817987Speter */ 68917987Speter for (sp = varlist.list ; sp ; sp = sp->next) 69017987Speter if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) 69117987Speter path = sp->text + sizeof(PATH) - 1; 69217987Speter 69317987Speter find_command(argv[0], &cmdentry, 1, path); 6941556Srgrimes if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */ 69520425Ssteve exitstatus = 127; 6961556Srgrimes flushout(&errout); 6971556Srgrimes return; 6981556Srgrimes } 6991556Srgrimes /* implement the bltin builtin here */ 7001556Srgrimes if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) { 7011556Srgrimes for (;;) { 7021556Srgrimes argv++; 7031556Srgrimes if (--argc == 0) 7041556Srgrimes break; 7051556Srgrimes if ((cmdentry.u.index = find_builtin(*argv)) < 0) { 7061556Srgrimes outfmt(&errout, "%s: not found\n", *argv); 70720425Ssteve exitstatus = 127; 7081556Srgrimes flushout(&errout); 7091556Srgrimes return; 7101556Srgrimes } 7111556Srgrimes if (cmdentry.u.index != BLTINCMD) 7121556Srgrimes break; 7131556Srgrimes } 7141556Srgrimes } 7151556Srgrimes } 7161556Srgrimes 7171556Srgrimes /* Fork off a child process if necessary. */ 7181556Srgrimes if (cmd->ncmd.backgnd 71945221Scracauer || (cmdentry.cmdtype == CMDNORMAL 72045221Scracauer && ((flags & EV_EXIT) == 0 || Tflag)) 72117987Speter || ((flags & EV_BACKCMD) != 0 7221556Srgrimes && (cmdentry.cmdtype != CMDBUILTIN 72348896Ssheldonh || cmdentry.u.index == CDCMD 7241556Srgrimes || cmdentry.u.index == DOTCMD 72517987Speter || cmdentry.u.index == EVALCMD))) { 7261556Srgrimes jp = makejob(cmd, 1); 7271556Srgrimes mode = cmd->ncmd.backgnd; 7281556Srgrimes if (flags & EV_BACKCMD) { 7291556Srgrimes mode = FORK_NOJOB; 7301556Srgrimes if (pipe(pip) < 0) 7311556Srgrimes error("Pipe call failed"); 7321556Srgrimes } 7331556Srgrimes if (forkshell(jp, cmd, mode) != 0) 7341556Srgrimes goto parent; /* at end of routine */ 7351556Srgrimes if (flags & EV_BACKCMD) { 7361556Srgrimes FORCEINTON; 7371556Srgrimes close(pip[0]); 7381556Srgrimes if (pip[1] != 1) { 7391556Srgrimes close(1); 7401556Srgrimes copyfd(pip[1], 1); 7411556Srgrimes close(pip[1]); 7421556Srgrimes } 7431556Srgrimes } 7441556Srgrimes flags |= EV_EXIT; 7451556Srgrimes } 7461556Srgrimes 7471556Srgrimes /* This is the child process if a fork occurred. */ 7481556Srgrimes /* Execute the command. */ 7491556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 75020425Ssteve#ifdef DEBUG 7511556Srgrimes trputs("Shell function: "); trargs(argv); 75220425Ssteve#endif 7531556Srgrimes redirect(cmd->ncmd.redirect, REDIR_PUSH); 7541556Srgrimes saveparam = shellparam; 7551556Srgrimes shellparam.malloc = 0; 75620425Ssteve shellparam.reset = 1; 7571556Srgrimes shellparam.nparam = argc - 1; 7581556Srgrimes shellparam.p = argv + 1; 7591556Srgrimes shellparam.optnext = NULL; 7601556Srgrimes INTOFF; 7611556Srgrimes savelocalvars = localvars; 7621556Srgrimes localvars = NULL; 7631556Srgrimes INTON; 7641556Srgrimes if (setjmp(jmploc.loc)) { 7651556Srgrimes if (exception == EXSHELLPROC) 7661556Srgrimes freeparam((struct shparam *)&saveparam); 7671556Srgrimes else { 7681556Srgrimes freeparam(&shellparam); 7691556Srgrimes shellparam = saveparam; 7701556Srgrimes } 7711556Srgrimes poplocalvars(); 7721556Srgrimes localvars = savelocalvars; 7731556Srgrimes handler = savehandler; 7741556Srgrimes longjmp(handler->loc, 1); 7751556Srgrimes } 7761556Srgrimes savehandler = handler; 7771556Srgrimes handler = &jmploc; 7781556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 7791556Srgrimes mklocal(sp->text); 7801556Srgrimes funcnest++; 78135675Scracauer if (flags & EV_TESTED) 78235675Scracauer evaltree(cmdentry.u.func, EV_TESTED); 78335675Scracauer else 78435675Scracauer evaltree(cmdentry.u.func, 0); 7851556Srgrimes funcnest--; 7861556Srgrimes INTOFF; 7871556Srgrimes poplocalvars(); 7881556Srgrimes localvars = savelocalvars; 7891556Srgrimes freeparam(&shellparam); 7901556Srgrimes shellparam = saveparam; 7911556Srgrimes handler = savehandler; 7921556Srgrimes popredir(); 7931556Srgrimes INTON; 7941556Srgrimes if (evalskip == SKIPFUNC) { 7951556Srgrimes evalskip = 0; 7961556Srgrimes skipcount = 0; 7971556Srgrimes } 7981556Srgrimes if (flags & EV_EXIT) 7991556Srgrimes exitshell(exitstatus); 8001556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 80120425Ssteve#ifdef DEBUG 8021556Srgrimes trputs("builtin command: "); trargs(argv); 80320425Ssteve#endif 8041556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 8051556Srgrimes if (flags == EV_BACKCMD) { 8061556Srgrimes memout.nleft = 0; 8071556Srgrimes memout.nextc = memout.buf; 8081556Srgrimes memout.bufsize = 64; 8091556Srgrimes mode |= REDIR_BACKQ; 8101556Srgrimes } 8111556Srgrimes redirect(cmd->ncmd.redirect, mode); 8121556Srgrimes savecmdname = commandname; 8131556Srgrimes cmdenviron = varlist.list; 8141556Srgrimes e = -1; 8151556Srgrimes if (setjmp(jmploc.loc)) { 8161556Srgrimes e = exception; 8171556Srgrimes exitstatus = (e == EXINT)? SIGINT+128 : 2; 8181556Srgrimes goto cmddone; 8191556Srgrimes } 8201556Srgrimes savehandler = handler; 8211556Srgrimes handler = &jmploc; 8221556Srgrimes commandname = argv[0]; 8231556Srgrimes argptr = argv + 1; 8241556Srgrimes optptr = NULL; /* initialize nextopt */ 8251556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 8261556Srgrimes flushall(); 8271556Srgrimescmddone: 8281556Srgrimes out1 = &output; 8291556Srgrimes out2 = &errout; 8301556Srgrimes freestdout(); 8311556Srgrimes if (e != EXSHELLPROC) { 8321556Srgrimes commandname = savecmdname; 8331556Srgrimes if (flags & EV_EXIT) { 8341556Srgrimes exitshell(exitstatus); 8351556Srgrimes } 8361556Srgrimes } 8371556Srgrimes handler = savehandler; 8381556Srgrimes if (e != -1) { 83920425Ssteve if ((e != EXERROR && e != EXEXEC) 84020425Ssteve || cmdentry.u.index == BLTINCMD 84120425Ssteve || cmdentry.u.index == DOTCMD 84220425Ssteve || cmdentry.u.index == EVALCMD 84317987Speter#ifndef NO_HISTORY 84420425Ssteve || cmdentry.u.index == HISTCMD 84517987Speter#endif 84620425Ssteve || cmdentry.u.index == EXECCMD) 8471556Srgrimes exraise(e); 8481556Srgrimes FORCEINTON; 8491556Srgrimes } 8501556Srgrimes if (cmdentry.u.index != EXECCMD) 8511556Srgrimes popredir(); 8521556Srgrimes if (flags == EV_BACKCMD) { 8531556Srgrimes backcmd->buf = memout.buf; 8541556Srgrimes backcmd->nleft = memout.nextc - memout.buf; 8551556Srgrimes memout.buf = NULL; 8561556Srgrimes } 8571556Srgrimes } else { 85820425Ssteve#ifdef DEBUG 8591556Srgrimes trputs("normal command: "); trargs(argv); 86020425Ssteve#endif 8611556Srgrimes clearredir(); 8621556Srgrimes redirect(cmd->ncmd.redirect, 0); 8631556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8641556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 8651556Srgrimes envp = environment(); 86617987Speter shellexec(argv, envp, pathval(), cmdentry.u.index); 8671556Srgrimes /*NOTREACHED*/ 8681556Srgrimes } 8691556Srgrimes goto out; 8701556Srgrimes 8711556Srgrimesparent: /* parent process gets here (if we forked) */ 8721556Srgrimes if (mode == 0) { /* argument to fork */ 8731556Srgrimes INTOFF; 87445916Scracauer exitstatus = waitforjob(jp, &realstatus); 8751556Srgrimes INTON; 87645916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 87745266Scracauer evalskip = SKIPBREAK; 87845266Scracauer skipcount = loopnest; 87945266Scracauer } 8801556Srgrimes } else if (mode == 2) { 8811556Srgrimes backcmd->fd = pip[0]; 8821556Srgrimes close(pip[1]); 8831556Srgrimes backcmd->jp = jp; 8841556Srgrimes } 8851556Srgrimes 8861556Srgrimesout: 8871556Srgrimes if (lastarg) 8881556Srgrimes setvar("_", lastarg, 0); 8891556Srgrimes popstackmark(&smark); 8901556Srgrimes} 8911556Srgrimes 8921556Srgrimes 8931556Srgrimes 8941556Srgrimes/* 8951556Srgrimes * Search for a command. This is called before we fork so that the 8961556Srgrimes * location of the command will be available in the parent as well as 8971556Srgrimes * the child. The check for "goodname" is an overly conservative 8981556Srgrimes * check that the name will not be subject to expansion. 8991556Srgrimes */ 9001556Srgrimes 9011556SrgrimesSTATIC void 9021556Srgrimesprehash(n) 9031556Srgrimes union node *n; 90417987Speter{ 9051556Srgrimes struct cmdentry entry; 9061556Srgrimes 90717987Speter if (n->type == NCMD && n->ncmd.args) 90817987Speter if (goodname(n->ncmd.args->narg.text)) 90917987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 91017987Speter pathval()); 9111556Srgrimes} 9121556Srgrimes 9131556Srgrimes 9141556Srgrimes 9151556Srgrimes/* 9161556Srgrimes * Builtin commands. Builtin commands whose functions are closely 9171556Srgrimes * tied to evaluation are implemented here. 9181556Srgrimes */ 9191556Srgrimes 9201556Srgrimes/* 9211556Srgrimes * No command given, or a bltin command with no arguments. Set the 9221556Srgrimes * specified variables. 9231556Srgrimes */ 9241556Srgrimes 92517987Speterint 92617987Speterbltincmd(argc, argv) 92725905Ssteve int argc __unused; 92825905Ssteve char **argv __unused; 92917987Speter{ 9301556Srgrimes listsetvar(cmdenviron); 93120425Ssteve /* 93217987Speter * Preserve exitstatus of a previous possible redirection 93320425Ssteve * as POSIX mandates 93417987Speter */ 9351556Srgrimes return exitstatus; 9361556Srgrimes} 9371556Srgrimes 9381556Srgrimes 9391556Srgrimes/* 9401556Srgrimes * Handle break and continue commands. Break, continue, and return are 9411556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 9421556Srgrimes * above all check this flag, and if it is set they start skipping 9431556Srgrimes * commands rather than executing them. The variable skipcount is 9441556Srgrimes * the number of loops to break/continue, or the number of function 9451556Srgrimes * levels to return. (The latter is always 1.) It should probably 9461556Srgrimes * be an error to break out of more loops than exist, but it isn't 9471556Srgrimes * in the standard shell so we don't make it one here. 9481556Srgrimes */ 9491556Srgrimes 95017987Speterint 95117987Speterbreakcmd(argc, argv) 95217987Speter int argc; 95320425Ssteve char **argv; 95417987Speter{ 95520425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 9561556Srgrimes 9571556Srgrimes if (n > loopnest) 9581556Srgrimes n = loopnest; 9591556Srgrimes if (n > 0) { 9601556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 9611556Srgrimes skipcount = n; 9621556Srgrimes } 9631556Srgrimes return 0; 9641556Srgrimes} 9651556Srgrimes 9661556Srgrimes 9671556Srgrimes/* 9681556Srgrimes * The return command. 9691556Srgrimes */ 9701556Srgrimes 97117987Speterint 97220425Sstevereturncmd(argc, argv) 97317987Speter int argc; 97420425Ssteve char **argv; 97517987Speter{ 97620425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 9771556Srgrimes 9781556Srgrimes if (funcnest) { 9791556Srgrimes evalskip = SKIPFUNC; 9801556Srgrimes skipcount = 1; 98120425Ssteve } else { 98220425Ssteve /* skip the rest of the file */ 98320425Ssteve evalskip = SKIPFILE; 98420425Ssteve skipcount = 1; 9851556Srgrimes } 9861556Srgrimes return ret; 9871556Srgrimes} 9881556Srgrimes 9891556Srgrimes 99017987Speterint 99120425Sstevefalsecmd(argc, argv) 99225905Ssteve int argc __unused; 99325905Ssteve char **argv __unused; 99417987Speter{ 99517987Speter return 1; 99617987Speter} 99717987Speter 99817987Speter 99917987Speterint 100020425Sstevetruecmd(argc, argv) 100125905Ssteve int argc __unused; 100225905Ssteve char **argv __unused; 100317987Speter{ 10041556Srgrimes return 0; 10051556Srgrimes} 10061556Srgrimes 10071556Srgrimes 100817987Speterint 100920425Ssteveexeccmd(argc, argv) 101017987Speter int argc; 101120425Ssteve char **argv; 101217987Speter{ 10131556Srgrimes if (argc > 1) { 101417987Speter struct strlist *sp; 101517987Speter 10161556Srgrimes iflag = 0; /* exit on error */ 10171556Srgrimes mflag = 0; 10181556Srgrimes optschanged(); 101917987Speter for (sp = cmdenviron; sp ; sp = sp->next) 102017987Speter setvareq(sp->text, VEXPORT|VSTACK); 10211556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 10221556Srgrimes 10231556Srgrimes } 10241556Srgrimes return 0; 10251556Srgrimes} 1026