302ea1ddb803ec7608ffa8189ce25f8edbf2b8f3
[deliverable/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((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));
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 int _rl_colored_completion_prefix = 1;
216 #endif
217
218 /* If non-zero, when completing in the middle of a word, don't insert
219 characters from the match that match characters following point in
220 the word. This means, for instance, completing when the cursor is
221 after the `e' in `Makefile' won't result in `Makefilefile'. */
222 int _rl_skip_completed_text = 0;
223
224 /* If non-zero, menu completion displays the common prefix first in the
225 cycle of possible completions instead of the last. */
226 int _rl_menu_complete_prefix_first = 0;
227
228 /* If non-zero, then this is the address of a function to call when
229 completing on a directory name. The function is called with
230 the address of a string (the current directory name) as an arg. */
231 rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
232
233 rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
234
235 rl_icppfunc_t *rl_filename_stat_hook = (rl_icppfunc_t *)NULL;
236
237 /* If non-zero, this is the address of a function to call when reading
238 directory entries from the filesystem for completion and comparing
239 them to the partial word to be completed. The function should
240 either return its first argument (if no conversion takes place) or
241 newly-allocated memory. This can, for instance, convert filenames
242 between character sets for comparison against what's typed at the
243 keyboard. The returned value is what is added to the list of
244 matches. The second argument is the length of the filename to be
245 converted. */
246 rl_dequote_func_t *rl_filename_rewrite_hook = (rl_dequote_func_t *)NULL;
247
248 /* Non-zero means readline completion functions perform tilde expansion. */
249 int rl_complete_with_tilde_expansion = 0;
250
251 /* Pointer to the generator function for completion_matches ().
252 NULL means to use rl_filename_completion_function (), the default filename
253 completer. */
254 rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL;
255
256 /* Pointer to generator function for rl_menu_complete (). NULL means to use
257 *rl_completion_entry_function (see above). */
258 rl_compentry_func_t *rl_menu_completion_entry_function = (rl_compentry_func_t *)NULL;
259
260 /* Pointer to alternative function to create matches.
261 Function is called with TEXT, START, and END.
262 START and END are indices in RL_LINE_BUFFER saying what the boundaries
263 of TEXT are.
264 If this function exists and returns NULL then call the value of
265 rl_completion_entry_function to try to match, otherwise use the
266 array of strings returned. */
267 rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL;
268
269 /* Non-zero means to suppress normal filename completion after the
270 user-specified completion function has been called. */
271 int rl_attempted_completion_over = 0;
272
273 /* Set to a character indicating the type of completion being performed
274 by rl_complete_internal, available for use by application completion
275 functions. */
276 int rl_completion_type = 0;
277
278 /* Up to this many items will be displayed in response to a
279 possible-completions call. After that, we ask the user if
280 she is sure she wants to see them all. A negative value means
281 don't ask. */
282 int rl_completion_query_items = 100;
283
284 int _rl_page_completions = 1;
285
286 /* The basic list of characters that signal a break between words for the
287 completer routine. The contents of this variable is what breaks words
288 in the shell, i.e. " \t\n\"\\'`@$><=" */
289 const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
290
291 /* List of basic quoting characters. */
292 const char *rl_basic_quote_characters = "\"'";
293
294 /* The list of characters that signal a break between words for
295 rl_complete_internal. The default list is the contents of
296 rl_basic_word_break_characters. */
297 /*const*/ char *rl_completer_word_break_characters = (/*const*/ char *)NULL;
298
299 /* Hook function to allow an application to set the completion word
300 break characters before readline breaks up the line. Allows
301 position-dependent word break characters. */
302 rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL;
303
304 /* List of characters which can be used to quote a substring of the line.
305 Completion occurs on the entire substring, and within the substring
306 rl_completer_word_break_characters are treated as any other character,
307 unless they also appear within this list. */
308 const char *rl_completer_quote_characters = (const char *)NULL;
309
310 /* List of characters that should be quoted in filenames by the completer. */
311 const char *rl_filename_quote_characters = (const char *)NULL;
312
313 /* List of characters that are word break characters, but should be left
314 in TEXT when it is passed to the completion function. The shell uses
315 this to help determine what kind of completing to do. */
316 const char *rl_special_prefixes = (const char *)NULL;
317
318 /* If non-zero, then disallow duplicates in the matches. */
319 int rl_ignore_completion_duplicates = 1;
320
321 /* Non-zero means that the results of the matches are to be treated
322 as filenames. This is ALWAYS zero on entry, and can only be changed
323 within a completion entry finder function. */
324 int rl_filename_completion_desired = 0;
325
326 /* Non-zero means that the results of the matches are to be quoted using
327 double quotes (or an application-specific quoting mechanism) if the
328 filename contains any characters in rl_filename_quote_chars. This is
329 ALWAYS non-zero on entry, and can only be changed within a completion
330 entry finder function. */
331 int rl_filename_quoting_desired = 1;
332
333 /* This function, if defined, is called by the completer when real
334 filename completion is done, after all the matching names have been
335 generated. It is passed a (char**) known as matches in the code below.
336 It consists of a NULL-terminated array of pointers to potential
337 matching strings. The 1st element (matches[0]) is the maximal
338 substring that is common to all matches. This function can re-arrange
339 the list of matches as required, but all elements of the array must be
340 free()'d if they are deleted. The main intent of this function is
341 to implement FIGNORE a la SunOS csh. */
342 rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL;
343
344 /* Set to a function to quote a filename in an application-specific fashion.
345 Called with the text to quote, the type of match found (single or multiple)
346 and a pointer to the quoting character to be used, which the function can
347 reset if desired. */
348 rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename;
349
350 /* Function to call to remove quoting characters from a filename. Called
351 before completion is attempted, so the embedded quotes do not interfere
352 with matching names in the file system. Readline doesn't do anything
353 with this; it's set only by applications. */
354 rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL;
355
356 /* Function to call to decide whether or not a word break character is
357 quoted. If a character is quoted, it does not break words for the
358 completer. */
359 rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL;
360
361 /* If non-zero, the completion functions don't append anything except a
362 possible closing quote. This is set to 0 by rl_complete_internal and
363 may be changed by an application-specific completion function. */
364 int rl_completion_suppress_append = 0;
365
366 /* Character appended to completed words when at the end of the line. The
367 default is a space. */
368 int rl_completion_append_character = ' ';
369
370 /* If non-zero, the completion functions don't append any closing quote.
371 This is set to 0 by rl_complete_internal and may be changed by an
372 application-specific completion function. */
373 int rl_completion_suppress_quote = 0;
374
375 /* Set to any quote character readline thinks it finds before any application
376 completion function is called. */
377 int rl_completion_quote_character;
378
379 /* Set to a non-zero value if readline found quoting anywhere in the word to
380 be completed; set before any application completion function is called. */
381 int rl_completion_found_quote;
382
383 /* If non-zero, a slash will be appended to completed filenames that are
384 symbolic links to directory names, subject to the value of the
385 mark-directories variable (which is user-settable). This exists so
386 that application completion functions can override the user's preference
387 (set via the mark-symlinked-directories variable) if appropriate.
388 It's set to the value of _rl_complete_mark_symlink_dirs in
389 rl_complete_internal before any application-specific completion
390 function is called, so without that function doing anything, the user's
391 preferences are honored. */
392 int rl_completion_mark_symlink_dirs;
393
394 /* If non-zero, inhibit completion (temporarily). */
395 int rl_inhibit_completion;
396
397 /* Set to the last key used to invoke one of the completion functions */
398 int rl_completion_invoking_key;
399
400 /* If non-zero, sort the completion matches. On by default. */
401 int rl_sort_completion_matches = 1;
402
403 /* Variables local to this file. */
404
405 /* Local variable states what happened during the last completion attempt. */
406 static int completion_changed_buffer;
407
408 /* The result of the query to the user about displaying completion matches */
409 static int completion_y_or_n;
410
411 static int _rl_complete_display_matches_interrupt = 0;
412
413 /*************************************/
414 /* */
415 /* Bindable completion functions */
416 /* */
417 /*************************************/
418
419 /* Complete the word at or before point. You have supplied the function
420 that does the initial simple matching selection algorithm (see
421 rl_completion_matches ()). The default is to do filename completion. */
422 int
423 rl_complete (ignore, invoking_key)
424 int ignore, invoking_key;
425 {
426 rl_completion_invoking_key = invoking_key;
427
428 if (rl_inhibit_completion)
429 return (_rl_insert_char (ignore, invoking_key));
430 else if (rl_last_func == rl_complete && !completion_changed_buffer)
431 return (rl_complete_internal ('?'));
432 else if (_rl_complete_show_all)
433 return (rl_complete_internal ('!'));
434 else if (_rl_complete_show_unmodified)
435 return (rl_complete_internal ('@'));
436 else
437 return (rl_complete_internal (TAB));
438 }
439
440 /* List the possible completions. See description of rl_complete (). */
441 int
442 rl_possible_completions (ignore, invoking_key)
443 int ignore, invoking_key;
444 {
445 rl_completion_invoking_key = invoking_key;
446 return (rl_complete_internal ('?'));
447 }
448
449 int
450 rl_insert_completions (ignore, invoking_key)
451 int ignore, invoking_key;
452 {
453 rl_completion_invoking_key = invoking_key;
454 return (rl_complete_internal ('*'));
455 }
456
457 /* Return the correct value to pass to rl_complete_internal performing
458 the same tests as rl_complete. This allows consecutive calls to an
459 application's completion function to list possible completions and for
460 an application-specific completion function to honor the
461 show-all-if-ambiguous readline variable. */
462 int
463 rl_completion_mode (cfunc)
464 rl_command_func_t *cfunc;
465 {
466 if (rl_last_func == cfunc && !completion_changed_buffer)
467 return '?';
468 else if (_rl_complete_show_all)
469 return '!';
470 else if (_rl_complete_show_unmodified)
471 return '@';
472 else
473 return TAB;
474 }
475
476 /************************************/
477 /* */
478 /* Completion utility functions */
479 /* */
480 /************************************/
481
482 /* Reset readline state on a signal or other event. */
483 void
484 _rl_reset_completion_state ()
485 {
486 rl_completion_found_quote = 0;
487 rl_completion_quote_character = 0;
488 }
489
490 static void
491 _rl_complete_sigcleanup (sig, ptr)
492 int sig;
493 void *ptr;
494 {
495 if (sig == SIGINT) /* XXX - for now */
496 {
497 _rl_free_match_list ((char **)ptr);
498 _rl_complete_display_matches_interrupt = 1;
499 }
500 }
501
502 /* Set default values for readline word completion. These are the variables
503 that application completion functions can change or inspect. */
504 static void
505 set_completion_defaults (what_to_do)
506 int what_to_do;
507 {
508 /* Only the completion entry function can change these. */
509 rl_filename_completion_desired = 0;
510 rl_filename_quoting_desired = 1;
511 rl_completion_type = what_to_do;
512 rl_completion_suppress_append = rl_completion_suppress_quote = 0;
513 rl_completion_append_character = ' ';
514
515 /* The completion entry function may optionally change this. */
516 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
517
518 /* Reset private state. */
519 _rl_complete_display_matches_interrupt = 0;
520 }
521
522 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
523 static int
524 get_y_or_n (for_pager)
525 int for_pager;
526 {
527 int c;
528
529 /* For now, disable pager in callback mode, until we later convert to state
530 driven functions. Have to wait until next major version to add new
531 state definition, since it will change value of RL_STATE_DONE. */
532 #if defined (READLINE_CALLBACKS)
533 if (RL_ISSTATE (RL_STATE_CALLBACK))
534 return 1;
535 #endif
536
537 for (;;)
538 {
539 RL_SETSTATE(RL_STATE_MOREINPUT);
540 c = rl_read_key ();
541 RL_UNSETSTATE(RL_STATE_MOREINPUT);
542
543 if (c == 'y' || c == 'Y' || c == ' ')
544 return (1);
545 if (c == 'n' || c == 'N' || c == RUBOUT)
546 return (0);
547 if (c == ABORT_CHAR || c < 0)
548 _rl_abort_internal ();
549 if (for_pager && (c == NEWLINE || c == RETURN))
550 return (2);
551 if (for_pager && (c == 'q' || c == 'Q'))
552 return (0);
553 rl_ding ();
554 }
555 }
556
557 static int
558 _rl_internal_pager (lines)
559 int lines;
560 {
561 int i;
562
563 fprintf (rl_outstream, "--More--");
564 fflush (rl_outstream);
565 i = get_y_or_n (1);
566 _rl_erase_entire_line ();
567 if (i == 0)
568 return -1;
569 else if (i == 2)
570 return (lines - 1);
571 else
572 return 0;
573 }
574
575 static int
576 path_isdir (filename)
577 const char *filename;
578 {
579 struct stat finfo;
580
581 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
582 }
583
584 #if defined (VISIBLE_STATS)
585 /* Return the character which best describes FILENAME.
586 `@' for symbolic links
587 `/' for directories
588 `*' for executables
589 `=' for sockets
590 `|' for FIFOs
591 `%' for character special devices
592 `#' for block special devices */
593 static int
594 stat_char (filename)
595 char *filename;
596 {
597 struct stat finfo;
598 int character, r;
599 char *f;
600 const char *fn;
601
602 /* Short-circuit a //server on cygwin, since that will always behave as
603 a directory. */
604 #if __CYGWIN__
605 if (filename[0] == '/' && filename[1] == '/' && strchr (filename+2, '/') == 0)
606 return '/';
607 #endif
608
609 f = 0;
610 if (rl_filename_stat_hook)
611 {
612 f = savestring (filename);
613 (*rl_filename_stat_hook) (&f);
614 fn = f;
615 }
616 else
617 fn = filename;
618
619 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
620 r = lstat (fn, &finfo);
621 #else
622 r = stat (fn, &finfo);
623 #endif
624
625 if (r == -1)
626 return (0);
627
628 character = 0;
629 if (S_ISDIR (finfo.st_mode))
630 character = '/';
631 #if defined (S_ISCHR)
632 else if (S_ISCHR (finfo.st_mode))
633 character = '%';
634 #endif /* S_ISCHR */
635 #if defined (S_ISBLK)
636 else if (S_ISBLK (finfo.st_mode))
637 character = '#';
638 #endif /* S_ISBLK */
639 #if defined (S_ISLNK)
640 else if (S_ISLNK (finfo.st_mode))
641 character = '@';
642 #endif /* S_ISLNK */
643 #if defined (S_ISSOCK)
644 else if (S_ISSOCK (finfo.st_mode))
645 character = '=';
646 #endif /* S_ISSOCK */
647 #if defined (S_ISFIFO)
648 else if (S_ISFIFO (finfo.st_mode))
649 character = '|';
650 #endif
651 else if (S_ISREG (finfo.st_mode))
652 {
653 #if defined (_WIN32) && !defined (__CYGWIN__)
654 char *ext;
655
656 /* Windows doesn't do access and X_OK; check file extension instead */
657 ext = strrchr (fn, '.');
658 if (ext && (_rl_stricmp (ext, ".exe") == 0 ||
659 _rl_stricmp (ext, ".cmd") == 0 ||
660 _rl_stricmp (ext, ".bat") == 0 ||
661 _rl_stricmp (ext, ".com") == 0))
662 character = '*';
663 #else
664 if (access (filename, X_OK) == 0)
665 character = '*';
666 #endif
667 }
668
669 xfree (f);
670 return (character);
671 }
672 #endif /* VISIBLE_STATS */
673
674 #if defined (COLOR_SUPPORT)
675 static int
676 colored_stat_start (filename)
677 char *filename;
678 {
679 _rl_set_normal_color ();
680 return (_rl_print_color_indicator (filename));
681 }
682
683 static void
684 colored_stat_end ()
685 {
686 _rl_prep_non_filename_text ();
687 _rl_put_indicator (&_rl_color_indicator[C_CLR_TO_EOL]);
688 }
689
690 static int
691 colored_prefix_start ()
692 {
693 _rl_set_normal_color ();
694 return (_rl_print_prefix_color ());
695 }
696
697 static void
698 colored_prefix_end ()
699 {
700 colored_stat_end (); /* for now */
701 }
702 #endif
703
704 /* Return the portion of PATHNAME that should be output when listing
705 possible completions. If we are hacking filename completion, we
706 are only interested in the basename, the portion following the
707 final slash. Otherwise, we return what we were passed. Since
708 printing empty strings is not very informative, if we're doing
709 filename completion, and the basename is the empty string, we look
710 for the previous slash and return the portion following that. If
711 there's no previous slash, we just return what we were passed. */
712 static char *
713 printable_part (pathname)
714 char *pathname;
715 {
716 char *temp, *x;
717
718 if (rl_filename_completion_desired == 0) /* don't need to do anything */
719 return (pathname);
720
721 temp = strrchr (pathname, '/');
722 #if defined (__MSDOS__) || defined (_WIN32)
723 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
724 temp = pathname + 1;
725 #endif
726
727 if (temp == 0 || *temp == '\0')
728 return (pathname);
729 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
730 Look for a previous slash and, if one is found, return the portion
731 following that slash. If there's no previous slash, just return the
732 pathname we were passed. */
733 else if (temp[1] == '\0')
734 {
735 for (x = temp - 1; x > pathname; x--)
736 if (*x == '/')
737 break;
738 return ((*x == '/') ? x + 1 : pathname);
739 }
740 else
741 return ++temp;
742 }
743
744 /* Compute width of STRING when displayed on screen by print_filename */
745 static int
746 fnwidth (string)
747 const char *string;
748 {
749 int width, pos;
750 #if defined (HANDLE_MULTIBYTE)
751 mbstate_t ps;
752 int left, w;
753 size_t clen;
754 wchar_t wc;
755
756 left = strlen (string) + 1;
757 memset (&ps, 0, sizeof (mbstate_t));
758 #endif
759
760 width = pos = 0;
761 while (string[pos])
762 {
763 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
764 {
765 width += 2;
766 pos++;
767 }
768 else
769 {
770 #if defined (HANDLE_MULTIBYTE)
771 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
772 if (MB_INVALIDCH (clen))
773 {
774 width++;
775 pos++;
776 memset (&ps, 0, sizeof (mbstate_t));
777 }
778 else if (MB_NULLWCH (clen))
779 break;
780 else
781 {
782 pos += clen;
783 w = WCWIDTH (wc);
784 width += (w >= 0) ? w : 1;
785 }
786 #else
787 width++;
788 pos++;
789 #endif
790 }
791 }
792
793 return width;
794 }
795
796 #define ELLIPSIS_LEN 3
797
798 static int
799 fnprint (to_print, prefix_bytes)
800 const char *to_print;
801 int prefix_bytes;
802 {
803 int printed_len, w;
804 const char *s;
805 int common_prefix_len;
806 #if defined (HANDLE_MULTIBYTE)
807 mbstate_t ps;
808 const char *end;
809 size_t tlen;
810 int width;
811 wchar_t wc;
812
813 end = to_print + strlen (to_print) + 1;
814 memset (&ps, 0, sizeof (mbstate_t));
815 #endif
816
817 printed_len = common_prefix_len = 0;
818
819 /* Don't print only the ellipsis if the common prefix is one of the
820 possible completions */
821 if (to_print[prefix_bytes] == '\0')
822 prefix_bytes = 0;
823
824 if (prefix_bytes && _rl_completion_prefix_display_length > 0)
825 {
826 char ellipsis;
827
828 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
829 for (w = 0; w < ELLIPSIS_LEN; w++)
830 putc (ellipsis, rl_outstream);
831 printed_len = ELLIPSIS_LEN;
832 }
833 #if defined (COLOR_SUPPORT)
834 else if (prefix_bytes && _rl_colored_completion_prefix > 0)
835 {
836 common_prefix_len = prefix_bytes;
837 prefix_bytes = 0;
838 /* XXX - print color indicator start here */
839 colored_prefix_start ();
840 }
841 #endif
842
843 s = to_print + prefix_bytes;
844 while (*s)
845 {
846 if (CTRL_CHAR (*s))
847 {
848 putc ('^', rl_outstream);
849 putc (UNCTRL (*s), rl_outstream);
850 printed_len += 2;
851 s++;
852 #if defined (HANDLE_MULTIBYTE)
853 memset (&ps, 0, sizeof (mbstate_t));
854 #endif
855 }
856 else if (*s == RUBOUT)
857 {
858 putc ('^', rl_outstream);
859 putc ('?', rl_outstream);
860 printed_len += 2;
861 s++;
862 #if defined (HANDLE_MULTIBYTE)
863 memset (&ps, 0, sizeof (mbstate_t));
864 #endif
865 }
866 else
867 {
868 #if defined (HANDLE_MULTIBYTE)
869 tlen = mbrtowc (&wc, s, end - s, &ps);
870 if (MB_INVALIDCH (tlen))
871 {
872 tlen = 1;
873 width = 1;
874 memset (&ps, 0, sizeof (mbstate_t));
875 }
876 else if (MB_NULLWCH (tlen))
877 break;
878 else
879 {
880 w = WCWIDTH (wc);
881 width = (w >= 0) ? w : 1;
882 }
883 fwrite (s, 1, tlen, rl_outstream);
884 s += tlen;
885 printed_len += width;
886 #else
887 putc (*s, rl_outstream);
888 s++;
889 printed_len++;
890 #endif
891 }
892 if (common_prefix_len > 0 && (s - to_print) >= common_prefix_len)
893 {
894 /* printed bytes = s - to_print */
895 /* printed bytes should never be > but check for paranoia's sake */
896 colored_prefix_end ();
897 common_prefix_len = 0;
898 }
899 }
900
901 return printed_len;
902 }
903
904 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
905 are using it, check for and output a single character for `special'
906 filenames. Return the number of characters we output. */
907
908 static int
909 print_filename (to_print, full_pathname, prefix_bytes)
910 char *to_print, *full_pathname;
911 int prefix_bytes;
912 {
913 int printed_len, extension_char, slen, tlen;
914 char *s, c, *new_full_pathname, *dn;
915
916 extension_char = 0;
917 #if defined (COLOR_SUPPORT)
918 /* Defer printing if we want to prefix with a color indicator */
919 if (_rl_colored_stats == 0 || rl_filename_completion_desired == 0)
920 #endif
921 printed_len = fnprint (to_print, prefix_bytes);
922
923 if (rl_filename_completion_desired && (
924 #if defined (VISIBLE_STATS)
925 rl_visible_stats ||
926 #endif
927 #if defined (COLOR_SUPPORT)
928 _rl_colored_stats ||
929 #endif
930 _rl_complete_mark_directories))
931 {
932 /* If to_print != full_pathname, to_print is the basename of the
933 path passed. In this case, we try to expand the directory
934 name before checking for the stat character. */
935 if (to_print != full_pathname)
936 {
937 /* Terminate the directory name. */
938 c = to_print[-1];
939 to_print[-1] = '\0';
940
941 /* If setting the last slash in full_pathname to a NUL results in
942 full_pathname being the empty string, we are trying to complete
943 files in the root directory. If we pass a null string to the
944 bash directory completion hook, for example, it will expand it
945 to the current directory. We just want the `/'. */
946 if (full_pathname == 0 || *full_pathname == 0)
947 dn = "/";
948 else if (full_pathname[0] != '/')
949 dn = full_pathname;
950 else if (full_pathname[1] == 0)
951 dn = "//"; /* restore trailing slash to `//' */
952 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
953 dn = "/"; /* don't turn /// into // */
954 else
955 dn = full_pathname;
956 s = tilde_expand (dn);
957 if (rl_directory_completion_hook)
958 (*rl_directory_completion_hook) (&s);
959
960 slen = strlen (s);
961 tlen = strlen (to_print);
962 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
963 strcpy (new_full_pathname, s);
964 if (s[slen - 1] == '/')
965 slen--;
966 else
967 new_full_pathname[slen] = '/';
968 new_full_pathname[slen] = '/';
969 strcpy (new_full_pathname + slen + 1, to_print);
970
971 #if defined (VISIBLE_STATS)
972 if (rl_visible_stats)
973 extension_char = stat_char (new_full_pathname);
974 else
975 #endif
976 if (_rl_complete_mark_directories)
977 {
978 dn = 0;
979 if (rl_directory_completion_hook == 0 && rl_filename_stat_hook)
980 {
981 dn = savestring (new_full_pathname);
982 (*rl_filename_stat_hook) (&dn);
983 xfree (new_full_pathname);
984 new_full_pathname = dn;
985 }
986 if (path_isdir (new_full_pathname))
987 extension_char = '/';
988 }
989
990 #if defined (COLOR_SUPPORT)
991 if (_rl_colored_stats)
992 {
993 colored_stat_start (new_full_pathname);
994 printed_len = fnprint (to_print, prefix_bytes);
995 colored_stat_end ();
996 }
997 #endif
998
999 xfree (new_full_pathname);
1000 to_print[-1] = c;
1001 }
1002 else
1003 {
1004 s = tilde_expand (full_pathname);
1005 #if defined (VISIBLE_STATS)
1006 if (rl_visible_stats)
1007 extension_char = stat_char (s);
1008 else
1009 #endif
1010 if (_rl_complete_mark_directories && path_isdir (s))
1011 extension_char = '/';
1012
1013 #if defined (COLOR_SUPPORT)
1014 if (_rl_colored_stats)
1015 {
1016 colored_stat_start (s);
1017 printed_len = fnprint (to_print, prefix_bytes);
1018 colored_stat_end ();
1019 }
1020 #endif
1021
1022 }
1023
1024 xfree (s);
1025 if (extension_char)
1026 {
1027 putc (extension_char, rl_outstream);
1028 printed_len++;
1029 }
1030 }
1031
1032 return printed_len;
1033 }
1034
1035 static char *
1036 rl_quote_filename (s, rtype, qcp)
1037 char *s;
1038 int rtype;
1039 char *qcp;
1040 {
1041 char *r;
1042
1043 r = (char *)xmalloc (strlen (s) + 2);
1044 *r = *rl_completer_quote_characters;
1045 strcpy (r + 1, s);
1046 if (qcp)
1047 *qcp = *rl_completer_quote_characters;
1048 return r;
1049 }
1050
1051 /* Find the bounds of the current word for completion purposes, and leave
1052 rl_point set to the end of the word. This function skips quoted
1053 substrings (characters between matched pairs of characters in
1054 rl_completer_quote_characters). First we try to find an unclosed
1055 quoted substring on which to do matching. If one is not found, we use
1056 the word break characters to find the boundaries of the current word.
1057 We call an application-specific function to decide whether or not a
1058 particular word break character is quoted; if that function returns a
1059 non-zero result, the character does not break a word. This function
1060 returns the opening quote character if we found an unclosed quoted
1061 substring, '\0' otherwise. FP, if non-null, is set to a value saying
1062 which (shell-like) quote characters we found (single quote, double
1063 quote, or backslash) anywhere in the string. DP, if non-null, is set to
1064 the value of the delimiter character that caused a word break. */
1065
1066 char
1067 _rl_find_completion_word (fp, dp)
1068 int *fp, *dp;
1069 {
1070 int scan, end, found_quote, delimiter, pass_next, isbrk;
1071 char quote_char, *brkchars;
1072
1073 end = rl_point;
1074 found_quote = delimiter = 0;
1075 quote_char = '\0';
1076
1077 brkchars = 0;
1078 if (rl_completion_word_break_hook)
1079 brkchars = (*rl_completion_word_break_hook) ();
1080 if (brkchars == 0)
1081 brkchars = rl_completer_word_break_characters;
1082
1083 if (rl_completer_quote_characters)
1084 {
1085 /* We have a list of characters which can be used in pairs to
1086 quote substrings for the completer. Try to find the start
1087 of an unclosed quoted substring. */
1088 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
1089 for (scan = pass_next = 0; scan < end; scan = MB_NEXTCHAR (rl_line_buffer, scan, 1, MB_FIND_ANY))
1090 {
1091 if (pass_next)
1092 {
1093 pass_next = 0;
1094 continue;
1095 }
1096
1097 /* Shell-like semantics for single quotes -- don't allow backslash
1098 to quote anything in single quotes, especially not the closing
1099 quote. If you don't like this, take out the check on the value
1100 of quote_char. */
1101 if (quote_char != '\'' && rl_line_buffer[scan] == '\\')
1102 {
1103 pass_next = 1;
1104 found_quote |= RL_QF_BACKSLASH;
1105 continue;
1106 }
1107
1108 if (quote_char != '\0')
1109 {
1110 /* Ignore everything until the matching close quote char. */
1111 if (rl_line_buffer[scan] == quote_char)
1112 {
1113 /* Found matching close. Abandon this substring. */
1114 quote_char = '\0';
1115 rl_point = end;
1116 }
1117 }
1118 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
1119 {
1120 /* Found start of a quoted substring. */
1121 quote_char = rl_line_buffer[scan];
1122 rl_point = scan + 1;
1123 /* Shell-like quoting conventions. */
1124 if (quote_char == '\'')
1125 found_quote |= RL_QF_SINGLE_QUOTE;
1126 else if (quote_char == '"')
1127 found_quote |= RL_QF_DOUBLE_QUOTE;
1128 else
1129 found_quote |= RL_QF_OTHER_QUOTE;
1130 }
1131 }
1132 }
1133
1134 if (rl_point == end && quote_char == '\0')
1135 {
1136 /* We didn't find an unclosed quoted substring upon which to do
1137 completion, so use the word break characters to find the
1138 substring on which to complete. */
1139 while (rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_ANY))
1140 {
1141 scan = rl_line_buffer[rl_point];
1142
1143 if (strchr (brkchars, scan) == 0)
1144 continue;
1145
1146 /* Call the application-specific function to tell us whether
1147 this word break character is quoted and should be skipped. */
1148 if (rl_char_is_quoted_p && found_quote &&
1149 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
1150 continue;
1151
1152 /* Convoluted code, but it avoids an n^2 algorithm with calls
1153 to char_is_quoted. */
1154 break;
1155 }
1156 }
1157
1158 /* If we are at an unquoted word break, then advance past it. */
1159 scan = rl_line_buffer[rl_point];
1160
1161 /* If there is an application-specific function to say whether or not
1162 a character is quoted and we found a quote character, let that
1163 function decide whether or not a character is a word break, even
1164 if it is found in rl_completer_word_break_characters. Don't bother
1165 if we're at the end of the line, though. */
1166 if (scan)
1167 {
1168 if (rl_char_is_quoted_p)
1169 isbrk = (found_quote == 0 ||
1170 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
1171 strchr (brkchars, scan) != 0;
1172 else
1173 isbrk = strchr (brkchars, scan) != 0;
1174
1175 if (isbrk)
1176 {
1177 /* If the character that caused the word break was a quoting
1178 character, then remember it as the delimiter. */
1179 if (rl_basic_quote_characters &&
1180 strchr (rl_basic_quote_characters, scan) &&
1181 (end - rl_point) > 1)
1182 delimiter = scan;
1183
1184 /* If the character isn't needed to determine something special
1185 about what kind of completion to perform, then advance past it. */
1186 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0)
1187 rl_point++;
1188 }
1189 }
1190
1191 if (fp)
1192 *fp = found_quote;
1193 if (dp)
1194 *dp = delimiter;
1195
1196 return (quote_char);
1197 }
1198
1199 static char **
1200 gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
1201 char *text;
1202 int start, end;
1203 rl_compentry_func_t *our_func;
1204 int found_quote, quote_char;
1205 {
1206 char **matches;
1207
1208 rl_completion_found_quote = found_quote;
1209 rl_completion_quote_character = quote_char;
1210
1211 /* If the user wants to TRY to complete, but then wants to give
1212 up and use the default completion function, they set the
1213 variable rl_attempted_completion_function. */
1214 if (rl_attempted_completion_function)
1215 {
1216 matches = (*rl_attempted_completion_function) (text, start, end);
1217 if (RL_SIG_RECEIVED())
1218 {
1219 _rl_free_match_list (matches);
1220 matches = 0;
1221 RL_CHECK_SIGNALS ();
1222 }
1223
1224 if (matches || rl_attempted_completion_over)
1225 {
1226 rl_attempted_completion_over = 0;
1227 return (matches);
1228 }
1229 }
1230
1231 /* XXX -- filename dequoting moved into rl_filename_completion_function */
1232
1233 /* rl_completion_matches will check for signals as well to avoid a long
1234 delay while reading a directory. */
1235 matches = rl_completion_matches (text, our_func);
1236 if (RL_SIG_RECEIVED())
1237 {
1238 _rl_free_match_list (matches);
1239 matches = 0;
1240 RL_CHECK_SIGNALS ();
1241 }
1242 return matches;
1243 }
1244
1245 /* Filter out duplicates in MATCHES. This frees up the strings in
1246 MATCHES. */
1247 static char **
1248 remove_duplicate_matches (matches)
1249 char **matches;
1250 {
1251 char *lowest_common;
1252 int i, j, newlen;
1253 char dead_slot;
1254 char **temp_array;
1255
1256 /* Sort the items. */
1257 for (i = 0; matches[i]; i++)
1258 ;
1259
1260 /* Sort the array without matches[0], since we need it to
1261 stay in place no matter what. */
1262 if (i && rl_sort_completion_matches)
1263 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1264
1265 /* Remember the lowest common denominator for it may be unique. */
1266 lowest_common = savestring (matches[0]);
1267
1268 for (i = newlen = 0; matches[i + 1]; i++)
1269 {
1270 if (strcmp (matches[i], matches[i + 1]) == 0)
1271 {
1272 xfree (matches[i]);
1273 matches[i] = (char *)&dead_slot;
1274 }
1275 else
1276 newlen++;
1277 }
1278
1279 /* We have marked all the dead slots with (char *)&dead_slot.
1280 Copy all the non-dead entries into a new array. */
1281 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
1282 for (i = j = 1; matches[i]; i++)
1283 {
1284 if (matches[i] != (char *)&dead_slot)
1285 temp_array[j++] = matches[i];
1286 }
1287 temp_array[j] = (char *)NULL;
1288
1289 if (matches[0] != (char *)&dead_slot)
1290 xfree (matches[0]);
1291
1292 /* Place the lowest common denominator back in [0]. */
1293 temp_array[0] = lowest_common;
1294
1295 /* If there is one string left, and it is identical to the
1296 lowest common denominator, then the LCD is the string to
1297 insert. */
1298 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
1299 {
1300 xfree (temp_array[1]);
1301 temp_array[1] = (char *)NULL;
1302 }
1303 return (temp_array);
1304 }
1305
1306 /* Find the common prefix of the list of matches, and put it into
1307 matches[0]. */
1308 static int
1309 compute_lcd_of_matches (match_list, matches, text)
1310 char **match_list;
1311 int matches;
1312 const char *text;
1313 {
1314 register int i, c1, c2, si;
1315 int low; /* Count of max-matched characters. */
1316 int lx;
1317 char *dtext; /* dequoted TEXT, if needed */
1318 #if defined (HANDLE_MULTIBYTE)
1319 int v;
1320 size_t v1, v2;
1321 mbstate_t ps1, ps2;
1322 wchar_t wc1, wc2;
1323 #endif
1324
1325 /* If only one match, just use that. Otherwise, compare each
1326 member of the list with the next, finding out where they
1327 stop matching. */
1328 if (matches == 1)
1329 {
1330 match_list[0] = match_list[1];
1331 match_list[1] = (char *)NULL;
1332 return 1;
1333 }
1334
1335 for (i = 1, low = 100000; i < matches; i++)
1336 {
1337 #if defined (HANDLE_MULTIBYTE)
1338 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1339 {
1340 memset (&ps1, 0, sizeof (mbstate_t));
1341 memset (&ps2, 0, sizeof (mbstate_t));
1342 }
1343 #endif
1344 if (_rl_completion_case_fold)
1345 {
1346 for (si = 0;
1347 (c1 = _rl_to_lower(match_list[i][si])) &&
1348 (c2 = _rl_to_lower(match_list[i + 1][si]));
1349 si++)
1350 #if defined (HANDLE_MULTIBYTE)
1351 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1352 {
1353 v1 = mbrtowc(&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1);
1354 v2 = mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2);
1355 if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
1356 {
1357 if (c1 != c2) /* do byte comparison */
1358 break;
1359 continue;
1360 }
1361 wc1 = towlower (wc1);
1362 wc2 = towlower (wc2);
1363 if (wc1 != wc2)
1364 break;
1365 else if (v1 > 1)
1366 si += v1 - 1;
1367 }
1368 else
1369 #endif
1370 if (c1 != c2)
1371 break;
1372 }
1373 else
1374 {
1375 for (si = 0;
1376 (c1 = match_list[i][si]) &&
1377 (c2 = match_list[i + 1][si]);
1378 si++)
1379 #if defined (HANDLE_MULTIBYTE)
1380 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1381 {
1382 mbstate_t ps_back;
1383 ps_back = ps1;
1384 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2))
1385 break;
1386 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1)
1387 si += v - 1;
1388 }
1389 else
1390 #endif
1391 if (c1 != c2)
1392 break;
1393 }
1394
1395 if (low > si)
1396 low = si;
1397 }
1398
1399 /* If there were multiple matches, but none matched up to even the
1400 first character, and the user typed something, use that as the
1401 value of matches[0]. */
1402 if (low == 0 && text && *text)
1403 {
1404 match_list[0] = (char *)xmalloc (strlen (text) + 1);
1405 strcpy (match_list[0], text);
1406 }
1407 else
1408 {
1409 match_list[0] = (char *)xmalloc (low + 1);
1410
1411 /* XXX - this might need changes in the presence of multibyte chars */
1412
1413 /* If we are ignoring case, try to preserve the case of the string
1414 the user typed in the face of multiple matches differing in case. */
1415 if (_rl_completion_case_fold)
1416 {
1417 /* We're making an assumption here:
1418 IF we're completing filenames AND
1419 the application has defined a filename dequoting function AND
1420 we found a quote character AND
1421 the application has requested filename quoting
1422 THEN
1423 we assume that TEXT was dequoted before checking against
1424 the file system and needs to be dequoted here before we
1425 check against the list of matches
1426 FI */
1427 dtext = (char *)NULL;
1428 if (rl_filename_completion_desired &&
1429 rl_filename_dequoting_function &&
1430 rl_completion_found_quote &&
1431 rl_filename_quoting_desired)
1432 {
1433 dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
1434 text = dtext;
1435 }
1436
1437 /* sort the list to get consistent answers. */
1438 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1439
1440 si = strlen (text);
1441 lx = (si <= low) ? si : low; /* check shorter of text and matches */
1442 /* Try to preserve the case of what the user typed in the presence of
1443 multiple matches: check each match for something that matches
1444 what the user typed taking case into account; use it up to common
1445 length of matches if one is found. If not, just use first match. */
1446 for (i = 1; i <= matches; i++)
1447 if (strncmp (match_list[i], text, lx) == 0)
1448 {
1449 strncpy (match_list[0], match_list[i], low);
1450 break;
1451 }
1452 /* no casematch, use first entry */
1453 if (i > matches)
1454 strncpy (match_list[0], match_list[1], low);
1455
1456 FREE (dtext);
1457 }
1458 else
1459 strncpy (match_list[0], match_list[1], low);
1460
1461 match_list[0][low] = '\0';
1462 }
1463
1464 return matches;
1465 }
1466
1467 static int
1468 postprocess_matches (matchesp, matching_filenames)
1469 char ***matchesp;
1470 int matching_filenames;
1471 {
1472 char *t, **matches, **temp_matches;
1473 int nmatch, i;
1474
1475 matches = *matchesp;
1476
1477 if (matches == 0)
1478 return 0;
1479
1480 /* It seems to me that in all the cases we handle we would like
1481 to ignore duplicate possibilities. Scan for the text to
1482 insert being identical to the other completions. */
1483 if (rl_ignore_completion_duplicates)
1484 {
1485 temp_matches = remove_duplicate_matches (matches);
1486 xfree (matches);
1487 matches = temp_matches;
1488 }
1489
1490 /* If we are matching filenames, then here is our chance to
1491 do clever processing by re-examining the list. Call the
1492 ignore function with the array as a parameter. It can
1493 munge the array, deleting matches as it desires. */
1494 if (rl_ignore_some_completions_function && matching_filenames)
1495 {
1496 for (nmatch = 1; matches[nmatch]; nmatch++)
1497 ;
1498 (void)(*rl_ignore_some_completions_function) (matches);
1499 if (matches == 0 || matches[0] == 0)
1500 {
1501 FREE (matches);
1502 *matchesp = (char **)0;
1503 return 0;
1504 }
1505 else
1506 {
1507 /* If we removed some matches, recompute the common prefix. */
1508 for (i = 1; matches[i]; i++)
1509 ;
1510 if (i > 1 && i < nmatch)
1511 {
1512 t = matches[0];
1513 compute_lcd_of_matches (matches, i - 1, t);
1514 FREE (t);
1515 }
1516 }
1517 }
1518
1519 *matchesp = matches;
1520 return (1);
1521 }
1522
1523 static int
1524 complete_get_screenwidth ()
1525 {
1526 int cols;
1527 char *envcols;
1528
1529 cols = _rl_completion_columns;
1530 if (cols >= 0 && cols <= _rl_screenwidth)
1531 return cols;
1532 envcols = getenv ("COLUMNS");
1533 if (envcols && *envcols)
1534 cols = atoi (envcols);
1535 if (cols >= 0 && cols <= _rl_screenwidth)
1536 return cols;
1537 return _rl_screenwidth;
1538 }
1539
1540 /* A convenience function for displaying a list of strings in
1541 columnar format on readline's output stream. MATCHES is the list
1542 of strings, in argv format, LEN is the number of strings in MATCHES,
1543 and MAX is the length of the longest string in MATCHES. */
1544 void
1545 rl_display_match_list (matches, len, max)
1546 char **matches;
1547 int len, max;
1548 {
1549 int count, limit, printed_len, lines, cols;
1550 int i, j, k, l, common_length, sind;
1551 char *temp, *t;
1552
1553 /* Find the length of the prefix common to all items: length as displayed
1554 characters (common_length) and as a byte index into the matches (sind) */
1555 common_length = sind = 0;
1556 if (_rl_completion_prefix_display_length > 0)
1557 {
1558 t = printable_part (matches[0]);
1559 temp = strrchr (t, '/'); /* check again in case of /usr/src/ */
1560 common_length = temp ? fnwidth (temp) : fnwidth (t);
1561 sind = temp ? strlen (temp) : strlen (t);
1562
1563 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
1564 max -= common_length - ELLIPSIS_LEN;
1565 else
1566 common_length = sind = 0;
1567 }
1568 #if defined (COLOR_SUPPORT)
1569 else if (_rl_colored_completion_prefix > 0)
1570 {
1571 t = printable_part (matches[0]);
1572 temp = strrchr (t, '/');
1573 common_length = temp ? fnwidth (temp) : fnwidth (t);
1574 sind = temp ? RL_STRLEN (temp+1) : RL_STRLEN (t); /* want portion after final slash */
1575 }
1576 #endif
1577
1578 /* How many items of MAX length can we fit in the screen window? */
1579 cols = complete_get_screenwidth ();
1580 max += 2;
1581 limit = cols / max;
1582 if (limit != 1 && (limit * max == cols))
1583 limit--;
1584
1585 /* If cols == 0, limit will end up -1 */
1586 if (cols < _rl_screenwidth && limit < 0)
1587 limit = 1;
1588
1589 /* Avoid a possible floating exception. If max > cols,
1590 limit will be 0 and a divide-by-zero fault will result. */
1591 if (limit == 0)
1592 limit = 1;
1593
1594 /* How many iterations of the printing loop? */
1595 count = (len + (limit - 1)) / limit;
1596
1597 /* Watch out for special case. If LEN is less than LIMIT, then
1598 just do the inner printing loop.
1599 0 < len <= limit implies count = 1. */
1600
1601 /* Sort the items if they are not already sorted. */
1602 if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
1603 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1604
1605 rl_crlf ();
1606
1607 lines = 0;
1608 if (_rl_print_completions_horizontally == 0)
1609 {
1610 /* Print the sorted items, up-and-down alphabetically, like ls. */
1611 for (i = 1; i <= count; i++)
1612 {
1613 for (j = 0, l = i; j < limit; j++)
1614 {
1615 if (l > len || matches[l] == 0)
1616 break;
1617 else
1618 {
1619 temp = printable_part (matches[l]);
1620 printed_len = print_filename (temp, matches[l], sind);
1621
1622 if (j + 1 < limit)
1623 for (k = 0; k < max - printed_len; k++)
1624 putc (' ', rl_outstream);
1625 }
1626 l += count;
1627 }
1628 rl_crlf ();
1629 #if defined (SIGWINCH)
1630 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1631 #else
1632 if (RL_SIG_RECEIVED ())
1633 #endif
1634 return;
1635 lines++;
1636 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count)
1637 {
1638 lines = _rl_internal_pager (lines);
1639 if (lines < 0)
1640 return;
1641 }
1642 }
1643 }
1644 else
1645 {
1646 /* Print the sorted items, across alphabetically, like ls -x. */
1647 for (i = 1; matches[i]; i++)
1648 {
1649 temp = printable_part (matches[i]);
1650 printed_len = print_filename (temp, matches[i], sind);
1651 /* Have we reached the end of this line? */
1652 #if defined (SIGWINCH)
1653 if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
1654 #else
1655 if (RL_SIG_RECEIVED ())
1656 #endif
1657 return;
1658 if (matches[i+1])
1659 {
1660 if (limit == 1 || (i && (limit > 1) && (i % limit) == 0))
1661 {
1662 rl_crlf ();
1663 lines++;
1664 if (_rl_page_completions && lines >= _rl_screenheight - 1)
1665 {
1666 lines = _rl_internal_pager (lines);
1667 if (lines < 0)
1668 return;
1669 }
1670 }
1671 else
1672 for (k = 0; k < max - printed_len; k++)
1673 putc (' ', rl_outstream);
1674 }
1675 }
1676 rl_crlf ();
1677 }
1678 }
1679
1680 /* Display MATCHES, a list of matching filenames in argv format. This
1681 handles the simple case -- a single match -- first. If there is more
1682 than one match, we compute the number of strings in the list and the
1683 length of the longest string, which will be needed by the display
1684 function. If the application wants to handle displaying the list of
1685 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1686 address of a function, and we just call it. If we're handling the
1687 display ourselves, we just call rl_display_match_list. We also check
1688 that the list of matches doesn't exceed the user-settable threshold,
1689 and ask the user if he wants to see the list if there are more matches
1690 than RL_COMPLETION_QUERY_ITEMS. */
1691 static void
1692 display_matches (matches)
1693 char **matches;
1694 {
1695 int len, max, i;
1696 char *temp;
1697
1698 /* Move to the last visible line of a possibly-multiple-line command. */
1699 _rl_move_vert (_rl_vis_botlin);
1700
1701 /* Handle simple case first. What if there is only one answer? */
1702 if (matches[1] == 0)
1703 {
1704 temp = printable_part (matches[0]);
1705 rl_crlf ();
1706 print_filename (temp, matches[0], 0);
1707 rl_crlf ();
1708
1709 rl_forced_update_display ();
1710 rl_display_fixed = 1;
1711
1712 return;
1713 }
1714
1715 /* There is more than one answer. Find out how many there are,
1716 and find the maximum printed length of a single entry. */
1717 for (max = 0, i = 1; matches[i]; i++)
1718 {
1719 temp = printable_part (matches[i]);
1720 len = fnwidth (temp);
1721
1722 if (len > max)
1723 max = len;
1724 }
1725
1726 len = i - 1;
1727
1728 /* If the caller has defined a display hook, then call that now. */
1729 if (rl_completion_display_matches_hook)
1730 {
1731 (*rl_completion_display_matches_hook) (matches, len, max);
1732 return;
1733 }
1734
1735 /* If there are many items, then ask the user if she really wants to
1736 see them all. */
1737 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
1738 {
1739 rl_crlf ();
1740 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1741 fflush (rl_outstream);
1742 if ((completion_y_or_n = get_y_or_n (0)) == 0)
1743 {
1744 rl_crlf ();
1745
1746 rl_forced_update_display ();
1747 rl_display_fixed = 1;
1748
1749 return;
1750 }
1751 }
1752
1753 rl_display_match_list (matches, len, max);
1754
1755 rl_forced_update_display ();
1756 rl_display_fixed = 1;
1757 }
1758
1759 static char *
1760 make_quoted_replacement (match, mtype, qc)
1761 char *match;
1762 int mtype;
1763 char *qc; /* Pointer to quoting character, if any */
1764 {
1765 int should_quote, do_replace;
1766 char *replacement;
1767
1768 /* If we are doing completion on quoted substrings, and any matches
1769 contain any of the completer_word_break_characters, then auto-
1770 matically prepend the substring with a quote character (just pick
1771 the first one from the list of such) if it does not already begin
1772 with a quote string. FIXME: Need to remove any such automatically
1773 inserted quote character when it no longer is necessary, such as
1774 if we change the string we are completing on and the new set of
1775 matches don't require a quoted substring. */
1776 replacement = match;
1777
1778 should_quote = match && rl_completer_quote_characters &&
1779 rl_filename_completion_desired &&
1780 rl_filename_quoting_desired;
1781
1782 if (should_quote)
1783 should_quote = should_quote && (!qc || !*qc ||
1784 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
1785
1786 if (should_quote)
1787 {
1788 /* If there is a single match, see if we need to quote it.
1789 This also checks whether the common prefix of several
1790 matches needs to be quoted. */
1791 should_quote = rl_filename_quote_characters
1792 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0)
1793 : 0;
1794
1795 do_replace = should_quote ? mtype : NO_MATCH;
1796 /* Quote the replacement, since we found an embedded
1797 word break character in a potential match. */
1798 if (do_replace != NO_MATCH && rl_filename_quoting_function)
1799 replacement = (*rl_filename_quoting_function) (match, do_replace, qc);
1800 }
1801 return (replacement);
1802 }
1803
1804 static void
1805 insert_match (match, start, mtype, qc)
1806 char *match;
1807 int start, mtype;
1808 char *qc;
1809 {
1810 char *replacement, *r;
1811 char oqc;
1812 int end, rlen;
1813
1814 oqc = qc ? *qc : '\0';
1815 replacement = make_quoted_replacement (match, mtype, qc);
1816
1817 /* Now insert the match. */
1818 if (replacement)
1819 {
1820 rlen = strlen (replacement);
1821 /* Don't double an opening quote character. */
1822 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc &&
1823 replacement[0] == *qc)
1824 start--;
1825 /* If make_quoted_replacement changed the quoting character, remove
1826 the opening quote and insert the (fully-quoted) replacement. */
1827 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
1828 replacement[0] != oqc)
1829 start--;
1830 end = rl_point - 1;
1831 /* Don't double a closing quote character */
1832 if (qc && *qc && end && rl_line_buffer[rl_point] == *qc && replacement[rlen - 1] == *qc)
1833 end++;
1834 if (_rl_skip_completed_text)
1835 {
1836 r = replacement;
1837 while (start < rl_end && *r && rl_line_buffer[start] == *r)
1838 {
1839 start++;
1840 r++;
1841 }
1842 if (start <= end || *r)
1843 _rl_replace_text (r, start, end);
1844 rl_point = start + strlen (r);
1845 }
1846 else
1847 _rl_replace_text (replacement, start, end);
1848 if (replacement != match)
1849 xfree (replacement);
1850 }
1851 }
1852
1853 /* Append any necessary closing quote and a separator character to the
1854 just-inserted match. If the user has specified that directories
1855 should be marked by a trailing `/', append one of those instead. The
1856 default trailing character is a space. Returns the number of characters
1857 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1858 has them) and don't add a suffix for a symlink to a directory. A
1859 nontrivial match is one that actually adds to the word being completed.
1860 The variable rl_completion_mark_symlink_dirs controls this behavior
1861 (it's initially set to the what the user has chosen, indicated by the
1862 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1863 application's completion function). */
1864 static int
1865 append_to_match (text, delimiter, quote_char, nontrivial_match)
1866 char *text;
1867 int delimiter, quote_char, nontrivial_match;
1868 {
1869 char temp_string[4], *filename, *fn;
1870 int temp_string_index, s;
1871 struct stat finfo;
1872
1873 temp_string_index = 0;
1874 if (quote_char && rl_point && rl_completion_suppress_quote == 0 &&
1875 rl_line_buffer[rl_point - 1] != quote_char)
1876 temp_string[temp_string_index++] = quote_char;
1877
1878 if (delimiter)
1879 temp_string[temp_string_index++] = delimiter;
1880 else if (rl_completion_suppress_append == 0 && rl_completion_append_character)
1881 temp_string[temp_string_index++] = rl_completion_append_character;
1882
1883 temp_string[temp_string_index++] = '\0';
1884
1885 if (rl_filename_completion_desired)
1886 {
1887 filename = tilde_expand (text);
1888 if (rl_filename_stat_hook)
1889 {
1890 fn = savestring (filename);
1891 (*rl_filename_stat_hook) (&fn);
1892 xfree (filename);
1893 filename = fn;
1894 }
1895 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1896 ? LSTAT (filename, &finfo)
1897 : stat (filename, &finfo);
1898 if (s == 0 && S_ISDIR (finfo.st_mode))
1899 {
1900 if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */)
1901 {
1902 /* This is clumsy. Avoid putting in a double slash if point
1903 is at the end of the line and the previous character is a
1904 slash. */
1905 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1906 ;
1907 else if (rl_line_buffer[rl_point] != '/')
1908 rl_insert_text ("/");
1909 }
1910 }
1911 #ifdef S_ISLNK
1912 /* Don't add anything if the filename is a symlink and resolves to a
1913 directory. */
1914 else if (s == 0 && S_ISLNK (finfo.st_mode) && path_isdir (filename))
1915 ;
1916 #endif
1917 else
1918 {
1919 if (rl_point == rl_end && temp_string_index)
1920 rl_insert_text (temp_string);
1921 }
1922 xfree (filename);
1923 }
1924 else
1925 {
1926 if (rl_point == rl_end && temp_string_index)
1927 rl_insert_text (temp_string);
1928 }
1929
1930 return (temp_string_index);
1931 }
1932
1933 static void
1934 insert_all_matches (matches, point, qc)
1935 char **matches;
1936 int point;
1937 char *qc;
1938 {
1939 int i;
1940 char *rp;
1941
1942 rl_begin_undo_group ();
1943 /* remove any opening quote character; make_quoted_replacement will add
1944 it back. */
1945 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
1946 point--;
1947 rl_delete_text (point, rl_point);
1948 rl_point = point;
1949
1950 if (matches[1])
1951 {
1952 for (i = 1; matches[i]; i++)
1953 {
1954 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
1955 rl_insert_text (rp);
1956 rl_insert_text (" ");
1957 if (rp != matches[i])
1958 xfree (rp);
1959 }
1960 }
1961 else
1962 {
1963 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
1964 rl_insert_text (rp);
1965 rl_insert_text (" ");
1966 if (rp != matches[0])
1967 xfree (rp);
1968 }
1969 rl_end_undo_group ();
1970 }
1971
1972 void
1973 _rl_free_match_list (matches)
1974 char **matches;
1975 {
1976 register int i;
1977
1978 if (matches == 0)
1979 return;
1980
1981 for (i = 0; matches[i]; i++)
1982 xfree (matches[i]);
1983 xfree (matches);
1984 }
1985
1986 /* Complete the word at or before point.
1987 WHAT_TO_DO says what to do with the completion.
1988 `?' means list the possible completions.
1989 TAB means do standard completion.
1990 `*' means insert all of the possible completions.
1991 `!' means to do standard completion, and list all possible completions if
1992 there is more than one.
1993 `@' means to do standard completion, and list all possible completions if
1994 there is more than one and partial completion is not possible. */
1995 int
1996 rl_complete_internal (what_to_do)
1997 int what_to_do;
1998 {
1999 char **matches;
2000 rl_compentry_func_t *our_func;
2001 int start, end, delimiter, found_quote, i, nontrivial_lcd;
2002 char *text, *saved_line_buffer;
2003 char quote_char;
2004 #if 1
2005 int tlen, mlen;
2006 #endif
2007
2008 RL_SETSTATE(RL_STATE_COMPLETING);
2009
2010 set_completion_defaults (what_to_do);
2011
2012 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
2013 our_func = rl_completion_entry_function
2014 ? rl_completion_entry_function
2015 : rl_filename_completion_function;
2016 /* We now look backwards for the start of a filename/variable word. */
2017 end = rl_point;
2018 found_quote = delimiter = 0;
2019 quote_char = '\0';
2020
2021 if (rl_point)
2022 /* This (possibly) changes rl_point. If it returns a non-zero char,
2023 we know we have an open quote. */
2024 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2025
2026 start = rl_point;
2027 rl_point = end;
2028
2029 text = rl_copy_text (start, end);
2030 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
2031 /* nontrivial_lcd is set if the common prefix adds something to the word
2032 being completed. */
2033 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
2034 if (what_to_do == '!' || what_to_do == '@')
2035 tlen = strlen (text);
2036 xfree (text);
2037
2038 if (matches == 0)
2039 {
2040 rl_ding ();
2041 FREE (saved_line_buffer);
2042 completion_changed_buffer = 0;
2043 RL_UNSETSTATE(RL_STATE_COMPLETING);
2044 _rl_reset_completion_state ();
2045 return (0);
2046 }
2047
2048 /* If we are matching filenames, the attempted completion function will
2049 have set rl_filename_completion_desired to a non-zero value. The basic
2050 rl_filename_completion_function does this. */
2051 i = rl_filename_completion_desired;
2052
2053 if (postprocess_matches (&matches, i) == 0)
2054 {
2055 rl_ding ();
2056 FREE (saved_line_buffer);
2057 completion_changed_buffer = 0;
2058 RL_UNSETSTATE(RL_STATE_COMPLETING);
2059 _rl_reset_completion_state ();
2060 return (0);
2061 }
2062
2063 switch (what_to_do)
2064 {
2065 case TAB:
2066 case '!':
2067 case '@':
2068 /* Insert the first match with proper quoting. */
2069 if (what_to_do == TAB)
2070 {
2071 if (*matches[0])
2072 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2073 }
2074 else if (*matches[0] && matches[1] == 0)
2075 /* should we perform the check only if there are multiple matches? */
2076 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2077 else if (*matches[0]) /* what_to_do != TAB && multiple matches */
2078 {
2079 mlen = *matches[0] ? strlen (matches[0]) : 0;
2080 if (mlen >= tlen)
2081 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2082 }
2083
2084 /* If there are more matches, ring the bell to indicate.
2085 If we are in vi mode, Posix.2 says to not ring the bell.
2086 If the `show-all-if-ambiguous' variable is set, display
2087 all the matches immediately. Otherwise, if this was the
2088 only match, and we are hacking files, check the file to
2089 see if it was a directory. If so, and the `mark-directories'
2090 variable is set, add a '/' to the name. If not, and we
2091 are at the end of the line, then add a space. */
2092 if (matches[1])
2093 {
2094 if (what_to_do == '!')
2095 {
2096 display_matches (matches);
2097 break;
2098 }
2099 else if (what_to_do == '@')
2100 {
2101 if (nontrivial_lcd == 0)
2102 display_matches (matches);
2103 break;
2104 }
2105 else if (rl_editing_mode != vi_mode)
2106 rl_ding (); /* There are other matches remaining. */
2107 }
2108 else
2109 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2110
2111 break;
2112
2113 case '*':
2114 insert_all_matches (matches, start, &quote_char);
2115 break;
2116
2117 case '?':
2118 if (rl_completion_display_matches_hook == 0)
2119 {
2120 _rl_sigcleanup = _rl_complete_sigcleanup;
2121 _rl_sigcleanarg = matches;
2122 _rl_complete_display_matches_interrupt = 0;
2123 }
2124 display_matches (matches);
2125 if (_rl_complete_display_matches_interrupt)
2126 {
2127 matches = 0; /* already freed by rl_complete_sigcleanup */
2128 _rl_complete_display_matches_interrupt = 0;
2129 if (rl_signal_event_hook)
2130 (*rl_signal_event_hook) (); /* XXX */
2131 }
2132 _rl_sigcleanup = 0;
2133 _rl_sigcleanarg = 0;
2134 break;
2135
2136 default:
2137 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do);
2138 rl_ding ();
2139 FREE (saved_line_buffer);
2140 RL_UNSETSTATE(RL_STATE_COMPLETING);
2141 _rl_free_match_list (matches);
2142 _rl_reset_completion_state ();
2143 return 1;
2144 }
2145
2146 _rl_free_match_list (matches);
2147
2148 /* Check to see if the line has changed through all of this manipulation. */
2149 if (saved_line_buffer)
2150 {
2151 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
2152 xfree (saved_line_buffer);
2153 }
2154
2155 RL_UNSETSTATE(RL_STATE_COMPLETING);
2156 _rl_reset_completion_state ();
2157
2158 RL_CHECK_SIGNALS ();
2159 return 0;
2160 }
2161
2162 /***************************************************************/
2163 /* */
2164 /* Application-callable completion match generator functions */
2165 /* */
2166 /***************************************************************/
2167
2168 /* Return an array of (char *) which is a list of completions for TEXT.
2169 If there are no completions, return a NULL pointer.
2170 The first entry in the returned array is the substitution for TEXT.
2171 The remaining entries are the possible completions.
2172 The array is terminated with a NULL pointer.
2173
2174 ENTRY_FUNCTION is a function of two args, and returns a (char *).
2175 The first argument is TEXT.
2176 The second is a state argument; it should be zero on the first call, and
2177 non-zero on subsequent calls. It returns a NULL pointer to the caller
2178 when there are no more matches.
2179 */
2180 char **
2181 rl_completion_matches (text, entry_function)
2182 const char *text;
2183 rl_compentry_func_t *entry_function;
2184 {
2185 register int i;
2186
2187 /* Number of slots in match_list. */
2188 int match_list_size;
2189
2190 /* The list of matches. */
2191 char **match_list;
2192
2193 /* Number of matches actually found. */
2194 int matches;
2195
2196 /* Temporary string binder. */
2197 char *string;
2198
2199 matches = 0;
2200 match_list_size = 10;
2201 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
2202 match_list[1] = (char *)NULL;
2203
2204 while (string = (*entry_function) (text, matches))
2205 {
2206 if (RL_SIG_RECEIVED ())
2207 {
2208 /* Start at 1 because we don't set matches[0] in this function.
2209 Only free the list members if we're building match list from
2210 rl_filename_completion_function, since we know that doesn't
2211 free the strings it returns. */
2212 if (entry_function == rl_filename_completion_function)
2213 {
2214 for (i = 1; match_list[i]; i++)
2215 xfree (match_list[i]);
2216 }
2217 xfree (match_list);
2218 match_list = 0;
2219 match_list_size = 0;
2220 matches = 0;
2221 RL_CHECK_SIGNALS ();
2222 }
2223
2224 if (matches + 1 >= match_list_size)
2225 match_list = (char **)xrealloc
2226 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
2227
2228 if (match_list == 0)
2229 return (match_list);
2230
2231 match_list[++matches] = string;
2232 match_list[matches + 1] = (char *)NULL;
2233 }
2234
2235 /* If there were any matches, then look through them finding out the
2236 lowest common denominator. That then becomes match_list[0]. */
2237 if (matches)
2238 compute_lcd_of_matches (match_list, matches, text);
2239 else /* There were no matches. */
2240 {
2241 xfree (match_list);
2242 match_list = (char **)NULL;
2243 }
2244 return (match_list);
2245 }
2246
2247 /* A completion function for usernames.
2248 TEXT contains a partial username preceded by a random
2249 character (usually `~'). */
2250 char *
2251 rl_username_completion_function (text, state)
2252 const char *text;
2253 int state;
2254 {
2255 #if defined (__WIN32__) || defined (__OPENNT)
2256 return (char *)NULL;
2257 #else /* !__WIN32__ && !__OPENNT) */
2258 static char *username = (char *)NULL;
2259 static struct passwd *entry;
2260 static int namelen, first_char, first_char_loc;
2261 char *value;
2262
2263 if (state == 0)
2264 {
2265 FREE (username);
2266
2267 first_char = *text;
2268 first_char_loc = first_char == '~';
2269
2270 username = savestring (&text[first_char_loc]);
2271 namelen = strlen (username);
2272 #if defined (HAVE_GETPWENT)
2273 setpwent ();
2274 #endif
2275 }
2276
2277 #if defined (HAVE_GETPWENT)
2278 while (entry = getpwent ())
2279 {
2280 /* Null usernames should result in all users as possible completions. */
2281 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
2282 break;
2283 }
2284 #endif
2285
2286 if (entry == 0)
2287 {
2288 #if defined (HAVE_GETPWENT)
2289 endpwent ();
2290 #endif
2291 return ((char *)NULL);
2292 }
2293 else
2294 {
2295 value = (char *)xmalloc (2 + strlen (entry->pw_name));
2296
2297 *value = *text;
2298
2299 strcpy (value + first_char_loc, entry->pw_name);
2300
2301 if (first_char == '~')
2302 rl_filename_completion_desired = 1;
2303
2304 return (value);
2305 }
2306 #endif /* !__WIN32__ && !__OPENNT */
2307 }
2308
2309 /* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME
2310 (FILENAME_LEN). If _rl_completion_case_fold is set, compare without
2311 regard to the alphabetic case of characters. If
2312 _rl_completion_case_map is set, make `-' and `_' equivalent. CONVFN is
2313 the possibly-converted directory entry; FILENAME is what the user typed. */
2314 static int
2315 complete_fncmp (convfn, convlen, filename, filename_len)
2316 const char *convfn;
2317 int convlen;
2318 const char *filename;
2319 int filename_len;
2320 {
2321 register char *s1, *s2;
2322 int d, len;
2323 #if defined (HANDLE_MULTIBYTE)
2324 size_t v1, v2;
2325 mbstate_t ps1, ps2;
2326 wchar_t wc1, wc2;
2327 #endif
2328
2329 #if defined (HANDLE_MULTIBYTE)
2330 memset (&ps1, 0, sizeof (mbstate_t));
2331 memset (&ps2, 0, sizeof (mbstate_t));
2332 #endif
2333
2334 if (filename_len == 0)
2335 return 1;
2336 if (convlen < filename_len)
2337 return 0;
2338
2339 len = filename_len;
2340 s1 = (char *)convfn;
2341 s2 = (char *)filename;
2342
2343 /* Otherwise, if these match up to the length of filename, then
2344 it is a match. */
2345 if (_rl_completion_case_fold && _rl_completion_case_map)
2346 {
2347 /* Case-insensitive comparison treating _ and - as equivalent */
2348 #if defined (HANDLE_MULTIBYTE)
2349 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2350 {
2351 do
2352 {
2353 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2354 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2355 if (v1 == 0 && v2 == 0)
2356 return 1;
2357 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2358 {
2359 if (*s1 != *s2) /* do byte comparison */
2360 return 0;
2361 else if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2362 return 0;
2363 s1++; s2++; len--;
2364 continue;
2365 }
2366 wc1 = towlower (wc1);
2367 wc2 = towlower (wc2);
2368 s1 += v1;
2369 s2 += v1;
2370 len -= v1;
2371 if ((wc1 == L'-' || wc1 == L'_') && (wc2 == L'-' || wc2 == L'_'))
2372 continue;
2373 if (wc1 != wc2)
2374 return 0;
2375 }
2376 while (len != 0);
2377 }
2378 else
2379 #endif
2380 {
2381 do
2382 {
2383 d = _rl_to_lower (*s1) - _rl_to_lower (*s2);
2384 /* *s1 == [-_] && *s2 == [-_] */
2385 if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2386 d = 0;
2387 if (d != 0)
2388 return 0;
2389 s1++; s2++; /* already checked convlen >= filename_len */
2390 }
2391 while (--len != 0);
2392 }
2393
2394 return 1;
2395 }
2396 else if (_rl_completion_case_fold)
2397 {
2398 #if defined (HANDLE_MULTIBYTE)
2399 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2400 {
2401 do
2402 {
2403 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2404 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2405 if (v1 == 0 && v2 == 0)
2406 return 1;
2407 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2408 {
2409 if (*s1 != *s2) /* do byte comparison */
2410 return 0;
2411 s1++; s2++; len--;
2412 continue;
2413 }
2414 wc1 = towlower (wc1);
2415 wc2 = towlower (wc2);
2416 if (wc1 != wc2)
2417 return 0;
2418 s1 += v1;
2419 s2 += v1;
2420 len -= v1;
2421 }
2422 while (len != 0);
2423 return 1;
2424 }
2425 else
2426 #endif
2427 if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) &&
2428 (convlen >= filename_len) &&
2429 (_rl_strnicmp (filename, convfn, filename_len) == 0))
2430 return 1;
2431 }
2432 else
2433 {
2434 if ((convfn[0] == filename[0]) &&
2435 (convlen >= filename_len) &&
2436 (strncmp (filename, convfn, filename_len) == 0))
2437 return 1;
2438 }
2439 return 0;
2440 }
2441
2442 /* Okay, now we write the entry_function for filename completion. In the
2443 general case. Note that completion in the shell is a little different
2444 because of all the pathnames that must be followed when looking up the
2445 completion for a command. */
2446 char *
2447 rl_filename_completion_function (text, state)
2448 const char *text;
2449 int state;
2450 {
2451 static DIR *directory = (DIR *)NULL;
2452 static char *filename = (char *)NULL;
2453 static char *dirname = (char *)NULL;
2454 static char *users_dirname = (char *)NULL;
2455 static int filename_len;
2456 char *temp, *dentry, *convfn;
2457 int dirlen, dentlen, convlen;
2458 int tilde_dirname;
2459 struct dirent *entry;
2460
2461 /* If we don't have any state, then do some initialization. */
2462 if (state == 0)
2463 {
2464 /* If we were interrupted before closing the directory or reading
2465 all of its contents, close it. */
2466 if (directory)
2467 {
2468 closedir (directory);
2469 directory = (DIR *)NULL;
2470 }
2471 FREE (dirname);
2472 FREE (filename);
2473 FREE (users_dirname);
2474
2475 filename = savestring (text);
2476 if (*text == 0)
2477 text = ".";
2478 dirname = savestring (text);
2479
2480 temp = strrchr (dirname, '/');
2481
2482 #if defined (__MSDOS__) || defined (_WIN32)
2483 /* special hack for //X/... */
2484 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/')
2485 temp = strrchr (dirname + 3, '/');
2486 #endif
2487
2488 if (temp)
2489 {
2490 strcpy (filename, ++temp);
2491 *temp = '\0';
2492 }
2493 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN__))
2494 /* searches from current directory on the drive */
2495 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':')
2496 {
2497 strcpy (filename, dirname + 2);
2498 dirname[2] = '\0';
2499 }
2500 #endif
2501 else
2502 {
2503 dirname[0] = '.';
2504 dirname[1] = '\0';
2505 }
2506
2507 /* We aren't done yet. We also support the "~user" syntax. */
2508
2509 /* Save the version of the directory that the user typed, dequoting
2510 it if necessary. */
2511 if (rl_completion_found_quote && rl_filename_dequoting_function)
2512 users_dirname = (*rl_filename_dequoting_function) (dirname, rl_completion_quote_character);
2513 else
2514 users_dirname = savestring (dirname);
2515
2516 tilde_dirname = 0;
2517 if (*dirname == '~')
2518 {
2519 temp = tilde_expand (dirname);
2520 xfree (dirname);
2521 dirname = temp;
2522 tilde_dirname = 1;
2523 }
2524
2525 /* We have saved the possibly-dequoted version of the directory name
2526 the user typed. Now transform the directory name we're going to
2527 pass to opendir(2). The directory rewrite hook modifies only the
2528 directory name; the directory completion hook modifies both the
2529 directory name passed to opendir(2) and the version the user
2530 typed. Both the directory completion and rewrite hooks should perform
2531 any necessary dequoting. The hook functions return 1 if they modify
2532 the directory name argument. If either hook returns 0, it should
2533 not modify the directory name pointer passed as an argument. */
2534 if (rl_directory_rewrite_hook)
2535 (*rl_directory_rewrite_hook) (&dirname);
2536 else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname))
2537 {
2538 xfree (users_dirname);
2539 users_dirname = savestring (dirname);
2540 }
2541 else if (tilde_dirname == 0 && rl_completion_found_quote && rl_filename_dequoting_function)
2542 {
2543 /* delete single and double quotes */
2544 xfree (dirname);
2545 dirname = savestring (users_dirname);
2546 }
2547 directory = opendir (dirname);
2548
2549 /* Now dequote a non-null filename. FILENAME will not be NULL, but may
2550 be empty. */
2551 if (*filename && rl_completion_found_quote && rl_filename_dequoting_function)
2552 {
2553 /* delete single and double quotes */
2554 temp = (*rl_filename_dequoting_function) (filename, rl_completion_quote_character);
2555 xfree (filename);
2556 filename = temp;
2557 }
2558 filename_len = strlen (filename);
2559
2560 rl_filename_completion_desired = 1;
2561 }
2562
2563 /* At this point we should entertain the possibility of hacking wildcarded
2564 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2565 contains globbing characters, then build an array of directories, and
2566 then map over that list while completing. */
2567 /* *** UNIMPLEMENTED *** */
2568
2569 /* Now that we have some state, we can read the directory. */
2570
2571 entry = (struct dirent *)NULL;
2572 while (directory && (entry = readdir (directory)))
2573 {
2574 convfn = dentry = entry->d_name;
2575 convlen = dentlen = D_NAMLEN (entry);
2576
2577 if (rl_filename_rewrite_hook)
2578 {
2579 convfn = (*rl_filename_rewrite_hook) (dentry, dentlen);
2580 convlen = (convfn == dentry) ? dentlen : strlen (convfn);
2581 }
2582
2583 /* Special case for no filename. If the user has disabled the
2584 `match-hidden-files' variable, skip filenames beginning with `.'.
2585 All other entries except "." and ".." match. */
2586 if (filename_len == 0)
2587 {
2588 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (convfn))
2589 continue;
2590
2591 if (convfn[0] != '.' ||
2592 (convfn[1] && (convfn[1] != '.' || convfn[2])))
2593 break;
2594 }
2595 else
2596 {
2597 if (complete_fncmp (convfn, convlen, filename, filename_len))
2598 break;
2599 }
2600 }
2601
2602 if (entry == 0)
2603 {
2604 if (directory)
2605 {
2606 closedir (directory);
2607 directory = (DIR *)NULL;
2608 }
2609 if (dirname)
2610 {
2611 xfree (dirname);
2612 dirname = (char *)NULL;
2613 }
2614 if (filename)
2615 {
2616 xfree (filename);
2617 filename = (char *)NULL;
2618 }
2619 if (users_dirname)
2620 {
2621 xfree (users_dirname);
2622 users_dirname = (char *)NULL;
2623 }
2624
2625 return (char *)NULL;
2626 }
2627 else
2628 {
2629 /* dirname && (strcmp (dirname, ".") != 0) */
2630 if (dirname && (dirname[0] != '.' || dirname[1]))
2631 {
2632 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
2633 {
2634 dirlen = strlen (dirname);
2635 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
2636 strcpy (temp, dirname);
2637 /* Canonicalization cuts off any final slash present. We
2638 may need to add it back. */
2639 if (dirname[dirlen - 1] != '/')
2640 {
2641 temp[dirlen++] = '/';
2642 temp[dirlen] = '\0';
2643 }
2644 }
2645 else
2646 {
2647 dirlen = strlen (users_dirname);
2648 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
2649 strcpy (temp, users_dirname);
2650 /* Make sure that temp has a trailing slash here. */
2651 if (users_dirname[dirlen - 1] != '/')
2652 temp[dirlen++] = '/';
2653 }
2654
2655 strcpy (temp + dirlen, convfn);
2656 }
2657 else
2658 temp = savestring (convfn);
2659
2660 if (convfn != dentry)
2661 xfree (convfn);
2662
2663 return (temp);
2664 }
2665 }
2666
2667 /* An initial implementation of a menu completion function a la tcsh. The
2668 first time (if the last readline command was not rl_old_menu_complete), we
2669 generate the list of matches. This code is very similar to the code in
2670 rl_complete_internal -- there should be a way to combine the two. Then,
2671 for each item in the list of matches, we insert the match in an undoable
2672 fashion, with the appropriate character appended (this happens on the
2673 second and subsequent consecutive calls to rl_old_menu_complete). When we
2674 hit the end of the match list, we restore the original unmatched text,
2675 ring the bell, and reset the counter to zero. */
2676 int
2677 rl_old_menu_complete (count, invoking_key)
2678 int count, invoking_key;
2679 {
2680 rl_compentry_func_t *our_func;
2681 int matching_filenames, found_quote;
2682
2683 static char *orig_text;
2684 static char **matches = (char **)0;
2685 static int match_list_index = 0;
2686 static int match_list_size = 0;
2687 static int orig_start, orig_end;
2688 static char quote_char;
2689 static int delimiter;
2690
2691 /* The first time through, we generate the list of matches and set things
2692 up to insert them. */
2693 if (rl_last_func != rl_old_menu_complete)
2694 {
2695 /* Clean up from previous call, if any. */
2696 FREE (orig_text);
2697 if (matches)
2698 _rl_free_match_list (matches);
2699
2700 match_list_index = match_list_size = 0;
2701 matches = (char **)NULL;
2702
2703 rl_completion_invoking_key = invoking_key;
2704
2705 RL_SETSTATE(RL_STATE_COMPLETING);
2706
2707 /* Only the completion entry function can change these. */
2708 set_completion_defaults ('%');
2709
2710 our_func = rl_menu_completion_entry_function;
2711 if (our_func == 0)
2712 our_func = rl_completion_entry_function
2713 ? rl_completion_entry_function
2714 : rl_filename_completion_function;
2715
2716 /* We now look backwards for the start of a filename/variable word. */
2717 orig_end = rl_point;
2718 found_quote = delimiter = 0;
2719 quote_char = '\0';
2720
2721 if (rl_point)
2722 /* This (possibly) changes rl_point. If it returns a non-zero char,
2723 we know we have an open quote. */
2724 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2725
2726 orig_start = rl_point;
2727 rl_point = orig_end;
2728
2729 orig_text = rl_copy_text (orig_start, orig_end);
2730 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2731 our_func, found_quote, quote_char);
2732
2733 /* If we are matching filenames, the attempted completion function will
2734 have set rl_filename_completion_desired to a non-zero value. The basic
2735 rl_filename_completion_function does this. */
2736 matching_filenames = rl_filename_completion_desired;
2737
2738 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2739 {
2740 rl_ding ();
2741 FREE (matches);
2742 matches = (char **)0;
2743 FREE (orig_text);
2744 orig_text = (char *)0;
2745 completion_changed_buffer = 0;
2746 RL_UNSETSTATE(RL_STATE_COMPLETING);
2747 return (0);
2748 }
2749
2750 RL_UNSETSTATE(RL_STATE_COMPLETING);
2751
2752 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2753 ;
2754 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2755 code below should take care of it. */
2756
2757 if (match_list_size > 1 && _rl_complete_show_all)
2758 display_matches (matches);
2759 }
2760
2761 /* Now we have the list of matches. Replace the text between
2762 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2763 matches[match_list_index], and add any necessary closing char. */
2764
2765 if (matches == 0 || match_list_size == 0)
2766 {
2767 rl_ding ();
2768 FREE (matches);
2769 matches = (char **)0;
2770 completion_changed_buffer = 0;
2771 return (0);
2772 }
2773
2774 match_list_index += count;
2775 if (match_list_index < 0)
2776 {
2777 while (match_list_index < 0)
2778 match_list_index += match_list_size;
2779 }
2780 else
2781 match_list_index %= match_list_size;
2782
2783 if (match_list_index == 0 && match_list_size > 1)
2784 {
2785 rl_ding ();
2786 insert_match (orig_text, orig_start, MULT_MATCH, &quote_char);
2787 }
2788 else
2789 {
2790 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
2791 append_to_match (matches[match_list_index], delimiter, quote_char,
2792 strcmp (orig_text, matches[match_list_index]));
2793 }
2794
2795 completion_changed_buffer = 1;
2796 return (0);
2797 }
2798
2799 int
2800 rl_menu_complete (count, ignore)
2801 int count, ignore;
2802 {
2803 rl_compentry_func_t *our_func;
2804 int matching_filenames, found_quote;
2805
2806 static char *orig_text;
2807 static char **matches = (char **)0;
2808 static int match_list_index = 0;
2809 static int match_list_size = 0;
2810 static int nontrivial_lcd = 0;
2811 static int full_completion = 0; /* set to 1 if menu completion should reinitialize on next call */
2812 static int orig_start, orig_end;
2813 static char quote_char;
2814 static int delimiter, cstate;
2815
2816 /* The first time through, we generate the list of matches and set things
2817 up to insert them. */
2818 if ((rl_last_func != rl_menu_complete && rl_last_func != rl_backward_menu_complete) || full_completion)
2819 {
2820 /* Clean up from previous call, if any. */
2821 FREE (orig_text);
2822 if (matches)
2823 _rl_free_match_list (matches);
2824
2825 match_list_index = match_list_size = 0;
2826 matches = (char **)NULL;
2827
2828 full_completion = 0;
2829
2830 RL_SETSTATE(RL_STATE_COMPLETING);
2831
2832 /* Only the completion entry function can change these. */
2833 set_completion_defaults ('%');
2834
2835 our_func = rl_menu_completion_entry_function;
2836 if (our_func == 0)
2837 our_func = rl_completion_entry_function
2838 ? rl_completion_entry_function
2839 : rl_filename_completion_function;
2840
2841 /* We now look backwards for the start of a filename/variable word. */
2842 orig_end = rl_point;
2843 found_quote = delimiter = 0;
2844 quote_char = '\0';
2845
2846 if (rl_point)
2847 /* This (possibly) changes rl_point. If it returns a non-zero char,
2848 we know we have an open quote. */
2849 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2850
2851 orig_start = rl_point;
2852 rl_point = orig_end;
2853
2854 orig_text = rl_copy_text (orig_start, orig_end);
2855 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2856 our_func, found_quote, quote_char);
2857
2858 nontrivial_lcd = matches && strcmp (orig_text, matches[0]) != 0;
2859
2860 /* If we are matching filenames, the attempted completion function will
2861 have set rl_filename_completion_desired to a non-zero value. The basic
2862 rl_filename_completion_function does this. */
2863 matching_filenames = rl_filename_completion_desired;
2864
2865 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2866 {
2867 rl_ding ();
2868 FREE (matches);
2869 matches = (char **)0;
2870 FREE (orig_text);
2871 orig_text = (char *)0;
2872 completion_changed_buffer = 0;
2873 RL_UNSETSTATE(RL_STATE_COMPLETING);
2874 return (0);
2875 }
2876
2877 RL_UNSETSTATE(RL_STATE_COMPLETING);
2878
2879 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2880 ;
2881
2882 if (match_list_size == 0)
2883 {
2884 rl_ding ();
2885 FREE (matches);
2886 matches = (char **)0;
2887 match_list_index = 0;
2888 completion_changed_buffer = 0;
2889 return (0);
2890 }
2891
2892 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2893 code below should take care of it. */
2894 if (*matches[0])
2895 {
2896 insert_match (matches[0], orig_start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2897 orig_end = orig_start + strlen (matches[0]);
2898 completion_changed_buffer = STREQ (orig_text, matches[0]) == 0;
2899 }
2900
2901 if (match_list_size > 1 && _rl_complete_show_all)
2902 {
2903 display_matches (matches);
2904 /* If there are so many matches that the user has to be asked
2905 whether or not he wants to see the matches, menu completion
2906 is unwieldy. */
2907 if (rl_completion_query_items > 0 && match_list_size >= rl_completion_query_items)
2908 {
2909 rl_ding ();
2910 FREE (matches);
2911 matches = (char **)0;
2912 full_completion = 1;
2913 return (0);
2914 }
2915 else if (_rl_menu_complete_prefix_first)
2916 {
2917 rl_ding ();
2918 return (0);
2919 }
2920 }
2921 else if (match_list_size <= 1)
2922 {
2923 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2924 full_completion = 1;
2925 return (0);
2926 }
2927 else if (_rl_menu_complete_prefix_first && match_list_size > 1)
2928 {
2929 rl_ding ();
2930 return (0);
2931 }
2932 }
2933
2934 /* Now we have the list of matches. Replace the text between
2935 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2936 matches[match_list_index], and add any necessary closing char. */
2937
2938 if (matches == 0 || match_list_size == 0)
2939 {
2940 rl_ding ();
2941 FREE (matches);
2942 matches = (char **)0;
2943 completion_changed_buffer = 0;
2944 return (0);
2945 }
2946
2947 match_list_index += count;
2948 if (match_list_index < 0)
2949 {
2950 while (match_list_index < 0)
2951 match_list_index += match_list_size;
2952 }
2953 else
2954 match_list_index %= match_list_size;
2955
2956 if (match_list_index == 0 && match_list_size > 1)
2957 {
2958 rl_ding ();
2959 insert_match (matches[0], orig_start, MULT_MATCH, &quote_char);
2960 }
2961 else
2962 {
2963 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
2964 append_to_match (matches[match_list_index], delimiter, quote_char,
2965 strcmp (orig_text, matches[match_list_index]));
2966 }
2967
2968 completion_changed_buffer = 1;
2969 return (0);
2970 }
2971
2972 int
2973 rl_backward_menu_complete (count, key)
2974 int count, key;
2975 {
2976 /* Positive arguments to backward-menu-complete translate into negative
2977 arguments for menu-complete, and vice versa. */
2978 return (rl_menu_complete (-count, key));
2979 }
This page took 0.102338 seconds and 4 git commands to generate.