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 /* Forward declarations for functions defined and used in this file. */
89 char *filename_completion_function ();
90 char **completion_matches ();
92 #if defined (VISIBLE_STATS)
96 static int stat_char ();
99 static char *rl_quote_filename ();
100 static char *rl_strpbrk ();
102 static char **remove_duplicate_matches ();
103 static void insert_match ();
104 static int append_to_match ();
105 static void insert_all_matches ();
106 static void display_matches ();
107 static int compute_lcd_of_matches ();
109 extern char *xmalloc (), *xrealloc ();
111 /* **************************************************************** */
113 /* Completion matching, from readline's point of view. */
115 /* **************************************************************** */
117 /* Variables known only to the readline library. */
119 /* If non-zero, non-unique completions always show the list of matches. */
120 int _rl_complete_show_all
= 0;
122 /* If non-zero, completed directory names have a slash appended. */
123 int _rl_complete_mark_directories
= 1;
125 /* If non-zero, completions are printed horizontally in alphabetical order,
127 int _rl_print_completions_horizontally
;
129 /* Non-zero means that case is not significant in filename completion. */
130 int _rl_completion_case_fold
;
132 /* Global variables available to applications using readline. */
134 #if defined (VISIBLE_STATS)
135 /* Non-zero means add an additional character to each filename displayed
136 during listing completion iff rl_filename_completion_desired which helps
137 to indicate the type of file being listed. */
138 int rl_visible_stats
= 0;
139 #endif /* VISIBLE_STATS */
141 /* If non-zero, then this is the address of a function to call when
142 completing on a directory name. The function is called with
143 the address of a string (the current directory name) as an arg. */
144 Function
*rl_directory_completion_hook
= (Function
*)NULL
;
146 /* Non-zero means readline completion functions perform tilde expansion. */
147 int rl_complete_with_tilde_expansion
= 0;
149 /* Pointer to the generator function for completion_matches ().
150 NULL means to use filename_completion_function (), the default filename
152 Function
*rl_completion_entry_function
= (Function
*)NULL
;
154 /* Pointer to alternative function to create matches.
155 Function is called with TEXT, START, and END.
156 START and END are indices in RL_LINE_BUFFER saying what the boundaries
158 If this function exists and returns NULL then call the value of
159 rl_completion_entry_function to try to match, otherwise use the
160 array of strings returned. */
161 CPPFunction
*rl_attempted_completion_function
= (CPPFunction
*)NULL
;
163 /* Non-zero means to suppress normal filename completion after the
164 user-specified completion function has been called. */
165 int rl_attempted_completion_over
= 0;
167 /* Set to a character indicating the type of completion being performed
168 by rl_complete_internal, available for use by application completion
170 int rl_completion_type
= 0;
172 /* Up to this many items will be displayed in response to a
173 possible-completions call. After that, we ask the user if
174 she is sure she wants to see them all. */
175 int rl_completion_query_items
= 100;
177 /* The basic list of characters that signal a break between words for the
178 completer routine. The contents of this variable is what breaks words
179 in the shell, i.e. " \t\n\"\\'`@$><=" */
180 char *rl_basic_word_break_characters
= " \t\n\"\\'`@$><=;|&{(";
182 /* List of basic quoting characters. */
183 char *rl_basic_quote_characters
= "\"'";
185 /* The list of characters that signal a break between words for
186 rl_complete_internal. The default list is the contents of
187 rl_basic_word_break_characters. */
188 char *rl_completer_word_break_characters
= (char *)NULL
;
190 /* List of characters which can be used to quote a substring of the line.
191 Completion occurs on the entire substring, and within the substring
192 rl_completer_word_break_characters are treated as any other character,
193 unless they also appear within this list. */
194 char *rl_completer_quote_characters
= (char *)NULL
;
196 /* List of characters that should be quoted in filenames by the completer. */
197 char *rl_filename_quote_characters
= (char *)NULL
;
199 /* List of characters that are word break characters, but should be left
200 in TEXT when it is passed to the completion function. The shell uses
201 this to help determine what kind of completing to do. */
202 char *rl_special_prefixes
= (char *)NULL
;
204 /* If non-zero, then disallow duplicates in the matches. */
205 int rl_ignore_completion_duplicates
= 1;
207 /* Non-zero means that the results of the matches are to be treated
208 as filenames. This is ALWAYS zero on entry, and can only be changed
209 within a completion entry finder function. */
210 int rl_filename_completion_desired
= 0;
212 /* Non-zero means that the results of the matches are to be quoted using
213 double quotes (or an application-specific quoting mechanism) if the
214 filename contains any characters in rl_filename_quote_chars. This is
215 ALWAYS non-zero on entry, and can only be changed within a completion
216 entry finder function. */
217 int rl_filename_quoting_desired
= 1;
219 /* This function, if defined, is called by the completer when real
220 filename completion is done, after all the matching names have been
221 generated. It is passed a (char**) known as matches in the code below.
222 It consists of a NULL-terminated array of pointers to potential
223 matching strings. The 1st element (matches[0]) is the maximal
224 substring that is common to all matches. This function can re-arrange
225 the list of matches as required, but all elements of the array must be
226 free()'d if they are deleted. The main intent of this function is
227 to implement FIGNORE a la SunOS csh. */
228 Function
*rl_ignore_some_completions_function
= (Function
*)NULL
;
230 /* Set to a function to quote a filename in an application-specific fashion.
231 Called with the text to quote, the type of match found (single or multiple)
232 and a pointer to the quoting character to be used, which the function can
234 CPFunction
*rl_filename_quoting_function
= rl_quote_filename
;
236 /* Function to call to remove quoting characters from a filename. Called
237 before completion is attempted, so the embedded quotes do not interfere
238 with matching names in the file system. Readline doesn't do anything
239 with this; it's set only by applications. */
240 CPFunction
*rl_filename_dequoting_function
= (CPFunction
*)NULL
;
242 /* Function to call to decide whether or not a word break character is
243 quoted. If a character is quoted, it does not break words for the
245 Function
*rl_char_is_quoted_p
= (Function
*)NULL
;
247 /* Character appended to completed words when at the end of the line. The
248 default is a space. */
249 int rl_completion_append_character
= ' ';
251 /* If non-zero, inhibit completion (temporarily). */
252 int rl_inhibit_completion
;
254 /* Variables local to this file. */
256 /* Local variable states what happened during the last completion attempt. */
257 static int completion_changed_buffer
;
259 /*************************************/
261 /* Bindable completion functions */
263 /*************************************/
265 /* Complete the word at or before point. You have supplied the function
266 that does the initial simple matching selection algorithm (see
267 completion_matches ()). The default is to do filename completion. */
269 rl_complete (ignore
, invoking_key
)
270 int ignore
, invoking_key
;
272 if (rl_inhibit_completion
)
273 return (rl_insert (ignore
, invoking_key
));
274 else if (rl_last_func
== rl_complete
&& !completion_changed_buffer
)
275 return (rl_complete_internal ('?'));
276 else if (_rl_complete_show_all
)
277 return (rl_complete_internal ('!'));
279 return (rl_complete_internal (TAB
));
282 /* List the possible completions. See description of rl_complete (). */
284 rl_possible_completions (ignore
, invoking_key
)
285 int ignore
, invoking_key
;
287 return (rl_complete_internal ('?'));
291 rl_insert_completions (ignore
, invoking_key
)
292 int ignore
, invoking_key
;
294 return (rl_complete_internal ('*'));
297 /************************************/
299 /* Completion utility functions */
301 /************************************/
303 /* Find the first occurrence in STRING1 of any character from STRING2.
304 Return a pointer to the character in STRING1. */
306 rl_strpbrk (string1
, string2
)
307 char *string1
, *string2
;
311 for (; *string1
; string1
++)
313 for (scan
= string2
; *scan
; scan
++)
315 if (*string1
== *scan
)
321 return ((char *)NULL
);
324 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
333 if (c
== 'y' || c
== 'Y' || c
== ' ')
335 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
338 _rl_abort_internal ();
343 #if defined (VISIBLE_STATS)
344 /* Return the character which best describes FILENAME.
345 `@' for symbolic links
350 `%' for character special devices
351 `#' for block special devices */
359 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
360 r
= lstat (filename
, &finfo
);
362 r
= stat (filename
, &finfo
);
369 if (S_ISDIR (finfo
.st_mode
))
371 #if defined (S_ISCHR)
372 else if (S_ISCHR (finfo
.st_mode
))
375 #if defined (S_ISBLK)
376 else if (S_ISBLK (finfo
.st_mode
))
379 #if defined (S_ISLNK)
380 else if (S_ISLNK (finfo
.st_mode
))
383 #if defined (S_ISSOCK)
384 else if (S_ISSOCK (finfo
.st_mode
))
386 #endif /* S_ISSOCK */
387 #if defined (S_ISFIFO)
388 else if (S_ISFIFO (finfo
.st_mode
))
391 else if (S_ISREG (finfo
.st_mode
))
393 if (access (filename
, X_OK
) == 0)
398 #endif /* VISIBLE_STATS */
400 /* Return the portion of PATHNAME that should be output when listing
401 possible completions. If we are hacking filename completion, we
402 are only interested in the basename, the portion following the
403 final slash. Otherwise, we return what we were passed. */
405 printable_part (pathname
)
410 temp
= rl_filename_completion_desired
? strrchr (pathname
, '/') : (char *)NULL
;
411 return (temp
? ++temp
: pathname
);
414 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
415 are using it, check for and output a single character for `special'
416 filenames. Return the number of characters we output. */
422 putc ('^', rl_outstream); \
423 putc (UNCTRL (c), rl_outstream); \
426 else if (c == RUBOUT) \
428 putc ('^', rl_outstream); \
429 putc ('?', rl_outstream); \
434 putc (c, rl_outstream); \
440 print_filename (to_print
, full_pathname
)
441 char *to_print
, *full_pathname
;
444 #if !defined (VISIBLE_STATS)
447 for (s
= to_print
; *s
; s
++)
452 char *s
, c
, *new_full_pathname
;
453 int extension_char
, slen
, tlen
;
455 for (s
= to_print
; *s
; s
++)
460 if (rl_filename_completion_desired
&& rl_visible_stats
)
462 /* If to_print != full_pathname, to_print is the basename of the
463 path passed. In this case, we try to expand the directory
464 name before checking for the stat character. */
465 if (to_print
!= full_pathname
)
467 /* Terminate the directory name. */
471 s
= tilde_expand (full_pathname
);
472 if (rl_directory_completion_hook
)
473 (*rl_directory_completion_hook
) (&s
);
476 tlen
= strlen (to_print
);
477 new_full_pathname
= xmalloc (slen
+ tlen
+ 2);
478 strcpy (new_full_pathname
, s
);
479 new_full_pathname
[slen
] = '/';
480 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
482 extension_char
= stat_char (new_full_pathname
);
484 free (new_full_pathname
);
489 s
= tilde_expand (full_pathname
);
490 extension_char
= stat_char (s
);
496 putc (extension_char
, rl_outstream
);
500 #endif /* VISIBLE_STATS */
505 rl_quote_filename (s
, rtype
, qcp
)
512 r
= xmalloc (strlen (s
) + 2);
513 *r
= *rl_completer_quote_characters
;
516 *qcp
= *rl_completer_quote_characters
;
520 /* Find the bounds of the current word for completion purposes, and leave
521 rl_point set to the end of the word. This function skips quoted
522 substrings (characters between matched pairs of characters in
523 rl_completer_quote_characters. First we try to find an unclosed
524 quoted substring on which to do matching. If one is not found, we use
525 the word break characters to find the boundaries of the current word.
526 We call an application-specific function to decide whether or not a
527 particular word break character is quoted; if that function returns a
528 non-zero result, the character does not break a word. This function
529 returns the opening quote character if we found an unclosed quoted
530 substring, '\0' otherwise. FP, if non-null, is set to a value saying
531 which (shell-like) quote characters we found (single quote, double
532 quote, or backslash) anywhere in the string. DP, if non-null, is set to
533 the value of the delimiter character that caused a word break. */
536 find_completion_word (fp
, dp
)
539 int scan
, end
, found_quote
, delimiter
, pass_next
, isbrk
;
543 found_quote
= delimiter
= 0;
546 if (rl_completer_quote_characters
)
548 /* We have a list of characters which can be used in pairs to
549 quote substrings for the completer. Try to find the start
550 of an unclosed quoted substring. */
551 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
552 for (scan
= pass_next
= 0; scan
< end
; scan
++)
560 if (rl_line_buffer
[scan
] == '\\')
563 found_quote
|= RL_QF_BACKSLASH
;
567 if (quote_char
!= '\0')
569 /* Ignore everything until the matching close quote char. */
570 if (rl_line_buffer
[scan
] == quote_char
)
572 /* Found matching close. Abandon this substring. */
577 else if (strchr (rl_completer_quote_characters
, rl_line_buffer
[scan
]))
579 /* Found start of a quoted substring. */
580 quote_char
= rl_line_buffer
[scan
];
582 /* Shell-like quoting conventions. */
583 if (quote_char
== '\'')
584 found_quote
|= RL_QF_SINGLE_QUOTE
;
585 else if (quote_char
== '"')
586 found_quote
|= RL_QF_DOUBLE_QUOTE
;
591 if (rl_point
== end
&& quote_char
== '\0')
593 /* We didn't find an unclosed quoted substring upon which to do
594 completion, so use the word break characters to find the
595 substring on which to complete. */
598 scan
= rl_line_buffer
[rl_point
];
600 if (strchr (rl_completer_word_break_characters
, scan
) == 0)
603 /* Call the application-specific function to tell us whether
604 this word break character is quoted and should be skipped. */
605 if (rl_char_is_quoted_p
&& found_quote
&&
606 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
))
609 /* Convoluted code, but it avoids an n^2 algorithm with calls
610 to char_is_quoted. */
615 /* If we are at an unquoted word break, then advance past it. */
616 scan
= rl_line_buffer
[rl_point
];
618 /* If there is an application-specific function to say whether or not
619 a character is quoted and we found a quote character, let that
620 function decide whether or not a character is a word break, even
621 if it is found in rl_completer_word_break_characters. */
622 if (rl_char_is_quoted_p
)
623 isbrk
= (found_quote
== 0 ||
624 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
) == 0) &&
625 strchr (rl_completer_word_break_characters
, scan
) != 0;
627 isbrk
= strchr (rl_completer_word_break_characters
, scan
) != 0;
631 /* If the character that caused the word break was a quoting
632 character, then remember it as the delimiter. */
633 if (rl_basic_quote_characters
&& strchr (rl_basic_quote_characters
, scan
) && (end
- rl_point
) > 1)
636 /* If the character isn't needed to determine something special
637 about what kind of completion to perform, then advance past it. */
638 if (rl_special_prefixes
== 0 || strchr (rl_special_prefixes
, scan
) == 0)
651 gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
)
655 int found_quote
, quote_char
;
657 char **matches
, *temp
;
659 /* If the user wants to TRY to complete, but then wants to give
660 up and use the default completion function, they set the
661 variable rl_attempted_completion_function. */
662 if (rl_attempted_completion_function
)
664 matches
= (*rl_attempted_completion_function
) (text
, start
, end
);
666 if (matches
|| rl_attempted_completion_over
)
668 rl_attempted_completion_over
= 0;
673 /* Beware -- we're stripping the quotes here. Do this only if we know
674 we are doing filename completion and the application has defined a
675 filename dequoting function. */
677 if (found_quote
&& our_func
== (Function
*)filename_completion_function
&&
678 rl_filename_dequoting_function
)
680 /* delete single and double quotes */
681 temp
= (*rl_filename_dequoting_function
) (text
, quote_char
);
682 text
= temp
; /* not freeing text is not a memory leak */
685 matches
= completion_matches (text
, our_func
);
690 /* Filter out duplicates in MATCHES. This frees up the strings in
693 remove_duplicate_matches (matches
)
701 /* Sort the items. */
702 for (i
= 0; matches
[i
]; i
++)
705 /* Sort the array without matches[0], since we need it to
706 stay in place no matter what. */
708 qsort (matches
+1, i
-1, sizeof (char *), _rl_qsort_string_compare
);
710 /* Remember the lowest common denominator for it may be unique. */
711 lowest_common
= savestring (matches
[0]);
713 for (i
= newlen
= 0; matches
[i
+ 1]; i
++)
715 if (strcmp (matches
[i
], matches
[i
+ 1]) == 0)
718 matches
[i
] = (char *)&dead_slot
;
724 /* We have marked all the dead slots with (char *)&dead_slot.
725 Copy all the non-dead entries into a new array. */
726 temp_array
= (char **)xmalloc ((3 + newlen
) * sizeof (char *));
727 for (i
= j
= 1; matches
[i
]; i
++)
729 if (matches
[i
] != (char *)&dead_slot
)
730 temp_array
[j
++] = matches
[i
];
732 temp_array
[j
] = (char *)NULL
;
734 if (matches
[0] != (char *)&dead_slot
)
737 /* Place the lowest common denominator back in [0]. */
738 temp_array
[0] = lowest_common
;
740 /* If there is one string left, and it is identical to the
741 lowest common denominator, then the LCD is the string to
743 if (j
== 2 && strcmp (temp_array
[0], temp_array
[1]) == 0)
745 free (temp_array
[1]);
746 temp_array
[1] = (char *)NULL
;
751 /* Find the common prefix of the list of matches, and put it into
754 compute_lcd_of_matches (match_list
, matches
, text
)
759 register int i
, c1
, c2
, si
;
760 int low
; /* Count of max-matched characters. */
762 /* If only one match, just use that. Otherwise, compare each
763 member of the list with the next, finding out where they
767 match_list
[0] = match_list
[1];
768 match_list
[1] = (char *)NULL
;
772 for (i
= 1, low
= 100000; i
< matches
; i
++)
774 if (_rl_completion_case_fold
)
777 (c1
= _rl_to_lower(match_list
[i
][si
])) &&
778 (c2
= _rl_to_lower(match_list
[i
+ 1][si
]));
786 (c1
= match_list
[i
][si
]) &&
787 (c2
= match_list
[i
+ 1][si
]);
797 /* If there were multiple matches, but none matched up to even the
798 first character, and the user typed something, use that as the
799 value of matches[0]. */
800 if (low
== 0 && text
&& *text
)
802 match_list
[0] = xmalloc (strlen (text
) + 1);
803 strcpy (match_list
[0], text
);
807 match_list
[0] = xmalloc (low
+ 1);
808 strncpy (match_list
[0], match_list
[1], low
);
809 match_list
[0][low
] = '\0';
816 postprocess_matches (text
, matchesp
, matching_filenames
)
819 int matching_filenames
;
821 char *t
, **matches
, **temp_matches
;
826 /* It seems to me that in all the cases we handle we would like
827 to ignore duplicate possiblilities. Scan for the text to
828 insert being identical to the other completions. */
829 if (rl_ignore_completion_duplicates
)
831 temp_matches
= remove_duplicate_matches (matches
);
833 matches
= temp_matches
;
836 /* If we are matching filenames, then here is our chance to
837 do clever processing by re-examining the list. Call the
838 ignore function with the array as a parameter. It can
839 munge the array, deleting matches as it desires. */
840 if (rl_ignore_some_completions_function
&& matching_filenames
)
842 for (nmatch
= 1; matches
[nmatch
]; nmatch
++)
844 (void)(*rl_ignore_some_completions_function
) (matches
);
845 if (matches
== 0 || matches
[0] == 0)
849 *matchesp
= (char **)0;
854 /* If we removed some matches, recompute the common prefix. */
855 for (i
= 1; matches
[i
]; i
++)
857 if (i
> 1 && i
< nmatch
)
860 compute_lcd_of_matches (matches
, i
- 1, text
);
871 display_matches (matches
)
874 int len
, count
, limit
, max
, printed_len
;
878 /* Move to the last visible line of a possibly-multiple-line command. */
879 _rl_move_vert (_rl_vis_botlin
);
881 /* Handle simple case first. What if there is only one answer? */
884 temp
= printable_part (matches
[0]);
886 print_filename (temp
, matches
[0]);
891 rl_forced_update_display ();
892 rl_display_fixed
= 1;
897 /* There is more than one answer. Find out how many there are,
898 and find the maximum printed length of a single entry. */
899 for (max
= 0, i
= 1; matches
[i
]; i
++)
901 temp
= printable_part (matches
[i
]);
910 /* If there are many items, then ask the user if she really wants to
912 if (len
>= rl_completion_query_items
)
915 fprintf (rl_outstream
, "Display all %d possibilities? (y or n)", len
);
916 fflush (rl_outstream
);
917 if (get_y_or_n () == 0)
923 rl_forced_update_display ();
924 rl_display_fixed
= 1;
930 /* How many items of MAX length can we fit in the screen window? */
932 limit
= screenwidth
/ max
;
933 if (limit
!= 1 && (limit
* max
== screenwidth
))
936 /* Avoid a possible floating exception. If max > screenwidth,
937 limit will be 0 and a divide-by-zero fault will result. */
941 /* How many iterations of the printing loop? */
942 count
= (len
+ (limit
- 1)) / limit
;
944 /* Watch out for special case. If LEN is less than LIMIT, then
945 just do the inner printing loop.
946 0 < len <= limit implies count = 1. */
948 /* Sort the items if they are not already sorted. */
949 if (rl_ignore_completion_duplicates
== 0)
950 qsort (matches
+ 1, len
, sizeof (char *), _rl_qsort_string_compare
);
954 if (_rl_print_completions_horizontally
== 0)
956 /* Print the sorted items, up-and-down alphabetically, like ls. */
957 for (i
= 1; i
<= count
; i
++)
959 for (j
= 0, l
= i
; j
< limit
; j
++)
961 if (l
> len
|| matches
[l
] == 0)
965 temp
= printable_part (matches
[l
]);
966 printed_len
= print_filename (temp
, matches
[l
]);
969 for (k
= 0; k
< max
- printed_len
; k
++)
970 putc (' ', rl_outstream
);
979 /* Print the sorted items, across alphabetically, like ls -x. */
980 for (i
= 1; matches
[i
]; i
++)
982 temp
= printable_part (matches
[i
]);
983 printed_len
= print_filename (temp
, matches
[i
]);
984 /* Have we reached the end of this line? */
987 if (i
&& (limit
> 1) && (i
% limit
) == 0)
990 for (k
= 0; k
< max
- printed_len
; k
++)
991 putc (' ', rl_outstream
);
1000 rl_forced_update_display ();
1001 rl_display_fixed
= 1;
1006 make_quoted_replacement (match
, mtype
, qc
)
1009 char *qc
; /* Pointer to quoting character, if any */
1011 int should_quote
, do_replace
;
1014 /* If we are doing completion on quoted substrings, and any matches
1015 contain any of the completer_word_break_characters, then auto-
1016 matically prepend the substring with a quote character (just pick
1017 the first one from the list of such) if it does not already begin
1018 with a quote string. FIXME: Need to remove any such automatically
1019 inserted quote character when it no longer is necessary, such as
1020 if we change the string we are completing on and the new set of
1021 matches don't require a quoted substring. */
1022 replacement
= match
;
1024 should_quote
= match
&& rl_completer_quote_characters
&&
1025 rl_filename_completion_desired
&&
1026 rl_filename_quoting_desired
;
1030 should_quote
= should_quote
&& (!qc
|| !*qc
|| *qc
== '"' || *qc
== '\'');
1032 should_quote
= should_quote
&& (!qc
|| !*qc
);
1037 /* If there is a single match, see if we need to quote it.
1038 This also checks whether the common prefix of several
1039 matches needs to be quoted. */
1040 should_quote
= rl_filename_quote_characters
1041 ? (rl_strpbrk (match
, rl_filename_quote_characters
) != 0)
1044 do_replace
= should_quote
? mtype
: NO_MATCH
;
1045 /* Quote the replacement, since we found an embedded
1046 word break character in a potential match. */
1047 if (do_replace
!= NO_MATCH
&& rl_filename_quoting_function
)
1048 replacement
= (*rl_filename_quoting_function
) (match
, do_replace
, qc
);
1050 return (replacement
);
1054 insert_match (match
, start
, mtype
, qc
)
1062 oqc
= qc
? *qc
: '\0';
1063 replacement
= make_quoted_replacement (match
, mtype
, qc
);
1065 /* Now insert the match. */
1068 /* Don't double an opening quote character. */
1069 if (qc
&& *qc
&& start
&& rl_line_buffer
[start
- 1] == *qc
&&
1070 replacement
[0] == *qc
)
1072 /* If make_quoted_replacement changed the quoting character, remove
1073 the opening quote and insert the (fully-quoted) replacement. */
1074 else if (qc
&& (*qc
!= oqc
) && start
&& rl_line_buffer
[start
- 1] == oqc
&&
1075 replacement
[0] != oqc
)
1077 _rl_replace_text (replacement
, start
, rl_point
- 1);
1078 if (replacement
!= match
)
1083 /* Append any necessary closing quote and a separator character to the
1084 just-inserted match. If the user has specified that directories
1085 should be marked by a trailing `/', append one of those instead. The
1086 default trailing character is a space. Returns the number of characters
1089 append_to_match (text
, delimiter
, quote_char
)
1091 int delimiter
, quote_char
;
1093 char temp_string
[4], *filename
;
1094 int temp_string_index
;
1097 temp_string_index
= 0;
1098 if (quote_char
&& rl_point
&& rl_line_buffer
[rl_point
- 1] != quote_char
)
1099 temp_string
[temp_string_index
++] = quote_char
;
1102 temp_string
[temp_string_index
++] = delimiter
;
1103 else if (rl_completion_append_character
)
1104 temp_string
[temp_string_index
++] = rl_completion_append_character
;
1106 temp_string
[temp_string_index
++] = '\0';
1108 if (rl_filename_completion_desired
)
1110 filename
= tilde_expand (text
);
1111 if (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
))
1113 if (_rl_complete_mark_directories
&& rl_line_buffer
[rl_point
] != '/')
1114 rl_insert_text ("/");
1118 if (rl_point
== rl_end
)
1119 rl_insert_text (temp_string
);
1125 if (rl_point
== rl_end
)
1126 rl_insert_text (temp_string
);
1129 return (temp_string_index
);
1133 insert_all_matches (matches
, point
, qc
)
1141 rl_begin_undo_group ();
1142 /* remove any opening quote character; make_quoted_replacement will add
1144 if (qc
&& *qc
&& point
&& rl_line_buffer
[point
- 1] == *qc
)
1146 rl_delete_text (point
, rl_point
);
1151 for (i
= 1; matches
[i
]; i
++)
1153 rp
= make_quoted_replacement (matches
[i
], SINGLE_MATCH
, qc
);
1154 rl_insert_text (rp
);
1155 rl_insert_text (" ");
1156 if (rp
!= matches
[i
])
1162 rp
= make_quoted_replacement (matches
[0], SINGLE_MATCH
, qc
);
1163 rl_insert_text (rp
);
1164 rl_insert_text (" ");
1165 if (rp
!= matches
[0])
1168 rl_end_undo_group ();
1171 /* Complete the word at or before point.
1172 WHAT_TO_DO says what to do with the completion.
1173 `?' means list the possible completions.
1174 TAB means do standard completion.
1175 `*' means insert all of the possible completions.
1176 `!' means to do standard completion, and list all possible completions if
1177 there is more than one. */
1179 rl_complete_internal (what_to_do
)
1184 int start
, end
, delimiter
, found_quote
, i
;
1185 char *text
, *saved_line_buffer
;
1188 /* Only the completion entry function can change these. */
1189 rl_filename_completion_desired
= 0;
1190 rl_filename_quoting_desired
= 1;
1191 rl_completion_type
= what_to_do
;
1193 saved_line_buffer
= rl_line_buffer
? savestring (rl_line_buffer
) : (char *)NULL
;
1194 our_func
= rl_completion_entry_function
1195 ? rl_completion_entry_function
1196 : (Function
*)filename_completion_function
;
1198 /* We now look backwards for the start of a filename/variable word. */
1200 found_quote
= delimiter
= 0;
1204 /* This (possibly) changes rl_point. If it returns a non-zero char,
1205 we know we have an open quote. */
1206 quote_char
= find_completion_word (&found_quote
, &delimiter
);
1211 text
= rl_copy_text (start
, end
);
1212 matches
= gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
);
1217 FREE (saved_line_buffer
);
1222 /* If we are matching filenames, our_func will have been set to
1223 filename_completion_function */
1224 i
= our_func
== (Function
*)filename_completion_function
;
1225 if (postprocess_matches (text
, &matches
, i
) == 0)
1227 FREE (saved_line_buffer
);
1238 /* Insert the first match with proper quoting. */
1240 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
1242 /* If there are more matches, ring the bell to indicate.
1243 If we are in vi mode, Posix.2 says to not ring the bell.
1244 If the `show-all-if-ambiguous' variable is set, display
1245 all the matches immediately. Otherwise, if this was the
1246 only match, and we are hacking files, check the file to
1247 see if it was a directory. If so, and the `mark-directories'
1248 variable is set, add a '/' to the name. If not, and we
1249 are at the end of the line, then add a space. */
1252 if (what_to_do
== '!')
1254 display_matches (matches
);
1257 else if (rl_editing_mode
!= vi_mode
)
1258 ding (); /* There are other matches remaining. */
1261 append_to_match (matches
[0], delimiter
, quote_char
);
1266 insert_all_matches (matches
, start
, "e_char
);
1270 display_matches (matches
);
1274 fprintf (stderr
, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do
);
1276 FREE (saved_line_buffer
);
1280 for (i
= 0; matches
[i
]; i
++)
1284 /* Check to see if the line has changed through all of this manipulation. */
1285 if (saved_line_buffer
)
1287 completion_changed_buffer
= strcmp (rl_line_buffer
, saved_line_buffer
) != 0;
1288 free (saved_line_buffer
);
1294 /***************************************************************/
1296 /* Application-callable completion match generator functions */
1298 /***************************************************************/
1300 /* Return an array of (char *) which is a list of completions for TEXT.
1301 If there are no completions, return a NULL pointer.
1302 The first entry in the returned array is the substitution for TEXT.
1303 The remaining entries are the possible completions.
1304 The array is terminated with a NULL pointer.
1306 ENTRY_FUNCTION is a function of two args, and returns a (char *).
1307 The first argument is TEXT.
1308 The second is a state argument; it should be zero on the first call, and
1309 non-zero on subsequent calls. It returns a NULL pointer to the caller
1310 when there are no more matches.
1313 completion_matches (text
, entry_function
)
1315 CPFunction
*entry_function
;
1317 /* Number of slots in match_list. */
1318 int match_list_size
;
1320 /* The list of matches. */
1323 /* Number of matches actually found. */
1326 /* Temporary string binder. */
1330 match_list_size
= 10;
1331 match_list
= (char **)xmalloc ((match_list_size
+ 1) * sizeof (char *));
1332 match_list
[1] = (char *)NULL
;
1334 while (string
= (*entry_function
) (text
, matches
))
1336 if (matches
+ 1 == match_list_size
)
1337 match_list
= (char **)xrealloc
1338 (match_list
, ((match_list_size
+= 10) + 1) * sizeof (char *));
1340 match_list
[++matches
] = string
;
1341 match_list
[matches
+ 1] = (char *)NULL
;
1344 /* If there were any matches, then look through them finding out the
1345 lowest common denominator. That then becomes match_list[0]. */
1347 compute_lcd_of_matches (match_list
, matches
, text
);
1348 else /* There were no matches. */
1351 match_list
= (char **)NULL
;
1353 return (match_list
);
1356 /* A completion function for usernames.
1357 TEXT contains a partial username preceded by a random
1358 character (usually `~'). */
1360 username_completion_function (text
, state
)
1364 #if defined (__GO32__) || defined (__WIN32__)
1365 return (char *)NULL
;
1366 #else /* !__GO32__ */
1367 static char *username
= (char *)NULL
;
1368 static struct passwd
*entry
;
1369 static int namelen
, first_char
, first_char_loc
;
1377 first_char_loc
= first_char
== '~';
1379 username
= savestring (&text
[first_char_loc
]);
1380 namelen
= strlen (username
);
1384 while (entry
= getpwent ())
1386 /* Null usernames should result in all users as possible completions. */
1387 if (namelen
== 0 || (STREQN (username
, entry
->pw_name
, namelen
)))
1394 return ((char *)NULL
);
1398 value
= xmalloc (2 + strlen (entry
->pw_name
));
1402 strcpy (value
+ first_char_loc
, entry
->pw_name
);
1404 if (first_char
== '~')
1405 rl_filename_completion_desired
= 1;
1409 #endif /* !__GO32__ */
1412 /* Okay, now we write the entry_function for filename completion. In the
1413 general case. Note that completion in the shell is a little different
1414 because of all the pathnames that must be followed when looking up the
1415 completion for a command. */
1417 filename_completion_function (text
, state
)
1421 static DIR *directory
= (DIR *)NULL
;
1422 static char *filename
= (char *)NULL
;
1423 static char *dirname
= (char *)NULL
;
1424 static char *users_dirname
= (char *)NULL
;
1425 static int filename_len
;
1428 struct dirent
*entry
;
1430 /* If we don't have any state, then do some initialization. */
1433 /* If we were interrupted before closing the directory or reading
1434 all of its contents, close it. */
1437 closedir (directory
);
1438 directory
= (DIR *)NULL
;
1442 FREE (users_dirname
);
1444 filename
= savestring (text
);
1447 dirname
= savestring (text
);
1449 temp
= strrchr (dirname
, '/');
1453 strcpy (filename
, ++temp
);
1462 /* We aren't done yet. We also support the "~user" syntax. */
1464 /* Save the version of the directory that the user typed. */
1465 users_dirname
= savestring (dirname
);
1467 if (*dirname
== '~')
1469 temp
= tilde_expand (dirname
);
1474 if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&dirname
))
1476 free (users_dirname
);
1477 users_dirname
= savestring (dirname
);
1480 directory
= opendir (dirname
);
1481 filename_len
= strlen (filename
);
1483 rl_filename_completion_desired
= 1;
1486 /* At this point we should entertain the possibility of hacking wildcarded
1487 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
1488 contains globbing characters, then build an array of directories, and
1489 then map over that list while completing. */
1490 /* *** UNIMPLEMENTED *** */
1492 /* Now that we have some state, we can read the directory. */
1494 entry
= (struct dirent
*)NULL
;
1495 while (directory
&& (entry
= readdir (directory
)))
1497 /* Special case for no filename.
1498 All entries except "." and ".." match. */
1499 if (filename_len
== 0)
1501 if (entry
->d_name
[0] != '.' ||
1502 (entry
->d_name
[1] &&
1503 (entry
->d_name
[1] != '.' || entry
->d_name
[2])))
1508 /* Otherwise, if these match up to the length of filename, then
1510 if (_rl_completion_case_fold
)
1512 if ((_rl_to_lower (entry
->d_name
[0]) == _rl_to_lower (filename
[0])) &&
1513 (((int)D_NAMLEN (entry
)) >= filename_len
) &&
1514 (_rl_strnicmp (filename
, entry
->d_name
, filename_len
) == 0))
1519 if ((entry
->d_name
[0] == filename
[0]) &&
1520 (((int)D_NAMLEN (entry
)) >= filename_len
) &&
1521 (strncmp (filename
, entry
->d_name
, filename_len
) == 0))
1531 closedir (directory
);
1532 directory
= (DIR *)NULL
;
1537 dirname
= (char *)NULL
;
1542 filename
= (char *)NULL
;
1546 free (users_dirname
);
1547 users_dirname
= (char *)NULL
;
1550 return (char *)NULL
;
1554 /* dirname && (strcmp (dirname, ".") != 0) */
1555 if (dirname
&& (dirname
[0] != '.' || dirname
[1]))
1557 if (rl_complete_with_tilde_expansion
&& *users_dirname
== '~')
1559 dirlen
= strlen (dirname
);
1560 temp
= xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
1561 strcpy (temp
, dirname
);
1562 /* Canonicalization cuts off any final slash present. We
1563 may need to add it back. */
1564 if (dirname
[dirlen
- 1] != '/')
1566 temp
[dirlen
++] = '/';
1567 temp
[dirlen
] = '\0';
1572 dirlen
= strlen (users_dirname
);
1573 temp
= xmalloc (1 + dirlen
+ D_NAMLEN (entry
));
1574 strcpy (temp
, users_dirname
);
1577 strcpy (temp
+ dirlen
, entry
->d_name
); /* strcat (temp, entry->d_name); */
1580 temp
= savestring (entry
->d_name
);
1586 /* An initial implementation of a menu completion function a la tcsh. The
1587 first time (if the last readline command was not rl_menu_complete), we
1588 generate the list of matches. This code is very similar to the code in
1589 rl_complete_internal -- there should be a way to combine the two. Then,
1590 for each item in the list of matches, we insert the match in an undoable
1591 fashion, with the appropriate character appended (this happens on the
1592 second and subsequent consecutive calls to rl_menu_complete). When we
1593 hit the end of the match list, we restore the original unmatched text,
1594 ring the bell, and reset the counter to zero. */
1596 rl_menu_complete (count
, ignore
)
1600 int matching_filenames
, found_quote
;
1602 static char *orig_text
;
1603 static char **matches
= (char **)0;
1604 static int match_list_index
= 0;
1605 static int match_list_size
= 0;
1606 static int orig_start
, orig_end
;
1607 static char quote_char
;
1608 static int delimiter
;
1610 /* The first time through, we generate the list of matches and set things
1611 up to insert them. */
1612 if (rl_last_func
!= rl_menu_complete
)
1614 /* Clean up from previous call, if any. */
1618 for (match_list_index
= 0; matches
[match_list_index
]; match_list_index
++)
1619 free (matches
[match_list_index
]);
1623 match_list_index
= match_list_size
= 0;
1624 matches
= (char **)NULL
;
1626 /* Only the completion entry function can change these. */
1627 rl_filename_completion_desired
= 0;
1628 rl_filename_quoting_desired
= 1;
1629 rl_completion_type
= '%';
1631 our_func
= rl_completion_entry_function
1632 ? rl_completion_entry_function
1633 : (Function
*)filename_completion_function
;
1635 /* We now look backwards for the start of a filename/variable word. */
1636 orig_end
= rl_point
;
1637 found_quote
= delimiter
= 0;
1641 /* This (possibly) changes rl_point. If it returns a non-zero char,
1642 we know we have an open quote. */
1643 quote_char
= find_completion_word (&found_quote
, &delimiter
);
1645 orig_start
= rl_point
;
1646 rl_point
= orig_end
;
1648 orig_text
= rl_copy_text (orig_start
, orig_end
);
1649 matches
= gen_completion_matches (orig_text
, orig_start
, orig_end
,
1650 our_func
, found_quote
, quote_char
);
1652 /* If we are matching filenames, our_func will have been set to
1653 filename_completion_function */
1654 matching_filenames
= our_func
== (Function
*)filename_completion_function
;
1655 if (matches
== 0 || postprocess_matches (orig_text
, &matches
, matching_filenames
) == 0)
1659 matches
= (char **)0;
1661 orig_text
= (char *)0;
1662 completion_changed_buffer
= 0;
1666 for (match_list_size
= 0; matches
[match_list_size
]; match_list_size
++)
1668 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
1669 code below should take care of it. */
1672 /* Now we have the list of matches. Replace the text between
1673 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
1674 matches[match_list_index], and add any necessary closing char. */
1676 if (matches
== 0 || match_list_size
== 0)
1680 matches
= (char **)0;
1681 completion_changed_buffer
= 0;
1685 match_list_index
= (match_list_index
+ count
) % match_list_size
;
1686 if (match_list_index
< 0)
1687 match_list_index
+= match_list_size
;
1689 if (match_list_index
== 0)
1692 insert_match (orig_text
, orig_start
, MULT_MATCH
, "e_char
);
1696 insert_match (matches
[match_list_index
], orig_start
, SINGLE_MATCH
, "e_char
);
1697 append_to_match (matches
[match_list_index
], delimiter
, quote_char
);
1700 completion_changed_buffer
= 1;