#include "sh.h"
#include "ed.h"
RCSID("$Id: sh.file.c,v 3.28 2005/01/05 16:06:13 christos Exp $")
#if defined(FILEC) && defined(TIOCSTI)
#define ON 1
#define OFF 0
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define ESC CTL_ESC('\033')
typedef enum {
LIST, RECOGNIZE
} COMMAND;
static void setup_tty __P((int));
static void back_to_col_1 __P((void));
static void pushback __P((Char *));
static void catn __P((Char *, Char *, int));
static void copyn __P((Char *, Char *, int));
static int filetype __P((Char *, Char *));
static void print_by_column __P((Char *, Char *[], size_t));
static Char *tilde __P((Char *, Char *));
static void retype __P((void));
static void beep __P((void));
static void print_recognized_stuff __P((Char *));
static void extract_dir_and_name __P((Char *, Char *, Char *));
static Char *getitem __P((DIR *, int));
static void free_items __P((Char **, size_t));
static int tsearch __P((Char *, COMMAND, int));
static int compare __P((const ptr_t, const ptr_t));
static int recognize __P((Char *, Char *, int, size_t));
static int is_prefix __P((Char *, Char *));
static int is_suffix __P((Char *, Char *));
static int ignored __P((Char *));
int filec = 0;
static void
setup_tty(on)
int on;
{
#ifdef TERMIO
# ifdef POSIX
struct termios tchars;
# else
struct termio tchars;
# endif
# ifdef POSIX
(void) tcgetattr(SHIN, &tchars);
# else
(void) ioctl(SHIN, TCGETA, (ioctl_t) &tchars);
# endif
if (on) {
tchars.c_cc[VEOL] = ESC;
if (tchars.c_lflag & ICANON)
# ifdef POSIX
on = TCSADRAIN;
# else
on = TCSETA;
# endif
else {
# ifdef POSIX
on = TCSAFLUSH;
# else
on = TCSETAF;
# endif
tchars.c_lflag |= ICANON;
}
}
else {
tchars.c_cc[VEOL] = _POSIX_VDISABLE;
# ifdef POSIX
on = TCSADRAIN;
# else
on = TCSETA;
# endif
}
# ifdef POSIX
(void) tcsetattr(SHIN, on, &tchars);
# else
(void) ioctl(SHIN, on, (ioctl_t) &tchars);
# endif
#else
struct sgttyb sgtty;
static struct tchars tchars;
if (on) {
(void) ioctl(SHIN, TIOCGETC, (ioctl_t) & tchars);
tchars.t_brkc = ESC;
(void) ioctl(SHIN, TIOCSETC, (ioctl_t) & tchars);
(void) ioctl(SHIN, TIOCGETP, (ioctl_t) & sgtty);
if (sgtty.sg_flags & (RAW | CBREAK)) {
sgtty.sg_flags &= ~(RAW | CBREAK);
(void) ioctl(SHIN, TIOCSETP, (ioctl_t) & sgtty);
}
}
else {
tchars.t_brkc = -1;
(void) ioctl(SHIN, TIOCSETC, (ioctl_t) & tchars);
}
#endif
}
static void
back_to_col_1()
{
#ifdef TERMIO
# ifdef POSIX
struct termios tty, tty_normal;
# else
struct termio tty, tty_normal;
# endif
#else
struct sgttyb tty, tty_normal;
#endif
# ifdef BSDSIGS
sigmask_t omask = sigblock(sigmask(SIGINT));
# else
(void) sighold(SIGINT);
# endif
#ifdef TERMIO
# ifdef POSIX
(void) tcgetattr(SHOUT, &tty);
# else
(void) ioctl(SHOUT, TCGETA, (ioctl_t) &tty_normal);
# endif
tty_normal = tty;
tty.c_iflag &= ~INLCR;
tty.c_oflag &= ~ONLCR;
# ifdef POSIX
(void) tcsetattr(SHOUT, TCSANOW, &tty);
# else
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
# endif
(void) write(SHOUT, "\r", 1);
# ifdef POSIX
(void) tcsetattr(SHOUT, TCSANOW, &tty_normal);
# else
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty_normal);
# endif
#else
(void) ioctl(SHIN, TIOCGETP, (ioctl_t) & tty);
tty_normal = tty;
tty.sg_flags &= ~CRMOD;
(void) ioctl(SHIN, TIOCSETN, (ioctl_t) & tty);
(void) write(SHOUT, "\r", 1);
(void) ioctl(SHIN, TIOCSETN, (ioctl_t) & tty_normal);
#endif
# ifdef BSDSIGS
(void) sigsetmask(omask);
# else
(void) sigrelse(SIGINT);
# endif
}
static void
pushback(string)
Char *string;
{
Char *p;
#ifdef TERMIO
# ifdef POSIX
struct termios tty, tty_normal;
# else
struct termio tty, tty_normal;
# endif
#else
struct sgttyb tty, tty_normal;
#endif
#ifdef BSDSIGS
sigmask_t omask = sigblock(sigmask(SIGINT));
#else
(void) sighold(SIGINT);
#endif
#ifdef TERMIO
# ifdef POSIX
(void) tcgetattr(SHOUT, &tty);
# else
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
# endif
tty_normal = tty;
tty.c_lflag &= ~(ECHOKE | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOCTL);
# ifdef POSIX
(void) tcsetattr(SHOUT, TCSANOW, &tty);
# else
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
# endif
for (p = string; *p != '\0'; p++) {
char buf[MB_LEN_MAX];
size_t i, len;
len = one_wctomb(buf, *p & CHAR);
for (i = 0; i < len; i++)
(void) ioctl(SHOUT, TIOCSTI, (ioctl_t) &buf[i]);
}
# ifdef POSIX
(void) tcsetattr(SHOUT, TCSANOW, &tty_normal);
# else
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty_normal);
# endif
#else
(void) ioctl(SHOUT, TIOCGETP, (ioctl_t) & tty);
tty_normal = tty;
tty.sg_flags &= ~ECHO;
(void) ioctl(SHOUT, TIOCSETN, (ioctl_t) & tty);
for (p = string; c = *p; p++)
(void) ioctl(SHOUT, TIOCSTI, (ioctl_t) & c);
(void) ioctl(SHOUT, TIOCSETN, (ioctl_t) & tty_normal);
#endif
# ifdef BSDSIGS
(void) sigsetmask(omask);
# else
(void) sigrelse(SIGINT);
# endif
}
static void
catn(des, src, count)
Char *des, *src;
int count;
{
while (--count >= 0 && *des)
des++;
while (--count >= 0)
if ((*des++ = *src++) == 0)
return;
*des = '\0';
}
static void
copyn(des, src, count)
Char *des, *src;
int count;
{
while (--count >= 0)
if ((*des++ = *src++) == 0)
return;
*des = '\0';
}
static int
filetype(dir, file)
Char *dir, *file;
{
Char path[MAXPATHLEN];
struct stat statb;
catn(Strcpy(path, dir), file, sizeof(path) / sizeof(Char));
if (lstat(short2str(path), &statb) == 0) {
switch (statb.st_mode & S_IFMT) {
case S_IFDIR:
return ('/');
case S_IFLNK:
if (stat(short2str(path), &statb) == 0 &&
S_ISDIR(statb.st_mode))
return ('>');
else
return ('@');
case S_IFSOCK:
return ('=');
default:
if (statb.st_mode & 0111)
return ('*');
}
}
return (' ');
}
static struct winsize win;
static void
print_by_column(dir, items, count)
Char *dir, *items[];
size_t count;
{
size_t i;
int rows, r, c, maxwidth = 0, columns;
if (ioctl(SHOUT, TIOCGWINSZ, (ioctl_t) & win) < 0 || win.ws_col == 0)
win.ws_col = 80;
for (i = 0; i < count; i++)
maxwidth = maxwidth > (r = Strlen(items[i])) ? maxwidth : r;
maxwidth += 2;
columns = win.ws_col / maxwidth;
if (columns == 0)
columns = 1;
rows = (count + (columns - 1)) / columns;
for (r = 0; r < rows; r++) {
for (c = 0; c < columns; c++) {
i = c * rows + r;
if (i < count) {
int w;
xprintf("%S", items[i]);
xputchar(dir ? filetype(dir, items[i]) : ' ');
if (c < columns - 1) {
w = Strlen(items[i]) + 1;
for (; w < maxwidth; w++)
xputchar(' ');
}
}
}
xputchar('\r');
xputchar('\n');
}
}
static Char *
tilde(new, old)
Char *new, *old;
{
Char *o, *p;
struct passwd *pw;
static Char person[40];
if (old[0] != '~')
return (Strcpy(new, old));
for (p = person, o = &old[1]; *o && *o != '/'; *p++ = *o++);
*p = '\0';
if (person[0] == '\0')
(void) Strcpy(new, varval(STRhome));
else {
pw = getpwnam(short2str(person));
if (pw == NULL)
return (NULL);
(void) Strcpy(new, str2short(pw->pw_dir));
}
(void) Strcat(new, o);
return (new);
}
static void
retype()
{
#ifdef TERMIO
# ifdef POSIX
struct termios tty;
(void) tcgetattr(SHOUT, &tty);
# else
struct termio tty;
(void) ioctl(SHOUT, TCGETA, (ioctl_t) &tty);
# endif
tty.c_lflag |= PENDIN;
# ifdef POSIX
(void) tcsetattr(SHOUT, TCSANOW, &tty);
# else
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
# endif
#else
int pending_input = LPENDIN;
(void) ioctl(SHOUT, TIOCLBIS, (ioctl_t) & pending_input);
#endif
}
static void
beep()
{
if (adrof(STRnobeep) == 0)
#ifdef IS_ASCII
(void) write(SHOUT, "\007", 1);
#else
{
unsigned char beep_ch = CTL_ESC('\007');
(void) write(SHOUT, &beep_ch, 1);
}
#endif
}
static void
print_recognized_stuff(recognized_part)
Char *recognized_part;
{
(void) putraw('\b');
(void) putraw('\b');
switch (Strlen(recognized_part)) {
case 0:
(void) putraw(' ');
(void) putraw(' ');
(void) putraw('\b');
(void) putraw('\b');
break;
case 1:
xprintf("%S", recognized_part);
(void) putraw(' ');
(void) putraw('\b');
break;
default:
xprintf("%S", recognized_part);
break;
}
flush();
}
static void
extract_dir_and_name(path, dir, name)
Char *path, *dir, *name;
{
Char *p;
p = Strrchr(path, '/');
if (p == NULL) {
copyn(name, path, MAXNAMLEN);
dir[0] = '\0';
}
else {
copyn(name, ++p, MAXNAMLEN);
copyn(dir, path, p - path);
}
}
static Char *
getitem(dir_fd, looking_for_lognames)
DIR *dir_fd;
int looking_for_lognames;
{
struct passwd *pw;
struct dirent *dirp;
if (looking_for_lognames) {
#ifndef HAVE_GETPWENT
return (NULL);
#else
if ((pw = getpwent()) == NULL)
return (NULL);
return (str2short(pw->pw_name));
#endif
}
if ((dirp = readdir(dir_fd)) != NULL)
return (str2short(dirp->d_name));
return (NULL);
}
static void
free_items(items, numitems)
Char **items;
size_t numitems;
{
size_t i;
for (i = 0; i < numitems; i++)
xfree((ptr_t) items[i]);
xfree((ptr_t) items);
}
#ifdef BSDSIGS
# define FREE_ITEMS(items, numitems) { \
sigmask_t omask;\
\
omask = sigblock(sigmask(SIGINT));\
free_items(items, numitems);\
(void) sigsetmask(omask);\
}
#else
# define FREE_ITEMS(items, numitems) { \
(void) sighold(SIGINT);\
free_items(items, numitems);\
(void) sigrelse(SIGINT);\
}
#endif
static int
tsearch(word, command, max_word_length)
Char *word;
int max_word_length;
COMMAND command;
{
DIR *dir_fd;
int ignoring = TRUE, nignored = 0;
int name_length, looking_for_lognames;
Char tilded_dir[MAXPATHLEN + 1], dir[MAXPATHLEN + 1];
Char name[MAXNAMLEN + 1], extended_name[MAXNAMLEN + 1];
Char *item;
Char **items = NULL;
size_t numitems = 0, maxitems = 0;
looking_for_lognames = (*word == '~') && (Strchr(word, '/') == NULL);
if (looking_for_lognames) {
#ifdef HAVE_GETPWENT
(void) setpwent();
#endif
copyn(name, &word[1], MAXNAMLEN);
dir_fd = NULL;
}
else {
extract_dir_and_name(word, dir, name);
if (tilde(tilded_dir, dir) == 0)
return (0);
dir_fd = opendir(*tilded_dir ? short2str(tilded_dir) : ".");
if (dir_fd == NULL)
return (0);
}
again:
name_length = Strlen(name);
for (numitems = 0;
(item = getitem(dir_fd, looking_for_lognames)) != NULL;) {
if (!is_prefix(name, item))
continue;
if (name_length == 0 && item[0] == '.' &&
!looking_for_lognames)
continue;
if (command == LIST) {
if (numitems >= maxitems) {
maxitems += 1024;
if (items == NULL)
items = (Char **) xmalloc(sizeof(*items) * maxitems);
else
items = (Char **) xrealloc((ptr_t) items,
sizeof(*items) * maxitems);
}
items[numitems] = (Char *) xmalloc((size_t) (Strlen(item) + 1) *
sizeof(Char));
copyn(items[numitems], item, MAXNAMLEN);
numitems++;
}
else {
if (ignoring && ignored(item))
nignored++;
else if (recognize(extended_name,
item, name_length, ++numitems))
break;
}
}
if (ignoring && numitems == 0 && nignored > 0) {
ignoring = FALSE;
nignored = 0;
if (looking_for_lognames) {
#ifdef HAVE_GETPWENT
(void) setpwent();
#endif
} else
rewinddir(dir_fd);
goto again;
}
if (looking_for_lognames) {
#ifndef HAVE_GETPWENT
(void) endpwent();
#endif
} else
(void) closedir(dir_fd);
if (numitems == 0)
return (0);
if (command == RECOGNIZE) {
if (looking_for_lognames)
copyn(word, STRtilde, 1);
else
copyn(word, dir, max_word_length);
catn(word, extended_name, max_word_length);
return (numitems);
}
else {
qsort((ptr_t) items, numitems, sizeof(items[0]),
(int (*) __P((const void *, const void *))) compare);
print_by_column(looking_for_lognames ? NULL : tilded_dir,
items, numitems);
if (items != NULL)
FREE_ITEMS(items, numitems);
}
return (0);
}
static int
compare(p, q)
const ptr_t p, q;
{
#ifdef WIDE_STRINGS
errno = 0;
return (wcscoll(*(Char **) p, *(Char **) q));
#else
char *p1, *q1;
int res;
p1 = strsave(short2str(*(Char **) p));
q1 = strsave(short2str(*(Char **) q));
# if defined(NLS) && !defined(NOSTRCOLL)
errno = 0;
res = strcoll(p1, q1);
# else
res = strcmp(p1, q1);
# endif
xfree (p1);
xfree (q1);
return res;
#endif
}
static int
recognize(extended_name, item, name_length, numitems)
Char *extended_name, *item;
int name_length;
size_t numitems;
{
if (numitems == 1)
copyn(extended_name, item, MAXNAMLEN);
else {
Char *x, *ent;
int len = 0;
x = extended_name;
for (ent = item; *x && *x == *ent++; x++, len++);
*x = '\0';
if (len == name_length)
return (-1);
}
return (0);
}
static int
is_prefix(check, template)
Char *check, *template;
{
do
if (*check == 0)
return (TRUE);
while (*check++ == *template++);
return (FALSE);
}
static int
is_suffix(check, template)
Char *check, *template;
{
Char *c, *t;
for (c = check; *c++;);
for (t = template; *t++;);
for (;;) {
if (t == template)
return 1;
if (c == check || *--t != *--c)
return 0;
}
}
int
tenex(inputline, inputline_size)
Char *inputline;
int inputline_size;
{
int numitems, num_read;
char tinputline[BUFSIZE + 1];
setup_tty(ON);
while ((num_read = read(SHIN, tinputline, BUFSIZE)) > 0) {
static Char delims[] = {' ', '\'', '"', '\t', ';', '&', '<',
'>', '(', ')', '|', '^', '%', '\0'};
Char *str_end, *word_start, last_Char, should_retype;
int space_left;
COMMAND command;
tinputline[num_read] = 0;
Strcpy(inputline, str2short(tinputline));
num_read = Strlen(inputline);
last_Char = inputline[num_read - 1] & ASCII;
if (last_Char == '\n' || num_read == inputline_size)
break;
command = (last_Char == ESC) ? RECOGNIZE : LIST;
if (command == LIST)
xputchar('\n');
str_end = &inputline[num_read];
if (last_Char == ESC)
--str_end;
*str_end = '\0';
for (word_start = str_end; word_start > inputline; --word_start)
if (Strchr(delims, word_start[-1]))
break;
space_left = inputline_size - (word_start - inputline) - 1;
numitems = tsearch(word_start, command, space_left);
if (command == RECOGNIZE) {
print_recognized_stuff(str_end);
if (numitems != 1)
beep();
}
should_retype = FALSE;
if (Strchr(inputline, '\t')) {
back_to_col_1();
should_retype = TRUE;
}
if (command == LIST)
should_retype = TRUE;
if (should_retype)
printprompt(0, NULL);
pushback(inputline);
if (should_retype)
retype();
}
setup_tty(OFF);
return (num_read);
}
static int
ignored(item)
Char *item;
{
struct varent *vp;
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);
}
#endif