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