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