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