eval.c revision 45916
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[] = 4245916Scracauer "$Id: eval.c,v 1.17 1999/04/03 12:55:51 cracauer Exp $"; 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/* 1251556Srgrimes * The eval commmand. 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(); 2781556Srgrimes if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED))) 2791556Srgrimes exitshell(exitstatus); 2801556Srgrimes} 2811556Srgrimes 2821556Srgrimes 2831556SrgrimesSTATIC void 2841556Srgrimesevalloop(n) 2851556Srgrimes union node *n; 28617987Speter{ 2871556Srgrimes int status; 2881556Srgrimes 2891556Srgrimes loopnest++; 2901556Srgrimes status = 0; 2911556Srgrimes for (;;) { 2921556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2931556Srgrimes if (evalskip) { 2941556Srgrimesskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 2951556Srgrimes evalskip = 0; 2961556Srgrimes continue; 2971556Srgrimes } 2981556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 2991556Srgrimes evalskip = 0; 3001556Srgrimes break; 3011556Srgrimes } 3021556Srgrimes if (n->type == NWHILE) { 3031556Srgrimes if (exitstatus != 0) 3041556Srgrimes break; 3051556Srgrimes } else { 3061556Srgrimes if (exitstatus == 0) 3071556Srgrimes break; 3081556Srgrimes } 3091556Srgrimes evaltree(n->nbinary.ch2, 0); 3101556Srgrimes status = exitstatus; 3111556Srgrimes if (evalskip) 3121556Srgrimes goto skipping; 3131556Srgrimes } 3141556Srgrimes loopnest--; 3151556Srgrimes exitstatus = status; 3161556Srgrimes} 3171556Srgrimes 3181556Srgrimes 3191556Srgrimes 3201556SrgrimesSTATIC void 3211556Srgrimesevalfor(n) 32217987Speter union node *n; 32317987Speter{ 3241556Srgrimes struct arglist arglist; 3251556Srgrimes union node *argp; 3261556Srgrimes struct strlist *sp; 3271556Srgrimes struct stackmark smark; 3281556Srgrimes 3291556Srgrimes setstackmark(&smark); 3301556Srgrimes arglist.lastp = &arglist.list; 3311556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 33217987Speter oexitstatus = exitstatus; 3331556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3341556Srgrimes if (evalskip) 3351556Srgrimes goto out; 3361556Srgrimes } 3371556Srgrimes *arglist.lastp = NULL; 3381556Srgrimes 3391556Srgrimes exitstatus = 0; 3401556Srgrimes loopnest++; 3411556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3421556Srgrimes setvar(n->nfor.var, sp->text, 0); 3431556Srgrimes evaltree(n->nfor.body, 0); 3441556Srgrimes if (evalskip) { 3451556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3461556Srgrimes evalskip = 0; 3471556Srgrimes continue; 3481556Srgrimes } 3491556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3501556Srgrimes evalskip = 0; 3511556Srgrimes break; 3521556Srgrimes } 3531556Srgrimes } 3541556Srgrimes loopnest--; 3551556Srgrimesout: 3561556Srgrimes popstackmark(&smark); 3571556Srgrimes} 3581556Srgrimes 3591556Srgrimes 3601556Srgrimes 3611556SrgrimesSTATIC void 3621556Srgrimesevalcase(n, flags) 3631556Srgrimes union node *n; 36417987Speter int flags; 36517987Speter{ 3661556Srgrimes union node *cp; 3671556Srgrimes union node *patp; 3681556Srgrimes struct arglist arglist; 3691556Srgrimes struct stackmark smark; 3701556Srgrimes 3711556Srgrimes setstackmark(&smark); 3721556Srgrimes arglist.lastp = &arglist.list; 37317987Speter oexitstatus = exitstatus; 3741556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3751556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3761556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3771556Srgrimes if (casematch(patp, arglist.list->text)) { 3781556Srgrimes if (evalskip == 0) { 3791556Srgrimes evaltree(cp->nclist.body, flags); 3801556Srgrimes } 3811556Srgrimes goto out; 3821556Srgrimes } 3831556Srgrimes } 3841556Srgrimes } 3851556Srgrimesout: 3861556Srgrimes popstackmark(&smark); 3871556Srgrimes} 3881556Srgrimes 3891556Srgrimes 3901556Srgrimes 3911556Srgrimes/* 3921556Srgrimes * Kick off a subshell to evaluate a tree. 3931556Srgrimes */ 3941556Srgrimes 3951556SrgrimesSTATIC void 3961556Srgrimesevalsubshell(n, flags) 3971556Srgrimes union node *n; 39817987Speter int flags; 39917987Speter{ 4001556Srgrimes struct job *jp; 4011556Srgrimes int backgnd = (n->type == NBACKGND); 4021556Srgrimes 4031556Srgrimes expredir(n->nredir.redirect); 4041556Srgrimes jp = makejob(n, 1); 4051556Srgrimes if (forkshell(jp, n, backgnd) == 0) { 4061556Srgrimes if (backgnd) 4071556Srgrimes flags &=~ EV_TESTED; 4081556Srgrimes redirect(n->nredir.redirect, 0); 4091556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 4101556Srgrimes } 4111556Srgrimes if (! backgnd) { 4121556Srgrimes INTOFF; 41345916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4141556Srgrimes INTON; 4151556Srgrimes } 4161556Srgrimes} 4171556Srgrimes 4181556Srgrimes 4191556Srgrimes 4201556Srgrimes/* 4211556Srgrimes * Compute the names of the files in a redirection list. 4221556Srgrimes */ 4231556Srgrimes 4241556SrgrimesSTATIC void 4251556Srgrimesexpredir(n) 4261556Srgrimes union node *n; 42717987Speter{ 42825222Ssteve union node *redir; 4291556Srgrimes 4301556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 43117987Speter struct arglist fn; 43217987Speter fn.lastp = &fn.list; 43317987Speter oexitstatus = exitstatus; 43417987Speter switch (redir->type) { 43517987Speter case NFROM: 43617987Speter case NTO: 43717987Speter case NAPPEND: 4381556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4391556Srgrimes redir->nfile.expfname = fn.list->text; 44017987Speter break; 44117987Speter case NFROMFD: 44217987Speter case NTOFD: 44317987Speter if (redir->ndup.vname) { 44417987Speter expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); 44517987Speter fixredir(redir, fn.list->text, 1); 44617987Speter } 44717987Speter break; 4481556Srgrimes } 4491556Srgrimes } 4501556Srgrimes} 4511556Srgrimes 4521556Srgrimes 4531556Srgrimes 4541556Srgrimes/* 4551556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 4561556Srgrimes * of the process creating the pipeline. (This differs from some versions 4571556Srgrimes * of the shell, which make the last process in a pipeline the parent 4581556Srgrimes * of all the rest.) 4591556Srgrimes */ 4601556Srgrimes 4611556SrgrimesSTATIC void 4621556Srgrimesevalpipe(n) 4631556Srgrimes union node *n; 46417987Speter{ 4651556Srgrimes struct job *jp; 4661556Srgrimes struct nodelist *lp; 4671556Srgrimes int pipelen; 4681556Srgrimes int prevfd; 4691556Srgrimes int pip[2]; 4701556Srgrimes 47117987Speter TRACE(("evalpipe(0x%lx) called\n", (long)n)); 4721556Srgrimes pipelen = 0; 4731556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 4741556Srgrimes pipelen++; 4751556Srgrimes INTOFF; 4761556Srgrimes jp = makejob(n, pipelen); 4771556Srgrimes prevfd = -1; 4781556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 4791556Srgrimes prehash(lp->n); 4801556Srgrimes pip[1] = -1; 4811556Srgrimes if (lp->next) { 4821556Srgrimes if (pipe(pip) < 0) { 4831556Srgrimes close(prevfd); 4841556Srgrimes error("Pipe call failed"); 4851556Srgrimes } 4861556Srgrimes } 4871556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 4881556Srgrimes INTON; 4891556Srgrimes if (prevfd > 0) { 4901556Srgrimes close(0); 4911556Srgrimes copyfd(prevfd, 0); 4921556Srgrimes close(prevfd); 4931556Srgrimes } 4941556Srgrimes if (pip[1] >= 0) { 4951556Srgrimes close(pip[0]); 4961556Srgrimes if (pip[1] != 1) { 4971556Srgrimes close(1); 4981556Srgrimes copyfd(pip[1], 1); 4991556Srgrimes close(pip[1]); 5001556Srgrimes } 5011556Srgrimes } 5021556Srgrimes evaltree(lp->n, EV_EXIT); 5031556Srgrimes } 5041556Srgrimes if (prevfd >= 0) 5051556Srgrimes close(prevfd); 5061556Srgrimes prevfd = pip[0]; 5071556Srgrimes close(pip[1]); 5081556Srgrimes } 5091556Srgrimes INTON; 5101556Srgrimes if (n->npipe.backgnd == 0) { 5111556Srgrimes INTOFF; 51245916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 5131556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5141556Srgrimes INTON; 5151556Srgrimes } 5161556Srgrimes} 5171556Srgrimes 5181556Srgrimes 5191556Srgrimes 5201556Srgrimes/* 5211556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5221556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5231556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5241556Srgrimes * Should be called with interrupts off. 5251556Srgrimes */ 5261556Srgrimes 5271556Srgrimesvoid 5281556Srgrimesevalbackcmd(n, result) 5291556Srgrimes union node *n; 5301556Srgrimes struct backcmd *result; 53117987Speter{ 5321556Srgrimes int pip[2]; 5331556Srgrimes struct job *jp; 5341556Srgrimes struct stackmark smark; /* unnecessary */ 5351556Srgrimes 5361556Srgrimes setstackmark(&smark); 5371556Srgrimes result->fd = -1; 5381556Srgrimes result->buf = NULL; 5391556Srgrimes result->nleft = 0; 5401556Srgrimes result->jp = NULL; 54117987Speter if (n == NULL) { 54217987Speter exitstatus = 0; 5431556Srgrimes goto out; 54417987Speter } 5451556Srgrimes if (n->type == NCMD) { 54617987Speter exitstatus = oexitstatus; 5471556Srgrimes evalcommand(n, EV_BACKCMD, result); 5481556Srgrimes } else { 54917987Speter exitstatus = 0; 5501556Srgrimes if (pipe(pip) < 0) 5511556Srgrimes error("Pipe call failed"); 5521556Srgrimes jp = makejob(n, 1); 5531556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 5541556Srgrimes FORCEINTON; 5551556Srgrimes close(pip[0]); 5561556Srgrimes if (pip[1] != 1) { 5571556Srgrimes close(1); 5581556Srgrimes copyfd(pip[1], 1); 5591556Srgrimes close(pip[1]); 5601556Srgrimes } 5611556Srgrimes evaltree(n, EV_EXIT); 5621556Srgrimes } 5631556Srgrimes close(pip[1]); 5641556Srgrimes result->fd = pip[0]; 5651556Srgrimes result->jp = jp; 5661556Srgrimes } 5671556Srgrimesout: 5681556Srgrimes popstackmark(&smark); 5691556Srgrimes TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", 5701556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 5711556Srgrimes} 5721556Srgrimes 5731556Srgrimes 5741556Srgrimes 5751556Srgrimes/* 5761556Srgrimes * Execute a simple command. 5771556Srgrimes */ 5781556Srgrimes 5791556SrgrimesSTATIC void 5801556Srgrimesevalcommand(cmd, flags, backcmd) 5811556Srgrimes union node *cmd; 58217987Speter int flags; 5831556Srgrimes struct backcmd *backcmd; 58417987Speter{ 5851556Srgrimes struct stackmark smark; 5861556Srgrimes union node *argp; 5871556Srgrimes struct arglist arglist; 5881556Srgrimes struct arglist varlist; 5891556Srgrimes char **argv; 5901556Srgrimes int argc; 5911556Srgrimes char **envp; 5921556Srgrimes int varflag; 5931556Srgrimes struct strlist *sp; 5941556Srgrimes int mode; 5951556Srgrimes int pip[2]; 5961556Srgrimes struct cmdentry cmdentry; 5971556Srgrimes struct job *jp; 5981556Srgrimes struct jmploc jmploc; 5991556Srgrimes struct jmploc *volatile savehandler; 6001556Srgrimes char *volatile savecmdname; 6011556Srgrimes volatile struct shparam saveparam; 6021556Srgrimes struct localvar *volatile savelocalvars; 6031556Srgrimes volatile int e; 6041556Srgrimes char *lastarg; 60545916Scracauer int realstatus; 60617987Speter#if __GNUC__ 60717987Speter /* Avoid longjmp clobbering */ 60817987Speter (void) &argv; 60917987Speter (void) &argc; 61017987Speter (void) &lastarg; 61117987Speter (void) &flags; 61217987Speter#endif 6131556Srgrimes 6141556Srgrimes /* First expand the arguments. */ 61517987Speter TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); 6161556Srgrimes setstackmark(&smark); 6171556Srgrimes arglist.lastp = &arglist.list; 6181556Srgrimes varlist.lastp = &varlist.list; 6191556Srgrimes varflag = 1; 62017987Speter oexitstatus = exitstatus; 62117987Speter exitstatus = 0; 6221556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 62317987Speter char *p = argp->narg.text; 6241556Srgrimes if (varflag && is_name(*p)) { 6251556Srgrimes do { 6261556Srgrimes p++; 6271556Srgrimes } while (is_in_name(*p)); 6281556Srgrimes if (*p == '=') { 6291556Srgrimes expandarg(argp, &varlist, EXP_VARTILDE); 6301556Srgrimes continue; 6311556Srgrimes } 6321556Srgrimes } 6331556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 6341556Srgrimes varflag = 0; 6351556Srgrimes } 6361556Srgrimes *arglist.lastp = NULL; 6371556Srgrimes *varlist.lastp = NULL; 6381556Srgrimes expredir(cmd->ncmd.redirect); 6391556Srgrimes argc = 0; 6401556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 6411556Srgrimes argc++; 6421556Srgrimes argv = stalloc(sizeof (char *) * (argc + 1)); 6431556Srgrimes 6441556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6451556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 6461556Srgrimes *argv++ = sp->text; 6471556Srgrimes } 6481556Srgrimes *argv = NULL; 6491556Srgrimes lastarg = NULL; 6501556Srgrimes if (iflag && funcnest == 0 && argc > 0) 6511556Srgrimes lastarg = argv[-1]; 6521556Srgrimes argv -= argc; 6531556Srgrimes 6541556Srgrimes /* Print the command if xflag is set. */ 6551556Srgrimes if (xflag) { 6561556Srgrimes outc('+', &errout); 6571556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 6581556Srgrimes outc(' ', &errout); 6591556Srgrimes out2str(sp->text); 6601556Srgrimes } 6611556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6621556Srgrimes outc(' ', &errout); 6631556Srgrimes out2str(sp->text); 6641556Srgrimes } 6651556Srgrimes outc('\n', &errout); 6661556Srgrimes flushout(&errout); 6671556Srgrimes } 6681556Srgrimes 6691556Srgrimes /* Now locate the command. */ 6701556Srgrimes if (argc == 0) { 6711556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 6721556Srgrimes cmdentry.u.index = BLTINCMD; 6731556Srgrimes } else { 67417987Speter static const char PATH[] = "PATH="; 67517987Speter char *path = pathval(); 67617987Speter 67717987Speter /* 67817987Speter * Modify the command lookup path, if a PATH= assignment 67917987Speter * is present 68017987Speter */ 68117987Speter for (sp = varlist.list ; sp ; sp = sp->next) 68217987Speter if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) 68317987Speter path = sp->text + sizeof(PATH) - 1; 68417987Speter 68517987Speter find_command(argv[0], &cmdentry, 1, path); 6861556Srgrimes if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */ 68720425Ssteve exitstatus = 127; 6881556Srgrimes flushout(&errout); 6891556Srgrimes return; 6901556Srgrimes } 6911556Srgrimes /* implement the bltin builtin here */ 6921556Srgrimes if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) { 6931556Srgrimes for (;;) { 6941556Srgrimes argv++; 6951556Srgrimes if (--argc == 0) 6961556Srgrimes break; 6971556Srgrimes if ((cmdentry.u.index = find_builtin(*argv)) < 0) { 6981556Srgrimes outfmt(&errout, "%s: not found\n", *argv); 69920425Ssteve exitstatus = 127; 7001556Srgrimes flushout(&errout); 7011556Srgrimes return; 7021556Srgrimes } 7031556Srgrimes if (cmdentry.u.index != BLTINCMD) 7041556Srgrimes break; 7051556Srgrimes } 7061556Srgrimes } 7071556Srgrimes } 7081556Srgrimes 7091556Srgrimes /* Fork off a child process if necessary. */ 7101556Srgrimes if (cmd->ncmd.backgnd 71145221Scracauer || (cmdentry.cmdtype == CMDNORMAL 71245221Scracauer && ((flags & EV_EXIT) == 0 || Tflag)) 71317987Speter || ((flags & EV_BACKCMD) != 0 7141556Srgrimes && (cmdentry.cmdtype != CMDBUILTIN 7151556Srgrimes || cmdentry.u.index == DOTCMD 71617987Speter || cmdentry.u.index == EVALCMD))) { 7171556Srgrimes jp = makejob(cmd, 1); 7181556Srgrimes mode = cmd->ncmd.backgnd; 7191556Srgrimes if (flags & EV_BACKCMD) { 7201556Srgrimes mode = FORK_NOJOB; 7211556Srgrimes if (pipe(pip) < 0) 7221556Srgrimes error("Pipe call failed"); 7231556Srgrimes } 7241556Srgrimes if (forkshell(jp, cmd, mode) != 0) 7251556Srgrimes goto parent; /* at end of routine */ 7261556Srgrimes if (flags & EV_BACKCMD) { 7271556Srgrimes FORCEINTON; 7281556Srgrimes close(pip[0]); 7291556Srgrimes if (pip[1] != 1) { 7301556Srgrimes close(1); 7311556Srgrimes copyfd(pip[1], 1); 7321556Srgrimes close(pip[1]); 7331556Srgrimes } 7341556Srgrimes } 7351556Srgrimes flags |= EV_EXIT; 7361556Srgrimes } 7371556Srgrimes 7381556Srgrimes /* This is the child process if a fork occurred. */ 7391556Srgrimes /* Execute the command. */ 7401556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 74120425Ssteve#ifdef DEBUG 7421556Srgrimes trputs("Shell function: "); trargs(argv); 74320425Ssteve#endif 7441556Srgrimes redirect(cmd->ncmd.redirect, REDIR_PUSH); 7451556Srgrimes saveparam = shellparam; 7461556Srgrimes shellparam.malloc = 0; 74720425Ssteve shellparam.reset = 1; 7481556Srgrimes shellparam.nparam = argc - 1; 7491556Srgrimes shellparam.p = argv + 1; 7501556Srgrimes shellparam.optnext = NULL; 7511556Srgrimes INTOFF; 7521556Srgrimes savelocalvars = localvars; 7531556Srgrimes localvars = NULL; 7541556Srgrimes INTON; 7551556Srgrimes if (setjmp(jmploc.loc)) { 7561556Srgrimes if (exception == EXSHELLPROC) 7571556Srgrimes freeparam((struct shparam *)&saveparam); 7581556Srgrimes else { 7591556Srgrimes freeparam(&shellparam); 7601556Srgrimes shellparam = saveparam; 7611556Srgrimes } 7621556Srgrimes poplocalvars(); 7631556Srgrimes localvars = savelocalvars; 7641556Srgrimes handler = savehandler; 7651556Srgrimes longjmp(handler->loc, 1); 7661556Srgrimes } 7671556Srgrimes savehandler = handler; 7681556Srgrimes handler = &jmploc; 7691556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 7701556Srgrimes mklocal(sp->text); 7711556Srgrimes funcnest++; 77235675Scracauer if (flags & EV_TESTED) 77335675Scracauer evaltree(cmdentry.u.func, EV_TESTED); 77435675Scracauer else 77535675Scracauer evaltree(cmdentry.u.func, 0); 7761556Srgrimes funcnest--; 7771556Srgrimes INTOFF; 7781556Srgrimes poplocalvars(); 7791556Srgrimes localvars = savelocalvars; 7801556Srgrimes freeparam(&shellparam); 7811556Srgrimes shellparam = saveparam; 7821556Srgrimes handler = savehandler; 7831556Srgrimes popredir(); 7841556Srgrimes INTON; 7851556Srgrimes if (evalskip == SKIPFUNC) { 7861556Srgrimes evalskip = 0; 7871556Srgrimes skipcount = 0; 7881556Srgrimes } 7891556Srgrimes if (flags & EV_EXIT) 7901556Srgrimes exitshell(exitstatus); 7911556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 79220425Ssteve#ifdef DEBUG 7931556Srgrimes trputs("builtin command: "); trargs(argv); 79420425Ssteve#endif 7951556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 7961556Srgrimes if (flags == EV_BACKCMD) { 7971556Srgrimes memout.nleft = 0; 7981556Srgrimes memout.nextc = memout.buf; 7991556Srgrimes memout.bufsize = 64; 8001556Srgrimes mode |= REDIR_BACKQ; 8011556Srgrimes } 8021556Srgrimes redirect(cmd->ncmd.redirect, mode); 8031556Srgrimes savecmdname = commandname; 8041556Srgrimes cmdenviron = varlist.list; 8051556Srgrimes e = -1; 8061556Srgrimes if (setjmp(jmploc.loc)) { 8071556Srgrimes e = exception; 8081556Srgrimes exitstatus = (e == EXINT)? SIGINT+128 : 2; 8091556Srgrimes goto cmddone; 8101556Srgrimes } 8111556Srgrimes savehandler = handler; 8121556Srgrimes handler = &jmploc; 8131556Srgrimes commandname = argv[0]; 8141556Srgrimes argptr = argv + 1; 8151556Srgrimes optptr = NULL; /* initialize nextopt */ 8161556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 8171556Srgrimes flushall(); 8181556Srgrimescmddone: 8191556Srgrimes out1 = &output; 8201556Srgrimes out2 = &errout; 8211556Srgrimes freestdout(); 8221556Srgrimes if (e != EXSHELLPROC) { 8231556Srgrimes commandname = savecmdname; 8241556Srgrimes if (flags & EV_EXIT) { 8251556Srgrimes exitshell(exitstatus); 8261556Srgrimes } 8271556Srgrimes } 8281556Srgrimes handler = savehandler; 8291556Srgrimes if (e != -1) { 83020425Ssteve if ((e != EXERROR && e != EXEXEC) 83120425Ssteve || cmdentry.u.index == BLTINCMD 83220425Ssteve || cmdentry.u.index == DOTCMD 83320425Ssteve || cmdentry.u.index == EVALCMD 83417987Speter#ifndef NO_HISTORY 83520425Ssteve || cmdentry.u.index == HISTCMD 83617987Speter#endif 83720425Ssteve || cmdentry.u.index == EXECCMD) 8381556Srgrimes exraise(e); 8391556Srgrimes FORCEINTON; 8401556Srgrimes } 8411556Srgrimes if (cmdentry.u.index != EXECCMD) 8421556Srgrimes popredir(); 8431556Srgrimes if (flags == EV_BACKCMD) { 8441556Srgrimes backcmd->buf = memout.buf; 8451556Srgrimes backcmd->nleft = memout.nextc - memout.buf; 8461556Srgrimes memout.buf = NULL; 8471556Srgrimes } 8481556Srgrimes } else { 84920425Ssteve#ifdef DEBUG 8501556Srgrimes trputs("normal command: "); trargs(argv); 85120425Ssteve#endif 8521556Srgrimes clearredir(); 8531556Srgrimes redirect(cmd->ncmd.redirect, 0); 8541556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8551556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 8561556Srgrimes envp = environment(); 85717987Speter shellexec(argv, envp, pathval(), cmdentry.u.index); 8581556Srgrimes /*NOTREACHED*/ 8591556Srgrimes } 8601556Srgrimes goto out; 8611556Srgrimes 8621556Srgrimesparent: /* parent process gets here (if we forked) */ 8631556Srgrimes if (mode == 0) { /* argument to fork */ 8641556Srgrimes INTOFF; 86545916Scracauer exitstatus = waitforjob(jp, &realstatus); 8661556Srgrimes INTON; 86745916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 86845266Scracauer evalskip = SKIPBREAK; 86945266Scracauer skipcount = loopnest; 87045266Scracauer } 8711556Srgrimes } else if (mode == 2) { 8721556Srgrimes backcmd->fd = pip[0]; 8731556Srgrimes close(pip[1]); 8741556Srgrimes backcmd->jp = jp; 8751556Srgrimes } 8761556Srgrimes 8771556Srgrimesout: 8781556Srgrimes if (lastarg) 8791556Srgrimes setvar("_", lastarg, 0); 8801556Srgrimes popstackmark(&smark); 8811556Srgrimes} 8821556Srgrimes 8831556Srgrimes 8841556Srgrimes 8851556Srgrimes/* 8861556Srgrimes * Search for a command. This is called before we fork so that the 8871556Srgrimes * location of the command will be available in the parent as well as 8881556Srgrimes * the child. The check for "goodname" is an overly conservative 8891556Srgrimes * check that the name will not be subject to expansion. 8901556Srgrimes */ 8911556Srgrimes 8921556SrgrimesSTATIC void 8931556Srgrimesprehash(n) 8941556Srgrimes union node *n; 89517987Speter{ 8961556Srgrimes struct cmdentry entry; 8971556Srgrimes 89817987Speter if (n->type == NCMD && n->ncmd.args) 89917987Speter if (goodname(n->ncmd.args->narg.text)) 90017987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 90117987Speter pathval()); 9021556Srgrimes} 9031556Srgrimes 9041556Srgrimes 9051556Srgrimes 9061556Srgrimes/* 9071556Srgrimes * Builtin commands. Builtin commands whose functions are closely 9081556Srgrimes * tied to evaluation are implemented here. 9091556Srgrimes */ 9101556Srgrimes 9111556Srgrimes/* 9121556Srgrimes * No command given, or a bltin command with no arguments. Set the 9131556Srgrimes * specified variables. 9141556Srgrimes */ 9151556Srgrimes 91617987Speterint 91717987Speterbltincmd(argc, argv) 91825905Ssteve int argc __unused; 91925905Ssteve char **argv __unused; 92017987Speter{ 9211556Srgrimes listsetvar(cmdenviron); 92220425Ssteve /* 92317987Speter * Preserve exitstatus of a previous possible redirection 92420425Ssteve * as POSIX mandates 92517987Speter */ 9261556Srgrimes return exitstatus; 9271556Srgrimes} 9281556Srgrimes 9291556Srgrimes 9301556Srgrimes/* 9311556Srgrimes * Handle break and continue commands. Break, continue, and return are 9321556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 9331556Srgrimes * above all check this flag, and if it is set they start skipping 9341556Srgrimes * commands rather than executing them. The variable skipcount is 9351556Srgrimes * the number of loops to break/continue, or the number of function 9361556Srgrimes * levels to return. (The latter is always 1.) It should probably 9371556Srgrimes * be an error to break out of more loops than exist, but it isn't 9381556Srgrimes * in the standard shell so we don't make it one here. 9391556Srgrimes */ 9401556Srgrimes 94117987Speterint 94217987Speterbreakcmd(argc, argv) 94317987Speter int argc; 94420425Ssteve char **argv; 94517987Speter{ 94620425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 9471556Srgrimes 9481556Srgrimes if (n > loopnest) 9491556Srgrimes n = loopnest; 9501556Srgrimes if (n > 0) { 9511556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 9521556Srgrimes skipcount = n; 9531556Srgrimes } 9541556Srgrimes return 0; 9551556Srgrimes} 9561556Srgrimes 9571556Srgrimes 9581556Srgrimes/* 9591556Srgrimes * The return command. 9601556Srgrimes */ 9611556Srgrimes 96217987Speterint 96320425Sstevereturncmd(argc, argv) 96417987Speter int argc; 96520425Ssteve char **argv; 96617987Speter{ 96720425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 9681556Srgrimes 9691556Srgrimes if (funcnest) { 9701556Srgrimes evalskip = SKIPFUNC; 9711556Srgrimes skipcount = 1; 97220425Ssteve } else { 97320425Ssteve /* skip the rest of the file */ 97420425Ssteve evalskip = SKIPFILE; 97520425Ssteve skipcount = 1; 9761556Srgrimes } 9771556Srgrimes return ret; 9781556Srgrimes} 9791556Srgrimes 9801556Srgrimes 98117987Speterint 98220425Sstevefalsecmd(argc, argv) 98325905Ssteve int argc __unused; 98425905Ssteve char **argv __unused; 98517987Speter{ 98617987Speter return 1; 98717987Speter} 98817987Speter 98917987Speter 99017987Speterint 99120425Sstevetruecmd(argc, argv) 99225905Ssteve int argc __unused; 99325905Ssteve char **argv __unused; 99417987Speter{ 9951556Srgrimes return 0; 9961556Srgrimes} 9971556Srgrimes 9981556Srgrimes 99917987Speterint 100020425Ssteveexeccmd(argc, argv) 100117987Speter int argc; 100220425Ssteve char **argv; 100317987Speter{ 10041556Srgrimes if (argc > 1) { 100517987Speter struct strlist *sp; 100617987Speter 10071556Srgrimes iflag = 0; /* exit on error */ 10081556Srgrimes mflag = 0; 10091556Srgrimes optschanged(); 101017987Speter for (sp = cmdenviron; sp ; sp = sp->next) 101117987Speter setvareq(sp->text, VEXPORT|VSTACK); 10121556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 10131556Srgrimes 10141556Srgrimes } 10151556Srgrimes return 0; 10161556Srgrimes} 1017