blob: 39562058368968e19fad369c5f14435dd1aac3fa [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* complete.c -- filename completion for readline. */
2
Chet Rameyac50fba2014-02-26 09:36:43 -05003/* Copyright (C) 1987-2012 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
Jari Aalto31859422009-01-12 13:36:28 +00005 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
Jari Aalto726f6381996-08-26 18:22:31 +00007
Jari Aalto31859422009-01-12 13:36:28 +00008 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
Jari Aalto726f6381996-08-26 18:22:31 +000011 (at your option) any later version.
12
Jari Aalto31859422009-01-12 13:36:28 +000013 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
Jari Aalto726f6381996-08-26 18:22:31 +000016 GNU General Public License for more details.
17
Jari Aalto31859422009-01-12 13:36:28 +000018 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
Jari Aalto726f6381996-08-26 18:22:31 +000022#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000025# include <config.h>
Jari Aalto726f6381996-08-26 18:22:31 +000026#endif
27
Jari Aalto726f6381996-08-26 18:22:31 +000028#include <sys/types.h>
29#include <fcntl.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000030#if defined (HAVE_SYS_FILE_H)
Jari Aaltob80f6442004-07-27 13:29:18 +000031# include <sys/file.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000032#endif
Jari Aalto726f6381996-08-26 18:22:31 +000033
Chet Rameyac50fba2014-02-26 09:36:43 -050034#include <signal.h>
35
Jari Aalto726f6381996-08-26 18:22:31 +000036#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
Jari Aaltod166f041997-06-05 14:59:13 +000046#include <stdio.h>
47
Jari Aalto726f6381996-08-26 18:22:31 +000048#include <errno.h>
Jari Aalto726f6381996-08-26 18:22:31 +000049#if !defined (errno)
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +010050#include <errno.h>
Jari Aalto726f6381996-08-26 18:22:31 +000051#endif /* !errno */
52
Jari Aalto95732b42005-12-07 14:08:12 +000053#if defined (HAVE_PWD_H)
Jari Aalto726f6381996-08-26 18:22:31 +000054#include <pwd.h>
Jari Aalto95732b42005-12-07 14:08:12 +000055#endif
Jari Aalto726f6381996-08-26 18:22:31 +000056
Jari Aaltoccc6cda1996-12-23 17:02:34 +000057#include "posixdir.h"
Jari Aalto726f6381996-08-26 18:22:31 +000058#include "posixstat.h"
59
60/* System-specific feature definitions and include files. */
61#include "rldefs.h"
Jari Aalto7117c2d2002-07-17 14:10:11 +000062#include "rlmbutil.h"
Jari Aalto726f6381996-08-26 18:22:31 +000063
64/* Some standard library routines. */
65#include "readline.h"
Jari Aaltobb706242000-03-17 21:46:59 +000066#include "xmalloc.h"
67#include "rlprivate.h"
Jari Aalto726f6381996-08-26 18:22:31 +000068
Chet Rameyac50fba2014-02-26 09:36:43 -050069#if defined (COLOR_SUPPORT)
70# include "colors.h"
71#endif
72
Jari Aaltobb706242000-03-17 21:46:59 +000073#ifdef __STDC__
74typedef int QSFUNC (const void *, const void *);
75#else
76typedef int QSFUNC ();
77#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000078
Jari Aaltof73dda02001-11-13 17:56:06 +000079#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
Jari Aalto28ef6c32001-04-06 19:14:31 +000088/* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
89 defined. */
Jari Aalto95732b42005-12-07 14:08:12 +000090#if defined (HAVE_GETPWENT) && (!defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE))
Jari Aaltof73dda02001-11-13 17:56:06 +000091extern struct passwd *getpwent PARAMS((void));
Jari Aalto95732b42005-12-07 14:08:12 +000092#endif /* HAVE_GETPWENT && (!HAVE_GETPW_DECLS || _POSIX_SOURCE) */
Jari Aalto28ef6c32001-04-06 19:14:31 +000093
Jari Aaltob72432f1999-02-19 17:11:39 +000094/* 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. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000101rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000102
Chet Rameyac50fba2014-02-26 09:36:43 -0500103#if defined (VISIBLE_STATS) || defined (COLOR_SUPPORT)
Jari Aaltocce855b1998-04-17 19:52:44 +0000104# if !defined (X_OK)
105# define X_OK 1
106# endif
Chet Rameyac50fba2014-02-26 09:36:43 -0500107#endif
108
109#if defined (VISIBLE_STATS)
Jari Aaltof73dda02001-11-13 17:56:06 +0000110static int stat_char PARAMS((char *));
Jari Aaltocce855b1998-04-17 19:52:44 +0000111#endif
112
Chet Rameyac50fba2014-02-26 09:36:43 -0500113#if defined (COLOR_SUPPORT)
114static int colored_stat_start PARAMS((char *));
115static void colored_stat_end PARAMS((void));
116#endif
117
Jari Aaltob80f6442004-07-27 13:29:18 +0000118static int path_isdir PARAMS((const char *));
119
Jari Aaltof73dda02001-11-13 17:56:06 +0000120static char *rl_quote_filename PARAMS((char *, int, char *));
Jari Aalto726f6381996-08-26 18:22:31 +0000121
Chet Rameyac50fba2014-02-26 09:36:43 -0500122static void _rl_complete_sigcleanup PARAMS((int, void *));
123
Jari Aalto7117c2d2002-07-17 14:10:11 +0000124static void set_completion_defaults PARAMS((int));
125static int get_y_or_n PARAMS((int));
126static int _rl_internal_pager PARAMS((int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000127static char *printable_part PARAMS((char *));
Jari Aaltob80f6442004-07-27 13:29:18 +0000128static int fnwidth PARAMS((const char *));
Jari Aalto31859422009-01-12 13:36:28 +0000129static int fnprint PARAMS((const char *, int));
130static int print_filename PARAMS((char *, char *, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000131
132static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int));
133
134static char **remove_duplicate_matches PARAMS((char **));
135static void insert_match PARAMS((char *, int, int, char *));
136static int append_to_match PARAMS((char *, int, int, int));
137static void insert_all_matches PARAMS((char **, int, char *));
Chet Ramey495aee42011-11-22 19:11:26 -0500138static int complete_fncmp PARAMS((const char *, int, const char *, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000139static void display_matches PARAMS((char **));
140static int compute_lcd_of_matches PARAMS((char **, int, const char *));
141static int postprocess_matches PARAMS((char ***, int));
Chet Ramey495aee42011-11-22 19:11:26 -0500142static int complete_get_screenwidth PARAMS((void));
Jari Aaltof73dda02001-11-13 17:56:06 +0000143
144static char *make_quoted_replacement PARAMS((char *, int, char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000145
Jari Aaltocce855b1998-04-17 19:52:44 +0000146/* **************************************************************** */
147/* */
148/* Completion matching, from readline's point of view. */
149/* */
150/* **************************************************************** */
151
152/* Variables known only to the readline library. */
153
154/* If non-zero, non-unique completions always show the list of matches. */
155int _rl_complete_show_all = 0;
156
Jari Aaltob80f6442004-07-27 13:29:18 +0000157/* If non-zero, non-unique completions show the list of matches, unless it
158 is not possible to do partial completion and modify the line. */
159int _rl_complete_show_unmodified = 0;
160
Jari Aaltocce855b1998-04-17 19:52:44 +0000161/* If non-zero, completed directory names have a slash appended. */
162int _rl_complete_mark_directories = 1;
163
Jari Aalto7117c2d2002-07-17 14:10:11 +0000164/* If non-zero, the symlinked directory completion behavior introduced in
165 readline-4.2a is disabled, and symlinks that point to directories have
166 a slash appended (subject to the value of _rl_complete_mark_directories).
167 This is user-settable via the mark-symlinked-directories variable. */
168int _rl_complete_mark_symlink_dirs = 0;
169
Jari Aaltocce855b1998-04-17 19:52:44 +0000170/* If non-zero, completions are printed horizontally in alphabetical order,
171 like `ls -x'. */
172int _rl_print_completions_horizontally;
173
174/* Non-zero means that case is not significant in filename completion. */
Jari Aaltobb706242000-03-17 21:46:59 +0000175#if defined (__MSDOS__) && !defined (__DJGPP__)
176int _rl_completion_case_fold = 1;
177#else
Chet Ramey495aee42011-11-22 19:11:26 -0500178int _rl_completion_case_fold = 0;
Jari Aaltobb706242000-03-17 21:46:59 +0000179#endif
Jari Aaltocce855b1998-04-17 19:52:44 +0000180
Chet Ramey495aee42011-11-22 19:11:26 -0500181/* Non-zero means that `-' and `_' are equivalent when comparing filenames
182 for completion. */
183int _rl_completion_case_map = 0;
184
185/* If zero, don't match hidden files (filenames beginning with a `.' on
Jari Aaltof73dda02001-11-13 17:56:06 +0000186 Unix) when doing filename completion. */
187int _rl_match_hidden_files = 1;
188
Jari Aalto31859422009-01-12 13:36:28 +0000189/* Length in characters of a common prefix replaced with an ellipsis (`...')
190 when displaying completion matches. Matches whose printable portion has
191 more than this number of displaying characters in common will have the common
192 display prefix replaced with an ellipsis. */
193int _rl_completion_prefix_display_length = 0;
194
Chet Ramey495aee42011-11-22 19:11:26 -0500195/* The readline-private number of screen columns to use when displaying
196 matches. If < 0 or > _rl_screenwidth, it is ignored. */
197int _rl_completion_columns = -1;
198
Jari Aaltocce855b1998-04-17 19:52:44 +0000199/* Global variables available to applications using readline. */
200
201#if defined (VISIBLE_STATS)
202/* Non-zero means add an additional character to each filename displayed
203 during listing completion iff rl_filename_completion_desired which helps
204 to indicate the type of file being listed. */
205int rl_visible_stats = 0;
206#endif /* VISIBLE_STATS */
207
Chet Rameyac50fba2014-02-26 09:36:43 -0500208#if defined (COLOR_SUPPORT)
209/* Non-zero means to use colors to indicate file type when listing possible
210 completions. The colors used are taken from $LS_COLORS, if set. */
211int _rl_colored_stats = 0;
212#endif
213
Chet Ramey00018032011-11-21 20:51:19 -0500214/* If non-zero, when completing in the middle of a word, don't insert
215 characters from the match that match characters following point in
216 the word. This means, for instance, completing when the cursor is
217 after the `e' in `Makefile' won't result in `Makefilefile'. */
218int _rl_skip_completed_text = 0;
219
Chet Ramey495aee42011-11-22 19:11:26 -0500220/* If non-zero, menu completion displays the common prefix first in the
221 cycle of possible completions instead of the last. */
222int _rl_menu_complete_prefix_first = 0;
223
Jari Aalto726f6381996-08-26 18:22:31 +0000224/* If non-zero, then this is the address of a function to call when
225 completing on a directory name. The function is called with
226 the address of a string (the current directory name) as an arg. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000227rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
228
229rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000230
Chet Rameyac50fba2014-02-26 09:36:43 -0500231rl_icppfunc_t *rl_filename_stat_hook = (rl_icppfunc_t *)NULL;
232
Chet Ramey00018032011-11-21 20:51:19 -0500233/* If non-zero, this is the address of a function to call when reading
234 directory entries from the filesystem for completion and comparing
235 them to the partial word to be completed. The function should
236 either return its first argument (if no conversion takes place) or
237 newly-allocated memory. This can, for instance, convert filenames
238 between character sets for comparison against what's typed at the
239 keyboard. The returned value is what is added to the list of
240 matches. The second argument is the length of the filename to be
241 converted. */
242rl_dequote_func_t *rl_filename_rewrite_hook = (rl_dequote_func_t *)NULL;
243
Jari Aalto726f6381996-08-26 18:22:31 +0000244/* Non-zero means readline completion functions perform tilde expansion. */
245int rl_complete_with_tilde_expansion = 0;
246
Jari Aalto726f6381996-08-26 18:22:31 +0000247/* Pointer to the generator function for completion_matches ().
Jari Aalto28ef6c32001-04-06 19:14:31 +0000248 NULL means to use rl_filename_completion_function (), the default filename
Jari Aalto726f6381996-08-26 18:22:31 +0000249 completer. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000250rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000251
Jari Aalto31859422009-01-12 13:36:28 +0000252/* Pointer to generator function for rl_menu_complete (). NULL means to use
253 *rl_completion_entry_function (see above). */
254rl_compentry_func_t *rl_menu_completion_entry_function = (rl_compentry_func_t *)NULL;
255
Jari Aalto726f6381996-08-26 18:22:31 +0000256/* Pointer to alternative function to create matches.
257 Function is called with TEXT, START, and END.
258 START and END are indices in RL_LINE_BUFFER saying what the boundaries
259 of TEXT are.
260 If this function exists and returns NULL then call the value of
261 rl_completion_entry_function to try to match, otherwise use the
262 array of strings returned. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000263rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000264
265/* Non-zero means to suppress normal filename completion after the
266 user-specified completion function has been called. */
267int rl_attempted_completion_over = 0;
268
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000269/* Set to a character indicating the type of completion being performed
270 by rl_complete_internal, available for use by application completion
271 functions. */
272int rl_completion_type = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000273
274/* Up to this many items will be displayed in response to a
275 possible-completions call. After that, we ask the user if
Jari Aalto95732b42005-12-07 14:08:12 +0000276 she is sure she wants to see them all. A negative value means
277 don't ask. */
Jari Aalto726f6381996-08-26 18:22:31 +0000278int rl_completion_query_items = 100;
279
Jari Aalto7117c2d2002-07-17 14:10:11 +0000280int _rl_page_completions = 1;
281
Jari Aalto726f6381996-08-26 18:22:31 +0000282/* The basic list of characters that signal a break between words for the
283 completer routine. The contents of this variable is what breaks words
284 in the shell, i.e. " \t\n\"\\'`@$><=" */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000285const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
Jari Aalto726f6381996-08-26 18:22:31 +0000286
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000287/* List of basic quoting characters. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000288const char *rl_basic_quote_characters = "\"'";
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000289
Jari Aalto726f6381996-08-26 18:22:31 +0000290/* The list of characters that signal a break between words for
291 rl_complete_internal. The default list is the contents of
292 rl_basic_word_break_characters. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000293/*const*/ char *rl_completer_word_break_characters = (/*const*/ char *)NULL;
294
295/* Hook function to allow an application to set the completion word
296 break characters before readline breaks up the line. Allows
297 position-dependent word break characters. */
298rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000299
300/* List of characters which can be used to quote a substring of the line.
301 Completion occurs on the entire substring, and within the substring
302 rl_completer_word_break_characters are treated as any other character,
303 unless they also appear within this list. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000304const char *rl_completer_quote_characters = (const char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000305
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000306/* List of characters that should be quoted in filenames by the completer. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000307const char *rl_filename_quote_characters = (const char *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000308
Jari Aalto726f6381996-08-26 18:22:31 +0000309/* List of characters that are word break characters, but should be left
310 in TEXT when it is passed to the completion function. The shell uses
311 this to help determine what kind of completing to do. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000312const char *rl_special_prefixes = (const char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000313
314/* If non-zero, then disallow duplicates in the matches. */
315int rl_ignore_completion_duplicates = 1;
316
317/* Non-zero means that the results of the matches are to be treated
318 as filenames. This is ALWAYS zero on entry, and can only be changed
319 within a completion entry finder function. */
320int rl_filename_completion_desired = 0;
321
322/* Non-zero means that the results of the matches are to be quoted using
323 double quotes (or an application-specific quoting mechanism) if the
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000324 filename contains any characters in rl_filename_quote_chars. This is
Jari Aalto726f6381996-08-26 18:22:31 +0000325 ALWAYS non-zero on entry, and can only be changed within a completion
326 entry finder function. */
327int rl_filename_quoting_desired = 1;
328
329/* This function, if defined, is called by the completer when real
330 filename completion is done, after all the matching names have been
331 generated. It is passed a (char**) known as matches in the code below.
332 It consists of a NULL-terminated array of pointers to potential
333 matching strings. The 1st element (matches[0]) is the maximal
334 substring that is common to all matches. This function can re-arrange
335 the list of matches as required, but all elements of the array must be
336 free()'d if they are deleted. The main intent of this function is
337 to implement FIGNORE a la SunOS csh. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000338rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000339
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000340/* Set to a function to quote a filename in an application-specific fashion.
341 Called with the text to quote, the type of match found (single or multiple)
342 and a pointer to the quoting character to be used, which the function can
343 reset if desired. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000344rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000345
346/* Function to call to remove quoting characters from a filename. Called
347 before completion is attempted, so the embedded quotes do not interfere
348 with matching names in the file system. Readline doesn't do anything
349 with this; it's set only by applications. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000350rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000351
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000352/* Function to call to decide whether or not a word break character is
353 quoted. If a character is quoted, it does not break words for the
354 completer. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000355rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000356
Jari Aalto7117c2d2002-07-17 14:10:11 +0000357/* If non-zero, the completion functions don't append anything except a
358 possible closing quote. This is set to 0 by rl_complete_internal and
359 may be changed by an application-specific completion function. */
360int rl_completion_suppress_append = 0;
361
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000362/* Character appended to completed words when at the end of the line. The
363 default is a space. */
364int rl_completion_append_character = ' ';
365
Jari Aaltob80f6442004-07-27 13:29:18 +0000366/* If non-zero, the completion functions don't append any closing quote.
367 This is set to 0 by rl_complete_internal and may be changed by an
368 application-specific completion function. */
369int rl_completion_suppress_quote = 0;
370
371/* Set to any quote character readline thinks it finds before any application
372 completion function is called. */
373int rl_completion_quote_character;
374
375/* Set to a non-zero value if readline found quoting anywhere in the word to
376 be completed; set before any application completion function is called. */
377int rl_completion_found_quote;
378
Jari Aalto7117c2d2002-07-17 14:10:11 +0000379/* If non-zero, a slash will be appended to completed filenames that are
380 symbolic links to directory names, subject to the value of the
381 mark-directories variable (which is user-settable). This exists so
382 that application completion functions can override the user's preference
383 (set via the mark-symlinked-directories variable) if appropriate.
384 It's set to the value of _rl_complete_mark_symlink_dirs in
385 rl_complete_internal before any application-specific completion
386 function is called, so without that function doing anything, the user's
387 preferences are honored. */
388int rl_completion_mark_symlink_dirs;
389
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000390/* If non-zero, inhibit completion (temporarily). */
391int rl_inhibit_completion;
392
Jari Aalto31859422009-01-12 13:36:28 +0000393/* Set to the last key used to invoke one of the completion functions */
394int rl_completion_invoking_key;
395
396/* If non-zero, sort the completion matches. On by default. */
397int rl_sort_completion_matches = 1;
398
Jari Aaltocce855b1998-04-17 19:52:44 +0000399/* Variables local to this file. */
400
401/* Local variable states what happened during the last completion attempt. */
402static int completion_changed_buffer;
403
Jari Aalto31859422009-01-12 13:36:28 +0000404/* The result of the query to the user about displaying completion matches */
405static int completion_y_or_n;
406
Jari Aaltocce855b1998-04-17 19:52:44 +0000407/*************************************/
408/* */
409/* Bindable completion functions */
410/* */
411/*************************************/
412
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000413/* Complete the word at or before point. You have supplied the function
414 that does the initial simple matching selection algorithm (see
Jari Aalto28ef6c32001-04-06 19:14:31 +0000415 rl_completion_matches ()). The default is to do filename completion. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000416int
417rl_complete (ignore, invoking_key)
418 int ignore, invoking_key;
419{
Jari Aalto31859422009-01-12 13:36:28 +0000420 rl_completion_invoking_key = invoking_key;
421
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000422 if (rl_inhibit_completion)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000423 return (_rl_insert_char (ignore, invoking_key));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000424 else if (rl_last_func == rl_complete && !completion_changed_buffer)
425 return (rl_complete_internal ('?'));
426 else if (_rl_complete_show_all)
427 return (rl_complete_internal ('!'));
Jari Aaltob80f6442004-07-27 13:29:18 +0000428 else if (_rl_complete_show_unmodified)
429 return (rl_complete_internal ('@'));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000430 else
431 return (rl_complete_internal (TAB));
Jari Aalto726f6381996-08-26 18:22:31 +0000432}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000433
434/* List the possible completions. See description of rl_complete (). */
435int
436rl_possible_completions (ignore, invoking_key)
437 int ignore, invoking_key;
438{
Jari Aalto31859422009-01-12 13:36:28 +0000439 rl_completion_invoking_key = invoking_key;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000440 return (rl_complete_internal ('?'));
441}
442
443int
444rl_insert_completions (ignore, invoking_key)
445 int ignore, invoking_key;
446{
Jari Aalto31859422009-01-12 13:36:28 +0000447 rl_completion_invoking_key = invoking_key;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000448 return (rl_complete_internal ('*'));
449}
450
Jari Aalto7117c2d2002-07-17 14:10:11 +0000451/* Return the correct value to pass to rl_complete_internal performing
452 the same tests as rl_complete. This allows consecutive calls to an
453 application's completion function to list possible completions and for
454 an application-specific completion function to honor the
455 show-all-if-ambiguous readline variable. */
456int
457rl_completion_mode (cfunc)
458 rl_command_func_t *cfunc;
459{
460 if (rl_last_func == cfunc && !completion_changed_buffer)
461 return '?';
462 else if (_rl_complete_show_all)
463 return '!';
Jari Aaltob80f6442004-07-27 13:29:18 +0000464 else if (_rl_complete_show_unmodified)
465 return '@';
Jari Aalto7117c2d2002-07-17 14:10:11 +0000466 else
467 return TAB;
468}
469
Jari Aaltocce855b1998-04-17 19:52:44 +0000470/************************************/
471/* */
472/* Completion utility functions */
473/* */
474/************************************/
475
Jari Aalto31859422009-01-12 13:36:28 +0000476/* Reset readline state on a signal or other event. */
477void
478_rl_reset_completion_state ()
479{
480 rl_completion_found_quote = 0;
481 rl_completion_quote_character = 0;
482}
483
Chet Rameyac50fba2014-02-26 09:36:43 -0500484static void
485_rl_complete_sigcleanup (sig, ptr)
486 int sig;
487 void *ptr;
488{
489 if (sig == SIGINT) /* XXX - for now */
490 _rl_free_match_list ((char **)ptr);
491}
492
Jari Aalto7117c2d2002-07-17 14:10:11 +0000493/* Set default values for readline word completion. These are the variables
494 that application completion functions can change or inspect. */
495static void
496set_completion_defaults (what_to_do)
497 int what_to_do;
498{
499 /* Only the completion entry function can change these. */
500 rl_filename_completion_desired = 0;
501 rl_filename_quoting_desired = 1;
502 rl_completion_type = what_to_do;
Jari Aaltob80f6442004-07-27 13:29:18 +0000503 rl_completion_suppress_append = rl_completion_suppress_quote = 0;
Jari Aalto31859422009-01-12 13:36:28 +0000504 rl_completion_append_character = ' ';
Jari Aalto7117c2d2002-07-17 14:10:11 +0000505
506 /* The completion entry function may optionally change this. */
507 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
508}
509
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000510/* The user must press "y" or "n". Non-zero return means "y" pressed. */
511static int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000512get_y_or_n (for_pager)
513 int for_pager;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000514{
515 int c;
516
Chet Ramey495aee42011-11-22 19:11:26 -0500517 /* For now, disable pager in callback mode, until we later convert to state
518 driven functions. Have to wait until next major version to add new
519 state definition, since it will change value of RL_STATE_DONE. */
520#if defined (READLINE_CALLBACKS)
521 if (RL_ISSTATE (RL_STATE_CALLBACK))
522 return 1;
523#endif
524
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000525 for (;;)
526 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000527 RL_SETSTATE(RL_STATE_MOREINPUT);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000528 c = rl_read_key ();
Jari Aalto28ef6c32001-04-06 19:14:31 +0000529 RL_UNSETSTATE(RL_STATE_MOREINPUT);
530
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000531 if (c == 'y' || c == 'Y' || c == ' ')
532 return (1);
533 if (c == 'n' || c == 'N' || c == RUBOUT)
534 return (0);
Jari Aaltof1be6662008-11-18 13:15:12 +0000535 if (c == ABORT_CHAR || c < 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000536 _rl_abort_internal ();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000537 if (for_pager && (c == NEWLINE || c == RETURN))
538 return (2);
539 if (for_pager && (c == 'q' || c == 'Q'))
540 return (0);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000541 rl_ding ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000542 }
543}
Jari Aalto726f6381996-08-26 18:22:31 +0000544
Jari Aalto7117c2d2002-07-17 14:10:11 +0000545static int
546_rl_internal_pager (lines)
547 int lines;
548{
549 int i;
550
551 fprintf (rl_outstream, "--More--");
552 fflush (rl_outstream);
553 i = get_y_or_n (1);
554 _rl_erase_entire_line ();
555 if (i == 0)
556 return -1;
557 else if (i == 2)
558 return (lines - 1);
559 else
560 return 0;
561}
562
Jari Aaltob80f6442004-07-27 13:29:18 +0000563static int
564path_isdir (filename)
565 const char *filename;
566{
567 struct stat finfo;
568
569 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
570}
571
Jari Aaltocce855b1998-04-17 19:52:44 +0000572#if defined (VISIBLE_STATS)
573/* Return the character which best describes FILENAME.
574 `@' for symbolic links
575 `/' for directories
576 `*' for executables
577 `=' for sockets
578 `|' for FIFOs
579 `%' for character special devices
580 `#' for block special devices */
581static int
582stat_char (filename)
583 char *filename;
584{
585 struct stat finfo;
586 int character, r;
Chet Rameyac50fba2014-02-26 09:36:43 -0500587 char *f;
588 const char *fn;
Jari Aaltocce855b1998-04-17 19:52:44 +0000589
Jari Aalto31859422009-01-12 13:36:28 +0000590 /* Short-circuit a //server on cygwin, since that will always behave as
591 a directory. */
592#if __CYGWIN__
593 if (filename[0] == '/' && filename[1] == '/' && strchr (filename+2, '/') == 0)
594 return '/';
595#endif
596
Chet Rameyac50fba2014-02-26 09:36:43 -0500597 f = 0;
598 if (rl_filename_stat_hook)
599 {
600 f = savestring (filename);
601 (*rl_filename_stat_hook) (&f);
602 fn = f;
603 }
604 else
605 fn = filename;
606
Jari Aaltocce855b1998-04-17 19:52:44 +0000607#if defined (HAVE_LSTAT) && defined (S_ISLNK)
Chet Rameyac50fba2014-02-26 09:36:43 -0500608 r = lstat (fn, &finfo);
Jari Aaltocce855b1998-04-17 19:52:44 +0000609#else
Chet Rameyac50fba2014-02-26 09:36:43 -0500610 r = stat (fn, &finfo);
Jari Aaltocce855b1998-04-17 19:52:44 +0000611#endif
612
613 if (r == -1)
614 return (0);
615
616 character = 0;
617 if (S_ISDIR (finfo.st_mode))
618 character = '/';
619#if defined (S_ISCHR)
620 else if (S_ISCHR (finfo.st_mode))
621 character = '%';
622#endif /* S_ISCHR */
623#if defined (S_ISBLK)
624 else if (S_ISBLK (finfo.st_mode))
625 character = '#';
626#endif /* S_ISBLK */
627#if defined (S_ISLNK)
628 else if (S_ISLNK (finfo.st_mode))
629 character = '@';
630#endif /* S_ISLNK */
631#if defined (S_ISSOCK)
632 else if (S_ISSOCK (finfo.st_mode))
633 character = '=';
634#endif /* S_ISSOCK */
635#if defined (S_ISFIFO)
636 else if (S_ISFIFO (finfo.st_mode))
637 character = '|';
638#endif
639 else if (S_ISREG (finfo.st_mode))
640 {
641 if (access (filename, X_OK) == 0)
642 character = '*';
643 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500644
645 free (f);
Jari Aaltocce855b1998-04-17 19:52:44 +0000646 return (character);
647}
648#endif /* VISIBLE_STATS */
649
Chet Rameyac50fba2014-02-26 09:36:43 -0500650#if defined (COLOR_SUPPORT)
651static int
652colored_stat_start (filename)
653 char *filename;
654{
655 _rl_set_normal_color ();
656 return (_rl_print_color_indicator (filename));
657}
658
659static void
660colored_stat_end ()
661{
662 _rl_prep_non_filename_text ();
663 _rl_put_indicator (&_rl_color_indicator[C_CLR_TO_EOL]);
664}
665#endif
666
Jari Aalto726f6381996-08-26 18:22:31 +0000667/* Return the portion of PATHNAME that should be output when listing
668 possible completions. If we are hacking filename completion, we
669 are only interested in the basename, the portion following the
Jari Aalto7117c2d2002-07-17 14:10:11 +0000670 final slash. Otherwise, we return what we were passed. Since
671 printing empty strings is not very informative, if we're doing
672 filename completion, and the basename is the empty string, we look
673 for the previous slash and return the portion following that. If
674 there's no previous slash, we just return what we were passed. */
Jari Aalto726f6381996-08-26 18:22:31 +0000675static char *
676printable_part (pathname)
677 char *pathname;
678{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000679 char *temp, *x;
Jari Aalto726f6381996-08-26 18:22:31 +0000680
Jari Aalto7117c2d2002-07-17 14:10:11 +0000681 if (rl_filename_completion_desired == 0) /* don't need to do anything */
682 return (pathname);
683
684 temp = strrchr (pathname, '/');
Jari Aaltobb706242000-03-17 21:46:59 +0000685#if defined (__MSDOS__)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000686 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
Jari Aaltobb706242000-03-17 21:46:59 +0000687 temp = pathname + 1;
688#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +0000689
690 if (temp == 0 || *temp == '\0')
691 return (pathname);
Chet Rameyce97d602015-08-13 15:38:31 -0400692 else if (temp[1] == 0 && temp == pathname)
693 return (pathname);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000694 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
695 Look for a previous slash and, if one is found, return the portion
696 following that slash. If there's no previous slash, just return the
697 pathname we were passed. */
698 else if (temp[1] == '\0')
699 {
700 for (x = temp - 1; x > pathname; x--)
701 if (*x == '/')
702 break;
703 return ((*x == '/') ? x + 1 : pathname);
704 }
705 else
706 return ++temp;
Jari Aalto726f6381996-08-26 18:22:31 +0000707}
708
Jari Aaltob80f6442004-07-27 13:29:18 +0000709/* Compute width of STRING when displayed on screen by print_filename */
710static int
711fnwidth (string)
712 const char *string;
713{
714 int width, pos;
715#if defined (HANDLE_MULTIBYTE)
716 mbstate_t ps;
717 int left, w;
718 size_t clen;
719 wchar_t wc;
720
721 left = strlen (string) + 1;
722 memset (&ps, 0, sizeof (mbstate_t));
723#endif
724
725 width = pos = 0;
726 while (string[pos])
727 {
Jari Aalto31859422009-01-12 13:36:28 +0000728 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
Jari Aaltob80f6442004-07-27 13:29:18 +0000729 {
730 width += 2;
731 pos++;
732 }
733 else
734 {
735#if defined (HANDLE_MULTIBYTE)
736 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
737 if (MB_INVALIDCH (clen))
738 {
739 width++;
740 pos++;
741 memset (&ps, 0, sizeof (mbstate_t));
742 }
743 else if (MB_NULLWCH (clen))
744 break;
745 else
746 {
747 pos += clen;
Chet Rameyac50fba2014-02-26 09:36:43 -0500748 w = WCWIDTH (wc);
Jari Aaltob80f6442004-07-27 13:29:18 +0000749 width += (w >= 0) ? w : 1;
750 }
751#else
752 width++;
753 pos++;
754#endif
755 }
756 }
757
758 return width;
759}
760
Jari Aalto31859422009-01-12 13:36:28 +0000761#define ELLIPSIS_LEN 3
762
Jari Aaltob80f6442004-07-27 13:29:18 +0000763static int
Jari Aalto31859422009-01-12 13:36:28 +0000764fnprint (to_print, prefix_bytes)
Jari Aaltob80f6442004-07-27 13:29:18 +0000765 const char *to_print;
Jari Aalto31859422009-01-12 13:36:28 +0000766 int prefix_bytes;
Jari Aaltob80f6442004-07-27 13:29:18 +0000767{
Jari Aalto31859422009-01-12 13:36:28 +0000768 int printed_len, w;
Jari Aaltob80f6442004-07-27 13:29:18 +0000769 const char *s;
770#if defined (HANDLE_MULTIBYTE)
771 mbstate_t ps;
772 const char *end;
773 size_t tlen;
Jari Aalto31859422009-01-12 13:36:28 +0000774 int width;
Jari Aalto95732b42005-12-07 14:08:12 +0000775 wchar_t wc;
Jari Aaltob80f6442004-07-27 13:29:18 +0000776
777 end = to_print + strlen (to_print) + 1;
778 memset (&ps, 0, sizeof (mbstate_t));
779#endif
780
781 printed_len = 0;
Jari Aalto31859422009-01-12 13:36:28 +0000782
783 /* Don't print only the ellipsis if the common prefix is one of the
784 possible completions */
785 if (to_print[prefix_bytes] == '\0')
786 prefix_bytes = 0;
787
788 if (prefix_bytes)
789 {
790 char ellipsis;
791
792 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
793 for (w = 0; w < ELLIPSIS_LEN; w++)
794 putc (ellipsis, rl_outstream);
795 printed_len = ELLIPSIS_LEN;
796 }
797
798 s = to_print + prefix_bytes;
Jari Aaltob80f6442004-07-27 13:29:18 +0000799 while (*s)
800 {
801 if (CTRL_CHAR (*s))
802 {
803 putc ('^', rl_outstream);
804 putc (UNCTRL (*s), rl_outstream);
805 printed_len += 2;
806 s++;
807#if defined (HANDLE_MULTIBYTE)
808 memset (&ps, 0, sizeof (mbstate_t));
809#endif
810 }
811 else if (*s == RUBOUT)
812 {
813 putc ('^', rl_outstream);
814 putc ('?', rl_outstream);
815 printed_len += 2;
816 s++;
817#if defined (HANDLE_MULTIBYTE)
818 memset (&ps, 0, sizeof (mbstate_t));
819#endif
820 }
821 else
822 {
823#if defined (HANDLE_MULTIBYTE)
Jari Aalto95732b42005-12-07 14:08:12 +0000824 tlen = mbrtowc (&wc, s, end - s, &ps);
Jari Aaltob80f6442004-07-27 13:29:18 +0000825 if (MB_INVALIDCH (tlen))
826 {
827 tlen = 1;
Jari Aalto95732b42005-12-07 14:08:12 +0000828 width = 1;
Jari Aaltob80f6442004-07-27 13:29:18 +0000829 memset (&ps, 0, sizeof (mbstate_t));
830 }
831 else if (MB_NULLWCH (tlen))
832 break;
Jari Aalto95732b42005-12-07 14:08:12 +0000833 else
834 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500835 w = WCWIDTH (wc);
Jari Aalto95732b42005-12-07 14:08:12 +0000836 width = (w >= 0) ? w : 1;
837 }
Jari Aaltob80f6442004-07-27 13:29:18 +0000838 fwrite (s, 1, tlen, rl_outstream);
839 s += tlen;
Jari Aalto95732b42005-12-07 14:08:12 +0000840 printed_len += width;
Jari Aaltob80f6442004-07-27 13:29:18 +0000841#else
842 putc (*s, rl_outstream);
843 s++;
Jari Aaltob80f6442004-07-27 13:29:18 +0000844 printed_len++;
Jari Aalto95732b42005-12-07 14:08:12 +0000845#endif
Jari Aaltob80f6442004-07-27 13:29:18 +0000846 }
847 }
848
849 return printed_len;
850}
851
Jari Aalto726f6381996-08-26 18:22:31 +0000852/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
853 are using it, check for and output a single character for `special'
Jari Aaltocce855b1998-04-17 19:52:44 +0000854 filenames. Return the number of characters we output. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000855
Jari Aalto726f6381996-08-26 18:22:31 +0000856static int
Jari Aalto31859422009-01-12 13:36:28 +0000857print_filename (to_print, full_pathname, prefix_bytes)
Jari Aalto726f6381996-08-26 18:22:31 +0000858 char *to_print, *full_pathname;
Jari Aalto31859422009-01-12 13:36:28 +0000859 int prefix_bytes;
Jari Aalto726f6381996-08-26 18:22:31 +0000860{
Jari Aaltob80f6442004-07-27 13:29:18 +0000861 int printed_len, extension_char, slen, tlen;
Jari Aalto95732b42005-12-07 14:08:12 +0000862 char *s, c, *new_full_pathname, *dn;
Jari Aalto726f6381996-08-26 18:22:31 +0000863
Jari Aaltob80f6442004-07-27 13:29:18 +0000864 extension_char = 0;
Chet Rameyac50fba2014-02-26 09:36:43 -0500865#if defined (COLOR_SUPPORT)
866 /* Defer printing if we want to prefix with a color indicator */
867 if (_rl_colored_stats == 0 || rl_filename_completion_desired == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000868#endif
Chet Rameyac50fba2014-02-26 09:36:43 -0500869 printed_len = fnprint (to_print, prefix_bytes);
870
871 if (rl_filename_completion_desired && (
872#if defined (VISIBLE_STATS)
873 rl_visible_stats ||
874#endif
875#if defined (COLOR_SUPPORT)
876 _rl_colored_stats ||
877#endif
878 _rl_complete_mark_directories))
Jari Aalto726f6381996-08-26 18:22:31 +0000879 {
880 /* If to_print != full_pathname, to_print is the basename of the
881 path passed. In this case, we try to expand the directory
882 name before checking for the stat character. */
883 if (to_print != full_pathname)
884 {
885 /* Terminate the directory name. */
886 c = to_print[-1];
887 to_print[-1] = '\0';
888
Jari Aaltobb706242000-03-17 21:46:59 +0000889 /* If setting the last slash in full_pathname to a NUL results in
890 full_pathname being the empty string, we are trying to complete
891 files in the root directory. If we pass a null string to the
892 bash directory completion hook, for example, it will expand it
893 to the current directory. We just want the `/'. */
Jari Aalto95732b42005-12-07 14:08:12 +0000894 if (full_pathname == 0 || *full_pathname == 0)
895 dn = "/";
896 else if (full_pathname[0] != '/')
897 dn = full_pathname;
898 else if (full_pathname[1] == 0)
899 dn = "//"; /* restore trailing slash to `//' */
900 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
901 dn = "/"; /* don't turn /// into // */
902 else
903 dn = full_pathname;
904 s = tilde_expand (dn);
Jari Aalto726f6381996-08-26 18:22:31 +0000905 if (rl_directory_completion_hook)
906 (*rl_directory_completion_hook) (&s);
907
908 slen = strlen (s);
909 tlen = strlen (to_print);
Jari Aaltof73dda02001-11-13 17:56:06 +0000910 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
Jari Aalto726f6381996-08-26 18:22:31 +0000911 strcpy (new_full_pathname, s);
Jari Aalto95732b42005-12-07 14:08:12 +0000912 if (s[slen - 1] == '/')
913 slen--;
914 else
915 new_full_pathname[slen] = '/';
Jari Aalto726f6381996-08-26 18:22:31 +0000916 new_full_pathname[slen] = '/';
917 strcpy (new_full_pathname + slen + 1, to_print);
918
Jari Aaltob80f6442004-07-27 13:29:18 +0000919#if defined (VISIBLE_STATS)
920 if (rl_visible_stats)
921 extension_char = stat_char (new_full_pathname);
922 else
923#endif
Chet Rameyac50fba2014-02-26 09:36:43 -0500924 if (_rl_complete_mark_directories)
925 {
926 dn = 0;
927 if (rl_directory_completion_hook == 0 && rl_filename_stat_hook)
928 {
929 dn = savestring (new_full_pathname);
930 (*rl_filename_stat_hook) (&dn);
931 free (new_full_pathname);
932 new_full_pathname = dn;
933 }
934 if (path_isdir (new_full_pathname))
935 extension_char = '/';
936 }
937
938#if defined (COLOR_SUPPORT)
939 if (_rl_colored_stats)
940 {
941 colored_stat_start (new_full_pathname);
942 printed_len = fnprint (to_print, prefix_bytes);
943 colored_stat_end ();
944 }
945#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000946
Chet Ramey495aee42011-11-22 19:11:26 -0500947 xfree (new_full_pathname);
Jari Aalto726f6381996-08-26 18:22:31 +0000948 to_print[-1] = c;
949 }
950 else
951 {
952 s = tilde_expand (full_pathname);
Jari Aaltob80f6442004-07-27 13:29:18 +0000953#if defined (VISIBLE_STATS)
954 if (rl_visible_stats)
955 extension_char = stat_char (s);
956 else
957#endif
Chet Rameyac50fba2014-02-26 09:36:43 -0500958 if (_rl_complete_mark_directories && path_isdir (s))
Jari Aaltob80f6442004-07-27 13:29:18 +0000959 extension_char = '/';
Chet Rameyac50fba2014-02-26 09:36:43 -0500960
961#if defined (COLOR_SUPPORT)
962 if (_rl_colored_stats)
963 {
964 colored_stat_start (s);
965 printed_len = fnprint (to_print, prefix_bytes);
966 colored_stat_end ();
967 }
968#endif
969
Jari Aalto726f6381996-08-26 18:22:31 +0000970 }
971
Chet Ramey495aee42011-11-22 19:11:26 -0500972 xfree (s);
Jari Aalto726f6381996-08-26 18:22:31 +0000973 if (extension_char)
Jari Aaltocce855b1998-04-17 19:52:44 +0000974 {
975 putc (extension_char, rl_outstream);
976 printed_len++;
977 }
Jari Aalto726f6381996-08-26 18:22:31 +0000978 }
Jari Aaltob80f6442004-07-27 13:29:18 +0000979
Jari Aaltocce855b1998-04-17 19:52:44 +0000980 return printed_len;
Jari Aalto726f6381996-08-26 18:22:31 +0000981}
982
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000983static char *
984rl_quote_filename (s, rtype, qcp)
985 char *s;
986 int rtype;
987 char *qcp;
Jari Aalto726f6381996-08-26 18:22:31 +0000988{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000989 char *r;
Jari Aalto726f6381996-08-26 18:22:31 +0000990
Jari Aaltof73dda02001-11-13 17:56:06 +0000991 r = (char *)xmalloc (strlen (s) + 2);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000992 *r = *rl_completer_quote_characters;
993 strcpy (r + 1, s);
994 if (qcp)
995 *qcp = *rl_completer_quote_characters;
996 return r;
997}
Jari Aalto726f6381996-08-26 18:22:31 +0000998
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000999/* Find the bounds of the current word for completion purposes, and leave
1000 rl_point set to the end of the word. This function skips quoted
1001 substrings (characters between matched pairs of characters in
Jari Aaltof73dda02001-11-13 17:56:06 +00001002 rl_completer_quote_characters). First we try to find an unclosed
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001003 quoted substring on which to do matching. If one is not found, we use
1004 the word break characters to find the boundaries of the current word.
1005 We call an application-specific function to decide whether or not a
1006 particular word break character is quoted; if that function returns a
1007 non-zero result, the character does not break a word. This function
1008 returns the opening quote character if we found an unclosed quoted
1009 substring, '\0' otherwise. FP, if non-null, is set to a value saying
1010 which (shell-like) quote characters we found (single quote, double
1011 quote, or backslash) anywhere in the string. DP, if non-null, is set to
1012 the value of the delimiter character that caused a word break. */
Jari Aalto726f6381996-08-26 18:22:31 +00001013
Jari Aalto7117c2d2002-07-17 14:10:11 +00001014char
1015_rl_find_completion_word (fp, dp)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001016 int *fp, *dp;
1017{
1018 int scan, end, found_quote, delimiter, pass_next, isbrk;
Jari Aaltob80f6442004-07-27 13:29:18 +00001019 char quote_char, *brkchars;
Jari Aalto726f6381996-08-26 18:22:31 +00001020
Jari Aalto726f6381996-08-26 18:22:31 +00001021 end = rl_point;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001022 found_quote = delimiter = 0;
1023 quote_char = '\0';
Jari Aalto726f6381996-08-26 18:22:31 +00001024
Jari Aaltob80f6442004-07-27 13:29:18 +00001025 brkchars = 0;
1026 if (rl_completion_word_break_hook)
1027 brkchars = (*rl_completion_word_break_hook) ();
1028 if (brkchars == 0)
1029 brkchars = rl_completer_word_break_characters;
1030
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001031 if (rl_completer_quote_characters)
Jari Aalto726f6381996-08-26 18:22:31 +00001032 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001033 /* We have a list of characters which can be used in pairs to
1034 quote substrings for the completer. Try to find the start
1035 of an unclosed quoted substring. */
1036 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
Jari Aalto95732b42005-12-07 14:08:12 +00001037 for (scan = pass_next = 0; scan < end; scan = MB_NEXTCHAR (rl_line_buffer, scan, 1, MB_FIND_ANY))
Jari Aalto726f6381996-08-26 18:22:31 +00001038 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001039 if (pass_next)
Jari Aalto726f6381996-08-26 18:22:31 +00001040 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001041 pass_next = 0;
1042 continue;
1043 }
Jari Aalto726f6381996-08-26 18:22:31 +00001044
Jari Aalto28ef6c32001-04-06 19:14:31 +00001045 /* Shell-like semantics for single quotes -- don't allow backslash
1046 to quote anything in single quotes, especially not the closing
1047 quote. If you don't like this, take out the check on the value
1048 of quote_char. */
1049 if (quote_char != '\'' && rl_line_buffer[scan] == '\\')
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001050 {
1051 pass_next = 1;
1052 found_quote |= RL_QF_BACKSLASH;
1053 continue;
1054 }
Jari Aalto726f6381996-08-26 18:22:31 +00001055
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001056 if (quote_char != '\0')
1057 {
1058 /* Ignore everything until the matching close quote char. */
1059 if (rl_line_buffer[scan] == quote_char)
Jari Aalto726f6381996-08-26 18:22:31 +00001060 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001061 /* Found matching close. Abandon this substring. */
1062 quote_char = '\0';
1063 rl_point = end;
Jari Aalto726f6381996-08-26 18:22:31 +00001064 }
1065 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001066 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
Jari Aalto726f6381996-08-26 18:22:31 +00001067 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001068 /* Found start of a quoted substring. */
1069 quote_char = rl_line_buffer[scan];
1070 rl_point = scan + 1;
1071 /* Shell-like quoting conventions. */
1072 if (quote_char == '\'')
1073 found_quote |= RL_QF_SINGLE_QUOTE;
1074 else if (quote_char == '"')
1075 found_quote |= RL_QF_DOUBLE_QUOTE;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001076 else
1077 found_quote |= RL_QF_OTHER_QUOTE;
Jari Aalto726f6381996-08-26 18:22:31 +00001078 }
1079 }
Jari Aalto726f6381996-08-26 18:22:31 +00001080 }
1081
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001082 if (rl_point == end && quote_char == '\0')
1083 {
1084 /* We didn't find an unclosed quoted substring upon which to do
1085 completion, so use the word break characters to find the
1086 substring on which to complete. */
Jari Aalto95732b42005-12-07 14:08:12 +00001087 while (rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_ANY))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001088 {
1089 scan = rl_line_buffer[rl_point];
1090
Jari Aaltob80f6442004-07-27 13:29:18 +00001091 if (strchr (brkchars, scan) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001092 continue;
1093
1094 /* Call the application-specific function to tell us whether
1095 this word break character is quoted and should be skipped. */
1096 if (rl_char_is_quoted_p && found_quote &&
1097 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
1098 continue;
1099
1100 /* Convoluted code, but it avoids an n^2 algorithm with calls
1101 to char_is_quoted. */
1102 break;
1103 }
1104 }
1105
1106 /* If we are at an unquoted word break, then advance past it. */
1107 scan = rl_line_buffer[rl_point];
1108
1109 /* If there is an application-specific function to say whether or not
1110 a character is quoted and we found a quote character, let that
1111 function decide whether or not a character is a word break, even
Jari Aaltobb706242000-03-17 21:46:59 +00001112 if it is found in rl_completer_word_break_characters. Don't bother
1113 if we're at the end of the line, though. */
1114 if (scan)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001115 {
Jari Aaltobb706242000-03-17 21:46:59 +00001116 if (rl_char_is_quoted_p)
1117 isbrk = (found_quote == 0 ||
1118 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
Jari Aaltob80f6442004-07-27 13:29:18 +00001119 strchr (brkchars, scan) != 0;
Jari Aaltobb706242000-03-17 21:46:59 +00001120 else
Jari Aaltob80f6442004-07-27 13:29:18 +00001121 isbrk = strchr (brkchars, scan) != 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001122
Jari Aaltobb706242000-03-17 21:46:59 +00001123 if (isbrk)
1124 {
1125 /* If the character that caused the word break was a quoting
1126 character, then remember it as the delimiter. */
1127 if (rl_basic_quote_characters &&
1128 strchr (rl_basic_quote_characters, scan) &&
1129 (end - rl_point) > 1)
1130 delimiter = scan;
1131
1132 /* If the character isn't needed to determine something special
1133 about what kind of completion to perform, then advance past it. */
1134 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0)
1135 rl_point++;
1136 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001137 }
1138
1139 if (fp)
1140 *fp = found_quote;
1141 if (dp)
1142 *dp = delimiter;
1143
1144 return (quote_char);
1145}
1146
1147static char **
1148gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
1149 char *text;
1150 int start, end;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001151 rl_compentry_func_t *our_func;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001152 int found_quote, quote_char;
1153{
Jari Aalto06285672006-10-10 14:15:34 +00001154 char **matches;
Jari Aalto726f6381996-08-26 18:22:31 +00001155
Jari Aaltob80f6442004-07-27 13:29:18 +00001156 rl_completion_found_quote = found_quote;
1157 rl_completion_quote_character = quote_char;
1158
Jari Aalto726f6381996-08-26 18:22:31 +00001159 /* If the user wants to TRY to complete, but then wants to give
1160 up and use the default completion function, they set the
1161 variable rl_attempted_completion_function. */
1162 if (rl_attempted_completion_function)
1163 {
1164 matches = (*rl_attempted_completion_function) (text, start, end);
Chet Rameyac50fba2014-02-26 09:36:43 -05001165 if (RL_SIG_RECEIVED())
1166 {
1167 _rl_free_match_list (matches);
1168 matches = 0;
1169 RL_CHECK_SIGNALS ();
1170 }
Jari Aalto726f6381996-08-26 18:22:31 +00001171
1172 if (matches || rl_attempted_completion_over)
1173 {
1174 rl_attempted_completion_over = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001175 return (matches);
Jari Aalto726f6381996-08-26 18:22:31 +00001176 }
1177 }
1178
Jari Aalto06285672006-10-10 14:15:34 +00001179 /* XXX -- filename dequoting moved into rl_filename_completion_function */
Jari Aalto726f6381996-08-26 18:22:31 +00001180
Chet Rameyac50fba2014-02-26 09:36:43 -05001181 /* rl_completion_matches will check for signals as well to avoid a long
1182 delay while reading a directory. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001183 matches = rl_completion_matches (text, our_func);
Chet Rameyac50fba2014-02-26 09:36:43 -05001184 if (RL_SIG_RECEIVED())
1185 {
1186 _rl_free_match_list (matches);
1187 matches = 0;
1188 RL_CHECK_SIGNALS ();
1189 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001190 return matches;
1191}
Jari Aalto726f6381996-08-26 18:22:31 +00001192
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001193/* Filter out duplicates in MATCHES. This frees up the strings in
1194 MATCHES. */
1195static char **
1196remove_duplicate_matches (matches)
1197 char **matches;
1198{
1199 char *lowest_common;
1200 int i, j, newlen;
1201 char dead_slot;
1202 char **temp_array;
Jari Aalto726f6381996-08-26 18:22:31 +00001203
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001204 /* Sort the items. */
1205 for (i = 0; matches[i]; i++)
1206 ;
1207
1208 /* Sort the array without matches[0], since we need it to
1209 stay in place no matter what. */
Jari Aalto31859422009-01-12 13:36:28 +00001210 if (i && rl_sort_completion_matches)
Jari Aaltobb706242000-03-17 21:46:59 +00001211 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001212
1213 /* Remember the lowest common denominator for it may be unique. */
1214 lowest_common = savestring (matches[0]);
1215
1216 for (i = newlen = 0; matches[i + 1]; i++)
Jari Aalto726f6381996-08-26 18:22:31 +00001217 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001218 if (strcmp (matches[i], matches[i + 1]) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00001219 {
Chet Ramey495aee42011-11-22 19:11:26 -05001220 xfree (matches[i]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001221 matches[i] = (char *)&dead_slot;
Jari Aalto726f6381996-08-26 18:22:31 +00001222 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001223 else
1224 newlen++;
1225 }
Jari Aalto726f6381996-08-26 18:22:31 +00001226
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001227 /* We have marked all the dead slots with (char *)&dead_slot.
1228 Copy all the non-dead entries into a new array. */
1229 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
1230 for (i = j = 1; matches[i]; i++)
1231 {
1232 if (matches[i] != (char *)&dead_slot)
1233 temp_array[j++] = matches[i];
1234 }
1235 temp_array[j] = (char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00001236
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001237 if (matches[0] != (char *)&dead_slot)
Chet Ramey495aee42011-11-22 19:11:26 -05001238 xfree (matches[0]);
Jari Aalto726f6381996-08-26 18:22:31 +00001239
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001240 /* Place the lowest common denominator back in [0]. */
1241 temp_array[0] = lowest_common;
Jari Aalto726f6381996-08-26 18:22:31 +00001242
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001243 /* If there is one string left, and it is identical to the
1244 lowest common denominator, then the LCD is the string to
1245 insert. */
1246 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
1247 {
Chet Ramey495aee42011-11-22 19:11:26 -05001248 xfree (temp_array[1]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001249 temp_array[1] = (char *)NULL;
1250 }
1251 return (temp_array);
1252}
1253
Jari Aaltocce855b1998-04-17 19:52:44 +00001254/* Find the common prefix of the list of matches, and put it into
1255 matches[0]. */
1256static int
1257compute_lcd_of_matches (match_list, matches, text)
1258 char **match_list;
1259 int matches;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001260 const char *text;
Jari Aaltocce855b1998-04-17 19:52:44 +00001261{
1262 register int i, c1, c2, si;
1263 int low; /* Count of max-matched characters. */
Chet Rameyac50fba2014-02-26 09:36:43 -05001264 int lx;
Jari Aaltob80f6442004-07-27 13:29:18 +00001265 char *dtext; /* dequoted TEXT, if needed */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001266#if defined (HANDLE_MULTIBYTE)
1267 int v;
Chet Rameyac50fba2014-02-26 09:36:43 -05001268 size_t v1, v2;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001269 mbstate_t ps1, ps2;
1270 wchar_t wc1, wc2;
1271#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001272
1273 /* If only one match, just use that. Otherwise, compare each
1274 member of the list with the next, finding out where they
1275 stop matching. */
1276 if (matches == 1)
1277 {
1278 match_list[0] = match_list[1];
1279 match_list[1] = (char *)NULL;
1280 return 1;
1281 }
1282
1283 for (i = 1, low = 100000; i < matches; i++)
1284 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00001285#if defined (HANDLE_MULTIBYTE)
1286 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1287 {
1288 memset (&ps1, 0, sizeof (mbstate_t));
1289 memset (&ps2, 0, sizeof (mbstate_t));
1290 }
1291#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001292 if (_rl_completion_case_fold)
1293 {
1294 for (si = 0;
1295 (c1 = _rl_to_lower(match_list[i][si])) &&
1296 (c2 = _rl_to_lower(match_list[i + 1][si]));
1297 si++)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001298#if defined (HANDLE_MULTIBYTE)
1299 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1300 {
Chet Rameyac50fba2014-02-26 09:36:43 -05001301 v1 = mbrtowc(&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1);
1302 v2 = mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2);
1303 if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
1304 {
1305 if (c1 != c2) /* do byte comparison */
1306 break;
1307 continue;
1308 }
Jari Aalto7117c2d2002-07-17 14:10:11 +00001309 wc1 = towlower (wc1);
1310 wc2 = towlower (wc2);
1311 if (wc1 != wc2)
1312 break;
Chet Rameyac50fba2014-02-26 09:36:43 -05001313 else if (v1 > 1)
1314 si += v1 - 1;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001315 }
1316 else
1317#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001318 if (c1 != c2)
1319 break;
1320 }
1321 else
1322 {
1323 for (si = 0;
1324 (c1 = match_list[i][si]) &&
1325 (c2 = match_list[i + 1][si]);
1326 si++)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001327#if defined (HANDLE_MULTIBYTE)
1328 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1329 {
Jari Aalto06285672006-10-10 14:15:34 +00001330 mbstate_t ps_back;
1331 ps_back = ps1;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001332 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2))
1333 break;
1334 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1)
1335 si += v - 1;
1336 }
1337 else
1338#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001339 if (c1 != c2)
1340 break;
1341 }
1342
1343 if (low > si)
1344 low = si;
1345 }
1346
1347 /* If there were multiple matches, but none matched up to even the
1348 first character, and the user typed something, use that as the
1349 value of matches[0]. */
1350 if (low == 0 && text && *text)
1351 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001352 match_list[0] = (char *)xmalloc (strlen (text) + 1);
Jari Aaltocce855b1998-04-17 19:52:44 +00001353 strcpy (match_list[0], text);
1354 }
1355 else
1356 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001357 match_list[0] = (char *)xmalloc (low + 1);
1358
Jari Aalto7117c2d2002-07-17 14:10:11 +00001359 /* XXX - this might need changes in the presence of multibyte chars */
1360
Jari Aaltof73dda02001-11-13 17:56:06 +00001361 /* If we are ignoring case, try to preserve the case of the string
1362 the user typed in the face of multiple matches differing in case. */
1363 if (_rl_completion_case_fold)
1364 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001365 /* We're making an assumption here:
1366 IF we're completing filenames AND
1367 the application has defined a filename dequoting function AND
1368 we found a quote character AND
1369 the application has requested filename quoting
1370 THEN
1371 we assume that TEXT was dequoted before checking against
1372 the file system and needs to be dequoted here before we
1373 check against the list of matches
1374 FI */
1375 dtext = (char *)NULL;
1376 if (rl_filename_completion_desired &&
1377 rl_filename_dequoting_function &&
1378 rl_completion_found_quote &&
1379 rl_filename_quoting_desired)
1380 {
Jari Aalto95732b42005-12-07 14:08:12 +00001381 dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
Jari Aaltob80f6442004-07-27 13:29:18 +00001382 text = dtext;
1383 }
1384
Jari Aaltof73dda02001-11-13 17:56:06 +00001385 /* sort the list to get consistent answers. */
1386 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1387
1388 si = strlen (text);
Chet Rameyac50fba2014-02-26 09:36:43 -05001389 lx = (si <= low) ? si : low; /* check shorter of text and matches */
1390 /* Try to preserve the case of what the user typed in the presence of
1391 multiple matches: check each match for something that matches
1392 what the user typed taking case into account; use it up to common
1393 length of matches if one is found. If not, just use first match. */
1394 for (i = 1; i <= matches; i++)
1395 if (strncmp (match_list[i], text, lx) == 0)
1396 {
1397 strncpy (match_list[0], match_list[i], low);
1398 break;
1399 }
1400 /* no casematch, use first entry */
1401 if (i > matches)
1402 strncpy (match_list[0], match_list[1], low);
Jari Aaltob80f6442004-07-27 13:29:18 +00001403
1404 FREE (dtext);
Jari Aaltof73dda02001-11-13 17:56:06 +00001405 }
1406 else
1407 strncpy (match_list[0], match_list[1], low);
1408
Jari Aaltocce855b1998-04-17 19:52:44 +00001409 match_list[0][low] = '\0';
1410 }
1411
1412 return matches;
1413}
1414
1415static int
Jari Aaltob72432f1999-02-19 17:11:39 +00001416postprocess_matches (matchesp, matching_filenames)
Jari Aaltocce855b1998-04-17 19:52:44 +00001417 char ***matchesp;
1418 int matching_filenames;
1419{
1420 char *t, **matches, **temp_matches;
1421 int nmatch, i;
1422
1423 matches = *matchesp;
1424
Jari Aalto7117c2d2002-07-17 14:10:11 +00001425 if (matches == 0)
1426 return 0;
1427
Jari Aaltocce855b1998-04-17 19:52:44 +00001428 /* It seems to me that in all the cases we handle we would like
Chet Rameyac50fba2014-02-26 09:36:43 -05001429 to ignore duplicate possibilities. Scan for the text to
Jari Aaltocce855b1998-04-17 19:52:44 +00001430 insert being identical to the other completions. */
1431 if (rl_ignore_completion_duplicates)
1432 {
1433 temp_matches = remove_duplicate_matches (matches);
Chet Ramey495aee42011-11-22 19:11:26 -05001434 xfree (matches);
Jari Aaltocce855b1998-04-17 19:52:44 +00001435 matches = temp_matches;
1436 }
1437
1438 /* If we are matching filenames, then here is our chance to
1439 do clever processing by re-examining the list. Call the
1440 ignore function with the array as a parameter. It can
1441 munge the array, deleting matches as it desires. */
1442 if (rl_ignore_some_completions_function && matching_filenames)
1443 {
1444 for (nmatch = 1; matches[nmatch]; nmatch++)
1445 ;
1446 (void)(*rl_ignore_some_completions_function) (matches);
1447 if (matches == 0 || matches[0] == 0)
1448 {
1449 FREE (matches);
Jari Aaltocce855b1998-04-17 19:52:44 +00001450 *matchesp = (char **)0;
1451 return 0;
1452 }
1453 else
1454 {
1455 /* If we removed some matches, recompute the common prefix. */
1456 for (i = 1; matches[i]; i++)
1457 ;
1458 if (i > 1 && i < nmatch)
1459 {
1460 t = matches[0];
Jari Aaltob72432f1999-02-19 17:11:39 +00001461 compute_lcd_of_matches (matches, i - 1, t);
Jari Aaltocce855b1998-04-17 19:52:44 +00001462 FREE (t);
1463 }
1464 }
1465 }
1466
1467 *matchesp = matches;
1468 return (1);
1469}
1470
Chet Ramey495aee42011-11-22 19:11:26 -05001471static int
1472complete_get_screenwidth ()
1473{
1474 int cols;
1475 char *envcols;
1476
1477 cols = _rl_completion_columns;
1478 if (cols >= 0 && cols <= _rl_screenwidth)
1479 return cols;
1480 envcols = getenv ("COLUMNS");
1481 if (envcols && *envcols)
1482 cols = atoi (envcols);
1483 if (cols >= 0 && cols <= _rl_screenwidth)
1484 return cols;
1485 return _rl_screenwidth;
1486}
1487
Jari Aaltob72432f1999-02-19 17:11:39 +00001488/* A convenience function for displaying a list of strings in
1489 columnar format on readline's output stream. MATCHES is the list
1490 of strings, in argv format, LEN is the number of strings in MATCHES,
1491 and MAX is the length of the longest string in MATCHES. */
1492void
1493rl_display_match_list (matches, len, max)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001494 char **matches;
Jari Aaltob72432f1999-02-19 17:11:39 +00001495 int len, max;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001496{
Chet Ramey495aee42011-11-22 19:11:26 -05001497 int count, limit, printed_len, lines, cols;
Jari Aalto31859422009-01-12 13:36:28 +00001498 int i, j, k, l, common_length, sind;
1499 char *temp, *t;
1500
1501 /* Find the length of the prefix common to all items: length as displayed
1502 characters (common_length) and as a byte index into the matches (sind) */
1503 common_length = sind = 0;
1504 if (_rl_completion_prefix_display_length > 0)
1505 {
1506 t = printable_part (matches[0]);
1507 temp = strrchr (t, '/');
1508 common_length = temp ? fnwidth (temp) : fnwidth (t);
1509 sind = temp ? strlen (temp) : strlen (t);
1510
1511 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
1512 max -= common_length - ELLIPSIS_LEN;
1513 else
1514 common_length = sind = 0;
1515 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001516
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001517 /* How many items of MAX length can we fit in the screen window? */
Chet Ramey495aee42011-11-22 19:11:26 -05001518 cols = complete_get_screenwidth ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001519 max += 2;
Chet Ramey495aee42011-11-22 19:11:26 -05001520 limit = cols / max;
1521 if (limit != 1 && (limit * max == cols))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001522 limit--;
Jari Aalto726f6381996-08-26 18:22:31 +00001523
Chet Ramey495aee42011-11-22 19:11:26 -05001524 /* If cols == 0, limit will end up -1 */
1525 if (cols < _rl_screenwidth && limit < 0)
1526 limit = 1;
1527
1528 /* Avoid a possible floating exception. If max > cols,
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001529 limit will be 0 and a divide-by-zero fault will result. */
1530 if (limit == 0)
1531 limit = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001532
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001533 /* How many iterations of the printing loop? */
1534 count = (len + (limit - 1)) / limit;
Jari Aalto726f6381996-08-26 18:22:31 +00001535
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001536 /* Watch out for special case. If LEN is less than LIMIT, then
1537 just do the inner printing loop.
1538 0 < len <= limit implies count = 1. */
Jari Aalto726f6381996-08-26 18:22:31 +00001539
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001540 /* Sort the items if they are not already sorted. */
Jari Aalto31859422009-01-12 13:36:28 +00001541 if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
Jari Aaltobb706242000-03-17 21:46:59 +00001542 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
Jari Aalto726f6381996-08-26 18:22:31 +00001543
Jari Aalto28ef6c32001-04-06 19:14:31 +00001544 rl_crlf ();
Jari Aalto726f6381996-08-26 18:22:31 +00001545
Jari Aalto7117c2d2002-07-17 14:10:11 +00001546 lines = 0;
Jari Aaltocce855b1998-04-17 19:52:44 +00001547 if (_rl_print_completions_horizontally == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001548 {
Jari Aaltocce855b1998-04-17 19:52:44 +00001549 /* Print the sorted items, up-and-down alphabetically, like ls. */
1550 for (i = 1; i <= count; i++)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001551 {
Jari Aaltocce855b1998-04-17 19:52:44 +00001552 for (j = 0, l = i; j < limit; j++)
Jari Aalto726f6381996-08-26 18:22:31 +00001553 {
Jari Aaltocce855b1998-04-17 19:52:44 +00001554 if (l > len || matches[l] == 0)
1555 break;
1556 else
1557 {
1558 temp = printable_part (matches[l]);
Jari Aalto31859422009-01-12 13:36:28 +00001559 printed_len = print_filename (temp, matches[l], sind);
Jari Aalto726f6381996-08-26 18:22:31 +00001560
Jari Aaltocce855b1998-04-17 19:52:44 +00001561 if (j + 1 < limit)
1562 for (k = 0; k < max - printed_len; k++)
1563 putc (' ', rl_outstream);
1564 }
1565 l += count;
1566 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00001567 rl_crlf ();
Jari Aalto7117c2d2002-07-17 14:10:11 +00001568 lines++;
1569 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count)
1570 {
1571 lines = _rl_internal_pager (lines);
1572 if (lines < 0)
1573 return;
1574 }
Jari Aaltocce855b1998-04-17 19:52:44 +00001575 }
1576 }
1577 else
1578 {
1579 /* Print the sorted items, across alphabetically, like ls -x. */
1580 for (i = 1; matches[i]; i++)
1581 {
1582 temp = printable_part (matches[i]);
Jari Aalto31859422009-01-12 13:36:28 +00001583 printed_len = print_filename (temp, matches[i], sind);
Jari Aaltocce855b1998-04-17 19:52:44 +00001584 /* Have we reached the end of this line? */
1585 if (matches[i+1])
1586 {
Chet Rameyac50fba2014-02-26 09:36:43 -05001587 if (limit == 1 || (i && (limit > 1) && (i % limit) == 0))
Jari Aalto7117c2d2002-07-17 14:10:11 +00001588 {
1589 rl_crlf ();
1590 lines++;
1591 if (_rl_page_completions && lines >= _rl_screenheight - 1)
1592 {
1593 lines = _rl_internal_pager (lines);
1594 if (lines < 0)
1595 return;
1596 }
1597 }
Jari Aaltocce855b1998-04-17 19:52:44 +00001598 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001599 for (k = 0; k < max - printed_len; k++)
1600 putc (' ', rl_outstream);
Jari Aalto726f6381996-08-26 18:22:31 +00001601 }
Jari Aalto726f6381996-08-26 18:22:31 +00001602 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00001603 rl_crlf ();
Jari Aalto726f6381996-08-26 18:22:31 +00001604 }
Jari Aaltob72432f1999-02-19 17:11:39 +00001605}
Jari Aalto726f6381996-08-26 18:22:31 +00001606
Jari Aaltob72432f1999-02-19 17:11:39 +00001607/* Display MATCHES, a list of matching filenames in argv format. This
1608 handles the simple case -- a single match -- first. If there is more
1609 than one match, we compute the number of strings in the list and the
1610 length of the longest string, which will be needed by the display
1611 function. If the application wants to handle displaying the list of
1612 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1613 address of a function, and we just call it. If we're handling the
1614 display ourselves, we just call rl_display_match_list. We also check
1615 that the list of matches doesn't exceed the user-settable threshold,
1616 and ask the user if he wants to see the list if there are more matches
1617 than RL_COMPLETION_QUERY_ITEMS. */
1618static void
1619display_matches (matches)
1620 char **matches;
1621{
1622 int len, max, i;
1623 char *temp;
1624
1625 /* Move to the last visible line of a possibly-multiple-line command. */
1626 _rl_move_vert (_rl_vis_botlin);
1627
1628 /* Handle simple case first. What if there is only one answer? */
1629 if (matches[1] == 0)
1630 {
1631 temp = printable_part (matches[0]);
Jari Aalto28ef6c32001-04-06 19:14:31 +00001632 rl_crlf ();
Jari Aalto31859422009-01-12 13:36:28 +00001633 print_filename (temp, matches[0], 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +00001634 rl_crlf ();
Jari Aaltob72432f1999-02-19 17:11:39 +00001635
1636 rl_forced_update_display ();
1637 rl_display_fixed = 1;
1638
1639 return;
1640 }
1641
1642 /* There is more than one answer. Find out how many there are,
1643 and find the maximum printed length of a single entry. */
1644 for (max = 0, i = 1; matches[i]; i++)
1645 {
1646 temp = printable_part (matches[i]);
Jari Aaltob80f6442004-07-27 13:29:18 +00001647 len = fnwidth (temp);
Jari Aaltob72432f1999-02-19 17:11:39 +00001648
1649 if (len > max)
1650 max = len;
1651 }
1652
1653 len = i - 1;
1654
1655 /* If the caller has defined a display hook, then call that now. */
1656 if (rl_completion_display_matches_hook)
1657 {
1658 (*rl_completion_display_matches_hook) (matches, len, max);
1659 return;
1660 }
1661
1662 /* If there are many items, then ask the user if she really wants to
1663 see them all. */
Jari Aalto95732b42005-12-07 14:08:12 +00001664 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
Jari Aaltob72432f1999-02-19 17:11:39 +00001665 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00001666 rl_crlf ();
Jari Aaltob72432f1999-02-19 17:11:39 +00001667 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1668 fflush (rl_outstream);
Jari Aalto31859422009-01-12 13:36:28 +00001669 if ((completion_y_or_n = get_y_or_n (0)) == 0)
Jari Aaltob72432f1999-02-19 17:11:39 +00001670 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00001671 rl_crlf ();
Jari Aaltob72432f1999-02-19 17:11:39 +00001672
1673 rl_forced_update_display ();
1674 rl_display_fixed = 1;
1675
1676 return;
1677 }
1678 }
1679
1680 rl_display_match_list (matches, len, max);
1681
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001682 rl_forced_update_display ();
1683 rl_display_fixed = 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001684}
1685
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001686static char *
Jari Aaltod166f041997-06-05 14:59:13 +00001687make_quoted_replacement (match, mtype, qc)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001688 char *match;
Jari Aaltod166f041997-06-05 14:59:13 +00001689 int mtype;
1690 char *qc; /* Pointer to quoting character, if any */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001691{
1692 int should_quote, do_replace;
Jari Aaltod166f041997-06-05 14:59:13 +00001693 char *replacement;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001694
1695 /* If we are doing completion on quoted substrings, and any matches
1696 contain any of the completer_word_break_characters, then auto-
1697 matically prepend the substring with a quote character (just pick
1698 the first one from the list of such) if it does not already begin
1699 with a quote string. FIXME: Need to remove any such automatically
1700 inserted quote character when it no longer is necessary, such as
1701 if we change the string we are completing on and the new set of
1702 matches don't require a quoted substring. */
1703 replacement = match;
1704
1705 should_quote = match && rl_completer_quote_characters &&
1706 rl_filename_completion_desired &&
1707 rl_filename_quoting_desired;
1708
1709 if (should_quote)
Jari Aaltob72432f1999-02-19 17:11:39 +00001710 should_quote = should_quote && (!qc || !*qc ||
1711 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001712
1713 if (should_quote)
1714 {
1715 /* If there is a single match, see if we need to quote it.
1716 This also checks whether the common prefix of several
1717 matches needs to be quoted. */
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001718 should_quote = rl_filename_quote_characters
Jari Aalto28ef6c32001-04-06 19:14:31 +00001719 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0)
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001720 : 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001721
1722 do_replace = should_quote ? mtype : NO_MATCH;
Jari Aaltod166f041997-06-05 14:59:13 +00001723 /* Quote the replacement, since we found an embedded
1724 word break character in a potential match. */
1725 if (do_replace != NO_MATCH && rl_filename_quoting_function)
1726 replacement = (*rl_filename_quoting_function) (match, do_replace, qc);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001727 }
1728 return (replacement);
1729}
1730
1731static void
Jari Aaltod166f041997-06-05 14:59:13 +00001732insert_match (match, start, mtype, qc)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001733 char *match;
Jari Aaltod166f041997-06-05 14:59:13 +00001734 int start, mtype;
1735 char *qc;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001736{
Chet Ramey00018032011-11-21 20:51:19 -05001737 char *replacement, *r;
Jari Aaltod166f041997-06-05 14:59:13 +00001738 char oqc;
Chet Ramey00018032011-11-21 20:51:19 -05001739 int end, rlen;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001740
Jari Aaltod166f041997-06-05 14:59:13 +00001741 oqc = qc ? *qc : '\0';
1742 replacement = make_quoted_replacement (match, mtype, qc);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001743
1744 /* Now insert the match. */
1745 if (replacement)
1746 {
Chet Ramey00018032011-11-21 20:51:19 -05001747 rlen = strlen (replacement);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001748 /* Don't double an opening quote character. */
Jari Aaltod166f041997-06-05 14:59:13 +00001749 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc &&
1750 replacement[0] == *qc)
1751 start--;
1752 /* If make_quoted_replacement changed the quoting character, remove
1753 the opening quote and insert the (fully-quoted) replacement. */
1754 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
1755 replacement[0] != oqc)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001756 start--;
Chet Ramey00018032011-11-21 20:51:19 -05001757 end = rl_point - 1;
1758 /* Don't double a closing quote character */
1759 if (qc && *qc && end && rl_line_buffer[rl_point] == *qc && replacement[rlen - 1] == *qc)
1760 end++;
1761 if (_rl_skip_completed_text)
1762 {
1763 r = replacement;
1764 while (start < rl_end && *r && rl_line_buffer[start] == *r)
1765 {
1766 start++;
1767 r++;
1768 }
1769 if (start <= end || *r)
1770 _rl_replace_text (r, start, end);
1771 rl_point = start + strlen (r);
1772 }
1773 else
1774 _rl_replace_text (replacement, start, end);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001775 if (replacement != match)
Chet Ramey495aee42011-11-22 19:11:26 -05001776 xfree (replacement);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001777 }
1778}
1779
1780/* Append any necessary closing quote and a separator character to the
1781 just-inserted match. If the user has specified that directories
1782 should be marked by a trailing `/', append one of those instead. The
Jari Aaltocce855b1998-04-17 19:52:44 +00001783 default trailing character is a space. Returns the number of characters
Jari Aaltof73dda02001-11-13 17:56:06 +00001784 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1785 has them) and don't add a suffix for a symlink to a directory. A
Jari Aalto7117c2d2002-07-17 14:10:11 +00001786 nontrivial match is one that actually adds to the word being completed.
1787 The variable rl_completion_mark_symlink_dirs controls this behavior
1788 (it's initially set to the what the user has chosen, indicated by the
1789 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1790 application's completion function). */
Jari Aaltocce855b1998-04-17 19:52:44 +00001791static int
Jari Aaltof73dda02001-11-13 17:56:06 +00001792append_to_match (text, delimiter, quote_char, nontrivial_match)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001793 char *text;
Jari Aaltof73dda02001-11-13 17:56:06 +00001794 int delimiter, quote_char, nontrivial_match;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001795{
Chet Rameyac50fba2014-02-26 09:36:43 -05001796 char temp_string[4], *filename, *fn;
Jari Aaltof73dda02001-11-13 17:56:06 +00001797 int temp_string_index, s;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001798 struct stat finfo;
1799
1800 temp_string_index = 0;
Jari Aaltob80f6442004-07-27 13:29:18 +00001801 if (quote_char && rl_point && rl_completion_suppress_quote == 0 &&
1802 rl_line_buffer[rl_point - 1] != quote_char)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001803 temp_string[temp_string_index++] = quote_char;
1804
1805 if (delimiter)
1806 temp_string[temp_string_index++] = delimiter;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001807 else if (rl_completion_suppress_append == 0 && rl_completion_append_character)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001808 temp_string[temp_string_index++] = rl_completion_append_character;
1809
1810 temp_string[temp_string_index++] = '\0';
1811
1812 if (rl_filename_completion_desired)
1813 {
1814 filename = tilde_expand (text);
Chet Rameyac50fba2014-02-26 09:36:43 -05001815 if (rl_filename_stat_hook)
1816 {
1817 fn = savestring (filename);
1818 (*rl_filename_stat_hook) (&fn);
1819 xfree (filename);
1820 filename = fn;
1821 }
Jari Aalto7117c2d2002-07-17 14:10:11 +00001822 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1823 ? LSTAT (filename, &finfo)
1824 : stat (filename, &finfo);
Jari Aaltof73dda02001-11-13 17:56:06 +00001825 if (s == 0 && S_ISDIR (finfo.st_mode))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001826 {
Jari Aalto95732b42005-12-07 14:08:12 +00001827 if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001828 {
1829 /* This is clumsy. Avoid putting in a double slash if point
1830 is at the end of the line and the previous character is a
1831 slash. */
1832 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1833 ;
1834 else if (rl_line_buffer[rl_point] != '/')
1835 rl_insert_text ("/");
1836 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001837 }
Jari Aaltof73dda02001-11-13 17:56:06 +00001838#ifdef S_ISLNK
1839 /* Don't add anything if the filename is a symlink and resolves to a
1840 directory. */
Chet Rameyac50fba2014-02-26 09:36:43 -05001841 else if (s == 0 && S_ISLNK (finfo.st_mode) && path_isdir (filename))
Jari Aaltof73dda02001-11-13 17:56:06 +00001842 ;
1843#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001844 else
1845 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00001846 if (rl_point == rl_end && temp_string_index)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001847 rl_insert_text (temp_string);
1848 }
Chet Ramey495aee42011-11-22 19:11:26 -05001849 xfree (filename);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001850 }
1851 else
1852 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00001853 if (rl_point == rl_end && temp_string_index)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001854 rl_insert_text (temp_string);
1855 }
Jari Aaltocce855b1998-04-17 19:52:44 +00001856
1857 return (temp_string_index);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001858}
1859
1860static void
Jari Aaltod166f041997-06-05 14:59:13 +00001861insert_all_matches (matches, point, qc)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001862 char **matches;
Jari Aaltod166f041997-06-05 14:59:13 +00001863 int point;
1864 char *qc;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001865{
1866 int i;
1867 char *rp;
1868
1869 rl_begin_undo_group ();
1870 /* remove any opening quote character; make_quoted_replacement will add
1871 it back. */
Jari Aaltod166f041997-06-05 14:59:13 +00001872 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001873 point--;
1874 rl_delete_text (point, rl_point);
1875 rl_point = point;
1876
1877 if (matches[1])
1878 {
1879 for (i = 1; matches[i]; i++)
1880 {
Jari Aaltod166f041997-06-05 14:59:13 +00001881 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001882 rl_insert_text (rp);
1883 rl_insert_text (" ");
1884 if (rp != matches[i])
Chet Ramey495aee42011-11-22 19:11:26 -05001885 xfree (rp);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001886 }
1887 }
1888 else
1889 {
Jari Aaltod166f041997-06-05 14:59:13 +00001890 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001891 rl_insert_text (rp);
1892 rl_insert_text (" ");
1893 if (rp != matches[0])
Chet Ramey495aee42011-11-22 19:11:26 -05001894 xfree (rp);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001895 }
1896 rl_end_undo_group ();
1897}
1898
Jari Aalto7117c2d2002-07-17 14:10:11 +00001899void
1900_rl_free_match_list (matches)
Jari Aaltob72432f1999-02-19 17:11:39 +00001901 char **matches;
1902{
1903 register int i;
1904
Jari Aalto7117c2d2002-07-17 14:10:11 +00001905 if (matches == 0)
1906 return;
1907
Jari Aaltob72432f1999-02-19 17:11:39 +00001908 for (i = 0; matches[i]; i++)
Chet Ramey495aee42011-11-22 19:11:26 -05001909 xfree (matches[i]);
1910 xfree (matches);
Jari Aaltob72432f1999-02-19 17:11:39 +00001911}
1912
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001913/* Complete the word at or before point.
1914 WHAT_TO_DO says what to do with the completion.
1915 `?' means list the possible completions.
1916 TAB means do standard completion.
1917 `*' means insert all of the possible completions.
1918 `!' means to do standard completion, and list all possible completions if
Jari Aaltob80f6442004-07-27 13:29:18 +00001919 there is more than one.
1920 `@' means to do standard completion, and list all possible completions if
1921 there is more than one and partial completion is not possible. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001922int
1923rl_complete_internal (what_to_do)
1924 int what_to_do;
1925{
Jari Aaltocce855b1998-04-17 19:52:44 +00001926 char **matches;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001927 rl_compentry_func_t *our_func;
Jari Aaltof73dda02001-11-13 17:56:06 +00001928 int start, end, delimiter, found_quote, i, nontrivial_lcd;
Jari Aaltocce855b1998-04-17 19:52:44 +00001929 char *text, *saved_line_buffer;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001930 char quote_char;
Chet Ramey495aee42011-11-22 19:11:26 -05001931#if 1
1932 int tlen, mlen;
1933#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001934
Jari Aalto28ef6c32001-04-06 19:14:31 +00001935 RL_SETSTATE(RL_STATE_COMPLETING);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001936
1937 set_completion_defaults (what_to_do);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001938
Jari Aaltocce855b1998-04-17 19:52:44 +00001939 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
1940 our_func = rl_completion_entry_function
1941 ? rl_completion_entry_function
Jari Aalto28ef6c32001-04-06 19:14:31 +00001942 : rl_filename_completion_function;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001943 /* We now look backwards for the start of a filename/variable word. */
1944 end = rl_point;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001945 found_quote = delimiter = 0;
1946 quote_char = '\0';
1947
1948 if (rl_point)
1949 /* This (possibly) changes rl_point. If it returns a non-zero char,
1950 we know we have an open quote. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001951 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001952
1953 start = rl_point;
1954 rl_point = end;
1955
1956 text = rl_copy_text (start, end);
1957 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
Jari Aaltof73dda02001-11-13 17:56:06 +00001958 /* nontrivial_lcd is set if the common prefix adds something to the word
1959 being completed. */
1960 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
Chet Ramey495aee42011-11-22 19:11:26 -05001961 if (what_to_do == '!' || what_to_do == '@')
1962 tlen = strlen (text);
Chet Ramey495aee42011-11-22 19:11:26 -05001963 xfree (text);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001964
1965 if (matches == 0)
1966 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00001967 rl_ding ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001968 FREE (saved_line_buffer);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001969 completion_changed_buffer = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001970 RL_UNSETSTATE(RL_STATE_COMPLETING);
Jari Aalto31859422009-01-12 13:36:28 +00001971 _rl_reset_completion_state ();
Jari Aaltocce855b1998-04-17 19:52:44 +00001972 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001973 }
1974
Jari Aaltob72432f1999-02-19 17:11:39 +00001975 /* If we are matching filenames, the attempted completion function will
1976 have set rl_filename_completion_desired to a non-zero value. The basic
Jari Aalto28ef6c32001-04-06 19:14:31 +00001977 rl_filename_completion_function does this. */
Jari Aaltob72432f1999-02-19 17:11:39 +00001978 i = rl_filename_completion_desired;
Jari Aaltob72432f1999-02-19 17:11:39 +00001979
1980 if (postprocess_matches (&matches, i) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001981 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00001982 rl_ding ();
Jari Aaltocce855b1998-04-17 19:52:44 +00001983 FREE (saved_line_buffer);
Jari Aaltob72432f1999-02-19 17:11:39 +00001984 completion_changed_buffer = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001985 RL_UNSETSTATE(RL_STATE_COMPLETING);
Jari Aalto31859422009-01-12 13:36:28 +00001986 _rl_reset_completion_state ();
Jari Aaltocce855b1998-04-17 19:52:44 +00001987 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001988 }
Jari Aaltocce855b1998-04-17 19:52:44 +00001989
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001990 switch (what_to_do)
1991 {
1992 case TAB:
1993 case '!':
Jari Aaltob80f6442004-07-27 13:29:18 +00001994 case '@':
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001995 /* Insert the first match with proper quoting. */
Chet Ramey495aee42011-11-22 19:11:26 -05001996 if (what_to_do == TAB)
1997 {
1998 if (*matches[0])
1999 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2000 }
2001 else if (*matches[0] && matches[1] == 0)
2002 /* should we perform the check only if there are multiple matches? */
2003 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2004 else if (*matches[0]) /* what_to_do != TAB && multiple matches */
2005 {
2006 mlen = *matches[0] ? strlen (matches[0]) : 0;
2007 if (mlen >= tlen)
2008 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2009 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002010
2011 /* If there are more matches, ring the bell to indicate.
2012 If we are in vi mode, Posix.2 says to not ring the bell.
2013 If the `show-all-if-ambiguous' variable is set, display
2014 all the matches immediately. Otherwise, if this was the
2015 only match, and we are hacking files, check the file to
2016 see if it was a directory. If so, and the `mark-directories'
2017 variable is set, add a '/' to the name. If not, and we
2018 are at the end of the line, then add a space. */
2019 if (matches[1])
2020 {
2021 if (what_to_do == '!')
2022 {
2023 display_matches (matches);
2024 break;
2025 }
Jari Aaltob80f6442004-07-27 13:29:18 +00002026 else if (what_to_do == '@')
2027 {
2028 if (nontrivial_lcd == 0)
2029 display_matches (matches);
2030 break;
2031 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002032 else if (rl_editing_mode != vi_mode)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002033 rl_ding (); /* There are other matches remaining. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002034 }
2035 else
Jari Aaltof73dda02001-11-13 17:56:06 +00002036 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002037
2038 break;
2039
2040 case '*':
Jari Aaltod166f041997-06-05 14:59:13 +00002041 insert_all_matches (matches, start, &quote_char);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002042 break;
2043
2044 case '?':
Chet Rameyac50fba2014-02-26 09:36:43 -05002045 if (rl_completion_display_matches_hook == 0)
2046 {
2047 _rl_sigcleanup = _rl_complete_sigcleanup;
2048 _rl_sigcleanarg = matches;
2049 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002050 display_matches (matches);
Chet Rameyac50fba2014-02-26 09:36:43 -05002051 _rl_sigcleanup = 0;
2052 _rl_sigcleanarg = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002053 break;
2054
2055 default:
Jari Aalto31859422009-01-12 13:36:28 +00002056 _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002057 rl_ding ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002058 FREE (saved_line_buffer);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002059 RL_UNSETSTATE(RL_STATE_COMPLETING);
Chet Rameyac50fba2014-02-26 09:36:43 -05002060 _rl_free_match_list (matches);
Jari Aalto31859422009-01-12 13:36:28 +00002061 _rl_reset_completion_state ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002062 return 1;
2063 }
2064
Jari Aalto7117c2d2002-07-17 14:10:11 +00002065 _rl_free_match_list (matches);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002066
Jari Aalto726f6381996-08-26 18:22:31 +00002067 /* Check to see if the line has changed through all of this manipulation. */
2068 if (saved_line_buffer)
2069 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002070 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
Chet Ramey495aee42011-11-22 19:11:26 -05002071 xfree (saved_line_buffer);
Jari Aalto726f6381996-08-26 18:22:31 +00002072 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002073
Jari Aalto28ef6c32001-04-06 19:14:31 +00002074 RL_UNSETSTATE(RL_STATE_COMPLETING);
Jari Aalto31859422009-01-12 13:36:28 +00002075 _rl_reset_completion_state ();
Jari Aalto726f6381996-08-26 18:22:31 +00002076 return 0;
2077}
2078
Jari Aaltocce855b1998-04-17 19:52:44 +00002079/***************************************************************/
2080/* */
2081/* Application-callable completion match generator functions */
2082/* */
2083/***************************************************************/
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002084
Jari Aalto726f6381996-08-26 18:22:31 +00002085/* Return an array of (char *) which is a list of completions for TEXT.
2086 If there are no completions, return a NULL pointer.
2087 The first entry in the returned array is the substitution for TEXT.
2088 The remaining entries are the possible completions.
2089 The array is terminated with a NULL pointer.
2090
2091 ENTRY_FUNCTION is a function of two args, and returns a (char *).
2092 The first argument is TEXT.
2093 The second is a state argument; it should be zero on the first call, and
2094 non-zero on subsequent calls. It returns a NULL pointer to the caller
2095 when there are no more matches.
2096 */
2097char **
Jari Aalto28ef6c32001-04-06 19:14:31 +00002098rl_completion_matches (text, entry_function)
2099 const char *text;
2100 rl_compentry_func_t *entry_function;
Jari Aalto726f6381996-08-26 18:22:31 +00002101{
Chet Rameyac50fba2014-02-26 09:36:43 -05002102 register int i;
2103
Jari Aalto726f6381996-08-26 18:22:31 +00002104 /* Number of slots in match_list. */
2105 int match_list_size;
2106
2107 /* The list of matches. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002108 char **match_list;
Jari Aalto726f6381996-08-26 18:22:31 +00002109
2110 /* Number of matches actually found. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002111 int matches;
Jari Aalto726f6381996-08-26 18:22:31 +00002112
2113 /* Temporary string binder. */
2114 char *string;
2115
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002116 matches = 0;
2117 match_list_size = 10;
2118 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
Jari Aalto726f6381996-08-26 18:22:31 +00002119 match_list[1] = (char *)NULL;
2120
2121 while (string = (*entry_function) (text, matches))
2122 {
Chet Rameyac50fba2014-02-26 09:36:43 -05002123 if (RL_SIG_RECEIVED ())
2124 {
2125 /* Start at 1 because we don't set matches[0] in this function.
2126 Only free the list members if we're building match list from
2127 rl_filename_completion_function, since we know that doesn't
2128 free the strings it returns. */
2129 if (entry_function == rl_filename_completion_function)
2130 {
2131 for (i = 1; match_list[i]; i++)
2132 xfree (match_list[i]);
2133 }
2134 xfree (match_list);
2135 match_list = 0;
2136 match_list_size = 0;
2137 matches = 0;
2138 RL_CHECK_SIGNALS ();
2139 }
2140
2141 if (matches + 1 >= match_list_size)
Jari Aalto726f6381996-08-26 18:22:31 +00002142 match_list = (char **)xrealloc
2143 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
2144
Chet Rameyac50fba2014-02-26 09:36:43 -05002145 if (match_list == 0)
2146 return (match_list);
2147
Jari Aalto726f6381996-08-26 18:22:31 +00002148 match_list[++matches] = string;
2149 match_list[matches + 1] = (char *)NULL;
2150 }
2151
2152 /* If there were any matches, then look through them finding out the
2153 lowest common denominator. That then becomes match_list[0]. */
2154 if (matches)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002155 compute_lcd_of_matches (match_list, matches, text);
Jari Aalto726f6381996-08-26 18:22:31 +00002156 else /* There were no matches. */
2157 {
Chet Ramey495aee42011-11-22 19:11:26 -05002158 xfree (match_list);
Jari Aalto726f6381996-08-26 18:22:31 +00002159 match_list = (char **)NULL;
2160 }
2161 return (match_list);
2162}
2163
Jari Aaltocce855b1998-04-17 19:52:44 +00002164/* A completion function for usernames.
2165 TEXT contains a partial username preceded by a random
2166 character (usually `~'). */
2167char *
Jari Aalto28ef6c32001-04-06 19:14:31 +00002168rl_username_completion_function (text, state)
2169 const char *text;
Jari Aaltob72432f1999-02-19 17:11:39 +00002170 int state;
Jari Aaltocce855b1998-04-17 19:52:44 +00002171{
Jari Aaltobb706242000-03-17 21:46:59 +00002172#if defined (__WIN32__) || defined (__OPENNT)
Jari Aaltocce855b1998-04-17 19:52:44 +00002173 return (char *)NULL;
Jari Aaltobb706242000-03-17 21:46:59 +00002174#else /* !__WIN32__ && !__OPENNT) */
Jari Aaltocce855b1998-04-17 19:52:44 +00002175 static char *username = (char *)NULL;
2176 static struct passwd *entry;
2177 static int namelen, first_char, first_char_loc;
2178 char *value;
2179
2180 if (state == 0)
2181 {
2182 FREE (username);
2183
2184 first_char = *text;
2185 first_char_loc = first_char == '~';
2186
2187 username = savestring (&text[first_char_loc]);
2188 namelen = strlen (username);
Chet Rameyac50fba2014-02-26 09:36:43 -05002189#if defined (HAVE_GETPWENT)
Jari Aaltocce855b1998-04-17 19:52:44 +00002190 setpwent ();
Chet Rameyac50fba2014-02-26 09:36:43 -05002191#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00002192 }
2193
Jari Aalto95732b42005-12-07 14:08:12 +00002194#if defined (HAVE_GETPWENT)
Jari Aaltocce855b1998-04-17 19:52:44 +00002195 while (entry = getpwent ())
2196 {
2197 /* Null usernames should result in all users as possible completions. */
2198 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
2199 break;
2200 }
Jari Aalto95732b42005-12-07 14:08:12 +00002201#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00002202
2203 if (entry == 0)
2204 {
Jari Aalto95732b42005-12-07 14:08:12 +00002205#if defined (HAVE_GETPWENT)
Jari Aaltocce855b1998-04-17 19:52:44 +00002206 endpwent ();
Jari Aalto95732b42005-12-07 14:08:12 +00002207#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00002208 return ((char *)NULL);
2209 }
2210 else
2211 {
Jari Aaltof73dda02001-11-13 17:56:06 +00002212 value = (char *)xmalloc (2 + strlen (entry->pw_name));
Jari Aaltocce855b1998-04-17 19:52:44 +00002213
2214 *value = *text;
2215
2216 strcpy (value + first_char_loc, entry->pw_name);
2217
2218 if (first_char == '~')
2219 rl_filename_completion_desired = 1;
2220
2221 return (value);
2222 }
Jari Aaltobb706242000-03-17 21:46:59 +00002223#endif /* !__WIN32__ && !__OPENNT */
Jari Aaltocce855b1998-04-17 19:52:44 +00002224}
2225
Chet Ramey495aee42011-11-22 19:11:26 -05002226/* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME
2227 (FILENAME_LEN). If _rl_completion_case_fold is set, compare without
Chet Rameyac50fba2014-02-26 09:36:43 -05002228 regard to the alphabetic case of characters. If
2229 _rl_completion_case_map is set, make `-' and `_' equivalent. CONVFN is
2230 the possibly-converted directory entry; FILENAME is what the user typed. */
Chet Ramey495aee42011-11-22 19:11:26 -05002231static int
2232complete_fncmp (convfn, convlen, filename, filename_len)
2233 const char *convfn;
2234 int convlen;
2235 const char *filename;
2236 int filename_len;
2237{
2238 register char *s1, *s2;
2239 int d, len;
Chet Rameyac50fba2014-02-26 09:36:43 -05002240#if defined (HANDLE_MULTIBYTE)
2241 size_t v1, v2;
2242 mbstate_t ps1, ps2;
2243 wchar_t wc1, wc2;
2244#endif
2245
2246#if defined (HANDLE_MULTIBYTE)
2247 memset (&ps1, 0, sizeof (mbstate_t));
2248 memset (&ps2, 0, sizeof (mbstate_t));
2249#endif
2250
2251 if (filename_len == 0)
2252 return 1;
2253 if (convlen < filename_len)
2254 return 0;
2255
2256 len = filename_len;
2257 s1 = (char *)convfn;
2258 s2 = (char *)filename;
Chet Ramey495aee42011-11-22 19:11:26 -05002259
2260 /* Otherwise, if these match up to the length of filename, then
2261 it is a match. */
2262 if (_rl_completion_case_fold && _rl_completion_case_map)
2263 {
2264 /* Case-insensitive comparison treating _ and - as equivalent */
Chet Rameyac50fba2014-02-26 09:36:43 -05002265#if defined (HANDLE_MULTIBYTE)
2266 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
Chet Ramey495aee42011-11-22 19:11:26 -05002267 {
Chet Rameyac50fba2014-02-26 09:36:43 -05002268 do
2269 {
2270 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2271 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2272 if (v1 == 0 && v2 == 0)
2273 return 1;
2274 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2275 {
2276 if (*s1 != *s2) /* do byte comparison */
2277 return 0;
2278 else if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2279 return 0;
2280 s1++; s2++; len--;
2281 continue;
2282 }
2283 wc1 = towlower (wc1);
2284 wc2 = towlower (wc2);
2285 s1 += v1;
2286 s2 += v1;
2287 len -= v1;
2288 if ((wc1 == L'-' || wc1 == L'_') && (wc2 == L'-' || wc2 == L'_'))
2289 continue;
2290 if (wc1 != wc2)
2291 return 0;
2292 }
2293 while (len != 0);
Chet Ramey495aee42011-11-22 19:11:26 -05002294 }
Chet Rameyac50fba2014-02-26 09:36:43 -05002295 else
2296#endif
2297 {
2298 do
2299 {
2300 d = _rl_to_lower (*s1) - _rl_to_lower (*s2);
2301 /* *s1 == [-_] && *s2 == [-_] */
2302 if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_'))
2303 d = 0;
2304 if (d != 0)
2305 return 0;
2306 s1++; s2++; /* already checked convlen >= filename_len */
2307 }
2308 while (--len != 0);
2309 }
2310
Chet Ramey495aee42011-11-22 19:11:26 -05002311 return 1;
2312 }
2313 else if (_rl_completion_case_fold)
2314 {
Chet Rameyac50fba2014-02-26 09:36:43 -05002315#if defined (HANDLE_MULTIBYTE)
2316 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
2317 {
2318 do
2319 {
2320 v1 = mbrtowc (&wc1, s1, convlen, &ps1);
2321 v2 = mbrtowc (&wc2, s2, filename_len, &ps2);
2322 if (v1 == 0 && v2 == 0)
2323 return 1;
2324 else if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
2325 {
2326 if (*s1 != *s2) /* do byte comparison */
2327 return 0;
2328 s1++; s2++; len--;
2329 continue;
2330 }
2331 wc1 = towlower (wc1);
2332 wc2 = towlower (wc2);
2333 if (wc1 != wc2)
2334 return 0;
2335 s1 += v1;
2336 s2 += v1;
2337 len -= v1;
2338 }
2339 while (len != 0);
2340 return 1;
2341 }
2342 else
2343#endif
Chet Ramey495aee42011-11-22 19:11:26 -05002344 if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) &&
2345 (convlen >= filename_len) &&
2346 (_rl_strnicmp (filename, convfn, filename_len) == 0))
2347 return 1;
2348 }
2349 else
2350 {
2351 if ((convfn[0] == filename[0]) &&
2352 (convlen >= filename_len) &&
2353 (strncmp (filename, convfn, filename_len) == 0))
2354 return 1;
2355 }
2356 return 0;
2357}
2358
Jari Aalto726f6381996-08-26 18:22:31 +00002359/* Okay, now we write the entry_function for filename completion. In the
2360 general case. Note that completion in the shell is a little different
2361 because of all the pathnames that must be followed when looking up the
2362 completion for a command. */
2363char *
Jari Aalto28ef6c32001-04-06 19:14:31 +00002364rl_filename_completion_function (text, state)
2365 const char *text;
Jari Aaltob72432f1999-02-19 17:11:39 +00002366 int state;
Jari Aalto726f6381996-08-26 18:22:31 +00002367{
Jari Aaltod166f041997-06-05 14:59:13 +00002368 static DIR *directory = (DIR *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00002369 static char *filename = (char *)NULL;
2370 static char *dirname = (char *)NULL;
2371 static char *users_dirname = (char *)NULL;
2372 static int filename_len;
Chet Ramey00018032011-11-21 20:51:19 -05002373 char *temp, *dentry, *convfn;
2374 int dirlen, dentlen, convlen;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002375 struct dirent *entry;
Jari Aalto726f6381996-08-26 18:22:31 +00002376
2377 /* If we don't have any state, then do some initialization. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002378 if (state == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002379 {
Jari Aaltod166f041997-06-05 14:59:13 +00002380 /* If we were interrupted before closing the directory or reading
2381 all of its contents, close it. */
2382 if (directory)
2383 {
2384 closedir (directory);
2385 directory = (DIR *)NULL;
2386 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002387 FREE (dirname);
2388 FREE (filename);
2389 FREE (users_dirname);
Jari Aalto726f6381996-08-26 18:22:31 +00002390
2391 filename = savestring (text);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002392 if (*text == 0)
2393 text = ".";
Jari Aalto726f6381996-08-26 18:22:31 +00002394 dirname = savestring (text);
2395
2396 temp = strrchr (dirname, '/');
2397
Jari Aaltobb706242000-03-17 21:46:59 +00002398#if defined (__MSDOS__)
2399 /* special hack for //X/... */
Jari Aaltof73dda02001-11-13 17:56:06 +00002400 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/')
Jari Aaltobb706242000-03-17 21:46:59 +00002401 temp = strrchr (dirname + 3, '/');
2402#endif
2403
Jari Aalto726f6381996-08-26 18:22:31 +00002404 if (temp)
2405 {
2406 strcpy (filename, ++temp);
2407 *temp = '\0';
2408 }
Jari Aaltobb706242000-03-17 21:46:59 +00002409#if defined (__MSDOS__)
2410 /* searches from current directory on the drive */
Jari Aaltof73dda02001-11-13 17:56:06 +00002411 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':')
Jari Aaltobb706242000-03-17 21:46:59 +00002412 {
2413 strcpy (filename, dirname + 2);
2414 dirname[2] = '\0';
2415 }
2416#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002417 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002418 {
2419 dirname[0] = '.';
2420 dirname[1] = '\0';
2421 }
Jari Aalto726f6381996-08-26 18:22:31 +00002422
2423 /* We aren't done yet. We also support the "~user" syntax. */
2424
Chet Ramey495aee42011-11-22 19:11:26 -05002425 /* Save the version of the directory that the user typed, dequoting
2426 it if necessary. */
2427 if (rl_completion_found_quote && rl_filename_dequoting_function)
2428 users_dirname = (*rl_filename_dequoting_function) (dirname, rl_completion_quote_character);
2429 else
2430 users_dirname = savestring (dirname);
Jari Aalto726f6381996-08-26 18:22:31 +00002431
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002432 if (*dirname == '~')
2433 {
2434 temp = tilde_expand (dirname);
Chet Ramey495aee42011-11-22 19:11:26 -05002435 xfree (dirname);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002436 dirname = temp;
2437 }
Jari Aalto726f6381996-08-26 18:22:31 +00002438
Chet Ramey495aee42011-11-22 19:11:26 -05002439 /* We have saved the possibly-dequoted version of the directory name
2440 the user typed. Now transform the directory name we're going to
2441 pass to opendir(2). The directory rewrite hook modifies only the
2442 directory name; the directory completion hook modifies both the
2443 directory name passed to opendir(2) and the version the user
2444 typed. Both the directory completion and rewrite hooks should perform
2445 any necessary dequoting. The hook functions return 1 if they modify
2446 the directory name argument. If either hook returns 0, it should
2447 not modify the directory name pointer passed as an argument. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00002448 if (rl_directory_rewrite_hook)
2449 (*rl_directory_rewrite_hook) (&dirname);
Chet Ramey495aee42011-11-22 19:11:26 -05002450 else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002451 {
Chet Ramey495aee42011-11-22 19:11:26 -05002452 xfree (users_dirname);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002453 users_dirname = savestring (dirname);
2454 }
Jari Aalto06285672006-10-10 14:15:34 +00002455 else if (rl_completion_found_quote && rl_filename_dequoting_function)
2456 {
2457 /* delete single and double quotes */
Chet Ramey495aee42011-11-22 19:11:26 -05002458 xfree (dirname);
2459 dirname = savestring (users_dirname);
Jari Aalto06285672006-10-10 14:15:34 +00002460 }
Jari Aalto726f6381996-08-26 18:22:31 +00002461 directory = opendir (dirname);
Jari Aalto06285672006-10-10 14:15:34 +00002462
Chet Rameyac50fba2014-02-26 09:36:43 -05002463 /* Now dequote a non-null filename. FILENAME will not be NULL, but may
2464 be empty. */
2465 if (*filename && rl_completion_found_quote && rl_filename_dequoting_function)
Jari Aalto06285672006-10-10 14:15:34 +00002466 {
2467 /* delete single and double quotes */
2468 temp = (*rl_filename_dequoting_function) (filename, rl_completion_quote_character);
Chet Ramey495aee42011-11-22 19:11:26 -05002469 xfree (filename);
Jari Aalto06285672006-10-10 14:15:34 +00002470 filename = temp;
2471 }
Jari Aalto726f6381996-08-26 18:22:31 +00002472 filename_len = strlen (filename);
2473
2474 rl_filename_completion_desired = 1;
2475 }
2476
2477 /* At this point we should entertain the possibility of hacking wildcarded
2478 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2479 contains globbing characters, then build an array of directories, and
2480 then map over that list while completing. */
2481 /* *** UNIMPLEMENTED *** */
2482
2483 /* Now that we have some state, we can read the directory. */
2484
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002485 entry = (struct dirent *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00002486 while (directory && (entry = readdir (directory)))
2487 {
Chet Ramey00018032011-11-21 20:51:19 -05002488 convfn = dentry = entry->d_name;
2489 convlen = dentlen = D_NAMLEN (entry);
2490
2491 if (rl_filename_rewrite_hook)
2492 {
2493 convfn = (*rl_filename_rewrite_hook) (dentry, dentlen);
2494 convlen = (convfn == dentry) ? dentlen : strlen (convfn);
2495 }
2496
Jari Aaltof73dda02001-11-13 17:56:06 +00002497 /* Special case for no filename. If the user has disabled the
2498 `match-hidden-files' variable, skip filenames beginning with `.'.
2499 All other entries except "." and ".." match. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002500 if (filename_len == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002501 {
Chet Ramey30d188c2011-11-21 20:57:16 -05002502 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (convfn))
Jari Aaltof73dda02001-11-13 17:56:06 +00002503 continue;
2504
Chet Ramey00018032011-11-21 20:51:19 -05002505 if (convfn[0] != '.' ||
2506 (convfn[1] && (convfn[1] != '.' || convfn[2])))
Jari Aalto726f6381996-08-26 18:22:31 +00002507 break;
2508 }
2509 else
2510 {
Chet Ramey495aee42011-11-22 19:11:26 -05002511 if (complete_fncmp (convfn, convlen, filename, filename_len))
2512 break;
Jari Aalto726f6381996-08-26 18:22:31 +00002513 }
2514 }
2515
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002516 if (entry == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002517 {
2518 if (directory)
2519 {
2520 closedir (directory);
2521 directory = (DIR *)NULL;
2522 }
2523 if (dirname)
2524 {
Chet Ramey495aee42011-11-22 19:11:26 -05002525 xfree (dirname);
Jari Aalto726f6381996-08-26 18:22:31 +00002526 dirname = (char *)NULL;
2527 }
2528 if (filename)
2529 {
Chet Ramey495aee42011-11-22 19:11:26 -05002530 xfree (filename);
Jari Aalto726f6381996-08-26 18:22:31 +00002531 filename = (char *)NULL;
2532 }
2533 if (users_dirname)
2534 {
Chet Ramey495aee42011-11-22 19:11:26 -05002535 xfree (users_dirname);
Jari Aalto726f6381996-08-26 18:22:31 +00002536 users_dirname = (char *)NULL;
2537 }
2538
2539 return (char *)NULL;
2540 }
2541 else
2542 {
Jari Aalto726f6381996-08-26 18:22:31 +00002543 /* dirname && (strcmp (dirname, ".") != 0) */
2544 if (dirname && (dirname[0] != '.' || dirname[1]))
2545 {
2546 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
2547 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002548 dirlen = strlen (dirname);
Jari Aaltof73dda02001-11-13 17:56:06 +00002549 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
Jari Aalto726f6381996-08-26 18:22:31 +00002550 strcpy (temp, dirname);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002551 /* Canonicalization cuts off any final slash present. We
2552 may need to add it back. */
Jari Aalto726f6381996-08-26 18:22:31 +00002553 if (dirname[dirlen - 1] != '/')
2554 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002555 temp[dirlen++] = '/';
2556 temp[dirlen] = '\0';
Jari Aalto726f6381996-08-26 18:22:31 +00002557 }
2558 }
2559 else
2560 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002561 dirlen = strlen (users_dirname);
Jari Aaltof73dda02001-11-13 17:56:06 +00002562 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
Jari Aalto726f6381996-08-26 18:22:31 +00002563 strcpy (temp, users_dirname);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002564 /* Make sure that temp has a trailing slash here. */
2565 if (users_dirname[dirlen - 1] != '/')
2566 temp[dirlen++] = '/';
Jari Aalto726f6381996-08-26 18:22:31 +00002567 }
2568
Chet Ramey30d188c2011-11-21 20:57:16 -05002569 strcpy (temp + dirlen, convfn);
Jari Aalto726f6381996-08-26 18:22:31 +00002570 }
2571 else
Chet Ramey00018032011-11-21 20:51:19 -05002572 temp = savestring (convfn);
2573
2574 if (convfn != dentry)
Chet Ramey495aee42011-11-22 19:11:26 -05002575 xfree (convfn);
Jari Aalto726f6381996-08-26 18:22:31 +00002576
2577 return (temp);
2578 }
2579}
2580
Jari Aaltocce855b1998-04-17 19:52:44 +00002581/* An initial implementation of a menu completion function a la tcsh. The
Chet Ramey00018032011-11-21 20:51:19 -05002582 first time (if the last readline command was not rl_old_menu_complete), we
Jari Aaltocce855b1998-04-17 19:52:44 +00002583 generate the list of matches. This code is very similar to the code in
2584 rl_complete_internal -- there should be a way to combine the two. Then,
2585 for each item in the list of matches, we insert the match in an undoable
2586 fashion, with the appropriate character appended (this happens on the
Chet Ramey00018032011-11-21 20:51:19 -05002587 second and subsequent consecutive calls to rl_old_menu_complete). When we
Jari Aaltocce855b1998-04-17 19:52:44 +00002588 hit the end of the match list, we restore the original unmatched text,
2589 ring the bell, and reset the counter to zero. */
Jari Aalto726f6381996-08-26 18:22:31 +00002590int
Jari Aalto31859422009-01-12 13:36:28 +00002591rl_old_menu_complete (count, invoking_key)
2592 int count, invoking_key;
Jari Aalto726f6381996-08-26 18:22:31 +00002593{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002594 rl_compentry_func_t *our_func;
Jari Aaltocce855b1998-04-17 19:52:44 +00002595 int matching_filenames, found_quote;
Jari Aalto726f6381996-08-26 18:22:31 +00002596
Jari Aaltocce855b1998-04-17 19:52:44 +00002597 static char *orig_text;
2598 static char **matches = (char **)0;
2599 static int match_list_index = 0;
2600 static int match_list_size = 0;
2601 static int orig_start, orig_end;
2602 static char quote_char;
2603 static int delimiter;
Jari Aalto726f6381996-08-26 18:22:31 +00002604
Jari Aaltocce855b1998-04-17 19:52:44 +00002605 /* The first time through, we generate the list of matches and set things
2606 up to insert them. */
Chet Ramey89a92862011-11-21 20:49:12 -05002607 if (rl_last_func != rl_old_menu_complete)
Jari Aalto726f6381996-08-26 18:22:31 +00002608 {
Jari Aaltocce855b1998-04-17 19:52:44 +00002609 /* Clean up from previous call, if any. */
2610 FREE (orig_text);
2611 if (matches)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002612 _rl_free_match_list (matches);
Jari Aaltocce855b1998-04-17 19:52:44 +00002613
2614 match_list_index = match_list_size = 0;
2615 matches = (char **)NULL;
2616
Jari Aalto31859422009-01-12 13:36:28 +00002617 rl_completion_invoking_key = invoking_key;
2618
Chet Ramey89a92862011-11-21 20:49:12 -05002619 RL_SETSTATE(RL_STATE_COMPLETING);
2620
Jari Aaltocce855b1998-04-17 19:52:44 +00002621 /* Only the completion entry function can change these. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002622 set_completion_defaults ('%');
Jari Aaltocce855b1998-04-17 19:52:44 +00002623
Jari Aalto31859422009-01-12 13:36:28 +00002624 our_func = rl_menu_completion_entry_function;
2625 if (our_func == 0)
2626 our_func = rl_completion_entry_function
Jari Aaltocce855b1998-04-17 19:52:44 +00002627 ? rl_completion_entry_function
Jari Aalto28ef6c32001-04-06 19:14:31 +00002628 : rl_filename_completion_function;
Jari Aaltocce855b1998-04-17 19:52:44 +00002629
2630 /* We now look backwards for the start of a filename/variable word. */
2631 orig_end = rl_point;
2632 found_quote = delimiter = 0;
2633 quote_char = '\0';
2634
2635 if (rl_point)
2636 /* This (possibly) changes rl_point. If it returns a non-zero char,
2637 we know we have an open quote. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002638 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
Jari Aaltocce855b1998-04-17 19:52:44 +00002639
2640 orig_start = rl_point;
2641 rl_point = orig_end;
2642
2643 orig_text = rl_copy_text (orig_start, orig_end);
2644 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2645 our_func, found_quote, quote_char);
2646
Jari Aaltob72432f1999-02-19 17:11:39 +00002647 /* If we are matching filenames, the attempted completion function will
2648 have set rl_filename_completion_desired to a non-zero value. The basic
Jari Aalto28ef6c32001-04-06 19:14:31 +00002649 rl_filename_completion_function does this. */
Jari Aaltob72432f1999-02-19 17:11:39 +00002650 matching_filenames = rl_filename_completion_desired;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002651
Jari Aaltob72432f1999-02-19 17:11:39 +00002652 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00002653 {
Chet Ramey495aee42011-11-22 19:11:26 -05002654 rl_ding ();
Jari Aaltocce855b1998-04-17 19:52:44 +00002655 FREE (matches);
2656 matches = (char **)0;
2657 FREE (orig_text);
2658 orig_text = (char *)0;
Chet Ramey495aee42011-11-22 19:11:26 -05002659 completion_changed_buffer = 0;
2660 RL_UNSETSTATE(RL_STATE_COMPLETING);
2661 return (0);
Jari Aaltocce855b1998-04-17 19:52:44 +00002662 }
2663
Chet Ramey89a92862011-11-21 20:49:12 -05002664 RL_UNSETSTATE(RL_STATE_COMPLETING);
2665
Jari Aaltocce855b1998-04-17 19:52:44 +00002666 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2667 ;
2668 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2669 code below should take care of it. */
Jari Aalto31859422009-01-12 13:36:28 +00002670
2671 if (match_list_size > 1 && _rl_complete_show_all)
2672 display_matches (matches);
Jari Aaltocce855b1998-04-17 19:52:44 +00002673 }
2674
2675 /* Now we have the list of matches. Replace the text between
2676 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2677 matches[match_list_index], and add any necessary closing char. */
2678
2679 if (matches == 0 || match_list_size == 0)
2680 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00002681 rl_ding ();
Jari Aaltocce855b1998-04-17 19:52:44 +00002682 FREE (matches);
2683 matches = (char **)0;
2684 completion_changed_buffer = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002685 return (0);
Jari Aalto726f6381996-08-26 18:22:31 +00002686 }
Jari Aaltocce855b1998-04-17 19:52:44 +00002687
Jari Aalto95732b42005-12-07 14:08:12 +00002688 match_list_index += count;
Jari Aaltocce855b1998-04-17 19:52:44 +00002689 if (match_list_index < 0)
Chet Ramey495aee42011-11-22 19:11:26 -05002690 {
2691 while (match_list_index < 0)
2692 match_list_index += match_list_size;
2693 }
Jari Aalto95732b42005-12-07 14:08:12 +00002694 else
2695 match_list_index %= match_list_size;
Jari Aaltocce855b1998-04-17 19:52:44 +00002696
Jari Aaltob72432f1999-02-19 17:11:39 +00002697 if (match_list_index == 0 && match_list_size > 1)
Jari Aalto726f6381996-08-26 18:22:31 +00002698 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00002699 rl_ding ();
Jari Aaltocce855b1998-04-17 19:52:44 +00002700 insert_match (orig_text, orig_start, MULT_MATCH, &quote_char);
2701 }
2702 else
2703 {
2704 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
Jari Aaltof73dda02001-11-13 17:56:06 +00002705 append_to_match (matches[match_list_index], delimiter, quote_char,
2706 strcmp (orig_text, matches[match_list_index]));
Jari Aalto726f6381996-08-26 18:22:31 +00002707 }
2708
Jari Aaltocce855b1998-04-17 19:52:44 +00002709 completion_changed_buffer = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00002710 return (0);
2711}
Jari Aalto31859422009-01-12 13:36:28 +00002712
2713int
2714rl_menu_complete (count, ignore)
2715 int count, ignore;
2716{
2717 rl_compentry_func_t *our_func;
2718 int matching_filenames, found_quote;
2719
2720 static char *orig_text;
2721 static char **matches = (char **)0;
2722 static int match_list_index = 0;
2723 static int match_list_size = 0;
2724 static int nontrivial_lcd = 0;
2725 static int full_completion = 0; /* set to 1 if menu completion should reinitialize on next call */
2726 static int orig_start, orig_end;
2727 static char quote_char;
Chet Ramey495aee42011-11-22 19:11:26 -05002728 static int delimiter, cstate;
Jari Aalto31859422009-01-12 13:36:28 +00002729
2730 /* The first time through, we generate the list of matches and set things
2731 up to insert them. */
Chet Ramey00018032011-11-21 20:51:19 -05002732 if ((rl_last_func != rl_menu_complete && rl_last_func != rl_backward_menu_complete) || full_completion)
Jari Aalto31859422009-01-12 13:36:28 +00002733 {
2734 /* Clean up from previous call, if any. */
2735 FREE (orig_text);
2736 if (matches)
2737 _rl_free_match_list (matches);
2738
2739 match_list_index = match_list_size = 0;
2740 matches = (char **)NULL;
2741
2742 full_completion = 0;
2743
Chet Ramey89a92862011-11-21 20:49:12 -05002744 RL_SETSTATE(RL_STATE_COMPLETING);
2745
Jari Aalto31859422009-01-12 13:36:28 +00002746 /* Only the completion entry function can change these. */
2747 set_completion_defaults ('%');
2748
2749 our_func = rl_menu_completion_entry_function;
2750 if (our_func == 0)
2751 our_func = rl_completion_entry_function
2752 ? rl_completion_entry_function
2753 : rl_filename_completion_function;
2754
2755 /* We now look backwards for the start of a filename/variable word. */
2756 orig_end = rl_point;
2757 found_quote = delimiter = 0;
2758 quote_char = '\0';
2759
2760 if (rl_point)
2761 /* This (possibly) changes rl_point. If it returns a non-zero char,
2762 we know we have an open quote. */
2763 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2764
2765 orig_start = rl_point;
2766 rl_point = orig_end;
2767
2768 orig_text = rl_copy_text (orig_start, orig_end);
2769 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2770 our_func, found_quote, quote_char);
2771
2772 nontrivial_lcd = matches && strcmp (orig_text, matches[0]) != 0;
2773
2774 /* If we are matching filenames, the attempted completion function will
2775 have set rl_filename_completion_desired to a non-zero value. The basic
2776 rl_filename_completion_function does this. */
2777 matching_filenames = rl_filename_completion_desired;
2778
2779 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2780 {
Chet Ramey495aee42011-11-22 19:11:26 -05002781 rl_ding ();
Jari Aalto31859422009-01-12 13:36:28 +00002782 FREE (matches);
2783 matches = (char **)0;
2784 FREE (orig_text);
2785 orig_text = (char *)0;
Chet Ramey495aee42011-11-22 19:11:26 -05002786 completion_changed_buffer = 0;
2787 RL_UNSETSTATE(RL_STATE_COMPLETING);
2788 return (0);
Jari Aalto31859422009-01-12 13:36:28 +00002789 }
2790
Chet Ramey89a92862011-11-21 20:49:12 -05002791 RL_UNSETSTATE(RL_STATE_COMPLETING);
2792
Jari Aalto31859422009-01-12 13:36:28 +00002793 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2794 ;
2795
2796 if (match_list_size == 0)
2797 {
2798 rl_ding ();
2799 FREE (matches);
2800 matches = (char **)0;
2801 match_list_index = 0;
2802 completion_changed_buffer = 0;
2803 return (0);
2804 }
2805
2806 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2807 code below should take care of it. */
2808 if (*matches[0])
2809 {
2810 insert_match (matches[0], orig_start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
2811 orig_end = orig_start + strlen (matches[0]);
2812 completion_changed_buffer = STREQ (orig_text, matches[0]) == 0;
2813 }
2814
2815 if (match_list_size > 1 && _rl_complete_show_all)
2816 {
2817 display_matches (matches);
2818 /* If there are so many matches that the user has to be asked
2819 whether or not he wants to see the matches, menu completion
2820 is unwieldy. */
2821 if (rl_completion_query_items > 0 && match_list_size >= rl_completion_query_items)
2822 {
2823 rl_ding ();
2824 FREE (matches);
2825 matches = (char **)0;
2826 full_completion = 1;
2827 return (0);
2828 }
Chet Rameyac50fba2014-02-26 09:36:43 -05002829 else if (_rl_menu_complete_prefix_first)
2830 {
2831 rl_ding ();
2832 return (0);
2833 }
Jari Aalto31859422009-01-12 13:36:28 +00002834 }
2835 else if (match_list_size <= 1)
2836 {
2837 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
2838 full_completion = 1;
2839 return (0);
2840 }
Chet Ramey495aee42011-11-22 19:11:26 -05002841 else if (_rl_menu_complete_prefix_first && match_list_size > 1)
2842 {
2843 rl_ding ();
2844 return (0);
2845 }
Jari Aalto31859422009-01-12 13:36:28 +00002846 }
2847
2848 /* Now we have the list of matches. Replace the text between
2849 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2850 matches[match_list_index], and add any necessary closing char. */
2851
2852 if (matches == 0 || match_list_size == 0)
2853 {
2854 rl_ding ();
2855 FREE (matches);
2856 matches = (char **)0;
2857 completion_changed_buffer = 0;
2858 return (0);
2859 }
2860
2861 match_list_index += count;
2862 if (match_list_index < 0)
Chet Ramey495aee42011-11-22 19:11:26 -05002863 {
2864 while (match_list_index < 0)
2865 match_list_index += match_list_size;
2866 }
Jari Aalto31859422009-01-12 13:36:28 +00002867 else
2868 match_list_index %= match_list_size;
2869
2870 if (match_list_index == 0 && match_list_size > 1)
2871 {
2872 rl_ding ();
2873 insert_match (matches[0], orig_start, MULT_MATCH, &quote_char);
2874 }
2875 else
2876 {
2877 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, &quote_char);
2878 append_to_match (matches[match_list_index], delimiter, quote_char,
2879 strcmp (orig_text, matches[match_list_index]));
2880 }
2881
2882 completion_changed_buffer = 1;
2883 return (0);
2884}
Chet Ramey00018032011-11-21 20:51:19 -05002885
2886int
2887rl_backward_menu_complete (count, key)
2888 int count, key;
2889{
2890 /* Positive arguments to backward-menu-complete translate into negative
2891 arguments for menu-complete, and vice versa. */
2892 return (rl_menu_complete (-count, key));
2893}