eval.c revision 99110
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 411556Srgrimes#endif /* not lint */ 4299110Sobrien#include <sys/cdefs.h> 4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/eval.c 99110 2002-06-30 05:15:05Z obrien $"); 441556Srgrimes 4517987Speter#include <signal.h> 4617987Speter#include <unistd.h> 4745266Scracauer#include <sys/wait.h> /* For WIFSIGNALED(status) */ 4853891Scracauer#include <errno.h> 4917987Speter 501556Srgrimes/* 511556Srgrimes * Evaluate a command. 521556Srgrimes */ 531556Srgrimes 541556Srgrimes#include "shell.h" 551556Srgrimes#include "nodes.h" 561556Srgrimes#include "syntax.h" 571556Srgrimes#include "expand.h" 581556Srgrimes#include "parser.h" 591556Srgrimes#include "jobs.h" 601556Srgrimes#include "eval.h" 611556Srgrimes#include "builtins.h" 621556Srgrimes#include "options.h" 631556Srgrimes#include "exec.h" 641556Srgrimes#include "redir.h" 651556Srgrimes#include "input.h" 661556Srgrimes#include "output.h" 671556Srgrimes#include "trap.h" 681556Srgrimes#include "var.h" 691556Srgrimes#include "memalloc.h" 701556Srgrimes#include "error.h" 7117987Speter#include "show.h" 721556Srgrimes#include "mystring.h" 7317987Speter#ifndef NO_HISTORY 741556Srgrimes#include "myhistedit.h" 7517987Speter#endif 761556Srgrimes 771556Srgrimes 781556Srgrimes/* flags in argument to evaltree */ 791556Srgrimes#define EV_EXIT 01 /* exit after evaluating tree */ 801556Srgrimes#define EV_TESTED 02 /* exit status is checked; ignore -e flag */ 811556Srgrimes#define EV_BACKCMD 04 /* command executing within back quotes */ 821556Srgrimes 831556SrgrimesMKINIT int evalskip; /* set if we are skipping commands */ 841556SrgrimesSTATIC int skipcount; /* number of levels to skip */ 851556SrgrimesMKINIT int loopnest; /* current loop nesting level */ 861556Srgrimesint funcnest; /* depth of function calls */ 871556Srgrimes 881556Srgrimes 891556Srgrimeschar *commandname; 901556Srgrimesstruct strlist *cmdenviron; 911556Srgrimesint exitstatus; /* exit status of last command */ 9217987Speterint oexitstatus; /* saved exit status */ 931556Srgrimes 941556Srgrimes 9590111SimpSTATIC void evalloop(union node *); 9690111SimpSTATIC void evalfor(union node *); 9790111SimpSTATIC void evalcase(union node *, int); 9890111SimpSTATIC void evalsubshell(union node *, int); 9990111SimpSTATIC void expredir(union node *); 10090111SimpSTATIC void evalpipe(union node *); 10190111SimpSTATIC void evalcommand(union node *, int, struct backcmd *); 10290111SimpSTATIC void prehash(union node *); 1031556Srgrimes 1041556Srgrimes 1051556Srgrimes/* 1061556Srgrimes * Called to reset things after an exception. 1071556Srgrimes */ 1081556Srgrimes 1091556Srgrimes#ifdef mkinit 1101556SrgrimesINCLUDE "eval.h" 1111556Srgrimes 1121556SrgrimesRESET { 1131556Srgrimes evalskip = 0; 1141556Srgrimes loopnest = 0; 1151556Srgrimes funcnest = 0; 1161556Srgrimes} 1171556Srgrimes 1181556SrgrimesSHELLPROC { 1191556Srgrimes exitstatus = 0; 1201556Srgrimes} 1211556Srgrimes#endif 1221556Srgrimes 1231556Srgrimes 1241556Srgrimes 1251556Srgrimes/* 12646684Skris * The eval command. 1271556Srgrimes */ 1281556Srgrimes 12917987Speterint 13090111Simpevalcmd(int argc, char **argv) 1311556Srgrimes{ 1321556Srgrimes char *p; 1331556Srgrimes char *concat; 1341556Srgrimes char **ap; 1351556Srgrimes 1361556Srgrimes if (argc > 1) { 1371556Srgrimes p = argv[1]; 1381556Srgrimes if (argc > 2) { 1391556Srgrimes STARTSTACKSTR(concat); 1401556Srgrimes ap = argv + 2; 1411556Srgrimes for (;;) { 1421556Srgrimes while (*p) 1431556Srgrimes STPUTC(*p++, concat); 1441556Srgrimes if ((p = *ap++) == NULL) 1451556Srgrimes break; 1461556Srgrimes STPUTC(' ', concat); 1471556Srgrimes } 1481556Srgrimes STPUTC('\0', concat); 1491556Srgrimes p = grabstackstr(concat); 1501556Srgrimes } 1511556Srgrimes evalstring(p); 1521556Srgrimes } 1531556Srgrimes return exitstatus; 1541556Srgrimes} 1551556Srgrimes 1561556Srgrimes 1571556Srgrimes/* 1581556Srgrimes * Execute a command or commands contained in a string. 1591556Srgrimes */ 1601556Srgrimes 1611556Srgrimesvoid 16290111Simpevalstring(char *s) 16390111Simp{ 1641556Srgrimes union node *n; 1651556Srgrimes struct stackmark smark; 1661556Srgrimes 1671556Srgrimes setstackmark(&smark); 1681556Srgrimes setinputstring(s, 1); 1691556Srgrimes while ((n = parsecmd(0)) != NEOF) { 1701556Srgrimes evaltree(n, 0); 1711556Srgrimes popstackmark(&smark); 1721556Srgrimes } 1731556Srgrimes popfile(); 1741556Srgrimes popstackmark(&smark); 1751556Srgrimes} 1761556Srgrimes 1771556Srgrimes 1781556Srgrimes 1791556Srgrimes/* 1801556Srgrimes * Evaluate a parse tree. The value is left in the global variable 1811556Srgrimes * exitstatus. 1821556Srgrimes */ 1831556Srgrimes 1841556Srgrimesvoid 18590111Simpevaltree(union node *n, int flags) 18617987Speter{ 1871556Srgrimes if (n == NULL) { 1881556Srgrimes TRACE(("evaltree(NULL) called\n")); 1891556Srgrimes exitstatus = 0; 1901556Srgrimes goto out; 1911556Srgrimes } 19217987Speter#ifndef NO_HISTORY 1931556Srgrimes displayhist = 1; /* show history substitutions done with fc */ 19417987Speter#endif 19517987Speter TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type)); 1961556Srgrimes switch (n->type) { 1971556Srgrimes case NSEMI: 1981556Srgrimes evaltree(n->nbinary.ch1, 0); 1991556Srgrimes if (evalskip) 2001556Srgrimes goto out; 2011556Srgrimes evaltree(n->nbinary.ch2, flags); 2021556Srgrimes break; 2031556Srgrimes case NAND: 2041556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 20518754Ssteve if (evalskip || exitstatus != 0) { 20618754Ssteve flags |= EV_TESTED; 2071556Srgrimes goto out; 20818754Ssteve } 2091556Srgrimes evaltree(n->nbinary.ch2, flags); 2101556Srgrimes break; 2111556Srgrimes case NOR: 2121556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 2131556Srgrimes if (evalskip || exitstatus == 0) 2141556Srgrimes goto out; 2151556Srgrimes evaltree(n->nbinary.ch2, flags); 2161556Srgrimes break; 2171556Srgrimes case NREDIR: 2181556Srgrimes expredir(n->nredir.redirect); 2191556Srgrimes redirect(n->nredir.redirect, REDIR_PUSH); 2201556Srgrimes evaltree(n->nredir.n, flags); 2211556Srgrimes popredir(); 2221556Srgrimes break; 2231556Srgrimes case NSUBSHELL: 2241556Srgrimes evalsubshell(n, flags); 2251556Srgrimes break; 2261556Srgrimes case NBACKGND: 2271556Srgrimes evalsubshell(n, flags); 2281556Srgrimes break; 2291556Srgrimes case NIF: { 2301556Srgrimes evaltree(n->nif.test, EV_TESTED); 2311556Srgrimes if (evalskip) 2321556Srgrimes goto out; 23320425Ssteve if (exitstatus == 0) 2341556Srgrimes evaltree(n->nif.ifpart, flags); 23517987Speter else if (n->nif.elsepart) 2361556Srgrimes evaltree(n->nif.elsepart, flags); 23720425Ssteve else 23820425Ssteve exitstatus = 0; 2391556Srgrimes break; 2401556Srgrimes } 2411556Srgrimes case NWHILE: 2421556Srgrimes case NUNTIL: 2431556Srgrimes evalloop(n); 2441556Srgrimes break; 2451556Srgrimes case NFOR: 2461556Srgrimes evalfor(n); 24777557Sgad /* 24877557Sgad * The 'for' command does not set exitstatus, so the value 24977557Sgad * now in exitstatus is from the last command executed in 25077557Sgad * the 'for' loop. That exit value had been tested (wrt 25177557Sgad * 'sh -e' checking) while processing that command, and 25277557Sgad * it should not be re-tested here. 25377557Sgad */ 25477557Sgad flags |= EV_TESTED; 2551556Srgrimes break; 2561556Srgrimes case NCASE: 2571556Srgrimes evalcase(n, flags); 25877557Sgad /* 25977557Sgad * The 'case' command does not set exitstatus, so the value 26077557Sgad * now in exitstatus is from the last command executed in 26177557Sgad * the 'case' block. That exit value had been tested (wrt 26277557Sgad * 'sh -e' checking) while processing that command, and 26377557Sgad * it should not be re-tested here. 26477557Sgad */ 26577557Sgad flags |= EV_TESTED; 2661556Srgrimes break; 2671556Srgrimes case NDEFUN: 2681556Srgrimes defun(n->narg.text, n->narg.next); 2691556Srgrimes exitstatus = 0; 2701556Srgrimes break; 2711556Srgrimes case NNOT: 2721556Srgrimes evaltree(n->nnot.com, EV_TESTED); 2731556Srgrimes exitstatus = !exitstatus; 2741556Srgrimes break; 2751556Srgrimes 2761556Srgrimes case NPIPE: 2771556Srgrimes evalpipe(n); 2781556Srgrimes break; 2791556Srgrimes case NCMD: 2801556Srgrimes evalcommand(n, flags, (struct backcmd *)NULL); 2811556Srgrimes break; 2821556Srgrimes default: 2831556Srgrimes out1fmt("Node type = %d\n", n->type); 2841556Srgrimes flushout(&output); 2851556Srgrimes break; 2861556Srgrimes } 2871556Srgrimesout: 2881556Srgrimes if (pendingsigs) 2891556Srgrimes dotrap(); 29052526Scracauer /* 29152526Scracauer * XXX - Like "!(n->type == NSEMI)", more types will probably 29252526Scracauer * need to be excluded from this test. It's probably better 29352526Scracauer * to set or unset EV_TESTED in the loop above than to bloat 29452526Scracauer * the conditional here. 29552526Scracauer */ 29652526Scracauer if ((flags & EV_EXIT) || (eflag && exitstatus 29752526Scracauer && !(flags & EV_TESTED) && !(n->type == NSEMI))) 2981556Srgrimes exitshell(exitstatus); 2991556Srgrimes} 3001556Srgrimes 3011556Srgrimes 3021556SrgrimesSTATIC void 30390111Simpevalloop(union node *n) 30417987Speter{ 3051556Srgrimes int status; 3061556Srgrimes 3071556Srgrimes loopnest++; 3081556Srgrimes status = 0; 3091556Srgrimes for (;;) { 3101556Srgrimes evaltree(n->nbinary.ch1, EV_TESTED); 3111556Srgrimes if (evalskip) { 3121556Srgrimesskipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 3131556Srgrimes evalskip = 0; 3141556Srgrimes continue; 3151556Srgrimes } 3161556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3171556Srgrimes evalskip = 0; 3181556Srgrimes break; 3191556Srgrimes } 3201556Srgrimes if (n->type == NWHILE) { 3211556Srgrimes if (exitstatus != 0) 3221556Srgrimes break; 3231556Srgrimes } else { 3241556Srgrimes if (exitstatus == 0) 3251556Srgrimes break; 3261556Srgrimes } 3271556Srgrimes evaltree(n->nbinary.ch2, 0); 3281556Srgrimes status = exitstatus; 3291556Srgrimes if (evalskip) 3301556Srgrimes goto skipping; 3311556Srgrimes } 3321556Srgrimes loopnest--; 3331556Srgrimes exitstatus = status; 3341556Srgrimes} 3351556Srgrimes 3361556Srgrimes 3371556Srgrimes 3381556SrgrimesSTATIC void 33990111Simpevalfor(union node *n) 34017987Speter{ 3411556Srgrimes struct arglist arglist; 3421556Srgrimes union node *argp; 3431556Srgrimes struct strlist *sp; 3441556Srgrimes struct stackmark smark; 3451556Srgrimes 3461556Srgrimes setstackmark(&smark); 3471556Srgrimes arglist.lastp = &arglist.list; 3481556Srgrimes for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 34917987Speter oexitstatus = exitstatus; 3501556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 3511556Srgrimes if (evalskip) 3521556Srgrimes goto out; 3531556Srgrimes } 3541556Srgrimes *arglist.lastp = NULL; 3551556Srgrimes 3561556Srgrimes exitstatus = 0; 3571556Srgrimes loopnest++; 3581556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 3591556Srgrimes setvar(n->nfor.var, sp->text, 0); 3601556Srgrimes evaltree(n->nfor.body, 0); 3611556Srgrimes if (evalskip) { 3621556Srgrimes if (evalskip == SKIPCONT && --skipcount <= 0) { 3631556Srgrimes evalskip = 0; 3641556Srgrimes continue; 3651556Srgrimes } 3661556Srgrimes if (evalskip == SKIPBREAK && --skipcount <= 0) 3671556Srgrimes evalskip = 0; 3681556Srgrimes break; 3691556Srgrimes } 3701556Srgrimes } 3711556Srgrimes loopnest--; 3721556Srgrimesout: 3731556Srgrimes popstackmark(&smark); 3741556Srgrimes} 3751556Srgrimes 3761556Srgrimes 3771556Srgrimes 3781556SrgrimesSTATIC void 37990111Simpevalcase(union node *n, int flags) 38017987Speter{ 3811556Srgrimes union node *cp; 3821556Srgrimes union node *patp; 3831556Srgrimes struct arglist arglist; 3841556Srgrimes struct stackmark smark; 3851556Srgrimes 3861556Srgrimes setstackmark(&smark); 3871556Srgrimes arglist.lastp = &arglist.list; 38817987Speter oexitstatus = exitstatus; 3891556Srgrimes expandarg(n->ncase.expr, &arglist, EXP_TILDE); 3901556Srgrimes for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 3911556Srgrimes for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 3921556Srgrimes if (casematch(patp, arglist.list->text)) { 3931556Srgrimes if (evalskip == 0) { 3941556Srgrimes evaltree(cp->nclist.body, flags); 3951556Srgrimes } 3961556Srgrimes goto out; 3971556Srgrimes } 3981556Srgrimes } 3991556Srgrimes } 4001556Srgrimesout: 4011556Srgrimes popstackmark(&smark); 4021556Srgrimes} 4031556Srgrimes 4041556Srgrimes 4051556Srgrimes 4061556Srgrimes/* 4071556Srgrimes * Kick off a subshell to evaluate a tree. 4081556Srgrimes */ 4091556Srgrimes 4101556SrgrimesSTATIC void 41190111Simpevalsubshell(union node *n, int flags) 41217987Speter{ 4131556Srgrimes struct job *jp; 4141556Srgrimes int backgnd = (n->type == NBACKGND); 4151556Srgrimes 4161556Srgrimes expredir(n->nredir.redirect); 4171556Srgrimes jp = makejob(n, 1); 4181556Srgrimes if (forkshell(jp, n, backgnd) == 0) { 4191556Srgrimes if (backgnd) 4201556Srgrimes flags &=~ EV_TESTED; 4211556Srgrimes redirect(n->nredir.redirect, 0); 4221556Srgrimes evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */ 4231556Srgrimes } 4241556Srgrimes if (! backgnd) { 4251556Srgrimes INTOFF; 42645916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 4271556Srgrimes INTON; 4281556Srgrimes } 4291556Srgrimes} 4301556Srgrimes 4311556Srgrimes 4321556Srgrimes 4331556Srgrimes/* 4341556Srgrimes * Compute the names of the files in a redirection list. 4351556Srgrimes */ 4361556Srgrimes 4371556SrgrimesSTATIC void 43890111Simpexpredir(union node *n) 43917987Speter{ 44025222Ssteve union node *redir; 4411556Srgrimes 4421556Srgrimes for (redir = n ; redir ; redir = redir->nfile.next) { 44317987Speter struct arglist fn; 44417987Speter fn.lastp = &fn.list; 44517987Speter oexitstatus = exitstatus; 44617987Speter switch (redir->type) { 44717987Speter case NFROM: 44817987Speter case NTO: 44966612Sbrian case NFROMTO: 45017987Speter case NAPPEND: 45196922Stjr case NCLOBBER: 4521556Srgrimes expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 4531556Srgrimes redir->nfile.expfname = fn.list->text; 45417987Speter break; 45517987Speter case NFROMFD: 45617987Speter case NTOFD: 45717987Speter if (redir->ndup.vname) { 45817987Speter expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); 45917987Speter fixredir(redir, fn.list->text, 1); 46017987Speter } 46117987Speter break; 4621556Srgrimes } 4631556Srgrimes } 4641556Srgrimes} 4651556Srgrimes 4661556Srgrimes 4671556Srgrimes 4681556Srgrimes/* 4691556Srgrimes * Evaluate a pipeline. All the processes in the pipeline are children 4701556Srgrimes * of the process creating the pipeline. (This differs from some versions 4711556Srgrimes * of the shell, which make the last process in a pipeline the parent 4721556Srgrimes * of all the rest.) 4731556Srgrimes */ 4741556Srgrimes 4751556SrgrimesSTATIC void 47690111Simpevalpipe(union node *n) 47717987Speter{ 4781556Srgrimes struct job *jp; 4791556Srgrimes struct nodelist *lp; 4801556Srgrimes int pipelen; 4811556Srgrimes int prevfd; 4821556Srgrimes int pip[2]; 4831556Srgrimes 48417987Speter TRACE(("evalpipe(0x%lx) called\n", (long)n)); 4851556Srgrimes pipelen = 0; 4861556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 4871556Srgrimes pipelen++; 4881556Srgrimes INTOFF; 4891556Srgrimes jp = makejob(n, pipelen); 4901556Srgrimes prevfd = -1; 4911556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 4921556Srgrimes prehash(lp->n); 4931556Srgrimes pip[1] = -1; 4941556Srgrimes if (lp->next) { 4951556Srgrimes if (pipe(pip) < 0) { 4961556Srgrimes close(prevfd); 49753891Scracauer error("Pipe call failed: %s", strerror(errno)); 4981556Srgrimes } 4991556Srgrimes } 5001556Srgrimes if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 5011556Srgrimes INTON; 5021556Srgrimes if (prevfd > 0) { 5031556Srgrimes close(0); 5041556Srgrimes copyfd(prevfd, 0); 5051556Srgrimes close(prevfd); 5061556Srgrimes } 5071556Srgrimes if (pip[1] >= 0) { 50853282Scracauer if (!(prevfd >= 0 && pip[0] == 0)) 50952900Scracauer close(pip[0]); 5101556Srgrimes if (pip[1] != 1) { 5111556Srgrimes close(1); 5121556Srgrimes copyfd(pip[1], 1); 5131556Srgrimes close(pip[1]); 5141556Srgrimes } 5151556Srgrimes } 5161556Srgrimes evaltree(lp->n, EV_EXIT); 5171556Srgrimes } 5181556Srgrimes if (prevfd >= 0) 5191556Srgrimes close(prevfd); 5201556Srgrimes prevfd = pip[0]; 5211556Srgrimes close(pip[1]); 5221556Srgrimes } 5231556Srgrimes INTON; 5241556Srgrimes if (n->npipe.backgnd == 0) { 5251556Srgrimes INTOFF; 52645916Scracauer exitstatus = waitforjob(jp, (int *)NULL); 5271556Srgrimes TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 5281556Srgrimes INTON; 5291556Srgrimes } 5301556Srgrimes} 5311556Srgrimes 5321556Srgrimes 5331556Srgrimes 5341556Srgrimes/* 5351556Srgrimes * Execute a command inside back quotes. If it's a builtin command, we 5361556Srgrimes * want to save its output in a block obtained from malloc. Otherwise 5371556Srgrimes * we fork off a subprocess and get the output of the command via a pipe. 5381556Srgrimes * Should be called with interrupts off. 5391556Srgrimes */ 5401556Srgrimes 5411556Srgrimesvoid 54290111Simpevalbackcmd(union node *n, struct backcmd *result) 54317987Speter{ 5441556Srgrimes int pip[2]; 5451556Srgrimes struct job *jp; 5461556Srgrimes struct stackmark smark; /* unnecessary */ 5471556Srgrimes 5481556Srgrimes setstackmark(&smark); 5491556Srgrimes result->fd = -1; 5501556Srgrimes result->buf = NULL; 5511556Srgrimes result->nleft = 0; 5521556Srgrimes result->jp = NULL; 55317987Speter if (n == NULL) { 55417987Speter exitstatus = 0; 5551556Srgrimes goto out; 55617987Speter } 5571556Srgrimes if (n->type == NCMD) { 55817987Speter exitstatus = oexitstatus; 5591556Srgrimes evalcommand(n, EV_BACKCMD, result); 5601556Srgrimes } else { 56117987Speter exitstatus = 0; 5621556Srgrimes if (pipe(pip) < 0) 56353891Scracauer error("Pipe call failed: %s", strerror(errno)); 5641556Srgrimes jp = makejob(n, 1); 5651556Srgrimes if (forkshell(jp, n, FORK_NOJOB) == 0) { 5661556Srgrimes FORCEINTON; 5671556Srgrimes close(pip[0]); 5681556Srgrimes if (pip[1] != 1) { 5691556Srgrimes close(1); 5701556Srgrimes copyfd(pip[1], 1); 5711556Srgrimes close(pip[1]); 5721556Srgrimes } 5731556Srgrimes evaltree(n, EV_EXIT); 5741556Srgrimes } 5751556Srgrimes close(pip[1]); 5761556Srgrimes result->fd = pip[0]; 5771556Srgrimes result->jp = jp; 5781556Srgrimes } 5791556Srgrimesout: 5801556Srgrimes popstackmark(&smark); 5811556Srgrimes TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", 5821556Srgrimes result->fd, result->buf, result->nleft, result->jp)); 5831556Srgrimes} 5841556Srgrimes 5851556Srgrimes 5861556Srgrimes 5871556Srgrimes/* 5881556Srgrimes * Execute a simple command. 5891556Srgrimes */ 5901556Srgrimes 5911556SrgrimesSTATIC void 59290111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd) 59317987Speter{ 5941556Srgrimes struct stackmark smark; 5951556Srgrimes union node *argp; 5961556Srgrimes struct arglist arglist; 5971556Srgrimes struct arglist varlist; 5981556Srgrimes char **argv; 5991556Srgrimes int argc; 6001556Srgrimes char **envp; 6011556Srgrimes int varflag; 6021556Srgrimes struct strlist *sp; 6031556Srgrimes int mode; 6041556Srgrimes int pip[2]; 6051556Srgrimes struct cmdentry cmdentry; 6061556Srgrimes struct job *jp; 6071556Srgrimes struct jmploc jmploc; 6081556Srgrimes struct jmploc *volatile savehandler; 6091556Srgrimes char *volatile savecmdname; 6101556Srgrimes volatile struct shparam saveparam; 6111556Srgrimes struct localvar *volatile savelocalvars; 6121556Srgrimes volatile int e; 6131556Srgrimes char *lastarg; 61445916Scracauer int realstatus; 61554884Scracauer int do_clearcmdentry; 61617987Speter#if __GNUC__ 61717987Speter /* Avoid longjmp clobbering */ 61817987Speter (void) &argv; 61917987Speter (void) &argc; 62017987Speter (void) &lastarg; 62117987Speter (void) &flags; 62254884Scracauer (void) &do_clearcmdentry; 62317987Speter#endif 6241556Srgrimes 6251556Srgrimes /* First expand the arguments. */ 62617987Speter TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); 6271556Srgrimes setstackmark(&smark); 6281556Srgrimes arglist.lastp = &arglist.list; 6291556Srgrimes varlist.lastp = &varlist.list; 6301556Srgrimes varflag = 1; 63154884Scracauer do_clearcmdentry = 0; 63217987Speter oexitstatus = exitstatus; 63317987Speter exitstatus = 0; 6341556Srgrimes for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 63517987Speter char *p = argp->narg.text; 6361556Srgrimes if (varflag && is_name(*p)) { 6371556Srgrimes do { 6381556Srgrimes p++; 6391556Srgrimes } while (is_in_name(*p)); 6401556Srgrimes if (*p == '=') { 6411556Srgrimes expandarg(argp, &varlist, EXP_VARTILDE); 6421556Srgrimes continue; 6431556Srgrimes } 6441556Srgrimes } 6451556Srgrimes expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 6461556Srgrimes varflag = 0; 6471556Srgrimes } 6481556Srgrimes *arglist.lastp = NULL; 6491556Srgrimes *varlist.lastp = NULL; 6501556Srgrimes expredir(cmd->ncmd.redirect); 6511556Srgrimes argc = 0; 6521556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) 6531556Srgrimes argc++; 6541556Srgrimes argv = stalloc(sizeof (char *) * (argc + 1)); 6551556Srgrimes 6561556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6571556Srgrimes TRACE(("evalcommand arg: %s\n", sp->text)); 6581556Srgrimes *argv++ = sp->text; 6591556Srgrimes } 6601556Srgrimes *argv = NULL; 6611556Srgrimes lastarg = NULL; 6621556Srgrimes if (iflag && funcnest == 0 && argc > 0) 6631556Srgrimes lastarg = argv[-1]; 6641556Srgrimes argv -= argc; 6651556Srgrimes 6661556Srgrimes /* Print the command if xflag is set. */ 6671556Srgrimes if (xflag) { 6681556Srgrimes outc('+', &errout); 6691556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) { 6701556Srgrimes outc(' ', &errout); 6711556Srgrimes out2str(sp->text); 6721556Srgrimes } 6731556Srgrimes for (sp = arglist.list ; sp ; sp = sp->next) { 6741556Srgrimes outc(' ', &errout); 6751556Srgrimes out2str(sp->text); 6761556Srgrimes } 6771556Srgrimes outc('\n', &errout); 6781556Srgrimes flushout(&errout); 6791556Srgrimes } 6801556Srgrimes 6811556Srgrimes /* Now locate the command. */ 6821556Srgrimes if (argc == 0) { 6831556Srgrimes cmdentry.cmdtype = CMDBUILTIN; 6841556Srgrimes cmdentry.u.index = BLTINCMD; 6851556Srgrimes } else { 68617987Speter static const char PATH[] = "PATH="; 68717987Speter char *path = pathval(); 68817987Speter 68917987Speter /* 69017987Speter * Modify the command lookup path, if a PATH= assignment 69117987Speter * is present 69217987Speter */ 69317987Speter for (sp = varlist.list ; sp ; sp = sp->next) 69454884Scracauer if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) { 69517987Speter path = sp->text + sizeof(PATH) - 1; 69654884Scracauer /* 69754884Scracauer * On `PATH=... command`, we need to make 69854884Scracauer * sure that the command isn't using the 69954884Scracauer * non-updated hash table of the outer PATH 70054884Scracauer * setting and we need to make sure that 70154884Scracauer * the hash table isn't filled with items 70254884Scracauer * from the temporary setting. 70354884Scracauer * 70454884Scracauer * It would be better to forbit using and 70554884Scracauer * updating the table while this command 70654884Scracauer * runs, by the command finding mechanism 70754884Scracauer * is heavily integrated with hash handling, 70854884Scracauer * so we just delete the hash before and after 70954884Scracauer * the command runs. Partly deleting like 71054884Scracauer * changepatch() does doesn't seem worth the 71154884Scracauer * bookinging effort, since most such runs add 71254884Scracauer * diretories in front of the new PATH. 71354884Scracauer */ 71454884Scracauer clearcmdentry(0); 71554884Scracauer do_clearcmdentry = 1; 71654884Scracauer } 71717987Speter 71817987Speter find_command(argv[0], &cmdentry, 1, path); 7191556Srgrimes if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */ 72020425Ssteve exitstatus = 127; 7211556Srgrimes flushout(&errout); 7221556Srgrimes return; 7231556Srgrimes } 7241556Srgrimes /* implement the bltin builtin here */ 7251556Srgrimes if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) { 7261556Srgrimes for (;;) { 7271556Srgrimes argv++; 7281556Srgrimes if (--argc == 0) 7291556Srgrimes break; 7301556Srgrimes if ((cmdentry.u.index = find_builtin(*argv)) < 0) { 7311556Srgrimes outfmt(&errout, "%s: not found\n", *argv); 73220425Ssteve exitstatus = 127; 7331556Srgrimes flushout(&errout); 7341556Srgrimes return; 7351556Srgrimes } 7361556Srgrimes if (cmdentry.u.index != BLTINCMD) 7371556Srgrimes break; 7381556Srgrimes } 7391556Srgrimes } 7401556Srgrimes } 7411556Srgrimes 7421556Srgrimes /* Fork off a child process if necessary. */ 7431556Srgrimes if (cmd->ncmd.backgnd 74445221Scracauer || (cmdentry.cmdtype == CMDNORMAL 74545221Scracauer && ((flags & EV_EXIT) == 0 || Tflag)) 74617987Speter || ((flags & EV_BACKCMD) != 0 7471556Srgrimes && (cmdentry.cmdtype != CMDBUILTIN 74848896Ssheldonh || cmdentry.u.index == CDCMD 7491556Srgrimes || cmdentry.u.index == DOTCMD 75017987Speter || cmdentry.u.index == EVALCMD))) { 7511556Srgrimes jp = makejob(cmd, 1); 7521556Srgrimes mode = cmd->ncmd.backgnd; 7531556Srgrimes if (flags & EV_BACKCMD) { 7541556Srgrimes mode = FORK_NOJOB; 7551556Srgrimes if (pipe(pip) < 0) 75653891Scracauer error("Pipe call failed: %s", strerror(errno)); 7571556Srgrimes } 7581556Srgrimes if (forkshell(jp, cmd, mode) != 0) 7591556Srgrimes goto parent; /* at end of routine */ 7601556Srgrimes if (flags & EV_BACKCMD) { 7611556Srgrimes FORCEINTON; 7621556Srgrimes close(pip[0]); 7631556Srgrimes if (pip[1] != 1) { 7641556Srgrimes close(1); 7651556Srgrimes copyfd(pip[1], 1); 7661556Srgrimes close(pip[1]); 7671556Srgrimes } 7681556Srgrimes } 7691556Srgrimes flags |= EV_EXIT; 7701556Srgrimes } 7711556Srgrimes 7721556Srgrimes /* This is the child process if a fork occurred. */ 7731556Srgrimes /* Execute the command. */ 7741556Srgrimes if (cmdentry.cmdtype == CMDFUNCTION) { 77520425Ssteve#ifdef DEBUG 7761556Srgrimes trputs("Shell function: "); trargs(argv); 77720425Ssteve#endif 7781556Srgrimes redirect(cmd->ncmd.redirect, REDIR_PUSH); 7791556Srgrimes saveparam = shellparam; 7801556Srgrimes shellparam.malloc = 0; 78120425Ssteve shellparam.reset = 1; 7821556Srgrimes shellparam.nparam = argc - 1; 7831556Srgrimes shellparam.p = argv + 1; 7841556Srgrimes shellparam.optnext = NULL; 7851556Srgrimes INTOFF; 7861556Srgrimes savelocalvars = localvars; 7871556Srgrimes localvars = NULL; 7881556Srgrimes INTON; 7891556Srgrimes if (setjmp(jmploc.loc)) { 7901556Srgrimes if (exception == EXSHELLPROC) 7911556Srgrimes freeparam((struct shparam *)&saveparam); 7921556Srgrimes else { 7931556Srgrimes freeparam(&shellparam); 7941556Srgrimes shellparam = saveparam; 7951556Srgrimes } 7961556Srgrimes poplocalvars(); 7971556Srgrimes localvars = savelocalvars; 7981556Srgrimes handler = savehandler; 7991556Srgrimes longjmp(handler->loc, 1); 8001556Srgrimes } 8011556Srgrimes savehandler = handler; 8021556Srgrimes handler = &jmploc; 8031556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8041556Srgrimes mklocal(sp->text); 8051556Srgrimes funcnest++; 80635675Scracauer if (flags & EV_TESTED) 80735675Scracauer evaltree(cmdentry.u.func, EV_TESTED); 80835675Scracauer else 80935675Scracauer evaltree(cmdentry.u.func, 0); 8101556Srgrimes funcnest--; 8111556Srgrimes INTOFF; 8121556Srgrimes poplocalvars(); 8131556Srgrimes localvars = savelocalvars; 8141556Srgrimes freeparam(&shellparam); 8151556Srgrimes shellparam = saveparam; 8161556Srgrimes handler = savehandler; 8171556Srgrimes popredir(); 8181556Srgrimes INTON; 8191556Srgrimes if (evalskip == SKIPFUNC) { 8201556Srgrimes evalskip = 0; 8211556Srgrimes skipcount = 0; 8221556Srgrimes } 8231556Srgrimes if (flags & EV_EXIT) 8241556Srgrimes exitshell(exitstatus); 8251556Srgrimes } else if (cmdentry.cmdtype == CMDBUILTIN) { 82620425Ssteve#ifdef DEBUG 8271556Srgrimes trputs("builtin command: "); trargs(argv); 82820425Ssteve#endif 8291556Srgrimes mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; 8301556Srgrimes if (flags == EV_BACKCMD) { 8311556Srgrimes memout.nleft = 0; 8321556Srgrimes memout.nextc = memout.buf; 8331556Srgrimes memout.bufsize = 64; 8341556Srgrimes mode |= REDIR_BACKQ; 8351556Srgrimes } 8361556Srgrimes redirect(cmd->ncmd.redirect, mode); 8371556Srgrimes savecmdname = commandname; 8381556Srgrimes cmdenviron = varlist.list; 8391556Srgrimes e = -1; 8401556Srgrimes if (setjmp(jmploc.loc)) { 8411556Srgrimes e = exception; 8421556Srgrimes exitstatus = (e == EXINT)? SIGINT+128 : 2; 8431556Srgrimes goto cmddone; 8441556Srgrimes } 8451556Srgrimes savehandler = handler; 8461556Srgrimes handler = &jmploc; 8471556Srgrimes commandname = argv[0]; 8481556Srgrimes argptr = argv + 1; 8491556Srgrimes optptr = NULL; /* initialize nextopt */ 8501556Srgrimes exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv); 8511556Srgrimes flushall(); 8521556Srgrimescmddone: 85360592Scracauer cmdenviron = NULL; 8541556Srgrimes out1 = &output; 8551556Srgrimes out2 = &errout; 8561556Srgrimes freestdout(); 8571556Srgrimes if (e != EXSHELLPROC) { 8581556Srgrimes commandname = savecmdname; 8591556Srgrimes if (flags & EV_EXIT) { 8601556Srgrimes exitshell(exitstatus); 8611556Srgrimes } 8621556Srgrimes } 8631556Srgrimes handler = savehandler; 8641556Srgrimes if (e != -1) { 86520425Ssteve if ((e != EXERROR && e != EXEXEC) 86620425Ssteve || cmdentry.u.index == BLTINCMD 86720425Ssteve || cmdentry.u.index == DOTCMD 86820425Ssteve || cmdentry.u.index == EVALCMD 86917987Speter#ifndef NO_HISTORY 87020425Ssteve || cmdentry.u.index == HISTCMD 87117987Speter#endif 87220425Ssteve || cmdentry.u.index == EXECCMD) 8731556Srgrimes exraise(e); 8741556Srgrimes FORCEINTON; 8751556Srgrimes } 8761556Srgrimes if (cmdentry.u.index != EXECCMD) 8771556Srgrimes popredir(); 8781556Srgrimes if (flags == EV_BACKCMD) { 8791556Srgrimes backcmd->buf = memout.buf; 8801556Srgrimes backcmd->nleft = memout.nextc - memout.buf; 8811556Srgrimes memout.buf = NULL; 8821556Srgrimes } 8831556Srgrimes } else { 88420425Ssteve#ifdef DEBUG 8851556Srgrimes trputs("normal command: "); trargs(argv); 88620425Ssteve#endif 8871556Srgrimes clearredir(); 8881556Srgrimes redirect(cmd->ncmd.redirect, 0); 8891556Srgrimes for (sp = varlist.list ; sp ; sp = sp->next) 8901556Srgrimes setvareq(sp->text, VEXPORT|VSTACK); 8911556Srgrimes envp = environment(); 89217987Speter shellexec(argv, envp, pathval(), cmdentry.u.index); 8931556Srgrimes /*NOTREACHED*/ 8941556Srgrimes } 8951556Srgrimes goto out; 8961556Srgrimes 8971556Srgrimesparent: /* parent process gets here (if we forked) */ 8981556Srgrimes if (mode == 0) { /* argument to fork */ 8991556Srgrimes INTOFF; 90045916Scracauer exitstatus = waitforjob(jp, &realstatus); 9011556Srgrimes INTON; 90245916Scracauer if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) { 90345266Scracauer evalskip = SKIPBREAK; 90445266Scracauer skipcount = loopnest; 90545266Scracauer } 9061556Srgrimes } else if (mode == 2) { 9071556Srgrimes backcmd->fd = pip[0]; 9081556Srgrimes close(pip[1]); 9091556Srgrimes backcmd->jp = jp; 9101556Srgrimes } 9111556Srgrimes 9121556Srgrimesout: 9131556Srgrimes if (lastarg) 9141556Srgrimes setvar("_", lastarg, 0); 91554884Scracauer if (do_clearcmdentry) 91654884Scracauer clearcmdentry(0); 9171556Srgrimes popstackmark(&smark); 9181556Srgrimes} 9191556Srgrimes 9201556Srgrimes 9211556Srgrimes 9221556Srgrimes/* 9231556Srgrimes * Search for a command. This is called before we fork so that the 9241556Srgrimes * location of the command will be available in the parent as well as 9251556Srgrimes * the child. The check for "goodname" is an overly conservative 9261556Srgrimes * check that the name will not be subject to expansion. 9271556Srgrimes */ 9281556Srgrimes 9291556SrgrimesSTATIC void 93090111Simpprehash(union node *n) 93117987Speter{ 9321556Srgrimes struct cmdentry entry; 9331556Srgrimes 93417987Speter if (n->type == NCMD && n->ncmd.args) 93517987Speter if (goodname(n->ncmd.args->narg.text)) 93617987Speter find_command(n->ncmd.args->narg.text, &entry, 0, 93717987Speter pathval()); 9381556Srgrimes} 9391556Srgrimes 9401556Srgrimes 9411556Srgrimes 9421556Srgrimes/* 9431556Srgrimes * Builtin commands. Builtin commands whose functions are closely 9441556Srgrimes * tied to evaluation are implemented here. 9451556Srgrimes */ 9461556Srgrimes 9471556Srgrimes/* 9481556Srgrimes * No command given, or a bltin command with no arguments. Set the 9491556Srgrimes * specified variables. 9501556Srgrimes */ 9511556Srgrimes 95217987Speterint 95390111Simpbltincmd(int argc __unused, char **argv __unused) 95417987Speter{ 9551556Srgrimes listsetvar(cmdenviron); 95620425Ssteve /* 95717987Speter * Preserve exitstatus of a previous possible redirection 95820425Ssteve * as POSIX mandates 95917987Speter */ 9601556Srgrimes return exitstatus; 9611556Srgrimes} 9621556Srgrimes 9631556Srgrimes 9641556Srgrimes/* 9651556Srgrimes * Handle break and continue commands. Break, continue, and return are 9661556Srgrimes * all handled by setting the evalskip flag. The evaluation routines 9671556Srgrimes * above all check this flag, and if it is set they start skipping 9681556Srgrimes * commands rather than executing them. The variable skipcount is 9691556Srgrimes * the number of loops to break/continue, or the number of function 9701556Srgrimes * levels to return. (The latter is always 1.) It should probably 9711556Srgrimes * be an error to break out of more loops than exist, but it isn't 9721556Srgrimes * in the standard shell so we don't make it one here. 9731556Srgrimes */ 9741556Srgrimes 97517987Speterint 97690111Simpbreakcmd(int argc, char **argv) 97717987Speter{ 97820425Ssteve int n = argc > 1 ? number(argv[1]) : 1; 9791556Srgrimes 9801556Srgrimes if (n > loopnest) 9811556Srgrimes n = loopnest; 9821556Srgrimes if (n > 0) { 9831556Srgrimes evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 9841556Srgrimes skipcount = n; 9851556Srgrimes } 9861556Srgrimes return 0; 9871556Srgrimes} 9881556Srgrimes 9891556Srgrimes 9901556Srgrimes/* 9911556Srgrimes * The return command. 9921556Srgrimes */ 9931556Srgrimes 99417987Speterint 99590111Simpreturncmd(int argc, char **argv) 99617987Speter{ 99720425Ssteve int ret = argc > 1 ? number(argv[1]) : oexitstatus; 9981556Srgrimes 9991556Srgrimes if (funcnest) { 10001556Srgrimes evalskip = SKIPFUNC; 10011556Srgrimes skipcount = 1; 100220425Ssteve } else { 100320425Ssteve /* skip the rest of the file */ 100420425Ssteve evalskip = SKIPFILE; 100520425Ssteve skipcount = 1; 10061556Srgrimes } 10071556Srgrimes return ret; 10081556Srgrimes} 10091556Srgrimes 10101556Srgrimes 101117987Speterint 101290111Simpfalsecmd(int argc __unused, char **argv __unused) 101317987Speter{ 101417987Speter return 1; 101517987Speter} 101617987Speter 101717987Speter 101817987Speterint 101990111Simptruecmd(int argc __unused, char **argv __unused) 102017987Speter{ 10211556Srgrimes return 0; 10221556Srgrimes} 10231556Srgrimes 10241556Srgrimes 102517987Speterint 102690111Simpexeccmd(int argc, char **argv) 102717987Speter{ 10281556Srgrimes if (argc > 1) { 102917987Speter struct strlist *sp; 103017987Speter 10311556Srgrimes iflag = 0; /* exit on error */ 10321556Srgrimes mflag = 0; 10331556Srgrimes optschanged(); 103417987Speter for (sp = cmdenviron; sp ; sp = sp->next) 103517987Speter setvareq(sp->text, VEXPORT|VSTACK); 10361556Srgrimes shellexec(argv + 1, environment(), pathval(), 0); 10371556Srgrimes 10381556Srgrimes } 10391556Srgrimes return 0; 10401556Srgrimes} 1041