]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - readline/complete.c
ac54d76a255e6d49e5e32f6de7459e94b33132b7
[thirdparty/binutils-gdb.git] / readline / complete.c
1 /* complete.c -- filename completion for readline. */
2
3 /* Copyright (C) 1987-2015 Free Software Foundation, Inc.
4
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.
7
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.
12
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.
17
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/>.
20 */
21
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 # include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #if defined (HAVE_SYS_FILE_H)
31 # include <sys/file.h>
32 #endif
33
34 #include <signal.h>
35
36 #if defined (HAVE_UNISTD_H)
37 # include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39
40 #if defined (HAVE_STDLIB_H)
41 # include <stdlib.h>
42 #else
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
45
46 #include <stdio.h>
47
48 #include <errno.h>
49 #if !defined (errno)
50 extern int errno;
51 #endif /* !errno */
52
53 #if defined (HAVE_PWD_H)
54 #include <pwd.h>
55 #endif
56
57 #include "posixdir.h"
58 #include "posixstat.h"
59
60 /* System-specific feature definitions and include files. */
61 #include "rldefs.h"
62 #include "rlmbutil.h"
63
64 /* Some standard library routines. */
65 #include "readline.h"
66 #include "xmalloc.h"
67 #include "rlprivate.h"
68
69 #if defined (COLOR_SUPPORT)
70 # include "colors.h"
71 #endif
72
73 #ifdef __STDC__
74 typedef int QSFUNC (const void *, const void *);
75 #else
76 typedef int QSFUNC ();
77 #endif
78
79 #ifdef HAVE_LSTAT
80 # define LSTAT lstat
81 #else
82 # define LSTAT stat
83 #endif
84
85 /* Unix version of a hidden file. Could be different on other systems. */
86 #define HIDDEN_FILE(fname) ((fname)[0] == '.')
87
88 /* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
89 defined. */
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) */
93
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;
102
103 #if defined (VISIBLE_STATS) || defined (COLOR_SUPPORT)
104 # if !defined (X_OK)
105 # define X_OK 1
106 # endif
107 #endif
108
109 #if defined (VISIBLE_STATS)
110 static int stat_char PARAMS((char *));
111 #endif
112
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));
118 #endif
119
120 static int path_isdir PARAMS((const char *));
121
122 static char *rl_quote_filename PARAMS((char *, int, char *));
123
124 static void _rl_complete_sigcleanup PARAMS((int, void *));
125
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));
133
134 static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int));
135
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));
145
146 static char *make_quoted_replacement PARAMS((char *, int, char *));
147
148 /* **************************************************************** */
149 /* */
150 /* Completion matching, from readline's point of view. */
151 /* */
152 /* **************************************************************** */
153
154 /* Variables known only to the readline library. */
155
156 /* If non-zero, non-unique completions always show the list of matches. */
157 int _rl_complete_show_all = 0;
158
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;
162
163 /* If non-zero, completed directory names have a slash appended. */
164 int _rl_complete_mark_directories = 1;
165
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;
171
172 /* If non-zero, completions are printed horizontally in alphabetical order,
173 like `ls -x'. */
174 int _rl_print_completions_horizontally;
175
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;
179 #else
180 int _rl_completion_case_fold = 0;
181 #endif
182
183 /* Non-zero means that `-' and `_' are equivalent when comparing filenames
184 for completion. */
185 int _rl_completion_case_map = 0;
186
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;
190
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;
196
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;
200
201 /* Global variables available to applications using readline. */
202
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 */
209
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;
214
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;
218 #endif
219
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;
225
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;
229
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;
234
235 rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
236
237 rl_icppfunc_t *rl_filename_stat_hook = (rl_icppfunc_t *)NULL;
238
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
247 converted. */
248 rl_dequote_func_t *rl_filename_rewrite_hook = (rl_dequote_func_t *)NULL;
249
250 /* Non-zero means readline completion functions perform tilde expansion. */
251 int rl_complete_with_tilde_expansion = 0;
252
253 /* Pointer to the generator function for completion_matches ().
254 NULL means to use rl_filename_completion_function (), the default filename
255 completer. */
256 rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL;
257
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;
261
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
265 of TEXT are.
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;
270
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;
274
275 /* Set to a character indicating the type of completion being performed
276 by rl_complete_internal, available for use by application completion
277 functions. */
278 int rl_completion_type = 0;
279
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
283 don't ask. */
284 int rl_completion_query_items = 100;
285
286 int _rl_page_completions = 1;
287
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\"\\'`@$><=;|&{("; /* }) */
292
293 /* List of basic quoting characters. */
294 const char *rl_basic_quote_characters = "\"'";
295
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;
300
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;
305
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;
311
312 /* List of characters that should be quoted in filenames by the completer. */
313 const char *rl_filename_quote_characters = (const char *)NULL;
314
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;
319
320 /* If non-zero, then disallow duplicates in the matches. */
321 int rl_ignore_completion_duplicates = 1;
322
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;
327
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;
334
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;
345
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
349 reset if desired. */
350 rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename;
351
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;
357
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
360 completer. */
361 rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL;
362
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;
367
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 = ' ';
371
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;
376
377 /* Set to any quote character readline thinks it finds before any application
378 completion function is called. */
379 int rl_completion_quote_character;
380
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;
384
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;
395
396 /* If non-zero, inhibit completion (temporarily). */
397 int rl_inhibit_completion;
398
399 /* Set to the last key used to invoke one of the completion functions */
400 int rl_completion_invoking_key;
401
402 /* If non-zero, sort the completion matches. On by default. */
403 int rl_sort_completion_matches = 1;
404
405 /* Variables local to this file. */
406
407 /* Local variable states what happened during the last completion attempt. */
408 static int completion_changed_buffer;
409
410 /* The result of the query to the user about displaying completion matches */
411 static int completion_y_or_n;
412
413 static int _rl_complete_display_matches_interrupt = 0;
414
415 /*************************************/
416 /* */
417 /* Bindable completion functions */
418 /* */
419 /*************************************/
420
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. */
424 int
425 rl_complete (ignore, invoking_key)
426 int ignore, invoking_key;
427 {
428 rl_completion_invoking_key = invoking_key;
429
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 ('@'));
438 else
439 return (rl_complete_internal (TAB));
440 }
441
442 /* List the possible completions. See description of rl_complete (). */
443 int
444 rl_possible_completions (ignore, invoking_key)
445 int ignore, invoking_key;
446 {
447 rl_completion_invoking_key = invoking_key;
448 return (rl_complete_internal ('?'));
449 }
450
451 int
452 rl_insert_completions (ignore, invoking_key)
453 int ignore, invoking_key;
454 {
455 rl_completion_invoking_key = invoking_key;
456 return (rl_complete_internal ('*'));
457 }
458
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. */
464 int
465 rl_completion_mode (cfunc)
466 rl_command_func_t *cfunc;
467 {
468 if (rl_last_func == cfunc && !completion_changed_buffer)
469 return '?';
470 else if (_rl_complete_show_all)
471 return '!';
472 else if (_rl_complete_show_unmodified)
473 return '@';
474 else
475 return TAB;
476 }
477
478 /************************************/
479 /* */
480 /* Completion utility functions */
481 /* */
482 /************************************/
483
484 /* Reset readline state on a signal or other event. */
485 void
486 _rl_reset_completion_state ()
487 {
488 rl_completion_found_quote = 0;
489 rl_completion_quote_character = 0;
490 }
491
492 static void
493 _rl_complete_sigcleanup (sig, ptr)
494 int sig;
495 void *ptr;
496 {
497 if (sig == SIGINT) /* XXX - for now */
498 {
499 _rl_free_match_list ((char **)ptr);
500 _rl_complete_display_matches_interrupt = 1;
501 }
502 }
503
504 /* Set default values for readline word completion. These are the variables
505 that application completion functions can change or inspect. */
506 static void
507 set_completion_defaults (what_to_do)
508 int what_to_do;
509 {
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 = ' ';
516
517 /* The completion entry function may optionally change this. */
518 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
519
520 /* Reset private state. */
521 _rl_complete_display_matches_interrupt = 0;
522 }
523
524 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
525 static int
526 get_y_or_n (for_pager)
527 int for_pager;
528 {
529 int c;
530
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 */
534 #if 0
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))
540 return 1;
541 #endif
542 #endif
543
544 for (;;)
545 {
546 RL_SETSTATE(RL_STATE_MOREINPUT);
547 c = rl_read_key ();
548 RL_UNSETSTATE(RL_STATE_MOREINPUT);
549
550 if (c == 'y' || c == 'Y' || c == ' ')
551 return (1);
552 if (c == 'n' || c == 'N' || c == RUBOUT)
553 return (0);
554 if (c == ABORT_CHAR || c < 0)
555 _rl_abort_internal ();
556 if (for_pager && (c == NEWLINE || c == RETURN))
557 return (2);
558 if (for_pager && (c == 'q' || c == 'Q'))
559 return (0);
560 rl_ding ();
561 }
562 }
563
564 static int
565 _rl_internal_pager (lines)
566 int lines;
567 {
568 int i;
569
570 fprintf (rl_outstream, "--More--");
571 fflush (rl_outstream);
572 i = get_y_or_n (1);
573 _rl_erase_entire_line ();
574 if (i == 0)
575 return -1;
576 else if (i == 2)
577 return (lines - 1);
578 else
579 return 0;
580 }
581
582 static int
583 path_isdir (filename)
584 const char *filename;
585 {
586 struct stat finfo;
587
588 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
589 }
590
591 #if defined (VISIBLE_STATS)
592 /* Return the character which best describes FILENAME.
593 `@' for symbolic links
594 `/' for directories
595 `*' for executables
596 `=' for sockets
597 `|' for FIFOs
598 `%' for character special devices
599 `#' for block special devices */
600 static int
601 stat_char (filename)
602 char *filename;
603 {
604 struct stat finfo;
605 int character, r;
606 char *f;
607 const char *fn;
608
609 /* Short-circuit a //server on cygwin, since that will always behave as
610 a directory. */
611 #if __CYGWIN__
612 if (filename[0] == '/' && filename[1] == '/' && strchr (filename+2, '/') == 0)
613 return '/';
614 #endif
615
616 f = 0;
617 if (rl_filename_stat_hook)
618 {
619 f = savestring (filename);
620 (*rl_filename_stat_hook) (&f);
621 fn = f;
622 }
623 else
624 fn = filename;
625
626 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
627 r = lstat (fn, &finfo);
628 #else
629 r = stat (fn, &finfo);
630 #endif
631
632 if (r == -1)
633 return (0);
634
635 character = 0;
636 if (S_ISDIR (finfo.st_mode))
637 character = '/';
638 #if defined (S_ISCHR)
639 else if (S_ISCHR (finfo.st_mode))
640 character = '%';
641 #endif /* S_ISCHR */
642 #if defined (S_ISBLK)
643 else if (S_ISBLK (finfo.st_mode))
644 character = '#';
645 #endif /* S_ISBLK */
646 #if defined (S_ISLNK)
647 else if (S_ISLNK (finfo.st_mode))
648 character = '@';
649 #endif /* S_ISLNK */
650 #if defined (S_ISSOCK)
651 else if (S_ISSOCK (finfo.st_mode))
652 character = '=';
653 #endif /* S_ISSOCK */
654 #if defined (S_ISFIFO)
655 else if (S_ISFIFO (finfo.st_mode))
656 character = '|';
657 #endif
658 else if (S_ISREG (finfo.st_mode))
659 {
660 #if defined (_WIN32) && !defined (__CYGWIN__)
661 char *ext;
662
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))
669 character = '*';
670 #else
671 if (access (filename, X_OK) == 0)
672 character = '*';
673 #endif
674 }
675
676 xfree (f);
677 return (character);
678 }
679 #endif /* VISIBLE_STATS */
680
681 #if defined (COLOR_SUPPORT)
682 static int
683 colored_stat_start (filename)
684 const char *filename;
685 {
686 _rl_set_normal_color ();
687 return (_rl_print_color_indicator (filename));
688 }
689
690 static void
691 colored_stat_end ()
692 {
693 _rl_prep_non_filename_text ();
694 _rl_put_indicator (&_rl_color_indicator[C_CLR_TO_EOL]);
695 }
696
697 static int
698 colored_prefix_start ()
699 {
700 _rl_set_normal_color ();
701 return (_rl_print_prefix_color ());
702 }
703
704 static void
705 colored_prefix_end ()
706 {
707 colored_stat_end (); /* for now */
708 }
709 #endif
710
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. */
719 static char *
720 printable_part (pathname)
721 char *pathname;
722 {
723 char *temp, *x;
724
725 if (rl_filename_completion_desired == 0) /* don't need to do anything */
726 return (pathname);
727
728 temp = strrchr (pathname, '/');
729 #if defined (__MSDOS__) || defined (_WIN32)
730 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
731 temp = pathname + 1;
732 #endif
733
734 if (temp == 0 || *temp == '\0')
735 return (pathname);
736 else if (temp[1] == 0 && temp == pathname)
737 return (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')
743 {
744 for (x = temp - 1; x > pathname; x--)
745 if (*x == '/')
746 break;
747 return ((*x == '/') ? x + 1 : pathname);
748 }
749 else
750 return ++temp;
751 }
752
753 /* Compute width of STRING when displayed on screen by print_filename */
754 static int
755 fnwidth (string)
756 const char *string;
757 {
758 int width, pos;
759 #if defined (HANDLE_MULTIBYTE)
760 mbstate_t ps;
761 int left, w;
762 size_t clen;
763 wchar_t wc;
764
765 left = strlen (string) + 1;
766 memset (&ps, 0, sizeof (mbstate_t));
767 #endif
768
769 width = pos = 0;
770 while (string[pos])
771 {
772 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
773 {
774 width += 2;
775 pos++;
776 }
777 else
778 {
779 #if defined (HANDLE_MULTIBYTE)
780 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
781 if (MB_INVALIDCH (clen))
782 {
783 width++;
784 pos++;
785 memset (&ps, 0, sizeof (mbstate_t));
786 }
787 else if (MB_NULLWCH (clen))
788 break;
789 else
790 {
791 pos += clen;
792 w = WCWIDTH (wc);
793 width += (w >= 0) ? w : 1;
794 }
795 #else
796 width++;
797 pos++;
798 #endif
799 }
800 }
801
802 return width;
803 }
804
805 #define ELLIPSIS_LEN 3
806
807 static int
808 fnprint (to_print, prefix_bytes, real_pathname)
809 const char *to_print;
810 int prefix_bytes;
811 const char *real_pathname;
812 {
813 int printed_len, w;
814 const char *s;
815 int common_prefix_len, print_len;
816 #if defined (HANDLE_MULTIBYTE)
817 mbstate_t ps;
818 const char *end;
819 size_t tlen;
820 int width;
821 wchar_t wc;
822
823 print_len = strlen (to_print);
824 end = to_print + print_len + 1;
825 memset (&ps, 0, sizeof (mbstate_t));
826 #endif
827
828 printed_len = common_prefix_len = 0;
829
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)
835 prefix_bytes = 0;
836
837 #if defined (COLOR_SUPPORT)
838 if (_rl_colored_stats && (prefix_bytes == 0 || _rl_colored_completion_prefix <= 0))
839 colored_stat_start (real_pathname);
840 #endif
841
842 if (prefix_bytes && _rl_completion_prefix_display_length > 0)
843 {
844 char ellipsis;
845
846 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
847 for (w = 0; w < ELLIPSIS_LEN; w++)
848 putc (ellipsis, rl_outstream);
849 printed_len = ELLIPSIS_LEN;
850 }
851 #if defined (COLOR_SUPPORT)
852 else if (prefix_bytes && _rl_colored_completion_prefix > 0)
853 {
854 common_prefix_len = prefix_bytes;
855 prefix_bytes = 0;
856 /* XXX - print color indicator start here */
857 colored_prefix_start ();
858 }
859 #endif
860
861 s = to_print + prefix_bytes;
862 while (*s)
863 {
864 if (CTRL_CHAR (*s))
865 {
866 putc ('^', rl_outstream);
867 putc (UNCTRL (*s), rl_outstream);
868 printed_len += 2;
869 s++;
870 #if defined (HANDLE_MULTIBYTE)
871 memset (&ps, 0, sizeof (mbstate_t));
872 #endif
873 }
874 else if (*s == RUBOUT)
875 {
876 putc ('^', rl_outstream);
877 putc ('?', rl_outstream);
878 printed_len += 2;
879 s++;
880 #if defined (HANDLE_MULTIBYTE)
881 memset (&ps, 0, sizeof (mbstate_t));
882 #endif
883 }
884 else
885 {
886 #if defined (HANDLE_MULTIBYTE)
887 tlen = mbrtowc (&wc, s, end - s, &ps);
888 if (MB_INVALIDCH (tlen))
889 {
890 tlen = 1;
891 width = 1;
892 memset (&ps, 0, sizeof (mbstate_t));
893 }
894 else if (MB_NULLWCH (tlen))
895 break;
896 else
897 {
898 w = WCWIDTH (wc);
899 width = (w >= 0) ? w : 1;
900 }
901 fwrite (s, 1, tlen, rl_outstream);
902 s += tlen;
903 printed_len += width;
904 #else
905 putc (*s, rl_outstream);
906 s++;
907 printed_len++;
908 #endif
909 }
910 if (common_prefix_len > 0 && (s - to_print) >= common_prefix_len)
911 {
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 */
918 #endif
919 common_prefix_len = 0;
920 }
921 }
922
923 #if defined (COLOR_SUPPORT)
924 /* XXX - unconditional for now */
925 if (_rl_colored_stats)
926 colored_stat_end ();
927 #endif
928
929 return printed_len;
930 }
931
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. */
935
936 static int
937 print_filename (to_print, full_pathname, prefix_bytes)
938 char *to_print, *full_pathname;
939 int prefix_bytes;
940 {
941 int printed_len, extension_char, slen, tlen;
942 char *s, c, *new_full_pathname, *dn;
943
944 extension_char = 0;
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)
948 #endif
949 printed_len = fnprint (to_print, prefix_bytes, to_print);
950
951 if (rl_filename_completion_desired && (
952 #if defined (VISIBLE_STATS)
953 rl_visible_stats ||
954 #endif
955 #if defined (COLOR_SUPPORT)
956 _rl_colored_stats ||
957 #endif
958 _rl_complete_mark_directories))
959 {
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)
964 {
965 /* Terminate the directory name. */
966 c = to_print[-1];
967 to_print[-1] = '\0';
968
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)
975 dn = "/";
976 else if (full_pathname[0] != '/')
977 dn = full_pathname;
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 // */
982 else
983 dn = full_pathname;
984 s = tilde_expand (dn);
985 if (rl_directory_completion_hook)
986 (*rl_directory_completion_hook) (&s);
987
988 slen = strlen (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] == '/')
993 slen--;
994 else
995 new_full_pathname[slen] = '/';
996 new_full_pathname[slen] = '/';
997 strcpy (new_full_pathname + slen + 1, to_print);
998
999 #if defined (VISIBLE_STATS)
1000 if (rl_visible_stats)
1001 extension_char = stat_char (new_full_pathname);
1002 else
1003 #endif
1004 if (_rl_complete_mark_directories)
1005 {
1006 dn = 0;
1007 if (rl_directory_completion_hook == 0 && rl_filename_stat_hook)
1008 {
1009 dn = savestring (new_full_pathname);
1010 (*rl_filename_stat_hook) (&dn);
1011 xfree (new_full_pathname);
1012 new_full_pathname = dn;
1013 }
1014 if (path_isdir (new_full_pathname))
1015 extension_char = '/';
1016 }
1017
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);
1022 #endif
1023
1024 xfree (new_full_pathname);
1025 to_print[-1] = c;
1026 }
1027 else
1028 {
1029 s = tilde_expand (full_pathname);
1030 #if defined (VISIBLE_STATS)
1031 if (rl_visible_stats)
1032 extension_char = stat_char (s);
1033 else
1034 #endif
1035 if (_rl_complete_mark_directories && path_isdir (s))
1036 extension_char = '/';
1037
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);
1042 #endif
1043 }
1044
1045 xfree (s);
1046 if (extension_char)
1047 {
1048 putc (extension_char, rl_outstream);
1049 printed_len++;
1050 }
1051 }
1052
1053 return printed_len;
1054 }
1055
1056 static char *
1057 rl_quote_filename (s, rtype, qcp)
1058 char *s;
1059 int rtype;
1060 char *qcp;
1061 {
1062 char *r;
1063
1064 r = (char *)xmalloc (strlen (s) + 2);
1065 *r = *rl_completer_quote_characters;
1066 strcpy (r + 1, s);
1067 if (qcp)
1068 *qcp = *rl_completer_quote_characters;
1069 return r;
1070 }
1071
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. */
1086
1087 char
1088 _rl_find_completion_word (fp, dp)
1089 int *fp, *dp;
1090 {
1091 int scan, end, found_quote, delimiter, pass_next, isbrk;
1092 char quote_char, *brkchars;
1093
1094 end = rl_point;
1095 found_quote = delimiter = 0;
1096 quote_char = '\0';
1097
1098 brkchars = 0;
1099 if (rl_completion_word_break_hook)
1100 brkchars = (*rl_completion_word_break_hook) ();
1101 if (brkchars == 0)
1102 brkchars = rl_completer_word_break_characters;
1103
1104 if (rl_completer_quote_characters)
1105 {
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))
1111 {
1112 if (pass_next)
1113 {
1114 pass_next = 0;
1115 continue;
1116 }
1117
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
1121 of quote_char. */
1122 if (quote_char != '\'' && rl_line_buffer[scan] == '\\')
1123 {
1124 pass_next = 1;
1125 found_quote |= RL_QF_BACKSLASH;
1126 continue;
1127 }
1128
1129 if (quote_char != '\0')
1130 {
1131 /* Ignore everything until the matching close quote char. */
1132 if (rl_line_buffer[scan] == quote_char)
1133 {
1134 /* Found matching close. Abandon this substring. */
1135 quote_char = '\0';
1136 rl_point = end;
1137 }
1138 }
1139 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
1140 {
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;
1149 else
1150 found_quote |= RL_QF_OTHER_QUOTE;
1151 }
1152 }
1153 }
1154
1155 if (rl_point == end && quote_char == '\0')
1156 {
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))
1161 {
1162 scan = rl_line_buffer[rl_point];
1163
1164 if (strchr (brkchars, scan) == 0)
1165 continue;
1166
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))
1171 continue;
1172
1173 /* Convoluted code, but it avoids an n^2 algorithm with calls
1174 to char_is_quoted. */
1175 break;
1176 }
1177 }
1178
1179 /* If we are at an unquoted word break, then advance past it. */
1180 scan = rl_line_buffer[rl_point];
1181
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. */
1187 if (scan)
1188 {
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;
1193 else
1194 isbrk = strchr (brkchars, scan) != 0;
1195
1196 if (isbrk)
1197 {
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)
1203 delimiter = scan;
1204
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)
1208 rl_point++;
1209 }
1210 }
1211
1212 if (fp)
1213 *fp = found_quote;
1214 if (dp)
1215 *dp = delimiter;
1216
1217 return (quote_char);
1218 }
1219
1220 static char **
1221 gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
1222 char *text;
1223 int start, end;
1224 rl_compentry_func_t *our_func;
1225 int found_quote, quote_char;
1226 {
1227 char **matches;
1228
1229 rl_completion_found_quote = found_quote;
1230 rl_completion_quote_character = quote_char;
1231
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)
1236 {
1237 matches = (*rl_attempted_completion_function) (text, start, end);
1238 if (RL_SIG_RECEIVED())
1239 {
1240 _rl_free_match_list (matches);
1241 matches = 0;
1242 RL_CHECK_SIGNALS ();
1243 }
1244
1245 if (matches || rl_attempted_completion_over)
1246 {
1247 rl_attempted_completion_over = 0;
1248 return (matches);
1249 }
1250 }
1251
1252 /* XXX -- filename dequoting moved into rl_filename_completion_function */
1253
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())
1258 {
1259 _rl_free_match_list (matches);
1260 matches = 0;
1261 RL_CHECK_SIGNALS ();
1262 }
1263 return matches;
1264 }
1265
1266 /* Filter out duplicates in MATCHES. This frees up the strings in
1267 MATCHES. */
1268 static char **
1269 remove_duplicate_matches (matches)
1270 char **matches;
1271 {
1272 char *lowest_common;
1273 int i, j, newlen;
1274 char dead_slot;
1275 char **temp_array;
1276
1277 /* Sort the items. */
1278 for (i = 0; matches[i]; i++)
1279 ;
1280
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);
1285
1286 /* Remember the lowest common denominator for it may be unique. */
1287 lowest_common = savestring (matches[0]);
1288
1289 for (i = newlen = 0; matches[i + 1]; i++)
1290 {
1291 if (strcmp (matches[i], matches[i + 1]) == 0)
1292 {
1293 xfree (matches[i]);
1294 matches[i] = (char *)&dead_slot;
1295 }
1296 else
1297 newlen++;
1298 }
1299
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++)
1304 {
1305 if (matches[i] != (char *)&dead_slot)
1306 temp_array[j++] = matches[i];
1307 }
1308 temp_array[j] = (char *)NULL;
1309
1310 if (matches[0] != (char *)&dead_slot)
1311 xfree (matches[0]);
1312
1313 /* Place the lowest common denominator back in [0]. */
1314 temp_array[0] = lowest_common;
1315
1316 /* If there is one string left, and it is identical to the
1317 lowest common denominator, then the LCD is the string to
1318 insert. */
1319 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
1320 {
1321 xfree (temp_array[1]);
1322 temp_array[1] = (char *)NULL;
1323 }
1324 return (temp_array);
1325 }
1326
1327 /* Find the common prefix of the list of matches, and put it into
1328 matches[0]. */
1329 static int
1330 compute_lcd_of_matches (match_list, matches, text)
1331 char **match_list;
1332 int matches;
1333 const char *text;
1334 {
1335 register int i, c1, c2, si;
1336 int low; /* Count of max-matched characters. */
1337 int lx;
1338 char *dtext; /* dequoted TEXT, if needed */
1339 #if defined (HANDLE_MULTIBYTE)
1340 int v;
1341 size_t v1, v2;
1342 mbstate_t ps1, ps2;
1343 wchar_t wc1, wc2;
1344 #endif
1345
1346 /* If only one match, just use that. Otherwise, compare each
1347 member of the list with the next, finding out where they
1348 stop matching. */
1349 if (matches == 1)
1350 {
1351 match_list[0] = match_list[1];
1352 match_list[1] = (char *)NULL;
1353 return 1;
1354 }
1355
1356 for (i = 1, low = 100000; i < matches; i++)
1357 {
1358 #if defined (HANDLE_MULTIBYTE)
1359 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1360 {
1361 memset (&ps1, 0, sizeof (mbstate_t));
1362 memset (&ps2, 0, sizeof (mbstate_t));
1363 }
1364 #endif
1365 if (_rl_completion_case_fold)
1366 {
1367 for (si = 0;
1368 (c1 = _rl_to_lower(match_list[i][si])) &&
1369 (c2 = _rl_to_lower(match_list[i + 1][si]));
1370 si++)
1371 #if defined (HANDLE_MULTIBYTE)
1372 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1373 {
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))
1377 {
1378 if (c1 != c2) /* do byte comparison */
1379 break;
1380 continue;
1381 }
1382 wc1 = towlower (wc1);
1383 wc2 = towlower (wc2);
1384 if (wc1 != wc2)
1385 break;
1386 else if (v1 > 1)
1387 si += v1 - 1;
1388 }
1389 else
1390 #endif
1391 if (c1 != c2)
1392 break;
1393 }
1394 else
1395 {
1396 for (si = 0;
1397 (c1 = match_list[i][si]) &&
1398 (c2 = match_list[i + 1][si]);
1399 si++)
1400 #if defined (HANDLE_MULTIBYTE)
1401 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1402 {
1403 mbstate_t ps_back;
1404 ps_back = ps1;
1405 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2))
1406 break;
1407 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1)
1408 si += v - 1;
1409 }
1410 else
1411 #endif
1412 if (c1 != c2)
1413 break;
1414 }
1415
1416 if (low > si)
1417 low = si;
1418 }
1419
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)
1424 {
1425 match_list[0] = (char *)xmalloc (strlen (text) + 1);
1426 strcpy (match_list[0], text);
1427 }
1428 else
1429 {
1430 match_list[0] = (char *)xmalloc (low + 1);
1431
1432 /* XXX - this might need changes in the presence of multibyte chars */
1433
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)
1437 {
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
1443 THEN
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
1447 FI */
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)
1453 {
1454 dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
1455 text = dtext;
1456 }
1457
1458 /* sort the list to get consistent answers. */
1459 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1460
1461 si = strlen (text);
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)
1469 {
1470 strncpy (match_list[0], match_list[i], low);
1471 break;
1472 }
1473 /* no casematch, use first entry */
1474 if (i > matches)
1475 strncpy (match_list[0], match_list[1], low);
1476
1477 FREE (dtext);
1478 }
1479 else
1480 strncpy (match_list[0], match_list[1], low);
1481
1482 match_list[0][low] = '\0';
1483 }
1484
1485 return matches;
1486 }
1487
1488 static int
1489 postprocess_matches (matchesp, matching_filenames)
1490 char ***matchesp;
1491 int matching_filenames;
1492 {
1493 char *t, **matches, **temp_matches;
1494 int nmatch, i;
1495
1496 matches = *matchesp;
1497
1498 if (matches == 0)
1499 return 0;
1500
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)
1505 {
1506 temp_matches = remove_duplicate_matches (matches);
1507 xfree (matches);
1508 matches = temp_matches;
1509 }
1510
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)
1516 {
1517 for (nmatch = 1; matches[nmatch]; nmatch++)
1518 ;
1519 (void)(*rl_ignore_some_completions_function) (matches);
1520 if (matches == 0 || matches[0] == 0)
1521 {
1522 FREE (matches);
1523 *matchesp = (char **)0;
1524 return 0;
1525 }
1526 else
1527 {
1528 /* If we removed some matches, recompute the common prefix. */
1529 for (i = 1; matches[i]; i++)
1530 ;
1531 if (i > 1 && i < nmatch)
1532 {
1533 t = matches[0];
1534 compute_lcd_of_matches (matches, i - 1, t);
1535 FREE (t);
1536 }
1537 }
1538 }
1539
1540 *matchesp = matches;
1541 return (1);
1542 }
1543
1544 static int
1545 complete_get_screenwidth ()
1546 {
1547 int cols;
1548 char *envcols;
1549
1550 cols = _rl_completion_columns;
1551 if (cols >= 0 && cols <= _rl_screenwidth)
1552 return cols;
1553 envcols = getenv ("COLUMNS");
1554 if (envcols && *envcols)
1555 cols = atoi (envcols);
1556 if (cols >= 0 && cols <= _rl_screenwidth)
1557 return cols;
1558 return _rl_screenwidth;
1559 }
1560
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. */
1565 void
1566 rl_display_match_list (matches, len, max)
1567 char **matches;
1568 int len, max;
1569 {
1570 int count, limit, printed_len, lines, cols;
1571 int i, j, k, l, common_length, sind;
1572 char *temp, *t;
1573
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)
1578 {
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;
1586
1587 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
1588 max -= common_length - ELLIPSIS_LEN;
1589 else
1590 common_length = sind = 0;
1591 }
1592 #if defined (COLOR_SUPPORT)
1593 else if (_rl_colored_completion_prefix > 0)
1594 {
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;
1601 }
1602 #endif
1603
1604 /* How many items of MAX length can we fit in the screen window? */
1605 cols = complete_get_screenwidth ();
1606 max += 2;
1607 limit = cols / max;
1608 if (limit != 1 && (limit * max == cols))
1609 limit--;
1610
1611 /* If cols == 0, limit will end up -1 */
1612 if (cols < _rl_screenwidth && limit < 0)
1613 limit = 1;
1614
1615 /* Avoid a possible floating exception. If max > cols,
1616 limit will be 0 and a divide-by-zero fault will result. */
1617 if (limit == 0)
1618 limit = 1;
1619
1620 /* How many iterations of the printing loop? */
1621 count = (len + (limit - 1)) / limit;
1622
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. */
1626
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);
1630
1631 rl_crlf ();
1632
1633 lines = 0;
1634 if (_rl_print_completions_horizontally == 0)
1635 {
1636 /* Print the sorted items, up-and-down alphabetically, like ls. */
1637 for (i = 1; i <= count; i++)
1638 {
1639 for (j = 0, l = i; j < limit; j++)
1640 {
1641 if (l > len || matches[l] == 0)
1642 break;
1643 else
1644 {
1645 temp = printable_part (matches[l]);
1646 printed_len = print_filename (temp, matches[l], sind);
1647
1648 if (j + 1 < limit)
1649 {
1650 if (max <= printed_len)
1651 putc (' ', rl_outstream);
1652 else
1653 for (k = 0; k < max - printed_len; k++)
1654 putc (' ', rl_outstream);
1655 }
1656 }
1657 l += count;
1658 }
1659 rl_crlf ();
1660 #if defined (SIGWINCH)
1661 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1662 #else
1663 if (RL_SIG_RECEIVED ())
1664 #endif
1665 return;
1666 lines++;
1667 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count)
1668 {
1669 lines = _rl_internal_pager (lines);
1670 if (lines < 0)
1671 return;
1672 }
1673 }
1674 }
1675 else
1676 {
1677 /* Print the sorted items, across alphabetically, like ls -x. */
1678 for (i = 1; matches[i]; i++)
1679 {
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)
1685 #else
1686 if (RL_SIG_RECEIVED ())
1687 #endif
1688 return;
1689 if (matches[i+1])
1690 {
1691 if (limit == 1 || (i && (limit > 1) && (i % limit) == 0))
1692 {
1693 rl_crlf ();
1694 lines++;
1695 if (_rl_page_completions && lines >= _rl_screenheight - 1)
1696 {
1697 lines = _rl_internal_pager (lines);
1698 if (lines < 0)
1699 return;
1700 }
1701 }
1702 else if (max <= printed_len)
1703 putc (' ', rl_outstream);
1704 else
1705 for (k = 0; k < max - printed_len; k++)
1706 putc (' ', rl_outstream);
1707 }
1708 }
1709 rl_crlf ();
1710 }
1711 }
1712
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. */
1724 static void
1725 display_matches (matches)
1726 char **matches;
1727 {
1728 int len, max, i;
1729 char *temp;
1730
1731 /* Move to the last visible line of a possibly-multiple-line command. */
1732 _rl_move_vert (_rl_vis_botlin);
1733
1734 /* Handle simple case first. What if there is only one answer? */
1735 if (matches[1] == 0)
1736 {
1737 temp = printable_part (matches[0]);
1738 rl_crlf ();
1739 print_filename (temp, matches[0], 0);
1740 rl_crlf ();
1741
1742 rl_forced_update_display ();
1743 rl_display_fixed = 1;
1744
1745 return;
1746 }
1747
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++)
1751 {
1752 temp = printable_part (matches[i]);
1753 len = fnwidth (temp);
1754
1755 if (len > max)
1756 max = len;
1757 }
1758
1759 len = i - 1;
1760
1761 /* If the caller has defined a display hook, then call that now. */
1762 if (rl_completion_display_matches_hook)
1763 {
1764 (*rl_completion_display_matches_hook) (matches, len, max);
1765 return;
1766 }
1767
1768 /* If there are many items, then ask the user if she really wants to
1769 see them all. */
1770 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
1771 {
1772 rl_crlf ();
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)
1776 {
1777 rl_crlf ();
1778
1779 rl_forced_update_display ();
1780 rl_display_fixed = 1;
1781
1782 return;
1783 }
1784 }
1785
1786 rl_display_match_list (matches, len, max);
1787
1788 rl_forced_update_display ();
1789 rl_display_fixed = 1;
1790 }
1791
1792 static char *
1793 make_quoted_replacement (match, mtype, qc)
1794 char *match;
1795 int mtype;
1796 char *qc; /* Pointer to quoting character, if any */
1797 {
1798 int should_quote, do_replace;
1799 char *replacement;
1800
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;
1810
1811 should_quote = match && rl_completer_quote_characters &&
1812 rl_filename_completion_desired &&
1813 rl_filename_quoting_desired;
1814
1815 if (should_quote)
1816 should_quote = should_quote && (!qc || !*qc ||
1817 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
1818
1819 if (should_quote)
1820 {
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)
1826 : 0;
1827
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);
1833 }
1834 return (replacement);
1835 }
1836
1837 static void
1838 insert_match (match, start, mtype, qc)
1839 char *match;
1840 int start, mtype;
1841 char *qc;
1842 {
1843 char *replacement, *r;
1844 char oqc;
1845 int end, rlen;
1846
1847 oqc = qc ? *qc : '\0';
1848 replacement = make_quoted_replacement (match, mtype, qc);
1849
1850 /* Now insert the match. */
1851 if (replacement)
1852 {
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)
1857 start--;
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)
1862 start--;
1863 end = rl_point - 1;
1864 /* Don't double a closing quote character */
1865 if (qc && *qc && end && rl_line_buffer[rl_point] == *qc && replacement[rlen - 1] == *qc)
1866 end++;
1867 if (_rl_skip_completed_text)
1868 {
1869 r = replacement;
1870 while (start < rl_end && *r && rl_line_buffer[start] == *r)
1871 {
1872 start++;
1873 r++;
1874 }
1875 if (start <= end || *r)
1876 _rl_replace_text (r, start, end);
1877 rl_point = start + strlen (r);
1878 }
1879 else
1880 _rl_replace_text (replacement, start, end);
1881 if (replacement != match)
1882 xfree (replacement);
1883 }
1884 }
1885
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). */
1897 static int
1898 append_to_match (text, delimiter, quote_char, nontrivial_match)
1899 char *text;
1900 int delimiter, quote_char, nontrivial_match;
1901 {
1902 char temp_string[4], *filename, *fn;
1903 int temp_string_index, s;
1904 struct stat finfo;
1905
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;
1910
1911 if (delimiter)
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;
1915
1916 temp_string[temp_string_index++] = '\0';
1917
1918 if (rl_filename_completion_desired)
1919 {
1920 filename = tilde_expand (text);
1921 if (rl_filename_stat_hook)
1922 {
1923 fn = savestring (filename);
1924 (*rl_filename_stat_hook) (&fn);
1925 xfree (filename);
1926 filename = fn;
1927 }
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))
1932 {
1933 if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */)
1934 {
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
1937 slash. */
1938 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1939 ;
1940 else if (rl_line_buffer[rl_point] != '/')
1941 rl_insert_text ("/");
1942 }
1943 }
1944 #ifdef S_ISLNK
1945 /* Don't add anything if the filename is a symlink and resolves to a
1946 directory. */
1947 else if (s == 0 && S_ISLNK (finfo.st_mode) && path_isdir (filename))
1948 ;
1949 #endif
1950 else
1951 {
1952 if (rl_point == rl_end && temp_string_index)
1953 rl_insert_text (temp_string);
1954 }
1955 xfree (filename);
1956 }
1957 else
1958 {
1959 if (rl_point == rl_end && temp_string_index)
1960 rl_insert_text (temp_string);
1961 }
1962
1963 return (temp_string_index);
1964 }
1965
1966 static void
1967 insert_all_matches (matches, point, qc)
1968 char **matches;
1969 int point;
1970 char *qc;
1971 {
1972 int i;
1973 char *rp;
1974
1975 rl_begin_undo_group ();
1976 /* remove any opening quote character; make_quoted_replacement will add
1977 it back. */
1978 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
1979 point--;
1980 rl_delete_text (point, rl_point);
1981 rl_point = point;
1982
1983 if (matches[1])
1984 {
1985 for (i = 1; matches[i]; i++)
1986 {
1987 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
1988 rl_insert_text (rp);
1989 rl_insert_text (" ");
1990 if (rp != matches[i])
1991 xfree (rp);
1992 }
1993 }
1994 else
1995 {
1996 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
1997 rl_insert_text (rp);
1998 rl_insert_text (" ");
1999 if (rp != matches[0])
2000 xfree (rp);
2001 }
2002 rl_end_undo_group ();
2003 }
2004
2005 void
2006 _rl_free_match_list (matches)
2007 char **matches;
2008 {
2009 register int i;
2010
2011 if (matches == 0)
2012 return;
2013
2014 for (i = 0; matches[i]; i++)
2015 xfree (matches[i]);
2016 xfree (matches);
2017 }
2018
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. */
2028 int
2029 rl_complete_internal (what_to_do)
2030 int what_to_do;
2031 {
2032 char **matches;
2033 rl_compentry_func_t *our_func;
2034 int start, end, delimiter, found_quote, i, nontrivial_lcd;
2035 char *text, *saved_line_buffer;
2036 char quote_char;
2037 #if 1
2038 int tlen, mlen;
2039 #endif
2040
2041 RL_SETSTATE(RL_STATE_COMPLETING);
2042
2043 set_completion_defaults (what_to_do);
2044
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. */
2050 end = rl_point;
2051 found_quote = delimiter = 0;
2052 quote_char = '\0';
2053
2054 if (rl_point)
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);
2058
2059 start = rl_point;
2060 rl_point = end;
2061
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
2065 being completed. */
2066 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
2067 if (what_to_do == '!' || what_to_do == '@')
2068 tlen = strlen (text);
2069 xfree (text);
2070
2071 if (matches == 0)
2072 {
2073 rl_ding ();
2074 FREE (saved_line_buffer);
2075 completion_changed_buffer = 0;
2076 RL_UNSETSTATE(RL_STATE_COMPLETING);
2077 _rl_reset_completion_state ();
2078 return (0);
2079 }
2080
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;
2085
2086 if (postprocess_matches (&matches, i) == 0)
2087 {
2088 rl_ding ();
2089 FREE (saved_line_buffer);
2090 completion_changed_buffer = 0;
2091 RL_UNSETSTATE(RL_STATE_COMPLETING);
2092 _rl_reset_completion_state ();
2093 return (0);
2094 }
2095
2096 switch (what_to_do)
2097 {
2098 case TAB:
2099 case '!':
2100 case '@':
2101 /* Insert the first match with proper quoting. */
2102 if (what_to_do == TAB)
2103 {
2104 if (*matches[0])
2105 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2106 }
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, &quote_char);
2110 else if (*matches[0]) /* what_to_do != TAB && multiple matches */
2111 {
2112 mlen = *matches[0] ? strlen (matches[0]) : 0;
2113 if (mlen >= tlen)
2114 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2115 }
2116
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. */
2125 if (matches[1])
2126 {
2127 if (what_to_do == '!')
2128 {
2129 display_matches (matches);
2130 break;
2131 }
2132 else if (what_to_do == '@')
2133 {
2134 if (nontrivial_lcd == 0)
2135 display_matches (matches);
2136 break;
2137 }
2138 else if (rl_editing_mode != vi_mode)
2139 rl_ding (); /* There are other matches remaining. */
2140 }
2141 else
2142 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2143
2144 break;
2145
2146 case '*':
2147 insert_all_matches (matches, start, &quote_char);
2148 break;
2149
2150 case '?':
2151 if (rl_completion_display_matches_hook == 0)
2152 {
2153 _rl_sigcleanup = _rl_complete_sigcleanup;
2154 _rl_sigcleanarg = matches;
2155 _rl_complete_display_matches_interrupt = 0;
2156 }
2157 display_matches (matches);
2158 if (_rl_complete_display_matches_interrupt)
2159 {
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 */
2164 }
2165 _rl_sigcleanup = 0;
2166 _rl_sigcleanarg = 0;
2167 break;
2168
2169 default:
2170 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do);
2171 rl_ding ();
2172 FREE (saved_line_buffer);
2173 RL_UNSETSTATE(RL_STATE_COMPLETING);
2174 _rl_free_match_list (matches);
2175 _rl_reset_completion_state ();
2176 return 1;
2177 }
2178
2179 _rl_free_match_list (matches);
2180
2181 /* Check to see if the line has changed through all of this manipulation. */
2182 if (saved_line_buffer)
2183 {
2184 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
2185 xfree (saved_line_buffer);
2186 }
2187
2188 RL_UNSETSTATE(RL_STATE_COMPLETING);
2189 _rl_reset_completion_state ();
2190
2191 RL_CHECK_SIGNALS ();
2192 return 0;
2193 }
2194
2195 /***************************************************************/
2196 /* */
2197 /* Application-callable completion match generator functions */
2198 /* */
2199 /***************************************************************/
2200
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.
2206
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.
2212 */
2213 char **
2214 rl_completion_matches (text, entry_function)
2215 const char *text;
2216 rl_compentry_func_t *entry_function;
2217 {
2218 register int i;
2219
2220 /* Number of slots in match_list. */
2221 int match_list_size;
2222
2223 /* The list of matches. */
2224 char **match_list;
2225
2226 /* Number of matches actually found. */
2227 int matches;
2228
2229 /* Temporary string binder. */
2230 char *string;
2231
2232 matches = 0;
2233 match_list_size = 10;
2234 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
2235 match_list[1] = (char *)NULL;
2236
2237 while (string = (*entry_function) (text, matches))
2238 {
2239 if (RL_SIG_RECEIVED ())
2240 {
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)
2246 {
2247 for (i = 1; match_list[i]; i++)
2248 xfree (match_list[i]);
2249 }
2250 xfree (match_list);
2251 match_list = 0;
2252 match_list_size = 0;
2253 matches = 0;
2254 RL_CHECK_SIGNALS ();
2255 }
2256
2257 if (matches + 1 >= match_list_size)
2258 match_list = (char **)xrealloc
2259 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
2260
2261 if (match_list == 0)
2262 return (match_list);
2263
2264 match_list[++matches] = string;
2265 match_list[matches + 1] = (char *)NULL;
2266 }
2267
2268 /* If there were any matches, then look through them finding out the
2269 lowest common denominator. That then becomes match_list[0]. */
2270 if (matches)
2271 compute_lcd_of_matches (match_list, matches, text);
2272 else /* There were no matches. */
2273 {
2274 xfree (match_list);
2275 match_list = (char **)NULL;
2276 }
2277 return (match_list);
2278 }
2279
2280 /* A completion function for usernames.
2281 TEXT contains a partial username preceded by a random
2282 character (usually `~'). */
2283 char *
2284 rl_username_completion_function (text, state)
2285 const char *text;
2286 int state;
2287 {
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;
2294 char *value;
2295
2296 if (state == 0)
2297 {
2298 FREE (username);
2299
2300 first_char = *text;
2301 first_char_loc = first_char == '~';
2302
2303 username = savestring (&text[first_char_loc]);
2304 namelen = strlen (username);
2305 #if defined (HAVE_GETPWENT)
2306 setpwent ();
2307 #endif
2308 }
2309
2310 #if defined (HAVE_GETPWENT)
2311 while (entry = getpwent ())
2312 {
2313 /* Null usernames should result in all users as possible completions. */
2314 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
2315 break;
2316 }
2317 #endif
2318
2319 if (entry == 0)
2320 {
2321 #if defined (HAVE_GETPWENT)
2322 endpwent ();
2323 #endif
2324 return ((char *)NULL);
2325 }
2326 else
2327 {
2328 value = (char *)xmalloc (2 + strlen (entry->pw_name));
2329
2330 *value = *text;
2331
2332 strcpy (value + first_char_loc, entry->pw_name);
2333
2334 if (first_char == '~')
2335 rl_filename_completion_desired = 1;
2336
2337 return (value);
2338 }
2339 #endif /* !__WIN32__ && !__OPENNT */
2340 }
2341
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. */
2347 static int
2348 complete_fncmp (convfn, convlen, filename, filename_len)
2349 const char *convfn;
2350 int convlen;
2351 const char *filename;
2352 int filename_len;
2353 {
2354 register char *s1, *s2;
2355 int d, len;
2356 #if defined (HANDLE_MULTIBYTE)
2357 size_t v1, v2;
2358 mbstate_t ps1, ps2;
2359 wchar_t wc1, wc2;
2360 #endif
2361
2362 #if defined (HANDLE_MULTIBYTE)
2363 memset (&ps1, 0, sizeof (mbstate_t));
2364 memset (&ps2, 0, sizeof (mbstate_t));
2365 #endif
2366
2367 if (filename_len == 0)
2368 return 1;
2369 if (convlen < filename_len)
2370 return 0;
2371
2372 len = filename_len;
2373 s1 = (char *)convfn;
2374 s2 = (char *)filename;
2375
2376 /* Otherwise, if these match up to the length of filename, then
2377 it is a match. */
2378 if (_rl_completion_case_fold && _rl_completion_case_map)
2379 {
2380 /* Case-insensitive comparison treating _ and - as equivalent */
2381 #if defined (HANDLE_MULTIBYTE)
2382 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2383 {
2384 do
2385 {
2386 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2387 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2388 if (v1 == 0 && v2 == 0)
2389 return 1;
2390 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2391 {
2392 if (*s1 != *s2) /* do byte comparison */
2393 return 0;
2394 else if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2395 return 0;
2396 s1++; s2++; len--;
2397 continue;
2398 }
2399 wc1 = towlower (wc1);
2400 wc2 = towlower (wc2);
2401 s1 += v1;
2402 s2 += v1;
2403 len -= v1;
2404 if ((wc1 == L'-' || wc1 == L'_') && (wc2 == L'-' || wc2 == L'_'))
2405 continue;
2406 if (wc1 != wc2)
2407 return 0;
2408 }
2409 while (len != 0);
2410 }
2411 else
2412 #endif
2413 {
2414 do
2415 {
2416 d = _rl_to_lower (*s1) - _rl_to_lower (*s2);
2417 /* *s1 == [-_] && *s2 == [-_] */
2418 if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2419 d = 0;
2420 if (d != 0)
2421 return 0;
2422 s1++; s2++; /* already checked convlen >= filename_len */
2423 }
2424 while (--len != 0);
2425 }
2426
2427 return 1;
2428 }
2429 else if (_rl_completion_case_fold)
2430 {
2431 #if defined (HANDLE_MULTIBYTE)
2432 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2433 {
2434 do
2435 {
2436 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2437 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2438 if (v1 == 0 && v2 == 0)
2439 return 1;
2440 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2441 {
2442 if (*s1 != *s2) /* do byte comparison */
2443 return 0;
2444 s1++; s2++; len--;
2445 continue;
2446 }
2447 wc1 = towlower (wc1);
2448 wc2 = towlower (wc2);
2449 if (wc1 != wc2)
2450 return 0;
2451 s1 += v1;
2452 s2 += v1;
2453 len -= v1;
2454 }
2455 while (len != 0);
2456 return 1;
2457 }
2458 else
2459 #endif
2460 if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) &&
2461 (convlen >= filename_len) &&
2462 (_rl_strnicmp (filename, convfn, filename_len) == 0))
2463 return 1;
2464 }
2465 else
2466 {
2467 if ((convfn[0] == filename[0]) &&
2468 (convlen >= filename_len) &&
2469 (strncmp (filename, convfn, filename_len) == 0))
2470 return 1;
2471 }
2472 return 0;
2473 }
2474
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. */
2479 char *
2480 rl_filename_completion_function (text, state)
2481 const char *text;
2482 int state;
2483 {
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;
2491 int tilde_dirname;
2492 struct dirent *entry;
2493
2494 /* If we don't have any state, then do some initialization. */
2495 if (state == 0)
2496 {
2497 /* If we were interrupted before closing the directory or reading
2498 all of its contents, close it. */
2499 if (directory)
2500 {
2501 closedir (directory);
2502 directory = (DIR *)NULL;
2503 }
2504 FREE (dirname);
2505 FREE (filename);
2506 FREE (users_dirname);
2507
2508 filename = savestring (text);
2509 if (*text == 0)
2510 text = ".";
2511 dirname = savestring (text);
2512
2513 temp = strrchr (dirname, '/');
2514
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, '/');
2519 #endif
2520
2521 if (temp)
2522 {
2523 strcpy (filename, ++temp);
2524 *temp = '\0';
2525 }
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] == ':')
2529 {
2530 strcpy (filename, dirname + 2);
2531 dirname[2] = '\0';
2532 }
2533 #endif
2534 else
2535 {
2536 dirname[0] = '.';
2537 dirname[1] = '\0';
2538 }
2539
2540 /* We aren't done yet. We also support the "~user" syntax. */
2541
2542 /* Save the version of the directory that the user typed, dequoting
2543 it if necessary. */
2544 if (rl_completion_found_quote && rl_filename_dequoting_function)
2545 users_dirname = (*rl_filename_dequoting_function) (dirname, rl_completion_quote_character);
2546 else
2547 users_dirname = savestring (dirname);
2548
2549 tilde_dirname = 0;
2550 if (*dirname == '~')
2551 {
2552 temp = tilde_expand (dirname);
2553 xfree (dirname);
2554 dirname = temp;
2555 tilde_dirname = 1;
2556 }
2557
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))
2570 {
2571 xfree (users_dirname);
2572 users_dirname = savestring (dirname);
2573 }
2574 else if (tilde_dirname == 0 && rl_completion_found_quote && rl_filename_dequoting_function)
2575 {
2576 /* delete single and double quotes */
2577 xfree (dirname);
2578 dirname = savestring (users_dirname);
2579 }
2580 directory = opendir (dirname);
2581
2582 /* Now dequote a non-null filename. FILENAME will not be NULL, but may
2583 be empty. */
2584 if (*filename && rl_completion_found_quote && rl_filename_dequoting_function)
2585 {
2586 /* delete single and double quotes */
2587 temp = (*rl_filename_dequoting_function) (filename, rl_completion_quote_character);
2588 xfree (filename);
2589 filename = temp;
2590 }
2591 filename_len = strlen (filename);
2592
2593 rl_filename_completion_desired = 1;
2594 }
2595
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 *** */
2601
2602 /* Now that we have some state, we can read the directory. */
2603
2604 entry = (struct dirent *)NULL;
2605 while (directory && (entry = readdir (directory)))
2606 {
2607 convfn = dentry = entry->d_name;
2608 convlen = dentlen = D_NAMLEN (entry);
2609
2610 if (rl_filename_rewrite_hook)
2611 {
2612 convfn = (*rl_filename_rewrite_hook) (dentry, dentlen);
2613 convlen = (convfn == dentry) ? dentlen : strlen (convfn);
2614 }
2615
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)
2620 {
2621 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (convfn))
2622 continue;
2623
2624 if (convfn[0] != '.' ||
2625 (convfn[1] && (convfn[1] != '.' || convfn[2])))
2626 break;
2627 }
2628 else
2629 {
2630 if (complete_fncmp (convfn, convlen, filename, filename_len))
2631 break;
2632 }
2633 }
2634
2635 if (entry == 0)
2636 {
2637 if (directory)
2638 {
2639 closedir (directory);
2640 directory = (DIR *)NULL;
2641 }
2642 if (dirname)
2643 {
2644 xfree (dirname);
2645 dirname = (char *)NULL;
2646 }
2647 if (filename)
2648 {
2649 xfree (filename);
2650 filename = (char *)NULL;
2651 }
2652 if (users_dirname)
2653 {
2654 xfree (users_dirname);
2655 users_dirname = (char *)NULL;
2656 }
2657
2658 return (char *)NULL;
2659 }
2660 else
2661 {
2662 /* dirname && (strcmp (dirname, ".") != 0) */
2663 if (dirname && (dirname[0] != '.' || dirname[1]))
2664 {
2665 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
2666 {
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] != '/')
2673 {
2674 temp[dirlen++] = '/';
2675 temp[dirlen] = '\0';
2676 }
2677 }
2678 else
2679 {
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++] = '/';
2686 }
2687
2688 strcpy (temp + dirlen, convfn);
2689 }
2690 else
2691 temp = savestring (convfn);
2692
2693 if (convfn != dentry)
2694 xfree (convfn);
2695
2696 return (temp);
2697 }
2698 }
2699
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. */
2709 int
2710 rl_old_menu_complete (count, invoking_key)
2711 int count, invoking_key;
2712 {
2713 rl_compentry_func_t *our_func;
2714 int matching_filenames, found_quote;
2715
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;
2723
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)
2727 {
2728 /* Clean up from previous call, if any. */
2729 FREE (orig_text);
2730 if (matches)
2731 _rl_free_match_list (matches);
2732
2733 match_list_index = match_list_size = 0;
2734 matches = (char **)NULL;
2735
2736 rl_completion_invoking_key = invoking_key;
2737
2738 RL_SETSTATE(RL_STATE_COMPLETING);
2739
2740 /* Only the completion entry function can change these. */
2741 set_completion_defaults ('%');
2742
2743 our_func = rl_menu_completion_entry_function;
2744 if (our_func == 0)
2745 our_func = rl_completion_entry_function
2746 ? rl_completion_entry_function
2747 : rl_filename_completion_function;
2748
2749 /* We now look backwards for the start of a filename/variable word. */
2750 orig_end = rl_point;
2751 found_quote = delimiter = 0;
2752 quote_char = '\0';
2753
2754 if (rl_point)
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);
2758
2759 orig_start = rl_point;
2760 rl_point = orig_end;
2761
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);
2765
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;
2770
2771 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2772 {
2773 rl_ding ();
2774 FREE (matches);
2775 matches = (char **)0;
2776 FREE (orig_text);
2777 orig_text = (char *)0;
2778 completion_changed_buffer = 0;
2779 RL_UNSETSTATE(RL_STATE_COMPLETING);
2780 return (0);
2781 }
2782
2783 RL_UNSETSTATE(RL_STATE_COMPLETING);
2784
2785 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2786 ;
2787 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2788 code below should take care of it. */
2789
2790 if (match_list_size > 1 && _rl_complete_show_all)
2791 display_matches (matches);
2792 }
2793
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. */
2797
2798 if (matches == 0 || match_list_size == 0)
2799 {
2800 rl_ding ();
2801 FREE (matches);
2802 matches = (char **)0;
2803 completion_changed_buffer = 0;
2804 return (0);
2805 }
2806
2807 match_list_index += count;
2808 if (match_list_index < 0)
2809 {
2810 while (match_list_index < 0)
2811 match_list_index += match_list_size;
2812 }
2813 else
2814 match_list_index %= match_list_size;
2815
2816 if (match_list_index == 0 && match_list_size > 1)
2817 {
2818 rl_ding ();
2819 insert_match (orig_text, orig_start, MULT_MATCH, &quote_char);
2820 }
2821 else
2822 {
2823 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
2824 append_to_match (matches[match_list_index], delimiter, quote_char,
2825 strcmp (orig_text, matches[match_list_index]));
2826 }
2827
2828 completion_changed_buffer = 1;
2829 return (0);
2830 }
2831
2832 int
2833 rl_menu_complete (count, ignore)
2834 int count, ignore;
2835 {
2836 rl_compentry_func_t *our_func;
2837 int matching_filenames, found_quote;
2838
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;
2848
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)
2852 {
2853 /* Clean up from previous call, if any. */
2854 FREE (orig_text);
2855 if (matches)
2856 _rl_free_match_list (matches);
2857
2858 match_list_index = match_list_size = 0;
2859 matches = (char **)NULL;
2860
2861 full_completion = 0;
2862
2863 RL_SETSTATE(RL_STATE_COMPLETING);
2864
2865 /* Only the completion entry function can change these. */
2866 set_completion_defaults ('%');
2867
2868 our_func = rl_menu_completion_entry_function;
2869 if (our_func == 0)
2870 our_func = rl_completion_entry_function
2871 ? rl_completion_entry_function
2872 : rl_filename_completion_function;
2873
2874 /* We now look backwards for the start of a filename/variable word. */
2875 orig_end = rl_point;
2876 found_quote = delimiter = 0;
2877 quote_char = '\0';
2878
2879 if (rl_point)
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);
2883
2884 orig_start = rl_point;
2885 rl_point = orig_end;
2886
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);
2890
2891 nontrivial_lcd = matches && strcmp (orig_text, matches[0]) != 0;
2892
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;
2897
2898 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2899 {
2900 rl_ding ();
2901 FREE (matches);
2902 matches = (char **)0;
2903 FREE (orig_text);
2904 orig_text = (char *)0;
2905 completion_changed_buffer = 0;
2906 RL_UNSETSTATE(RL_STATE_COMPLETING);
2907 return (0);
2908 }
2909
2910 RL_UNSETSTATE(RL_STATE_COMPLETING);
2911
2912 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2913 ;
2914
2915 if (match_list_size == 0)
2916 {
2917 rl_ding ();
2918 FREE (matches);
2919 matches = (char **)0;
2920 match_list_index = 0;
2921 completion_changed_buffer = 0;
2922 return (0);
2923 }
2924
2925 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2926 code below should take care of it. */
2927 if (*matches[0])
2928 {
2929 insert_match (matches[0], orig_start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2930 orig_end = orig_start + strlen (matches[0]);
2931 completion_changed_buffer = STREQ (orig_text, matches[0]) == 0;
2932 }
2933
2934 if (match_list_size > 1 && _rl_complete_show_all)
2935 {
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
2939 is unwieldy. */
2940 if (rl_completion_query_items > 0 && match_list_size >= rl_completion_query_items)
2941 {
2942 rl_ding ();
2943 FREE (matches);
2944 matches = (char **)0;
2945 full_completion = 1;
2946 return (0);
2947 }
2948 else if (_rl_menu_complete_prefix_first)
2949 {
2950 rl_ding ();
2951 return (0);
2952 }
2953 }
2954 else if (match_list_size <= 1)
2955 {
2956 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2957 full_completion = 1;
2958 return (0);
2959 }
2960 else if (_rl_menu_complete_prefix_first && match_list_size > 1)
2961 {
2962 rl_ding ();
2963 return (0);
2964 }
2965 }
2966
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. */
2970
2971 if (matches == 0 || match_list_size == 0)
2972 {
2973 rl_ding ();
2974 FREE (matches);
2975 matches = (char **)0;
2976 completion_changed_buffer = 0;
2977 return (0);
2978 }
2979
2980 match_list_index += count;
2981 if (match_list_index < 0)
2982 {
2983 while (match_list_index < 0)
2984 match_list_index += match_list_size;
2985 }
2986 else
2987 match_list_index %= match_list_size;
2988
2989 if (match_list_index == 0 && match_list_size > 1)
2990 {
2991 rl_ding ();
2992 insert_match (matches[0], orig_start, MULT_MATCH, &quote_char);
2993 }
2994 else
2995 {
2996 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
2997 append_to_match (matches[match_list_index], delimiter, quote_char,
2998 strcmp (orig_text, matches[match_list_index]));
2999 }
3000
3001 completion_changed_buffer = 1;
3002 return (0);
3003 }
3004
3005 int
3006 rl_backward_menu_complete (count, key)
3007 int count, key;
3008 {
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));
3012 }