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 /* Disabled for GDB due to the gdb.base/readline-ask.exp regression.
532 [patch] testsuite: Test readline-6.2 "ask" regression
533 http://sourceware.org/ml/gdb-patches/2011-05/msg00002.html */
535 /* For now, disable pager in callback mode, until we later convert to state
536 driven functions. Have to wait until next major version to add new
537 state definition, since it will change value of RL_STATE_DONE. */
538 #if defined (READLINE_CALLBACKS)
539 if (RL_ISSTATE (RL_STATE_CALLBACK
))
546 RL_SETSTATE(RL_STATE_MOREINPUT
);
548 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
550 if (c
== 'y' || c
== 'Y' || c
== ' ')
552 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
554 if (c
== ABORT_CHAR
|| c
< 0)
555 _rl_abort_internal ();
556 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
558 if (for_pager
&& (c
== 'q' || c
== 'Q'))
565 _rl_internal_pager (lines
)
570 fprintf (rl_outstream
, "--More--");
571 fflush (rl_outstream
);
573 _rl_erase_entire_line ();
583 path_isdir (filename
)
584 const char *filename
;
588 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
591 #if defined (VISIBLE_STATS)
592 /* Return the character which best describes FILENAME.
593 `@' for symbolic links
598 `%' for character special devices
599 `#' for block special devices */
609 /* Short-circuit a //server on cygwin, since that will always behave as
612 if (filename
[0] == '/' && filename
[1] == '/' && strchr (filename
+2, '/') == 0)
617 if (rl_filename_stat_hook
)
619 f
= savestring (filename
);
620 (*rl_filename_stat_hook
) (&f
);
626 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
627 r
= lstat (fn
, &finfo
);
629 r
= stat (fn
, &finfo
);
636 if (S_ISDIR (finfo
.st_mode
))
638 #if defined (S_ISCHR)
639 else if (S_ISCHR (finfo
.st_mode
))
642 #if defined (S_ISBLK)
643 else if (S_ISBLK (finfo
.st_mode
))
646 #if defined (S_ISLNK)
647 else if (S_ISLNK (finfo
.st_mode
))
650 #if defined (S_ISSOCK)
651 else if (S_ISSOCK (finfo
.st_mode
))
653 #endif /* S_ISSOCK */
654 #if defined (S_ISFIFO)
655 else if (S_ISFIFO (finfo
.st_mode
))
658 else if (S_ISREG (finfo
.st_mode
))
660 #if defined (_WIN32) && !defined (__CYGWIN__)
663 /* Windows doesn't do access and X_OK; check file extension instead */
664 ext
= strrchr (fn
, '.');
665 if (ext
&& (_rl_stricmp (ext
, ".exe") == 0 ||
666 _rl_stricmp (ext
, ".cmd") == 0 ||
667 _rl_stricmp (ext
, ".bat") == 0 ||
668 _rl_stricmp (ext
, ".com") == 0))
671 if (access (filename
, X_OK
) == 0)
679 #endif /* VISIBLE_STATS */
681 #if defined (COLOR_SUPPORT)
683 colored_stat_start (filename
)
684 const char *filename
;
686 _rl_set_normal_color ();
687 return (_rl_print_color_indicator (filename
));
693 _rl_prep_non_filename_text ();
694 _rl_put_indicator (&_rl_color_indicator
[C_CLR_TO_EOL
]);
698 colored_prefix_start ()
700 _rl_set_normal_color ();
701 return (_rl_print_prefix_color ());
705 colored_prefix_end ()
707 colored_stat_end (); /* for now */
711 /* Return the portion of PATHNAME that should be output when listing
712 possible completions. If we are hacking filename completion, we
713 are only interested in the basename, the portion following the
714 final slash. Otherwise, we return what we were passed. Since
715 printing empty strings is not very informative, if we're doing
716 filename completion, and the basename is the empty string, we look
717 for the previous slash and return the portion following that. If
718 there's no previous slash, we just return what we were passed. */
720 printable_part (pathname
)
725 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
728 temp
= strrchr (pathname
, '/');
729 #if defined (__MSDOS__) || defined (_WIN32)
730 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
734 if (temp
== 0 || *temp
== '\0')
736 else if (temp
[1] == 0 && temp
== pathname
)
738 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
739 Look for a previous slash and, if one is found, return the portion
740 following that slash. If there's no previous slash, just return the
741 pathname we were passed. */
742 else if (temp
[1] == '\0')
744 for (x
= temp
- 1; x
> pathname
; x
--)
747 return ((*x
== '/') ? x
+ 1 : pathname
);
753 /* Compute width of STRING when displayed on screen by print_filename */
759 #if defined (HANDLE_MULTIBYTE)
765 left
= strlen (string
) + 1;
766 memset (&ps
, 0, sizeof (mbstate_t));
772 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
779 #if defined (HANDLE_MULTIBYTE)
780 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
781 if (MB_INVALIDCH (clen
))
785 memset (&ps
, 0, sizeof (mbstate_t));
787 else if (MB_NULLWCH (clen
))
793 width
+= (w
>= 0) ? w
: 1;
805 #define ELLIPSIS_LEN 3
808 fnprint (to_print
, prefix_bytes
, real_pathname
)
809 const char *to_print
;
811 const char *real_pathname
;
815 int common_prefix_len
, print_len
;
816 #if defined (HANDLE_MULTIBYTE)
823 print_len
= strlen (to_print
);
824 end
= to_print
+ print_len
+ 1;
825 memset (&ps
, 0, sizeof (mbstate_t));
828 printed_len
= common_prefix_len
= 0;
830 /* Don't print only the ellipsis if the common prefix is one of the
831 possible completions. Only cut off prefix_bytes if we're going to be
832 printing the ellipsis, which takes precedence over coloring the
833 completion prefix (see print_filename() below). */
834 if (_rl_completion_prefix_display_length
> 0 && prefix_bytes
>= print_len
)
837 #if defined (COLOR_SUPPORT)
838 if (_rl_colored_stats
&& (prefix_bytes
== 0 || _rl_colored_completion_prefix
<= 0))
839 colored_stat_start (real_pathname
);
842 if (prefix_bytes
&& _rl_completion_prefix_display_length
> 0)
846 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
847 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
848 putc (ellipsis
, rl_outstream
);
849 printed_len
= ELLIPSIS_LEN
;
851 #if defined (COLOR_SUPPORT)
852 else if (prefix_bytes
&& _rl_colored_completion_prefix
> 0)
854 common_prefix_len
= prefix_bytes
;
856 /* XXX - print color indicator start here */
857 colored_prefix_start ();
861 s
= to_print
+ prefix_bytes
;
866 putc ('^', rl_outstream
);
867 putc (UNCTRL (*s
), rl_outstream
);
870 #if defined (HANDLE_MULTIBYTE)
871 memset (&ps
, 0, sizeof (mbstate_t));
874 else if (*s
== RUBOUT
)
876 putc ('^', rl_outstream
);
877 putc ('?', rl_outstream
);
880 #if defined (HANDLE_MULTIBYTE)
881 memset (&ps
, 0, sizeof (mbstate_t));
886 #if defined (HANDLE_MULTIBYTE)
887 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
888 if (MB_INVALIDCH (tlen
))
892 memset (&ps
, 0, sizeof (mbstate_t));
894 else if (MB_NULLWCH (tlen
))
899 width
= (w
>= 0) ? w
: 1;
901 fwrite (s
, 1, tlen
, rl_outstream
);
903 printed_len
+= width
;
905 putc (*s
, rl_outstream
);
910 if (common_prefix_len
> 0 && (s
- to_print
) >= common_prefix_len
)
912 #if defined (COLOR_SUPPORT)
913 /* printed bytes = s - to_print */
914 /* printed bytes should never be > but check for paranoia's sake */
915 colored_prefix_end ();
916 if (_rl_colored_stats
)
917 colored_stat_start (real_pathname
); /* XXX - experiment */
919 common_prefix_len
= 0;
923 #if defined (COLOR_SUPPORT)
924 /* XXX - unconditional for now */
925 if (_rl_colored_stats
)
932 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
933 are using it, check for and output a single character for `special'
934 filenames. Return the number of characters we output. */
937 print_filename (to_print
, full_pathname
, prefix_bytes
)
938 char *to_print
, *full_pathname
;
941 int printed_len
, extension_char
, slen
, tlen
;
942 char *s
, c
, *new_full_pathname
, *dn
;
945 #if defined (COLOR_SUPPORT)
946 /* Defer printing if we want to prefix with a color indicator */
947 if (_rl_colored_stats
== 0 || rl_filename_completion_desired
== 0)
949 printed_len
= fnprint (to_print
, prefix_bytes
, to_print
);
951 if (rl_filename_completion_desired
&& (
952 #if defined (VISIBLE_STATS)
955 #if defined (COLOR_SUPPORT)
958 _rl_complete_mark_directories
))
960 /* If to_print != full_pathname, to_print is the basename of the
961 path passed. In this case, we try to expand the directory
962 name before checking for the stat character. */
963 if (to_print
!= full_pathname
)
965 /* Terminate the directory name. */
969 /* If setting the last slash in full_pathname to a NUL results in
970 full_pathname being the empty string, we are trying to complete
971 files in the root directory. If we pass a null string to the
972 bash directory completion hook, for example, it will expand it
973 to the current directory. We just want the `/'. */
974 if (full_pathname
== 0 || *full_pathname
== 0)
976 else if (full_pathname
[0] != '/')
978 else if (full_pathname
[1] == 0)
979 dn
= "//"; /* restore trailing slash to `//' */
980 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
981 dn
= "/"; /* don't turn /// into // */
984 s
= tilde_expand (dn
);
985 if (rl_directory_completion_hook
)
986 (*rl_directory_completion_hook
) (&s
);
989 tlen
= strlen (to_print
);
990 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
991 strcpy (new_full_pathname
, s
);
992 if (s
[slen
- 1] == '/')
995 new_full_pathname
[slen
] = '/';
996 new_full_pathname
[slen
] = '/';
997 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
999 #if defined (VISIBLE_STATS)
1000 if (rl_visible_stats
)
1001 extension_char
= stat_char (new_full_pathname
);
1004 if (_rl_complete_mark_directories
)
1007 if (rl_directory_completion_hook
== 0 && rl_filename_stat_hook
)
1009 dn
= savestring (new_full_pathname
);
1010 (*rl_filename_stat_hook
) (&dn
);
1011 xfree (new_full_pathname
);
1012 new_full_pathname
= dn
;
1014 if (path_isdir (new_full_pathname
))
1015 extension_char
= '/';
1018 /* Move colored-stats code inside fnprint() */
1019 #if defined (COLOR_SUPPORT)
1020 if (_rl_colored_stats
)
1021 printed_len
= fnprint (to_print
, prefix_bytes
, new_full_pathname
);
1024 xfree (new_full_pathname
);
1029 s
= tilde_expand (full_pathname
);
1030 #if defined (VISIBLE_STATS)
1031 if (rl_visible_stats
)
1032 extension_char
= stat_char (s
);
1035 if (_rl_complete_mark_directories
&& path_isdir (s
))
1036 extension_char
= '/';
1038 /* Move colored-stats code inside fnprint() */
1039 #if defined (COLOR_SUPPORT)
1040 if (_rl_colored_stats
)
1041 printed_len
= fnprint (to_print
, prefix_bytes
, s
);
1048 putc (extension_char
, rl_outstream
);
1057 rl_quote_filename (s
, rtype
, qcp
)
1064 r
= (char *)xmalloc (strlen (s
) + 2);
1065 *r
= *rl_completer_quote_characters
;
1068 *qcp
= *rl_completer_quote_characters
;
1072 /* Find the bounds of the current word for completion purposes, and leave
1073 rl_point set to the end of the word. This function skips quoted
1074 substrings (characters between matched pairs of characters in
1075 rl_completer_quote_characters). First we try to find an unclosed
1076 quoted substring on which to do matching. If one is not found, we use
1077 the word break characters to find the boundaries of the current word.
1078 We call an application-specific function to decide whether or not a
1079 particular word break character is quoted; if that function returns a
1080 non-zero result, the character does not break a word. This function
1081 returns the opening quote character if we found an unclosed quoted
1082 substring, '\0' otherwise. FP, if non-null, is set to a value saying
1083 which (shell-like) quote characters we found (single quote, double
1084 quote, or backslash) anywhere in the string. DP, if non-null, is set to
1085 the value of the delimiter character that caused a word break. */
1088 _rl_find_completion_word (fp
, dp
)
1091 int scan
, end
, found_quote
, delimiter
, pass_next
, isbrk
;
1092 char quote_char
, *brkchars
;
1095 found_quote
= delimiter
= 0;
1099 if (rl_completion_word_break_hook
)
1100 brkchars
= (*rl_completion_word_break_hook
) ();
1102 brkchars
= rl_completer_word_break_characters
;
1104 if (rl_completer_quote_characters
)
1106 /* We have a list of characters which can be used in pairs to
1107 quote substrings for the completer. Try to find the start
1108 of an unclosed quoted substring. */
1109 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
1110 for (scan
= pass_next
= 0; scan
< end
; scan
= MB_NEXTCHAR (rl_line_buffer
, scan
, 1, MB_FIND_ANY
))
1118 /* Shell-like semantics for single quotes -- don't allow backslash
1119 to quote anything in single quotes, especially not the closing
1120 quote. If you don't like this, take out the check on the value
1122 if (quote_char
!= '\'' && rl_line_buffer
[scan
] == '\\')
1125 found_quote
|= RL_QF_BACKSLASH
;
1129 if (quote_char
!= '\0')
1131 /* Ignore everything until the matching close quote char. */
1132 if (rl_line_buffer
[scan
] == quote_char
)
1134 /* Found matching close. Abandon this substring. */
1139 else if (strchr (rl_completer_quote_characters
, rl_line_buffer
[scan
]))
1141 /* Found start of a quoted substring. */
1142 quote_char
= rl_line_buffer
[scan
];
1143 rl_point
= scan
+ 1;
1144 /* Shell-like quoting conventions. */
1145 if (quote_char
== '\'')
1146 found_quote
|= RL_QF_SINGLE_QUOTE
;
1147 else if (quote_char
== '"')
1148 found_quote
|= RL_QF_DOUBLE_QUOTE
;
1150 found_quote
|= RL_QF_OTHER_QUOTE
;
1155 if (rl_point
== end
&& quote_char
== '\0')
1157 /* We didn't find an unclosed quoted substring upon which to do
1158 completion, so use the word break characters to find the
1159 substring on which to complete. */
1160 while (rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_ANY
))
1162 scan
= rl_line_buffer
[rl_point
];
1164 if (strchr (brkchars
, scan
) == 0)
1167 /* Call the application-specific function to tell us whether
1168 this word break character is quoted and should be skipped. */
1169 if (rl_char_is_quoted_p
&& found_quote
&&
1170 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
))
1173 /* Convoluted code, but it avoids an n^2 algorithm with calls
1174 to char_is_quoted. */
1179 /* If we are at an unquoted word break, then advance past it. */
1180 scan
= rl_line_buffer
[rl_point
];
1182 /* If there is an application-specific function to say whether or not
1183 a character is quoted and we found a quote character, let that
1184 function decide whether or not a character is a word break, even
1185 if it is found in rl_completer_word_break_characters. Don't bother
1186 if we're at the end of the line, though. */
1189 if (rl_char_is_quoted_p
)
1190 isbrk
= (found_quote
== 0 ||
1191 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
) == 0) &&
1192 strchr (brkchars
, scan
) != 0;
1194 isbrk
= strchr (brkchars
, scan
) != 0;
1198 /* If the character that caused the word break was a quoting
1199 character, then remember it as the delimiter. */
1200 if (rl_basic_quote_characters
&&
1201 strchr (rl_basic_quote_characters
, scan
) &&
1202 (end
- rl_point
) > 1)
1205 /* If the character isn't needed to determine something special
1206 about what kind of completion to perform, then advance past it. */
1207 if (rl_special_prefixes
== 0 || strchr (rl_special_prefixes
, scan
) == 0)
1217 return (quote_char
);
1221 gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
)
1224 rl_compentry_func_t
*our_func
;
1225 int found_quote
, quote_char
;
1229 rl_completion_found_quote
= found_quote
;
1230 rl_completion_quote_character
= quote_char
;
1232 /* If the user wants to TRY to complete, but then wants to give
1233 up and use the default completion function, they set the
1234 variable rl_attempted_completion_function. */
1235 if (rl_attempted_completion_function
)
1237 matches
= (*rl_attempted_completion_function
) (text
, start
, end
);
1238 if (RL_SIG_RECEIVED())
1240 _rl_free_match_list (matches
);
1242 RL_CHECK_SIGNALS ();
1245 if (matches
|| rl_attempted_completion_over
)
1247 rl_attempted_completion_over
= 0;
1252 /* XXX -- filename dequoting moved into rl_filename_completion_function */
1254 /* rl_completion_matches will check for signals as well to avoid a long
1255 delay while reading a directory. */
1256 matches
= rl_completion_matches (text
, our_func
);
1257 if (RL_SIG_RECEIVED())
1259 _rl_free_match_list (matches
);
1261 RL_CHECK_SIGNALS ();
1266 /* Filter out duplicates in MATCHES. This frees up the strings in
1269 remove_duplicate_matches (matches
)
1272 char *lowest_common
;
1277 /* Sort the items. */
1278 for (i
= 0; matches
[i
]; i
++)
1281 /* Sort the array without matches[0], since we need it to
1282 stay in place no matter what. */
1283 if (i
&& rl_sort_completion_matches
)
1284 qsort (matches
+1, i
-1, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1286 /* Remember the lowest common denominator for it may be unique. */
1287 lowest_common
= savestring (matches
[0]);
1289 for (i
= newlen
= 0; matches
[i
+ 1]; i
++)
1291 if (strcmp (matches
[i
], matches
[i
+ 1]) == 0)
1294 matches
[i
] = (char *)&dead_slot
;
1300 /* We have marked all the dead slots with (char *)&dead_slot.
1301 Copy all the non-dead entries into a new array. */
1302 temp_array
= (char **)xmalloc ((3 + newlen
) * sizeof (char *));
1303 for (i
= j
= 1; matches
[i
]; i
++)
1305 if (matches
[i
] != (char *)&dead_slot
)
1306 temp_array
[j
++] = matches
[i
];
1308 temp_array
[j
] = (char *)NULL
;
1310 if (matches
[0] != (char *)&dead_slot
)
1313 /* Place the lowest common denominator back in [0]. */
1314 temp_array
[0] = lowest_common
;
1316 /* If there is one string left, and it is identical to the
1317 lowest common denominator, then the LCD is the string to
1319 if (j
== 2 && strcmp (temp_array
[0], temp_array
[1]) == 0)
1321 xfree (temp_array
[1]);
1322 temp_array
[1] = (char *)NULL
;
1324 return (temp_array
);
1327 /* Find the common prefix of the list of matches, and put it into
1330 compute_lcd_of_matches (match_list
, matches
, text
)
1335 register int i
, c1
, c2
, si
;
1336 int low
; /* Count of max-matched characters. */
1338 char *dtext
; /* dequoted TEXT, if needed */
1339 #if defined (HANDLE_MULTIBYTE)
1346 /* If only one match, just use that. Otherwise, compare each
1347 member of the list with the next, finding out where they
1351 match_list
[0] = match_list
[1];
1352 match_list
[1] = (char *)NULL
;
1356 for (i
= 1, low
= 100000; i
< matches
; i
++)
1358 #if defined (HANDLE_MULTIBYTE)
1359 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1361 memset (&ps1
, 0, sizeof (mbstate_t));
1362 memset (&ps2
, 0, sizeof (mbstate_t));
1365 if (_rl_completion_case_fold
)
1368 (c1
= _rl_to_lower(match_list
[i
][si
])) &&
1369 (c2
= _rl_to_lower(match_list
[i
+ 1][si
]));
1371 #if defined (HANDLE_MULTIBYTE)
1372 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1374 v1
= mbrtowc(&wc1
, match_list
[i
]+si
, strlen (match_list
[i
]+si
), &ps1
);
1375 v2
= mbrtowc (&wc2
, match_list
[i
+1]+si
, strlen (match_list
[i
+1]+si
), &ps2
);
1376 if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
1378 if (c1
!= c2
) /* do byte comparison */
1382 wc1
= towlower (wc1
);
1383 wc2
= towlower (wc2
);
1397 (c1
= match_list
[i
][si
]) &&
1398 (c2
= match_list
[i
+ 1][si
]);
1400 #if defined (HANDLE_MULTIBYTE)
1401 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1405 if (!_rl_compare_chars (match_list
[i
], si
, &ps1
, match_list
[i
+1], si
, &ps2
))
1407 else if ((v
= _rl_get_char_len (&match_list
[i
][si
], &ps_back
)) > 1)
1420 /* If there were multiple matches, but none matched up to even the
1421 first character, and the user typed something, use that as the
1422 value of matches[0]. */
1423 if (low
== 0 && text
&& *text
)
1425 match_list
[0] = (char *)xmalloc (strlen (text
) + 1);
1426 strcpy (match_list
[0], text
);
1430 match_list
[0] = (char *)xmalloc (low
+ 1);
1432 /* XXX - this might need changes in the presence of multibyte chars */
1434 /* If we are ignoring case, try to preserve the case of the string
1435 the user typed in the face of multiple matches differing in case. */
1436 if (_rl_completion_case_fold
)
1438 /* We're making an assumption here:
1439 IF we're completing filenames AND
1440 the application has defined a filename dequoting function AND
1441 we found a quote character AND
1442 the application has requested filename quoting
1444 we assume that TEXT was dequoted before checking against
1445 the file system and needs to be dequoted here before we
1446 check against the list of matches
1448 dtext
= (char *)NULL
;
1449 if (rl_filename_completion_desired
&&
1450 rl_filename_dequoting_function
&&
1451 rl_completion_found_quote
&&
1452 rl_filename_quoting_desired
)
1454 dtext
= (*rl_filename_dequoting_function
) ((char *)text
, rl_completion_quote_character
);
1458 /* sort the list to get consistent answers. */
1459 qsort (match_list
+1, matches
, sizeof(char *), (QSFUNC
*)_rl_qsort_string_compare
);
1462 lx
= (si
<= low
) ? si
: low
; /* check shorter of text and matches */
1463 /* Try to preserve the case of what the user typed in the presence of
1464 multiple matches: check each match for something that matches
1465 what the user typed taking case into account; use it up to common
1466 length of matches if one is found. If not, just use first match. */
1467 for (i
= 1; i
<= matches
; i
++)
1468 if (strncmp (match_list
[i
], text
, lx
) == 0)
1470 strncpy (match_list
[0], match_list
[i
], low
);
1473 /* no casematch, use first entry */
1475 strncpy (match_list
[0], match_list
[1], low
);
1480 strncpy (match_list
[0], match_list
[1], low
);
1482 match_list
[0][low
] = '\0';
1489 postprocess_matches (matchesp
, matching_filenames
)
1491 int matching_filenames
;
1493 char *t
, **matches
, **temp_matches
;
1496 matches
= *matchesp
;
1501 /* It seems to me that in all the cases we handle we would like
1502 to ignore duplicate possibilities. Scan for the text to
1503 insert being identical to the other completions. */
1504 if (rl_ignore_completion_duplicates
)
1506 temp_matches
= remove_duplicate_matches (matches
);
1508 matches
= temp_matches
;
1511 /* If we are matching filenames, then here is our chance to
1512 do clever processing by re-examining the list. Call the
1513 ignore function with the array as a parameter. It can
1514 munge the array, deleting matches as it desires. */
1515 if (rl_ignore_some_completions_function
&& matching_filenames
)
1517 for (nmatch
= 1; matches
[nmatch
]; nmatch
++)
1519 (void)(*rl_ignore_some_completions_function
) (matches
);
1520 if (matches
== 0 || matches
[0] == 0)
1523 *matchesp
= (char **)0;
1528 /* If we removed some matches, recompute the common prefix. */
1529 for (i
= 1; matches
[i
]; i
++)
1531 if (i
> 1 && i
< nmatch
)
1534 compute_lcd_of_matches (matches
, i
- 1, t
);
1540 *matchesp
= matches
;
1545 complete_get_screenwidth ()
1550 cols
= _rl_completion_columns
;
1551 if (cols
>= 0 && cols
<= _rl_screenwidth
)
1553 envcols
= getenv ("COLUMNS");
1554 if (envcols
&& *envcols
)
1555 cols
= atoi (envcols
);
1556 if (cols
>= 0 && cols
<= _rl_screenwidth
)
1558 return _rl_screenwidth
;
1561 /* A convenience function for displaying a list of strings in
1562 columnar format on readline's output stream. MATCHES is the list
1563 of strings, in argv format, LEN is the number of strings in MATCHES,
1564 and MAX is the length of the longest string in MATCHES. */
1566 rl_display_match_list (matches
, len
, max
)
1570 int count
, limit
, printed_len
, lines
, cols
;
1571 int i
, j
, k
, l
, common_length
, sind
;
1574 /* Find the length of the prefix common to all items: length as displayed
1575 characters (common_length) and as a byte index into the matches (sind) */
1576 common_length
= sind
= 0;
1577 if (_rl_completion_prefix_display_length
> 0)
1579 t
= printable_part (matches
[0]);
1580 /* check again in case of /usr/src/ */
1581 temp
= rl_filename_completion_desired
? strrchr (t
, '/') : 0;
1582 common_length
= temp
? fnwidth (temp
) : fnwidth (t
);
1583 sind
= temp
? strlen (temp
) : strlen (t
);
1584 if (common_length
> max
|| sind
> max
)
1585 common_length
= sind
= 0;
1587 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
1588 max
-= common_length
- ELLIPSIS_LEN
;
1590 common_length
= sind
= 0;
1592 #if defined (COLOR_SUPPORT)
1593 else if (_rl_colored_completion_prefix
> 0)
1595 t
= printable_part (matches
[0]);
1596 temp
= rl_filename_completion_desired
? strrchr (t
, '/') : 0;
1597 common_length
= temp
? fnwidth (temp
) : fnwidth (t
);
1598 sind
= temp
? RL_STRLEN (temp
+1) : RL_STRLEN (t
); /* want portion after final slash */
1599 if (common_length
> max
|| sind
> max
)
1600 common_length
= sind
= 0;
1604 /* How many items of MAX length can we fit in the screen window? */
1605 cols
= complete_get_screenwidth ();
1608 if (limit
!= 1 && (limit
* max
== cols
))
1611 /* If cols == 0, limit will end up -1 */
1612 if (cols
< _rl_screenwidth
&& limit
< 0)
1615 /* Avoid a possible floating exception. If max > cols,
1616 limit will be 0 and a divide-by-zero fault will result. */
1620 /* How many iterations of the printing loop? */
1621 count
= (len
+ (limit
- 1)) / limit
;
1623 /* Watch out for special case. If LEN is less than LIMIT, then
1624 just do the inner printing loop.
1625 0 < len <= limit implies count = 1. */
1627 /* Sort the items if they are not already sorted. */
1628 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
1629 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1634 if (_rl_print_completions_horizontally
== 0)
1636 /* Print the sorted items, up-and-down alphabetically, like ls. */
1637 for (i
= 1; i
<= count
; i
++)
1639 for (j
= 0, l
= i
; j
< limit
; j
++)
1641 if (l
> len
|| matches
[l
] == 0)
1645 temp
= printable_part (matches
[l
]);
1646 printed_len
= print_filename (temp
, matches
[l
], sind
);
1650 if (max
<= printed_len
)
1651 putc (' ', rl_outstream
);
1653 for (k
= 0; k
< max
- printed_len
; k
++)
1654 putc (' ', rl_outstream
);
1660 #if defined (SIGWINCH)
1661 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1663 if (RL_SIG_RECEIVED ())
1667 if (_rl_page_completions
&& lines
>= (_rl_screenheight
- 1) && i
< count
)
1669 lines
= _rl_internal_pager (lines
);
1677 /* Print the sorted items, across alphabetically, like ls -x. */
1678 for (i
= 1; matches
[i
]; i
++)
1680 temp
= printable_part (matches
[i
]);
1681 printed_len
= print_filename (temp
, matches
[i
], sind
);
1682 /* Have we reached the end of this line? */
1683 #if defined (SIGWINCH)
1684 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1686 if (RL_SIG_RECEIVED ())
1691 if (limit
== 1 || (i
&& (limit
> 1) && (i
% limit
) == 0))
1695 if (_rl_page_completions
&& lines
>= _rl_screenheight
- 1)
1697 lines
= _rl_internal_pager (lines
);
1702 else if (max
<= printed_len
)
1703 putc (' ', rl_outstream
);
1705 for (k
= 0; k
< max
- printed_len
; k
++)
1706 putc (' ', rl_outstream
);
1713 /* Display MATCHES, a list of matching filenames in argv format. This
1714 handles the simple case -- a single match -- first. If there is more
1715 than one match, we compute the number of strings in the list and the
1716 length of the longest string, which will be needed by the display
1717 function. If the application wants to handle displaying the list of
1718 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1719 address of a function, and we just call it. If we're handling the
1720 display ourselves, we just call rl_display_match_list. We also check
1721 that the list of matches doesn't exceed the user-settable threshold,
1722 and ask the user if he wants to see the list if there are more matches
1723 than RL_COMPLETION_QUERY_ITEMS. */
1725 display_matches (matches
)
1731 /* Move to the last visible line of a possibly-multiple-line command. */
1732 _rl_move_vert (_rl_vis_botlin
);
1734 /* Handle simple case first. What if there is only one answer? */
1735 if (matches
[1] == 0)
1737 temp
= printable_part (matches
[0]);
1739 print_filename (temp
, matches
[0], 0);
1742 rl_forced_update_display ();
1743 rl_display_fixed
= 1;
1748 /* There is more than one answer. Find out how many there are,
1749 and find the maximum printed length of a single entry. */
1750 for (max
= 0, i
= 1; matches
[i
]; i
++)
1752 temp
= printable_part (matches
[i
]);
1753 len
= fnwidth (temp
);
1761 /* If the caller has defined a display hook, then call that now. */
1762 if (rl_completion_display_matches_hook
)
1764 (*rl_completion_display_matches_hook
) (matches
, len
, max
);
1768 /* If there are many items, then ask the user if she really wants to
1770 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
1773 fprintf (rl_outstream
, "Display all %d possibilities? (y or n)", len
);
1774 fflush (rl_outstream
);
1775 if ((completion_y_or_n
= get_y_or_n (0)) == 0)
1779 rl_forced_update_display ();
1780 rl_display_fixed
= 1;
1786 rl_display_match_list (matches
, len
, max
);
1788 rl_forced_update_display ();
1789 rl_display_fixed
= 1;
1793 make_quoted_replacement (match
, mtype
, qc
)
1796 char *qc
; /* Pointer to quoting character, if any */
1798 int should_quote
, do_replace
;
1801 /* If we are doing completion on quoted substrings, and any matches
1802 contain any of the completer_word_break_characters, then auto-
1803 matically prepend the substring with a quote character (just pick
1804 the first one from the list of such) if it does not already begin
1805 with a quote string. FIXME: Need to remove any such automatically
1806 inserted quote character when it no longer is necessary, such as
1807 if we change the string we are completing on and the new set of
1808 matches don't require a quoted substring. */
1809 replacement
= match
;
1811 should_quote
= match
&& rl_completer_quote_characters
&&
1812 rl_filename_completion_desired
&&
1813 rl_filename_quoting_desired
;
1816 should_quote
= should_quote
&& (!qc
|| !*qc
||
1817 (rl_completer_quote_characters
&& strchr (rl_completer_quote_characters
, *qc
)));
1821 /* If there is a single match, see if we need to quote it.
1822 This also checks whether the common prefix of several
1823 matches needs to be quoted. */
1824 should_quote
= rl_filename_quote_characters
1825 ? (_rl_strpbrk (match
, rl_filename_quote_characters
) != 0)
1828 do_replace
= should_quote
? mtype
: NO_MATCH
;
1829 /* Quote the replacement, since we found an embedded
1830 word break character in a potential match. */
1831 if (do_replace
!= NO_MATCH
&& rl_filename_quoting_function
)
1832 replacement
= (*rl_filename_quoting_function
) (match
, do_replace
, qc
);
1834 return (replacement
);
1838 insert_match (match
, start
, mtype
, qc
)
1843 char *replacement
, *r
;
1847 oqc
= qc
? *qc
: '\0';
1848 replacement
= make_quoted_replacement (match
, mtype
, qc
);
1850 /* Now insert the match. */
1853 rlen
= strlen (replacement
);
1854 /* Don't double an opening quote character. */
1855 if (qc
&& *qc
&& start
&& rl_line_buffer
[start
- 1] == *qc
&&
1856 replacement
[0] == *qc
)
1858 /* If make_quoted_replacement changed the quoting character, remove
1859 the opening quote and insert the (fully-quoted) replacement. */
1860 else if (qc
&& (*qc
!= oqc
) && start
&& rl_line_buffer
[start
- 1] == oqc
&&
1861 replacement
[0] != oqc
)
1864 /* Don't double a closing quote character */
1865 if (qc
&& *qc
&& end
&& rl_line_buffer
[rl_point
] == *qc
&& replacement
[rlen
- 1] == *qc
)
1867 if (_rl_skip_completed_text
)
1870 while (start
< rl_end
&& *r
&& rl_line_buffer
[start
] == *r
)
1875 if (start
<= end
|| *r
)
1876 _rl_replace_text (r
, start
, end
);
1877 rl_point
= start
+ strlen (r
);
1880 _rl_replace_text (replacement
, start
, end
);
1881 if (replacement
!= match
)
1882 xfree (replacement
);
1886 /* Append any necessary closing quote and a separator character to the
1887 just-inserted match. If the user has specified that directories
1888 should be marked by a trailing `/', append one of those instead. The
1889 default trailing character is a space. Returns the number of characters
1890 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1891 has them) and don't add a suffix for a symlink to a directory. A
1892 nontrivial match is one that actually adds to the word being completed.
1893 The variable rl_completion_mark_symlink_dirs controls this behavior
1894 (it's initially set to the what the user has chosen, indicated by the
1895 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1896 application's completion function). */
1898 append_to_match (text
, delimiter
, quote_char
, nontrivial_match
)
1900 int delimiter
, quote_char
, nontrivial_match
;
1902 char temp_string
[4], *filename
, *fn
;
1903 int temp_string_index
, s
;
1906 temp_string_index
= 0;
1907 if (quote_char
&& rl_point
&& rl_completion_suppress_quote
== 0 &&
1908 rl_line_buffer
[rl_point
- 1] != quote_char
)
1909 temp_string
[temp_string_index
++] = quote_char
;
1912 temp_string
[temp_string_index
++] = delimiter
;
1913 else if (rl_completion_suppress_append
== 0 && rl_completion_append_character
)
1914 temp_string
[temp_string_index
++] = rl_completion_append_character
;
1916 temp_string
[temp_string_index
++] = '\0';
1918 if (rl_filename_completion_desired
)
1920 filename
= tilde_expand (text
);
1921 if (rl_filename_stat_hook
)
1923 fn
= savestring (filename
);
1924 (*rl_filename_stat_hook
) (&fn
);
1928 s
= (nontrivial_match
&& rl_completion_mark_symlink_dirs
== 0)
1929 ? LSTAT (filename
, &finfo
)
1930 : stat (filename
, &finfo
);
1931 if (s
== 0 && S_ISDIR (finfo
.st_mode
))
1933 if (_rl_complete_mark_directories
/* && rl_completion_suppress_append == 0 */)
1935 /* This is clumsy. Avoid putting in a double slash if point
1936 is at the end of the line and the previous character is a
1938 if (rl_point
&& rl_line_buffer
[rl_point
] == '\0' && rl_line_buffer
[rl_point
- 1] == '/')
1940 else if (rl_line_buffer
[rl_point
] != '/')
1941 rl_insert_text ("/");
1945 /* Don't add anything if the filename is a symlink and resolves to a
1947 else if (s
== 0 && S_ISLNK (finfo
.st_mode
) && path_isdir (filename
))
1952 if (rl_point
== rl_end
&& temp_string_index
)
1953 rl_insert_text (temp_string
);
1959 if (rl_point
== rl_end
&& temp_string_index
)
1960 rl_insert_text (temp_string
);
1963 return (temp_string_index
);
1967 insert_all_matches (matches
, point
, qc
)
1975 rl_begin_undo_group ();
1976 /* remove any opening quote character; make_quoted_replacement will add
1978 if (qc
&& *qc
&& point
&& rl_line_buffer
[point
- 1] == *qc
)
1980 rl_delete_text (point
, rl_point
);
1985 for (i
= 1; matches
[i
]; i
++)
1987 rp
= make_quoted_replacement (matches
[i
], SINGLE_MATCH
, qc
);
1988 rl_insert_text (rp
);
1989 rl_insert_text (" ");
1990 if (rp
!= matches
[i
])
1996 rp
= make_quoted_replacement (matches
[0], SINGLE_MATCH
, qc
);
1997 rl_insert_text (rp
);
1998 rl_insert_text (" ");
1999 if (rp
!= matches
[0])
2002 rl_end_undo_group ();
2006 _rl_free_match_list (matches
)
2014 for (i
= 0; matches
[i
]; i
++)
2019 /* Complete the word at or before point.
2020 WHAT_TO_DO says what to do with the completion.
2021 `?' means list the possible completions.
2022 TAB means do standard completion.
2023 `*' means insert all of the possible completions.
2024 `!' means to do standard completion, and list all possible completions if
2025 there is more than one.
2026 `@' means to do standard completion, and list all possible completions if
2027 there is more than one and partial completion is not possible. */
2029 rl_complete_internal (what_to_do
)
2033 rl_compentry_func_t
*our_func
;
2034 int start
, end
, delimiter
, found_quote
, i
, nontrivial_lcd
;
2035 char *text
, *saved_line_buffer
;
2041 RL_SETSTATE(RL_STATE_COMPLETING
);
2043 set_completion_defaults (what_to_do
);
2045 saved_line_buffer
= rl_line_buffer
? savestring (rl_line_buffer
) : (char *)NULL
;
2046 our_func
= rl_completion_entry_function
2047 ? rl_completion_entry_function
2048 : rl_filename_completion_function
;
2049 /* We now look backwards for the start of a filename/variable word. */
2051 found_quote
= delimiter
= 0;
2055 /* This (possibly) changes rl_point. If it returns a non-zero char,
2056 we know we have an open quote. */
2057 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2062 text
= rl_copy_text (start
, end
);
2063 matches
= gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
);
2064 /* nontrivial_lcd is set if the common prefix adds something to the word
2066 nontrivial_lcd
= matches
&& strcmp (text
, matches
[0]) != 0;
2067 if (what_to_do
== '!' || what_to_do
== '@')
2068 tlen
= strlen (text
);
2074 FREE (saved_line_buffer
);
2075 completion_changed_buffer
= 0;
2076 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2077 _rl_reset_completion_state ();
2081 /* If we are matching filenames, the attempted completion function will
2082 have set rl_filename_completion_desired to a non-zero value. The basic
2083 rl_filename_completion_function does this. */
2084 i
= rl_filename_completion_desired
;
2086 if (postprocess_matches (&matches
, i
) == 0)
2089 FREE (saved_line_buffer
);
2090 completion_changed_buffer
= 0;
2091 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2092 _rl_reset_completion_state ();
2101 /* Insert the first match with proper quoting. */
2102 if (what_to_do
== TAB
)
2105 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2107 else if (*matches
[0] && matches
[1] == 0)
2108 /* should we perform the check only if there are multiple matches? */
2109 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2110 else if (*matches
[0]) /* what_to_do != TAB && multiple matches */
2112 mlen
= *matches
[0] ? strlen (matches
[0]) : 0;
2114 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2117 /* If there are more matches, ring the bell to indicate.
2118 If we are in vi mode, Posix.2 says to not ring the bell.
2119 If the `show-all-if-ambiguous' variable is set, display
2120 all the matches immediately. Otherwise, if this was the
2121 only match, and we are hacking files, check the file to
2122 see if it was a directory. If so, and the `mark-directories'
2123 variable is set, add a '/' to the name. If not, and we
2124 are at the end of the line, then add a space. */
2127 if (what_to_do
== '!')
2129 display_matches (matches
);
2132 else if (what_to_do
== '@')
2134 if (nontrivial_lcd
== 0)
2135 display_matches (matches
);
2138 else if (rl_editing_mode
!= vi_mode
)
2139 rl_ding (); /* There are other matches remaining. */
2142 append_to_match (matches
[0], delimiter
, quote_char
, nontrivial_lcd
);
2147 insert_all_matches (matches
, start
, "e_char
);
2151 if (rl_completion_display_matches_hook
== 0)
2153 _rl_sigcleanup
= _rl_complete_sigcleanup
;
2154 _rl_sigcleanarg
= matches
;
2155 _rl_complete_display_matches_interrupt
= 0;
2157 display_matches (matches
);
2158 if (_rl_complete_display_matches_interrupt
)
2160 matches
= 0; /* already freed by rl_complete_sigcleanup */
2161 _rl_complete_display_matches_interrupt
= 0;
2162 if (rl_signal_event_hook
)
2163 (*rl_signal_event_hook
) (); /* XXX */
2166 _rl_sigcleanarg
= 0;
2170 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do
);
2172 FREE (saved_line_buffer
);
2173 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2174 _rl_free_match_list (matches
);
2175 _rl_reset_completion_state ();
2179 _rl_free_match_list (matches
);
2181 /* Check to see if the line has changed through all of this manipulation. */
2182 if (saved_line_buffer
)
2184 completion_changed_buffer
= strcmp (rl_line_buffer
, saved_line_buffer
) != 0;
2185 xfree (saved_line_buffer
);
2188 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2189 _rl_reset_completion_state ();
2191 RL_CHECK_SIGNALS ();
2195 /***************************************************************/
2197 /* Application-callable completion match generator functions */
2199 /***************************************************************/
2201 /* Return an array of (char *) which is a list of completions for TEXT.
2202 If there are no completions, return a NULL pointer.
2203 The first entry in the returned array is the substitution for TEXT.
2204 The remaining entries are the possible completions.
2205 The array is terminated with a NULL pointer.
2207 ENTRY_FUNCTION is a function of two args, and returns a (char *).
2208 The first argument is TEXT.
2209 The second is a state argument; it should be zero on the first call, and
2210 non-zero on subsequent calls. It returns a NULL pointer to the caller
2211 when there are no more matches.
2214 rl_completion_matches (text
, entry_function
)
2216 rl_compentry_func_t
*entry_function
;
2220 /* Number of slots in match_list. */
2221 int match_list_size
;
2223 /* The list of matches. */
2226 /* Number of matches actually found. */
2229 /* Temporary string binder. */
2233 match_list_size
= 10;
2234 match_list
= (char **)xmalloc ((match_list_size
+ 1) * sizeof (char *));
2235 match_list
[1] = (char *)NULL
;
2237 while (string
= (*entry_function
) (text
, matches
))
2239 if (RL_SIG_RECEIVED ())
2241 /* Start at 1 because we don't set matches[0] in this function.
2242 Only free the list members if we're building match list from
2243 rl_filename_completion_function, since we know that doesn't
2244 free the strings it returns. */
2245 if (entry_function
== rl_filename_completion_function
)
2247 for (i
= 1; match_list
[i
]; i
++)
2248 xfree (match_list
[i
]);
2252 match_list_size
= 0;
2254 RL_CHECK_SIGNALS ();
2257 if (matches
+ 1 >= match_list_size
)
2258 match_list
= (char **)xrealloc
2259 (match_list
, ((match_list_size
+= 10) + 1) * sizeof (char *));
2261 if (match_list
== 0)
2262 return (match_list
);
2264 match_list
[++matches
] = string
;
2265 match_list
[matches
+ 1] = (char *)NULL
;
2268 /* If there were any matches, then look through them finding out the
2269 lowest common denominator. That then becomes match_list[0]. */
2271 compute_lcd_of_matches (match_list
, matches
, text
);
2272 else /* There were no matches. */
2275 match_list
= (char **)NULL
;
2277 return (match_list
);
2280 /* A completion function for usernames.
2281 TEXT contains a partial username preceded by a random
2282 character (usually `~'). */
2284 rl_username_completion_function (text
, state
)
2288 #if defined (__WIN32__) || defined (__OPENNT)
2289 return (char *)NULL
;
2290 #else /* !__WIN32__ && !__OPENNT) */
2291 static char *username
= (char *)NULL
;
2292 static struct passwd
*entry
;
2293 static int namelen
, first_char
, first_char_loc
;
2301 first_char_loc
= first_char
== '~';
2303 username
= savestring (&text
[first_char_loc
]);
2304 namelen
= strlen (username
);
2305 #if defined (HAVE_GETPWENT)
2310 #if defined (HAVE_GETPWENT)
2311 while (entry
= getpwent ())
2313 /* Null usernames should result in all users as possible completions. */
2314 if (namelen
== 0 || (STREQN (username
, entry
->pw_name
, namelen
)))
2321 #if defined (HAVE_GETPWENT)
2324 return ((char *)NULL
);
2328 value
= (char *)xmalloc (2 + strlen (entry
->pw_name
));
2332 strcpy (value
+ first_char_loc
, entry
->pw_name
);
2334 if (first_char
== '~')
2335 rl_filename_completion_desired
= 1;
2339 #endif /* !__WIN32__ && !__OPENNT */
2342 /* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME
2343 (FILENAME_LEN). If _rl_completion_case_fold is set, compare without
2344 regard to the alphabetic case of characters. If
2345 _rl_completion_case_map is set, make `-' and `_' equivalent. CONVFN is
2346 the possibly-converted directory entry; FILENAME is what the user typed. */
2348 complete_fncmp (convfn
, convlen
, filename
, filename_len
)
2351 const char *filename
;
2354 register char *s1
, *s2
;
2356 #if defined (HANDLE_MULTIBYTE)
2362 #if defined (HANDLE_MULTIBYTE)
2363 memset (&ps1
, 0, sizeof (mbstate_t));
2364 memset (&ps2
, 0, sizeof (mbstate_t));
2367 if (filename_len
== 0)
2369 if (convlen
< filename_len
)
2373 s1
= (char *)convfn
;
2374 s2
= (char *)filename
;
2376 /* Otherwise, if these match up to the length of filename, then
2378 if (_rl_completion_case_fold
&& _rl_completion_case_map
)
2380 /* Case-insensitive comparison treating _ and - as equivalent */
2381 #if defined (HANDLE_MULTIBYTE)
2382 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2386 v1
= mbrtowc (&wc1
, s1
, convlen
, &ps1
);
2387 v2
= mbrtowc (&wc2
, s2
, filename_len
, &ps2
);
2388 if (v1
== 0 && v2
== 0)
2390 else if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
2392 if (*s1
!= *s2
) /* do byte comparison */
2394 else if ((*s1
== '-' || *s1
== '_') && (*s2
== '-' || *s2
== '_'))
2399 wc1
= towlower (wc1
);
2400 wc2
= towlower (wc2
);
2404 if ((wc1
== L
'-' || wc1
== L
'_') && (wc2
== L
'-' || wc2
== L
'_'))
2416 d
= _rl_to_lower (*s1
) - _rl_to_lower (*s2
);
2417 /* *s1 == [-_] && *s2 == [-_] */
2418 if ((*s1
== '-' || *s1
== '_') && (*s2
== '-' || *s2
== '_'))
2422 s1
++; s2
++; /* already checked convlen >= filename_len */
2429 else if (_rl_completion_case_fold
)
2431 #if defined (HANDLE_MULTIBYTE)
2432 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2436 v1
= mbrtowc (&wc1
, s1
, convlen
, &ps1
);
2437 v2
= mbrtowc (&wc2
, s2
, filename_len
, &ps2
);
2438 if (v1
== 0 && v2
== 0)
2440 else if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
2442 if (*s1
!= *s2
) /* do byte comparison */
2447 wc1
= towlower (wc1
);
2448 wc2
= towlower (wc2
);
2460 if ((_rl_to_lower (convfn
[0]) == _rl_to_lower (filename
[0])) &&
2461 (convlen
>= filename_len
) &&
2462 (_rl_strnicmp (filename
, convfn
, filename_len
) == 0))
2467 if ((convfn
[0] == filename
[0]) &&
2468 (convlen
>= filename_len
) &&
2469 (strncmp (filename
, convfn
, filename_len
) == 0))
2475 /* Okay, now we write the entry_function for filename completion. In the
2476 general case. Note that completion in the shell is a little different
2477 because of all the pathnames that must be followed when looking up the
2478 completion for a command. */
2480 rl_filename_completion_function (text
, state
)
2484 static DIR *directory
= (DIR *)NULL
;
2485 static char *filename
= (char *)NULL
;
2486 static char *dirname
= (char *)NULL
;
2487 static char *users_dirname
= (char *)NULL
;
2488 static int filename_len
;
2489 char *temp
, *dentry
, *convfn
;
2490 int dirlen
, dentlen
, convlen
;
2492 struct dirent
*entry
;
2494 /* If we don't have any state, then do some initialization. */
2497 /* If we were interrupted before closing the directory or reading
2498 all of its contents, close it. */
2501 closedir (directory
);
2502 directory
= (DIR *)NULL
;
2506 FREE (users_dirname
);
2508 filename
= savestring (text
);
2511 dirname
= savestring (text
);
2513 temp
= strrchr (dirname
, '/');
2515 #if defined (__MSDOS__) || defined (_WIN32)
2516 /* special hack for //X/... */
2517 if (dirname
[0] == '/' && dirname
[1] == '/' && ISALPHA ((unsigned char)dirname
[2]) && dirname
[3] == '/')
2518 temp
= strrchr (dirname
+ 3, '/');
2523 strcpy (filename
, ++temp
);
2526 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN__))
2527 /* searches from current directory on the drive */
2528 else if (ISALPHA ((unsigned char)dirname
[0]) && dirname
[1] == ':')
2530 strcpy (filename
, dirname
+ 2);
2540 /* We aren't done yet. We also support the "~user" syntax. */
2542 /* Save the version of the directory that the user typed, dequoting
2544 if (rl_completion_found_quote
&& rl_filename_dequoting_function
)
2545 users_dirname
= (*rl_filename_dequoting_function
) (dirname
, rl_completion_quote_character
);
2547 users_dirname
= savestring (dirname
);
2550 if (*dirname
== '~')
2552 temp
= tilde_expand (dirname
);
2558 /* We have saved the possibly-dequoted version of the directory name
2559 the user typed. Now transform the directory name we're going to
2560 pass to opendir(2). The directory rewrite hook modifies only the
2561 directory name; the directory completion hook modifies both the
2562 directory name passed to opendir(2) and the version the user
2563 typed. Both the directory completion and rewrite hooks should perform
2564 any necessary dequoting. The hook functions return 1 if they modify
2565 the directory name argument. If either hook returns 0, it should
2566 not modify the directory name pointer passed as an argument. */
2567 if (rl_directory_rewrite_hook
)
2568 (*rl_directory_rewrite_hook
) (&dirname
);
2569 else if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&dirname
))
2571 xfree (users_dirname
);
2572 users_dirname
= savestring (dirname
);
2574 else if (tilde_dirname
== 0 && rl_completion_found_quote
&& rl_filename_dequoting_function
)
2576 /* delete single and double quotes */
2578 dirname
= savestring (users_dirname
);
2580 directory
= opendir (dirname
);
2582 /* Now dequote a non-null filename. FILENAME will not be NULL, but may
2584 if (*filename
&& rl_completion_found_quote
&& rl_filename_dequoting_function
)
2586 /* delete single and double quotes */
2587 temp
= (*rl_filename_dequoting_function
) (filename
, rl_completion_quote_character
);
2591 filename_len
= strlen (filename
);
2593 rl_filename_completion_desired
= 1;
2596 /* At this point we should entertain the possibility of hacking wildcarded
2597 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2598 contains globbing characters, then build an array of directories, and
2599 then map over that list while completing. */
2600 /* *** UNIMPLEMENTED *** */
2602 /* Now that we have some state, we can read the directory. */
2604 entry
= (struct dirent
*)NULL
;
2605 while (directory
&& (entry
= readdir (directory
)))
2607 convfn
= dentry
= entry
->d_name
;
2608 convlen
= dentlen
= D_NAMLEN (entry
);
2610 if (rl_filename_rewrite_hook
)
2612 convfn
= (*rl_filename_rewrite_hook
) (dentry
, dentlen
);
2613 convlen
= (convfn
== dentry
) ? dentlen
: strlen (convfn
);
2616 /* Special case for no filename. If the user has disabled the
2617 `match-hidden-files' variable, skip filenames beginning with `.'.
2618 All other entries except "." and ".." match. */
2619 if (filename_len
== 0)
2621 if (_rl_match_hidden_files
== 0 && HIDDEN_FILE (convfn
))
2624 if (convfn
[0] != '.' ||
2625 (convfn
[1] && (convfn
[1] != '.' || convfn
[2])))
2630 if (complete_fncmp (convfn
, convlen
, filename
, filename_len
))
2639 closedir (directory
);
2640 directory
= (DIR *)NULL
;
2645 dirname
= (char *)NULL
;
2650 filename
= (char *)NULL
;
2654 xfree (users_dirname
);
2655 users_dirname
= (char *)NULL
;
2658 return (char *)NULL
;
2662 /* dirname && (strcmp (dirname, ".") != 0) */
2663 if (dirname
&& (dirname
[0] != '.' || dirname
[1]))
2665 if (rl_complete_with_tilde_expansion
&& *users_dirname
== '~')
2667 dirlen
= strlen (dirname
);
2668 temp
= (char *)xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
2669 strcpy (temp
, dirname
);
2670 /* Canonicalization cuts off any final slash present. We
2671 may need to add it back. */
2672 if (dirname
[dirlen
- 1] != '/')
2674 temp
[dirlen
++] = '/';
2675 temp
[dirlen
] = '\0';
2680 dirlen
= strlen (users_dirname
);
2681 temp
= (char *)xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
2682 strcpy (temp
, users_dirname
);
2683 /* Make sure that temp has a trailing slash here. */
2684 if (users_dirname
[dirlen
- 1] != '/')
2685 temp
[dirlen
++] = '/';
2688 strcpy (temp
+ dirlen
, convfn
);
2691 temp
= savestring (convfn
);
2693 if (convfn
!= dentry
)
2700 /* An initial implementation of a menu completion function a la tcsh. The
2701 first time (if the last readline command was not rl_old_menu_complete), we
2702 generate the list of matches. This code is very similar to the code in
2703 rl_complete_internal -- there should be a way to combine the two. Then,
2704 for each item in the list of matches, we insert the match in an undoable
2705 fashion, with the appropriate character appended (this happens on the
2706 second and subsequent consecutive calls to rl_old_menu_complete). When we
2707 hit the end of the match list, we restore the original unmatched text,
2708 ring the bell, and reset the counter to zero. */
2710 rl_old_menu_complete (count
, invoking_key
)
2711 int count
, invoking_key
;
2713 rl_compentry_func_t
*our_func
;
2714 int matching_filenames
, found_quote
;
2716 static char *orig_text
;
2717 static char **matches
= (char **)0;
2718 static int match_list_index
= 0;
2719 static int match_list_size
= 0;
2720 static int orig_start
, orig_end
;
2721 static char quote_char
;
2722 static int delimiter
;
2724 /* The first time through, we generate the list of matches and set things
2725 up to insert them. */
2726 if (rl_last_func
!= rl_old_menu_complete
)
2728 /* Clean up from previous call, if any. */
2731 _rl_free_match_list (matches
);
2733 match_list_index
= match_list_size
= 0;
2734 matches
= (char **)NULL
;
2736 rl_completion_invoking_key
= invoking_key
;
2738 RL_SETSTATE(RL_STATE_COMPLETING
);
2740 /* Only the completion entry function can change these. */
2741 set_completion_defaults ('%');
2743 our_func
= rl_menu_completion_entry_function
;
2745 our_func
= rl_completion_entry_function
2746 ? rl_completion_entry_function
2747 : rl_filename_completion_function
;
2749 /* We now look backwards for the start of a filename/variable word. */
2750 orig_end
= rl_point
;
2751 found_quote
= delimiter
= 0;
2755 /* This (possibly) changes rl_point. If it returns a non-zero char,
2756 we know we have an open quote. */
2757 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2759 orig_start
= rl_point
;
2760 rl_point
= orig_end
;
2762 orig_text
= rl_copy_text (orig_start
, orig_end
);
2763 matches
= gen_completion_matches (orig_text
, orig_start
, orig_end
,
2764 our_func
, found_quote
, quote_char
);
2766 /* If we are matching filenames, the attempted completion function will
2767 have set rl_filename_completion_desired to a non-zero value. The basic
2768 rl_filename_completion_function does this. */
2769 matching_filenames
= rl_filename_completion_desired
;
2771 if (matches
== 0 || postprocess_matches (&matches
, matching_filenames
) == 0)
2775 matches
= (char **)0;
2777 orig_text
= (char *)0;
2778 completion_changed_buffer
= 0;
2779 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2783 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2785 for (match_list_size
= 0; matches
[match_list_size
]; match_list_size
++)
2787 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2788 code below should take care of it. */
2790 if (match_list_size
> 1 && _rl_complete_show_all
)
2791 display_matches (matches
);
2794 /* Now we have the list of matches. Replace the text between
2795 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2796 matches[match_list_index], and add any necessary closing char. */
2798 if (matches
== 0 || match_list_size
== 0)
2802 matches
= (char **)0;
2803 completion_changed_buffer
= 0;
2807 match_list_index
+= count
;
2808 if (match_list_index
< 0)
2810 while (match_list_index
< 0)
2811 match_list_index
+= match_list_size
;
2814 match_list_index
%= match_list_size
;
2816 if (match_list_index
== 0 && match_list_size
> 1)
2819 insert_match (orig_text
, orig_start
, MULT_MATCH
, "e_char
);
2823 insert_match (matches
[match_list_index
], orig_start
, SINGLE_MATCH
, "e_char
);
2824 append_to_match (matches
[match_list_index
], delimiter
, quote_char
,
2825 strcmp (orig_text
, matches
[match_list_index
]));
2828 completion_changed_buffer
= 1;
2833 rl_menu_complete (count
, ignore
)
2836 rl_compentry_func_t
*our_func
;
2837 int matching_filenames
, found_quote
;
2839 static char *orig_text
;
2840 static char **matches
= (char **)0;
2841 static int match_list_index
= 0;
2842 static int match_list_size
= 0;
2843 static int nontrivial_lcd
= 0;
2844 static int full_completion
= 0; /* set to 1 if menu completion should reinitialize on next call */
2845 static int orig_start
, orig_end
;
2846 static char quote_char
;
2847 static int delimiter
, cstate
;
2849 /* The first time through, we generate the list of matches and set things
2850 up to insert them. */
2851 if ((rl_last_func
!= rl_menu_complete
&& rl_last_func
!= rl_backward_menu_complete
) || full_completion
)
2853 /* Clean up from previous call, if any. */
2856 _rl_free_match_list (matches
);
2858 match_list_index
= match_list_size
= 0;
2859 matches
= (char **)NULL
;
2861 full_completion
= 0;
2863 RL_SETSTATE(RL_STATE_COMPLETING
);
2865 /* Only the completion entry function can change these. */
2866 set_completion_defaults ('%');
2868 our_func
= rl_menu_completion_entry_function
;
2870 our_func
= rl_completion_entry_function
2871 ? rl_completion_entry_function
2872 : rl_filename_completion_function
;
2874 /* We now look backwards for the start of a filename/variable word. */
2875 orig_end
= rl_point
;
2876 found_quote
= delimiter
= 0;
2880 /* This (possibly) changes rl_point. If it returns a non-zero char,
2881 we know we have an open quote. */
2882 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2884 orig_start
= rl_point
;
2885 rl_point
= orig_end
;
2887 orig_text
= rl_copy_text (orig_start
, orig_end
);
2888 matches
= gen_completion_matches (orig_text
, orig_start
, orig_end
,
2889 our_func
, found_quote
, quote_char
);
2891 nontrivial_lcd
= matches
&& strcmp (orig_text
, matches
[0]) != 0;
2893 /* If we are matching filenames, the attempted completion function will
2894 have set rl_filename_completion_desired to a non-zero value. The basic
2895 rl_filename_completion_function does this. */
2896 matching_filenames
= rl_filename_completion_desired
;
2898 if (matches
== 0 || postprocess_matches (&matches
, matching_filenames
) == 0)
2902 matches
= (char **)0;
2904 orig_text
= (char *)0;
2905 completion_changed_buffer
= 0;
2906 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2910 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2912 for (match_list_size
= 0; matches
[match_list_size
]; match_list_size
++)
2915 if (match_list_size
== 0)
2919 matches
= (char **)0;
2920 match_list_index
= 0;
2921 completion_changed_buffer
= 0;
2925 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2926 code below should take care of it. */
2929 insert_match (matches
[0], orig_start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2930 orig_end
= orig_start
+ strlen (matches
[0]);
2931 completion_changed_buffer
= STREQ (orig_text
, matches
[0]) == 0;
2934 if (match_list_size
> 1 && _rl_complete_show_all
)
2936 display_matches (matches
);
2937 /* If there are so many matches that the user has to be asked
2938 whether or not he wants to see the matches, menu completion
2940 if (rl_completion_query_items
> 0 && match_list_size
>= rl_completion_query_items
)
2944 matches
= (char **)0;
2945 full_completion
= 1;
2948 else if (_rl_menu_complete_prefix_first
)
2954 else if (match_list_size
<= 1)
2956 append_to_match (matches
[0], delimiter
, quote_char
, nontrivial_lcd
);
2957 full_completion
= 1;
2960 else if (_rl_menu_complete_prefix_first
&& match_list_size
> 1)
2967 /* Now we have the list of matches. Replace the text between
2968 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2969 matches[match_list_index], and add any necessary closing char. */
2971 if (matches
== 0 || match_list_size
== 0)
2975 matches
= (char **)0;
2976 completion_changed_buffer
= 0;
2980 match_list_index
+= count
;
2981 if (match_list_index
< 0)
2983 while (match_list_index
< 0)
2984 match_list_index
+= match_list_size
;
2987 match_list_index
%= match_list_size
;
2989 if (match_list_index
== 0 && match_list_size
> 1)
2992 insert_match (matches
[0], orig_start
, MULT_MATCH
, "e_char
);
2996 insert_match (matches
[match_list_index
], orig_start
, SINGLE_MATCH
, "e_char
);
2997 append_to_match (matches
[match_list_index
], delimiter
, quote_char
,
2998 strcmp (orig_text
, matches
[match_list_index
]));
3001 completion_changed_buffer
= 1;
3006 rl_backward_menu_complete (count
, key
)
3009 /* Positive arguments to backward-menu-complete translate into negative
3010 arguments for menu-complete, and vice versa. */
3011 return (rl_menu_complete (-count
, key
));