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 2, 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 59 Temple Place, Suite 330, Boston, MA 02111 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 */
54 #include "posixstat.h"
56 /* System-specific feature definitions and include files. */
60 /* Some standard library routines. */
63 #include "rlprivate.h"
66 typedef int QSFUNC (const void *, const void *);
68 typedef int QSFUNC ();
77 /* Unix version of a hidden file. Could be different on other systems. */
78 #define HIDDEN_FILE(fname) ((fname)[0] == '.')
80 /* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
82 #if !defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE)
83 extern struct passwd
*getpwent
PARAMS((void));
84 #endif /* !HAVE_GETPW_DECLS || _POSIX_SOURCE */
86 /* If non-zero, then this is the address of a function to call when
87 completing a word would normally display the list of possible matches.
88 This function is called instead of actually doing the display.
89 It takes three arguments: (char **matches, int num_matches, int max_length)
90 where MATCHES is the array of strings that matched, NUM_MATCHES is the
91 number of strings in that array, and MAX_LENGTH is the length of the
92 longest string in that array. */
93 rl_compdisp_func_t
*rl_completion_display_matches_hook
= (rl_compdisp_func_t
*)NULL
;
95 #if defined (VISIBLE_STATS)
99 static int stat_char
PARAMS((char *));
102 static char *rl_quote_filename
PARAMS((char *, int, char *));
104 static void set_completion_defaults
PARAMS((int));
105 static int get_y_or_n
PARAMS((int));
106 static int _rl_internal_pager
PARAMS((int));
107 static char *printable_part
PARAMS((char *));
108 static int print_filename
PARAMS((char *, char *));
110 static char **gen_completion_matches
PARAMS((char *, int, int, rl_compentry_func_t
*, int, int));
112 static char **remove_duplicate_matches
PARAMS((char **));
113 static void insert_match
PARAMS((char *, int, int, char *));
114 static int append_to_match
PARAMS((char *, int, int, int));
115 static void insert_all_matches
PARAMS((char **, int, char *));
116 static void display_matches
PARAMS((char **));
117 static int compute_lcd_of_matches
PARAMS((char **, int, const char *));
118 static int postprocess_matches
PARAMS((char ***, int));
120 static char *make_quoted_replacement
PARAMS((char *, int, char *));
122 /* **************************************************************** */
124 /* Completion matching, from readline's point of view. */
126 /* **************************************************************** */
128 /* Variables known only to the readline library. */
130 /* If non-zero, non-unique completions always show the list of matches. */
131 int _rl_complete_show_all
= 0;
133 /* If non-zero, completed directory names have a slash appended. */
134 int _rl_complete_mark_directories
= 1;
136 /* If non-zero, the symlinked directory completion behavior introduced in
137 readline-4.2a is disabled, and symlinks that point to directories have
138 a slash appended (subject to the value of _rl_complete_mark_directories).
139 This is user-settable via the mark-symlinked-directories variable. */
140 int _rl_complete_mark_symlink_dirs
= 0;
142 /* If non-zero, completions are printed horizontally in alphabetical order,
144 int _rl_print_completions_horizontally
;
146 /* Non-zero means that case is not significant in filename completion. */
147 #if defined (__MSDOS__) && !defined (__DJGPP__)
148 int _rl_completion_case_fold
= 1;
150 int _rl_completion_case_fold
;
153 /* If non-zero, don't match hidden files (filenames beginning with a `.' on
154 Unix) when doing filename completion. */
155 int _rl_match_hidden_files
= 1;
157 /* Global variables available to applications using readline. */
159 #if defined (VISIBLE_STATS)
160 /* Non-zero means add an additional character to each filename displayed
161 during listing completion iff rl_filename_completion_desired which helps
162 to indicate the type of file being listed. */
163 int rl_visible_stats
= 0;
164 #endif /* VISIBLE_STATS */
166 /* If non-zero, then this is the address of a function to call when
167 completing on a directory name. The function is called with
168 the address of a string (the current directory name) as an arg. */
169 rl_icppfunc_t
*rl_directory_completion_hook
= (rl_icppfunc_t
*)NULL
;
171 rl_icppfunc_t
*rl_directory_rewrite_hook
= (rl_icppfunc_t
*)NULL
;
173 /* Non-zero means readline completion functions perform tilde expansion. */
174 int rl_complete_with_tilde_expansion
= 0;
176 /* Pointer to the generator function for completion_matches ().
177 NULL means to use rl_filename_completion_function (), the default filename
179 rl_compentry_func_t
*rl_completion_entry_function
= (rl_compentry_func_t
*)NULL
;
181 /* Pointer to alternative function to create matches.
182 Function is called with TEXT, START, and END.
183 START and END are indices in RL_LINE_BUFFER saying what the boundaries
185 If this function exists and returns NULL then call the value of
186 rl_completion_entry_function to try to match, otherwise use the
187 array of strings returned. */
188 rl_completion_func_t
*rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
190 /* Non-zero means to suppress normal filename completion after the
191 user-specified completion function has been called. */
192 int rl_attempted_completion_over
= 0;
194 /* Set to a character indicating the type of completion being performed
195 by rl_complete_internal, available for use by application completion
197 int rl_completion_type
= 0;
199 /* Up to this many items will be displayed in response to a
200 possible-completions call. After that, we ask the user if
201 she is sure she wants to see them all. */
202 int rl_completion_query_items
= 100;
204 int _rl_page_completions
= 1;
206 /* The basic list of characters that signal a break between words for the
207 completer routine. The contents of this variable is what breaks words
208 in the shell, i.e. " \t\n\"\\'`@$><=" */
209 const char *rl_basic_word_break_characters
= " \t\n\"\\'`@$><=;|&{("; /* }) */
211 /* List of basic quoting characters. */
212 const char *rl_basic_quote_characters
= "\"'";
214 /* The list of characters that signal a break between words for
215 rl_complete_internal. The default list is the contents of
216 rl_basic_word_break_characters. */
217 const char *rl_completer_word_break_characters
= (const char *)NULL
;
219 /* List of characters which can be used to quote a substring of the line.
220 Completion occurs on the entire substring, and within the substring
221 rl_completer_word_break_characters are treated as any other character,
222 unless they also appear within this list. */
223 const char *rl_completer_quote_characters
= (const char *)NULL
;
225 /* List of characters that should be quoted in filenames by the completer. */
226 const char *rl_filename_quote_characters
= (const char *)NULL
;
228 /* List of characters that are word break characters, but should be left
229 in TEXT when it is passed to the completion function. The shell uses
230 this to help determine what kind of completing to do. */
231 const char *rl_special_prefixes
= (const char *)NULL
;
233 /* If non-zero, then disallow duplicates in the matches. */
234 int rl_ignore_completion_duplicates
= 1;
236 /* Non-zero means that the results of the matches are to be treated
237 as filenames. This is ALWAYS zero on entry, and can only be changed
238 within a completion entry finder function. */
239 int rl_filename_completion_desired
= 0;
241 /* Non-zero means that the results of the matches are to be quoted using
242 double quotes (or an application-specific quoting mechanism) if the
243 filename contains any characters in rl_filename_quote_chars. This is
244 ALWAYS non-zero on entry, and can only be changed within a completion
245 entry finder function. */
246 int rl_filename_quoting_desired
= 1;
248 /* This function, if defined, is called by the completer when real
249 filename completion is done, after all the matching names have been
250 generated. It is passed a (char**) known as matches in the code below.
251 It consists of a NULL-terminated array of pointers to potential
252 matching strings. The 1st element (matches[0]) is the maximal
253 substring that is common to all matches. This function can re-arrange
254 the list of matches as required, but all elements of the array must be
255 free()'d if they are deleted. The main intent of this function is
256 to implement FIGNORE a la SunOS csh. */
257 rl_compignore_func_t
*rl_ignore_some_completions_function
= (rl_compignore_func_t
*)NULL
;
259 /* Set to a function to quote a filename in an application-specific fashion.
260 Called with the text to quote, the type of match found (single or multiple)
261 and a pointer to the quoting character to be used, which the function can
263 rl_quote_func_t
*rl_filename_quoting_function
= rl_quote_filename
;
265 /* Function to call to remove quoting characters from a filename. Called
266 before completion is attempted, so the embedded quotes do not interfere
267 with matching names in the file system. Readline doesn't do anything
268 with this; it's set only by applications. */
269 rl_dequote_func_t
*rl_filename_dequoting_function
= (rl_dequote_func_t
*)NULL
;
271 /* Function to call to decide whether or not a word break character is
272 quoted. If a character is quoted, it does not break words for the
274 rl_linebuf_func_t
*rl_char_is_quoted_p
= (rl_linebuf_func_t
*)NULL
;
276 /* If non-zero, the completion functions don't append anything except a
277 possible closing quote. This is set to 0 by rl_complete_internal and
278 may be changed by an application-specific completion function. */
279 int rl_completion_suppress_append
= 0;
281 /* Character appended to completed words when at the end of the line. The
282 default is a space. */
283 int rl_completion_append_character
= ' ';
285 /* If non-zero, a slash will be appended to completed filenames that are
286 symbolic links to directory names, subject to the value of the
287 mark-directories variable (which is user-settable). This exists so
288 that application completion functions can override the user's preference
289 (set via the mark-symlinked-directories variable) if appropriate.
290 It's set to the value of _rl_complete_mark_symlink_dirs in
291 rl_complete_internal before any application-specific completion
292 function is called, so without that function doing anything, the user's
293 preferences are honored. */
294 int rl_completion_mark_symlink_dirs
;
296 /* If non-zero, inhibit completion (temporarily). */
297 int rl_inhibit_completion
;
299 /* Variables local to this file. */
301 /* Local variable states what happened during the last completion attempt. */
302 static int completion_changed_buffer
;
304 /*************************************/
306 /* Bindable completion functions */
308 /*************************************/
310 /* Complete the word at or before point. You have supplied the function
311 that does the initial simple matching selection algorithm (see
312 rl_completion_matches ()). The default is to do filename completion. */
314 rl_complete (ignore
, invoking_key
)
315 int ignore
, invoking_key
;
317 if (rl_inhibit_completion
)
318 return (_rl_insert_char (ignore
, invoking_key
));
319 else if (rl_last_func
== rl_complete
&& !completion_changed_buffer
)
320 return (rl_complete_internal ('?'));
321 else if (_rl_complete_show_all
)
322 return (rl_complete_internal ('!'));
324 return (rl_complete_internal (TAB
));
327 /* List the possible completions. See description of rl_complete (). */
329 rl_possible_completions (ignore
, invoking_key
)
330 int ignore
, invoking_key
;
332 return (rl_complete_internal ('?'));
336 rl_insert_completions (ignore
, invoking_key
)
337 int ignore
, invoking_key
;
339 return (rl_complete_internal ('*'));
342 /* Return the correct value to pass to rl_complete_internal performing
343 the same tests as rl_complete. This allows consecutive calls to an
344 application's completion function to list possible completions and for
345 an application-specific completion function to honor the
346 show-all-if-ambiguous readline variable. */
348 rl_completion_mode (cfunc
)
349 rl_command_func_t
*cfunc
;
351 if (rl_last_func
== cfunc
&& !completion_changed_buffer
)
353 else if (_rl_complete_show_all
)
359 /************************************/
361 /* Completion utility functions */
363 /************************************/
365 /* Set default values for readline word completion. These are the variables
366 that application completion functions can change or inspect. */
368 set_completion_defaults (what_to_do
)
371 /* Only the completion entry function can change these. */
372 rl_filename_completion_desired
= 0;
373 rl_filename_quoting_desired
= 1;
374 rl_completion_type
= what_to_do
;
375 rl_completion_suppress_append
= 0;
377 /* The completion entry function may optionally change this. */
378 rl_completion_mark_symlink_dirs
= _rl_complete_mark_symlink_dirs
;
381 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
383 get_y_or_n (for_pager
)
390 RL_SETSTATE(RL_STATE_MOREINPUT
);
392 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
394 if (c
== 'y' || c
== 'Y' || c
== ' ')
396 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
399 _rl_abort_internal ();
400 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
402 if (for_pager
&& (c
== 'q' || c
== 'Q'))
409 _rl_internal_pager (lines
)
414 fprintf (rl_outstream
, "--More--");
415 fflush (rl_outstream
);
417 _rl_erase_entire_line ();
426 #if defined (VISIBLE_STATS)
427 /* Return the character which best describes FILENAME.
428 `@' for symbolic links
433 `%' for character special devices
434 `#' for block special devices */
442 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
443 r
= lstat (filename
, &finfo
);
445 r
= stat (filename
, &finfo
);
452 if (S_ISDIR (finfo
.st_mode
))
454 #if defined (S_ISCHR)
455 else if (S_ISCHR (finfo
.st_mode
))
458 #if defined (S_ISBLK)
459 else if (S_ISBLK (finfo
.st_mode
))
462 #if defined (S_ISLNK)
463 else if (S_ISLNK (finfo
.st_mode
))
466 #if defined (S_ISSOCK)
467 else if (S_ISSOCK (finfo
.st_mode
))
469 #endif /* S_ISSOCK */
470 #if defined (S_ISFIFO)
471 else if (S_ISFIFO (finfo
.st_mode
))
474 else if (S_ISREG (finfo
.st_mode
))
476 if (access (filename
, X_OK
) == 0)
481 #endif /* VISIBLE_STATS */
483 /* Return the portion of PATHNAME that should be output when listing
484 possible completions. If we are hacking filename completion, we
485 are only interested in the basename, the portion following the
486 final slash. Otherwise, we return what we were passed. Since
487 printing empty strings is not very informative, if we're doing
488 filename completion, and the basename is the empty string, we look
489 for the previous slash and return the portion following that. If
490 there's no previous slash, we just return what we were passed. */
492 printable_part (pathname
)
497 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
500 temp
= strrchr (pathname
, '/');
501 #if defined (__MSDOS__)
502 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
506 if (temp
== 0 || *temp
== '\0')
508 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
509 Look for a previous slash and, if one is found, return the portion
510 following that slash. If there's no previous slash, just return the
511 pathname we were passed. */
512 else if (temp
[1] == '\0')
514 for (x
= temp
- 1; x
> pathname
; x
--)
517 return ((*x
== '/') ? x
+ 1 : pathname
);
523 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
524 are using it, check for and output a single character for `special'
525 filenames. Return the number of characters we output. */
531 putc ('^', rl_outstream); \
532 putc (UNCTRL (c), rl_outstream); \
535 else if (c == RUBOUT) \
537 putc ('^', rl_outstream); \
538 putc ('?', rl_outstream); \
543 putc (c, rl_outstream); \
549 print_filename (to_print
, full_pathname
)
550 char *to_print
, *full_pathname
;
553 #if !defined (VISIBLE_STATS)
556 for (s
= to_print
; *s
; s
++)
561 char *s
, c
, *new_full_pathname
;
562 int extension_char
, slen
, tlen
;
564 for (s
= to_print
; *s
; s
++)
569 if (rl_filename_completion_desired
&& rl_visible_stats
)
571 /* If to_print != full_pathname, to_print is the basename of the
572 path passed. In this case, we try to expand the directory
573 name before checking for the stat character. */
574 if (to_print
!= full_pathname
)
576 /* Terminate the directory name. */
580 /* If setting the last slash in full_pathname to a NUL results in
581 full_pathname being the empty string, we are trying to complete
582 files in the root directory. If we pass a null string to the
583 bash directory completion hook, for example, it will expand it
584 to the current directory. We just want the `/'. */
585 s
= tilde_expand (full_pathname
&& *full_pathname
? full_pathname
: "/");
586 if (rl_directory_completion_hook
)
587 (*rl_directory_completion_hook
) (&s
);
590 tlen
= strlen (to_print
);
591 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
592 strcpy (new_full_pathname
, s
);
593 new_full_pathname
[slen
] = '/';
594 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
596 extension_char
= stat_char (new_full_pathname
);
598 free (new_full_pathname
);
603 s
= tilde_expand (full_pathname
);
604 extension_char
= stat_char (s
);
610 putc (extension_char
, rl_outstream
);
614 #endif /* VISIBLE_STATS */
619 rl_quote_filename (s
, rtype
, qcp
)
626 r
= (char *)xmalloc (strlen (s
) + 2);
627 *r
= *rl_completer_quote_characters
;
630 *qcp
= *rl_completer_quote_characters
;
634 /* Find the bounds of the current word for completion purposes, and leave
635 rl_point set to the end of the word. This function skips quoted
636 substrings (characters between matched pairs of characters in
637 rl_completer_quote_characters). First we try to find an unclosed
638 quoted substring on which to do matching. If one is not found, we use
639 the word break characters to find the boundaries of the current word.
640 We call an application-specific function to decide whether or not a
641 particular word break character is quoted; if that function returns a
642 non-zero result, the character does not break a word. This function
643 returns the opening quote character if we found an unclosed quoted
644 substring, '\0' otherwise. FP, if non-null, is set to a value saying
645 which (shell-like) quote characters we found (single quote, double
646 quote, or backslash) anywhere in the string. DP, if non-null, is set to
647 the value of the delimiter character that caused a word break. */
650 _rl_find_completion_word (fp
, dp
)
653 int scan
, end
, found_quote
, delimiter
, pass_next
, isbrk
;
657 found_quote
= delimiter
= 0;
660 if (rl_completer_quote_characters
)
662 /* We have a list of characters which can be used in pairs to
663 quote substrings for the completer. Try to find the start
664 of an unclosed quoted substring. */
665 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
666 for (scan
= pass_next
= 0; scan
< end
; scan
++)
674 /* Shell-like semantics for single quotes -- don't allow backslash
675 to quote anything in single quotes, especially not the closing
676 quote. If you don't like this, take out the check on the value
678 if (quote_char
!= '\'' && rl_line_buffer
[scan
] == '\\')
681 found_quote
|= RL_QF_BACKSLASH
;
685 if (quote_char
!= '\0')
687 /* Ignore everything until the matching close quote char. */
688 if (rl_line_buffer
[scan
] == quote_char
)
690 /* Found matching close. Abandon this substring. */
695 else if (strchr (rl_completer_quote_characters
, rl_line_buffer
[scan
]))
697 /* Found start of a quoted substring. */
698 quote_char
= rl_line_buffer
[scan
];
700 /* Shell-like quoting conventions. */
701 if (quote_char
== '\'')
702 found_quote
|= RL_QF_SINGLE_QUOTE
;
703 else if (quote_char
== '"')
704 found_quote
|= RL_QF_DOUBLE_QUOTE
;
706 found_quote
|= RL_QF_OTHER_QUOTE
;
711 if (rl_point
== end
&& quote_char
== '\0')
713 /* We didn't find an unclosed quoted substring upon which to do
714 completion, so use the word break characters to find the
715 substring on which to complete. */
716 #if defined (HANDLE_MULTIBYTE)
717 while (rl_point
= _rl_find_prev_mbchar (rl_line_buffer
, rl_point
, MB_FIND_ANY
))
722 scan
= rl_line_buffer
[rl_point
];
724 if (strchr (rl_completer_word_break_characters
, scan
) == 0)
727 /* Call the application-specific function to tell us whether
728 this word break character is quoted and should be skipped. */
729 if (rl_char_is_quoted_p
&& found_quote
&&
730 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
))
733 /* Convoluted code, but it avoids an n^2 algorithm with calls
734 to char_is_quoted. */
739 /* If we are at an unquoted word break, then advance past it. */
740 scan
= rl_line_buffer
[rl_point
];
742 /* If there is an application-specific function to say whether or not
743 a character is quoted and we found a quote character, let that
744 function decide whether or not a character is a word break, even
745 if it is found in rl_completer_word_break_characters. Don't bother
746 if we're at the end of the line, though. */
749 if (rl_char_is_quoted_p
)
750 isbrk
= (found_quote
== 0 ||
751 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
) == 0) &&
752 strchr (rl_completer_word_break_characters
, scan
) != 0;
754 isbrk
= strchr (rl_completer_word_break_characters
, scan
) != 0;
758 /* If the character that caused the word break was a quoting
759 character, then remember it as the delimiter. */
760 if (rl_basic_quote_characters
&&
761 strchr (rl_basic_quote_characters
, scan
) &&
762 (end
- rl_point
) > 1)
765 /* If the character isn't needed to determine something special
766 about what kind of completion to perform, then advance past it. */
767 if (rl_special_prefixes
== 0 || strchr (rl_special_prefixes
, scan
) == 0)
781 gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
)
784 rl_compentry_func_t
*our_func
;
785 int found_quote
, quote_char
;
787 char **matches
, *temp
;
789 /* If the user wants to TRY to complete, but then wants to give
790 up and use the default completion function, they set the
791 variable rl_attempted_completion_function. */
792 if (rl_attempted_completion_function
)
794 matches
= (*rl_attempted_completion_function
) (text
, start
, end
);
796 if (matches
|| rl_attempted_completion_over
)
798 rl_attempted_completion_over
= 0;
803 /* Beware -- we're stripping the quotes here. Do this only if we know
804 we are doing filename completion and the application has defined a
805 filename dequoting function. */
808 if (found_quote
&& our_func
== rl_filename_completion_function
&&
809 rl_filename_dequoting_function
)
811 /* delete single and double quotes */
812 temp
= (*rl_filename_dequoting_function
) (text
, quote_char
);
813 text
= temp
; /* not freeing text is not a memory leak */
816 matches
= rl_completion_matches (text
, our_func
);
821 /* Filter out duplicates in MATCHES. This frees up the strings in
824 remove_duplicate_matches (matches
)
832 /* Sort the items. */
833 for (i
= 0; matches
[i
]; i
++)
836 /* Sort the array without matches[0], since we need it to
837 stay in place no matter what. */
839 qsort (matches
+1, i
-1, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
841 /* Remember the lowest common denominator for it may be unique. */
842 lowest_common
= savestring (matches
[0]);
844 for (i
= newlen
= 0; matches
[i
+ 1]; i
++)
846 if (strcmp (matches
[i
], matches
[i
+ 1]) == 0)
849 matches
[i
] = (char *)&dead_slot
;
855 /* We have marked all the dead slots with (char *)&dead_slot.
856 Copy all the non-dead entries into a new array. */
857 temp_array
= (char **)xmalloc ((3 + newlen
) * sizeof (char *));
858 for (i
= j
= 1; matches
[i
]; i
++)
860 if (matches
[i
] != (char *)&dead_slot
)
861 temp_array
[j
++] = matches
[i
];
863 temp_array
[j
] = (char *)NULL
;
865 if (matches
[0] != (char *)&dead_slot
)
868 /* Place the lowest common denominator back in [0]. */
869 temp_array
[0] = lowest_common
;
871 /* If there is one string left, and it is identical to the
872 lowest common denominator, then the LCD is the string to
874 if (j
== 2 && strcmp (temp_array
[0], temp_array
[1]) == 0)
876 free (temp_array
[1]);
877 temp_array
[1] = (char *)NULL
;
882 /* Find the common prefix of the list of matches, and put it into
885 compute_lcd_of_matches (match_list
, matches
, text
)
890 register int i
, c1
, c2
, si
;
891 int low
; /* Count of max-matched characters. */
892 #if defined (HANDLE_MULTIBYTE)
898 /* If only one match, just use that. Otherwise, compare each
899 member of the list with the next, finding out where they
903 match_list
[0] = match_list
[1];
904 match_list
[1] = (char *)NULL
;
908 for (i
= 1, low
= 100000; i
< matches
; i
++)
910 #if defined (HANDLE_MULTIBYTE)
911 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
913 memset (&ps1
, 0, sizeof (mbstate_t));
914 memset (&ps2
, 0, sizeof (mbstate_t));
917 if (_rl_completion_case_fold
)
920 (c1
= _rl_to_lower(match_list
[i
][si
])) &&
921 (c2
= _rl_to_lower(match_list
[i
+ 1][si
]));
923 #if defined (HANDLE_MULTIBYTE)
924 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
926 v
= mbrtowc (&wc1
, match_list
[i
]+si
, strlen (match_list
[i
]+si
), &ps1
);
927 mbrtowc (&wc2
, match_list
[i
+1]+si
, strlen (match_list
[i
+1]+si
), &ps2
);
928 wc1
= towlower (wc1
);
929 wc2
= towlower (wc2
);
943 (c1
= match_list
[i
][si
]) &&
944 (c2
= match_list
[i
+ 1][si
]);
946 #if defined (HANDLE_MULTIBYTE)
947 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
949 mbstate_t ps_back
= ps1
;
950 if (!_rl_compare_chars (match_list
[i
], si
, &ps1
, match_list
[i
+1], si
, &ps2
))
952 else if ((v
= _rl_get_char_len (&match_list
[i
][si
], &ps_back
)) > 1)
965 /* If there were multiple matches, but none matched up to even the
966 first character, and the user typed something, use that as the
967 value of matches[0]. */
968 if (low
== 0 && text
&& *text
)
970 match_list
[0] = (char *)xmalloc (strlen (text
) + 1);
971 strcpy (match_list
[0], text
);
975 match_list
[0] = (char *)xmalloc (low
+ 1);
977 /* XXX - this might need changes in the presence of multibyte chars */
979 /* If we are ignoring case, try to preserve the case of the string
980 the user typed in the face of multiple matches differing in case. */
981 if (_rl_completion_case_fold
)
983 /* sort the list to get consistent answers. */
984 qsort (match_list
+1, matches
, sizeof(char *), (QSFUNC
*)_rl_qsort_string_compare
);
989 for (i
= 1; i
<= matches
; i
++)
990 if (strncmp (match_list
[i
], text
, si
) == 0)
992 strncpy (match_list
[0], match_list
[i
], low
);
995 /* no casematch, use first entry */
997 strncpy (match_list
[0], match_list
[1], low
);
1000 /* otherwise, just use the text the user typed. */
1001 strncpy (match_list
[0], text
, low
);
1004 strncpy (match_list
[0], match_list
[1], low
);
1006 match_list
[0][low
] = '\0';
1013 postprocess_matches (matchesp
, matching_filenames
)
1015 int matching_filenames
;
1017 char *t
, **matches
, **temp_matches
;
1020 matches
= *matchesp
;
1025 /* It seems to me that in all the cases we handle we would like
1026 to ignore duplicate possiblilities. Scan for the text to
1027 insert being identical to the other completions. */
1028 if (rl_ignore_completion_duplicates
)
1030 temp_matches
= remove_duplicate_matches (matches
);
1032 matches
= temp_matches
;
1035 /* If we are matching filenames, then here is our chance to
1036 do clever processing by re-examining the list. Call the
1037 ignore function with the array as a parameter. It can
1038 munge the array, deleting matches as it desires. */
1039 if (rl_ignore_some_completions_function
&& matching_filenames
)
1041 for (nmatch
= 1; matches
[nmatch
]; nmatch
++)
1043 (void)(*rl_ignore_some_completions_function
) (matches
);
1044 if (matches
== 0 || matches
[0] == 0)
1047 *matchesp
= (char **)0;
1052 /* If we removed some matches, recompute the common prefix. */
1053 for (i
= 1; matches
[i
]; i
++)
1055 if (i
> 1 && i
< nmatch
)
1058 compute_lcd_of_matches (matches
, i
- 1, t
);
1064 *matchesp
= matches
;
1068 /* A convenience function for displaying a list of strings in
1069 columnar format on readline's output stream. MATCHES is the list
1070 of strings, in argv format, LEN is the number of strings in MATCHES,
1071 and MAX is the length of the longest string in MATCHES. */
1073 rl_display_match_list (matches
, len
, max
)
1077 int count
, limit
, printed_len
, lines
;
1081 /* How many items of MAX length can we fit in the screen window? */
1083 limit
= _rl_screenwidth
/ max
;
1084 if (limit
!= 1 && (limit
* max
== _rl_screenwidth
))
1087 /* Avoid a possible floating exception. If max > _rl_screenwidth,
1088 limit will be 0 and a divide-by-zero fault will result. */
1092 /* How many iterations of the printing loop? */
1093 count
= (len
+ (limit
- 1)) / limit
;
1095 /* Watch out for special case. If LEN is less than LIMIT, then
1096 just do the inner printing loop.
1097 0 < len <= limit implies count = 1. */
1099 /* Sort the items if they are not already sorted. */
1100 if (rl_ignore_completion_duplicates
== 0)
1101 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1106 if (_rl_print_completions_horizontally
== 0)
1108 /* Print the sorted items, up-and-down alphabetically, like ls. */
1109 for (i
= 1; i
<= count
; i
++)
1111 for (j
= 0, l
= i
; j
< limit
; j
++)
1113 if (l
> len
|| matches
[l
] == 0)
1117 temp
= printable_part (matches
[l
]);
1118 printed_len
= print_filename (temp
, matches
[l
]);
1121 for (k
= 0; k
< max
- printed_len
; k
++)
1122 putc (' ', rl_outstream
);
1128 if (_rl_page_completions
&& lines
>= (_rl_screenheight
- 1) && i
< count
)
1130 lines
= _rl_internal_pager (lines
);
1138 /* Print the sorted items, across alphabetically, like ls -x. */
1139 for (i
= 1; matches
[i
]; i
++)
1141 temp
= printable_part (matches
[i
]);
1142 printed_len
= print_filename (temp
, matches
[i
]);
1143 /* Have we reached the end of this line? */
1146 if (i
&& (limit
> 1) && (i
% limit
) == 0)
1150 if (_rl_page_completions
&& lines
>= _rl_screenheight
- 1)
1152 lines
= _rl_internal_pager (lines
);
1158 for (k
= 0; k
< max
- printed_len
; k
++)
1159 putc (' ', rl_outstream
);
1166 /* Display MATCHES, a list of matching filenames in argv format. This
1167 handles the simple case -- a single match -- first. If there is more
1168 than one match, we compute the number of strings in the list and the
1169 length of the longest string, which will be needed by the display
1170 function. If the application wants to handle displaying the list of
1171 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1172 address of a function, and we just call it. If we're handling the
1173 display ourselves, we just call rl_display_match_list. We also check
1174 that the list of matches doesn't exceed the user-settable threshold,
1175 and ask the user if he wants to see the list if there are more matches
1176 than RL_COMPLETION_QUERY_ITEMS. */
1178 display_matches (matches
)
1184 /* Move to the last visible line of a possibly-multiple-line command. */
1185 _rl_move_vert (_rl_vis_botlin
);
1187 /* Handle simple case first. What if there is only one answer? */
1188 if (matches
[1] == 0)
1190 temp
= printable_part (matches
[0]);
1192 print_filename (temp
, matches
[0]);
1195 rl_forced_update_display ();
1196 rl_display_fixed
= 1;
1201 /* There is more than one answer. Find out how many there are,
1202 and find the maximum printed length of a single entry. */
1203 for (max
= 0, i
= 1; matches
[i
]; i
++)
1205 temp
= printable_part (matches
[i
]);
1206 len
= strlen (temp
);
1214 /* If the caller has defined a display hook, then call that now. */
1215 if (rl_completion_display_matches_hook
)
1217 (*rl_completion_display_matches_hook
) (matches
, len
, max
);
1221 /* If there are many items, then ask the user if she really wants to
1223 if (len
>= rl_completion_query_items
)
1226 fprintf (rl_outstream
, "Display all %d possibilities? (y or n)", len
);
1227 fflush (rl_outstream
);
1228 if (get_y_or_n (0) == 0)
1232 rl_forced_update_display ();
1233 rl_display_fixed
= 1;
1239 rl_display_match_list (matches
, len
, max
);
1241 rl_forced_update_display ();
1242 rl_display_fixed
= 1;
1246 make_quoted_replacement (match
, mtype
, qc
)
1249 char *qc
; /* Pointer to quoting character, if any */
1251 int should_quote
, do_replace
;
1254 /* If we are doing completion on quoted substrings, and any matches
1255 contain any of the completer_word_break_characters, then auto-
1256 matically prepend the substring with a quote character (just pick
1257 the first one from the list of such) if it does not already begin
1258 with a quote string. FIXME: Need to remove any such automatically
1259 inserted quote character when it no longer is necessary, such as
1260 if we change the string we are completing on and the new set of
1261 matches don't require a quoted substring. */
1262 replacement
= match
;
1264 should_quote
= match
&& rl_completer_quote_characters
&&
1265 rl_filename_completion_desired
&&
1266 rl_filename_quoting_desired
;
1269 should_quote
= should_quote
&& (!qc
|| !*qc
||
1270 (rl_completer_quote_characters
&& strchr (rl_completer_quote_characters
, *qc
)));
1274 /* If there is a single match, see if we need to quote it.
1275 This also checks whether the common prefix of several
1276 matches needs to be quoted. */
1277 should_quote
= rl_filename_quote_characters
1278 ? (_rl_strpbrk (match
, rl_filename_quote_characters
) != 0)
1281 do_replace
= should_quote
? mtype
: NO_MATCH
;
1282 /* Quote the replacement, since we found an embedded
1283 word break character in a potential match. */
1284 if (do_replace
!= NO_MATCH
&& rl_filename_quoting_function
)
1285 replacement
= (*rl_filename_quoting_function
) (match
, do_replace
, qc
);
1287 return (replacement
);
1291 insert_match (match
, start
, mtype
, qc
)
1299 oqc
= qc
? *qc
: '\0';
1300 replacement
= make_quoted_replacement (match
, mtype
, qc
);
1302 /* Now insert the match. */
1305 /* Don't double an opening quote character. */
1306 if (qc
&& *qc
&& start
&& rl_line_buffer
[start
- 1] == *qc
&&
1307 replacement
[0] == *qc
)
1309 /* If make_quoted_replacement changed the quoting character, remove
1310 the opening quote and insert the (fully-quoted) replacement. */
1311 else if (qc
&& (*qc
!= oqc
) && start
&& rl_line_buffer
[start
- 1] == oqc
&&
1312 replacement
[0] != oqc
)
1314 _rl_replace_text (replacement
, start
, rl_point
- 1);
1315 if (replacement
!= match
)
1320 /* Append any necessary closing quote and a separator character to the
1321 just-inserted match. If the user has specified that directories
1322 should be marked by a trailing `/', append one of those instead. The
1323 default trailing character is a space. Returns the number of characters
1324 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1325 has them) and don't add a suffix for a symlink to a directory. A
1326 nontrivial match is one that actually adds to the word being completed.
1327 The variable rl_completion_mark_symlink_dirs controls this behavior
1328 (it's initially set to the what the user has chosen, indicated by the
1329 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1330 application's completion function). */
1332 append_to_match (text
, delimiter
, quote_char
, nontrivial_match
)
1334 int delimiter
, quote_char
, nontrivial_match
;
1336 char temp_string
[4], *filename
;
1337 int temp_string_index
, s
;
1340 temp_string_index
= 0;
1341 if (quote_char
&& rl_point
&& rl_line_buffer
[rl_point
- 1] != quote_char
)
1342 temp_string
[temp_string_index
++] = quote_char
;
1345 temp_string
[temp_string_index
++] = delimiter
;
1346 else if (rl_completion_suppress_append
== 0 && rl_completion_append_character
)
1347 temp_string
[temp_string_index
++] = rl_completion_append_character
;
1349 temp_string
[temp_string_index
++] = '\0';
1351 if (rl_filename_completion_desired
)
1353 filename
= tilde_expand (text
);
1354 s
= (nontrivial_match
&& rl_completion_mark_symlink_dirs
== 0)
1355 ? LSTAT (filename
, &finfo
)
1356 : stat (filename
, &finfo
);
1357 if (s
== 0 && S_ISDIR (finfo
.st_mode
))
1359 if (_rl_complete_mark_directories
)
1361 /* This is clumsy. Avoid putting in a double slash if point
1362 is at the end of the line and the previous character is a
1364 if (rl_point
&& rl_line_buffer
[rl_point
] == '\0' && rl_line_buffer
[rl_point
- 1] == '/')
1366 else if (rl_line_buffer
[rl_point
] != '/')
1367 rl_insert_text ("/");
1371 /* Don't add anything if the filename is a symlink and resolves to a
1373 else if (s
== 0 && S_ISLNK (finfo
.st_mode
) &&
1374 stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
))
1379 if (rl_point
== rl_end
&& temp_string_index
)
1380 rl_insert_text (temp_string
);
1386 if (rl_point
== rl_end
&& temp_string_index
)
1387 rl_insert_text (temp_string
);
1390 return (temp_string_index
);
1394 insert_all_matches (matches
, point
, qc
)
1402 rl_begin_undo_group ();
1403 /* remove any opening quote character; make_quoted_replacement will add
1405 if (qc
&& *qc
&& point
&& rl_line_buffer
[point
- 1] == *qc
)
1407 rl_delete_text (point
, rl_point
);
1412 for (i
= 1; matches
[i
]; i
++)
1414 rp
= make_quoted_replacement (matches
[i
], SINGLE_MATCH
, qc
);
1415 rl_insert_text (rp
);
1416 rl_insert_text (" ");
1417 if (rp
!= matches
[i
])
1423 rp
= make_quoted_replacement (matches
[0], SINGLE_MATCH
, qc
);
1424 rl_insert_text (rp
);
1425 rl_insert_text (" ");
1426 if (rp
!= matches
[0])
1429 rl_end_undo_group ();
1433 _rl_free_match_list (matches
)
1441 for (i
= 0; matches
[i
]; i
++)
1446 /* Complete the word at or before point.
1447 WHAT_TO_DO says what to do with the completion.
1448 `?' means list the possible completions.
1449 TAB means do standard completion.
1450 `*' means insert all of the possible completions.
1451 `!' means to do standard completion, and list all possible completions if
1452 there is more than one. */
1454 rl_complete_internal (what_to_do
)
1458 rl_compentry_func_t
*our_func
;
1459 int start
, end
, delimiter
, found_quote
, i
, nontrivial_lcd
;
1460 char *text
, *saved_line_buffer
;
1463 RL_SETSTATE(RL_STATE_COMPLETING
);
1465 set_completion_defaults (what_to_do
);
1467 saved_line_buffer
= rl_line_buffer
? savestring (rl_line_buffer
) : (char *)NULL
;
1468 our_func
= rl_completion_entry_function
1469 ? rl_completion_entry_function
1470 : rl_filename_completion_function
;
1472 /* We now look backwards for the start of a filename/variable word. */
1474 found_quote
= delimiter
= 0;
1478 /* This (possibly) changes rl_point. If it returns a non-zero char,
1479 we know we have an open quote. */
1480 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
1485 text
= rl_copy_text (start
, end
);
1486 matches
= gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
);
1487 /* nontrivial_lcd is set if the common prefix adds something to the word
1489 nontrivial_lcd
= matches
&& strcmp (text
, matches
[0]) != 0;
1495 FREE (saved_line_buffer
);
1496 completion_changed_buffer
= 0;
1497 RL_UNSETSTATE(RL_STATE_COMPLETING
);
1501 /* If we are matching filenames, the attempted completion function will
1502 have set rl_filename_completion_desired to a non-zero value. The basic
1503 rl_filename_completion_function does this. */
1504 i
= rl_filename_completion_desired
;
1506 if (postprocess_matches (&matches
, i
) == 0)
1509 FREE (saved_line_buffer
);
1510 completion_changed_buffer
= 0;
1511 RL_UNSETSTATE(RL_STATE_COMPLETING
);
1519 /* Insert the first match with proper quoting. */
1521 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
1523 /* If there are more matches, ring the bell to indicate.
1524 If we are in vi mode, Posix.2 says to not ring the bell.
1525 If the `show-all-if-ambiguous' variable is set, display
1526 all the matches immediately. Otherwise, if this was the
1527 only match, and we are hacking files, check the file to
1528 see if it was a directory. If so, and the `mark-directories'
1529 variable is set, add a '/' to the name. If not, and we
1530 are at the end of the line, then add a space. */
1533 if (what_to_do
== '!')
1535 display_matches (matches
);
1538 else if (rl_editing_mode
!= vi_mode
)
1539 rl_ding (); /* There are other matches remaining. */
1542 append_to_match (matches
[0], delimiter
, quote_char
, nontrivial_lcd
);
1547 insert_all_matches (matches
, start
, "e_char
);
1551 display_matches (matches
);
1555 fprintf (stderr
, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do
);
1557 FREE (saved_line_buffer
);
1558 RL_UNSETSTATE(RL_STATE_COMPLETING
);
1562 _rl_free_match_list (matches
);
1564 /* Check to see if the line has changed through all of this manipulation. */
1565 if (saved_line_buffer
)
1567 completion_changed_buffer
= strcmp (rl_line_buffer
, saved_line_buffer
) != 0;
1568 free (saved_line_buffer
);
1571 RL_UNSETSTATE(RL_STATE_COMPLETING
);
1575 /***************************************************************/
1577 /* Application-callable completion match generator functions */
1579 /***************************************************************/
1581 /* Return an array of (char *) which is a list of completions for TEXT.
1582 If there are no completions, return a NULL pointer.
1583 The first entry in the returned array is the substitution for TEXT.
1584 The remaining entries are the possible completions.
1585 The array is terminated with a NULL pointer.
1587 ENTRY_FUNCTION is a function of two args, and returns a (char *).
1588 The first argument is TEXT.
1589 The second is a state argument; it should be zero on the first call, and
1590 non-zero on subsequent calls. It returns a NULL pointer to the caller
1591 when there are no more matches.
1594 rl_completion_matches (text
, entry_function
)
1596 rl_compentry_func_t
*entry_function
;
1598 /* Number of slots in match_list. */
1599 int match_list_size
;
1601 /* The list of matches. */
1604 /* Number of matches actually found. */
1607 /* Temporary string binder. */
1611 match_list_size
= 10;
1612 match_list
= (char **)xmalloc ((match_list_size
+ 1) * sizeof (char *));
1613 match_list
[1] = (char *)NULL
;
1615 while (string
= (*entry_function
) (text
, matches
))
1617 if (matches
+ 1 == match_list_size
)
1618 match_list
= (char **)xrealloc
1619 (match_list
, ((match_list_size
+= 10) + 1) * sizeof (char *));
1621 match_list
[++matches
] = string
;
1622 match_list
[matches
+ 1] = (char *)NULL
;
1625 /* If there were any matches, then look through them finding out the
1626 lowest common denominator. That then becomes match_list[0]. */
1628 compute_lcd_of_matches (match_list
, matches
, text
);
1629 else /* There were no matches. */
1632 match_list
= (char **)NULL
;
1634 return (match_list
);
1637 /* A completion function for usernames.
1638 TEXT contains a partial username preceded by a random
1639 character (usually `~'). */
1641 rl_username_completion_function (text
, state
)
1645 #if defined (__WIN32__) || defined (__OPENNT)
1646 return (char *)NULL
;
1647 #else /* !__WIN32__ && !__OPENNT) */
1648 static char *username
= (char *)NULL
;
1649 static struct passwd
*entry
;
1650 static int namelen
, first_char
, first_char_loc
;
1658 first_char_loc
= first_char
== '~';
1660 username
= savestring (&text
[first_char_loc
]);
1661 namelen
= strlen (username
);
1665 while (entry
= getpwent ())
1667 /* Null usernames should result in all users as possible completions. */
1668 if (namelen
== 0 || (STREQN (username
, entry
->pw_name
, namelen
)))
1675 return ((char *)NULL
);
1679 value
= (char *)xmalloc (2 + strlen (entry
->pw_name
));
1683 strcpy (value
+ first_char_loc
, entry
->pw_name
);
1685 if (first_char
== '~')
1686 rl_filename_completion_desired
= 1;
1690 #endif /* !__WIN32__ && !__OPENNT */
1693 /* Okay, now we write the entry_function for filename completion. In the
1694 general case. Note that completion in the shell is a little different
1695 because of all the pathnames that must be followed when looking up the
1696 completion for a command. */
1698 rl_filename_completion_function (text
, state
)
1702 static DIR *directory
= (DIR *)NULL
;
1703 static char *filename
= (char *)NULL
;
1704 static char *dirname
= (char *)NULL
;
1705 static char *users_dirname
= (char *)NULL
;
1706 static int filename_len
;
1709 struct dirent
*entry
;
1711 /* If we don't have any state, then do some initialization. */
1714 /* If we were interrupted before closing the directory or reading
1715 all of its contents, close it. */
1718 closedir (directory
);
1719 directory
= (DIR *)NULL
;
1723 FREE (users_dirname
);
1725 filename
= savestring (text
);
1728 dirname
= savestring (text
);
1730 temp
= strrchr (dirname
, '/');
1732 #if defined (__MSDOS__)
1733 /* special hack for //X/... */
1734 if (dirname
[0] == '/' && dirname
[1] == '/' && ISALPHA ((unsigned char)dirname
[2]) && dirname
[3] == '/')
1735 temp
= strrchr (dirname
+ 3, '/');
1740 strcpy (filename
, ++temp
);
1743 #if defined (__MSDOS__)
1744 /* searches from current directory on the drive */
1745 else if (ISALPHA ((unsigned char)dirname
[0]) && dirname
[1] == ':')
1747 strcpy (filename
, dirname
+ 2);
1757 /* We aren't done yet. We also support the "~user" syntax. */
1759 /* Save the version of the directory that the user typed. */
1760 users_dirname
= savestring (dirname
);
1762 if (*dirname
== '~')
1764 temp
= tilde_expand (dirname
);
1769 if (rl_directory_rewrite_hook
)
1770 (*rl_directory_rewrite_hook
) (&dirname
);
1772 if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&dirname
))
1774 free (users_dirname
);
1775 users_dirname
= savestring (dirname
);
1778 directory
= opendir (dirname
);
1779 filename_len
= strlen (filename
);
1781 rl_filename_completion_desired
= 1;
1784 /* At this point we should entertain the possibility of hacking wildcarded
1785 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
1786 contains globbing characters, then build an array of directories, and
1787 then map over that list while completing. */
1788 /* *** UNIMPLEMENTED *** */
1790 /* Now that we have some state, we can read the directory. */
1792 entry
= (struct dirent
*)NULL
;
1793 while (directory
&& (entry
= readdir (directory
)))
1795 /* Special case for no filename. If the user has disabled the
1796 `match-hidden-files' variable, skip filenames beginning with `.'.
1797 All other entries except "." and ".." match. */
1798 if (filename_len
== 0)
1800 if (_rl_match_hidden_files
== 0 && HIDDEN_FILE (entry
->d_name
))
1803 if (entry
->d_name
[0] != '.' ||
1804 (entry
->d_name
[1] &&
1805 (entry
->d_name
[1] != '.' || entry
->d_name
[2])))
1810 /* Otherwise, if these match up to the length of filename, then
1812 if (_rl_completion_case_fold
)
1814 if ((_rl_to_lower (entry
->d_name
[0]) == _rl_to_lower (filename
[0])) &&
1815 (((int)D_NAMLEN (entry
)) >= filename_len
) &&
1816 (_rl_strnicmp (filename
, entry
->d_name
, filename_len
) == 0))
1821 if ((entry
->d_name
[0] == filename
[0]) &&
1822 (((int)D_NAMLEN (entry
)) >= filename_len
) &&
1823 (strncmp (filename
, entry
->d_name
, filename_len
) == 0))
1833 closedir (directory
);
1834 directory
= (DIR *)NULL
;
1839 dirname
= (char *)NULL
;
1844 filename
= (char *)NULL
;
1848 free (users_dirname
);
1849 users_dirname
= (char *)NULL
;
1852 return (char *)NULL
;
1856 /* dirname && (strcmp (dirname, ".") != 0) */
1857 if (dirname
&& (dirname
[0] != '.' || dirname
[1]))
1859 if (rl_complete_with_tilde_expansion
&& *users_dirname
== '~')
1861 dirlen
= strlen (dirname
);
1862 temp
= (char *)xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
1863 strcpy (temp
, dirname
);
1864 /* Canonicalization cuts off any final slash present. We
1865 may need to add it back. */
1866 if (dirname
[dirlen
- 1] != '/')
1868 temp
[dirlen
++] = '/';
1869 temp
[dirlen
] = '\0';
1874 dirlen
= strlen (users_dirname
);
1875 temp
= (char *)xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
1876 strcpy (temp
, users_dirname
);
1877 /* Make sure that temp has a trailing slash here. */
1878 if (users_dirname
[dirlen
- 1] != '/')
1879 temp
[dirlen
++] = '/';
1882 strcpy (temp
+ dirlen
, entry
->d_name
);
1885 temp
= savestring (entry
->d_name
);
1891 /* An initial implementation of a menu completion function a la tcsh. The
1892 first time (if the last readline command was not rl_menu_complete), we
1893 generate the list of matches. This code is very similar to the code in
1894 rl_complete_internal -- there should be a way to combine the two. Then,
1895 for each item in the list of matches, we insert the match in an undoable
1896 fashion, with the appropriate character appended (this happens on the
1897 second and subsequent consecutive calls to rl_menu_complete). When we
1898 hit the end of the match list, we restore the original unmatched text,
1899 ring the bell, and reset the counter to zero. */
1901 rl_menu_complete (count
, ignore
)
1904 rl_compentry_func_t
*our_func
;
1905 int matching_filenames
, found_quote
;
1907 static char *orig_text
;
1908 static char **matches
= (char **)0;
1909 static int match_list_index
= 0;
1910 static int match_list_size
= 0;
1911 static int orig_start
, orig_end
;
1912 static char quote_char
;
1913 static int delimiter
;
1915 /* The first time through, we generate the list of matches and set things
1916 up to insert them. */
1917 if (rl_last_func
!= rl_menu_complete
)
1919 /* Clean up from previous call, if any. */
1922 _rl_free_match_list (matches
);
1924 match_list_index
= match_list_size
= 0;
1925 matches
= (char **)NULL
;
1927 /* Only the completion entry function can change these. */
1928 set_completion_defaults ('%');
1930 our_func
= rl_completion_entry_function
1931 ? rl_completion_entry_function
1932 : rl_filename_completion_function
;
1934 /* We now look backwards for the start of a filename/variable word. */
1935 orig_end
= rl_point
;
1936 found_quote
= delimiter
= 0;
1940 /* This (possibly) changes rl_point. If it returns a non-zero char,
1941 we know we have an open quote. */
1942 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
1944 orig_start
= rl_point
;
1945 rl_point
= orig_end
;
1947 orig_text
= rl_copy_text (orig_start
, orig_end
);
1948 matches
= gen_completion_matches (orig_text
, orig_start
, orig_end
,
1949 our_func
, found_quote
, quote_char
);
1951 /* If we are matching filenames, the attempted completion function will
1952 have set rl_filename_completion_desired to a non-zero value. The basic
1953 rl_filename_completion_function does this. */
1954 matching_filenames
= rl_filename_completion_desired
;
1956 if (matches
== 0 || postprocess_matches (&matches
, matching_filenames
) == 0)
1960 matches
= (char **)0;
1962 orig_text
= (char *)0;
1963 completion_changed_buffer
= 0;
1967 for (match_list_size
= 0; matches
[match_list_size
]; match_list_size
++)
1969 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
1970 code below should take care of it. */
1973 /* Now we have the list of matches. Replace the text between
1974 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
1975 matches[match_list_index], and add any necessary closing char. */
1977 if (matches
== 0 || match_list_size
== 0)
1981 matches
= (char **)0;
1982 completion_changed_buffer
= 0;
1986 match_list_index
= (match_list_index
+ count
) % match_list_size
;
1987 if (match_list_index
< 0)
1988 match_list_index
+= match_list_size
;
1990 if (match_list_index
== 0 && match_list_size
> 1)
1993 insert_match (orig_text
, orig_start
, MULT_MATCH
, "e_char
);
1997 insert_match (matches
[match_list_index
], orig_start
, SINGLE_MATCH
, "e_char
);
1998 append_to_match (matches
[match_list_index
], delimiter
, quote_char
,
1999 strcmp (orig_text
, matches
[match_list_index
]));
2002 completion_changed_buffer
= 1;