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