1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-26 06:18:55 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2002-07-21 06:49:14 +00:00
|
|
|
#include <paths.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <signal.h>
|
2002-08-29 14:26:49 +00:00
|
|
|
#include <stdlib.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <unistd.h>
|
2005-12-04 18:44:21 +00:00
|
|
|
#include <sys/resource.h>
|
1999-11-29 19:11:01 +00:00
|
|
|
#include <errno.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1994-05-26 06:18:55 +00: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"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "show.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "mystring.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "myhistedit.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
2009-12-27 18:04:05 +00:00
|
|
|
int evalskip; /* set if we are skipping commands */
|
2012-01-16 11:07:46 +00:00
|
|
|
int skipcount; /* number of levels to skip */
|
2013-07-25 19:48:15 +00:00
|
|
|
static int loopnest; /* current loop nesting level */
|
1994-05-26 06:18:55 +00:00
|
|
|
int funcnest; /* depth of function calls */
|
2010-10-13 04:01:01 +00:00
|
|
|
static int builtin_flags; /* evalcommand flags for builtins */
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
char *commandname;
|
2015-10-11 21:33:00 +00:00
|
|
|
struct arglist *cmdenviron;
|
1994-05-26 06:18:55 +00:00
|
|
|
int exitstatus; /* exit status of last command */
|
1996-09-01 10:22:36 +00:00
|
|
|
int oexitstatus; /* saved exit status */
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void evalloop(union node *, int);
|
|
|
|
static void evalfor(union node *, int);
|
2012-01-15 21:39:38 +00:00
|
|
|
static union node *evalcase(union node *);
|
2010-10-13 22:18:03 +00:00
|
|
|
static void evalsubshell(union node *, int);
|
|
|
|
static void evalredir(union node *, int);
|
2013-02-03 15:54:57 +00:00
|
|
|
static void exphere(union node *, struct arglist *);
|
2010-10-13 22:18:03 +00:00
|
|
|
static void expredir(union node *);
|
|
|
|
static void evalpipe(union node *);
|
2010-12-28 21:27:08 +00:00
|
|
|
static int is_valid_fast_cmdsubst(union node *n);
|
2010-10-13 22:18:03 +00:00
|
|
|
static void evalcommand(union node *, int, struct backcmd *);
|
|
|
|
static void prehash(union node *);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to reset things after an exception.
|
|
|
|
*/
|
|
|
|
|
2013-07-25 15:08:41 +00:00
|
|
|
void
|
|
|
|
reseteval(void)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
evalskip = 0;
|
|
|
|
loopnest = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1999-05-08 10:22:15 +00:00
|
|
|
* The eval command.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
evalcmd(int argc, char **argv)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char *concat;
|
|
|
|
char **ap;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
p = argv[1];
|
|
|
|
if (argc > 2) {
|
|
|
|
STARTSTACKSTR(concat);
|
|
|
|
ap = argv + 2;
|
|
|
|
for (;;) {
|
2010-11-23 22:17:39 +00:00
|
|
|
STPUTS(p, concat);
|
1994-05-26 06:18:55 +00:00
|
|
|
if ((p = *ap++) == NULL)
|
|
|
|
break;
|
|
|
|
STPUTC(' ', concat);
|
|
|
|
}
|
|
|
|
STPUTC('\0', concat);
|
|
|
|
p = grabstackstr(concat);
|
|
|
|
}
|
2011-06-16 21:50:28 +00:00
|
|
|
evalstring(p, builtin_flags);
|
2010-08-03 22:17:29 +00:00
|
|
|
} else
|
|
|
|
exitstatus = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
return exitstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a command or commands contained in a string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2015-03-01 22:32:23 +00:00
|
|
|
evalstring(const char *s, int flags)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
union node *n;
|
|
|
|
struct stackmark smark;
|
2009-06-13 21:17:45 +00:00
|
|
|
int flags_exit;
|
2010-08-03 22:17:29 +00:00
|
|
|
int any;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2009-06-13 21:17:45 +00:00
|
|
|
flags_exit = flags & EV_EXIT;
|
|
|
|
flags &= ~EV_EXIT;
|
2010-08-03 22:17:29 +00:00
|
|
|
any = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
setstackmark(&smark);
|
|
|
|
setinputstring(s, 1);
|
|
|
|
while ((n = parsecmd(0)) != NEOF) {
|
2011-06-04 11:28:42 +00:00
|
|
|
if (n != NULL && !nflag) {
|
2009-06-13 21:17:45 +00:00
|
|
|
if (flags_exit && preadateof())
|
|
|
|
evaltree(n, flags | EV_EXIT);
|
|
|
|
else
|
|
|
|
evaltree(n, flags);
|
2010-08-03 22:17:29 +00:00
|
|
|
any = 1;
|
2014-10-12 13:12:06 +00:00
|
|
|
if (evalskip)
|
|
|
|
break;
|
2009-06-13 21:17:45 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
popstackmark(&smark);
|
2013-02-19 23:46:51 +00:00
|
|
|
setstackmark(&smark);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
popfile();
|
|
|
|
popstackmark(&smark);
|
2010-08-03 22:17:29 +00:00
|
|
|
if (!any)
|
|
|
|
exitstatus = 0;
|
2009-06-13 21:17:45 +00:00
|
|
|
if (flags_exit)
|
2011-04-23 22:28:56 +00:00
|
|
|
exraise(EXEXIT);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluate a parse tree. The value is left in the global variable
|
|
|
|
* exitstatus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
evaltree(union node *n, int flags)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2005-09-10 07:41:36 +00:00
|
|
|
int do_etest;
|
2010-10-31 12:06:02 +00:00
|
|
|
union node *next;
|
2013-01-20 21:28:05 +00:00
|
|
|
struct stackmark smark;
|
2005-09-10 07:41:36 +00:00
|
|
|
|
2013-01-20 21:28:05 +00:00
|
|
|
setstackmark(&smark);
|
2005-09-10 07:41:36 +00:00
|
|
|
do_etest = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (n == NULL) {
|
|
|
|
TRACE(("evaltree(NULL) called\n"));
|
|
|
|
exitstatus = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-10-31 12:06:02 +00:00
|
|
|
do {
|
2010-10-31 12:08:16 +00:00
|
|
|
next = NULL;
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
2010-10-31 12:08:16 +00:00
|
|
|
displayhist = 1; /* show history substitutions done with fc */
|
1996-09-01 10:22:36 +00:00
|
|
|
#endif
|
2010-10-31 12:08:16 +00:00
|
|
|
TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
|
|
|
|
switch (n->type) {
|
|
|
|
case NSEMI:
|
|
|
|
evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
|
|
|
|
if (evalskip)
|
|
|
|
goto out;
|
|
|
|
next = n->nbinary.ch2;
|
|
|
|
break;
|
|
|
|
case NAND:
|
|
|
|
evaltree(n->nbinary.ch1, EV_TESTED);
|
|
|
|
if (evalskip || exitstatus != 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
next = n->nbinary.ch2;
|
|
|
|
break;
|
|
|
|
case NOR:
|
|
|
|
evaltree(n->nbinary.ch1, EV_TESTED);
|
|
|
|
if (evalskip || exitstatus == 0)
|
|
|
|
goto out;
|
|
|
|
next = n->nbinary.ch2;
|
|
|
|
break;
|
|
|
|
case NREDIR:
|
|
|
|
evalredir(n, flags);
|
|
|
|
break;
|
|
|
|
case NSUBSHELL:
|
|
|
|
evalsubshell(n, flags);
|
|
|
|
do_etest = !(flags & EV_TESTED);
|
|
|
|
break;
|
|
|
|
case NBACKGND:
|
|
|
|
evalsubshell(n, flags);
|
|
|
|
break;
|
|
|
|
case NIF: {
|
|
|
|
evaltree(n->nif.test, EV_TESTED);
|
|
|
|
if (evalskip)
|
|
|
|
goto out;
|
|
|
|
if (exitstatus == 0)
|
|
|
|
next = n->nif.ifpart;
|
|
|
|
else if (n->nif.elsepart)
|
|
|
|
next = n->nif.elsepart;
|
|
|
|
else
|
|
|
|
exitstatus = 0;
|
|
|
|
break;
|
1996-10-06 15:17:19 +00:00
|
|
|
}
|
2010-10-31 12:08:16 +00:00
|
|
|
case NWHILE:
|
|
|
|
case NUNTIL:
|
|
|
|
evalloop(n, flags & ~EV_EXIT);
|
|
|
|
break;
|
|
|
|
case NFOR:
|
|
|
|
evalfor(n, flags & ~EV_EXIT);
|
|
|
|
break;
|
|
|
|
case NCASE:
|
2012-01-15 21:39:38 +00:00
|
|
|
next = evalcase(n);
|
|
|
|
break;
|
|
|
|
case NCLIST:
|
|
|
|
next = n->nclist.body;
|
|
|
|
break;
|
|
|
|
case NCLISTFALLTHRU:
|
|
|
|
if (n->nclist.body) {
|
|
|
|
evaltree(n->nclist.body, flags & ~EV_EXIT);
|
|
|
|
if (evalskip)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
next = n->nclist.next;
|
2010-10-31 12:08:16 +00:00
|
|
|
break;
|
|
|
|
case NDEFUN:
|
|
|
|
defun(n->narg.text, n->narg.next);
|
1996-12-14 06:20:03 +00:00
|
|
|
exitstatus = 0;
|
2010-10-31 12:08:16 +00:00
|
|
|
break;
|
|
|
|
case NNOT:
|
|
|
|
evaltree(n->nnot.com, EV_TESTED);
|
2013-04-12 15:19:35 +00:00
|
|
|
if (evalskip)
|
|
|
|
goto out;
|
2010-10-31 12:08:16 +00:00
|
|
|
exitstatus = !exitstatus;
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-31 12:08:16 +00:00
|
|
|
case NPIPE:
|
|
|
|
evalpipe(n);
|
|
|
|
do_etest = !(flags & EV_TESTED);
|
|
|
|
break;
|
|
|
|
case NCMD:
|
|
|
|
evalcommand(n, flags, (struct backcmd *)NULL);
|
|
|
|
do_etest = !(flags & EV_TESTED);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
out1fmt("Node type = %d\n", n->type);
|
|
|
|
flushout(&output);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n = next;
|
2013-01-20 21:28:05 +00:00
|
|
|
popstackmark(&smark);
|
2013-02-19 23:46:51 +00:00
|
|
|
setstackmark(&smark);
|
2010-10-31 12:06:02 +00:00
|
|
|
} while (n != NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
out:
|
2013-01-20 21:28:05 +00:00
|
|
|
popstackmark(&smark);
|
2013-02-23 22:50:57 +00:00
|
|
|
if (pendingsig)
|
1994-05-26 06:18:55 +00:00
|
|
|
dotrap();
|
2011-04-23 22:28:56 +00:00
|
|
|
if (eflag && exitstatus != 0 && do_etest)
|
1994-05-26 06:18:55 +00:00
|
|
|
exitshell(exitstatus);
|
2011-04-23 22:28:56 +00:00
|
|
|
if (flags & EV_EXIT)
|
|
|
|
exraise(EXEXIT);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2005-09-10 08:25:28 +00:00
|
|
|
evalloop(union node *n, int flags)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int status;
|
|
|
|
|
|
|
|
loopnest++;
|
|
|
|
status = 0;
|
|
|
|
for (;;) {
|
2014-10-05 21:51:36 +00:00
|
|
|
if (!evalskip)
|
|
|
|
evaltree(n->nbinary.ch1, EV_TESTED);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (evalskip) {
|
2014-10-05 21:51:36 +00:00
|
|
|
if (evalskip == SKIPCONT && --skipcount <= 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
evalskip = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (evalskip == SKIPBREAK && --skipcount <= 0)
|
|
|
|
evalskip = 0;
|
2013-09-04 22:10:16 +00:00
|
|
|
if (evalskip == SKIPRETURN)
|
2010-09-11 15:07:40 +00:00
|
|
|
status = exitstatus;
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (n->type == NWHILE) {
|
|
|
|
if (exitstatus != 0)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (exitstatus == 0)
|
|
|
|
break;
|
|
|
|
}
|
2005-09-10 08:25:28 +00:00
|
|
|
evaltree(n->nbinary.ch2, flags);
|
1994-05-26 06:18:55 +00:00
|
|
|
status = exitstatus;
|
|
|
|
}
|
|
|
|
loopnest--;
|
|
|
|
exitstatus = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2005-09-10 08:25:28 +00:00
|
|
|
evalfor(union node *n, int flags)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct arglist arglist;
|
|
|
|
union node *argp;
|
2015-10-11 21:33:00 +00:00
|
|
|
int i;
|
2012-01-22 14:00:33 +00:00
|
|
|
int status;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2015-10-11 21:33:00 +00:00
|
|
|
emptyarglist(&arglist);
|
1994-05-26 06:18:55 +00:00
|
|
|
for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
|
1996-09-01 10:22:36 +00:00
|
|
|
oexitstatus = exitstatus;
|
1994-05-26 06:18:55 +00:00
|
|
|
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
|
|
|
|
}
|
|
|
|
|
|
|
|
loopnest++;
|
2012-01-22 14:00:33 +00:00
|
|
|
status = 0;
|
2015-10-11 21:33:00 +00:00
|
|
|
for (i = 0; i < arglist.count; i++) {
|
|
|
|
setvar(n->nfor.var, arglist.args[i], 0);
|
2005-09-10 08:25:28 +00:00
|
|
|
evaltree(n->nfor.body, flags);
|
2012-01-22 14:00:33 +00:00
|
|
|
status = exitstatus;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (evalskip) {
|
|
|
|
if (evalskip == SKIPCONT && --skipcount <= 0) {
|
|
|
|
evalskip = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (evalskip == SKIPBREAK && --skipcount <= 0)
|
|
|
|
evalskip = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loopnest--;
|
2012-01-22 14:00:33 +00:00
|
|
|
exitstatus = status;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-15 21:39:38 +00:00
|
|
|
/*
|
|
|
|
* Evaluate a case statement, returning the selected tree.
|
|
|
|
*
|
|
|
|
* The exit status needs care to get right.
|
|
|
|
*/
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2011-11-26 23:28:31 +00:00
|
|
|
static union node *
|
2012-01-15 21:39:38 +00:00
|
|
|
evalcase(union node *n)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
union node *cp;
|
|
|
|
union node *patp;
|
|
|
|
struct arglist arglist;
|
|
|
|
|
2015-10-11 21:33:00 +00:00
|
|
|
emptyarglist(&arglist);
|
1996-09-01 10:22:36 +00:00
|
|
|
oexitstatus = exitstatus;
|
1994-05-26 06:18:55 +00:00
|
|
|
expandarg(n->ncase.expr, &arglist, EXP_TILDE);
|
2011-11-26 23:28:31 +00:00
|
|
|
for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
|
1994-05-26 06:18:55 +00:00
|
|
|
for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
|
2015-10-11 21:33:00 +00:00
|
|
|
if (casematch(patp, arglist.args[0])) {
|
2011-06-17 13:03:49 +00:00
|
|
|
while (cp->nclist.next &&
|
2012-01-15 21:39:38 +00:00
|
|
|
cp->type == NCLISTFALLTHRU &&
|
|
|
|
cp->nclist.body == NULL)
|
2011-06-17 13:03:49 +00:00
|
|
|
cp = cp->nclist.next;
|
2012-01-15 21:39:38 +00:00
|
|
|
if (cp->nclist.next &&
|
|
|
|
cp->type == NCLISTFALLTHRU)
|
|
|
|
return (cp);
|
2012-01-15 20:04:05 +00:00
|
|
|
if (cp->nclist.body == NULL)
|
|
|
|
exitstatus = 0;
|
2011-11-26 23:28:31 +00:00
|
|
|
return (cp->nclist.body);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-15 20:04:05 +00:00
|
|
|
exitstatus = 0;
|
2011-11-26 23:28:31 +00:00
|
|
|
return (NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Kick off a subshell to evaluate a tree.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
evalsubshell(union node *n, int flags)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct job *jp;
|
|
|
|
int backgnd = (n->type == NBACKGND);
|
|
|
|
|
2011-06-05 14:13:15 +00:00
|
|
|
oexitstatus = exitstatus;
|
1994-05-26 06:18:55 +00:00
|
|
|
expredir(n->nredir.redirect);
|
Do not fork for a subshell if it is the last thing this shell is doing
(EV_EXIT). The fork is still done as normal if any traps are active.
In many cases, the fork can be avoided even without this change by using {}
instead of (), but in practice many scripts use (), likely because the
syntax is simpler.
Example:
sh -c '(/bin/sleep 10)& sleep 1;ps -p $! -o comm='
Now prints "sleep" instead of "sh". $! is more useful this way.
Most shells (dash, bash, pdksh, ksh93, zsh) seem to print "sleep" for this.
Example:
sh -c '( ( ( (ps jT))))'
Now shows no waiting shell processes instead of four.
Most shells (dash, bash, pdksh, ksh93, zsh) seem to show zero or one.
PR: bin/74404
Approved by: ed (mentor) (implicit)
2009-06-23 21:50:06 +00:00
|
|
|
if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
|
|
|
|
forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (backgnd)
|
|
|
|
flags &=~ EV_TESTED;
|
|
|
|
redirect(n->nredir.redirect, 0);
|
|
|
|
evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
|
2009-12-27 18:04:05 +00:00
|
|
|
} else if (! backgnd) {
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
1999-04-21 11:52:39 +00:00
|
|
|
exitstatus = waitforjob(jp, (int *)NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
2011-04-25 20:54:12 +00:00
|
|
|
} else
|
|
|
|
exitstatus = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-14 14:24:35 +00:00
|
|
|
/*
|
|
|
|
* Evaluate a redirected compound command.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2010-03-14 14:24:35 +00:00
|
|
|
evalredir(union node *n, int flags)
|
|
|
|
{
|
|
|
|
struct jmploc jmploc;
|
|
|
|
struct jmploc *savehandler;
|
|
|
|
volatile int in_redirect = 1;
|
|
|
|
|
2011-06-05 14:13:15 +00:00
|
|
|
oexitstatus = exitstatus;
|
2010-03-14 14:24:35 +00:00
|
|
|
expredir(n->nredir.redirect);
|
|
|
|
savehandler = handler;
|
|
|
|
if (setjmp(jmploc.loc)) {
|
|
|
|
int e;
|
|
|
|
|
|
|
|
handler = savehandler;
|
|
|
|
e = exception;
|
2011-04-23 22:28:56 +00:00
|
|
|
popredir();
|
2018-11-09 14:58:24 +00:00
|
|
|
if (e == EXERROR && in_redirect) {
|
|
|
|
FORCEINTON;
|
|
|
|
return;
|
2010-03-14 14:24:35 +00:00
|
|
|
}
|
|
|
|
longjmp(handler->loc, 1);
|
|
|
|
} else {
|
|
|
|
INTOFF;
|
|
|
|
handler = &jmploc;
|
|
|
|
redirect(n->nredir.redirect, REDIR_PUSH);
|
|
|
|
in_redirect = 0;
|
|
|
|
INTON;
|
|
|
|
evaltree(n->nredir.n, flags);
|
|
|
|
}
|
|
|
|
INTOFF;
|
|
|
|
handler = savehandler;
|
|
|
|
popredir();
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2013-02-03 15:54:57 +00:00
|
|
|
static void
|
|
|
|
exphere(union node *redir, struct arglist *fn)
|
|
|
|
{
|
|
|
|
struct jmploc jmploc;
|
|
|
|
struct jmploc *savehandler;
|
|
|
|
struct localvar *savelocalvars;
|
|
|
|
int need_longjmp = 0;
|
2016-01-07 20:48:24 +00:00
|
|
|
unsigned char saveoptreset;
|
2013-02-03 15:54:57 +00:00
|
|
|
|
2015-02-15 21:47:43 +00:00
|
|
|
redir->nhere.expdoc = "";
|
2013-02-03 15:54:57 +00:00
|
|
|
savelocalvars = localvars;
|
|
|
|
localvars = NULL;
|
2016-01-07 20:48:24 +00:00
|
|
|
saveoptreset = shellparam.reset;
|
2013-02-03 15:54:57 +00:00
|
|
|
forcelocal++;
|
|
|
|
savehandler = handler;
|
|
|
|
if (setjmp(jmploc.loc))
|
2018-11-09 14:58:24 +00:00
|
|
|
need_longjmp = exception != EXERROR;
|
2013-02-03 15:54:57 +00:00
|
|
|
else {
|
|
|
|
handler = &jmploc;
|
|
|
|
expandarg(redir->nhere.doc, fn, 0);
|
2015-10-11 21:33:00 +00:00
|
|
|
redir->nhere.expdoc = fn->args[0];
|
2013-02-03 15:54:57 +00:00
|
|
|
INTOFF;
|
|
|
|
}
|
|
|
|
handler = savehandler;
|
|
|
|
forcelocal--;
|
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
2016-01-07 20:48:24 +00:00
|
|
|
shellparam.reset = saveoptreset;
|
2013-02-03 15:54:57 +00:00
|
|
|
if (need_longjmp)
|
|
|
|
longjmp(handler->loc, 1);
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Compute the names of the files in a redirection list.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
expredir(union node *n)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1997-04-28 03:06:52 +00:00
|
|
|
union node *redir;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
for (redir = n ; redir ; redir = redir->nfile.next) {
|
1996-09-01 10:22:36 +00:00
|
|
|
struct arglist fn;
|
2015-10-11 21:33:00 +00:00
|
|
|
emptyarglist(&fn);
|
1996-09-01 10:22:36 +00:00
|
|
|
switch (redir->type) {
|
|
|
|
case NFROM:
|
|
|
|
case NTO:
|
2000-10-03 23:13:14 +00:00
|
|
|
case NFROMTO:
|
1996-09-01 10:22:36 +00:00
|
|
|
case NAPPEND:
|
2002-05-19 06:03:05 +00:00
|
|
|
case NCLOBBER:
|
2014-12-21 22:18:30 +00:00
|
|
|
expandarg(redir->nfile.fname, &fn, EXP_TILDE);
|
2015-10-11 21:33:00 +00:00
|
|
|
redir->nfile.expfname = fn.args[0];
|
1996-09-01 10:22:36 +00:00
|
|
|
break;
|
|
|
|
case NFROMFD:
|
|
|
|
case NTOFD:
|
|
|
|
if (redir->ndup.vname) {
|
2014-12-21 22:18:30 +00:00
|
|
|
expandarg(redir->ndup.vname, &fn, EXP_TILDE);
|
2015-10-11 21:33:00 +00:00
|
|
|
fixredir(redir, fn.args[0], 1);
|
1996-09-01 10:22:36 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-02-03 15:54:57 +00:00
|
|
|
case NXHERE:
|
|
|
|
exphere(redir, &fn);
|
|
|
|
break;
|
1994-05-26 06:18:55 +00: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.)
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
evalpipe(union node *n)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct job *jp;
|
|
|
|
struct nodelist *lp;
|
|
|
|
int pipelen;
|
|
|
|
int prevfd;
|
|
|
|
int pip[2];
|
|
|
|
|
2005-09-05 17:57:19 +00:00
|
|
|
TRACE(("evalpipe(%p) called\n", (void *)n));
|
1994-05-26 06:18:55 +00: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) {
|
|
|
|
if (pipe(pip) < 0) {
|
2013-06-28 21:47:08 +00:00
|
|
|
if (prevfd >= 0)
|
|
|
|
close(prevfd);
|
1999-11-29 19:11:01 +00:00
|
|
|
error("Pipe call failed: %s", strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
|
|
|
|
INTON;
|
|
|
|
if (prevfd > 0) {
|
2004-01-21 12:50:01 +00:00
|
|
|
dup2(prevfd, 0);
|
1994-05-26 06:18:55 +00:00
|
|
|
close(prevfd);
|
|
|
|
}
|
|
|
|
if (pip[1] >= 0) {
|
1999-11-17 16:53:34 +00:00
|
|
|
if (!(prevfd >= 0 && pip[0] == 0))
|
1999-11-05 12:06:30 +00:00
|
|
|
close(pip[0]);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (pip[1] != 1) {
|
2004-01-21 12:50:01 +00:00
|
|
|
dup2(pip[1], 1);
|
1994-05-26 06:18:55 +00:00
|
|
|
close(pip[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
evaltree(lp->n, EV_EXIT);
|
|
|
|
}
|
|
|
|
if (prevfd >= 0)
|
|
|
|
close(prevfd);
|
|
|
|
prevfd = pip[0];
|
2011-05-15 17:00:43 +00:00
|
|
|
if (pip[1] != -1)
|
|
|
|
close(pip[1]);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
if (n->npipe.backgnd == 0) {
|
|
|
|
INTOFF;
|
1999-04-21 11:52:39 +00:00
|
|
|
exitstatus = waitforjob(jp, (int *)NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
TRACE(("evalpipe: job done exit status %d\n", exitstatus));
|
|
|
|
INTON;
|
2011-04-25 20:54:12 +00:00
|
|
|
} else
|
|
|
|
exitstatus = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-28 21:27:08 +00:00
|
|
|
static int
|
|
|
|
is_valid_fast_cmdsubst(union node *n)
|
|
|
|
{
|
|
|
|
|
2011-06-12 23:06:04 +00:00
|
|
|
return (n->type == NCMD);
|
2010-12-28 21:27:08 +00:00
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00: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-02-02 06:50:57 +00:00
|
|
|
evalbackcmd(union node *n, struct backcmd *result)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int pip[2];
|
|
|
|
struct job *jp;
|
2013-01-20 21:28:05 +00:00
|
|
|
struct stackmark smark;
|
2010-12-28 13:28:24 +00:00
|
|
|
struct jmploc jmploc;
|
|
|
|
struct jmploc *savehandler;
|
2011-06-12 23:06:04 +00:00
|
|
|
struct localvar *savelocalvars;
|
2016-01-07 20:48:24 +00:00
|
|
|
unsigned char saveoptreset;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
result->fd = -1;
|
|
|
|
result->buf = NULL;
|
|
|
|
result->nleft = 0;
|
|
|
|
result->jp = NULL;
|
1996-09-01 10:22:36 +00:00
|
|
|
if (n == NULL) {
|
|
|
|
exitstatus = 0;
|
2014-10-05 21:51:36 +00:00
|
|
|
return;
|
1996-09-01 10:22:36 +00:00
|
|
|
}
|
2014-10-05 21:51:36 +00:00
|
|
|
setstackmark(&smark);
|
2013-01-14 12:20:55 +00:00
|
|
|
exitstatus = oexitstatus;
|
2010-12-28 21:27:08 +00:00
|
|
|
if (is_valid_fast_cmdsubst(n)) {
|
2011-06-12 23:06:04 +00:00
|
|
|
savelocalvars = localvars;
|
|
|
|
localvars = NULL;
|
2016-01-07 20:48:24 +00:00
|
|
|
saveoptreset = shellparam.reset;
|
2011-06-12 23:06:04 +00:00
|
|
|
forcelocal++;
|
2010-12-28 13:28:24 +00:00
|
|
|
savehandler = handler;
|
|
|
|
if (setjmp(jmploc.loc)) {
|
2018-10-27 20:17:57 +00:00
|
|
|
if (exception == EXERROR)
|
|
|
|
/* nothing */;
|
2010-12-28 13:28:24 +00:00
|
|
|
else if (exception != 0) {
|
|
|
|
handler = savehandler;
|
2011-06-12 23:06:04 +00:00
|
|
|
forcelocal--;
|
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
2016-01-07 20:48:24 +00:00
|
|
|
shellparam.reset = saveoptreset;
|
2010-12-28 13:28:24 +00:00
|
|
|
longjmp(handler->loc, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
handler = &jmploc;
|
|
|
|
evalcommand(n, EV_BACKCMD, result);
|
|
|
|
}
|
|
|
|
handler = savehandler;
|
2011-06-12 23:06:04 +00:00
|
|
|
forcelocal--;
|
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
2016-01-07 20:48:24 +00:00
|
|
|
shellparam.reset = saveoptreset;
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
|
|
|
if (pipe(pip) < 0)
|
1999-11-29 19:11:01 +00:00
|
|
|
error("Pipe call failed: %s", strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
jp = makejob(n, 1);
|
|
|
|
if (forkshell(jp, n, FORK_NOJOB) == 0) {
|
|
|
|
FORCEINTON;
|
|
|
|
close(pip[0]);
|
|
|
|
if (pip[1] != 1) {
|
2004-01-21 12:50:01 +00:00
|
|
|
dup2(pip[1], 1);
|
1994-05-26 06:18:55 +00:00
|
|
|
close(pip[1]);
|
|
|
|
}
|
|
|
|
evaltree(n, EV_EXIT);
|
|
|
|
}
|
|
|
|
close(pip[1]);
|
|
|
|
result->fd = pip[0];
|
|
|
|
result->jp = jp;
|
|
|
|
}
|
|
|
|
popstackmark(&smark);
|
2003-01-21 10:06:04 +00:00
|
|
|
TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
|
1994-05-26 06:18:55 +00:00
|
|
|
result->fd, result->buf, result->nleft, result->jp));
|
|
|
|
}
|
|
|
|
|
2012-07-15 10:19:43 +00:00
|
|
|
static int
|
|
|
|
mustexpandto(const char *argtext, const char *mask)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
|
|
|
|
argtext++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*argtext == CTLESC)
|
|
|
|
argtext++;
|
|
|
|
else if (BASESYNTAX[(int)*argtext] == CCTL)
|
|
|
|
return (0);
|
|
|
|
if (*argtext != *mask)
|
|
|
|
return (0);
|
|
|
|
if (*argtext == '\0')
|
|
|
|
return (1);
|
|
|
|
argtext++;
|
|
|
|
mask++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
isdeclarationcmd(struct narg *arg)
|
|
|
|
{
|
|
|
|
int have_command = 0;
|
|
|
|
|
|
|
|
if (arg == NULL)
|
|
|
|
return (0);
|
|
|
|
while (mustexpandto(arg->text, "command")) {
|
|
|
|
have_command = 1;
|
|
|
|
arg = &arg->next->narg;
|
|
|
|
if (arg == NULL)
|
|
|
|
return (0);
|
|
|
|
/*
|
|
|
|
* To also allow "command -p" and "command --" as part of
|
|
|
|
* a declaration command, add code here.
|
|
|
|
* We do not do this, as ksh does not do it either and it
|
|
|
|
* is not required by POSIX.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
return (mustexpandto(arg->text, "export") ||
|
|
|
|
mustexpandto(arg->text, "readonly") ||
|
|
|
|
(mustexpandto(arg->text, "local") &&
|
|
|
|
(have_command || !isfunc("local"))));
|
|
|
|
}
|
|
|
|
|
2013-12-06 22:24:37 +00:00
|
|
|
static void
|
2015-11-01 22:07:40 +00:00
|
|
|
xtracecommand(struct arglist *varlist, int argc, char **argv)
|
2013-12-06 22:24:37 +00:00
|
|
|
{
|
|
|
|
char sep = 0;
|
2015-10-11 21:33:00 +00:00
|
|
|
const char *text, *p, *ps4;
|
|
|
|
int i;
|
2013-12-06 22:24:37 +00:00
|
|
|
|
|
|
|
ps4 = expandstr(ps4val());
|
|
|
|
out2str(ps4 != NULL ? ps4 : ps4val());
|
2015-10-11 21:33:00 +00:00
|
|
|
for (i = 0; i < varlist->count; i++) {
|
|
|
|
text = varlist->args[i];
|
2013-12-06 22:24:37 +00:00
|
|
|
if (sep != 0)
|
|
|
|
out2c(' ');
|
2015-10-11 21:33:00 +00:00
|
|
|
p = strchr(text, '=');
|
2013-12-06 22:24:37 +00:00
|
|
|
if (p != NULL) {
|
|
|
|
p++;
|
2015-10-11 21:33:00 +00:00
|
|
|
outbin(text, p - text, out2);
|
2013-12-06 22:24:37 +00:00
|
|
|
out2qstr(p);
|
|
|
|
} else
|
2015-10-11 21:33:00 +00:00
|
|
|
out2qstr(text);
|
2013-12-06 22:24:37 +00:00
|
|
|
sep = ' ';
|
|
|
|
}
|
2015-11-01 22:07:40 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
text = argv[i];
|
2013-12-06 22:24:37 +00:00
|
|
|
if (sep != 0)
|
|
|
|
out2c(' ');
|
2015-10-11 21:33:00 +00:00
|
|
|
out2qstr(text);
|
2013-12-06 22:24:37 +00:00
|
|
|
sep = ' ';
|
|
|
|
}
|
|
|
|
out2c('\n');
|
|
|
|
flushout(&errout);
|
|
|
|
}
|
|
|
|
|
2010-12-30 22:33:55 +00:00
|
|
|
/*
|
|
|
|
* Check if a builtin can safely be executed in the same process,
|
|
|
|
* even though it should be in a subshell (command substitution).
|
|
|
|
* Note that jobid, jobs, times and trap can show information not
|
|
|
|
* available in a child process; this is deliberate.
|
|
|
|
* The arguments should already have been expanded.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
safe_builtin(int idx, int argc, char **argv)
|
|
|
|
{
|
2017-06-04 21:02:48 +00:00
|
|
|
/* Generated from builtins.def. */
|
|
|
|
if (safe_builtin_always(idx))
|
2010-12-30 22:33:55 +00:00
|
|
|
return (1);
|
|
|
|
if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
|
|
|
|
idx == UMASKCMD)
|
|
|
|
return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
|
|
|
|
if (idx == SETCMD)
|
|
|
|
return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
|
|
|
|
argv[1][0] == '+') && argv[1][1] == 'o' &&
|
|
|
|
argv[1][2] == '\0'));
|
|
|
|
return (0);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a simple command.
|
2011-01-05 23:17:29 +00:00
|
|
|
* Note: This may or may not return if (flags & EV_EXIT).
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
union node *argp;
|
|
|
|
struct arglist arglist;
|
|
|
|
struct arglist varlist;
|
|
|
|
char **argv;
|
|
|
|
int argc;
|
|
|
|
char **envp;
|
|
|
|
int varflag;
|
|
|
|
int mode;
|
|
|
|
int pip[2];
|
|
|
|
struct cmdentry cmdentry;
|
|
|
|
struct job *jp;
|
|
|
|
struct jmploc jmploc;
|
2009-06-23 20:45:12 +00:00
|
|
|
struct jmploc *savehandler;
|
|
|
|
char *savecmdname;
|
|
|
|
struct shparam saveparam;
|
|
|
|
struct localvar *savelocalvars;
|
2009-11-22 14:04:20 +00:00
|
|
|
struct parsefile *savetopfile;
|
1994-05-26 06:18:55 +00:00
|
|
|
volatile int e;
|
|
|
|
char *lastarg;
|
2017-12-26 16:23:18 +00:00
|
|
|
int signaled;
|
1999-12-20 13:42:59 +00:00
|
|
|
int do_clearcmdentry;
|
2010-08-13 20:29:43 +00:00
|
|
|
const char *path = pathval();
|
2015-10-11 21:33:00 +00:00
|
|
|
int i;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/* First expand the arguments. */
|
2005-09-05 17:57:19 +00:00
|
|
|
TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
|
2015-10-11 21:33:00 +00:00
|
|
|
emptyarglist(&arglist);
|
|
|
|
emptyarglist(&varlist);
|
1994-05-26 06:18:55 +00:00
|
|
|
varflag = 1;
|
2011-01-05 23:17:29 +00:00
|
|
|
jp = NULL;
|
1999-12-20 13:42:59 +00:00
|
|
|
do_clearcmdentry = 0;
|
1996-09-01 10:22:36 +00:00
|
|
|
oexitstatus = exitstatus;
|
|
|
|
exitstatus = 0;
|
2015-11-01 22:07:40 +00:00
|
|
|
/* Add one slot at the beginning for tryexec(). */
|
|
|
|
appendarglist(&arglist, nullstr);
|
1994-05-26 06:18:55 +00:00
|
|
|
for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
|
2011-05-21 22:03:06 +00:00
|
|
|
if (varflag && isassignment(argp->narg.text)) {
|
2012-07-15 10:19:43 +00:00
|
|
|
expandarg(argp, varflag == 1 ? &varlist : &arglist,
|
|
|
|
EXP_VARTILDE);
|
2011-05-21 22:03:06 +00:00
|
|
|
continue;
|
2012-07-15 10:19:43 +00:00
|
|
|
} else if (varflag == 1)
|
|
|
|
varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
|
|
|
|
}
|
2015-11-01 22:07:40 +00:00
|
|
|
appendarglist(&arglist, nullstr);
|
1994-05-26 06:18:55 +00:00
|
|
|
expredir(cmd->ncmd.redirect);
|
2015-11-01 22:07:40 +00:00
|
|
|
argc = arglist.count - 2;
|
|
|
|
argv = &arglist.args[1];
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2015-10-11 21:33:00 +00:00
|
|
|
argv[argc] = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
lastarg = NULL;
|
|
|
|
if (iflag && funcnest == 0 && argc > 0)
|
2015-10-11 21:33:00 +00:00
|
|
|
lastarg = argv[argc - 1];
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/* Print the command if xflag is set. */
|
2013-12-06 22:24:37 +00:00
|
|
|
if (xflag)
|
2015-11-01 22:07:40 +00:00
|
|
|
xtracecommand(&varlist, argc, argv);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/* Now locate the command. */
|
|
|
|
if (argc == 0) {
|
2006-04-09 12:21:20 +00:00
|
|
|
/* Variable assignment(s) without command */
|
1994-05-26 06:18:55 +00:00
|
|
|
cmdentry.cmdtype = CMDBUILTIN;
|
|
|
|
cmdentry.u.index = BLTINCMD;
|
2010-03-13 22:53:17 +00:00
|
|
|
cmdentry.special = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
1996-09-01 10:22:36 +00:00
|
|
|
static const char PATH[] = "PATH=";
|
2010-03-06 16:57:53 +00:00
|
|
|
int cmd_flags = 0, bltinonly = 0;
|
1996-09-01 10:22:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Modify the command lookup path, if a PATH= assignment
|
|
|
|
* is present
|
|
|
|
*/
|
2015-10-11 21:33:00 +00:00
|
|
|
for (i = 0; i < varlist.count; i++)
|
|
|
|
if (strncmp(varlist.args[i], PATH, sizeof(PATH) - 1) == 0) {
|
|
|
|
path = varlist.args[i] + sizeof(PATH) - 1;
|
2006-02-04 14:37:50 +00:00
|
|
|
/*
|
1999-12-20 13:42:59 +00:00
|
|
|
* On `PATH=... command`, we need to make
|
|
|
|
* sure that the command isn't using the
|
|
|
|
* non-updated hash table of the outer PATH
|
2006-02-04 14:37:50 +00:00
|
|
|
* setting and we need to make sure that
|
1999-12-20 13:42:59 +00:00
|
|
|
* the hash table isn't filled with items
|
|
|
|
* from the temporary setting.
|
|
|
|
*
|
2021-10-02 16:33:51 +00:00
|
|
|
* It would be better to forbid using and
|
1999-12-20 13:42:59 +00:00
|
|
|
* updating the table while this command
|
|
|
|
* runs, by the command finding mechanism
|
|
|
|
* is heavily integrated with hash handling,
|
|
|
|
* so we just delete the hash before and after
|
|
|
|
* the command runs. Partly deleting like
|
|
|
|
* changepatch() does doesn't seem worth the
|
|
|
|
* bookinging effort, since most such runs add
|
2003-12-30 15:39:33 +00:00
|
|
|
* directories in front of the new PATH.
|
1999-12-20 13:42:59 +00:00
|
|
|
*/
|
2011-02-05 14:08:51 +00:00
|
|
|
clearcmdentry();
|
1999-12-20 13:42:59 +00:00
|
|
|
do_clearcmdentry = 1;
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
|
2010-03-06 16:57:53 +00:00
|
|
|
for (;;) {
|
|
|
|
if (bltinonly) {
|
|
|
|
cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
|
|
|
|
if (cmdentry.u.index < 0) {
|
2010-01-03 15:01:38 +00:00
|
|
|
cmdentry.u.index = BLTINCMD;
|
|
|
|
argv--;
|
|
|
|
argc++;
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2010-03-06 16:57:53 +00:00
|
|
|
} else
|
|
|
|
find_command(argv[0], &cmdentry, cmd_flags, path);
|
|
|
|
/* implement the bltin and command builtins here */
|
|
|
|
if (cmdentry.cmdtype != CMDBUILTIN)
|
|
|
|
break;
|
|
|
|
if (cmdentry.u.index == BLTINCMD) {
|
|
|
|
if (argc == 1)
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
2010-03-06 16:57:53 +00:00
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
bltinonly = 1;
|
|
|
|
} else if (cmdentry.u.index == COMMANDCMD) {
|
|
|
|
if (argc == 1)
|
|
|
|
break;
|
|
|
|
if (!strcmp(argv[1], "-p")) {
|
|
|
|
if (argc == 2)
|
|
|
|
break;
|
|
|
|
if (argv[2][0] == '-') {
|
|
|
|
if (strcmp(argv[2], "--"))
|
|
|
|
break;
|
|
|
|
if (argc == 3)
|
|
|
|
break;
|
|
|
|
argv += 3;
|
|
|
|
argc -= 3;
|
|
|
|
} else {
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
|
|
|
}
|
|
|
|
path = _PATH_STDPATH;
|
2011-02-05 14:08:51 +00:00
|
|
|
clearcmdentry();
|
2010-03-06 16:57:53 +00:00
|
|
|
do_clearcmdentry = 1;
|
|
|
|
} else if (!strcmp(argv[1], "--")) {
|
|
|
|
if (argc == 2)
|
|
|
|
break;
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
|
|
|
} else if (argv[1][0] == '-')
|
|
|
|
break;
|
|
|
|
else {
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
cmd_flags |= DO_NOFUNC;
|
|
|
|
bltinonly = 0;
|
|
|
|
} else
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2010-03-06 16:57:53 +00:00
|
|
|
/*
|
|
|
|
* Special builtins lose their special properties when
|
|
|
|
* called via 'command'.
|
|
|
|
*/
|
|
|
|
if (cmd_flags & DO_NOFUNC)
|
|
|
|
cmdentry.special = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fork off a child process if necessary. */
|
2011-06-18 23:58:59 +00:00
|
|
|
if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
|
2009-06-13 21:10:41 +00:00
|
|
|
&& ((flags & EV_EXIT) == 0 || have_traps()))
|
1996-09-01 10:22:36 +00:00
|
|
|
|| ((flags & EV_BACKCMD) != 0
|
2010-12-30 22:33:55 +00:00
|
|
|
&& (cmdentry.cmdtype != CMDBUILTIN ||
|
|
|
|
!safe_builtin(cmdentry.u.index, argc, argv)))) {
|
1994-05-26 06:18:55 +00:00
|
|
|
jp = makejob(cmd, 1);
|
2011-06-18 23:58:59 +00:00
|
|
|
mode = FORK_FG;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (flags & EV_BACKCMD) {
|
|
|
|
mode = FORK_NOJOB;
|
|
|
|
if (pipe(pip) < 0)
|
1999-11-29 19:11:01 +00:00
|
|
|
error("Pipe call failed: %s", strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2012-02-04 23:12:14 +00:00
|
|
|
if (cmdentry.cmdtype == CMDNORMAL &&
|
|
|
|
cmd->ncmd.redirect == NULL &&
|
2015-10-11 21:33:00 +00:00
|
|
|
varlist.count == 0 &&
|
2012-02-04 23:12:14 +00:00
|
|
|
(mode == FORK_FG || mode == FORK_NOJOB) &&
|
|
|
|
!disvforkset() && !iflag && !mflag) {
|
|
|
|
vforkexecshell(jp, argv, environment(), path,
|
|
|
|
cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
|
|
|
|
goto parent;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
if (forkshell(jp, cmd, mode) != 0)
|
|
|
|
goto parent; /* at end of routine */
|
|
|
|
if (flags & EV_BACKCMD) {
|
|
|
|
FORCEINTON;
|
|
|
|
close(pip[0]);
|
|
|
|
if (pip[1] != 1) {
|
2004-01-21 12:50:01 +00:00
|
|
|
dup2(pip[1], 1);
|
1994-05-26 06:18:55 +00:00
|
|
|
close(pip[1]);
|
|
|
|
}
|
2011-06-16 21:50:28 +00:00
|
|
|
flags &= ~EV_BACKCMD;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
flags |= EV_EXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the child process if a fork occurred. */
|
|
|
|
/* Execute the command. */
|
|
|
|
if (cmdentry.cmdtype == CMDFUNCTION) {
|
1996-12-14 06:20:03 +00:00
|
|
|
#ifdef DEBUG
|
1994-05-26 06:18:55 +00:00
|
|
|
trputs("Shell function: "); trargs(argv);
|
1996-12-14 06:20:03 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
saveparam = shellparam;
|
|
|
|
shellparam.malloc = 0;
|
1996-12-14 06:20:03 +00:00
|
|
|
shellparam.reset = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
shellparam.nparam = argc - 1;
|
|
|
|
shellparam.p = argv + 1;
|
2014-10-26 17:50:33 +00:00
|
|
|
shellparam.optp = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
shellparam.optnext = NULL;
|
|
|
|
INTOFF;
|
|
|
|
savelocalvars = localvars;
|
|
|
|
localvars = NULL;
|
2009-08-23 21:09:46 +00:00
|
|
|
reffunc(cmdentry.u.func);
|
2009-06-23 20:45:12 +00:00
|
|
|
savehandler = handler;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (setjmp(jmploc.loc)) {
|
2011-04-23 22:28:56 +00:00
|
|
|
popredir();
|
2009-08-23 21:09:46 +00:00
|
|
|
unreffunc(cmdentry.u.func);
|
1994-05-26 06:18:55 +00:00
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
2016-01-10 16:31:28 +00:00
|
|
|
freeparam(&shellparam);
|
|
|
|
shellparam = saveparam;
|
2009-12-30 21:46:33 +00:00
|
|
|
funcnest--;
|
1994-05-26 06:18:55 +00:00
|
|
|
handler = savehandler;
|
|
|
|
longjmp(handler->loc, 1);
|
|
|
|
}
|
|
|
|
handler = &jmploc;
|
2009-12-30 21:46:33 +00:00
|
|
|
funcnest++;
|
2010-03-06 17:31:09 +00:00
|
|
|
redirect(cmd->ncmd.redirect, REDIR_PUSH);
|
2009-11-22 18:23:30 +00:00
|
|
|
INTON;
|
2015-10-11 21:33:00 +00:00
|
|
|
for (i = 0; i < varlist.count; i++)
|
|
|
|
mklocal(varlist.args[i]);
|
2008-11-23 20:23:57 +00:00
|
|
|
exitstatus = oexitstatus;
|
2011-04-23 22:28:56 +00:00
|
|
|
evaltree(getfuncnode(cmdentry.u.func),
|
|
|
|
flags & (EV_TESTED | EV_EXIT));
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
2009-08-23 21:09:46 +00:00
|
|
|
unreffunc(cmdentry.u.func);
|
1994-05-26 06:18:55 +00:00
|
|
|
poplocalvars();
|
|
|
|
localvars = savelocalvars;
|
|
|
|
freeparam(&shellparam);
|
|
|
|
shellparam = saveparam;
|
|
|
|
handler = savehandler;
|
2009-12-30 21:46:33 +00:00
|
|
|
funcnest--;
|
1994-05-26 06:18:55 +00:00
|
|
|
popredir();
|
|
|
|
INTON;
|
2013-09-04 22:10:16 +00:00
|
|
|
if (evalskip == SKIPRETURN) {
|
1994-05-26 06:18:55 +00:00
|
|
|
evalskip = 0;
|
|
|
|
skipcount = 0;
|
|
|
|
}
|
2011-01-05 23:17:29 +00:00
|
|
|
if (jp)
|
1994-05-26 06:18:55 +00:00
|
|
|
exitshell(exitstatus);
|
|
|
|
} else if (cmdentry.cmdtype == CMDBUILTIN) {
|
1996-12-14 06:20:03 +00:00
|
|
|
#ifdef DEBUG
|
1994-05-26 06:18:55 +00:00
|
|
|
trputs("builtin command: "); trargs(argv);
|
1996-12-14 06:20:03 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
|
|
|
|
if (flags == EV_BACKCMD) {
|
|
|
|
memout.nextc = memout.buf;
|
|
|
|
mode |= REDIR_BACKQ;
|
|
|
|
}
|
|
|
|
savecmdname = commandname;
|
2009-11-22 14:04:20 +00:00
|
|
|
savetopfile = getcurrentfile();
|
2015-10-11 21:33:00 +00:00
|
|
|
cmdenviron = &varlist;
|
1994-05-26 06:18:55 +00:00
|
|
|
e = -1;
|
2009-06-23 20:45:12 +00:00
|
|
|
savehandler = handler;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (setjmp(jmploc.loc)) {
|
|
|
|
e = exception;
|
2011-04-23 22:28:56 +00:00
|
|
|
if (e == EXINT)
|
|
|
|
exitstatus = SIGINT+128;
|
1994-05-26 06:18:55 +00:00
|
|
|
goto cmddone;
|
|
|
|
}
|
|
|
|
handler = &jmploc;
|
2006-04-09 12:21:20 +00:00
|
|
|
redirect(cmd->ncmd.redirect, mode);
|
2012-12-12 22:01:10 +00:00
|
|
|
outclearerror(out1);
|
2010-03-13 22:53:17 +00:00
|
|
|
/*
|
|
|
|
* If there is no command word, redirection errors should
|
|
|
|
* not be fatal but assignment errors should.
|
|
|
|
*/
|
2011-12-28 22:10:12 +00:00
|
|
|
if (argc == 0)
|
2010-03-13 22:53:17 +00:00
|
|
|
cmdentry.special = 1;
|
2011-01-01 13:26:18 +00:00
|
|
|
listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
|
2010-05-05 21:48:40 +00:00
|
|
|
if (argc > 0)
|
|
|
|
bltinsetlocale();
|
1994-05-26 06:18:55 +00:00
|
|
|
commandname = argv[0];
|
|
|
|
argptr = argv + 1;
|
2009-12-27 18:04:05 +00:00
|
|
|
nextopt_optptr = NULL; /* initialize nextopt */
|
2009-05-31 12:36:14 +00:00
|
|
|
builtin_flags = flags;
|
1994-05-26 06:18:55 +00:00
|
|
|
exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
|
|
|
|
flushall();
|
2012-12-12 22:01:10 +00:00
|
|
|
if (outiserror(out1)) {
|
|
|
|
warning("write error on stdout");
|
|
|
|
if (exitstatus == 0 || exitstatus == 1)
|
|
|
|
exitstatus = 2;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
cmddone:
|
2010-05-05 21:48:40 +00:00
|
|
|
if (argc > 0)
|
|
|
|
bltinunsetlocale();
|
2000-05-15 12:33:17 +00:00
|
|
|
cmdenviron = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
out1 = &output;
|
|
|
|
out2 = &errout;
|
|
|
|
freestdout();
|
2011-01-05 23:17:29 +00:00
|
|
|
handler = savehandler;
|
2011-02-04 22:47:55 +00:00
|
|
|
commandname = savecmdname;
|
|
|
|
if (jp)
|
|
|
|
exitshell(exitstatus);
|
2010-01-01 18:17:46 +00:00
|
|
|
if (flags == EV_BACKCMD) {
|
|
|
|
backcmd->buf = memout.buf;
|
2017-05-16 21:54:51 +00:00
|
|
|
backcmd->nleft = memout.buf != NULL ?
|
|
|
|
memout.nextc - memout.buf : 0;
|
2010-01-01 18:17:46 +00:00
|
|
|
memout.buf = NULL;
|
2017-05-16 21:54:51 +00:00
|
|
|
memout.nextc = NULL;
|
|
|
|
memout.bufend = NULL;
|
2017-05-18 21:44:14 +00:00
|
|
|
memout.bufsize = 64;
|
2010-01-01 18:17:46 +00:00
|
|
|
}
|
2011-04-23 22:28:56 +00:00
|
|
|
if (cmdentry.u.index != EXECCMD)
|
2010-03-06 17:09:22 +00:00
|
|
|
popredir();
|
1994-05-26 06:18:55 +00:00
|
|
|
if (e != -1) {
|
2018-11-09 14:58:24 +00:00
|
|
|
if (e != EXERROR || cmdentry.special)
|
1994-05-26 06:18:55 +00:00
|
|
|
exraise(e);
|
2009-11-22 14:04:20 +00:00
|
|
|
popfilesupto(savetopfile);
|
2010-01-01 18:17:46 +00:00
|
|
|
if (flags != EV_BACKCMD)
|
|
|
|
FORCEINTON;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
1996-12-14 06:20:03 +00:00
|
|
|
#ifdef DEBUG
|
1994-05-26 06:18:55 +00:00
|
|
|
trputs("normal command: "); trargs(argv);
|
1996-12-14 06:20:03 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
redirect(cmd->ncmd.redirect, 0);
|
2015-10-11 21:33:00 +00:00
|
|
|
for (i = 0; i < varlist.count; i++)
|
|
|
|
setvareq(varlist.args[i], VEXPORT|VSTACK);
|
1994-05-26 06:18:55 +00:00
|
|
|
envp = environment();
|
2010-03-06 16:57:53 +00:00
|
|
|
shellexec(argv, envp, path, cmdentry.u.index);
|
1994-05-26 06:18:55 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
parent: /* parent process gets here (if we forked) */
|
2010-09-04 21:23:46 +00:00
|
|
|
if (mode == FORK_FG) { /* argument to fork */
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
2017-12-26 16:23:18 +00:00
|
|
|
exitstatus = waitforjob(jp, &signaled);
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
2017-12-26 16:23:18 +00:00
|
|
|
if (iflag && loopnest > 0 && signaled) {
|
1999-04-03 12:55:51 +00:00
|
|
|
evalskip = SKIPBREAK;
|
|
|
|
skipcount = loopnest;
|
|
|
|
}
|
2010-09-04 21:23:46 +00:00
|
|
|
} else if (mode == FORK_NOJOB) {
|
1994-05-26 06:18:55 +00:00
|
|
|
backcmd->fd = pip[0];
|
|
|
|
close(pip[1]);
|
|
|
|
backcmd->jp = jp;
|
2011-06-18 23:58:59 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (lastarg)
|
|
|
|
setvar("_", lastarg, 0);
|
1999-12-20 13:42:59 +00:00
|
|
|
if (do_clearcmdentry)
|
2011-02-05 14:08:51 +00:00
|
|
|
clearcmdentry();
|
1994-05-26 06:18:55 +00: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.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
prehash(union node *n)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct cmdentry entry;
|
|
|
|
|
2006-06-15 07:57:05 +00:00
|
|
|
if (n && n->type == NCMD && n->ncmd.args)
|
1996-09-01 10:22:36 +00:00
|
|
|
if (goodname(n->ncmd.args->narg.text))
|
|
|
|
find_command(n->ncmd.args->narg.text, &entry, 0,
|
|
|
|
pathval());
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builtin commands. Builtin commands whose functions are closely
|
|
|
|
* tied to evaluation are implemented here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2010-01-03 15:01:38 +00:00
|
|
|
* No command given, a bltin command with no arguments, or a bltin command
|
|
|
|
* with an invalid name.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2010-01-03 15:01:38 +00:00
|
|
|
bltincmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2010-01-03 15:01:38 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
out2fmt_flush("%s: not found\n", argv[1]);
|
|
|
|
return 127;
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
/*
|
2017-05-06 13:28:42 +00:00
|
|
|
* Preserve exitstatus of a previous possible command substitution
|
1996-12-14 06:20:03 +00:00
|
|
|
* as POSIX mandates
|
1996-09-01 10:22:36 +00:00
|
|
|
*/
|
1994-05-26 06:18:55 +00:00
|
|
|
return exitstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
breakcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2014-07-20 20:29:09 +00:00
|
|
|
long n;
|
|
|
|
char *end;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2014-07-20 20:29:09 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
/* Allow arbitrarily large numbers. */
|
|
|
|
n = strtol(argv[1], &end, 10);
|
|
|
|
if (!is_digit(argv[1][0]) || *end != '\0')
|
|
|
|
error("Illegal number: %s", argv[1]);
|
|
|
|
} else
|
|
|
|
n = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (n > loopnest)
|
|
|
|
n = loopnest;
|
|
|
|
if (n > 0) {
|
|
|
|
evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
|
|
|
|
skipcount = n;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-21 06:49:14 +00:00
|
|
|
/*
|
|
|
|
* The `command' command.
|
|
|
|
*/
|
|
|
|
int
|
2012-09-15 21:56:30 +00:00
|
|
|
commandcmd(int argc __unused, char **argv __unused)
|
2002-07-21 06:49:14 +00:00
|
|
|
{
|
2010-05-08 14:00:01 +00:00
|
|
|
const char *path;
|
2002-07-21 06:49:14 +00:00
|
|
|
int ch;
|
2005-10-28 18:37:09 +00:00
|
|
|
int cmd = -1;
|
2002-07-21 06:49:14 +00:00
|
|
|
|
2010-03-06 16:57:53 +00:00
|
|
|
path = bltinlookup("PATH", 1);
|
2002-07-21 06:49:14 +00:00
|
|
|
|
2012-09-15 21:56:30 +00:00
|
|
|
while ((ch = nextopt("pvV")) != '\0') {
|
2002-07-21 06:49:14 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'p':
|
2010-05-08 14:00:01 +00:00
|
|
|
path = _PATH_STDPATH;
|
2002-07-21 06:49:14 +00:00
|
|
|
break;
|
2005-10-28 18:37:09 +00:00
|
|
|
case 'v':
|
|
|
|
cmd = TYPECMD_SMALLV;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
cmd = TYPECMD_BIGV;
|
|
|
|
break;
|
2002-07-21 06:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-28 18:37:09 +00:00
|
|
|
if (cmd != -1) {
|
2012-09-15 21:56:30 +00:00
|
|
|
if (*argptr == NULL || argptr[1] != NULL)
|
2005-10-28 18:37:09 +00:00
|
|
|
error("wrong number of arguments");
|
2012-09-15 21:56:30 +00:00
|
|
|
return typecmd_impl(2, argptr - 1, cmd, path);
|
2005-10-28 18:37:09 +00:00
|
|
|
}
|
2012-09-15 21:56:30 +00:00
|
|
|
if (*argptr != NULL)
|
2010-10-29 21:44:43 +00:00
|
|
|
error("commandcmd bad call");
|
2002-07-21 06:49:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do nothing successfully if no command was specified;
|
|
|
|
* ksh also does this.
|
|
|
|
*/
|
2010-03-06 16:57:53 +00:00
|
|
|
return 0;
|
2002-07-21 06:49:14 +00:00
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The return command.
|
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
returncmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1996-12-14 06:20:03 +00:00
|
|
|
int ret = argc > 1 ? number(argv[1]) : oexitstatus;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2013-09-04 22:10:16 +00:00
|
|
|
evalskip = SKIPRETURN;
|
|
|
|
skipcount = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
falsecmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
truecmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
execcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2015-10-11 21:33:00 +00:00
|
|
|
int i;
|
|
|
|
|
2010-05-28 22:40:24 +00:00
|
|
|
/*
|
|
|
|
* Because we have historically not supported any options,
|
|
|
|
* only treat "--" specially.
|
|
|
|
*/
|
|
|
|
if (argc > 1 && strcmp(argv[1], "--") == 0)
|
|
|
|
argc--, argv++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
iflag = 0; /* exit on error */
|
|
|
|
mflag = 0;
|
|
|
|
optschanged();
|
2015-10-11 21:33:00 +00:00
|
|
|
for (i = 0; i < cmdenviron->count; i++)
|
|
|
|
setvareq(cmdenviron->args[i], VEXPORT|VSTACK);
|
1994-05-26 06:18:55 +00:00
|
|
|
shellexec(argv + 1, environment(), pathval(), 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-12-04 18:44:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
timescmd(int argc __unused, char **argv __unused)
|
|
|
|
{
|
|
|
|
struct rusage ru;
|
|
|
|
long shumins, shsmins, chumins, chsmins;
|
|
|
|
double shusecs, shssecs, chusecs, chssecs;
|
|
|
|
|
|
|
|
if (getrusage(RUSAGE_SELF, &ru) < 0)
|
|
|
|
return 1;
|
|
|
|
shumins = ru.ru_utime.tv_sec / 60;
|
|
|
|
shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
|
|
|
|
shsmins = ru.ru_stime.tv_sec / 60;
|
|
|
|
shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
|
|
|
|
if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
|
|
|
|
return 1;
|
|
|
|
chumins = ru.ru_utime.tv_sec / 60;
|
|
|
|
chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
|
|
|
|
chsmins = ru.ru_stime.tv_sec / 60;
|
|
|
|
chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
|
|
|
|
out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
|
|
|
|
shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);
|
|
|
|
return 0;
|
|
|
|
}
|