1 /* complete.c -- filename completion for readline. */
3 /* Copyright (C) 1987-2020 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (__TANDEM)
25 # define _XOPEN_SOURCE_EXTENDED 1
28 #if defined (HAVE_CONFIG_H)
32 #include <sys/types.h>
33 #if defined (__TANDEM)
34 # include <sys/stat.h>
37 #if defined (HAVE_SYS_FILE_H)
38 # include <sys/file.h>
43 #if defined (HAVE_UNISTD_H)
45 #endif /* HAVE_UNISTD_H */
47 #if defined (HAVE_STDLIB_H)
50 # include "ansi_stdlib.h"
51 #endif /* HAVE_STDLIB_H */
60 #if defined (HAVE_PWD_H)
65 #include "posixstat.h"
67 /* System-specific feature definitions and include files. */
71 /* Some standard library routines. */
74 #include "rlprivate.h"
76 #if defined (COLOR_SUPPORT)
81 typedef int QSFUNC (const void *, const void *);
83 typedef int QSFUNC ();
92 /* Unix version of a hidden file. Could be different on other systems. */
93 #define HIDDEN_FILE(fname) ((fname)[0] == '.')
95 /* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
97 #if defined (HAVE_GETPWENT) && (!defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE))
98 extern struct passwd
*getpwent
PARAMS((void));
99 #endif /* HAVE_GETPWENT && (!HAVE_GETPW_DECLS || _POSIX_SOURCE) */
101 /* If non-zero, then this is the address of a function to call when
102 completing a word would normally display the list of possible matches.
103 This function is called instead of actually doing the display.
104 It takes three arguments: (char **matches, int num_matches, int max_length)
105 where MATCHES is the array of strings that matched, NUM_MATCHES is the
106 number of strings in that array, and MAX_LENGTH is the length of the
107 longest string in that array. */
108 rl_compdisp_func_t
*rl_completion_display_matches_hook
= (rl_compdisp_func_t
*)NULL
;
110 #if defined (VISIBLE_STATS) || defined (COLOR_SUPPORT)
116 #if defined (VISIBLE_STATS)
117 static int stat_char
PARAMS((char *));
120 #if defined (COLOR_SUPPORT)
121 static int colored_stat_start
PARAMS((const char *));
122 static void colored_stat_end
PARAMS((void));
123 static int colored_prefix_start
PARAMS((void));
124 static void colored_prefix_end
PARAMS((void));
127 static int path_isdir
PARAMS((const char *));
129 static char *rl_quote_filename
PARAMS((char *, int, char *));
131 static void _rl_complete_sigcleanup
PARAMS((int, void *));
133 static void set_completion_defaults
PARAMS((int));
134 static int get_y_or_n
PARAMS((int));
135 static int _rl_internal_pager
PARAMS((int));
136 static char *printable_part
PARAMS((char *));
137 static int fnwidth
PARAMS((const char *));
138 static int fnprint
PARAMS((const char *, int, const char *));
139 static int print_filename
PARAMS((char *, char *, int));
141 static char **gen_completion_matches
PARAMS((char *, int, int, rl_compentry_func_t
*, int, int));
143 static char **remove_duplicate_matches
PARAMS((char **));
144 static void insert_match
PARAMS((char *, int, int, char *));
145 static int append_to_match
PARAMS((char *, int, int, int));
146 static void insert_all_matches
PARAMS((char **, int, char *));
147 static int complete_fncmp
PARAMS((const char *, int, const char *, int));
148 static void display_matches
PARAMS((char **));
149 static int compute_lcd_of_matches
PARAMS((char **, int, const char *));
150 static int postprocess_matches
PARAMS((char ***, int));
151 static int compare_match
PARAMS((char *, const char *));
152 static int complete_get_screenwidth
PARAMS((void));
154 static char *make_quoted_replacement
PARAMS((char *, int, char *));
156 /* **************************************************************** */
158 /* Completion matching, from readline's point of view. */
160 /* **************************************************************** */
162 /* Variables known only to the readline library. */
164 /* If non-zero, non-unique completions always show the list of matches. */
165 int _rl_complete_show_all
= 0;
167 /* If non-zero, non-unique completions show the list of matches, unless it
168 is not possible to do partial completion and modify the line. */
169 int _rl_complete_show_unmodified
= 0;
171 /* If non-zero, completed directory names have a slash appended. */
172 int _rl_complete_mark_directories
= 1;
174 /* If non-zero, the symlinked directory completion behavior introduced in
175 readline-4.2a is disabled, and symlinks that point to directories have
176 a slash appended (subject to the value of _rl_complete_mark_directories).
177 This is user-settable via the mark-symlinked-directories variable. */
178 int _rl_complete_mark_symlink_dirs
= 0;
180 /* If non-zero, completions are printed horizontally in alphabetical order,
182 int _rl_print_completions_horizontally
;
184 /* Non-zero means that case is not significant in filename completion. */
185 #if (defined (__MSDOS__) && !defined (__DJGPP__)) || (defined (_WIN32) && !defined (__CYGWIN__))
186 int _rl_completion_case_fold
= 1;
188 int _rl_completion_case_fold
= 0;
191 /* Non-zero means that `-' and `_' are equivalent when comparing filenames
193 int _rl_completion_case_map
= 0;
195 /* If zero, don't match hidden files (filenames beginning with a `.' on
196 Unix) when doing filename completion. */
197 int _rl_match_hidden_files
= 1;
199 /* Length in characters of a common prefix replaced with an ellipsis (`...')
200 when displaying completion matches. Matches whose printable portion has
201 more than this number of displaying characters in common will have the common
202 display prefix replaced with an ellipsis. */
203 int _rl_completion_prefix_display_length
= 0;
205 /* The readline-private number of screen columns to use when displaying
206 matches. If < 0 or > _rl_screenwidth, it is ignored. */
207 int _rl_completion_columns
= -1;
209 #if defined (COLOR_SUPPORT)
210 /* Non-zero means to use colors to indicate file type when listing possible
211 completions. The colors used are taken from $LS_COLORS, if set. */
212 int _rl_colored_stats
= 0;
214 /* Non-zero means to use a color (currently magenta) to indicate the common
215 prefix of a set of possible word completions. */
216 int _rl_colored_completion_prefix
= 0;
219 /* If non-zero, when completing in the middle of a word, don't insert
220 characters from the match that match characters following point in
221 the word. This means, for instance, completing when the cursor is
222 after the `e' in `Makefile' won't result in `Makefilefile'. */
223 int _rl_skip_completed_text
= 0;
225 /* If non-zero, menu completion displays the common prefix first in the
226 cycle of possible completions instead of the last. */
227 int _rl_menu_complete_prefix_first
= 0;
229 /* Global variables available to applications using readline. */
231 #if defined (VISIBLE_STATS)
232 /* Non-zero means add an additional character to each filename displayed
233 during listing completion iff rl_filename_completion_desired which helps
234 to indicate the type of file being listed. */
235 int rl_visible_stats
= 0;
236 #endif /* VISIBLE_STATS */
238 /* If non-zero, then this is the address of a function to call when
239 completing on a directory name. The function is called with
240 the address of a string (the current directory name) as an arg. */
241 rl_icppfunc_t
*rl_directory_completion_hook
= (rl_icppfunc_t
*)NULL
;
243 rl_icppfunc_t
*rl_directory_rewrite_hook
= (rl_icppfunc_t
*)NULL
;
245 rl_icppfunc_t
*rl_filename_stat_hook
= (rl_icppfunc_t
*)NULL
;
247 /* If non-zero, this is the address of a function to call when reading
248 directory entries from the filesystem for completion and comparing
249 them to the partial word to be completed. The function should
250 either return its first argument (if no conversion takes place) or
251 newly-allocated memory. This can, for instance, convert filenames
252 between character sets for comparison against what's typed at the
253 keyboard. The returned value is what is added to the list of
254 matches. The second argument is the length of the filename to be
256 rl_dequote_func_t
*rl_filename_rewrite_hook
= (rl_dequote_func_t
*)NULL
;
258 /* Non-zero means readline completion functions perform tilde expansion. */
259 int rl_complete_with_tilde_expansion
= 0;
261 /* Pointer to the generator function for completion_matches ().
262 NULL means to use rl_filename_completion_function (), the default filename
264 rl_compentry_func_t
*rl_completion_entry_function
= (rl_compentry_func_t
*)NULL
;
266 /* Pointer to generator function for rl_menu_complete (). NULL means to use
267 *rl_completion_entry_function (see above). */
268 rl_compentry_func_t
*rl_menu_completion_entry_function
= (rl_compentry_func_t
*)NULL
;
270 /* Pointer to alternative function to create matches.
271 Function is called with TEXT, START, and END.
272 START and END are indices in RL_LINE_BUFFER saying what the boundaries
274 If this function exists and returns NULL then call the value of
275 rl_completion_entry_function to try to match, otherwise use the
276 array of strings returned. */
277 rl_completion_func_t
*rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
279 /* Non-zero means to suppress normal filename completion after the
280 user-specified completion function has been called. */
281 int rl_attempted_completion_over
= 0;
283 /* Set to a character indicating the type of completion being performed
284 by rl_complete_internal, available for use by application completion
286 int rl_completion_type
= 0;
288 /* Up to this many items will be displayed in response to a
289 possible-completions call. After that, we ask the user if
290 she is sure she wants to see them all. A negative value means
292 int rl_completion_query_items
= 100;
294 int _rl_page_completions
= 1;
296 /* The basic list of characters that signal a break between words for the
297 completer routine. The contents of this variable is what breaks words
298 in the shell, i.e. " \t\n\"\\'`@$><=" */
299 const char *rl_basic_word_break_characters
= " \t\n\"\\'`@$><=;|&{("; /* }) */
301 /* List of basic quoting characters. */
302 const char *rl_basic_quote_characters
= "\"'";
304 /* The list of characters that signal a break between words for
305 rl_complete_internal. The default list is the contents of
306 rl_basic_word_break_characters. */
307 /*const*/ char *rl_completer_word_break_characters
= (/*const*/ char *)NULL
;
309 /* Hook function to allow an application to set the completion word
310 break characters before readline breaks up the line. Allows
311 position-dependent word break characters. */
312 rl_cpvfunc_t
*rl_completion_word_break_hook
= (rl_cpvfunc_t
*)NULL
;
314 /* List of characters which can be used to quote a substring of the line.
315 Completion occurs on the entire substring, and within the substring
316 rl_completer_word_break_characters are treated as any other character,
317 unless they also appear within this list. */
318 const char *rl_completer_quote_characters
= (const char *)NULL
;
320 /* List of characters that should be quoted in filenames by the completer. */
321 const char *rl_filename_quote_characters
= (const char *)NULL
;
323 /* List of characters that are word break characters, but should be left
324 in TEXT when it is passed to the completion function. The shell uses
325 this to help determine what kind of completing to do. */
326 const char *rl_special_prefixes
= (const char *)NULL
;
328 /* If non-zero, then disallow duplicates in the matches. */
329 int rl_ignore_completion_duplicates
= 1;
331 /* Non-zero means that the results of the matches are to be treated
332 as filenames. This is ALWAYS zero on entry, and can only be changed
333 within a completion entry finder function. */
334 int rl_filename_completion_desired
= 0;
336 /* Non-zero means that the results of the matches are to be quoted using
337 double quotes (or an application-specific quoting mechanism) if the
338 filename contains any characters in rl_filename_quote_chars. This is
339 ALWAYS non-zero on entry, and can only be changed within a completion
340 entry finder function. */
341 int rl_filename_quoting_desired
= 1;
343 /* This function, if defined, is called by the completer when real
344 filename completion is done, after all the matching names have been
345 generated. It is passed a (char**) known as matches in the code below.
346 It consists of a NULL-terminated array of pointers to potential
347 matching strings. The 1st element (matches[0]) is the maximal
348 substring that is common to all matches. This function can re-arrange
349 the list of matches as required, but all elements of the array must be
350 free()'d if they are deleted. The main intent of this function is
351 to implement FIGNORE a la SunOS csh. */
352 rl_compignore_func_t
*rl_ignore_some_completions_function
= (rl_compignore_func_t
*)NULL
;
354 /* Set to a function to quote a filename in an application-specific fashion.
355 Called with the text to quote, the type of match found (single or multiple)
356 and a pointer to the quoting character to be used, which the function can
358 rl_quote_func_t
*rl_filename_quoting_function
= rl_quote_filename
;
360 /* Function to call to remove quoting characters from a filename. Called
361 before completion is attempted, so the embedded quotes do not interfere
362 with matching names in the file system. Readline doesn't do anything
363 with this; it's set only by applications. */
364 rl_dequote_func_t
*rl_filename_dequoting_function
= (rl_dequote_func_t
*)NULL
;
366 /* Function to call to decide whether or not a word break character is
367 quoted. If a character is quoted, it does not break words for the
369 rl_linebuf_func_t
*rl_char_is_quoted_p
= (rl_linebuf_func_t
*)NULL
;
371 /* If non-zero, the completion functions don't append anything except a
372 possible closing quote. This is set to 0 by rl_complete_internal and
373 may be changed by an application-specific completion function. */
374 int rl_completion_suppress_append
= 0;
376 /* Character appended to completed words when at the end of the line. The
377 default is a space. */
378 int rl_completion_append_character
= ' ';
380 /* If non-zero, the completion functions don't append any closing quote.
381 This is set to 0 by rl_complete_internal and may be changed by an
382 application-specific completion function. */
383 int rl_completion_suppress_quote
= 0;
385 /* Set to any quote character readline thinks it finds before any application
386 completion function is called. */
387 int rl_completion_quote_character
;
389 /* Set to a non-zero value if readline found quoting anywhere in the word to
390 be completed; set before any application completion function is called. */
391 int rl_completion_found_quote
;
393 /* If non-zero, a slash will be appended to completed filenames that are
394 symbolic links to directory names, subject to the value of the
395 mark-directories variable (which is user-settable). This exists so
396 that application completion functions can override the user's preference
397 (set via the mark-symlinked-directories variable) if appropriate.
398 It's set to the value of _rl_complete_mark_symlink_dirs in
399 rl_complete_internal before any application-specific completion
400 function is called, so without that function doing anything, the user's
401 preferences are honored. */
402 int rl_completion_mark_symlink_dirs
;
404 /* If non-zero, inhibit completion (temporarily). */
405 int rl_inhibit_completion
;
407 /* Set to the last key used to invoke one of the completion functions */
408 int rl_completion_invoking_key
;
410 /* If non-zero, sort the completion matches. On by default. */
411 int rl_sort_completion_matches
= 1;
413 /* Variables local to this file. */
415 /* Local variable states what happened during the last completion attempt. */
416 static int completion_changed_buffer
;
417 static int last_completion_failed
= 0;
419 /* The result of the query to the user about displaying completion matches */
420 static int completion_y_or_n
;
422 static int _rl_complete_display_matches_interrupt
= 0;
424 /*************************************/
426 /* Bindable completion functions */
428 /*************************************/
430 /* Complete the word at or before point. You have supplied the function
431 that does the initial simple matching selection algorithm (see
432 rl_completion_matches ()). The default is to do filename completion. */
434 rl_complete (int ignore
, int invoking_key
)
436 rl_completion_invoking_key
= invoking_key
;
438 if (rl_inhibit_completion
)
439 return (_rl_insert_char (ignore
, invoking_key
));
441 else if (rl_last_func
== rl_complete
&& completion_changed_buffer
== 0 && last_completion_failed
== 0)
443 else if (rl_last_func
== rl_complete
&& completion_changed_buffer
== 0)
445 return (rl_complete_internal ('?'));
446 else if (_rl_complete_show_all
)
447 return (rl_complete_internal ('!'));
448 else if (_rl_complete_show_unmodified
)
449 return (rl_complete_internal ('@'));
451 return (rl_complete_internal (TAB
));
454 /* List the possible completions. See description of rl_complete (). */
456 rl_possible_completions (int ignore
, int invoking_key
)
458 rl_completion_invoking_key
= invoking_key
;
459 return (rl_complete_internal ('?'));
463 rl_insert_completions (int ignore
, int invoking_key
)
465 rl_completion_invoking_key
= invoking_key
;
466 return (rl_complete_internal ('*'));
469 /* Return the correct value to pass to rl_complete_internal performing
470 the same tests as rl_complete. This allows consecutive calls to an
471 application's completion function to list possible completions and for
472 an application-specific completion function to honor the
473 show-all-if-ambiguous readline variable. */
475 rl_completion_mode (rl_command_func_t
*cfunc
)
477 if (rl_last_func
== cfunc
&& !completion_changed_buffer
)
479 else if (_rl_complete_show_all
)
481 else if (_rl_complete_show_unmodified
)
487 /************************************/
489 /* Completion utility functions */
491 /************************************/
493 /* Reset public readline state on a signal or other event. */
495 _rl_reset_completion_state (void)
497 rl_completion_found_quote
= 0;
498 rl_completion_quote_character
= 0;
502 _rl_complete_sigcleanup (int sig
, void *ptr
)
504 if (sig
== SIGINT
) /* XXX - for now */
506 _rl_free_match_list ((char **)ptr
);
507 _rl_complete_display_matches_interrupt
= 1;
511 /* Set default values for readline word completion. These are the variables
512 that application completion functions can change or inspect. */
514 set_completion_defaults (int what_to_do
)
516 /* Only the completion entry function can change these. */
517 rl_filename_completion_desired
= 0;
518 rl_filename_quoting_desired
= 1;
519 rl_completion_type
= what_to_do
;
520 rl_completion_suppress_append
= rl_completion_suppress_quote
= 0;
521 rl_completion_append_character
= ' ';
523 /* The completion entry function may optionally change this. */
524 rl_completion_mark_symlink_dirs
= _rl_complete_mark_symlink_dirs
;
526 /* Reset private state. */
527 _rl_complete_display_matches_interrupt
= 0;
530 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
532 get_y_or_n (int for_pager
)
536 /* For now, disable pager in callback mode, until we later convert to state
537 driven functions. Have to wait until next major version to add new
538 state definition, since it will change value of RL_STATE_DONE. */
539 #if defined (READLINE_CALLBACKS)
540 if (RL_ISSTATE (RL_STATE_CALLBACK
))
546 RL_SETSTATE(RL_STATE_MOREINPUT
);
548 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
550 if (c
== 'y' || c
== 'Y' || c
== ' ')
552 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
554 if (c
== ABORT_CHAR
|| c
< 0)
555 _rl_abort_internal ();
556 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
558 if (for_pager
&& (c
== 'q' || c
== 'Q'))
565 _rl_internal_pager (int lines
)
569 fprintf (rl_outstream
, "--More--");
570 fflush (rl_outstream
);
572 _rl_erase_entire_line ();
582 path_isdir (const char *filename
)
586 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
589 #if defined (VISIBLE_STATS)
590 /* Return the character which best describes FILENAME.
591 `@' for symbolic links
596 `%' for character special devices
597 `#' for block special devices */
599 stat_char (char *filename
)
606 /* Short-circuit a //server on cygwin, since that will always behave as
609 if (filename
[0] == '/' && filename
[1] == '/' && strchr (filename
+2, '/') == 0)
614 if (rl_filename_stat_hook
)
616 f
= savestring (filename
);
617 (*rl_filename_stat_hook
) (&f
);
623 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
624 r
= lstat (fn
, &finfo
);
626 r
= stat (fn
, &finfo
);
636 if (S_ISDIR (finfo
.st_mode
))
638 #if defined (S_ISCHR)
639 else if (S_ISCHR (finfo
.st_mode
))
642 #if defined (S_ISBLK)
643 else if (S_ISBLK (finfo
.st_mode
))
646 #if defined (S_ISLNK)
647 else if (S_ISLNK (finfo
.st_mode
))
650 #if defined (S_ISSOCK)
651 else if (S_ISSOCK (finfo
.st_mode
))
653 #endif /* S_ISSOCK */
654 #if defined (S_ISFIFO)
655 else if (S_ISFIFO (finfo
.st_mode
))
658 else if (S_ISREG (finfo
.st_mode
))
660 #if defined (_WIN32) && !defined (__CYGWIN__)
663 /* Windows doesn't do access and X_OK; check file extension instead */
664 ext
= strrchr (fn
, '.');
665 if (ext
&& (_rl_stricmp (ext
, ".exe") == 0 ||
666 _rl_stricmp (ext
, ".cmd") == 0 ||
667 _rl_stricmp (ext
, ".bat") == 0 ||
668 _rl_stricmp (ext
, ".com") == 0))
671 if (access (filename
, X_OK
) == 0)
679 #endif /* VISIBLE_STATS */
681 #if defined (COLOR_SUPPORT)
683 colored_stat_start (const char *filename
)
685 _rl_set_normal_color ();
686 return (_rl_print_color_indicator (filename
));
690 colored_stat_end (void)
692 _rl_prep_non_filename_text ();
693 _rl_put_indicator (&_rl_color_indicator
[C_CLR_TO_EOL
]);
697 colored_prefix_start (void)
699 _rl_set_normal_color ();
700 return (_rl_print_prefix_color ());
704 colored_prefix_end (void)
706 colored_stat_end (); /* for now */
710 /* Return the portion of PATHNAME that should be output when listing
711 possible completions. If we are hacking filename completion, we
712 are only interested in the basename, the portion following the
713 final slash. Otherwise, we return what we were passed. Since
714 printing empty strings is not very informative, if we're doing
715 filename completion, and the basename is the empty string, we look
716 for the previous slash and return the portion following that. If
717 there's no previous slash, we just return what we were passed. */
719 printable_part (char *pathname
)
723 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
726 temp
= strrchr (pathname
, '/');
727 #if defined (__MSDOS__) || defined (_WIN32)
728 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
732 if (temp
== 0 || *temp
== '\0')
734 else if (temp
[1] == 0 && temp
== pathname
)
736 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
737 Look for a previous slash and, if one is found, return the portion
738 following that slash. If there's no previous slash, just return the
739 pathname we were passed. */
740 else if (temp
[1] == '\0')
742 for (x
= temp
- 1; x
> pathname
; x
--)
745 return ((*x
== '/') ? x
+ 1 : pathname
);
751 /* Compute width of STRING when displayed on screen by print_filename */
753 fnwidth (const char *string
)
756 #if defined (HANDLE_MULTIBYTE)
762 left
= strlen (string
) + 1;
763 memset (&ps
, 0, sizeof (mbstate_t));
769 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
776 #if defined (HANDLE_MULTIBYTE)
777 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
778 if (MB_INVALIDCH (clen
))
782 memset (&ps
, 0, sizeof (mbstate_t));
784 else if (MB_NULLWCH (clen
))
790 width
+= (w
>= 0) ? w
: 1;
802 #define ELLIPSIS_LEN 3
805 fnprint (const char *to_print
, int prefix_bytes
, const char *real_pathname
)
809 int common_prefix_len
, print_len
;
810 #if defined (HANDLE_MULTIBYTE)
817 print_len
= strlen (to_print
);
818 end
= to_print
+ print_len
+ 1;
819 memset (&ps
, 0, sizeof (mbstate_t));
821 print_len
= strlen (to_print
);
824 printed_len
= common_prefix_len
= 0;
826 /* Don't print only the ellipsis if the common prefix is one of the
827 possible completions. Only cut off prefix_bytes if we're going to be
828 printing the ellipsis, which takes precedence over coloring the
829 completion prefix (see print_filename() below). */
830 if (_rl_completion_prefix_display_length
> 0 && prefix_bytes
>= print_len
)
833 #if defined (COLOR_SUPPORT)
834 if (_rl_colored_stats
&& (prefix_bytes
== 0 || _rl_colored_completion_prefix
<= 0))
835 colored_stat_start (real_pathname
);
838 if (prefix_bytes
&& _rl_completion_prefix_display_length
> 0)
842 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
843 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
844 putc (ellipsis
, rl_outstream
);
845 printed_len
= ELLIPSIS_LEN
;
847 #if defined (COLOR_SUPPORT)
848 else if (prefix_bytes
&& _rl_colored_completion_prefix
> 0)
850 common_prefix_len
= prefix_bytes
;
852 /* XXX - print color indicator start here */
853 colored_prefix_start ();
857 s
= to_print
+ prefix_bytes
;
862 putc ('^', rl_outstream
);
863 putc (UNCTRL (*s
), rl_outstream
);
866 #if defined (HANDLE_MULTIBYTE)
867 memset (&ps
, 0, sizeof (mbstate_t));
870 else if (*s
== RUBOUT
)
872 putc ('^', rl_outstream
);
873 putc ('?', rl_outstream
);
876 #if defined (HANDLE_MULTIBYTE)
877 memset (&ps
, 0, sizeof (mbstate_t));
882 #if defined (HANDLE_MULTIBYTE)
883 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
884 if (MB_INVALIDCH (tlen
))
888 memset (&ps
, 0, sizeof (mbstate_t));
890 else if (MB_NULLWCH (tlen
))
895 width
= (w
>= 0) ? w
: 1;
897 fwrite (s
, 1, tlen
, rl_outstream
);
899 printed_len
+= width
;
901 putc (*s
, rl_outstream
);
906 if (common_prefix_len
> 0 && (s
- to_print
) >= common_prefix_len
)
908 #if defined (COLOR_SUPPORT)
909 /* printed bytes = s - to_print */
910 /* printed bytes should never be > but check for paranoia's sake */
911 colored_prefix_end ();
912 if (_rl_colored_stats
)
913 colored_stat_start (real_pathname
); /* XXX - experiment */
915 common_prefix_len
= 0;
919 #if defined (COLOR_SUPPORT)
920 /* XXX - unconditional for now */
921 if (_rl_colored_stats
)
928 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
929 are using it, check for and output a single character for `special'
930 filenames. Return the number of characters we output. */
933 print_filename (char *to_print
, char *full_pathname
, int prefix_bytes
)
935 int printed_len
, extension_char
, slen
, tlen
;
936 char *s
, c
, *new_full_pathname
, *dn
;
939 #if defined (COLOR_SUPPORT)
940 /* Defer printing if we want to prefix with a color indicator */
941 if (_rl_colored_stats
== 0 || rl_filename_completion_desired
== 0)
943 printed_len
= fnprint (to_print
, prefix_bytes
, to_print
);
945 if (rl_filename_completion_desired
&& (
946 #if defined (VISIBLE_STATS)
949 #if defined (COLOR_SUPPORT)
952 _rl_complete_mark_directories
))
954 /* If to_print != full_pathname, to_print is the basename of the
955 path passed. In this case, we try to expand the directory
956 name before checking for the stat character. */
957 if (to_print
!= full_pathname
)
959 /* Terminate the directory name. */
963 /* If setting the last slash in full_pathname to a NUL results in
964 full_pathname being the empty string, we are trying to complete
965 files in the root directory. If we pass a null string to the
966 bash directory completion hook, for example, it will expand it
967 to the current directory. We just want the `/'. */
968 if (full_pathname
== 0 || *full_pathname
== 0)
970 else if (full_pathname
[0] != '/')
972 else if (full_pathname
[1] == 0)
973 dn
= "//"; /* restore trailing slash to `//' */
974 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
975 dn
= "/"; /* don't turn /// into // */
978 s
= tilde_expand (dn
);
979 if (rl_directory_completion_hook
)
980 (*rl_directory_completion_hook
) (&s
);
983 tlen
= strlen (to_print
);
984 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
985 strcpy (new_full_pathname
, s
);
986 if (s
[slen
- 1] == '/')
989 new_full_pathname
[slen
] = '/';
990 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
992 #if defined (VISIBLE_STATS)
993 if (rl_visible_stats
)
994 extension_char
= stat_char (new_full_pathname
);
997 if (_rl_complete_mark_directories
)
1000 if (rl_directory_completion_hook
== 0 && rl_filename_stat_hook
)
1002 dn
= savestring (new_full_pathname
);
1003 (*rl_filename_stat_hook
) (&dn
);
1004 xfree (new_full_pathname
);
1005 new_full_pathname
= dn
;
1007 if (path_isdir (new_full_pathname
))
1008 extension_char
= '/';
1011 /* Move colored-stats code inside fnprint() */
1012 #if defined (COLOR_SUPPORT)
1013 if (_rl_colored_stats
)
1014 printed_len
= fnprint (to_print
, prefix_bytes
, new_full_pathname
);
1017 xfree (new_full_pathname
);
1022 s
= tilde_expand (full_pathname
);
1023 #if defined (VISIBLE_STATS)
1024 if (rl_visible_stats
)
1025 extension_char
= stat_char (s
);
1028 if (_rl_complete_mark_directories
&& path_isdir (s
))
1029 extension_char
= '/';
1031 /* Move colored-stats code inside fnprint() */
1032 #if defined (COLOR_SUPPORT)
1033 if (_rl_colored_stats
)
1034 printed_len
= fnprint (to_print
, prefix_bytes
, s
);
1041 putc (extension_char
, rl_outstream
);
1050 rl_quote_filename (char *s
, int rtype
, char *qcp
)
1054 r
= (char *)xmalloc (strlen (s
) + 2);
1055 *r
= *rl_completer_quote_characters
;
1058 *qcp
= *rl_completer_quote_characters
;
1062 /* Find the bounds of the current word for completion purposes, and leave
1063 rl_point set to the end of the word. This function skips quoted
1064 substrings (characters between matched pairs of characters in
1065 rl_completer_quote_characters). First we try to find an unclosed
1066 quoted substring on which to do matching. If one is not found, we use
1067 the word break characters to find the boundaries of the current word.
1068 We call an application-specific function to decide whether or not a
1069 particular word break character is quoted; if that function returns a
1070 non-zero result, the character does not break a word. This function
1071 returns the opening quote character if we found an unclosed quoted
1072 substring, '\0' otherwise. FP, if non-null, is set to a value saying
1073 which (shell-like) quote characters we found (single quote, double
1074 quote, or backslash) anywhere in the string. DP, if non-null, is set to
1075 the value of the delimiter character that caused a word break. */
1078 _rl_find_completion_word (int *fp
, int *dp
)
1080 int scan
, end
, found_quote
, delimiter
, pass_next
, isbrk
;
1081 char quote_char
, *brkchars
;
1084 found_quote
= delimiter
= 0;
1088 if (rl_completion_word_break_hook
)
1089 brkchars
= (*rl_completion_word_break_hook
) ();
1091 brkchars
= rl_completer_word_break_characters
;
1093 if (rl_completer_quote_characters
)
1095 /* We have a list of characters which can be used in pairs to
1096 quote substrings for the completer. Try to find the start
1097 of an unclosed quoted substring. */
1098 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
1099 for (scan
= pass_next
= 0; scan
< end
; scan
= MB_NEXTCHAR (rl_line_buffer
, scan
, 1, MB_FIND_ANY
))
1107 /* Shell-like semantics for single quotes -- don't allow backslash
1108 to quote anything in single quotes, especially not the closing
1109 quote. If you don't like this, take out the check on the value
1111 if (quote_char
!= '\'' && rl_line_buffer
[scan
] == '\\')
1114 found_quote
|= RL_QF_BACKSLASH
;
1118 if (quote_char
!= '\0')
1120 /* Ignore everything until the matching close quote char. */
1121 if (rl_line_buffer
[scan
] == quote_char
)
1123 /* Found matching close. Abandon this substring. */
1128 else if (strchr (rl_completer_quote_characters
, rl_line_buffer
[scan
]))
1130 /* Found start of a quoted substring. */
1131 quote_char
= rl_line_buffer
[scan
];
1132 rl_point
= scan
+ 1;
1133 /* Shell-like quoting conventions. */
1134 if (quote_char
== '\'')
1135 found_quote
|= RL_QF_SINGLE_QUOTE
;
1136 else if (quote_char
== '"')
1137 found_quote
|= RL_QF_DOUBLE_QUOTE
;
1139 found_quote
|= RL_QF_OTHER_QUOTE
;
1144 if (rl_point
== end
&& quote_char
== '\0')
1146 /* We didn't find an unclosed quoted substring upon which to do
1147 completion, so use the word break characters to find the
1148 substring on which to complete. */
1149 while (rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_ANY
))
1151 scan
= rl_line_buffer
[rl_point
];
1153 if (strchr (brkchars
, scan
) == 0)
1156 /* Call the application-specific function to tell us whether
1157 this word break character is quoted and should be skipped. */
1158 if (rl_char_is_quoted_p
&& found_quote
&&
1159 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
))
1162 /* Convoluted code, but it avoids an n^2 algorithm with calls
1163 to char_is_quoted. */
1168 /* If we are at an unquoted word break, then advance past it. */
1169 scan
= rl_line_buffer
[rl_point
];
1171 /* If there is an application-specific function to say whether or not
1172 a character is quoted and we found a quote character, let that
1173 function decide whether or not a character is a word break, even
1174 if it is found in rl_completer_word_break_characters. Don't bother
1175 if we're at the end of the line, though. */
1178 if (rl_char_is_quoted_p
)
1179 isbrk
= (found_quote
== 0 ||
1180 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
) == 0) &&
1181 strchr (brkchars
, scan
) != 0;
1183 isbrk
= strchr (brkchars
, scan
) != 0;
1187 /* If the character that caused the word break was a quoting
1188 character, then remember it as the delimiter. */
1189 if (rl_basic_quote_characters
&&
1190 strchr (rl_basic_quote_characters
, scan
) &&
1191 (end
- rl_point
) > 1)
1194 /* If the character isn't needed to determine something special
1195 about what kind of completion to perform, then advance past it. */
1196 if (rl_special_prefixes
== 0 || strchr (rl_special_prefixes
, scan
) == 0)
1206 return (quote_char
);
1210 gen_completion_matches (char *text
, int start
, int end
, rl_compentry_func_t
*our_func
, int found_quote
, int quote_char
)
1214 rl_completion_found_quote
= found_quote
;
1215 rl_completion_quote_character
= quote_char
;
1217 /* If the user wants to TRY to complete, but then wants to give
1218 up and use the default completion function, they set the
1219 variable rl_attempted_completion_function. */
1220 if (rl_attempted_completion_function
)
1222 matches
= (*rl_attempted_completion_function
) (text
, start
, end
);
1223 if (RL_SIG_RECEIVED())
1225 _rl_free_match_list (matches
);
1227 RL_CHECK_SIGNALS ();
1230 if (matches
|| rl_attempted_completion_over
)
1232 rl_attempted_completion_over
= 0;
1237 /* XXX -- filename dequoting moved into rl_filename_completion_function */
1239 /* rl_completion_matches will check for signals as well to avoid a long
1240 delay while reading a directory. */
1241 matches
= rl_completion_matches (text
, our_func
);
1242 if (RL_SIG_RECEIVED())
1244 _rl_free_match_list (matches
);
1246 RL_CHECK_SIGNALS ();
1251 /* Filter out duplicates in MATCHES. This frees up the strings in
1254 remove_duplicate_matches (char **matches
)
1256 char *lowest_common
;
1261 /* Sort the items. */
1262 for (i
= 0; matches
[i
]; i
++)
1265 /* Sort the array without matches[0], since we need it to
1266 stay in place no matter what. */
1267 if (i
&& rl_sort_completion_matches
)
1268 qsort (matches
+1, i
-1, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1270 /* Remember the lowest common denominator for it may be unique. */
1271 lowest_common
= savestring (matches
[0]);
1273 for (i
= newlen
= 0; matches
[i
+ 1]; i
++)
1275 if (strcmp (matches
[i
], matches
[i
+ 1]) == 0)
1278 matches
[i
] = (char *)&dead_slot
;
1284 /* We have marked all the dead slots with (char *)&dead_slot.
1285 Copy all the non-dead entries into a new array. */
1286 temp_array
= (char **)xmalloc ((3 + newlen
) * sizeof (char *));
1287 for (i
= j
= 1; matches
[i
]; i
++)
1289 if (matches
[i
] != (char *)&dead_slot
)
1290 temp_array
[j
++] = matches
[i
];
1292 temp_array
[j
] = (char *)NULL
;
1294 if (matches
[0] != (char *)&dead_slot
)
1297 /* Place the lowest common denominator back in [0]. */
1298 temp_array
[0] = lowest_common
;
1300 /* If there is one string left, and it is identical to the
1301 lowest common denominator, then the LCD is the string to
1303 if (j
== 2 && strcmp (temp_array
[0], temp_array
[1]) == 0)
1305 xfree (temp_array
[1]);
1306 temp_array
[1] = (char *)NULL
;
1308 return (temp_array
);
1311 /* Find the common prefix of the list of matches, and put it into
1314 compute_lcd_of_matches (char **match_list
, int matches
, const char *text
)
1316 register int i
, c1
, c2
, si
;
1317 int low
; /* Count of max-matched characters. */
1319 char *dtext
; /* dequoted TEXT, if needed */
1320 #if defined (HANDLE_MULTIBYTE)
1327 /* If only one match, just use that. Otherwise, compare each
1328 member of the list with the next, finding out where they
1332 match_list
[0] = match_list
[1];
1333 match_list
[1] = (char *)NULL
;
1337 for (i
= 1, low
= 100000; i
< matches
; i
++)
1339 #if defined (HANDLE_MULTIBYTE)
1340 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1342 memset (&ps1
, 0, sizeof (mbstate_t));
1343 memset (&ps2
, 0, sizeof (mbstate_t));
1346 for (si
= 0; (c1
= match_list
[i
][si
]) && (c2
= match_list
[i
+ 1][si
]); si
++)
1348 if (_rl_completion_case_fold
)
1350 c1
= _rl_to_lower (c1
);
1351 c2
= _rl_to_lower (c2
);
1353 #if defined (HANDLE_MULTIBYTE)
1354 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1356 v1
= mbrtowc(&wc1
, match_list
[i
]+si
, strlen (match_list
[i
]+si
), &ps1
);
1357 v2
= mbrtowc (&wc2
, match_list
[i
+1]+si
, strlen (match_list
[i
+1]+si
), &ps2
);
1358 if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
1360 if (c1
!= c2
) /* do byte comparison */
1364 if (_rl_completion_case_fold
)
1366 wc1
= towlower (wc1
);
1367 wc2
= towlower (wc2
);
1384 /* If there were multiple matches, but none matched up to even the
1385 first character, and the user typed something, use that as the
1386 value of matches[0]. */
1387 if (low
== 0 && text
&& *text
)
1389 match_list
[0] = (char *)xmalloc (strlen (text
) + 1);
1390 strcpy (match_list
[0], text
);
1394 match_list
[0] = (char *)xmalloc (low
+ 1);
1396 /* XXX - this might need changes in the presence of multibyte chars */
1398 /* If we are ignoring case, try to preserve the case of the string
1399 the user typed in the face of multiple matches differing in case. */
1400 if (_rl_completion_case_fold
)
1402 /* We're making an assumption here:
1403 IF we're completing filenames AND
1404 the application has defined a filename dequoting function AND
1405 we found a quote character AND
1406 the application has requested filename quoting
1408 we assume that TEXT was dequoted before checking against
1409 the file system and needs to be dequoted here before we
1410 check against the list of matches
1412 dtext
= (char *)NULL
;
1413 if (rl_filename_completion_desired
&&
1414 rl_filename_dequoting_function
&&
1415 rl_completion_found_quote
&&
1416 rl_filename_quoting_desired
)
1418 dtext
= (*rl_filename_dequoting_function
) ((char *)text
, rl_completion_quote_character
);
1422 /* sort the list to get consistent answers. */
1423 if (rl_sort_completion_matches
)
1424 qsort (match_list
+1, matches
, sizeof(char *), (QSFUNC
*)_rl_qsort_string_compare
);
1427 lx
= (si
<= low
) ? si
: low
; /* check shorter of text and matches */
1428 /* Try to preserve the case of what the user typed in the presence of
1429 multiple matches: check each match for something that matches
1430 what the user typed taking case into account; use it up to common
1431 length of matches if one is found. If not, just use first match. */
1432 for (i
= 1; i
<= matches
; i
++)
1433 if (strncmp (match_list
[i
], text
, lx
) == 0)
1435 strncpy (match_list
[0], match_list
[i
], low
);
1438 /* no casematch, use first entry */
1440 strncpy (match_list
[0], match_list
[1], low
);
1445 strncpy (match_list
[0], match_list
[1], low
);
1447 match_list
[0][low
] = '\0';
1454 postprocess_matches (char ***matchesp
, int matching_filenames
)
1456 char *t
, **matches
, **temp_matches
;
1459 matches
= *matchesp
;
1464 /* It seems to me that in all the cases we handle we would like
1465 to ignore duplicate possibilities. Scan for the text to
1466 insert being identical to the other completions. */
1467 if (rl_ignore_completion_duplicates
)
1469 temp_matches
= remove_duplicate_matches (matches
);
1471 matches
= temp_matches
;
1474 /* If we are matching filenames, then here is our chance to
1475 do clever processing by re-examining the list. Call the
1476 ignore function with the array as a parameter. It can
1477 munge the array, deleting matches as it desires. */
1478 if (rl_ignore_some_completions_function
&& matching_filenames
)
1480 for (nmatch
= 1; matches
[nmatch
]; nmatch
++)
1482 (void)(*rl_ignore_some_completions_function
) (matches
);
1483 if (matches
== 0 || matches
[0] == 0)
1486 *matchesp
= (char **)0;
1491 /* If we removed some matches, recompute the common prefix. */
1492 for (i
= 1; matches
[i
]; i
++)
1494 if (i
> 1 && i
< nmatch
)
1497 compute_lcd_of_matches (matches
, i
- 1, t
);
1503 *matchesp
= matches
;
1508 complete_get_screenwidth (void)
1513 cols
= _rl_completion_columns
;
1514 if (cols
>= 0 && cols
<= _rl_screenwidth
)
1516 envcols
= getenv ("COLUMNS");
1517 if (envcols
&& *envcols
)
1518 cols
= atoi (envcols
);
1519 if (cols
>= 0 && cols
<= _rl_screenwidth
)
1521 return _rl_screenwidth
;
1524 /* A convenience function for displaying a list of strings in
1525 columnar format on readline's output stream. MATCHES is the list
1526 of strings, in argv format, LEN is the number of strings in MATCHES,
1527 and MAX is the length of the longest string in MATCHES. */
1529 rl_display_match_list (char **matches
, int len
, int max
)
1531 int count
, limit
, printed_len
, lines
, cols
;
1532 int i
, j
, k
, l
, common_length
, sind
;
1535 /* Find the length of the prefix common to all items: length as displayed
1536 characters (common_length) and as a byte index into the matches (sind) */
1537 common_length
= sind
= 0;
1538 if (_rl_completion_prefix_display_length
> 0)
1540 t
= printable_part (matches
[0]);
1541 /* check again in case of /usr/src/ */
1542 temp
= rl_filename_completion_desired
? strrchr (t
, '/') : 0;
1543 common_length
= temp
? fnwidth (temp
) : fnwidth (t
);
1544 sind
= temp
? strlen (temp
) : strlen (t
);
1545 if (common_length
> max
|| sind
> max
)
1546 common_length
= sind
= 0;
1548 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
1549 max
-= common_length
- ELLIPSIS_LEN
;
1551 common_length
= sind
= 0;
1553 #if defined (COLOR_SUPPORT)
1554 else if (_rl_colored_completion_prefix
> 0)
1556 t
= printable_part (matches
[0]);
1557 temp
= rl_filename_completion_desired
? strrchr (t
, '/') : 0;
1558 common_length
= temp
? fnwidth (temp
) : fnwidth (t
);
1559 sind
= temp
? RL_STRLEN (temp
+1) : RL_STRLEN (t
); /* want portion after final slash */
1560 if (common_length
> max
|| sind
> max
)
1561 common_length
= sind
= 0;
1565 /* How many items of MAX length can we fit in the screen window? */
1566 cols
= complete_get_screenwidth ();
1569 if (limit
!= 1 && (limit
* max
== cols
))
1572 /* If cols == 0, limit will end up -1 */
1573 if (cols
< _rl_screenwidth
&& limit
< 0)
1576 /* Avoid a possible floating exception. If max > cols,
1577 limit will be 0 and a divide-by-zero fault will result. */
1581 /* How many iterations of the printing loop? */
1582 count
= (len
+ (limit
- 1)) / limit
;
1584 /* Watch out for special case. If LEN is less than LIMIT, then
1585 just do the inner printing loop.
1586 0 < len <= limit implies count = 1. */
1588 /* Sort the items if they are not already sorted. */
1589 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
1590 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1595 if (_rl_print_completions_horizontally
== 0)
1597 /* Print the sorted items, up-and-down alphabetically, like ls. */
1598 for (i
= 1; i
<= count
; i
++)
1600 for (j
= 0, l
= i
; j
< limit
; j
++)
1602 if (l
> len
|| matches
[l
] == 0)
1606 temp
= printable_part (matches
[l
]);
1607 printed_len
= print_filename (temp
, matches
[l
], sind
);
1611 if (max
<= printed_len
)
1612 putc (' ', rl_outstream
);
1614 for (k
= 0; k
< max
- printed_len
; k
++)
1615 putc (' ', rl_outstream
);
1621 #if defined (SIGWINCH)
1622 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1624 if (RL_SIG_RECEIVED ())
1628 if (_rl_page_completions
&& lines
>= (_rl_screenheight
- 1) && i
< count
)
1630 lines
= _rl_internal_pager (lines
);
1638 /* Print the sorted items, across alphabetically, like ls -x. */
1639 for (i
= 1; matches
[i
]; i
++)
1641 temp
= printable_part (matches
[i
]);
1642 printed_len
= print_filename (temp
, matches
[i
], sind
);
1643 /* Have we reached the end of this line? */
1644 #if defined (SIGWINCH)
1645 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1647 if (RL_SIG_RECEIVED ())
1652 if (limit
== 1 || (i
&& (limit
> 1) && (i
% limit
) == 0))
1656 if (_rl_page_completions
&& lines
>= _rl_screenheight
- 1)
1658 lines
= _rl_internal_pager (lines
);
1663 else if (max
<= printed_len
)
1664 putc (' ', rl_outstream
);
1666 for (k
= 0; k
< max
- printed_len
; k
++)
1667 putc (' ', rl_outstream
);
1674 /* Display MATCHES, a list of matching filenames in argv format. This
1675 handles the simple case -- a single match -- first. If there is more
1676 than one match, we compute the number of strings in the list and the
1677 length of the longest string, which will be needed by the display
1678 function. If the application wants to handle displaying the list of
1679 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1680 address of a function, and we just call it. If we're handling the
1681 display ourselves, we just call rl_display_match_list. We also check
1682 that the list of matches doesn't exceed the user-settable threshold,
1683 and ask the user if he wants to see the list if there are more matches
1684 than RL_COMPLETION_QUERY_ITEMS. */
1686 display_matches (char **matches
)
1691 /* Move to the last visible line of a possibly-multiple-line command. */
1692 _rl_move_vert (_rl_vis_botlin
);
1694 /* Handle simple case first. What if there is only one answer? */
1695 if (matches
[1] == 0)
1697 temp
= printable_part (matches
[0]);
1699 print_filename (temp
, matches
[0], 0);
1702 rl_forced_update_display ();
1703 rl_display_fixed
= 1;
1708 /* There is more than one answer. Find out how many there are,
1709 and find the maximum printed length of a single entry. */
1710 for (max
= 0, i
= 1; matches
[i
]; i
++)
1712 temp
= printable_part (matches
[i
]);
1713 len
= fnwidth (temp
);
1721 /* If the caller has defined a display hook, then call that now. */
1722 if (rl_completion_display_matches_hook
)
1724 (*rl_completion_display_matches_hook
) (matches
, len
, max
);
1728 /* If there are many items, then ask the user if she really wants to
1730 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
1733 fprintf (rl_outstream
, "Display all %d possibilities? (y or n)", len
);
1734 fflush (rl_outstream
);
1735 if ((completion_y_or_n
= get_y_or_n (0)) == 0)
1739 rl_forced_update_display ();
1740 rl_display_fixed
= 1;
1746 rl_display_match_list (matches
, len
, max
);
1748 rl_forced_update_display ();
1749 rl_display_fixed
= 1;
1752 /* qc == pointer to quoting character, if any */
1754 make_quoted_replacement (char *match
, int mtype
, char *qc
)
1756 int should_quote
, do_replace
;
1759 /* If we are doing completion on quoted substrings, and any matches
1760 contain any of the completer_word_break_characters, then auto-
1761 matically prepend the substring with a quote character (just pick
1762 the first one from the list of such) if it does not already begin
1763 with a quote string. FIXME: Need to remove any such automatically
1764 inserted quote character when it no longer is necessary, such as
1765 if we change the string we are completing on and the new set of
1766 matches don't require a quoted substring. */
1767 replacement
= match
;
1769 should_quote
= match
&& rl_completer_quote_characters
&&
1770 rl_filename_completion_desired
&&
1771 rl_filename_quoting_desired
;
1774 should_quote
= should_quote
&& (!qc
|| !*qc
||
1775 (rl_completer_quote_characters
&& strchr (rl_completer_quote_characters
, *qc
)));
1779 /* If there is a single match, see if we need to quote it.
1780 This also checks whether the common prefix of several
1781 matches needs to be quoted. */
1782 should_quote
= rl_filename_quote_characters
1783 ? (_rl_strpbrk (match
, rl_filename_quote_characters
) != 0)
1786 do_replace
= should_quote
? mtype
: NO_MATCH
;
1787 /* Quote the replacement, since we found an embedded
1788 word break character in a potential match. */
1789 if (do_replace
!= NO_MATCH
&& rl_filename_quoting_function
)
1790 replacement
= (*rl_filename_quoting_function
) (match
, do_replace
, qc
);
1792 return (replacement
);
1796 insert_match (char *match
, int start
, int mtype
, char *qc
)
1798 char *replacement
, *r
;
1802 oqc
= qc
? *qc
: '\0';
1803 replacement
= make_quoted_replacement (match
, mtype
, qc
);
1805 /* Now insert the match. */
1808 rlen
= strlen (replacement
);
1809 /* Don't double an opening quote character. */
1810 if (qc
&& *qc
&& start
&& rl_line_buffer
[start
- 1] == *qc
&&
1811 replacement
[0] == *qc
)
1813 /* If make_quoted_replacement changed the quoting character, remove
1814 the opening quote and insert the (fully-quoted) replacement. */
1815 else if (qc
&& (*qc
!= oqc
) && start
&& rl_line_buffer
[start
- 1] == oqc
&&
1816 replacement
[0] != oqc
)
1819 /* Don't double a closing quote character */
1820 if (qc
&& *qc
&& end
&& rl_line_buffer
[rl_point
] == *qc
&& replacement
[rlen
- 1] == *qc
)
1822 if (_rl_skip_completed_text
)
1825 while (start
< rl_end
&& *r
&& rl_line_buffer
[start
] == *r
)
1830 if (start
<= end
|| *r
)
1831 _rl_replace_text (r
, start
, end
);
1832 rl_point
= start
+ strlen (r
);
1835 _rl_replace_text (replacement
, start
, end
);
1836 if (replacement
!= match
)
1837 xfree (replacement
);
1841 /* Append any necessary closing quote and a separator character to the
1842 just-inserted match. If the user has specified that directories
1843 should be marked by a trailing `/', append one of those instead. The
1844 default trailing character is a space. Returns the number of characters
1845 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1846 has them) and don't add a suffix for a symlink to a directory. A
1847 nontrivial match is one that actually adds to the word being completed.
1848 The variable rl_completion_mark_symlink_dirs controls this behavior
1849 (it's initially set to the what the user has chosen, indicated by the
1850 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1851 application's completion function). */
1853 append_to_match (char *text
, int delimiter
, int quote_char
, int nontrivial_match
)
1855 char temp_string
[4], *filename
, *fn
;
1856 int temp_string_index
, s
;
1859 temp_string_index
= 0;
1860 if (quote_char
&& rl_point
&& rl_completion_suppress_quote
== 0 &&
1861 rl_line_buffer
[rl_point
- 1] != quote_char
)
1862 temp_string
[temp_string_index
++] = quote_char
;
1865 temp_string
[temp_string_index
++] = delimiter
;
1866 else if (rl_completion_suppress_append
== 0 && rl_completion_append_character
)
1867 temp_string
[temp_string_index
++] = rl_completion_append_character
;
1869 temp_string
[temp_string_index
++] = '\0';
1871 if (rl_filename_completion_desired
)
1873 filename
= tilde_expand (text
);
1874 if (rl_filename_stat_hook
)
1876 fn
= savestring (filename
);
1877 (*rl_filename_stat_hook
) (&fn
);
1881 s
= (nontrivial_match
&& rl_completion_mark_symlink_dirs
== 0)
1882 ? LSTAT (filename
, &finfo
)
1883 : stat (filename
, &finfo
);
1884 if (s
== 0 && S_ISDIR (finfo
.st_mode
))
1886 if (_rl_complete_mark_directories
/* && rl_completion_suppress_append == 0 */)
1888 /* This is clumsy. Avoid putting in a double slash if point
1889 is at the end of the line and the previous character is a
1891 if (rl_point
&& rl_line_buffer
[rl_point
] == '\0' && rl_line_buffer
[rl_point
- 1] == '/')
1893 else if (rl_line_buffer
[rl_point
] != '/')
1894 rl_insert_text ("/");
1898 /* Don't add anything if the filename is a symlink and resolves to a
1900 else if (s
== 0 && S_ISLNK (finfo
.st_mode
) && path_isdir (filename
))
1905 if (rl_point
== rl_end
&& temp_string_index
)
1906 rl_insert_text (temp_string
);
1912 if (rl_point
== rl_end
&& temp_string_index
)
1913 rl_insert_text (temp_string
);
1916 return (temp_string_index
);
1920 insert_all_matches (char **matches
, int point
, char *qc
)
1925 rl_begin_undo_group ();
1926 /* remove any opening quote character; make_quoted_replacement will add
1928 if (qc
&& *qc
&& point
&& rl_line_buffer
[point
- 1] == *qc
)
1930 rl_delete_text (point
, rl_point
);
1935 for (i
= 1; matches
[i
]; i
++)
1937 rp
= make_quoted_replacement (matches
[i
], SINGLE_MATCH
, qc
);
1938 rl_insert_text (rp
);
1939 rl_insert_text (" ");
1940 if (rp
!= matches
[i
])
1946 rp
= make_quoted_replacement (matches
[0], SINGLE_MATCH
, qc
);
1947 rl_insert_text (rp
);
1948 rl_insert_text (" ");
1949 if (rp
!= matches
[0])
1952 rl_end_undo_group ();
1956 _rl_free_match_list (char **matches
)
1963 for (i
= 0; matches
[i
]; i
++)
1968 /* Compare a possibly-quoted filename TEXT from the line buffer and a possible
1969 MATCH that is the product of filename completion, which acts on the dequoted
1972 compare_match (char *text
, const char *match
)
1977 if (rl_filename_completion_desired
&& rl_filename_quoting_desired
&&
1978 rl_completion_found_quote
&& rl_filename_dequoting_function
)
1980 temp
= (*rl_filename_dequoting_function
) (text
, rl_completion_quote_character
);
1981 r
= strcmp (temp
, match
);
1985 return (strcmp (text
, match
));
1988 /* Complete the word at or before point.
1989 WHAT_TO_DO says what to do with the completion.
1990 `?' means list the possible completions.
1991 TAB means do standard completion.
1992 `*' means insert all of the possible completions.
1993 `!' means to do standard completion, and list all possible completions if
1994 there is more than one.
1995 `@' means to do standard completion, and list all possible completions if
1996 there is more than one and partial completion is not possible. */
1998 rl_complete_internal (int what_to_do
)
2001 rl_compentry_func_t
*our_func
;
2002 int start
, end
, delimiter
, found_quote
, i
, nontrivial_lcd
;
2003 char *text
, *saved_line_buffer
;
2005 int tlen
, mlen
, saved_last_completion_failed
;
2007 RL_SETSTATE(RL_STATE_COMPLETING
);
2009 saved_last_completion_failed
= last_completion_failed
;
2011 set_completion_defaults (what_to_do
);
2013 saved_line_buffer
= rl_line_buffer
? savestring (rl_line_buffer
) : (char *)NULL
;
2014 our_func
= rl_completion_entry_function
2015 ? rl_completion_entry_function
2016 : rl_filename_completion_function
;
2017 /* We now look backwards for the start of a filename/variable word. */
2019 found_quote
= delimiter
= 0;
2023 /* This (possibly) changes rl_point. If it returns a non-zero char,
2024 we know we have an open quote. */
2025 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2030 text
= rl_copy_text (start
, end
);
2031 matches
= gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
);
2032 /* nontrivial_lcd is set if the common prefix adds something to the word
2034 nontrivial_lcd
= matches
&& compare_match (text
, matches
[0]) != 0;
2035 if (what_to_do
== '!' || what_to_do
== '@')
2036 tlen
= strlen (text
);
2042 FREE (saved_line_buffer
);
2043 completion_changed_buffer
= 0;
2044 last_completion_failed
= 1;
2045 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2046 _rl_reset_completion_state ();
2050 /* If we are matching filenames, the attempted completion function will
2051 have set rl_filename_completion_desired to a non-zero value. The basic
2052 rl_filename_completion_function does this. */
2053 i
= rl_filename_completion_desired
;
2055 if (postprocess_matches (&matches
, i
) == 0)
2058 FREE (saved_line_buffer
);
2059 completion_changed_buffer
= 0;
2060 last_completion_failed
= 1;
2061 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2062 _rl_reset_completion_state ();
2066 if (matches
&& matches
[0] && *matches
[0])
2067 last_completion_failed
= 0;
2074 /* Insert the first match with proper quoting. */
2075 if (what_to_do
== TAB
)
2078 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2080 else if (*matches
[0] && matches
[1] == 0)
2081 /* should we perform the check only if there are multiple matches? */
2082 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2083 else if (*matches
[0]) /* what_to_do != TAB && multiple matches */
2085 mlen
= *matches
[0] ? strlen (matches
[0]) : 0;
2087 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2090 /* If there are more matches, ring the bell to indicate.
2091 If we are in vi mode, Posix.2 says to not ring the bell.
2092 If the `show-all-if-ambiguous' variable is set, display
2093 all the matches immediately. Otherwise, if this was the
2094 only match, and we are hacking files, check the file to
2095 see if it was a directory. If so, and the `mark-directories'
2096 variable is set, add a '/' to the name. If not, and we
2097 are at the end of the line, then add a space. */
2100 if (what_to_do
== '!')
2102 display_matches (matches
);
2105 else if (what_to_do
== '@')
2107 if (nontrivial_lcd
== 0)
2108 display_matches (matches
);
2111 else if (rl_editing_mode
!= vi_mode
)
2112 rl_ding (); /* There are other matches remaining. */
2115 append_to_match (matches
[0], delimiter
, quote_char
, nontrivial_lcd
);
2120 insert_all_matches (matches
, start
, "e_char
);
2124 /* Let's try to insert a single match here if the last completion failed
2125 but this attempt returned a single match. */
2126 if (saved_last_completion_failed
&& matches
[0] && *matches
[0] && matches
[1] == 0)
2128 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2129 append_to_match (matches
[0], delimiter
, quote_char
, nontrivial_lcd
);
2133 if (rl_completion_display_matches_hook
== 0)
2135 _rl_sigcleanup
= _rl_complete_sigcleanup
;
2136 _rl_sigcleanarg
= matches
;
2137 _rl_complete_display_matches_interrupt
= 0;
2139 display_matches (matches
);
2140 if (_rl_complete_display_matches_interrupt
)
2142 matches
= 0; /* already freed by rl_complete_sigcleanup */
2143 _rl_complete_display_matches_interrupt
= 0;
2144 if (rl_signal_event_hook
)
2145 (*rl_signal_event_hook
) (); /* XXX */
2148 _rl_sigcleanarg
= 0;
2152 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do
);
2154 FREE (saved_line_buffer
);
2155 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2156 _rl_free_match_list (matches
);
2157 _rl_reset_completion_state ();
2161 _rl_free_match_list (matches
);
2163 /* Check to see if the line has changed through all of this manipulation. */
2164 if (saved_line_buffer
)
2166 completion_changed_buffer
= strcmp (rl_line_buffer
, saved_line_buffer
) != 0;
2167 xfree (saved_line_buffer
);
2170 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2171 _rl_reset_completion_state ();
2173 RL_CHECK_SIGNALS ();
2177 /***************************************************************/
2179 /* Application-callable completion match generator functions */
2181 /***************************************************************/
2183 /* Return an array of (char *) which is a list of completions for TEXT.
2184 If there are no completions, return a NULL pointer.
2185 The first entry in the returned array is the substitution for TEXT.
2186 The remaining entries are the possible completions.
2187 The array is terminated with a NULL pointer.
2189 ENTRY_FUNCTION is a function of two args, and returns a (char *).
2190 The first argument is TEXT.
2191 The second is a state argument; it should be zero on the first call, and
2192 non-zero on subsequent calls. It returns a NULL pointer to the caller
2193 when there are no more matches.
2196 rl_completion_matches (const char *text
, rl_compentry_func_t
*entry_function
)
2200 /* Number of slots in match_list. */
2201 int match_list_size
;
2203 /* The list of matches. */
2206 /* Number of matches actually found. */
2209 /* Temporary string binder. */
2213 match_list_size
= 10;
2214 match_list
= (char **)xmalloc ((match_list_size
+ 1) * sizeof (char *));
2215 match_list
[1] = (char *)NULL
;
2217 while (string
= (*entry_function
) (text
, matches
))
2219 if (RL_SIG_RECEIVED ())
2221 /* Start at 1 because we don't set matches[0] in this function.
2222 Only free the list members if we're building match list from
2223 rl_filename_completion_function, since we know that doesn't
2224 free the strings it returns. */
2225 if (entry_function
== rl_filename_completion_function
)
2227 for (i
= 1; match_list
[i
]; i
++)
2228 xfree (match_list
[i
]);
2232 match_list_size
= 0;
2234 RL_CHECK_SIGNALS ();
2237 if (matches
+ 1 >= match_list_size
)
2238 match_list
= (char **)xrealloc
2239 (match_list
, ((match_list_size
+= 10) + 1) * sizeof (char *));
2241 if (match_list
== 0)
2242 return (match_list
);
2244 match_list
[++matches
] = string
;
2245 match_list
[matches
+ 1] = (char *)NULL
;
2248 /* If there were any matches, then look through them finding out the
2249 lowest common denominator. That then becomes match_list[0]. */
2251 compute_lcd_of_matches (match_list
, matches
, text
);
2252 else /* There were no matches. */
2255 match_list
= (char **)NULL
;
2257 return (match_list
);
2260 /* A completion function for usernames.
2261 TEXT contains a partial username preceded by a random
2262 character (usually `~'). */
2264 rl_username_completion_function (const char *text
, int state
)
2266 #if defined (__WIN32__) || defined (__OPENNT)
2267 return (char *)NULL
;
2268 #else /* !__WIN32__ && !__OPENNT) */
2269 static char *username
= (char *)NULL
;
2270 static struct passwd
*entry
;
2271 static int namelen
, first_char
, first_char_loc
;
2279 first_char_loc
= first_char
== '~';
2281 username
= savestring (&text
[first_char_loc
]);
2282 namelen
= strlen (username
);
2283 #if defined (HAVE_GETPWENT)
2288 #if defined (HAVE_GETPWENT)
2289 while (entry
= getpwent ())
2291 /* Null usernames should result in all users as possible completions. */
2292 if (namelen
== 0 || (STREQN (username
, entry
->pw_name
, namelen
)))
2299 #if defined (HAVE_GETPWENT)
2302 return ((char *)NULL
);
2306 value
= (char *)xmalloc (2 + strlen (entry
->pw_name
));
2310 strcpy (value
+ first_char_loc
, entry
->pw_name
);
2312 if (first_char
== '~')
2313 rl_filename_completion_desired
= 1;
2317 #endif /* !__WIN32__ && !__OPENNT */
2320 /* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME
2321 (FILENAME_LEN). If _rl_completion_case_fold is set, compare without
2322 regard to the alphabetic case of characters. If
2323 _rl_completion_case_map is set, make `-' and `_' equivalent. CONVFN is
2324 the possibly-converted directory entry; FILENAME is what the user typed. */
2326 complete_fncmp (const char *convfn
, int convlen
, const char *filename
, int filename_len
)
2328 register char *s1
, *s2
;
2330 #if defined (HANDLE_MULTIBYTE)
2336 #if defined (HANDLE_MULTIBYTE)
2337 memset (&ps1
, 0, sizeof (mbstate_t));
2338 memset (&ps2
, 0, sizeof (mbstate_t));
2341 if (filename_len
== 0)
2343 if (convlen
< filename_len
)
2347 s1
= (char *)convfn
;
2348 s2
= (char *)filename
;
2350 /* Otherwise, if these match up to the length of filename, then
2352 if (_rl_completion_case_fold
&& _rl_completion_case_map
)
2354 /* Case-insensitive comparison treating _ and - as equivalent */
2355 #if defined (HANDLE_MULTIBYTE)
2356 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2360 v1
= mbrtowc (&wc1
, s1
, convlen
, &ps1
);
2361 v2
= mbrtowc (&wc2
, s2
, filename_len
, &ps2
);
2362 if (v1
== 0 && v2
== 0)
2364 else if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
2366 if (*s1
!= *s2
) /* do byte comparison */
2368 else if ((*s1
== '-' || *s1
== '_') && (*s2
== '-' || *s2
== '_'))
2373 wc1
= towlower (wc1
);
2374 wc2
= towlower (wc2
);
2378 if ((wc1
== L
'-' || wc1
== L
'_') && (wc2
== L
'-' || wc2
== L
'_'))
2390 d
= _rl_to_lower (*s1
) - _rl_to_lower (*s2
);
2391 /* *s1 == [-_] && *s2 == [-_] */
2392 if ((*s1
== '-' || *s1
== '_') && (*s2
== '-' || *s2
== '_'))
2396 s1
++; s2
++; /* already checked convlen >= filename_len */
2403 else if (_rl_completion_case_fold
)
2405 #if defined (HANDLE_MULTIBYTE)
2406 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2410 v1
= mbrtowc (&wc1
, s1
, convlen
, &ps1
);
2411 v2
= mbrtowc (&wc2
, s2
, filename_len
, &ps2
);
2412 if (v1
== 0 && v2
== 0)
2414 else if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
2416 if (*s1
!= *s2
) /* do byte comparison */
2421 wc1
= towlower (wc1
);
2422 wc2
= towlower (wc2
);
2434 if ((_rl_to_lower (convfn
[0]) == _rl_to_lower (filename
[0])) &&
2435 (convlen
>= filename_len
) &&
2436 (_rl_strnicmp (filename
, convfn
, filename_len
) == 0))
2441 if ((convfn
[0] == filename
[0]) &&
2442 (convlen
>= filename_len
) &&
2443 (strncmp (filename
, convfn
, filename_len
) == 0))
2449 /* Okay, now we write the entry_function for filename completion. In the
2450 general case. Note that completion in the shell is a little different
2451 because of all the pathnames that must be followed when looking up the
2452 completion for a command. */
2454 rl_filename_completion_function (const char *text
, int state
)
2456 static DIR *directory
= (DIR *)NULL
;
2457 static char *filename
= (char *)NULL
;
2458 static char *dirname
= (char *)NULL
;
2459 static char *users_dirname
= (char *)NULL
;
2460 static int filename_len
;
2461 char *temp
, *dentry
, *convfn
;
2462 int dirlen
, dentlen
, convlen
;
2464 struct dirent
*entry
;
2466 /* If we don't have any state, then do some initialization. */
2469 /* If we were interrupted before closing the directory or reading
2470 all of its contents, close it. */
2473 closedir (directory
);
2474 directory
= (DIR *)NULL
;
2478 FREE (users_dirname
);
2480 filename
= savestring (text
);
2483 dirname
= savestring (text
);
2485 temp
= strrchr (dirname
, '/');
2487 #if defined (__MSDOS__) || defined (_WIN32)
2488 /* special hack for //X/... */
2489 if (dirname
[0] == '/' && dirname
[1] == '/' && ISALPHA ((unsigned char)dirname
[2]) && dirname
[3] == '/')
2490 temp
= strrchr (dirname
+ 3, '/');
2495 strcpy (filename
, ++temp
);
2498 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN__))
2499 /* searches from current directory on the drive */
2500 else if (ISALPHA ((unsigned char)dirname
[0]) && dirname
[1] == ':')
2502 strcpy (filename
, dirname
+ 2);
2512 /* We aren't done yet. We also support the "~user" syntax. */
2514 /* Save the version of the directory that the user typed, dequoting
2516 if (rl_completion_found_quote
&& rl_filename_dequoting_function
)
2517 users_dirname
= (*rl_filename_dequoting_function
) (dirname
, rl_completion_quote_character
);
2519 users_dirname
= savestring (dirname
);
2522 if (*dirname
== '~')
2524 temp
= tilde_expand (dirname
);
2530 /* We have saved the possibly-dequoted version of the directory name
2531 the user typed. Now transform the directory name we're going to
2532 pass to opendir(2). The directory rewrite hook modifies only the
2533 directory name; the directory completion hook modifies both the
2534 directory name passed to opendir(2) and the version the user
2535 typed. Both the directory completion and rewrite hooks should perform
2536 any necessary dequoting. The hook functions return 1 if they modify
2537 the directory name argument. If either hook returns 0, it should
2538 not modify the directory name pointer passed as an argument. */
2539 if (rl_directory_rewrite_hook
)
2540 (*rl_directory_rewrite_hook
) (&dirname
);
2541 else if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&dirname
))
2543 xfree (users_dirname
);
2544 users_dirname
= savestring (dirname
);
2546 else if (tilde_dirname
== 0 && rl_completion_found_quote
&& rl_filename_dequoting_function
)
2548 /* delete single and double quotes */
2550 dirname
= savestring (users_dirname
);
2552 directory
= opendir (dirname
);
2554 /* Now dequote a non-null filename. FILENAME will not be NULL, but may
2556 if (*filename
&& rl_completion_found_quote
&& rl_filename_dequoting_function
)
2558 /* delete single and double quotes */
2559 temp
= (*rl_filename_dequoting_function
) (filename
, rl_completion_quote_character
);
2563 filename_len
= strlen (filename
);
2565 rl_filename_completion_desired
= 1;
2568 /* At this point we should entertain the possibility of hacking wildcarded
2569 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2570 contains globbing characters, then build an array of directories, and
2571 then map over that list while completing. */
2572 /* *** UNIMPLEMENTED *** */
2574 /* Now that we have some state, we can read the directory. */
2576 entry
= (struct dirent
*)NULL
;
2577 while (directory
&& (entry
= readdir (directory
)))
2579 convfn
= dentry
= entry
->d_name
;
2580 convlen
= dentlen
= D_NAMLEN (entry
);
2582 if (rl_filename_rewrite_hook
)
2584 convfn
= (*rl_filename_rewrite_hook
) (dentry
, dentlen
);
2585 convlen
= (convfn
== dentry
) ? dentlen
: strlen (convfn
);
2588 /* Special case for no filename. If the user has disabled the
2589 `match-hidden-files' variable, skip filenames beginning with `.'.
2590 All other entries except "." and ".." match. */
2591 if (filename_len
== 0)
2593 if (_rl_match_hidden_files
== 0 && HIDDEN_FILE (convfn
))
2596 if (convfn
[0] != '.' ||
2597 (convfn
[1] && (convfn
[1] != '.' || convfn
[2])))
2602 if (complete_fncmp (convfn
, convlen
, filename
, filename_len
))
2611 closedir (directory
);
2612 directory
= (DIR *)NULL
;
2617 dirname
= (char *)NULL
;
2622 filename
= (char *)NULL
;
2626 xfree (users_dirname
);
2627 users_dirname
= (char *)NULL
;
2630 return (char *)NULL
;
2634 /* dirname && (strcmp (dirname, ".") != 0) */
2635 if (dirname
&& (dirname
[0] != '.' || dirname
[1]))
2637 if (rl_complete_with_tilde_expansion
&& *users_dirname
== '~')
2639 dirlen
= strlen (dirname
);
2640 temp
= (char *)xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
2641 strcpy (temp
, dirname
);
2642 /* Canonicalization cuts off any final slash present. We
2643 may need to add it back. */
2644 if (dirname
[dirlen
- 1] != '/')
2646 temp
[dirlen
++] = '/';
2647 temp
[dirlen
] = '\0';
2652 dirlen
= strlen (users_dirname
);
2653 temp
= (char *)xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
2654 strcpy (temp
, users_dirname
);
2655 /* Make sure that temp has a trailing slash here. */
2656 if (users_dirname
[dirlen
- 1] != '/')
2657 temp
[dirlen
++] = '/';
2660 strcpy (temp
+ dirlen
, convfn
);
2663 temp
= savestring (convfn
);
2665 if (convfn
!= dentry
)
2672 /* An initial implementation of a menu completion function a la tcsh. The
2673 first time (if the last readline command was not rl_old_menu_complete), we
2674 generate the list of matches. This code is very similar to the code in
2675 rl_complete_internal -- there should be a way to combine the two. Then,
2676 for each item in the list of matches, we insert the match in an undoable
2677 fashion, with the appropriate character appended (this happens on the
2678 second and subsequent consecutive calls to rl_old_menu_complete). When we
2679 hit the end of the match list, we restore the original unmatched text,
2680 ring the bell, and reset the counter to zero. */
2682 rl_old_menu_complete (int count
, int invoking_key
)
2684 rl_compentry_func_t
*our_func
;
2685 int matching_filenames
, found_quote
;
2687 static char *orig_text
;
2688 static char **matches
= (char **)0;
2689 static int match_list_index
= 0;
2690 static int match_list_size
= 0;
2691 static int orig_start
, orig_end
;
2692 static char quote_char
;
2693 static int delimiter
;
2695 /* The first time through, we generate the list of matches and set things
2696 up to insert them. */
2697 if (rl_last_func
!= rl_old_menu_complete
)
2699 /* Clean up from previous call, if any. */
2702 _rl_free_match_list (matches
);
2704 match_list_index
= match_list_size
= 0;
2705 matches
= (char **)NULL
;
2707 rl_completion_invoking_key
= invoking_key
;
2709 RL_SETSTATE(RL_STATE_COMPLETING
);
2711 /* Only the completion entry function can change these. */
2712 set_completion_defaults ('%');
2714 our_func
= rl_menu_completion_entry_function
;
2716 our_func
= rl_completion_entry_function
2717 ? rl_completion_entry_function
2718 : rl_filename_completion_function
;
2720 /* We now look backwards for the start of a filename/variable word. */
2721 orig_end
= rl_point
;
2722 found_quote
= delimiter
= 0;
2726 /* This (possibly) changes rl_point. If it returns a non-zero char,
2727 we know we have an open quote. */
2728 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2730 orig_start
= rl_point
;
2731 rl_point
= orig_end
;
2733 orig_text
= rl_copy_text (orig_start
, orig_end
);
2734 matches
= gen_completion_matches (orig_text
, orig_start
, orig_end
,
2735 our_func
, found_quote
, quote_char
);
2737 /* If we are matching filenames, the attempted completion function will
2738 have set rl_filename_completion_desired to a non-zero value. The basic
2739 rl_filename_completion_function does this. */
2740 matching_filenames
= rl_filename_completion_desired
;
2742 if (matches
== 0 || postprocess_matches (&matches
, matching_filenames
) == 0)
2746 matches
= (char **)0;
2748 orig_text
= (char *)0;
2749 completion_changed_buffer
= 0;
2750 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2754 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2756 for (match_list_size
= 0; matches
[match_list_size
]; match_list_size
++)
2758 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2759 code below should take care of it. */
2761 if (match_list_size
> 1 && _rl_complete_show_all
)
2762 display_matches (matches
);
2765 /* Now we have the list of matches. Replace the text between
2766 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2767 matches[match_list_index], and add any necessary closing char. */
2769 if (matches
== 0 || match_list_size
== 0)
2773 matches
= (char **)0;
2774 completion_changed_buffer
= 0;
2778 match_list_index
+= count
;
2779 if (match_list_index
< 0)
2781 while (match_list_index
< 0)
2782 match_list_index
+= match_list_size
;
2785 match_list_index
%= match_list_size
;
2787 if (match_list_index
== 0 && match_list_size
> 1)
2790 insert_match (orig_text
, orig_start
, MULT_MATCH
, "e_char
);
2794 insert_match (matches
[match_list_index
], orig_start
, SINGLE_MATCH
, "e_char
);
2795 append_to_match (matches
[match_list_index
], delimiter
, quote_char
,
2796 compare_match (orig_text
, matches
[match_list_index
]));
2799 completion_changed_buffer
= 1;
2803 /* The current version of menu completion.
2804 The differences between this function and the original are:
2806 1. It honors the maximum number of completions variable (completion-query-items)
2807 2. It appends to the word as usual if there is only one match
2808 3. It displays the common prefix if there is one, and makes it the first menu
2809 choice if the menu-complete-display-prefix option is enabled
2813 rl_menu_complete (int count
, int ignore
)
2815 rl_compentry_func_t
*our_func
;
2816 int matching_filenames
, found_quote
;
2818 static char *orig_text
;
2819 static char **matches
= (char **)0;
2820 static int match_list_index
= 0;
2821 static int match_list_size
= 0;
2822 static int nontrivial_lcd
= 0;
2823 static int full_completion
= 0; /* set to 1 if menu completion should reinitialize on next call */
2824 static int orig_start
, orig_end
;
2825 static char quote_char
;
2826 static int delimiter
, cstate
;
2828 /* The first time through, we generate the list of matches and set things
2829 up to insert them. */
2830 if ((rl_last_func
!= rl_menu_complete
&& rl_last_func
!= rl_backward_menu_complete
) || full_completion
)
2832 /* Clean up from previous call, if any. */
2835 _rl_free_match_list (matches
);
2837 match_list_index
= match_list_size
= 0;
2838 matches
= (char **)NULL
;
2840 full_completion
= 0;
2842 RL_SETSTATE(RL_STATE_COMPLETING
);
2844 /* Only the completion entry function can change these. */
2845 set_completion_defaults ('%');
2847 our_func
= rl_menu_completion_entry_function
;
2849 our_func
= rl_completion_entry_function
2850 ? rl_completion_entry_function
2851 : rl_filename_completion_function
;
2853 /* We now look backwards for the start of a filename/variable word. */
2854 orig_end
= rl_point
;
2855 found_quote
= delimiter
= 0;
2859 /* This (possibly) changes rl_point. If it returns a non-zero char,
2860 we know we have an open quote. */
2861 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2863 orig_start
= rl_point
;
2864 rl_point
= orig_end
;
2866 orig_text
= rl_copy_text (orig_start
, orig_end
);
2867 matches
= gen_completion_matches (orig_text
, orig_start
, orig_end
,
2868 our_func
, found_quote
, quote_char
);
2870 nontrivial_lcd
= matches
&& compare_match (orig_text
, matches
[0]) != 0;
2872 /* If we are matching filenames, the attempted completion function will
2873 have set rl_filename_completion_desired to a non-zero value. The basic
2874 rl_filename_completion_function does this. */
2875 matching_filenames
= rl_filename_completion_desired
;
2877 if (matches
== 0 || postprocess_matches (&matches
, matching_filenames
) == 0)
2881 matches
= (char **)0;
2883 orig_text
= (char *)0;
2884 completion_changed_buffer
= 0;
2885 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2889 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2891 for (match_list_size
= 0; matches
[match_list_size
]; match_list_size
++)
2894 if (match_list_size
== 0)
2898 matches
= (char **)0;
2899 match_list_index
= 0;
2900 completion_changed_buffer
= 0;
2904 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2905 code below should take care of it. */
2908 insert_match (matches
[0], orig_start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2909 orig_end
= orig_start
+ strlen (matches
[0]);
2910 completion_changed_buffer
= STREQ (orig_text
, matches
[0]) == 0;
2913 if (match_list_size
> 1 && _rl_complete_show_all
)
2915 display_matches (matches
);
2916 /* If there are so many matches that the user has to be asked
2917 whether or not he wants to see the matches, menu completion
2919 if (rl_completion_query_items
> 0 && match_list_size
>= rl_completion_query_items
)
2923 matches
= (char **)0;
2924 full_completion
= 1;
2927 else if (_rl_menu_complete_prefix_first
)
2933 else if (match_list_size
<= 1)
2935 append_to_match (matches
[0], delimiter
, quote_char
, nontrivial_lcd
);
2936 full_completion
= 1;
2939 else if (_rl_menu_complete_prefix_first
&& match_list_size
> 1)
2946 /* Now we have the list of matches. Replace the text between
2947 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2948 matches[match_list_index], and add any necessary closing char. */
2950 if (matches
== 0 || match_list_size
== 0)
2954 matches
= (char **)0;
2955 completion_changed_buffer
= 0;
2959 match_list_index
+= count
;
2960 if (match_list_index
< 0)
2962 while (match_list_index
< 0)
2963 match_list_index
+= match_list_size
;
2966 match_list_index
%= match_list_size
;
2968 if (match_list_index
== 0 && match_list_size
> 1)
2971 insert_match (matches
[0], orig_start
, MULT_MATCH
, "e_char
);
2975 insert_match (matches
[match_list_index
], orig_start
, SINGLE_MATCH
, "e_char
);
2976 append_to_match (matches
[match_list_index
], delimiter
, quote_char
,
2977 compare_match (orig_text
, matches
[match_list_index
]));
2980 completion_changed_buffer
= 1;
2985 rl_backward_menu_complete (int count
, int key
)
2987 /* Positive arguments to backward-menu-complete translate into negative
2988 arguments for menu-complete, and vice versa. */
2989 return (rl_menu_complete (-count
, key
));