Home | History | Annotate | Line # | Download | only in sh
eval.c revision 1.64
      1  1.64  christos /*	$NetBSD: eval.c,v 1.64 2002/09/27 22:56:24 christos Exp $	*/
      2  1.19       cgd 
      3   1.1       cgd /*-
      4   1.7       jtc  * Copyright (c) 1993
      5   1.7       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39  1.36  christos #include <sys/cdefs.h>
     40   1.1       cgd #ifndef lint
     41  1.19       cgd #if 0
     42  1.26  christos static char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
     43  1.19       cgd #else
     44  1.64  christos __RCSID("$NetBSD: eval.c,v 1.64 2002/09/27 22:56:24 christos Exp $");
     45  1.24       cgd #endif
     46   1.1       cgd #endif /* not lint */
     47   1.1       cgd 
     48  1.21  christos #include <signal.h>
     49  1.21  christos #include <unistd.h>
     50  1.53     elric 
     51  1.61  christos #include <sys/types.h>
     52  1.61  christos #include <sys/wait.h>
     53  1.61  christos 
     54   1.1       cgd /*
     55   1.1       cgd  * Evaluate a command.
     56   1.1       cgd  */
     57   1.1       cgd 
     58   1.1       cgd #include "shell.h"
     59   1.1       cgd #include "nodes.h"
     60   1.1       cgd #include "syntax.h"
     61   1.1       cgd #include "expand.h"
     62   1.1       cgd #include "parser.h"
     63   1.1       cgd #include "jobs.h"
     64   1.1       cgd #include "eval.h"
     65   1.1       cgd #include "builtins.h"
     66   1.1       cgd #include "options.h"
     67   1.1       cgd #include "exec.h"
     68   1.1       cgd #include "redir.h"
     69   1.1       cgd #include "input.h"
     70   1.1       cgd #include "output.h"
     71   1.1       cgd #include "trap.h"
     72   1.1       cgd #include "var.h"
     73   1.1       cgd #include "memalloc.h"
     74   1.1       cgd #include "error.h"
     75  1.21  christos #include "show.h"
     76   1.1       cgd #include "mystring.h"
     77  1.64  christos #include "main.h"
     78  1.35  christos #ifndef SMALL
     79   1.7       jtc #include "myhistedit.h"
     80  1.11       cgd #endif
     81   1.1       cgd 
     82   1.1       cgd 
     83   1.1       cgd /* flags in argument to evaltree */
     84   1.1       cgd #define EV_EXIT 01		/* exit after evaluating tree */
     85   1.1       cgd #define EV_TESTED 02		/* exit status is checked; ignore -e flag */
     86   1.1       cgd #define EV_BACKCMD 04		/* command executing within back quotes */
     87   1.1       cgd 
     88  1.57  christos int evalskip;			/* set if we are skipping commands */
     89   1.1       cgd STATIC int skipcount;		/* number of levels to skip */
     90   1.1       cgd MKINIT int loopnest;		/* current loop nesting level */
     91   1.1       cgd int funcnest;			/* depth of function calls */
     92   1.1       cgd 
     93   1.1       cgd 
     94   1.1       cgd char *commandname;
     95   1.1       cgd struct strlist *cmdenviron;
     96   1.1       cgd int exitstatus;			/* exit status of last command */
     97  1.23  christos int oexitstatus;		/* saved exit status */
     98   1.1       cgd 
     99   1.1       cgd 
    100  1.64  christos STATIC void evalloop __P((union node *, int, int *));
    101  1.64  christos STATIC void evalfor __P((union node *, int, int *));
    102  1.64  christos STATIC void evalcase __P((union node *, int, int *));
    103  1.64  christos STATIC void evalsubshell __P((union node *, int, int *));
    104  1.21  christos STATIC void expredir __P((union node *));
    105  1.64  christos STATIC void evalpipe __P((union node *, int *));
    106  1.64  christos STATIC void evalcommand __P((union node *, int, struct backcmd *, int *));
    107  1.21  christos STATIC void prehash __P((union node *));
    108   1.1       cgd 
    109   1.1       cgd 
    110   1.1       cgd /*
    111   1.1       cgd  * Called to reset things after an exception.
    112   1.1       cgd  */
    113   1.1       cgd 
    114   1.1       cgd #ifdef mkinit
    115   1.1       cgd INCLUDE "eval.h"
    116   1.1       cgd 
    117   1.1       cgd RESET {
    118   1.1       cgd 	evalskip = 0;
    119   1.1       cgd 	loopnest = 0;
    120   1.1       cgd 	funcnest = 0;
    121   1.1       cgd }
    122   1.1       cgd 
    123   1.1       cgd SHELLPROC {
    124   1.1       cgd 	exitstatus = 0;
    125   1.1       cgd }
    126   1.1       cgd #endif
    127   1.1       cgd 
    128   1.1       cgd 
    129   1.1       cgd 
    130   1.1       cgd /*
    131   1.1       cgd  * The eval commmand.
    132   1.1       cgd  */
    133   1.1       cgd 
    134  1.16       cgd int
    135  1.31  christos evalcmd(argc, argv)
    136  1.16       cgd 	int argc;
    137  1.31  christos 	char **argv;
    138   1.1       cgd {
    139   1.1       cgd         char *p;
    140   1.1       cgd         char *concat;
    141   1.1       cgd         char **ap;
    142   1.1       cgd 
    143   1.1       cgd         if (argc > 1) {
    144   1.1       cgd                 p = argv[1];
    145   1.1       cgd                 if (argc > 2) {
    146   1.1       cgd                         STARTSTACKSTR(concat);
    147   1.1       cgd                         ap = argv + 2;
    148   1.1       cgd                         for (;;) {
    149   1.1       cgd                                 while (*p)
    150   1.1       cgd                                         STPUTC(*p++, concat);
    151   1.1       cgd                                 if ((p = *ap++) == NULL)
    152   1.1       cgd                                         break;
    153   1.1       cgd                                 STPUTC(' ', concat);
    154   1.1       cgd                         }
    155   1.1       cgd                         STPUTC('\0', concat);
    156   1.1       cgd                         p = grabstackstr(concat);
    157   1.1       cgd                 }
    158  1.50  christos                 evalstring(p, EV_TESTED);
    159   1.1       cgd         }
    160   1.1       cgd         return exitstatus;
    161   1.1       cgd }
    162   1.1       cgd 
    163   1.1       cgd 
    164   1.1       cgd /*
    165   1.1       cgd  * Execute a command or commands contained in a string.
    166   1.1       cgd  */
    167   1.1       cgd 
    168   1.1       cgd void
    169  1.50  christos evalstring(s, flag)
    170   1.1       cgd 	char *s;
    171  1.50  christos 	int flag;
    172   1.1       cgd 	{
    173   1.1       cgd 	union node *n;
    174   1.1       cgd 	struct stackmark smark;
    175  1.64  christos 	int isroot;
    176   1.1       cgd 
    177   1.1       cgd 	setstackmark(&smark);
    178   1.1       cgd 	setinputstring(s, 1);
    179  1.64  christos 
    180   1.1       cgd 	while ((n = parsecmd(0)) != NEOF) {
    181  1.64  christos 		isroot = rootshell;
    182  1.64  christos 		evaltree(n, flag, &isroot);
    183   1.1       cgd 		popstackmark(&smark);
    184   1.1       cgd 	}
    185   1.1       cgd 	popfile();
    186   1.1       cgd 	popstackmark(&smark);
    187   1.1       cgd }
    188   1.1       cgd 
    189   1.1       cgd 
    190   1.1       cgd 
    191   1.1       cgd /*
    192   1.1       cgd  * Evaluate a parse tree.  The value is left in the global variable
    193   1.1       cgd  * exitstatus.
    194   1.1       cgd  */
    195   1.1       cgd 
    196   1.1       cgd void
    197  1.64  christos evaltree(n, flags, isroot)
    198   1.1       cgd 	union node *n;
    199  1.17       cgd 	int flags;
    200  1.64  christos 	int *isroot;
    201  1.17       cgd {
    202   1.1       cgd 	if (n == NULL) {
    203   1.1       cgd 		TRACE(("evaltree(NULL) called\n"));
    204   1.7       jtc 		exitstatus = 0;
    205   1.7       jtc 		goto out;
    206   1.1       cgd 	}
    207  1.35  christos #ifndef SMALL
    208   1.7       jtc 	displayhist = 1;	/* show history substitutions done with fc */
    209  1.10       cgd #endif
    210  1.64  christos 	TRACE(("evaltree(0x%lx: %d, %d) called\n", (long)n, n->type, *isroot));
    211   1.1       cgd 	switch (n->type) {
    212   1.1       cgd 	case NSEMI:
    213  1.64  christos 		evaltree(n->nbinary.ch1, flags & EV_TESTED, isroot);
    214   1.1       cgd 		if (evalskip)
    215   1.1       cgd 			goto out;
    216  1.64  christos 		evaltree(n->nbinary.ch2, flags, isroot);
    217   1.1       cgd 		break;
    218   1.1       cgd 	case NAND:
    219  1.64  christos 		evaltree(n->nbinary.ch1, EV_TESTED, isroot);
    220  1.31  christos 		if (evalskip || exitstatus != 0) {
    221  1.31  christos 			/* don't bomb out on "set -e; false && true" */
    222  1.31  christos 			flags |= EV_TESTED;
    223   1.1       cgd 			goto out;
    224  1.31  christos 		}
    225  1.64  christos 		evaltree(n->nbinary.ch2, flags | EV_TESTED, isroot);
    226   1.1       cgd 		break;
    227   1.1       cgd 	case NOR:
    228  1.64  christos 		evaltree(n->nbinary.ch1, EV_TESTED, isroot);
    229   1.1       cgd 		if (evalskip || exitstatus == 0)
    230   1.1       cgd 			goto out;
    231  1.64  christos 		evaltree(n->nbinary.ch2, flags | EV_TESTED, isroot);
    232   1.1       cgd 		break;
    233   1.1       cgd 	case NREDIR:
    234   1.1       cgd 		expredir(n->nredir.redirect);
    235   1.1       cgd 		redirect(n->nredir.redirect, REDIR_PUSH);
    236  1.64  christos 		evaltree(n->nredir.n, flags, isroot);
    237   1.1       cgd 		popredir();
    238   1.1       cgd 		break;
    239   1.1       cgd 	case NSUBSHELL:
    240  1.64  christos 		evalsubshell(n, flags, isroot);
    241   1.1       cgd 		break;
    242   1.1       cgd 	case NBACKGND:
    243  1.64  christos 		evalsubshell(n, flags, isroot);
    244   1.1       cgd 		break;
    245   1.1       cgd 	case NIF: {
    246  1.64  christos 		evaltree(n->nif.test, EV_TESTED, isroot);
    247   1.1       cgd 		if (evalskip)
    248   1.1       cgd 			goto out;
    249  1.28  christos 		if (exitstatus == 0)
    250  1.64  christos 			evaltree(n->nif.ifpart, flags, isroot);
    251  1.22  christos 		else if (n->nif.elsepart)
    252  1.64  christos 			evaltree(n->nif.elsepart, flags, isroot);
    253  1.29        pk 		else
    254  1.29        pk 			exitstatus = 0;
    255   1.1       cgd 		break;
    256   1.1       cgd 	}
    257   1.1       cgd 	case NWHILE:
    258   1.1       cgd 	case NUNTIL:
    259  1.64  christos 		evalloop(n, flags, isroot);
    260   1.1       cgd 		break;
    261   1.1       cgd 	case NFOR:
    262  1.64  christos 		evalfor(n, flags, isroot);
    263   1.1       cgd 		break;
    264   1.1       cgd 	case NCASE:
    265  1.64  christos 		evalcase(n, flags, isroot);
    266   1.1       cgd 		break;
    267   1.1       cgd 	case NDEFUN:
    268   1.1       cgd 		defun(n->narg.text, n->narg.next);
    269   1.1       cgd 		exitstatus = 0;
    270   1.1       cgd 		break;
    271   1.7       jtc 	case NNOT:
    272  1.64  christos 		evaltree(n->nnot.com, EV_TESTED, isroot);
    273   1.7       jtc 		exitstatus = !exitstatus;
    274   1.7       jtc 		break;
    275   1.7       jtc 
    276   1.1       cgd 	case NPIPE:
    277  1.64  christos 		evalpipe(n, isroot);
    278   1.1       cgd 		break;
    279   1.1       cgd 	case NCMD:
    280  1.64  christos 		evalcommand(n, flags, (struct backcmd *)NULL, isroot);
    281   1.1       cgd 		break;
    282   1.1       cgd 	default:
    283   1.1       cgd 		out1fmt("Node type = %d\n", n->type);
    284   1.1       cgd 		flushout(&output);
    285   1.1       cgd 		break;
    286   1.1       cgd 	}
    287   1.1       cgd out:
    288   1.1       cgd 	if (pendingsigs)
    289   1.1       cgd 		dotrap();
    290  1.58  christos 	if ((flags & EV_EXIT) != 0)
    291   1.1       cgd 		exitshell(exitstatus);
    292   1.1       cgd }
    293   1.1       cgd 
    294   1.1       cgd 
    295   1.1       cgd STATIC void
    296  1.64  christos evalloop(n, flags, isroot)
    297   1.1       cgd 	union node *n;
    298  1.50  christos 	int flags;
    299  1.64  christos 	int *isroot;
    300  1.22  christos {
    301   1.1       cgd 	int status;
    302   1.1       cgd 
    303   1.1       cgd 	loopnest++;
    304   1.1       cgd 	status = 0;
    305   1.1       cgd 	for (;;) {
    306  1.64  christos 		evaltree(n->nbinary.ch1, EV_TESTED, isroot);
    307   1.1       cgd 		if (evalskip) {
    308   1.1       cgd skipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
    309   1.1       cgd 				evalskip = 0;
    310   1.1       cgd 				continue;
    311   1.1       cgd 			}
    312   1.1       cgd 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    313   1.1       cgd 				evalskip = 0;
    314   1.1       cgd 			break;
    315   1.1       cgd 		}
    316   1.1       cgd 		if (n->type == NWHILE) {
    317   1.1       cgd 			if (exitstatus != 0)
    318   1.1       cgd 				break;
    319   1.1       cgd 		} else {
    320   1.1       cgd 			if (exitstatus == 0)
    321   1.1       cgd 				break;
    322   1.1       cgd 		}
    323  1.64  christos 		evaltree(n->nbinary.ch2, flags & EV_TESTED, isroot);
    324   1.1       cgd 		status = exitstatus;
    325   1.1       cgd 		if (evalskip)
    326   1.1       cgd 			goto skipping;
    327   1.1       cgd 	}
    328   1.1       cgd 	loopnest--;
    329   1.1       cgd 	exitstatus = status;
    330   1.1       cgd }
    331   1.1       cgd 
    332   1.1       cgd 
    333   1.1       cgd 
    334   1.1       cgd STATIC void
    335  1.64  christos evalfor(n, flags, isroot)
    336  1.22  christos     union node *n;
    337  1.50  christos     int flags;
    338  1.64  christos     int *isroot;
    339  1.22  christos {
    340   1.1       cgd 	struct arglist arglist;
    341   1.1       cgd 	union node *argp;
    342   1.1       cgd 	struct strlist *sp;
    343   1.1       cgd 	struct stackmark smark;
    344   1.1       cgd 
    345   1.1       cgd 	setstackmark(&smark);
    346   1.1       cgd 	arglist.lastp = &arglist.list;
    347   1.1       cgd 	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
    348  1.23  christos 		oexitstatus = exitstatus;
    349  1.42  christos 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD);
    350   1.1       cgd 		if (evalskip)
    351   1.1       cgd 			goto out;
    352   1.1       cgd 	}
    353   1.1       cgd 	*arglist.lastp = NULL;
    354   1.1       cgd 
    355   1.1       cgd 	exitstatus = 0;
    356   1.1       cgd 	loopnest++;
    357   1.1       cgd 	for (sp = arglist.list ; sp ; sp = sp->next) {
    358   1.1       cgd 		setvar(n->nfor.var, sp->text, 0);
    359  1.64  christos 		evaltree(n->nfor.body, flags & EV_TESTED, isroot);
    360   1.1       cgd 		if (evalskip) {
    361   1.1       cgd 			if (evalskip == SKIPCONT && --skipcount <= 0) {
    362   1.1       cgd 				evalskip = 0;
    363   1.1       cgd 				continue;
    364   1.1       cgd 			}
    365   1.1       cgd 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    366   1.1       cgd 				evalskip = 0;
    367   1.1       cgd 			break;
    368   1.1       cgd 		}
    369   1.1       cgd 	}
    370   1.1       cgd 	loopnest--;
    371   1.1       cgd out:
    372   1.1       cgd 	popstackmark(&smark);
    373   1.1       cgd }
    374   1.1       cgd 
    375   1.1       cgd 
    376   1.1       cgd 
    377   1.1       cgd STATIC void
    378  1.64  christos evalcase(n, flags, isroot)
    379   1.1       cgd 	union node *n;
    380  1.16       cgd 	int flags;
    381  1.64  christos 	int *isroot;
    382  1.16       cgd {
    383   1.1       cgd 	union node *cp;
    384   1.1       cgd 	union node *patp;
    385   1.1       cgd 	struct arglist arglist;
    386   1.1       cgd 	struct stackmark smark;
    387   1.1       cgd 
    388   1.1       cgd 	setstackmark(&smark);
    389   1.1       cgd 	arglist.lastp = &arglist.list;
    390  1.23  christos 	oexitstatus = exitstatus;
    391   1.7       jtc 	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
    392   1.1       cgd 	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
    393   1.1       cgd 		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
    394   1.1       cgd 			if (casematch(patp, arglist.list->text)) {
    395   1.1       cgd 				if (evalskip == 0) {
    396  1.64  christos 					evaltree(cp->nclist.body, flags, isroot);
    397   1.1       cgd 				}
    398   1.1       cgd 				goto out;
    399   1.1       cgd 			}
    400   1.1       cgd 		}
    401   1.1       cgd 	}
    402   1.1       cgd out:
    403   1.1       cgd 	popstackmark(&smark);
    404   1.1       cgd }
    405   1.1       cgd 
    406   1.1       cgd 
    407   1.1       cgd 
    408   1.1       cgd /*
    409   1.1       cgd  * Kick off a subshell to evaluate a tree.
    410   1.1       cgd  */
    411   1.1       cgd 
    412   1.1       cgd STATIC void
    413  1.64  christos evalsubshell(n, flags, isroot)
    414   1.1       cgd 	union node *n;
    415  1.16       cgd 	int flags;
    416  1.64  christos 	int *isroot;
    417  1.16       cgd {
    418   1.1       cgd 	struct job *jp;
    419   1.1       cgd 	int backgnd = (n->type == NBACKGND);
    420   1.1       cgd 
    421   1.1       cgd 	expredir(n->nredir.redirect);
    422  1.63   mycroft 	INTOFF;
    423   1.1       cgd 	jp = makejob(n, 1);
    424  1.64  christos 	if (forkshell(jp, n, backgnd, isroot) == 0) {
    425  1.63   mycroft 		INTON;
    426   1.1       cgd 		if (backgnd)
    427   1.1       cgd 			flags &=~ EV_TESTED;
    428   1.1       cgd 		redirect(n->nredir.redirect, 0);
    429  1.64  christos 		/* never returns */
    430  1.64  christos 		evaltree(n->nredir.n, flags | EV_EXIT, isroot);
    431   1.1       cgd 	}
    432  1.63   mycroft 	if (! backgnd)
    433   1.1       cgd 		exitstatus = waitforjob(jp);
    434  1.63   mycroft 	INTON;
    435   1.1       cgd }
    436   1.1       cgd 
    437   1.1       cgd 
    438   1.1       cgd 
    439   1.1       cgd /*
    440   1.1       cgd  * Compute the names of the files in a redirection list.
    441   1.1       cgd  */
    442   1.1       cgd 
    443   1.1       cgd STATIC void
    444   1.1       cgd expredir(n)
    445   1.1       cgd 	union node *n;
    446  1.22  christos {
    447  1.34       tls 	union node *redir;
    448   1.1       cgd 
    449   1.1       cgd 	for (redir = n ; redir ; redir = redir->nfile.next) {
    450  1.14       jtc 		struct arglist fn;
    451  1.14       jtc 		fn.lastp = &fn.list;
    452  1.23  christos 		oexitstatus = exitstatus;
    453  1.14       jtc 		switch (redir->type) {
    454  1.45  christos 		case NFROMTO:
    455  1.14       jtc 		case NFROM:
    456  1.14       jtc 		case NTO:
    457  1.59  christos 		case NCLOBBER:
    458  1.14       jtc 		case NAPPEND:
    459   1.7       jtc 			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
    460   1.1       cgd 			redir->nfile.expfname = fn.list->text;
    461  1.14       jtc 			break;
    462  1.14       jtc 		case NFROMFD:
    463  1.14       jtc 		case NTOFD:
    464  1.14       jtc 			if (redir->ndup.vname) {
    465  1.14       jtc 				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
    466  1.14       jtc 				fixredir(redir, fn.list->text, 1);
    467  1.14       jtc 			}
    468  1.14       jtc 			break;
    469   1.1       cgd 		}
    470   1.1       cgd 	}
    471   1.1       cgd }
    472   1.1       cgd 
    473   1.1       cgd 
    474   1.1       cgd 
    475   1.1       cgd /*
    476   1.1       cgd  * Evaluate a pipeline.  All the processes in the pipeline are children
    477   1.1       cgd  * of the process creating the pipeline.  (This differs from some versions
    478   1.1       cgd  * of the shell, which make the last process in a pipeline the parent
    479   1.1       cgd  * of all the rest.)
    480   1.1       cgd  */
    481   1.1       cgd 
    482   1.1       cgd STATIC void
    483  1.64  christos evalpipe(n, isroot)
    484   1.1       cgd 	union node *n;
    485  1.64  christos 	int *isroot;
    486  1.22  christos {
    487   1.1       cgd 	struct job *jp;
    488   1.1       cgd 	struct nodelist *lp;
    489   1.1       cgd 	int pipelen;
    490   1.1       cgd 	int prevfd;
    491   1.1       cgd 	int pip[2];
    492   1.1       cgd 
    493  1.18       cgd 	TRACE(("evalpipe(0x%lx) called\n", (long)n));
    494   1.1       cgd 	pipelen = 0;
    495   1.1       cgd 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
    496   1.1       cgd 		pipelen++;
    497   1.1       cgd 	INTOFF;
    498   1.1       cgd 	jp = makejob(n, pipelen);
    499   1.1       cgd 	prevfd = -1;
    500   1.1       cgd 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
    501   1.1       cgd 		prehash(lp->n);
    502   1.1       cgd 		pip[1] = -1;
    503   1.1       cgd 		if (lp->next) {
    504   1.1       cgd 			if (pipe(pip) < 0) {
    505   1.1       cgd 				close(prevfd);
    506   1.1       cgd 				error("Pipe call failed");
    507   1.1       cgd 			}
    508   1.1       cgd 		}
    509  1.64  christos 		if (forkshell(jp, lp->n, n->npipe.backgnd, isroot) == 0) {
    510   1.1       cgd 			INTON;
    511   1.1       cgd 			if (prevfd > 0) {
    512   1.1       cgd 				close(0);
    513   1.1       cgd 				copyfd(prevfd, 0);
    514   1.1       cgd 				close(prevfd);
    515   1.1       cgd 			}
    516   1.1       cgd 			if (pip[1] >= 0) {
    517   1.1       cgd 				close(pip[0]);
    518   1.1       cgd 				if (pip[1] != 1) {
    519   1.1       cgd 					close(1);
    520   1.1       cgd 					copyfd(pip[1], 1);
    521   1.1       cgd 					close(pip[1]);
    522   1.1       cgd 				}
    523   1.1       cgd 			}
    524  1.64  christos 			evaltree(lp->n, EV_EXIT, isroot);
    525   1.1       cgd 		}
    526   1.1       cgd 		if (prevfd >= 0)
    527   1.1       cgd 			close(prevfd);
    528   1.1       cgd 		prevfd = pip[0];
    529   1.1       cgd 		close(pip[1]);
    530   1.1       cgd 	}
    531   1.1       cgd 	if (n->npipe.backgnd == 0) {
    532   1.1       cgd 		exitstatus = waitforjob(jp);
    533   1.1       cgd 		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
    534   1.1       cgd 	}
    535  1.60   mycroft 	INTON;
    536   1.1       cgd }
    537   1.1       cgd 
    538   1.1       cgd 
    539   1.1       cgd 
    540   1.1       cgd /*
    541   1.1       cgd  * Execute a command inside back quotes.  If it's a builtin command, we
    542   1.1       cgd  * want to save its output in a block obtained from malloc.  Otherwise
    543   1.1       cgd  * we fork off a subprocess and get the output of the command via a pipe.
    544   1.1       cgd  * Should be called with interrupts off.
    545   1.1       cgd  */
    546   1.1       cgd 
    547   1.1       cgd void
    548  1.64  christos evalbackcmd(n, result, isroot)
    549   1.1       cgd 	union node *n;
    550   1.1       cgd 	struct backcmd *result;
    551  1.64  christos 	int *isroot;
    552  1.22  christos {
    553   1.1       cgd 	int pip[2];
    554   1.1       cgd 	struct job *jp;
    555   1.1       cgd 	struct stackmark smark;		/* unnecessary */
    556   1.1       cgd 
    557   1.1       cgd 	setstackmark(&smark);
    558   1.1       cgd 	result->fd = -1;
    559   1.1       cgd 	result->buf = NULL;
    560   1.1       cgd 	result->nleft = 0;
    561   1.1       cgd 	result->jp = NULL;
    562  1.23  christos 	if (n == NULL) {
    563  1.23  christos 		exitstatus = 0;
    564   1.7       jtc 		goto out;
    565  1.23  christos 	}
    566  1.46  christos #ifdef notyet
    567  1.46  christos 	/*
    568  1.46  christos 	 * For now we disable executing builtins in the same
    569  1.46  christos 	 * context as the shell, because we are not keeping
    570  1.46  christos 	 * enough state to recover from changes that are
    571  1.46  christos 	 * supposed only to affect subshells. eg. echo "`cd /`"
    572  1.46  christos 	 */
    573   1.7       jtc 	if (n->type == NCMD) {
    574  1.23  christos 		exitstatus = oexitstatus;
    575  1.64  christos 		evalcommand(n, EV_BACKCMD, result, isroot);
    576  1.46  christos 	} else
    577  1.46  christos #endif
    578  1.46  christos 	{
    579  1.23  christos 		exitstatus = 0;
    580  1.63   mycroft 		INTOFF;
    581   1.1       cgd 		if (pipe(pip) < 0)
    582   1.1       cgd 			error("Pipe call failed");
    583   1.1       cgd 		jp = makejob(n, 1);
    584  1.64  christos 		if (forkshell(jp, n, FORK_NOJOB, isroot) == 0) {
    585   1.1       cgd 			FORCEINTON;
    586   1.1       cgd 			close(pip[0]);
    587   1.1       cgd 			if (pip[1] != 1) {
    588   1.1       cgd 				close(1);
    589   1.1       cgd 				copyfd(pip[1], 1);
    590   1.1       cgd 				close(pip[1]);
    591   1.1       cgd 			}
    592  1.50  christos 			eflag = 0;
    593  1.64  christos 			evaltree(n, EV_EXIT, isroot);
    594   1.1       cgd 		}
    595   1.1       cgd 		close(pip[1]);
    596   1.1       cgd 		result->fd = pip[0];
    597   1.1       cgd 		result->jp = jp;
    598  1.63   mycroft 		INTON;
    599   1.1       cgd 	}
    600   1.7       jtc out:
    601   1.1       cgd 	popstackmark(&smark);
    602   1.1       cgd 	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
    603   1.1       cgd 		result->fd, result->buf, result->nleft, result->jp));
    604   1.1       cgd }
    605   1.1       cgd 
    606   1.1       cgd 
    607  1.61  christos int vforked = 0;
    608   1.1       cgd 
    609   1.1       cgd /*
    610   1.1       cgd  * Execute a simple command.
    611   1.1       cgd  */
    612   1.1       cgd 
    613   1.1       cgd STATIC void
    614  1.64  christos evalcommand(cmd, flags, backcmd, isroot)
    615   1.1       cgd 	union node *cmd;
    616  1.16       cgd 	int flags;
    617   1.1       cgd 	struct backcmd *backcmd;
    618  1.64  christos 	int *isroot;
    619  1.16       cgd {
    620   1.1       cgd 	struct stackmark smark;
    621   1.1       cgd 	union node *argp;
    622   1.1       cgd 	struct arglist arglist;
    623   1.1       cgd 	struct arglist varlist;
    624   1.1       cgd 	char **argv;
    625   1.1       cgd 	int argc;
    626   1.1       cgd 	char **envp;
    627   1.1       cgd 	int varflag;
    628   1.1       cgd 	struct strlist *sp;
    629   1.1       cgd 	int mode;
    630   1.1       cgd 	int pip[2];
    631   1.1       cgd 	struct cmdentry cmdentry;
    632   1.1       cgd 	struct job *jp;
    633   1.1       cgd 	struct jmploc jmploc;
    634   1.1       cgd 	struct jmploc *volatile savehandler;
    635   1.1       cgd 	char *volatile savecmdname;
    636   1.1       cgd 	volatile struct shparam saveparam;
    637   1.1       cgd 	struct localvar *volatile savelocalvars;
    638   1.1       cgd 	volatile int e;
    639   1.1       cgd 	char *lastarg;
    640  1.21  christos #if __GNUC__
    641  1.21  christos 	/* Avoid longjmp clobbering */
    642  1.21  christos 	(void) &argv;
    643  1.21  christos 	(void) &argc;
    644  1.21  christos 	(void) &lastarg;
    645  1.21  christos 	(void) &flags;
    646  1.21  christos #endif
    647   1.1       cgd 
    648  1.61  christos 	vforked = 0;
    649   1.1       cgd 	/* First expand the arguments. */
    650  1.64  christos 	TRACE(("evalcommand(0x%lx, %d, %d) called\n", (long)cmd, flags,
    651  1.64  christos 	    *isroot));
    652   1.1       cgd 	setstackmark(&smark);
    653   1.1       cgd 	arglist.lastp = &arglist.list;
    654   1.1       cgd 	varlist.lastp = &varlist.list;
    655   1.1       cgd 	varflag = 1;
    656  1.23  christos 	oexitstatus = exitstatus;
    657  1.23  christos 	exitstatus = 0;
    658   1.1       cgd 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    659  1.20  christos 		char *p = argp->narg.text;
    660   1.1       cgd 		if (varflag && is_name(*p)) {
    661   1.1       cgd 			do {
    662   1.1       cgd 				p++;
    663   1.1       cgd 			} while (is_in_name(*p));
    664   1.1       cgd 			if (*p == '=') {
    665   1.7       jtc 				expandarg(argp, &varlist, EXP_VARTILDE);
    666   1.1       cgd 				continue;
    667   1.1       cgd 			}
    668   1.1       cgd 		}
    669   1.7       jtc 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    670   1.1       cgd 		varflag = 0;
    671   1.1       cgd 	}
    672   1.1       cgd 	*arglist.lastp = NULL;
    673   1.1       cgd 	*varlist.lastp = NULL;
    674   1.1       cgd 	expredir(cmd->ncmd.redirect);
    675   1.1       cgd 	argc = 0;
    676   1.1       cgd 	for (sp = arglist.list ; sp ; sp = sp->next)
    677   1.1       cgd 		argc++;
    678   1.1       cgd 	argv = stalloc(sizeof (char *) * (argc + 1));
    679   1.7       jtc 
    680   1.7       jtc 	for (sp = arglist.list ; sp ; sp = sp->next) {
    681   1.7       jtc 		TRACE(("evalcommand arg: %s\n", sp->text));
    682   1.1       cgd 		*argv++ = sp->text;
    683   1.7       jtc 	}
    684   1.1       cgd 	*argv = NULL;
    685   1.1       cgd 	lastarg = NULL;
    686   1.1       cgd 	if (iflag && funcnest == 0 && argc > 0)
    687   1.1       cgd 		lastarg = argv[-1];
    688   1.1       cgd 	argv -= argc;
    689   1.1       cgd 
    690   1.1       cgd 	/* Print the command if xflag is set. */
    691   1.1       cgd 	if (xflag) {
    692   1.1       cgd 		outc('+', &errout);
    693   1.1       cgd 		for (sp = varlist.list ; sp ; sp = sp->next) {
    694   1.1       cgd 			outc(' ', &errout);
    695   1.1       cgd 			out2str(sp->text);
    696   1.1       cgd 		}
    697   1.1       cgd 		for (sp = arglist.list ; sp ; sp = sp->next) {
    698   1.1       cgd 			outc(' ', &errout);
    699   1.1       cgd 			out2str(sp->text);
    700   1.1       cgd 		}
    701   1.1       cgd 		outc('\n', &errout);
    702   1.1       cgd 		flushout(&errout);
    703   1.1       cgd 	}
    704   1.1       cgd 
    705   1.1       cgd 	/* Now locate the command. */
    706   1.1       cgd 	if (argc == 0) {
    707   1.1       cgd 		cmdentry.cmdtype = CMDBUILTIN;
    708   1.1       cgd 		cmdentry.u.index = BLTINCMD;
    709   1.1       cgd 	} else {
    710  1.26  christos 		static const char PATH[] = "PATH=";
    711  1.47  christos 		const char *path = pathval();
    712  1.26  christos 
    713  1.26  christos 		/*
    714  1.26  christos 		 * Modify the command lookup path, if a PATH= assignment
    715  1.26  christos 		 * is present
    716  1.26  christos 		 */
    717  1.26  christos 		for (sp = varlist.list ; sp ; sp = sp->next)
    718  1.26  christos 			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
    719  1.26  christos 				path = sp->text + sizeof(PATH) - 1;
    720  1.26  christos 
    721  1.38  christos 		find_command(argv[0], &cmdentry, DO_ERR, path);
    722   1.1       cgd 		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
    723  1.31  christos 			exitstatus = 127;
    724   1.1       cgd 			flushout(&errout);
    725   1.1       cgd 			return;
    726   1.1       cgd 		}
    727   1.1       cgd 		/* implement the bltin builtin here */
    728   1.1       cgd 		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
    729   1.1       cgd 			for (;;) {
    730   1.1       cgd 				argv++;
    731   1.1       cgd 				if (--argc == 0)
    732   1.1       cgd 					break;
    733   1.1       cgd 				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
    734   1.1       cgd 					outfmt(&errout, "%s: not found\n", *argv);
    735  1.31  christos 					exitstatus = 127;
    736   1.1       cgd 					flushout(&errout);
    737   1.1       cgd 					return;
    738   1.1       cgd 				}
    739   1.1       cgd 				if (cmdentry.u.index != BLTINCMD)
    740   1.1       cgd 					break;
    741   1.1       cgd 			}
    742   1.1       cgd 		}
    743   1.1       cgd 	}
    744   1.1       cgd 
    745   1.1       cgd 	/* Fork off a child process if necessary. */
    746   1.1       cgd 	if (cmd->ncmd.backgnd
    747  1.21  christos 	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
    748  1.21  christos 	 || ((flags & EV_BACKCMD) != 0
    749   1.1       cgd 	    && (cmdentry.cmdtype != CMDBUILTIN
    750   1.1       cgd 		 || cmdentry.u.index == DOTCMD
    751  1.21  christos 		 || cmdentry.u.index == EVALCMD))) {
    752  1.62  christos 		INTOFF;
    753   1.1       cgd 		jp = makejob(cmd, 1);
    754   1.1       cgd 		mode = cmd->ncmd.backgnd;
    755   1.1       cgd 		if (flags & EV_BACKCMD) {
    756   1.1       cgd 			mode = FORK_NOJOB;
    757   1.1       cgd 			if (pipe(pip) < 0)
    758   1.1       cgd 				error("Pipe call failed");
    759   1.1       cgd 		}
    760  1.61  christos #ifdef DO_SHAREDVFORK
    761  1.61  christos 		/* It is essential that if DO_SHAREDVFORK is defined that the
    762  1.61  christos 		 * child's address space is actually shared with the parent as
    763  1.61  christos 		 * we rely on this.
    764  1.61  christos 		 */
    765  1.61  christos 		if (cmdentry.cmdtype == CMDNORMAL) {
    766  1.61  christos 			pid_t	pid;
    767  1.61  christos 
    768  1.61  christos 			savelocalvars = localvars;
    769  1.61  christos 			localvars = NULL;
    770  1.61  christos 			for (sp = varlist.list ; sp ; sp = sp->next)
    771  1.61  christos 				mklocal(sp->text, VEXPORT);
    772  1.61  christos 			vforked = 1;
    773  1.61  christos 			switch (pid = vfork()) {
    774  1.61  christos 			case -1:
    775  1.61  christos 				TRACE(("Vfork failed, errno=%d", errno));
    776  1.61  christos 				INTON;
    777  1.61  christos 				error("Cannot vfork");
    778  1.61  christos 				break;
    779  1.61  christos 			case 0:
    780  1.61  christos 				/* Make sure that exceptions only unwind to
    781  1.61  christos 				 * after the vfork(2)
    782  1.61  christos 				 */
    783  1.61  christos 				if (setjmp(jmploc.loc)) {
    784  1.61  christos 					if (exception == EXSHELLPROC) {
    785  1.61  christos 						/* We can't progress with the vfork,
    786  1.61  christos 						 * so, set vforked = 2 so the parent
    787  1.61  christos 						 * knows, and _exit();
    788  1.61  christos 						 */
    789  1.61  christos 						vforked = 2;
    790  1.61  christos 						_exit(0);
    791  1.61  christos 					} else {
    792  1.61  christos 						_exit(exerrno);
    793  1.61  christos 					}
    794  1.61  christos 				}
    795  1.61  christos 				savehandler = handler;
    796  1.61  christos 				handler = &jmploc;
    797  1.64  christos 				forkchild(jp, cmd, mode, vforked, isroot);
    798  1.61  christos 				break;
    799  1.61  christos 			default:
    800  1.61  christos 				handler = savehandler;	/* restore from vfork(2) */
    801  1.61  christos 				poplocalvars();
    802  1.61  christos 				localvars = savelocalvars;
    803  1.61  christos 				if (vforked == 2) {
    804  1.61  christos 					vforked = 0;
    805  1.61  christos 
    806  1.61  christos 					(void)waitpid(pid, NULL, 0);
    807  1.61  christos 					/* We need to progress in a normal fork fashion */
    808  1.61  christos 					goto normal_fork;
    809  1.61  christos 				}
    810  1.61  christos 				vforked = 0;
    811  1.64  christos 				forkparent(jp, cmd, mode, pid, isroot);
    812  1.61  christos 				goto parent;
    813  1.61  christos 			}
    814  1.61  christos 		} else {
    815  1.61  christos normal_fork:
    816  1.61  christos #endif
    817  1.64  christos 			if (forkshell(jp, cmd, mode, isroot) != 0)
    818  1.61  christos 				goto parent;	/* at end of routine */
    819  1.62  christos 			INTON;
    820  1.61  christos #ifdef DO_SHAREDVFORK
    821  1.61  christos 		}
    822  1.61  christos #endif
    823   1.1       cgd 		if (flags & EV_BACKCMD) {
    824  1.61  christos 			if (!vforked) {
    825  1.61  christos 				FORCEINTON;
    826  1.61  christos 			}
    827   1.1       cgd 			close(pip[0]);
    828   1.1       cgd 			if (pip[1] != 1) {
    829   1.1       cgd 				close(1);
    830   1.1       cgd 				copyfd(pip[1], 1);
    831   1.1       cgd 				close(pip[1]);
    832   1.1       cgd 			}
    833   1.1       cgd 		}
    834   1.1       cgd 		flags |= EV_EXIT;
    835   1.1       cgd 	}
    836   1.1       cgd 
    837   1.1       cgd 	/* This is the child process if a fork occurred. */
    838   1.1       cgd 	/* Execute the command. */
    839   1.1       cgd 	if (cmdentry.cmdtype == CMDFUNCTION) {
    840  1.31  christos #ifdef DEBUG
    841   1.1       cgd 		trputs("Shell function:  ");  trargs(argv);
    842  1.31  christos #endif
    843   1.1       cgd 		redirect(cmd->ncmd.redirect, REDIR_PUSH);
    844   1.1       cgd 		saveparam = shellparam;
    845   1.1       cgd 		shellparam.malloc = 0;
    846  1.32  christos 		shellparam.reset = 1;
    847   1.1       cgd 		shellparam.nparam = argc - 1;
    848   1.1       cgd 		shellparam.p = argv + 1;
    849   1.1       cgd 		shellparam.optnext = NULL;
    850   1.1       cgd 		INTOFF;
    851   1.1       cgd 		savelocalvars = localvars;
    852   1.1       cgd 		localvars = NULL;
    853   1.1       cgd 		INTON;
    854   1.1       cgd 		if (setjmp(jmploc.loc)) {
    855  1.47  christos 			if (exception == EXSHELLPROC) {
    856  1.47  christos 				freeparam((volatile struct shparam *)
    857  1.47  christos 				    &saveparam);
    858  1.47  christos 			} else {
    859   1.1       cgd 				freeparam(&shellparam);
    860   1.1       cgd 				shellparam = saveparam;
    861   1.1       cgd 			}
    862   1.1       cgd 			poplocalvars();
    863   1.1       cgd 			localvars = savelocalvars;
    864   1.1       cgd 			handler = savehandler;
    865   1.1       cgd 			longjmp(handler->loc, 1);
    866   1.1       cgd 		}
    867   1.1       cgd 		savehandler = handler;
    868   1.1       cgd 		handler = &jmploc;
    869   1.1       cgd 		for (sp = varlist.list ; sp ; sp = sp->next)
    870  1.61  christos 			mklocal(sp->text, 0);
    871   1.1       cgd 		funcnest++;
    872  1.64  christos 		evaltree(cmdentry.u.func, flags & EV_TESTED, isroot);
    873   1.1       cgd 		funcnest--;
    874   1.1       cgd 		INTOFF;
    875   1.1       cgd 		poplocalvars();
    876   1.1       cgd 		localvars = savelocalvars;
    877   1.1       cgd 		freeparam(&shellparam);
    878   1.1       cgd 		shellparam = saveparam;
    879   1.1       cgd 		handler = savehandler;
    880   1.1       cgd 		popredir();
    881   1.1       cgd 		INTON;
    882   1.1       cgd 		if (evalskip == SKIPFUNC) {
    883   1.1       cgd 			evalskip = 0;
    884   1.1       cgd 			skipcount = 0;
    885   1.1       cgd 		}
    886   1.1       cgd 		if (flags & EV_EXIT)
    887   1.1       cgd 			exitshell(exitstatus);
    888   1.1       cgd 	} else if (cmdentry.cmdtype == CMDBUILTIN) {
    889  1.31  christos #ifdef DEBUG
    890   1.1       cgd 		trputs("builtin command:  ");  trargs(argv);
    891  1.31  christos #endif
    892   1.1       cgd 		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
    893   1.1       cgd 		if (flags == EV_BACKCMD) {
    894   1.1       cgd 			memout.nleft = 0;
    895   1.1       cgd 			memout.nextc = memout.buf;
    896   1.1       cgd 			memout.bufsize = 64;
    897   1.1       cgd 			mode |= REDIR_BACKQ;
    898   1.1       cgd 		}
    899   1.1       cgd 		redirect(cmd->ncmd.redirect, mode);
    900   1.1       cgd 		savecmdname = commandname;
    901   1.1       cgd 		cmdenviron = varlist.list;
    902   1.1       cgd 		e = -1;
    903   1.1       cgd 		if (setjmp(jmploc.loc)) {
    904   1.1       cgd 			e = exception;
    905   1.1       cgd 			exitstatus = (e == EXINT)? SIGINT+128 : 2;
    906   1.1       cgd 			goto cmddone;
    907   1.1       cgd 		}
    908   1.1       cgd 		savehandler = handler;
    909   1.1       cgd 		handler = &jmploc;
    910   1.1       cgd 		commandname = argv[0];
    911   1.1       cgd 		argptr = argv + 1;
    912   1.1       cgd 		optptr = NULL;			/* initialize nextopt */
    913   1.1       cgd 		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
    914   1.1       cgd 		flushall();
    915   1.1       cgd cmddone:
    916   1.1       cgd 		out1 = &output;
    917   1.1       cgd 		out2 = &errout;
    918   1.1       cgd 		freestdout();
    919  1.39   thorpej 		cmdenviron = NULL;
    920   1.1       cgd 		if (e != EXSHELLPROC) {
    921   1.1       cgd 			commandname = savecmdname;
    922  1.44   mycroft 			if (flags & EV_EXIT)
    923   1.1       cgd 				exitshell(exitstatus);
    924   1.1       cgd 		}
    925   1.1       cgd 		handler = savehandler;
    926   1.1       cgd 		if (e != -1) {
    927  1.31  christos 			if ((e != EXERROR && e != EXEXEC)
    928  1.31  christos 			   || cmdentry.u.index == BLTINCMD
    929  1.31  christos 			   || cmdentry.u.index == DOTCMD
    930  1.31  christos 			   || cmdentry.u.index == EVALCMD
    931  1.35  christos #ifndef SMALL
    932  1.31  christos 			   || cmdentry.u.index == HISTCMD
    933  1.10       cgd #endif
    934  1.44   mycroft 			   || cmdentry.u.index == EXECCMD)
    935   1.1       cgd 				exraise(e);
    936   1.1       cgd 			FORCEINTON;
    937   1.1       cgd 		}
    938   1.1       cgd 		if (cmdentry.u.index != EXECCMD)
    939   1.1       cgd 			popredir();
    940   1.1       cgd 		if (flags == EV_BACKCMD) {
    941   1.1       cgd 			backcmd->buf = memout.buf;
    942   1.1       cgd 			backcmd->nleft = memout.nextc - memout.buf;
    943   1.1       cgd 			memout.buf = NULL;
    944   1.1       cgd 		}
    945   1.1       cgd 	} else {
    946  1.31  christos #ifdef DEBUG
    947   1.1       cgd 		trputs("normal command:  ");  trargs(argv);
    948  1.31  christos #endif
    949  1.61  christos 		clearredir(vforked);
    950  1.61  christos 		redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0);
    951  1.61  christos 		if (!vforked)
    952  1.61  christos 			for (sp = varlist.list ; sp ; sp = sp->next)
    953  1.61  christos 				setvareq(sp->text, VEXPORT|VSTACK);
    954   1.1       cgd 		envp = environment();
    955  1.61  christos 		shellexec(argv, envp, pathval(), cmdentry.u.index, vforked);
    956   1.1       cgd 	}
    957   1.1       cgd 	goto out;
    958   1.1       cgd 
    959   1.1       cgd parent:	/* parent process gets here (if we forked) */
    960   1.1       cgd 	if (mode == 0) {	/* argument to fork */
    961   1.1       cgd 		exitstatus = waitforjob(jp);
    962   1.1       cgd 	} else if (mode == 2) {
    963   1.1       cgd 		backcmd->fd = pip[0];
    964   1.1       cgd 		close(pip[1]);
    965   1.1       cgd 		backcmd->jp = jp;
    966   1.1       cgd 	}
    967  1.62  christos 	INTON;
    968   1.1       cgd 
    969   1.1       cgd out:
    970   1.1       cgd 	if (lastarg)
    971   1.1       cgd 		setvar("_", lastarg, 0);
    972   1.1       cgd 	popstackmark(&smark);
    973  1.50  christos 
    974  1.50  christos 	if (eflag && exitstatus && !(flags & EV_TESTED))
    975  1.50  christos 	    exitshell(exitstatus);
    976   1.1       cgd }
    977   1.1       cgd 
    978   1.1       cgd 
    979   1.1       cgd /*
    980   1.1       cgd  * Search for a command.  This is called before we fork so that the
    981   1.1       cgd  * location of the command will be available in the parent as well as
    982   1.1       cgd  * the child.  The check for "goodname" is an overly conservative
    983   1.1       cgd  * check that the name will not be subject to expansion.
    984   1.1       cgd  */
    985   1.1       cgd 
    986   1.1       cgd STATIC void
    987   1.1       cgd prehash(n)
    988   1.1       cgd 	union node *n;
    989  1.22  christos {
    990   1.1       cgd 	struct cmdentry entry;
    991   1.1       cgd 
    992  1.15   mycroft 	if (n->type == NCMD && n->ncmd.args)
    993  1.15   mycroft 		if (goodname(n->ncmd.args->narg.text))
    994  1.26  christos 			find_command(n->ncmd.args->narg.text, &entry, 0,
    995  1.26  christos 				     pathval());
    996   1.1       cgd }
    997   1.1       cgd 
    998   1.1       cgd 
    999   1.1       cgd 
   1000   1.1       cgd /*
   1001   1.1       cgd  * Builtin commands.  Builtin commands whose functions are closely
   1002   1.1       cgd  * tied to evaluation are implemented here.
   1003   1.1       cgd  */
   1004   1.1       cgd 
   1005   1.1       cgd /*
   1006   1.1       cgd  * No command given, or a bltin command with no arguments.  Set the
   1007   1.1       cgd  * specified variables.
   1008   1.1       cgd  */
   1009   1.1       cgd 
   1010  1.16       cgd int
   1011  1.16       cgd bltincmd(argc, argv)
   1012  1.16       cgd 	int argc;
   1013  1.31  christos 	char **argv;
   1014  1.16       cgd {
   1015   1.1       cgd 	listsetvar(cmdenviron);
   1016  1.31  christos 	/*
   1017  1.22  christos 	 * Preserve exitstatus of a previous possible redirection
   1018  1.31  christos 	 * as POSIX mandates
   1019  1.22  christos 	 */
   1020  1.21  christos 	return exitstatus;
   1021   1.1       cgd }
   1022   1.1       cgd 
   1023   1.1       cgd 
   1024   1.1       cgd /*
   1025   1.1       cgd  * Handle break and continue commands.  Break, continue, and return are
   1026   1.1       cgd  * all handled by setting the evalskip flag.  The evaluation routines
   1027   1.1       cgd  * above all check this flag, and if it is set they start skipping
   1028   1.1       cgd  * commands rather than executing them.  The variable skipcount is
   1029   1.1       cgd  * the number of loops to break/continue, or the number of function
   1030   1.1       cgd  * levels to return.  (The latter is always 1.)  It should probably
   1031   1.1       cgd  * be an error to break out of more loops than exist, but it isn't
   1032   1.1       cgd  * in the standard shell so we don't make it one here.
   1033   1.1       cgd  */
   1034   1.1       cgd 
   1035  1.16       cgd int
   1036  1.16       cgd breakcmd(argc, argv)
   1037  1.16       cgd 	int argc;
   1038  1.31  christos 	char **argv;
   1039  1.16       cgd {
   1040  1.30  christos 	int n = argc > 1 ? number(argv[1]) : 1;
   1041   1.1       cgd 
   1042   1.1       cgd 	if (n > loopnest)
   1043   1.1       cgd 		n = loopnest;
   1044   1.1       cgd 	if (n > 0) {
   1045   1.1       cgd 		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
   1046   1.1       cgd 		skipcount = n;
   1047   1.1       cgd 	}
   1048   1.1       cgd 	return 0;
   1049   1.1       cgd }
   1050   1.1       cgd 
   1051   1.1       cgd 
   1052   1.1       cgd /*
   1053   1.1       cgd  * The return command.
   1054   1.1       cgd  */
   1055   1.1       cgd 
   1056  1.16       cgd int
   1057  1.31  christos returncmd(argc, argv)
   1058  1.16       cgd 	int argc;
   1059  1.31  christos 	char **argv;
   1060  1.16       cgd {
   1061  1.30  christos 	int ret = argc > 1 ? number(argv[1]) : oexitstatus;
   1062   1.1       cgd 
   1063   1.1       cgd 	if (funcnest) {
   1064   1.1       cgd 		evalskip = SKIPFUNC;
   1065   1.1       cgd 		skipcount = 1;
   1066  1.27  christos 		return ret;
   1067  1.27  christos 	}
   1068  1.27  christos 	else {
   1069  1.27  christos 		/* Do what ksh does; skip the rest of the file */
   1070  1.27  christos 		evalskip = SKIPFILE;
   1071  1.27  christos 		skipcount = 1;
   1072  1.27  christos 		return ret;
   1073   1.1       cgd 	}
   1074   1.1       cgd }
   1075   1.1       cgd 
   1076   1.1       cgd 
   1077  1.16       cgd int
   1078  1.31  christos falsecmd(argc, argv)
   1079  1.16       cgd 	int argc;
   1080  1.31  christos 	char **argv;
   1081  1.16       cgd {
   1082   1.8       jtc 	return 1;
   1083   1.8       jtc }
   1084   1.8       jtc 
   1085   1.8       jtc 
   1086  1.16       cgd int
   1087  1.31  christos truecmd(argc, argv)
   1088  1.16       cgd 	int argc;
   1089  1.31  christos 	char **argv;
   1090  1.16       cgd {
   1091   1.1       cgd 	return 0;
   1092   1.1       cgd }
   1093   1.1       cgd 
   1094   1.1       cgd 
   1095  1.16       cgd int
   1096  1.31  christos execcmd(argc, argv)
   1097  1.16       cgd 	int argc;
   1098  1.31  christos 	char **argv;
   1099  1.16       cgd {
   1100   1.1       cgd 	if (argc > 1) {
   1101  1.20  christos 		struct strlist *sp;
   1102  1.20  christos 
   1103   1.1       cgd 		iflag = 0;		/* exit on error */
   1104   1.7       jtc 		mflag = 0;
   1105   1.7       jtc 		optschanged();
   1106  1.20  christos 		for (sp = cmdenviron; sp ; sp = sp->next)
   1107  1.20  christos 			setvareq(sp->text, VEXPORT|VSTACK);
   1108  1.61  christos 		shellexec(argv + 1, environment(), pathval(), 0, 0);
   1109   1.1       cgd 	}
   1110   1.1       cgd 	return 0;
   1111   1.1       cgd }
   1112