1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.file.c,v 3.35 2006/08/23 15:03:14 christos Exp $ */
3 * sh.file.c: File completion for csh. This file is not used in tcsh.
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$tcsh: sh.file.c,v 3.35 2006/08/23 15:03:14 christos Exp $")
38 #if defined(FILEC) && defined(TIOCSTI)
41 * Tenex style file name recognition, .. and more.
43 * Author: Ken Greer, Sept. 1975, CMU.
44 * Finally got around to adding to the Cshell., Ken Greer, Dec. 1981.
56 #define ESC CTL_ESC('\033')
62 static void setup_tty (int);
63 static void back_to_col_1 (void);
64 static void pushback (const Char
*);
65 static int filetype (const Char
*, const Char
*);
66 static void print_by_column (const Char
*, Char
*[], size_t);
67 static Char
*tilde (const Char
*);
68 static void retype (void);
69 static void beep (void);
70 static void print_recognized_stuff (const Char
*);
71 static void extract_dir_and_name (const Char
*, Char
**, const Char
**);
72 static Char
*getitem (DIR *, int);
73 static size_t tsearch (Char
*, COMMAND
, size_t);
74 static int compare (const void *, const void *);
75 static int recognize (Char
**, Char
*, size_t, size_t);
76 static int is_prefix (const Char
*, const Char
*);
77 static int is_suffix (const Char
*, const Char
*);
78 static int ignored (const Char
*);
82 * Put this here so the binary can be patched with adb to enable file
83 * completion by default. Filec controls completion, nobeep controls
84 * ringing the terminal bell on incomplete expansions.
93 struct termios tchars
;
99 (void) tcgetattr(SHIN
, &tchars
);
101 (void) ioctl(SHIN
, TCGETA
, (ioctl_t
) &tchars
);
104 tchars
.c_cc
[VEOL
] = ESC
;
105 if (tchars
.c_lflag
& ICANON
)
117 tchars
.c_lflag
|= ICANON
;
122 tchars
.c_cc
[VEOL
] = _POSIX_VDISABLE
;
130 (void) xtcsetattr(SHIN
, on
, &tchars
);
132 (void) ioctl(SHIN
, on
, (ioctl_t
) &tchars
);
136 static struct tchars tchars
;/* INT, QUIT, XON, XOFF, EOF, BRK */
139 (void) ioctl(SHIN
, TIOCGETC
, (ioctl_t
) & tchars
);
141 (void) ioctl(SHIN
, TIOCSETC
, (ioctl_t
) & tchars
);
143 * This must be done after every command: if the tty gets into raw or
144 * cbreak mode the user can't even type 'reset'.
146 (void) ioctl(SHIN
, TIOCGETP
, (ioctl_t
) & sgtty
);
147 if (sgtty
.sg_flags
& (RAW
| CBREAK
)) {
148 sgtty
.sg_flags
&= ~(RAW
| CBREAK
);
149 (void) ioctl(SHIN
, TIOCSETP
, (ioctl_t
) & sgtty
);
154 (void) ioctl(SHIN
, TIOCSETC
, (ioctl_t
) & tchars
);
160 * Move back to beginning of current line
167 struct termios tty
, tty_normal
;
169 struct termio tty
, tty_normal
;
172 struct sgttyb tty
, tty_normal
;
176 cleanup_push(&pintr_disabled
, disabled_cleanup
);
180 (void) tcgetattr(SHOUT
, &tty
);
182 (void) ioctl(SHOUT
, TCGETA
, (ioctl_t
) &tty_normal
);
185 tty
.c_iflag
&= ~INLCR
;
186 tty
.c_oflag
&= ~ONLCR
;
188 (void) xtcsetattr(SHOUT
, TCSANOW
, &tty
);
190 (void) ioctl(SHOUT
, TCSETAW
, (ioctl_t
) &tty
);
192 (void) xwrite(SHOUT
, "\r", 1);
194 (void) xtcsetattr(SHOUT
, TCSANOW
, &tty_normal
);
196 (void) ioctl(SHOUT
, TCSETAW
, (ioctl_t
) &tty_normal
);
199 (void) ioctl(SHIN
, TIOCGETP
, (ioctl_t
) & tty
);
201 tty
.sg_flags
&= ~CRMOD
;
202 (void) ioctl(SHIN
, TIOCSETN
, (ioctl_t
) & tty
);
203 (void) xwrite(SHOUT
, "\r", 1);
204 (void) ioctl(SHIN
, TIOCSETN
, (ioctl_t
) & tty_normal
);
207 cleanup_until(&pintr_disabled
);
211 * Push string contents back into tty queue
214 pushback(const Char
*string
)
219 struct termios tty
, tty_normal
;
221 struct termio tty
, tty_normal
;
224 struct sgttyb tty
, tty_normal
;
228 cleanup_push(&pintr_disabled
, disabled_cleanup
);
232 (void) tcgetattr(SHOUT
, &tty
);
234 (void) ioctl(SHOUT
, TCSETAW
, (ioctl_t
) &tty
);
237 tty
.c_lflag
&= ~(ECHOKE
| ECHO
| ECHOE
| ECHOK
| ECHONL
| ECHOPRT
| ECHOCTL
);
239 (void) xtcsetattr(SHOUT
, TCSANOW
, &tty
);
241 (void) ioctl(SHOUT
, TCSETAW
, (ioctl_t
) &tty
);
244 for (p
= string
; *p
!= '\0'; p
++) {
245 char buf
[MB_LEN_MAX
];
248 len
= one_wctomb(buf
, *p
& CHAR
);
249 for (i
= 0; i
< len
; i
++)
250 (void) ioctl(SHOUT
, TIOCSTI
, (ioctl_t
) &buf
[i
]);
253 (void) xtcsetattr(SHOUT
, TCSANOW
, &tty_normal
);
255 (void) ioctl(SHOUT
, TCSETAW
, (ioctl_t
) &tty_normal
);
258 (void) ioctl(SHOUT
, TIOCGETP
, (ioctl_t
) & tty
);
260 tty
.sg_flags
&= ~ECHO
;
261 (void) ioctl(SHOUT
, TIOCSETN
, (ioctl_t
) & tty
);
263 for (p
= string
; c
= *p
; p
++)
264 (void) ioctl(SHOUT
, TIOCSTI
, (ioctl_t
) & c
);
265 (void) ioctl(SHOUT
, TIOCSETN
, (ioctl_t
) & tty_normal
);
268 cleanup_until(&pintr_disabled
);
272 filetype(const Char
*dir
, const Char
*file
)
278 path
= Strspl(dir
, file
);
279 spath
= short2str(path
);
281 if (lstat(spath
, &statb
) == 0) {
282 switch (statb
.st_mode
& S_IFMT
) {
287 if (stat(spath
, &statb
) == 0 && /* follow it out */
288 S_ISDIR(statb
.st_mode
))
297 if (statb
.st_mode
& 0111)
305 * Print sorted down columns
308 print_by_column(const Char
*dir
, Char
*items
[], size_t count
)
312 int rows
, r
, c
, maxwidth
= 0, columns
;
314 if (ioctl(SHOUT
, TIOCGWINSZ
, (ioctl_t
) & win
) < 0 || win
.ws_col
== 0)
316 for (i
= 0; i
< count
; i
++)
317 maxwidth
= maxwidth
> (r
= Strlen(items
[i
])) ? maxwidth
: r
;
318 maxwidth
+= 2; /* for the file tag and space */
319 columns
= win
.ws_col
/ maxwidth
;
322 rows
= (count
+ (columns
- 1)) / columns
;
323 for (r
= 0; r
< rows
; r
++) {
324 for (c
= 0; c
< columns
; c
++) {
329 xprintf("%S", items
[i
]);
330 xputchar(dir
? filetype(dir
, items
[i
]) : ' ');
331 if (c
< columns
- 1) { /* last column? */
332 w
= Strlen(items
[i
]) + 1;
333 for (; w
< maxwidth
; w
++)
344 * Expand file name with possible tilde usage
347 * home_directory_of_person/mumble
350 tilde(const Char
*old
)
352 const Char
*o
, *home
;
356 return (Strsave(old
));
359 for (o
= old
; *o
!= '\0' && *o
!= '/'; o
++)
362 home
= varval(STRhome
);
366 person
= Strnsave(old
, o
- old
);
367 pw
= xgetpwnam(short2str(person
));
371 home
= str2short(pw
->pw_dir
);
373 return Strspl(home
, o
);
377 * Cause pending line to be printed
386 (void) tcgetattr(SHOUT
, &tty
);
390 (void) ioctl(SHOUT
, TCGETA
, (ioctl_t
) &tty
);
393 tty
.c_lflag
|= PENDIN
;
396 (void) xtcsetattr(SHOUT
, TCSANOW
, &tty
);
398 (void) ioctl(SHOUT
, TCSETAW
, (ioctl_t
) &tty
);
401 int pending_input
= LPENDIN
;
403 (void) ioctl(SHOUT
, TIOCLBIS
, (ioctl_t
) & pending_input
);
410 if (adrof(STRnobeep
) == 0)
412 (void) xwrite(SHOUT
, "\007", 1);
415 unsigned char beep_ch
= CTL_ESC('\007');
416 (void) xwrite(SHOUT
, &beep_ch
, 1);
422 * Erase that silly ^[ and
423 * print the recognized part of the string
426 print_recognized_stuff(const Char
*recognized_part
)
428 /* An optimized erasing of that silly ^[ */
431 switch (Strlen(recognized_part
)) {
433 case 0: /* erase two Characters: ^[ */
440 case 1: /* overstrike the ^, erase the [ */
441 xprintf("%S", recognized_part
);
446 default: /* overstrike both Characters ^[ */
447 xprintf("%S", recognized_part
);
454 * Parse full path in file into 2 parts: directory and file names
455 * Should leave final slash (/) at end of dir.
458 extract_dir_and_name(const Char
*path
, Char
**dir
, const Char
**name
)
462 p
= Strrchr(path
, '/');
468 *dir
= Strnsave(path
, p
- path
);
472 getitem(DIR *dir_fd
, int looking_for_lognames
)
477 if (looking_for_lognames
) {
478 #ifndef HAVE_GETPWENT
481 if ((pw
= getpwent()) == NULL
)
483 return (str2short(pw
->pw_name
));
484 #endif /* atp vmsposix */
486 if ((dirp
= readdir(dir_fd
)) != NULL
)
487 return (str2short(dirp
->d_name
));
492 * Perform a RECOGNIZE or LIST command on string "word".
495 tsearch(Char
*word
, COMMAND command
, size_t max_word_length
)
498 int ignoring
= TRUE
, nignored
= 0;
499 int looking_for_lognames
;
500 Char
*tilded_dir
= NULL
, *dir
= NULL
;
501 Char
*extended_name
= NULL
;
504 struct blk_buf items
= BLK_BUF_INIT
;
507 looking_for_lognames
= (*word
== '~') && (Strchr(word
, '/') == NULL
);
508 if (looking_for_lognames
) {
512 name
= word
+ 1; /* name sans ~ */
514 cleanup_push(dir
, xfree
);
517 extract_dir_and_name(word
, &dir
, &name
);
518 cleanup_push(dir
, xfree
);
519 tilded_dir
= tilde(dir
);
520 if (tilded_dir
== NULL
)
522 cleanup_push(tilded_dir
, xfree
);
523 dir_fd
= opendir(*tilded_dir
? short2str(tilded_dir
) : ".");
528 name_length
= Strlen(name
);
529 cleanup_push(&extended_name
, xfree_indirect
);
530 cleanup_push(&items
, bb_cleanup
);
531 again
: /* search for matches */
532 while ((item
= getitem(dir_fd
, looking_for_lognames
)) != NULL
) {
533 if (!is_prefix(name
, item
))
535 /* Don't match . files on null prefix match */
536 if (name_length
== 0 && item
[0] == '.' &&
537 !looking_for_lognames
)
540 bb_append(&items
, Strsave(item
));
541 else { /* RECOGNIZE command */
542 if (ignoring
&& ignored(item
))
544 else if (recognize(&extended_name
, item
, name_length
, ++items
.len
))
548 if (ignoring
&& items
.len
== 0 && nignored
> 0) {
551 if (looking_for_lognames
) {
554 #endif /* atp vmsposix */
560 if (looking_for_lognames
) {
561 #ifndef HAVE_GETPWENT
566 if (items
.len
!= 0) {
567 if (command
== RECOGNIZE
) {
568 if (looking_for_lognames
)
569 copyn(word
, STRtilde
, 2);/*FIXBUF, sort of */
571 /* put back dir part */
572 copyn(word
, dir
, max_word_length
);/*FIXBUF*/
573 /* add extended name */
574 catn(word
, extended_name
, max_word_length
);/*FIXBUF*/
577 qsort(items
.vec
, items
.len
, sizeof(items
.vec
[0]), compare
);
578 print_by_column(looking_for_lognames
? NULL
: tilded_dir
,
579 items
.vec
, items
.len
);
589 compare(const void *p
, const void *q
)
594 return (wcscoll(*(Char
*const *) p
, *(Char
*const *) q
));
599 p1
= strsave(short2str(*(Char
*const *) p
));
600 q1
= strsave(short2str(*(Char
*const *) q
));
601 # if defined(NLS) && defined(HAVE_STRCOLL)
602 res
= strcoll(p1
, q1
);
604 res
= strcmp(p1
, q1
);
605 # endif /* NLS && HAVE_STRCOLL */
609 #endif /* not WIDE_STRINGS */
613 * Object: extend what user typed up to an ambiguity.
615 * On first match, copy full item (assume it'll be the only match)
616 * On subsequent matches, shorten extended_name to the first
617 * Character mismatch between extended_name and item.
618 * If we shorten it back to the prefix length, stop searching.
621 recognize(Char
**extended_name
, Char
*item
, size_t name_length
,
624 if (numitems
== 1) /* 1st match */
625 *extended_name
= Strsave(item
);
626 else { /* 2nd & subsequent matches */
631 for (ent
= item
; *x
&& *x
== *ent
++; x
++, len
++);
632 *x
= '\0'; /* Shorten at 1st Char diff */
633 if (len
== name_length
) /* Ambiguous to prefix? */
634 return (-1); /* So stop now and save time */
640 * Return true if check matches initial Chars in template.
641 * This differs from PWB imatch in that if check is null
642 * it matches anything.
645 is_prefix(const Char
*check
, const Char
*template)
650 while (*check
++ == *template++);
655 * Return true if the Chars in template appear at the
656 * end of check, I.e., are it's suffix.
659 is_suffix(const Char
*check
, const Char
*template)
663 for (c
= check
; *c
++;);
664 for (t
= template; *t
++;);
668 if (c
== check
|| *--t
!= *--c
)
674 setup_tty_cleanup(void *dummy
)
681 tenex(Char
*inputline
, size_t inputline_size
)
685 char tinputline
[BUFSIZE
+ 1];/*FIXBUF*/
688 cleanup_push(&num_read
, setup_tty_cleanup
); /* num_read is only a marker */
690 while ((num_read
= xread(SHIN
, tinputline
, BUFSIZE
)) > 0) {/*FIXBUF*/
691 static const Char delims
[] = {' ', '\'', '"', '\t', ';', '&', '<',
692 '>', '(', ')', '|', '^', '%', '\0'};
693 Char
*str_end
, *word_start
, last_Char
, should_retype
;
697 tinputline
[num_read
] = 0;
698 Strcpy(inputline
, str2short(tinputline
));/*FIXBUF*/
699 num_read
= Strlen(inputline
);
700 last_Char
= CTL_ESC(ASC(inputline
[num_read
- 1]) & ASCII
);
702 if (last_Char
== '\n' || (size_t)num_read
== inputline_size
)
704 command
= (last_Char
== ESC
) ? RECOGNIZE
: LIST
;
707 str_end
= &inputline
[num_read
];
708 if (last_Char
== ESC
)
709 --str_end
; /* wipeout trailing cmd Char */
712 * Find LAST occurence of a delimiter in the inputline. The word start
713 * is one Character past it.
715 for (word_start
= str_end
; word_start
> inputline
; --word_start
)
716 if (Strchr(delims
, word_start
[-1]))
718 space_left
= inputline_size
- (word_start
- inputline
) - 1;
719 numitems
= tsearch(word_start
, command
, space_left
);
721 if (command
== RECOGNIZE
) {
722 /* print from str_end on */
723 print_recognized_stuff(str_end
);
724 if (numitems
!= 1) /* Beep = No match/ambiguous */
729 * Tabs in the input line cause trouble after a pushback. tty driver
730 * won't backspace over them because column positions are now
731 * incorrect. This is solved by retyping over current line.
733 should_retype
= FALSE
;
734 if (Strchr(inputline
, '\t')) { /* tab Char in input line? */
736 should_retype
= TRUE
;
738 if (command
== LIST
) /* Always retype after a LIST */
739 should_retype
= TRUE
;
741 printprompt(0, NULL
);
746 cleanup_until(&num_read
);
751 ignored(const Char
*item
)
756 if ((vp
= adrof(STRfignore
)) == NULL
|| (cp
= vp
->vec
) == NULL
)
758 for (; *cp
!= NULL
; cp
++)
759 if (is_suffix(item
, *cp
))
763 #endif /* FILEC && TIOCSTI */