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