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