1 /* complete.c -- filename completion for readline. */
3 /* Copyright (C) 1987-2015 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 (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_SYS_FILE_H)
31 # include <sys/file.h>
36 #if defined (HAVE_UNISTD_H)
38 #endif /* HAVE_UNISTD_H */
40 #if defined (HAVE_STDLIB_H)
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
53 #if defined (HAVE_PWD_H)
58 #include "posixstat.h"
60 /* System-specific feature definitions and include files. */
64 /* Some standard library routines. */
67 #include "rlprivate.h"
69 #if defined (COLOR_SUPPORT)
74 typedef int QSFUNC (const void *, const void *);
76 typedef int QSFUNC ();
85 /* Unix version of a hidden file. Could be different on other systems. */
86 #define HIDDEN_FILE(fname) ((fname)[0] == '.')
88 /* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
90 #if defined (HAVE_GETPWENT) && (!defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE))
91 extern struct passwd *getpwent PARAMS((void));
92 #endif /* HAVE_GETPWENT && (!HAVE_GETPW_DECLS || _POSIX_SOURCE) */
94 /* If non-zero, then this is the address of a function to call when
95 completing a word would normally display the list of possible matches.
96 This function is called instead of actually doing the display.
97 It takes three arguments: (char **matches, int num_matches, int max_length)
98 where MATCHES is the array of strings that matched, NUM_MATCHES is the
99 number of strings in that array, and MAX_LENGTH is the length of the
100 longest string in that array. */
101 rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)NULL;
103 #if defined (VISIBLE_STATS) || defined (COLOR_SUPPORT)
109 #if defined (VISIBLE_STATS)
110 static int stat_char PARAMS((char *));
113 #if defined (COLOR_SUPPORT)
114 static int colored_stat_start PARAMS((const char *));
115 static void colored_stat_end PARAMS((void));
116 static int colored_prefix_start PARAMS((void));
117 static void colored_prefix_end PARAMS((void));
120 static int path_isdir PARAMS((const char *));
122 static char *rl_quote_filename PARAMS((char *, int, char *));
124 static void _rl_complete_sigcleanup PARAMS((int, void *));
126 static void set_completion_defaults PARAMS((int));
127 static int get_y_or_n PARAMS((int));
128 static int _rl_internal_pager PARAMS((int));
129 static char *printable_part PARAMS((char *));
130 static int fnwidth PARAMS((const char *));
131 static int fnprint PARAMS((const char *, int, const char *));
132 static int print_filename PARAMS((char *, char *, int));
134 static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int));
136 static char **remove_duplicate_matches PARAMS((char **));
137 static void insert_match PARAMS((char *, int, int, char *));
138 static int append_to_match PARAMS((char *, int, int, int));
139 static void insert_all_matches PARAMS((char **, int, char *));
140 static int complete_fncmp PARAMS((const char *, int, const char *, int));
141 static void display_matches PARAMS((char **));
142 static int compute_lcd_of_matches PARAMS((char **, int, const char *));
143 static int postprocess_matches PARAMS((char ***, int));
144 static int complete_get_screenwidth PARAMS((void));
146 static char *make_quoted_replacement PARAMS((char *, int, char *));
148 /* **************************************************************** */
150 /* Completion matching, from readline's point of view. */
152 /* **************************************************************** */
154 /* Variables known only to the readline library. */
156 /* If non-zero, non-unique completions always show the list of matches. */
157 int _rl_complete_show_all = 0;
159 /* If non-zero, non-unique completions show the list of matches, unless it
160 is not possible to do partial completion and modify the line. */
161 int _rl_complete_show_unmodified = 0;
163 /* If non-zero, completed directory names have a slash appended. */
164 int _rl_complete_mark_directories = 1;
166 /* If non-zero, the symlinked directory completion behavior introduced in
167 readline-4.2a is disabled, and symlinks that point to directories have
168 a slash appended (subject to the value of _rl_complete_mark_directories).
169 This is user-settable via the mark-symlinked-directories variable. */
170 int _rl_complete_mark_symlink_dirs = 0;
172 /* If non-zero, completions are printed horizontally in alphabetical order,
174 int _rl_print_completions_horizontally;
176 /* Non-zero means that case is not significant in filename completion. */
177 #if (defined (__MSDOS__) && !defined (__DJGPP__)) || (defined (_WIN32) && !defined (__CYGWIN__))
178 int _rl_completion_case_fold = 1;
180 int _rl_completion_case_fold = 0;
183 /* Non-zero means that `-' and `_' are equivalent when comparing filenames
185 int _rl_completion_case_map = 0;
187 /* If zero, don't match hidden files (filenames beginning with a `.' on
188 Unix) when doing filename completion. */
189 int _rl_match_hidden_files = 1;
191 /* Length in characters of a common prefix replaced with an ellipsis (`...')
192 when displaying completion matches. Matches whose printable portion has
193 more than this number of displaying characters in common will have the common
194 display prefix replaced with an ellipsis. */
195 int _rl_completion_prefix_display_length = 0;
197 /* The readline-private number of screen columns to use when displaying
198 matches. If < 0 or > _rl_screenwidth, it is ignored. */
199 int _rl_completion_columns = -1;
201 /* Global variables available to applications using readline. */
203 #if defined (VISIBLE_STATS)
204 /* Non-zero means add an additional character to each filename displayed
205 during listing completion iff rl_filename_completion_desired which helps
206 to indicate the type of file being listed. */
207 int rl_visible_stats = 0;
208 #endif /* VISIBLE_STATS */
210 #if defined (COLOR_SUPPORT)
211 /* Non-zero means to use colors to indicate file type when listing possible
212 completions. The colors used are taken from $LS_COLORS, if set. */
213 int _rl_colored_stats = 0;
215 /* Non-zero means to use a color (currently magenta) to indicate the common
216 prefix of a set of possible word completions. */
217 int _rl_colored_completion_prefix = 0;
220 /* If non-zero, when completing in the middle of a word, don't insert
221 characters from the match that match characters following point in
222 the word. This means, for instance, completing when the cursor is
223 after the `e' in `Makefile' won't result in `Makefilefile'. */
224 int _rl_skip_completed_text = 0;
226 /* If non-zero, menu completion displays the common prefix first in the
227 cycle of possible completions instead of the last. */
228 int _rl_menu_complete_prefix_first = 0;
230 /* If non-zero, then this is the address of a function to call when
231 completing on a directory name. The function is called with
232 the address of a string (the current directory name) as an arg. */
233 rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
235 rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
237 rl_icppfunc_t *rl_filename_stat_hook = (rl_icppfunc_t *)NULL;
239 /* If non-zero, this is the address of a function to call when reading
240 directory entries from the filesystem for completion and comparing
241 them to the partial word to be completed. The function should
242 either return its first argument (if no conversion takes place) or
243 newly-allocated memory. This can, for instance, convert filenames
244 between character sets for comparison against what's typed at the
245 keyboard. The returned value is what is added to the list of
246 matches. The second argument is the length of the filename to be
248 rl_dequote_func_t *rl_filename_rewrite_hook = (rl_dequote_func_t *)NULL;
250 /* Non-zero means readline completion functions perform tilde expansion. */
251 int rl_complete_with_tilde_expansion = 0;
253 /* Pointer to the generator function for completion_matches ().
254 NULL means to use rl_filename_completion_function (), the default filename
256 rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL;
258 /* Pointer to generator function for rl_menu_complete (). NULL means to use
259 *rl_completion_entry_function (see above). */
260 rl_compentry_func_t *rl_menu_completion_entry_function = (rl_compentry_func_t *)NULL;
262 /* Pointer to alternative function to create matches.
263 Function is called with TEXT, START, and END.
264 START and END are indices in RL_LINE_BUFFER saying what the boundaries
266 If this function exists and returns NULL then call the value of
267 rl_completion_entry_function to try to match, otherwise use the
268 array of strings returned. */
269 rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL;
271 /* Non-zero means to suppress normal filename completion after the
272 user-specified completion function has been called. */
273 int rl_attempted_completion_over = 0;
275 /* Set to a character indicating the type of completion being performed
276 by rl_complete_internal, available for use by application completion
278 int rl_completion_type = 0;
280 /* Up to this many items will be displayed in response to a
281 possible-completions call. After that, we ask the user if
282 she is sure she wants to see them all. A negative value means
284 int rl_completion_query_items = 100;
286 int _rl_page_completions = 1;
288 /* The basic list of characters that signal a break between words for the
289 completer routine. The contents of this variable is what breaks words
290 in the shell, i.e. " \t\n\"\\'`@$><=" */
291 const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
293 /* List of basic quoting characters. */
294 const char *rl_basic_quote_characters = "\"'";
296 /* The list of characters that signal a break between words for
297 rl_complete_internal. The default list is the contents of
298 rl_basic_word_break_characters. */
299 /*const*/ char *rl_completer_word_break_characters = (/*const*/ char *)NULL;
301 /* Hook function to allow an application to set the completion word
302 break characters before readline breaks up the line. Allows
303 position-dependent word break characters. */
304 rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL;
306 /* List of characters which can be used to quote a substring of the line.
307 Completion occurs on the entire substring, and within the substring
308 rl_completer_word_break_characters are treated as any other character,
309 unless they also appear within this list. */
310 const char *rl_completer_quote_characters = (const char *)NULL;
312 /* List of characters that should be quoted in filenames by the completer. */
313 const char *rl_filename_quote_characters = (const char *)NULL;
315 /* List of characters that are word break characters, but should be left
316 in TEXT when it is passed to the completion function. The shell uses
317 this to help determine what kind of completing to do. */
318 const char *rl_special_prefixes = (const char *)NULL;
320 /* If non-zero, then disallow duplicates in the matches. */
321 int rl_ignore_completion_duplicates = 1;
323 /* Non-zero means that the results of the matches are to be treated
324 as filenames. This is ALWAYS zero on entry, and can only be changed
325 within a completion entry finder function. */
326 int rl_filename_completion_desired = 0;
328 /* Non-zero means that the results of the matches are to be quoted using
329 double quotes (or an application-specific quoting mechanism) if the
330 filename contains any characters in rl_filename_quote_chars. This is
331 ALWAYS non-zero on entry, and can only be changed within a completion
332 entry finder function. */
333 int rl_filename_quoting_desired = 1;
335 /* This function, if defined, is called by the completer when real
336 filename completion is done, after all the matching names have been
337 generated. It is passed a (char**) known as matches in the code below.
338 It consists of a NULL-terminated array of pointers to potential
339 matching strings. The 1st element (matches[0]) is the maximal
340 substring that is common to all matches. This function can re-arrange
341 the list of matches as required, but all elements of the array must be
342 free()'d if they are deleted. The main intent of this function is
343 to implement FIGNORE a la SunOS csh. */
344 rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL;
346 /* Set to a function to quote a filename in an application-specific fashion.
347 Called with the text to quote, the type of match found (single or multiple)
348 and a pointer to the quoting character to be used, which the function can
350 rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename;
352 /* Function to call to remove quoting characters from a filename. Called
353 before completion is attempted, so the embedded quotes do not interfere
354 with matching names in the file system. Readline doesn't do anything
355 with this; it's set only by applications. */
356 rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL;
358 /* Function to call to decide whether or not a word break character is
359 quoted. If a character is quoted, it does not break words for the
361 rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL;
363 /* If non-zero, the completion functions don't append anything except a
364 possible closing quote. This is set to 0 by rl_complete_internal and
365 may be changed by an application-specific completion function. */
366 int rl_completion_suppress_append = 0;
368 /* Character appended to completed words when at the end of the line. The
369 default is a space. */
370 int rl_completion_append_character = ' ';
372 /* If non-zero, the completion functions don't append any closing quote.
373 This is set to 0 by rl_complete_internal and may be changed by an
374 application-specific completion function. */
375 int rl_completion_suppress_quote = 0;
377 /* Set to any quote character readline thinks it finds before any application
378 completion function is called. */
379 int rl_completion_quote_character;
381 /* Set to a non-zero value if readline found quoting anywhere in the word to
382 be completed; set before any application completion function is called. */
383 int rl_completion_found_quote;
385 /* If non-zero, a slash will be appended to completed filenames that are
386 symbolic links to directory names, subject to the value of the
387 mark-directories variable (which is user-settable). This exists so
388 that application completion functions can override the user's preference
389 (set via the mark-symlinked-directories variable) if appropriate.
390 It's set to the value of _rl_complete_mark_symlink_dirs in
391 rl_complete_internal before any application-specific completion
392 function is called, so without that function doing anything, the user's
393 preferences are honored. */
394 int rl_completion_mark_symlink_dirs;
396 /* If non-zero, inhibit completion (temporarily). */
397 int rl_inhibit_completion;
399 /* Set to the last key used to invoke one of the completion functions */
400 int rl_completion_invoking_key;
402 /* If non-zero, sort the completion matches. On by default. */
403 int rl_sort_completion_matches = 1;
405 /* Variables local to this file. */
407 /* Local variable states what happened during the last completion attempt. */
408 static int completion_changed_buffer;
410 /* The result of the query to the user about displaying completion matches */
411 static int completion_y_or_n;
413 static int _rl_complete_display_matches_interrupt = 0;
415 /*************************************/
417 /* Bindable completion functions */
419 /*************************************/
421 /* Complete the word at or before point. You have supplied the function
422 that does the initial simple matching selection algorithm (see
423 rl_completion_matches ()). The default is to do filename completion. */
425 rl_complete (ignore, invoking_key)
426 int ignore, invoking_key;
428 rl_completion_invoking_key = invoking_key;
430 if (rl_inhibit_completion)
431 return (_rl_insert_char (ignore, invoking_key));
432 else if (rl_last_func == rl_complete && !completion_changed_buffer)
433 return (rl_complete_internal ('?'));
434 else if (_rl_complete_show_all)
435 return (rl_complete_internal ('!'));
436 else if (_rl_complete_show_unmodified)
437 return (rl_complete_internal ('@'));
439 return (rl_complete_internal (TAB));
442 /* List the possible completions. See description of rl_complete (). */
444 rl_possible_completions (ignore, invoking_key)
445 int ignore, invoking_key;
447 rl_completion_invoking_key = invoking_key;
448 return (rl_complete_internal ('?'));
452 rl_insert_completions (ignore, invoking_key)
453 int ignore, invoking_key;
455 rl_completion_invoking_key = invoking_key;
456 return (rl_complete_internal ('*'));
459 /* Return the correct value to pass to rl_complete_internal performing
460 the same tests as rl_complete. This allows consecutive calls to an
461 application's completion function to list possible completions and for
462 an application-specific completion function to honor the
463 show-all-if-ambiguous readline variable. */
465 rl_completion_mode (cfunc)
466 rl_command_func_t *cfunc;
468 if (rl_last_func == cfunc && !completion_changed_buffer)
470 else if (_rl_complete_show_all)
472 else if (_rl_complete_show_unmodified)
478 /************************************/
480 /* Completion utility functions */
482 /************************************/
484 /* Reset readline state on a signal or other event. */
486 _rl_reset_completion_state ()
488 rl_completion_found_quote = 0;
489 rl_completion_quote_character = 0;
493 _rl_complete_sigcleanup (sig, ptr)
497 if (sig == SIGINT) /* XXX - for now */
499 _rl_free_match_list ((char **)ptr);
500 _rl_complete_display_matches_interrupt = 1;
504 /* Set default values for readline word completion. These are the variables
505 that application completion functions can change or inspect. */
507 set_completion_defaults (what_to_do)
510 /* Only the completion entry function can change these. */
511 rl_filename_completion_desired = 0;
512 rl_filename_quoting_desired = 1;
513 rl_completion_type = what_to_do;
514 rl_completion_suppress_append = rl_completion_suppress_quote = 0;
515 rl_completion_append_character = ' ';
517 /* The completion entry function may optionally change this. */
518 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
520 /* Reset private state. */
521 _rl_complete_display_matches_interrupt = 0;
524 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
526 get_y_or_n (for_pager)
531 /* For now, disable pager in callback mode, until we later convert to state
532 driven functions. Have to wait until next major version to add new
533 state definition, since it will change value of RL_STATE_DONE. */
534 #if defined (READLINE_CALLBACKS)
535 if (RL_ISSTATE (RL_STATE_CALLBACK))
541 RL_SETSTATE(RL_STATE_MOREINPUT);
543 RL_UNSETSTATE(RL_STATE_MOREINPUT);
545 if (c == 'y' || c == 'Y' || c == ' ')
547 if (c == 'n' || c == 'N' || c == RUBOUT)
549 if (c == ABORT_CHAR || c < 0)
550 _rl_abort_internal ();
551 if (for_pager && (c == NEWLINE || c == RETURN))
553 if (for_pager && (c == 'q' || c == 'Q'))
560 _rl_internal_pager (lines)
565 fprintf (rl_outstream, "--More--");
566 fflush (rl_outstream);
568 _rl_erase_entire_line ();
578 path_isdir (filename)
579 const char *filename;
583 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
586 #if defined (VISIBLE_STATS)
587 /* Return the character which best describes FILENAME.
588 `@' for symbolic links
593 `%' for character special devices
594 `#' for block special devices */
604 /* Short-circuit a //server on cygwin, since that will always behave as
607 if (filename[0] == '/' && filename[1] == '/' && strchr (filename+2, '/') == 0)
612 if (rl_filename_stat_hook)
614 f = savestring (filename);
615 (*rl_filename_stat_hook) (&f);
621 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
622 r = lstat (fn, &finfo);
624 r = stat (fn, &finfo);
631 if (S_ISDIR (finfo.st_mode))
633 #if defined (S_ISCHR)
634 else if (S_ISCHR (finfo.st_mode))
637 #if defined (S_ISBLK)
638 else if (S_ISBLK (finfo.st_mode))
641 #if defined (S_ISLNK)
642 else if (S_ISLNK (finfo.st_mode))
645 #if defined (S_ISSOCK)
646 else if (S_ISSOCK (finfo.st_mode))
648 #endif /* S_ISSOCK */
649 #if defined (S_ISFIFO)
650 else if (S_ISFIFO (finfo.st_mode))
653 else if (S_ISREG (finfo.st_mode))
655 #if defined (_WIN32) && !defined (__CYGWIN__)
658 /* Windows doesn't do access and X_OK; check file extension instead */
659 ext = strrchr (fn, '.');
660 if (ext && (_rl_stricmp (ext, ".exe") == 0 ||
661 _rl_stricmp (ext, ".cmd") == 0 ||
662 _rl_stricmp (ext, ".bat") == 0 ||
663 _rl_stricmp (ext, ".com") == 0))
666 if (access (filename, X_OK) == 0)
674 #endif /* VISIBLE_STATS */
676 #if defined (COLOR_SUPPORT)
678 colored_stat_start (filename)
679 const char *filename;
681 _rl_set_normal_color ();
682 return (_rl_print_color_indicator (filename));
688 _rl_prep_non_filename_text ();
689 _rl_put_indicator (&_rl_color_indicator[C_CLR_TO_EOL]);
693 colored_prefix_start ()
695 _rl_set_normal_color ();
696 return (_rl_print_prefix_color ());
700 colored_prefix_end ()
702 colored_stat_end (); /* for now */
706 /* Return the portion of PATHNAME that should be output when listing
707 possible completions. If we are hacking filename completion, we
708 are only interested in the basename, the portion following the
709 final slash. Otherwise, we return what we were passed. Since
710 printing empty strings is not very informative, if we're doing
711 filename completion, and the basename is the empty string, we look
712 for the previous slash and return the portion following that. If
713 there's no previous slash, we just return what we were passed. */
715 printable_part (pathname)
720 if (rl_filename_completion_desired == 0) /* don't need to do anything */
723 temp = strrchr (pathname, '/');
724 #if defined (__MSDOS__) || defined (_WIN32)
725 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
729 if (temp == 0 || *temp == '\0')
731 else if (temp[1] == 0 && temp == pathname)
733 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
734 Look for a previous slash and, if one is found, return the portion
735 following that slash. If there's no previous slash, just return the
736 pathname we were passed. */
737 else if (temp[1] == '\0')
739 for (x = temp - 1; x > pathname; x--)
742 return ((*x == '/') ? x + 1 : pathname);
748 /* Compute width of STRING when displayed on screen by print_filename */
754 #if defined (HANDLE_MULTIBYTE)
760 left = strlen (string) + 1;
761 memset (&ps, 0, sizeof (mbstate_t));
767 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
774 #if defined (HANDLE_MULTIBYTE)
775 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
776 if (MB_INVALIDCH (clen))
780 memset (&ps, 0, sizeof (mbstate_t));
782 else if (MB_NULLWCH (clen))
788 width += (w >= 0) ? w : 1;
800 #define ELLIPSIS_LEN 3
803 fnprint (to_print, prefix_bytes, real_pathname)
804 const char *to_print;
806 const char *real_pathname;
810 int common_prefix_len, print_len;
811 #if defined (HANDLE_MULTIBYTE)
818 print_len = strlen (to_print);
819 end = to_print + print_len + 1;
820 memset (&ps, 0, sizeof (mbstate_t));
823 printed_len = common_prefix_len = 0;
825 /* Don't print only the ellipsis if the common prefix is one of the
826 possible completions. Only cut off prefix_bytes if we're going to be
827 printing the ellipsis, which takes precedence over coloring the
828 completion prefix (see print_filename() below). */
829 if (_rl_completion_prefix_display_length > 0 && prefix_bytes >= print_len)
832 #if defined (COLOR_SUPPORT)
833 if (_rl_colored_stats && (prefix_bytes == 0 || _rl_colored_completion_prefix <= 0))
834 colored_stat_start (real_pathname);
837 if (prefix_bytes && _rl_completion_prefix_display_length > 0)
841 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
842 for (w = 0; w < ELLIPSIS_LEN; w++)
843 putc (ellipsis, rl_outstream);
844 printed_len = ELLIPSIS_LEN;
846 #if defined (COLOR_SUPPORT)
847 else if (prefix_bytes && _rl_colored_completion_prefix > 0)
849 common_prefix_len = prefix_bytes;
851 /* XXX - print color indicator start here */
852 colored_prefix_start ();
856 s = to_print + prefix_bytes;
861 putc ('^', rl_outstream);
862 putc (UNCTRL (*s), rl_outstream);
865 #if defined (HANDLE_MULTIBYTE)
866 memset (&ps, 0, sizeof (mbstate_t));
869 else if (*s == RUBOUT)
871 putc ('^', rl_outstream);
872 putc ('?', rl_outstream);
875 #if defined (HANDLE_MULTIBYTE)
876 memset (&ps, 0, sizeof (mbstate_t));
881 #if defined (HANDLE_MULTIBYTE)
882 tlen = mbrtowc (&wc, s, end - s, &ps);
883 if (MB_INVALIDCH (tlen))
887 memset (&ps, 0, sizeof (mbstate_t));
889 else if (MB_NULLWCH (tlen))
894 width = (w >= 0) ? w : 1;
896 fwrite (s, 1, tlen, rl_outstream);
898 printed_len += width;
900 putc (*s, rl_outstream);
905 if (common_prefix_len > 0 && (s - to_print) >= common_prefix_len)
907 #if defined (COLOR_SUPPORT)
908 /* printed bytes = s - to_print */
909 /* printed bytes should never be > but check for paranoia's sake */
910 colored_prefix_end ();
911 if (_rl_colored_stats)
912 colored_stat_start (real_pathname); /* XXX - experiment */
914 common_prefix_len = 0;
918 #if defined (COLOR_SUPPORT)
919 /* XXX - unconditional for now */
920 if (_rl_colored_stats)
927 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
928 are using it, check for and output a single character for `special'
929 filenames. Return the number of characters we output. */
932 print_filename (to_print, full_pathname, prefix_bytes)
933 char *to_print, *full_pathname;
936 int printed_len, extension_char, slen, tlen;
937 char *s, c, *new_full_pathname, *dn;
940 #if defined (COLOR_SUPPORT)
941 /* Defer printing if we want to prefix with a color indicator */
942 if (_rl_colored_stats == 0 || rl_filename_completion_desired == 0)
944 printed_len = fnprint (to_print, prefix_bytes, to_print);
946 if (rl_filename_completion_desired && (
947 #if defined (VISIBLE_STATS)
950 #if defined (COLOR_SUPPORT)
953 _rl_complete_mark_directories))
955 /* If to_print != full_pathname, to_print is the basename of the
956 path passed. In this case, we try to expand the directory
957 name before checking for the stat character. */
958 if (to_print != full_pathname)
960 /* Terminate the directory name. */
964 /* If setting the last slash in full_pathname to a NUL results in
965 full_pathname being the empty string, we are trying to complete
966 files in the root directory. If we pass a null string to the
967 bash directory completion hook, for example, it will expand it
968 to the current directory. We just want the `/'. */
969 if (full_pathname == 0 || *full_pathname == 0)
971 else if (full_pathname[0] != '/')
973 else if (full_pathname[1] == 0)
974 dn = "//"; /* restore trailing slash to `//' */
975 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
976 dn = "/"; /* don't turn /// into // */
979 s = tilde_expand (dn);
980 if (rl_directory_completion_hook)
981 (*rl_directory_completion_hook) (&s);
984 tlen = strlen (to_print);
985 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
986 strcpy (new_full_pathname, s);
987 if (s[slen - 1] == '/')
990 new_full_pathname[slen] = '/';
991 new_full_pathname[slen] = '/';
992 strcpy (new_full_pathname + slen + 1, to_print);
994 #if defined (VISIBLE_STATS)
995 if (rl_visible_stats)
996 extension_char = stat_char (new_full_pathname);
999 if (_rl_complete_mark_directories)
1002 if (rl_directory_completion_hook == 0 && rl_filename_stat_hook)
1004 dn = savestring (new_full_pathname);
1005 (*rl_filename_stat_hook) (&dn);
1006 xfree (new_full_pathname);
1007 new_full_pathname = dn;
1009 if (path_isdir (new_full_pathname))
1010 extension_char = '/';
1013 /* Move colored-stats code inside fnprint() */
1014 #if defined (COLOR_SUPPORT)
1015 if (_rl_colored_stats)
1016 printed_len = fnprint (to_print, prefix_bytes, new_full_pathname);
1019 xfree (new_full_pathname);
1024 s = tilde_expand (full_pathname);
1025 #if defined (VISIBLE_STATS)
1026 if (rl_visible_stats)
1027 extension_char = stat_char (s);
1030 if (_rl_complete_mark_directories && path_isdir (s))
1031 extension_char = '/';
1033 /* Move colored-stats code inside fnprint() */
1034 #if defined (COLOR_SUPPORT)
1035 if (_rl_colored_stats)
1036 printed_len = fnprint (to_print, prefix_bytes, s);
1043 putc (extension_char, rl_outstream);
1052 rl_quote_filename (s, rtype, qcp)
1059 r = (char *)xmalloc (strlen (s) + 2);
1060 *r = *rl_completer_quote_characters;
1063 *qcp = *rl_completer_quote_characters;
1067 /* Find the bounds of the current word for completion purposes, and leave
1068 rl_point set to the end of the word. This function skips quoted
1069 substrings (characters between matched pairs of characters in
1070 rl_completer_quote_characters). First we try to find an unclosed
1071 quoted substring on which to do matching. If one is not found, we use
1072 the word break characters to find the boundaries of the current word.
1073 We call an application-specific function to decide whether or not a
1074 particular word break character is quoted; if that function returns a
1075 non-zero result, the character does not break a word. This function
1076 returns the opening quote character if we found an unclosed quoted
1077 substring, '\0' otherwise. FP, if non-null, is set to a value saying
1078 which (shell-like) quote characters we found (single quote, double
1079 quote, or backslash) anywhere in the string. DP, if non-null, is set to
1080 the value of the delimiter character that caused a word break. */
1083 _rl_find_completion_word (fp, dp)
1086 int scan, end, found_quote, delimiter, pass_next, isbrk;
1087 char quote_char, *brkchars;
1090 found_quote = delimiter = 0;
1094 if (rl_completion_word_break_hook)
1095 brkchars = (*rl_completion_word_break_hook) ();
1097 brkchars = rl_completer_word_break_characters;
1099 if (rl_completer_quote_characters)
1101 /* We have a list of characters which can be used in pairs to
1102 quote substrings for the completer. Try to find the start
1103 of an unclosed quoted substring. */
1104 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
1105 for (scan = pass_next = 0; scan < end; scan = MB_NEXTCHAR (rl_line_buffer, scan, 1, MB_FIND_ANY))
1113 /* Shell-like semantics for single quotes -- don't allow backslash
1114 to quote anything in single quotes, especially not the closing
1115 quote. If you don't like this, take out the check on the value
1117 if (quote_char != '\'' && rl_line_buffer[scan] == '\\')
1120 found_quote |= RL_QF_BACKSLASH;
1124 if (quote_char != '\0')
1126 /* Ignore everything until the matching close quote char. */
1127 if (rl_line_buffer[scan] == quote_char)
1129 /* Found matching close. Abandon this substring. */
1134 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
1136 /* Found start of a quoted substring. */
1137 quote_char = rl_line_buffer[scan];
1138 rl_point = scan + 1;
1139 /* Shell-like quoting conventions. */
1140 if (quote_char == '\'')
1141 found_quote |= RL_QF_SINGLE_QUOTE;
1142 else if (quote_char == '"')
1143 found_quote |= RL_QF_DOUBLE_QUOTE;
1145 found_quote |= RL_QF_OTHER_QUOTE;
1150 if (rl_point == end && quote_char == '\0')
1152 /* We didn't find an unclosed quoted substring upon which to do
1153 completion, so use the word break characters to find the
1154 substring on which to complete. */
1155 while (rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_ANY))
1157 scan = rl_line_buffer[rl_point];
1159 if (strchr (brkchars, scan) == 0)
1162 /* Call the application-specific function to tell us whether
1163 this word break character is quoted and should be skipped. */
1164 if (rl_char_is_quoted_p && found_quote &&
1165 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
1168 /* Convoluted code, but it avoids an n^2 algorithm with calls
1169 to char_is_quoted. */
1174 /* If we are at an unquoted word break, then advance past it. */
1175 scan = rl_line_buffer[rl_point];
1177 /* If there is an application-specific function to say whether or not
1178 a character is quoted and we found a quote character, let that
1179 function decide whether or not a character is a word break, even
1180 if it is found in rl_completer_word_break_characters. Don't bother
1181 if we're at the end of the line, though. */
1184 if (rl_char_is_quoted_p)
1185 isbrk = (found_quote == 0 ||
1186 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
1187 strchr (brkchars, scan) != 0;
1189 isbrk = strchr (brkchars, scan) != 0;
1193 /* If the character that caused the word break was a quoting
1194 character, then remember it as the delimiter. */
1195 if (rl_basic_quote_characters &&
1196 strchr (rl_basic_quote_characters, scan) &&
1197 (end - rl_point) > 1)
1200 /* If the character isn't needed to determine something special
1201 about what kind of completion to perform, then advance past it. */
1202 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0)
1212 return (quote_char);
1216 gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
1219 rl_compentry_func_t *our_func;
1220 int found_quote, quote_char;
1224 rl_completion_found_quote = found_quote;
1225 rl_completion_quote_character = quote_char;
1227 /* If the user wants to TRY to complete, but then wants to give
1228 up and use the default completion function, they set the
1229 variable rl_attempted_completion_function. */
1230 if (rl_attempted_completion_function)
1232 matches = (*rl_attempted_completion_function) (text, start, end);
1233 if (RL_SIG_RECEIVED())
1235 _rl_free_match_list (matches);
1237 RL_CHECK_SIGNALS ();
1240 if (matches || rl_attempted_completion_over)
1242 rl_attempted_completion_over = 0;
1247 /* XXX -- filename dequoting moved into rl_filename_completion_function */
1249 /* rl_completion_matches will check for signals as well to avoid a long
1250 delay while reading a directory. */
1251 matches = rl_completion_matches (text, our_func);
1252 if (RL_SIG_RECEIVED())
1254 _rl_free_match_list (matches);
1256 RL_CHECK_SIGNALS ();
1261 /* Filter out duplicates in MATCHES. This frees up the strings in
1264 remove_duplicate_matches (matches)
1267 char *lowest_common;
1272 /* Sort the items. */
1273 for (i = 0; matches[i]; i++)
1276 /* Sort the array without matches[0], since we need it to
1277 stay in place no matter what. */
1278 if (i && rl_sort_completion_matches)
1279 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1281 /* Remember the lowest common denominator for it may be unique. */
1282 lowest_common = savestring (matches[0]);
1284 for (i = newlen = 0; matches[i + 1]; i++)
1286 if (strcmp (matches[i], matches[i + 1]) == 0)
1289 matches[i] = (char *)&dead_slot;
1295 /* We have marked all the dead slots with (char *)&dead_slot.
1296 Copy all the non-dead entries into a new array. */
1297 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
1298 for (i = j = 1; matches[i]; i++)
1300 if (matches[i] != (char *)&dead_slot)
1301 temp_array[j++] = matches[i];
1303 temp_array[j] = (char *)NULL;
1305 if (matches[0] != (char *)&dead_slot)
1308 /* Place the lowest common denominator back in [0]. */
1309 temp_array[0] = lowest_common;
1311 /* If there is one string left, and it is identical to the
1312 lowest common denominator, then the LCD is the string to
1314 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
1316 xfree (temp_array[1]);
1317 temp_array[1] = (char *)NULL;
1319 return (temp_array);
1322 /* Find the common prefix of the list of matches, and put it into
1325 compute_lcd_of_matches (match_list, matches, text)
1330 register int i, c1, c2, si;
1331 int low; /* Count of max-matched characters. */
1333 char *dtext; /* dequoted TEXT, if needed */
1334 #if defined (HANDLE_MULTIBYTE)
1341 /* If only one match, just use that. Otherwise, compare each
1342 member of the list with the next, finding out where they
1346 match_list[0] = match_list[1];
1347 match_list[1] = (char *)NULL;
1351 for (i = 1, low = 100000; i < matches; i++)
1353 #if defined (HANDLE_MULTIBYTE)
1354 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1356 memset (&ps1, 0, sizeof (mbstate_t));
1357 memset (&ps2, 0, sizeof (mbstate_t));
1360 if (_rl_completion_case_fold)
1363 (c1 = _rl_to_lower(match_list[i][si])) &&
1364 (c2 = _rl_to_lower(match_list[i + 1][si]));
1366 #if defined (HANDLE_MULTIBYTE)
1367 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1369 v1 = mbrtowc(&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1);
1370 v2 = mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2);
1371 if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
1373 if (c1 != c2) /* do byte comparison */
1377 wc1 = towlower (wc1);
1378 wc2 = towlower (wc2);
1392 (c1 = match_list[i][si]) &&
1393 (c2 = match_list[i + 1][si]);
1395 #if defined (HANDLE_MULTIBYTE)
1396 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1400 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2))
1402 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1)
1415 /* If there were multiple matches, but none matched up to even the
1416 first character, and the user typed something, use that as the
1417 value of matches[0]. */
1418 if (low == 0 && text && *text)
1420 match_list[0] = (char *)xmalloc (strlen (text) + 1);
1421 strcpy (match_list[0], text);
1425 match_list[0] = (char *)xmalloc (low + 1);
1427 /* XXX - this might need changes in the presence of multibyte chars */
1429 /* If we are ignoring case, try to preserve the case of the string
1430 the user typed in the face of multiple matches differing in case. */
1431 if (_rl_completion_case_fold)
1433 /* We're making an assumption here:
1434 IF we're completing filenames AND
1435 the application has defined a filename dequoting function AND
1436 we found a quote character AND
1437 the application has requested filename quoting
1439 we assume that TEXT was dequoted before checking against
1440 the file system and needs to be dequoted here before we
1441 check against the list of matches
1443 dtext = (char *)NULL;
1444 if (rl_filename_completion_desired &&
1445 rl_filename_dequoting_function &&
1446 rl_completion_found_quote &&
1447 rl_filename_quoting_desired)
1449 dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
1453 /* sort the list to get consistent answers. */
1454 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1457 lx = (si <= low) ? si : low; /* check shorter of text and matches */
1458 /* Try to preserve the case of what the user typed in the presence of
1459 multiple matches: check each match for something that matches
1460 what the user typed taking case into account; use it up to common
1461 length of matches if one is found. If not, just use first match. */
1462 for (i = 1; i <= matches; i++)
1463 if (strncmp (match_list[i], text, lx) == 0)
1465 strncpy (match_list[0], match_list[i], low);
1468 /* no casematch, use first entry */
1470 strncpy (match_list[0], match_list[1], low);
1475 strncpy (match_list[0], match_list[1], low);
1477 match_list[0][low] = '\0';
1484 postprocess_matches (matchesp, matching_filenames)
1486 int matching_filenames;
1488 char *t, **matches, **temp_matches;
1491 matches = *matchesp;
1496 /* It seems to me that in all the cases we handle we would like
1497 to ignore duplicate possibilities. Scan for the text to
1498 insert being identical to the other completions. */
1499 if (rl_ignore_completion_duplicates)
1501 temp_matches = remove_duplicate_matches (matches);
1503 matches = temp_matches;
1506 /* If we are matching filenames, then here is our chance to
1507 do clever processing by re-examining the list. Call the
1508 ignore function with the array as a parameter. It can
1509 munge the array, deleting matches as it desires. */
1510 if (rl_ignore_some_completions_function && matching_filenames)
1512 for (nmatch = 1; matches[nmatch]; nmatch++)
1514 (void)(*rl_ignore_some_completions_function) (matches);
1515 if (matches == 0 || matches[0] == 0)
1518 *matchesp = (char **)0;
1523 /* If we removed some matches, recompute the common prefix. */
1524 for (i = 1; matches[i]; i++)
1526 if (i > 1 && i < nmatch)
1529 compute_lcd_of_matches (matches, i - 1, t);
1535 *matchesp = matches;
1540 complete_get_screenwidth ()
1545 cols = _rl_completion_columns;
1546 if (cols >= 0 && cols <= _rl_screenwidth)
1548 envcols = getenv ("COLUMNS");
1549 if (envcols && *envcols)
1550 cols = atoi (envcols);
1551 if (cols >= 0 && cols <= _rl_screenwidth)
1553 return _rl_screenwidth;
1556 /* A convenience function for displaying a list of strings in
1557 columnar format on readline's output stream. MATCHES is the list
1558 of strings, in argv format, LEN is the number of strings in MATCHES,
1559 and MAX is the length of the longest string in MATCHES. */
1561 rl_display_match_list (matches, len, max)
1565 int count, limit, printed_len, lines, cols;
1566 int i, j, k, l, common_length, sind;
1569 /* Find the length of the prefix common to all items: length as displayed
1570 characters (common_length) and as a byte index into the matches (sind) */
1571 common_length = sind = 0;
1572 if (_rl_completion_prefix_display_length > 0)
1574 t = printable_part (matches[0]);
1575 /* check again in case of /usr/src/ */
1576 temp = rl_filename_completion_desired ? strrchr (t, '/') : 0;
1577 common_length = temp ? fnwidth (temp) : fnwidth (t);
1578 sind = temp ? strlen (temp) : strlen (t);
1579 if (common_length > max || sind > max)
1580 common_length = sind = 0;
1582 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
1583 max -= common_length - ELLIPSIS_LEN;
1585 common_length = sind = 0;
1587 #if defined (COLOR_SUPPORT)
1588 else if (_rl_colored_completion_prefix > 0)
1590 t = printable_part (matches[0]);
1591 temp = rl_filename_completion_desired ? strrchr (t, '/') : 0;
1592 common_length = temp ? fnwidth (temp) : fnwidth (t);
1593 sind = temp ? RL_STRLEN (temp+1) : RL_STRLEN (t); /* want portion after final slash */
1594 if (common_length > max || sind > max)
1595 common_length = sind = 0;
1599 /* How many items of MAX length can we fit in the screen window? */
1600 cols = complete_get_screenwidth ();
1603 if (limit != 1 && (limit * max == cols))
1606 /* If cols == 0, limit will end up -1 */
1607 if (cols < _rl_screenwidth && limit < 0)
1610 /* Avoid a possible floating exception. If max > cols,
1611 limit will be 0 and a divide-by-zero fault will result. */
1615 /* How many iterations of the printing loop? */
1616 count = (len + (limit - 1)) / limit;
1618 /* Watch out for special case. If LEN is less than LIMIT, then
1619 just do the inner printing loop.
1620 0 < len <= limit implies count = 1. */
1622 /* Sort the items if they are not already sorted. */
1623 if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
1624 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1629 if (_rl_print_completions_horizontally == 0)
1631 /* Print the sorted items, up-and-down alphabetically, like ls. */
1632 for (i = 1; i <= count; i++)
1634 for (j = 0, l = i; j < limit; j++)
1636 if (l > len || matches[l] == 0)
1640 temp = printable_part (matches[l]);
1641 printed_len = print_filename (temp, matches[l], sind);
1645 if (max <= printed_len)
1646 putc (' ', rl_outstream);
1648 for (k = 0; k < max - printed_len; k++)
1649 putc (' ', rl_outstream);
1655 #if defined (SIGWINCH)
1656 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1658 if (RL_SIG_RECEIVED ())
1662 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count)
1664 lines = _rl_internal_pager (lines);
1672 /* Print the sorted items, across alphabetically, like ls -x. */
1673 for (i = 1; matches[i]; i++)
1675 temp = printable_part (matches[i]);
1676 printed_len = print_filename (temp, matches[i], sind);
1677 /* Have we reached the end of this line? */
1678 #if defined (SIGWINCH)
1679 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1681 if (RL_SIG_RECEIVED ())
1686 if (limit == 1 || (i && (limit > 1) && (i % limit) == 0))
1690 if (_rl_page_completions && lines >= _rl_screenheight - 1)
1692 lines = _rl_internal_pager (lines);
1697 else if (max <= printed_len)
1698 putc (' ', rl_outstream);
1700 for (k = 0; k < max - printed_len; k++)
1701 putc (' ', rl_outstream);
1708 /* Display MATCHES, a list of matching filenames in argv format. This
1709 handles the simple case -- a single match -- first. If there is more
1710 than one match, we compute the number of strings in the list and the
1711 length of the longest string, which will be needed by the display
1712 function. If the application wants to handle displaying the list of
1713 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1714 address of a function, and we just call it. If we're handling the
1715 display ourselves, we just call rl_display_match_list. We also check
1716 that the list of matches doesn't exceed the user-settable threshold,
1717 and ask the user if he wants to see the list if there are more matches
1718 than RL_COMPLETION_QUERY_ITEMS. */
1720 display_matches (matches)
1726 /* Move to the last visible line of a possibly-multiple-line command. */
1727 _rl_move_vert (_rl_vis_botlin);
1729 /* Handle simple case first. What if there is only one answer? */
1730 if (matches[1] == 0)
1732 temp = printable_part (matches[0]);
1734 print_filename (temp, matches[0], 0);
1737 rl_forced_update_display ();
1738 rl_display_fixed = 1;
1743 /* There is more than one answer. Find out how many there are,
1744 and find the maximum printed length of a single entry. */
1745 for (max = 0, i = 1; matches[i]; i++)
1747 temp = printable_part (matches[i]);
1748 len = fnwidth (temp);
1756 /* If the caller has defined a display hook, then call that now. */
1757 if (rl_completion_display_matches_hook)
1759 (*rl_completion_display_matches_hook) (matches, len, max);
1763 /* If there are many items, then ask the user if she really wants to
1765 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
1768 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1769 fflush (rl_outstream);
1770 if ((completion_y_or_n = get_y_or_n (0)) == 0)
1774 rl_forced_update_display ();
1775 rl_display_fixed = 1;
1781 rl_display_match_list (matches, len, max);
1783 rl_forced_update_display ();
1784 rl_display_fixed = 1;
1788 make_quoted_replacement (match, mtype, qc)
1791 char *qc; /* Pointer to quoting character, if any */
1793 int should_quote, do_replace;
1796 /* If we are doing completion on quoted substrings, and any matches
1797 contain any of the completer_word_break_characters, then auto-
1798 matically prepend the substring with a quote character (just pick
1799 the first one from the list of such) if it does not already begin
1800 with a quote string. FIXME: Need to remove any such automatically
1801 inserted quote character when it no longer is necessary, such as
1802 if we change the string we are completing on and the new set of
1803 matches don't require a quoted substring. */
1804 replacement = match;
1806 should_quote = match && rl_completer_quote_characters &&
1807 rl_filename_completion_desired &&
1808 rl_filename_quoting_desired;
1811 should_quote = should_quote && (!qc || !*qc ||
1812 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
1816 /* If there is a single match, see if we need to quote it.
1817 This also checks whether the common prefix of several
1818 matches needs to be quoted. */
1819 should_quote = rl_filename_quote_characters
1820 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0)
1823 do_replace = should_quote ? mtype : NO_MATCH;
1824 /* Quote the replacement, since we found an embedded
1825 word break character in a potential match. */
1826 if (do_replace != NO_MATCH && rl_filename_quoting_function)
1827 replacement = (*rl_filename_quoting_function) (match, do_replace, qc);
1829 return (replacement);
1833 insert_match (match, start, mtype, qc)
1838 char *replacement, *r;
1842 oqc = qc ? *qc : '\0';
1843 replacement = make_quoted_replacement (match, mtype, qc);
1845 /* Now insert the match. */
1848 rlen = strlen (replacement);
1849 /* Don't double an opening quote character. */
1850 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc &&
1851 replacement[0] == *qc)
1853 /* If make_quoted_replacement changed the quoting character, remove
1854 the opening quote and insert the (fully-quoted) replacement. */
1855 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
1856 replacement[0] != oqc)
1859 /* Don't double a closing quote character */
1860 if (qc && *qc && end && rl_line_buffer[rl_point] == *qc && replacement[rlen - 1] == *qc)
1862 if (_rl_skip_completed_text)
1865 while (start < rl_end && *r && rl_line_buffer[start] == *r)
1870 if (start <= end || *r)
1871 _rl_replace_text (r, start, end);
1872 rl_point = start + strlen (r);
1875 _rl_replace_text (replacement, start, end);
1876 if (replacement != match)
1877 xfree (replacement);
1881 /* Append any necessary closing quote and a separator character to the
1882 just-inserted match. If the user has specified that directories
1883 should be marked by a trailing `/', append one of those instead. The
1884 default trailing character is a space. Returns the number of characters
1885 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1886 has them) and don't add a suffix for a symlink to a directory. A
1887 nontrivial match is one that actually adds to the word being completed.
1888 The variable rl_completion_mark_symlink_dirs controls this behavior
1889 (it's initially set to the what the user has chosen, indicated by the
1890 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1891 application's completion function). */
1893 append_to_match (text, delimiter, quote_char, nontrivial_match)
1895 int delimiter, quote_char, nontrivial_match;
1897 char temp_string[4], *filename, *fn;
1898 int temp_string_index, s;
1901 temp_string_index = 0;
1902 if (quote_char && rl_point && rl_completion_suppress_quote == 0 &&
1903 rl_line_buffer[rl_point - 1] != quote_char)
1904 temp_string[temp_string_index++] = quote_char;
1907 temp_string[temp_string_index++] = delimiter;
1908 else if (rl_completion_suppress_append == 0 && rl_completion_append_character)
1909 temp_string[temp_string_index++] = rl_completion_append_character;
1911 temp_string[temp_string_index++] = '\0';
1913 if (rl_filename_completion_desired)
1915 filename = tilde_expand (text);
1916 if (rl_filename_stat_hook)
1918 fn = savestring (filename);
1919 (*rl_filename_stat_hook) (&fn);
1923 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1924 ? LSTAT (filename, &finfo)
1925 : stat (filename, &finfo);
1926 if (s == 0 && S_ISDIR (finfo.st_mode))
1928 if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */)
1930 /* This is clumsy. Avoid putting in a double slash if point
1931 is at the end of the line and the previous character is a
1933 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1935 else if (rl_line_buffer[rl_point] != '/')
1936 rl_insert_text ("/");
1940 /* Don't add anything if the filename is a symlink and resolves to a
1942 else if (s == 0 && S_ISLNK (finfo.st_mode) && path_isdir (filename))
1947 if (rl_point == rl_end && temp_string_index)
1948 rl_insert_text (temp_string);
1954 if (rl_point == rl_end && temp_string_index)
1955 rl_insert_text (temp_string);
1958 return (temp_string_index);
1962 insert_all_matches (matches, point, qc)
1970 rl_begin_undo_group ();
1971 /* remove any opening quote character; make_quoted_replacement will add
1973 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
1975 rl_delete_text (point, rl_point);
1980 for (i = 1; matches[i]; i++)
1982 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
1983 rl_insert_text (rp);
1984 rl_insert_text (" ");
1985 if (rp != matches[i])
1991 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
1992 rl_insert_text (rp);
1993 rl_insert_text (" ");
1994 if (rp != matches[0])
1997 rl_end_undo_group ();
2001 _rl_free_match_list (matches)
2009 for (i = 0; matches[i]; i++)
2014 /* Complete the word at or before point.
2015 WHAT_TO_DO says what to do with the completion.
2016 `?' means list the possible completions.
2017 TAB means do standard completion.
2018 `*' means insert all of the possible completions.
2019 `!' means to do standard completion, and list all possible completions if
2020 there is more than one.
2021 `@' means to do standard completion, and list all possible completions if
2022 there is more than one and partial completion is not possible. */
2024 rl_complete_internal (what_to_do)
2028 rl_compentry_func_t *our_func;
2029 int start, end, delimiter, found_quote, i, nontrivial_lcd;
2030 char *text, *saved_line_buffer;
2036 RL_SETSTATE(RL_STATE_COMPLETING);
2038 set_completion_defaults (what_to_do);
2040 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
2041 our_func = rl_completion_entry_function
2042 ? rl_completion_entry_function
2043 : rl_filename_completion_function;
2044 /* We now look backwards for the start of a filename/variable word. */
2046 found_quote = delimiter = 0;
2050 /* This (possibly) changes rl_point. If it returns a non-zero char,
2051 we know we have an open quote. */
2052 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2057 text = rl_copy_text (start, end);
2058 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
2059 /* nontrivial_lcd is set if the common prefix adds something to the word
2061 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
2062 if (what_to_do == '!' || what_to_do == '@')
2063 tlen = strlen (text);
2069 FREE (saved_line_buffer);
2070 completion_changed_buffer = 0;
2071 RL_UNSETSTATE(RL_STATE_COMPLETING);
2072 _rl_reset_completion_state ();
2076 /* If we are matching filenames, the attempted completion function will
2077 have set rl_filename_completion_desired to a non-zero value. The basic
2078 rl_filename_completion_function does this. */
2079 i = rl_filename_completion_desired;
2081 if (postprocess_matches (&matches, i) == 0)
2084 FREE (saved_line_buffer);
2085 completion_changed_buffer = 0;
2086 RL_UNSETSTATE(RL_STATE_COMPLETING);
2087 _rl_reset_completion_state ();
2096 /* Insert the first match with proper quoting. */
2097 if (what_to_do == TAB)
2100 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
2102 else if (*matches[0] && matches[1] == 0)
2103 /* should we perform the check only if there are multiple matches? */
2104 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
2105 else if (*matches[0]) /* what_to_do != TAB && multiple matches */
2107 mlen = *matches[0] ? strlen (matches[0]) : 0;
2109 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
2112 /* If there are more matches, ring the bell to indicate.
2113 If we are in vi mode, Posix.2 says to not ring the bell.
2114 If the `show-all-if-ambiguous' variable is set, display
2115 all the matches immediately. Otherwise, if this was the
2116 only match, and we are hacking files, check the file to
2117 see if it was a directory. If so, and the `mark-directories'
2118 variable is set, add a '/' to the name. If not, and we
2119 are at the end of the line, then add a space. */
2122 if (what_to_do == '!')
2124 display_matches (matches);
2127 else if (what_to_do == '@')
2129 if (nontrivial_lcd == 0)
2130 display_matches (matches);
2133 else if (rl_editing_mode != vi_mode)
2134 rl_ding (); /* There are other matches remaining. */
2137 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2142 insert_all_matches (matches, start, "e_char);
2146 if (rl_completion_display_matches_hook == 0)
2148 _rl_sigcleanup = _rl_complete_sigcleanup;
2149 _rl_sigcleanarg = matches;
2150 _rl_complete_display_matches_interrupt = 0;
2152 display_matches (matches);
2153 if (_rl_complete_display_matches_interrupt)
2155 matches = 0; /* already freed by rl_complete_sigcleanup */
2156 _rl_complete_display_matches_interrupt = 0;
2157 if (rl_signal_event_hook)
2158 (*rl_signal_event_hook) (); /* XXX */
2161 _rl_sigcleanarg = 0;
2165 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do);
2167 FREE (saved_line_buffer);
2168 RL_UNSETSTATE(RL_STATE_COMPLETING);
2169 _rl_free_match_list (matches);
2170 _rl_reset_completion_state ();
2174 _rl_free_match_list (matches);
2176 /* Check to see if the line has changed through all of this manipulation. */
2177 if (saved_line_buffer)
2179 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
2180 xfree (saved_line_buffer);
2183 RL_UNSETSTATE(RL_STATE_COMPLETING);
2184 _rl_reset_completion_state ();
2186 RL_CHECK_SIGNALS ();
2190 /***************************************************************/
2192 /* Application-callable completion match generator functions */
2194 /***************************************************************/
2196 /* Return an array of (char *) which is a list of completions for TEXT.
2197 If there are no completions, return a NULL pointer.
2198 The first entry in the returned array is the substitution for TEXT.
2199 The remaining entries are the possible completions.
2200 The array is terminated with a NULL pointer.
2202 ENTRY_FUNCTION is a function of two args, and returns a (char *).
2203 The first argument is TEXT.
2204 The second is a state argument; it should be zero on the first call, and
2205 non-zero on subsequent calls. It returns a NULL pointer to the caller
2206 when there are no more matches.
2209 rl_completion_matches (text, entry_function)
2211 rl_compentry_func_t *entry_function;
2215 /* Number of slots in match_list. */
2216 int match_list_size;
2218 /* The list of matches. */
2221 /* Number of matches actually found. */
2224 /* Temporary string binder. */
2228 match_list_size = 10;
2229 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
2230 match_list[1] = (char *)NULL;
2232 while (string = (*entry_function) (text, matches))
2234 if (RL_SIG_RECEIVED ())
2236 /* Start at 1 because we don't set matches[0] in this function.
2237 Only free the list members if we're building match list from
2238 rl_filename_completion_function, since we know that doesn't
2239 free the strings it returns. */
2240 if (entry_function == rl_filename_completion_function)
2242 for (i = 1; match_list[i]; i++)
2243 xfree (match_list[i]);
2247 match_list_size = 0;
2249 RL_CHECK_SIGNALS ();
2252 if (matches + 1 >= match_list_size)
2253 match_list = (char **)xrealloc
2254 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
2256 if (match_list == 0)
2257 return (match_list);
2259 match_list[++matches] = string;
2260 match_list[matches + 1] = (char *)NULL;
2263 /* If there were any matches, then look through them finding out the
2264 lowest common denominator. That then becomes match_list[0]. */
2266 compute_lcd_of_matches (match_list, matches, text);
2267 else /* There were no matches. */
2270 match_list = (char **)NULL;
2272 return (match_list);
2275 /* A completion function for usernames.
2276 TEXT contains a partial username preceded by a random
2277 character (usually `~'). */
2279 rl_username_completion_function (text, state)
2283 #if defined (__WIN32__) || defined (__OPENNT)
2284 return (char *)NULL;
2285 #else /* !__WIN32__ && !__OPENNT) */
2286 static char *username = (char *)NULL;
2287 static struct passwd *entry;
2288 static int namelen, first_char, first_char_loc;
2296 first_char_loc = first_char == '~';
2298 username = savestring (&text[first_char_loc]);
2299 namelen = strlen (username);
2300 #if defined (HAVE_GETPWENT)
2305 #if defined (HAVE_GETPWENT)
2306 while (entry = getpwent ())
2308 /* Null usernames should result in all users as possible completions. */
2309 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
2316 #if defined (HAVE_GETPWENT)
2319 return ((char *)NULL);
2323 value = (char *)xmalloc (2 + strlen (entry->pw_name));
2327 strcpy (value + first_char_loc, entry->pw_name);
2329 if (first_char == '~')
2330 rl_filename_completion_desired = 1;
2334 #endif /* !__WIN32__ && !__OPENNT */
2337 /* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME
2338 (FILENAME_LEN). If _rl_completion_case_fold is set, compare without
2339 regard to the alphabetic case of characters. If
2340 _rl_completion_case_map is set, make `-' and `_' equivalent. CONVFN is
2341 the possibly-converted directory entry; FILENAME is what the user typed. */
2343 complete_fncmp (convfn, convlen, filename, filename_len)
2346 const char *filename;
2349 register char *s1, *s2;
2351 #if defined (HANDLE_MULTIBYTE)
2357 #if defined (HANDLE_MULTIBYTE)
2358 memset (&ps1, 0, sizeof (mbstate_t));
2359 memset (&ps2, 0, sizeof (mbstate_t));
2362 if (filename_len == 0)
2364 if (convlen < filename_len)
2368 s1 = (char *)convfn;
2369 s2 = (char *)filename;
2371 /* Otherwise, if these match up to the length of filename, then
2373 if (_rl_completion_case_fold && _rl_completion_case_map)
2375 /* Case-insensitive comparison treating _ and - as equivalent */
2376 #if defined (HANDLE_MULTIBYTE)
2377 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2381 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2382 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2383 if (v1 == 0 && v2 == 0)
2385 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2387 if (*s1 != *s2) /* do byte comparison */
2389 else if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2394 wc1 = towlower (wc1);
2395 wc2 = towlower (wc2);
2399 if ((wc1 == L'-' || wc1 == L'_') && (wc2 == L'-' || wc2 == L'_'))
2411 d = _rl_to_lower (*s1) - _rl_to_lower (*s2);
2412 /* *s1 == [-_] && *s2 == [-_] */
2413 if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2417 s1++; s2++; /* already checked convlen >= filename_len */
2424 else if (_rl_completion_case_fold)
2426 #if defined (HANDLE_MULTIBYTE)
2427 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2431 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2432 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2433 if (v1 == 0 && v2 == 0)
2435 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2437 if (*s1 != *s2) /* do byte comparison */
2442 wc1 = towlower (wc1);
2443 wc2 = towlower (wc2);
2455 if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) &&
2456 (convlen >= filename_len) &&
2457 (_rl_strnicmp (filename, convfn, filename_len) == 0))
2462 if ((convfn[0] == filename[0]) &&
2463 (convlen >= filename_len) &&
2464 (strncmp (filename, convfn, filename_len) == 0))
2470 /* Okay, now we write the entry_function for filename completion. In the
2471 general case. Note that completion in the shell is a little different
2472 because of all the pathnames that must be followed when looking up the
2473 completion for a command. */
2475 rl_filename_completion_function (text, state)
2479 static DIR *directory = (DIR *)NULL;
2480 static char *filename = (char *)NULL;
2481 static char *dirname = (char *)NULL;
2482 static char *users_dirname = (char *)NULL;
2483 static int filename_len;
2484 char *temp, *dentry, *convfn;
2485 int dirlen, dentlen, convlen;
2487 struct dirent *entry;
2489 /* If we don't have any state, then do some initialization. */
2492 /* If we were interrupted before closing the directory or reading
2493 all of its contents, close it. */
2496 closedir (directory);
2497 directory = (DIR *)NULL;
2501 FREE (users_dirname);
2503 filename = savestring (text);
2506 dirname = savestring (text);
2508 temp = strrchr (dirname, '/');
2510 #if defined (__MSDOS__) || defined (_WIN32)
2511 /* special hack for //X/... */
2512 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/')
2513 temp = strrchr (dirname + 3, '/');
2518 strcpy (filename, ++temp);
2521 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN__))
2522 /* searches from current directory on the drive */
2523 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':')
2525 strcpy (filename, dirname + 2);
2535 /* We aren't done yet. We also support the "~user" syntax. */
2537 /* Save the version of the directory that the user typed, dequoting
2539 if (rl_completion_found_quote && rl_filename_dequoting_function)
2540 users_dirname = (*rl_filename_dequoting_function) (dirname, rl_completion_quote_character);
2542 users_dirname = savestring (dirname);
2545 if (*dirname == '~')
2547 temp = tilde_expand (dirname);
2553 /* We have saved the possibly-dequoted version of the directory name
2554 the user typed. Now transform the directory name we're going to
2555 pass to opendir(2). The directory rewrite hook modifies only the
2556 directory name; the directory completion hook modifies both the
2557 directory name passed to opendir(2) and the version the user
2558 typed. Both the directory completion and rewrite hooks should perform
2559 any necessary dequoting. The hook functions return 1 if they modify
2560 the directory name argument. If either hook returns 0, it should
2561 not modify the directory name pointer passed as an argument. */
2562 if (rl_directory_rewrite_hook)
2563 (*rl_directory_rewrite_hook) (&dirname);
2564 else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname))
2566 xfree (users_dirname);
2567 users_dirname = savestring (dirname);
2569 else if (tilde_dirname == 0 && rl_completion_found_quote && rl_filename_dequoting_function)
2571 /* delete single and double quotes */
2573 dirname = savestring (users_dirname);
2575 directory = opendir (dirname);
2577 /* Now dequote a non-null filename. FILENAME will not be NULL, but may
2579 if (*filename && rl_completion_found_quote && rl_filename_dequoting_function)
2581 /* delete single and double quotes */
2582 temp = (*rl_filename_dequoting_function) (filename, rl_completion_quote_character);
2586 filename_len = strlen (filename);
2588 rl_filename_completion_desired = 1;
2591 /* At this point we should entertain the possibility of hacking wildcarded
2592 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2593 contains globbing characters, then build an array of directories, and
2594 then map over that list while completing. */
2595 /* *** UNIMPLEMENTED *** */
2597 /* Now that we have some state, we can read the directory. */
2599 entry = (struct dirent *)NULL;
2600 while (directory && (entry = readdir (directory)))
2602 convfn = dentry = entry->d_name;
2603 convlen = dentlen = D_NAMLEN (entry);
2605 if (rl_filename_rewrite_hook)
2607 convfn = (*rl_filename_rewrite_hook) (dentry, dentlen);
2608 convlen = (convfn == dentry) ? dentlen : strlen (convfn);
2611 /* Special case for no filename. If the user has disabled the
2612 `match-hidden-files' variable, skip filenames beginning with `.'.
2613 All other entries except "." and ".." match. */
2614 if (filename_len == 0)
2616 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (convfn))
2619 if (convfn[0] != '.' ||
2620 (convfn[1] && (convfn[1] != '.' || convfn[2])))
2625 if (complete_fncmp (convfn, convlen, filename, filename_len))
2634 closedir (directory);
2635 directory = (DIR *)NULL;
2640 dirname = (char *)NULL;
2645 filename = (char *)NULL;
2649 xfree (users_dirname);
2650 users_dirname = (char *)NULL;
2653 return (char *)NULL;
2657 /* dirname && (strcmp (dirname, ".") != 0) */
2658 if (dirname && (dirname[0] != '.' || dirname[1]))
2660 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
2662 dirlen = strlen (dirname);
2663 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
2664 strcpy (temp, dirname);
2665 /* Canonicalization cuts off any final slash present. We
2666 may need to add it back. */
2667 if (dirname[dirlen - 1] != '/')
2669 temp[dirlen++] = '/';
2670 temp[dirlen] = '\0';
2675 dirlen = strlen (users_dirname);
2676 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
2677 strcpy (temp, users_dirname);
2678 /* Make sure that temp has a trailing slash here. */
2679 if (users_dirname[dirlen - 1] != '/')
2680 temp[dirlen++] = '/';
2683 strcpy (temp + dirlen, convfn);
2686 temp = savestring (convfn);
2688 if (convfn != dentry)
2695 /* An initial implementation of a menu completion function a la tcsh. The
2696 first time (if the last readline command was not rl_old_menu_complete), we
2697 generate the list of matches. This code is very similar to the code in
2698 rl_complete_internal -- there should be a way to combine the two. Then,
2699 for each item in the list of matches, we insert the match in an undoable
2700 fashion, with the appropriate character appended (this happens on the
2701 second and subsequent consecutive calls to rl_old_menu_complete). When we
2702 hit the end of the match list, we restore the original unmatched text,
2703 ring the bell, and reset the counter to zero. */
2705 rl_old_menu_complete (count, invoking_key)
2706 int count, invoking_key;
2708 rl_compentry_func_t *our_func;
2709 int matching_filenames, found_quote;
2711 static char *orig_text;
2712 static char **matches = (char **)0;
2713 static int match_list_index = 0;
2714 static int match_list_size = 0;
2715 static int orig_start, orig_end;
2716 static char quote_char;
2717 static int delimiter;
2719 /* The first time through, we generate the list of matches and set things
2720 up to insert them. */
2721 if (rl_last_func != rl_old_menu_complete)
2723 /* Clean up from previous call, if any. */
2726 _rl_free_match_list (matches);
2728 match_list_index = match_list_size = 0;
2729 matches = (char **)NULL;
2731 rl_completion_invoking_key = invoking_key;
2733 RL_SETSTATE(RL_STATE_COMPLETING);
2735 /* Only the completion entry function can change these. */
2736 set_completion_defaults ('%');
2738 our_func = rl_menu_completion_entry_function;
2740 our_func = rl_completion_entry_function
2741 ? rl_completion_entry_function
2742 : rl_filename_completion_function;
2744 /* We now look backwards for the start of a filename/variable word. */
2745 orig_end = rl_point;
2746 found_quote = delimiter = 0;
2750 /* This (possibly) changes rl_point. If it returns a non-zero char,
2751 we know we have an open quote. */
2752 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2754 orig_start = rl_point;
2755 rl_point = orig_end;
2757 orig_text = rl_copy_text (orig_start, orig_end);
2758 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2759 our_func, found_quote, quote_char);
2761 /* If we are matching filenames, the attempted completion function will
2762 have set rl_filename_completion_desired to a non-zero value. The basic
2763 rl_filename_completion_function does this. */
2764 matching_filenames = rl_filename_completion_desired;
2766 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2770 matches = (char **)0;
2772 orig_text = (char *)0;
2773 completion_changed_buffer = 0;
2774 RL_UNSETSTATE(RL_STATE_COMPLETING);
2778 RL_UNSETSTATE(RL_STATE_COMPLETING);
2780 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2782 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2783 code below should take care of it. */
2785 if (match_list_size > 1 && _rl_complete_show_all)
2786 display_matches (matches);
2789 /* Now we have the list of matches. Replace the text between
2790 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2791 matches[match_list_index], and add any necessary closing char. */
2793 if (matches == 0 || match_list_size == 0)
2797 matches = (char **)0;
2798 completion_changed_buffer = 0;
2802 match_list_index += count;
2803 if (match_list_index < 0)
2805 while (match_list_index < 0)
2806 match_list_index += match_list_size;
2809 match_list_index %= match_list_size;
2811 if (match_list_index == 0 && match_list_size > 1)
2814 insert_match (orig_text, orig_start, MULT_MATCH, "e_char);
2818 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
2819 append_to_match (matches[match_list_index], delimiter, quote_char,
2820 strcmp (orig_text, matches[match_list_index]));
2823 completion_changed_buffer = 1;
2828 rl_menu_complete (count, ignore)
2831 rl_compentry_func_t *our_func;
2832 int matching_filenames, found_quote;
2834 static char *orig_text;
2835 static char **matches = (char **)0;
2836 static int match_list_index = 0;
2837 static int match_list_size = 0;
2838 static int nontrivial_lcd = 0;
2839 static int full_completion = 0; /* set to 1 if menu completion should reinitialize on next call */
2840 static int orig_start, orig_end;
2841 static char quote_char;
2842 static int delimiter, cstate;
2844 /* The first time through, we generate the list of matches and set things
2845 up to insert them. */
2846 if ((rl_last_func != rl_menu_complete && rl_last_func != rl_backward_menu_complete) || full_completion)
2848 /* Clean up from previous call, if any. */
2851 _rl_free_match_list (matches);
2853 match_list_index = match_list_size = 0;
2854 matches = (char **)NULL;
2856 full_completion = 0;
2858 RL_SETSTATE(RL_STATE_COMPLETING);
2860 /* Only the completion entry function can change these. */
2861 set_completion_defaults ('%');
2863 our_func = rl_menu_completion_entry_function;
2865 our_func = rl_completion_entry_function
2866 ? rl_completion_entry_function
2867 : rl_filename_completion_function;
2869 /* We now look backwards for the start of a filename/variable word. */
2870 orig_end = rl_point;
2871 found_quote = delimiter = 0;
2875 /* This (possibly) changes rl_point. If it returns a non-zero char,
2876 we know we have an open quote. */
2877 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2879 orig_start = rl_point;
2880 rl_point = orig_end;
2882 orig_text = rl_copy_text (orig_start, orig_end);
2883 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2884 our_func, found_quote, quote_char);
2886 nontrivial_lcd = matches && strcmp (orig_text, matches[0]) != 0;
2888 /* If we are matching filenames, the attempted completion function will
2889 have set rl_filename_completion_desired to a non-zero value. The basic
2890 rl_filename_completion_function does this. */
2891 matching_filenames = rl_filename_completion_desired;
2893 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2897 matches = (char **)0;
2899 orig_text = (char *)0;
2900 completion_changed_buffer = 0;
2901 RL_UNSETSTATE(RL_STATE_COMPLETING);
2905 RL_UNSETSTATE(RL_STATE_COMPLETING);
2907 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2910 if (match_list_size == 0)
2914 matches = (char **)0;
2915 match_list_index = 0;
2916 completion_changed_buffer = 0;
2920 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2921 code below should take care of it. */
2924 insert_match (matches[0], orig_start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
2925 orig_end = orig_start + strlen (matches[0]);
2926 completion_changed_buffer = STREQ (orig_text, matches[0]) == 0;
2929 if (match_list_size > 1 && _rl_complete_show_all)
2931 display_matches (matches);
2932 /* If there are so many matches that the user has to be asked
2933 whether or not he wants to see the matches, menu completion
2935 if (rl_completion_query_items > 0 && match_list_size >= rl_completion_query_items)
2939 matches = (char **)0;
2940 full_completion = 1;
2943 else if (_rl_menu_complete_prefix_first)
2949 else if (match_list_size <= 1)
2951 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2952 full_completion = 1;
2955 else if (_rl_menu_complete_prefix_first && match_list_size > 1)
2962 /* Now we have the list of matches. Replace the text between
2963 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2964 matches[match_list_index], and add any necessary closing char. */
2966 if (matches == 0 || match_list_size == 0)
2970 matches = (char **)0;
2971 completion_changed_buffer = 0;
2975 match_list_index += count;
2976 if (match_list_index < 0)
2978 while (match_list_index < 0)
2979 match_list_index += match_list_size;
2982 match_list_index %= match_list_size;
2984 if (match_list_index == 0 && match_list_size > 1)
2987 insert_match (matches[0], orig_start, MULT_MATCH, "e_char);
2991 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
2992 append_to_match (matches[match_list_index], delimiter, quote_char,
2993 strcmp (orig_text, matches[match_list_index]));
2996 completion_changed_buffer = 1;
3001 rl_backward_menu_complete (count, key)
3004 /* Positive arguments to backward-menu-complete translate into negative
3005 arguments for menu-complete, and vice versa. */
3006 return (rl_menu_complete (-count, key));