2006-10-16 04:36:19 +04:00
|
|
|
/* $NetBSD: eval.c,v 1.88 2006/10/16 00:36:19 christos Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-11 21:09:42 +04:00
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:01:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1995-06-09 05:53:44 +04:00
|
|
|
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
2006-10-16 04:36:19 +04:00
|
|
|
__RCSID("$NetBSD: eval.c,v 1.88 2006/10/16 00:36:19 christos Exp $");
|
1995-05-15 07:29:54 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2003-01-22 23:36:03 +03:00
|
|
|
#include <stdlib.h>
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <signal.h>
|
2002-11-26 00:55:58 +03:00
|
|
|
#include <stdio.h>
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <unistd.h>
|
2004-04-30 10:27:59 +04:00
|
|
|
#include <sys/fcntl.h>
|
2002-11-25 01:35:38 +03:00
|
|
|
#include <sys/times.h>
|
2003-01-23 06:33:16 +03:00
|
|
|
#include <sys/param.h>
|
2002-09-27 22:56:50 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2003-01-22 23:36:03 +03:00
|
|
|
#include <sys/sysctl.h>
|
2002-09-27 22:56:50 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Evaluate a command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "nodes.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "expand.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
#include "eval.h"
|
|
|
|
#include "builtins.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "exec.h"
|
|
|
|
#include "redir.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "trap.h"
|
|
|
|
#include "var.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
1995-05-12 01:28:33 +04:00
|
|
|
#include "show.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "mystring.h"
|
2002-09-28 02:56:24 +04:00
|
|
|
#include "main.h"
|
1997-03-14 04:42:18 +03:00
|
|
|
#ifndef SMALL
|
1994-05-11 21:09:42 +04:00
|
|
|
#include "myhistedit.h"
|
1994-05-21 05:42:30 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* flags in argument to evaltree */
|
|
|
|
#define EV_EXIT 01 /* exit after evaluating tree */
|
|
|
|
#define EV_TESTED 02 /* exit status is checked; ignore -e flag */
|
|
|
|
#define EV_BACKCMD 04 /* command executing within back quotes */
|
|
|
|
|
2001-02-04 22:52:06 +03:00
|
|
|
int evalskip; /* set if we are skipping commands */
|
1993-03-21 12:45:37 +03:00
|
|
|
STATIC int skipcount; /* number of levels to skip */
|
|
|
|
MKINIT int loopnest; /* current loop nesting level */
|
|
|
|
int funcnest; /* depth of function calls */
|
|
|
|
|
|
|
|
|
|
|
|
char *commandname;
|
|
|
|
struct strlist *cmdenviron;
|
|
|
|
int exitstatus; /* exit status of last command */
|
2002-11-25 01:35:38 +03:00
|
|
|
int back_exitstatus; /* exit status of backquoted command */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
2002-11-25 01:35:38 +03:00
|
|
|
STATIC void evalloop(union node *, int);
|
|
|
|
STATIC void evalfor(union node *, int);
|
|
|
|
STATIC void evalcase(union node *, int);
|
|
|
|
STATIC void evalsubshell(union node *, int);
|
|
|
|
STATIC void expredir(union node *);
|
|
|
|
STATIC void evalpipe(union node *);
|
|
|
|
STATIC void evalcommand(union node *, int, struct backcmd *);
|
|
|
|
STATIC void prehash(union node *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to reset things after an exception.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef mkinit
|
|
|
|
INCLUDE "eval.h"
|
|
|
|
|
|
|
|
RESET {
|
|
|
|
evalskip = 0;
|
|
|
|
loopnest = 0;
|
|
|
|
funcnest = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SHELLPROC {
|
|
|
|
exitstatus = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-30 10:27:59 +04:00
|
|
|
static int
|
|
|
|
sh_pipe(int fds[2])
|
|
|
|
{
|
|
|
|
int nfd;
|
|
|
|
|
|
|
|
if (pipe(fds))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (fds[0] < 3) {
|
|
|
|
nfd = fcntl(fds[0], F_DUPFD, 3);
|
|
|
|
if (nfd != -1) {
|
|
|
|
close(fds[0]);
|
|
|
|
fds[0] = nfd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fds[1] < 3) {
|
|
|
|
nfd = fcntl(fds[1], F_DUPFD, 3);
|
|
|
|
if (nfd != -1) {
|
|
|
|
close(fds[1]);
|
|
|
|
fds[1] = nfd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The eval commmand.
|
|
|
|
*/
|
|
|
|
|
1994-12-04 10:11:37 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
evalcmd(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char *concat;
|
|
|
|
char **ap;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
p = argv[1];
|
|
|
|
if (argc > 2) {
|
|
|
|
STARTSTACKSTR(concat);
|
|
|
|
ap = argv + 2;
|
|
|
|
for (;;) {
|
|
|
|
while (*p)
|
|
|
|
STPUTC(*p++, concat);
|
|
|
|
if ((p = *ap++) == NULL)
|
|
|
|
break;
|
|
|
|
STPUTC(' ', concat);
|
|
|
|
}
|
|
|
|
STPUTC('\0', concat);
|
|
|
|
p = grabstackstr(concat);
|
|
|
|
}
|
2000-01-28 02:39:38 +03:00
|
|
|
evalstring(p, EV_TESTED);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
return exitstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a command or commands contained in a string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
evalstring(char *s, int flag)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
union node *n;
|
|
|
|
struct stackmark smark;
|
|
|
|
|
|
|
|
setstackmark(&smark);
|
|
|
|
setinputstring(s, 1);
|
2002-09-28 02:56:24 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
while ((n = parsecmd(0)) != NEOF) {
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n, flag);
|
1993-03-21 12:45:37 +03:00
|
|
|
popstackmark(&smark);
|
|
|
|
}
|
|
|
|
popfile();
|
|
|
|
popstackmark(&smark);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluate a parse tree. The value is left in the global variable
|
|
|
|
* exitstatus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
evaltree(union node *n, int flags)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
if (n == NULL) {
|
|
|
|
TRACE(("evaltree(NULL) called\n"));
|
1994-05-11 21:09:42 +04:00
|
|
|
exitstatus = 0;
|
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-03-14 04:42:18 +03:00
|
|
|
#ifndef SMALL
|
1994-05-11 21:09:42 +04:00
|
|
|
displayhist = 1; /* show history substitutions done with fc */
|
1994-05-15 02:20:04 +04:00
|
|
|
#endif
|
2003-01-22 23:36:03 +03:00
|
|
|
TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
|
|
|
|
getpid(), n, n->type, flags));
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (n->type) {
|
|
|
|
case NSEMI:
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nbinary.ch1, flags & EV_TESTED);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (evalskip)
|
|
|
|
goto out;
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nbinary.ch2, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NAND:
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nbinary.ch1, EV_TESTED);
|
2003-01-22 23:36:03 +03:00
|
|
|
if (evalskip || exitstatus != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
goto out;
|
2004-06-30 13:32:38 +04:00
|
|
|
evaltree(n->nbinary.ch2, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NOR:
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nbinary.ch1, EV_TESTED);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (evalskip || exitstatus == 0)
|
|
|
|
goto out;
|
2004-06-30 13:32:38 +04:00
|
|
|
evaltree(n->nbinary.ch2, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NREDIR:
|
|
|
|
expredir(n->nredir.redirect);
|
|
|
|
redirect(n->nredir.redirect, REDIR_PUSH);
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nredir.n, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
popredir();
|
|
|
|
break;
|
|
|
|
case NSUBSHELL:
|
2002-09-28 05:25:01 +04:00
|
|
|
evalsubshell(n, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NBACKGND:
|
2002-09-28 05:25:01 +04:00
|
|
|
evalsubshell(n, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NIF: {
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nif.test, EV_TESTED);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (evalskip)
|
|
|
|
goto out;
|
1996-03-06 00:03:52 +03:00
|
|
|
if (exitstatus == 0)
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nif.ifpart, flags);
|
1995-05-14 08:19:18 +04:00
|
|
|
else if (n->nif.elsepart)
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nif.elsepart, flags);
|
1996-03-06 17:49:29 +03:00
|
|
|
else
|
|
|
|
exitstatus = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NWHILE:
|
|
|
|
case NUNTIL:
|
2002-09-28 05:25:01 +04:00
|
|
|
evalloop(n, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NFOR:
|
2002-09-28 05:25:01 +04:00
|
|
|
evalfor(n, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NCASE:
|
2002-09-28 05:25:01 +04:00
|
|
|
evalcase(n, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NDEFUN:
|
|
|
|
defun(n->narg.text, n->narg.next);
|
|
|
|
exitstatus = 0;
|
|
|
|
break;
|
1994-05-11 21:09:42 +04:00
|
|
|
case NNOT:
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nnot.com, EV_TESTED);
|
1994-05-11 21:09:42 +04:00
|
|
|
exitstatus = !exitstatus;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case NPIPE:
|
2002-09-28 05:25:01 +04:00
|
|
|
evalpipe(n);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NCMD:
|
2002-09-28 05:25:01 +04:00
|
|
|
evalcommand(n, flags, (struct backcmd *)NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
out1fmt("Node type = %d\n", n->type);
|
|
|
|
flushout(&output);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (pendingsigs)
|
|
|
|
dotrap();
|
2002-02-15 00:51:41 +03:00
|
|
|
if ((flags & EV_EXIT) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
exitshell(exitstatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
evalloop(union node *n, int flags)
|
1995-05-14 08:19:18 +04:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int status;
|
|
|
|
|
|
|
|
loopnest++;
|
|
|
|
status = 0;
|
|
|
|
for (;;) {
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nbinary.ch1, EV_TESTED);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (evalskip) {
|
|
|
|
skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
|
|
|
|
evalskip = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (evalskip == SKIPBREAK && --skipcount <= 0)
|
|
|
|
evalskip = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (n->type == NWHILE) {
|
|
|
|
if (exitstatus != 0)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (exitstatus == 0)
|
|
|
|
break;
|
|
|
|
}
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nbinary.ch2, flags & EV_TESTED);
|
1993-03-21 12:45:37 +03:00
|
|
|
status = exitstatus;
|
|
|
|
if (evalskip)
|
|
|
|
goto skipping;
|
|
|
|
}
|
|
|
|
loopnest--;
|
|
|
|
exitstatus = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
evalfor(union node *n, int flags)
|
1995-05-14 08:19:18 +04:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct arglist arglist;
|
|
|
|
union node *argp;
|
|
|
|
struct strlist *sp;
|
|
|
|
struct stackmark smark;
|
2002-11-25 01:35:38 +03:00
|
|
|
int status = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
setstackmark(&smark);
|
|
|
|
arglist.lastp = &arglist.list;
|
|
|
|
for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
|
2004-06-27 02:09:49 +04:00
|
|
|
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (evalskip)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*arglist.lastp = NULL;
|
|
|
|
|
|
|
|
loopnest++;
|
|
|
|
for (sp = arglist.list ; sp ; sp = sp->next) {
|
|
|
|
setvar(n->nfor.var, sp->text, 0);
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nfor.body, flags & EV_TESTED);
|
2002-11-25 01:35:38 +03:00
|
|
|
status = exitstatus;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (evalskip) {
|
|
|
|
if (evalskip == SKIPCONT && --skipcount <= 0) {
|
|
|
|
evalskip = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (evalskip == SKIPBREAK && --skipcount <= 0)
|
|
|
|
evalskip = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loopnest--;
|
2002-11-25 01:35:38 +03:00
|
|
|
exitstatus = status;
|
1993-03-21 12:45:37 +03:00
|
|
|
out:
|
|
|
|
popstackmark(&smark);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
evalcase(union node *n, int flags)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
union node *cp;
|
|
|
|
union node *patp;
|
|
|
|
struct arglist arglist;
|
|
|
|
struct stackmark smark;
|
2002-11-25 01:35:38 +03:00
|
|
|
int status = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
setstackmark(&smark);
|
|
|
|
arglist.lastp = &arglist.list;
|
1994-05-11 21:09:42 +04:00
|
|
|
expandarg(n->ncase.expr, &arglist, EXP_TILDE);
|
1993-03-21 12:45:37 +03:00
|
|
|
for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
|
|
|
|
for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
|
|
|
|
if (casematch(patp, arglist.list->text)) {
|
|
|
|
if (evalskip == 0) {
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(cp->nclist.body, flags);
|
2002-11-25 01:35:38 +03:00
|
|
|
status = exitstatus;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
2002-11-25 01:35:38 +03:00
|
|
|
exitstatus = status;
|
1993-03-21 12:45:37 +03:00
|
|
|
popstackmark(&smark);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Kick off a subshell to evaluate a tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
evalsubshell(union node *n, int flags)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct job *jp;
|
|
|
|
int backgnd = (n->type == NBACKGND);
|
|
|
|
|
|
|
|
expredir(n->nredir.redirect);
|
2002-09-28 01:32:24 +04:00
|
|
|
INTOFF;
|
1993-03-21 12:45:37 +03:00
|
|
|
jp = makejob(n, 1);
|
2005-06-23 03:32:45 +04:00
|
|
|
if (forkshell(jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
|
2002-09-28 01:32:24 +04:00
|
|
|
INTON;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (backgnd)
|
|
|
|
flags &=~ EV_TESTED;
|
|
|
|
redirect(n->nredir.redirect, 0);
|
2002-09-28 02:56:24 +04:00
|
|
|
/* never returns */
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n->nredir.n, flags | EV_EXIT);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2005-06-24 03:05:29 +04:00
|
|
|
if (! backgnd)
|
1993-03-21 12:45:37 +03:00
|
|
|
exitstatus = waitforjob(jp);
|
2002-09-28 01:32:24 +04:00
|
|
|
INTON;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the names of the files in a redirection list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
expredir(union node *n)
|
1995-05-14 08:19:18 +04:00
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
union node *redir;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (redir = n ; redir ; redir = redir->nfile.next) {
|
1994-06-14 09:49:19 +04:00
|
|
|
struct arglist fn;
|
|
|
|
fn.lastp = &fn.list;
|
|
|
|
switch (redir->type) {
|
1999-02-04 19:17:39 +03:00
|
|
|
case NFROMTO:
|
1994-06-14 09:49:19 +04:00
|
|
|
case NFROM:
|
|
|
|
case NTO:
|
2002-05-15 20:33:35 +04:00
|
|
|
case NCLOBBER:
|
1994-06-14 09:49:19 +04:00
|
|
|
case NAPPEND:
|
1994-05-11 21:09:42 +04:00
|
|
|
expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
|
1993-03-21 12:45:37 +03:00
|
|
|
redir->nfile.expfname = fn.list->text;
|
1994-06-14 09:49:19 +04:00
|
|
|
break;
|
|
|
|
case NFROMFD:
|
|
|
|
case NTOFD:
|
|
|
|
if (redir->ndup.vname) {
|
|
|
|
expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
|
|
|
|
fixredir(redir, fn.list->text, 1);
|
|
|
|
}
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluate a pipeline. All the processes in the pipeline are children
|
|
|
|
* of the process creating the pipeline. (This differs from some versions
|
|
|
|
* of the shell, which make the last process in a pipeline the parent
|
|
|
|
* of all the rest.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
evalpipe(union node *n)
|
1995-05-14 08:19:18 +04:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct job *jp;
|
|
|
|
struct nodelist *lp;
|
|
|
|
int pipelen;
|
|
|
|
int prevfd;
|
|
|
|
int pip[2];
|
|
|
|
|
1994-12-23 16:24:39 +03:00
|
|
|
TRACE(("evalpipe(0x%lx) called\n", (long)n));
|
1993-03-21 12:45:37 +03:00
|
|
|
pipelen = 0;
|
|
|
|
for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
|
|
|
|
pipelen++;
|
|
|
|
INTOFF;
|
|
|
|
jp = makejob(n, pipelen);
|
|
|
|
prevfd = -1;
|
|
|
|
for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
|
|
|
|
prehash(lp->n);
|
|
|
|
pip[1] = -1;
|
|
|
|
if (lp->next) {
|
2004-04-30 10:27:59 +04:00
|
|
|
if (sh_pipe(pip) < 0) {
|
2006-05-13 23:47:22 +04:00
|
|
|
if (prevfd >= 0)
|
|
|
|
close(prevfd);
|
1993-03-21 12:45:37 +03:00
|
|
|
error("Pipe call failed");
|
|
|
|
}
|
|
|
|
}
|
2005-06-23 03:32:45 +04:00
|
|
|
if (forkshell(jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
INTON;
|
|
|
|
if (prevfd > 0) {
|
|
|
|
close(0);
|
|
|
|
copyfd(prevfd, 0);
|
|
|
|
close(prevfd);
|
|
|
|
}
|
|
|
|
if (pip[1] >= 0) {
|
|
|
|
close(pip[0]);
|
|
|
|
if (pip[1] != 1) {
|
|
|
|
close(1);
|
|
|
|
copyfd(pip[1], 1);
|
|
|
|
close(pip[1]);
|
|
|
|
}
|
|
|
|
}
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(lp->n, EV_EXIT);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (prevfd >= 0)
|
|
|
|
close(prevfd);
|
|
|
|
prevfd = pip[0];
|
|
|
|
close(pip[1]);
|
|
|
|
}
|
|
|
|
if (n->npipe.backgnd == 0) {
|
|
|
|
exitstatus = waitforjob(jp);
|
|
|
|
TRACE(("evalpipe: job done exit status %d\n", exitstatus));
|
|
|
|
}
|
2002-09-27 21:37:12 +04:00
|
|
|
INTON;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a command inside back quotes. If it's a builtin command, we
|
|
|
|
* want to save its output in a block obtained from malloc. Otherwise
|
|
|
|
* we fork off a subprocess and get the output of the command via a pipe.
|
|
|
|
* Should be called with interrupts off.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
evalbackcmd(union node *n, struct backcmd *result)
|
1995-05-14 08:19:18 +04:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int pip[2];
|
|
|
|
struct job *jp;
|
|
|
|
struct stackmark smark; /* unnecessary */
|
|
|
|
|
|
|
|
setstackmark(&smark);
|
|
|
|
result->fd = -1;
|
|
|
|
result->buf = NULL;
|
|
|
|
result->nleft = 0;
|
|
|
|
result->jp = NULL;
|
1995-05-15 06:47:38 +04:00
|
|
|
if (n == NULL) {
|
1994-05-11 21:09:42 +04:00
|
|
|
goto out;
|
1995-05-15 06:47:38 +04:00
|
|
|
}
|
1999-06-26 20:31:47 +04:00
|
|
|
#ifdef notyet
|
|
|
|
/*
|
|
|
|
* For now we disable executing builtins in the same
|
|
|
|
* context as the shell, because we are not keeping
|
|
|
|
* enough state to recover from changes that are
|
|
|
|
* supposed only to affect subshells. eg. echo "`cd /`"
|
|
|
|
*/
|
1994-05-11 21:09:42 +04:00
|
|
|
if (n->type == NCMD) {
|
1995-05-15 06:47:38 +04:00
|
|
|
exitstatus = oexitstatus;
|
2002-09-28 05:25:01 +04:00
|
|
|
evalcommand(n, EV_BACKCMD, result);
|
1999-06-26 20:31:47 +04:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2002-09-28 01:32:24 +04:00
|
|
|
INTOFF;
|
2004-04-30 10:27:59 +04:00
|
|
|
if (sh_pipe(pip) < 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
error("Pipe call failed");
|
|
|
|
jp = makejob(n, 1);
|
2002-09-28 05:25:01 +04:00
|
|
|
if (forkshell(jp, n, FORK_NOJOB) == 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
FORCEINTON;
|
|
|
|
close(pip[0]);
|
|
|
|
if (pip[1] != 1) {
|
|
|
|
close(1);
|
|
|
|
copyfd(pip[1], 1);
|
|
|
|
close(pip[1]);
|
|
|
|
}
|
2000-01-28 02:39:38 +03:00
|
|
|
eflag = 0;
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(n, EV_EXIT);
|
2002-11-25 01:35:38 +03:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
close(pip[1]);
|
|
|
|
result->fd = pip[0];
|
|
|
|
result->jp = jp;
|
2002-09-28 01:32:24 +04:00
|
|
|
INTON;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
out:
|
1993-03-21 12:45:37 +03:00
|
|
|
popstackmark(&smark);
|
|
|
|
TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
|
|
|
|
result->fd, result->buf, result->nleft, result->jp));
|
|
|
|
}
|
|
|
|
|
2003-01-22 23:36:03 +03:00
|
|
|
static const char *
|
|
|
|
syspath(void)
|
|
|
|
{
|
|
|
|
static char *sys_path = NULL;
|
|
|
|
static int mib[] = {CTL_USER, USER_CS_PATH};
|
2004-10-30 23:29:27 +04:00
|
|
|
static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
|
2003-01-23 17:58:07 +03:00
|
|
|
size_t len;
|
2003-01-22 23:36:03 +03:00
|
|
|
|
|
|
|
if (sys_path == NULL) {
|
|
|
|
if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
|
|
|
|
(sys_path = ckmalloc(len + 5)) != NULL &&
|
|
|
|
sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
|
|
|
|
memcpy(sys_path, "PATH=", 5);
|
|
|
|
} else {
|
|
|
|
ckfree(sys_path);
|
|
|
|
/* something to keep things happy */
|
2004-10-30 23:29:27 +04:00
|
|
|
sys_path = def_path;
|
2003-01-22 23:36:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sys_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_command_args(int argc, char **argv, int *use_syspath)
|
|
|
|
{
|
|
|
|
int sv_argc = argc;
|
|
|
|
char *cp, c;
|
|
|
|
|
|
|
|
*use_syspath = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
argv++;
|
|
|
|
if (--argc == 0)
|
|
|
|
break;
|
|
|
|
cp = *argv;
|
|
|
|
if (*cp++ != '-')
|
|
|
|
break;
|
|
|
|
if (*cp == '-' && cp[1] == 0) {
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while ((c = *cp++)) {
|
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
*use_syspath = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* run 'typecmd' for other options */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sv_argc - argc;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-09-27 22:56:50 +04:00
|
|
|
int vforked = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a simple command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2006-10-16 04:36:19 +04:00
|
|
|
evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stackmark smark;
|
|
|
|
union node *argp;
|
|
|
|
struct arglist arglist;
|
|
|
|
struct arglist varlist;
|
2006-10-16 04:36:19 +04:00
|
|
|
volatile int flags = flgs;
|
|
|
|
char ** volatile argv;
|
|
|
|
volatile int argc;
|
1993-03-21 12:45:37 +03:00
|
|
|
char **envp;
|
|
|
|
int varflag;
|
|
|
|
struct strlist *sp;
|
2006-10-16 04:36:19 +04:00
|
|
|
volatile int mode;
|
1993-03-21 12:45:37 +03:00
|
|
|
int pip[2];
|
|
|
|
struct cmdentry cmdentry;
|
2006-10-16 04:36:19 +04:00
|
|
|
struct job * volatile jp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct jmploc jmploc;
|
2006-03-17 20:20:47 +03:00
|
|
|
struct jmploc *volatile savehandler = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *volatile savecmdname;
|
|
|
|
volatile struct shparam saveparam;
|
|
|
|
struct localvar *volatile savelocalvars;
|
|
|
|
volatile int e;
|
2006-10-16 04:36:19 +04:00
|
|
|
char * volatile lastarg;
|
|
|
|
const char * volatile path = pathval();
|
2005-06-01 19:38:32 +04:00
|
|
|
volatile int temp_path;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-09-27 22:56:50 +04:00
|
|
|
vforked = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
/* First expand the arguments. */
|
2002-09-28 05:25:01 +04:00
|
|
|
TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
|
1993-03-21 12:45:37 +03:00
|
|
|
setstackmark(&smark);
|
2002-11-25 01:35:38 +03:00
|
|
|
back_exitstatus = 0;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
arglist.lastp = &arglist.list;
|
|
|
|
varflag = 1;
|
2004-06-27 02:09:49 +04:00
|
|
|
/* Expand arguments, ignoring the initial 'name=value' ones */
|
1993-03-21 12:45:37 +03:00
|
|
|
for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
|
1995-04-01 01:58:09 +04:00
|
|
|
char *p = argp->narg.text;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (varflag && is_name(*p)) {
|
|
|
|
do {
|
|
|
|
p++;
|
|
|
|
} while (is_in_name(*p));
|
2002-11-25 01:35:38 +03:00
|
|
|
if (*p == '=')
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
|
1993-03-21 12:45:37 +03:00
|
|
|
varflag = 0;
|
|
|
|
}
|
|
|
|
*arglist.lastp = NULL;
|
2002-11-25 01:35:38 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
expredir(cmd->ncmd.redirect);
|
2002-11-25 01:35:38 +03:00
|
|
|
|
2004-06-27 02:09:49 +04:00
|
|
|
/* Now do the initial 'name=value' ones we skipped above */
|
2002-11-25 01:35:38 +03:00
|
|
|
varlist.lastp = &varlist.list;
|
|
|
|
for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
|
|
|
|
char *p = argp->narg.text;
|
|
|
|
if (!is_name(*p))
|
|
|
|
break;
|
|
|
|
do
|
|
|
|
p++;
|
|
|
|
while (is_in_name(*p));
|
|
|
|
if (*p != '=')
|
|
|
|
break;
|
|
|
|
expandarg(argp, &varlist, EXP_VARTILDE);
|
|
|
|
}
|
|
|
|
*varlist.lastp = NULL;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
argc = 0;
|
|
|
|
for (sp = arglist.list ; sp ; sp = sp->next)
|
|
|
|
argc++;
|
|
|
|
argv = stalloc(sizeof (char *) * (argc + 1));
|
1994-05-11 21:09:42 +04:00
|
|
|
|
|
|
|
for (sp = arglist.list ; sp ; sp = sp->next) {
|
|
|
|
TRACE(("evalcommand arg: %s\n", sp->text));
|
1993-03-21 12:45:37 +03:00
|
|
|
*argv++ = sp->text;
|
1994-05-11 21:09:42 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
*argv = NULL;
|
|
|
|
lastarg = NULL;
|
|
|
|
if (iflag && funcnest == 0 && argc > 0)
|
|
|
|
lastarg = argv[-1];
|
|
|
|
argv -= argc;
|
|
|
|
|
|
|
|
/* Print the command if xflag is set. */
|
|
|
|
if (xflag) {
|
2003-01-22 23:36:03 +03:00
|
|
|
char sep = 0;
|
|
|
|
out2str(ps4val());
|
1993-03-21 12:45:37 +03:00
|
|
|
for (sp = varlist.list ; sp ; sp = sp->next) {
|
2003-01-22 23:36:03 +03:00
|
|
|
if (sep != 0)
|
|
|
|
outc(sep, &errout);
|
1993-03-21 12:45:37 +03:00
|
|
|
out2str(sp->text);
|
2003-01-22 23:36:03 +03:00
|
|
|
sep = ' ';
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
for (sp = arglist.list ; sp ; sp = sp->next) {
|
2003-01-22 23:36:03 +03:00
|
|
|
if (sep != 0)
|
|
|
|
outc(sep, &errout);
|
1993-03-21 12:45:37 +03:00
|
|
|
out2str(sp->text);
|
2003-01-22 23:36:03 +03:00
|
|
|
sep = ' ';
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
outc('\n', &errout);
|
|
|
|
flushout(&errout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now locate the command. */
|
|
|
|
if (argc == 0) {
|
2003-01-22 23:36:03 +03:00
|
|
|
cmdentry.cmdtype = CMDSPLBLTIN;
|
2002-11-25 01:35:38 +03:00
|
|
|
cmdentry.u.bltin = bltincmd;
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
1995-06-09 05:53:44 +04:00
|
|
|
static const char PATH[] = "PATH=";
|
2003-01-22 23:36:03 +03:00
|
|
|
int cmd_flags = DO_ERR;
|
1995-06-09 05:53:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Modify the command lookup path, if a PATH= assignment
|
|
|
|
* is present
|
|
|
|
*/
|
2003-01-22 23:36:03 +03:00
|
|
|
for (sp = varlist.list; sp; sp = sp->next)
|
1995-06-09 05:53:44 +04:00
|
|
|
if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
|
|
|
|
path = sp->text + sizeof(PATH) - 1;
|
|
|
|
|
2003-01-22 23:36:03 +03:00
|
|
|
do {
|
|
|
|
int argsused, use_syspath;
|
|
|
|
find_command(argv[0], &cmdentry, cmd_flags, path);
|
|
|
|
if (cmdentry.cmdtype == CMDUNKNOWN) {
|
2002-11-25 01:35:38 +03:00
|
|
|
exitstatus = 127;
|
|
|
|
flushout(&errout);
|
2003-01-22 23:36:03 +03:00
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-01-22 23:36:03 +03:00
|
|
|
|
|
|
|
/* implement the 'command' builtin here */
|
|
|
|
if (cmdentry.cmdtype != CMDBUILTIN ||
|
|
|
|
cmdentry.u.bltin != bltincmd)
|
|
|
|
break;
|
|
|
|
cmd_flags |= DO_NOFUNC;
|
|
|
|
argsused = parse_command_args(argc, argv, &use_syspath);
|
|
|
|
if (argsused == 0) {
|
|
|
|
/* use 'type' builting to display info */
|
|
|
|
cmdentry.u.bltin = typecmd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
argc -= argsused;
|
|
|
|
argv += argsused;
|
|
|
|
if (use_syspath)
|
|
|
|
path = syspath() + 5;
|
|
|
|
} while (argc != 0);
|
|
|
|
if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
|
|
|
|
/* posix mandates that 'command <splbltin>' act as if
|
|
|
|
<splbltin> was a normal builtin */
|
|
|
|
cmdentry.cmdtype = CMDBUILTIN;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fork off a child process if necessary. */
|
|
|
|
if (cmd->ncmd.backgnd
|
1995-05-12 01:28:33 +04:00
|
|
|
|| (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
|
|
|
|
|| ((flags & EV_BACKCMD) != 0
|
2002-11-25 01:35:38 +03:00
|
|
|
&& ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN)
|
|
|
|
|| cmdentry.u.bltin == dotcmd
|
|
|
|
|| cmdentry.u.bltin == evalcmd))) {
|
2002-09-28 00:24:36 +04:00
|
|
|
INTOFF;
|
1993-03-21 12:45:37 +03:00
|
|
|
jp = makejob(cmd, 1);
|
|
|
|
mode = cmd->ncmd.backgnd;
|
|
|
|
if (flags & EV_BACKCMD) {
|
|
|
|
mode = FORK_NOJOB;
|
2004-04-30 10:27:59 +04:00
|
|
|
if (sh_pipe(pip) < 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
error("Pipe call failed");
|
|
|
|
}
|
2002-09-27 22:56:50 +04:00
|
|
|
#ifdef DO_SHAREDVFORK
|
|
|
|
/* It is essential that if DO_SHAREDVFORK is defined that the
|
|
|
|
* child's address space is actually shared with the parent as
|
|
|
|
* we rely on this.
|
|
|
|
*/
|
|
|
|
if (cmdentry.cmdtype == CMDNORMAL) {
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
savelocalvars = localvars;
|
|
|
|
localvars = NULL;
|
|
|
|
vforked = 1;
|
|
|
|
switch (pid = vfork()) {
|
|
|
|
case -1:
|
2003-11-14 13:27:10 +03:00
|
|
|
TRACE(("Vfork failed, errno=%d\n", errno));
|
2002-09-27 22:56:50 +04:00
|
|
|
INTON;
|
|
|
|
error("Cannot vfork");
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
/* Make sure that exceptions only unwind to
|
|
|
|
* after the vfork(2)
|
|
|
|
*/
|
|
|
|
if (setjmp(jmploc.loc)) {
|
|
|
|
if (exception == EXSHELLPROC) {
|
|
|
|
/* We can't progress with the vfork,
|
|
|
|
* so, set vforked = 2 so the parent
|
|
|
|
* knows, and _exit();
|
|
|
|
*/
|
|
|
|
vforked = 2;
|
|
|
|
_exit(0);
|
|
|
|
} else {
|
|
|
|
_exit(exerrno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
savehandler = handler;
|
|
|
|
handler = &jmploc;
|
2003-01-22 23:36:03 +03:00
|
|
|
listmklocal(varlist.list, VEXPORT | VNOFUNC);
|
2002-09-28 05:25:01 +04:00
|
|
|
forkchild(jp, cmd, mode, vforked);
|
2002-09-27 22:56:50 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
handler = savehandler; /* restore from vfork(2) */
|
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
|
|
|
if (vforked == 2) {
|
|
|
|
vforked = 0;
|
|
|
|
|
|
|
|
(void)waitpid(pid, NULL, 0);
|
|
|
|
/* We need to progress in a normal fork fashion */
|
|
|
|
goto normal_fork;
|
|
|
|
}
|
|
|
|
vforked = 0;
|
2002-09-28 05:25:01 +04:00
|
|
|
forkparent(jp, cmd, mode, pid);
|
2002-09-27 22:56:50 +04:00
|
|
|
goto parent;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
normal_fork:
|
|
|
|
#endif
|
2002-09-28 05:25:01 +04:00
|
|
|
if (forkshell(jp, cmd, mode) != 0)
|
2002-09-27 22:56:50 +04:00
|
|
|
goto parent; /* at end of routine */
|
2002-10-23 17:25:24 +04:00
|
|
|
FORCEINTON;
|
2002-09-27 22:56:50 +04:00
|
|
|
#ifdef DO_SHAREDVFORK
|
|
|
|
}
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
if (flags & EV_BACKCMD) {
|
2002-09-27 22:56:50 +04:00
|
|
|
if (!vforked) {
|
|
|
|
FORCEINTON;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
close(pip[0]);
|
|
|
|
if (pip[1] != 1) {
|
|
|
|
close(1);
|
|
|
|
copyfd(pip[1], 1);
|
|
|
|
close(pip[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flags |= EV_EXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the child process if a fork occurred. */
|
|
|
|
/* Execute the command. */
|
2003-01-22 23:36:03 +03:00
|
|
|
switch (cmdentry.cmdtype) {
|
|
|
|
case CMDFUNCTION:
|
1996-10-16 18:31:57 +04:00
|
|
|
#ifdef DEBUG
|
1993-03-21 12:45:37 +03:00
|
|
|
trputs("Shell function: "); trargs(argv);
|
1996-10-16 18:31:57 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
redirect(cmd->ncmd.redirect, REDIR_PUSH);
|
|
|
|
saveparam = shellparam;
|
|
|
|
shellparam.malloc = 0;
|
1996-11-06 04:17:10 +03:00
|
|
|
shellparam.reset = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
shellparam.nparam = argc - 1;
|
|
|
|
shellparam.p = argv + 1;
|
|
|
|
shellparam.optnext = NULL;
|
|
|
|
INTOFF;
|
|
|
|
savelocalvars = localvars;
|
|
|
|
localvars = NULL;
|
|
|
|
INTON;
|
|
|
|
if (setjmp(jmploc.loc)) {
|
1999-07-09 07:05:49 +04:00
|
|
|
if (exception == EXSHELLPROC) {
|
|
|
|
freeparam((volatile struct shparam *)
|
|
|
|
&saveparam);
|
|
|
|
} else {
|
1993-03-21 12:45:37 +03:00
|
|
|
freeparam(&shellparam);
|
|
|
|
shellparam = saveparam;
|
|
|
|
}
|
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
|
|
|
handler = savehandler;
|
|
|
|
longjmp(handler->loc, 1);
|
|
|
|
}
|
|
|
|
savehandler = handler;
|
|
|
|
handler = &jmploc;
|
2003-01-22 23:36:03 +03:00
|
|
|
listmklocal(varlist.list, 0);
|
|
|
|
/* stop shell blowing its stack */
|
|
|
|
if (++funcnest > 1000)
|
|
|
|
error("too many nested function calls");
|
2002-09-28 05:25:01 +04:00
|
|
|
evaltree(cmdentry.u.func, flags & EV_TESTED);
|
1993-03-21 12:45:37 +03:00
|
|
|
funcnest--;
|
|
|
|
INTOFF;
|
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
|
|
|
freeparam(&shellparam);
|
|
|
|
shellparam = saveparam;
|
|
|
|
handler = savehandler;
|
|
|
|
popredir();
|
|
|
|
INTON;
|
|
|
|
if (evalskip == SKIPFUNC) {
|
|
|
|
evalskip = 0;
|
|
|
|
skipcount = 0;
|
|
|
|
}
|
|
|
|
if (flags & EV_EXIT)
|
|
|
|
exitshell(exitstatus);
|
2003-01-22 23:36:03 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMDBUILTIN:
|
|
|
|
case CMDSPLBLTIN:
|
1996-10-16 18:31:57 +04:00
|
|
|
#ifdef DEBUG
|
1993-03-21 12:45:37 +03:00
|
|
|
trputs("builtin command: "); trargs(argv);
|
1996-10-16 18:31:57 +04:00
|
|
|
#endif
|
2002-11-25 01:35:38 +03:00
|
|
|
mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (flags == EV_BACKCMD) {
|
|
|
|
memout.nleft = 0;
|
|
|
|
memout.nextc = memout.buf;
|
|
|
|
memout.bufsize = 64;
|
|
|
|
mode |= REDIR_BACKQ;
|
|
|
|
}
|
2003-01-22 23:36:03 +03:00
|
|
|
e = -1;
|
2002-11-25 01:35:38 +03:00
|
|
|
savehandler = handler;
|
2005-03-02 23:57:31 +03:00
|
|
|
savecmdname = commandname;
|
2002-11-25 01:35:38 +03:00
|
|
|
handler = &jmploc;
|
|
|
|
if (!setjmp(jmploc.loc)) {
|
2003-01-22 23:36:03 +03:00
|
|
|
/* We need to ensure the command hash table isn't
|
|
|
|
* corruped by temporary PATH assignments.
|
|
|
|
* However we must ensure the 'local' command works!
|
|
|
|
*/
|
|
|
|
if (path != pathval() && (cmdentry.u.bltin == hashcmd ||
|
|
|
|
cmdentry.u.bltin == typecmd)) {
|
|
|
|
savelocalvars = localvars;
|
|
|
|
localvars = 0;
|
|
|
|
mklocal(path - 5 /* PATH= */, 0);
|
|
|
|
temp_path = 1;
|
|
|
|
} else
|
|
|
|
temp_path = 0;
|
2002-11-25 01:35:38 +03:00
|
|
|
redirect(cmd->ncmd.redirect, mode);
|
|
|
|
|
|
|
|
/* exec is a special builtin, but needs this list... */
|
|
|
|
cmdenviron = varlist.list;
|
|
|
|
/* we must check 'readonly' flag for all builtins */
|
|
|
|
listsetvar(varlist.list,
|
|
|
|
cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
|
|
|
|
commandname = argv[0];
|
|
|
|
/* initialize nextopt */
|
|
|
|
argptr = argv + 1;
|
|
|
|
optptr = NULL;
|
|
|
|
/* and getopt */
|
|
|
|
optreset = 1;
|
|
|
|
optind = 1;
|
|
|
|
exitstatus = cmdentry.u.bltin(argc, argv);
|
|
|
|
} else {
|
1993-03-21 12:45:37 +03:00
|
|
|
e = exception;
|
2003-01-22 23:36:03 +03:00
|
|
|
exitstatus = e == EXINT ? SIGINT + 128 :
|
2002-11-25 01:35:38 +03:00
|
|
|
e == EXEXEC ? exerrno : 2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-01-22 23:36:03 +03:00
|
|
|
handler = savehandler;
|
1993-03-21 12:45:37 +03:00
|
|
|
flushall();
|
|
|
|
out1 = &output;
|
|
|
|
out2 = &errout;
|
|
|
|
freestdout();
|
2003-01-22 23:36:03 +03:00
|
|
|
if (temp_path) {
|
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
|
|
|
}
|
1997-08-27 00:09:59 +04:00
|
|
|
cmdenviron = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (e != EXSHELLPROC) {
|
|
|
|
commandname = savecmdname;
|
1998-07-28 15:41:40 +04:00
|
|
|
if (flags & EV_EXIT)
|
1993-03-21 12:45:37 +03:00
|
|
|
exitshell(exitstatus);
|
|
|
|
}
|
|
|
|
if (e != -1) {
|
1996-10-16 18:31:57 +04:00
|
|
|
if ((e != EXERROR && e != EXEXEC)
|
2003-01-22 23:36:03 +03:00
|
|
|
|| cmdentry.cmdtype == CMDSPLBLTIN)
|
1993-03-21 12:45:37 +03:00
|
|
|
exraise(e);
|
|
|
|
FORCEINTON;
|
|
|
|
}
|
2002-11-25 01:35:38 +03:00
|
|
|
if (cmdentry.u.bltin != execcmd)
|
1993-03-21 12:45:37 +03:00
|
|
|
popredir();
|
|
|
|
if (flags == EV_BACKCMD) {
|
|
|
|
backcmd->buf = memout.buf;
|
|
|
|
backcmd->nleft = memout.nextc - memout.buf;
|
|
|
|
memout.buf = NULL;
|
|
|
|
}
|
2003-01-22 23:36:03 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1996-10-16 18:31:57 +04:00
|
|
|
#ifdef DEBUG
|
1993-03-21 12:45:37 +03:00
|
|
|
trputs("normal command: "); trargs(argv);
|
1996-10-16 18:31:57 +04:00
|
|
|
#endif
|
2002-09-27 22:56:50 +04:00
|
|
|
clearredir(vforked);
|
|
|
|
redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0);
|
|
|
|
if (!vforked)
|
|
|
|
for (sp = varlist.list ; sp ; sp = sp->next)
|
|
|
|
setvareq(sp->text, VEXPORT|VSTACK);
|
1993-03-21 12:45:37 +03:00
|
|
|
envp = environment();
|
2003-01-22 23:36:03 +03:00
|
|
|
shellexec(argv, envp, path, cmdentry.u.index, vforked);
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
parent: /* parent process gets here (if we forked) */
|
2002-11-25 01:35:38 +03:00
|
|
|
if (mode == FORK_FG) { /* argument to fork */
|
1993-03-21 12:45:37 +03:00
|
|
|
exitstatus = waitforjob(jp);
|
2002-11-25 01:35:38 +03:00
|
|
|
} else if (mode == FORK_NOJOB) {
|
1993-03-21 12:45:37 +03:00
|
|
|
backcmd->fd = pip[0];
|
|
|
|
close(pip[1]);
|
|
|
|
backcmd->jp = jp;
|
|
|
|
}
|
2002-10-23 17:25:24 +04:00
|
|
|
FORCEINTON;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (lastarg)
|
2003-01-22 23:36:03 +03:00
|
|
|
/* dsl: I think this is intended to be used to support
|
|
|
|
* '_' in 'vi' command mode during line editing...
|
|
|
|
* However I implemented that within libedit itself.
|
|
|
|
*/
|
1993-03-21 12:45:37 +03:00
|
|
|
setvar("_", lastarg, 0);
|
|
|
|
popstackmark(&smark);
|
2000-01-28 02:39:38 +03:00
|
|
|
|
|
|
|
if (eflag && exitstatus && !(flags & EV_TESTED))
|
2002-11-25 01:35:38 +03:00
|
|
|
exitshell(exitstatus);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for a command. This is called before we fork so that the
|
|
|
|
* location of the command will be available in the parent as well as
|
|
|
|
* the child. The check for "goodname" is an overly conservative
|
|
|
|
* check that the name will not be subject to expansion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
prehash(union node *n)
|
1995-05-14 08:19:18 +04:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct cmdentry entry;
|
|
|
|
|
2006-04-18 18:44:05 +04:00
|
|
|
if (n && n->type == NCMD && n->ncmd.args)
|
1994-08-24 09:48:26 +04:00
|
|
|
if (goodname(n->ncmd.args->narg.text))
|
1995-06-09 05:53:44 +04:00
|
|
|
find_command(n->ncmd.args->narg.text, &entry, 0,
|
|
|
|
pathval());
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builtin commands. Builtin commands whose functions are closely
|
|
|
|
* tied to evaluation are implemented here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2003-01-22 23:36:03 +03:00
|
|
|
* No command given.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
1994-12-04 10:11:37 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
bltincmd(int argc, char **argv)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1996-10-16 18:31:57 +04:00
|
|
|
/*
|
1995-05-14 08:19:18 +04:00
|
|
|
* Preserve exitstatus of a previous possible redirection
|
1996-10-16 18:31:57 +04:00
|
|
|
* as POSIX mandates
|
1995-05-14 08:19:18 +04:00
|
|
|
*/
|
2002-11-25 01:35:38 +03:00
|
|
|
return back_exitstatus;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle break and continue commands. Break, continue, and return are
|
|
|
|
* all handled by setting the evalskip flag. The evaluation routines
|
|
|
|
* above all check this flag, and if it is set they start skipping
|
|
|
|
* commands rather than executing them. The variable skipcount is
|
|
|
|
* the number of loops to break/continue, or the number of function
|
|
|
|
* levels to return. (The latter is always 1.) It should probably
|
|
|
|
* be an error to break out of more loops than exist, but it isn't
|
|
|
|
* in the standard shell so we don't make it one here.
|
|
|
|
*/
|
|
|
|
|
1994-12-04 10:11:37 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
breakcmd(int argc, char **argv)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1996-06-03 16:21:17 +04:00
|
|
|
int n = argc > 1 ? number(argv[1]) : 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (n > loopnest)
|
|
|
|
n = loopnest;
|
|
|
|
if (n > 0) {
|
|
|
|
evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
|
|
|
|
skipcount = n;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The return command.
|
|
|
|
*/
|
|
|
|
|
1994-12-04 10:11:37 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
returncmd(int argc, char **argv)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
2002-11-25 01:35:38 +03:00
|
|
|
int ret = argc > 1 ? number(argv[1]) : exitstatus;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (funcnest) {
|
|
|
|
evalskip = SKIPFUNC;
|
|
|
|
skipcount = 1;
|
1995-09-11 21:05:41 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Do what ksh does; skip the rest of the file */
|
|
|
|
evalskip = SKIPFILE;
|
|
|
|
skipcount = 1;
|
|
|
|
return ret;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-12-04 10:11:37 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
falsecmd(int argc, char **argv)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1994-05-11 21:42:57 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-12-04 10:11:37 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
truecmd(int argc, char **argv)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-12-04 10:11:37 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
execcmd(int argc, char **argv)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
if (argc > 1) {
|
1995-04-01 01:58:09 +04:00
|
|
|
struct strlist *sp;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
iflag = 0; /* exit on error */
|
1994-05-11 21:09:42 +04:00
|
|
|
mflag = 0;
|
|
|
|
optschanged();
|
2003-01-22 23:36:03 +03:00
|
|
|
for (sp = cmdenviron; sp; sp = sp->next)
|
1995-04-01 01:58:09 +04:00
|
|
|
setvareq(sp->text, VEXPORT|VSTACK);
|
2002-09-27 22:56:50 +04:00
|
|
|
shellexec(argv + 1, environment(), pathval(), 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2002-11-25 01:35:38 +03:00
|
|
|
|
|
|
|
static int
|
2003-07-13 12:31:13 +04:00
|
|
|
conv_time(clock_t ticks, char *seconds, size_t l)
|
2002-11-25 01:35:38 +03:00
|
|
|
{
|
|
|
|
static clock_t tpm = 0;
|
|
|
|
clock_t mins;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!tpm)
|
|
|
|
tpm = sysconf(_SC_CLK_TCK) * 60;
|
|
|
|
|
|
|
|
mins = ticks / tpm;
|
2003-07-13 12:31:13 +04:00
|
|
|
snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
|
2002-11-25 01:35:38 +03:00
|
|
|
|
|
|
|
if (seconds[0] == '6' && seconds[1] == '0') {
|
|
|
|
/* 59.99995 got rounded up... */
|
|
|
|
mins++;
|
2003-07-13 12:31:13 +04:00
|
|
|
strlcpy(seconds, "0.0", l);
|
2002-11-25 01:35:38 +03:00
|
|
|
return mins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* suppress trailing zeros */
|
|
|
|
i = strlen(seconds) - 1;
|
|
|
|
for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
|
|
|
|
seconds[i] = 0;
|
|
|
|
return mins;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
timescmd(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct tms tms;
|
|
|
|
int u, s, cu, cs;
|
|
|
|
char us[8], ss[8], cus[8], css[8];
|
|
|
|
|
|
|
|
nextopt("");
|
|
|
|
|
|
|
|
times(&tms);
|
|
|
|
|
2003-07-13 12:31:13 +04:00
|
|
|
u = conv_time(tms.tms_utime, us, sizeof(us));
|
|
|
|
s = conv_time(tms.tms_stime, ss, sizeof(ss));
|
|
|
|
cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
|
|
|
|
cs = conv_time(tms.tms_cstime, css, sizeof(css));
|
2002-11-25 01:35:38 +03:00
|
|
|
|
|
|
|
outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
|
|
|
|
u, us, s, ss, cu, cus, cs, css);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|