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