#include "sh.h"
RCSID("$tcsh: tw.init.c,v 3.39 2006/03/02 18:46:45 christos Exp $")
#include "tw.h"
#include "ed.h"
#include "tc.h"
#include "sh.proc.h"
#define TW_INCR 128
typedef struct {
Char **list,
*buff;
size_t nlist,
nbuff,
tlist,
tbuff;
} stringlist_t;
static struct varent *tw_vptr = NULL;
static Char **tw_env = NULL;
static const 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 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 {
size_t cur;
Char **pathv;
DIR *dfd;
} tw_cmd_state;
#define SETDIR(dfd) \
{ \
tw_dir_fd = dfd; \
if (tw_dir_fd != NULL) \
rewinddir(tw_dir_fd); \
}
#define CLRDIR(dfd) \
if (dfd != NULL) { \
pintr_disabled++; \
xclosedir(dfd); \
dfd = NULL; \
disabled_cleanup(&pintr_disabled); \
}
static Char *tw_str_add (stringlist_t *, size_t);
static void tw_str_free (stringlist_t *);
static int tw_dir_next (struct Strbuf *, DIR *);
static void tw_cmd_add (const Char *name);
static void tw_cmd_cmd (void);
static void tw_cmd_builtin (void);
static void tw_cmd_alias (void);
static void tw_cmd_sort (void);
static void tw_vptr_start (struct varent *);
static Char *
tw_str_add(stringlist_t *sl, size_t len)
{
Char *ptr;
if (sl->tlist <= sl->nlist) {
pintr_disabled++;
sl->tlist += TW_INCR;
sl->list = xrealloc(sl->list, sl->tlist * sizeof(Char *));
disabled_cleanup(&pintr_disabled);
}
if (sl->tbuff <= sl->nbuff + len) {
size_t i;
ptr = sl->buff;
pintr_disabled++;
sl->tbuff += TW_INCR + len;
sl->buff = xrealloc(sl->buff, sl->tbuff * sizeof(Char));
if (ptr != NULL && ptr != sl->buff) {
intptr_t offs = sl->buff - ptr;
for (i = 0; i < sl->nlist; i++)
sl->list[i] += offs;
}
disabled_cleanup(&pintr_disabled);
}
ptr = sl->list[sl->nlist++] = &sl->buff[sl->nbuff];
sl->nbuff += len;
return ptr;
}
static void
tw_str_free(stringlist_t *sl)
{
pintr_disabled++;
if (sl->list) {
xfree(sl->list);
sl->list = NULL;
sl->tlist = sl->nlist = 0;
}
if (sl->buff) {
xfree(sl->buff);
sl->buff = NULL;
sl->tbuff = sl->nbuff = 0;
}
disabled_cleanup(&pintr_disabled);
}
static int
tw_dir_next(struct Strbuf *res, DIR *dfd)
{
struct dirent *dirp;
if (dfd == NULL)
return 0;
if ((dirp = readdir(dfd)) != NULL) {
Strbuf_append(res, str2short(dirp->d_name));
return 1;
}
return 0;
}
static void
tw_cmd_add(const Char *name)
{
size_t len;
len = Strlen(name) + 2;
(void) Strcpy(tw_str_add(&tw_cmd, len), name);
}
void
tw_cmd_free(void)
{
CLRDIR(tw_dir_fd)
tw_str_free(&tw_cmd);
tw_cmd_got = 0;
}
static void
tw_cmd_cmd(void)
{
DIR *dirp;
struct dirent *dp;
Char *dir = NULL, *name;
Char **pv;
struct varent *v = adrof(STRpath);
struct varent *recexec = adrof(STRrecognize_only_executables);
size_t len;
if (v == NULL || v->vec == 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;
cleanup_push(dirp, opendir_cleanup);
if (recexec) {
dir = Strspl(*pv, STRslash);
cleanup_push(dir, xfree);
}
while ((dp = readdir(dirp)) != NULL) {
#if defined(_UWIN) || defined(__CYGWIN__)
len = strlen(dp->d_name);
if (len > 4 && (strcmp(&dp->d_name[len - 4], ".exe") == 0 ||
strcmp(&dp->d_name[len - 4], ".bat") == 0 ||
strcmp(&dp->d_name[len - 4], ".com") == 0))
dp->d_name[len - 4] = '\0';
#endif
name = str2short(dp->d_name);
if (dp->d_ino == 0 || (recexec && !executable(dir, name, 0)))
continue;
len = Strlen(name);
if (name[0] == '#' ||
name[0] == '.' ||
name[len - 1] == '~' ||
name[len - 1] == '%')
continue;
tw_cmd_add(name);
}
cleanup_until(dirp);
}
}
static void
tw_cmd_builtin(void)
{
const 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(void)
{
struct varent *p;
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(void)
{
size_t fwd, i;
pintr_disabled++;
qsort(tw_cmd.list, tw_cmd.nlist, sizeof(Char *), fcompare);
for (i = 0, fwd = 0; i + 1 < tw_cmd.nlist; 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;
disabled_cleanup(&pintr_disabled);
}
void
tw_cmd_start(DIR *dfd, const 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;
}
int
tw_cmd_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
int ret = 0;
Char *ptr;
if (tw_cmd_state.cur < tw_cmd.nlist) {
*flags = TW_DIR_OK;
Strbuf_append(res, tw_cmd.list[tw_cmd_state.cur++]);
return 1;
}
while ((tw_cmd_state.dfd == NULL ||
(ret = tw_dir_next(res, tw_cmd_state.dfd)) == 0) &&
*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) {
Strbuf_append(res, ptr);
ret = 1;
dir->len = 0;
if (ptr[0] == '\0' || (ptr[0] == '.' && ptr[1] == '\0')) {
tw_cmd_state.dfd = opendir(".");
*flags = TW_DIR_OK | TW_EXEC_CHK;
}
else {
Strbuf_append(dir, *tw_cmd_state.pathv);
Strbuf_append1(dir, '/');
tw_cmd_state.dfd = opendir(short2str(*tw_cmd_state.pathv));
*flags = TW_EXEC_CHK;
}
Strbuf_terminate(dir);
tw_cmd_state.pathv++;
}
}
return ret;
}
static void
tw_vptr_start(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;
}
}
int
tw_shvar_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
struct varent *p;
struct varent *c;
USE(flags);
USE(dir);
if ((p = tw_vptr) == NULL)
return 0;
Strbuf_append(res, 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 1;
}
if (p->v_name) {
tw_vptr = p;
return 1;
}
}
}
int
tw_envvar_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
const Char *ps;
USE(flags);
USE(dir);
if (tw_env == NULL || *tw_env == NULL)
return 0;
for (ps = *tw_env; *ps && *ps != '='; ps++)
continue;
Strbuf_appendn(res, *tw_env, ps - *tw_env);
tw_env++;
return 1;
}
void
tw_var_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
tw_vptr_start(&shvhed);
tw_env = STR_environ;
}
void
tw_alias_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
tw_vptr_start(&aliases);
tw_env = NULL;
}
void
tw_complete_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
tw_vptr_start(&completions);
tw_env = NULL;
}
int
tw_var_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
int ret = 0;
if (tw_vptr)
ret = tw_shvar_next(res, dir, flags);
if (ret == 0 && tw_env)
ret = tw_envvar_next(res, dir, flags);
return ret;
}
void
tw_logname_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
#ifdef HAVE_GETPWENT
(void) setpwent();
#endif
}
int
tw_logname_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
struct passwd *pw;
USE(flags);
USE(dir);
pintr_disabled++;
#ifdef HAVE_GETPWENT
pw = getpwent();
#else
pw = NULL;
#endif
disabled_cleanup(&pintr_disabled);
if (pw == NULL) {
#ifdef YPBUGS
fix_yp_bugs();
#endif
return 0;
}
Strbuf_append(res, str2short(pw->pw_name));
return 1;
}
void
tw_logname_end(void)
{
#ifdef YPBUGS
fix_yp_bugs();
#endif
#ifdef HAVE_GETPWENT
(void) endpwent();
#endif
}
void
tw_grpname_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
(void) setgrent();
#endif
}
int
tw_grpname_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
struct group *gr;
USE(flags);
USE(dir);
pintr_disabled++;
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
errno = 0;
while ((gr = getgrent()) == NULL && errno == EINTR) {
handle_pending_signals();
errno = 0;
}
#else
gr = NULL;
#endif
disabled_cleanup(&pintr_disabled);
if (gr == NULL) {
#ifdef YPBUGS
fix_yp_bugs();
#endif
return 0;
}
Strbuf_append(res, str2short(gr->gr_name));
return 1;
}
void
tw_grpname_end(void)
{
#ifdef YPBUGS
fix_yp_bugs();
#endif
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
(void) endgrent();
#endif
}
void
tw_file_start(DIR *dfd, const Char *pat)
{
struct varent *vp;
USE(pat);
SETDIR(dfd)
if ((vp = adrof(STRcdpath)) != NULL)
tw_env = vp->vec;
}
int
tw_file_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
int ret = tw_dir_next(res, tw_dir_fd);
if (ret == 0 && (*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) {
dir->len = 0;
Strbuf_append(dir, *tw_env++);
Strbuf_append1(dir, '/');
Strbuf_terminate(dir);
ret = tw_dir_next(res, tw_dir_fd);
}
}
return ret;
}
void
tw_dir_end(void)
{
CLRDIR(tw_dir_fd)
CLRDIR(tw_cmd_state.dfd)
}
void
tw_item_free(void)
{
tw_str_free(&tw_item);
}
Char **
tw_item_get(void)
{
return tw_item.list;
}
void
tw_item_add(const struct Strbuf *s)
{
Char *p;
p = tw_str_add(&tw_item, s->len + 1);
Strcpy(p, s->s);
}
Char *
tw_item_find(Char *str)
{
size_t 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(DIR *dfd, const 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(DIR *dfd, const Char *pat)
{
SETDIR(dfd);
tw_word = pat;
}
int
tw_wl_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
const Char *p;
USE(dir);
USE(flags);
if (tw_word == NULL || tw_word[0] == '\0')
return 0;
while (*tw_word && Isspace(*tw_word)) tw_word++;
for (p = tw_word; *tw_word && !Isspace(*tw_word); tw_word++)
continue;
if (tw_word == p)
return 0;
Strbuf_appendn(res, p, tw_word - p);
if (*tw_word)
tw_word++;
return 1;
}
void
tw_bind_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
tw_bind = FuncNames;
}
int
tw_bind_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
USE(dir);
USE(flags);
if (tw_bind && tw_bind->name) {
const char *ptr;
for (ptr = tw_bind->name; *ptr != '\0'; ptr++)
Strbuf_append1(res, *ptr);
tw_bind++;
return 1;
}
return 0;
}
void
tw_limit_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
#ifndef HAVENOLIMIT
tw_limit = limits;
#endif
}
int
tw_limit_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
USE(dir);
USE(flags);
#ifndef HAVENOLIMIT
if (tw_limit && tw_limit->limname) {
const char *ptr;
for (ptr = tw_limit->limname; *ptr != '\0'; ptr++)
Strbuf_append1(res, *ptr);
tw_limit++;
return 1;
}
#endif
return 0;
}
void
tw_sig_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
tw_index = 0;
}
int
tw_sig_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
USE(dir);
USE(flags);
for (;tw_index < nsig; tw_index++) {
const char *ptr;
if (mesg[tw_index].iname == NULL)
continue;
for (ptr = mesg[tw_index].iname; *ptr != '\0'; ptr++)
Strbuf_append1(res, *ptr);
tw_index++;
return 1;
}
return 0;
}
void
tw_job_start(DIR *dfd, const Char *pat)
{
USE(pat);
SETDIR(dfd)
tw_index = 1;
}
int
tw_job_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
{
struct process *j;
USE(dir);
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;
Strbuf_append(res, j->p_command);
tw_index++;
return 1;
}
return 0;
}