1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 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[] = "@(#)exec.c 8.4 (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
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2011-02-04 22:47:55 +00:00
|
|
|
#include <paths.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* When commands are first encountered, they are entered in a hash table.
|
|
|
|
* This ensures that a full path search will not have to be done for them
|
|
|
|
* on each invocation.
|
|
|
|
*
|
|
|
|
* We should investigate converting to a linear search, even though that
|
|
|
|
* would make the command name "hash" a misnomer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "nodes.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "redir.h"
|
|
|
|
#include "eval.h"
|
|
|
|
#include "exec.h"
|
|
|
|
#include "builtins.h"
|
|
|
|
#include "var.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "mystring.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "show.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "jobs.h"
|
1997-04-28 03:08:38 +00:00
|
|
|
#include "alias.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define CMDTABLESIZE 31 /* should be prime */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct tblentry {
|
|
|
|
struct tblentry *next; /* next entry in hash chain */
|
|
|
|
union param param; /* definition of builtin function */
|
2006-04-09 12:21:20 +00:00
|
|
|
int special; /* flag for special builtin commands */
|
2012-11-05 17:52:18 +00:00
|
|
|
signed char cmdtype; /* index identifying command */
|
2012-11-03 22:23:08 +00:00
|
|
|
char cmdname[]; /* name of command */
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static struct tblentry *cmdtable[CMDTABLESIZE];
|
2013-01-14 16:40:50 +00:00
|
|
|
static int cmdtable_cd = 0; /* cmdtable contains cd-dependent entries */
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void tryexec(char *, char **, char **);
|
|
|
|
static void printentry(struct tblentry *, int);
|
|
|
|
static struct tblentry *cmdlookup(const char *, int);
|
|
|
|
static void delete_cmd_entry(void);
|
2012-01-01 22:17:12 +00:00
|
|
|
static void addcmdentry(const char *, struct cmdentry *);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exec a program. Never returns. If you change this routine, you may
|
|
|
|
* have to change the find_command routine as well.
|
2011-02-04 22:47:55 +00:00
|
|
|
*
|
|
|
|
* The argv array may be changed and element argv[-1] should be writable.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2009-12-27 18:04:05 +00:00
|
|
|
shellexec(char **argv, char **envp, const char *path, int idx)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char *cmdname;
|
2018-07-15 21:55:17 +00:00
|
|
|
const char *opt;
|
1994-05-26 06:18:55 +00:00
|
|
|
int e;
|
|
|
|
|
|
|
|
if (strchr(argv[0], '/') != NULL) {
|
|
|
|
tryexec(argv[0], argv, envp);
|
|
|
|
e = errno;
|
|
|
|
} else {
|
|
|
|
e = ENOENT;
|
2018-07-15 21:55:17 +00:00
|
|
|
while ((cmdname = padvance(&path, &opt, argv[0])) != NULL) {
|
|
|
|
if (--idx < 0 && opt == NULL) {
|
1994-05-26 06:18:55 +00:00
|
|
|
tryexec(cmdname, argv, envp);
|
|
|
|
if (errno != ENOENT && errno != ENOTDIR)
|
|
|
|
e = errno;
|
2011-02-05 12:54:59 +00:00
|
|
|
if (e == ENOEXEC)
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
stunalloc(cmdname);
|
|
|
|
}
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
|
|
|
|
/* Map to POSIX errors */
|
2018-11-09 14:58:24 +00:00
|
|
|
if (e == ENOENT || e == ENOTDIR)
|
|
|
|
errorwithstatus(127, "%s: not found", argv[0]);
|
|
|
|
else
|
|
|
|
errorwithstatus(126, "%s: %s", argv[0], strerror(e));
|
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
|
|
|
tryexec(char *cmd, char **argv, char **envp)
|
|
|
|
{
|
2011-02-05 12:54:59 +00:00
|
|
|
int e, in;
|
|
|
|
ssize_t n;
|
|
|
|
char buf[256];
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
execve(cmd, argv, envp);
|
|
|
|
e = errno;
|
|
|
|
if (e == ENOEXEC) {
|
2011-02-05 12:54:59 +00:00
|
|
|
INTOFF;
|
|
|
|
in = open(cmd, O_RDONLY | O_NONBLOCK);
|
|
|
|
if (in != -1) {
|
|
|
|
n = pread(in, buf, sizeof buf, 0);
|
|
|
|
close(in);
|
|
|
|
if (n > 0 && memchr(buf, '\0', n) != NULL) {
|
|
|
|
errno = ENOEXEC;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-02-04 22:47:55 +00:00
|
|
|
*argv = cmd;
|
2013-04-01 17:18:22 +00:00
|
|
|
*--argv = __DECONST(char *, _PATH_BSHELL);
|
2011-02-04 22:47:55 +00:00
|
|
|
execve(_PATH_BSHELL, argv, envp);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
errno = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do a path search. The variable path (passed by reference) should be
|
|
|
|
* set to the start of the path before the first call; padvance will update
|
|
|
|
* this value as it proceeds. Successive calls to padvance will return
|
2018-07-15 21:55:17 +00:00
|
|
|
* the possible path expansions in sequence. If popt is not NULL, options
|
|
|
|
* are processed: if an option (indicated by a percent sign) appears in
|
|
|
|
* the path entry then *popt will be set to point to it; else *popt will be
|
|
|
|
* set to NULL. If popt is NULL, percent signs are not special.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
2018-07-15 21:55:17 +00:00
|
|
|
padvance(const char **path, const char **popt, const char *name)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
2009-12-24 18:41:14 +00:00
|
|
|
const char *p, *start;
|
|
|
|
char *q;
|
2013-11-30 21:27:11 +00:00
|
|
|
size_t len, namelen;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (*path == NULL)
|
|
|
|
return NULL;
|
|
|
|
start = *path;
|
2018-07-15 21:55:17 +00:00
|
|
|
if (popt != NULL)
|
|
|
|
for (p = start; *p && *p != ':' && *p != '%'; p++)
|
|
|
|
; /* nothing */
|
|
|
|
else
|
|
|
|
for (p = start; *p && *p != ':'; p++)
|
|
|
|
; /* nothing */
|
2013-11-30 21:27:11 +00:00
|
|
|
namelen = strlen(name);
|
|
|
|
len = p - start + namelen + 2; /* "2" is for '/' and '\0' */
|
2010-12-26 13:25:47 +00:00
|
|
|
STARTSTACKSTR(q);
|
|
|
|
CHECKSTRSPACE(len, q);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (p != start) {
|
1996-09-01 10:22:36 +00:00
|
|
|
memcpy(q, start, p - start);
|
1994-05-26 06:18:55 +00:00
|
|
|
q += p - start;
|
|
|
|
*q++ = '/';
|
|
|
|
}
|
2013-11-30 21:27:11 +00:00
|
|
|
memcpy(q, name, namelen + 1);
|
2018-07-15 21:55:17 +00:00
|
|
|
if (popt != NULL) {
|
|
|
|
if (*p == '%') {
|
|
|
|
*popt = ++p;
|
|
|
|
while (*p && *p != ':') p++;
|
|
|
|
} else
|
|
|
|
*popt = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
if (*p == ':')
|
|
|
|
*path = p + 1;
|
|
|
|
else
|
|
|
|
*path = NULL;
|
|
|
|
return stalloc(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*** Command hashing code ***/
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
hashcmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct tblentry **pp;
|
|
|
|
struct tblentry *cmdp;
|
|
|
|
int c;
|
|
|
|
int verbose;
|
|
|
|
struct cmdentry entry;
|
|
|
|
char *name;
|
2012-02-11 21:06:45 +00:00
|
|
|
int errors;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2012-02-11 21:06:45 +00:00
|
|
|
errors = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
verbose = 0;
|
|
|
|
while ((c = nextopt("rv")) != '\0') {
|
|
|
|
if (c == 'r') {
|
2011-02-05 14:08:51 +00:00
|
|
|
clearcmdentry();
|
1994-05-26 06:18:55 +00:00
|
|
|
} else if (c == 'v') {
|
|
|
|
verbose++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*argptr == NULL) {
|
|
|
|
for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
|
|
|
|
for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
|
2002-06-13 07:50:50 +00:00
|
|
|
if (cmdp->cmdtype == CMDNORMAL)
|
|
|
|
printentry(cmdp, verbose);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while ((name = *argptr) != NULL) {
|
|
|
|
if ((cmdp = cmdlookup(name, 0)) != NULL
|
2011-01-09 21:07:30 +00:00
|
|
|
&& cmdp->cmdtype == CMDNORMAL)
|
1994-05-26 06:18:55 +00:00
|
|
|
delete_cmd_entry();
|
2010-03-06 16:57:53 +00:00
|
|
|
find_command(name, &entry, DO_ERR, pathval());
|
2012-02-11 21:06:45 +00:00
|
|
|
if (entry.cmdtype == CMDUNKNOWN)
|
|
|
|
errors = 1;
|
|
|
|
else if (verbose) {
|
|
|
|
cmdp = cmdlookup(name, 0);
|
|
|
|
if (cmdp != NULL)
|
|
|
|
printentry(cmdp, verbose);
|
|
|
|
else {
|
|
|
|
outfmt(out2, "%s: not found\n", name);
|
|
|
|
errors = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
flushall();
|
|
|
|
}
|
|
|
|
argptr++;
|
|
|
|
}
|
2012-02-11 21:06:45 +00:00
|
|
|
return errors;
|
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
|
|
|
printentry(struct tblentry *cmdp, int verbose)
|
|
|
|
{
|
2009-12-27 18:04:05 +00:00
|
|
|
int idx;
|
2018-07-15 21:55:17 +00:00
|
|
|
const char *path, *opt;
|
1994-05-26 06:18:55 +00:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (cmdp->cmdtype == CMDNORMAL) {
|
2009-12-27 18:04:05 +00:00
|
|
|
idx = cmdp->param.index;
|
1994-05-26 06:18:55 +00:00
|
|
|
path = pathval();
|
|
|
|
do {
|
2018-07-15 21:55:17 +00:00
|
|
|
name = padvance(&path, &opt, cmdp->cmdname);
|
1994-05-26 06:18:55 +00:00
|
|
|
stunalloc(name);
|
2009-12-27 18:04:05 +00:00
|
|
|
} while (--idx >= 0);
|
1994-05-26 06:18:55 +00:00
|
|
|
out1str(name);
|
|
|
|
} else if (cmdp->cmdtype == CMDBUILTIN) {
|
|
|
|
out1fmt("builtin %s", cmdp->cmdname);
|
|
|
|
} else if (cmdp->cmdtype == CMDFUNCTION) {
|
|
|
|
out1fmt("function %s", cmdp->cmdname);
|
|
|
|
if (verbose) {
|
|
|
|
INTOFF;
|
2009-08-28 22:41:25 +00:00
|
|
|
name = commandtext(getfuncnode(cmdp->param.func));
|
1994-05-26 06:18:55 +00:00
|
|
|
out1c(' ');
|
|
|
|
out1str(name);
|
|
|
|
ckfree(name);
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
} else {
|
|
|
|
error("internal error: cmdtype %d", cmdp->cmdtype);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
out1c('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resolve a command name. If you change this routine, you may have to
|
|
|
|
* change the shellexec routine as well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2010-03-06 16:57:53 +00:00
|
|
|
find_command(const char *name, struct cmdentry *entry, int act,
|
2009-12-24 18:41:14 +00:00
|
|
|
const char *path)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2010-03-06 16:57:53 +00:00
|
|
|
struct tblentry *cmdp, loc_cmd;
|
2009-12-27 18:04:05 +00:00
|
|
|
int idx;
|
2018-07-15 21:55:17 +00:00
|
|
|
const char *opt;
|
1994-05-26 06:18:55 +00:00
|
|
|
char *fullname;
|
|
|
|
struct stat statb;
|
|
|
|
int e;
|
|
|
|
int i;
|
2006-04-09 12:21:20 +00:00
|
|
|
int spec;
|
2013-01-14 16:40:50 +00:00
|
|
|
int cd;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/* If name contains a slash, don't use the hash table */
|
|
|
|
if (strchr(name, '/') != NULL) {
|
|
|
|
entry->cmdtype = CMDNORMAL;
|
|
|
|
entry->u.index = 0;
|
2016-03-13 22:54:14 +00:00
|
|
|
entry->special = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-14 16:40:50 +00:00
|
|
|
cd = 0;
|
|
|
|
|
2017-05-06 13:28:42 +00:00
|
|
|
/* If name is in the table, we're done */
|
2013-01-14 16:40:50 +00:00
|
|
|
if ((cmdp = cmdlookup(name, 0)) != NULL) {
|
2010-03-06 16:57:53 +00:00
|
|
|
if (cmdp->cmdtype == CMDFUNCTION && act & DO_NOFUNC)
|
|
|
|
cmdp = NULL;
|
|
|
|
else
|
|
|
|
goto success;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2011-01-09 21:07:30 +00:00
|
|
|
/* Check for builtin next */
|
|
|
|
if ((i = find_builtin(name, &spec)) >= 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
|
|
|
cmdp = cmdlookup(name, 1);
|
2010-03-06 16:57:53 +00:00
|
|
|
if (cmdp->cmdtype == CMDFUNCTION)
|
|
|
|
cmdp = &loc_cmd;
|
1994-05-26 06:18:55 +00:00
|
|
|
cmdp->cmdtype = CMDBUILTIN;
|
|
|
|
cmdp->param.index = i;
|
2006-04-09 12:21:20 +00:00
|
|
|
cmdp->special = spec;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have to search path. */
|
|
|
|
|
|
|
|
e = ENOENT;
|
2009-12-27 18:04:05 +00:00
|
|
|
idx = -1;
|
2018-07-15 21:55:17 +00:00
|
|
|
for (;(fullname = padvance(&path, &opt, name)) != NULL;
|
|
|
|
stunalloc(fullname)) {
|
2009-12-27 18:04:05 +00:00
|
|
|
idx++;
|
2018-07-15 21:55:17 +00:00
|
|
|
if (opt) {
|
|
|
|
if (strncmp(opt, "func", 4) == 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
/* handled below */
|
|
|
|
} else {
|
2014-05-31 22:25:45 +00:00
|
|
|
continue; /* ignore unimplemented options */
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-14 16:40:50 +00:00
|
|
|
if (fullname[0] != '/')
|
|
|
|
cd = 1;
|
2002-07-19 08:09:04 +00:00
|
|
|
if (stat(fullname, &statb) < 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (errno != ENOENT && errno != ENOTDIR)
|
|
|
|
e = errno;
|
2014-05-31 22:25:45 +00:00
|
|
|
continue;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
e = EACCES; /* if we fail, this will be the error */
|
1996-09-01 10:22:36 +00:00
|
|
|
if (!S_ISREG(statb.st_mode))
|
2014-05-31 22:25:45 +00:00
|
|
|
continue;
|
2018-07-15 21:55:17 +00:00
|
|
|
if (opt) { /* this is a %func directory */
|
1994-05-26 06:18:55 +00:00
|
|
|
readcmdfile(fullname);
|
|
|
|
if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
|
|
|
|
error("%s not defined in %s", name, fullname);
|
|
|
|
stunalloc(fullname);
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
#ifdef notdef
|
|
|
|
if (statb.st_uid == geteuid()) {
|
|
|
|
if ((statb.st_mode & 0100) == 0)
|
|
|
|
goto loop;
|
|
|
|
} else if (statb.st_gid == getegid()) {
|
|
|
|
if ((statb.st_mode & 010) == 0)
|
|
|
|
goto loop;
|
|
|
|
} else {
|
|
|
|
if ((statb.st_mode & 01) == 0)
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
|
|
|
|
INTOFF;
|
2014-05-31 22:25:45 +00:00
|
|
|
stunalloc(fullname);
|
1994-05-26 06:18:55 +00:00
|
|
|
cmdp = cmdlookup(name, 1);
|
2010-03-06 16:57:53 +00:00
|
|
|
if (cmdp->cmdtype == CMDFUNCTION)
|
|
|
|
cmdp = &loc_cmd;
|
1994-05-26 06:18:55 +00:00
|
|
|
cmdp->cmdtype = CMDNORMAL;
|
2009-12-27 18:04:05 +00:00
|
|
|
cmdp->param.index = idx;
|
2016-03-13 22:54:14 +00:00
|
|
|
cmdp->special = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
2010-03-06 16:57:53 +00:00
|
|
|
if (act & DO_ERR) {
|
2002-10-01 11:48:18 +00:00
|
|
|
if (e == ENOENT || e == ENOTDIR)
|
|
|
|
outfmt(out2, "%s: not found\n", name);
|
|
|
|
else
|
|
|
|
outfmt(out2, "%s: %s\n", name, strerror(e));
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
entry->cmdtype = CMDUNKNOWN;
|
2009-10-06 22:00:14 +00:00
|
|
|
entry->u.index = 0;
|
2016-03-13 22:54:14 +00:00
|
|
|
entry->special = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
success:
|
2013-01-14 16:40:50 +00:00
|
|
|
if (cd)
|
|
|
|
cmdtable_cd = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
entry->cmdtype = cmdp->cmdtype;
|
|
|
|
entry->u = cmdp->param;
|
2006-04-09 12:21:20 +00:00
|
|
|
entry->special = cmdp->special;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search the table of builtin commands.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-12-24 18:41:14 +00:00
|
|
|
find_builtin(const char *name, int *special)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2016-01-03 21:30:22 +00:00
|
|
|
const unsigned char *bp;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strlen(name);
|
|
|
|
for (bp = builtincmd ; *bp ; bp += 2 + bp[0]) {
|
|
|
|
if (bp[0] == len && memcmp(bp + 2, name, len) == 0) {
|
|
|
|
*special = (bp[1] & BUILTIN_SPECIAL) != 0;
|
|
|
|
return bp[1] & ~BUILTIN_SPECIAL;
|
2006-04-09 12:21:20 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-01-14 16:40:50 +00:00
|
|
|
* Called when a cd is done. If any entry in cmdtable depends on the current
|
|
|
|
* directory, simply clear cmdtable completely.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
hashcd(void)
|
|
|
|
{
|
2013-01-14 16:40:50 +00:00
|
|
|
if (cmdtable_cd)
|
|
|
|
clearcmdentry();
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called before PATH is changed. The argument is the new value of PATH;
|
|
|
|
* pathval() still returns the old value at this point. Called with
|
|
|
|
* interrupts off.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2012-01-25 08:42:19 +00:00
|
|
|
changepath(const char *newval __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2011-02-05 14:08:51 +00:00
|
|
|
clearcmdentry();
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-05-06 13:28:42 +00:00
|
|
|
* Clear out cached utility locations.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1999-12-20 13:42:59 +00:00
|
|
|
void
|
2011-02-05 14:08:51 +00:00
|
|
|
clearcmdentry(void)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct tblentry **tblp;
|
|
|
|
struct tblentry **pp;
|
|
|
|
struct tblentry *cmdp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
|
|
|
|
pp = tblp;
|
|
|
|
while ((cmdp = *pp) != NULL) {
|
2011-02-05 14:08:51 +00:00
|
|
|
if (cmdp->cmdtype == CMDNORMAL) {
|
1994-05-26 06:18:55 +00:00
|
|
|
*pp = cmdp->next;
|
|
|
|
ckfree(cmdp);
|
|
|
|
} else {
|
|
|
|
pp = &cmdp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 16:40:50 +00:00
|
|
|
cmdtable_cd = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Locate a command in the command hash table. If "add" is nonzero,
|
|
|
|
* add the command to the table if it is not already present. The
|
|
|
|
* variable "lastcmdentry" is set to point to the address of the link
|
|
|
|
* pointing to the entry, so that delete_cmd_entry can delete the
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static struct tblentry **lastcmdentry;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static struct tblentry *
|
2009-12-24 18:41:14 +00:00
|
|
|
cmdlookup(const char *name, int add)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2015-06-24 20:51:48 +00:00
|
|
|
unsigned int hashval;
|
2009-12-24 18:41:14 +00:00
|
|
|
const char *p;
|
1994-05-26 06:18:55 +00:00
|
|
|
struct tblentry *cmdp;
|
|
|
|
struct tblentry **pp;
|
2013-11-30 21:27:11 +00:00
|
|
|
size_t len;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
p = name;
|
2015-06-24 20:51:48 +00:00
|
|
|
hashval = (unsigned char)*p << 4;
|
1994-05-26 06:18:55 +00:00
|
|
|
while (*p)
|
|
|
|
hashval += *p++;
|
|
|
|
pp = &cmdtable[hashval % CMDTABLESIZE];
|
|
|
|
for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
|
|
|
|
if (equal(cmdp->cmdname, name))
|
|
|
|
break;
|
|
|
|
pp = &cmdp->next;
|
|
|
|
}
|
|
|
|
if (add && cmdp == NULL) {
|
|
|
|
INTOFF;
|
2013-11-30 21:27:11 +00:00
|
|
|
len = strlen(name);
|
|
|
|
cmdp = *pp = ckmalloc(sizeof (struct tblentry) + len + 1);
|
1994-05-26 06:18:55 +00:00
|
|
|
cmdp->next = NULL;
|
|
|
|
cmdp->cmdtype = CMDUNKNOWN;
|
2013-11-30 21:27:11 +00:00
|
|
|
memcpy(cmdp->cmdname, name, len + 1);
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
lastcmdentry = pp;
|
|
|
|
return cmdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete the command entry returned on the last lookup.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
delete_cmd_entry(void)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct tblentry *cmdp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
cmdp = *lastcmdentry;
|
|
|
|
*lastcmdentry = cmdp->next;
|
|
|
|
ckfree(cmdp);
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new command entry, replacing any existing command entry for
|
|
|
|
* the same name.
|
|
|
|
*/
|
|
|
|
|
2012-01-01 22:17:12 +00:00
|
|
|
static void
|
2009-12-24 18:41:14 +00:00
|
|
|
addcmdentry(const char *name, struct cmdentry *entry)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct tblentry *cmdp;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
cmdp = cmdlookup(name, 1);
|
|
|
|
if (cmdp->cmdtype == CMDFUNCTION) {
|
2009-08-23 21:09:46 +00:00
|
|
|
unreffunc(cmdp->param.func);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
cmdp->cmdtype = entry->cmdtype;
|
|
|
|
cmdp->param = entry->u;
|
2016-03-13 22:54:14 +00:00
|
|
|
cmdp->special = entry->special;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define a shell function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2009-12-24 18:41:14 +00:00
|
|
|
defun(const char *name, union node *func)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct cmdentry entry;
|
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
entry.cmdtype = CMDFUNCTION;
|
|
|
|
entry.u.func = copyfunc(func);
|
2016-03-13 22:54:14 +00:00
|
|
|
entry.special = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
addcmdentry(name, &entry);
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete a function if it exists.
|
2014-03-26 20:43:40 +00:00
|
|
|
* Called with interrupts off.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-12-24 18:41:14 +00:00
|
|
|
unsetfunc(const char *name)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct tblentry *cmdp;
|
|
|
|
|
|
|
|
if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
|
2009-08-23 21:09:46 +00:00
|
|
|
unreffunc(cmdp->param.func);
|
1994-05-26 06:18:55 +00:00
|
|
|
delete_cmd_entry();
|
|
|
|
return (0);
|
|
|
|
}
|
2004-09-27 18:43:18 +00:00
|
|
|
return (0);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
1997-04-28 03:08:38 +00:00
|
|
|
|
2012-07-15 10:19:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if a function by a certain name exists.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
isfunc(const char *name)
|
|
|
|
{
|
|
|
|
struct tblentry *cmdp;
|
|
|
|
cmdp = cmdlookup(name, 0);
|
|
|
|
return (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-28 03:08:38 +00:00
|
|
|
/*
|
2005-10-28 18:37:09 +00:00
|
|
|
* Shared code for the following builtin commands:
|
|
|
|
* type, command -v, command -V
|
1997-04-28 03:08:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-12-31 16:13:33 +00:00
|
|
|
typecmd_impl(int argc, char **argv, int cmd, const char *path)
|
1997-04-28 03:08:38 +00:00
|
|
|
{
|
|
|
|
struct cmdentry entry;
|
|
|
|
struct tblentry *cmdp;
|
2009-12-27 18:04:05 +00:00
|
|
|
const char *const *pp;
|
1997-04-28 03:08:38 +00:00
|
|
|
struct alias *ap;
|
|
|
|
int i;
|
2009-12-27 18:04:05 +00:00
|
|
|
int error1 = 0;
|
1997-04-28 03:08:38 +00:00
|
|
|
|
2009-12-31 16:13:33 +00:00
|
|
|
if (path != pathval())
|
2011-02-05 14:08:51 +00:00
|
|
|
clearcmdentry();
|
2009-12-31 16:13:33 +00:00
|
|
|
|
1997-04-28 03:08:38 +00:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
/* First look at the keywords */
|
2009-12-27 18:04:05 +00:00
|
|
|
for (pp = parsekwd; *pp; pp++)
|
1997-04-28 03:08:38 +00:00
|
|
|
if (**pp == *argv[i] && equal(*pp, argv[i]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*pp) {
|
2005-10-28 18:37:09 +00:00
|
|
|
if (cmd == TYPECMD_SMALLV)
|
|
|
|
out1fmt("%s\n", argv[i]);
|
|
|
|
else
|
2008-11-28 18:55:42 +00:00
|
|
|
out1fmt("%s is a shell keyword\n", argv[i]);
|
1997-04-28 03:08:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then look at the aliases */
|
|
|
|
if ((ap = lookupalias(argv[i], 1)) != NULL) {
|
2013-11-10 23:00:39 +00:00
|
|
|
if (cmd == TYPECMD_SMALLV) {
|
|
|
|
out1fmt("alias %s=", argv[i]);
|
|
|
|
out1qstr(ap->val);
|
|
|
|
outcslow('\n', out1);
|
|
|
|
} else
|
2008-11-28 18:55:42 +00:00
|
|
|
out1fmt("%s is an alias for %s\n", argv[i],
|
|
|
|
ap->val);
|
1997-04-28 03:08:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then check if it is a tracked alias */
|
|
|
|
if ((cmdp = cmdlookup(argv[i], 0)) != NULL) {
|
|
|
|
entry.cmdtype = cmdp->cmdtype;
|
|
|
|
entry.u = cmdp->param;
|
2009-06-24 22:04:04 +00:00
|
|
|
entry.special = cmdp->special;
|
1997-04-28 03:08:38 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Finally use brute force */
|
2009-12-31 16:13:33 +00:00
|
|
|
find_command(argv[i], &entry, 0, path);
|
1997-04-28 03:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (entry.cmdtype) {
|
|
|
|
case CMDNORMAL: {
|
2000-08-16 12:08:02 +00:00
|
|
|
if (strchr(argv[i], '/') == NULL) {
|
2009-12-31 16:13:33 +00:00
|
|
|
const char *path2 = path;
|
2018-07-15 21:55:17 +00:00
|
|
|
const char *opt2;
|
2009-12-24 18:41:14 +00:00
|
|
|
char *name;
|
2000-08-16 12:08:02 +00:00
|
|
|
int j = entry.u.index;
|
|
|
|
do {
|
2018-07-15 21:55:17 +00:00
|
|
|
name = padvance(&path2, &opt2, argv[i]);
|
2000-08-16 12:08:02 +00:00
|
|
|
stunalloc(name);
|
|
|
|
} while (--j >= 0);
|
2005-10-28 18:37:09 +00:00
|
|
|
if (cmd == TYPECMD_SMALLV)
|
|
|
|
out1fmt("%s\n", name);
|
|
|
|
else
|
2008-11-28 18:55:42 +00:00
|
|
|
out1fmt("%s is%s %s\n", argv[i],
|
2005-10-28 18:37:09 +00:00
|
|
|
(cmdp && cmd == TYPECMD_TYPE) ?
|
|
|
|
" a tracked alias for" : "",
|
|
|
|
name);
|
2000-08-16 12:08:02 +00:00
|
|
|
} else {
|
2007-01-18 22:31:22 +00:00
|
|
|
if (eaccess(argv[i], X_OK) == 0) {
|
2005-10-28 18:37:09 +00:00
|
|
|
if (cmd == TYPECMD_SMALLV)
|
|
|
|
out1fmt("%s\n", argv[i]);
|
|
|
|
else
|
2008-11-28 18:55:42 +00:00
|
|
|
out1fmt("%s is %s\n", argv[i],
|
|
|
|
argv[i]);
|
2007-01-11 00:19:00 +00:00
|
|
|
} else {
|
|
|
|
if (cmd != TYPECMD_SMALLV)
|
2008-11-28 18:55:42 +00:00
|
|
|
outfmt(out2, "%s: %s\n",
|
|
|
|
argv[i], strerror(errno));
|
2009-12-27 18:04:05 +00:00
|
|
|
error1 |= 127;
|
2005-10-28 18:37:09 +00:00
|
|
|
}
|
2000-08-16 12:08:02 +00:00
|
|
|
}
|
1997-04-28 03:08:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMDFUNCTION:
|
2005-10-28 18:37:09 +00:00
|
|
|
if (cmd == TYPECMD_SMALLV)
|
|
|
|
out1fmt("%s\n", argv[i]);
|
|
|
|
else
|
2008-11-28 18:55:42 +00:00
|
|
|
out1fmt("%s is a shell function\n", argv[i]);
|
1997-04-28 03:08:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMDBUILTIN:
|
2005-10-28 18:37:09 +00:00
|
|
|
if (cmd == TYPECMD_SMALLV)
|
|
|
|
out1fmt("%s\n", argv[i]);
|
2009-06-24 22:04:04 +00:00
|
|
|
else if (entry.special)
|
|
|
|
out1fmt("%s is a special shell builtin\n",
|
|
|
|
argv[i]);
|
2005-10-28 18:37:09 +00:00
|
|
|
else
|
2008-11-28 18:55:42 +00:00
|
|
|
out1fmt("%s is a shell builtin\n", argv[i]);
|
1997-04-28 03:08:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2005-10-28 18:37:09 +00:00
|
|
|
if (cmd != TYPECMD_SMALLV)
|
2008-11-28 18:55:42 +00:00
|
|
|
outfmt(out2, "%s: not found\n", argv[i]);
|
2009-12-27 18:04:05 +00:00
|
|
|
error1 |= 127;
|
1997-04-28 03:08:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-12-31 16:13:33 +00:00
|
|
|
|
|
|
|
if (path != pathval())
|
2011-02-05 14:08:51 +00:00
|
|
|
clearcmdentry();
|
2009-12-31 16:13:33 +00:00
|
|
|
|
2009-12-27 18:04:05 +00:00
|
|
|
return error1;
|
1997-04-28 03:08:38 +00:00
|
|
|
}
|
2005-10-28 18:37:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Locate and print what a word is...
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
typecmd(int argc, char **argv)
|
|
|
|
{
|
2013-08-30 12:09:59 +00:00
|
|
|
if (argc > 2 && strcmp(argv[1], "--") == 0)
|
|
|
|
argc--, argv++;
|
2009-12-31 17:44:24 +00:00
|
|
|
return typecmd_impl(argc, argv, TYPECMD_TYPE, bltinlookup("PATH", 1));
|
2005-10-28 18:37:09 +00:00
|
|
|
}
|