1 /* complete.c -- filename completion for readline. */
3 /* Copyright (C) 1987-2012 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((char *));
115 static void colored_stat_end PARAMS((void));
118 static int path_isdir PARAMS((const char *));
120 static char *rl_quote_filename PARAMS((char *, int, char *));
122 static void _rl_complete_sigcleanup PARAMS((int, void *));
124 static void set_completion_defaults PARAMS((int));
125 static int get_y_or_n PARAMS((int));
126 static int _rl_internal_pager PARAMS((int));
127 static char *printable_part PARAMS((char *));
128 static int fnwidth PARAMS((const char *));
129 static int fnprint PARAMS((const char *, int));
130 static int print_filename PARAMS((char *, char *, int));
132 static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int));
134 static char **remove_duplicate_matches PARAMS((char **));
135 static void insert_match PARAMS((char *, int, int, char *));
136 static int append_to_match PARAMS((char *, int, int, int));
137 static void insert_all_matches PARAMS((char **, int, char *));
138 static int complete_fncmp PARAMS((const char *, int, const char *, int));
139 static void display_matches PARAMS((char **));
140 static int compute_lcd_of_matches PARAMS((char **, int, const char *));
141 static int postprocess_matches PARAMS((char ***, int));
142 static int complete_get_screenwidth PARAMS((void));
144 static char *make_quoted_replacement PARAMS((char *, int, char *));
146 /* **************************************************************** */
148 /* Completion matching, from readline's point of view. */
150 /* **************************************************************** */
152 /* Variables known only to the readline library. */
154 /* If non-zero, non-unique completions always show the list of matches. */
155 int _rl_complete_show_all = 0;
157 /* If non-zero, non-unique completions show the list of matches, unless it
158 is not possible to do partial completion and modify the line. */
159 int _rl_complete_show_unmodified = 0;
161 /* If non-zero, completed directory names have a slash appended. */
162 int _rl_complete_mark_directories = 1;
164 /* If non-zero, the symlinked directory completion behavior introduced in
165 readline-4.2a is disabled, and symlinks that point to directories have
166 a slash appended (subject to the value of _rl_complete_mark_directories).
167 This is user-settable via the mark-symlinked-directories variable. */
168 int _rl_complete_mark_symlink_dirs = 0;
170 /* If non-zero, completions are printed horizontally in alphabetical order,
172 int _rl_print_completions_horizontally;
174 /* Non-zero means that case is not significant in filename completion. */
175 #if defined (__MSDOS__) && !defined (__DJGPP__)
176 int _rl_completion_case_fold = 1;
178 int _rl_completion_case_fold = 0;
181 /* Non-zero means that `-' and `_' are equivalent when comparing filenames
183 int _rl_completion_case_map = 0;
185 /* If zero, don't match hidden files (filenames beginning with a `.' on
186 Unix) when doing filename completion. */
187 int _rl_match_hidden_files = 1;
189 /* Length in characters of a common prefix replaced with an ellipsis (`...')
190 when displaying completion matches. Matches whose printable portion has
191 more than this number of displaying characters in common will have the common
192 display prefix replaced with an ellipsis. */
193 int _rl_completion_prefix_display_length = 0;
195 /* The readline-private number of screen columns to use when displaying
196 matches. If < 0 or > _rl_screenwidth, it is ignored. */
197 int _rl_completion_columns = -1;
199 /* Global variables available to applications using readline. */
201 #if defined (VISIBLE_STATS)
202 /* Non-zero means add an additional character to each filename displayed
203 during listing completion iff rl_filename_completion_desired which helps
204 to indicate the type of file being listed. */
205 int rl_visible_stats = 0;
206 #endif /* VISIBLE_STATS */
208 #if defined (COLOR_SUPPORT)
209 /* Non-zero means to use colors to indicate file type when listing possible
210 completions. The colors used are taken from $LS_COLORS, if set. */
211 int _rl_colored_stats = 0;
214 /* If non-zero, when completing in the middle of a word, don't insert
215 characters from the match that match characters following point in
216 the word. This means, for instance, completing when the cursor is
217 after the `e' in `Makefile' won't result in `Makefilefile'. */
218 int _rl_skip_completed_text = 0;
220 /* If non-zero, menu completion displays the common prefix first in the
221 cycle of possible completions instead of the last. */
222 int _rl_menu_complete_prefix_first = 0;
224 /* If non-zero, then this is the address of a function to call when
225 completing on a directory name. The function is called with
226 the address of a string (the current directory name) as an arg. */
227 rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
229 rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
231 rl_icppfunc_t *rl_filename_stat_hook = (rl_icppfunc_t *)NULL;
233 /* If non-zero, this is the address of a function to call when reading
234 directory entries from the filesystem for completion and comparing
235 them to the partial word to be completed. The function should
236 either return its first argument (if no conversion takes place) or
237 newly-allocated memory. This can, for instance, convert filenames
238 between character sets for comparison against what's typed at the
239 keyboard. The returned value is what is added to the list of
240 matches. The second argument is the length of the filename to be
242 rl_dequote_func_t *rl_filename_rewrite_hook = (rl_dequote_func_t *)NULL;
244 /* Non-zero means readline completion functions perform tilde expansion. */
245 int rl_complete_with_tilde_expansion = 0;
247 /* Pointer to the generator function for completion_matches ().
248 NULL means to use rl_filename_completion_function (), the default filename
250 rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL;
252 /* Pointer to generator function for rl_menu_complete (). NULL means to use
253 *rl_completion_entry_function (see above). */
254 rl_compentry_func_t *rl_menu_completion_entry_function = (rl_compentry_func_t *)NULL;
256 /* Pointer to alternative function to create matches.
257 Function is called with TEXT, START, and END.
258 START and END are indices in RL_LINE_BUFFER saying what the boundaries
260 If this function exists and returns NULL then call the value of
261 rl_completion_entry_function to try to match, otherwise use the
262 array of strings returned. */
263 rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL;
265 /* Non-zero means to suppress normal filename completion after the
266 user-specified completion function has been called. */
267 int rl_attempted_completion_over = 0;
269 /* Set to a character indicating the type of completion being performed
270 by rl_complete_internal, available for use by application completion
272 int rl_completion_type = 0;
274 /* Up to this many items will be displayed in response to a
275 possible-completions call. After that, we ask the user if
276 she is sure she wants to see them all. A negative value means
278 int rl_completion_query_items = 100;
280 int _rl_page_completions = 1;
282 /* The basic list of characters that signal a break between words for the
283 completer routine. The contents of this variable is what breaks words
284 in the shell, i.e. " \t\n\"\\'`@$><=" */
285 const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
287 /* List of basic quoting characters. */
288 const char *rl_basic_quote_characters = "\"'";
290 /* The list of characters that signal a break between words for
291 rl_complete_internal. The default list is the contents of
292 rl_basic_word_break_characters. */
293 /*const*/ char *rl_completer_word_break_characters = (/*const*/ char *)NULL;
295 /* Hook function to allow an application to set the completion word
296 break characters before readline breaks up the line. Allows
297 position-dependent word break characters. */
298 rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL;
300 /* List of characters which can be used to quote a substring of the line.
301 Completion occurs on the entire substring, and within the substring
302 rl_completer_word_break_characters are treated as any other character,
303 unless they also appear within this list. */
304 const char *rl_completer_quote_characters = (const char *)NULL;
306 /* List of characters that should be quoted in filenames by the completer. */
307 const char *rl_filename_quote_characters = (const char *)NULL;
309 /* List of characters that are word break characters, but should be left
310 in TEXT when it is passed to the completion function. The shell uses
311 this to help determine what kind of completing to do. */
312 const char *rl_special_prefixes = (const char *)NULL;
314 /* If non-zero, then disallow duplicates in the matches. */
315 int rl_ignore_completion_duplicates = 1;
317 /* Non-zero means that the results of the matches are to be treated
318 as filenames. This is ALWAYS zero on entry, and can only be changed
319 within a completion entry finder function. */
320 int rl_filename_completion_desired = 0;
322 /* Non-zero means that the results of the matches are to be quoted using
323 double quotes (or an application-specific quoting mechanism) if the
324 filename contains any characters in rl_filename_quote_chars. This is
325 ALWAYS non-zero on entry, and can only be changed within a completion
326 entry finder function. */
327 int rl_filename_quoting_desired = 1;
329 /* This function, if defined, is called by the completer when real
330 filename completion is done, after all the matching names have been
331 generated. It is passed a (char**) known as matches in the code below.
332 It consists of a NULL-terminated array of pointers to potential
333 matching strings. The 1st element (matches[0]) is the maximal
334 substring that is common to all matches. This function can re-arrange
335 the list of matches as required, but all elements of the array must be
336 free()'d if they are deleted. The main intent of this function is
337 to implement FIGNORE a la SunOS csh. */
338 rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL;
340 /* Set to a function to quote a filename in an application-specific fashion.
341 Called with the text to quote, the type of match found (single or multiple)
342 and a pointer to the quoting character to be used, which the function can
344 rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename;
346 /* Function to call to remove quoting characters from a filename. Called
347 before completion is attempted, so the embedded quotes do not interfere
348 with matching names in the file system. Readline doesn't do anything
349 with this; it's set only by applications. */
350 rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL;
352 /* Function to call to decide whether or not a word break character is
353 quoted. If a character is quoted, it does not break words for the
355 rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL;
357 /* If non-zero, the completion functions don't append anything except a
358 possible closing quote. This is set to 0 by rl_complete_internal and
359 may be changed by an application-specific completion function. */
360 int rl_completion_suppress_append = 0;
362 /* Character appended to completed words when at the end of the line. The
363 default is a space. */
364 int rl_completion_append_character = ' ';
366 /* If non-zero, the completion functions don't append any closing quote.
367 This is set to 0 by rl_complete_internal and may be changed by an
368 application-specific completion function. */
369 int rl_completion_suppress_quote = 0;
371 /* Set to any quote character readline thinks it finds before any application
372 completion function is called. */
373 int rl_completion_quote_character;
375 /* Set to a non-zero value if readline found quoting anywhere in the word to
376 be completed; set before any application completion function is called. */
377 int rl_completion_found_quote;
379 /* If non-zero, a slash will be appended to completed filenames that are
380 symbolic links to directory names, subject to the value of the
381 mark-directories variable (which is user-settable). This exists so
382 that application completion functions can override the user's preference
383 (set via the mark-symlinked-directories variable) if appropriate.
384 It's set to the value of _rl_complete_mark_symlink_dirs in
385 rl_complete_internal before any application-specific completion
386 function is called, so without that function doing anything, the user's
387 preferences are honored. */
388 int rl_completion_mark_symlink_dirs;
390 /* If non-zero, inhibit completion (temporarily). */
391 int rl_inhibit_completion;
393 /* Set to the last key used to invoke one of the completion functions */
394 int rl_completion_invoking_key;
396 /* If non-zero, sort the completion matches. On by default. */
397 int rl_sort_completion_matches = 1;
399 /* Variables local to this file. */
401 /* Local variable states what happened during the last completion attempt. */
402 static int completion_changed_buffer;
404 /* The result of the query to the user about displaying completion matches */
405 static int completion_y_or_n;
407 /*************************************/
409 /* Bindable completion functions */
411 /*************************************/
413 /* Complete the word at or before point. You have supplied the function
414 that does the initial simple matching selection algorithm (see
415 rl_completion_matches ()). The default is to do filename completion. */
417 rl_complete (ignore, invoking_key)
418 int ignore, invoking_key;
420 rl_completion_invoking_key = invoking_key;
422 if (rl_inhibit_completion)
423 return (_rl_insert_char (ignore, invoking_key));
424 else if (rl_last_func == rl_complete && !completion_changed_buffer)
425 return (rl_complete_internal ('?'));
426 else if (_rl_complete_show_all)
427 return (rl_complete_internal ('!'));
428 else if (_rl_complete_show_unmodified)
429 return (rl_complete_internal ('@'));
431 return (rl_complete_internal (TAB));
434 /* List the possible completions. See description of rl_complete (). */
436 rl_possible_completions (ignore, invoking_key)
437 int ignore, invoking_key;
439 rl_completion_invoking_key = invoking_key;
440 return (rl_complete_internal ('?'));
444 rl_insert_completions (ignore, invoking_key)
445 int ignore, invoking_key;
447 rl_completion_invoking_key = invoking_key;
448 return (rl_complete_internal ('*'));
451 /* Return the correct value to pass to rl_complete_internal performing
452 the same tests as rl_complete. This allows consecutive calls to an
453 application's completion function to list possible completions and for
454 an application-specific completion function to honor the
455 show-all-if-ambiguous readline variable. */
457 rl_completion_mode (cfunc)
458 rl_command_func_t *cfunc;
460 if (rl_last_func == cfunc && !completion_changed_buffer)
462 else if (_rl_complete_show_all)
464 else if (_rl_complete_show_unmodified)
470 /************************************/
472 /* Completion utility functions */
474 /************************************/
476 /* Reset readline state on a signal or other event. */
478 _rl_reset_completion_state ()
480 rl_completion_found_quote = 0;
481 rl_completion_quote_character = 0;
485 _rl_complete_sigcleanup (sig, ptr)
489 if (sig == SIGINT) /* XXX - for now */
490 _rl_free_match_list ((char **)ptr);
493 /* Set default values for readline word completion. These are the variables
494 that application completion functions can change or inspect. */
496 set_completion_defaults (what_to_do)
499 /* Only the completion entry function can change these. */
500 rl_filename_completion_desired = 0;
501 rl_filename_quoting_desired = 1;
502 rl_completion_type = what_to_do;
503 rl_completion_suppress_append = rl_completion_suppress_quote = 0;
504 rl_completion_append_character = ' ';
506 /* The completion entry function may optionally change this. */
507 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
510 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
512 get_y_or_n (for_pager)
517 /* For now, disable pager in callback mode, until we later convert to state
518 driven functions. Have to wait until next major version to add new
519 state definition, since it will change value of RL_STATE_DONE. */
520 #if defined (READLINE_CALLBACKS)
521 if (RL_ISSTATE (RL_STATE_CALLBACK))
527 RL_SETSTATE(RL_STATE_MOREINPUT);
529 RL_UNSETSTATE(RL_STATE_MOREINPUT);
531 if (c == 'y' || c == 'Y' || c == ' ')
533 if (c == 'n' || c == 'N' || c == RUBOUT)
535 if (c == ABORT_CHAR || c < 0)
536 _rl_abort_internal ();
537 if (for_pager && (c == NEWLINE || c == RETURN))
539 if (for_pager && (c == 'q' || c == 'Q'))
546 _rl_internal_pager (lines)
551 fprintf (rl_outstream, "--More--");
552 fflush (rl_outstream);
554 _rl_erase_entire_line ();
564 path_isdir (filename)
565 const char *filename;
569 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
572 #if defined (VISIBLE_STATS)
573 /* Return the character which best describes FILENAME.
574 `@' for symbolic links
579 `%' for character special devices
580 `#' for block special devices */
590 /* Short-circuit a //server on cygwin, since that will always behave as
593 if (filename[0] == '/' && filename[1] == '/' && strchr (filename+2, '/') == 0)
598 if (rl_filename_stat_hook)
600 f = savestring (filename);
601 (*rl_filename_stat_hook) (&f);
607 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
608 r = lstat (fn, &finfo);
610 r = stat (fn, &finfo);
617 if (S_ISDIR (finfo.st_mode))
619 #if defined (S_ISCHR)
620 else if (S_ISCHR (finfo.st_mode))
623 #if defined (S_ISBLK)
624 else if (S_ISBLK (finfo.st_mode))
627 #if defined (S_ISLNK)
628 else if (S_ISLNK (finfo.st_mode))
631 #if defined (S_ISSOCK)
632 else if (S_ISSOCK (finfo.st_mode))
634 #endif /* S_ISSOCK */
635 #if defined (S_ISFIFO)
636 else if (S_ISFIFO (finfo.st_mode))
639 else if (S_ISREG (finfo.st_mode))
641 if (access (filename, X_OK) == 0)
648 #endif /* VISIBLE_STATS */
650 #if defined (COLOR_SUPPORT)
652 colored_stat_start (filename)
655 _rl_set_normal_color ();
656 return (_rl_print_color_indicator (filename));
662 _rl_prep_non_filename_text ();
663 _rl_put_indicator (&_rl_color_indicator[C_CLR_TO_EOL]);
667 /* Return the portion of PATHNAME that should be output when listing
668 possible completions. If we are hacking filename completion, we
669 are only interested in the basename, the portion following the
670 final slash. Otherwise, we return what we were passed. Since
671 printing empty strings is not very informative, if we're doing
672 filename completion, and the basename is the empty string, we look
673 for the previous slash and return the portion following that. If
674 there's no previous slash, we just return what we were passed. */
676 printable_part (pathname)
681 if (rl_filename_completion_desired == 0) /* don't need to do anything */
684 temp = strrchr (pathname, '/');
685 #if defined (__MSDOS__)
686 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
690 if (temp == 0 || *temp == '\0')
692 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
693 Look for a previous slash and, if one is found, return the portion
694 following that slash. If there's no previous slash, just return the
695 pathname we were passed. */
696 else if (temp[1] == '\0')
698 for (x = temp - 1; x > pathname; x--)
701 return ((*x == '/') ? x + 1 : pathname);
707 /* Compute width of STRING when displayed on screen by print_filename */
713 #if defined (HANDLE_MULTIBYTE)
719 left = strlen (string) + 1;
720 memset (&ps, 0, sizeof (mbstate_t));
726 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
733 #if defined (HANDLE_MULTIBYTE)
734 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
735 if (MB_INVALIDCH (clen))
739 memset (&ps, 0, sizeof (mbstate_t));
741 else if (MB_NULLWCH (clen))
747 width += (w >= 0) ? w : 1;
759 #define ELLIPSIS_LEN 3
762 fnprint (to_print, prefix_bytes)
763 const char *to_print;
768 #if defined (HANDLE_MULTIBYTE)
775 end = to_print + strlen (to_print) + 1;
776 memset (&ps, 0, sizeof (mbstate_t));
781 /* Don't print only the ellipsis if the common prefix is one of the
782 possible completions */
783 if (to_print[prefix_bytes] == '\0')
790 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
791 for (w = 0; w < ELLIPSIS_LEN; w++)
792 putc (ellipsis, rl_outstream);
793 printed_len = ELLIPSIS_LEN;
796 s = to_print + prefix_bytes;
801 putc ('^', rl_outstream);
802 putc (UNCTRL (*s), rl_outstream);
805 #if defined (HANDLE_MULTIBYTE)
806 memset (&ps, 0, sizeof (mbstate_t));
809 else if (*s == RUBOUT)
811 putc ('^', rl_outstream);
812 putc ('?', rl_outstream);
815 #if defined (HANDLE_MULTIBYTE)
816 memset (&ps, 0, sizeof (mbstate_t));
821 #if defined (HANDLE_MULTIBYTE)
822 tlen = mbrtowc (&wc, s, end - s, &ps);
823 if (MB_INVALIDCH (tlen))
827 memset (&ps, 0, sizeof (mbstate_t));
829 else if (MB_NULLWCH (tlen))
834 width = (w >= 0) ? w : 1;
836 fwrite (s, 1, tlen, rl_outstream);
838 printed_len += width;
840 putc (*s, rl_outstream);
850 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
851 are using it, check for and output a single character for `special'
852 filenames. Return the number of characters we output. */
855 print_filename (to_print, full_pathname, prefix_bytes)
856 char *to_print, *full_pathname;
859 int printed_len, extension_char, slen, tlen;
860 char *s, c, *new_full_pathname, *dn;
863 #if defined (COLOR_SUPPORT)
864 /* Defer printing if we want to prefix with a color indicator */
865 if (_rl_colored_stats == 0 || rl_filename_completion_desired == 0)
867 printed_len = fnprint (to_print, prefix_bytes);
869 if (rl_filename_completion_desired && (
870 #if defined (VISIBLE_STATS)
873 #if defined (COLOR_SUPPORT)
876 _rl_complete_mark_directories))
878 /* If to_print != full_pathname, to_print is the basename of the
879 path passed. In this case, we try to expand the directory
880 name before checking for the stat character. */
881 if (to_print != full_pathname)
883 /* Terminate the directory name. */
887 /* If setting the last slash in full_pathname to a NUL results in
888 full_pathname being the empty string, we are trying to complete
889 files in the root directory. If we pass a null string to the
890 bash directory completion hook, for example, it will expand it
891 to the current directory. We just want the `/'. */
892 if (full_pathname == 0 || *full_pathname == 0)
894 else if (full_pathname[0] != '/')
896 else if (full_pathname[1] == 0)
897 dn = "//"; /* restore trailing slash to `//' */
898 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
899 dn = "/"; /* don't turn /// into // */
902 s = tilde_expand (dn);
903 if (rl_directory_completion_hook)
904 (*rl_directory_completion_hook) (&s);
907 tlen = strlen (to_print);
908 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
909 strcpy (new_full_pathname, s);
910 if (s[slen - 1] == '/')
913 new_full_pathname[slen] = '/';
914 new_full_pathname[slen] = '/';
915 strcpy (new_full_pathname + slen + 1, to_print);
917 #if defined (VISIBLE_STATS)
918 if (rl_visible_stats)
919 extension_char = stat_char (new_full_pathname);
922 if (_rl_complete_mark_directories)
925 if (rl_directory_completion_hook == 0 && rl_filename_stat_hook)
927 dn = savestring (new_full_pathname);
928 (*rl_filename_stat_hook) (&dn);
929 free (new_full_pathname);
930 new_full_pathname = dn;
932 if (path_isdir (new_full_pathname))
933 extension_char = '/';
936 #if defined (COLOR_SUPPORT)
937 if (_rl_colored_stats)
939 colored_stat_start (new_full_pathname);
940 printed_len = fnprint (to_print, prefix_bytes);
945 xfree (new_full_pathname);
950 s = tilde_expand (full_pathname);
951 #if defined (VISIBLE_STATS)
952 if (rl_visible_stats)
953 extension_char = stat_char (s);
956 if (_rl_complete_mark_directories && path_isdir (s))
957 extension_char = '/';
959 #if defined (COLOR_SUPPORT)
960 if (_rl_colored_stats)
962 colored_stat_start (s);
963 printed_len = fnprint (to_print, prefix_bytes);
973 putc (extension_char, rl_outstream);
982 rl_quote_filename (s, rtype, qcp)
989 r = (char *)xmalloc (strlen (s) + 2);
990 *r = *rl_completer_quote_characters;
993 *qcp = *rl_completer_quote_characters;
997 /* Find the bounds of the current word for completion purposes, and leave
998 rl_point set to the end of the word. This function skips quoted
999 substrings (characters between matched pairs of characters in
1000 rl_completer_quote_characters). First we try to find an unclosed
1001 quoted substring on which to do matching. If one is not found, we use
1002 the word break characters to find the boundaries of the current word.
1003 We call an application-specific function to decide whether or not a
1004 particular word break character is quoted; if that function returns a
1005 non-zero result, the character does not break a word. This function
1006 returns the opening quote character if we found an unclosed quoted
1007 substring, '\0' otherwise. FP, if non-null, is set to a value saying
1008 which (shell-like) quote characters we found (single quote, double
1009 quote, or backslash) anywhere in the string. DP, if non-null, is set to
1010 the value of the delimiter character that caused a word break. */
1013 _rl_find_completion_word (fp, dp)
1016 int scan, end, found_quote, delimiter, pass_next, isbrk;
1017 char quote_char, *brkchars;
1020 found_quote = delimiter = 0;
1024 if (rl_completion_word_break_hook)
1025 brkchars = (*rl_completion_word_break_hook) ();
1027 brkchars = rl_completer_word_break_characters;
1029 if (rl_completer_quote_characters)
1031 /* We have a list of characters which can be used in pairs to
1032 quote substrings for the completer. Try to find the start
1033 of an unclosed quoted substring. */
1034 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
1035 for (scan = pass_next = 0; scan < end; scan = MB_NEXTCHAR (rl_line_buffer, scan, 1, MB_FIND_ANY))
1043 /* Shell-like semantics for single quotes -- don't allow backslash
1044 to quote anything in single quotes, especially not the closing
1045 quote. If you don't like this, take out the check on the value
1047 if (quote_char != '\'' && rl_line_buffer[scan] == '\\')
1050 found_quote |= RL_QF_BACKSLASH;
1054 if (quote_char != '\0')
1056 /* Ignore everything until the matching close quote char. */
1057 if (rl_line_buffer[scan] == quote_char)
1059 /* Found matching close. Abandon this substring. */
1064 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
1066 /* Found start of a quoted substring. */
1067 quote_char = rl_line_buffer[scan];
1068 rl_point = scan + 1;
1069 /* Shell-like quoting conventions. */
1070 if (quote_char == '\'')
1071 found_quote |= RL_QF_SINGLE_QUOTE;
1072 else if (quote_char == '"')
1073 found_quote |= RL_QF_DOUBLE_QUOTE;
1075 found_quote |= RL_QF_OTHER_QUOTE;
1080 if (rl_point == end && quote_char == '\0')
1082 /* We didn't find an unclosed quoted substring upon which to do
1083 completion, so use the word break characters to find the
1084 substring on which to complete. */
1085 while (rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_ANY))
1087 scan = rl_line_buffer[rl_point];
1089 if (strchr (brkchars, scan) == 0)
1092 /* Call the application-specific function to tell us whether
1093 this word break character is quoted and should be skipped. */
1094 if (rl_char_is_quoted_p && found_quote &&
1095 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
1098 /* Convoluted code, but it avoids an n^2 algorithm with calls
1099 to char_is_quoted. */
1104 /* If we are at an unquoted word break, then advance past it. */
1105 scan = rl_line_buffer[rl_point];
1107 /* If there is an application-specific function to say whether or not
1108 a character is quoted and we found a quote character, let that
1109 function decide whether or not a character is a word break, even
1110 if it is found in rl_completer_word_break_characters. Don't bother
1111 if we're at the end of the line, though. */
1114 if (rl_char_is_quoted_p)
1115 isbrk = (found_quote == 0 ||
1116 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
1117 strchr (brkchars, scan) != 0;
1119 isbrk = strchr (brkchars, scan) != 0;
1123 /* If the character that caused the word break was a quoting
1124 character, then remember it as the delimiter. */
1125 if (rl_basic_quote_characters &&
1126 strchr (rl_basic_quote_characters, scan) &&
1127 (end - rl_point) > 1)
1130 /* If the character isn't needed to determine something special
1131 about what kind of completion to perform, then advance past it. */
1132 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0)
1142 return (quote_char);
1146 gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
1149 rl_compentry_func_t *our_func;
1150 int found_quote, quote_char;
1154 rl_completion_found_quote = found_quote;
1155 rl_completion_quote_character = quote_char;
1157 /* If the user wants to TRY to complete, but then wants to give
1158 up and use the default completion function, they set the
1159 variable rl_attempted_completion_function. */
1160 if (rl_attempted_completion_function)
1162 matches = (*rl_attempted_completion_function) (text, start, end);
1163 if (RL_SIG_RECEIVED())
1165 _rl_free_match_list (matches);
1167 RL_CHECK_SIGNALS ();
1170 if (matches || rl_attempted_completion_over)
1172 rl_attempted_completion_over = 0;
1177 /* XXX -- filename dequoting moved into rl_filename_completion_function */
1179 /* rl_completion_matches will check for signals as well to avoid a long
1180 delay while reading a directory. */
1181 matches = rl_completion_matches (text, our_func);
1182 if (RL_SIG_RECEIVED())
1184 _rl_free_match_list (matches);
1186 RL_CHECK_SIGNALS ();
1191 /* Filter out duplicates in MATCHES. This frees up the strings in
1194 remove_duplicate_matches (matches)
1197 char *lowest_common;
1202 /* Sort the items. */
1203 for (i = 0; matches[i]; i++)
1206 /* Sort the array without matches[0], since we need it to
1207 stay in place no matter what. */
1208 if (i && rl_sort_completion_matches)
1209 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1211 /* Remember the lowest common denominator for it may be unique. */
1212 lowest_common = savestring (matches[0]);
1214 for (i = newlen = 0; matches[i + 1]; i++)
1216 if (strcmp (matches[i], matches[i + 1]) == 0)
1219 matches[i] = (char *)&dead_slot;
1225 /* We have marked all the dead slots with (char *)&dead_slot.
1226 Copy all the non-dead entries into a new array. */
1227 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
1228 for (i = j = 1; matches[i]; i++)
1230 if (matches[i] != (char *)&dead_slot)
1231 temp_array[j++] = matches[i];
1233 temp_array[j] = (char *)NULL;
1235 if (matches[0] != (char *)&dead_slot)
1238 /* Place the lowest common denominator back in [0]. */
1239 temp_array[0] = lowest_common;
1241 /* If there is one string left, and it is identical to the
1242 lowest common denominator, then the LCD is the string to
1244 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
1246 xfree (temp_array[1]);
1247 temp_array[1] = (char *)NULL;
1249 return (temp_array);
1252 /* Find the common prefix of the list of matches, and put it into
1255 compute_lcd_of_matches (match_list, matches, text)
1260 register int i, c1, c2, si;
1261 int low; /* Count of max-matched characters. */
1263 char *dtext; /* dequoted TEXT, if needed */
1264 #if defined (HANDLE_MULTIBYTE)
1271 /* If only one match, just use that. Otherwise, compare each
1272 member of the list with the next, finding out where they
1276 match_list[0] = match_list[1];
1277 match_list[1] = (char *)NULL;
1281 for (i = 1, low = 100000; i < matches; i++)
1283 #if defined (HANDLE_MULTIBYTE)
1284 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1286 memset (&ps1, 0, sizeof (mbstate_t));
1287 memset (&ps2, 0, sizeof (mbstate_t));
1290 if (_rl_completion_case_fold)
1293 (c1 = _rl_to_lower(match_list[i][si])) &&
1294 (c2 = _rl_to_lower(match_list[i + 1][si]));
1296 #if defined (HANDLE_MULTIBYTE)
1297 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1299 v1 = mbrtowc(&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1);
1300 v2 = mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2);
1301 if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
1303 if (c1 != c2) /* do byte comparison */
1307 wc1 = towlower (wc1);
1308 wc2 = towlower (wc2);
1322 (c1 = match_list[i][si]) &&
1323 (c2 = match_list[i + 1][si]);
1325 #if defined (HANDLE_MULTIBYTE)
1326 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1330 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2))
1332 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1)
1345 /* If there were multiple matches, but none matched up to even the
1346 first character, and the user typed something, use that as the
1347 value of matches[0]. */
1348 if (low == 0 && text && *text)
1350 match_list[0] = (char *)xmalloc (strlen (text) + 1);
1351 strcpy (match_list[0], text);
1355 match_list[0] = (char *)xmalloc (low + 1);
1357 /* XXX - this might need changes in the presence of multibyte chars */
1359 /* If we are ignoring case, try to preserve the case of the string
1360 the user typed in the face of multiple matches differing in case. */
1361 if (_rl_completion_case_fold)
1363 /* We're making an assumption here:
1364 IF we're completing filenames AND
1365 the application has defined a filename dequoting function AND
1366 we found a quote character AND
1367 the application has requested filename quoting
1369 we assume that TEXT was dequoted before checking against
1370 the file system and needs to be dequoted here before we
1371 check against the list of matches
1373 dtext = (char *)NULL;
1374 if (rl_filename_completion_desired &&
1375 rl_filename_dequoting_function &&
1376 rl_completion_found_quote &&
1377 rl_filename_quoting_desired)
1379 dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
1383 /* sort the list to get consistent answers. */
1384 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1387 lx = (si <= low) ? si : low; /* check shorter of text and matches */
1388 /* Try to preserve the case of what the user typed in the presence of
1389 multiple matches: check each match for something that matches
1390 what the user typed taking case into account; use it up to common
1391 length of matches if one is found. If not, just use first match. */
1392 for (i = 1; i <= matches; i++)
1393 if (strncmp (match_list[i], text, lx) == 0)
1395 strncpy (match_list[0], match_list[i], low);
1398 /* no casematch, use first entry */
1400 strncpy (match_list[0], match_list[1], low);
1405 strncpy (match_list[0], match_list[1], low);
1407 match_list[0][low] = '\0';
1414 postprocess_matches (matchesp, matching_filenames)
1416 int matching_filenames;
1418 char *t, **matches, **temp_matches;
1421 matches = *matchesp;
1426 /* It seems to me that in all the cases we handle we would like
1427 to ignore duplicate possibilities. Scan for the text to
1428 insert being identical to the other completions. */
1429 if (rl_ignore_completion_duplicates)
1431 temp_matches = remove_duplicate_matches (matches);
1433 matches = temp_matches;
1436 /* If we are matching filenames, then here is our chance to
1437 do clever processing by re-examining the list. Call the
1438 ignore function with the array as a parameter. It can
1439 munge the array, deleting matches as it desires. */
1440 if (rl_ignore_some_completions_function && matching_filenames)
1442 for (nmatch = 1; matches[nmatch]; nmatch++)
1444 (void)(*rl_ignore_some_completions_function) (matches);
1445 if (matches == 0 || matches[0] == 0)
1448 *matchesp = (char **)0;
1453 /* If we removed some matches, recompute the common prefix. */
1454 for (i = 1; matches[i]; i++)
1456 if (i > 1 && i < nmatch)
1459 compute_lcd_of_matches (matches, i - 1, t);
1465 *matchesp = matches;
1470 complete_get_screenwidth ()
1475 cols = _rl_completion_columns;
1476 if (cols >= 0 && cols <= _rl_screenwidth)
1478 envcols = getenv ("COLUMNS");
1479 if (envcols && *envcols)
1480 cols = atoi (envcols);
1481 if (cols >= 0 && cols <= _rl_screenwidth)
1483 return _rl_screenwidth;
1486 /* A convenience function for displaying a list of strings in
1487 columnar format on readline's output stream. MATCHES is the list
1488 of strings, in argv format, LEN is the number of strings in MATCHES,
1489 and MAX is the length of the longest string in MATCHES. */
1491 rl_display_match_list (matches, len, max)
1495 int count, limit, printed_len, lines, cols;
1496 int i, j, k, l, common_length, sind;
1499 /* Find the length of the prefix common to all items: length as displayed
1500 characters (common_length) and as a byte index into the matches (sind) */
1501 common_length = sind = 0;
1502 if (_rl_completion_prefix_display_length > 0)
1504 t = printable_part (matches[0]);
1505 temp = strrchr (t, '/');
1506 common_length = temp ? fnwidth (temp) : fnwidth (t);
1507 sind = temp ? strlen (temp) : strlen (t);
1509 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
1510 max -= common_length - ELLIPSIS_LEN;
1512 common_length = sind = 0;
1515 /* How many items of MAX length can we fit in the screen window? */
1516 cols = complete_get_screenwidth ();
1519 if (limit != 1 && (limit * max == cols))
1522 /* If cols == 0, limit will end up -1 */
1523 if (cols < _rl_screenwidth && limit < 0)
1526 /* Avoid a possible floating exception. If max > cols,
1527 limit will be 0 and a divide-by-zero fault will result. */
1531 /* How many iterations of the printing loop? */
1532 count = (len + (limit - 1)) / limit;
1534 /* Watch out for special case. If LEN is less than LIMIT, then
1535 just do the inner printing loop.
1536 0 < len <= limit implies count = 1. */
1538 /* Sort the items if they are not already sorted. */
1539 if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
1540 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1545 if (_rl_print_completions_horizontally == 0)
1547 /* Print the sorted items, up-and-down alphabetically, like ls. */
1548 for (i = 1; i <= count; i++)
1550 for (j = 0, l = i; j < limit; j++)
1552 if (l > len || matches[l] == 0)
1556 temp = printable_part (matches[l]);
1557 printed_len = print_filename (temp, matches[l], sind);
1560 for (k = 0; k < max - printed_len; k++)
1561 putc (' ', rl_outstream);
1567 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count)
1569 lines = _rl_internal_pager (lines);
1577 /* Print the sorted items, across alphabetically, like ls -x. */
1578 for (i = 1; matches[i]; i++)
1580 temp = printable_part (matches[i]);
1581 printed_len = print_filename (temp, matches[i], sind);
1582 /* Have we reached the end of this line? */
1585 if (limit == 1 || (i && (limit > 1) && (i % limit) == 0))
1589 if (_rl_page_completions && lines >= _rl_screenheight - 1)
1591 lines = _rl_internal_pager (lines);
1597 for (k = 0; k < max - printed_len; k++)
1598 putc (' ', rl_outstream);
1605 /* Display MATCHES, a list of matching filenames in argv format. This
1606 handles the simple case -- a single match -- first. If there is more
1607 than one match, we compute the number of strings in the list and the
1608 length of the longest string, which will be needed by the display
1609 function. If the application wants to handle displaying the list of
1610 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1611 address of a function, and we just call it. If we're handling the
1612 display ourselves, we just call rl_display_match_list. We also check
1613 that the list of matches doesn't exceed the user-settable threshold,
1614 and ask the user if he wants to see the list if there are more matches
1615 than RL_COMPLETION_QUERY_ITEMS. */
1617 display_matches (matches)
1623 /* Move to the last visible line of a possibly-multiple-line command. */
1624 _rl_move_vert (_rl_vis_botlin);
1626 /* Handle simple case first. What if there is only one answer? */
1627 if (matches[1] == 0)
1629 temp = printable_part (matches[0]);
1631 print_filename (temp, matches[0], 0);
1634 rl_forced_update_display ();
1635 rl_display_fixed = 1;
1640 /* There is more than one answer. Find out how many there are,
1641 and find the maximum printed length of a single entry. */
1642 for (max = 0, i = 1; matches[i]; i++)
1644 temp = printable_part (matches[i]);
1645 len = fnwidth (temp);
1653 /* If the caller has defined a display hook, then call that now. */
1654 if (rl_completion_display_matches_hook)
1656 (*rl_completion_display_matches_hook) (matches, len, max);
1660 /* If there are many items, then ask the user if she really wants to
1662 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
1665 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1666 fflush (rl_outstream);
1667 if ((completion_y_or_n = get_y_or_n (0)) == 0)
1671 rl_forced_update_display ();
1672 rl_display_fixed = 1;
1678 rl_display_match_list (matches, len, max);
1680 rl_forced_update_display ();
1681 rl_display_fixed = 1;
1685 make_quoted_replacement (match, mtype, qc)
1688 char *qc; /* Pointer to quoting character, if any */
1690 int should_quote, do_replace;
1693 /* If we are doing completion on quoted substrings, and any matches
1694 contain any of the completer_word_break_characters, then auto-
1695 matically prepend the substring with a quote character (just pick
1696 the first one from the list of such) if it does not already begin
1697 with a quote string. FIXME: Need to remove any such automatically
1698 inserted quote character when it no longer is necessary, such as
1699 if we change the string we are completing on and the new set of
1700 matches don't require a quoted substring. */
1701 replacement = match;
1703 should_quote = match && rl_completer_quote_characters &&
1704 rl_filename_completion_desired &&
1705 rl_filename_quoting_desired;
1708 should_quote = should_quote && (!qc || !*qc ||
1709 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
1713 /* If there is a single match, see if we need to quote it.
1714 This also checks whether the common prefix of several
1715 matches needs to be quoted. */
1716 should_quote = rl_filename_quote_characters
1717 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0)
1720 do_replace = should_quote ? mtype : NO_MATCH;
1721 /* Quote the replacement, since we found an embedded
1722 word break character in a potential match. */
1723 if (do_replace != NO_MATCH && rl_filename_quoting_function)
1724 replacement = (*rl_filename_quoting_function) (match, do_replace, qc);
1726 return (replacement);
1730 insert_match (match, start, mtype, qc)
1735 char *replacement, *r;
1739 oqc = qc ? *qc : '\0';
1740 replacement = make_quoted_replacement (match, mtype, qc);
1742 /* Now insert the match. */
1745 rlen = strlen (replacement);
1746 /* Don't double an opening quote character. */
1747 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc &&
1748 replacement[0] == *qc)
1750 /* If make_quoted_replacement changed the quoting character, remove
1751 the opening quote and insert the (fully-quoted) replacement. */
1752 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
1753 replacement[0] != oqc)
1756 /* Don't double a closing quote character */
1757 if (qc && *qc && end && rl_line_buffer[rl_point] == *qc && replacement[rlen - 1] == *qc)
1759 if (_rl_skip_completed_text)
1762 while (start < rl_end && *r && rl_line_buffer[start] == *r)
1767 if (start <= end || *r)
1768 _rl_replace_text (r, start, end);
1769 rl_point = start + strlen (r);
1772 _rl_replace_text (replacement, start, end);
1773 if (replacement != match)
1774 xfree (replacement);
1778 /* Append any necessary closing quote and a separator character to the
1779 just-inserted match. If the user has specified that directories
1780 should be marked by a trailing `/', append one of those instead. The
1781 default trailing character is a space. Returns the number of characters
1782 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1783 has them) and don't add a suffix for a symlink to a directory. A
1784 nontrivial match is one that actually adds to the word being completed.
1785 The variable rl_completion_mark_symlink_dirs controls this behavior
1786 (it's initially set to the what the user has chosen, indicated by the
1787 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1788 application's completion function). */
1790 append_to_match (text, delimiter, quote_char, nontrivial_match)
1792 int delimiter, quote_char, nontrivial_match;
1794 char temp_string[4], *filename, *fn;
1795 int temp_string_index, s;
1798 temp_string_index = 0;
1799 if (quote_char && rl_point && rl_completion_suppress_quote == 0 &&
1800 rl_line_buffer[rl_point - 1] != quote_char)
1801 temp_string[temp_string_index++] = quote_char;
1804 temp_string[temp_string_index++] = delimiter;
1805 else if (rl_completion_suppress_append == 0 && rl_completion_append_character)
1806 temp_string[temp_string_index++] = rl_completion_append_character;
1808 temp_string[temp_string_index++] = '\0';
1810 if (rl_filename_completion_desired)
1812 filename = tilde_expand (text);
1813 if (rl_filename_stat_hook)
1815 fn = savestring (filename);
1816 (*rl_filename_stat_hook) (&fn);
1820 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1821 ? LSTAT (filename, &finfo)
1822 : stat (filename, &finfo);
1823 if (s == 0 && S_ISDIR (finfo.st_mode))
1825 if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */)
1827 /* This is clumsy. Avoid putting in a double slash if point
1828 is at the end of the line and the previous character is a
1830 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1832 else if (rl_line_buffer[rl_point] != '/')
1833 rl_insert_text ("/");
1837 /* Don't add anything if the filename is a symlink and resolves to a
1839 else if (s == 0 && S_ISLNK (finfo.st_mode) && path_isdir (filename))
1844 if (rl_point == rl_end && temp_string_index)
1845 rl_insert_text (temp_string);
1851 if (rl_point == rl_end && temp_string_index)
1852 rl_insert_text (temp_string);
1855 return (temp_string_index);
1859 insert_all_matches (matches, point, qc)
1867 rl_begin_undo_group ();
1868 /* remove any opening quote character; make_quoted_replacement will add
1870 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
1872 rl_delete_text (point, rl_point);
1877 for (i = 1; matches[i]; i++)
1879 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
1880 rl_insert_text (rp);
1881 rl_insert_text (" ");
1882 if (rp != matches[i])
1888 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
1889 rl_insert_text (rp);
1890 rl_insert_text (" ");
1891 if (rp != matches[0])
1894 rl_end_undo_group ();
1898 _rl_free_match_list (matches)
1906 for (i = 0; matches[i]; i++)
1911 /* Complete the word at or before point.
1912 WHAT_TO_DO says what to do with the completion.
1913 `?' means list the possible completions.
1914 TAB means do standard completion.
1915 `*' means insert all of the possible completions.
1916 `!' means to do standard completion, and list all possible completions if
1917 there is more than one.
1918 `@' means to do standard completion, and list all possible completions if
1919 there is more than one and partial completion is not possible. */
1921 rl_complete_internal (what_to_do)
1925 rl_compentry_func_t *our_func;
1926 int start, end, delimiter, found_quote, i, nontrivial_lcd;
1927 char *text, *saved_line_buffer;
1933 RL_SETSTATE(RL_STATE_COMPLETING);
1935 set_completion_defaults (what_to_do);
1937 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
1938 our_func = rl_completion_entry_function
1939 ? rl_completion_entry_function
1940 : rl_filename_completion_function;
1941 /* We now look backwards for the start of a filename/variable word. */
1943 found_quote = delimiter = 0;
1947 /* This (possibly) changes rl_point. If it returns a non-zero char,
1948 we know we have an open quote. */
1949 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
1954 text = rl_copy_text (start, end);
1955 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
1956 /* nontrivial_lcd is set if the common prefix adds something to the word
1958 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
1959 if (what_to_do == '!' || what_to_do == '@')
1960 tlen = strlen (text);
1966 FREE (saved_line_buffer);
1967 completion_changed_buffer = 0;
1968 RL_UNSETSTATE(RL_STATE_COMPLETING);
1969 _rl_reset_completion_state ();
1973 /* If we are matching filenames, the attempted completion function will
1974 have set rl_filename_completion_desired to a non-zero value. The basic
1975 rl_filename_completion_function does this. */
1976 i = rl_filename_completion_desired;
1978 if (postprocess_matches (&matches, i) == 0)
1981 FREE (saved_line_buffer);
1982 completion_changed_buffer = 0;
1983 RL_UNSETSTATE(RL_STATE_COMPLETING);
1984 _rl_reset_completion_state ();
1993 /* Insert the first match with proper quoting. */
1994 if (what_to_do == TAB)
1997 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
1999 else if (*matches[0] && matches[1] == 0)
2000 /* should we perform the check only if there are multiple matches? */
2001 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
2002 else if (*matches[0]) /* what_to_do != TAB && multiple matches */
2004 mlen = *matches[0] ? strlen (matches[0]) : 0;
2006 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
2009 /* If there are more matches, ring the bell to indicate.
2010 If we are in vi mode, Posix.2 says to not ring the bell.
2011 If the `show-all-if-ambiguous' variable is set, display
2012 all the matches immediately. Otherwise, if this was the
2013 only match, and we are hacking files, check the file to
2014 see if it was a directory. If so, and the `mark-directories'
2015 variable is set, add a '/' to the name. If not, and we
2016 are at the end of the line, then add a space. */
2019 if (what_to_do == '!')
2021 display_matches (matches);
2024 else if (what_to_do == '@')
2026 if (nontrivial_lcd == 0)
2027 display_matches (matches);
2030 else if (rl_editing_mode != vi_mode)
2031 rl_ding (); /* There are other matches remaining. */
2034 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2039 insert_all_matches (matches, start, "e_char);
2043 if (rl_completion_display_matches_hook == 0)
2045 _rl_sigcleanup = _rl_complete_sigcleanup;
2046 _rl_sigcleanarg = matches;
2048 display_matches (matches);
2050 _rl_sigcleanarg = 0;
2054 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do);
2056 FREE (saved_line_buffer);
2057 RL_UNSETSTATE(RL_STATE_COMPLETING);
2058 _rl_free_match_list (matches);
2059 _rl_reset_completion_state ();
2063 _rl_free_match_list (matches);
2065 /* Check to see if the line has changed through all of this manipulation. */
2066 if (saved_line_buffer)
2068 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
2069 xfree (saved_line_buffer);
2072 RL_UNSETSTATE(RL_STATE_COMPLETING);
2073 _rl_reset_completion_state ();
2077 /***************************************************************/
2079 /* Application-callable completion match generator functions */
2081 /***************************************************************/
2083 /* Return an array of (char *) which is a list of completions for TEXT.
2084 If there are no completions, return a NULL pointer.
2085 The first entry in the returned array is the substitution for TEXT.
2086 The remaining entries are the possible completions.
2087 The array is terminated with a NULL pointer.
2089 ENTRY_FUNCTION is a function of two args, and returns a (char *).
2090 The first argument is TEXT.
2091 The second is a state argument; it should be zero on the first call, and
2092 non-zero on subsequent calls. It returns a NULL pointer to the caller
2093 when there are no more matches.
2096 rl_completion_matches (text, entry_function)
2098 rl_compentry_func_t *entry_function;
2102 /* Number of slots in match_list. */
2103 int match_list_size;
2105 /* The list of matches. */
2108 /* Number of matches actually found. */
2111 /* Temporary string binder. */
2115 match_list_size = 10;
2116 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
2117 match_list[1] = (char *)NULL;
2119 while (string = (*entry_function) (text, matches))
2121 if (RL_SIG_RECEIVED ())
2123 /* Start at 1 because we don't set matches[0] in this function.
2124 Only free the list members if we're building match list from
2125 rl_filename_completion_function, since we know that doesn't
2126 free the strings it returns. */
2127 if (entry_function == rl_filename_completion_function)
2129 for (i = 1; match_list[i]; i++)
2130 xfree (match_list[i]);
2134 match_list_size = 0;
2136 RL_CHECK_SIGNALS ();
2139 if (matches + 1 >= match_list_size)
2140 match_list = (char **)xrealloc
2141 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
2143 if (match_list == 0)
2144 return (match_list);
2146 match_list[++matches] = string;
2147 match_list[matches + 1] = (char *)NULL;
2150 /* If there were any matches, then look through them finding out the
2151 lowest common denominator. That then becomes match_list[0]. */
2153 compute_lcd_of_matches (match_list, matches, text);
2154 else /* There were no matches. */
2157 match_list = (char **)NULL;
2159 return (match_list);
2162 /* A completion function for usernames.
2163 TEXT contains a partial username preceded by a random
2164 character (usually `~'). */
2166 rl_username_completion_function (text, state)
2170 #if defined (__WIN32__) || defined (__OPENNT)
2171 return (char *)NULL;
2172 #else /* !__WIN32__ && !__OPENNT) */
2173 static char *username = (char *)NULL;
2174 static struct passwd *entry;
2175 static int namelen, first_char, first_char_loc;
2183 first_char_loc = first_char == '~';
2185 username = savestring (&text[first_char_loc]);
2186 namelen = strlen (username);
2187 #if defined (HAVE_GETPWENT)
2192 #if defined (HAVE_GETPWENT)
2193 while (entry = getpwent ())
2195 /* Null usernames should result in all users as possible completions. */
2196 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
2203 #if defined (HAVE_GETPWENT)
2206 return ((char *)NULL);
2210 value = (char *)xmalloc (2 + strlen (entry->pw_name));
2214 strcpy (value + first_char_loc, entry->pw_name);
2216 if (first_char == '~')
2217 rl_filename_completion_desired = 1;
2221 #endif /* !__WIN32__ && !__OPENNT */
2224 /* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME
2225 (FILENAME_LEN). If _rl_completion_case_fold is set, compare without
2226 regard to the alphabetic case of characters. If
2227 _rl_completion_case_map is set, make `-' and `_' equivalent. CONVFN is
2228 the possibly-converted directory entry; FILENAME is what the user typed. */
2230 complete_fncmp (convfn, convlen, filename, filename_len)
2233 const char *filename;
2236 register char *s1, *s2;
2238 #if defined (HANDLE_MULTIBYTE)
2244 #if defined (HANDLE_MULTIBYTE)
2245 memset (&ps1, 0, sizeof (mbstate_t));
2246 memset (&ps2, 0, sizeof (mbstate_t));
2249 if (filename_len == 0)
2251 if (convlen < filename_len)
2255 s1 = (char *)convfn;
2256 s2 = (char *)filename;
2258 /* Otherwise, if these match up to the length of filename, then
2260 if (_rl_completion_case_fold && _rl_completion_case_map)
2262 /* Case-insensitive comparison treating _ and - as equivalent */
2263 #if defined (HANDLE_MULTIBYTE)
2264 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2268 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2269 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2270 if (v1 == 0 && v2 == 0)
2272 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2274 if (*s1 != *s2) /* do byte comparison */
2276 else if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2281 wc1 = towlower (wc1);
2282 wc2 = towlower (wc2);
2286 if ((wc1 == L'-' || wc1 == L'_') && (wc2 == L'-' || wc2 == L'_'))
2298 d = _rl_to_lower (*s1) - _rl_to_lower (*s2);
2299 /* *s1 == [-_] && *s2 == [-_] */
2300 if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2304 s1++; s2++; /* already checked convlen >= filename_len */
2311 else if (_rl_completion_case_fold)
2313 #if defined (HANDLE_MULTIBYTE)
2314 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2318 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2319 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2320 if (v1 == 0 && v2 == 0)
2322 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2324 if (*s1 != *s2) /* do byte comparison */
2329 wc1 = towlower (wc1);
2330 wc2 = towlower (wc2);
2342 if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) &&
2343 (convlen >= filename_len) &&
2344 (_rl_strnicmp (filename, convfn, filename_len) == 0))
2349 if ((convfn[0] == filename[0]) &&
2350 (convlen >= filename_len) &&
2351 (strncmp (filename, convfn, filename_len) == 0))
2357 /* Okay, now we write the entry_function for filename completion. In the
2358 general case. Note that completion in the shell is a little different
2359 because of all the pathnames that must be followed when looking up the
2360 completion for a command. */
2362 rl_filename_completion_function (text, state)
2366 static DIR *directory = (DIR *)NULL;
2367 static char *filename = (char *)NULL;
2368 static char *dirname = (char *)NULL;
2369 static char *users_dirname = (char *)NULL;
2370 static int filename_len;
2371 char *temp, *dentry, *convfn;
2372 int dirlen, dentlen, convlen;
2373 struct dirent *entry;
2375 /* If we don't have any state, then do some initialization. */
2378 /* If we were interrupted before closing the directory or reading
2379 all of its contents, close it. */
2382 closedir (directory);
2383 directory = (DIR *)NULL;
2387 FREE (users_dirname);
2389 filename = savestring (text);
2392 dirname = savestring (text);
2394 temp = strrchr (dirname, '/');
2396 #if defined (__MSDOS__)
2397 /* special hack for //X/... */
2398 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/')
2399 temp = strrchr (dirname + 3, '/');
2404 strcpy (filename, ++temp);
2407 #if defined (__MSDOS__)
2408 /* searches from current directory on the drive */
2409 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':')
2411 strcpy (filename, dirname + 2);
2421 /* We aren't done yet. We also support the "~user" syntax. */
2423 /* Save the version of the directory that the user typed, dequoting
2425 if (rl_completion_found_quote && rl_filename_dequoting_function)
2426 users_dirname = (*rl_filename_dequoting_function) (dirname, rl_completion_quote_character);
2428 users_dirname = savestring (dirname);
2430 if (*dirname == '~')
2432 temp = tilde_expand (dirname);
2437 /* We have saved the possibly-dequoted version of the directory name
2438 the user typed. Now transform the directory name we're going to
2439 pass to opendir(2). The directory rewrite hook modifies only the
2440 directory name; the directory completion hook modifies both the
2441 directory name passed to opendir(2) and the version the user
2442 typed. Both the directory completion and rewrite hooks should perform
2443 any necessary dequoting. The hook functions return 1 if they modify
2444 the directory name argument. If either hook returns 0, it should
2445 not modify the directory name pointer passed as an argument. */
2446 if (rl_directory_rewrite_hook)
2447 (*rl_directory_rewrite_hook) (&dirname);
2448 else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname))
2450 xfree (users_dirname);
2451 users_dirname = savestring (dirname);
2453 else if (rl_completion_found_quote && rl_filename_dequoting_function)
2455 /* delete single and double quotes */
2457 dirname = savestring (users_dirname);
2459 directory = opendir (dirname);
2461 /* Now dequote a non-null filename. FILENAME will not be NULL, but may
2463 if (*filename && rl_completion_found_quote && rl_filename_dequoting_function)
2465 /* delete single and double quotes */
2466 temp = (*rl_filename_dequoting_function) (filename, rl_completion_quote_character);
2470 filename_len = strlen (filename);
2472 rl_filename_completion_desired = 1;
2475 /* At this point we should entertain the possibility of hacking wildcarded
2476 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2477 contains globbing characters, then build an array of directories, and
2478 then map over that list while completing. */
2479 /* *** UNIMPLEMENTED *** */
2481 /* Now that we have some state, we can read the directory. */
2483 entry = (struct dirent *)NULL;
2484 while (directory && (entry = readdir (directory)))
2486 convfn = dentry = entry->d_name;
2487 convlen = dentlen = D_NAMLEN (entry);
2489 if (rl_filename_rewrite_hook)
2491 convfn = (*rl_filename_rewrite_hook) (dentry, dentlen);
2492 convlen = (convfn == dentry) ? dentlen : strlen (convfn);
2495 /* Special case for no filename. If the user has disabled the
2496 `match-hidden-files' variable, skip filenames beginning with `.'.
2497 All other entries except "." and ".." match. */
2498 if (filename_len == 0)
2500 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (convfn))
2503 if (convfn[0] != '.' ||
2504 (convfn[1] && (convfn[1] != '.' || convfn[2])))
2509 if (complete_fncmp (convfn, convlen, filename, filename_len))
2518 closedir (directory);
2519 directory = (DIR *)NULL;
2524 dirname = (char *)NULL;
2529 filename = (char *)NULL;
2533 xfree (users_dirname);
2534 users_dirname = (char *)NULL;
2537 return (char *)NULL;
2541 /* dirname && (strcmp (dirname, ".") != 0) */
2542 if (dirname && (dirname[0] != '.' || dirname[1]))
2544 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
2546 dirlen = strlen (dirname);
2547 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
2548 strcpy (temp, dirname);
2549 /* Canonicalization cuts off any final slash present. We
2550 may need to add it back. */
2551 if (dirname[dirlen - 1] != '/')
2553 temp[dirlen++] = '/';
2554 temp[dirlen] = '\0';
2559 dirlen = strlen (users_dirname);
2560 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
2561 strcpy (temp, users_dirname);
2562 /* Make sure that temp has a trailing slash here. */
2563 if (users_dirname[dirlen - 1] != '/')
2564 temp[dirlen++] = '/';
2567 strcpy (temp + dirlen, convfn);
2570 temp = savestring (convfn);
2572 if (convfn != dentry)
2579 /* An initial implementation of a menu completion function a la tcsh. The
2580 first time (if the last readline command was not rl_old_menu_complete), we
2581 generate the list of matches. This code is very similar to the code in
2582 rl_complete_internal -- there should be a way to combine the two. Then,
2583 for each item in the list of matches, we insert the match in an undoable
2584 fashion, with the appropriate character appended (this happens on the
2585 second and subsequent consecutive calls to rl_old_menu_complete). When we
2586 hit the end of the match list, we restore the original unmatched text,
2587 ring the bell, and reset the counter to zero. */
2589 rl_old_menu_complete (count, invoking_key)
2590 int count, invoking_key;
2592 rl_compentry_func_t *our_func;
2593 int matching_filenames, found_quote;
2595 static char *orig_text;
2596 static char **matches = (char **)0;
2597 static int match_list_index = 0;
2598 static int match_list_size = 0;
2599 static int orig_start, orig_end;
2600 static char quote_char;
2601 static int delimiter;
2603 /* The first time through, we generate the list of matches and set things
2604 up to insert them. */
2605 if (rl_last_func != rl_old_menu_complete)
2607 /* Clean up from previous call, if any. */
2610 _rl_free_match_list (matches);
2612 match_list_index = match_list_size = 0;
2613 matches = (char **)NULL;
2615 rl_completion_invoking_key = invoking_key;
2617 RL_SETSTATE(RL_STATE_COMPLETING);
2619 /* Only the completion entry function can change these. */
2620 set_completion_defaults ('%');
2622 our_func = rl_menu_completion_entry_function;
2624 our_func = rl_completion_entry_function
2625 ? rl_completion_entry_function
2626 : rl_filename_completion_function;
2628 /* We now look backwards for the start of a filename/variable word. */
2629 orig_end = rl_point;
2630 found_quote = delimiter = 0;
2634 /* This (possibly) changes rl_point. If it returns a non-zero char,
2635 we know we have an open quote. */
2636 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2638 orig_start = rl_point;
2639 rl_point = orig_end;
2641 orig_text = rl_copy_text (orig_start, orig_end);
2642 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2643 our_func, found_quote, quote_char);
2645 /* If we are matching filenames, the attempted completion function will
2646 have set rl_filename_completion_desired to a non-zero value. The basic
2647 rl_filename_completion_function does this. */
2648 matching_filenames = rl_filename_completion_desired;
2650 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2654 matches = (char **)0;
2656 orig_text = (char *)0;
2657 completion_changed_buffer = 0;
2658 RL_UNSETSTATE(RL_STATE_COMPLETING);
2662 RL_UNSETSTATE(RL_STATE_COMPLETING);
2664 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2666 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2667 code below should take care of it. */
2669 if (match_list_size > 1 && _rl_complete_show_all)
2670 display_matches (matches);
2673 /* Now we have the list of matches. Replace the text between
2674 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2675 matches[match_list_index], and add any necessary closing char. */
2677 if (matches == 0 || match_list_size == 0)
2681 matches = (char **)0;
2682 completion_changed_buffer = 0;
2686 match_list_index += count;
2687 if (match_list_index < 0)
2689 while (match_list_index < 0)
2690 match_list_index += match_list_size;
2693 match_list_index %= match_list_size;
2695 if (match_list_index == 0 && match_list_size > 1)
2698 insert_match (orig_text, orig_start, MULT_MATCH, "e_char);
2702 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
2703 append_to_match (matches[match_list_index], delimiter, quote_char,
2704 strcmp (orig_text, matches[match_list_index]));
2707 completion_changed_buffer = 1;
2712 rl_menu_complete (count, ignore)
2715 rl_compentry_func_t *our_func;
2716 int matching_filenames, found_quote;
2718 static char *orig_text;
2719 static char **matches = (char **)0;
2720 static int match_list_index = 0;
2721 static int match_list_size = 0;
2722 static int nontrivial_lcd = 0;
2723 static int full_completion = 0; /* set to 1 if menu completion should reinitialize on next call */
2724 static int orig_start, orig_end;
2725 static char quote_char;
2726 static int delimiter, cstate;
2728 /* The first time through, we generate the list of matches and set things
2729 up to insert them. */
2730 if ((rl_last_func != rl_menu_complete && rl_last_func != rl_backward_menu_complete) || full_completion)
2732 /* Clean up from previous call, if any. */
2735 _rl_free_match_list (matches);
2737 match_list_index = match_list_size = 0;
2738 matches = (char **)NULL;
2740 full_completion = 0;
2742 RL_SETSTATE(RL_STATE_COMPLETING);
2744 /* Only the completion entry function can change these. */
2745 set_completion_defaults ('%');
2747 our_func = rl_menu_completion_entry_function;
2749 our_func = rl_completion_entry_function
2750 ? rl_completion_entry_function
2751 : rl_filename_completion_function;
2753 /* We now look backwards for the start of a filename/variable word. */
2754 orig_end = rl_point;
2755 found_quote = delimiter = 0;
2759 /* This (possibly) changes rl_point. If it returns a non-zero char,
2760 we know we have an open quote. */
2761 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2763 orig_start = rl_point;
2764 rl_point = orig_end;
2766 orig_text = rl_copy_text (orig_start, orig_end);
2767 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2768 our_func, found_quote, quote_char);
2770 nontrivial_lcd = matches && strcmp (orig_text, matches[0]) != 0;
2772 /* If we are matching filenames, the attempted completion function will
2773 have set rl_filename_completion_desired to a non-zero value. The basic
2774 rl_filename_completion_function does this. */
2775 matching_filenames = rl_filename_completion_desired;
2777 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2781 matches = (char **)0;
2783 orig_text = (char *)0;
2784 completion_changed_buffer = 0;
2785 RL_UNSETSTATE(RL_STATE_COMPLETING);
2789 RL_UNSETSTATE(RL_STATE_COMPLETING);
2791 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2794 if (match_list_size == 0)
2798 matches = (char **)0;
2799 match_list_index = 0;
2800 completion_changed_buffer = 0;
2804 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2805 code below should take care of it. */
2808 insert_match (matches[0], orig_start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
2809 orig_end = orig_start + strlen (matches[0]);
2810 completion_changed_buffer = STREQ (orig_text, matches[0]) == 0;
2813 if (match_list_size > 1 && _rl_complete_show_all)
2815 display_matches (matches);
2816 /* If there are so many matches that the user has to be asked
2817 whether or not he wants to see the matches, menu completion
2819 if (rl_completion_query_items > 0 && match_list_size >= rl_completion_query_items)
2823 matches = (char **)0;
2824 full_completion = 1;
2827 else if (_rl_menu_complete_prefix_first)
2833 else if (match_list_size <= 1)
2835 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2836 full_completion = 1;
2839 else if (_rl_menu_complete_prefix_first && match_list_size > 1)
2846 /* Now we have the list of matches. Replace the text between
2847 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2848 matches[match_list_index], and add any necessary closing char. */
2850 if (matches == 0 || match_list_size == 0)
2854 matches = (char **)0;
2855 completion_changed_buffer = 0;
2859 match_list_index += count;
2860 if (match_list_index < 0)
2862 while (match_list_index < 0)
2863 match_list_index += match_list_size;
2866 match_list_index %= match_list_size;
2868 if (match_list_index == 0 && match_list_size > 1)
2871 insert_match (matches[0], orig_start, MULT_MATCH, "e_char);
2875 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
2876 append_to_match (matches[match_list_index], delimiter, quote_char,
2877 strcmp (orig_text, matches[match_list_index]));
2880 completion_changed_buffer = 1;
2885 rl_backward_menu_complete (count, key)
2888 /* Positive arguments to backward-menu-complete translate into negative
2889 arguments for menu-complete, and vice versa. */
2890 return (rl_menu_complete (-count, key));