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