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((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));
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 int _rl_colored_completion_prefix
= 1;
218 /* If non-zero, when completing in the middle of a word, don't insert
219 characters from the match that match characters following point in
220 the word. This means, for instance, completing when the cursor is
221 after the `e' in `Makefile' won't result in `Makefilefile'. */
222 int _rl_skip_completed_text
= 0;
224 /* If non-zero, menu completion displays the common prefix first in the
225 cycle of possible completions instead of the last. */
226 int _rl_menu_complete_prefix_first
= 0;
228 /* If non-zero, then this is the address of a function to call when
229 completing on a directory name. The function is called with
230 the address of a string (the current directory name) as an arg. */
231 rl_icppfunc_t
*rl_directory_completion_hook
= (rl_icppfunc_t
*)NULL
;
233 rl_icppfunc_t
*rl_directory_rewrite_hook
= (rl_icppfunc_t
*)NULL
;
235 rl_icppfunc_t
*rl_filename_stat_hook
= (rl_icppfunc_t
*)NULL
;
237 /* If non-zero, this is the address of a function to call when reading
238 directory entries from the filesystem for completion and comparing
239 them to the partial word to be completed. The function should
240 either return its first argument (if no conversion takes place) or
241 newly-allocated memory. This can, for instance, convert filenames
242 between character sets for comparison against what's typed at the
243 keyboard. The returned value is what is added to the list of
244 matches. The second argument is the length of the filename to be
246 rl_dequote_func_t
*rl_filename_rewrite_hook
= (rl_dequote_func_t
*)NULL
;
248 /* Non-zero means readline completion functions perform tilde expansion. */
249 int rl_complete_with_tilde_expansion
= 0;
251 /* Pointer to the generator function for completion_matches ().
252 NULL means to use rl_filename_completion_function (), the default filename
254 rl_compentry_func_t
*rl_completion_entry_function
= (rl_compentry_func_t
*)NULL
;
256 /* Pointer to generator function for rl_menu_complete (). NULL means to use
257 *rl_completion_entry_function (see above). */
258 rl_compentry_func_t
*rl_menu_completion_entry_function
= (rl_compentry_func_t
*)NULL
;
260 /* Pointer to alternative function to create matches.
261 Function is called with TEXT, START, and END.
262 START and END are indices in RL_LINE_BUFFER saying what the boundaries
264 If this function exists and returns NULL then call the value of
265 rl_completion_entry_function to try to match, otherwise use the
266 array of strings returned. */
267 rl_completion_func_t
*rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
269 /* Non-zero means to suppress normal filename completion after the
270 user-specified completion function has been called. */
271 int rl_attempted_completion_over
= 0;
273 /* Set to a character indicating the type of completion being performed
274 by rl_complete_internal, available for use by application completion
276 int rl_completion_type
= 0;
278 /* Up to this many items will be displayed in response to a
279 possible-completions call. After that, we ask the user if
280 she is sure she wants to see them all. A negative value means
282 int rl_completion_query_items
= 100;
284 int _rl_page_completions
= 1;
286 /* The basic list of characters that signal a break between words for the
287 completer routine. The contents of this variable is what breaks words
288 in the shell, i.e. " \t\n\"\\'`@$><=" */
289 const char *rl_basic_word_break_characters
= " \t\n\"\\'`@$><=;|&{("; /* }) */
291 /* List of basic quoting characters. */
292 const char *rl_basic_quote_characters
= "\"'";
294 /* The list of characters that signal a break between words for
295 rl_complete_internal. The default list is the contents of
296 rl_basic_word_break_characters. */
297 /*const*/ char *rl_completer_word_break_characters
= (/*const*/ char *)NULL
;
299 /* Hook function to allow an application to set the completion word
300 break characters before readline breaks up the line. Allows
301 position-dependent word break characters. */
302 rl_cpvfunc_t
*rl_completion_word_break_hook
= (rl_cpvfunc_t
*)NULL
;
304 /* List of characters which can be used to quote a substring of the line.
305 Completion occurs on the entire substring, and within the substring
306 rl_completer_word_break_characters are treated as any other character,
307 unless they also appear within this list. */
308 const char *rl_completer_quote_characters
= (const char *)NULL
;
310 /* List of characters that should be quoted in filenames by the completer. */
311 const char *rl_filename_quote_characters
= (const char *)NULL
;
313 /* List of characters that are word break characters, but should be left
314 in TEXT when it is passed to the completion function. The shell uses
315 this to help determine what kind of completing to do. */
316 const char *rl_special_prefixes
= (const char *)NULL
;
318 /* If non-zero, then disallow duplicates in the matches. */
319 int rl_ignore_completion_duplicates
= 1;
321 /* Non-zero means that the results of the matches are to be treated
322 as filenames. This is ALWAYS zero on entry, and can only be changed
323 within a completion entry finder function. */
324 int rl_filename_completion_desired
= 0;
326 /* Non-zero means that the results of the matches are to be quoted using
327 double quotes (or an application-specific quoting mechanism) if the
328 filename contains any characters in rl_filename_quote_chars. This is
329 ALWAYS non-zero on entry, and can only be changed within a completion
330 entry finder function. */
331 int rl_filename_quoting_desired
= 1;
333 /* This function, if defined, is called by the completer when real
334 filename completion is done, after all the matching names have been
335 generated. It is passed a (char**) known as matches in the code below.
336 It consists of a NULL-terminated array of pointers to potential
337 matching strings. The 1st element (matches[0]) is the maximal
338 substring that is common to all matches. This function can re-arrange
339 the list of matches as required, but all elements of the array must be
340 free()'d if they are deleted. The main intent of this function is
341 to implement FIGNORE a la SunOS csh. */
342 rl_compignore_func_t
*rl_ignore_some_completions_function
= (rl_compignore_func_t
*)NULL
;
344 /* Set to a function to quote a filename in an application-specific fashion.
345 Called with the text to quote, the type of match found (single or multiple)
346 and a pointer to the quoting character to be used, which the function can
348 rl_quote_func_t
*rl_filename_quoting_function
= rl_quote_filename
;
350 /* Function to call to remove quoting characters from a filename. Called
351 before completion is attempted, so the embedded quotes do not interfere
352 with matching names in the file system. Readline doesn't do anything
353 with this; it's set only by applications. */
354 rl_dequote_func_t
*rl_filename_dequoting_function
= (rl_dequote_func_t
*)NULL
;
356 /* Function to call to decide whether or not a word break character is
357 quoted. If a character is quoted, it does not break words for the
359 rl_linebuf_func_t
*rl_char_is_quoted_p
= (rl_linebuf_func_t
*)NULL
;
361 /* If non-zero, the completion functions don't append anything except a
362 possible closing quote. This is set to 0 by rl_complete_internal and
363 may be changed by an application-specific completion function. */
364 int rl_completion_suppress_append
= 0;
366 /* Character appended to completed words when at the end of the line. The
367 default is a space. */
368 int rl_completion_append_character
= ' ';
370 /* If non-zero, the completion functions don't append any closing quote.
371 This is set to 0 by rl_complete_internal and may be changed by an
372 application-specific completion function. */
373 int rl_completion_suppress_quote
= 0;
375 /* Set to any quote character readline thinks it finds before any application
376 completion function is called. */
377 int rl_completion_quote_character
;
379 /* Set to a non-zero value if readline found quoting anywhere in the word to
380 be completed; set before any application completion function is called. */
381 int rl_completion_found_quote
;
383 /* If non-zero, a slash will be appended to completed filenames that are
384 symbolic links to directory names, subject to the value of the
385 mark-directories variable (which is user-settable). This exists so
386 that application completion functions can override the user's preference
387 (set via the mark-symlinked-directories variable) if appropriate.
388 It's set to the value of _rl_complete_mark_symlink_dirs in
389 rl_complete_internal before any application-specific completion
390 function is called, so without that function doing anything, the user's
391 preferences are honored. */
392 int rl_completion_mark_symlink_dirs
;
394 /* If non-zero, inhibit completion (temporarily). */
395 int rl_inhibit_completion
;
397 /* Set to the last key used to invoke one of the completion functions */
398 int rl_completion_invoking_key
;
400 /* If non-zero, sort the completion matches. On by default. */
401 int rl_sort_completion_matches
= 1;
403 /* Variables local to this file. */
405 /* Local variable states what happened during the last completion attempt. */
406 static int completion_changed_buffer
;
408 /* The result of the query to the user about displaying completion matches */
409 static int completion_y_or_n
;
411 static int _rl_complete_display_matches_interrupt
= 0;
413 /*************************************/
415 /* Bindable completion functions */
417 /*************************************/
419 /* Complete the word at or before point. You have supplied the function
420 that does the initial simple matching selection algorithm (see
421 rl_completion_matches ()). The default is to do filename completion. */
423 rl_complete (ignore
, invoking_key
)
424 int ignore
, invoking_key
;
426 rl_completion_invoking_key
= invoking_key
;
428 if (rl_inhibit_completion
)
429 return (_rl_insert_char (ignore
, invoking_key
));
430 else if (rl_last_func
== rl_complete
&& !completion_changed_buffer
)
431 return (rl_complete_internal ('?'));
432 else if (_rl_complete_show_all
)
433 return (rl_complete_internal ('!'));
434 else if (_rl_complete_show_unmodified
)
435 return (rl_complete_internal ('@'));
437 return (rl_complete_internal (TAB
));
440 /* List the possible completions. See description of rl_complete (). */
442 rl_possible_completions (ignore
, invoking_key
)
443 int ignore
, invoking_key
;
445 rl_completion_invoking_key
= invoking_key
;
446 return (rl_complete_internal ('?'));
450 rl_insert_completions (ignore
, invoking_key
)
451 int ignore
, invoking_key
;
453 rl_completion_invoking_key
= invoking_key
;
454 return (rl_complete_internal ('*'));
457 /* Return the correct value to pass to rl_complete_internal performing
458 the same tests as rl_complete. This allows consecutive calls to an
459 application's completion function to list possible completions and for
460 an application-specific completion function to honor the
461 show-all-if-ambiguous readline variable. */
463 rl_completion_mode (cfunc
)
464 rl_command_func_t
*cfunc
;
466 if (rl_last_func
== cfunc
&& !completion_changed_buffer
)
468 else if (_rl_complete_show_all
)
470 else if (_rl_complete_show_unmodified
)
476 /************************************/
478 /* Completion utility functions */
480 /************************************/
482 /* Reset readline state on a signal or other event. */
484 _rl_reset_completion_state ()
486 rl_completion_found_quote
= 0;
487 rl_completion_quote_character
= 0;
491 _rl_complete_sigcleanup (sig
, ptr
)
495 if (sig
== SIGINT
) /* XXX - for now */
497 _rl_free_match_list ((char **)ptr
);
498 _rl_complete_display_matches_interrupt
= 1;
502 /* Set default values for readline word completion. These are the variables
503 that application completion functions can change or inspect. */
505 set_completion_defaults (what_to_do
)
508 /* Only the completion entry function can change these. */
509 rl_filename_completion_desired
= 0;
510 rl_filename_quoting_desired
= 1;
511 rl_completion_type
= what_to_do
;
512 rl_completion_suppress_append
= rl_completion_suppress_quote
= 0;
513 rl_completion_append_character
= ' ';
515 /* The completion entry function may optionally change this. */
516 rl_completion_mark_symlink_dirs
= _rl_complete_mark_symlink_dirs
;
518 /* Reset private state. */
519 _rl_complete_display_matches_interrupt
= 0;
522 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
524 get_y_or_n (for_pager
)
529 /* For now, disable pager in callback mode, until we later convert to state
530 driven functions. Have to wait until next major version to add new
531 state definition, since it will change value of RL_STATE_DONE. */
532 #if defined (READLINE_CALLBACKS)
533 if (RL_ISSTATE (RL_STATE_CALLBACK
))
539 RL_SETSTATE(RL_STATE_MOREINPUT
);
541 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
543 if (c
== 'y' || c
== 'Y' || c
== ' ')
545 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
547 if (c
== ABORT_CHAR
|| c
< 0)
548 _rl_abort_internal ();
549 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
551 if (for_pager
&& (c
== 'q' || c
== 'Q'))
558 _rl_internal_pager (lines
)
563 fprintf (rl_outstream
, "--More--");
564 fflush (rl_outstream
);
566 _rl_erase_entire_line ();
576 path_isdir (filename
)
577 const char *filename
;
581 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
584 #if defined (VISIBLE_STATS)
585 /* Return the character which best describes FILENAME.
586 `@' for symbolic links
591 `%' for character special devices
592 `#' for block special devices */
602 /* Short-circuit a //server on cygwin, since that will always behave as
605 if (filename
[0] == '/' && filename
[1] == '/' && strchr (filename
+2, '/') == 0)
610 if (rl_filename_stat_hook
)
612 f
= savestring (filename
);
613 (*rl_filename_stat_hook
) (&f
);
619 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
620 r
= lstat (fn
, &finfo
);
622 r
= stat (fn
, &finfo
);
629 if (S_ISDIR (finfo
.st_mode
))
631 #if defined (S_ISCHR)
632 else if (S_ISCHR (finfo
.st_mode
))
635 #if defined (S_ISBLK)
636 else if (S_ISBLK (finfo
.st_mode
))
639 #if defined (S_ISLNK)
640 else if (S_ISLNK (finfo
.st_mode
))
643 #if defined (S_ISSOCK)
644 else if (S_ISSOCK (finfo
.st_mode
))
646 #endif /* S_ISSOCK */
647 #if defined (S_ISFIFO)
648 else if (S_ISFIFO (finfo
.st_mode
))
651 else if (S_ISREG (finfo
.st_mode
))
653 #if defined (_WIN32) && !defined (__CYGWIN__)
656 /* Windows doesn't do access and X_OK; check file extension instead */
657 ext
= strrchr (fn
, '.');
658 if (ext
&& (_rl_stricmp (ext
, ".exe") == 0 ||
659 _rl_stricmp (ext
, ".cmd") == 0 ||
660 _rl_stricmp (ext
, ".bat") == 0 ||
661 _rl_stricmp (ext
, ".com") == 0))
664 if (access (filename
, X_OK
) == 0)
672 #endif /* VISIBLE_STATS */
674 #if defined (COLOR_SUPPORT)
676 colored_stat_start (filename
)
679 _rl_set_normal_color ();
680 return (_rl_print_color_indicator (filename
));
686 _rl_prep_non_filename_text ();
687 _rl_put_indicator (&_rl_color_indicator
[C_CLR_TO_EOL
]);
691 colored_prefix_start ()
693 _rl_set_normal_color ();
694 return (_rl_print_prefix_color ());
698 colored_prefix_end ()
700 colored_stat_end (); /* for now */
704 /* Return the portion of PATHNAME that should be output when listing
705 possible completions. If we are hacking filename completion, we
706 are only interested in the basename, the portion following the
707 final slash. Otherwise, we return what we were passed. Since
708 printing empty strings is not very informative, if we're doing
709 filename completion, and the basename is the empty string, we look
710 for the previous slash and return the portion following that. If
711 there's no previous slash, we just return what we were passed. */
713 printable_part (pathname
)
718 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
721 temp
= strrchr (pathname
, '/');
722 #if defined (__MSDOS__) || defined (_WIN32)
723 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
727 if (temp
== 0 || *temp
== '\0')
729 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
730 Look for a previous slash and, if one is found, return the portion
731 following that slash. If there's no previous slash, just return the
732 pathname we were passed. */
733 else if (temp
[1] == '\0')
735 for (x
= temp
- 1; x
> pathname
; x
--)
738 return ((*x
== '/') ? x
+ 1 : pathname
);
744 /* Compute width of STRING when displayed on screen by print_filename */
750 #if defined (HANDLE_MULTIBYTE)
756 left
= strlen (string
) + 1;
757 memset (&ps
, 0, sizeof (mbstate_t));
763 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
770 #if defined (HANDLE_MULTIBYTE)
771 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
772 if (MB_INVALIDCH (clen
))
776 memset (&ps
, 0, sizeof (mbstate_t));
778 else if (MB_NULLWCH (clen
))
784 width
+= (w
>= 0) ? w
: 1;
796 #define ELLIPSIS_LEN 3
799 fnprint (to_print
, prefix_bytes
)
800 const char *to_print
;
805 int common_prefix_len
;
806 #if defined (HANDLE_MULTIBYTE)
813 end
= to_print
+ strlen (to_print
) + 1;
814 memset (&ps
, 0, sizeof (mbstate_t));
817 printed_len
= common_prefix_len
= 0;
819 /* Don't print only the ellipsis if the common prefix is one of the
820 possible completions */
821 if (to_print
[prefix_bytes
] == '\0')
824 if (prefix_bytes
&& _rl_completion_prefix_display_length
> 0)
828 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
829 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
830 putc (ellipsis
, rl_outstream
);
831 printed_len
= ELLIPSIS_LEN
;
833 #if defined (COLOR_SUPPORT)
834 else if (prefix_bytes
&& _rl_colored_completion_prefix
> 0)
836 common_prefix_len
= prefix_bytes
;
838 /* XXX - print color indicator start here */
839 colored_prefix_start ();
843 s
= to_print
+ prefix_bytes
;
848 putc ('^', rl_outstream
);
849 putc (UNCTRL (*s
), rl_outstream
);
852 #if defined (HANDLE_MULTIBYTE)
853 memset (&ps
, 0, sizeof (mbstate_t));
856 else if (*s
== RUBOUT
)
858 putc ('^', rl_outstream
);
859 putc ('?', rl_outstream
);
862 #if defined (HANDLE_MULTIBYTE)
863 memset (&ps
, 0, sizeof (mbstate_t));
868 #if defined (HANDLE_MULTIBYTE)
869 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
870 if (MB_INVALIDCH (tlen
))
874 memset (&ps
, 0, sizeof (mbstate_t));
876 else if (MB_NULLWCH (tlen
))
881 width
= (w
>= 0) ? w
: 1;
883 fwrite (s
, 1, tlen
, rl_outstream
);
885 printed_len
+= width
;
887 putc (*s
, rl_outstream
);
892 if (common_prefix_len
> 0 && (s
- to_print
) >= common_prefix_len
)
894 /* printed bytes = s - to_print */
895 /* printed bytes should never be > but check for paranoia's sake */
896 colored_prefix_end ();
897 common_prefix_len
= 0;
904 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
905 are using it, check for and output a single character for `special'
906 filenames. Return the number of characters we output. */
909 print_filename (to_print
, full_pathname
, prefix_bytes
)
910 char *to_print
, *full_pathname
;
913 int printed_len
, extension_char
, slen
, tlen
;
914 char *s
, c
, *new_full_pathname
, *dn
;
917 #if defined (COLOR_SUPPORT)
918 /* Defer printing if we want to prefix with a color indicator */
919 if (_rl_colored_stats
== 0 || rl_filename_completion_desired
== 0)
921 printed_len
= fnprint (to_print
, prefix_bytes
);
923 if (rl_filename_completion_desired
&& (
924 #if defined (VISIBLE_STATS)
927 #if defined (COLOR_SUPPORT)
930 _rl_complete_mark_directories
))
932 /* If to_print != full_pathname, to_print is the basename of the
933 path passed. In this case, we try to expand the directory
934 name before checking for the stat character. */
935 if (to_print
!= full_pathname
)
937 /* Terminate the directory name. */
941 /* If setting the last slash in full_pathname to a NUL results in
942 full_pathname being the empty string, we are trying to complete
943 files in the root directory. If we pass a null string to the
944 bash directory completion hook, for example, it will expand it
945 to the current directory. We just want the `/'. */
946 if (full_pathname
== 0 || *full_pathname
== 0)
948 else if (full_pathname
[0] != '/')
950 else if (full_pathname
[1] == 0)
951 dn
= "//"; /* restore trailing slash to `//' */
952 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
953 dn
= "/"; /* don't turn /// into // */
956 s
= tilde_expand (dn
);
957 if (rl_directory_completion_hook
)
958 (*rl_directory_completion_hook
) (&s
);
961 tlen
= strlen (to_print
);
962 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
963 strcpy (new_full_pathname
, s
);
964 if (s
[slen
- 1] == '/')
967 new_full_pathname
[slen
] = '/';
968 new_full_pathname
[slen
] = '/';
969 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
971 #if defined (VISIBLE_STATS)
972 if (rl_visible_stats
)
973 extension_char
= stat_char (new_full_pathname
);
976 if (_rl_complete_mark_directories
)
979 if (rl_directory_completion_hook
== 0 && rl_filename_stat_hook
)
981 dn
= savestring (new_full_pathname
);
982 (*rl_filename_stat_hook
) (&dn
);
983 xfree (new_full_pathname
);
984 new_full_pathname
= dn
;
986 if (path_isdir (new_full_pathname
))
987 extension_char
= '/';
990 #if defined (COLOR_SUPPORT)
991 if (_rl_colored_stats
)
993 colored_stat_start (new_full_pathname
);
994 printed_len
= fnprint (to_print
, prefix_bytes
);
999 xfree (new_full_pathname
);
1004 s
= tilde_expand (full_pathname
);
1005 #if defined (VISIBLE_STATS)
1006 if (rl_visible_stats
)
1007 extension_char
= stat_char (s
);
1010 if (_rl_complete_mark_directories
&& path_isdir (s
))
1011 extension_char
= '/';
1013 #if defined (COLOR_SUPPORT)
1014 if (_rl_colored_stats
)
1016 colored_stat_start (s
);
1017 printed_len
= fnprint (to_print
, prefix_bytes
);
1018 colored_stat_end ();
1027 putc (extension_char
, rl_outstream
);
1036 rl_quote_filename (s
, rtype
, qcp
)
1043 r
= (char *)xmalloc (strlen (s
) + 2);
1044 *r
= *rl_completer_quote_characters
;
1047 *qcp
= *rl_completer_quote_characters
;
1051 /* Find the bounds of the current word for completion purposes, and leave
1052 rl_point set to the end of the word. This function skips quoted
1053 substrings (characters between matched pairs of characters in
1054 rl_completer_quote_characters). First we try to find an unclosed
1055 quoted substring on which to do matching. If one is not found, we use
1056 the word break characters to find the boundaries of the current word.
1057 We call an application-specific function to decide whether or not a
1058 particular word break character is quoted; if that function returns a
1059 non-zero result, the character does not break a word. This function
1060 returns the opening quote character if we found an unclosed quoted
1061 substring, '\0' otherwise. FP, if non-null, is set to a value saying
1062 which (shell-like) quote characters we found (single quote, double
1063 quote, or backslash) anywhere in the string. DP, if non-null, is set to
1064 the value of the delimiter character that caused a word break. */
1067 _rl_find_completion_word (fp
, dp
)
1070 int scan
, end
, found_quote
, delimiter
, pass_next
, isbrk
;
1071 char quote_char
, *brkchars
;
1074 found_quote
= delimiter
= 0;
1078 if (rl_completion_word_break_hook
)
1079 brkchars
= (*rl_completion_word_break_hook
) ();
1081 brkchars
= rl_completer_word_break_characters
;
1083 if (rl_completer_quote_characters
)
1085 /* We have a list of characters which can be used in pairs to
1086 quote substrings for the completer. Try to find the start
1087 of an unclosed quoted substring. */
1088 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
1089 for (scan
= pass_next
= 0; scan
< end
; scan
= MB_NEXTCHAR (rl_line_buffer
, scan
, 1, MB_FIND_ANY
))
1097 /* Shell-like semantics for single quotes -- don't allow backslash
1098 to quote anything in single quotes, especially not the closing
1099 quote. If you don't like this, take out the check on the value
1101 if (quote_char
!= '\'' && rl_line_buffer
[scan
] == '\\')
1104 found_quote
|= RL_QF_BACKSLASH
;
1108 if (quote_char
!= '\0')
1110 /* Ignore everything until the matching close quote char. */
1111 if (rl_line_buffer
[scan
] == quote_char
)
1113 /* Found matching close. Abandon this substring. */
1118 else if (strchr (rl_completer_quote_characters
, rl_line_buffer
[scan
]))
1120 /* Found start of a quoted substring. */
1121 quote_char
= rl_line_buffer
[scan
];
1122 rl_point
= scan
+ 1;
1123 /* Shell-like quoting conventions. */
1124 if (quote_char
== '\'')
1125 found_quote
|= RL_QF_SINGLE_QUOTE
;
1126 else if (quote_char
== '"')
1127 found_quote
|= RL_QF_DOUBLE_QUOTE
;
1129 found_quote
|= RL_QF_OTHER_QUOTE
;
1134 if (rl_point
== end
&& quote_char
== '\0')
1136 /* We didn't find an unclosed quoted substring upon which to do
1137 completion, so use the word break characters to find the
1138 substring on which to complete. */
1139 while (rl_point
= MB_PREVCHAR (rl_line_buffer
, rl_point
, MB_FIND_ANY
))
1141 scan
= rl_line_buffer
[rl_point
];
1143 if (strchr (brkchars
, scan
) == 0)
1146 /* Call the application-specific function to tell us whether
1147 this word break character is quoted and should be skipped. */
1148 if (rl_char_is_quoted_p
&& found_quote
&&
1149 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
))
1152 /* Convoluted code, but it avoids an n^2 algorithm with calls
1153 to char_is_quoted. */
1158 /* If we are at an unquoted word break, then advance past it. */
1159 scan
= rl_line_buffer
[rl_point
];
1161 /* If there is an application-specific function to say whether or not
1162 a character is quoted and we found a quote character, let that
1163 function decide whether or not a character is a word break, even
1164 if it is found in rl_completer_word_break_characters. Don't bother
1165 if we're at the end of the line, though. */
1168 if (rl_char_is_quoted_p
)
1169 isbrk
= (found_quote
== 0 ||
1170 (*rl_char_is_quoted_p
) (rl_line_buffer
, rl_point
) == 0) &&
1171 strchr (brkchars
, scan
) != 0;
1173 isbrk
= strchr (brkchars
, scan
) != 0;
1177 /* If the character that caused the word break was a quoting
1178 character, then remember it as the delimiter. */
1179 if (rl_basic_quote_characters
&&
1180 strchr (rl_basic_quote_characters
, scan
) &&
1181 (end
- rl_point
) > 1)
1184 /* If the character isn't needed to determine something special
1185 about what kind of completion to perform, then advance past it. */
1186 if (rl_special_prefixes
== 0 || strchr (rl_special_prefixes
, scan
) == 0)
1196 return (quote_char
);
1200 gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
)
1203 rl_compentry_func_t
*our_func
;
1204 int found_quote
, quote_char
;
1208 rl_completion_found_quote
= found_quote
;
1209 rl_completion_quote_character
= quote_char
;
1211 /* If the user wants to TRY to complete, but then wants to give
1212 up and use the default completion function, they set the
1213 variable rl_attempted_completion_function. */
1214 if (rl_attempted_completion_function
)
1216 matches
= (*rl_attempted_completion_function
) (text
, start
, end
);
1217 if (RL_SIG_RECEIVED())
1219 _rl_free_match_list (matches
);
1221 RL_CHECK_SIGNALS ();
1224 if (matches
|| rl_attempted_completion_over
)
1226 rl_attempted_completion_over
= 0;
1231 /* XXX -- filename dequoting moved into rl_filename_completion_function */
1233 /* rl_completion_matches will check for signals as well to avoid a long
1234 delay while reading a directory. */
1235 matches
= rl_completion_matches (text
, our_func
);
1236 if (RL_SIG_RECEIVED())
1238 _rl_free_match_list (matches
);
1240 RL_CHECK_SIGNALS ();
1245 /* Filter out duplicates in MATCHES. This frees up the strings in
1248 remove_duplicate_matches (matches
)
1251 char *lowest_common
;
1256 /* Sort the items. */
1257 for (i
= 0; matches
[i
]; i
++)
1260 /* Sort the array without matches[0], since we need it to
1261 stay in place no matter what. */
1262 if (i
&& rl_sort_completion_matches
)
1263 qsort (matches
+1, i
-1, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1265 /* Remember the lowest common denominator for it may be unique. */
1266 lowest_common
= savestring (matches
[0]);
1268 for (i
= newlen
= 0; matches
[i
+ 1]; i
++)
1270 if (strcmp (matches
[i
], matches
[i
+ 1]) == 0)
1273 matches
[i
] = (char *)&dead_slot
;
1279 /* We have marked all the dead slots with (char *)&dead_slot.
1280 Copy all the non-dead entries into a new array. */
1281 temp_array
= (char **)xmalloc ((3 + newlen
) * sizeof (char *));
1282 for (i
= j
= 1; matches
[i
]; i
++)
1284 if (matches
[i
] != (char *)&dead_slot
)
1285 temp_array
[j
++] = matches
[i
];
1287 temp_array
[j
] = (char *)NULL
;
1289 if (matches
[0] != (char *)&dead_slot
)
1292 /* Place the lowest common denominator back in [0]. */
1293 temp_array
[0] = lowest_common
;
1295 /* If there is one string left, and it is identical to the
1296 lowest common denominator, then the LCD is the string to
1298 if (j
== 2 && strcmp (temp_array
[0], temp_array
[1]) == 0)
1300 xfree (temp_array
[1]);
1301 temp_array
[1] = (char *)NULL
;
1303 return (temp_array
);
1306 /* Find the common prefix of the list of matches, and put it into
1309 compute_lcd_of_matches (match_list
, matches
, text
)
1314 register int i
, c1
, c2
, si
;
1315 int low
; /* Count of max-matched characters. */
1317 char *dtext
; /* dequoted TEXT, if needed */
1318 #if defined (HANDLE_MULTIBYTE)
1325 /* If only one match, just use that. Otherwise, compare each
1326 member of the list with the next, finding out where they
1330 match_list
[0] = match_list
[1];
1331 match_list
[1] = (char *)NULL
;
1335 for (i
= 1, low
= 100000; i
< matches
; i
++)
1337 #if defined (HANDLE_MULTIBYTE)
1338 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1340 memset (&ps1
, 0, sizeof (mbstate_t));
1341 memset (&ps2
, 0, sizeof (mbstate_t));
1344 if (_rl_completion_case_fold
)
1347 (c1
= _rl_to_lower(match_list
[i
][si
])) &&
1348 (c2
= _rl_to_lower(match_list
[i
+ 1][si
]));
1350 #if defined (HANDLE_MULTIBYTE)
1351 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1353 v1
= mbrtowc(&wc1
, match_list
[i
]+si
, strlen (match_list
[i
]+si
), &ps1
);
1354 v2
= mbrtowc (&wc2
, match_list
[i
+1]+si
, strlen (match_list
[i
+1]+si
), &ps2
);
1355 if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
1357 if (c1
!= c2
) /* do byte comparison */
1361 wc1
= towlower (wc1
);
1362 wc2
= towlower (wc2
);
1376 (c1
= match_list
[i
][si
]) &&
1377 (c2
= match_list
[i
+ 1][si
]);
1379 #if defined (HANDLE_MULTIBYTE)
1380 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1384 if (!_rl_compare_chars (match_list
[i
], si
, &ps1
, match_list
[i
+1], si
, &ps2
))
1386 else if ((v
= _rl_get_char_len (&match_list
[i
][si
], &ps_back
)) > 1)
1399 /* If there were multiple matches, but none matched up to even the
1400 first character, and the user typed something, use that as the
1401 value of matches[0]. */
1402 if (low
== 0 && text
&& *text
)
1404 match_list
[0] = (char *)xmalloc (strlen (text
) + 1);
1405 strcpy (match_list
[0], text
);
1409 match_list
[0] = (char *)xmalloc (low
+ 1);
1411 /* XXX - this might need changes in the presence of multibyte chars */
1413 /* If we are ignoring case, try to preserve the case of the string
1414 the user typed in the face of multiple matches differing in case. */
1415 if (_rl_completion_case_fold
)
1417 /* We're making an assumption here:
1418 IF we're completing filenames AND
1419 the application has defined a filename dequoting function AND
1420 we found a quote character AND
1421 the application has requested filename quoting
1423 we assume that TEXT was dequoted before checking against
1424 the file system and needs to be dequoted here before we
1425 check against the list of matches
1427 dtext
= (char *)NULL
;
1428 if (rl_filename_completion_desired
&&
1429 rl_filename_dequoting_function
&&
1430 rl_completion_found_quote
&&
1431 rl_filename_quoting_desired
)
1433 dtext
= (*rl_filename_dequoting_function
) ((char *)text
, rl_completion_quote_character
);
1437 /* sort the list to get consistent answers. */
1438 qsort (match_list
+1, matches
, sizeof(char *), (QSFUNC
*)_rl_qsort_string_compare
);
1441 lx
= (si
<= low
) ? si
: low
; /* check shorter of text and matches */
1442 /* Try to preserve the case of what the user typed in the presence of
1443 multiple matches: check each match for something that matches
1444 what the user typed taking case into account; use it up to common
1445 length of matches if one is found. If not, just use first match. */
1446 for (i
= 1; i
<= matches
; i
++)
1447 if (strncmp (match_list
[i
], text
, lx
) == 0)
1449 strncpy (match_list
[0], match_list
[i
], low
);
1452 /* no casematch, use first entry */
1454 strncpy (match_list
[0], match_list
[1], low
);
1459 strncpy (match_list
[0], match_list
[1], low
);
1461 match_list
[0][low
] = '\0';
1468 postprocess_matches (matchesp
, matching_filenames
)
1470 int matching_filenames
;
1472 char *t
, **matches
, **temp_matches
;
1475 matches
= *matchesp
;
1480 /* It seems to me that in all the cases we handle we would like
1481 to ignore duplicate possibilities. Scan for the text to
1482 insert being identical to the other completions. */
1483 if (rl_ignore_completion_duplicates
)
1485 temp_matches
= remove_duplicate_matches (matches
);
1487 matches
= temp_matches
;
1490 /* If we are matching filenames, then here is our chance to
1491 do clever processing by re-examining the list. Call the
1492 ignore function with the array as a parameter. It can
1493 munge the array, deleting matches as it desires. */
1494 if (rl_ignore_some_completions_function
&& matching_filenames
)
1496 for (nmatch
= 1; matches
[nmatch
]; nmatch
++)
1498 (void)(*rl_ignore_some_completions_function
) (matches
);
1499 if (matches
== 0 || matches
[0] == 0)
1502 *matchesp
= (char **)0;
1507 /* If we removed some matches, recompute the common prefix. */
1508 for (i
= 1; matches
[i
]; i
++)
1510 if (i
> 1 && i
< nmatch
)
1513 compute_lcd_of_matches (matches
, i
- 1, t
);
1519 *matchesp
= matches
;
1524 complete_get_screenwidth ()
1529 cols
= _rl_completion_columns
;
1530 if (cols
>= 0 && cols
<= _rl_screenwidth
)
1532 envcols
= getenv ("COLUMNS");
1533 if (envcols
&& *envcols
)
1534 cols
= atoi (envcols
);
1535 if (cols
>= 0 && cols
<= _rl_screenwidth
)
1537 return _rl_screenwidth
;
1540 /* A convenience function for displaying a list of strings in
1541 columnar format on readline's output stream. MATCHES is the list
1542 of strings, in argv format, LEN is the number of strings in MATCHES,
1543 and MAX is the length of the longest string in MATCHES. */
1545 rl_display_match_list (matches
, len
, max
)
1549 int count
, limit
, printed_len
, lines
, cols
;
1550 int i
, j
, k
, l
, common_length
, sind
;
1553 /* Find the length of the prefix common to all items: length as displayed
1554 characters (common_length) and as a byte index into the matches (sind) */
1555 common_length
= sind
= 0;
1556 if (_rl_completion_prefix_display_length
> 0)
1558 t
= printable_part (matches
[0]);
1559 temp
= strrchr (t
, '/'); /* check again in case of /usr/src/ */
1560 common_length
= temp
? fnwidth (temp
) : fnwidth (t
);
1561 sind
= temp
? strlen (temp
) : strlen (t
);
1563 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
1564 max
-= common_length
- ELLIPSIS_LEN
;
1566 common_length
= sind
= 0;
1568 #if defined (COLOR_SUPPORT)
1569 else if (_rl_colored_completion_prefix
> 0)
1571 t
= printable_part (matches
[0]);
1572 temp
= strrchr (t
, '/');
1573 common_length
= temp
? fnwidth (temp
) : fnwidth (t
);
1574 sind
= temp
? RL_STRLEN (temp
+1) : RL_STRLEN (t
); /* want portion after final slash */
1578 /* How many items of MAX length can we fit in the screen window? */
1579 cols
= complete_get_screenwidth ();
1582 if (limit
!= 1 && (limit
* max
== cols
))
1585 /* If cols == 0, limit will end up -1 */
1586 if (cols
< _rl_screenwidth
&& limit
< 0)
1589 /* Avoid a possible floating exception. If max > cols,
1590 limit will be 0 and a divide-by-zero fault will result. */
1594 /* How many iterations of the printing loop? */
1595 count
= (len
+ (limit
- 1)) / limit
;
1597 /* Watch out for special case. If LEN is less than LIMIT, then
1598 just do the inner printing loop.
1599 0 < len <= limit implies count = 1. */
1601 /* Sort the items if they are not already sorted. */
1602 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
1603 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1608 if (_rl_print_completions_horizontally
== 0)
1610 /* Print the sorted items, up-and-down alphabetically, like ls. */
1611 for (i
= 1; i
<= count
; i
++)
1613 for (j
= 0, l
= i
; j
< limit
; j
++)
1615 if (l
> len
|| matches
[l
] == 0)
1619 temp
= printable_part (matches
[l
]);
1620 printed_len
= print_filename (temp
, matches
[l
], sind
);
1623 for (k
= 0; k
< max
- printed_len
; k
++)
1624 putc (' ', rl_outstream
);
1629 #if defined (SIGWINCH)
1630 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1632 if (RL_SIG_RECEIVED ())
1636 if (_rl_page_completions
&& lines
>= (_rl_screenheight
- 1) && i
< count
)
1638 lines
= _rl_internal_pager (lines
);
1646 /* Print the sorted items, across alphabetically, like ls -x. */
1647 for (i
= 1; matches
[i
]; i
++)
1649 temp
= printable_part (matches
[i
]);
1650 printed_len
= print_filename (temp
, matches
[i
], sind
);
1651 /* Have we reached the end of this line? */
1652 #if defined (SIGWINCH)
1653 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1655 if (RL_SIG_RECEIVED ())
1660 if (limit
== 1 || (i
&& (limit
> 1) && (i
% limit
) == 0))
1664 if (_rl_page_completions
&& lines
>= _rl_screenheight
- 1)
1666 lines
= _rl_internal_pager (lines
);
1672 for (k
= 0; k
< max
- printed_len
; k
++)
1673 putc (' ', rl_outstream
);
1680 /* Display MATCHES, a list of matching filenames in argv format. This
1681 handles the simple case -- a single match -- first. If there is more
1682 than one match, we compute the number of strings in the list and the
1683 length of the longest string, which will be needed by the display
1684 function. If the application wants to handle displaying the list of
1685 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1686 address of a function, and we just call it. If we're handling the
1687 display ourselves, we just call rl_display_match_list. We also check
1688 that the list of matches doesn't exceed the user-settable threshold,
1689 and ask the user if he wants to see the list if there are more matches
1690 than RL_COMPLETION_QUERY_ITEMS. */
1692 display_matches (matches
)
1698 /* Move to the last visible line of a possibly-multiple-line command. */
1699 _rl_move_vert (_rl_vis_botlin
);
1701 /* Handle simple case first. What if there is only one answer? */
1702 if (matches
[1] == 0)
1704 temp
= printable_part (matches
[0]);
1706 print_filename (temp
, matches
[0], 0);
1709 rl_forced_update_display ();
1710 rl_display_fixed
= 1;
1715 /* There is more than one answer. Find out how many there are,
1716 and find the maximum printed length of a single entry. */
1717 for (max
= 0, i
= 1; matches
[i
]; i
++)
1719 temp
= printable_part (matches
[i
]);
1720 len
= fnwidth (temp
);
1728 /* If the caller has defined a display hook, then call that now. */
1729 if (rl_completion_display_matches_hook
)
1731 (*rl_completion_display_matches_hook
) (matches
, len
, max
);
1735 /* If there are many items, then ask the user if she really wants to
1737 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
1740 fprintf (rl_outstream
, "Display all %d possibilities? (y or n)", len
);
1741 fflush (rl_outstream
);
1742 if ((completion_y_or_n
= get_y_or_n (0)) == 0)
1746 rl_forced_update_display ();
1747 rl_display_fixed
= 1;
1753 rl_display_match_list (matches
, len
, max
);
1755 rl_forced_update_display ();
1756 rl_display_fixed
= 1;
1760 make_quoted_replacement (match
, mtype
, qc
)
1763 char *qc
; /* Pointer to quoting character, if any */
1765 int should_quote
, do_replace
;
1768 /* If we are doing completion on quoted substrings, and any matches
1769 contain any of the completer_word_break_characters, then auto-
1770 matically prepend the substring with a quote character (just pick
1771 the first one from the list of such) if it does not already begin
1772 with a quote string. FIXME: Need to remove any such automatically
1773 inserted quote character when it no longer is necessary, such as
1774 if we change the string we are completing on and the new set of
1775 matches don't require a quoted substring. */
1776 replacement
= match
;
1778 should_quote
= match
&& rl_completer_quote_characters
&&
1779 rl_filename_completion_desired
&&
1780 rl_filename_quoting_desired
;
1783 should_quote
= should_quote
&& (!qc
|| !*qc
||
1784 (rl_completer_quote_characters
&& strchr (rl_completer_quote_characters
, *qc
)));
1788 /* If there is a single match, see if we need to quote it.
1789 This also checks whether the common prefix of several
1790 matches needs to be quoted. */
1791 should_quote
= rl_filename_quote_characters
1792 ? (_rl_strpbrk (match
, rl_filename_quote_characters
) != 0)
1795 do_replace
= should_quote
? mtype
: NO_MATCH
;
1796 /* Quote the replacement, since we found an embedded
1797 word break character in a potential match. */
1798 if (do_replace
!= NO_MATCH
&& rl_filename_quoting_function
)
1799 replacement
= (*rl_filename_quoting_function
) (match
, do_replace
, qc
);
1801 return (replacement
);
1805 insert_match (match
, start
, mtype
, qc
)
1810 char *replacement
, *r
;
1814 oqc
= qc
? *qc
: '\0';
1815 replacement
= make_quoted_replacement (match
, mtype
, qc
);
1817 /* Now insert the match. */
1820 rlen
= strlen (replacement
);
1821 /* Don't double an opening quote character. */
1822 if (qc
&& *qc
&& start
&& rl_line_buffer
[start
- 1] == *qc
&&
1823 replacement
[0] == *qc
)
1825 /* If make_quoted_replacement changed the quoting character, remove
1826 the opening quote and insert the (fully-quoted) replacement. */
1827 else if (qc
&& (*qc
!= oqc
) && start
&& rl_line_buffer
[start
- 1] == oqc
&&
1828 replacement
[0] != oqc
)
1831 /* Don't double a closing quote character */
1832 if (qc
&& *qc
&& end
&& rl_line_buffer
[rl_point
] == *qc
&& replacement
[rlen
- 1] == *qc
)
1834 if (_rl_skip_completed_text
)
1837 while (start
< rl_end
&& *r
&& rl_line_buffer
[start
] == *r
)
1842 if (start
<= end
|| *r
)
1843 _rl_replace_text (r
, start
, end
);
1844 rl_point
= start
+ strlen (r
);
1847 _rl_replace_text (replacement
, start
, end
);
1848 if (replacement
!= match
)
1849 xfree (replacement
);
1853 /* Append any necessary closing quote and a separator character to the
1854 just-inserted match. If the user has specified that directories
1855 should be marked by a trailing `/', append one of those instead. The
1856 default trailing character is a space. Returns the number of characters
1857 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1858 has them) and don't add a suffix for a symlink to a directory. A
1859 nontrivial match is one that actually adds to the word being completed.
1860 The variable rl_completion_mark_symlink_dirs controls this behavior
1861 (it's initially set to the what the user has chosen, indicated by the
1862 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1863 application's completion function). */
1865 append_to_match (text
, delimiter
, quote_char
, nontrivial_match
)
1867 int delimiter
, quote_char
, nontrivial_match
;
1869 char temp_string
[4], *filename
, *fn
;
1870 int temp_string_index
, s
;
1873 temp_string_index
= 0;
1874 if (quote_char
&& rl_point
&& rl_completion_suppress_quote
== 0 &&
1875 rl_line_buffer
[rl_point
- 1] != quote_char
)
1876 temp_string
[temp_string_index
++] = quote_char
;
1879 temp_string
[temp_string_index
++] = delimiter
;
1880 else if (rl_completion_suppress_append
== 0 && rl_completion_append_character
)
1881 temp_string
[temp_string_index
++] = rl_completion_append_character
;
1883 temp_string
[temp_string_index
++] = '\0';
1885 if (rl_filename_completion_desired
)
1887 filename
= tilde_expand (text
);
1888 if (rl_filename_stat_hook
)
1890 fn
= savestring (filename
);
1891 (*rl_filename_stat_hook
) (&fn
);
1895 s
= (nontrivial_match
&& rl_completion_mark_symlink_dirs
== 0)
1896 ? LSTAT (filename
, &finfo
)
1897 : stat (filename
, &finfo
);
1898 if (s
== 0 && S_ISDIR (finfo
.st_mode
))
1900 if (_rl_complete_mark_directories
/* && rl_completion_suppress_append == 0 */)
1902 /* This is clumsy. Avoid putting in a double slash if point
1903 is at the end of the line and the previous character is a
1905 if (rl_point
&& rl_line_buffer
[rl_point
] == '\0' && rl_line_buffer
[rl_point
- 1] == '/')
1907 else if (rl_line_buffer
[rl_point
] != '/')
1908 rl_insert_text ("/");
1912 /* Don't add anything if the filename is a symlink and resolves to a
1914 else if (s
== 0 && S_ISLNK (finfo
.st_mode
) && path_isdir (filename
))
1919 if (rl_point
== rl_end
&& temp_string_index
)
1920 rl_insert_text (temp_string
);
1926 if (rl_point
== rl_end
&& temp_string_index
)
1927 rl_insert_text (temp_string
);
1930 return (temp_string_index
);
1934 insert_all_matches (matches
, point
, qc
)
1942 rl_begin_undo_group ();
1943 /* remove any opening quote character; make_quoted_replacement will add
1945 if (qc
&& *qc
&& point
&& rl_line_buffer
[point
- 1] == *qc
)
1947 rl_delete_text (point
, rl_point
);
1952 for (i
= 1; matches
[i
]; i
++)
1954 rp
= make_quoted_replacement (matches
[i
], SINGLE_MATCH
, qc
);
1955 rl_insert_text (rp
);
1956 rl_insert_text (" ");
1957 if (rp
!= matches
[i
])
1963 rp
= make_quoted_replacement (matches
[0], SINGLE_MATCH
, qc
);
1964 rl_insert_text (rp
);
1965 rl_insert_text (" ");
1966 if (rp
!= matches
[0])
1969 rl_end_undo_group ();
1973 _rl_free_match_list (matches
)
1981 for (i
= 0; matches
[i
]; i
++)
1986 /* Complete the word at or before point.
1987 WHAT_TO_DO says what to do with the completion.
1988 `?' means list the possible completions.
1989 TAB means do standard completion.
1990 `*' means insert all of the possible completions.
1991 `!' means to do standard completion, and list all possible completions if
1992 there is more than one.
1993 `@' means to do standard completion, and list all possible completions if
1994 there is more than one and partial completion is not possible. */
1996 rl_complete_internal (what_to_do
)
2000 rl_compentry_func_t
*our_func
;
2001 int start
, end
, delimiter
, found_quote
, i
, nontrivial_lcd
;
2002 char *text
, *saved_line_buffer
;
2008 RL_SETSTATE(RL_STATE_COMPLETING
);
2010 set_completion_defaults (what_to_do
);
2012 saved_line_buffer
= rl_line_buffer
? savestring (rl_line_buffer
) : (char *)NULL
;
2013 our_func
= rl_completion_entry_function
2014 ? rl_completion_entry_function
2015 : rl_filename_completion_function
;
2016 /* We now look backwards for the start of a filename/variable word. */
2018 found_quote
= delimiter
= 0;
2022 /* This (possibly) changes rl_point. If it returns a non-zero char,
2023 we know we have an open quote. */
2024 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2029 text
= rl_copy_text (start
, end
);
2030 matches
= gen_completion_matches (text
, start
, end
, our_func
, found_quote
, quote_char
);
2031 /* nontrivial_lcd is set if the common prefix adds something to the word
2033 nontrivial_lcd
= matches
&& strcmp (text
, matches
[0]) != 0;
2034 if (what_to_do
== '!' || what_to_do
== '@')
2035 tlen
= strlen (text
);
2041 FREE (saved_line_buffer
);
2042 completion_changed_buffer
= 0;
2043 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2044 _rl_reset_completion_state ();
2048 /* If we are matching filenames, the attempted completion function will
2049 have set rl_filename_completion_desired to a non-zero value. The basic
2050 rl_filename_completion_function does this. */
2051 i
= rl_filename_completion_desired
;
2053 if (postprocess_matches (&matches
, i
) == 0)
2056 FREE (saved_line_buffer
);
2057 completion_changed_buffer
= 0;
2058 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2059 _rl_reset_completion_state ();
2068 /* Insert the first match with proper quoting. */
2069 if (what_to_do
== TAB
)
2072 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2074 else if (*matches
[0] && matches
[1] == 0)
2075 /* should we perform the check only if there are multiple matches? */
2076 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2077 else if (*matches
[0]) /* what_to_do != TAB && multiple matches */
2079 mlen
= *matches
[0] ? strlen (matches
[0]) : 0;
2081 insert_match (matches
[0], start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2084 /* If there are more matches, ring the bell to indicate.
2085 If we are in vi mode, Posix.2 says to not ring the bell.
2086 If the `show-all-if-ambiguous' variable is set, display
2087 all the matches immediately. Otherwise, if this was the
2088 only match, and we are hacking files, check the file to
2089 see if it was a directory. If so, and the `mark-directories'
2090 variable is set, add a '/' to the name. If not, and we
2091 are at the end of the line, then add a space. */
2094 if (what_to_do
== '!')
2096 display_matches (matches
);
2099 else if (what_to_do
== '@')
2101 if (nontrivial_lcd
== 0)
2102 display_matches (matches
);
2105 else if (rl_editing_mode
!= vi_mode
)
2106 rl_ding (); /* There are other matches remaining. */
2109 append_to_match (matches
[0], delimiter
, quote_char
, nontrivial_lcd
);
2114 insert_all_matches (matches
, start
, "e_char
);
2118 if (rl_completion_display_matches_hook
== 0)
2120 _rl_sigcleanup
= _rl_complete_sigcleanup
;
2121 _rl_sigcleanarg
= matches
;
2122 _rl_complete_display_matches_interrupt
= 0;
2124 display_matches (matches
);
2125 if (_rl_complete_display_matches_interrupt
)
2127 matches
= 0; /* already freed by rl_complete_sigcleanup */
2128 _rl_complete_display_matches_interrupt
= 0;
2129 if (rl_signal_event_hook
)
2130 (*rl_signal_event_hook
) (); /* XXX */
2133 _rl_sigcleanarg
= 0;
2137 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do
);
2139 FREE (saved_line_buffer
);
2140 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2141 _rl_free_match_list (matches
);
2142 _rl_reset_completion_state ();
2146 _rl_free_match_list (matches
);
2148 /* Check to see if the line has changed through all of this manipulation. */
2149 if (saved_line_buffer
)
2151 completion_changed_buffer
= strcmp (rl_line_buffer
, saved_line_buffer
) != 0;
2152 xfree (saved_line_buffer
);
2155 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2156 _rl_reset_completion_state ();
2158 RL_CHECK_SIGNALS ();
2162 /***************************************************************/
2164 /* Application-callable completion match generator functions */
2166 /***************************************************************/
2168 /* Return an array of (char *) which is a list of completions for TEXT.
2169 If there are no completions, return a NULL pointer.
2170 The first entry in the returned array is the substitution for TEXT.
2171 The remaining entries are the possible completions.
2172 The array is terminated with a NULL pointer.
2174 ENTRY_FUNCTION is a function of two args, and returns a (char *).
2175 The first argument is TEXT.
2176 The second is a state argument; it should be zero on the first call, and
2177 non-zero on subsequent calls. It returns a NULL pointer to the caller
2178 when there are no more matches.
2181 rl_completion_matches (text
, entry_function
)
2183 rl_compentry_func_t
*entry_function
;
2187 /* Number of slots in match_list. */
2188 int match_list_size
;
2190 /* The list of matches. */
2193 /* Number of matches actually found. */
2196 /* Temporary string binder. */
2200 match_list_size
= 10;
2201 match_list
= (char **)xmalloc ((match_list_size
+ 1) * sizeof (char *));
2202 match_list
[1] = (char *)NULL
;
2204 while (string
= (*entry_function
) (text
, matches
))
2206 if (RL_SIG_RECEIVED ())
2208 /* Start at 1 because we don't set matches[0] in this function.
2209 Only free the list members if we're building match list from
2210 rl_filename_completion_function, since we know that doesn't
2211 free the strings it returns. */
2212 if (entry_function
== rl_filename_completion_function
)
2214 for (i
= 1; match_list
[i
]; i
++)
2215 xfree (match_list
[i
]);
2219 match_list_size
= 0;
2221 RL_CHECK_SIGNALS ();
2224 if (matches
+ 1 >= match_list_size
)
2225 match_list
= (char **)xrealloc
2226 (match_list
, ((match_list_size
+= 10) + 1) * sizeof (char *));
2228 if (match_list
== 0)
2229 return (match_list
);
2231 match_list
[++matches
] = string
;
2232 match_list
[matches
+ 1] = (char *)NULL
;
2235 /* If there were any matches, then look through them finding out the
2236 lowest common denominator. That then becomes match_list[0]. */
2238 compute_lcd_of_matches (match_list
, matches
, text
);
2239 else /* There were no matches. */
2242 match_list
= (char **)NULL
;
2244 return (match_list
);
2247 /* A completion function for usernames.
2248 TEXT contains a partial username preceded by a random
2249 character (usually `~'). */
2251 rl_username_completion_function (text
, state
)
2255 #if defined (__WIN32__) || defined (__OPENNT)
2256 return (char *)NULL
;
2257 #else /* !__WIN32__ && !__OPENNT) */
2258 static char *username
= (char *)NULL
;
2259 static struct passwd
*entry
;
2260 static int namelen
, first_char
, first_char_loc
;
2268 first_char_loc
= first_char
== '~';
2270 username
= savestring (&text
[first_char_loc
]);
2271 namelen
= strlen (username
);
2272 #if defined (HAVE_GETPWENT)
2277 #if defined (HAVE_GETPWENT)
2278 while (entry
= getpwent ())
2280 /* Null usernames should result in all users as possible completions. */
2281 if (namelen
== 0 || (STREQN (username
, entry
->pw_name
, namelen
)))
2288 #if defined (HAVE_GETPWENT)
2291 return ((char *)NULL
);
2295 value
= (char *)xmalloc (2 + strlen (entry
->pw_name
));
2299 strcpy (value
+ first_char_loc
, entry
->pw_name
);
2301 if (first_char
== '~')
2302 rl_filename_completion_desired
= 1;
2306 #endif /* !__WIN32__ && !__OPENNT */
2309 /* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME
2310 (FILENAME_LEN). If _rl_completion_case_fold is set, compare without
2311 regard to the alphabetic case of characters. If
2312 _rl_completion_case_map is set, make `-' and `_' equivalent. CONVFN is
2313 the possibly-converted directory entry; FILENAME is what the user typed. */
2315 complete_fncmp (convfn
, convlen
, filename
, filename_len
)
2318 const char *filename
;
2321 register char *s1
, *s2
;
2323 #if defined (HANDLE_MULTIBYTE)
2329 #if defined (HANDLE_MULTIBYTE)
2330 memset (&ps1
, 0, sizeof (mbstate_t));
2331 memset (&ps2
, 0, sizeof (mbstate_t));
2334 if (filename_len
== 0)
2336 if (convlen
< filename_len
)
2340 s1
= (char *)convfn
;
2341 s2
= (char *)filename
;
2343 /* Otherwise, if these match up to the length of filename, then
2345 if (_rl_completion_case_fold
&& _rl_completion_case_map
)
2347 /* Case-insensitive comparison treating _ and - as equivalent */
2348 #if defined (HANDLE_MULTIBYTE)
2349 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2353 v1
= mbrtowc (&wc1
, s1
, convlen
, &ps1
);
2354 v2
= mbrtowc (&wc2
, s2
, filename_len
, &ps2
);
2355 if (v1
== 0 && v2
== 0)
2357 else if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
2359 if (*s1
!= *s2
) /* do byte comparison */
2361 else if ((*s1
== '-' || *s1
== '_') && (*s2
== '-' || *s2
== '_'))
2366 wc1
= towlower (wc1
);
2367 wc2
= towlower (wc2
);
2371 if ((wc1
== L
'-' || wc1
== L
'_') && (wc2
== L
'-' || wc2
== L
'_'))
2383 d
= _rl_to_lower (*s1
) - _rl_to_lower (*s2
);
2384 /* *s1 == [-_] && *s2 == [-_] */
2385 if ((*s1
== '-' || *s1
== '_') && (*s2
== '-' || *s2
== '_'))
2389 s1
++; s2
++; /* already checked convlen >= filename_len */
2396 else if (_rl_completion_case_fold
)
2398 #if defined (HANDLE_MULTIBYTE)
2399 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
2403 v1
= mbrtowc (&wc1
, s1
, convlen
, &ps1
);
2404 v2
= mbrtowc (&wc2
, s2
, filename_len
, &ps2
);
2405 if (v1
== 0 && v2
== 0)
2407 else if (MB_INVALIDCH (v1
) || MB_INVALIDCH (v2
))
2409 if (*s1
!= *s2
) /* do byte comparison */
2414 wc1
= towlower (wc1
);
2415 wc2
= towlower (wc2
);
2427 if ((_rl_to_lower (convfn
[0]) == _rl_to_lower (filename
[0])) &&
2428 (convlen
>= filename_len
) &&
2429 (_rl_strnicmp (filename
, convfn
, filename_len
) == 0))
2434 if ((convfn
[0] == filename
[0]) &&
2435 (convlen
>= filename_len
) &&
2436 (strncmp (filename
, convfn
, filename_len
) == 0))
2442 /* Okay, now we write the entry_function for filename completion. In the
2443 general case. Note that completion in the shell is a little different
2444 because of all the pathnames that must be followed when looking up the
2445 completion for a command. */
2447 rl_filename_completion_function (text
, state
)
2451 static DIR *directory
= (DIR *)NULL
;
2452 static char *filename
= (char *)NULL
;
2453 static char *dirname
= (char *)NULL
;
2454 static char *users_dirname
= (char *)NULL
;
2455 static int filename_len
;
2456 char *temp
, *dentry
, *convfn
;
2457 int dirlen
, dentlen
, convlen
;
2459 struct dirent
*entry
;
2461 /* If we don't have any state, then do some initialization. */
2464 /* If we were interrupted before closing the directory or reading
2465 all of its contents, close it. */
2468 closedir (directory
);
2469 directory
= (DIR *)NULL
;
2473 FREE (users_dirname
);
2475 filename
= savestring (text
);
2478 dirname
= savestring (text
);
2480 temp
= strrchr (dirname
, '/');
2482 #if defined (__MSDOS__) || defined (_WIN32)
2483 /* special hack for //X/... */
2484 if (dirname
[0] == '/' && dirname
[1] == '/' && ISALPHA ((unsigned char)dirname
[2]) && dirname
[3] == '/')
2485 temp
= strrchr (dirname
+ 3, '/');
2490 strcpy (filename
, ++temp
);
2493 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN__))
2494 /* searches from current directory on the drive */
2495 else if (ISALPHA ((unsigned char)dirname
[0]) && dirname
[1] == ':')
2497 strcpy (filename
, dirname
+ 2);
2507 /* We aren't done yet. We also support the "~user" syntax. */
2509 /* Save the version of the directory that the user typed, dequoting
2511 if (rl_completion_found_quote
&& rl_filename_dequoting_function
)
2512 users_dirname
= (*rl_filename_dequoting_function
) (dirname
, rl_completion_quote_character
);
2514 users_dirname
= savestring (dirname
);
2517 if (*dirname
== '~')
2519 temp
= tilde_expand (dirname
);
2525 /* We have saved the possibly-dequoted version of the directory name
2526 the user typed. Now transform the directory name we're going to
2527 pass to opendir(2). The directory rewrite hook modifies only the
2528 directory name; the directory completion hook modifies both the
2529 directory name passed to opendir(2) and the version the user
2530 typed. Both the directory completion and rewrite hooks should perform
2531 any necessary dequoting. The hook functions return 1 if they modify
2532 the directory name argument. If either hook returns 0, it should
2533 not modify the directory name pointer passed as an argument. */
2534 if (rl_directory_rewrite_hook
)
2535 (*rl_directory_rewrite_hook
) (&dirname
);
2536 else if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&dirname
))
2538 xfree (users_dirname
);
2539 users_dirname
= savestring (dirname
);
2541 else if (tilde_dirname
== 0 && rl_completion_found_quote
&& rl_filename_dequoting_function
)
2543 /* delete single and double quotes */
2545 dirname
= savestring (users_dirname
);
2547 directory
= opendir (dirname
);
2549 /* Now dequote a non-null filename. FILENAME will not be NULL, but may
2551 if (*filename
&& rl_completion_found_quote
&& rl_filename_dequoting_function
)
2553 /* delete single and double quotes */
2554 temp
= (*rl_filename_dequoting_function
) (filename
, rl_completion_quote_character
);
2558 filename_len
= strlen (filename
);
2560 rl_filename_completion_desired
= 1;
2563 /* At this point we should entertain the possibility of hacking wildcarded
2564 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2565 contains globbing characters, then build an array of directories, and
2566 then map over that list while completing. */
2567 /* *** UNIMPLEMENTED *** */
2569 /* Now that we have some state, we can read the directory. */
2571 entry
= (struct dirent
*)NULL
;
2572 while (directory
&& (entry
= readdir (directory
)))
2574 convfn
= dentry
= entry
->d_name
;
2575 convlen
= dentlen
= D_NAMLEN (entry
);
2577 if (rl_filename_rewrite_hook
)
2579 convfn
= (*rl_filename_rewrite_hook
) (dentry
, dentlen
);
2580 convlen
= (convfn
== dentry
) ? dentlen
: strlen (convfn
);
2583 /* Special case for no filename. If the user has disabled the
2584 `match-hidden-files' variable, skip filenames beginning with `.'.
2585 All other entries except "." and ".." match. */
2586 if (filename_len
== 0)
2588 if (_rl_match_hidden_files
== 0 && HIDDEN_FILE (convfn
))
2591 if (convfn
[0] != '.' ||
2592 (convfn
[1] && (convfn
[1] != '.' || convfn
[2])))
2597 if (complete_fncmp (convfn
, convlen
, filename
, filename_len
))
2606 closedir (directory
);
2607 directory
= (DIR *)NULL
;
2612 dirname
= (char *)NULL
;
2617 filename
= (char *)NULL
;
2621 xfree (users_dirname
);
2622 users_dirname
= (char *)NULL
;
2625 return (char *)NULL
;
2629 /* dirname && (strcmp (dirname, ".") != 0) */
2630 if (dirname
&& (dirname
[0] != '.' || dirname
[1]))
2632 if (rl_complete_with_tilde_expansion
&& *users_dirname
== '~')
2634 dirlen
= strlen (dirname
);
2635 temp
= (char *)xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
2636 strcpy (temp
, dirname
);
2637 /* Canonicalization cuts off any final slash present. We
2638 may need to add it back. */
2639 if (dirname
[dirlen
- 1] != '/')
2641 temp
[dirlen
++] = '/';
2642 temp
[dirlen
] = '\0';
2647 dirlen
= strlen (users_dirname
);
2648 temp
= (char *)xmalloc (2 + dirlen
+ D_NAMLEN (entry
));
2649 strcpy (temp
, users_dirname
);
2650 /* Make sure that temp has a trailing slash here. */
2651 if (users_dirname
[dirlen
- 1] != '/')
2652 temp
[dirlen
++] = '/';
2655 strcpy (temp
+ dirlen
, convfn
);
2658 temp
= savestring (convfn
);
2660 if (convfn
!= dentry
)
2667 /* An initial implementation of a menu completion function a la tcsh. The
2668 first time (if the last readline command was not rl_old_menu_complete), we
2669 generate the list of matches. This code is very similar to the code in
2670 rl_complete_internal -- there should be a way to combine the two. Then,
2671 for each item in the list of matches, we insert the match in an undoable
2672 fashion, with the appropriate character appended (this happens on the
2673 second and subsequent consecutive calls to rl_old_menu_complete). When we
2674 hit the end of the match list, we restore the original unmatched text,
2675 ring the bell, and reset the counter to zero. */
2677 rl_old_menu_complete (count
, invoking_key
)
2678 int count
, invoking_key
;
2680 rl_compentry_func_t
*our_func
;
2681 int matching_filenames
, found_quote
;
2683 static char *orig_text
;
2684 static char **matches
= (char **)0;
2685 static int match_list_index
= 0;
2686 static int match_list_size
= 0;
2687 static int orig_start
, orig_end
;
2688 static char quote_char
;
2689 static int delimiter
;
2691 /* The first time through, we generate the list of matches and set things
2692 up to insert them. */
2693 if (rl_last_func
!= rl_old_menu_complete
)
2695 /* Clean up from previous call, if any. */
2698 _rl_free_match_list (matches
);
2700 match_list_index
= match_list_size
= 0;
2701 matches
= (char **)NULL
;
2703 rl_completion_invoking_key
= invoking_key
;
2705 RL_SETSTATE(RL_STATE_COMPLETING
);
2707 /* Only the completion entry function can change these. */
2708 set_completion_defaults ('%');
2710 our_func
= rl_menu_completion_entry_function
;
2712 our_func
= rl_completion_entry_function
2713 ? rl_completion_entry_function
2714 : rl_filename_completion_function
;
2716 /* We now look backwards for the start of a filename/variable word. */
2717 orig_end
= rl_point
;
2718 found_quote
= delimiter
= 0;
2722 /* This (possibly) changes rl_point. If it returns a non-zero char,
2723 we know we have an open quote. */
2724 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2726 orig_start
= rl_point
;
2727 rl_point
= orig_end
;
2729 orig_text
= rl_copy_text (orig_start
, orig_end
);
2730 matches
= gen_completion_matches (orig_text
, orig_start
, orig_end
,
2731 our_func
, found_quote
, quote_char
);
2733 /* If we are matching filenames, the attempted completion function will
2734 have set rl_filename_completion_desired to a non-zero value. The basic
2735 rl_filename_completion_function does this. */
2736 matching_filenames
= rl_filename_completion_desired
;
2738 if (matches
== 0 || postprocess_matches (&matches
, matching_filenames
) == 0)
2742 matches
= (char **)0;
2744 orig_text
= (char *)0;
2745 completion_changed_buffer
= 0;
2746 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2750 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2752 for (match_list_size
= 0; matches
[match_list_size
]; match_list_size
++)
2754 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2755 code below should take care of it. */
2757 if (match_list_size
> 1 && _rl_complete_show_all
)
2758 display_matches (matches
);
2761 /* Now we have the list of matches. Replace the text between
2762 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2763 matches[match_list_index], and add any necessary closing char. */
2765 if (matches
== 0 || match_list_size
== 0)
2769 matches
= (char **)0;
2770 completion_changed_buffer
= 0;
2774 match_list_index
+= count
;
2775 if (match_list_index
< 0)
2777 while (match_list_index
< 0)
2778 match_list_index
+= match_list_size
;
2781 match_list_index
%= match_list_size
;
2783 if (match_list_index
== 0 && match_list_size
> 1)
2786 insert_match (orig_text
, orig_start
, MULT_MATCH
, "e_char
);
2790 insert_match (matches
[match_list_index
], orig_start
, SINGLE_MATCH
, "e_char
);
2791 append_to_match (matches
[match_list_index
], delimiter
, quote_char
,
2792 strcmp (orig_text
, matches
[match_list_index
]));
2795 completion_changed_buffer
= 1;
2800 rl_menu_complete (count
, ignore
)
2803 rl_compentry_func_t
*our_func
;
2804 int matching_filenames
, found_quote
;
2806 static char *orig_text
;
2807 static char **matches
= (char **)0;
2808 static int match_list_index
= 0;
2809 static int match_list_size
= 0;
2810 static int nontrivial_lcd
= 0;
2811 static int full_completion
= 0; /* set to 1 if menu completion should reinitialize on next call */
2812 static int orig_start
, orig_end
;
2813 static char quote_char
;
2814 static int delimiter
, cstate
;
2816 /* The first time through, we generate the list of matches and set things
2817 up to insert them. */
2818 if ((rl_last_func
!= rl_menu_complete
&& rl_last_func
!= rl_backward_menu_complete
) || full_completion
)
2820 /* Clean up from previous call, if any. */
2823 _rl_free_match_list (matches
);
2825 match_list_index
= match_list_size
= 0;
2826 matches
= (char **)NULL
;
2828 full_completion
= 0;
2830 RL_SETSTATE(RL_STATE_COMPLETING
);
2832 /* Only the completion entry function can change these. */
2833 set_completion_defaults ('%');
2835 our_func
= rl_menu_completion_entry_function
;
2837 our_func
= rl_completion_entry_function
2838 ? rl_completion_entry_function
2839 : rl_filename_completion_function
;
2841 /* We now look backwards for the start of a filename/variable word. */
2842 orig_end
= rl_point
;
2843 found_quote
= delimiter
= 0;
2847 /* This (possibly) changes rl_point. If it returns a non-zero char,
2848 we know we have an open quote. */
2849 quote_char
= _rl_find_completion_word (&found_quote
, &delimiter
);
2851 orig_start
= rl_point
;
2852 rl_point
= orig_end
;
2854 orig_text
= rl_copy_text (orig_start
, orig_end
);
2855 matches
= gen_completion_matches (orig_text
, orig_start
, orig_end
,
2856 our_func
, found_quote
, quote_char
);
2858 nontrivial_lcd
= matches
&& strcmp (orig_text
, matches
[0]) != 0;
2860 /* If we are matching filenames, the attempted completion function will
2861 have set rl_filename_completion_desired to a non-zero value. The basic
2862 rl_filename_completion_function does this. */
2863 matching_filenames
= rl_filename_completion_desired
;
2865 if (matches
== 0 || postprocess_matches (&matches
, matching_filenames
) == 0)
2869 matches
= (char **)0;
2871 orig_text
= (char *)0;
2872 completion_changed_buffer
= 0;
2873 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2877 RL_UNSETSTATE(RL_STATE_COMPLETING
);
2879 for (match_list_size
= 0; matches
[match_list_size
]; match_list_size
++)
2882 if (match_list_size
== 0)
2886 matches
= (char **)0;
2887 match_list_index
= 0;
2888 completion_changed_buffer
= 0;
2892 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2893 code below should take care of it. */
2896 insert_match (matches
[0], orig_start
, matches
[1] ? MULT_MATCH
: SINGLE_MATCH
, "e_char
);
2897 orig_end
= orig_start
+ strlen (matches
[0]);
2898 completion_changed_buffer
= STREQ (orig_text
, matches
[0]) == 0;
2901 if (match_list_size
> 1 && _rl_complete_show_all
)
2903 display_matches (matches
);
2904 /* If there are so many matches that the user has to be asked
2905 whether or not he wants to see the matches, menu completion
2907 if (rl_completion_query_items
> 0 && match_list_size
>= rl_completion_query_items
)
2911 matches
= (char **)0;
2912 full_completion
= 1;
2915 else if (_rl_menu_complete_prefix_first
)
2921 else if (match_list_size
<= 1)
2923 append_to_match (matches
[0], delimiter
, quote_char
, nontrivial_lcd
);
2924 full_completion
= 1;
2927 else if (_rl_menu_complete_prefix_first
&& match_list_size
> 1)
2934 /* Now we have the list of matches. Replace the text between
2935 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2936 matches[match_list_index], and add any necessary closing char. */
2938 if (matches
== 0 || match_list_size
== 0)
2942 matches
= (char **)0;
2943 completion_changed_buffer
= 0;
2947 match_list_index
+= count
;
2948 if (match_list_index
< 0)
2950 while (match_list_index
< 0)
2951 match_list_index
+= match_list_size
;
2954 match_list_index
%= match_list_size
;
2956 if (match_list_index
== 0 && match_list_size
> 1)
2959 insert_match (matches
[0], orig_start
, MULT_MATCH
, "e_char
);
2963 insert_match (matches
[match_list_index
], orig_start
, SINGLE_MATCH
, "e_char
);
2964 append_to_match (matches
[match_list_index
], delimiter
, quote_char
,
2965 strcmp (orig_text
, matches
[match_list_index
]));
2968 completion_changed_buffer
= 1;
2973 rl_backward_menu_complete (count
, key
)
2976 /* Positive arguments to backward-menu-complete translate into negative
2977 arguments for menu-complete, and vice versa. */
2978 return (rl_menu_complete (-count
, key
));