1
/* complete.c -- filename completion for readline. */
3
/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
This file is part of the GNU Readline Library, a library for
6
reading lines of text with interactive input and history editing.
8
The GNU Readline Library is free software; you can redistribute it
9
and/or modify it under the terms of the GNU General Public License
10
as published by the Free Software Foundation; either version 1, or
11
(at your option) any later version.
13
The GNU Readline Library is distributed in the hope that it will be
14
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
The GNU General Public License is often shipped with GNU software, and
19
is generally kept in a file called COPYING or LICENSE. If you do not
20
have a copy of the license, write to the Free Software Foundation,
21
675 Mass Ave, Cambridge, MA 02139, USA. */
22
#define READLINE_LIBRARY
24
#if defined (HAVE_CONFIG_H)
28
#include <sys/types.h>
30
#if defined (HAVE_SYS_FILE_H)
34
#if defined (HAVE_UNISTD_H)
36
#endif /* HAVE_UNISTD_H */
38
#if defined (HAVE_STDLIB_H)
41
# include "ansi_stdlib.h"
42
#endif /* HAVE_STDLIB_H */
52
#if !defined (HAVE_GETPW_DECLS)
53
extern struct passwd *getpwent ();
54
#endif /* USG && !HAVE_GETPW_DECLS */
56
/* ISC systems don't define getpwent() if _POSIX_SOURCE is defined. */
57
#if defined (isc386) && defined (_POSIX_SOURCE)
58
# if defined (__STDC__)
59
extern struct passwd *getpwent (void);
61
extern struct passwd *getpwent ();
62
# endif /* !__STDC__ */
63
#endif /* isc386 && _POSIX_SOURCE */
66
#include "posixstat.h"
68
/* System-specific feature definitions and include files. */
71
/* Some standard library routines. */
74
extern char *tilde_expand ();
75
extern char *rl_copy_text ();
76
extern void _rl_abort_internal ();
77
extern int _rl_qsort_string_compare ();
78
extern void _rl_replace_text ();
80
extern Function *rl_last_func;
81
extern int rl_editing_mode;
82
extern int screenwidth;
84
extern void _rl_move_vert ();
85
extern int _rl_vis_botlin;
86
extern int rl_display_fixed;
88
/* If non-zero, then this is the address of a function to call when
89
completing a word would normally display the list of possible matches.
90
This function is called instead of actually doing the display.
91
It takes three arguments: (char **matches, int num_matches, int max_length)
92
where MATCHES is the array of strings that matched, NUM_MATCHES is the
93
number of strings in that array, and MAX_LENGTH is the length of the
94
longest string in that array. */
95
VFunction *rl_completion_display_matches_hook = (VFunction *)NULL;
97
/* Forward declarations for functions defined and used in this file. */
98
char *filename_completion_function ();
99
char **completion_matches ();
101
#if defined (VISIBLE_STATS)
105
static int stat_char ();
108
static char *rl_quote_filename ();
109
static char *rl_strpbrk ();
111
static char **remove_duplicate_matches ();
112
static void insert_match ();
113
static int append_to_match ();
114
static void insert_all_matches ();
115
static void display_matches ();
116
static int compute_lcd_of_matches ();
118
extern char *xmalloc (), *xrealloc ();
120
/* **************************************************************** */
122
/* Completion matching, from readline's point of view. */
124
/* **************************************************************** */
126
/* Variables known only to the readline library. */
128
/* If non-zero, non-unique completions always show the list of matches. */
129
int _rl_complete_show_all = 0;
131
/* If non-zero, completed directory names have a slash appended. */
132
int _rl_complete_mark_directories = 1;
134
/* If non-zero, completions are printed horizontally in alphabetical order,
136
int _rl_print_completions_horizontally;
138
/* Non-zero means that case is not significant in filename completion. */
139
int _rl_completion_case_fold;
141
/* Global variables available to applications using readline. */
143
#if defined (VISIBLE_STATS)
144
/* Non-zero means add an additional character to each filename displayed
145
during listing completion iff rl_filename_completion_desired which helps
146
to indicate the type of file being listed. */
147
int rl_visible_stats = 0;
148
#endif /* VISIBLE_STATS */
150
/* If non-zero, then this is the address of a function to call when
151
completing on a directory name. The function is called with
152
the address of a string (the current directory name) as an arg. */
153
Function *rl_directory_completion_hook = (Function *)NULL;
155
/* Non-zero means readline completion functions perform tilde expansion. */
156
int rl_complete_with_tilde_expansion = 0;
158
/* Pointer to the generator function for completion_matches ().
159
NULL means to use filename_completion_function (), the default filename
161
Function *rl_completion_entry_function = (Function *)NULL;
163
/* Pointer to alternative function to create matches.
164
Function is called with TEXT, START, and END.
165
START and END are indices in RL_LINE_BUFFER saying what the boundaries
167
If this function exists and returns NULL then call the value of
168
rl_completion_entry_function to try to match, otherwise use the
169
array of strings returned. */
170
CPPFunction *rl_attempted_completion_function = (CPPFunction *)NULL;
172
/* Non-zero means to suppress normal filename completion after the
173
user-specified completion function has been called. */
174
int rl_attempted_completion_over = 0;
176
/* Set to a character indicating the type of completion being performed
177
by rl_complete_internal, available for use by application completion
179
int rl_completion_type = 0;
181
/* Up to this many items will be displayed in response to a
182
possible-completions call. After that, we ask the user if
183
she is sure she wants to see them all. */
184
int rl_completion_query_items = 100;
186
/* The basic list of characters that signal a break between words for the
187
completer routine. The contents of this variable is what breaks words
188
in the shell, i.e. " \t\n\"\\'`@$><=" */
189
char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
191
/* List of basic quoting characters. */
192
char *rl_basic_quote_characters = "\"'";
194
/* The list of characters that signal a break between words for
195
rl_complete_internal. The default list is the contents of
196
rl_basic_word_break_characters. */
197
char *rl_completer_word_break_characters = (char *)NULL;
199
/* List of characters which can be used to quote a substring of the line.
200
Completion occurs on the entire substring, and within the substring
201
rl_completer_word_break_characters are treated as any other character,
202
unless they also appear within this list. */
203
char *rl_completer_quote_characters = (char *)NULL;
205
/* List of characters that should be quoted in filenames by the completer. */
206
char *rl_filename_quote_characters = (char *)NULL;
208
/* List of characters that are word break characters, but should be left
209
in TEXT when it is passed to the completion function. The shell uses
210
this to help determine what kind of completing to do. */
211
char *rl_special_prefixes = (char *)NULL;
213
/* If non-zero, then disallow duplicates in the matches. */
214
int rl_ignore_completion_duplicates = 1;
216
/* Non-zero means that the results of the matches are to be treated
217
as filenames. This is ALWAYS zero on entry, and can only be changed
218
within a completion entry finder function. */
219
int rl_filename_completion_desired = 0;
221
/* Non-zero means that the results of the matches are to be quoted using
222
double quotes (or an application-specific quoting mechanism) if the
223
filename contains any characters in rl_filename_quote_chars. This is
224
ALWAYS non-zero on entry, and can only be changed within a completion
225
entry finder function. */
226
int rl_filename_quoting_desired = 1;
228
/* This function, if defined, is called by the completer when real
229
filename completion is done, after all the matching names have been
230
generated. It is passed a (char**) known as matches in the code below.
231
It consists of a NULL-terminated array of pointers to potential
232
matching strings. The 1st element (matches[0]) is the maximal
233
substring that is common to all matches. This function can re-arrange
234
the list of matches as required, but all elements of the array must be
235
free()'d if they are deleted. The main intent of this function is
236
to implement FIGNORE a la SunOS csh. */
237
Function *rl_ignore_some_completions_function = (Function *)NULL;
239
/* Set to a function to quote a filename in an application-specific fashion.
240
Called with the text to quote, the type of match found (single or multiple)
241
and a pointer to the quoting character to be used, which the function can
243
CPFunction *rl_filename_quoting_function = rl_quote_filename;
245
/* Function to call to remove quoting characters from a filename. Called
246
before completion is attempted, so the embedded quotes do not interfere
247
with matching names in the file system. Readline doesn't do anything
248
with this; it's set only by applications. */
249
CPFunction *rl_filename_dequoting_function = (CPFunction *)NULL;
251
/* Function to call to decide whether or not a word break character is
252
quoted. If a character is quoted, it does not break words for the
254
Function *rl_char_is_quoted_p = (Function *)NULL;
256
/* Character appended to completed words when at the end of the line. The
257
default is a space. */
258
int rl_completion_append_character = ' ';
260
/* If non-zero, inhibit completion (temporarily). */
261
int rl_inhibit_completion;
263
/* Variables local to this file. */
265
/* Local variable states what happened during the last completion attempt. */
266
static int completion_changed_buffer;
268
/*************************************/
270
/* Bindable completion functions */
272
/*************************************/
274
/* Complete the word at or before point. You have supplied the function
275
that does the initial simple matching selection algorithm (see
276
completion_matches ()). The default is to do filename completion. */
278
rl_complete (ignore, invoking_key)
279
int ignore, invoking_key;
281
if (rl_inhibit_completion)
282
return (rl_insert (ignore, invoking_key));
283
else if (rl_last_func == rl_complete && !completion_changed_buffer)
284
return (rl_complete_internal ('?'));
285
else if (_rl_complete_show_all)
286
return (rl_complete_internal ('!'));
288
return (rl_complete_internal (TAB));
291
/* List the possible completions. See description of rl_complete (). */
293
rl_possible_completions (ignore, invoking_key)
294
int ignore, invoking_key;
296
return (rl_complete_internal ('?'));
300
rl_insert_completions (ignore, invoking_key)
301
int ignore, invoking_key;
303
return (rl_complete_internal ('*'));
306
/************************************/
308
/* Completion utility functions */
310
/************************************/
312
/* Find the first occurrence in STRING1 of any character from STRING2.
313
Return a pointer to the character in STRING1. */
315
rl_strpbrk (string1, string2)
316
char *string1, *string2;
320
for (; *string1; string1++)
322
for (scan = string2; *scan; scan++)
324
if (*string1 == *scan)
330
return ((char *)NULL);
333
/* The user must press "y" or "n". Non-zero return means "y" pressed. */
342
if (c == 'y' || c == 'Y' || c == ' ')
344
if (c == 'n' || c == 'N' || c == RUBOUT)
347
_rl_abort_internal ();
352
#if defined (VISIBLE_STATS)
353
/* Return the character which best describes FILENAME.
354
`@' for symbolic links
359
`%' for character special devices
360
`#' for block special devices */
368
#if defined (HAVE_LSTAT) && defined (S_ISLNK)
369
r = lstat (filename, &finfo);
371
r = stat (filename, &finfo);
378
if (S_ISDIR (finfo.st_mode))
380
#if defined (S_ISCHR)
381
else if (S_ISCHR (finfo.st_mode))
384
#if defined (S_ISBLK)
385
else if (S_ISBLK (finfo.st_mode))
388
#if defined (S_ISLNK)
389
else if (S_ISLNK (finfo.st_mode))
392
#if defined (S_ISSOCK)
393
else if (S_ISSOCK (finfo.st_mode))
395
#endif /* S_ISSOCK */
396
#if defined (S_ISFIFO)
397
else if (S_ISFIFO (finfo.st_mode))
400
else if (S_ISREG (finfo.st_mode))
402
if (access (filename, X_OK) == 0)
407
#endif /* VISIBLE_STATS */
409
/* Return the portion of PATHNAME that should be output when listing
410
possible completions. If we are hacking filename completion, we
411
are only interested in the basename, the portion following the
412
final slash. Otherwise, we return what we were passed. */
414
printable_part (pathname)
419
temp = rl_filename_completion_desired ? strrchr (pathname, '/') : (char *)NULL;
420
return (temp ? ++temp : pathname);
423
/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
424
are using it, check for and output a single character for `special'
425
filenames. Return the number of characters we output. */
431
putc ('^', rl_outstream); \
432
putc (UNCTRL (c), rl_outstream); \
435
else if (c == RUBOUT) \
437
putc ('^', rl_outstream); \
438
putc ('?', rl_outstream); \
443
putc (c, rl_outstream); \
449
print_filename (to_print, full_pathname)
450
char *to_print, *full_pathname;
453
#if !defined (VISIBLE_STATS)
456
for (s = to_print; *s; s++)
461
char *s, c, *new_full_pathname;
462
int extension_char, slen, tlen;
464
for (s = to_print; *s; s++)
469
if (rl_filename_completion_desired && rl_visible_stats)
471
/* If to_print != full_pathname, to_print is the basename of the
472
path passed. In this case, we try to expand the directory
473
name before checking for the stat character. */
474
if (to_print != full_pathname)
476
/* Terminate the directory name. */
480
s = tilde_expand (full_pathname);
481
if (rl_directory_completion_hook)
482
(*rl_directory_completion_hook) (&s);
485
tlen = strlen (to_print);
486
new_full_pathname = xmalloc (slen + tlen + 2);
487
strcpy (new_full_pathname, s);
488
new_full_pathname[slen] = '/';
489
strcpy (new_full_pathname + slen + 1, to_print);
491
extension_char = stat_char (new_full_pathname);
493
free (new_full_pathname);
498
s = tilde_expand (full_pathname);
499
extension_char = stat_char (s);
505
putc (extension_char, rl_outstream);
509
#endif /* VISIBLE_STATS */
514
rl_quote_filename (s, rtype, qcp)
521
r = xmalloc (strlen (s) + 2);
522
*r = *rl_completer_quote_characters;
525
*qcp = *rl_completer_quote_characters;
529
/* Find the bounds of the current word for completion purposes, and leave
530
rl_point set to the end of the word. This function skips quoted
531
substrings (characters between matched pairs of characters in
532
rl_completer_quote_characters. First we try to find an unclosed
533
quoted substring on which to do matching. If one is not found, we use
534
the word break characters to find the boundaries of the current word.
535
We call an application-specific function to decide whether or not a
536
particular word break character is quoted; if that function returns a
537
non-zero result, the character does not break a word. This function
538
returns the opening quote character if we found an unclosed quoted
539
substring, '\0' otherwise. FP, if non-null, is set to a value saying
540
which (shell-like) quote characters we found (single quote, double
541
quote, or backslash) anywhere in the string. DP, if non-null, is set to
542
the value of the delimiter character that caused a word break. */
545
find_completion_word (fp, dp)
548
int scan, end, found_quote, delimiter, pass_next, isbrk;
552
found_quote = delimiter = 0;
555
if (rl_completer_quote_characters)
557
/* We have a list of characters which can be used in pairs to
558
quote substrings for the completer. Try to find the start
559
of an unclosed quoted substring. */
560
/* FOUND_QUOTE is set so we know what kind of quotes we found. */
561
for (scan = pass_next = 0; scan < end; scan++)
569
if (rl_line_buffer[scan] == '\\')
572
found_quote |= RL_QF_BACKSLASH;
576
if (quote_char != '\0')
578
/* Ignore everything until the matching close quote char. */
579
if (rl_line_buffer[scan] == quote_char)
581
/* Found matching close. Abandon this substring. */
586
else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
588
/* Found start of a quoted substring. */
589
quote_char = rl_line_buffer[scan];
591
/* Shell-like quoting conventions. */
592
if (quote_char == '\'')
593
found_quote |= RL_QF_SINGLE_QUOTE;
594
else if (quote_char == '"')
595
found_quote |= RL_QF_DOUBLE_QUOTE;
600
if (rl_point == end && quote_char == '\0')
602
/* We didn't find an unclosed quoted substring upon which to do
603
completion, so use the word break characters to find the
604
substring on which to complete. */
607
scan = rl_line_buffer[rl_point];
609
if (strchr (rl_completer_word_break_characters, scan) == 0)
612
/* Call the application-specific function to tell us whether
613
this word break character is quoted and should be skipped. */
614
if (rl_char_is_quoted_p && found_quote &&
615
(*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
618
/* Convoluted code, but it avoids an n^2 algorithm with calls
619
to char_is_quoted. */
624
/* If we are at an unquoted word break, then advance past it. */
625
scan = rl_line_buffer[rl_point];
627
/* If there is an application-specific function to say whether or not
628
a character is quoted and we found a quote character, let that
629
function decide whether or not a character is a word break, even
630
if it is found in rl_completer_word_break_characters. */
631
if (rl_char_is_quoted_p)
632
isbrk = (found_quote == 0 ||
633
(*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
634
strchr (rl_completer_word_break_characters, scan) != 0;
636
isbrk = strchr (rl_completer_word_break_characters, scan) != 0;
640
/* If the character that caused the word break was a quoting
641
character, then remember it as the delimiter. */
642
if (rl_basic_quote_characters && strchr (rl_basic_quote_characters, scan) && (end - rl_point) > 1)
645
/* If the character isn't needed to determine something special
646
about what kind of completion to perform, then advance past it. */
647
if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0)
660
gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
664
int found_quote, quote_char;
666
char **matches, *temp;
668
/* If the user wants to TRY to complete, but then wants to give
669
up and use the default completion function, they set the
670
variable rl_attempted_completion_function. */
671
if (rl_attempted_completion_function)
673
matches = (*rl_attempted_completion_function) (text, start, end);
675
if (matches || rl_attempted_completion_over)
677
rl_attempted_completion_over = 0;
682
/* Beware -- we're stripping the quotes here. Do this only if we know
683
we are doing filename completion and the application has defined a
684
filename dequoting function. */
687
if (found_quote && our_func == (Function *)filename_completion_function &&
688
rl_filename_dequoting_function)
690
/* delete single and double quotes */
691
temp = (*rl_filename_dequoting_function) (text, quote_char);
692
text = temp; /* not freeing text is not a memory leak */
695
matches = completion_matches (text, (CPFunction *)our_func);
700
/* Filter out duplicates in MATCHES. This frees up the strings in
703
remove_duplicate_matches (matches)
711
/* Sort the items. */
712
for (i = 0; matches[i]; i++)
715
/* Sort the array without matches[0], since we need it to
716
stay in place no matter what. */
718
qsort (matches+1, i-1, sizeof (char *), _rl_qsort_string_compare);
720
/* Remember the lowest common denominator for it may be unique. */
721
lowest_common = savestring (matches[0]);
723
for (i = newlen = 0; matches[i + 1]; i++)
725
if (strcmp (matches[i], matches[i + 1]) == 0)
728
matches[i] = (char *)&dead_slot;
734
/* We have marked all the dead slots with (char *)&dead_slot.
735
Copy all the non-dead entries into a new array. */
736
temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
737
for (i = j = 1; matches[i]; i++)
739
if (matches[i] != (char *)&dead_slot)
740
temp_array[j++] = matches[i];
742
temp_array[j] = (char *)NULL;
744
if (matches[0] != (char *)&dead_slot)
747
/* Place the lowest common denominator back in [0]. */
748
temp_array[0] = lowest_common;
750
/* If there is one string left, and it is identical to the
751
lowest common denominator, then the LCD is the string to
753
if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
755
free (temp_array[1]);
756
temp_array[1] = (char *)NULL;
761
/* Find the common prefix of the list of matches, and put it into
764
compute_lcd_of_matches (match_list, matches, text)
769
register int i, c1, c2, si;
770
int low; /* Count of max-matched characters. */
772
/* If only one match, just use that. Otherwise, compare each
773
member of the list with the next, finding out where they
777
match_list[0] = match_list[1];
778
match_list[1] = (char *)NULL;
782
for (i = 1, low = 100000; i < matches; i++)
784
if (_rl_completion_case_fold)
787
(c1 = _rl_to_lower(match_list[i][si])) &&
788
(c2 = _rl_to_lower(match_list[i + 1][si]));
796
(c1 = match_list[i][si]) &&
797
(c2 = match_list[i + 1][si]);
807
/* If there were multiple matches, but none matched up to even the
808
first character, and the user typed something, use that as the
809
value of matches[0]. */
810
if (low == 0 && text && *text)
812
match_list[0] = xmalloc (strlen (text) + 1);
813
strcpy (match_list[0], text);
817
match_list[0] = xmalloc (low + 1);
818
strncpy (match_list[0], match_list[1], low);
819
match_list[0][low] = '\0';
826
postprocess_matches (matchesp, matching_filenames)
828
int matching_filenames;
830
char *t, **matches, **temp_matches;
835
/* It seems to me that in all the cases we handle we would like
836
to ignore duplicate possiblilities. Scan for the text to
837
insert being identical to the other completions. */
838
if (rl_ignore_completion_duplicates)
840
temp_matches = remove_duplicate_matches (matches);
842
matches = temp_matches;
845
/* If we are matching filenames, then here is our chance to
846
do clever processing by re-examining the list. Call the
847
ignore function with the array as a parameter. It can
848
munge the array, deleting matches as it desires. */
849
if (rl_ignore_some_completions_function && matching_filenames)
851
for (nmatch = 1; matches[nmatch]; nmatch++)
853
(void)(*rl_ignore_some_completions_function) (matches);
854
if (matches == 0 || matches[0] == 0)
857
*matchesp = (char **)0;
862
/* If we removed some matches, recompute the common prefix. */
863
for (i = 1; matches[i]; i++)
865
if (i > 1 && i < nmatch)
868
compute_lcd_of_matches (matches, i - 1, t);
878
/* A convenience function for displaying a list of strings in
879
columnar format on readline's output stream. MATCHES is the list
880
of strings, in argv format, LEN is the number of strings in MATCHES,
881
and MAX is the length of the longest string in MATCHES. */
883
rl_display_match_list (matches, len, max)
887
int count, limit, printed_len;
891
/* How many items of MAX length can we fit in the screen window? */
893
limit = screenwidth / max;
894
if (limit != 1 && (limit * max == screenwidth))
897
/* Avoid a possible floating exception. If max > screenwidth,
898
limit will be 0 and a divide-by-zero fault will result. */
902
/* How many iterations of the printing loop? */
903
count = (len + (limit - 1)) / limit;
905
/* Watch out for special case. If LEN is less than LIMIT, then
906
just do the inner printing loop.
907
0 < len <= limit implies count = 1. */
909
/* Sort the items if they are not already sorted. */
910
if (rl_ignore_completion_duplicates == 0)
911
qsort (matches + 1, len, sizeof (char *), _rl_qsort_string_compare);
915
if (_rl_print_completions_horizontally == 0)
917
/* Print the sorted items, up-and-down alphabetically, like ls. */
918
for (i = 1; i <= count; i++)
920
for (j = 0, l = i; j < limit; j++)
922
if (l > len || matches[l] == 0)
926
temp = printable_part (matches[l]);
927
printed_len = print_filename (temp, matches[l]);
930
for (k = 0; k < max - printed_len; k++)
931
putc (' ', rl_outstream);
940
/* Print the sorted items, across alphabetically, like ls -x. */
941
for (i = 1; matches[i]; i++)
943
temp = printable_part (matches[i]);
944
printed_len = print_filename (temp, matches[i]);
945
/* Have we reached the end of this line? */
948
if (i && (limit > 1) && (i % limit) == 0)
951
for (k = 0; k < max - printed_len; k++)
952
putc (' ', rl_outstream);
959
/* Display MATCHES, a list of matching filenames in argv format. This
960
handles the simple case -- a single match -- first. If there is more
961
than one match, we compute the number of strings in the list and the
962
length of the longest string, which will be needed by the display
963
function. If the application wants to handle displaying the list of
964
matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
965
address of a function, and we just call it. If we're handling the
966
display ourselves, we just call rl_display_match_list. We also check
967
that the list of matches doesn't exceed the user-settable threshold,
968
and ask the user if he wants to see the list if there are more matches
969
than RL_COMPLETION_QUERY_ITEMS. */
971
display_matches (matches)
977
/* Move to the last visible line of a possibly-multiple-line command. */
978
_rl_move_vert (_rl_vis_botlin);
980
/* Handle simple case first. What if there is only one answer? */
983
temp = printable_part (matches[0]);
985
print_filename (temp, matches[0]);
988
rl_forced_update_display ();
989
rl_display_fixed = 1;
994
/* There is more than one answer. Find out how many there are,
995
and find the maximum printed length of a single entry. */
996
for (max = 0, i = 1; matches[i]; i++)
998
temp = printable_part (matches[i]);
1007
/* If the caller has defined a display hook, then call that now. */
1008
if (rl_completion_display_matches_hook)
1010
(*rl_completion_display_matches_hook) (matches, len, max);
1014
/* If there are many items, then ask the user if she really wants to
1016
if (len >= rl_completion_query_items)
1019
fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1020
fflush (rl_outstream);
1021
if (get_y_or_n () == 0)
1025
rl_forced_update_display ();
1026
rl_display_fixed = 1;
1032
rl_display_match_list (matches, len, max);
1034
rl_forced_update_display ();
1035
rl_display_fixed = 1;
1039
make_quoted_replacement (match, mtype, qc)
1042
char *qc; /* Pointer to quoting character, if any */
1044
int should_quote, do_replace;
1047
/* If we are doing completion on quoted substrings, and any matches
1048
contain any of the completer_word_break_characters, then auto-
1049
matically prepend the substring with a quote character (just pick
1050
the first one from the list of such) if it does not already begin
1051
with a quote string. FIXME: Need to remove any such automatically
1052
inserted quote character when it no longer is necessary, such as
1053
if we change the string we are completing on and the new set of
1054
matches don't require a quoted substring. */
1055
replacement = match;
1057
should_quote = match && rl_completer_quote_characters &&
1058
rl_filename_completion_desired &&
1059
rl_filename_quoting_desired;
1062
should_quote = should_quote && (!qc || !*qc ||
1063
(rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
1067
/* If there is a single match, see if we need to quote it.
1068
This also checks whether the common prefix of several
1069
matches needs to be quoted. */
1070
should_quote = rl_filename_quote_characters
1071
? (rl_strpbrk (match, rl_filename_quote_characters) != 0)
1074
do_replace = should_quote ? mtype : NO_MATCH;
1075
/* Quote the replacement, since we found an embedded
1076
word break character in a potential match. */
1077
if (do_replace != NO_MATCH && rl_filename_quoting_function)
1078
replacement = (*rl_filename_quoting_function) (match, do_replace, qc);
1080
return (replacement);
1084
insert_match (match, start, mtype, qc)
1092
oqc = qc ? *qc : '\0';
1093
replacement = make_quoted_replacement (match, mtype, qc);
1095
/* Now insert the match. */
1098
/* Don't double an opening quote character. */
1099
if (qc && *qc && start && rl_line_buffer[start - 1] == *qc &&
1100
replacement[0] == *qc)
1102
/* If make_quoted_replacement changed the quoting character, remove
1103
the opening quote and insert the (fully-quoted) replacement. */
1104
else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
1105
replacement[0] != oqc)
1107
_rl_replace_text (replacement, start, rl_point - 1);
1108
if (replacement != match)
1113
/* Append any necessary closing quote and a separator character to the
1114
just-inserted match. If the user has specified that directories
1115
should be marked by a trailing `/', append one of those instead. The
1116
default trailing character is a space. Returns the number of characters
1119
append_to_match (text, delimiter, quote_char)
1121
int delimiter, quote_char;
1123
char temp_string[4], *filename;
1124
int temp_string_index;
1127
temp_string_index = 0;
1128
if (quote_char && rl_point && rl_line_buffer[rl_point - 1] != quote_char)
1129
temp_string[temp_string_index++] = quote_char;
1132
temp_string[temp_string_index++] = delimiter;
1133
else if (rl_completion_append_character)
1134
temp_string[temp_string_index++] = rl_completion_append_character;
1136
temp_string[temp_string_index++] = '\0';
1138
if (rl_filename_completion_desired)
1140
filename = tilde_expand (text);
1141
if (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode))
1143
if (_rl_complete_mark_directories && rl_line_buffer[rl_point] != '/')
1144
rl_insert_text ("/");
1148
if (rl_point == rl_end)
1149
rl_insert_text (temp_string);
1155
if (rl_point == rl_end)
1156
rl_insert_text (temp_string);
1159
return (temp_string_index);
1163
insert_all_matches (matches, point, qc)
1171
rl_begin_undo_group ();
1172
/* remove any opening quote character; make_quoted_replacement will add
1174
if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
1176
rl_delete_text (point, rl_point);
1181
for (i = 1; matches[i]; i++)
1183
rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
1184
rl_insert_text (rp);
1185
rl_insert_text (" ");
1186
if (rp != matches[i])
1192
rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
1193
rl_insert_text (rp);
1194
rl_insert_text (" ");
1195
if (rp != matches[0])
1198
rl_end_undo_group ();
1202
free_match_list (matches)
1207
for (i = 0; matches[i]; i++)
1212
/* Complete the word at or before point.
1213
WHAT_TO_DO says what to do with the completion.
1214
`?' means list the possible completions.
1215
TAB means do standard completion.
1216
`*' means insert all of the possible completions.
1217
`!' means to do standard completion, and list all possible completions if
1218
there is more than one. */
1220
rl_complete_internal (what_to_do)
1225
int start, end, delimiter, found_quote, i;
1226
char *text, *saved_line_buffer;
1229
/* Only the completion entry function can change these. */
1230
rl_filename_completion_desired = 0;
1231
rl_filename_quoting_desired = 1;
1232
rl_completion_type = what_to_do;
1234
saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
1235
our_func = rl_completion_entry_function
1236
? rl_completion_entry_function
1237
: (Function *)filename_completion_function;
1239
/* We now look backwards for the start of a filename/variable word. */
1241
found_quote = delimiter = 0;
1245
/* This (possibly) changes rl_point. If it returns a non-zero char,
1246
we know we have an open quote. */
1247
quote_char = find_completion_word (&found_quote, &delimiter);
1252
text = rl_copy_text (start, end);
1253
matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
1259
FREE (saved_line_buffer);
1264
/* If we are matching filenames, our_func will have been set to
1265
filename_completion_function */
1266
i = our_func == (Function *)filename_completion_function;
1268
/* If we are matching filenames, the attempted completion function will
1269
have set rl_filename_completion_desired to a non-zero value. The basic
1270
filename_completion_function does this. */
1271
i = rl_filename_completion_desired;
1274
if (postprocess_matches (&matches, i) == 0)
1277
FREE (saved_line_buffer);
1278
completion_changed_buffer = 0;
1286
/* Insert the first match with proper quoting. */
1288
insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
1290
/* If there are more matches, ring the bell to indicate.
1291
If we are in vi mode, Posix.2 says to not ring the bell.
1292
If the `show-all-if-ambiguous' variable is set, display
1293
all the matches immediately. Otherwise, if this was the
1294
only match, and we are hacking files, check the file to
1295
see if it was a directory. If so, and the `mark-directories'
1296
variable is set, add a '/' to the name. If not, and we
1297
are at the end of the line, then add a space. */
1300
if (what_to_do == '!')
1302
display_matches (matches);
1305
else if (rl_editing_mode != vi_mode)
1306
ding (); /* There are other matches remaining. */
1309
append_to_match (matches[0], delimiter, quote_char);
1314
insert_all_matches (matches, start, "e_char);
1318
display_matches (matches);
1322
fprintf (stderr, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do);
1324
FREE (saved_line_buffer);
1328
free_match_list (matches);
1330
/* Check to see if the line has changed through all of this manipulation. */
1331
if (saved_line_buffer)
1333
completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
1334
free (saved_line_buffer);
1340
/***************************************************************/
1342
/* Application-callable completion match generator functions */
1344
/***************************************************************/
1346
/* Return an array of (char *) which is a list of completions for TEXT.
1347
If there are no completions, return a NULL pointer.
1348
The first entry in the returned array is the substitution for TEXT.
1349
The remaining entries are the possible completions.
1350
The array is terminated with a NULL pointer.
1352
ENTRY_FUNCTION is a function of two args, and returns a (char *).
1353
The first argument is TEXT.
1354
The second is a state argument; it should be zero on the first call, and
1355
non-zero on subsequent calls. It returns a NULL pointer to the caller
1356
when there are no more matches.
1359
completion_matches (text, entry_function)
1361
CPFunction *entry_function;
1363
/* Number of slots in match_list. */
1364
int match_list_size;
1366
/* The list of matches. */
1369
/* Number of matches actually found. */
1372
/* Temporary string binder. */
1376
match_list_size = 10;
1377
match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
1378
match_list[1] = (char *)NULL;
1380
while (string = (*entry_function) (text, matches))
1382
if (matches + 1 == match_list_size)
1383
match_list = (char **)xrealloc
1384
(match_list, ((match_list_size += 10) + 1) * sizeof (char *));
1386
match_list[++matches] = string;
1387
match_list[matches + 1] = (char *)NULL;
1390
/* If there were any matches, then look through them finding out the
1391
lowest common denominator. That then becomes match_list[0]. */
1393
compute_lcd_of_matches (match_list, matches, text);
1394
else /* There were no matches. */
1397
match_list = (char **)NULL;
1399
return (match_list);
1402
/* A completion function for usernames.
1403
TEXT contains a partial username preceded by a random
1404
character (usually `~'). */
1406
username_completion_function (text, state)
1410
#if defined (__GO32__) || defined (__WIN32__) || defined (__OPENNT)
1411
return (char *)NULL;
1412
#else /* !__GO32__ */
1413
static char *username = (char *)NULL;
1414
static struct passwd *entry;
1415
static int namelen, first_char, first_char_loc;
1423
first_char_loc = first_char == '~';
1425
username = savestring (&text[first_char_loc]);
1426
namelen = strlen (username);
1430
while (entry = getpwent ())
1432
/* Null usernames should result in all users as possible completions. */
1433
if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
1440
return ((char *)NULL);
1444
value = xmalloc (2 + strlen (entry->pw_name));
1448
strcpy (value + first_char_loc, entry->pw_name);
1450
if (first_char == '~')
1451
rl_filename_completion_desired = 1;
1455
#endif /* !__GO32__ */
1458
/* Okay, now we write the entry_function for filename completion. In the
1459
general case. Note that completion in the shell is a little different
1460
because of all the pathnames that must be followed when looking up the
1461
completion for a command. */
1463
filename_completion_function (text, state)
1467
static DIR *directory = (DIR *)NULL;
1468
static char *filename = (char *)NULL;
1469
static char *dirname = (char *)NULL;
1470
static char *users_dirname = (char *)NULL;
1471
static int filename_len;
1474
struct dirent *entry;
1476
/* If we don't have any state, then do some initialization. */
1479
/* If we were interrupted before closing the directory or reading
1480
all of its contents, close it. */
1483
closedir (directory);
1484
directory = (DIR *)NULL;
1488
FREE (users_dirname);
1490
filename = savestring (text);
1493
dirname = savestring (text);
1495
temp = strrchr (dirname, '/');
1499
strcpy (filename, ++temp);
1508
/* We aren't done yet. We also support the "~user" syntax. */
1510
/* Save the version of the directory that the user typed. */
1511
users_dirname = savestring (dirname);
1513
if (*dirname == '~')
1515
temp = tilde_expand (dirname);
1520
if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname))
1522
free (users_dirname);
1523
users_dirname = savestring (dirname);
1526
directory = opendir (dirname);
1527
filename_len = strlen (filename);
1529
rl_filename_completion_desired = 1;
1532
/* At this point we should entertain the possibility of hacking wildcarded
1533
filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
1534
contains globbing characters, then build an array of directories, and
1535
then map over that list while completing. */
1536
/* *** UNIMPLEMENTED *** */
1538
/* Now that we have some state, we can read the directory. */
1540
entry = (struct dirent *)NULL;
1541
while (directory && (entry = readdir (directory)))
1543
/* Special case for no filename.
1544
All entries except "." and ".." match. */
1545
if (filename_len == 0)
1547
if (entry->d_name[0] != '.' ||
1548
(entry->d_name[1] &&
1549
(entry->d_name[1] != '.' || entry->d_name[2])))
1554
/* Otherwise, if these match up to the length of filename, then
1556
if (_rl_completion_case_fold)
1558
if ((_rl_to_lower (entry->d_name[0]) == _rl_to_lower (filename[0])) &&
1559
(((int)D_NAMLEN (entry)) >= filename_len) &&
1560
(_rl_strnicmp (filename, entry->d_name, filename_len) == 0))
1565
if ((entry->d_name[0] == filename[0]) &&
1566
(((int)D_NAMLEN (entry)) >= filename_len) &&
1567
(strncmp (filename, entry->d_name, filename_len) == 0))
1577
closedir (directory);
1578
directory = (DIR *)NULL;
1583
dirname = (char *)NULL;
1588
filename = (char *)NULL;
1592
free (users_dirname);
1593
users_dirname = (char *)NULL;
1596
return (char *)NULL;
1600
/* dirname && (strcmp (dirname, ".") != 0) */
1601
if (dirname && (dirname[0] != '.' || dirname[1]))
1603
if (rl_complete_with_tilde_expansion && *users_dirname == '~')
1605
dirlen = strlen (dirname);
1606
temp = xmalloc (2 + dirlen + D_NAMLEN (entry));
1607
strcpy (temp, dirname);
1608
/* Canonicalization cuts off any final slash present. We
1609
may need to add it back. */
1610
if (dirname[dirlen - 1] != '/')
1612
temp[dirlen++] = '/';
1613
temp[dirlen] = '\0';
1618
dirlen = strlen (users_dirname);
1619
temp = xmalloc (1 + dirlen + D_NAMLEN (entry));
1620
strcpy (temp, users_dirname);
1623
strcpy (temp + dirlen, entry->d_name);
1626
temp = savestring (entry->d_name);
1632
/* An initial implementation of a menu completion function a la tcsh. The
1633
first time (if the last readline command was not rl_menu_complete), we
1634
generate the list of matches. This code is very similar to the code in
1635
rl_complete_internal -- there should be a way to combine the two. Then,
1636
for each item in the list of matches, we insert the match in an undoable
1637
fashion, with the appropriate character appended (this happens on the
1638
second and subsequent consecutive calls to rl_menu_complete). When we
1639
hit the end of the match list, we restore the original unmatched text,
1640
ring the bell, and reset the counter to zero. */
1642
rl_menu_complete (count, ignore)
1646
int matching_filenames, found_quote;
1648
static char *orig_text;
1649
static char **matches = (char **)0;
1650
static int match_list_index = 0;
1651
static int match_list_size = 0;
1652
static int orig_start, orig_end;
1653
static char quote_char;
1654
static int delimiter;
1656
/* The first time through, we generate the list of matches and set things
1657
up to insert them. */
1658
if (rl_last_func != rl_menu_complete)
1660
/* Clean up from previous call, if any. */
1664
for (match_list_index = 0; matches[match_list_index]; match_list_index++)
1665
free (matches[match_list_index]);
1669
match_list_index = match_list_size = 0;
1670
matches = (char **)NULL;
1672
/* Only the completion entry function can change these. */
1673
rl_filename_completion_desired = 0;
1674
rl_filename_quoting_desired = 1;
1675
rl_completion_type = '%';
1677
our_func = rl_completion_entry_function
1678
? rl_completion_entry_function
1679
: (Function *)filename_completion_function;
1681
/* We now look backwards for the start of a filename/variable word. */
1682
orig_end = rl_point;
1683
found_quote = delimiter = 0;
1687
/* This (possibly) changes rl_point. If it returns a non-zero char,
1688
we know we have an open quote. */
1689
quote_char = find_completion_word (&found_quote, &delimiter);
1691
orig_start = rl_point;
1692
rl_point = orig_end;
1694
orig_text = rl_copy_text (orig_start, orig_end);
1695
matches = gen_completion_matches (orig_text, orig_start, orig_end,
1696
our_func, found_quote, quote_char);
1699
/* If we are matching filenames, our_func will have been set to
1700
filename_completion_function */
1701
matching_filenames = our_func == (Function *)filename_completion_function;
1703
/* If we are matching filenames, the attempted completion function will
1704
have set rl_filename_completion_desired to a non-zero value. The basic
1705
filename_completion_function does this. */
1706
matching_filenames = rl_filename_completion_desired;
1708
if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
1712
matches = (char **)0;
1714
orig_text = (char *)0;
1715
completion_changed_buffer = 0;
1719
for (match_list_size = 0; matches[match_list_size]; match_list_size++)
1721
/* matches[0] is lcd if match_list_size > 1, but the circular buffer
1722
code below should take care of it. */
1725
/* Now we have the list of matches. Replace the text between
1726
rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
1727
matches[match_list_index], and add any necessary closing char. */
1729
if (matches == 0 || match_list_size == 0)
1733
matches = (char **)0;
1734
completion_changed_buffer = 0;
1738
match_list_index = (match_list_index + count) % match_list_size;
1739
if (match_list_index < 0)
1740
match_list_index += match_list_size;
1742
if (match_list_index == 0 && match_list_size > 1)
1745
insert_match (orig_text, orig_start, MULT_MATCH, "e_char);
1749
insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
1750
append_to_match (matches[match_list_index], delimiter, quote_char);
1753
completion_changed_buffer = 1;