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