Home | History | Annotate | Line # | Download | only in csh
file.c revision 1.29.8.1
      1  1.29.8.1       tls /* $NetBSD: file.c,v 1.29.8.1 2014/08/19 23:45:10 tls Exp $ */
      2       1.9       cgd 
      3       1.1       cgd /*-
      4       1.8   mycroft  * Copyright (c) 1980, 1991, 1993
      5       1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15      1.23       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  */
     31       1.1       cgd 
     32      1.13  christos #include <sys/cdefs.h>
     33       1.1       cgd #ifndef lint
     34       1.9       cgd #if 0
     35       1.9       cgd static char sccsid[] = "@(#)file.c	8.2 (Berkeley) 3/19/94";
     36       1.9       cgd #else
     37  1.29.8.1       tls __RCSID("$NetBSD: file.c,v 1.29.8.1 2014/08/19 23:45:10 tls Exp $");
     38       1.9       cgd #endif
     39       1.1       cgd #endif /* not lint */
     40       1.1       cgd 
     41       1.1       cgd #ifdef FILEC
     42       1.1       cgd 
     43      1.18       wiz #include <sys/ioctl.h>
     44       1.1       cgd #include <sys/param.h>
     45       1.1       cgd #include <sys/stat.h>
     46      1.16     itohy #include <sys/tty.h>
     47      1.18       wiz 
     48       1.1       cgd #include <dirent.h>
     49       1.1       cgd #include <pwd.h>
     50      1.18       wiz #include <termios.h>
     51      1.19       wiz #include <stdarg.h>
     52       1.1       cgd #include <stdlib.h>
     53       1.1       cgd #include <unistd.h>
     54      1.18       wiz 
     55       1.8   mycroft #ifndef SHORT_STRINGS
     56       1.8   mycroft #include <string.h>
     57       1.8   mycroft #endif /* SHORT_STRINGS */
     58       1.1       cgd 
     59       1.1       cgd #include "csh.h"
     60       1.1       cgd #include "extern.h"
     61       1.1       cgd 
     62       1.1       cgd /*
     63       1.1       cgd  * Tenex style file name recognition, .. and more.
     64       1.1       cgd  * History:
     65       1.1       cgd  *	Author: Ken Greer, Sept. 1975, CMU.
     66       1.1       cgd  *	Finally got around to adding to the Cshell., Ken Greer, Dec. 1981.
     67       1.1       cgd  */
     68       1.1       cgd 
     69       1.1       cgd #define ON	1
     70       1.1       cgd #define OFF	0
     71       1.1       cgd #ifndef TRUE
     72       1.1       cgd #define TRUE 1
     73       1.1       cgd #endif
     74       1.1       cgd #ifndef FALSE
     75       1.1       cgd #define FALSE 0
     76       1.1       cgd #endif
     77       1.1       cgd 
     78      1.18       wiz #define ESC '\033'
     79       1.1       cgd 
     80       1.1       cgd typedef enum {
     81       1.1       cgd     LIST, RECOGNIZE
     82       1.1       cgd }       COMMAND;
     83       1.1       cgd 
     84      1.18       wiz static void setup_tty(int);
     85      1.18       wiz static void back_to_col_1(void);
     86      1.18       wiz static int pushback(Char *);
     87  1.29.8.1       tls static void catn(Char *, Char *, size_t);
     88  1.29.8.1       tls static void copyn(Char *, Char *, size_t);
     89      1.18       wiz static Char filetype(Char *, Char *);
     90  1.29.8.1       tls static void print_by_column(Char *, Char *[], size_t);
     91      1.18       wiz static Char *tilde(Char *, Char *);
     92      1.18       wiz static void retype(void);
     93      1.18       wiz static void beep(void);
     94      1.18       wiz static void print_recognized_stuff(Char *);
     95      1.18       wiz static void extract_dir_and_name(Char *, Char *, Char *);
     96      1.18       wiz static Char *getentry(DIR *, int);
     97      1.22  christos static void free_items(Char **, size_t);
     98  1.29.8.1       tls static size_t tsearch(Char *, COMMAND, size_t);
     99  1.29.8.1       tls static int recognize(Char *, Char *, size_t, size_t);
    100      1.18       wiz static int is_prefix(Char *, Char *);
    101      1.18       wiz static int is_suffix(Char *, Char *);
    102      1.18       wiz static int ignored(Char *);
    103       1.1       cgd 
    104       1.1       cgd /*
    105       1.1       cgd  * Put this here so the binary can be patched with adb to enable file
    106       1.1       cgd  * completion by default.  Filec controls completion, nobeep controls
    107       1.1       cgd  * ringing the terminal bell on incomplete expansions.
    108       1.1       cgd  */
    109      1.27  christos int filec = 0;
    110       1.1       cgd 
    111       1.1       cgd static void
    112      1.18       wiz setup_tty(int on)
    113       1.1       cgd {
    114       1.6       cgd     struct termios tchars;
    115       1.1       cgd 
    116      1.18       wiz     (void)tcgetattr(SHIN, &tchars);
    117       1.8   mycroft 
    118       1.1       cgd     if (on) {
    119       1.1       cgd 	tchars.c_cc[VEOL] = ESC;
    120       1.1       cgd 	if (tchars.c_lflag & ICANON)
    121       1.7        pk 	    on = TCSADRAIN;
    122       1.1       cgd 	else {
    123       1.8   mycroft 	    tchars.c_lflag |= ICANON;
    124       1.1       cgd 	    on = TCSAFLUSH;
    125       1.1       cgd 	}
    126       1.1       cgd     }
    127       1.1       cgd     else {
    128       1.1       cgd 	tchars.c_cc[VEOL] = _POSIX_VDISABLE;
    129       1.8   mycroft 	on = TCSADRAIN;
    130       1.1       cgd     }
    131       1.8   mycroft 
    132      1.18       wiz     (void)tcsetattr(SHIN, on, &tchars);
    133       1.1       cgd }
    134       1.1       cgd 
    135       1.1       cgd /*
    136       1.1       cgd  * Move back to beginning of current line
    137       1.1       cgd  */
    138       1.1       cgd static void
    139      1.18       wiz back_to_col_1(void)
    140       1.1       cgd {
    141       1.1       cgd     struct termios tty, tty_normal;
    142      1.21    kleink     sigset_t nsigset, osigset;
    143       1.1       cgd 
    144      1.21    kleink     sigemptyset(&nsigset);
    145      1.21    kleink     (void)sigaddset(&nsigset, SIGINT);
    146      1.21    kleink     (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    147      1.18       wiz     (void)tcgetattr(SHOUT, &tty);
    148       1.1       cgd     tty_normal = tty;
    149       1.1       cgd     tty.c_iflag &= ~INLCR;
    150       1.1       cgd     tty.c_oflag &= ~ONLCR;
    151      1.18       wiz     (void)tcsetattr(SHOUT, TCSADRAIN, &tty);
    152      1.18       wiz     (void)write(SHOUT, "\r", 1);
    153      1.18       wiz     (void)tcsetattr(SHOUT, TCSADRAIN, &tty_normal);
    154      1.18       wiz     (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
    155       1.1       cgd }
    156       1.1       cgd 
    157       1.1       cgd /*
    158       1.1       cgd  * Push string contents back into tty queue
    159       1.1       cgd  */
    160      1.15     itohy static int
    161      1.18       wiz pushback(Char *string)
    162       1.1       cgd {
    163       1.1       cgd     struct termios tty, tty_normal;
    164      1.29  christos     char buf[64], svchars[sizeof(buf)];
    165      1.21    kleink     sigset_t nsigset, osigset;
    166      1.18       wiz     Char *p;
    167      1.29  christos     size_t bufidx, i, len_str, nbuf, nsv, onsv, retrycnt;
    168      1.18       wiz     char c;
    169       1.1       cgd 
    170      1.18       wiz     nsv = 0;
    171      1.21    kleink     sigemptyset(&nsigset);
    172      1.21    kleink     (void)sigaddset(&nsigset, SIGINT);
    173      1.21    kleink     (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    174      1.18       wiz     (void)tcgetattr(SHOUT, &tty);
    175       1.1       cgd     tty_normal = tty;
    176       1.1       cgd     tty.c_lflag &= ~(ECHOKE | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOCTL);
    177      1.15     itohy     /* FIONREAD works only in noncanonical mode. */
    178      1.15     itohy     tty.c_lflag &= ~ICANON;
    179      1.15     itohy     tty.c_cc[VMIN] = 0;
    180      1.18       wiz     (void)tcsetattr(SHOUT, TCSADRAIN, &tty);
    181       1.1       cgd 
    182      1.15     itohy     for (retrycnt = 5; ; retrycnt--) {
    183      1.15     itohy 	/*
    184      1.15     itohy 	 * Push back characters.
    185      1.15     itohy 	 */
    186  1.29.8.1       tls 	for (p = string; (c = (char)*p) != '\0'; p++)
    187      1.18       wiz 	    (void)ioctl(SHOUT, TIOCSTI, (ioctl_t) &c);
    188      1.15     itohy 	for (i = 0; i < nsv; i++)
    189      1.18       wiz 	    (void)ioctl(SHOUT, TIOCSTI, (ioctl_t) &svchars[i]);
    190      1.15     itohy 
    191      1.15     itohy 	if (retrycnt == 0)
    192      1.15     itohy 	    break;		/* give up salvaging characters */
    193      1.15     itohy 
    194  1.29.8.1       tls 	len_str = (size_t)(p - string);
    195      1.15     itohy 
    196      1.15     itohy 	if (ioctl(SHOUT, FIONREAD, (ioctl_t) &nbuf) ||
    197      1.15     itohy 	    nbuf <= len_str + nsv ||	/* The string fit. */
    198      1.29  christos 	    nbuf > sizeof(buf))		/* For future binary compatibility
    199      1.15     itohy 					   (and safety). */
    200      1.15     itohy 	    break;
    201      1.15     itohy 
    202      1.15     itohy 	/*
    203      1.15     itohy 	 * User has typed characters before the pushback finished.
    204      1.15     itohy 	 * Salvage the characters.
    205      1.15     itohy 	 */
    206      1.15     itohy 
    207      1.15     itohy 	/* This read() should be in noncanonical mode. */
    208      1.29  christos 	if (read(SHOUT, &buf, nbuf) != (ssize_t)nbuf)
    209      1.15     itohy 	    continue;		/* hangup? */
    210      1.15     itohy 
    211      1.15     itohy 	onsv = nsv;
    212      1.15     itohy 	for (bufidx = 0, i = 0; bufidx < nbuf; bufidx++, i++) {
    213      1.15     itohy 	    c = buf[bufidx];
    214      1.18       wiz 	    if ((i < len_str) ? c != (char)string[i] :
    215      1.15     itohy 			(i < len_str + onsv) ? c != svchars[i - len_str] : 1) {
    216      1.15     itohy 		/* Salvage a character. */
    217      1.18       wiz 		if (nsv < (int)(sizeof svchars / sizeof svchars[0])) {
    218      1.15     itohy 		    svchars[nsv++] = c;
    219      1.15     itohy 		    i--;	/* try this comparison with the next char */
    220      1.15     itohy 		} else
    221      1.16     itohy 		    break;	/* too many */
    222      1.15     itohy 	    }
    223      1.15     itohy 	}
    224      1.15     itohy     }
    225      1.15     itohy 
    226      1.15     itohy #if 1
    227      1.15     itohy     /*
    228      1.15     itohy      * XXX  Is this a bug or a feature of kernel tty driver?
    229      1.15     itohy      *
    230      1.15     itohy      * FIONREAD in canonical mode does not return correct byte count
    231      1.15     itohy      * in tty input queue, but this is required to avoid unwanted echo.
    232      1.15     itohy      */
    233      1.15     itohy     tty.c_lflag |= ICANON;
    234      1.18       wiz     (void)tcsetattr(SHOUT, TCSADRAIN, &tty);
    235      1.18       wiz     (void)ioctl(SHOUT, FIONREAD, (ioctl_t) &i);
    236      1.15     itohy #endif
    237      1.18       wiz     (void)tcsetattr(SHOUT, TCSADRAIN, &tty_normal);
    238      1.18       wiz     (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
    239      1.15     itohy 
    240  1.29.8.1       tls     return (int)nsv;
    241       1.1       cgd }
    242       1.1       cgd 
    243       1.1       cgd /*
    244       1.1       cgd  * Concatenate src onto tail of des.
    245       1.1       cgd  * Des is a string whose maximum length is count.
    246       1.1       cgd  * Always null terminate.
    247       1.1       cgd  */
    248       1.1       cgd static void
    249  1.29.8.1       tls catn(Char *des, Char *src, size_t count)
    250       1.1       cgd {
    251  1.29.8.1       tls     while (count-- > 0 && *des)
    252       1.1       cgd 	des++;
    253  1.29.8.1       tls     while (count-- > 0)
    254       1.1       cgd 	if ((*des++ = *src++) == 0)
    255       1.1       cgd 	    return;
    256       1.1       cgd     *des = '\0';
    257       1.1       cgd }
    258       1.1       cgd 
    259       1.1       cgd /*
    260       1.1       cgd  * Like strncpy but always leave room for trailing \0
    261       1.1       cgd  * and always null terminate.
    262       1.1       cgd  */
    263       1.1       cgd static void
    264  1.29.8.1       tls copyn(Char *des, Char *src, size_t count)
    265       1.1       cgd {
    266  1.29.8.1       tls     while (count-- > 0)
    267       1.1       cgd 	if ((*des++ = *src++) == 0)
    268       1.1       cgd 	    return;
    269       1.1       cgd     *des = '\0';
    270       1.1       cgd }
    271       1.1       cgd 
    272      1.18       wiz static Char
    273      1.18       wiz filetype(Char *dir, Char *file)
    274       1.1       cgd {
    275       1.1       cgd     struct stat statb;
    276      1.18       wiz     Char path[MAXPATHLEN];
    277       1.1       cgd 
    278       1.1       cgd     catn(Strcpy(path, dir), file, sizeof(path) / sizeof(Char));
    279       1.1       cgd     if (lstat(short2str(path), &statb) == 0) {
    280       1.1       cgd 	switch (statb.st_mode & S_IFMT) {
    281       1.1       cgd 	case S_IFDIR:
    282       1.1       cgd 	    return ('/');
    283       1.1       cgd 	case S_IFLNK:
    284       1.1       cgd 	    if (stat(short2str(path), &statb) == 0 &&	/* follow it out */
    285       1.1       cgd 		S_ISDIR(statb.st_mode))
    286       1.1       cgd 		return ('>');
    287       1.1       cgd 	    else
    288       1.1       cgd 		return ('@');
    289       1.1       cgd 	case S_IFSOCK:
    290       1.1       cgd 	    return ('=');
    291       1.1       cgd 	default:
    292       1.1       cgd 	    if (statb.st_mode & 0111)
    293       1.1       cgd 		return ('*');
    294       1.1       cgd 	}
    295       1.1       cgd     }
    296       1.1       cgd     return (' ');
    297       1.1       cgd }
    298       1.1       cgd 
    299       1.1       cgd static struct winsize win;
    300       1.1       cgd 
    301       1.1       cgd /*
    302       1.1       cgd  * Print sorted down columns
    303       1.1       cgd  */
    304       1.1       cgd static void
    305  1.29.8.1       tls print_by_column(Char *dir, Char *items[], size_t count)
    306       1.1       cgd {
    307  1.29.8.1       tls     size_t c, columns, i, maxwidth, r, rows;
    308      1.18       wiz 
    309      1.18       wiz     maxwidth = 0;
    310       1.1       cgd 
    311       1.1       cgd     if (ioctl(SHOUT, TIOCGWINSZ, (ioctl_t) & win) < 0 || win.ws_col == 0)
    312       1.1       cgd 	win.ws_col = 80;
    313       1.1       cgd     for (i = 0; i < count; i++)
    314       1.1       cgd 	maxwidth = maxwidth > (r = Strlen(items[i])) ? maxwidth : r;
    315       1.1       cgd     maxwidth += 2;		/* for the file tag and space */
    316       1.1       cgd     columns = win.ws_col / maxwidth;
    317       1.1       cgd     if (columns == 0)
    318       1.1       cgd 	columns = 1;
    319       1.1       cgd     rows = (count + (columns - 1)) / columns;
    320       1.1       cgd     for (r = 0; r < rows; r++) {
    321       1.1       cgd 	for (c = 0; c < columns; c++) {
    322       1.1       cgd 	    i = c * rows + r;
    323       1.1       cgd 	    if (i < count) {
    324  1.29.8.1       tls 		size_t w;
    325       1.1       cgd 
    326      1.18       wiz 		(void)fprintf(cshout, "%s", vis_str(items[i]));
    327      1.18       wiz 		(void)fputc(dir ? filetype(dir, items[i]) : ' ', cshout);
    328       1.1       cgd 		if (c < columns - 1) {	/* last column? */
    329       1.1       cgd 		    w = Strlen(items[i]) + 1;
    330       1.1       cgd 		    for (; w < maxwidth; w++)
    331       1.8   mycroft 			(void) fputc(' ', cshout);
    332       1.1       cgd 		}
    333       1.1       cgd 	    }
    334       1.1       cgd 	}
    335      1.18       wiz 	(void)fputc('\r', cshout);
    336      1.18       wiz 	(void)fputc('\n', cshout);
    337       1.1       cgd     }
    338       1.1       cgd }
    339       1.1       cgd 
    340       1.1       cgd /*
    341       1.1       cgd  * Expand file name with possible tilde usage
    342       1.1       cgd  *	~person/mumble
    343       1.1       cgd  * expands to
    344       1.1       cgd  *	home_directory_of_person/mumble
    345       1.1       cgd  */
    346       1.1       cgd static Char *
    347      1.18       wiz tilde(Char *new, Char *old)
    348       1.1       cgd {
    349      1.18       wiz     static Char person[40];
    350      1.18       wiz     struct passwd *pw;
    351      1.12       tls     Char *o, *p;
    352       1.1       cgd 
    353       1.1       cgd     if (old[0] != '~')
    354       1.1       cgd 	return (Strcpy(new, old));
    355       1.1       cgd 
    356       1.8   mycroft     for (p = person, o = &old[1]; *o && *o != '/'; *p++ = *o++)
    357       1.8   mycroft 	continue;
    358       1.1       cgd     *p = '\0';
    359       1.1       cgd     if (person[0] == '\0')
    360      1.18       wiz 	(void)Strcpy(new, value(STRhome));
    361       1.1       cgd     else {
    362       1.1       cgd 	pw = getpwnam(short2str(person));
    363       1.1       cgd 	if (pw == NULL)
    364       1.1       cgd 	    return (NULL);
    365      1.18       wiz 	(void)Strcpy(new, str2short(pw->pw_dir));
    366       1.1       cgd     }
    367      1.18       wiz     (void)Strcat(new, o);
    368       1.1       cgd     return (new);
    369       1.1       cgd }
    370       1.1       cgd 
    371       1.1       cgd /*
    372       1.1       cgd  * Cause pending line to be printed
    373       1.1       cgd  */
    374       1.1       cgd static void
    375      1.18       wiz retype(void)
    376       1.1       cgd {
    377       1.1       cgd     struct termios tty;
    378       1.1       cgd 
    379      1.18       wiz     (void)tcgetattr(SHOUT, &tty);
    380       1.1       cgd     tty.c_lflag |= PENDIN;
    381      1.18       wiz     (void)tcsetattr(SHOUT, TCSADRAIN, &tty);
    382       1.1       cgd }
    383       1.1       cgd 
    384       1.1       cgd static void
    385      1.18       wiz beep(void)
    386       1.1       cgd {
    387       1.1       cgd     if (adrof(STRnobeep) == 0)
    388      1.18       wiz 	(void)write(SHOUT, "\007", 1);
    389       1.1       cgd }
    390       1.1       cgd 
    391       1.1       cgd /*
    392       1.1       cgd  * Erase that silly ^[ and
    393       1.1       cgd  * print the recognized part of the string
    394       1.1       cgd  */
    395       1.1       cgd static void
    396      1.18       wiz print_recognized_stuff(Char *recognized_part)
    397       1.1       cgd {
    398       1.1       cgd     /* An optimized erasing of that silly ^[ */
    399      1.18       wiz     (void)fputc('\b', cshout);
    400      1.18       wiz     (void)fputc('\b', cshout);
    401       1.1       cgd     switch (Strlen(recognized_part)) {
    402       1.1       cgd     case 0:			/* erase two Characters: ^[ */
    403      1.18       wiz 	(void)fputc(' ', cshout);
    404      1.18       wiz 	(void)fputc(' ', cshout);
    405      1.18       wiz 	(void)fputc('\b', cshout);
    406      1.18       wiz 	(void)fputc('\b', cshout);
    407       1.1       cgd 	break;
    408       1.1       cgd     case 1:			/* overstrike the ^, erase the [ */
    409      1.18       wiz 	(void)fprintf(cshout, "%s", vis_str(recognized_part));
    410      1.18       wiz 	(void)fputc(' ', cshout);
    411      1.18       wiz 	(void)fputc('\b', cshout);
    412       1.1       cgd 	break;
    413       1.1       cgd     default:			/* overstrike both Characters ^[ */
    414      1.18       wiz 	(void)fprintf(cshout, "%s", vis_str(recognized_part));
    415       1.1       cgd 	break;
    416       1.1       cgd     }
    417      1.18       wiz     (void)fflush(cshout);
    418       1.1       cgd }
    419       1.1       cgd 
    420       1.1       cgd /*
    421       1.1       cgd  * Parse full path in file into 2 parts: directory and file names
    422       1.1       cgd  * Should leave final slash (/) at end of dir.
    423       1.1       cgd  */
    424       1.1       cgd static void
    425      1.18       wiz extract_dir_and_name(Char *path, Char *dir, Char *name)
    426       1.1       cgd {
    427      1.12       tls     Char *p;
    428       1.1       cgd 
    429       1.1       cgd     p = Strrchr(path, '/');
    430       1.1       cgd     if (p == NULL) {
    431       1.1       cgd 	copyn(name, path, MAXNAMLEN);
    432       1.1       cgd 	dir[0] = '\0';
    433       1.1       cgd     }
    434       1.1       cgd     else {
    435       1.1       cgd 	copyn(name, ++p, MAXNAMLEN);
    436  1.29.8.1       tls 	copyn(dir, path, (size_t)(p - path));
    437       1.1       cgd     }
    438       1.1       cgd }
    439       1.1       cgd 
    440       1.1       cgd static Char *
    441      1.18       wiz getentry(DIR *dir_fd, int looking_for_lognames)
    442       1.1       cgd {
    443      1.18       wiz     struct dirent *dirp;
    444      1.12       tls     struct passwd *pw;
    445       1.1       cgd 
    446       1.1       cgd     if (looking_for_lognames) {
    447       1.1       cgd 	if ((pw = getpwent()) == NULL)
    448       1.1       cgd 	    return (NULL);
    449       1.1       cgd 	return (str2short(pw->pw_name));
    450       1.1       cgd     }
    451       1.8   mycroft     if ((dirp = readdir(dir_fd)) != NULL)
    452       1.1       cgd 	return (str2short(dirp->d_name));
    453       1.1       cgd     return (NULL);
    454       1.1       cgd }
    455       1.1       cgd 
    456       1.1       cgd static void
    457      1.22  christos free_items(Char **items, size_t numitems)
    458       1.1       cgd {
    459      1.22  christos     size_t i;
    460       1.1       cgd 
    461      1.22  christos     for (i = 0; i < numitems; i++)
    462       1.1       cgd 	xfree((ptr_t) items[i]);
    463       1.1       cgd     xfree((ptr_t) items);
    464       1.1       cgd }
    465       1.1       cgd 
    466      1.22  christos #define FREE_ITEMS(items, numitems) { \
    467      1.21    kleink 	sigset_t nsigset, osigset;\
    468       1.1       cgd \
    469      1.21    kleink 	sigemptyset(&nsigset);\
    470      1.21    kleink 	(void) sigaddset(&nsigset, SIGINT);\
    471      1.21    kleink 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);\
    472      1.22  christos 	free_items(items, numitems);\
    473      1.14   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, NULL);\
    474       1.1       cgd }
    475       1.1       cgd 
    476       1.1       cgd /*
    477       1.1       cgd  * Perform a RECOGNIZE or LIST command on string "word".
    478       1.1       cgd  */
    479  1.29.8.1       tls static size_t
    480  1.29.8.1       tls tsearch(Char *word, COMMAND command, size_t max_word_length)
    481       1.1       cgd {
    482      1.18       wiz     Char dir[MAXPATHLEN + 1], extended_name[MAXNAMLEN + 1];
    483      1.18       wiz     Char name[MAXNAMLEN + 1], tilded_dir[MAXPATHLEN + 1];
    484      1.12       tls     DIR *dir_fd;
    485      1.18       wiz     Char *entry;
    486  1.29.8.1       tls     int ignoring, looking_for_lognames;
    487  1.29.8.1       tls     size_t name_length, nignored, numitems;
    488      1.22  christos     Char **items = NULL;
    489      1.22  christos     size_t maxitems = 0;
    490      1.18       wiz 
    491      1.18       wiz     numitems = 0;
    492      1.18       wiz     ignoring = TRUE;
    493      1.18       wiz     nignored = 0;
    494       1.1       cgd 
    495       1.1       cgd     looking_for_lognames = (*word == '~') && (Strchr(word, '/') == NULL);
    496       1.1       cgd     if (looking_for_lognames) {
    497      1.18       wiz 	(void)setpwent();
    498       1.1       cgd 	copyn(name, &word[1], MAXNAMLEN);	/* name sans ~ */
    499       1.1       cgd 	dir_fd = NULL;
    500       1.1       cgd     }
    501       1.1       cgd     else {
    502       1.1       cgd 	extract_dir_and_name(word, dir, name);
    503       1.1       cgd 	if (tilde(tilded_dir, dir) == 0)
    504       1.1       cgd 	    return (0);
    505       1.1       cgd 	dir_fd = opendir(*tilded_dir ? short2str(tilded_dir) : ".");
    506       1.1       cgd 	if (dir_fd == NULL)
    507       1.1       cgd 	    return (0);
    508       1.1       cgd     }
    509       1.1       cgd 
    510       1.1       cgd again:				/* search for matches */
    511       1.1       cgd     name_length = Strlen(name);
    512       1.8   mycroft     for (numitems = 0; (entry = getentry(dir_fd, looking_for_lognames)) != NULL;) {
    513       1.1       cgd 	if (!is_prefix(name, entry))
    514       1.1       cgd 	    continue;
    515       1.1       cgd 	/* Don't match . files on null prefix match */
    516       1.1       cgd 	if (name_length == 0 && entry[0] == '.' &&
    517       1.1       cgd 	    !looking_for_lognames)
    518       1.1       cgd 	    continue;
    519       1.1       cgd 	if (command == LIST) {
    520      1.28     lukem 	    if ((size_t)numitems >= maxitems) {
    521      1.22  christos 		maxitems += 1024;
    522      1.22  christos 		if (items == NULL)
    523  1.29.8.1       tls 			items = xmalloc(sizeof(*items) * maxitems);
    524      1.22  christos 		else
    525  1.29.8.1       tls 			items = xrealloc((ptr_t) items,
    526      1.22  christos 			    sizeof(*items) * maxitems);
    527      1.22  christos  	    }
    528  1.29.8.1       tls 	    items[numitems] = xmalloc((size_t) (Strlen(entry) + 1) *
    529      1.18       wiz 	        sizeof(Char));
    530       1.1       cgd 	    copyn(items[numitems], entry, MAXNAMLEN);
    531       1.1       cgd 	    numitems++;
    532       1.1       cgd 	}
    533       1.1       cgd 	else {			/* RECOGNIZE command */
    534       1.1       cgd 	    if (ignoring && ignored(entry))
    535       1.1       cgd 		nignored++;
    536       1.1       cgd 	    else if (recognize(extended_name,
    537      1.18       wiz 	        entry, name_length, ++numitems))
    538       1.1       cgd 		break;
    539       1.1       cgd 	}
    540       1.1       cgd     }
    541       1.1       cgd     if (ignoring && numitems == 0 && nignored > 0) {
    542       1.1       cgd 	ignoring = FALSE;
    543       1.1       cgd 	nignored = 0;
    544       1.1       cgd 	if (looking_for_lognames)
    545      1.18       wiz 	    (void)setpwent();
    546       1.1       cgd 	else
    547       1.1       cgd 	    rewinddir(dir_fd);
    548       1.1       cgd 	goto again;
    549       1.1       cgd     }
    550       1.1       cgd 
    551       1.1       cgd     if (looking_for_lognames)
    552      1.18       wiz 	(void)endpwent();
    553       1.1       cgd     else
    554      1.18       wiz 	(void)closedir(dir_fd);
    555       1.1       cgd     if (numitems == 0)
    556       1.1       cgd 	return (0);
    557       1.1       cgd     if (command == RECOGNIZE) {
    558       1.1       cgd 	if (looking_for_lognames)
    559       1.1       cgd 	    copyn(word, STRtilde, 1);
    560       1.1       cgd 	else
    561       1.1       cgd 	    /* put back dir part */
    562       1.1       cgd 	    copyn(word, dir, max_word_length);
    563       1.1       cgd 	/* add extended name */
    564       1.1       cgd 	catn(word, extended_name, max_word_length);
    565       1.1       cgd 	return (numitems);
    566       1.1       cgd     }
    567       1.1       cgd     else {			/* LIST */
    568       1.1       cgd 	qsort((ptr_t) items, numitems, sizeof(items[0]),
    569      1.18       wiz 		(int (*) (const void *, const void *)) sortscmp);
    570       1.1       cgd 	print_by_column(looking_for_lognames ? NULL : tilded_dir,
    571       1.1       cgd 			items, numitems);
    572       1.1       cgd 	if (items != NULL)
    573      1.22  christos 	    FREE_ITEMS(items, numitems);
    574       1.1       cgd     }
    575       1.1       cgd     return (0);
    576       1.1       cgd }
    577       1.1       cgd 
    578       1.1       cgd /*
    579       1.1       cgd  * Object: extend what user typed up to an ambiguity.
    580       1.1       cgd  * Algorithm:
    581       1.1       cgd  * On first match, copy full entry (assume it'll be the only match)
    582       1.1       cgd  * On subsequent matches, shorten extended_name to the first
    583       1.1       cgd  * Character mismatch between extended_name and entry.
    584       1.1       cgd  * If we shorten it back to the prefix length, stop searching.
    585       1.1       cgd  */
    586       1.1       cgd static int
    587  1.29.8.1       tls recognize(Char *extended_name, Char *entry, size_t name_length, size_t numitems)
    588       1.1       cgd {
    589       1.1       cgd     if (numitems == 1)		/* 1st match */
    590       1.1       cgd 	copyn(extended_name, entry, MAXNAMLEN);
    591       1.1       cgd     else {			/* 2nd & subsequent matches */
    592      1.18       wiz 	Char *ent, *x;
    593  1.29.8.1       tls 	size_t len = 0;
    594       1.1       cgd 
    595       1.1       cgd 	x = extended_name;
    596       1.8   mycroft 	for (ent = entry; *x && *x == *ent++; x++, len++)
    597       1.8   mycroft 	    continue;
    598       1.1       cgd 	*x = '\0';		/* Shorten at 1st Char diff */
    599       1.1       cgd 	if (len == name_length)	/* Ambiguous to prefix? */
    600       1.1       cgd 	    return (-1);	/* So stop now and save time */
    601       1.1       cgd     }
    602       1.1       cgd     return (0);
    603       1.1       cgd }
    604       1.1       cgd 
    605       1.1       cgd /*
    606       1.1       cgd  * Return true if check matches initial Chars in template.
    607       1.1       cgd  * This differs from PWB imatch in that if check is null
    608       1.1       cgd  * it matches anything.
    609       1.1       cgd  */
    610       1.1       cgd static int
    611      1.18       wiz is_prefix(Char *check, Char *template)
    612       1.1       cgd {
    613       1.1       cgd     do
    614       1.1       cgd 	if (*check == 0)
    615       1.1       cgd 	    return (TRUE);
    616       1.1       cgd     while (*check++ == *template++);
    617       1.1       cgd     return (FALSE);
    618       1.1       cgd }
    619       1.1       cgd 
    620       1.1       cgd /*
    621       1.1       cgd  *  Return true if the Chars in template appear at the
    622      1.24       snj  *  end of check, I.e., are its suffix.
    623       1.1       cgd  */
    624       1.1       cgd static int
    625      1.18       wiz is_suffix(Char *check, Char *template)
    626       1.1       cgd {
    627      1.12       tls     Char *c, *t;
    628       1.1       cgd 
    629       1.8   mycroft     for (c = check; *c++;)
    630       1.8   mycroft 	continue;
    631       1.8   mycroft     for (t = template; *t++;)
    632       1.8   mycroft 	continue;
    633       1.1       cgd     for (;;) {
    634       1.1       cgd 	if (t == template)
    635       1.1       cgd 	    return 1;
    636       1.1       cgd 	if (c == check || *--t != *--c)
    637       1.1       cgd 	    return 0;
    638       1.1       cgd     }
    639       1.1       cgd }
    640       1.1       cgd 
    641  1.29.8.1       tls ssize_t
    642  1.29.8.1       tls tenex(Char *inputline, size_t inputline_size)
    643       1.1       cgd {
    644      1.18       wiz     char tinputline[BUFSIZE];
    645  1.29.8.1       tls     ssize_t num_read;
    646  1.29.8.1       tls     size_t numitems;
    647       1.1       cgd 
    648       1.1       cgd     setup_tty(ON);
    649       1.1       cgd 
    650      1.17  christos     while ((num_read = read(SHIN, tinputline, BUFSIZE)) > 0) {
    651  1.29.8.1       tls 	size_t i, nr = (size_t) num_read;
    652  1.29.8.1       tls 
    653      1.18       wiz 
    654       1.1       cgd 	static Char delims[] = {' ', '\'', '"', '\t', ';', '&', '<',
    655       1.1       cgd 	'>', '(', ')', '|', '^', '%', '\0'};
    656      1.12       tls 	Char *str_end, *word_start, last_Char, should_retype;
    657  1.29.8.1       tls 	size_t space_left;
    658       1.1       cgd 	COMMAND command;
    659       1.1       cgd 
    660  1.29.8.1       tls 	for (i = 0; i < nr; i++)
    661       1.1       cgd 	    inputline[i] = (unsigned char) tinputline[i];
    662  1.29.8.1       tls 	last_Char = inputline[nr - 1] & ASCII;
    663       1.1       cgd 
    664  1.29.8.1       tls 	if (last_Char == '\n' || nr == inputline_size)
    665       1.1       cgd 	    break;
    666       1.1       cgd 	command = (last_Char == ESC) ? RECOGNIZE : LIST;
    667       1.1       cgd 	if (command == LIST)
    668      1.18       wiz 	    (void)fputc('\n', cshout);
    669  1.29.8.1       tls 	str_end = &inputline[nr];
    670       1.1       cgd 	if (last_Char == ESC)
    671       1.1       cgd 	    --str_end;		/* wipeout trailing cmd Char */
    672       1.1       cgd 	*str_end = '\0';
    673       1.1       cgd 	/*
    674       1.1       cgd 	 * Find LAST occurence of a delimiter in the inputline. The word start
    675       1.1       cgd 	 * is one Character past it.
    676       1.1       cgd 	 */
    677       1.1       cgd 	for (word_start = str_end; word_start > inputline; --word_start)
    678       1.1       cgd 	    if (Strchr(delims, word_start[-1]))
    679       1.1       cgd 		break;
    680  1.29.8.1       tls 	space_left = inputline_size - (size_t)(word_start - inputline) - 1;
    681       1.1       cgd 	numitems = tsearch(word_start, command, space_left);
    682       1.1       cgd 
    683       1.1       cgd 	if (command == RECOGNIZE) {
    684       1.1       cgd 	    /* print from str_end on */
    685       1.1       cgd 	    print_recognized_stuff(str_end);
    686       1.1       cgd 	    if (numitems != 1)	/* Beep = No match/ambiguous */
    687       1.1       cgd 		beep();
    688       1.1       cgd 	}
    689       1.1       cgd 
    690       1.1       cgd 	/*
    691       1.1       cgd 	 * Tabs in the input line cause trouble after a pushback. tty driver
    692       1.1       cgd 	 * won't backspace over them because column positions are now
    693       1.1       cgd 	 * incorrect. This is solved by retyping over current line.
    694       1.1       cgd 	 */
    695       1.1       cgd 	should_retype = FALSE;
    696       1.1       cgd 	if (Strchr(inputline, '\t')) {	/* tab Char in input line? */
    697       1.1       cgd 	    back_to_col_1();
    698       1.1       cgd 	    should_retype = TRUE;
    699       1.1       cgd 	}
    700       1.1       cgd 	if (command == LIST)	/* Always retype after a LIST */
    701       1.1       cgd 	    should_retype = TRUE;
    702      1.15     itohy 	if (pushback(inputline))
    703      1.15     itohy 	    should_retype = TRUE;
    704      1.15     itohy 	if (should_retype) {
    705      1.15     itohy 	    if (command == RECOGNIZE)
    706      1.15     itohy 		(void) fputc('\n', cshout);
    707       1.1       cgd 	    printprompt();
    708      1.15     itohy 	}
    709       1.1       cgd 	if (should_retype)
    710       1.1       cgd 	    retype();
    711       1.1       cgd     }
    712       1.1       cgd     setup_tty(OFF);
    713  1.29.8.1       tls     return num_read;
    714       1.1       cgd }
    715       1.1       cgd 
    716       1.1       cgd static int
    717      1.18       wiz ignored(Char *entry)
    718       1.1       cgd {
    719       1.1       cgd     struct varent *vp;
    720      1.12       tls     Char **cp;
    721       1.1       cgd 
    722       1.1       cgd     if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL)
    723       1.1       cgd 	return (FALSE);
    724       1.1       cgd     for (; *cp != NULL; cp++)
    725       1.1       cgd 	if (is_suffix(entry, *cp))
    726       1.1       cgd 	    return (TRUE);
    727       1.1       cgd     return (FALSE);
    728       1.1       cgd }
    729       1.1       cgd #endif				/* FILEC */
    730