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