#include "sh.h"
RCSID("$Id: tw.init.c,v 1.1.1.2 2001/06/28 23:10:57 bbraun Exp $")
#include "tw.h"
#include "ed.h"
#include "tc.h"
#include "sh.proc.h"
#if !defined(NSIG) && defined(SIGMAX)
# define NSIG (SIGMAX+1)
#endif
#if !defined(NSIG) && defined(_NSIG)
# define NSIG _NSIG
#endif
#define TW_INCR 128
typedef struct {
Char **list,
*buff;
int nlist,
nbuff,
tlist,
tbuff;
} stringlist_t;
static struct varent *tw_vptr = NULL;
static Char **tw_env = NULL;
static Char *tw_word;
static struct KeyFuncs *tw_bind = NULL;
#ifndef HAVENOLIMIT
static struct limits *tw_limit = NULL;
#endif
static int tw_index = 0;
static DIR *tw_dir_fd = NULL;
static Char tw_retname[MAXPATHLEN+1];
static int tw_cmd_got = 0;
static stringlist_t tw_cmd = { NULL, NULL, 0, 0, 0, 0 };
static stringlist_t tw_item = { NULL, NULL, 0, 0, 0, 0 };
#define TW_FL_CMD 0x01
#define TW_FL_ALIAS 0x02
#define TW_FL_BUILTIN 0x04
#define TW_FL_SORT 0x08
#define TW_FL_REL 0x10
static struct {
int cur;
Char **pathv;
DIR *dfd;
} tw_cmd_state;
#ifdef BSDSIGS
static sigmask_t tw_omask;
# define TW_HOLD() tw_omask = sigblock(sigmask(SIGINT))
# define TW_RELS() (void) sigsetmask(tw_omask)
#else
# define TW_HOLD() (void) sighold(SIGINT)
# define TW_RELS() (void) sigrelse(SIGINT)
#endif
#define SETDIR(dfd) \
{ \
tw_dir_fd = dfd; \
if (tw_dir_fd != NULL) \
rewinddir(tw_dir_fd); \
}
#define CLRDIR(dfd) \
if (dfd != NULL) { \
TW_HOLD(); \
(void) closedir(dfd); \
dfd = NULL; \
TW_RELS(); \
}
static Char *tw_str_add __P((stringlist_t *, int));
static void tw_str_free __P((stringlist_t *));
static Char *tw_dir_next __P((DIR *));
static void tw_cmd_add __P((Char *name));
static void tw_cmd_cmd __P((void));
static void tw_cmd_builtin __P((void));
static void tw_cmd_alias __P((void));
static void tw_cmd_sort __P((void));
static void tw_vptr_start __P((struct varent *));
static Char *
tw_str_add(sl, len)
stringlist_t *sl;
int len;
{
Char *ptr;
if (sl->tlist <= sl->nlist) {
TW_HOLD();
sl->tlist += TW_INCR;
sl->list = sl->list ?
(Char **) xrealloc((ptr_t) sl->list,
(size_t) (sl->tlist * sizeof(Char *))) :
(Char **) xmalloc((size_t) (sl->tlist * sizeof(Char *)));
TW_RELS();
}
if (sl->tbuff <= sl->nbuff + len) {
int i;
ptr = sl->buff;
TW_HOLD();
sl->tbuff += TW_INCR + len;
sl->buff = sl->buff ?
(Char *) xrealloc((ptr_t) sl->buff,
(size_t) (sl->tbuff * sizeof(Char))) :
(Char *) xmalloc((size_t) (sl->tbuff * sizeof(Char)));
if (ptr != NULL && ptr != sl->buff) {
int offs = (int) (sl->buff - ptr);
for (i = 0; i < sl->nlist; i++)
sl->list[i] += offs;
}
TW_RELS();
}
ptr = sl->list[sl->nlist++] = &sl->buff[sl->nbuff];
sl->nbuff += len;
return ptr;
}
static void
tw_str_free(sl)
stringlist_t *sl;
{
TW_HOLD();
if (sl->list) {
xfree((ptr_t) sl->list);
sl->list = NULL;
sl->tlist = sl->nlist = 0;
}
if (sl->buff) {
xfree((ptr_t) sl->buff);
sl->buff = NULL;
sl->tbuff = sl->nbuff = 0;
}
TW_RELS();
}
static Char *
tw_dir_next(dfd)
DIR *dfd;
{
register struct dirent *dirp;
if (dfd == NULL)
return NULL;
if ((dirp = readdir(dfd)) != NULL) {
(void) Strcpy(tw_retname, str2short(dirp->d_name));
return (tw_retname);
}
return NULL;
}
static void
tw_cmd_add(name)
Char *name;
{
int len;
len = (int) Strlen(name) + 2;
(void) Strcpy(tw_str_add(&tw_cmd, len), name);
}
void
tw_cmd_free()
{
CLRDIR(tw_dir_fd)
tw_str_free(&tw_cmd);
tw_cmd_got = 0;
}
static void
tw_cmd_cmd()
{
register DIR *dirp;
register struct dirent *dp;
register Char *dir = NULL, *name;
register Char **pv;
struct varent *v = adrof(STRpath);
struct varent *recexec = adrof(STRrecognize_only_executables);
int len;
if (v == NULL)
return;
for (pv = v->vec; *pv; pv++) {
if (pv[0][0] != '/') {
tw_cmd_got |= TW_FL_REL;
continue;
}
if ((dirp = opendir(short2str(*pv))) == NULL)
continue;
if (recexec)
dir = Strspl(*pv, STRslash);
while ((dp = readdir(dirp)) != NULL) {
#if defined(_UWIN) || defined(__CYGWIN__)
size_t ext = strlen(dp->d_name) - 4;
if ((ext > 0) && (strcmp(&dp->d_name[ext], ".exe") == 0 ||
strcmp(&dp->d_name[ext], ".bat") == 0 ||
strcmp(&dp->d_name[ext], ".com") == 0))
dp->d_name[ext] = '\0';
#endif
name = str2short(dp->d_name);
if (dp->d_ino == 0 || (recexec && !executable(dir, name, 0)))
continue;
len = (int) Strlen(name) + 2;
if (name[0] == '#' ||
name[0] == '.' ||
name[len - 3] == '~' ||
name[len - 3] == '%')
continue;
tw_cmd_add(name);
}
(void) closedir(dirp);
if (recexec)
xfree((ptr_t) dir);
}
}
static void
tw_cmd_builtin()
{
register struct biltins *bptr;
for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++)
if (bptr->bname)
tw_cmd_add(str2short(bptr->bname));
#ifdef WINNT_NATIVE
for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++)
if (bptr->bname)
tw_cmd_add(str2short(bptr->bname));
#endif
}
static void
tw_cmd_alias()
{
register struct varent *p;
register struct varent *c;
p = &aliases;
for (;;) {
while (p->v_left)
p = p->v_left;
x:
if (p->v_parent == 0)
return;
if (p->v_name)
tw_cmd_add(p->v_name);
if (p->v_right) {
p = p->v_right;
continue;
}
do {
c = p;
p = p->v_parent;
} while (p->v_right == c);
goto x;
}
}
static void
tw_cmd_sort()
{
int fwd, i;
TW_HOLD();
qsort((ptr_t) tw_cmd.list, (size_t) tw_cmd.nlist, sizeof(Char *),
(int (*) __P((const void *, const void *))) fcompare);
for (i = 0, fwd = 0; i < tw_cmd.nlist - 1; i++) {
if (Strcmp(tw_cmd.list[i], tw_cmd.list[i + 1]) == 0)
fwd++;
else if (fwd)
tw_cmd.list[i - fwd] = tw_cmd.list[i];
}
if (fwd)
tw_cmd.list[i - fwd] = tw_cmd.list[i];
tw_cmd.nlist -= fwd;
TW_RELS();
}
void
tw_cmd_start(dfd, pat)
DIR *dfd;
Char *pat;
{
static Char *defpath[] = { STRNULL, 0 };
USE(pat);
SETDIR(dfd)
if ((tw_cmd_got & TW_FL_CMD) == 0) {
tw_cmd_free();
tw_cmd_cmd();
tw_cmd_got |= TW_FL_CMD;
}
if ((tw_cmd_got & TW_FL_ALIAS) == 0) {
tw_cmd_alias();
tw_cmd_got &= ~TW_FL_SORT;
tw_cmd_got |= TW_FL_ALIAS;
}
if ((tw_cmd_got & TW_FL_BUILTIN) == 0) {
tw_cmd_builtin();
tw_cmd_got &= ~TW_FL_SORT;
tw_cmd_got |= TW_FL_BUILTIN;
}
if ((tw_cmd_got & TW_FL_SORT) == 0) {
tw_cmd_sort();
tw_cmd_got |= TW_FL_SORT;
}
tw_cmd_state.cur = 0;
CLRDIR(tw_cmd_state.dfd)
if (tw_cmd_got & TW_FL_REL) {
struct varent *vp = adrof(STRpath);
if (vp && vp->vec)
tw_cmd_state.pathv = vp->vec;
else
tw_cmd_state.pathv = defpath;
}
else
tw_cmd_state.pathv = defpath;
}
Char *
tw_cmd_next(dir, flags)
Char *dir;
int *flags;
{
Char *ptr = NULL;
if (tw_cmd_state.cur < tw_cmd.nlist) {
*flags = TW_DIR_OK;
return tw_cmd.list[tw_cmd_state.cur++];
}
while (((tw_cmd_state.dfd == NULL) ||
((ptr = tw_dir_next(tw_cmd_state.dfd)) == NULL)) &&
(*tw_cmd_state.pathv != NULL)) {
CLRDIR(tw_cmd_state.dfd)
while (*tw_cmd_state.pathv && tw_cmd_state.pathv[0][0] == '/')
tw_cmd_state.pathv++;
if ((ptr = *tw_cmd_state.pathv) != 0) {
if (ptr[0] == '\0' || (ptr[0] == '.' && ptr[1] == '\0')) {
*dir = '\0';
tw_cmd_state.dfd = opendir(".");
*flags = TW_DIR_OK | TW_EXEC_CHK;
}
else {
copyn(dir, *tw_cmd_state.pathv, FILSIZ);
catn(dir, STRslash, FILSIZ);
tw_cmd_state.dfd = opendir(short2str(*tw_cmd_state.pathv));
*flags = TW_EXEC_CHK;
}
tw_cmd_state.pathv++;
}
}
return ptr;
}
static void
tw_vptr_start(c)
struct varent *c;
{
tw_vptr = c;
for (;;) {
while (tw_vptr->v_left)
tw_vptr = tw_vptr->v_left;
x:
if (tw_vptr->v_parent == 0) {
tw_vptr = NULL;
return;
}
if (tw_vptr->v_name)
return;
if (tw_vptr->v_right) {
tw_vptr = tw_vptr->v_right;
continue;
}
do {
c = tw_vptr;
tw_vptr = tw_vptr->v_parent;
} while (tw_vptr->v_right == c);
goto x;
}
}
Char *
tw_shvar_next(dir, flags)
Char *dir;
int *flags;
{
register struct varent *p;
register struct varent *c;
register Char *cp;
USE(flags);
USE(dir);
if ((p = tw_vptr) == NULL)
return (NULL);
cp = p->v_name;
for (;;) {
if (p->v_right) {
p = p->v_right;
while (p->v_left)
p = p->v_left;
}
else {
do {
c = p;
p = p->v_parent;
} while (p->v_right == c);
}
if (p->v_parent == 0) {
tw_vptr = NULL;
return (cp);
}
if (p->v_name) {
tw_vptr = p;
return (cp);
}
}
}
Char *
tw_envvar_next(dir, flags)
Char *dir;
int *flags;
{
Char *ps, *pd;
USE(flags);
USE(dir);
if (tw_env == NULL || *tw_env == NULL)
return (NULL);
for (ps = *tw_env, pd = tw_retname;
*ps && *ps != '=' && pd <= &tw_retname[MAXPATHLEN]; *pd++ = *ps++)
continue;
*pd = '\0';
tw_env++;
return (tw_retname);
}
void
tw_var_start(dfd, pat)
DIR *dfd;
Char *pat;
{
USE(pat);
SETDIR(dfd)
tw_vptr_start(&shvhed);
tw_env = STR_environ;
}
void
tw_alias_start(dfd, pat)
DIR *dfd;
Char *pat;
{
USE(pat);
SETDIR(dfd)
tw_vptr_start(&aliases);
tw_env = NULL;
}
void
tw_complete_start(dfd, pat)
DIR *dfd;
Char *pat;
{
extern struct varent completions;
USE(pat);
SETDIR(dfd)
tw_vptr_start(&completions);
tw_env = NULL;
}
Char *
tw_var_next(dir, flags)
Char *dir;
int *flags;
{
Char *ptr = NULL;
if (tw_vptr)
ptr = tw_shvar_next(dir, flags);
if (!ptr && tw_env)
ptr = tw_envvar_next(dir, flags);
return ptr;
}
void
tw_logname_start(dfd, pat)
DIR *dfd;
Char *pat;
{
USE(pat);
SETDIR(dfd)
#if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
(void) setpwent();
#endif
}
Char *
tw_logname_next(dir, flags)
Char *dir;
int *flags;
{
static Char retname[MAXPATHLEN];
struct passwd *pw;
USE(flags);
USE(dir);
TW_HOLD();
#if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
pw = (struct passwd *) getpwent();
#else
pw = NULL;
#endif
TW_RELS();
if (pw == NULL) {
#ifdef YPBUGS
fix_yp_bugs();
#endif
return (NULL);
}
(void) Strcpy(retname, str2short(pw->pw_name));
return (retname);
}
void
tw_logname_end()
{
#ifdef YPBUGS
fix_yp_bugs();
#endif
#if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
(void) endpwent();
#endif
}
void
tw_grpname_start(dfd, pat)
DIR *dfd;
Char *pat;
{
USE(pat);
SETDIR(dfd)
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
(void) setgrent();
#endif
}
Char *
tw_grpname_next(dir, flags)
Char *dir;
int *flags;
{
static Char retname[MAXPATHLEN];
struct group *gr;
USE(flags);
USE(dir);
TW_HOLD();
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
gr = (struct group *) getgrent();
#else
gr = NULL;
#endif
TW_RELS();
if (gr == NULL) {
#ifdef YPBUGS
fix_yp_bugs();
#endif
return (NULL);
}
(void) Strcpy(retname, str2short(gr->gr_name));
return (retname);
}
void
tw_grpname_end()
{
#ifdef YPBUGS
fix_yp_bugs();
#endif
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
(void) endgrent();
#endif
}
void
tw_file_start(dfd, pat)
DIR *dfd;
Char *pat;
{
struct varent *vp;
USE(pat);
SETDIR(dfd)
if ((vp = adrof(STRcdpath)) != NULL)
tw_env = vp->vec;
}
Char *
tw_file_next(dir, flags)
Char *dir;
int *flags;
{
Char *ptr = tw_dir_next(tw_dir_fd);
if (ptr == NULL && (*flags & TW_DIR_OK) != 0) {
CLRDIR(tw_dir_fd)
while (tw_env && *tw_env)
if ((tw_dir_fd = opendir(short2str(*tw_env))) != NULL)
break;
else
tw_env++;
if (tw_dir_fd) {
copyn(dir, *tw_env++, MAXPATHLEN);
catn(dir, STRslash, MAXPATHLEN);
ptr = tw_dir_next(tw_dir_fd);
}
}
return ptr;
}
void
tw_dir_end()
{
CLRDIR(tw_dir_fd)
CLRDIR(tw_cmd_state.dfd)
}
void
tw_item_free()
{
tw_str_free(&tw_item);
}
Char **
tw_item_get()
{
return tw_item.list;
}
Char *
tw_item_add(len)
int len;
{
return tw_str_add(&tw_item, len);
}
Char *
tw_item_find(str)
Char *str;
{
int i;
if (tw_item.list == NULL || str == NULL)
return NULL;
for (i = 0; i < tw_item.nlist; i++)
if (tw_item.list[i] != NULL && Strcmp(tw_item.list[i], str) == 0)
return tw_item.list[i];
return NULL;
}
void
tw_vl_start(dfd, pat)
DIR *dfd;
Char *pat;
{
SETDIR(dfd)
if ((tw_vptr = adrof(pat)) != NULL) {
tw_env = tw_vptr->vec;
tw_vptr = NULL;
}
else
tw_env = NULL;
}
void
tw_wl_start(dfd, pat)
DIR *dfd;
Char *pat;
{
SETDIR(dfd);
tw_word = pat;
}
Char *
tw_wl_next(dir, flags)
Char *dir;
int *flags;
{
USE(flags);
if (tw_word == NULL || tw_word[0] == '\0')
return NULL;
while (*tw_word && Isspace(*tw_word)) tw_word++;
for (dir = tw_word; *tw_word && !Isspace(*tw_word); tw_word++)
continue;
if (*tw_word)
*tw_word++ = '\0';
return *dir ? dir : NULL;
}
void
tw_bind_start(dfd, pat)
DIR *dfd;
Char *pat;
{
USE(pat);
SETDIR(dfd)
tw_bind = FuncNames;
}
Char *
tw_bind_next(dir, flags)
Char *dir;
int *flags;
{
char *ptr;
USE(flags);
if (tw_bind && tw_bind->name) {
for (ptr = tw_bind->name, dir = tw_retname;
(*dir++ = (Char) *ptr++) != '\0';)
continue;
tw_bind++;
return(tw_retname);
}
return NULL;
}
void
tw_limit_start(dfd, pat)
DIR *dfd;
Char *pat;
{
USE(pat);
SETDIR(dfd)
#ifndef HAVENOLIMIT
tw_limit = limits;
#endif
}
Char *
tw_limit_next(dir, flags)
Char *dir;
int *flags;
{
#ifndef HAVENOLIMIT
char *ptr;
if (tw_limit && tw_limit->limname) {
for (ptr = tw_limit->limname, dir = tw_retname;
(*dir++ = (Char) *ptr++) != '\0';)
continue;
tw_limit++;
return(tw_retname);
}
#endif
USE(flags);
return NULL;
}
void
tw_sig_start(dfd, pat)
DIR *dfd;
Char *pat;
{
USE(pat);
SETDIR(dfd)
tw_index = 0;
}
Char *
tw_sig_next(dir, flags)
Char *dir;
int *flags;
{
char *ptr;
extern int nsig;
USE(flags);
for (;tw_index < nsig; tw_index++) {
if (mesg[tw_index].iname == NULL)
continue;
for (ptr = mesg[tw_index].iname, dir = tw_retname;
(*dir++ = (Char) *ptr++) != '\0';)
continue;
tw_index++;
return(tw_retname);
}
return NULL;
}
void
tw_job_start(dfd, pat)
DIR *dfd;
Char *pat;
{
USE(pat);
SETDIR(dfd)
tw_index = 1;
}
Char *
tw_job_next(dir, flags)
Char *dir;
int *flags;
{
Char *ptr;
struct process *j;
USE(flags);
for (;tw_index <= pmaxindex; tw_index++) {
for (j = proclist.p_next; j != NULL; j = j->p_next)
if (j->p_index == tw_index && j->p_procid == j->p_jobid)
break;
if (j == NULL)
continue;
for (ptr = j->p_command, dir = tw_retname; (*dir++ = *ptr++) != '\0';)
continue;
*dir = '\0';
tw_index++;
return(tw_retname);
}
return NULL;
}