#include "sh.h"
RCSID("$tcsh: sh.exec.c,v 3.75 2009/06/25 21:15:37 christos Exp $")
#include "tc.h"
#include "tw.h"
#ifdef WINNT_NATIVE
#include <nt.const.h>
#endif
#ifndef OLDHASH
# define FASTHASH
#endif
static char *exerr;
static Char *expath;
#define BITS_PER_BYTE 8
#ifdef FASTHASH
# define HSHMUL 241
static unsigned long *xhash = NULL;
static unsigned int hashlength = 0, uhashlength = 0;
static unsigned int hashwidth = 0, uhashwidth = 0;
static int hashdebug = 0;
# define hash(a, b) (((a) * HSHMUL + (b)) % (hashlength))
# define widthof(t) (sizeof(t) * BITS_PER_BYTE)
# define tbit(f, i, t) (((t *) xhash)[(f)] & \
(1UL << (i & (widthof(t) - 1))))
# define tbis(f, i, t) (((t *) xhash)[(f)] |= \
(1UL << (i & (widthof(t) - 1))))
# define cbit(f, i) tbit(f, i, unsigned char)
# define cbis(f, i) tbis(f, i, unsigned char)
# define sbit(f, i) tbit(f, i, unsigned short)
# define sbis(f, i) tbis(f, i, unsigned short)
# define ibit(f, i) tbit(f, i, unsigned int)
# define ibis(f, i) tbis(f, i, unsigned int)
# define lbit(f, i) tbit(f, i, unsigned long)
# define lbis(f, i) tbis(f, i, unsigned long)
# define bit(f, i) (hashwidth==sizeof(unsigned char) ? cbit(f,i) : \
((hashwidth==sizeof(unsigned short) ? sbit(f,i) : \
((hashwidth==sizeof(unsigned int) ? ibit(f,i) : \
lbit(f,i))))))
# define bis(f, i) (hashwidth==sizeof(unsigned char) ? cbis(f,i) : \
((hashwidth==sizeof(unsigned short) ? sbis(f,i) : \
((hashwidth==sizeof(unsigned int) ? ibis(f,i) : \
lbis(f,i))))))
#else
# define HSHSIZ 8192
# define HSHMASK (HSHSIZ - 1)
# define HSHMUL 243
static char xhash[HSHSIZ / BITS_PER_BYTE];
# define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
# define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7))
# define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7))
#endif
#ifdef VFORK
static int hits, misses;
#endif
static Char *justabs[] = {STRNULL, 0};
static void pexerr (void);
static void texec (Char *, Char **);
int hashname (Char *);
static int iscommand (Char *);
void
doexec(struct command *t, int do_glob)
{
Char *dp, **pv, **av, *sav;
struct varent *v;
int slash, gflag;
int hashval, i;
Char *blk[2];
blk[0] = t->t_dcom[0];
blk[1] = 0;
gflag = 0;
if (do_glob)
gflag = tglob(blk);
if (gflag) {
pv = globall(blk, gflag);
if (pv == 0) {
setname(short2str(blk[0]));
stderror(ERR_NAME | ERR_NOMATCH);
}
}
else
pv = saveblk(blk);
cleanup_push(pv, blk_cleanup);
trim(pv);
exerr = 0;
expath = Strsave(pv[0]);
#ifdef VFORK
Vexpath = expath;
#endif
v = adrof(STRpath);
if (v == 0 && expath[0] != '/' && expath[0] != '.')
pexerr();
slash = any(short2str(expath), '/');
gflag = 0;
av = &t->t_dcom[1];
if (do_glob)
gflag = tglob(av);
if (gflag) {
av = globall(av, gflag);
if (av == 0) {
setname(short2str(expath));
stderror(ERR_NAME | ERR_NOMATCH);
}
}
else
av = saveblk(av);
blkfree(t->t_dcom);
cleanup_ignore(pv);
cleanup_until(pv);
t->t_dcom = blkspl(pv, av);
xfree(pv);
xfree(av);
av = t->t_dcom;
trim(av);
if (*av == NULL || **av == '\0')
pexerr();
xechoit(av);
#ifdef CLOSE_ON_EXEC
SHIN = 0;
SHOUT = 1;
SHDIAG = 2;
OLDSTD = 0;
isoutatty = isatty(SHOUT);
isdiagatty = isatty(SHDIAG);
#else
closech();
#endif
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_UNBLOCK, &set, NULL);
}
pintr_disabled = 0;
pchild_disabled = 0;
if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash)
pv = justabs;
else
pv = v->vec;
sav = Strspl(STRslash, *av);
#ifndef VFORK
cleanup_push(sav, xfree);
#else
Vsav = sav;
#endif
hashval = havhash ? hashname(*av) : 0;
i = 0;
#ifdef VFORK
hits++;
#endif
do {
if (!slash && ABSOLUTEP(pv[0]) && havhash) {
#ifdef FASTHASH
if (!bit(hashval, i))
goto cont;
#else
int hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
goto cont;
#endif
}
if (pv[0][0] == 0 || eq(pv[0], STRdot))
texec(*av, av);
else {
dp = Strspl(*pv, sav);
#ifndef VFORK
cleanup_push(dp, xfree);
#else
Vdp = dp;
#endif
texec(dp, av);
#ifndef VFORK
cleanup_until(dp);
#else
Vdp = 0;
xfree(dp);
#endif
}
#ifdef VFORK
misses++;
#endif
cont:
pv++;
i++;
} while (*pv);
#ifdef VFORK
hits--;
#endif
#ifndef VFORK
cleanup_until(sav);
#else
Vsav = 0;
xfree(sav);
#endif
pexerr();
}
static void
pexerr(void)
{
if (expath) {
setname(short2str(expath));
#ifdef VFORK
Vexpath = 0;
#endif
xfree(expath);
expath = 0;
}
else
setname("");
if (exerr)
stderror(ERR_NAME | ERR_STRING, exerr);
stderror(ERR_NAME | ERR_COMMAND);
}
static void
texec(Char *sf, Char **st)
{
char **t;
char *f;
struct varent *v;
Char **vp;
Char *lastsh[2];
char pref[2];
int fd;
Char *st0, **ost;
t = short2blk(st);
f = short2str(sf);
#ifdef VFORK
Vt = t;
#endif
errno = 0;
#ifdef apollo
{
struct stat stb;
if (stat(f, &stb) == 0 && S_ISDIR(stb.st_mode))
errno = EISDIR;
}
if (errno == 0)
#endif
{
#ifdef ISC_POSIX_EXEC_BUG
__setostype(0);
#endif
(void) execv(f, t);
#ifdef ISC_POSIX_EXEC_BUG
__setostype(1);
#endif
}
#ifdef VFORK
Vt = 0;
#endif
blkfree((Char **) t);
switch (errno) {
case ENOEXEC:
#ifdef WINNT_NATIVE
nt_feed_to_cmd(f,t);
#endif
if ((fd = xopen(f, O_RDONLY|O_LARGEFILE)) != -1) {
int nread;
if ((nread = xread(fd, pref, 2)) == 2) {
if (!isprint((unsigned char)pref[0]) &&
(pref[0] != '\n' && pref[0] != '\t')) {
int err;
err = errno;
xclose(fd);
stderror(ERR_ARCH, f, strerror(err));
}
}
else if (nread < 0) {
#ifdef convex
int err;
err = errno;
xclose(fd);
stderror(ERR_SYSTEM, f, strerror(err));
#endif
}
#ifdef _PATH_BSHELL
else {
pref[0] = '#';
pref[1] = '\0';
}
#endif
}
#ifdef HASHBANG
if (fd == -1 ||
pref[0] != '#' || pref[1] != '!' || hashbang(fd, &vp) == -1) {
#endif
v = adrof1(STRshell, &aliases);
if (v == NULL || v->vec == NULL) {
vp = lastsh;
vp[0] = adrof(STRshell) ? varval(STRshell) : STR_SHELLPATH;
vp[1] = NULL;
#ifdef _PATH_BSHELL
if (fd != -1
# ifndef ISC
&& pref[0] != '#'
# endif
)
vp[0] = STR_BSHELL;
#endif
vp = saveblk(vp);
}
else
vp = saveblk(v->vec);
#ifdef HASHBANG
}
#endif
if (fd != -1)
xclose(fd);
st0 = st[0];
st[0] = sf;
ost = st;
st = blkspl(vp, st);
ost[0] = st0;
sf = *st;
t = short2blk(st);
f = short2str(sf);
xfree(st);
blkfree((Char **) vp);
#ifdef VFORK
Vt = t;
#endif
#ifdef ISC_POSIX_EXEC_BUG
__setostype(0);
#endif
(void) execv(f, t);
#ifdef ISC_POSIX_EXEC_BUG
__setostype(1);
#endif
#ifdef VFORK
Vt = 0;
#endif
blkfree((Char **) t);
stderror(ERR_SYSTEM, f, strerror(errno));
break;
case ENOMEM:
stderror(ERR_SYSTEM, f, strerror(errno));
break;
#ifdef _IBMR2
case 0:
#endif
case ENOENT:
break;
default:
if (exerr == 0) {
exerr = strerror(errno);
xfree(expath);
expath = Strsave(sf);
#ifdef VFORK
Vexpath = expath;
#endif
}
break;
}
}
struct execash_state
{
int saveIN, saveOUT, saveDIAG, saveSTD;
int SHIN, SHOUT, SHDIAG, OLDSTD;
int didfds;
#ifndef CLOSE_ON_EXEC
int didcch;
#endif
struct sigaction sigint, sigquit, sigterm;
};
static void
execash_cleanup(void *xstate)
{
struct execash_state *state;
state = xstate;
sigaction(SIGINT, &state->sigint, NULL);
sigaction(SIGQUIT, &state->sigquit, NULL);
sigaction(SIGTERM, &state->sigterm, NULL);
doneinp = 0;
#ifndef CLOSE_ON_EXEC
didcch = state->didcch;
#endif
didfds = state->didfds;
xclose(SHIN);
xclose(SHOUT);
xclose(SHDIAG);
xclose(OLDSTD);
close_on_exec(SHIN = dmove(state->saveIN, state->SHIN), 1);
close_on_exec(SHOUT = dmove(state->saveOUT, state->SHOUT), 1);
close_on_exec(SHDIAG = dmove(state->saveDIAG, state->SHDIAG), 1);
close_on_exec(OLDSTD = dmove(state->saveSTD, state->OLDSTD), 1);
}
void
execash(Char **t, struct command *kp)
{
struct execash_state state;
USE(t);
if (chkstop == 0 && setintr)
panystop(0);
rechist(NULL, adrof(STRsavehist) != NULL);
sigaction(SIGINT, &parintr, &state.sigint);
sigaction(SIGQUIT, &parintr, &state.sigquit);
sigaction(SIGTERM, &parterm, &state.sigterm);
state.didfds = didfds;
#ifndef CLOSE_ON_EXEC
state.didcch = didcch;
#endif
state.SHIN = SHIN;
state.SHOUT = SHOUT;
state.SHDIAG = SHDIAG;
state.OLDSTD = OLDSTD;
(void)close_on_exec (state.saveIN = dcopy(SHIN, -1), 1);
(void)close_on_exec (state.saveOUT = dcopy(SHOUT, -1), 1);
(void)close_on_exec (state.saveDIAG = dcopy(SHDIAG, -1), 1);
(void)close_on_exec (state.saveSTD = dcopy(OLDSTD, -1), 1);
lshift(kp->t_dcom, 1);
(void)close_on_exec (SHIN = dcopy(0, -1), 1);
(void)close_on_exec (SHOUT = dcopy(1, -1), 1);
(void)close_on_exec (SHDIAG = dcopy(2, -1), 1);
#ifndef CLOSE_ON_EXEC
didcch = 0;
#endif
didfds = 0;
cleanup_push(&state, execash_cleanup);
shlvl(-1);
#ifdef WINNT_NATIVE
__nt_really_exec=1;
#endif
doexec(kp, 1);
cleanup_until(&state);
}
void
xechoit(Char **t)
{
if (adrof(STRecho)) {
int odidfds = didfds;
flush();
haderr = 1;
didfds = 0;
blkpr(t), xputchar('\n');
flush();
didfds = odidfds;
haderr = 0;
}
}
void
dohash(Char **vv, struct command *c)
{
#ifdef COMMENT
struct stat stb;
#endif
DIR *dirp;
struct dirent *dp;
int i = 0;
struct varent *v = adrof(STRpath);
Char **pv;
int hashval;
#ifdef WINNT_NATIVE
int is_windir;
USE(hashval);
#endif
USE(c);
#ifdef FASTHASH
if (vv && vv[1]) {
uhashlength = atoi(short2str(vv[1]));
if (vv[2]) {
uhashwidth = atoi(short2str(vv[2]));
if ((uhashwidth != sizeof(unsigned char)) &&
(uhashwidth != sizeof(unsigned short)) &&
(uhashwidth != sizeof(unsigned long)))
uhashwidth = 0;
if (vv[3])
hashdebug = atoi(short2str(vv[3]));
}
}
if (uhashwidth)
hashwidth = uhashwidth;
else {
hashwidth = 0;
if (v == NULL)
return;
for (pv = v->vec; pv && *pv; pv++, hashwidth++)
continue;
if (hashwidth <= widthof(unsigned char))
hashwidth = sizeof(unsigned char);
else if (hashwidth <= widthof(unsigned short))
hashwidth = sizeof(unsigned short);
else if (hashwidth <= widthof(unsigned int))
hashwidth = sizeof(unsigned int);
else
hashwidth = sizeof(unsigned long);
}
if (uhashlength)
hashlength = uhashlength;
else
hashlength = hashwidth * (8*64);
xfree(xhash);
xhash = xcalloc(hashlength * hashwidth, 1);
#endif
(void) getusername(NULL);
tw_cmd_free();
havhash = 1;
if (v == NULL)
return;
for (pv = v->vec; pv && *pv; pv++, i++) {
if (!ABSOLUTEP(pv[0]))
continue;
dirp = opendir(short2str(*pv));
if (dirp == NULL)
continue;
cleanup_push(dirp, opendir_cleanup);
#ifdef COMMENT
if (fstat(dirp->dd_fd, &stb) < 0 || !S_ISDIR(stb.st_mode)) {
cleanup_until(dirp);
continue;
}
#endif
#ifdef WINNT_NATIVE
is_windir = nt_check_if_windir(short2str(*pv));
#endif
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_ino == 0)
continue;
if (dp->d_name[0] == '.' &&
(dp->d_name[1] == '\0' ||
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
continue;
#ifdef WINNT_NATIVE
nt_check_name_and_hash(is_windir, dp->d_name, i);
#else
#if defined(_UWIN) || defined(__CYGWIN__)
{
ssize_t ext = strlen(dp->d_name) - 4;
if ((ext > 0) && (strcasecmp(&dp->d_name[ext], ".exe") == 0 ||
strcasecmp(&dp->d_name[ext], ".bat") == 0 ||
strcasecmp(&dp->d_name[ext], ".com") == 0)) {
#ifdef __CYGWIN__
hashval = hashname(str2short(dp->d_name));
bis(hashval, i);
#endif
dp->d_name[ext] = '\0';
}
}
#endif
# ifdef FASTHASH
hashval = hashname(str2short(dp->d_name));
bis(hashval, i);
if (hashdebug & 1)
xprintf(CGETS(13, 1, "hash=%-4d dir=%-2d prog=%s\n"),
hashname(str2short(dp->d_name)), i, dp->d_name);
# else
hashval = hash(hashname(str2short(dp->d_name)), i);
bis(xhash, hashval);
# endif
#endif
}
cleanup_until(dirp);
}
}
void
dounhash(Char **v, struct command *c)
{
USE(c);
USE(v);
havhash = 0;
#ifdef FASTHASH
xfree(xhash);
xhash = NULL;
#endif
}
void
hashstat(Char **v, struct command *c)
{
USE(c);
USE(v);
#ifdef FASTHASH
if (havhash && hashlength && hashwidth)
xprintf(CGETS(13, 2, "%d hash buckets of %d bits each\n"),
hashlength, hashwidth*8);
if (hashdebug)
xprintf(CGETS(13, 3, "debug mask = 0x%08x\n"), hashdebug);
#endif
#ifdef VFORK
if (hits + misses)
xprintf(CGETS(13, 4, "%d hits, %d misses, %d%%\n"),
hits, misses, 100 * hits / (hits + misses));
#endif
}
int
hashname(Char *cp)
{
unsigned long h;
for (h = 0; *cp; cp++)
h = hash(h, *cp);
return ((int) h);
}
static int
iscommand(Char *name)
{
Char **pv;
Char *sav;
struct varent *v;
int slash = any(short2str(name), '/');
int hashval, i;
v = adrof(STRpath);
if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash)
pv = justabs;
else
pv = v->vec;
sav = Strspl(STRslash, name);
hashval = havhash ? hashname(name) : 0;
i = 0;
do {
if (!slash && ABSOLUTEP(pv[0]) && havhash) {
#ifdef FASTHASH
if (!bit(hashval, i))
goto cont;
#else
int hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
goto cont;
#endif
}
if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
if (executable(NULL, name, 0)) {
xfree(sav);
return i + 1;
}
}
else {
if (executable(*pv, sav, 0)) {
xfree(sav);
return i + 1;
}
}
cont:
pv++;
i++;
} while (*pv);
xfree(sav);
return 0;
}
#ifndef WINNT_NATIVE
int
executable(const Char *dir, const Char *name, int dir_ok)
{
struct stat stbuf;
char *strname;
if (dir && *dir) {
Char *path;
path = Strspl(dir, name);
strname = short2str(path);
xfree(path);
}
else
strname = short2str(name);
return (stat(strname, &stbuf) != -1 &&
((dir_ok && S_ISDIR(stbuf.st_mode)) ||
(S_ISREG(stbuf.st_mode) &&
(stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
access(strname, X_OK) == 0
)));
}
#endif
struct tellmewhat_s0_cleanup
{
Char **dest, *val;
};
static void
tellmewhat_s0_cleanup(void *xstate)
{
struct tellmewhat_s0_cleanup *state;
state = xstate;
*state->dest = state->val;
}
int
tellmewhat(struct wordent *lexp, Char **str)
{
struct tellmewhat_s0_cleanup s0;
int i;
const struct biltins *bptr;
struct wordent *sp = lexp->next;
int aliased = 0, found;
Char *s1, *s2, *cmd;
Char qc;
if (adrof1(sp->word, &aliases)) {
alias(lexp);
sp = lexp->next;
aliased = 1;
}
s0.dest = &sp->word;
s0.val = sp->word;
cleanup_push(&s0, tellmewhat_s0_cleanup);
if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
(sp->word)++;
s1 = s2 = sp->word;
while (*s2)
switch (*s2) {
case '\'':
case '"':
qc = *s2++;
while (*s2 && *s2 != qc)
*s1++ = *s2++ | QUOTE;
if (*s2)
s2++;
break;
case '\\':
if (*++s2)
*s1++ = *s2++ | QUOTE;
break;
default:
*s1++ = *s2++;
}
*s1 = '\0';
for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
if (eq(sp->word, str2short(bptr->bname))) {
if (str == NULL) {
if (aliased)
prlex(lexp);
xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
sp->word);
flush();
}
else
*str = Strsave(sp->word);
cleanup_until(&s0);
return TRUE;
}
}
#ifdef WINNT_NATIVE
for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++) {
if (eq(sp->word, str2short(bptr->bname))) {
if (str == NULL) {
if (aliased)
prlex(lexp);
xprintf(CGETS(13, 5, "%S: shell built-in command.\n"),
sp->word);
flush();
}
else
*str = Strsave(sp->word);
cleanup_until(&s0);
return TRUE;
}
}
#endif
sp->word = cmd = globone(sp->word, G_IGNORE);
cleanup_push(cmd, xfree);
if ((i = iscommand(sp->word)) != 0) {
Char **pv;
struct varent *v;
int slash = any(short2str(sp->word), '/');
v = adrof(STRpath);
if (v == NULL || v->vec == NULL || v->vec[0] == NULL || slash)
pv = justabs;
else
pv = v->vec;
pv += i - 1;
if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
if (!slash) {
sp->word = Strspl(STRdotsl, sp->word);
cleanup_push(sp->word, xfree);
prlex(lexp);
cleanup_until(sp->word);
}
else
prlex(lexp);
}
else {
s1 = Strspl(*pv, STRslash);
sp->word = Strspl(s1, sp->word);
xfree(s1);
cleanup_push(sp->word, xfree);
if (str == NULL)
prlex(lexp);
else
*str = Strsave(sp->word);
cleanup_until(sp->word);
}
found = 1;
}
else {
if (str == NULL) {
if (aliased)
prlex(lexp);
xprintf(CGETS(13, 6, "%S: Command not found.\n"), sp->word);
flush();
}
else
*str = Strsave(sp->word);
found = 0;
}
cleanup_until(&s0);
return found;
}
void
dowhere(Char **v, struct command *c)
{
int found = 1;
USE(c);
for (v++; *v; v++)
found &= find_cmd(*v, 1);
if (!found)
setcopy(STRstatus, STR1, VAR_READWRITE);
}
int
find_cmd(Char *cmd, int prt)
{
struct varent *var;
const struct biltins *bptr;
Char **pv;
Char *sv;
int hashval, i, ex, rval = 0;
if (prt && any(short2str(cmd), '/')) {
xprintf("%s", CGETS(13, 7, "where: / in command makes no sense\n"));
return rval;
}
if (prt && adrof1(cmd, &aliases)) {
if ((var = adrof1(cmd, &aliases)) != NULL) {
xprintf(CGETS(13, 8, "%S is aliased to "), cmd);
if (var->vec != NULL)
blkpr(var->vec);
xputchar('\n');
rval = 1;
}
}
for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
if (eq(cmd, str2short(bptr->bname))) {
rval = 1;
if (prt)
xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd);
else
return rval;
}
}
#ifdef WINNT_NATIVE
for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++) {
if (eq(cmd, str2short(bptr->bname))) {
rval = 1;
if (prt)
xprintf(CGETS(13, 9, "%S is a shell built-in\n"), cmd);
else
return rval;
}
}
#endif
if ((var = adrof(STRpath)) == NULL)
return rval;
hashval = havhash ? hashname(cmd) : 0;
sv = Strspl(STRslash, cmd);
cleanup_push(sv, xfree);
for (pv = var->vec, i = 0; pv && *pv; pv++, i++) {
if (havhash && !eq(*pv, STRdot)) {
#ifdef FASTHASH
if (!bit(hashval, i))
continue;
#else
int hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
continue;
#endif
}
ex = executable(*pv, sv, 0);
#ifdef FASTHASH
if (!ex && (hashdebug & 2)) {
xprintf("%s", CGETS(13, 10, "hash miss: "));
ex = 1;
}
#endif
if (ex) {
rval = 1;
if (prt) {
xprintf("%S/", *pv);
xprintf("%S\n", cmd);
}
else
return rval;
}
}
cleanup_until(sv);
return rval;
}
#ifdef WINNT_NATIVE
int hashval_extern(cp)
Char *cp;
{
return havhash?hashname(cp):0;
}
int bit_extern(val,i)
int val;
int i;
{
return bit(val,i);
}
void bis_extern(val,i)
int val;
int i;
{
bis(val,i);
}
#endif