xref: /freebsd/contrib/tcsh/sh.file.c (revision 6560ac57ce879857203bc456cdc3849808dc0700)
1c80476e4SDavid E. O'Brien /*
2c80476e4SDavid E. O'Brien  * sh.file.c: File completion for csh. This file is not used in tcsh.
3c80476e4SDavid E. O'Brien  */
4c80476e4SDavid E. O'Brien /*-
5c80476e4SDavid E. O'Brien  * Copyright (c) 1980, 1991 The Regents of the University of California.
6c80476e4SDavid E. O'Brien  * All rights reserved.
7c80476e4SDavid E. O'Brien  *
8c80476e4SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
9c80476e4SDavid E. O'Brien  * modification, are permitted provided that the following conditions
10c80476e4SDavid E. O'Brien  * are met:
11c80476e4SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
12c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
13c80476e4SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
14c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
15c80476e4SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
1629301572SMark Peek  * 3. Neither the name of the University nor the names of its contributors
17c80476e4SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
18c80476e4SDavid E. O'Brien  *    without specific prior written permission.
19c80476e4SDavid E. O'Brien  *
20c80476e4SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21c80476e4SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22c80476e4SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23c80476e4SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24c80476e4SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25c80476e4SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26c80476e4SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27c80476e4SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28c80476e4SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29c80476e4SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30c80476e4SDavid E. O'Brien  * SUCH DAMAGE.
31c80476e4SDavid E. O'Brien  */
32c80476e4SDavid E. O'Brien #include "sh.h"
3329301572SMark Peek #include "ed.h"
34c80476e4SDavid E. O'Brien 
3529301572SMark Peek #if defined(FILEC) && defined(TIOCSTI)
36c80476e4SDavid E. O'Brien 
37c80476e4SDavid E. O'Brien /*
38c80476e4SDavid E. O'Brien  * Tenex style file name recognition, .. and more.
39c80476e4SDavid E. O'Brien  * History:
40c80476e4SDavid E. O'Brien  *	Author: Ken Greer, Sept. 1975, CMU.
41c80476e4SDavid E. O'Brien  *	Finally got around to adding to the Cshell., Ken Greer, Dec. 1981.
42c80476e4SDavid E. O'Brien  */
43c80476e4SDavid E. O'Brien 
44c80476e4SDavid E. O'Brien #define ON	1
45c80476e4SDavid E. O'Brien #define OFF	0
46c80476e4SDavid E. O'Brien #ifndef TRUE
47c80476e4SDavid E. O'Brien #define TRUE 1
48c80476e4SDavid E. O'Brien #endif
49c80476e4SDavid E. O'Brien #ifndef FALSE
50c80476e4SDavid E. O'Brien #define FALSE 0
51c80476e4SDavid E. O'Brien #endif
52c80476e4SDavid E. O'Brien 
53c80476e4SDavid E. O'Brien #define ESC     CTL_ESC('\033')
54c80476e4SDavid E. O'Brien 
55c80476e4SDavid E. O'Brien typedef enum {
56c80476e4SDavid E. O'Brien     LIST, RECOGNIZE
57c80476e4SDavid E. O'Brien }       COMMAND;
58c80476e4SDavid E. O'Brien 
5945e5710bSMark Peek static	void	 setup_tty		(int);
6045e5710bSMark Peek static	void	 back_to_col_1		(void);
6145e5710bSMark Peek static	void	 pushback		(const Char *);
6245e5710bSMark Peek static	int	 filetype		(const Char *, const Char *);
6345e5710bSMark Peek static	void	 print_by_column	(const Char *, Char *[], size_t);
6445e5710bSMark Peek static	Char 	*tilde			(const Char *);
6545e5710bSMark Peek static	void	 retype			(void);
6645e5710bSMark Peek static	void	 beep			(void);
6745e5710bSMark Peek static	void 	 print_recognized_stuff	(const Char *);
6845e5710bSMark Peek static	void	 extract_dir_and_name	(const Char *, Char **, const Char **);
6945e5710bSMark Peek static	Char	*getitem		(DIR *, int);
7045e5710bSMark Peek static	size_t	 tsearch		(Char *, COMMAND, size_t);
7145e5710bSMark Peek static	int	 compare		(const void *, const void *);
7245e5710bSMark Peek static	int	 recognize		(Char **, Char *, size_t, size_t);
7345e5710bSMark Peek static	int	 is_prefix		(const Char *, const Char *);
7445e5710bSMark Peek static	int	 is_suffix		(const Char *, const Char *);
7545e5710bSMark Peek static	int	 ignored		(const Char *);
76c80476e4SDavid E. O'Brien 
77c80476e4SDavid E. O'Brien 
78c80476e4SDavid E. O'Brien /*
79c80476e4SDavid E. O'Brien  * Put this here so the binary can be patched with adb to enable file
80c80476e4SDavid E. O'Brien  * completion by default.  Filec controls completion, nobeep controls
81c80476e4SDavid E. O'Brien  * ringing the terminal bell on incomplete expansions.
82c80476e4SDavid E. O'Brien  */
8323338178SMark Peek int    filec = 0;
84c80476e4SDavid E. O'Brien 
85c80476e4SDavid E. O'Brien static void
setup_tty(int on)8645e5710bSMark Peek setup_tty(int on)
87c80476e4SDavid E. O'Brien {
88c80476e4SDavid E. O'Brien #ifdef TERMIO
89c80476e4SDavid E. O'Brien # ifdef POSIX
90c80476e4SDavid E. O'Brien     struct termios tchars;
91c80476e4SDavid E. O'Brien # else
92c80476e4SDavid E. O'Brien     struct termio tchars;
93c80476e4SDavid E. O'Brien # endif /* POSIX */
94c80476e4SDavid E. O'Brien 
95c80476e4SDavid E. O'Brien # ifdef POSIX
96c80476e4SDavid E. O'Brien     (void) tcgetattr(SHIN, &tchars);
97c80476e4SDavid E. O'Brien # else
98c80476e4SDavid E. O'Brien     (void) ioctl(SHIN, TCGETA, (ioctl_t) &tchars);
99c80476e4SDavid E. O'Brien # endif /* POSIX */
100c80476e4SDavid E. O'Brien     if (on) {
101c80476e4SDavid E. O'Brien 	tchars.c_cc[VEOL] = ESC;
102c80476e4SDavid E. O'Brien 	if (tchars.c_lflag & ICANON)
103c80476e4SDavid E. O'Brien # ifdef POSIX
104c80476e4SDavid E. O'Brien 	    on = TCSADRAIN;
105c80476e4SDavid E. O'Brien # else
106c80476e4SDavid E. O'Brien 	    on = TCSETA;
107c80476e4SDavid E. O'Brien # endif /* POSIX */
108c80476e4SDavid E. O'Brien 	else {
109c80476e4SDavid E. O'Brien # ifdef POSIX
110c80476e4SDavid E. O'Brien 	    on = TCSAFLUSH;
111c80476e4SDavid E. O'Brien # else
112c80476e4SDavid E. O'Brien 	    on = TCSETAF;
113c80476e4SDavid E. O'Brien # endif /* POSIX */
114c80476e4SDavid E. O'Brien 	    tchars.c_lflag |= ICANON;
115c80476e4SDavid E. O'Brien 
116c80476e4SDavid E. O'Brien 	}
117c80476e4SDavid E. O'Brien     }
118c80476e4SDavid E. O'Brien     else {
119c80476e4SDavid E. O'Brien 	tchars.c_cc[VEOL] = _POSIX_VDISABLE;
120c80476e4SDavid E. O'Brien # ifdef POSIX
121c80476e4SDavid E. O'Brien 	on = TCSADRAIN;
122c80476e4SDavid E. O'Brien # else
123c80476e4SDavid E. O'Brien         on = TCSETA;
124c80476e4SDavid E. O'Brien # endif /* POSIX */
125c80476e4SDavid E. O'Brien     }
126c80476e4SDavid E. O'Brien # ifdef POSIX
12745e5710bSMark Peek     (void) xtcsetattr(SHIN, on, &tchars);
128c80476e4SDavid E. O'Brien # else
129c80476e4SDavid E. O'Brien     (void) ioctl(SHIN, on, (ioctl_t) &tchars);
130c80476e4SDavid E. O'Brien # endif /* POSIX */
131c80476e4SDavid E. O'Brien #else
132c80476e4SDavid E. O'Brien     struct sgttyb sgtty;
133c80476e4SDavid E. O'Brien     static struct tchars tchars;/* INT, QUIT, XON, XOFF, EOF, BRK */
134c80476e4SDavid E. O'Brien 
135c80476e4SDavid E. O'Brien     if (on) {
136c80476e4SDavid E. O'Brien 	(void) ioctl(SHIN, TIOCGETC, (ioctl_t) & tchars);
137c80476e4SDavid E. O'Brien 	tchars.t_brkc = ESC;
138c80476e4SDavid E. O'Brien 	(void) ioctl(SHIN, TIOCSETC, (ioctl_t) & tchars);
139c80476e4SDavid E. O'Brien 	/*
140c80476e4SDavid E. O'Brien 	 * This must be done after every command: if the tty gets into raw or
141c80476e4SDavid E. O'Brien 	 * cbreak mode the user can't even type 'reset'.
142c80476e4SDavid E. O'Brien 	 */
143c80476e4SDavid E. O'Brien 	(void) ioctl(SHIN, TIOCGETP, (ioctl_t) & sgtty);
144c80476e4SDavid E. O'Brien 	if (sgtty.sg_flags & (RAW | CBREAK)) {
145c80476e4SDavid E. O'Brien 	    sgtty.sg_flags &= ~(RAW | CBREAK);
146c80476e4SDavid E. O'Brien 	    (void) ioctl(SHIN, TIOCSETP, (ioctl_t) & sgtty);
147c80476e4SDavid E. O'Brien 	}
148c80476e4SDavid E. O'Brien     }
149c80476e4SDavid E. O'Brien     else {
150c80476e4SDavid E. O'Brien 	tchars.t_brkc = -1;
151c80476e4SDavid E. O'Brien 	(void) ioctl(SHIN, TIOCSETC, (ioctl_t) & tchars);
152c80476e4SDavid E. O'Brien     }
153c80476e4SDavid E. O'Brien #endif /* TERMIO */
154c80476e4SDavid E. O'Brien }
155c80476e4SDavid E. O'Brien 
156c80476e4SDavid E. O'Brien /*
157c80476e4SDavid E. O'Brien  * Move back to beginning of current line
158c80476e4SDavid E. O'Brien  */
159c80476e4SDavid E. O'Brien static void
back_to_col_1(void)16045e5710bSMark Peek back_to_col_1(void)
161c80476e4SDavid E. O'Brien {
162c80476e4SDavid E. O'Brien #ifdef TERMIO
163c80476e4SDavid E. O'Brien # ifdef POSIX
164c80476e4SDavid E. O'Brien     struct termios tty, tty_normal;
165c80476e4SDavid E. O'Brien # else
166c80476e4SDavid E. O'Brien     struct termio tty, tty_normal;
167c80476e4SDavid E. O'Brien # endif /* POSIX */
168c80476e4SDavid E. O'Brien #else
169c80476e4SDavid E. O'Brien     struct sgttyb tty, tty_normal;
170c80476e4SDavid E. O'Brien #endif /* TERMIO */
171c80476e4SDavid E. O'Brien 
17245e5710bSMark Peek     pintr_disabled++;
17345e5710bSMark Peek     cleanup_push(&pintr_disabled, disabled_cleanup);
174c80476e4SDavid E. O'Brien 
175c80476e4SDavid E. O'Brien #ifdef TERMIO
176c80476e4SDavid E. O'Brien # ifdef POSIX
177c80476e4SDavid E. O'Brien     (void) tcgetattr(SHOUT, &tty);
178c80476e4SDavid E. O'Brien # else
179c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TCGETA, (ioctl_t) &tty_normal);
180c80476e4SDavid E. O'Brien # endif /* POSIX */
181c80476e4SDavid E. O'Brien     tty_normal = tty;
182c80476e4SDavid E. O'Brien     tty.c_iflag &= ~INLCR;
183c80476e4SDavid E. O'Brien     tty.c_oflag &= ~ONLCR;
184c80476e4SDavid E. O'Brien # ifdef POSIX
18545e5710bSMark Peek     (void) xtcsetattr(SHOUT, TCSANOW, &tty);
186c80476e4SDavid E. O'Brien # else
187c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
188c80476e4SDavid E. O'Brien # endif /* POSIX */
18945e5710bSMark Peek     (void) xwrite(SHOUT, "\r", 1);
190c80476e4SDavid E. O'Brien # ifdef POSIX
19145e5710bSMark Peek     (void) xtcsetattr(SHOUT, TCSANOW, &tty_normal);
192c80476e4SDavid E. O'Brien # else
193c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty_normal);
194c80476e4SDavid E. O'Brien # endif /* POSIX */
195c80476e4SDavid E. O'Brien #else
196c80476e4SDavid E. O'Brien     (void) ioctl(SHIN, TIOCGETP, (ioctl_t) & tty);
197c80476e4SDavid E. O'Brien     tty_normal = tty;
198c80476e4SDavid E. O'Brien     tty.sg_flags &= ~CRMOD;
199c80476e4SDavid E. O'Brien     (void) ioctl(SHIN, TIOCSETN, (ioctl_t) & tty);
20045e5710bSMark Peek     (void) xwrite(SHOUT, "\r", 1);
201c80476e4SDavid E. O'Brien     (void) ioctl(SHIN, TIOCSETN, (ioctl_t) & tty_normal);
202c80476e4SDavid E. O'Brien #endif /* TERMIO */
203c80476e4SDavid E. O'Brien 
20445e5710bSMark Peek     cleanup_until(&pintr_disabled);
205c80476e4SDavid E. O'Brien }
206c80476e4SDavid E. O'Brien 
207c80476e4SDavid E. O'Brien /*
208c80476e4SDavid E. O'Brien  * Push string contents back into tty queue
209c80476e4SDavid E. O'Brien  */
210c80476e4SDavid E. O'Brien static void
pushback(const Char * string)21145e5710bSMark Peek pushback(const Char *string)
212c80476e4SDavid E. O'Brien {
21345e5710bSMark Peek     const Char *p;
214c80476e4SDavid E. O'Brien #ifdef TERMIO
215c80476e4SDavid E. O'Brien # ifdef POSIX
216c80476e4SDavid E. O'Brien     struct termios tty, tty_normal;
217c80476e4SDavid E. O'Brien # else
218c80476e4SDavid E. O'Brien     struct termio tty, tty_normal;
219c80476e4SDavid E. O'Brien # endif /* POSIX */
220c80476e4SDavid E. O'Brien #else
221c80476e4SDavid E. O'Brien     struct sgttyb tty, tty_normal;
222c80476e4SDavid E. O'Brien #endif /* TERMIO */
223c80476e4SDavid E. O'Brien 
22445e5710bSMark Peek     pintr_disabled++;
22545e5710bSMark Peek     cleanup_push(&pintr_disabled, disabled_cleanup);
226c80476e4SDavid E. O'Brien 
227c80476e4SDavid E. O'Brien #ifdef TERMIO
228c80476e4SDavid E. O'Brien # ifdef POSIX
229c80476e4SDavid E. O'Brien     (void) tcgetattr(SHOUT, &tty);
230c80476e4SDavid E. O'Brien # else
231*19d2e3deSDmitry Chagin     (void) ioctl(SHOUT, TCGETA, (ioctl_t) &tty);
232c80476e4SDavid E. O'Brien # endif /* POSIX */
233c80476e4SDavid E. O'Brien     tty_normal = tty;
234a15e6f9aSMark Peek     tty.c_lflag &= ~(ECHOKE | ECHO | ECHOE | ECHOK | ECHONL |
235a15e6f9aSMark Peek #ifndef __QNXNTO__
236a15e6f9aSMark Peek 	ECHOPRT |
237a15e6f9aSMark Peek #endif
238a15e6f9aSMark Peek 	ECHOCTL);
239c80476e4SDavid E. O'Brien # ifdef POSIX
24045e5710bSMark Peek     (void) xtcsetattr(SHOUT, TCSANOW, &tty);
241c80476e4SDavid E. O'Brien # else
242c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
243c80476e4SDavid E. O'Brien # endif /* POSIX */
244c80476e4SDavid E. O'Brien 
24523338178SMark Peek     for (p = string; *p != '\0'; p++) {
24623338178SMark Peek 	char buf[MB_LEN_MAX];
24723338178SMark Peek 	size_t i, len;
24823338178SMark Peek 
249*19d2e3deSDmitry Chagin 	len = one_wctomb(buf, *p);
25023338178SMark Peek 	for (i = 0; i < len; i++)
25123338178SMark Peek 	    (void) ioctl(SHOUT, TIOCSTI, (ioctl_t) &buf[i]);
25223338178SMark Peek     }
253c80476e4SDavid E. O'Brien # ifdef POSIX
25445e5710bSMark Peek     (void) xtcsetattr(SHOUT, TCSANOW, &tty_normal);
255c80476e4SDavid E. O'Brien # else
256c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty_normal);
257c80476e4SDavid E. O'Brien # endif /* POSIX */
258c80476e4SDavid E. O'Brien #else
259c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TIOCGETP, (ioctl_t) & tty);
260c80476e4SDavid E. O'Brien     tty_normal = tty;
261c80476e4SDavid E. O'Brien     tty.sg_flags &= ~ECHO;
262c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TIOCSETN, (ioctl_t) & tty);
263c80476e4SDavid E. O'Brien 
264c80476e4SDavid E. O'Brien     for (p = string; c = *p; p++)
265c80476e4SDavid E. O'Brien 	(void) ioctl(SHOUT, TIOCSTI, (ioctl_t) & c);
266c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TIOCSETN, (ioctl_t) & tty_normal);
267c80476e4SDavid E. O'Brien #endif /* TERMIO */
268c80476e4SDavid E. O'Brien 
26945e5710bSMark Peek     cleanup_until(&pintr_disabled);
270c80476e4SDavid E. O'Brien }
271c80476e4SDavid E. O'Brien 
27223338178SMark Peek static int
filetype(const Char * dir,const Char * file)27345e5710bSMark Peek filetype(const Char *dir, const Char *file)
274c80476e4SDavid E. O'Brien {
27545e5710bSMark Peek     Char    *path;
27645e5710bSMark Peek     char *spath;
277c80476e4SDavid E. O'Brien     struct stat statb;
278c80476e4SDavid E. O'Brien 
27945e5710bSMark Peek     path = Strspl(dir, file);
28045e5710bSMark Peek     spath = short2str(path);
28145e5710bSMark Peek     xfree(path);
28245e5710bSMark Peek     if (lstat(spath, &statb) == 0) {
283c80476e4SDavid E. O'Brien 	switch (statb.st_mode & S_IFMT) {
284c80476e4SDavid E. O'Brien 	case S_IFDIR:
285c80476e4SDavid E. O'Brien 	    return ('/');
286c80476e4SDavid E. O'Brien 
287c80476e4SDavid E. O'Brien 	case S_IFLNK:
28845e5710bSMark Peek 	    if (stat(spath, &statb) == 0 &&	/* follow it out */
289c80476e4SDavid E. O'Brien 		S_ISDIR(statb.st_mode))
290c80476e4SDavid E. O'Brien 		return ('>');
291c80476e4SDavid E. O'Brien 	    else
292c80476e4SDavid E. O'Brien 		return ('@');
293c80476e4SDavid E. O'Brien 
294c80476e4SDavid E. O'Brien 	case S_IFSOCK:
295c80476e4SDavid E. O'Brien 	    return ('=');
296c80476e4SDavid E. O'Brien 
297c80476e4SDavid E. O'Brien 	default:
298c80476e4SDavid E. O'Brien 	    if (statb.st_mode & 0111)
299c80476e4SDavid E. O'Brien 		return ('*');
300c80476e4SDavid E. O'Brien 	}
301c80476e4SDavid E. O'Brien     }
302c80476e4SDavid E. O'Brien     return (' ');
303c80476e4SDavid E. O'Brien }
304c80476e4SDavid E. O'Brien 
305c80476e4SDavid E. O'Brien /*
306c80476e4SDavid E. O'Brien  * Print sorted down columns
307c80476e4SDavid E. O'Brien  */
308c80476e4SDavid E. O'Brien static void
print_by_column(const Char * dir,Char * items[],size_t count)30945e5710bSMark Peek print_by_column(const Char *dir, Char *items[], size_t count)
310c80476e4SDavid E. O'Brien {
31145e5710bSMark Peek     struct winsize win;
31223338178SMark Peek     size_t i;
31323338178SMark Peek     int rows, r, c, maxwidth = 0, columns;
314c80476e4SDavid E. O'Brien 
315c80476e4SDavid E. O'Brien     if (ioctl(SHOUT, TIOCGWINSZ, (ioctl_t) & win) < 0 || win.ws_col == 0)
316c80476e4SDavid E. O'Brien 	win.ws_col = 80;
317c80476e4SDavid E. O'Brien     for (i = 0; i < count; i++)
318c80476e4SDavid E. O'Brien 	maxwidth = maxwidth > (r = Strlen(items[i])) ? maxwidth : r;
319c80476e4SDavid E. O'Brien     maxwidth += 2;		/* for the file tag and space */
320c80476e4SDavid E. O'Brien     columns = win.ws_col / maxwidth;
321c80476e4SDavid E. O'Brien     if (columns == 0)
322c80476e4SDavid E. O'Brien 	columns = 1;
323c80476e4SDavid E. O'Brien     rows = (count + (columns - 1)) / columns;
324c80476e4SDavid E. O'Brien     for (r = 0; r < rows; r++) {
325c80476e4SDavid E. O'Brien 	for (c = 0; c < columns; c++) {
326c80476e4SDavid E. O'Brien 	    i = c * rows + r;
327c80476e4SDavid E. O'Brien 	    if (i < count) {
32829301572SMark Peek 		int w;
329c80476e4SDavid E. O'Brien 
330c80476e4SDavid E. O'Brien 		xprintf("%S", items[i]);
331c80476e4SDavid E. O'Brien 		xputchar(dir ? filetype(dir, items[i]) : ' ');
332c80476e4SDavid E. O'Brien 		if (c < columns - 1) {	/* last column? */
333c80476e4SDavid E. O'Brien 		    w = Strlen(items[i]) + 1;
334c80476e4SDavid E. O'Brien 		    for (; w < maxwidth; w++)
335c80476e4SDavid E. O'Brien 			xputchar(' ');
336c80476e4SDavid E. O'Brien 		}
337c80476e4SDavid E. O'Brien 	    }
338c80476e4SDavid E. O'Brien 	}
339c80476e4SDavid E. O'Brien 	xputchar('\r');
340c80476e4SDavid E. O'Brien 	xputchar('\n');
341c80476e4SDavid E. O'Brien     }
342c80476e4SDavid E. O'Brien }
343c80476e4SDavid E. O'Brien 
344c80476e4SDavid E. O'Brien /*
345c80476e4SDavid E. O'Brien  * Expand file name with possible tilde usage
346c80476e4SDavid E. O'Brien  *	~person/mumble
347c80476e4SDavid E. O'Brien  * expands to
348c80476e4SDavid E. O'Brien  *	home_directory_of_person/mumble
349c80476e4SDavid E. O'Brien  */
350c80476e4SDavid E. O'Brien static Char *
tilde(const Char * old)35145e5710bSMark Peek tilde(const Char *old)
352c80476e4SDavid E. O'Brien {
35345e5710bSMark Peek     const Char *o, *home;
35429301572SMark Peek     struct passwd *pw;
355c80476e4SDavid E. O'Brien 
356c80476e4SDavid E. O'Brien     if (old[0] != '~')
35745e5710bSMark Peek 	return (Strsave(old));
35845e5710bSMark Peek     old++;
359c80476e4SDavid E. O'Brien 
36045e5710bSMark Peek     for (o = old; *o != '\0' && *o != '/'; o++)
36145e5710bSMark Peek 	;
36245e5710bSMark Peek     if (o == old)
36345e5710bSMark Peek 	home = varval(STRhome);
364c80476e4SDavid E. O'Brien     else {
36545e5710bSMark Peek 	Char *person;
36645e5710bSMark Peek 
36745e5710bSMark Peek 	person = Strnsave(old, o - old);
36845e5710bSMark Peek 	pw = xgetpwnam(short2str(person));
36945e5710bSMark Peek 	xfree(person);
370c80476e4SDavid E. O'Brien 	if (pw == NULL)
371c80476e4SDavid E. O'Brien 	    return (NULL);
37245e5710bSMark Peek 	home = str2short(pw->pw_dir);
373c80476e4SDavid E. O'Brien     }
37445e5710bSMark Peek     return Strspl(home, o);
375c80476e4SDavid E. O'Brien }
376c80476e4SDavid E. O'Brien 
377c80476e4SDavid E. O'Brien /*
378c80476e4SDavid E. O'Brien  * Cause pending line to be printed
379c80476e4SDavid E. O'Brien  */
380c80476e4SDavid E. O'Brien static void
retype(void)38145e5710bSMark Peek retype(void)
382c80476e4SDavid E. O'Brien {
383c80476e4SDavid E. O'Brien #ifdef TERMIO
384c80476e4SDavid E. O'Brien # ifdef POSIX
385c80476e4SDavid E. O'Brien     struct termios tty;
386c80476e4SDavid E. O'Brien 
387c80476e4SDavid E. O'Brien     (void) tcgetattr(SHOUT, &tty);
388c80476e4SDavid E. O'Brien # else
389c80476e4SDavid E. O'Brien     struct termio tty;
390c80476e4SDavid E. O'Brien 
391c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TCGETA, (ioctl_t) &tty);
392c80476e4SDavid E. O'Brien # endif /* POSIX */
393c80476e4SDavid E. O'Brien 
394a15e6f9aSMark Peek #ifndef __QNXNTO__
395c80476e4SDavid E. O'Brien     tty.c_lflag |= PENDIN;
396a15e6f9aSMark Peek #endif
397c80476e4SDavid E. O'Brien 
398c80476e4SDavid E. O'Brien # ifdef POSIX
39945e5710bSMark Peek     (void) xtcsetattr(SHOUT, TCSANOW, &tty);
400c80476e4SDavid E. O'Brien # else
401c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
402c80476e4SDavid E. O'Brien # endif /* POSIX */
403c80476e4SDavid E. O'Brien #else
404c80476e4SDavid E. O'Brien     int     pending_input = LPENDIN;
405c80476e4SDavid E. O'Brien 
406c80476e4SDavid E. O'Brien     (void) ioctl(SHOUT, TIOCLBIS, (ioctl_t) & pending_input);
407c80476e4SDavid E. O'Brien #endif /* TERMIO */
408c80476e4SDavid E. O'Brien }
409c80476e4SDavid E. O'Brien 
410c80476e4SDavid E. O'Brien static void
beep(void)41145e5710bSMark Peek beep(void)
412c80476e4SDavid E. O'Brien {
413c80476e4SDavid E. O'Brien     if (adrof(STRnobeep) == 0)
4143b6eaa7bSAndrey A. Chernov #ifdef IS_ASCII
41545e5710bSMark Peek 	(void) xwrite(SHOUT, "\007", 1);
4163b6eaa7bSAndrey A. Chernov #else
417c80476e4SDavid E. O'Brien     {
418c80476e4SDavid E. O'Brien 	unsigned char beep_ch = CTL_ESC('\007');
41945e5710bSMark Peek 	(void) xwrite(SHOUT, &beep_ch, 1);
420c80476e4SDavid E. O'Brien     }
4213b6eaa7bSAndrey A. Chernov #endif
422c80476e4SDavid E. O'Brien }
423c80476e4SDavid E. O'Brien 
424c80476e4SDavid E. O'Brien /*
425c80476e4SDavid E. O'Brien  * Erase that silly ^[ and
426c80476e4SDavid E. O'Brien  * print the recognized part of the string
427c80476e4SDavid E. O'Brien  */
428c80476e4SDavid E. O'Brien static void
print_recognized_stuff(const Char * recognized_part)42945e5710bSMark Peek print_recognized_stuff(const Char *recognized_part)
430c80476e4SDavid E. O'Brien {
431c80476e4SDavid E. O'Brien     /* An optimized erasing of that silly ^[ */
432c80476e4SDavid E. O'Brien     (void) putraw('\b');
433c80476e4SDavid E. O'Brien     (void) putraw('\b');
434c80476e4SDavid E. O'Brien     switch (Strlen(recognized_part)) {
435c80476e4SDavid E. O'Brien 
436c80476e4SDavid E. O'Brien     case 0:			/* erase two Characters: ^[ */
437c80476e4SDavid E. O'Brien 	(void) putraw(' ');
438c80476e4SDavid E. O'Brien 	(void) putraw(' ');
439c80476e4SDavid E. O'Brien 	(void) putraw('\b');
440c80476e4SDavid E. O'Brien 	(void) putraw('\b');
441c80476e4SDavid E. O'Brien 	break;
442c80476e4SDavid E. O'Brien 
443c80476e4SDavid E. O'Brien     case 1:			/* overstrike the ^, erase the [ */
444c80476e4SDavid E. O'Brien 	xprintf("%S", recognized_part);
445c80476e4SDavid E. O'Brien 	(void) putraw(' ');
446c80476e4SDavid E. O'Brien 	(void) putraw('\b');
447c80476e4SDavid E. O'Brien 	break;
448c80476e4SDavid E. O'Brien 
449c80476e4SDavid E. O'Brien     default:			/* overstrike both Characters ^[ */
450c80476e4SDavid E. O'Brien 	xprintf("%S", recognized_part);
451c80476e4SDavid E. O'Brien 	break;
452c80476e4SDavid E. O'Brien     }
453c80476e4SDavid E. O'Brien     flush();
454c80476e4SDavid E. O'Brien }
455c80476e4SDavid E. O'Brien 
456c80476e4SDavid E. O'Brien /*
457c80476e4SDavid E. O'Brien  * Parse full path in file into 2 parts: directory and file names
458c80476e4SDavid E. O'Brien  * Should leave final slash (/) at end of dir.
459c80476e4SDavid E. O'Brien  */
460c80476e4SDavid E. O'Brien static void
extract_dir_and_name(const Char * path,Char ** dir,const Char ** name)46145e5710bSMark Peek extract_dir_and_name(const Char *path, Char **dir, const Char **name)
462c80476e4SDavid E. O'Brien {
46345e5710bSMark Peek     const Char *p;
464c80476e4SDavid E. O'Brien 
465c80476e4SDavid E. O'Brien     p = Strrchr(path, '/');
46645e5710bSMark Peek     if (p == NULL)
46745e5710bSMark Peek 	p = path;
46845e5710bSMark Peek     else
46945e5710bSMark Peek 	p++;
47045e5710bSMark Peek     *name = p;
47145e5710bSMark Peek     *dir = Strnsave(path, p - path);
472c80476e4SDavid E. O'Brien }
47323338178SMark Peek 
474c80476e4SDavid E. O'Brien static Char *
getitem(DIR * dir_fd,int looking_for_lognames)47545e5710bSMark Peek getitem(DIR *dir_fd, int looking_for_lognames)
476c80476e4SDavid E. O'Brien {
47729301572SMark Peek     struct passwd *pw;
47829301572SMark Peek     struct dirent *dirp;
479c80476e4SDavid E. O'Brien 
480c80476e4SDavid E. O'Brien     if (looking_for_lognames) {
48123338178SMark Peek #ifndef HAVE_GETPWENT
482c80476e4SDavid E. O'Brien 	    return (NULL);
483c80476e4SDavid E. O'Brien #else
484c80476e4SDavid E. O'Brien 	if ((pw = getpwent()) == NULL)
485c80476e4SDavid E. O'Brien 	    return (NULL);
486c80476e4SDavid E. O'Brien 	return (str2short(pw->pw_name));
487c80476e4SDavid E. O'Brien #endif /* atp vmsposix */
488c80476e4SDavid E. O'Brien     }
48929301572SMark Peek     if ((dirp = readdir(dir_fd)) != NULL)
490c80476e4SDavid E. O'Brien 	return (str2short(dirp->d_name));
491c80476e4SDavid E. O'Brien     return (NULL);
492c80476e4SDavid E. O'Brien }
493c80476e4SDavid E. O'Brien 
494c80476e4SDavid E. O'Brien /*
495c80476e4SDavid E. O'Brien  * Perform a RECOGNIZE or LIST command on string "word".
496c80476e4SDavid E. O'Brien  */
49745e5710bSMark Peek static size_t
tsearch(Char * word,COMMAND command,size_t max_word_length)49845e5710bSMark Peek tsearch(Char *word, COMMAND command, size_t max_word_length)
499c80476e4SDavid E. O'Brien {
50029301572SMark Peek     DIR *dir_fd;
50123338178SMark Peek     int ignoring = TRUE, nignored = 0;
50245e5710bSMark Peek     int looking_for_lognames;
50345e5710bSMark Peek     Char *tilded_dir = NULL, *dir = NULL;
50445e5710bSMark Peek     Char *extended_name = NULL;
50545e5710bSMark Peek     const Char *name;
506c80476e4SDavid E. O'Brien     Char *item;
50745e5710bSMark Peek     struct blk_buf items = BLK_BUF_INIT;
50845e5710bSMark Peek     size_t name_length;
509c80476e4SDavid E. O'Brien 
510c80476e4SDavid E. O'Brien     looking_for_lognames = (*word == '~') && (Strchr(word, '/') == NULL);
511c80476e4SDavid E. O'Brien     if (looking_for_lognames) {
51223338178SMark Peek #ifdef HAVE_GETPWENT
513c80476e4SDavid E. O'Brien 	(void) setpwent();
51423338178SMark Peek #endif
51545e5710bSMark Peek 	name = word + 1;	/* name sans ~ */
516c80476e4SDavid E. O'Brien 	dir_fd = NULL;
51745e5710bSMark Peek 	cleanup_push(dir, xfree);
518c80476e4SDavid E. O'Brien     }
519c80476e4SDavid E. O'Brien     else {
52045e5710bSMark Peek 	extract_dir_and_name(word, &dir, &name);
52145e5710bSMark Peek 	cleanup_push(dir, xfree);
52245e5710bSMark Peek 	tilded_dir = tilde(dir);
52345e5710bSMark Peek 	if (tilded_dir == NULL)
52445e5710bSMark Peek 	    goto end;
52545e5710bSMark Peek 	cleanup_push(tilded_dir, xfree);
526c80476e4SDavid E. O'Brien 	dir_fd = opendir(*tilded_dir ? short2str(tilded_dir) : ".");
527c80476e4SDavid E. O'Brien 	if (dir_fd == NULL)
52845e5710bSMark Peek 	    goto end;
529c80476e4SDavid E. O'Brien     }
530c80476e4SDavid E. O'Brien 
531c80476e4SDavid E. O'Brien     name_length = Strlen(name);
53245e5710bSMark Peek     cleanup_push(&extended_name, xfree_indirect);
53345e5710bSMark Peek     cleanup_push(&items, bb_cleanup);
53445e5710bSMark Peek again:				/* search for matches */
53545e5710bSMark Peek     while ((item = getitem(dir_fd, looking_for_lognames)) != NULL) {
536c80476e4SDavid E. O'Brien 	if (!is_prefix(name, item))
537c80476e4SDavid E. O'Brien 	    continue;
538c80476e4SDavid E. O'Brien 	/* Don't match . files on null prefix match */
539c80476e4SDavid E. O'Brien 	if (name_length == 0 && item[0] == '.' &&
540c80476e4SDavid E. O'Brien 	    !looking_for_lognames)
541c80476e4SDavid E. O'Brien 	    continue;
54245e5710bSMark Peek 	if (command == LIST)
54345e5710bSMark Peek 	    bb_append(&items, Strsave(item));
544c80476e4SDavid E. O'Brien 	else {			/* RECOGNIZE command */
545c80476e4SDavid E. O'Brien 	    if (ignoring && ignored(item))
546c80476e4SDavid E. O'Brien 		nignored++;
54745e5710bSMark Peek 	    else if (recognize(&extended_name, item, name_length, ++items.len))
548c80476e4SDavid E. O'Brien 		break;
549c80476e4SDavid E. O'Brien 	}
550c80476e4SDavid E. O'Brien     }
55145e5710bSMark Peek     if (ignoring && items.len == 0 && nignored > 0) {
552c80476e4SDavid E. O'Brien 	ignoring = FALSE;
553c80476e4SDavid E. O'Brien 	nignored = 0;
55423338178SMark Peek 	if (looking_for_lognames) {
55523338178SMark Peek #ifdef HAVE_GETPWENT
556c80476e4SDavid E. O'Brien 	    (void) setpwent();
557c80476e4SDavid E. O'Brien #endif /* atp vmsposix */
55823338178SMark Peek 	} else
559c80476e4SDavid E. O'Brien 	    rewinddir(dir_fd);
560c80476e4SDavid E. O'Brien 	goto again;
561c80476e4SDavid E. O'Brien     }
562c80476e4SDavid E. O'Brien 
56323338178SMark Peek     if (looking_for_lognames) {
564*19d2e3deSDmitry Chagin #ifdef HAVE_GETPWENT
565c80476e4SDavid E. O'Brien 	(void) endpwent();
56623338178SMark Peek #endif
56723338178SMark Peek     } else
56845e5710bSMark Peek 	xclosedir(dir_fd);
56945e5710bSMark Peek     if (items.len != 0) {
570c80476e4SDavid E. O'Brien 	if (command == RECOGNIZE) {
571c80476e4SDavid E. O'Brien 	    if (looking_for_lognames)
57245e5710bSMark Peek 		copyn(word, STRtilde, 2);/*FIXBUF, sort of */
573c80476e4SDavid E. O'Brien 	    else
574c80476e4SDavid E. O'Brien 		/* put back dir part */
57545e5710bSMark Peek 		copyn(word, dir, max_word_length);/*FIXBUF*/
576c80476e4SDavid E. O'Brien 	    /* add extended name */
57745e5710bSMark Peek 	    catn(word, extended_name, max_word_length);/*FIXBUF*/
578c80476e4SDavid E. O'Brien 	}
579c80476e4SDavid E. O'Brien 	else {			/* LIST */
58045e5710bSMark Peek 	    qsort(items.vec, items.len, sizeof(items.vec[0]), compare);
581c80476e4SDavid E. O'Brien 	    print_by_column(looking_for_lognames ? NULL : tilded_dir,
58245e5710bSMark Peek 			    items.vec, items.len);
583c80476e4SDavid E. O'Brien 	}
58445e5710bSMark Peek     }
58545e5710bSMark Peek  end:
58645e5710bSMark Peek     cleanup_until(dir);
58745e5710bSMark Peek     return items.len;
588c80476e4SDavid E. O'Brien }
589c80476e4SDavid E. O'Brien 
59029301572SMark Peek 
59129301572SMark Peek static int
compare(const void * p,const void * q)59245e5710bSMark Peek compare(const void *p, const void *q)
59329301572SMark Peek {
5949ccc37e3SMark Peek #if defined (WIDE_STRINGS) && !defined (UTF16_STRING)
59523338178SMark Peek     errno = 0;
59623338178SMark Peek 
59745e5710bSMark Peek     return (wcscoll(*(Char *const *) p, *(Char *const *) q));
59823338178SMark Peek #else
59923338178SMark Peek     char *p1, *q1;
60023338178SMark Peek     int res;
60123338178SMark Peek 
60245e5710bSMark Peek     p1 = strsave(short2str(*(Char *const *) p));
60345e5710bSMark Peek     q1 = strsave(short2str(*(Char *const *) q));
60445e5710bSMark Peek # if defined(NLS) && defined(HAVE_STRCOLL)
60523338178SMark Peek     res = strcoll(p1, q1);
60629301572SMark Peek # else
60723338178SMark Peek     res = strcmp(p1, q1);
60845e5710bSMark Peek # endif /* NLS && HAVE_STRCOLL */
60923338178SMark Peek     xfree (p1);
61023338178SMark Peek     xfree (q1);
61123338178SMark Peek     return res;
61223338178SMark Peek #endif /* not WIDE_STRINGS */
61329301572SMark Peek }
61429301572SMark Peek 
615c80476e4SDavid E. O'Brien /*
616c80476e4SDavid E. O'Brien  * Object: extend what user typed up to an ambiguity.
617c80476e4SDavid E. O'Brien  * Algorithm:
618c80476e4SDavid E. O'Brien  * On first match, copy full item (assume it'll be the only match)
619c80476e4SDavid E. O'Brien  * On subsequent matches, shorten extended_name to the first
620c80476e4SDavid E. O'Brien  * Character mismatch between extended_name and item.
621c80476e4SDavid E. O'Brien  * If we shorten it back to the prefix length, stop searching.
622c80476e4SDavid E. O'Brien  */
623c80476e4SDavid E. O'Brien static int
recognize(Char ** extended_name,Char * item,size_t name_length,size_t numitems)62445e5710bSMark Peek recognize(Char **extended_name, Char *item, size_t name_length,
62545e5710bSMark Peek 	  size_t numitems)
626c80476e4SDavid E. O'Brien {
627c80476e4SDavid E. O'Brien     if (numitems == 1)		/* 1st match */
62845e5710bSMark Peek 	*extended_name = Strsave(item);
629c80476e4SDavid E. O'Brien     else {			/* 2nd & subsequent matches */
63029301572SMark Peek 	Char *x, *ent;
63145e5710bSMark Peek 	size_t len = 0;
632c80476e4SDavid E. O'Brien 
63345e5710bSMark Peek 	x = *extended_name;
634c80476e4SDavid E. O'Brien 	for (ent = item; *x && *x == *ent++; x++, len++);
635c80476e4SDavid E. O'Brien 	*x = '\0';		/* Shorten at 1st Char diff */
636c80476e4SDavid E. O'Brien 	if (len == name_length)	/* Ambiguous to prefix? */
637c80476e4SDavid E. O'Brien 	    return (-1);	/* So stop now and save time */
638c80476e4SDavid E. O'Brien     }
639c80476e4SDavid E. O'Brien     return (0);
640c80476e4SDavid E. O'Brien }
641c80476e4SDavid E. O'Brien 
642c80476e4SDavid E. O'Brien /*
643c80476e4SDavid E. O'Brien  * Return true if check matches initial Chars in template.
644c80476e4SDavid E. O'Brien  * This differs from PWB imatch in that if check is null
645c80476e4SDavid E. O'Brien  * it matches anything.
646c80476e4SDavid E. O'Brien  */
647c80476e4SDavid E. O'Brien static int
is_prefix(const Char * check,const Char * template)64845e5710bSMark Peek is_prefix(const Char *check, const Char *template)
649c80476e4SDavid E. O'Brien {
650c80476e4SDavid E. O'Brien     do
651c80476e4SDavid E. O'Brien 	if (*check == 0)
652c80476e4SDavid E. O'Brien 	    return (TRUE);
653c80476e4SDavid E. O'Brien     while (*check++ == *template++);
654c80476e4SDavid E. O'Brien     return (FALSE);
655c80476e4SDavid E. O'Brien }
656c80476e4SDavid E. O'Brien 
657c80476e4SDavid E. O'Brien /*
658c80476e4SDavid E. O'Brien  *  Return true if the Chars in template appear at the
659c80476e4SDavid E. O'Brien  *  end of check, I.e., are it's suffix.
660c80476e4SDavid E. O'Brien  */
661c80476e4SDavid E. O'Brien static int
is_suffix(const Char * check,const Char * template)66245e5710bSMark Peek is_suffix(const Char *check, const Char *template)
663c80476e4SDavid E. O'Brien {
66445e5710bSMark Peek     const Char *c, *t;
665c80476e4SDavid E. O'Brien 
666c80476e4SDavid E. O'Brien     for (c = check; *c++;);
667c80476e4SDavid E. O'Brien     for (t = template; *t++;);
668c80476e4SDavid E. O'Brien     for (;;) {
669c80476e4SDavid E. O'Brien 	if (t == template)
670c80476e4SDavid E. O'Brien 	    return 1;
671c80476e4SDavid E. O'Brien 	if (c == check || *--t != *--c)
672c80476e4SDavid E. O'Brien 	    return 0;
673c80476e4SDavid E. O'Brien     }
674c80476e4SDavid E. O'Brien }
675c80476e4SDavid E. O'Brien 
67645e5710bSMark Peek static void
setup_tty_cleanup(void * dummy)67745e5710bSMark Peek setup_tty_cleanup(void *dummy)
678c80476e4SDavid E. O'Brien {
67945e5710bSMark Peek     USE(dummy);
68045e5710bSMark Peek     setup_tty(OFF);
68145e5710bSMark Peek }
682c80476e4SDavid E. O'Brien 
68345e5710bSMark Peek size_t
tenex(Char * inputline,size_t inputline_size)68445e5710bSMark Peek tenex(Char *inputline, size_t inputline_size)
68545e5710bSMark Peek {
68645e5710bSMark Peek     size_t numitems;
68745e5710bSMark Peek     ssize_t num_read;
68845e5710bSMark Peek     char    tinputline[BUFSIZE + 1];/*FIXBUF*/
689c80476e4SDavid E. O'Brien 
690c80476e4SDavid E. O'Brien     setup_tty(ON);
69145e5710bSMark Peek     cleanup_push(&num_read, setup_tty_cleanup); /* num_read is only a marker */
692c80476e4SDavid E. O'Brien 
69345e5710bSMark Peek     while ((num_read = xread(SHIN, tinputline, BUFSIZE)) > 0) {/*FIXBUF*/
69445e5710bSMark Peek 	static const Char delims[] = {' ', '\'', '"', '\t', ';', '&', '<',
695c80476e4SDavid E. O'Brien 	'>', '(', ')', '|', '^', '%', '\0'};
69629301572SMark Peek 	Char *str_end, *word_start, last_Char, should_retype;
69745e5710bSMark Peek 	size_t space_left;
698c80476e4SDavid E. O'Brien 	COMMAND command;
699c80476e4SDavid E. O'Brien 
70023338178SMark Peek 	tinputline[num_read] = 0;
70145e5710bSMark Peek 	Strcpy(inputline, str2short(tinputline));/*FIXBUF*/
70223338178SMark Peek 	num_read = Strlen(inputline);
70345e5710bSMark Peek 	last_Char = CTL_ESC(ASC(inputline[num_read - 1]) & ASCII);
704c80476e4SDavid E. O'Brien 
70545e5710bSMark Peek 	if (last_Char == '\n' || (size_t)num_read == inputline_size)
706c80476e4SDavid E. O'Brien 	    break;
707c80476e4SDavid E. O'Brien 	command = (last_Char == ESC) ? RECOGNIZE : LIST;
708c80476e4SDavid E. O'Brien 	if (command == LIST)
709c80476e4SDavid E. O'Brien 	    xputchar('\n');
710c80476e4SDavid E. O'Brien 	str_end = &inputline[num_read];
711c80476e4SDavid E. O'Brien 	if (last_Char == ESC)
712c80476e4SDavid E. O'Brien 	    --str_end;		/* wipeout trailing cmd Char */
713c80476e4SDavid E. O'Brien 	*str_end = '\0';
714c80476e4SDavid E. O'Brien 	/*
715c80476e4SDavid E. O'Brien 	 * Find LAST occurence of a delimiter in the inputline. The word start
716c80476e4SDavid E. O'Brien 	 * is one Character past it.
717c80476e4SDavid E. O'Brien 	 */
718c80476e4SDavid E. O'Brien 	for (word_start = str_end; word_start > inputline; --word_start)
719c80476e4SDavid E. O'Brien 	    if (Strchr(delims, word_start[-1]))
720c80476e4SDavid E. O'Brien 		break;
721c80476e4SDavid E. O'Brien 	space_left = inputline_size - (word_start - inputline) - 1;
722c80476e4SDavid E. O'Brien 	numitems = tsearch(word_start, command, space_left);
723c80476e4SDavid E. O'Brien 
724c80476e4SDavid E. O'Brien 	if (command == RECOGNIZE) {
725c80476e4SDavid E. O'Brien 	    /* print from str_end on */
726c80476e4SDavid E. O'Brien 	    print_recognized_stuff(str_end);
727c80476e4SDavid E. O'Brien 	    if (numitems != 1)	/* Beep = No match/ambiguous */
728c80476e4SDavid E. O'Brien 		beep();
729c80476e4SDavid E. O'Brien 	}
730c80476e4SDavid E. O'Brien 
731c80476e4SDavid E. O'Brien 	/*
732c80476e4SDavid E. O'Brien 	 * Tabs in the input line cause trouble after a pushback. tty driver
733c80476e4SDavid E. O'Brien 	 * won't backspace over them because column positions are now
734c80476e4SDavid E. O'Brien 	 * incorrect. This is solved by retyping over current line.
735c80476e4SDavid E. O'Brien 	 */
736c80476e4SDavid E. O'Brien 	should_retype = FALSE;
737c80476e4SDavid E. O'Brien 	if (Strchr(inputline, '\t')) {	/* tab Char in input line? */
738c80476e4SDavid E. O'Brien 	    back_to_col_1();
739c80476e4SDavid E. O'Brien 	    should_retype = TRUE;
740c80476e4SDavid E. O'Brien 	}
741c80476e4SDavid E. O'Brien 	if (command == LIST)	/* Always retype after a LIST */
742c80476e4SDavid E. O'Brien 	    should_retype = TRUE;
743c80476e4SDavid E. O'Brien 	if (should_retype)
744c80476e4SDavid E. O'Brien 	    printprompt(0, NULL);
745c80476e4SDavid E. O'Brien 	pushback(inputline);
746c80476e4SDavid E. O'Brien 	if (should_retype)
747c80476e4SDavid E. O'Brien 	    retype();
748c80476e4SDavid E. O'Brien     }
74945e5710bSMark Peek     cleanup_until(&num_read);
750c80476e4SDavid E. O'Brien     return (num_read);
751c80476e4SDavid E. O'Brien }
752c80476e4SDavid E. O'Brien 
753c80476e4SDavid E. O'Brien static int
ignored(const Char * item)75445e5710bSMark Peek ignored(const Char *item)
755c80476e4SDavid E. O'Brien {
756c80476e4SDavid E. O'Brien     struct varent *vp;
75729301572SMark Peek     Char **cp;
758c80476e4SDavid E. O'Brien 
759c80476e4SDavid E. O'Brien     if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL)
760c80476e4SDavid E. O'Brien 	return (FALSE);
761c80476e4SDavid E. O'Brien     for (; *cp != NULL; cp++)
762c80476e4SDavid E. O'Brien 	if (is_suffix(item, *cp))
763c80476e4SDavid E. O'Brien 	    return (TRUE);
764c80476e4SDavid E. O'Brien     return (FALSE);
765c80476e4SDavid E. O'Brien }
76629301572SMark Peek #endif	/* FILEC && TIOCSTI */
767