2
* sh.file.c: File completion for csh. This file is not used in tcsh.
5
* Copyright (c) 1980, 1991 The Regents of the University of California.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. All advertising materials mentioning features or use of this software
17
* must display the following acknowledgement:
18
* This product includes software developed by the University of
19
* California, Berkeley and its contributors.
20
* 4. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43
* Tenex style file name recognition, .. and more.
45
* Author: Ken Greer, Sept. 1975, CMU.
46
* Finally got around to adding to the Cshell., Ken Greer, Dec. 1981.
58
#define ESC CTL_ESC('\033')
64
static void setup_tty __P((int));
65
static void back_to_col_1 __P((void));
66
static void pushback __P((Char *));
67
static void catn __P((Char *, Char *, int));
68
static void copyn __P((Char *, Char *, int));
69
static Char filetype __P((Char *, Char *));
70
static void print_by_column __P((Char *, Char *[], int));
71
static Char *tilde __P((Char *, Char *));
72
static void retype __P((void));
73
static void beep __P((void));
74
static void print_recognized_stuff __P((Char *));
75
static void extract_dir_and_name __P((Char *, Char *, Char *));
76
static Char *getitem __P((DIR *, int));
77
static void free_items __P((Char **));
78
static int tsearch __P((Char *, COMMAND, int));
79
static int recognize __P((Char *, Char *, int, int));
80
static int is_prefix __P((Char *, Char *));
81
static int is_suffix __P((Char *, Char *));
82
static int ignored __P((Char *));
86
* Put this here so the binary can be patched with adb to enable file
87
* completion by default. Filec controls completion, nobeep controls
88
* ringing the terminal bell on incomplete expansions.
98
struct termios tchars;
100
struct termio tchars;
104
(void) tcgetattr(SHIN, &tchars);
106
(void) ioctl(SHIN, TCGETA, (ioctl_t) &tchars);
109
tchars.c_cc[VEOL] = ESC;
110
if (tchars.c_lflag & ICANON)
122
tchars.c_lflag |= ICANON;
127
tchars.c_cc[VEOL] = _POSIX_VDISABLE;
135
(void) tcsetattr(SHIN, on, &tchars);
137
(void) ioctl(SHIN, on, (ioctl_t) &tchars);
141
static struct tchars tchars;/* INT, QUIT, XON, XOFF, EOF, BRK */
144
(void) ioctl(SHIN, TIOCGETC, (ioctl_t) & tchars);
146
(void) ioctl(SHIN, TIOCSETC, (ioctl_t) & tchars);
148
* This must be done after every command: if the tty gets into raw or
149
* cbreak mode the user can't even type 'reset'.
151
(void) ioctl(SHIN, TIOCGETP, (ioctl_t) & sgtty);
152
if (sgtty.sg_flags & (RAW | CBREAK)) {
153
sgtty.sg_flags &= ~(RAW | CBREAK);
154
(void) ioctl(SHIN, TIOCSETP, (ioctl_t) & sgtty);
159
(void) ioctl(SHIN, TIOCSETC, (ioctl_t) & tchars);
165
* Move back to beginning of current line
172
struct termios tty, tty_normal;
174
struct termio tty, tty_normal;
177
struct sgttyb tty, tty_normal;
181
sigmask_t omask = sigblock(sigmask(SIGINT));
183
(void) sighold(SIGINT);
184
# endif /* BSDSIGS */
188
(void) tcgetattr(SHOUT, &tty);
190
(void) ioctl(SHOUT, TCGETA, (ioctl_t) &tty_normal);
193
tty.c_iflag &= ~INLCR;
194
tty.c_oflag &= ~ONLCR;
196
(void) tcsetattr(SHOUT, TCSANOW, &tty);
198
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
200
(void) write(SHOUT, "\r", 1);
202
(void) tcsetattr(SHOUT, TCSANOW, &tty_normal);
204
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty_normal);
207
(void) ioctl(SHIN, TIOCGETP, (ioctl_t) & tty);
209
tty.sg_flags &= ~CRMOD;
210
(void) ioctl(SHIN, TIOCSETN, (ioctl_t) & tty);
211
(void) write(SHOUT, "\r", 1);
212
(void) ioctl(SHIN, TIOCSETN, (ioctl_t) & tty_normal);
216
(void) sigsetmask(omask);
218
(void) sigrelse(SIGINT);
219
# endif /* BSDISGS */
223
* Push string contents back into tty queue
233
struct termios tty, tty_normal;
235
struct termio tty, tty_normal;
238
struct sgttyb tty, tty_normal;
242
sigmask_t omask = sigblock(sigmask(SIGINT));
244
(void) sighold(SIGINT);
249
(void) tcgetattr(SHOUT, &tty);
251
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
255
tty.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ECHOCTL);
257
tty.c_lflag &= ~(ECHOKE | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOCTL);
260
(void) tcsetattr(SHOUT, TCSANOW, &tty);
262
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
265
for (p = string; c = *p; p++)
266
(void) ioctl(SHOUT, TIOCSTI, (ioctl_t) & c);
268
(void) tcsetattr(SHOUT, TCSANOW, &tty_normal);
270
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty_normal);
272
(void) sigsetmask(omask);
274
(void) ioctl(SHOUT, TIOCGETP, (ioctl_t) & tty);
276
tty.sg_flags &= ~ECHO;
277
(void) ioctl(SHOUT, TIOCSETN, (ioctl_t) & tty);
279
for (p = string; c = *p; p++)
280
(void) ioctl(SHOUT, TIOCSTI, (ioctl_t) & c);
281
(void) ioctl(SHOUT, TIOCSETN, (ioctl_t) & tty_normal);
285
(void) sigsetmask(omask);
287
(void) sigrelse(SIGINT);
288
# endif /* BSDISGS */
292
* Concatenate src onto tail of des.
293
* Des is a string whose maximum length is count.
294
* Always null terminate.
297
catn(des, src, count)
298
register Char *des, *src;
301
while (--count >= 0 && *des)
304
if ((*des++ = *src++) == 0)
310
* Like strncpy but always leave room for trailing \0
311
* and always null terminate.
314
copyn(des, src, count)
315
register Char *des, *src;
319
if ((*des++ = *src++) == 0)
328
Char path[MAXPATHLEN];
331
catn(Strcpy(path, dir), file, sizeof(path) / sizeof(Char));
332
if (lstat(short2str(path), &statb) == 0) {
333
switch (statb.st_mode & S_IFMT) {
338
if (stat(short2str(path), &statb) == 0 && /* follow it out */
339
S_ISDIR(statb.st_mode))
348
if (statb.st_mode & 0111)
355
static struct winsize win;
358
* Print sorted down columns
361
print_by_column(dir, items, count)
365
register int i, rows, r, c, maxwidth = 0, columns;
367
if (ioctl(SHOUT, TIOCGWINSZ, (ioctl_t) & win) < 0 || win.ws_col == 0)
369
for (i = 0; i < count; i++)
370
maxwidth = maxwidth > (r = Strlen(items[i])) ? maxwidth : r;
371
maxwidth += 2; /* for the file tag and space */
372
columns = win.ws_col / maxwidth;
375
rows = (count + (columns - 1)) / columns;
376
for (r = 0; r < rows; r++) {
377
for (c = 0; c < columns; c++) {
382
xprintf("%S", items[i]);
383
xputchar(dir ? filetype(dir, items[i]) : ' ');
384
if (c < columns - 1) { /* last column? */
385
w = Strlen(items[i]) + 1;
386
for (; w < maxwidth; w++)
397
* Expand file name with possible tilde usage
400
* home_directory_of_person/mumble
406
register Char *o, *p;
407
register struct passwd *pw;
408
static Char person[40];
411
return (Strcpy(new, old));
413
for (p = person, o = &old[1]; *o && *o != '/'; *p++ = *o++);
415
if (person[0] == '\0')
416
(void) Strcpy(new, varval(STRhome));
418
pw = getpwnam(short2str(person));
421
(void) Strcpy(new, str2short(pw->pw_dir));
423
(void) Strcat(new, o);
428
* Cause pending line to be printed
437
(void) tcgetattr(SHOUT, &tty);
441
(void) ioctl(SHOUT, TCGETA, (ioctl_t) &tty);
445
tty.c_lflag |= PENDIN;
449
(void) tcsetattr(SHOUT, TCSANOW, &tty);
451
(void) ioctl(SHOUT, TCSETAW, (ioctl_t) &tty);
454
int pending_input = LPENDIN;
456
(void) ioctl(SHOUT, TIOCLBIS, (ioctl_t) & pending_input);
463
if (adrof(STRnobeep) == 0)
465
(void) write(SHOUT, "\007", 1);
468
unsigned char beep_ch = CTL_ESC('\007');
469
(void) write(SHOUT, &beep_ch, 1);
471
#endif /*_OSD_POSIX*/
475
* Erase that silly ^[ and
476
* print the recognized part of the string
479
print_recognized_stuff(recognized_part)
480
Char *recognized_part;
482
/* An optimized erasing of that silly ^[ */
485
switch (Strlen(recognized_part)) {
487
case 0: /* erase two Characters: ^[ */
494
case 1: /* overstrike the ^, erase the [ */
495
xprintf("%S", recognized_part);
500
default: /* overstrike both Characters ^[ */
501
xprintf("%S", recognized_part);
508
* Parse full path in file into 2 parts: directory and file names
509
* Should leave final slash (/) at end of dir.
512
extract_dir_and_name(path, dir, name)
513
Char *path, *dir, *name;
517
p = Strrchr(path, '/');
519
copyn(name, path, MAXNAMLEN);
523
copyn(name, ++p, MAXNAMLEN);
524
copyn(dir, path, p - path);
527
/* atp vmsposix - I need to remove all the setpwent
528
* getpwent endpwent stuff. VMS_POSIX has getpwnam getpwuid
529
* and getlogin. This needs fixing. (There is no access to
530
* pw->passwd in VMS - a secure system benefit :-| )
533
getitem(dir_fd, looking_for_lognames)
535
int looking_for_lognames;
537
register struct passwd *pw;
538
register struct dirent *dirp;
540
if (looking_for_lognames) {
544
if ((pw = getpwent()) == NULL)
546
return (str2short(pw->pw_name));
547
#endif /* atp vmsposix */
549
if (dirp = readdir(dir_fd))
550
return (str2short(dirp->d_name));
556
register Char **items;
560
for (i = 0; items[i]; i++)
561
xfree((ptr_t) items[i]);
562
xfree((ptr_t) items);
566
# define FREE_ITEMS(items) { \
569
omask = sigblock(sigmask(SIGINT));\
572
(void) sigsetmask(omask);\
575
# define FREE_ITEMS(items) { \
576
(void) sighold(SIGINT);\
579
(void) sigrelse(SIGINT);\
584
* Perform a RECOGNIZE or LIST command on string "word".
587
tsearch(word, command, max_word_length)
592
static Char **items = NULL;
593
register DIR *dir_fd;
594
register numitems = 0, ignoring = TRUE, nignored = 0;
595
register name_length, looking_for_lognames;
596
Char tilded_dir[MAXPATHLEN + 1], dir[MAXPATHLEN + 1];
597
Char name[MAXNAMLEN + 1], extended_name[MAXNAMLEN + 1];
600
#define MAXITEMS 1024
605
looking_for_lognames = (*word == '~') && (Strchr(word, '/') == NULL);
606
if (looking_for_lognames) {
609
#endif /*atp vmsposix */
610
copyn(name, &word[1], MAXNAMLEN); /* name sans ~ */
614
extract_dir_and_name(word, dir, name);
615
if (tilde(tilded_dir, dir) == 0)
617
dir_fd = opendir(*tilded_dir ? short2str(tilded_dir) : ".");
622
again: /* search for matches */
623
name_length = Strlen(name);
624
for (numitems = 0; item = getitem(dir_fd, looking_for_lognames);) {
625
if (!is_prefix(name, item))
627
/* Don't match . files on null prefix match */
628
if (name_length == 0 && item[0] == '.' &&
629
!looking_for_lognames)
631
if (command == LIST) {
632
if (numitems >= MAXITEMS) {
633
xprintf(CGETS(14, 1, "\nYikes!! Too many %s!!\n"),
634
looking_for_lognames ?
635
CGETS(14, 2, "names in password file") :
636
CGETS(14, 3, "files");
640
* From Beto Appleton (beto@aixwiz.austin.ibm.com)
641
* typing "./control-d" will cause the csh to core-dump.
642
* the problem can be reproduce as following:
645
* 3. create a directory with 1050 files
646
* 4. typing "./control-d" will cause the csh to core-dump
647
* Solution: Add + 1 to MAXITEMS
650
items = (Char **) xcalloc(sizeof(items[0]), MAXITEMS + 1);
651
items[numitems] = (Char *) xmalloc((size_t) (Strlen(item) + 1) *
653
copyn(items[numitems], item, MAXNAMLEN);
656
else { /* RECOGNIZE command */
657
if (ignoring && ignored(item))
659
else if (recognize(extended_name,
660
item, name_length, ++numitems))
664
if (ignoring && numitems == 0 && nignored > 0) {
667
if (looking_for_lognames)
670
#endif /* atp vmsposix */
676
if (looking_for_lognames)
679
#endif /*atp vmsposix */
681
(void) closedir(dir_fd);
684
if (command == RECOGNIZE) {
685
if (looking_for_lognames)
686
copyn(word, STRtilde, 1);
688
/* put back dir part */
689
copyn(word, dir, max_word_length);
690
/* add extended name */
691
catn(word, extended_name, max_word_length);
695
qsort((ptr_t) items, (size_t) numitems, sizeof(items[0]), sortscmp);
696
print_by_column(looking_for_lognames ? NULL : tilded_dir,
705
* Object: extend what user typed up to an ambiguity.
707
* On first match, copy full item (assume it'll be the only match)
708
* On subsequent matches, shorten extended_name to the first
709
* Character mismatch between extended_name and item.
710
* If we shorten it back to the prefix length, stop searching.
713
recognize(extended_name, item, name_length, numitems)
714
Char *extended_name, *item;
715
int name_length, numitems;
717
if (numitems == 1) /* 1st match */
718
copyn(extended_name, item, MAXNAMLEN);
719
else { /* 2nd & subsequent matches */
720
register Char *x, *ent;
721
register int len = 0;
724
for (ent = item; *x && *x == *ent++; x++, len++);
725
*x = '\0'; /* Shorten at 1st Char diff */
726
if (len == name_length) /* Ambiguous to prefix? */
727
return (-1); /* So stop now and save time */
733
* Return true if check matches initial Chars in template.
734
* This differs from PWB imatch in that if check is null
735
* it matches anything.
738
is_prefix(check, template)
739
register Char *check, *template;
744
while (*check++ == *template++);
749
* Return true if the Chars in template appear at the
750
* end of check, I.e., are it's suffix.
753
is_suffix(check, template)
754
Char *check, *template;
756
register Char *c, *t;
758
for (c = check; *c++;);
759
for (t = template; *t++;);
763
if (c == check || *--t != *--c)
769
tenex(inputline, inputline_size)
773
register int numitems, num_read;
774
char tinputline[BUFSIZE];
779
while ((num_read = read(SHIN, tinputline, BUFSIZE)) > 0) {
781
static Char delims[] = {' ', '\'', '"', '\t', ';', '&', '<',
782
'>', '(', ')', '|', '^', '%', '\0'};
783
register Char *str_end, *word_start, last_Char, should_retype;
784
register int space_left;
787
for (i = 0; i < num_read; i++)
788
inputline[i] = (unsigned char) tinputline[i];
789
last_Char = inputline[num_read - 1] & ASCII;
791
if (last_Char == '\n' || num_read == inputline_size)
793
command = (last_Char == ESC) ? RECOGNIZE : LIST;
796
str_end = &inputline[num_read];
797
if (last_Char == ESC)
798
--str_end; /* wipeout trailing cmd Char */
801
* Find LAST occurence of a delimiter in the inputline. The word start
802
* is one Character past it.
804
for (word_start = str_end; word_start > inputline; --word_start)
805
if (Strchr(delims, word_start[-1]))
807
space_left = inputline_size - (word_start - inputline) - 1;
808
numitems = tsearch(word_start, command, space_left);
810
if (command == RECOGNIZE) {
811
/* print from str_end on */
812
print_recognized_stuff(str_end);
813
if (numitems != 1) /* Beep = No match/ambiguous */
818
* Tabs in the input line cause trouble after a pushback. tty driver
819
* won't backspace over them because column positions are now
820
* incorrect. This is solved by retyping over current line.
822
should_retype = FALSE;
823
if (Strchr(inputline, '\t')) { /* tab Char in input line? */
825
should_retype = TRUE;
827
if (command == LIST) /* Always retype after a LIST */
828
should_retype = TRUE;
830
printprompt(0, NULL);
846
if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL)
848
for (; *cp != NULL; cp++)
849
if (is_suffix(item, *cp))