#include "sh.h"
RCSID("$Id: tw.parse.c,v 1.1.1.2 2001/06/28 23:10:57 bbraun Exp $")
#include "tw.h"
#include "ed.h"
#include "tc.h"
#ifdef WINNT_NATIVE
#include "nt.const.h"
#endif
#define EVEN(x) (((x) & 1) != 1)
#define DOT_NONE 0
#define DOT_NOT 1
#define DOT_ALL 2
static void (*tw_start_entry[]) __P((DIR *, Char *)) = {
tw_file_start, tw_cmd_start, tw_var_start, tw_logname_start,
tw_file_start, tw_file_start, tw_vl_start, tw_logname_start,
tw_complete_start, tw_alias_start, tw_var_start, tw_var_start,
tw_bind_start, tw_wl_start, tw_limit_start, tw_sig_start,
tw_job_start, tw_file_start, tw_file_start, tw_grpname_start
};
static Char * (*tw_next_entry[]) __P((Char *, int *)) = {
tw_file_next, tw_cmd_next, tw_var_next, tw_logname_next,
tw_file_next, tw_file_next, tw_var_next, tw_logname_next,
tw_var_next, tw_var_next, tw_shvar_next, tw_envvar_next,
tw_bind_next, tw_wl_next, tw_limit_next, tw_sig_next,
tw_job_next, tw_file_next, tw_file_next, tw_grpname_next
};
static void (*tw_end_entry[]) __P((void)) = {
tw_dir_end, tw_dir_end, tw_dir_end, tw_logname_end,
tw_dir_end, tw_dir_end, tw_dir_end, tw_logname_end,
tw_dir_end, tw_dir_end, tw_dir_end, tw_dir_end,
tw_dir_end, tw_dir_end, tw_dir_end, tw_dir_end,
tw_dir_end, tw_dir_end, tw_dir_end, tw_grpname_end
};
int curchoice = -1;
int match_unique_match = FALSE;
int non_unique_match = FALSE;
static bool SearchNoDirErr = 0;
int InsideCompletion = 0;
extern Char NeedsRedraw;
extern int Tty_raw_mode;
extern int TermH;
extern int lbuffed;
static void extract_dir_and_name __P((Char *, Char *, Char *));
static int insert_meta __P((Char *, Char *, Char *, bool));
static Char *tilde __P((Char *, Char *));
#ifndef __MVS__
static int expand_dir __P((Char *, Char *, DIR **, COMMAND));
#endif
static bool nostat __P((Char *));
static Char filetype __P((Char *, Char *));
static int t_glob __P((Char ***, int));
static int c_glob __P((Char ***));
static int is_prefix __P((Char *, Char *));
static int is_prefixmatch __P((Char *, Char *, int));
static int is_suffix __P((Char *, Char *));
static int recognize __P((Char *, Char *, int, int, int));
static int ignored __P((Char *));
static int isadirectory __P((Char *, Char *));
#ifndef __MVS__
static int tw_collect_items __P((COMMAND, int, Char *, Char *,
Char *, Char *, int));
static int tw_collect __P((COMMAND, int, Char *, Char *,
Char **, Char *, int, DIR *));
#endif
static Char tw_suffix __P((int, Char *, Char *, Char *,
Char *));
static void tw_fixword __P((int, Char *, Char *, Char *, int));
static void tw_list_items __P((int, int, int));
static void add_scroll_tab __P((Char *));
static void choose_scroll_tab __P((Char **, int));
static void free_scroll_tab __P((void));
static int find_rows __P((Char *[], int, int));
#ifdef notdef
#define isaset(c, w) ((w)[-1] == '=' && \
((c)[0] == 's' && (c)[1] == 'e' && (c)[2] == 't' && \
((c[3] == ' ' || (c)[3] == '\t'))))
#endif
#define QLINESIZE (INBUFSIZE + 1)
#define tricky(w) (cmap(w, _META | _DOL | _QF | _QB | _ESC | _GLOB) && w != '#')
#define tricky_dq(w) (cmap(w, _DOL | _QB))
int
tenematch(inputline, num_read, command)
Char *inputline;
int num_read;
COMMAND command;
{
Char qline[QLINESIZE];
Char qu = 0, *pat = STRNULL;
Char *str_end, *cp, *wp, *wordp;
Char *cmd_start, *word_start, *word;
Char *ocmd_start = NULL, *oword_start = NULL, *oword = NULL;
int suf = 0;
int space_left;
int looking;
int search_ret;
int backq = 0;
if (num_read > QLINESIZE - 1)
return -1;
str_end = &inputline[num_read];
word_start = inputline;
word = cmd_start = wp = qline;
for (cp = inputline; cp < str_end; cp++) {
if (!cmap(qu, _ESC)) {
if (cmap(*cp, _QF|_ESC)) {
if (qu == 0 || qu == *cp) {
qu ^= *cp;
continue;
}
}
if (qu != '\'' && cmap(*cp, _QB)) {
if ((backq ^= 1) != 0) {
ocmd_start = cmd_start;
oword_start = word_start;
oword = word;
word_start = cp + 1;
word = cmd_start = wp + 1;
}
else {
cmd_start = ocmd_start;
word_start = oword_start;
word = oword;
}
*wp++ = *cp;
continue;
}
}
if (iscmdmeta(*cp))
cmd_start = wp + 1;
if (cmap(*cp, _ESC) && cp < str_end - 1 && cp[1] == HIST)
*wp = *++cp | QUOTE;
else if (qu && (tricky(*cp) || *cp == '~') && !(qu == '\"' && tricky_dq(*cp)))
*wp = *cp | QUOTE;
else
*wp = *cp;
if (ismetahash(*wp) )
word = wp + 1, word_start = cp + 1;
wp++;
if (cmap(qu, _ESC))
qu = 0;
}
*wp = 0;
#ifdef masscomp
space_left = QLINESIZE - 1;
space_left -= word - qline;
#else
space_left = QLINESIZE - 1 - (int) (word - qline);
#endif
looking = starting_a_command(word - 1, qline) ?
TW_COMMAND : TW_ZERO;
wordp = word;
#ifdef TDEBUG
xprintf(CGETS(30, 1, "starting_a_command %d\n"), looking);
xprintf("\ncmd_start:%S:\n", cmd_start);
xprintf("qline:%S:\n", qline);
xprintf("qline:");
for (wp = qline; *wp; wp++)
xprintf("%c", *wp & QUOTE ? '-' : ' ');
xprintf(":\n");
xprintf("word:%S:\n", word);
xprintf("word:");
for (wp = word; *wp; wp++)
xprintf("%c", *wp & QUOTE ? '-' : ' ');
xprintf(":\n");
#endif
if ((looking == TW_COMMAND || looking == TW_ZERO) &&
(command == RECOGNIZE || command == LIST || command == SPELL ||
command == RECOGNIZE_SCROLL)) {
#ifdef TDEBUG
xprintf(CGETS(30, 2, "complete %d "), looking);
#endif
looking = tw_complete(cmd_start, &wordp, &pat, looking, &suf);
#ifdef TDEBUG
xprintf(CGETS(30, 3, "complete %d %S\n"), looking, pat);
#endif
}
switch (command) {
Char buffer[FILSIZ + 1], *bptr;
Char *slshp;
Char *items[2], **ptr;
int i, count;
case RECOGNIZE:
case RECOGNIZE_SCROLL:
case RECOGNIZE_ALL:
if (adrof(STRautocorrect)) {
if ((slshp = Strrchr(wordp, '/')) != NULL && slshp[1] != '\0') {
SearchNoDirErr = 1;
for (bptr = wordp; bptr < slshp; bptr++) {
if (isglob(*bptr)) {
SearchNoDirErr = 0;
break;
}
}
}
}
else
slshp = STRNULL;
search_ret = t_search(wordp, wp, command, space_left, looking, 1,
pat, suf);
SearchNoDirErr = 0;
if (search_ret == -2) {
Char rword[FILSIZ + 1];
(void) Strcpy(rword, slshp);
if (slshp != STRNULL)
*slshp = '\0';
search_ret = spell_me(wordp, QLINESIZE - (wordp - qline), looking,
pat, suf);
if (search_ret == 1) {
(void) Strcat(wordp, rword);
wp = wordp + (int) Strlen(wordp);
search_ret = t_search(wordp, wp, command, space_left,
looking, 1, pat, suf);
}
}
if (*wp && insert_meta(word_start, str_end, word, !qu) < 0)
return -1;
return search_ret;
case SPELL:
for (bptr = word_start; bptr < str_end; bptr++) {
if (isglob(*bptr))
return 0;
}
search_ret = spell_me(wordp, QLINESIZE - (wordp - qline), looking,
pat, suf);
if (search_ret == 1) {
if (insert_meta(word_start, str_end, word, !qu) < 0)
return -1;
}
return search_ret;
case PRINT_HELP:
do_help(cmd_start);
return 1;
case GLOB:
case GLOB_EXPAND:
(void) Strncpy(buffer, wordp, FILSIZ + 1);
items[0] = buffer;
items[1] = NULL;
ptr = items;
count = (looking == TW_COMMAND && Strchr(wordp, '/') == 0) ?
c_glob(&ptr) :
t_glob(&ptr, looking == TW_COMMAND);
if (count > 0) {
if (command == GLOB)
print_by_column(STRNULL, ptr, count, 0);
else {
DeleteBack(str_end - word_start);
for (i = 0; i < count; i++)
if (ptr[i] && *ptr[i]) {
(void) quote(ptr[i]);
if (insert_meta(0, 0, ptr[i], 0) < 0 ||
InsertStr(STRspace) < 0) {
blkfree(ptr);
return -1;
}
}
}
blkfree(ptr);
}
return count;
case VARS_EXPAND:
if (dollar(buffer, word)) {
if (insert_meta(word_start, str_end, buffer, !qu) < 0)
return -1;
return 1;
}
return 0;
case PATH_NORMALIZE:
if ((bptr = dnormalize(wordp, symlinks == SYM_IGNORE ||
symlinks == SYM_EXPAND)) != NULL) {
(void) Strcpy(buffer, bptr);
xfree((ptr_t) bptr);
if (insert_meta(word_start, str_end, buffer, !qu) < 0)
return -1;
return 1;
}
return 0;
case COMMAND_NORMALIZE:
if (!cmd_expand(wordp, buffer))
return 0;
if (insert_meta(word_start, str_end, buffer, !qu) < 0)
return -1;
return 1;
case LIST:
case LIST_ALL:
search_ret = t_search(wordp, wp, LIST, space_left, looking, 1,
pat, suf);
return search_ret;
default:
xprintf(CGETS(30, 4, "%s: Internal match error.\n"), progname);
return 1;
}
}
static int
t_glob(v, cmd)
register Char ***v;
int cmd;
{
jmp_buf_t osetexit;
if (**v == 0)
return (0);
gflag = 0, tglob(*v);
if (gflag) {
getexit(osetexit);
if (setexit() == 0)
*v = globall(*v);
resexit(osetexit);
gargv = 0;
if (haderr) {
haderr = 0;
NeedsRedraw = 1;
return (-1);
}
if (*v == 0)
return (0);
}
else
return (0);
if (cmd) {
Char **av = *v, *p;
int fwd, i, ac = gargc;
for (i = 0, fwd = 0; i < ac; i++)
if (!executable(NULL, av[i], 0)) {
fwd++;
p = av[i];
av[i] = NULL;
xfree((ptr_t) p);
}
else if (fwd)
av[i - fwd] = av[i];
if (fwd)
av[i - fwd] = av[i];
gargc -= fwd;
av[gargc] = NULL;
}
return (gargc);
}
static int
c_glob(v)
register Char ***v;
{
Char *pat = **v, *cmd, **av;
Char dir[MAXPATHLEN+1];
int flag, at, ac;
if (pat == NULL)
return (0);
ac = 0;
at = 10;
av = (Char **) xmalloc((size_t) (at * sizeof(Char *)));
av[ac] = NULL;
tw_cmd_start(NULL, NULL);
while ((cmd = tw_cmd_next(dir, &flag)) != NULL)
if (Gmatch(cmd, pat)) {
if (ac + 1 >= at) {
at += 10;
av = (Char **) xrealloc((ptr_t) av,
(size_t) (at * sizeof(Char *)));
}
av[ac++] = Strsave(cmd);
av[ac] = NULL;
}
tw_dir_end();
*v = av;
return (ac);
}
static int
insert_meta(cp, cpend, word, closequotes)
Char *cp;
Char *cpend;
Char *word;
bool closequotes;
{
Char buffer[2 * FILSIZ + 1], *bptr, *wptr;
int in_sync = (cp != NULL);
int qu = 0;
int ndel = (int) (cp ? cpend - cp : 0);
Char w, wq;
#ifdef DSPMBYTE
int mbytepos = 1;
#endif
for (bptr = buffer, wptr = word;;) {
if (bptr > buffer + 2 * FILSIZ - 5)
break;
if (cp >= cpend)
in_sync = 0;
#ifdef DSPMBYTE
if (mbytepos == 1)
#endif
if (in_sync && !cmap(qu, _ESC) && cmap(*cp, _QF|_ESC))
if (qu == 0 || qu == *cp) {
qu ^= *cp;
*bptr++ = *cp++;
continue;
}
w = *wptr;
if (w == 0)
break;
wq = w & QUOTE;
w &= ~QUOTE;
#ifdef DSPMBYTE
if (mbytepos == 2)
goto mbyteskip;
#endif
if (cmap(w, _ESC | _QF))
wq = QUOTE;
if (!wq && qu && tricky(w) && !(qu == '\"' && tricky_dq(w))) {
in_sync = 0;
if (cmap(qu, _ESC))
bptr[-1] = w;
else {
*bptr++ = (Char) qu;
*bptr++ = w;
if (wptr[1] == 0)
qu = 0;
else
*bptr++ = (Char) qu;
}
} else if (qu && w == qu) {
in_sync = 0;
if (bptr > buffer && bptr[-1] == qu) {
bptr[-1] = '\\';
*bptr++ = w;
qu = 0;
} else {
*bptr++ = (Char) qu;
*bptr++ = '\\';
*bptr++ = w;
*bptr++ = (Char) qu;
}
}
else if (wq && qu == '\"' && tricky_dq(w)) {
in_sync = 0;
*bptr++ = (Char) qu;
*bptr++ = '\\';
*bptr++ = w;
*bptr++ = (Char) qu;
} else if (wq && ((!qu && (tricky(w) || (w == HISTSUB && bptr == buffer))) || (!cmap(qu, _ESC) && w == HIST))) {
in_sync = 0;
*bptr++ = '\\';
*bptr++ = w;
} else {
#ifdef DSPMBYTE
mbyteskip:
#endif
if (in_sync && *cp++ != w)
in_sync = 0;
*bptr++ = w;
#ifdef DSPMBYTE
if (mbytepos == 1 && Ismbyte1(w))
mbytepos = 2;
else
mbytepos = 1;
#endif
}
wptr++;
if (cmap(qu, _ESC))
qu = 0;
}
if (closequotes && qu && !cmap(qu, _ESC))
*bptr++ = (Char) qu;
*bptr = '\0';
if (ndel)
DeleteBack(ndel);
return InsertStr(buffer);
}
static int
is_prefix(check, template)
register Char *check, *template;
{
for (; *check; check++, template++)
if ((*check & TRIM) != (*template & TRIM))
return (FALSE);
return (TRUE);
}
static int
is_prefixmatch(check, template, igncase)
Char *check, *template;
int igncase;
{
Char MCH1, MCH2;
for (; *check; check++, template++) {
if ((*check & TRIM) != (*template & TRIM)) {
MCH1 = (*check & TRIM);
MCH2 = (*template & TRIM);
MCH1 = Isupper(MCH1) ? Tolower(MCH1) : MCH1;
MCH2 = Isupper(MCH2) ? Tolower(MCH2) : MCH2;
if (MCH1 != MCH2) {
if (!igncase && ((*check & TRIM) == '-' ||
(*check & TRIM) == '.' ||
(*check & TRIM) == '_')) {
MCH1 = MCH2 = (*check & TRIM);
if (MCH1 == '_') {
MCH2 = '-';
} else if (MCH1 == '-') {
MCH2 = '_';
}
for (;*template && (*template & TRIM) != MCH1 &&
(*template & TRIM) != MCH2; template++)
continue;
if (!*template) {
return (FALSE);
}
} else {
return (FALSE);
}
}
}
}
return (TRUE);
}
static int
is_suffix(check, template)
register Char *check, *template;
{
register Char *t, *c;
for (t = template; *t++;)
continue;
for (c = check; *c++;)
continue;
for (;;) {
if (t == template)
return 1;
if (c == check || (*--t & TRIM) != (*--c & TRIM))
return 0;
}
}
static int
ignored(item)
register Char *item;
{
struct varent *vp;
register Char **cp;
if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL)
return (FALSE);
for (; *cp != NULL; cp++)
if (is_suffix(item, *cp))
return (TRUE);
return (FALSE);
}
int
starting_a_command(wordstart, inputline)
register Char *wordstart, *inputline;
{
register Char *ptr, *ncmdstart;
int count;
static Char
cmdstart[] = {'`', ';', '&', '(', '|', '\0'},
cmdalive[] = {' ', '\t', '\'', '"', '<', '>', '\0'};
for (ptr = wordstart, count = 0;
ptr >= inputline;
count += (*ptr-- == '`'))
continue;
ncmdstart = cmdstart + EVEN(count);
for (count = 0; wordstart >= inputline; wordstart--) {
if (*wordstart == '\0')
continue;
if (Strchr(ncmdstart, *wordstart))
break;
if ((ptr = Strchr(cmdalive, *wordstart)) != NULL)
count = 1;
if (count == 1 && !ptr)
return (FALSE);
}
if (wordstart > inputline)
switch (*wordstart) {
case '&':
while (wordstart > inputline &&
(*--wordstart == ' ' || *wordstart == '\t'))
continue;
if (*wordstart == '>')
return (FALSE);
break;
case '(':
while (wordstart > inputline &&
(*--wordstart == ' ' || *wordstart == '\t'))
continue;
if (!iscmdmeta(*wordstart) &&
(*wordstart != ' ' && *wordstart != '\t'))
return (FALSE);
break;
default:
break;
}
return (TRUE);
}
static int
recognize(exp_name, item, name_length, numitems, enhanced)
Char *exp_name, *item;
int name_length, numitems, enhanced;
{
Char MCH1, MCH2;
register Char *x, *ent;
register int len = 0;
#ifdef WINNT_NATIVE
struct varent *vp;
int igncase;
igncase = (vp = adrof(STRcomplete)) != NULL &&
Strcmp(*(vp->vec), STRigncase) == 0;
#endif
if (numitems == 1) {
copyn(exp_name, item, MAXNAMLEN);
return (0);
}
if (!enhanced
#ifdef WINNT_NATIVE
&& !igncase
#endif
) {
for (x = exp_name, ent = item; *x && (*x & TRIM) == (*ent & TRIM); x++, ent++)
len++;
} else {
for (x = exp_name, ent = item; *x; x++, ent++) {
MCH1 = *x & TRIM;
MCH2 = *ent & TRIM;
MCH1 = Isupper(MCH1) ? Tolower(MCH1) : MCH1;
MCH2 = Isupper(MCH2) ? Tolower(MCH2) : MCH2;
if (MCH1 != MCH2)
break;
len++;
}
if (*x || !*ent)
copyn(exp_name, item, MAXNAMLEN);
}
*x = '\0';
if (!(match_unique_match || is_set(STRrecexact) || (enhanced && *ent)) && len == name_length)
return (-1);
return (0);
}
static int
tw_collect_items(command, looking, exp_dir, exp_name, target, pat, flags)
COMMAND command;
int looking;
Char *exp_dir, *exp_name, *target, *pat;
int flags;
{
int done = FALSE;
int showdots;
int nignored = 0;
int numitems = 0;
int name_length = (int) Strlen(target);
int exec_check = flags & TW_EXEC_CHK;
int dir_check = flags & TW_DIR_CHK;
int text_check = flags & TW_TEXT_CHK;
int dir_ok = flags & TW_DIR_OK;
int gpat = flags & TW_PAT_OK;
int ignoring = flags & TW_IGN_OK;
int d = 4, nd;
Char *item, *ptr;
Char buf[MAXPATHLEN+1];
struct varent *vp;
int len, enhanced;
int cnt = 0;
int igncase = 0;
flags = 0;
showdots = DOT_NONE;
if ((ptr = varval(STRlistflags)) != STRNULL)
while (*ptr)
switch (*ptr++) {
case 'a':
showdots = DOT_ALL;
break;
case 'A':
showdots = DOT_NOT;
break;
default:
break;
}
while (!done && (item = (*tw_next_entry[looking])(exp_dir, &flags))) {
#ifdef TDEBUG
xprintf("item = %S\n", item);
#endif
switch (looking) {
case TW_FILE:
case TW_DIRECTORY:
case TW_TEXT:
if (showdots == DOT_NOT && (ISDOT(item) || ISDOTDOT(item)))
done = TRUE;
if (name_length == 0 && item[0] == '.' && showdots == DOT_NONE)
done = TRUE;
break;
case TW_COMMAND:
exec_check = flags & TW_EXEC_CHK;
dir_ok = flags & TW_DIR_OK;
break;
default:
break;
}
if (done) {
done = FALSE;
continue;
}
switch (command) {
case SPELL:
if (name_length == 0) {
exp_name[0] = '\0';
d = 0;
done = TRUE;
break;
}
if (gpat && !Gmatch(item, pat))
break;
nd = spdist(target, item);
if (nd <= d && nd != 4) {
if (!(exec_check && !executable(exp_dir, item, dir_ok))) {
(void) Strcpy(exp_name, item);
d = nd;
if (d == 0)
done = TRUE;
}
}
else if (nd == 4) {
if (spdir(exp_name, exp_dir, item, target)) {
if (exec_check && !executable(exp_dir, exp_name, dir_ok))
break;
#ifdef notdef
d = 0;
done = TRUE;
#endif
d = 3;
}
}
break;
case LIST:
case RECOGNIZE:
case RECOGNIZE_ALL:
case RECOGNIZE_SCROLL:
#ifdef WINNT_NATIVE
igncase = (vp = adrof(STRcomplete)) != NULL &&
Strcmp(*(vp->vec), STRigncase) == 0;
#endif
enhanced = (vp = adrof(STRcomplete)) != NULL && !Strcmp(*(vp->vec),STRenhance);
if (enhanced || igncase) {
if (!is_prefixmatch(target, item, igncase))
break;
} else {
if (!is_prefix(target, item))
break;
}
if (exec_check && !executable(exp_dir, item, dir_ok))
break;
if (dir_check && !isadirectory(exp_dir, item))
break;
if (text_check && isadirectory(exp_dir, item))
break;
if (gpat && !Gmatch(item, pat) &&
(dir_check || !isadirectory(exp_dir, item)))
break;
if (looking == TW_COMMAND || looking == TW_LOGNAME
|| looking == TW_USER || command == LIST) {
copyn(buf, item, MAXPATHLEN);
len = (int) Strlen(buf);
switch (looking) {
case TW_COMMAND:
if (!(dir_ok && exec_check))
break;
if (filetype(exp_dir, item) == '/') {
buf[len++] = '/';
buf[len] = '\0';
}
break;
case TW_FILE:
case TW_DIRECTORY:
buf[len++] = filetype(exp_dir, item);
buf[len] = '\0';
break;
default:
break;
}
if ((looking == TW_COMMAND || looking == TW_USER
|| looking == TW_LOGNAME) && tw_item_find(buf))
break;
else {
ptr = tw_item_add(len + 3);
copyn(ptr, buf, MAXPATHLEN);
if (command == LIST)
numitems++;
}
}
if (command == RECOGNIZE || command == RECOGNIZE_ALL ||
command == RECOGNIZE_SCROLL) {
if (ignoring && ignored(item)) {
nignored++;
break;
}
else if (command == RECOGNIZE_SCROLL) {
add_scroll_tab(item);
cnt++;
}
if (match_unique_match || is_set(STRrecexact)) {
if (StrQcmp(target, item) == 0) {
copyn(exp_name, item, MAXNAMLEN);
numitems = 1;
non_unique_match = TRUE;
done = TRUE;
break;
}
}
if (recognize(exp_name, item, name_length, ++numitems, enhanced))
if (command != RECOGNIZE_SCROLL)
done = TRUE;
if (enhanced && (int)Strlen(exp_name) < name_length)
copyn(exp_name, target, MAXNAMLEN);
}
break;
default:
break;
}
#ifdef TDEBUG
xprintf("done item = %S\n", item);
#endif
}
if (command == RECOGNIZE_SCROLL) {
if ((cnt <= curchoice) || (curchoice == -1)) {
curchoice = -1;
nignored = 0;
numitems = 0;
} else if (numitems > 1) {
if (curchoice < -1)
curchoice = cnt - 1;
choose_scroll_tab(&exp_name, cnt);
numitems = 1;
}
}
free_scroll_tab();
if (command == SPELL)
return d;
else {
if (ignoring && numitems == 0 && nignored > 0)
return -nignored;
else
return numitems;
}
}
static Char
tw_suffix(looking, exp_dir, exp_name, target, name)
int looking;
Char *exp_dir, *exp_name, *target, *name;
{
Char *ptr;
struct varent *vp;
USE(name);
(void) strip(exp_name);
switch (looking) {
case TW_LOGNAME:
return '/';
case TW_VARIABLE:
if ((vp = adrof(exp_name)) != NULL) {
if ((ptr = vp->vec[0]) == NULL || *ptr == '\0' ||
vp->vec[1] != NULL)
return ' ';
}
else if ((ptr = tgetenv(exp_name)) == NULL || *ptr == '\0')
return ' ';
*--target = '\0';
return isadirectory(exp_dir, ptr) ? '/' : ' ';
case TW_DIRECTORY:
return '/';
case TW_COMMAND:
case TW_FILE:
return isadirectory(exp_dir, exp_name) ? '/' : ' ';
case TW_ALIAS:
case TW_VARLIST:
case TW_WORDLIST:
case TW_SHELLVAR:
case TW_ENVVAR:
case TW_USER:
case TW_BINDING:
case TW_LIMIT:
case TW_SIGNAL:
case TW_JOB:
case TW_COMPLETION:
case TW_TEXT:
case TW_GRPNAME:
return ' ';
default:
return '\0';
}
}
static void
tw_fixword(looking, word, dir, exp_name, max_word_length)
int looking;
Char *word, *dir, *exp_name;
int max_word_length;
{
Char *ptr;
switch (looking) {
case TW_LOGNAME:
copyn(word, STRtilde, 1);
break;
case TW_VARIABLE:
if ((ptr = Strrchr(word, '$')) != NULL)
*++ptr = '\0';
else
word[0] = '\0';
break;
case TW_DIRECTORY:
case TW_FILE:
case TW_TEXT:
copyn(word, dir, max_word_length);
break;
default:
word[0] = '\0';
break;
}
(void) quote(exp_name);
catn(word, exp_name, max_word_length);
}
static int
tw_collect(command, looking, exp_dir, exp_name, target, pat, flags, dir_fd)
COMMAND command;
int looking;
Char *exp_dir, *exp_name, **target, *pat;
int flags;
DIR *dir_fd;
{
static int ni;
jmp_buf_t osetexit;
#ifdef TDEBUG
xprintf("target = %S\n", *target);
#endif
ni = 0;
getexit(osetexit);
for (;;) {
(*tw_start_entry[looking])(dir_fd, pat);
InsideCompletion = 1;
if (setexit()) {
resexit(osetexit);
InsideCompletion = 0;
haderr = 0;
#if defined(SOLARIS2) && defined(i386) && !defined(__GNUC__)
if ((looking == TW_LOGNAME) || (looking == TW_USER))
tw_logname_end();
else
if (looking == TW_GRPNAME)
tw_grpname_end();
else
tw_dir_end();
#else
(*tw_end_entry[looking])();
#endif
return(-1);
}
if ((ni = tw_collect_items(command, looking, exp_dir, exp_name,
*target, pat,
ni >= 0 ? flags :
flags & ~TW_IGN_OK)) >= 0) {
resexit(osetexit);
InsideCompletion = 0;
#if defined(SOLARIS2) && defined(i386) && !defined(__GNUC__)
if ((looking == TW_LOGNAME) || (looking == TW_USER))
tw_logname_end();
else
if (looking == TW_GRPNAME)
tw_grpname_end();
else
tw_dir_end();
#else
(*tw_end_entry[looking])();
#endif
return(ni);
}
}
}
static void
tw_list_items(looking, numitems, list_max)
int looking, numitems, list_max;
{
Char *ptr;
int max_items = 0;
int max_rows = 0;
if ((ptr = varval(STRlistmax)) != STRNULL) {
while (*ptr) {
if (!Isdigit(*ptr)) {
max_items = 0;
break;
}
max_items = max_items * 10 + *ptr++ - '0';
}
if ((max_items > 0) && (numitems > max_items) && list_max)
max_items = numitems;
else
max_items = 0;
}
if (max_items == 0 && (ptr = varval(STRlistmaxrows)) != STRNULL) {
int rows;
while (*ptr) {
if (!Isdigit(*ptr)) {
max_rows = 0;
break;
}
max_rows = max_rows * 10 + *ptr++ - '0';
}
if (max_rows != 0 && looking != TW_JOB)
rows = find_rows(tw_item_get(), numitems, TRUE);
else
rows = numitems;
if ((max_rows > 0) && (rows > max_rows) && list_max)
max_rows = rows;
else
max_rows = 0;
}
if (max_items || max_rows) {
char tc;
const char *name;
int maxs;
if (max_items) {
name = CGETS(30, 5, "items");
maxs = max_items;
}
else {
name = CGETS(30, 6, "rows");
maxs = max_rows;
}
xprintf(CGETS(30, 7, "There are %d %s, list them anyway? [n/y] "),
maxs, name);
flush();
(void) read(SHIN, &tc, 1);
xprintf("%c\r\n", tc);
if (strchr(CGETS(30, 13, "Yy"), tc) == NULL)
return;
}
if (looking != TW_SIGNAL)
qsort((ptr_t) tw_item_get(), (size_t) numitems, sizeof(Char *),
(int (*) __P((const void *, const void *))) fcompare);
if (looking != TW_JOB)
print_by_column(STRNULL, tw_item_get(), numitems, TRUE);
else {
int i;
Char **w = tw_item_get();
for (i = 0; i < numitems; i++) {
xprintf("%S", w[i]);
if (Tty_raw_mode)
xputchar('\r');
xputchar('\n');
}
}
}
int
t_search(word, wp, command, max_word_length, looking, list_max, pat, suf)
Char *word, *wp;
COMMAND command;
int max_word_length, looking, list_max;
Char *pat;
int suf;
{
int numitems,
flags = 0,
gpat = pat[0] != '\0',
nd;
Char exp_dir[FILSIZ + 1],
dir[FILSIZ + 1],
exp_name[MAXNAMLEN + 1],
name[MAXNAMLEN + 1],
*target;
DIR *dir_fd = NULL;
USE(wp);
tw_item_free();
non_unique_match = FALSE;
extract_dir_and_name(word, dir, name);
if ((*word == '~') && (Strchr(word, '/') == NULL)) {
looking = TW_LOGNAME;
target = name;
gpat = 0;
}
else if ((target = Strrchr(name, '$')) != 0 &&
(Strchr(name, '/') == NULL)) {
target++;
looking = TW_VARIABLE;
gpat = 0;
}
else
target = name;
if (looking & TW_PATH) {
gpat = 0;
copyn(exp_dir, pat, MAXNAMLEN);
if (exp_dir[Strlen(exp_dir) - 1] != '/')
catn(exp_dir, STRslash, MAXNAMLEN);
catn(exp_dir, dir, MAXNAMLEN);
}
else
exp_dir[0] = '\0';
switch (looking & ~TW_PATH) {
case TW_NONE:
return -1;
case TW_ZERO:
looking = TW_FILE;
break;
case TW_COMMAND:
if (Strchr(word, '/') || (looking & TW_PATH)) {
looking = TW_FILE;
flags |= TW_EXEC_CHK;
flags |= TW_DIR_OK;
}
#ifdef notdef
if (looking == TW_COMMAND && (*word == '\0'))
return (-1);
#endif
break;
case TW_VARLIST:
case TW_WORDLIST:
gpat = 0;
break;
case TW_EXPLAIN:
if (command == LIST && pat != NULL) {
xprintf("%S", pat);
if (Tty_raw_mode)
xputchar('\r');
xputchar('\n');
}
return 2;
default:
break;
}
flags |= (gpat == 0) ? TW_IGN_OK : TW_PAT_OK;
#ifdef TDEBUG
xprintf(CGETS(30, 8, "looking = %d\n"), looking);
#endif
switch (looking) {
case TW_ALIAS:
case TW_SHELLVAR:
case TW_ENVVAR:
case TW_BINDING:
case TW_LIMIT:
case TW_SIGNAL:
case TW_JOB:
case TW_COMPLETION:
case TW_GRPNAME:
break;
case TW_VARIABLE:
if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
return nd;
break;
case TW_DIRECTORY:
flags |= TW_DIR_CHK;
#ifdef notyet
flags |= TW_DIR_OK;
#endif
#ifdef notyet
if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
return nd;
if (isadirectory(exp_dir, name)) {
if (exp_dir[0] != '\0' || name[0] != '\0') {
catn(dir, name, MAXNAMLEN);
if (dir[Strlen(dir) - 1] != '/')
catn(dir, STRslash, MAXNAMLEN);
if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
return nd;
if (word[Strlen(word) - 1] != '/')
catn(word, STRslash, MAXNAMLEN);
name[0] = '\0';
}
}
#endif
if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
return nd;
break;
case TW_TEXT:
flags |= TW_TEXT_CHK;
case TW_FILE:
if ((nd = expand_dir(dir, exp_dir, &dir_fd, command)) != 0)
return nd;
break;
case TW_PATH | TW_TEXT:
case TW_PATH | TW_FILE:
case TW_PATH | TW_DIRECTORY:
case TW_PATH | TW_COMMAND:
if ((dir_fd = opendir(short2str(exp_dir))) == NULL) {
xprintf("%S: %s\n", exp_dir, strerror(errno));
return -1;
}
if (exp_dir[Strlen(exp_dir) - 1] != '/')
catn(exp_dir, STRslash, MAXNAMLEN);
looking &= ~TW_PATH;
switch (looking) {
case TW_TEXT:
flags |= TW_TEXT_CHK;
break;
case TW_FILE:
break;
case TW_DIRECTORY:
flags |= TW_DIR_CHK;
break;
case TW_COMMAND:
copyn(target, word, MAXNAMLEN);
break;
default:
abort();
break;
}
break;
case TW_LOGNAME:
word++;
case TW_USER:
if (command == SPELL && getpwnam(short2str(word)) != NULL) {
#ifdef YPBUGS
fix_yp_bugs();
#endif
return (0);
}
copyn(name, word, MAXNAMLEN);
if (looking == TW_LOGNAME)
word--;
break;
case TW_COMMAND:
case TW_VARLIST:
case TW_WORDLIST:
copyn(target, word, MAXNAMLEN);
break;
default:
xprintf(CGETS(30, 9,
"\n%s internal error: I don't know what I'm looking for!\n"),
progname);
NeedsRedraw = 1;
return (-1);
}
numitems = tw_collect(command, looking, exp_dir, exp_name,
&target, pat, flags, dir_fd);
if (numitems == -1)
return -1;
switch (command) {
case RECOGNIZE:
case RECOGNIZE_ALL:
case RECOGNIZE_SCROLL:
if (numitems <= 0)
return (numitems);
tw_fixword(looking, word, dir, exp_name, max_word_length);
if (!match_unique_match && is_set(STRaddsuffix) && numitems == 1) {
Char suffix[2];
suffix[1] = '\0';
switch (suf) {
case 0:
suffix[0] = tw_suffix(looking, exp_dir, exp_name, target, name);
break;
case -1:
return numitems;
default:
suffix[0] = (Char) suf;
break;
}
catn(word, suffix, max_word_length);
}
return numitems;
case LIST:
tw_list_items(looking, numitems, list_max);
tw_item_free();
return (numitems);
case SPELL:
tw_fixword(looking, word, dir, exp_name, max_word_length);
return (numitems);
default:
xprintf("Bad tw_command\n");
return (0);
}
}
static void
extract_dir_and_name(path, dir, name)
Char *path, *dir, *name;
{
register Char *p;
p = Strrchr(path, '/');
#ifdef WINNT_NATIVE
if (p == NULL)
p = Strrchr(path, ':');
#endif
if (p == NULL) {
copyn(name, path, MAXNAMLEN);
dir[0] = '\0';
}
else {
p++;
copyn(name, p, MAXNAMLEN);
copyn(dir, path, p - path);
}
}
Char *
dollar(new, old)
Char *new;
const Char *old;
{
Char *p;
size_t space;
for (space = FILSIZ, p = new; *old && space > 0;)
if (*old != '$') {
*p++ = *old++;
space--;
}
else {
if (expdollar(&p, &old, &space, QUOTE) == NULL)
return NULL;
}
*p = '\0';
return (new);
}
static Char *
tilde(new, old)
Char *new, *old;
{
register Char *o, *p;
switch (old[0]) {
case '~':
for (p = new, o = &old[1]; *o && *o != '/'; *p++ = *o++)
continue;
*p = '\0';
if (gethdir(new)) {
new[0] = '\0';
return NULL;
}
(void) Strcat(new, o);
return new;
case '=':
if ((p = globequal(new, old)) == NULL) {
*new = '\0';
return NULL;
}
if (p == new)
return new;
default:
(void) Strcpy(new, old);
return new;
}
}
static int
expand_dir(dir, edir, dfd, cmd)
Char *dir, *edir;
DIR **dfd;
COMMAND cmd;
{
Char *nd = NULL;
Char tdir[MAXPATHLEN + 1];
if ((dollar(tdir, dir) == 0) ||
(tilde(edir, tdir) == 0) ||
!(nd = dnormalize(*edir ? edir : STRdot, symlinks == SYM_IGNORE ||
symlinks == SYM_EXPAND)) ||
((*dfd = opendir(short2str(nd))) == NULL)) {
xfree((ptr_t) nd);
if (cmd == SPELL || SearchNoDirErr)
return (-2);
xprintf("\n%S %s\n",
*edir ? edir :
(*tdir ? tdir : dir),
(errno == ENOTDIR ? CGETS(30, 10, "not a directory") :
(errno == ENOENT ? CGETS(30, 11, "not found") :
CGETS(30, 12, "unreadable"))));
NeedsRedraw = 1;
return (-1);
}
if (nd) {
if (*dir != '\0') {
Char *s, *d, *p;
for (p = edir; *p; p++)
continue;
if (*--p == '/') {
for (p = nd; *p; p++)
continue;
if (*--p != '/')
p = NULL;
}
for (d = edir, s = nd; (*d++ = *s++) != '\0';)
continue;
if (!p) {
*d-- = '\0';
*d = '/';
}
}
xfree((ptr_t) nd);
}
return 0;
}
static bool
nostat(dir)
Char *dir;
{
struct varent *vp;
register Char **cp;
if ((vp = adrof(STRnostat)) == NULL || (cp = vp->vec) == NULL)
return FALSE;
for (; *cp != NULL; cp++) {
if (Strcmp(*cp, STRstar) == 0)
return TRUE;
if (Gmatch(dir, *cp))
return TRUE;
}
return FALSE;
}
static Char
filetype(dir, file)
Char *dir, *file;
{
if (dir) {
Char path[512];
char *ptr;
struct stat statb;
#ifdef S_ISCDF
struct stat hpstatb;
#endif
if (nostat(dir)) return(' ');
(void) Strcpy(path, dir);
catn(path, file, (int) (sizeof(path) / sizeof(Char)));
if (lstat(ptr = short2str(path), &statb) != -1)
{
#ifdef S_ISLNK
if (S_ISLNK(statb.st_mode)) {
if (adrof(STRlistlinks)) {
if (stat(ptr, &statb) == -1)
return ('&');
else if (S_ISDIR(statb.st_mode))
return ('>');
else
return ('@');
}
else
return ('@');
}
#endif
#ifdef S_ISSOCK
if (S_ISSOCK(statb.st_mode))
return ('=');
#endif
#ifdef S_ISFIFO
if (S_ISFIFO(statb.st_mode))
return ('|');
#endif
#ifdef S_ISHIDDEN
if (S_ISHIDDEN(statb.st_mode))
return ('+');
#endif
#ifdef S_ISCDF
(void) strcat(ptr, "+");
if ((stat(ptr, &hpstatb) != -1) && S_ISCDF(hpstatb.st_mode))
return ('+');
#endif
#ifdef S_ISNWK
if (S_ISNWK(statb.st_mode))
return (':');
#endif
#ifdef S_ISCHR
if (S_ISCHR(statb.st_mode))
return ('%');
#endif
#ifdef S_ISBLK
if (S_ISBLK(statb.st_mode))
return ('#');
#endif
#ifdef S_ISDIR
if (S_ISDIR(statb.st_mode))
return ('/');
#endif
if (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
return ('*');
}
}
return (' ');
}
static int
isadirectory(dir, file)
Char *dir, *file;
{
if (dir) {
Char path[MAXPATHLEN];
struct stat statb;
(void) Strcpy(path, dir);
catn(path, file, (int) (sizeof(path) / sizeof(Char)));
if (stat(short2str(path), &statb) >= 0) {
#ifdef S_ISSOCK
if (S_ISSOCK(statb.st_mode))
return 0;
#endif
#ifdef S_ISFIFO
if (S_ISFIFO(statb.st_mode))
return 0;
#endif
if (S_ISDIR(statb.st_mode))
return 1;
}
}
return 0;
}
static int
find_rows(items, count, no_file_suffix)
Char *items[];
int count, no_file_suffix;
{
register int i, columns, rows;
unsigned int maxwidth = 0;
for (i = 0; i < count; i++)
maxwidth = max(maxwidth, (unsigned int) Strlen(items[i]));
maxwidth += no_file_suffix ? 1 : 2;
columns = (TermH + 1) / maxwidth;
if (!columns)
columns = 1;
rows = (count + (columns - 1)) / columns;
return rows;
}
void
print_by_column(dir, items, count, no_file_suffix)
register Char *dir, *items[];
int count, no_file_suffix;
{
register int i, r, c, columns, rows;
unsigned int w, maxwidth = 0;
Char *val;
bool across;
lbuffed = 0;
across = ((val = varval(STRlistflags)) != STRNULL) &&
(Strchr(val, 'x') != NULL);
for (i = 0; i < count; i++)
maxwidth = max(maxwidth, (unsigned int) Strlen(items[i]));
maxwidth += no_file_suffix ? 1 : 2;
columns = TermH / maxwidth;
if (!columns || !isatty(didfds ? 1 : SHOUT))
columns = 1;
rows = (count + (columns - 1)) / columns;
i = -1;
for (r = 0; r < rows; r++) {
for (c = 0; c < columns; c++) {
i = across ? (i + 1) : (c * rows + r);
if (i < count) {
w = (unsigned int) Strlen(items[i]);
#ifdef COLOR_LS_F
if (no_file_suffix) {
Char f = items[i][w - 1];
items[i][w - 1] = 0;
print_with_color(items[i], w - 1, f);
}
else {
print_with_color(items[i], w, filetype(dir, items[i]));
w++;
}
#else
if (no_file_suffix) {
xprintf("%S", items[i]);
}
else {
xprintf("%S%c", items[i],
filetype(dir, items[i]));
w++;
}
#endif
if (c < (columns - 1))
for (; w < maxwidth; w++)
xputchar(' ');
}
else if (across)
break;
}
if (Tty_raw_mode)
xputchar('\r');
xputchar('\n');
}
lbuffed = 1;
flush();
}
int
StrQcmp(str1, str2)
register Char *str1, *str2;
{
for (; *str1 && samecase(*str1 & TRIM) == samecase(*str2 & TRIM);
str1++, str2++)
continue;
if (*str1 == '\0' && *str2 == '\0')
return (0);
else if (*str1 == '\0')
return (-1);
else if (*str2 == '\0')
return (1);
else
return ((*str1 & TRIM) - (*str2 & TRIM));
}
int
fcompare(file1, file2)
Char **file1, **file2;
{
return (int) collate(*file1, *file2);
}
void
catn(des, src, count)
register Char *des, *src;
int count;
{
while (--count >= 0 && *des)
des++;
while (--count >= 0)
if ((*des++ = *src++) == 0)
return;
*des = '\0';
}
void
copyn(des, src, count)
register Char *des, *src;
int count;
{
while (--count >= 0)
if ((*des++ = *src++) == 0)
return;
*des = '\0';
}
Char *
tgetenv(str)
Char *str;
{
Char **var;
int len, res;
len = (int) Strlen(str);
for (var = STR_environ; var != NULL && *var != NULL; var++)
if (Strlen(*var) >= len && (*var)[len] == '=') {
(*var)[len] = '\0';
res = StrQcmp(*var, str);
(*var)[len] = '=';
if (res == 0)
return (&((*var)[len + 1]));
}
return (NULL);
}
struct scroll_tab_list *scroll_tab = 0;
static void
add_scroll_tab(item)
Char *item;
{
struct scroll_tab_list *new_scroll;
new_scroll = (struct scroll_tab_list *) xmalloc((size_t)
sizeof(struct scroll_tab_list));
new_scroll->element = Strsave(item);
new_scroll->next = scroll_tab;
scroll_tab = new_scroll;
}
static void
choose_scroll_tab(exp_name, cnt)
Char **exp_name;
int cnt;
{
struct scroll_tab_list *loop;
int tmp = cnt;
Char **ptr;
ptr = (Char **) xmalloc((size_t) sizeof(Char *) * cnt);
for(loop = scroll_tab; loop && (tmp >= 0); loop = loop->next)
ptr[--tmp] = loop->element;
qsort((ptr_t) ptr, (size_t) cnt, sizeof(Char *),
(int (*) __P((const void *, const void *))) fcompare);
copyn(*exp_name, ptr[curchoice], (int) Strlen(ptr[curchoice]));
xfree((ptr_t) ptr);
}
static void
free_scroll_tab()
{
struct scroll_tab_list *loop;
while(scroll_tab) {
loop = scroll_tab;
scroll_tab = scroll_tab->next;
xfree((ptr_t) loop->element);
xfree((ptr_t) loop);
}
}