2011-05-11 Sterling Augustine <saugustine@google.com>
[deliverable/binutils-gdb.git] / readline / complete.c
CommitLineData
d60d9f65
SS
1/* complete.c -- filename completion for readline. */
2
cc88a640 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
SS
96
97#if defined (VISIBLE_STATS)
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. */
1b17e766
EZ
159#if defined (__MSDOS__) && !defined (__DJGPP__)
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
cc88a640
JK
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
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;
494#endif
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
SS
567#if defined (HAVE_LSTAT) && defined (S_ISLNK)
568 r = lstat (filename, &finfo);
569#else
570 r = stat (filename, &finfo);
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 {
601 if (access (filename, X_OK) == 0)
602 character = '*';
603 }
604 return (character);
605}
606#endif /* VISIBLE_STATS */
607
608/* Return the portion of PATHNAME that should be output when listing
609 possible completions. If we are hacking filename completion, we
610 are only interested in the basename, the portion following the
9255ee31
EZ
611 final slash. Otherwise, we return what we were passed. Since
612 printing empty strings is not very informative, if we're doing
613 filename completion, and the basename is the empty string, we look
614 for the previous slash and return the portion following that. If
615 there's no previous slash, we just return what we were passed. */
d60d9f65
SS
616static char *
617printable_part (pathname)
618 char *pathname;
619{
9255ee31
EZ
620 char *temp, *x;
621
622 if (rl_filename_completion_desired == 0) /* don't need to do anything */
623 return (pathname);
d60d9f65 624
9255ee31 625 temp = strrchr (pathname, '/');
1b17e766 626#if defined (__MSDOS__)
9255ee31 627 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
1b17e766
EZ
628 temp = pathname + 1;
629#endif
9255ee31
EZ
630
631 if (temp == 0 || *temp == '\0')
632 return (pathname);
633 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
634 Look for a previous slash and, if one is found, return the portion
635 following that slash. If there's no previous slash, just return the
636 pathname we were passed. */
637 else if (temp[1] == '\0')
638 {
639 for (x = temp - 1; x > pathname; x--)
640 if (*x == '/')
641 break;
642 return ((*x == '/') ? x + 1 : pathname);
643 }
644 else
645 return ++temp;
d60d9f65
SS
646}
647
5bdf8622
DJ
648/* Compute width of STRING when displayed on screen by print_filename */
649static int
650fnwidth (string)
651 const char *string;
652{
653 int width, pos;
654#if defined (HANDLE_MULTIBYTE)
655 mbstate_t ps;
656 int left, w;
657 size_t clen;
658 wchar_t wc;
659
660 left = strlen (string) + 1;
661 memset (&ps, 0, sizeof (mbstate_t));
662#endif
663
664 width = pos = 0;
665 while (string[pos])
666 {
cc88a640 667 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
5bdf8622
DJ
668 {
669 width += 2;
670 pos++;
671 }
672 else
673 {
674#if defined (HANDLE_MULTIBYTE)
675 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
676 if (MB_INVALIDCH (clen))
677 {
678 width++;
679 pos++;
680 memset (&ps, 0, sizeof (mbstate_t));
681 }
682 else if (MB_NULLWCH (clen))
683 break;
684 else
685 {
686 pos += clen;
687 w = wcwidth (wc);
688 width += (w >= 0) ? w : 1;
689 }
690#else
691 width++;
692 pos++;
693#endif
694 }
695 }
696
697 return width;
698}
699
cc88a640
JK
700#define ELLIPSIS_LEN 3
701
5bdf8622 702static int
cc88a640 703fnprint (to_print, prefix_bytes)
5bdf8622 704 const char *to_print;
cc88a640 705 int prefix_bytes;
5bdf8622 706{
cc88a640 707 int printed_len, w;
5bdf8622
DJ
708 const char *s;
709#if defined (HANDLE_MULTIBYTE)
710 mbstate_t ps;
711 const char *end;
712 size_t tlen;
cc88a640 713 int width;
5bdf8622
DJ
714 wchar_t wc;
715
716 end = to_print + strlen (to_print) + 1;
717 memset (&ps, 0, sizeof (mbstate_t));
718#endif
719
720 printed_len = 0;
cc88a640
JK
721
722 /* Don't print only the ellipsis if the common prefix is one of the
723 possible completions */
724 if (to_print[prefix_bytes] == '\0')
725 prefix_bytes = 0;
726
727 if (prefix_bytes)
728 {
729 char ellipsis;
730
731 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
732 for (w = 0; w < ELLIPSIS_LEN; w++)
733 putc (ellipsis, rl_outstream);
734 printed_len = ELLIPSIS_LEN;
735 }
736
737 s = to_print + prefix_bytes;
5bdf8622
DJ
738 while (*s)
739 {
740 if (CTRL_CHAR (*s))
741 {
742 putc ('^', rl_outstream);
743 putc (UNCTRL (*s), rl_outstream);
744 printed_len += 2;
745 s++;
746#if defined (HANDLE_MULTIBYTE)
747 memset (&ps, 0, sizeof (mbstate_t));
748#endif
749 }
750 else if (*s == RUBOUT)
751 {
752 putc ('^', rl_outstream);
753 putc ('?', rl_outstream);
754 printed_len += 2;
755 s++;
756#if defined (HANDLE_MULTIBYTE)
757 memset (&ps, 0, sizeof (mbstate_t));
758#endif
759 }
760 else
761 {
762#if defined (HANDLE_MULTIBYTE)
763 tlen = mbrtowc (&wc, s, end - s, &ps);
764 if (MB_INVALIDCH (tlen))
765 {
766 tlen = 1;
767 width = 1;
768 memset (&ps, 0, sizeof (mbstate_t));
769 }
770 else if (MB_NULLWCH (tlen))
771 break;
772 else
773 {
774 w = wcwidth (wc);
775 width = (w >= 0) ? w : 1;
776 }
777 fwrite (s, 1, tlen, rl_outstream);
778 s += tlen;
779 printed_len += width;
780#else
781 putc (*s, rl_outstream);
782 s++;
783 printed_len++;
784#endif
785 }
786 }
787
788 return printed_len;
789}
790
d60d9f65
SS
791/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
792 are using it, check for and output a single character for `special'
793 filenames. Return the number of characters we output. */
794
d60d9f65 795static int
cc88a640 796print_filename (to_print, full_pathname, prefix_bytes)
d60d9f65 797 char *to_print, *full_pathname;
cc88a640 798 int prefix_bytes;
d60d9f65 799{
5bdf8622
DJ
800 int printed_len, extension_char, slen, tlen;
801 char *s, c, *new_full_pathname, *dn;
d60d9f65 802
5bdf8622 803 extension_char = 0;
cc88a640 804 printed_len = fnprint (to_print, prefix_bytes);
d60d9f65 805
5bdf8622
DJ
806#if defined (VISIBLE_STATS)
807 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
808#else
809 if (rl_filename_completion_desired && _rl_complete_mark_directories)
810#endif
d60d9f65
SS
811 {
812 /* If to_print != full_pathname, to_print is the basename of the
813 path passed. In this case, we try to expand the directory
814 name before checking for the stat character. */
815 if (to_print != full_pathname)
816 {
817 /* Terminate the directory name. */
818 c = to_print[-1];
819 to_print[-1] = '\0';
820
1b17e766
EZ
821 /* If setting the last slash in full_pathname to a NUL results in
822 full_pathname being the empty string, we are trying to complete
823 files in the root directory. If we pass a null string to the
824 bash directory completion hook, for example, it will expand it
825 to the current directory. We just want the `/'. */
5bdf8622
DJ
826 if (full_pathname == 0 || *full_pathname == 0)
827 dn = "/";
828 else if (full_pathname[0] != '/')
829 dn = full_pathname;
830 else if (full_pathname[1] == 0)
831 dn = "//"; /* restore trailing slash to `//' */
832 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
833 dn = "/"; /* don't turn /// into // */
834 else
835 dn = full_pathname;
836 s = tilde_expand (dn);
d60d9f65
SS
837 if (rl_directory_completion_hook)
838 (*rl_directory_completion_hook) (&s);
839
840 slen = strlen (s);
841 tlen = strlen (to_print);
9255ee31 842 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
d60d9f65 843 strcpy (new_full_pathname, s);
5bdf8622
DJ
844 if (s[slen - 1] == '/')
845 slen--;
846 else
847 new_full_pathname[slen] = '/';
d60d9f65
SS
848 new_full_pathname[slen] = '/';
849 strcpy (new_full_pathname + slen + 1, to_print);
850
5bdf8622
DJ
851#if defined (VISIBLE_STATS)
852 if (rl_visible_stats)
853 extension_char = stat_char (new_full_pathname);
854 else
855#endif
856 if (path_isdir (new_full_pathname))
857 extension_char = '/';
d60d9f65 858
cc88a640 859 xfree (new_full_pathname);
d60d9f65
SS
860 to_print[-1] = c;
861 }
862 else
863 {
864 s = tilde_expand (full_pathname);
5bdf8622
DJ
865#if defined (VISIBLE_STATS)
866 if (rl_visible_stats)
867 extension_char = stat_char (s);
868 else
869#endif
870 if (path_isdir (s))
871 extension_char = '/';
d60d9f65
SS
872 }
873
cc88a640 874 xfree (s);
d60d9f65
SS
875 if (extension_char)
876 {
877 putc (extension_char, rl_outstream);
878 printed_len++;
879 }
880 }
5bdf8622 881
d60d9f65
SS
882 return printed_len;
883}
884
885static char *
886rl_quote_filename (s, rtype, qcp)
887 char *s;
888 int rtype;
889 char *qcp;
890{
891 char *r;
892
9255ee31 893 r = (char *)xmalloc (strlen (s) + 2);
d60d9f65
SS
894 *r = *rl_completer_quote_characters;
895 strcpy (r + 1, s);
896 if (qcp)
897 *qcp = *rl_completer_quote_characters;
898 return r;
899}
900
901/* Find the bounds of the current word for completion purposes, and leave
902 rl_point set to the end of the word. This function skips quoted
903 substrings (characters between matched pairs of characters in
9255ee31 904 rl_completer_quote_characters). First we try to find an unclosed
d60d9f65
SS
905 quoted substring on which to do matching. If one is not found, we use
906 the word break characters to find the boundaries of the current word.
907 We call an application-specific function to decide whether or not a
908 particular word break character is quoted; if that function returns a
909 non-zero result, the character does not break a word. This function
910 returns the opening quote character if we found an unclosed quoted
911 substring, '\0' otherwise. FP, if non-null, is set to a value saying
912 which (shell-like) quote characters we found (single quote, double
913 quote, or backslash) anywhere in the string. DP, if non-null, is set to
914 the value of the delimiter character that caused a word break. */
915
9255ee31
EZ
916char
917_rl_find_completion_word (fp, dp)
d60d9f65
SS
918 int *fp, *dp;
919{
920 int scan, end, found_quote, delimiter, pass_next, isbrk;
5bdf8622 921 char quote_char, *brkchars;
d60d9f65
SS
922
923 end = rl_point;
924 found_quote = delimiter = 0;
925 quote_char = '\0';
926
5bdf8622
DJ
927 brkchars = 0;
928 if (rl_completion_word_break_hook)
929 brkchars = (*rl_completion_word_break_hook) ();
930 if (brkchars == 0)
931 brkchars = rl_completer_word_break_characters;
932
d60d9f65
SS
933 if (rl_completer_quote_characters)
934 {
935 /* We have a list of characters which can be used in pairs to
936 quote substrings for the completer. Try to find the start
937 of an unclosed quoted substring. */
938 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
5bdf8622 939 for (scan = pass_next = 0; scan < end; scan = MB_NEXTCHAR (rl_line_buffer, scan, 1, MB_FIND_ANY))
d60d9f65
SS
940 {
941 if (pass_next)
942 {
943 pass_next = 0;
944 continue;
945 }
946
9255ee31
EZ
947 /* Shell-like semantics for single quotes -- don't allow backslash
948 to quote anything in single quotes, especially not the closing
949 quote. If you don't like this, take out the check on the value
950 of quote_char. */
951 if (quote_char != '\'' && rl_line_buffer[scan] == '\\')
d60d9f65
SS
952 {
953 pass_next = 1;
954 found_quote |= RL_QF_BACKSLASH;
955 continue;
956 }
957
958 if (quote_char != '\0')
959 {
960 /* Ignore everything until the matching close quote char. */
961 if (rl_line_buffer[scan] == quote_char)
962 {
963 /* Found matching close. Abandon this substring. */
964 quote_char = '\0';
965 rl_point = end;
966 }
967 }
968 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
969 {
970 /* Found start of a quoted substring. */
971 quote_char = rl_line_buffer[scan];
972 rl_point = scan + 1;
973 /* Shell-like quoting conventions. */
974 if (quote_char == '\'')
975 found_quote |= RL_QF_SINGLE_QUOTE;
976 else if (quote_char == '"')
977 found_quote |= RL_QF_DOUBLE_QUOTE;
9255ee31
EZ
978 else
979 found_quote |= RL_QF_OTHER_QUOTE;
d60d9f65
SS
980 }
981 }
982 }
983
984 if (rl_point == end && quote_char == '\0')
985 {
986 /* We didn't find an unclosed quoted substring upon which to do
987 completion, so use the word break characters to find the
988 substring on which to complete. */
5bdf8622 989 while (rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_ANY))
d60d9f65
SS
990 {
991 scan = rl_line_buffer[rl_point];
992
5bdf8622 993 if (strchr (brkchars, scan) == 0)
d60d9f65
SS
994 continue;
995
996 /* Call the application-specific function to tell us whether
997 this word break character is quoted and should be skipped. */
998 if (rl_char_is_quoted_p && found_quote &&
999 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
1000 continue;
1001
1002 /* Convoluted code, but it avoids an n^2 algorithm with calls
1003 to char_is_quoted. */
1004 break;
1005 }
1006 }
1007
1008 /* If we are at an unquoted word break, then advance past it. */
1009 scan = rl_line_buffer[rl_point];
1010
1011 /* If there is an application-specific function to say whether or not
1012 a character is quoted and we found a quote character, let that
1013 function decide whether or not a character is a word break, even
1b17e766
EZ
1014 if it is found in rl_completer_word_break_characters. Don't bother
1015 if we're at the end of the line, though. */
1016 if (scan)
d60d9f65 1017 {
1b17e766
EZ
1018 if (rl_char_is_quoted_p)
1019 isbrk = (found_quote == 0 ||
1020 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
5bdf8622 1021 strchr (brkchars, scan) != 0;
1b17e766 1022 else
5bdf8622 1023 isbrk = strchr (brkchars, scan) != 0;
1b17e766
EZ
1024
1025 if (isbrk)
1026 {
1027 /* If the character that caused the word break was a quoting
1028 character, then remember it as the delimiter. */
1029 if (rl_basic_quote_characters &&
1030 strchr (rl_basic_quote_characters, scan) &&
1031 (end - rl_point) > 1)
1032 delimiter = scan;
1033
1034 /* If the character isn't needed to determine something special
1035 about what kind of completion to perform, then advance past it. */
1036 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0)
1037 rl_point++;
1038 }
d60d9f65
SS
1039 }
1040
1041 if (fp)
1042 *fp = found_quote;
1043 if (dp)
1044 *dp = delimiter;
1045
1046 return (quote_char);
1047}
1048
1049static char **
1050gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
1051 char *text;
1052 int start, end;
9255ee31 1053 rl_compentry_func_t *our_func;
d60d9f65
SS
1054 int found_quote, quote_char;
1055{
cc88a640 1056 char **matches;
d60d9f65 1057
5bdf8622
DJ
1058 rl_completion_found_quote = found_quote;
1059 rl_completion_quote_character = quote_char;
1060
d60d9f65
SS
1061 /* If the user wants to TRY to complete, but then wants to give
1062 up and use the default completion function, they set the
1063 variable rl_attempted_completion_function. */
1064 if (rl_attempted_completion_function)
1065 {
cc88a640 1066 _rl_interrupt_immediately++;
d60d9f65 1067 matches = (*rl_attempted_completion_function) (text, start, end);
cc88a640
JK
1068 if (_rl_interrupt_immediately > 0)
1069 _rl_interrupt_immediately--;
d60d9f65
SS
1070
1071 if (matches || rl_attempted_completion_over)
1072 {
1073 rl_attempted_completion_over = 0;
1074 return (matches);
1075 }
1076 }
1077
cc88a640 1078 /* XXX -- filename dequoting moved into rl_filename_completion_function */
d60d9f65 1079
9255ee31 1080 matches = rl_completion_matches (text, our_func);
d60d9f65
SS
1081 return matches;
1082}
1083
1084/* Filter out duplicates in MATCHES. This frees up the strings in
1085 MATCHES. */
1086static char **
1087remove_duplicate_matches (matches)
1088 char **matches;
1089{
1090 char *lowest_common;
1091 int i, j, newlen;
1092 char dead_slot;
1093 char **temp_array;
1094
1095 /* Sort the items. */
1096 for (i = 0; matches[i]; i++)
1097 ;
1098
1099 /* Sort the array without matches[0], since we need it to
1100 stay in place no matter what. */
cc88a640 1101 if (i && rl_sort_completion_matches)
1b17e766 1102 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
d60d9f65
SS
1103
1104 /* Remember the lowest common denominator for it may be unique. */
1105 lowest_common = savestring (matches[0]);
1106
1107 for (i = newlen = 0; matches[i + 1]; i++)
1108 {
1109 if (strcmp (matches[i], matches[i + 1]) == 0)
1110 {
cc88a640 1111 xfree (matches[i]);
d60d9f65
SS
1112 matches[i] = (char *)&dead_slot;
1113 }
1114 else
1115 newlen++;
1116 }
1117
1118 /* We have marked all the dead slots with (char *)&dead_slot.
1119 Copy all the non-dead entries into a new array. */
1120 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
1121 for (i = j = 1; matches[i]; i++)
1122 {
1123 if (matches[i] != (char *)&dead_slot)
1124 temp_array[j++] = matches[i];
1125 }
1126 temp_array[j] = (char *)NULL;
1127
1128 if (matches[0] != (char *)&dead_slot)
cc88a640 1129 xfree (matches[0]);
d60d9f65
SS
1130
1131 /* Place the lowest common denominator back in [0]. */
1132 temp_array[0] = lowest_common;
1133
1134 /* If there is one string left, and it is identical to the
1135 lowest common denominator, then the LCD is the string to
1136 insert. */
1137 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
1138 {
cc88a640 1139 xfree (temp_array[1]);
d60d9f65
SS
1140 temp_array[1] = (char *)NULL;
1141 }
1142 return (temp_array);
1143}
1144
1145/* Find the common prefix of the list of matches, and put it into
1146 matches[0]. */
1147static int
1148compute_lcd_of_matches (match_list, matches, text)
1149 char **match_list;
1150 int matches;
9255ee31 1151 const char *text;
d60d9f65
SS
1152{
1153 register int i, c1, c2, si;
1154 int low; /* Count of max-matched characters. */
5bdf8622 1155 char *dtext; /* dequoted TEXT, if needed */
9255ee31
EZ
1156#if defined (HANDLE_MULTIBYTE)
1157 int v;
1158 mbstate_t ps1, ps2;
1159 wchar_t wc1, wc2;
1160#endif
d60d9f65
SS
1161
1162 /* If only one match, just use that. Otherwise, compare each
1163 member of the list with the next, finding out where they
1164 stop matching. */
1165 if (matches == 1)
1166 {
1167 match_list[0] = match_list[1];
1168 match_list[1] = (char *)NULL;
1169 return 1;
1170 }
1171
1172 for (i = 1, low = 100000; i < matches; i++)
1173 {
9255ee31
EZ
1174#if defined (HANDLE_MULTIBYTE)
1175 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1176 {
1177 memset (&ps1, 0, sizeof (mbstate_t));
1178 memset (&ps2, 0, sizeof (mbstate_t));
1179 }
1180#endif
d60d9f65
SS
1181 if (_rl_completion_case_fold)
1182 {
1183 for (si = 0;
1184 (c1 = _rl_to_lower(match_list[i][si])) &&
1185 (c2 = _rl_to_lower(match_list[i + 1][si]));
1186 si++)
9255ee31
EZ
1187#if defined (HANDLE_MULTIBYTE)
1188 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1189 {
1190 v = mbrtowc (&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1);
1191 mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2);
1192 wc1 = towlower (wc1);
1193 wc2 = towlower (wc2);
1194 if (wc1 != wc2)
1195 break;
1196 else if (v > 1)
1197 si += v - 1;
1198 }
1199 else
1200#endif
d60d9f65
SS
1201 if (c1 != c2)
1202 break;
1203 }
1204 else
1205 {
1206 for (si = 0;
1207 (c1 = match_list[i][si]) &&
1208 (c2 = match_list[i + 1][si]);
1209 si++)
9255ee31
EZ
1210#if defined (HANDLE_MULTIBYTE)
1211 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1212 {
cc88a640
JK
1213 mbstate_t ps_back;
1214 ps_back = ps1;
9255ee31
EZ
1215 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2))
1216 break;
1217 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1)
1218 si += v - 1;
1219 }
1220 else
1221#endif
d60d9f65
SS
1222 if (c1 != c2)
1223 break;
1224 }
1225
1226 if (low > si)
1227 low = si;
1228 }
1229
1230 /* If there were multiple matches, but none matched up to even the
1231 first character, and the user typed something, use that as the
1232 value of matches[0]. */
1233 if (low == 0 && text && *text)
1234 {
9255ee31 1235 match_list[0] = (char *)xmalloc (strlen (text) + 1);
d60d9f65
SS
1236 strcpy (match_list[0], text);
1237 }
1238 else
1239 {
9255ee31
EZ
1240 match_list[0] = (char *)xmalloc (low + 1);
1241
1242 /* XXX - this might need changes in the presence of multibyte chars */
1243
1244 /* If we are ignoring case, try to preserve the case of the string
1245 the user typed in the face of multiple matches differing in case. */
1246 if (_rl_completion_case_fold)
1247 {
5bdf8622
DJ
1248 /* We're making an assumption here:
1249 IF we're completing filenames AND
1250 the application has defined a filename dequoting function AND
1251 we found a quote character AND
1252 the application has requested filename quoting
1253 THEN
1254 we assume that TEXT was dequoted before checking against
1255 the file system and needs to be dequoted here before we
1256 check against the list of matches
1257 FI */
1258 dtext = (char *)NULL;
1259 if (rl_filename_completion_desired &&
1260 rl_filename_dequoting_function &&
1261 rl_completion_found_quote &&
1262 rl_filename_quoting_desired)
1263 {
1264 dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
1265 text = dtext;
1266 }
1267
9255ee31
EZ
1268 /* sort the list to get consistent answers. */
1269 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1270
1271 si = strlen (text);
1272 if (si <= low)
1273 {
1274 for (i = 1; i <= matches; i++)
1275 if (strncmp (match_list[i], text, si) == 0)
1276 {
1277 strncpy (match_list[0], match_list[i], low);
1278 break;
1279 }
1280 /* no casematch, use first entry */
1281 if (i > matches)
1282 strncpy (match_list[0], match_list[1], low);
1283 }
1284 else
1285 /* otherwise, just use the text the user typed. */
1286 strncpy (match_list[0], text, low);
5bdf8622
DJ
1287
1288 FREE (dtext);
9255ee31
EZ
1289 }
1290 else
1291 strncpy (match_list[0], match_list[1], low);
1292
d60d9f65
SS
1293 match_list[0][low] = '\0';
1294 }
1295
1296 return matches;
1297}
1298
1299static int
c862e87b 1300postprocess_matches (matchesp, matching_filenames)
d60d9f65
SS
1301 char ***matchesp;
1302 int matching_filenames;
1303{
1304 char *t, **matches, **temp_matches;
1305 int nmatch, i;
1306
1307 matches = *matchesp;
1308
9255ee31
EZ
1309 if (matches == 0)
1310 return 0;
1311
d60d9f65
SS
1312 /* It seems to me that in all the cases we handle we would like
1313 to ignore duplicate possiblilities. Scan for the text to
1314 insert being identical to the other completions. */
1315 if (rl_ignore_completion_duplicates)
1316 {
1317 temp_matches = remove_duplicate_matches (matches);
cc88a640 1318 xfree (matches);
d60d9f65
SS
1319 matches = temp_matches;
1320 }
1321
1322 /* If we are matching filenames, then here is our chance to
1323 do clever processing by re-examining the list. Call the
1324 ignore function with the array as a parameter. It can
1325 munge the array, deleting matches as it desires. */
1326 if (rl_ignore_some_completions_function && matching_filenames)
1327 {
1328 for (nmatch = 1; matches[nmatch]; nmatch++)
1329 ;
1330 (void)(*rl_ignore_some_completions_function) (matches);
1331 if (matches == 0 || matches[0] == 0)
1332 {
1333 FREE (matches);
d60d9f65
SS
1334 *matchesp = (char **)0;
1335 return 0;
1336 }
1337 else
1338 {
1339 /* If we removed some matches, recompute the common prefix. */
1340 for (i = 1; matches[i]; i++)
1341 ;
1342 if (i > 1 && i < nmatch)
1343 {
1344 t = matches[0];
c862e87b 1345 compute_lcd_of_matches (matches, i - 1, t);
d60d9f65
SS
1346 FREE (t);
1347 }
1348 }
1349 }
1350
1351 *matchesp = matches;
1352 return (1);
1353}
1354
cc88a640
JK
1355static int
1356complete_get_screenwidth ()
1357{
1358 int cols;
1359 char *envcols;
1360
1361 cols = _rl_completion_columns;
1362 if (cols >= 0 && cols <= _rl_screenwidth)
1363 return cols;
1364 envcols = getenv ("COLUMNS");
1365 if (envcols && *envcols)
1366 cols = atoi (envcols);
1367 if (cols >= 0 && cols <= _rl_screenwidth)
1368 return cols;
1369 return _rl_screenwidth;
1370}
1371
c862e87b
JM
1372/* A convenience function for displaying a list of strings in
1373 columnar format on readline's output stream. MATCHES is the list
1374 of strings, in argv format, LEN is the number of strings in MATCHES,
1375 and MAX is the length of the longest string in MATCHES. */
1376void
1377rl_display_match_list (matches, len, max)
d60d9f65 1378 char **matches;
c862e87b 1379 int len, max;
d60d9f65 1380{
cc88a640
JK
1381 int count, limit, printed_len, lines, cols;
1382 int i, j, k, l, common_length, sind;
1383 char *temp, *t;
1384
1385 /* Find the length of the prefix common to all items: length as displayed
1386 characters (common_length) and as a byte index into the matches (sind) */
1387 common_length = sind = 0;
1388 if (_rl_completion_prefix_display_length > 0)
1389 {
1390 t = printable_part (matches[0]);
1391 temp = strrchr (t, '/');
1392 common_length = temp ? fnwidth (temp) : fnwidth (t);
1393 sind = temp ? strlen (temp) : strlen (t);
1394
1395 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
1396 max -= common_length - ELLIPSIS_LEN;
1397 else
1398 common_length = sind = 0;
1399 }
d60d9f65 1400
d60d9f65 1401 /* How many items of MAX length can we fit in the screen window? */
cc88a640 1402 cols = complete_get_screenwidth ();
d60d9f65 1403 max += 2;
cc88a640
JK
1404 limit = cols / max;
1405 if (limit != 1 && (limit * max == cols))
d60d9f65
SS
1406 limit--;
1407
cc88a640
JK
1408 /* If cols == 0, limit will end up -1 */
1409 if (cols < _rl_screenwidth && limit < 0)
1410 limit = 1;
1411
1412 /* Avoid a possible floating exception. If max > cols,
d60d9f65
SS
1413 limit will be 0 and a divide-by-zero fault will result. */
1414 if (limit == 0)
1415 limit = 1;
1416
1417 /* How many iterations of the printing loop? */
1418 count = (len + (limit - 1)) / limit;
1419
1420 /* Watch out for special case. If LEN is less than LIMIT, then
1421 just do the inner printing loop.
1422 0 < len <= limit implies count = 1. */
1423
1424 /* Sort the items if they are not already sorted. */
cc88a640 1425 if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
1b17e766 1426 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
d60d9f65 1427
9255ee31 1428 rl_crlf ();
d60d9f65 1429
9255ee31 1430 lines = 0;
d60d9f65
SS
1431 if (_rl_print_completions_horizontally == 0)
1432 {
1433 /* Print the sorted items, up-and-down alphabetically, like ls. */
1434 for (i = 1; i <= count; i++)
1435 {
1436 for (j = 0, l = i; j < limit; j++)
1437 {
1438 if (l > len || matches[l] == 0)
1439 break;
1440 else
1441 {
1442 temp = printable_part (matches[l]);
cc88a640 1443 printed_len = print_filename (temp, matches[l], sind);
d60d9f65
SS
1444
1445 if (j + 1 < limit)
1446 for (k = 0; k < max - printed_len; k++)
1447 putc (' ', rl_outstream);
1448 }
1449 l += count;
1450 }
9255ee31
EZ
1451 rl_crlf ();
1452 lines++;
1453 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count)
1454 {
1455 lines = _rl_internal_pager (lines);
1456 if (lines < 0)
1457 return;
1458 }
d60d9f65
SS
1459 }
1460 }
1461 else
1462 {
1463 /* Print the sorted items, across alphabetically, like ls -x. */
1464 for (i = 1; matches[i]; i++)
1465 {
1466 temp = printable_part (matches[i]);
cc88a640 1467 printed_len = print_filename (temp, matches[i], sind);
d60d9f65
SS
1468 /* Have we reached the end of this line? */
1469 if (matches[i+1])
1470 {
1471 if (i && (limit > 1) && (i % limit) == 0)
9255ee31
EZ
1472 {
1473 rl_crlf ();
1474 lines++;
1475 if (_rl_page_completions && lines >= _rl_screenheight - 1)
1476 {
1477 lines = _rl_internal_pager (lines);
1478 if (lines < 0)
1479 return;
1480 }
1481 }
d60d9f65
SS
1482 else
1483 for (k = 0; k < max - printed_len; k++)
1484 putc (' ', rl_outstream);
1485 }
1486 }
9255ee31 1487 rl_crlf ();
d60d9f65 1488 }
c862e87b
JM
1489}
1490
1491/* Display MATCHES, a list of matching filenames in argv format. This
1492 handles the simple case -- a single match -- first. If there is more
1493 than one match, we compute the number of strings in the list and the
1494 length of the longest string, which will be needed by the display
1495 function. If the application wants to handle displaying the list of
1496 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1497 address of a function, and we just call it. If we're handling the
1498 display ourselves, we just call rl_display_match_list. We also check
1499 that the list of matches doesn't exceed the user-settable threshold,
1500 and ask the user if he wants to see the list if there are more matches
1501 than RL_COMPLETION_QUERY_ITEMS. */
1502static void
1503display_matches (matches)
1504 char **matches;
1505{
1506 int len, max, i;
1507 char *temp;
1508
1509 /* Move to the last visible line of a possibly-multiple-line command. */
1510 _rl_move_vert (_rl_vis_botlin);
1511
1512 /* Handle simple case first. What if there is only one answer? */
1513 if (matches[1] == 0)
1514 {
1515 temp = printable_part (matches[0]);
9255ee31 1516 rl_crlf ();
cc88a640 1517 print_filename (temp, matches[0], 0);
9255ee31 1518 rl_crlf ();
c862e87b
JM
1519
1520 rl_forced_update_display ();
1521 rl_display_fixed = 1;
1522
1523 return;
1524 }
1525
1526 /* There is more than one answer. Find out how many there are,
1527 and find the maximum printed length of a single entry. */
1528 for (max = 0, i = 1; matches[i]; i++)
1529 {
1530 temp = printable_part (matches[i]);
5bdf8622 1531 len = fnwidth (temp);
c862e87b
JM
1532
1533 if (len > max)
1534 max = len;
1535 }
1536
1537 len = i - 1;
1538
1539 /* If the caller has defined a display hook, then call that now. */
1540 if (rl_completion_display_matches_hook)
1541 {
1542 (*rl_completion_display_matches_hook) (matches, len, max);
1543 return;
1544 }
1545
1546 /* If there are many items, then ask the user if she really wants to
1547 see them all. */
5bdf8622 1548 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
c862e87b 1549 {
9255ee31 1550 rl_crlf ();
c862e87b
JM
1551 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1552 fflush (rl_outstream);
cc88a640 1553 if ((completion_y_or_n = get_y_or_n (0)) == 0)
c862e87b 1554 {
9255ee31 1555 rl_crlf ();
c862e87b
JM
1556
1557 rl_forced_update_display ();
1558 rl_display_fixed = 1;
1559
1560 return;
1561 }
1562 }
1563
1564 rl_display_match_list (matches, len, max);
d60d9f65 1565
d60d9f65
SS
1566 rl_forced_update_display ();
1567 rl_display_fixed = 1;
d60d9f65
SS
1568}
1569
1570static char *
1571make_quoted_replacement (match, mtype, qc)
1572 char *match;
1573 int mtype;
1574 char *qc; /* Pointer to quoting character, if any */
1575{
1576 int should_quote, do_replace;
1577 char *replacement;
1578
1579 /* If we are doing completion on quoted substrings, and any matches
1580 contain any of the completer_word_break_characters, then auto-
1581 matically prepend the substring with a quote character (just pick
1582 the first one from the list of such) if it does not already begin
1583 with a quote string. FIXME: Need to remove any such automatically
1584 inserted quote character when it no longer is necessary, such as
1585 if we change the string we are completing on and the new set of
1586 matches don't require a quoted substring. */
1587 replacement = match;
1588
1589 should_quote = match && rl_completer_quote_characters &&
1590 rl_filename_completion_desired &&
1591 rl_filename_quoting_desired;
1592
1593 if (should_quote)
c862e87b
JM
1594 should_quote = should_quote && (!qc || !*qc ||
1595 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
d60d9f65
SS
1596
1597 if (should_quote)
1598 {
1599 /* If there is a single match, see if we need to quote it.
1600 This also checks whether the common prefix of several
1601 matches needs to be quoted. */
1602 should_quote = rl_filename_quote_characters
9255ee31 1603 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0)
d60d9f65
SS
1604 : 0;
1605
1606 do_replace = should_quote ? mtype : NO_MATCH;
1607 /* Quote the replacement, since we found an embedded
1608 word break character in a potential match. */
1609 if (do_replace != NO_MATCH && rl_filename_quoting_function)
1610 replacement = (*rl_filename_quoting_function) (match, do_replace, qc);
1611 }
1612 return (replacement);
1613}
1614
1615static void
1616insert_match (match, start, mtype, qc)
1617 char *match;
1618 int start, mtype;
1619 char *qc;
1620{
cc88a640 1621 char *replacement, *r;
d60d9f65 1622 char oqc;
cc88a640 1623 int end, rlen;
d60d9f65
SS
1624
1625 oqc = qc ? *qc : '\0';
1626 replacement = make_quoted_replacement (match, mtype, qc);
1627
1628 /* Now insert the match. */
1629 if (replacement)
1630 {
cc88a640 1631 rlen = strlen (replacement);
d60d9f65
SS
1632 /* Don't double an opening quote character. */
1633 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc &&
1634 replacement[0] == *qc)
1635 start--;
1636 /* If make_quoted_replacement changed the quoting character, remove
1637 the opening quote and insert the (fully-quoted) replacement. */
1638 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
1639 replacement[0] != oqc)
1640 start--;
cc88a640
JK
1641 end = rl_point - 1;
1642 /* Don't double a closing quote character */
1643 if (qc && *qc && end && rl_line_buffer[rl_point] == *qc && replacement[rlen - 1] == *qc)
1644 end++;
1645 if (_rl_skip_completed_text)
1646 {
1647 r = replacement;
1648 while (start < rl_end && *r && rl_line_buffer[start] == *r)
1649 {
1650 start++;
1651 r++;
1652 }
1653 if (start <= end || *r)
1654 _rl_replace_text (r, start, end);
1655 rl_point = start + strlen (r);
1656 }
1657 else
1658 _rl_replace_text (replacement, start, end);
d60d9f65 1659 if (replacement != match)
cc88a640 1660 xfree (replacement);
d60d9f65
SS
1661 }
1662}
1663
1664/* Append any necessary closing quote and a separator character to the
1665 just-inserted match. If the user has specified that directories
1666 should be marked by a trailing `/', append one of those instead. The
1667 default trailing character is a space. Returns the number of characters
9255ee31
EZ
1668 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1669 has them) and don't add a suffix for a symlink to a directory. A
1670 nontrivial match is one that actually adds to the word being completed.
1671 The variable rl_completion_mark_symlink_dirs controls this behavior
1672 (it's initially set to the what the user has chosen, indicated by the
1673 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1674 application's completion function). */
d60d9f65 1675static int
9255ee31 1676append_to_match (text, delimiter, quote_char, nontrivial_match)
d60d9f65 1677 char *text;
9255ee31 1678 int delimiter, quote_char, nontrivial_match;
d60d9f65
SS
1679{
1680 char temp_string[4], *filename;
9255ee31 1681 int temp_string_index, s;
d60d9f65
SS
1682 struct stat finfo;
1683
1684 temp_string_index = 0;
5bdf8622
DJ
1685 if (quote_char && rl_point && rl_completion_suppress_quote == 0 &&
1686 rl_line_buffer[rl_point - 1] != quote_char)
d60d9f65
SS
1687 temp_string[temp_string_index++] = quote_char;
1688
1689 if (delimiter)
1690 temp_string[temp_string_index++] = delimiter;
9255ee31 1691 else if (rl_completion_suppress_append == 0 && rl_completion_append_character)
d60d9f65
SS
1692 temp_string[temp_string_index++] = rl_completion_append_character;
1693
1694 temp_string[temp_string_index++] = '\0';
1695
1696 if (rl_filename_completion_desired)
1697 {
1698 filename = tilde_expand (text);
9255ee31
EZ
1699 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1700 ? LSTAT (filename, &finfo)
1701 : stat (filename, &finfo);
1702 if (s == 0 && S_ISDIR (finfo.st_mode))
d60d9f65 1703 {
5bdf8622 1704 if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */)
9255ee31
EZ
1705 {
1706 /* This is clumsy. Avoid putting in a double slash if point
1707 is at the end of the line and the previous character is a
1708 slash. */
1709 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1710 ;
1711 else if (rl_line_buffer[rl_point] != '/')
1712 rl_insert_text ("/");
1713 }
d60d9f65 1714 }
9255ee31
EZ
1715#ifdef S_ISLNK
1716 /* Don't add anything if the filename is a symlink and resolves to a
1717 directory. */
1718 else if (s == 0 && S_ISLNK (finfo.st_mode) &&
1719 stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode))
1720 ;
1721#endif
d60d9f65
SS
1722 else
1723 {
9255ee31 1724 if (rl_point == rl_end && temp_string_index)
d60d9f65
SS
1725 rl_insert_text (temp_string);
1726 }
cc88a640 1727 xfree (filename);
d60d9f65
SS
1728 }
1729 else
1730 {
9255ee31 1731 if (rl_point == rl_end && temp_string_index)
d60d9f65
SS
1732 rl_insert_text (temp_string);
1733 }
1734
1735 return (temp_string_index);
1736}
1737
1738static void
1739insert_all_matches (matches, point, qc)
1740 char **matches;
1741 int point;
1742 char *qc;
1743{
1744 int i;
1745 char *rp;
1746
1747 rl_begin_undo_group ();
1748 /* remove any opening quote character; make_quoted_replacement will add
1749 it back. */
1750 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
1751 point--;
1752 rl_delete_text (point, rl_point);
1753 rl_point = point;
1754
1755 if (matches[1])
1756 {
1757 for (i = 1; matches[i]; i++)
1758 {
1759 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
1760 rl_insert_text (rp);
1761 rl_insert_text (" ");
1762 if (rp != matches[i])
cc88a640 1763 xfree (rp);
d60d9f65
SS
1764 }
1765 }
1766 else
1767 {
1768 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
1769 rl_insert_text (rp);
1770 rl_insert_text (" ");
1771 if (rp != matches[0])
cc88a640 1772 xfree (rp);
d60d9f65
SS
1773 }
1774 rl_end_undo_group ();
1775}
1776
9255ee31
EZ
1777void
1778_rl_free_match_list (matches)
c862e87b
JM
1779 char **matches;
1780{
1781 register int i;
1782
9255ee31
EZ
1783 if (matches == 0)
1784 return;
1785
c862e87b 1786 for (i = 0; matches[i]; i++)
cc88a640
JK
1787 xfree (matches[i]);
1788 xfree (matches);
c862e87b
JM
1789}
1790
d60d9f65
SS
1791/* Complete the word at or before point.
1792 WHAT_TO_DO says what to do with the completion.
1793 `?' means list the possible completions.
1794 TAB means do standard completion.
1795 `*' means insert all of the possible completions.
1796 `!' means to do standard completion, and list all possible completions if
5bdf8622
DJ
1797 there is more than one.
1798 `@' means to do standard completion, and list all possible completions if
1799 there is more than one and partial completion is not possible. */
d60d9f65
SS
1800int
1801rl_complete_internal (what_to_do)
1802 int what_to_do;
1803{
1804 char **matches;
9255ee31
EZ
1805 rl_compentry_func_t *our_func;
1806 int start, end, delimiter, found_quote, i, nontrivial_lcd;
d60d9f65
SS
1807 char *text, *saved_line_buffer;
1808 char quote_char;
cc88a640
JK
1809#if 1
1810 int tlen, mlen;
1811#endif
d60d9f65 1812
9255ee31
EZ
1813 RL_SETSTATE(RL_STATE_COMPLETING);
1814
1815 set_completion_defaults (what_to_do);
d60d9f65
SS
1816
1817 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
1818 our_func = rl_completion_entry_function
1819 ? rl_completion_entry_function
9255ee31 1820 : rl_filename_completion_function;
d60d9f65
SS
1821 /* We now look backwards for the start of a filename/variable word. */
1822 end = rl_point;
1823 found_quote = delimiter = 0;
1824 quote_char = '\0';
1825
1826 if (rl_point)
1827 /* This (possibly) changes rl_point. If it returns a non-zero char,
1828 we know we have an open quote. */
9255ee31 1829 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
d60d9f65
SS
1830
1831 start = rl_point;
1832 rl_point = end;
1833
1834 text = rl_copy_text (start, end);
1835 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
9255ee31
EZ
1836 /* nontrivial_lcd is set if the common prefix adds something to the word
1837 being completed. */
1838 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
cc88a640
JK
1839#if 1
1840 if (what_to_do == '!' || what_to_do == '@')
1841 tlen = strlen (text);
1842#endif
1843 xfree (text);
d60d9f65
SS
1844
1845 if (matches == 0)
1846 {
9255ee31 1847 rl_ding ();
d60d9f65 1848 FREE (saved_line_buffer);
9255ee31
EZ
1849 completion_changed_buffer = 0;
1850 RL_UNSETSTATE(RL_STATE_COMPLETING);
cc88a640 1851 _rl_reset_completion_state ();
d60d9f65
SS
1852 return (0);
1853 }
1854
c862e87b
JM
1855 /* If we are matching filenames, the attempted completion function will
1856 have set rl_filename_completion_desired to a non-zero value. The basic
9255ee31 1857 rl_filename_completion_function does this. */
c862e87b 1858 i = rl_filename_completion_desired;
c862e87b
JM
1859
1860 if (postprocess_matches (&matches, i) == 0)
d60d9f65 1861 {
9255ee31 1862 rl_ding ();
d60d9f65 1863 FREE (saved_line_buffer);
c862e87b 1864 completion_changed_buffer = 0;
9255ee31 1865 RL_UNSETSTATE(RL_STATE_COMPLETING);
cc88a640 1866 _rl_reset_completion_state ();
d60d9f65
SS
1867 return (0);
1868 }
1869
d60d9f65
SS
1870 switch (what_to_do)
1871 {
1872 case TAB:
1873 case '!':
5bdf8622 1874 case '@':
d60d9f65 1875 /* Insert the first match with proper quoting. */
cc88a640 1876#if 0
d60d9f65
SS
1877 if (*matches[0])
1878 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
cc88a640
JK
1879#else
1880 if (what_to_do == TAB)
1881 {
1882 if (*matches[0])
1883 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
1884 }
1885 else if (*matches[0] && matches[1] == 0)
1886 /* should we perform the check only if there are multiple matches? */
1887 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
1888 else if (*matches[0]) /* what_to_do != TAB && multiple matches */
1889 {
1890 mlen = *matches[0] ? strlen (matches[0]) : 0;
1891 if (mlen >= tlen)
1892 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
1893 }
1894#endif
d60d9f65
SS
1895
1896 /* If there are more matches, ring the bell to indicate.
1897 If we are in vi mode, Posix.2 says to not ring the bell.
1898 If the `show-all-if-ambiguous' variable is set, display
1899 all the matches immediately. Otherwise, if this was the
1900 only match, and we are hacking files, check the file to
1901 see if it was a directory. If so, and the `mark-directories'
1902 variable is set, add a '/' to the name. If not, and we
1903 are at the end of the line, then add a space. */
1904 if (matches[1])
1905 {
1906 if (what_to_do == '!')
1907 {
1908 display_matches (matches);
1909 break;
1910 }
5bdf8622
DJ
1911 else if (what_to_do == '@')
1912 {
1913 if (nontrivial_lcd == 0)
1914 display_matches (matches);
1915 break;
1916 }
d60d9f65 1917 else if (rl_editing_mode != vi_mode)
9255ee31 1918 rl_ding (); /* There are other matches remaining. */
d60d9f65
SS
1919 }
1920 else
9255ee31 1921 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
d60d9f65
SS
1922
1923 break;
1924
1925 case '*':
1926 insert_all_matches (matches, start, &quote_char);
1927 break;
1928
1929 case '?':
1930 display_matches (matches);
1931 break;
1932
1933 default:
cc88a640 1934 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do);
9255ee31 1935 rl_ding ();
d60d9f65 1936 FREE (saved_line_buffer);
9255ee31 1937 RL_UNSETSTATE(RL_STATE_COMPLETING);
cc88a640 1938 _rl_reset_completion_state ();
d60d9f65
SS
1939 return 1;
1940 }
1941
9255ee31 1942 _rl_free_match_list (matches);
d60d9f65
SS
1943
1944 /* Check to see if the line has changed through all of this manipulation. */
1945 if (saved_line_buffer)
1946 {
1947 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
cc88a640 1948 xfree (saved_line_buffer);
d60d9f65
SS
1949 }
1950
9255ee31 1951 RL_UNSETSTATE(RL_STATE_COMPLETING);
cc88a640 1952 _rl_reset_completion_state ();
d60d9f65
SS
1953 return 0;
1954}
1955
1956/***************************************************************/
1957/* */
1958/* Application-callable completion match generator functions */
1959/* */
1960/***************************************************************/
1961
1962/* Return an array of (char *) which is a list of completions for TEXT.
1963 If there are no completions, return a NULL pointer.
1964 The first entry in the returned array is the substitution for TEXT.
1965 The remaining entries are the possible completions.
1966 The array is terminated with a NULL pointer.
1967
1968 ENTRY_FUNCTION is a function of two args, and returns a (char *).
1969 The first argument is TEXT.
1970 The second is a state argument; it should be zero on the first call, and
1971 non-zero on subsequent calls. It returns a NULL pointer to the caller
1972 when there are no more matches.
1973 */
1974char **
9255ee31
EZ
1975rl_completion_matches (text, entry_function)
1976 const char *text;
1977 rl_compentry_func_t *entry_function;
d60d9f65
SS
1978{
1979 /* Number of slots in match_list. */
1980 int match_list_size;
1981
1982 /* The list of matches. */
1983 char **match_list;
1984
1985 /* Number of matches actually found. */
1986 int matches;
1987
1988 /* Temporary string binder. */
1989 char *string;
1990
1991 matches = 0;
1992 match_list_size = 10;
1993 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
1994 match_list[1] = (char *)NULL;
1995
cc88a640 1996 _rl_interrupt_immediately++;
729b8652 1997 while (string = (*entry_function) (text, matches))
d60d9f65
SS
1998 {
1999 if (matches + 1 == match_list_size)
2000 match_list = (char **)xrealloc
2001 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
2002
2003 match_list[++matches] = string;
2004 match_list[matches + 1] = (char *)NULL;
2005 }
cc88a640
JK
2006 if (_rl_interrupt_immediately > 0)
2007 _rl_interrupt_immediately--;
d60d9f65
SS
2008
2009 /* If there were any matches, then look through them finding out the
2010 lowest common denominator. That then becomes match_list[0]. */
2011 if (matches)
2012 compute_lcd_of_matches (match_list, matches, text);
2013 else /* There were no matches. */
2014 {
cc88a640 2015 xfree (match_list);
d60d9f65
SS
2016 match_list = (char **)NULL;
2017 }
2018 return (match_list);
2019}
2020
2021/* A completion function for usernames.
2022 TEXT contains a partial username preceded by a random
2023 character (usually `~'). */
2024char *
9255ee31
EZ
2025rl_username_completion_function (text, state)
2026 const char *text;
c862e87b 2027 int state;
d60d9f65 2028{
cd0040c1 2029#if defined (__WIN32__) || defined (__OPENNT)
d60d9f65 2030 return (char *)NULL;
1b17e766 2031#else /* !__WIN32__ && !__OPENNT) */
d60d9f65
SS
2032 static char *username = (char *)NULL;
2033 static struct passwd *entry;
2034 static int namelen, first_char, first_char_loc;
2035 char *value;
2036
2037 if (state == 0)
2038 {
2039 FREE (username);
2040
2041 first_char = *text;
2042 first_char_loc = first_char == '~';
2043
2044 username = savestring (&text[first_char_loc]);
2045 namelen = strlen (username);
2046 setpwent ();
2047 }
2048
5bdf8622 2049#if defined (HAVE_GETPWENT)
d60d9f65
SS
2050 while (entry = getpwent ())
2051 {
2052 /* Null usernames should result in all users as possible completions. */
2053 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
2054 break;
2055 }
430b7832 2056#endif
d60d9f65
SS
2057
2058 if (entry == 0)
2059 {
5bdf8622 2060#if defined (HAVE_GETPWENT)
d60d9f65 2061 endpwent ();
430b7832 2062#endif
d60d9f65
SS
2063 return ((char *)NULL);
2064 }
2065 else
2066 {
9255ee31 2067 value = (char *)xmalloc (2 + strlen (entry->pw_name));
d60d9f65
SS
2068
2069 *value = *text;
2070
2071 strcpy (value + first_char_loc, entry->pw_name);
2072
2073 if (first_char == '~')
2074 rl_filename_completion_desired = 1;
2075
2076 return (value);
2077 }
1b17e766 2078#endif /* !__WIN32__ && !__OPENNT */
d60d9f65
SS
2079}
2080
cc88a640
JK
2081/* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME
2082 (FILENAME_LEN). If _rl_completion_case_fold is set, compare without
2083 regard to the alphabetic case of characters. CONVFN is the possibly-
2084 converted directory entry; FILENAME is what the user typed. */
2085static int
2086complete_fncmp (convfn, convlen, filename, filename_len)
2087 const char *convfn;
2088 int convlen;
2089 const char *filename;
2090 int filename_len;
2091{
2092 register char *s1, *s2;
2093 int d, len;
2094
2095 /* Otherwise, if these match up to the length of filename, then
2096 it is a match. */
2097 if (_rl_completion_case_fold && _rl_completion_case_map)
2098 {
2099 /* Case-insensitive comparison treating _ and - as equivalent */
2100 if (filename_len == 0)
2101 return 1;
2102 if (convlen < filename_len)
2103 return 0;
2104 s1 = (char *)convfn;
2105 s2 = (char *)filename;
2106 len = filename_len;
2107 do
2108 {
2109 d = _rl_to_lower (*s1) - _rl_to_lower (*s2);
2110 /* *s1 == [-_] && *s2 == [-_] */
2111 if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2112 d = 0;
2113 if (d != 0)
2114 return 0;
2115 s1++; s2++; /* already checked convlen >= filename_len */
2116 }
2117 while (--len != 0);
2118 return 1;
2119 }
2120 else if (_rl_completion_case_fold)
2121 {
2122 if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) &&
2123 (convlen >= filename_len) &&
2124 (_rl_strnicmp (filename, convfn, filename_len) == 0))
2125 return 1;
2126 }
2127 else
2128 {
2129 if ((convfn[0] == filename[0]) &&
2130 (convlen >= filename_len) &&
2131 (strncmp (filename, convfn, filename_len) == 0))
2132 return 1;
2133 }
2134 return 0;
2135}
2136
d60d9f65
SS
2137/* Okay, now we write the entry_function for filename completion. In the
2138 general case. Note that completion in the shell is a little different
2139 because of all the pathnames that must be followed when looking up the
2140 completion for a command. */
2141char *
9255ee31
EZ
2142rl_filename_completion_function (text, state)
2143 const char *text;
c862e87b 2144 int state;
d60d9f65
SS
2145{
2146 static DIR *directory = (DIR *)NULL;
2147 static char *filename = (char *)NULL;
2148 static char *dirname = (char *)NULL;
2149 static char *users_dirname = (char *)NULL;
2150 static int filename_len;
cc88a640
JK
2151 char *temp, *dentry, *convfn;
2152 int dirlen, dentlen, convlen;
d60d9f65
SS
2153 struct dirent *entry;
2154
2155 /* If we don't have any state, then do some initialization. */
2156 if (state == 0)
2157 {
2158 /* If we were interrupted before closing the directory or reading
2159 all of its contents, close it. */
2160 if (directory)
2161 {
2162 closedir (directory);
2163 directory = (DIR *)NULL;
2164 }
2165 FREE (dirname);
2166 FREE (filename);
2167 FREE (users_dirname);
2168
2169 filename = savestring (text);
2170 if (*text == 0)
2171 text = ".";
2172 dirname = savestring (text);
2173
2174 temp = strrchr (dirname, '/');
2175
1b17e766
EZ
2176#if defined (__MSDOS__)
2177 /* special hack for //X/... */
9255ee31 2178 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/')
1b17e766
EZ
2179 temp = strrchr (dirname + 3, '/');
2180#endif
2181
d60d9f65
SS
2182 if (temp)
2183 {
2184 strcpy (filename, ++temp);
2185 *temp = '\0';
2186 }
1b17e766
EZ
2187#if defined (__MSDOS__)
2188 /* searches from current directory on the drive */
9255ee31 2189 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':')
1b17e766
EZ
2190 {
2191 strcpy (filename, dirname + 2);
2192 dirname[2] = '\0';
2193 }
cd0040c1 2194#endif
d60d9f65
SS
2195 else
2196 {
2197 dirname[0] = '.';
2198 dirname[1] = '\0';
2199 }
2200
2201 /* We aren't done yet. We also support the "~user" syntax. */
2202
cc88a640
JK
2203 /* Save the version of the directory that the user typed, dequoting
2204 it if necessary. */
2205 if (rl_completion_found_quote && rl_filename_dequoting_function)
2206 users_dirname = (*rl_filename_dequoting_function) (dirname, rl_completion_quote_character);
2207 else
2208 users_dirname = savestring (dirname);
d60d9f65
SS
2209
2210 if (*dirname == '~')
2211 {
2212 temp = tilde_expand (dirname);
cc88a640 2213 xfree (dirname);
d60d9f65
SS
2214 dirname = temp;
2215 }
2216
cc88a640
JK
2217 /* We have saved the possibly-dequoted version of the directory name
2218 the user typed. Now transform the directory name we're going to
2219 pass to opendir(2). The directory rewrite hook modifies only the
2220 directory name; the directory completion hook modifies both the
2221 directory name passed to opendir(2) and the version the user
2222 typed. Both the directory completion and rewrite hooks should perform
2223 any necessary dequoting. The hook functions return 1 if they modify
2224 the directory name argument. If either hook returns 0, it should
2225 not modify the directory name pointer passed as an argument. */
9255ee31
EZ
2226 if (rl_directory_rewrite_hook)
2227 (*rl_directory_rewrite_hook) (&dirname);
cc88a640 2228 else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname))
d60d9f65 2229 {
cc88a640 2230 xfree (users_dirname);
d60d9f65
SS
2231 users_dirname = savestring (dirname);
2232 }
cc88a640
JK
2233 else if (rl_completion_found_quote && rl_filename_dequoting_function)
2234 {
2235 /* delete single and double quotes */
2236 xfree (dirname);
2237 dirname = savestring (users_dirname);
2238 }
d60d9f65 2239 directory = opendir (dirname);
cc88a640
JK
2240
2241 /* Now dequote a non-null filename. */
2242 if (filename && *filename && rl_completion_found_quote && rl_filename_dequoting_function)
2243 {
2244 /* delete single and double quotes */
2245 temp = (*rl_filename_dequoting_function) (filename, rl_completion_quote_character);
2246 xfree (filename);
2247 filename = temp;
2248 }
d60d9f65
SS
2249 filename_len = strlen (filename);
2250
2251 rl_filename_completion_desired = 1;
2252 }
2253
2254 /* At this point we should entertain the possibility of hacking wildcarded
2255 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2256 contains globbing characters, then build an array of directories, and
2257 then map over that list while completing. */
2258 /* *** UNIMPLEMENTED *** */
2259
2260 /* Now that we have some state, we can read the directory. */
2261
2262 entry = (struct dirent *)NULL;
2263 while (directory && (entry = readdir (directory)))
2264 {
cc88a640
JK
2265 convfn = dentry = entry->d_name;
2266 convlen = dentlen = D_NAMLEN (entry);
2267
2268 if (rl_filename_rewrite_hook)
2269 {
2270 convfn = (*rl_filename_rewrite_hook) (dentry, dentlen);
2271 convlen = (convfn == dentry) ? dentlen : strlen (convfn);
2272 }
2273
9255ee31
EZ
2274 /* Special case for no filename. If the user has disabled the
2275 `match-hidden-files' variable, skip filenames beginning with `.'.
2276 All other entries except "." and ".." match. */
d60d9f65
SS
2277 if (filename_len == 0)
2278 {
cc88a640 2279 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (convfn))
9255ee31
EZ
2280 continue;
2281
cc88a640
JK
2282 if (convfn[0] != '.' ||
2283 (convfn[1] && (convfn[1] != '.' || convfn[2])))
d60d9f65
SS
2284 break;
2285 }
2286 else
2287 {
cc88a640
JK
2288 if (complete_fncmp (convfn, convlen, filename, filename_len))
2289 break;
d60d9f65
SS
2290 }
2291 }
2292
2293 if (entry == 0)
2294 {
2295 if (directory)
2296 {
2297 closedir (directory);
2298 directory = (DIR *)NULL;
2299 }
2300 if (dirname)
2301 {
cc88a640 2302 xfree (dirname);
d60d9f65
SS
2303 dirname = (char *)NULL;
2304 }
2305 if (filename)
2306 {
cc88a640 2307 xfree (filename);
d60d9f65
SS
2308 filename = (char *)NULL;
2309 }
2310 if (users_dirname)
2311 {
cc88a640 2312 xfree (users_dirname);
d60d9f65
SS
2313 users_dirname = (char *)NULL;
2314 }
2315
2316 return (char *)NULL;
2317 }
2318 else
2319 {
2320 /* dirname && (strcmp (dirname, ".") != 0) */
2321 if (dirname && (dirname[0] != '.' || dirname[1]))
2322 {
2323 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
2324 {
2325 dirlen = strlen (dirname);
9255ee31 2326 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
d60d9f65
SS
2327 strcpy (temp, dirname);
2328 /* Canonicalization cuts off any final slash present. We
2329 may need to add it back. */
2330 if (dirname[dirlen - 1] != '/')
2331 {
2332 temp[dirlen++] = '/';
2333 temp[dirlen] = '\0';
2334 }
2335 }
2336 else
2337 {
2338 dirlen = strlen (users_dirname);
9255ee31 2339 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
d60d9f65 2340 strcpy (temp, users_dirname);
9255ee31
EZ
2341 /* Make sure that temp has a trailing slash here. */
2342 if (users_dirname[dirlen - 1] != '/')
2343 temp[dirlen++] = '/';
d60d9f65
SS
2344 }
2345
cc88a640 2346 strcpy (temp + dirlen, convfn);
d60d9f65
SS
2347 }
2348 else
cc88a640
JK
2349 temp = savestring (convfn);
2350
2351 if (convfn != dentry)
2352 xfree (convfn);
d60d9f65
SS
2353
2354 return (temp);
2355 }
2356}
2357
2358/* An initial implementation of a menu completion function a la tcsh. The
cc88a640 2359 first time (if the last readline command was not rl_old_menu_complete), we
d60d9f65
SS
2360 generate the list of matches. This code is very similar to the code in
2361 rl_complete_internal -- there should be a way to combine the two. Then,
2362 for each item in the list of matches, we insert the match in an undoable
2363 fashion, with the appropriate character appended (this happens on the
cc88a640 2364 second and subsequent consecutive calls to rl_old_menu_complete). When we
d60d9f65
SS
2365 hit the end of the match list, we restore the original unmatched text,
2366 ring the bell, and reset the counter to zero. */
2367int
cc88a640
JK
2368rl_old_menu_complete (count, invoking_key)
2369 int count, invoking_key;
d60d9f65 2370{
9255ee31 2371 rl_compentry_func_t *our_func;
d60d9f65
SS
2372 int matching_filenames, found_quote;
2373
2374 static char *orig_text;
2375 static char **matches = (char **)0;
2376 static int match_list_index = 0;
2377 static int match_list_size = 0;
2378 static int orig_start, orig_end;
2379 static char quote_char;
2380 static int delimiter;
2381
2382 /* The first time through, we generate the list of matches and set things
2383 up to insert them. */
cc88a640 2384 if (rl_last_func != rl_old_menu_complete)
d60d9f65
SS
2385 {
2386 /* Clean up from previous call, if any. */
2387 FREE (orig_text);
2388 if (matches)
9255ee31 2389 _rl_free_match_list (matches);
d60d9f65
SS
2390
2391 match_list_index = match_list_size = 0;
2392 matches = (char **)NULL;
2393
cc88a640
JK
2394 rl_completion_invoking_key = invoking_key;
2395
2396 RL_SETSTATE(RL_STATE_COMPLETING);
2397
d60d9f65 2398 /* Only the completion entry function can change these. */
9255ee31 2399 set_completion_defaults ('%');
d60d9f65 2400
cc88a640
JK
2401 our_func = rl_menu_completion_entry_function;
2402 if (our_func == 0)
2403 our_func = rl_completion_entry_function
d60d9f65 2404 ? rl_completion_entry_function
9255ee31 2405 : rl_filename_completion_function;
d60d9f65
SS
2406
2407 /* We now look backwards for the start of a filename/variable word. */
2408 orig_end = rl_point;
2409 found_quote = delimiter = 0;
2410 quote_char = '\0';
2411
2412 if (rl_point)
2413 /* This (possibly) changes rl_point. If it returns a non-zero char,
2414 we know we have an open quote. */
9255ee31 2415 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
d60d9f65
SS
2416
2417 orig_start = rl_point;
2418 rl_point = orig_end;
2419
2420 orig_text = rl_copy_text (orig_start, orig_end);
2421 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2422 our_func, found_quote, quote_char);
2423
c862e87b
JM
2424 /* If we are matching filenames, the attempted completion function will
2425 have set rl_filename_completion_desired to a non-zero value. The basic
9255ee31 2426 rl_filename_completion_function does this. */
c862e87b 2427 matching_filenames = rl_filename_completion_desired;
9255ee31 2428
c862e87b 2429 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
d60d9f65 2430 {
cc88a640 2431 rl_ding ();
d60d9f65
SS
2432 FREE (matches);
2433 matches = (char **)0;
2434 FREE (orig_text);
2435 orig_text = (char *)0;
cc88a640
JK
2436 completion_changed_buffer = 0;
2437 RL_UNSETSTATE(RL_STATE_COMPLETING);
2438 return (0);
d60d9f65
SS
2439 }
2440
cc88a640
JK
2441 RL_UNSETSTATE(RL_STATE_COMPLETING);
2442
d60d9f65
SS
2443 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2444 ;
2445 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2446 code below should take care of it. */
cc88a640
JK
2447
2448 if (match_list_size > 1 && _rl_complete_show_all)
2449 display_matches (matches);
d60d9f65
SS
2450 }
2451
2452 /* Now we have the list of matches. Replace the text between
2453 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2454 matches[match_list_index], and add any necessary closing char. */
2455
2456 if (matches == 0 || match_list_size == 0)
2457 {
9255ee31 2458 rl_ding ();
d60d9f65
SS
2459 FREE (matches);
2460 matches = (char **)0;
2461 completion_changed_buffer = 0;
2462 return (0);
2463 }
2464
5bdf8622 2465 match_list_index += count;
d60d9f65 2466 if (match_list_index < 0)
cc88a640
JK
2467 {
2468 while (match_list_index < 0)
2469 match_list_index += match_list_size;
2470 }
5bdf8622
DJ
2471 else
2472 match_list_index %= match_list_size;
d60d9f65 2473
c862e87b 2474 if (match_list_index == 0 && match_list_size > 1)
d60d9f65 2475 {
9255ee31 2476 rl_ding ();
d60d9f65
SS
2477 insert_match (orig_text, orig_start, MULT_MATCH, &quote_char);
2478 }
2479 else
2480 {
2481 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
9255ee31
EZ
2482 append_to_match (matches[match_list_index], delimiter, quote_char,
2483 strcmp (orig_text, matches[match_list_index]));
d60d9f65
SS
2484 }
2485
2486 completion_changed_buffer = 1;
2487 return (0);
2488}
cc88a640
JK
2489
2490int
2491rl_menu_complete (count, ignore)
2492 int count, ignore;
2493{
2494 rl_compentry_func_t *our_func;
2495 int matching_filenames, found_quote;
2496
2497 static char *orig_text;
2498 static char **matches = (char **)0;
2499 static int match_list_index = 0;
2500 static int match_list_size = 0;
2501 static int nontrivial_lcd = 0;
2502 static int full_completion = 0; /* set to 1 if menu completion should reinitialize on next call */
2503 static int orig_start, orig_end;
2504 static char quote_char;
2505 static int delimiter, cstate;
2506
2507 /* The first time through, we generate the list of matches and set things
2508 up to insert them. */
2509 if ((rl_last_func != rl_menu_complete && rl_last_func != rl_backward_menu_complete) || full_completion)
2510 {
2511 /* Clean up from previous call, if any. */
2512 FREE (orig_text);
2513 if (matches)
2514 _rl_free_match_list (matches);
2515
2516 match_list_index = match_list_size = 0;
2517 matches = (char **)NULL;
2518
2519 full_completion = 0;
2520
2521 RL_SETSTATE(RL_STATE_COMPLETING);
2522
2523 /* Only the completion entry function can change these. */
2524 set_completion_defaults ('%');
2525
2526 our_func = rl_menu_completion_entry_function;
2527 if (our_func == 0)
2528 our_func = rl_completion_entry_function
2529 ? rl_completion_entry_function
2530 : rl_filename_completion_function;
2531
2532 /* We now look backwards for the start of a filename/variable word. */
2533 orig_end = rl_point;
2534 found_quote = delimiter = 0;
2535 quote_char = '\0';
2536
2537 if (rl_point)
2538 /* This (possibly) changes rl_point. If it returns a non-zero char,
2539 we know we have an open quote. */
2540 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2541
2542 orig_start = rl_point;
2543 rl_point = orig_end;
2544
2545 orig_text = rl_copy_text (orig_start, orig_end);
2546 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2547 our_func, found_quote, quote_char);
2548
2549 nontrivial_lcd = matches && strcmp (orig_text, matches[0]) != 0;
2550
2551 /* If we are matching filenames, the attempted completion function will
2552 have set rl_filename_completion_desired to a non-zero value. The basic
2553 rl_filename_completion_function does this. */
2554 matching_filenames = rl_filename_completion_desired;
2555
2556 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2557 {
2558 rl_ding ();
2559 FREE (matches);
2560 matches = (char **)0;
2561 FREE (orig_text);
2562 orig_text = (char *)0;
2563 completion_changed_buffer = 0;
2564 RL_UNSETSTATE(RL_STATE_COMPLETING);
2565 return (0);
2566 }
2567
2568 RL_UNSETSTATE(RL_STATE_COMPLETING);
2569
2570 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2571 ;
2572
2573 if (match_list_size == 0)
2574 {
2575 rl_ding ();
2576 FREE (matches);
2577 matches = (char **)0;
2578 match_list_index = 0;
2579 completion_changed_buffer = 0;
2580 return (0);
2581 }
2582
2583 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2584 code below should take care of it. */
2585 if (*matches[0])
2586 {
2587 insert_match (matches[0], orig_start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2588 orig_end = orig_start + strlen (matches[0]);
2589 completion_changed_buffer = STREQ (orig_text, matches[0]) == 0;
2590 }
2591
2592 if (match_list_size > 1 && _rl_complete_show_all)
2593 {
2594 display_matches (matches);
2595 /* If there are so many matches that the user has to be asked
2596 whether or not he wants to see the matches, menu completion
2597 is unwieldy. */
2598 if (rl_completion_query_items > 0 && match_list_size >= rl_completion_query_items)
2599 {
2600 rl_ding ();
2601 FREE (matches);
2602 matches = (char **)0;
2603 full_completion = 1;
2604 return (0);
2605 }
2606 }
2607 else if (match_list_size <= 1)
2608 {
2609 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2610 full_completion = 1;
2611 return (0);
2612 }
2613 else if (_rl_menu_complete_prefix_first && match_list_size > 1)
2614 {
2615 rl_ding ();
2616 return (0);
2617 }
2618 }
2619
2620 /* Now we have the list of matches. Replace the text between
2621 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2622 matches[match_list_index], and add any necessary closing char. */
2623
2624 if (matches == 0 || match_list_size == 0)
2625 {
2626 rl_ding ();
2627 FREE (matches);
2628 matches = (char **)0;
2629 completion_changed_buffer = 0;
2630 return (0);
2631 }
2632
2633 match_list_index += count;
2634 if (match_list_index < 0)
2635 {
2636 while (match_list_index < 0)
2637 match_list_index += match_list_size;
2638 }
2639 else
2640 match_list_index %= match_list_size;
2641
2642 if (match_list_index == 0 && match_list_size > 1)
2643 {
2644 rl_ding ();
2645 insert_match (matches[0], orig_start, MULT_MATCH, &quote_char);
2646 }
2647 else
2648 {
2649 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
2650 append_to_match (matches[match_list_index], delimiter, quote_char,
2651 strcmp (orig_text, matches[match_list_index]));
2652 }
2653
2654 completion_changed_buffer = 1;
2655 return (0);
2656}
2657
2658int
2659rl_backward_menu_complete (count, key)
2660 int count, key;
2661{
2662 /* Positive arguments to backward-menu-complete translate into negative
2663 arguments for menu-complete, and vice versa. */
2664 return (rl_menu_complete (-count, key));
2665}
This page took 0.795741 seconds and 4 git commands to generate.