#include "sh.h"
#include <dirent.h>
#include <string.h>
#include "sh.tconst.h"
char xhash[HSHSIZ / 8];
tchar **Vav;
tchar *Vdp;
tchar *Vsav;
struct varent aliases;
bool havhash;
static int hits;
static int misses;
short SHOUT;
short SHIN;
void (*parintr)();
void (*parterm)();
char *exerr;
void pexerr(void);
void texec(struct command *, tchar *, tchar **);
void xechoit(tchar **);
void dohash(char []);
static void tconvert(struct command *, tchar *, tchar **);
extern DIR *opendir_(tchar *);
void
doexec(struct command *t)
{
tchar *sav;
tchar *dp, **pv, **av;
struct varent *v;
bool slash;
int hashval, hashval1, i;
tchar *blk[2];
#ifdef TRACE
tprintf("TRACE- doexec()\n");
#endif
dp = globone(t->t_dcom[0]);
sav = t->t_dcom[0];
exerr = 0; t->t_dcom[0] = dp;
setname(dp);
xfree(sav);
v = adrof(S_path );
if (v == 0 && dp[0] != '/') {
pexerr();
}
slash = gflag;
gflag = 0; av = &t->t_dcom[1];
tglob(av);
if (gflag) {
av = glob(av);
if (av == 0)
error("No match");
}
blk[0] = t->t_dcom[0];
blk[1] = 0;
av = blkspl(blk, av);
#ifdef VFORK
Vav = av;
#endif
trim(av);
slash |= any('/', av[0]);
xechoit(av);
SHIN = 0; SHOUT = 1; SHDIAG = 2; OLDSTD = 0;
(void) sigsetmask(0);
if (v == 0 || v->vec[0] == 0 || slash)
pv = justabs;
else
pv = v->vec;
sav = strspl(S_SLASH , *av);
#ifdef VFORK
Vsav = sav;
#endif
if (havhash)
hashval = hashname(*av);
i = 0;
#ifdef VFORK
hits++;
#endif
do {
if (!slash && pv[0][0] == '/' && havhash) {
hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
goto cont;
}
if (pv[0][0] == 0 || eq(pv[0], S_DOT )) {
texec(t, *av, av);
} else {
dp = strspl(*pv, sav);
#ifdef VFORK
Vdp = dp;
#endif
texec(t, dp, av);
#ifdef VFORK
Vdp = 0;
#endif
xfree(dp);
}
#ifdef VFORK
misses++;
#endif
cont:
pv++;
i++;
} while (*pv);
#ifdef VFORK
hits--;
#endif
#ifdef VFORK
Vsav = 0;
Vav = 0;
#endif
xfree(sav);
xfree((char *)av);
pexerr();
}
void
pexerr(void)
{
#ifdef TRACE
tprintf("TRACE- pexerr()\n");
#endif
if (exerr)
bferr(exerr);
bferr("Command not found");
}
void
texec(struct command *cmd, tchar *f, tchar **t)
{
struct varent *v;
tchar **vp;
tchar *lastsh[2];
#ifdef TRACE
tprintf("TRACE- texec()\n");
#endif
tconvert(cmd, f, t);
execv(cmd->cfname, cmd->cargs);
xfree(cmd->cfname);
chr_blkfree(cmd->cargs);
cmd->cfname = (char *)0;
cmd->cargs = (char **)0;
switch (errno) {
case ENOEXEC:
{
int ff = open_(f, 0);
tchar ch[MB_LEN_MAX];
if (ff != -1 && read_(ff, ch, 1) == 1 &&
!isprint(ch[0]) && !isspace(ch[0])) {
printf("Cannot execute binary file.\n");
Perror(f);
(void) close(ff);
unsetfd(ff);
return;
}
(void) close(ff);
unsetfd(ff);
}
v = adrof1(S_shell , &aliases);
if (v == 0) {
#ifdef OTHERSH
int ff = open_(f, 0);
tchar ch[MB_LEN_MAX];
#endif
vp = lastsh;
vp[0] = adrof(S_shell ) ?
value(S_shell ) :
S_SHELLPATH ;
vp[1] = (tchar *) NULL;
#ifdef OTHERSH
if (ff != -1 && read_(ff, ch, 1) == 1 && ch[0] != '#')
vp[0] = S_OTHERSH ;
(void) close(ff);
unsetfd(ff);
#endif
} else
vp = v->vec;
t[0] = f;
t = blkspl(vp, t);
f = *t;
tconvert(cmd, f, t);
xfree((char *)t);
execv(cmd->cfname, cmd->cargs);
xfree(cmd->cfname);
chr_blkfree(cmd->cargs);
cmd->cfname = (char *)0;
cmd->cargs = (char **)0;
case ENOMEM:
Perror(f);
case ENOENT:
break;
default:
if (exerr == 0) {
exerr = strerror(errno);
setname(f);
}
}
}
static void
tconvert(struct command *cmd, tchar *fname, tchar **list)
{
char **rc;
int len;
cmd->cfname = tstostr(NULL, fname);
len = blklen(list);
rc = cmd->cargs = (char **)
xcalloc((uint_t)(len + 1), sizeof (char **));
while (len--)
*rc++ = tstostr(NULL, *list++);
*rc = NULL;
}
void
execash(tchar **t, struct command *kp)
{
#ifdef TRACE
tprintf("TRACE- execash()\n");
#endif
rechist();
(void) signal(SIGINT, parintr);
(void) signal(SIGQUIT, parintr);
(void) signal(SIGTERM, parterm);
lshift(kp->t_dcom, 1);
exiterr++;
doexec(kp);
}
void
xechoit(tchar **t)
{
#ifdef TRACE
tprintf("TRACE- xechoit()\n");
#endif
if (adrof(S_echo )) {
flush();
haderr = 1;
blkpr(t), Putchar('\n');
haderr = 0;
}
}
void
dorehash(void)
{
dohash(xhash);
dohash(xhash2);
}
void
dohash(char cachearray[])
{
struct stat stb;
DIR *dirp;
struct dirent *dp;
int cnt;
int i = 0;
struct varent *v;
tchar **pv;
int hashval;
tchar curdir_[MAXNAMLEN+1];
#ifdef TRACE
tprintf("TRACE- dohash()\n");
#endif
if (cachearray == xhash) {
havhash = 1;
v = adrof(S_path );
} else {
havhash2 = 1;
v = adrof(S_cdpath );
}
for (cnt = 0; cnt < (HSHSIZ / 8); cnt++)
cachearray[cnt] = 0;
if (v == 0)
return;
for (pv = v->vec; *pv; pv++, i++) {
if (pv[0][0] != '/')
continue;
dirp = opendir_(*pv);
if (dirp == NULL)
continue;
if (fstat(dirp->dd_fd, &stb) < 0 || !isdir(stb)) {
unsetfd(dirp->dd_fd);
closedir_(dirp);
continue;
}
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;
hashval = hash(hashname(strtots(curdir_, dp->d_name)),
i);
bis(cachearray, hashval);
}
unsetfd(dirp->dd_fd);
closedir_(dirp);
}
}
void
dounhash(void)
{
#ifdef TRACE
tprintf("TRACE- dounhash()\n");
#endif
havhash = 0;
havhash2 = 0;
}
#ifdef VFORK
void
hashstat(void)
{
#ifdef TRACE
tprintf("TRACE- hashstat_()\n");
#endif
if (hits+misses)
printf("%d hits, %d misses, %d%%\n",
hits, misses, 100 * hits / (hits + misses));
}
#endif
int
hashname(tchar *cp)
{
long h = 0;
#ifdef TRACE
tprintf("TRACE- hashname()\n");
#endif
while (*cp)
h = hash(h, *cp++);
return ((int)h);
}