Home | History | Annotate | Line # | Download | only in sh
eval.c revision 1.136
      1  1.136       kre /*	$NetBSD: eval.c,v 1.136 2017/05/09 03:38:24 kre Exp $	*/
      2   1.19       cgd 
      3    1.1       cgd /*-
      4    1.7       jtc  * Copyright (c) 1993
      5    1.7       jtc  *	The Regents of the University of California.  All rights reserved.
      6    1.1       cgd  *
      7    1.1       cgd  * This code is derived from software contributed to Berkeley by
      8    1.1       cgd  * Kenneth Almquist.
      9    1.1       cgd  *
     10    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11    1.1       cgd  * modification, are permitted provided that the following conditions
     12    1.1       cgd  * are met:
     13    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.74       agc  * 3. Neither the name of the University nor the names of its contributors
     19    1.1       cgd  *    may be used to endorse or promote products derived from this software
     20    1.1       cgd  *    without specific prior written permission.
     21    1.1       cgd  *
     22    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32    1.1       cgd  * SUCH DAMAGE.
     33    1.1       cgd  */
     34    1.1       cgd 
     35   1.36  christos #include <sys/cdefs.h>
     36    1.1       cgd #ifndef lint
     37   1.19       cgd #if 0
     38   1.26  christos static char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
     39   1.19       cgd #else
     40  1.136       kre __RCSID("$NetBSD: eval.c,v 1.136 2017/05/09 03:38:24 kre Exp $");
     41   1.24       cgd #endif
     42    1.1       cgd #endif /* not lint */
     43    1.1       cgd 
     44   1.90      tron #include <stdbool.h>
     45   1.70       dsl #include <stdlib.h>
     46   1.21  christos #include <signal.h>
     47   1.69       agc #include <stdio.h>
     48  1.116  christos #include <string.h>
     49  1.100  christos #include <errno.h>
     50  1.105       dsl #include <limits.h>
     51   1.21  christos #include <unistd.h>
     52   1.76       dsl #include <sys/fcntl.h>
     53  1.129  christos #include <sys/stat.h>
     54   1.68  christos #include <sys/times.h>
     55   1.71     rafal #include <sys/param.h>
     56   1.61  christos #include <sys/types.h>
     57   1.61  christos #include <sys/wait.h>
     58   1.70       dsl #include <sys/sysctl.h>
     59   1.61  christos 
     60    1.1       cgd /*
     61    1.1       cgd  * Evaluate a command.
     62    1.1       cgd  */
     63    1.1       cgd 
     64    1.1       cgd #include "shell.h"
     65    1.1       cgd #include "nodes.h"
     66    1.1       cgd #include "syntax.h"
     67    1.1       cgd #include "expand.h"
     68    1.1       cgd #include "parser.h"
     69    1.1       cgd #include "jobs.h"
     70    1.1       cgd #include "eval.h"
     71    1.1       cgd #include "builtins.h"
     72    1.1       cgd #include "options.h"
     73    1.1       cgd #include "exec.h"
     74    1.1       cgd #include "redir.h"
     75    1.1       cgd #include "input.h"
     76    1.1       cgd #include "output.h"
     77    1.1       cgd #include "trap.h"
     78    1.1       cgd #include "var.h"
     79    1.1       cgd #include "memalloc.h"
     80    1.1       cgd #include "error.h"
     81   1.21  christos #include "show.h"
     82    1.1       cgd #include "mystring.h"
     83   1.64  christos #include "main.h"
     84   1.35  christos #ifndef SMALL
     85  1.114  christos #include "nodenames.h"
     86    1.7       jtc #include "myhistedit.h"
     87   1.11       cgd #endif
     88    1.1       cgd 
     89    1.1       cgd 
     90  1.109  christos STATIC enum skipstate evalskip;	/* != SKIPNONE if we are skipping commands */
     91    1.1       cgd STATIC int skipcount;		/* number of levels to skip */
     92  1.109  christos STATIC int loopnest;		/* current loop nesting level */
     93  1.109  christos STATIC int funcnest;		/* depth of function calls */
     94  1.103  christos STATIC int builtin_flags;	/* evalcommand flags for builtins */
     95  1.109  christos /*
     96  1.109  christos  * Base function nesting level inside a dot command.  Set to 0 initially
     97  1.109  christos  * and to (funcnest + 1) before every dot command to enable
     98  1.109  christos  *   1) detection of being in a file sourced by a dot command and
     99  1.109  christos  *   2) counting of function nesting in that file for the implementation
    100  1.109  christos  *      of the return command.
    101  1.109  christos  * The value is reset to its previous value after the dot command.
    102  1.109  christos  */
    103  1.109  christos STATIC int dot_funcnest;
    104    1.1       cgd 
    105    1.1       cgd 
    106   1.89      matt const char *commandname;
    107    1.1       cgd struct strlist *cmdenviron;
    108    1.1       cgd int exitstatus;			/* exit status of last command */
    109   1.68  christos int back_exitstatus;		/* exit status of backquoted command */
    110    1.1       cgd 
    111    1.1       cgd 
    112   1.68  christos STATIC void evalloop(union node *, int);
    113   1.68  christos STATIC void evalfor(union node *, int);
    114   1.68  christos STATIC void evalcase(union node *, int);
    115   1.68  christos STATIC void evalsubshell(union node *, int);
    116   1.68  christos STATIC void expredir(union node *);
    117   1.68  christos STATIC void evalpipe(union node *);
    118   1.68  christos STATIC void evalcommand(union node *, int, struct backcmd *);
    119   1.68  christos STATIC void prehash(union node *);
    120    1.1       cgd 
    121  1.109  christos STATIC char *find_dot_file(char *);
    122    1.1       cgd 
    123    1.1       cgd /*
    124    1.1       cgd  * Called to reset things after an exception.
    125    1.1       cgd  */
    126    1.1       cgd 
    127    1.1       cgd #ifdef mkinit
    128    1.1       cgd INCLUDE "eval.h"
    129    1.1       cgd 
    130    1.1       cgd RESET {
    131  1.109  christos 	reset_eval();
    132    1.1       cgd }
    133    1.1       cgd 
    134    1.1       cgd SHELLPROC {
    135    1.1       cgd 	exitstatus = 0;
    136    1.1       cgd }
    137    1.1       cgd #endif
    138    1.1       cgd 
    139  1.109  christos void
    140  1.109  christos reset_eval(void)
    141  1.109  christos {
    142  1.109  christos 	evalskip = SKIPNONE;
    143  1.109  christos 	dot_funcnest = 0;
    144  1.109  christos 	loopnest = 0;
    145  1.109  christos 	funcnest = 0;
    146  1.109  christos }
    147  1.109  christos 
    148   1.76       dsl static int
    149   1.76       dsl sh_pipe(int fds[2])
    150   1.76       dsl {
    151   1.76       dsl 	int nfd;
    152   1.76       dsl 
    153   1.76       dsl 	if (pipe(fds))
    154   1.76       dsl 		return -1;
    155   1.76       dsl 
    156   1.76       dsl 	if (fds[0] < 3) {
    157   1.76       dsl 		nfd = fcntl(fds[0], F_DUPFD, 3);
    158   1.76       dsl 		if (nfd != -1) {
    159   1.76       dsl 			close(fds[0]);
    160   1.76       dsl 			fds[0] = nfd;
    161   1.76       dsl 		}
    162   1.76       dsl 	}
    163   1.76       dsl 
    164   1.76       dsl 	if (fds[1] < 3) {
    165   1.76       dsl 		nfd = fcntl(fds[1], F_DUPFD, 3);
    166   1.76       dsl 		if (nfd != -1) {
    167   1.76       dsl 			close(fds[1]);
    168   1.76       dsl 			fds[1] = nfd;
    169   1.76       dsl 		}
    170   1.76       dsl 	}
    171   1.76       dsl 	return 0;
    172   1.76       dsl }
    173    1.1       cgd 
    174    1.1       cgd 
    175    1.1       cgd /*
    176    1.1       cgd  * The eval commmand.
    177    1.1       cgd  */
    178    1.1       cgd 
    179   1.16       cgd int
    180   1.68  christos evalcmd(int argc, char **argv)
    181    1.1       cgd {
    182    1.1       cgd         char *p;
    183    1.1       cgd         char *concat;
    184    1.1       cgd         char **ap;
    185    1.1       cgd 
    186    1.1       cgd         if (argc > 1) {
    187    1.1       cgd                 p = argv[1];
    188    1.1       cgd                 if (argc > 2) {
    189    1.1       cgd                         STARTSTACKSTR(concat);
    190    1.1       cgd                         ap = argv + 2;
    191    1.1       cgd                         for (;;) {
    192    1.1       cgd                                 while (*p)
    193    1.1       cgd                                         STPUTC(*p++, concat);
    194    1.1       cgd                                 if ((p = *ap++) == NULL)
    195    1.1       cgd                                         break;
    196    1.1       cgd                                 STPUTC(' ', concat);
    197    1.1       cgd                         }
    198    1.1       cgd                         STPUTC('\0', concat);
    199    1.1       cgd                         p = grabstackstr(concat);
    200    1.1       cgd                 }
    201  1.103  christos                 evalstring(p, builtin_flags & EV_TESTED);
    202  1.133       kre         } else
    203  1.133       kre 		exitstatus = 0;
    204    1.1       cgd         return exitstatus;
    205    1.1       cgd }
    206    1.1       cgd 
    207    1.1       cgd 
    208    1.1       cgd /*
    209    1.1       cgd  * Execute a command or commands contained in a string.
    210    1.1       cgd  */
    211    1.1       cgd 
    212    1.1       cgd void
    213   1.68  christos evalstring(char *s, int flag)
    214   1.68  christos {
    215    1.1       cgd 	union node *n;
    216    1.1       cgd 	struct stackmark smark;
    217    1.1       cgd 
    218    1.1       cgd 	setstackmark(&smark);
    219    1.1       cgd 	setinputstring(s, 1);
    220   1.64  christos 
    221    1.1       cgd 	while ((n = parsecmd(0)) != NEOF) {
    222  1.114  christos 		TRACE(("evalstring: "); showtree(n));
    223  1.112  christos 		if (nflag == 0)
    224  1.125       kre 			evaltree(n, flag | EV_MORE);
    225    1.1       cgd 		popstackmark(&smark);
    226    1.1       cgd 	}
    227    1.1       cgd 	popfile();
    228    1.1       cgd 	popstackmark(&smark);
    229    1.1       cgd }
    230    1.1       cgd 
    231    1.1       cgd 
    232    1.1       cgd 
    233    1.1       cgd /*
    234    1.1       cgd  * Evaluate a parse tree.  The value is left in the global variable
    235    1.1       cgd  * exitstatus.
    236    1.1       cgd  */
    237    1.1       cgd 
    238    1.1       cgd void
    239   1.68  christos evaltree(union node *n, int flags)
    240   1.17       cgd {
    241   1.90      tron 	bool do_etest;
    242  1.136       kre 	int sflags = flags & ~EV_EXIT;
    243   1.90      tron 
    244   1.90      tron 	do_etest = false;
    245  1.115  christos 	if (n == NULL || nflag) {
    246  1.115  christos 		TRACE(("evaltree(%s) called\n", n == NULL ? "NULL" : "-n"));
    247  1.115  christos 		if (nflag == 0)
    248  1.115  christos 			exitstatus = 0;
    249    1.7       jtc 		goto out;
    250    1.1       cgd 	}
    251   1.35  christos #ifndef SMALL
    252    1.7       jtc 	displayhist = 1;	/* show history substitutions done with fc */
    253   1.10       cgd #endif
    254  1.114  christos #ifdef NODETYPENAME
    255  1.115  christos 	TRACE(("pid %d, evaltree(%p: %s(%d), %#x) called\n",
    256  1.114  christos 	    getpid(), n, NODETYPENAME(n->type), n->type, flags));
    257  1.114  christos #else
    258  1.115  christos 	TRACE(("pid %d, evaltree(%p: %d, %#x) called\n",
    259   1.70       dsl 	    getpid(), n, n->type, flags));
    260  1.114  christos #endif
    261    1.1       cgd 	switch (n->type) {
    262    1.1       cgd 	case NSEMI:
    263  1.136       kre 		evaltree(n->nbinary.ch1, (sflags & EV_TESTED) |
    264  1.125       kre 		    (n->nbinary.ch2 ? EV_MORE : 0));
    265  1.115  christos 		if (nflag || evalskip)
    266    1.1       cgd 			goto out;
    267   1.65  christos 		evaltree(n->nbinary.ch2, flags);
    268    1.1       cgd 		break;
    269    1.1       cgd 	case NAND:
    270  1.125       kre 		evaltree(n->nbinary.ch1, EV_TESTED | EV_MORE);
    271  1.115  christos 		if (nflag || evalskip || exitstatus != 0)
    272    1.1       cgd 			goto out;
    273   1.79   mycroft 		evaltree(n->nbinary.ch2, flags);
    274    1.1       cgd 		break;
    275    1.1       cgd 	case NOR:
    276  1.125       kre 		evaltree(n->nbinary.ch1, EV_TESTED | EV_MORE);
    277  1.115  christos 		if (nflag || evalskip || exitstatus == 0)
    278    1.1       cgd 			goto out;
    279   1.79   mycroft 		evaltree(n->nbinary.ch2, flags);
    280    1.1       cgd 		break;
    281    1.1       cgd 	case NREDIR:
    282    1.1       cgd 		expredir(n->nredir.redirect);
    283  1.118  christos 		redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP);
    284   1.65  christos 		evaltree(n->nredir.n, flags);
    285    1.1       cgd 		popredir();
    286    1.1       cgd 		break;
    287    1.1       cgd 	case NSUBSHELL:
    288  1.125       kre 		evalsubshell(n, flags & ~EV_MORE);
    289   1.93      tron 		do_etest = !(flags & EV_TESTED);
    290    1.1       cgd 		break;
    291    1.1       cgd 	case NBACKGND:
    292  1.125       kre 		evalsubshell(n, flags & ~EV_MORE);
    293    1.1       cgd 		break;
    294    1.1       cgd 	case NIF: {
    295  1.125       kre 		evaltree(n->nif.test, EV_TESTED | EV_MORE);
    296  1.115  christos 		if (nflag || evalskip)
    297    1.1       cgd 			goto out;
    298   1.28  christos 		if (exitstatus == 0)
    299   1.65  christos 			evaltree(n->nif.ifpart, flags);
    300   1.22  christos 		else if (n->nif.elsepart)
    301   1.65  christos 			evaltree(n->nif.elsepart, flags);
    302   1.29        pk 		else
    303   1.29        pk 			exitstatus = 0;
    304    1.1       cgd 		break;
    305    1.1       cgd 	}
    306    1.1       cgd 	case NWHILE:
    307    1.1       cgd 	case NUNTIL:
    308  1.136       kre 		evalloop(n, sflags);
    309    1.1       cgd 		break;
    310    1.1       cgd 	case NFOR:
    311  1.136       kre 		evalfor(n, sflags);
    312    1.1       cgd 		break;
    313    1.1       cgd 	case NCASE:
    314  1.136       kre 		evalcase(n, sflags);
    315    1.1       cgd 		break;
    316    1.1       cgd 	case NDEFUN:
    317    1.1       cgd 		defun(n->narg.text, n->narg.next);
    318    1.1       cgd 		exitstatus = 0;
    319    1.1       cgd 		break;
    320    1.7       jtc 	case NNOT:
    321  1.136       kre 		evaltree(n->nnot.com, (sflags & EV_MORE) | EV_TESTED);
    322    1.7       jtc 		exitstatus = !exitstatus;
    323    1.7       jtc 		break;
    324    1.1       cgd 	case NPIPE:
    325   1.65  christos 		evalpipe(n);
    326   1.93      tron 		do_etest = !(flags & EV_TESTED);
    327    1.1       cgd 		break;
    328    1.1       cgd 	case NCMD:
    329  1.102    plunky 		evalcommand(n, flags, NULL);
    330   1.93      tron 		do_etest = !(flags & EV_TESTED);
    331    1.1       cgd 		break;
    332    1.1       cgd 	default:
    333  1.115  christos #ifdef NODETYPENAME
    334  1.115  christos 		out1fmt("Node type = %d(%s)\n", n->type, NODETYPENAME(n->type));
    335  1.115  christos #else
    336    1.1       cgd 		out1fmt("Node type = %d\n", n->type);
    337  1.115  christos #endif
    338    1.1       cgd 		flushout(&output);
    339    1.1       cgd 		break;
    340    1.1       cgd 	}
    341    1.1       cgd out:
    342    1.1       cgd 	if (pendingsigs)
    343    1.1       cgd 		dotrap();
    344   1.93      tron 	if ((flags & EV_EXIT) != 0 || (eflag && exitstatus != 0 && do_etest))
    345    1.1       cgd 		exitshell(exitstatus);
    346    1.1       cgd }
    347    1.1       cgd 
    348    1.1       cgd 
    349    1.1       cgd STATIC void
    350   1.68  christos evalloop(union node *n, int flags)
    351   1.22  christos {
    352    1.1       cgd 	int status;
    353    1.1       cgd 
    354    1.1       cgd 	loopnest++;
    355    1.1       cgd 	status = 0;
    356  1.114  christos 
    357  1.114  christos #ifdef NODETYPENAME
    358  1.114  christos 	TRACE(("evalloop %s: ", NODETYPENAME(n->type)));
    359  1.114  christos #else
    360  1.114  christos 	TRACE(("evalloop %s: ", n->type == NWHILE ? "while" : "until"));
    361  1.114  christos #endif
    362  1.114  christos 	TRACE((""); showtree(n->nbinary.ch1));
    363  1.114  christos 	TRACE(("evalloop    do: "); showtree(n->nbinary.ch2));
    364  1.114  christos 	TRACE(("evalloop  done\n"));
    365  1.114  christos 
    366    1.1       cgd 	for (;;) {
    367  1.125       kre 		evaltree(n->nbinary.ch1, EV_TESTED | EV_MORE);
    368  1.115  christos 		if (nflag)
    369  1.115  christos 			break;
    370    1.1       cgd 		if (evalskip) {
    371    1.1       cgd skipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
    372  1.109  christos 				evalskip = SKIPNONE;
    373    1.1       cgd 				continue;
    374    1.1       cgd 			}
    375    1.1       cgd 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    376  1.109  christos 				evalskip = SKIPNONE;
    377    1.1       cgd 			break;
    378    1.1       cgd 		}
    379    1.1       cgd 		if (n->type == NWHILE) {
    380    1.1       cgd 			if (exitstatus != 0)
    381    1.1       cgd 				break;
    382    1.1       cgd 		} else {
    383    1.1       cgd 			if (exitstatus == 0)
    384    1.1       cgd 				break;
    385    1.1       cgd 		}
    386  1.126       kre 		evaltree(n->nbinary.ch2, (flags & EV_TESTED) | EV_MORE);
    387    1.1       cgd 		status = exitstatus;
    388    1.1       cgd 		if (evalskip)
    389    1.1       cgd 			goto skipping;
    390    1.1       cgd 	}
    391    1.1       cgd 	loopnest--;
    392    1.1       cgd 	exitstatus = status;
    393    1.1       cgd }
    394    1.1       cgd 
    395    1.1       cgd 
    396    1.1       cgd 
    397    1.1       cgd STATIC void
    398   1.68  christos evalfor(union node *n, int flags)
    399   1.22  christos {
    400    1.1       cgd 	struct arglist arglist;
    401    1.1       cgd 	union node *argp;
    402    1.1       cgd 	struct strlist *sp;
    403    1.1       cgd 	struct stackmark smark;
    404  1.115  christos 	int status;
    405  1.115  christos 
    406  1.115  christos 	status = nflag ? exitstatus : 0;
    407    1.1       cgd 
    408    1.1       cgd 	setstackmark(&smark);
    409    1.1       cgd 	arglist.lastp = &arglist.list;
    410    1.1       cgd 	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
    411   1.78       dsl 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    412    1.1       cgd 		if (evalskip)
    413    1.1       cgd 			goto out;
    414    1.1       cgd 	}
    415    1.1       cgd 	*arglist.lastp = NULL;
    416    1.1       cgd 
    417    1.1       cgd 	loopnest++;
    418    1.1       cgd 	for (sp = arglist.list ; sp ; sp = sp->next) {
    419  1.127       kre 		int f = flags & (EV_TESTED | EV_MORE);
    420  1.127       kre 
    421  1.127       kre 		if (sp->next)
    422  1.127       kre 			f |= EV_MORE;
    423  1.127       kre 
    424  1.131       kre 		if (xflag) {
    425  1.131       kre 			out2str(ps4val());
    426  1.131       kre 			out2str("for ");
    427  1.131       kre 			out2str(n->nfor.var);
    428  1.131       kre 			out2c('=');
    429  1.131       kre 			out2shstr(sp->text);
    430  1.131       kre 			out2c('\n');
    431  1.131       kre 			flushout(&errout);
    432  1.131       kre 		}
    433  1.131       kre 
    434    1.1       cgd 		setvar(n->nfor.var, sp->text, 0);
    435  1.127       kre 		evaltree(n->nfor.body, f);
    436   1.68  christos 		status = exitstatus;
    437  1.115  christos 		if (nflag)
    438  1.115  christos 			break;
    439    1.1       cgd 		if (evalskip) {
    440    1.1       cgd 			if (evalskip == SKIPCONT && --skipcount <= 0) {
    441  1.109  christos 				evalskip = SKIPNONE;
    442    1.1       cgd 				continue;
    443    1.1       cgd 			}
    444    1.1       cgd 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    445  1.109  christos 				evalskip = SKIPNONE;
    446    1.1       cgd 			break;
    447    1.1       cgd 		}
    448    1.1       cgd 	}
    449    1.1       cgd 	loopnest--;
    450   1.68  christos 	exitstatus = status;
    451    1.1       cgd out:
    452    1.1       cgd 	popstackmark(&smark);
    453    1.1       cgd }
    454    1.1       cgd 
    455    1.1       cgd 
    456    1.1       cgd 
    457    1.1       cgd STATIC void
    458   1.68  christos evalcase(union node *n, int flags)
    459   1.16       cgd {
    460  1.134       kre 	union node *cp, *ncp;
    461    1.1       cgd 	union node *patp;
    462    1.1       cgd 	struct arglist arglist;
    463    1.1       cgd 	struct stackmark smark;
    464   1.68  christos 	int status = 0;
    465    1.1       cgd 
    466    1.1       cgd 	setstackmark(&smark);
    467    1.1       cgd 	arglist.lastp = &arglist.list;
    468    1.7       jtc 	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
    469  1.134       kre 	for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
    470  1.134       kre 		for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
    471    1.1       cgd 			if (casematch(patp, arglist.list->text)) {
    472  1.134       kre 				while (cp != NULL && evalskip == 0 &&
    473  1.134       kre 				    nflag == 0) {
    474  1.134       kre 					if (cp->type == NCLISTCONT)
    475  1.134       kre 						ncp = cp->nclist.next;
    476  1.134       kre 					else
    477  1.134       kre 						ncp = NULL;
    478  1.134       kre 					evaltree(cp->nclist.body,
    479  1.134       kre 					    ncp ? (flags & ~EV_EXIT) | EV_MORE
    480  1.134       kre 						: flags );
    481   1.68  christos 					status = exitstatus;
    482  1.134       kre 					cp = ncp;
    483    1.1       cgd 				}
    484    1.1       cgd 				goto out;
    485    1.1       cgd 			}
    486    1.1       cgd 		}
    487    1.1       cgd 	}
    488  1.134       kre  out:
    489   1.68  christos 	exitstatus = status;
    490    1.1       cgd 	popstackmark(&smark);
    491    1.1       cgd }
    492    1.1       cgd 
    493    1.1       cgd 
    494    1.1       cgd 
    495    1.1       cgd /*
    496    1.1       cgd  * Kick off a subshell to evaluate a tree.
    497    1.1       cgd  */
    498    1.1       cgd 
    499    1.1       cgd STATIC void
    500   1.68  christos evalsubshell(union node *n, int flags)
    501   1.16       cgd {
    502    1.1       cgd 	struct job *jp;
    503    1.1       cgd 	int backgnd = (n->type == NBACKGND);
    504    1.1       cgd 
    505    1.1       cgd 	expredir(n->nredir.redirect);
    506   1.63   mycroft 	INTOFF;
    507    1.1       cgd 	jp = makejob(n, 1);
    508   1.83  christos 	if (forkshell(jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
    509   1.63   mycroft 		INTON;
    510    1.1       cgd 		if (backgnd)
    511    1.1       cgd 			flags &=~ EV_TESTED;
    512  1.119  christos 		redirect(n->nredir.redirect, REDIR_KEEP);
    513   1.64  christos 		/* never returns */
    514   1.65  christos 		evaltree(n->nredir.n, flags | EV_EXIT);
    515    1.1       cgd 	}
    516  1.113  christos 	exitstatus = backgnd ? 0 : waitforjob(jp);
    517   1.63   mycroft 	INTON;
    518    1.1       cgd }
    519    1.1       cgd 
    520    1.1       cgd 
    521    1.1       cgd 
    522    1.1       cgd /*
    523    1.1       cgd  * Compute the names of the files in a redirection list.
    524    1.1       cgd  */
    525    1.1       cgd 
    526    1.1       cgd STATIC void
    527   1.68  christos expredir(union node *n)
    528   1.22  christos {
    529   1.34       tls 	union node *redir;
    530    1.1       cgd 
    531    1.1       cgd 	for (redir = n ; redir ; redir = redir->nfile.next) {
    532   1.14       jtc 		struct arglist fn;
    533  1.120  christos 
    534   1.14       jtc 		fn.lastp = &fn.list;
    535   1.14       jtc 		switch (redir->type) {
    536   1.45  christos 		case NFROMTO:
    537   1.14       jtc 		case NFROM:
    538   1.14       jtc 		case NTO:
    539   1.59  christos 		case NCLOBBER:
    540   1.14       jtc 		case NAPPEND:
    541    1.7       jtc 			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
    542    1.1       cgd 			redir->nfile.expfname = fn.list->text;
    543   1.14       jtc 			break;
    544   1.14       jtc 		case NFROMFD:
    545   1.14       jtc 		case NTOFD:
    546   1.14       jtc 			if (redir->ndup.vname) {
    547   1.14       jtc 				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
    548   1.14       jtc 				fixredir(redir, fn.list->text, 1);
    549   1.14       jtc 			}
    550   1.14       jtc 			break;
    551    1.1       cgd 		}
    552    1.1       cgd 	}
    553    1.1       cgd }
    554    1.1       cgd 
    555    1.1       cgd 
    556    1.1       cgd 
    557    1.1       cgd /*
    558    1.1       cgd  * Evaluate a pipeline.  All the processes in the pipeline are children
    559    1.1       cgd  * of the process creating the pipeline.  (This differs from some versions
    560    1.1       cgd  * of the shell, which make the last process in a pipeline the parent
    561    1.1       cgd  * of all the rest.)
    562    1.1       cgd  */
    563    1.1       cgd 
    564    1.1       cgd STATIC void
    565   1.68  christos evalpipe(union node *n)
    566   1.22  christos {
    567    1.1       cgd 	struct job *jp;
    568    1.1       cgd 	struct nodelist *lp;
    569    1.1       cgd 	int pipelen;
    570    1.1       cgd 	int prevfd;
    571    1.1       cgd 	int pip[2];
    572    1.1       cgd 
    573   1.18       cgd 	TRACE(("evalpipe(0x%lx) called\n", (long)n));
    574    1.1       cgd 	pipelen = 0;
    575    1.1       cgd 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
    576    1.1       cgd 		pipelen++;
    577    1.1       cgd 	INTOFF;
    578    1.1       cgd 	jp = makejob(n, pipelen);
    579    1.1       cgd 	prevfd = -1;
    580    1.1       cgd 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
    581    1.1       cgd 		prehash(lp->n);
    582    1.1       cgd 		pip[1] = -1;
    583    1.1       cgd 		if (lp->next) {
    584   1.76       dsl 			if (sh_pipe(pip) < 0) {
    585   1.87  christos 				if (prevfd >= 0)
    586   1.87  christos 					close(prevfd);
    587    1.1       cgd 				error("Pipe call failed");
    588    1.1       cgd 			}
    589    1.1       cgd 		}
    590   1.83  christos 		if (forkshell(jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
    591    1.1       cgd 			INTON;
    592  1.120  christos 			if (prevfd > 0)
    593  1.120  christos 				movefd(prevfd, 0);
    594    1.1       cgd 			if (pip[1] >= 0) {
    595    1.1       cgd 				close(pip[0]);
    596  1.120  christos 				movefd(pip[1], 1);
    597    1.1       cgd 			}
    598   1.65  christos 			evaltree(lp->n, EV_EXIT);
    599    1.1       cgd 		}
    600    1.1       cgd 		if (prevfd >= 0)
    601    1.1       cgd 			close(prevfd);
    602    1.1       cgd 		prevfd = pip[0];
    603    1.1       cgd 		close(pip[1]);
    604    1.1       cgd 	}
    605    1.1       cgd 	if (n->npipe.backgnd == 0) {
    606    1.1       cgd 		exitstatus = waitforjob(jp);
    607    1.1       cgd 		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
    608  1.115  christos 	} else
    609  1.115  christos 		exitstatus = 0;
    610   1.60   mycroft 	INTON;
    611    1.1       cgd }
    612    1.1       cgd 
    613    1.1       cgd 
    614    1.1       cgd 
    615    1.1       cgd /*
    616    1.1       cgd  * Execute a command inside back quotes.  If it's a builtin command, we
    617    1.1       cgd  * want to save its output in a block obtained from malloc.  Otherwise
    618    1.1       cgd  * we fork off a subprocess and get the output of the command via a pipe.
    619    1.1       cgd  * Should be called with interrupts off.
    620    1.1       cgd  */
    621    1.1       cgd 
    622    1.1       cgd void
    623   1.68  christos evalbackcmd(union node *n, struct backcmd *result)
    624   1.22  christos {
    625    1.1       cgd 	int pip[2];
    626    1.1       cgd 	struct job *jp;
    627    1.1       cgd 	struct stackmark smark;		/* unnecessary */
    628    1.1       cgd 
    629    1.1       cgd 	setstackmark(&smark);
    630    1.1       cgd 	result->fd = -1;
    631    1.1       cgd 	result->buf = NULL;
    632    1.1       cgd 	result->nleft = 0;
    633    1.1       cgd 	result->jp = NULL;
    634  1.115  christos 	if (nflag || n == NULL) {
    635    1.7       jtc 		goto out;
    636   1.23  christos 	}
    637   1.46  christos #ifdef notyet
    638   1.46  christos 	/*
    639   1.46  christos 	 * For now we disable executing builtins in the same
    640   1.46  christos 	 * context as the shell, because we are not keeping
    641   1.46  christos 	 * enough state to recover from changes that are
    642   1.46  christos 	 * supposed only to affect subshells. eg. echo "`cd /`"
    643   1.46  christos 	 */
    644    1.7       jtc 	if (n->type == NCMD) {
    645   1.23  christos 		exitstatus = oexitstatus;
    646   1.65  christos 		evalcommand(n, EV_BACKCMD, result);
    647   1.46  christos 	} else
    648   1.46  christos #endif
    649   1.46  christos 	{
    650   1.63   mycroft 		INTOFF;
    651   1.76       dsl 		if (sh_pipe(pip) < 0)
    652    1.1       cgd 			error("Pipe call failed");
    653    1.1       cgd 		jp = makejob(n, 1);
    654   1.65  christos 		if (forkshell(jp, n, FORK_NOJOB) == 0) {
    655    1.1       cgd 			FORCEINTON;
    656    1.1       cgd 			close(pip[0]);
    657  1.120  christos 			movefd(pip[1], 1);
    658   1.50  christos 			eflag = 0;
    659   1.65  christos 			evaltree(n, EV_EXIT);
    660   1.68  christos 			/* NOTREACHED */
    661    1.1       cgd 		}
    662    1.1       cgd 		close(pip[1]);
    663    1.1       cgd 		result->fd = pip[0];
    664    1.1       cgd 		result->jp = jp;
    665   1.63   mycroft 		INTON;
    666    1.1       cgd 	}
    667    1.7       jtc out:
    668    1.1       cgd 	popstackmark(&smark);
    669    1.1       cgd 	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
    670    1.1       cgd 		result->fd, result->buf, result->nleft, result->jp));
    671    1.1       cgd }
    672    1.1       cgd 
    673   1.70       dsl static const char *
    674   1.70       dsl syspath(void)
    675   1.70       dsl {
    676   1.70       dsl 	static char *sys_path = NULL;
    677   1.70       dsl 	static int mib[] = {CTL_USER, USER_CS_PATH};
    678   1.80  christos 	static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
    679   1.72       agc 	size_t len;
    680   1.70       dsl 
    681   1.70       dsl 	if (sys_path == NULL) {
    682   1.70       dsl 		if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
    683   1.70       dsl 		    (sys_path = ckmalloc(len + 5)) != NULL &&
    684   1.70       dsl 		    sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
    685   1.70       dsl 			memcpy(sys_path, "PATH=", 5);
    686   1.70       dsl 		} else {
    687   1.70       dsl 			ckfree(sys_path);
    688   1.70       dsl 			/* something to keep things happy */
    689   1.80  christos 			sys_path = def_path;
    690   1.70       dsl 		}
    691   1.70       dsl 	}
    692   1.70       dsl 	return sys_path;
    693   1.70       dsl }
    694   1.70       dsl 
    695   1.70       dsl static int
    696   1.70       dsl parse_command_args(int argc, char **argv, int *use_syspath)
    697   1.70       dsl {
    698   1.70       dsl 	int sv_argc = argc;
    699   1.70       dsl 	char *cp, c;
    700   1.70       dsl 
    701   1.70       dsl 	*use_syspath = 0;
    702   1.70       dsl 
    703   1.70       dsl 	for (;;) {
    704   1.70       dsl 		argv++;
    705   1.70       dsl 		if (--argc == 0)
    706   1.70       dsl 			break;
    707   1.70       dsl 		cp = *argv;
    708   1.70       dsl 		if (*cp++ != '-')
    709   1.70       dsl 			break;
    710   1.70       dsl 		if (*cp == '-' && cp[1] == 0) {
    711   1.70       dsl 			argv++;
    712   1.70       dsl 			argc--;
    713   1.70       dsl 			break;
    714   1.70       dsl 		}
    715   1.70       dsl 		while ((c = *cp++)) {
    716   1.70       dsl 			switch (c) {
    717   1.70       dsl 			case 'p':
    718   1.70       dsl 				*use_syspath = 1;
    719   1.70       dsl 				break;
    720   1.70       dsl 			default:
    721   1.70       dsl 				/* run 'typecmd' for other options */
    722   1.70       dsl 				return 0;
    723   1.70       dsl 			}
    724   1.70       dsl 		}
    725   1.70       dsl 	}
    726   1.70       dsl 	return sv_argc - argc;
    727   1.70       dsl }
    728    1.1       cgd 
    729   1.61  christos int vforked = 0;
    730   1.97  christos extern char *trap[];
    731    1.1       cgd 
    732    1.1       cgd /*
    733    1.1       cgd  * Execute a simple command.
    734    1.1       cgd  */
    735    1.1       cgd 
    736    1.1       cgd STATIC void
    737   1.88  christos evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
    738   1.16       cgd {
    739    1.1       cgd 	struct stackmark smark;
    740    1.1       cgd 	union node *argp;
    741    1.1       cgd 	struct arglist arglist;
    742    1.1       cgd 	struct arglist varlist;
    743   1.88  christos 	volatile int flags = flgs;
    744   1.88  christos 	char ** volatile argv;
    745   1.88  christos 	volatile int argc;
    746    1.1       cgd 	char **envp;
    747    1.1       cgd 	int varflag;
    748    1.1       cgd 	struct strlist *sp;
    749   1.88  christos 	volatile int mode;
    750    1.1       cgd 	int pip[2];
    751    1.1       cgd 	struct cmdentry cmdentry;
    752   1.88  christos 	struct job * volatile jp;
    753    1.1       cgd 	struct jmploc jmploc;
    754   1.85  christos 	struct jmploc *volatile savehandler = NULL;
    755   1.89      matt 	const char *volatile savecmdname;
    756    1.1       cgd 	volatile struct shparam saveparam;
    757    1.1       cgd 	struct localvar *volatile savelocalvars;
    758    1.1       cgd 	volatile int e;
    759   1.88  christos 	char * volatile lastarg;
    760   1.88  christos 	const char * volatile path = pathval();
    761   1.82     lukem 	volatile int temp_path;
    762    1.1       cgd 
    763   1.61  christos 	vforked = 0;
    764    1.1       cgd 	/* First expand the arguments. */
    765   1.65  christos 	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
    766    1.1       cgd 	setstackmark(&smark);
    767   1.68  christos 	back_exitstatus = 0;
    768   1.68  christos 
    769    1.1       cgd 	arglist.lastp = &arglist.list;
    770    1.1       cgd 	varflag = 1;
    771   1.78       dsl 	/* Expand arguments, ignoring the initial 'name=value' ones */
    772    1.1       cgd 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    773   1.20  christos 		char *p = argp->narg.text;
    774    1.1       cgd 		if (varflag && is_name(*p)) {
    775    1.1       cgd 			do {
    776    1.1       cgd 				p++;
    777    1.1       cgd 			} while (is_in_name(*p));
    778   1.68  christos 			if (*p == '=')
    779    1.1       cgd 				continue;
    780    1.1       cgd 		}
    781    1.7       jtc 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    782    1.1       cgd 		varflag = 0;
    783    1.1       cgd 	}
    784    1.1       cgd 	*arglist.lastp = NULL;
    785   1.68  christos 
    786   1.68  christos 	expredir(cmd->ncmd.redirect);
    787   1.68  christos 
    788   1.78       dsl 	/* Now do the initial 'name=value' ones we skipped above */
    789   1.68  christos 	varlist.lastp = &varlist.list;
    790   1.68  christos 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    791   1.68  christos 		char *p = argp->narg.text;
    792   1.68  christos 		if (!is_name(*p))
    793   1.68  christos 			break;
    794   1.68  christos 		do
    795   1.68  christos 			p++;
    796   1.68  christos 		while (is_in_name(*p));
    797   1.68  christos 		if (*p != '=')
    798   1.68  christos 			break;
    799   1.68  christos 		expandarg(argp, &varlist, EXP_VARTILDE);
    800   1.68  christos 	}
    801    1.1       cgd 	*varlist.lastp = NULL;
    802   1.68  christos 
    803    1.1       cgd 	argc = 0;
    804    1.1       cgd 	for (sp = arglist.list ; sp ; sp = sp->next)
    805    1.1       cgd 		argc++;
    806    1.1       cgd 	argv = stalloc(sizeof (char *) * (argc + 1));
    807    1.7       jtc 
    808    1.7       jtc 	for (sp = arglist.list ; sp ; sp = sp->next) {
    809    1.7       jtc 		TRACE(("evalcommand arg: %s\n", sp->text));
    810    1.1       cgd 		*argv++ = sp->text;
    811    1.7       jtc 	}
    812    1.1       cgd 	*argv = NULL;
    813    1.1       cgd 	lastarg = NULL;
    814    1.1       cgd 	if (iflag && funcnest == 0 && argc > 0)
    815    1.1       cgd 		lastarg = argv[-1];
    816    1.1       cgd 	argv -= argc;
    817    1.1       cgd 
    818    1.1       cgd 	/* Print the command if xflag is set. */
    819    1.1       cgd 	if (xflag) {
    820   1.70       dsl 		char sep = 0;
    821   1.70       dsl 		out2str(ps4val());
    822    1.1       cgd 		for (sp = varlist.list ; sp ; sp = sp->next) {
    823  1.116  christos 			char *p;
    824  1.116  christos 
    825   1.70       dsl 			if (sep != 0)
    826   1.70       dsl 				outc(sep, &errout);
    827  1.116  christos 
    828  1.116  christos 			/*
    829  1.116  christos 			 * The "var=" part should not be quoted, regardless
    830  1.116  christos 			 * of the value, or it would not represent an
    831  1.116  christos 			 * assignment, but rather a command
    832  1.116  christos 			 */
    833  1.116  christos 			p = strchr(sp->text, '=');
    834  1.116  christos 			if (p != NULL) {
    835  1.116  christos 				*p = '\0';	/*XXX*/
    836  1.116  christos 				out2shstr(sp->text);
    837  1.116  christos 				out2c('=');
    838  1.116  christos 				*p++ = '=';	/*XXX*/
    839  1.116  christos 			} else
    840  1.116  christos 				p = sp->text;
    841  1.116  christos 			out2shstr(p);
    842   1.70       dsl 			sep = ' ';
    843    1.1       cgd 		}
    844    1.1       cgd 		for (sp = arglist.list ; sp ; sp = sp->next) {
    845   1.70       dsl 			if (sep != 0)
    846   1.70       dsl 				outc(sep, &errout);
    847   1.94  christos 			out2shstr(sp->text);
    848   1.70       dsl 			sep = ' ';
    849    1.1       cgd 		}
    850    1.1       cgd 		outc('\n', &errout);
    851    1.1       cgd 		flushout(&errout);
    852    1.1       cgd 	}
    853    1.1       cgd 
    854    1.1       cgd 	/* Now locate the command. */
    855    1.1       cgd 	if (argc == 0) {
    856   1.70       dsl 		cmdentry.cmdtype = CMDSPLBLTIN;
    857   1.68  christos 		cmdentry.u.bltin = bltincmd;
    858    1.1       cgd 	} else {
    859   1.26  christos 		static const char PATH[] = "PATH=";
    860   1.70       dsl 		int cmd_flags = DO_ERR;
    861   1.26  christos 
    862   1.26  christos 		/*
    863   1.26  christos 		 * Modify the command lookup path, if a PATH= assignment
    864   1.26  christos 		 * is present
    865   1.26  christos 		 */
    866   1.70       dsl 		for (sp = varlist.list; sp; sp = sp->next)
    867   1.26  christos 			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
    868   1.26  christos 				path = sp->text + sizeof(PATH) - 1;
    869   1.26  christos 
    870   1.70       dsl 		do {
    871   1.70       dsl 			int argsused, use_syspath;
    872   1.70       dsl 			find_command(argv[0], &cmdentry, cmd_flags, path);
    873   1.70       dsl 			if (cmdentry.cmdtype == CMDUNKNOWN) {
    874   1.68  christos 				exitstatus = 127;
    875   1.68  christos 				flushout(&errout);
    876   1.70       dsl 				goto out;
    877    1.1       cgd 			}
    878   1.70       dsl 
    879   1.70       dsl 			/* implement the 'command' builtin here */
    880   1.70       dsl 			if (cmdentry.cmdtype != CMDBUILTIN ||
    881   1.70       dsl 			    cmdentry.u.bltin != bltincmd)
    882   1.70       dsl 				break;
    883   1.70       dsl 			cmd_flags |= DO_NOFUNC;
    884   1.70       dsl 			argsused = parse_command_args(argc, argv, &use_syspath);
    885   1.70       dsl 			if (argsused == 0) {
    886   1.70       dsl 				/* use 'type' builting to display info */
    887   1.70       dsl 				cmdentry.u.bltin = typecmd;
    888   1.70       dsl 				break;
    889   1.70       dsl 			}
    890   1.70       dsl 			argc -= argsused;
    891   1.70       dsl 			argv += argsused;
    892   1.70       dsl 			if (use_syspath)
    893   1.70       dsl 				path = syspath() + 5;
    894   1.70       dsl 		} while (argc != 0);
    895   1.70       dsl 		if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
    896   1.70       dsl 			/* posix mandates that 'command <splbltin>' act as if
    897   1.70       dsl 			   <splbltin> was a normal builtin */
    898   1.70       dsl 			cmdentry.cmdtype = CMDBUILTIN;
    899    1.1       cgd 	}
    900    1.1       cgd 
    901    1.1       cgd 	/* Fork off a child process if necessary. */
    902   1.98  christos 	if (cmd->ncmd.backgnd || (trap[0] && (flags & EV_EXIT) != 0)
    903   1.96  christos 	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
    904   1.21  christos 	 || ((flags & EV_BACKCMD) != 0
    905   1.68  christos 	    && ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN)
    906   1.68  christos 		 || cmdentry.u.bltin == dotcmd
    907   1.68  christos 		 || cmdentry.u.bltin == evalcmd))) {
    908   1.62  christos 		INTOFF;
    909    1.1       cgd 		jp = makejob(cmd, 1);
    910    1.1       cgd 		mode = cmd->ncmd.backgnd;
    911  1.125       kre 		if (mode)
    912  1.125       kre 			flags &= ~EV_MORE;
    913    1.1       cgd 		if (flags & EV_BACKCMD) {
    914    1.1       cgd 			mode = FORK_NOJOB;
    915   1.76       dsl 			if (sh_pipe(pip) < 0)
    916    1.1       cgd 				error("Pipe call failed");
    917    1.1       cgd 		}
    918   1.61  christos #ifdef DO_SHAREDVFORK
    919   1.61  christos 		/* It is essential that if DO_SHAREDVFORK is defined that the
    920   1.61  christos 		 * child's address space is actually shared with the parent as
    921   1.61  christos 		 * we rely on this.
    922   1.61  christos 		 */
    923  1.110  christos 		if (usefork == 0 && cmdentry.cmdtype == CMDNORMAL) {
    924   1.61  christos 			pid_t	pid;
    925  1.108  christos 			int serrno;
    926   1.61  christos 
    927   1.61  christos 			savelocalvars = localvars;
    928   1.61  christos 			localvars = NULL;
    929   1.61  christos 			vforked = 1;
    930   1.61  christos 			switch (pid = vfork()) {
    931   1.61  christos 			case -1:
    932  1.108  christos 				serrno = errno;
    933  1.108  christos 				TRACE(("Vfork failed, errno=%d\n", serrno));
    934   1.61  christos 				INTON;
    935  1.108  christos 				error("Cannot vfork (%s)", strerror(serrno));
    936   1.61  christos 				break;
    937   1.61  christos 			case 0:
    938   1.61  christos 				/* Make sure that exceptions only unwind to
    939   1.61  christos 				 * after the vfork(2)
    940   1.61  christos 				 */
    941   1.61  christos 				if (setjmp(jmploc.loc)) {
    942   1.61  christos 					if (exception == EXSHELLPROC) {
    943   1.61  christos 						/* We can't progress with the vfork,
    944   1.61  christos 						 * so, set vforked = 2 so the parent
    945   1.61  christos 						 * knows, and _exit();
    946   1.61  christos 						 */
    947   1.61  christos 						vforked = 2;
    948   1.61  christos 						_exit(0);
    949   1.61  christos 					} else {
    950   1.61  christos 						_exit(exerrno);
    951   1.61  christos 					}
    952   1.61  christos 				}
    953   1.61  christos 				savehandler = handler;
    954   1.61  christos 				handler = &jmploc;
    955   1.70       dsl 				listmklocal(varlist.list, VEXPORT | VNOFUNC);
    956   1.65  christos 				forkchild(jp, cmd, mode, vforked);
    957   1.61  christos 				break;
    958   1.61  christos 			default:
    959   1.61  christos 				handler = savehandler;	/* restore from vfork(2) */
    960   1.61  christos 				poplocalvars();
    961   1.61  christos 				localvars = savelocalvars;
    962   1.61  christos 				if (vforked == 2) {
    963   1.61  christos 					vforked = 0;
    964   1.61  christos 
    965   1.61  christos 					(void)waitpid(pid, NULL, 0);
    966   1.61  christos 					/* We need to progress in a normal fork fashion */
    967   1.61  christos 					goto normal_fork;
    968   1.61  christos 				}
    969   1.61  christos 				vforked = 0;
    970   1.65  christos 				forkparent(jp, cmd, mode, pid);
    971   1.61  christos 				goto parent;
    972   1.61  christos 			}
    973   1.61  christos 		} else {
    974   1.61  christos normal_fork:
    975   1.61  christos #endif
    976   1.65  christos 			if (forkshell(jp, cmd, mode) != 0)
    977   1.61  christos 				goto parent;	/* at end of routine */
    978   1.66  christos 			FORCEINTON;
    979   1.61  christos #ifdef DO_SHAREDVFORK
    980   1.61  christos 		}
    981   1.61  christos #endif
    982    1.1       cgd 		if (flags & EV_BACKCMD) {
    983   1.61  christos 			if (!vforked) {
    984   1.61  christos 				FORCEINTON;
    985   1.61  christos 			}
    986    1.1       cgd 			close(pip[0]);
    987  1.120  christos 			movefd(pip[1], 1);
    988    1.1       cgd 		}
    989    1.1       cgd 		flags |= EV_EXIT;
    990    1.1       cgd 	}
    991    1.1       cgd 
    992    1.1       cgd 	/* This is the child process if a fork occurred. */
    993    1.1       cgd 	/* Execute the command. */
    994   1.70       dsl 	switch (cmdentry.cmdtype) {
    995   1.70       dsl 	case CMDFUNCTION:
    996   1.31  christos #ifdef DEBUG
    997    1.1       cgd 		trputs("Shell function:  ");  trargs(argv);
    998   1.31  christos #endif
    999  1.125       kre 		redirect(cmd->ncmd.redirect, flags & EV_MORE ? REDIR_PUSH : 0);
   1000    1.1       cgd 		saveparam = shellparam;
   1001    1.1       cgd 		shellparam.malloc = 0;
   1002   1.32  christos 		shellparam.reset = 1;
   1003    1.1       cgd 		shellparam.nparam = argc - 1;
   1004    1.1       cgd 		shellparam.p = argv + 1;
   1005    1.1       cgd 		shellparam.optnext = NULL;
   1006    1.1       cgd 		INTOFF;
   1007    1.1       cgd 		savelocalvars = localvars;
   1008    1.1       cgd 		localvars = NULL;
   1009    1.1       cgd 		INTON;
   1010    1.1       cgd 		if (setjmp(jmploc.loc)) {
   1011   1.47  christos 			if (exception == EXSHELLPROC) {
   1012   1.47  christos 				freeparam((volatile struct shparam *)
   1013   1.47  christos 				    &saveparam);
   1014   1.47  christos 			} else {
   1015    1.1       cgd 				freeparam(&shellparam);
   1016    1.1       cgd 				shellparam = saveparam;
   1017    1.1       cgd 			}
   1018    1.1       cgd 			poplocalvars();
   1019    1.1       cgd 			localvars = savelocalvars;
   1020    1.1       cgd 			handler = savehandler;
   1021    1.1       cgd 			longjmp(handler->loc, 1);
   1022    1.1       cgd 		}
   1023    1.1       cgd 		savehandler = handler;
   1024    1.1       cgd 		handler = &jmploc;
   1025  1.106  christos 		listmklocal(varlist.list, VEXPORT);
   1026   1.70       dsl 		/* stop shell blowing its stack */
   1027   1.70       dsl 		if (++funcnest > 1000)
   1028   1.70       dsl 			error("too many nested function calls");
   1029   1.65  christos 		evaltree(cmdentry.u.func, flags & EV_TESTED);
   1030    1.1       cgd 		funcnest--;
   1031    1.1       cgd 		INTOFF;
   1032    1.1       cgd 		poplocalvars();
   1033    1.1       cgd 		localvars = savelocalvars;
   1034    1.1       cgd 		freeparam(&shellparam);
   1035    1.1       cgd 		shellparam = saveparam;
   1036    1.1       cgd 		handler = savehandler;
   1037  1.125       kre 		if (flags & EV_MORE)
   1038  1.125       kre 			popredir();
   1039    1.1       cgd 		INTON;
   1040    1.1       cgd 		if (evalskip == SKIPFUNC) {
   1041  1.109  christos 			evalskip = SKIPNONE;
   1042    1.1       cgd 			skipcount = 0;
   1043    1.1       cgd 		}
   1044    1.1       cgd 		if (flags & EV_EXIT)
   1045    1.1       cgd 			exitshell(exitstatus);
   1046   1.70       dsl 		break;
   1047   1.70       dsl 
   1048   1.70       dsl 	case CMDBUILTIN:
   1049   1.70       dsl 	case CMDSPLBLTIN:
   1050   1.31  christos #ifdef DEBUG
   1051    1.1       cgd 		trputs("builtin command:  ");  trargs(argv);
   1052   1.31  christos #endif
   1053   1.68  christos 		mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
   1054    1.1       cgd 		if (flags == EV_BACKCMD) {
   1055    1.1       cgd 			memout.nleft = 0;
   1056    1.1       cgd 			memout.nextc = memout.buf;
   1057    1.1       cgd 			memout.bufsize = 64;
   1058    1.1       cgd 			mode |= REDIR_BACKQ;
   1059    1.1       cgd 		}
   1060   1.70       dsl 		e = -1;
   1061   1.68  christos 		savehandler = handler;
   1062   1.81       dsl 		savecmdname = commandname;
   1063   1.68  christos 		handler = &jmploc;
   1064  1.104     joerg 		temp_path = 0;
   1065   1.68  christos 		if (!setjmp(jmploc.loc)) {
   1066   1.70       dsl 			/* We need to ensure the command hash table isn't
   1067  1.130  christos 			 * corrupted by temporary PATH assignments.
   1068   1.70       dsl 			 * However we must ensure the 'local' command works!
   1069   1.70       dsl 			 */
   1070   1.70       dsl 			if (path != pathval() && (cmdentry.u.bltin == hashcmd ||
   1071   1.70       dsl 			    cmdentry.u.bltin == typecmd)) {
   1072   1.70       dsl 				savelocalvars = localvars;
   1073   1.70       dsl 				localvars = 0;
   1074  1.104     joerg 				temp_path = 1;
   1075   1.70       dsl 				mklocal(path - 5 /* PATH= */, 0);
   1076  1.104     joerg 			}
   1077   1.68  christos 			redirect(cmd->ncmd.redirect, mode);
   1078   1.68  christos 
   1079   1.68  christos 			/* exec is a special builtin, but needs this list... */
   1080   1.68  christos 			cmdenviron = varlist.list;
   1081   1.68  christos 			/* we must check 'readonly' flag for all builtins */
   1082   1.68  christos 			listsetvar(varlist.list,
   1083   1.68  christos 				cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
   1084   1.68  christos 			commandname = argv[0];
   1085   1.68  christos 			/* initialize nextopt */
   1086   1.68  christos 			argptr = argv + 1;
   1087   1.68  christos 			optptr = NULL;
   1088   1.68  christos 			/* and getopt */
   1089   1.68  christos 			optreset = 1;
   1090   1.68  christos 			optind = 1;
   1091  1.103  christos 			builtin_flags = flags;
   1092   1.68  christos 			exitstatus = cmdentry.u.bltin(argc, argv);
   1093   1.68  christos 		} else {
   1094    1.1       cgd 			e = exception;
   1095   1.70       dsl 			exitstatus = e == EXINT ? SIGINT + 128 :
   1096   1.68  christos 					e == EXEXEC ? exerrno : 2;
   1097    1.1       cgd 		}
   1098   1.70       dsl 		handler = savehandler;
   1099    1.1       cgd 		flushall();
   1100    1.1       cgd 		out1 = &output;
   1101    1.1       cgd 		out2 = &errout;
   1102    1.1       cgd 		freestdout();
   1103   1.70       dsl 		if (temp_path) {
   1104   1.70       dsl 			poplocalvars();
   1105   1.70       dsl 			localvars = savelocalvars;
   1106   1.70       dsl 		}
   1107   1.39   thorpej 		cmdenviron = NULL;
   1108    1.1       cgd 		if (e != EXSHELLPROC) {
   1109    1.1       cgd 			commandname = savecmdname;
   1110   1.44   mycroft 			if (flags & EV_EXIT)
   1111    1.1       cgd 				exitshell(exitstatus);
   1112    1.1       cgd 		}
   1113    1.1       cgd 		if (e != -1) {
   1114   1.31  christos 			if ((e != EXERROR && e != EXEXEC)
   1115   1.70       dsl 			    || cmdentry.cmdtype == CMDSPLBLTIN)
   1116    1.1       cgd 				exraise(e);
   1117    1.1       cgd 			FORCEINTON;
   1118    1.1       cgd 		}
   1119   1.68  christos 		if (cmdentry.u.bltin != execcmd)
   1120    1.1       cgd 			popredir();
   1121    1.1       cgd 		if (flags == EV_BACKCMD) {
   1122    1.1       cgd 			backcmd->buf = memout.buf;
   1123    1.1       cgd 			backcmd->nleft = memout.nextc - memout.buf;
   1124    1.1       cgd 			memout.buf = NULL;
   1125    1.1       cgd 		}
   1126   1.70       dsl 		break;
   1127   1.70       dsl 
   1128   1.70       dsl 	default:
   1129   1.31  christos #ifdef DEBUG
   1130    1.1       cgd 		trputs("normal command:  ");  trargs(argv);
   1131   1.31  christos #endif
   1132  1.117  christos 		redirect(cmd->ncmd.redirect,
   1133  1.117  christos 		    (vforked ? REDIR_VFORK : 0) | REDIR_KEEP);
   1134   1.61  christos 		if (!vforked)
   1135   1.61  christos 			for (sp = varlist.list ; sp ; sp = sp->next)
   1136   1.61  christos 				setvareq(sp->text, VEXPORT|VSTACK);
   1137    1.1       cgd 		envp = environment();
   1138   1.70       dsl 		shellexec(argv, envp, path, cmdentry.u.index, vforked);
   1139   1.70       dsl 		break;
   1140    1.1       cgd 	}
   1141    1.1       cgd 	goto out;
   1142    1.1       cgd 
   1143    1.1       cgd parent:	/* parent process gets here (if we forked) */
   1144  1.113  christos 	exitstatus = 0;		/* if not altered just below */
   1145   1.68  christos 	if (mode == FORK_FG) {	/* argument to fork */
   1146    1.1       cgd 		exitstatus = waitforjob(jp);
   1147   1.68  christos 	} else if (mode == FORK_NOJOB) {
   1148    1.1       cgd 		backcmd->fd = pip[0];
   1149    1.1       cgd 		close(pip[1]);
   1150    1.1       cgd 		backcmd->jp = jp;
   1151    1.1       cgd 	}
   1152   1.66  christos 	FORCEINTON;
   1153    1.1       cgd 
   1154    1.1       cgd out:
   1155    1.1       cgd 	if (lastarg)
   1156  1.132       kre 		/* implement $_ for whatever use that really is */
   1157    1.1       cgd 		setvar("_", lastarg, 0);
   1158    1.1       cgd 	popstackmark(&smark);
   1159    1.1       cgd }
   1160    1.1       cgd 
   1161    1.1       cgd 
   1162    1.1       cgd /*
   1163    1.1       cgd  * Search for a command.  This is called before we fork so that the
   1164    1.1       cgd  * location of the command will be available in the parent as well as
   1165    1.1       cgd  * the child.  The check for "goodname" is an overly conservative
   1166    1.1       cgd  * check that the name will not be subject to expansion.
   1167    1.1       cgd  */
   1168    1.1       cgd 
   1169    1.1       cgd STATIC void
   1170   1.68  christos prehash(union node *n)
   1171   1.22  christos {
   1172    1.1       cgd 	struct cmdentry entry;
   1173    1.1       cgd 
   1174   1.86  christos 	if (n && n->type == NCMD && n->ncmd.args)
   1175   1.15   mycroft 		if (goodname(n->ncmd.args->narg.text))
   1176   1.26  christos 			find_command(n->ncmd.args->narg.text, &entry, 0,
   1177   1.26  christos 				     pathval());
   1178    1.1       cgd }
   1179    1.1       cgd 
   1180  1.122       kre int
   1181  1.109  christos in_function(void)
   1182  1.109  christos {
   1183  1.109  christos 	return funcnest;
   1184  1.109  christos }
   1185    1.1       cgd 
   1186  1.122       kre enum skipstate
   1187  1.109  christos current_skipstate(void)
   1188  1.109  christos {
   1189  1.109  christos 	return evalskip;
   1190  1.109  christos }
   1191  1.109  christos 
   1192  1.122       kre void
   1193  1.109  christos stop_skipping(void)
   1194  1.109  christos {
   1195  1.109  christos 	evalskip = SKIPNONE;
   1196  1.109  christos 	skipcount = 0;
   1197  1.109  christos }
   1198    1.1       cgd 
   1199    1.1       cgd /*
   1200    1.1       cgd  * Builtin commands.  Builtin commands whose functions are closely
   1201    1.1       cgd  * tied to evaluation are implemented here.
   1202    1.1       cgd  */
   1203    1.1       cgd 
   1204    1.1       cgd /*
   1205   1.70       dsl  * No command given.
   1206    1.1       cgd  */
   1207    1.1       cgd 
   1208   1.16       cgd int
   1209   1.68  christos bltincmd(int argc, char **argv)
   1210   1.16       cgd {
   1211   1.31  christos 	/*
   1212   1.22  christos 	 * Preserve exitstatus of a previous possible redirection
   1213   1.31  christos 	 * as POSIX mandates
   1214   1.22  christos 	 */
   1215   1.68  christos 	return back_exitstatus;
   1216    1.1       cgd }
   1217    1.1       cgd 
   1218    1.1       cgd 
   1219    1.1       cgd /*
   1220    1.1       cgd  * Handle break and continue commands.  Break, continue, and return are
   1221    1.1       cgd  * all handled by setting the evalskip flag.  The evaluation routines
   1222    1.1       cgd  * above all check this flag, and if it is set they start skipping
   1223    1.1       cgd  * commands rather than executing them.  The variable skipcount is
   1224    1.1       cgd  * the number of loops to break/continue, or the number of function
   1225    1.1       cgd  * levels to return.  (The latter is always 1.)  It should probably
   1226    1.1       cgd  * be an error to break out of more loops than exist, but it isn't
   1227    1.1       cgd  * in the standard shell so we don't make it one here.
   1228    1.1       cgd  */
   1229    1.1       cgd 
   1230   1.16       cgd int
   1231   1.68  christos breakcmd(int argc, char **argv)
   1232   1.16       cgd {
   1233   1.30  christos 	int n = argc > 1 ? number(argv[1]) : 1;
   1234    1.1       cgd 
   1235  1.135       kre 	if (n <= 0)
   1236  1.135       kre 		error("invalid count: %d", n);
   1237    1.1       cgd 	if (n > loopnest)
   1238    1.1       cgd 		n = loopnest;
   1239    1.1       cgd 	if (n > 0) {
   1240    1.1       cgd 		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
   1241    1.1       cgd 		skipcount = n;
   1242    1.1       cgd 	}
   1243    1.1       cgd 	return 0;
   1244    1.1       cgd }
   1245    1.1       cgd 
   1246  1.109  christos int
   1247  1.109  christos dotcmd(int argc, char **argv)
   1248  1.109  christos {
   1249  1.109  christos 	exitstatus = 0;
   1250  1.109  christos 
   1251  1.109  christos 	if (argc >= 2) {		/* That's what SVR2 does */
   1252  1.109  christos 		char *fullname;
   1253  1.109  christos 		/*
   1254  1.109  christos 		 * dot_funcnest needs to be 0 when not in a dotcmd, so it
   1255  1.109  christos 		 * cannot be restored with (funcnest + 1).
   1256  1.109  christos 		 */
   1257  1.109  christos 		int dot_funcnest_old;
   1258  1.109  christos 		struct stackmark smark;
   1259  1.109  christos 
   1260  1.109  christos 		setstackmark(&smark);
   1261  1.109  christos 		fullname = find_dot_file(argv[1]);
   1262  1.109  christos 		setinputfile(fullname, 1);
   1263  1.109  christos 		commandname = fullname;
   1264  1.109  christos 		dot_funcnest_old = dot_funcnest;
   1265  1.109  christos 		dot_funcnest = funcnest + 1;
   1266  1.109  christos 		cmdloop(0);
   1267  1.109  christos 		dot_funcnest = dot_funcnest_old;
   1268  1.109  christos 		popfile();
   1269  1.109  christos 		popstackmark(&smark);
   1270  1.109  christos 	}
   1271  1.109  christos 	return exitstatus;
   1272  1.109  christos }
   1273  1.109  christos 
   1274  1.109  christos /*
   1275  1.109  christos  * Take commands from a file.  To be compatible we should do a path
   1276  1.109  christos  * search for the file, which is necessary to find sub-commands.
   1277  1.109  christos  */
   1278  1.109  christos 
   1279  1.109  christos STATIC char *
   1280  1.109  christos find_dot_file(char *basename)
   1281  1.109  christos {
   1282  1.109  christos 	char *fullname;
   1283  1.109  christos 	const char *path = pathval();
   1284  1.109  christos 	struct stat statb;
   1285  1.109  christos 
   1286  1.109  christos 	/* don't try this for absolute or relative paths */
   1287  1.121       kre 	if (strchr(basename, '/')) {
   1288  1.121       kre 		if (stat(basename, &statb) == 0) {
   1289  1.128       kre 			if (S_ISDIR(statb.st_mode))
   1290  1.128       kre 				error("%s: is a directory", basename);
   1291  1.128       kre 			if (S_ISBLK(statb.st_mode))
   1292  1.128       kre 				error("%s: is a block device", basename);
   1293  1.128       kre 			return basename;
   1294  1.121       kre 		}
   1295  1.121       kre 	} else while ((fullname = padvance(&path, basename)) != NULL) {
   1296  1.128       kre 		if ((stat(fullname, &statb) == 0)) {
   1297  1.128       kre 			/* weird format is to ease future code... */
   1298  1.128       kre 			if (S_ISDIR(statb.st_mode) || S_ISBLK(statb.st_mode))
   1299  1.128       kre 				;
   1300  1.128       kre #if notyet
   1301  1.128       kre 			else if (unreadable()) {
   1302  1.128       kre 				/*
   1303  1.128       kre 				 * testing this via st_mode is ugly to get
   1304  1.128       kre 				 * correct (and would ignore ACLs).
   1305  1.128       kre 				 * better way is just to open the file.
   1306  1.128       kre 				 * But doing that here would (currently)
   1307  1.128       kre 				 * mean opening the file twice, which
   1308  1.128       kre 				 * might not be safe.  So, defer this
   1309  1.128       kre 				 * test until code is restructures so
   1310  1.128       kre 				 * we can return a fd.   Then we also
   1311  1.128       kre 				 * get to fix the mem leak just below...
   1312  1.128       kre 				 */
   1313  1.128       kre 			}
   1314  1.128       kre #endif
   1315  1.128       kre 			else {
   1316  1.128       kre 				/*
   1317  1.128       kre 				 * Don't bother freeing here, since
   1318  1.128       kre 				 * it will be freed by the caller.
   1319  1.128       kre 				 * XXX no it won't - a bug for later.
   1320  1.128       kre 				 */
   1321  1.128       kre 				return fullname;
   1322  1.128       kre 			}
   1323  1.109  christos 		}
   1324  1.109  christos 		stunalloc(fullname);
   1325  1.109  christos 	}
   1326  1.109  christos 
   1327  1.109  christos 	/* not found in the PATH */
   1328  1.109  christos 	error("%s: not found", basename);
   1329  1.109  christos 	/* NOTREACHED */
   1330  1.109  christos }
   1331  1.109  christos 
   1332  1.109  christos 
   1333    1.1       cgd 
   1334    1.1       cgd /*
   1335    1.1       cgd  * The return command.
   1336  1.109  christos  *
   1337  1.109  christos  * Quoth the POSIX standard:
   1338  1.109  christos  *   The return utility shall cause the shell to stop executing the current
   1339  1.109  christos  *   function or dot script. If the shell is not currently executing
   1340  1.109  christos  *   a function or dot script, the results are unspecified.
   1341  1.109  christos  *
   1342  1.109  christos  * As for the unspecified part, there seems to be no de-facto standard: bash
   1343  1.109  christos  * ignores the return with a warning, zsh ignores the return in interactive
   1344  1.109  christos  * mode but seems to liken it to exit in a script.  (checked May 2014)
   1345  1.109  christos  *
   1346  1.109  christos  * We choose to silently ignore the return.  Older versions of this shell
   1347  1.109  christos  * set evalskip to SKIPFILE causing the shell to (indirectly) exit.  This
   1348  1.109  christos  * had at least the problem of circumventing the check for stopped jobs,
   1349  1.109  christos  * which would occur for exit or ^D.
   1350    1.1       cgd  */
   1351    1.1       cgd 
   1352   1.16       cgd int
   1353   1.68  christos returncmd(int argc, char **argv)
   1354   1.16       cgd {
   1355   1.68  christos 	int ret = argc > 1 ? number(argv[1]) : exitstatus;
   1356    1.1       cgd 
   1357  1.109  christos 	if ((dot_funcnest == 0 && funcnest)
   1358  1.109  christos 	    || (dot_funcnest > 0 && funcnest - (dot_funcnest - 1) > 0)) {
   1359    1.1       cgd 		evalskip = SKIPFUNC;
   1360    1.1       cgd 		skipcount = 1;
   1361  1.109  christos 	} else if (dot_funcnest > 0) {
   1362   1.27  christos 		evalskip = SKIPFILE;
   1363   1.27  christos 		skipcount = 1;
   1364  1.109  christos 	} else {
   1365  1.109  christos 		/* XXX: should a warning be issued? */
   1366  1.109  christos 		ret = 0;
   1367    1.1       cgd 	}
   1368  1.109  christos 
   1369  1.109  christos 	return ret;
   1370    1.1       cgd }
   1371    1.1       cgd 
   1372    1.1       cgd 
   1373   1.16       cgd int
   1374   1.68  christos falsecmd(int argc, char **argv)
   1375   1.16       cgd {
   1376    1.8       jtc 	return 1;
   1377    1.8       jtc }
   1378    1.8       jtc 
   1379    1.8       jtc 
   1380   1.16       cgd int
   1381   1.68  christos truecmd(int argc, char **argv)
   1382   1.16       cgd {
   1383    1.1       cgd 	return 0;
   1384    1.1       cgd }
   1385    1.1       cgd 
   1386    1.1       cgd 
   1387   1.16       cgd int
   1388   1.68  christos execcmd(int argc, char **argv)
   1389   1.16       cgd {
   1390    1.1       cgd 	if (argc > 1) {
   1391   1.20  christos 		struct strlist *sp;
   1392   1.20  christos 
   1393    1.1       cgd 		iflag = 0;		/* exit on error */
   1394    1.7       jtc 		mflag = 0;
   1395    1.7       jtc 		optschanged();
   1396   1.70       dsl 		for (sp = cmdenviron; sp; sp = sp->next)
   1397   1.20  christos 			setvareq(sp->text, VEXPORT|VSTACK);
   1398   1.61  christos 		shellexec(argv + 1, environment(), pathval(), 0, 0);
   1399    1.1       cgd 	}
   1400   1.68  christos 	return 0;
   1401   1.68  christos }
   1402   1.68  christos 
   1403   1.68  christos static int
   1404   1.73    itojun conv_time(clock_t ticks, char *seconds, size_t l)
   1405   1.68  christos {
   1406   1.68  christos 	static clock_t tpm = 0;
   1407   1.68  christos 	clock_t mins;
   1408   1.68  christos 	int i;
   1409   1.68  christos 
   1410   1.68  christos 	if (!tpm)
   1411   1.68  christos 		tpm = sysconf(_SC_CLK_TCK) * 60;
   1412   1.68  christos 
   1413   1.68  christos 	mins = ticks / tpm;
   1414   1.73    itojun 	snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
   1415   1.68  christos 
   1416   1.68  christos 	if (seconds[0] == '6' && seconds[1] == '0') {
   1417   1.68  christos 		/* 59.99995 got rounded up... */
   1418   1.68  christos 		mins++;
   1419   1.73    itojun 		strlcpy(seconds, "0.0", l);
   1420   1.68  christos 		return mins;
   1421   1.68  christos 	}
   1422   1.68  christos 
   1423   1.68  christos 	/* suppress trailing zeros */
   1424   1.68  christos 	i = strlen(seconds) - 1;
   1425   1.68  christos 	for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
   1426   1.68  christos 		seconds[i] = 0;
   1427   1.68  christos 	return mins;
   1428   1.68  christos }
   1429   1.68  christos 
   1430   1.68  christos int
   1431   1.68  christos timescmd(int argc, char **argv)
   1432   1.68  christos {
   1433   1.68  christos 	struct tms tms;
   1434   1.68  christos 	int u, s, cu, cs;
   1435   1.68  christos 	char us[8], ss[8], cus[8], css[8];
   1436   1.68  christos 
   1437   1.68  christos 	nextopt("");
   1438   1.68  christos 
   1439   1.68  christos 	times(&tms);
   1440   1.68  christos 
   1441   1.73    itojun 	u = conv_time(tms.tms_utime, us, sizeof(us));
   1442   1.73    itojun 	s = conv_time(tms.tms_stime, ss, sizeof(ss));
   1443   1.73    itojun 	cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
   1444   1.73    itojun 	cs = conv_time(tms.tms_cstime, css, sizeof(css));
   1445   1.68  christos 
   1446   1.68  christos 	outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
   1447   1.68  christos 		u, us, s, ss, cu, cus, cs, css);
   1448   1.68  christos 
   1449    1.1       cgd 	return 0;
   1450    1.1       cgd }
   1451