Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* complete.c -- filename completion for readline. */ |
| 2 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1987-2012 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5 | This file is part of the GNU Readline Library (Readline), a library |
| 6 | for reading lines of text with interactive input and history editing. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Readline is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Readline is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 16 | GNU General Public License for more details. |
| 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Readline. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 22 | #define READLINE_LIBRARY |
| 23 | |
| 24 | #if defined (HAVE_CONFIG_H) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 25 | # include <config.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <fcntl.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 30 | #if defined (HAVE_SYS_FILE_H) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 31 | # include <sys/file.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 32 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 33 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 34 | #include <signal.h> |
| 35 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 36 | #if defined (HAVE_UNISTD_H) |
| 37 | # include <unistd.h> |
| 38 | #endif /* HAVE_UNISTD_H */ |
| 39 | |
| 40 | #if defined (HAVE_STDLIB_H) |
| 41 | # include <stdlib.h> |
| 42 | #else |
| 43 | # include "ansi_stdlib.h" |
| 44 | #endif /* HAVE_STDLIB_H */ |
| 45 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 46 | #include <stdio.h> |
| 47 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 48 | #include <errno.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 49 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 50 | #include <errno.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 51 | #endif /* !errno */ |
| 52 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 53 | #if defined (HAVE_PWD_H) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 54 | #include <pwd.h> |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 55 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 56 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 57 | #include "posixdir.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 58 | #include "posixstat.h" |
| 59 | |
| 60 | /* System-specific feature definitions and include files. */ |
| 61 | #include "rldefs.h" |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 62 | #include "rlmbutil.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 63 | |
| 64 | /* Some standard library routines. */ |
| 65 | #include "readline.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 66 | #include "xmalloc.h" |
| 67 | #include "rlprivate.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 68 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 69 | #if defined (COLOR_SUPPORT) |
| 70 | # include "colors.h" |
| 71 | #endif |
| 72 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 73 | #ifdef __STDC__ |
| 74 | typedef int QSFUNC (const void *, const void *); |
| 75 | #else |
| 76 | typedef int QSFUNC (); |
| 77 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 78 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 79 | #ifdef HAVE_LSTAT |
| 80 | # define LSTAT lstat |
| 81 | #else |
| 82 | # define LSTAT stat |
| 83 | #endif |
| 84 | |
| 85 | /* Unix version of a hidden file. Could be different on other systems. */ |
| 86 | #define HIDDEN_FILE(fname) ((fname)[0] == '.') |
| 87 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 88 | /* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is |
| 89 | defined. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 90 | #if defined (HAVE_GETPWENT) && (!defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE)) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 91 | extern struct passwd *getpwent PARAMS((void)); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 92 | #endif /* HAVE_GETPWENT && (!HAVE_GETPW_DECLS || _POSIX_SOURCE) */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 93 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 94 | /* If non-zero, then this is the address of a function to call when |
| 95 | completing a word would normally display the list of possible matches. |
| 96 | This function is called instead of actually doing the display. |
| 97 | It takes three arguments: (char **matches, int num_matches, int max_length) |
| 98 | where MATCHES is the array of strings that matched, NUM_MATCHES is the |
| 99 | number of strings in that array, and MAX_LENGTH is the length of the |
| 100 | longest string in that array. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 101 | rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 102 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 103 | #if defined (VISIBLE_STATS) || defined (COLOR_SUPPORT) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 104 | # if !defined (X_OK) |
| 105 | # define X_OK 1 |
| 106 | # endif |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | #if defined (VISIBLE_STATS) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 110 | static int stat_char PARAMS((char *)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 111 | #endif |
| 112 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 113 | #if defined (COLOR_SUPPORT) |
| 114 | static int colored_stat_start PARAMS((char *)); |
| 115 | static void colored_stat_end PARAMS((void)); |
| 116 | #endif |
| 117 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 118 | static int path_isdir PARAMS((const char *)); |
| 119 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 120 | static char *rl_quote_filename PARAMS((char *, int, char *)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 121 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 122 | static void _rl_complete_sigcleanup PARAMS((int, void *)); |
| 123 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 124 | static void set_completion_defaults PARAMS((int)); |
| 125 | static int get_y_or_n PARAMS((int)); |
| 126 | static int _rl_internal_pager PARAMS((int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 127 | static char *printable_part PARAMS((char *)); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 128 | static int fnwidth PARAMS((const char *)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 129 | static int fnprint PARAMS((const char *, int)); |
| 130 | static int print_filename PARAMS((char *, char *, int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 131 | |
| 132 | static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int)); |
| 133 | |
| 134 | static char **remove_duplicate_matches PARAMS((char **)); |
| 135 | static void insert_match PARAMS((char *, int, int, char *)); |
| 136 | static int append_to_match PARAMS((char *, int, int, int)); |
| 137 | static void insert_all_matches PARAMS((char **, int, char *)); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 138 | static int complete_fncmp PARAMS((const char *, int, const char *, int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 139 | static void display_matches PARAMS((char **)); |
| 140 | static int compute_lcd_of_matches PARAMS((char **, int, const char *)); |
| 141 | static int postprocess_matches PARAMS((char ***, int)); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 142 | static int complete_get_screenwidth PARAMS((void)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 143 | |
| 144 | static char *make_quoted_replacement PARAMS((char *, int, char *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 145 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 146 | /* **************************************************************** */ |
| 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. */ |
| 155 | int _rl_complete_show_all = 0; |
| 156 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 157 | /* 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. */ |
| 159 | int _rl_complete_show_unmodified = 0; |
| 160 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 161 | /* If non-zero, completed directory names have a slash appended. */ |
| 162 | int _rl_complete_mark_directories = 1; |
| 163 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 164 | /* 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. */ |
| 168 | int _rl_complete_mark_symlink_dirs = 0; |
| 169 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 170 | /* If non-zero, completions are printed horizontally in alphabetical order, |
| 171 | like `ls -x'. */ |
| 172 | int _rl_print_completions_horizontally; |
| 173 | |
| 174 | /* Non-zero means that case is not significant in filename completion. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 175 | #if defined (__MSDOS__) && !defined (__DJGPP__) |
| 176 | int _rl_completion_case_fold = 1; |
| 177 | #else |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 178 | int _rl_completion_case_fold = 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 179 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 180 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 181 | /* Non-zero means that `-' and `_' are equivalent when comparing filenames |
| 182 | for completion. */ |
| 183 | int _rl_completion_case_map = 0; |
| 184 | |
| 185 | /* If zero, don't match hidden files (filenames beginning with a `.' on |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 186 | Unix) when doing filename completion. */ |
| 187 | int _rl_match_hidden_files = 1; |
| 188 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 189 | /* 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. */ |
| 193 | int _rl_completion_prefix_display_length = 0; |
| 194 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 195 | /* The readline-private number of screen columns to use when displaying |
| 196 | matches. If < 0 or > _rl_screenwidth, it is ignored. */ |
| 197 | int _rl_completion_columns = -1; |
| 198 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 199 | /* 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. */ |
| 205 | int rl_visible_stats = 0; |
| 206 | #endif /* VISIBLE_STATS */ |
| 207 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 208 | #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. */ |
| 211 | int _rl_colored_stats = 0; |
| 212 | #endif |
| 213 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 214 | /* 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'. */ |
| 218 | int _rl_skip_completed_text = 0; |
| 219 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 220 | /* If non-zero, menu completion displays the common prefix first in the |
| 221 | cycle of possible completions instead of the last. */ |
| 222 | int _rl_menu_complete_prefix_first = 0; |
| 223 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 224 | /* 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 227 | rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL; |
| 228 | |
| 229 | rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 230 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 231 | rl_icppfunc_t *rl_filename_stat_hook = (rl_icppfunc_t *)NULL; |
| 232 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 233 | /* 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. */ |
| 242 | rl_dequote_func_t *rl_filename_rewrite_hook = (rl_dequote_func_t *)NULL; |
| 243 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 244 | /* Non-zero means readline completion functions perform tilde expansion. */ |
| 245 | int rl_complete_with_tilde_expansion = 0; |
| 246 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 247 | /* Pointer to the generator function for completion_matches (). |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 248 | NULL means to use rl_filename_completion_function (), the default filename |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 249 | completer. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 250 | rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 251 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 252 | /* Pointer to generator function for rl_menu_complete (). NULL means to use |
| 253 | *rl_completion_entry_function (see above). */ |
| 254 | rl_compentry_func_t *rl_menu_completion_entry_function = (rl_compentry_func_t *)NULL; |
| 255 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 256 | /* 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 263 | rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 264 | |
| 265 | /* Non-zero means to suppress normal filename completion after the |
| 266 | user-specified completion function has been called. */ |
| 267 | int rl_attempted_completion_over = 0; |
| 268 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 269 | /* Set to a character indicating the type of completion being performed |
| 270 | by rl_complete_internal, available for use by application completion |
| 271 | functions. */ |
| 272 | int rl_completion_type = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 273 | |
| 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 Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 276 | she is sure she wants to see them all. A negative value means |
| 277 | don't ask. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 278 | int rl_completion_query_items = 100; |
| 279 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 280 | int _rl_page_completions = 1; |
| 281 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 282 | /* 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 285 | const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 286 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 287 | /* List of basic quoting characters. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 288 | const char *rl_basic_quote_characters = "\"'"; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 289 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 290 | /* 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 293 | /*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. */ |
| 298 | rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 299 | |
| 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 304 | const char *rl_completer_quote_characters = (const char *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 305 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 306 | /* List of characters that should be quoted in filenames by the completer. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 307 | const char *rl_filename_quote_characters = (const char *)NULL; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 308 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 309 | /* 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 312 | const char *rl_special_prefixes = (const char *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 313 | |
| 314 | /* If non-zero, then disallow duplicates in the matches. */ |
| 315 | int 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. */ |
| 320 | int 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 324 | filename contains any characters in rl_filename_quote_chars. This is |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 325 | ALWAYS non-zero on entry, and can only be changed within a completion |
| 326 | entry finder function. */ |
| 327 | int 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 338 | rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 339 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 340 | /* 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 344 | rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 345 | |
| 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 350 | rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 351 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 352 | /* 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 355 | rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 356 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 357 | /* 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. */ |
| 360 | int rl_completion_suppress_append = 0; |
| 361 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 362 | /* Character appended to completed words when at the end of the line. The |
| 363 | default is a space. */ |
| 364 | int rl_completion_append_character = ' '; |
| 365 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 366 | /* 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. */ |
| 369 | int rl_completion_suppress_quote = 0; |
| 370 | |
| 371 | /* Set to any quote character readline thinks it finds before any application |
| 372 | completion function is called. */ |
| 373 | int 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. */ |
| 377 | int rl_completion_found_quote; |
| 378 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 379 | /* 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. */ |
| 388 | int rl_completion_mark_symlink_dirs; |
| 389 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 390 | /* If non-zero, inhibit completion (temporarily). */ |
| 391 | int rl_inhibit_completion; |
| 392 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 393 | /* Set to the last key used to invoke one of the completion functions */ |
| 394 | int rl_completion_invoking_key; |
| 395 | |
| 396 | /* If non-zero, sort the completion matches. On by default. */ |
| 397 | int rl_sort_completion_matches = 1; |
| 398 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 399 | /* Variables local to this file. */ |
| 400 | |
| 401 | /* Local variable states what happened during the last completion attempt. */ |
| 402 | static int completion_changed_buffer; |
| 403 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 404 | /* The result of the query to the user about displaying completion matches */ |
| 405 | static int completion_y_or_n; |
| 406 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 407 | /*************************************/ |
| 408 | /* */ |
| 409 | /* Bindable completion functions */ |
| 410 | /* */ |
| 411 | /*************************************/ |
| 412 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 413 | /* Complete the word at or before point. You have supplied the function |
| 414 | that does the initial simple matching selection algorithm (see |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 415 | rl_completion_matches ()). The default is to do filename completion. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 416 | int |
| 417 | rl_complete (ignore, invoking_key) |
| 418 | int ignore, invoking_key; |
| 419 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 420 | rl_completion_invoking_key = invoking_key; |
| 421 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 422 | if (rl_inhibit_completion) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 423 | return (_rl_insert_char (ignore, invoking_key)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 424 | 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 428 | else if (_rl_complete_show_unmodified) |
| 429 | return (rl_complete_internal ('@')); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 430 | else |
| 431 | return (rl_complete_internal (TAB)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 432 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 433 | |
| 434 | /* List the possible completions. See description of rl_complete (). */ |
| 435 | int |
| 436 | rl_possible_completions (ignore, invoking_key) |
| 437 | int ignore, invoking_key; |
| 438 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 439 | rl_completion_invoking_key = invoking_key; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 440 | return (rl_complete_internal ('?')); |
| 441 | } |
| 442 | |
| 443 | int |
| 444 | rl_insert_completions (ignore, invoking_key) |
| 445 | int ignore, invoking_key; |
| 446 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 447 | rl_completion_invoking_key = invoking_key; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 448 | return (rl_complete_internal ('*')); |
| 449 | } |
| 450 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 451 | /* 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. */ |
| 456 | int |
| 457 | rl_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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 464 | else if (_rl_complete_show_unmodified) |
| 465 | return '@'; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 466 | else |
| 467 | return TAB; |
| 468 | } |
| 469 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 470 | /************************************/ |
| 471 | /* */ |
| 472 | /* Completion utility functions */ |
| 473 | /* */ |
| 474 | /************************************/ |
| 475 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 476 | /* Reset readline state on a signal or other event. */ |
| 477 | void |
| 478 | _rl_reset_completion_state () |
| 479 | { |
| 480 | rl_completion_found_quote = 0; |
| 481 | rl_completion_quote_character = 0; |
| 482 | } |
| 483 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 484 | static 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 493 | /* Set default values for readline word completion. These are the variables |
| 494 | that application completion functions can change or inspect. */ |
| 495 | static void |
| 496 | set_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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 503 | rl_completion_suppress_append = rl_completion_suppress_quote = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 504 | rl_completion_append_character = ' '; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 505 | |
| 506 | /* The completion entry function may optionally change this. */ |
| 507 | rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs; |
| 508 | } |
| 509 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 510 | /* The user must press "y" or "n". Non-zero return means "y" pressed. */ |
| 511 | static int |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 512 | get_y_or_n (for_pager) |
| 513 | int for_pager; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 514 | { |
| 515 | int c; |
| 516 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 517 | /* 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 525 | for (;;) |
| 526 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 527 | RL_SETSTATE(RL_STATE_MOREINPUT); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 528 | c = rl_read_key (); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 529 | RL_UNSETSTATE(RL_STATE_MOREINPUT); |
| 530 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 531 | if (c == 'y' || c == 'Y' || c == ' ') |
| 532 | return (1); |
| 533 | if (c == 'n' || c == 'N' || c == RUBOUT) |
| 534 | return (0); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 535 | if (c == ABORT_CHAR || c < 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 536 | _rl_abort_internal (); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 537 | if (for_pager && (c == NEWLINE || c == RETURN)) |
| 538 | return (2); |
| 539 | if (for_pager && (c == 'q' || c == 'Q')) |
| 540 | return (0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 541 | rl_ding (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 544 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 545 | static 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 563 | static int |
| 564 | path_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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 572 | #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 */ |
| 581 | static int |
| 582 | stat_char (filename) |
| 583 | char *filename; |
| 584 | { |
| 585 | struct stat finfo; |
| 586 | int character, r; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 587 | char *f; |
| 588 | const char *fn; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 589 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 590 | /* 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 597 | 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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 607 | #if defined (HAVE_LSTAT) && defined (S_ISLNK) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 608 | r = lstat (fn, &finfo); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 609 | #else |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 610 | r = stat (fn, &finfo); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 611 | #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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 644 | |
| 645 | free (f); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 646 | return (character); |
| 647 | } |
| 648 | #endif /* VISIBLE_STATS */ |
| 649 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 650 | #if defined (COLOR_SUPPORT) |
| 651 | static int |
| 652 | colored_stat_start (filename) |
| 653 | char *filename; |
| 654 | { |
| 655 | _rl_set_normal_color (); |
| 656 | return (_rl_print_color_indicator (filename)); |
| 657 | } |
| 658 | |
| 659 | static void |
| 660 | colored_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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 667 | /* 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 670 | 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 675 | static char * |
| 676 | printable_part (pathname) |
| 677 | char *pathname; |
| 678 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 679 | char *temp, *x; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 680 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 681 | if (rl_filename_completion_desired == 0) /* don't need to do anything */ |
| 682 | return (pathname); |
| 683 | |
| 684 | temp = strrchr (pathname, '/'); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 685 | #if defined (__MSDOS__) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 686 | if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':') |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 687 | temp = pathname + 1; |
| 688 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 689 | |
| 690 | if (temp == 0 || *temp == '\0') |
| 691 | return (pathname); |
Chet Ramey | ce97d60 | 2015-08-13 15:38:31 -0400 | [diff] [blame] | 692 | else if (temp[1] == 0 && temp == pathname) |
| 693 | return (pathname); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 694 | /* 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 709 | /* Compute width of STRING when displayed on screen by print_filename */ |
| 710 | static int |
| 711 | fnwidth (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 Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 728 | if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 729 | { |
| 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 748 | w = WCWIDTH (wc); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 749 | width += (w >= 0) ? w : 1; |
| 750 | } |
| 751 | #else |
| 752 | width++; |
| 753 | pos++; |
| 754 | #endif |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | return width; |
| 759 | } |
| 760 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 761 | #define ELLIPSIS_LEN 3 |
| 762 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 763 | static int |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 764 | fnprint (to_print, prefix_bytes) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 765 | const char *to_print; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 766 | int prefix_bytes; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 767 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 768 | int printed_len, w; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 769 | const char *s; |
| 770 | #if defined (HANDLE_MULTIBYTE) |
| 771 | mbstate_t ps; |
| 772 | const char *end; |
| 773 | size_t tlen; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 774 | int width; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 775 | wchar_t wc; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 776 | |
| 777 | end = to_print + strlen (to_print) + 1; |
| 778 | memset (&ps, 0, sizeof (mbstate_t)); |
| 779 | #endif |
| 780 | |
| 781 | printed_len = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 782 | |
| 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 799 | 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 Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 824 | tlen = mbrtowc (&wc, s, end - s, &ps); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 825 | if (MB_INVALIDCH (tlen)) |
| 826 | { |
| 827 | tlen = 1; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 828 | width = 1; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 829 | memset (&ps, 0, sizeof (mbstate_t)); |
| 830 | } |
| 831 | else if (MB_NULLWCH (tlen)) |
| 832 | break; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 833 | else |
| 834 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 835 | w = WCWIDTH (wc); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 836 | width = (w >= 0) ? w : 1; |
| 837 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 838 | fwrite (s, 1, tlen, rl_outstream); |
| 839 | s += tlen; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 840 | printed_len += width; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 841 | #else |
| 842 | putc (*s, rl_outstream); |
| 843 | s++; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 844 | printed_len++; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 845 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | return printed_len; |
| 850 | } |
| 851 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 852 | /* 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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 854 | filenames. Return the number of characters we output. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 855 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 856 | static int |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 857 | print_filename (to_print, full_pathname, prefix_bytes) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 858 | char *to_print, *full_pathname; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 859 | int prefix_bytes; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 860 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 861 | int printed_len, extension_char, slen, tlen; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 862 | char *s, c, *new_full_pathname, *dn; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 863 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 864 | extension_char = 0; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 865 | #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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 868 | #endif |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 869 | 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 879 | { |
| 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 Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 889 | /* 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 Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 894 | 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 905 | if (rl_directory_completion_hook) |
| 906 | (*rl_directory_completion_hook) (&s); |
| 907 | |
| 908 | slen = strlen (s); |
| 909 | tlen = strlen (to_print); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 910 | new_full_pathname = (char *)xmalloc (slen + tlen + 2); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 911 | strcpy (new_full_pathname, s); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 912 | if (s[slen - 1] == '/') |
| 913 | slen--; |
| 914 | else |
| 915 | new_full_pathname[slen] = '/'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 916 | new_full_pathname[slen] = '/'; |
| 917 | strcpy (new_full_pathname + slen + 1, to_print); |
| 918 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 919 | #if defined (VISIBLE_STATS) |
| 920 | if (rl_visible_stats) |
| 921 | extension_char = stat_char (new_full_pathname); |
| 922 | else |
| 923 | #endif |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 924 | 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 946 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 947 | xfree (new_full_pathname); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 948 | to_print[-1] = c; |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | s = tilde_expand (full_pathname); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 953 | #if defined (VISIBLE_STATS) |
| 954 | if (rl_visible_stats) |
| 955 | extension_char = stat_char (s); |
| 956 | else |
| 957 | #endif |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 958 | if (_rl_complete_mark_directories && path_isdir (s)) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 959 | extension_char = '/'; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 960 | |
| 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 972 | xfree (s); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 973 | if (extension_char) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 974 | { |
| 975 | putc (extension_char, rl_outstream); |
| 976 | printed_len++; |
| 977 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 978 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 979 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 980 | return printed_len; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 983 | static char * |
| 984 | rl_quote_filename (s, rtype, qcp) |
| 985 | char *s; |
| 986 | int rtype; |
| 987 | char *qcp; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 988 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 989 | char *r; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 990 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 991 | r = (char *)xmalloc (strlen (s) + 2); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 992 | *r = *rl_completer_quote_characters; |
| 993 | strcpy (r + 1, s); |
| 994 | if (qcp) |
| 995 | *qcp = *rl_completer_quote_characters; |
| 996 | return r; |
| 997 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 998 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 999 | /* 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 Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1002 | rl_completer_quote_characters). First we try to find an unclosed |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1003 | 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1013 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1014 | char |
| 1015 | _rl_find_completion_word (fp, dp) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1016 | int *fp, *dp; |
| 1017 | { |
| 1018 | int scan, end, found_quote, delimiter, pass_next, isbrk; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1019 | char quote_char, *brkchars; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1020 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1021 | end = rl_point; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1022 | found_quote = delimiter = 0; |
| 1023 | quote_char = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1024 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1025 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1031 | if (rl_completer_quote_characters) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1032 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1033 | /* 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 Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1037 | for (scan = pass_next = 0; scan < end; scan = MB_NEXTCHAR (rl_line_buffer, scan, 1, MB_FIND_ANY)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1038 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1039 | if (pass_next) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1040 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1041 | pass_next = 0; |
| 1042 | continue; |
| 1043 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1044 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1045 | /* 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1050 | { |
| 1051 | pass_next = 1; |
| 1052 | found_quote |= RL_QF_BACKSLASH; |
| 1053 | continue; |
| 1054 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1055 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1056 | if (quote_char != '\0') |
| 1057 | { |
| 1058 | /* Ignore everything until the matching close quote char. */ |
| 1059 | if (rl_line_buffer[scan] == quote_char) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1060 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1061 | /* Found matching close. Abandon this substring. */ |
| 1062 | quote_char = '\0'; |
| 1063 | rl_point = end; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1066 | else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan])) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1067 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1068 | /* 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1076 | else |
| 1077 | found_quote |= RL_QF_OTHER_QUOTE; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1082 | 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 Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1087 | while (rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_ANY)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1088 | { |
| 1089 | scan = rl_line_buffer[rl_point]; |
| 1090 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1091 | if (strchr (brkchars, scan) == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1092 | 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 Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1112 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1115 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1116 | if (rl_char_is_quoted_p) |
| 1117 | isbrk = (found_quote == 0 || |
| 1118 | (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) && |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1119 | strchr (brkchars, scan) != 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1120 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1121 | isbrk = strchr (brkchars, scan) != 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1122 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1123 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | if (fp) |
| 1140 | *fp = found_quote; |
| 1141 | if (dp) |
| 1142 | *dp = delimiter; |
| 1143 | |
| 1144 | return (quote_char); |
| 1145 | } |
| 1146 | |
| 1147 | static char ** |
| 1148 | gen_completion_matches (text, start, end, our_func, found_quote, quote_char) |
| 1149 | char *text; |
| 1150 | int start, end; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1151 | rl_compentry_func_t *our_func; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1152 | int found_quote, quote_char; |
| 1153 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1154 | char **matches; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1155 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1156 | rl_completion_found_quote = found_quote; |
| 1157 | rl_completion_quote_character = quote_char; |
| 1158 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1159 | /* 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1165 | if (RL_SIG_RECEIVED()) |
| 1166 | { |
| 1167 | _rl_free_match_list (matches); |
| 1168 | matches = 0; |
| 1169 | RL_CHECK_SIGNALS (); |
| 1170 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1171 | |
| 1172 | if (matches || rl_attempted_completion_over) |
| 1173 | { |
| 1174 | rl_attempted_completion_over = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1175 | return (matches); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1179 | /* XXX -- filename dequoting moved into rl_filename_completion_function */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1180 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1181 | /* rl_completion_matches will check for signals as well to avoid a long |
| 1182 | delay while reading a directory. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1183 | matches = rl_completion_matches (text, our_func); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1184 | if (RL_SIG_RECEIVED()) |
| 1185 | { |
| 1186 | _rl_free_match_list (matches); |
| 1187 | matches = 0; |
| 1188 | RL_CHECK_SIGNALS (); |
| 1189 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1190 | return matches; |
| 1191 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1192 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1193 | /* Filter out duplicates in MATCHES. This frees up the strings in |
| 1194 | MATCHES. */ |
| 1195 | static char ** |
| 1196 | remove_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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1203 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1204 | /* 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 Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1210 | if (i && rl_sort_completion_matches) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1211 | qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1212 | |
| 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1217 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1218 | if (strcmp (matches[i], matches[i + 1]) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1219 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1220 | xfree (matches[i]); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1221 | matches[i] = (char *)&dead_slot; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1222 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1223 | else |
| 1224 | newlen++; |
| 1225 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1226 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1227 | /* 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1236 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1237 | if (matches[0] != (char *)&dead_slot) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1238 | xfree (matches[0]); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1239 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1240 | /* Place the lowest common denominator back in [0]. */ |
| 1241 | temp_array[0] = lowest_common; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1242 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1243 | /* 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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1248 | xfree (temp_array[1]); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1249 | temp_array[1] = (char *)NULL; |
| 1250 | } |
| 1251 | return (temp_array); |
| 1252 | } |
| 1253 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1254 | /* Find the common prefix of the list of matches, and put it into |
| 1255 | matches[0]. */ |
| 1256 | static int |
| 1257 | compute_lcd_of_matches (match_list, matches, text) |
| 1258 | char **match_list; |
| 1259 | int matches; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1260 | const char *text; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1261 | { |
| 1262 | register int i, c1, c2, si; |
| 1263 | int low; /* Count of max-matched characters. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1264 | int lx; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1265 | char *dtext; /* dequoted TEXT, if needed */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1266 | #if defined (HANDLE_MULTIBYTE) |
| 1267 | int v; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1268 | size_t v1, v2; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1269 | mbstate_t ps1, ps2; |
| 1270 | wchar_t wc1, wc2; |
| 1271 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1272 | |
| 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1285 | #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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1292 | 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1298 | #if defined (HANDLE_MULTIBYTE) |
| 1299 | if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) |
| 1300 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1301 | 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1309 | wc1 = towlower (wc1); |
| 1310 | wc2 = towlower (wc2); |
| 1311 | if (wc1 != wc2) |
| 1312 | break; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1313 | else if (v1 > 1) |
| 1314 | si += v1 - 1; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1315 | } |
| 1316 | else |
| 1317 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1318 | 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1327 | #if defined (HANDLE_MULTIBYTE) |
| 1328 | if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) |
| 1329 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1330 | mbstate_t ps_back; |
| 1331 | ps_back = ps1; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1332 | 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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1339 | 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 Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1352 | match_list[0] = (char *)xmalloc (strlen (text) + 1); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1353 | strcpy (match_list[0], text); |
| 1354 | } |
| 1355 | else |
| 1356 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1357 | match_list[0] = (char *)xmalloc (low + 1); |
| 1358 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1359 | /* XXX - this might need changes in the presence of multibyte chars */ |
| 1360 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1361 | /* 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1365 | /* 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 Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1381 | dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1382 | text = dtext; |
| 1383 | } |
| 1384 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1385 | /* 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1389 | 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1403 | |
| 1404 | FREE (dtext); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1405 | } |
| 1406 | else |
| 1407 | strncpy (match_list[0], match_list[1], low); |
| 1408 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1409 | match_list[0][low] = '\0'; |
| 1410 | } |
| 1411 | |
| 1412 | return matches; |
| 1413 | } |
| 1414 | |
| 1415 | static int |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1416 | postprocess_matches (matchesp, matching_filenames) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1417 | char ***matchesp; |
| 1418 | int matching_filenames; |
| 1419 | { |
| 1420 | char *t, **matches, **temp_matches; |
| 1421 | int nmatch, i; |
| 1422 | |
| 1423 | matches = *matchesp; |
| 1424 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1425 | if (matches == 0) |
| 1426 | return 0; |
| 1427 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1428 | /* It seems to me that in all the cases we handle we would like |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1429 | to ignore duplicate possibilities. Scan for the text to |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1430 | insert being identical to the other completions. */ |
| 1431 | if (rl_ignore_completion_duplicates) |
| 1432 | { |
| 1433 | temp_matches = remove_duplicate_matches (matches); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1434 | xfree (matches); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1435 | 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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1450 | *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 Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1461 | compute_lcd_of_matches (matches, i - 1, t); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1462 | FREE (t); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | *matchesp = matches; |
| 1468 | return (1); |
| 1469 | } |
| 1470 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1471 | static int |
| 1472 | complete_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 Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1488 | /* 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. */ |
| 1492 | void |
| 1493 | rl_display_match_list (matches, len, max) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1494 | char **matches; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1495 | int len, max; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1496 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1497 | int count, limit, printed_len, lines, cols; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1498 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1516 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1517 | /* How many items of MAX length can we fit in the screen window? */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1518 | cols = complete_get_screenwidth (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1519 | max += 2; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1520 | limit = cols / max; |
| 1521 | if (limit != 1 && (limit * max == cols)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1522 | limit--; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1523 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1524 | /* 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1529 | limit will be 0 and a divide-by-zero fault will result. */ |
| 1530 | if (limit == 0) |
| 1531 | limit = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1532 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1533 | /* How many iterations of the printing loop? */ |
| 1534 | count = (len + (limit - 1)) / limit; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1535 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1536 | /* 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1539 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1540 | /* Sort the items if they are not already sorted. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1541 | if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1542 | qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1543 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1544 | rl_crlf (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1545 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1546 | lines = 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1547 | if (_rl_print_completions_horizontally == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1548 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1549 | /* Print the sorted items, up-and-down alphabetically, like ls. */ |
| 1550 | for (i = 1; i <= count; i++) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1551 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1552 | for (j = 0, l = i; j < limit; j++) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1553 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1554 | if (l > len || matches[l] == 0) |
| 1555 | break; |
| 1556 | else |
| 1557 | { |
| 1558 | temp = printable_part (matches[l]); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1559 | printed_len = print_filename (temp, matches[l], sind); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1560 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1561 | if (j + 1 < limit) |
| 1562 | for (k = 0; k < max - printed_len; k++) |
| 1563 | putc (' ', rl_outstream); |
| 1564 | } |
| 1565 | l += count; |
| 1566 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1567 | rl_crlf (); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1568 | 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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1575 | } |
| 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 Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1583 | printed_len = print_filename (temp, matches[i], sind); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1584 | /* Have we reached the end of this line? */ |
| 1585 | if (matches[i+1]) |
| 1586 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1587 | if (limit == 1 || (i && (limit > 1) && (i % limit) == 0)) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1588 | { |
| 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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1598 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1599 | for (k = 0; k < max - printed_len; k++) |
| 1600 | putc (' ', rl_outstream); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1601 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1602 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1603 | rl_crlf (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1604 | } |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1605 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1606 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1607 | /* 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. */ |
| 1618 | static void |
| 1619 | display_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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1632 | rl_crlf (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1633 | print_filename (temp, matches[0], 0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1634 | rl_crlf (); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1635 | |
| 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1647 | len = fnwidth (temp); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1648 | |
| 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 Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1664 | if (rl_completion_query_items > 0 && len >= rl_completion_query_items) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1665 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1666 | rl_crlf (); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1667 | fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len); |
| 1668 | fflush (rl_outstream); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1669 | if ((completion_y_or_n = get_y_or_n (0)) == 0) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1670 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1671 | rl_crlf (); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1672 | |
| 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1682 | rl_forced_update_display (); |
| 1683 | rl_display_fixed = 1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1686 | static char * |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1687 | make_quoted_replacement (match, mtype, qc) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1688 | char *match; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1689 | int mtype; |
| 1690 | char *qc; /* Pointer to quoting character, if any */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1691 | { |
| 1692 | int should_quote, do_replace; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1693 | char *replacement; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1694 | |
| 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 Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1710 | should_quote = should_quote && (!qc || !*qc || |
| 1711 | (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc))); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1712 | |
| 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 Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1718 | should_quote = rl_filename_quote_characters |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1719 | ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0) |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1720 | : 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1721 | |
| 1722 | do_replace = should_quote ? mtype : NO_MATCH; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1723 | /* 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1727 | } |
| 1728 | return (replacement); |
| 1729 | } |
| 1730 | |
| 1731 | static void |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1732 | insert_match (match, start, mtype, qc) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1733 | char *match; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1734 | int start, mtype; |
| 1735 | char *qc; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1736 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1737 | char *replacement, *r; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1738 | char oqc; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1739 | int end, rlen; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1740 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1741 | oqc = qc ? *qc : '\0'; |
| 1742 | replacement = make_quoted_replacement (match, mtype, qc); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1743 | |
| 1744 | /* Now insert the match. */ |
| 1745 | if (replacement) |
| 1746 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1747 | rlen = strlen (replacement); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1748 | /* Don't double an opening quote character. */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1749 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1756 | start--; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1757 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1775 | if (replacement != match) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1776 | xfree (replacement); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1777 | } |
| 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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1783 | default trailing character is a space. Returns the number of characters |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1784 | 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1786 | 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 Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1791 | static int |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1792 | append_to_match (text, delimiter, quote_char, nontrivial_match) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1793 | char *text; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1794 | int delimiter, quote_char, nontrivial_match; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1795 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1796 | char temp_string[4], *filename, *fn; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1797 | int temp_string_index, s; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1798 | struct stat finfo; |
| 1799 | |
| 1800 | temp_string_index = 0; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1801 | if (quote_char && rl_point && rl_completion_suppress_quote == 0 && |
| 1802 | rl_line_buffer[rl_point - 1] != quote_char) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1803 | temp_string[temp_string_index++] = quote_char; |
| 1804 | |
| 1805 | if (delimiter) |
| 1806 | temp_string[temp_string_index++] = delimiter; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1807 | else if (rl_completion_suppress_append == 0 && rl_completion_append_character) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1808 | 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1815 | if (rl_filename_stat_hook) |
| 1816 | { |
| 1817 | fn = savestring (filename); |
| 1818 | (*rl_filename_stat_hook) (&fn); |
| 1819 | xfree (filename); |
| 1820 | filename = fn; |
| 1821 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1822 | s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0) |
| 1823 | ? LSTAT (filename, &finfo) |
| 1824 | : stat (filename, &finfo); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1825 | if (s == 0 && S_ISDIR (finfo.st_mode)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1826 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1827 | if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1828 | { |
| 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1837 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1838 | #ifdef S_ISLNK |
| 1839 | /* Don't add anything if the filename is a symlink and resolves to a |
| 1840 | directory. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1841 | else if (s == 0 && S_ISLNK (finfo.st_mode) && path_isdir (filename)) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1842 | ; |
| 1843 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1844 | else |
| 1845 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1846 | if (rl_point == rl_end && temp_string_index) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1847 | rl_insert_text (temp_string); |
| 1848 | } |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1849 | xfree (filename); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1850 | } |
| 1851 | else |
| 1852 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1853 | if (rl_point == rl_end && temp_string_index) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1854 | rl_insert_text (temp_string); |
| 1855 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1856 | |
| 1857 | return (temp_string_index); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static void |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1861 | insert_all_matches (matches, point, qc) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1862 | char **matches; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1863 | int point; |
| 1864 | char *qc; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1865 | { |
| 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 Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1872 | if (qc && *qc && point && rl_line_buffer[point - 1] == *qc) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1873 | 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 Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1881 | rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1882 | rl_insert_text (rp); |
| 1883 | rl_insert_text (" "); |
| 1884 | if (rp != matches[i]) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1885 | xfree (rp); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | else |
| 1889 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1890 | rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1891 | rl_insert_text (rp); |
| 1892 | rl_insert_text (" "); |
| 1893 | if (rp != matches[0]) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1894 | xfree (rp); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1895 | } |
| 1896 | rl_end_undo_group (); |
| 1897 | } |
| 1898 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1899 | void |
| 1900 | _rl_free_match_list (matches) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1901 | char **matches; |
| 1902 | { |
| 1903 | register int i; |
| 1904 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1905 | if (matches == 0) |
| 1906 | return; |
| 1907 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1908 | for (i = 0; matches[i]; i++) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1909 | xfree (matches[i]); |
| 1910 | xfree (matches); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1913 | /* 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1919 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1922 | int |
| 1923 | rl_complete_internal (what_to_do) |
| 1924 | int what_to_do; |
| 1925 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1926 | char **matches; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1927 | rl_compentry_func_t *our_func; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1928 | int start, end, delimiter, found_quote, i, nontrivial_lcd; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1929 | char *text, *saved_line_buffer; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1930 | char quote_char; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1931 | #if 1 |
| 1932 | int tlen, mlen; |
| 1933 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1934 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1935 | RL_SETSTATE(RL_STATE_COMPLETING); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1936 | |
| 1937 | set_completion_defaults (what_to_do); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1938 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1939 | 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1942 | : rl_filename_completion_function; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1943 | /* We now look backwards for the start of a filename/variable word. */ |
| 1944 | end = rl_point; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1945 | 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1951 | quote_char = _rl_find_completion_word (&found_quote, &delimiter); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1952 | |
| 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 Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1958 | /* 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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1961 | if (what_to_do == '!' || what_to_do == '@') |
| 1962 | tlen = strlen (text); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1963 | xfree (text); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (matches == 0) |
| 1966 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1967 | rl_ding (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1968 | FREE (saved_line_buffer); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1969 | completion_changed_buffer = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1970 | RL_UNSETSTATE(RL_STATE_COMPLETING); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1971 | _rl_reset_completion_state (); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1972 | return (0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1975 | /* 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1977 | rl_filename_completion_function does this. */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1978 | i = rl_filename_completion_desired; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1979 | |
| 1980 | if (postprocess_matches (&matches, i) == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1981 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1982 | rl_ding (); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1983 | FREE (saved_line_buffer); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1984 | completion_changed_buffer = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1985 | RL_UNSETSTATE(RL_STATE_COMPLETING); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1986 | _rl_reset_completion_state (); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1987 | return (0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1988 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1989 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1990 | switch (what_to_do) |
| 1991 | { |
| 1992 | case TAB: |
| 1993 | case '!': |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1994 | case '@': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1995 | /* Insert the first match with proper quoting. */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1996 | if (what_to_do == TAB) |
| 1997 | { |
| 1998 | if (*matches[0]) |
| 1999 | insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_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, "e_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, "e_char); |
| 2009 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2010 | |
| 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 Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2026 | else if (what_to_do == '@') |
| 2027 | { |
| 2028 | if (nontrivial_lcd == 0) |
| 2029 | display_matches (matches); |
| 2030 | break; |
| 2031 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2032 | else if (rl_editing_mode != vi_mode) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2033 | rl_ding (); /* There are other matches remaining. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2034 | } |
| 2035 | else |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2036 | append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2037 | |
| 2038 | break; |
| 2039 | |
| 2040 | case '*': |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2041 | insert_all_matches (matches, start, "e_char); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2042 | break; |
| 2043 | |
| 2044 | case '?': |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2045 | if (rl_completion_display_matches_hook == 0) |
| 2046 | { |
| 2047 | _rl_sigcleanup = _rl_complete_sigcleanup; |
| 2048 | _rl_sigcleanarg = matches; |
| 2049 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2050 | display_matches (matches); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2051 | _rl_sigcleanup = 0; |
| 2052 | _rl_sigcleanarg = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2053 | break; |
| 2054 | |
| 2055 | default: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2056 | _rl_ttymsg ("bad value %d for what_to_do in rl_complete", what_to_do); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2057 | rl_ding (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2058 | FREE (saved_line_buffer); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2059 | RL_UNSETSTATE(RL_STATE_COMPLETING); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2060 | _rl_free_match_list (matches); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2061 | _rl_reset_completion_state (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2062 | return 1; |
| 2063 | } |
| 2064 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2065 | _rl_free_match_list (matches); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2066 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2067 | /* Check to see if the line has changed through all of this manipulation. */ |
| 2068 | if (saved_line_buffer) |
| 2069 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2070 | completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2071 | xfree (saved_line_buffer); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2072 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2073 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2074 | RL_UNSETSTATE(RL_STATE_COMPLETING); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2075 | _rl_reset_completion_state (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2076 | return 0; |
| 2077 | } |
| 2078 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2079 | /***************************************************************/ |
| 2080 | /* */ |
| 2081 | /* Application-callable completion match generator functions */ |
| 2082 | /* */ |
| 2083 | /***************************************************************/ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2084 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2085 | /* 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 | */ |
| 2097 | char ** |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2098 | rl_completion_matches (text, entry_function) |
| 2099 | const char *text; |
| 2100 | rl_compentry_func_t *entry_function; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2101 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2102 | register int i; |
| 2103 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2104 | /* Number of slots in match_list. */ |
| 2105 | int match_list_size; |
| 2106 | |
| 2107 | /* The list of matches. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2108 | char **match_list; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2109 | |
| 2110 | /* Number of matches actually found. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2111 | int matches; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2112 | |
| 2113 | /* Temporary string binder. */ |
| 2114 | char *string; |
| 2115 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2116 | matches = 0; |
| 2117 | match_list_size = 10; |
| 2118 | match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2119 | match_list[1] = (char *)NULL; |
| 2120 | |
| 2121 | while (string = (*entry_function) (text, matches)) |
| 2122 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2123 | 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2142 | match_list = (char **)xrealloc |
| 2143 | (match_list, ((match_list_size += 10) + 1) * sizeof (char *)); |
| 2144 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2145 | if (match_list == 0) |
| 2146 | return (match_list); |
| 2147 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2148 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2155 | compute_lcd_of_matches (match_list, matches, text); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2156 | else /* There were no matches. */ |
| 2157 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2158 | xfree (match_list); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2159 | match_list = (char **)NULL; |
| 2160 | } |
| 2161 | return (match_list); |
| 2162 | } |
| 2163 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2164 | /* A completion function for usernames. |
| 2165 | TEXT contains a partial username preceded by a random |
| 2166 | character (usually `~'). */ |
| 2167 | char * |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2168 | rl_username_completion_function (text, state) |
| 2169 | const char *text; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2170 | int state; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2171 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2172 | #if defined (__WIN32__) || defined (__OPENNT) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2173 | return (char *)NULL; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2174 | #else /* !__WIN32__ && !__OPENNT) */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2175 | 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2189 | #if defined (HAVE_GETPWENT) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2190 | setpwent (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2191 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2194 | #if defined (HAVE_GETPWENT) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2195 | 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 Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2201 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2202 | |
| 2203 | if (entry == 0) |
| 2204 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2205 | #if defined (HAVE_GETPWENT) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2206 | endpwent (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2207 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2208 | return ((char *)NULL); |
| 2209 | } |
| 2210 | else |
| 2211 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2212 | value = (char *)xmalloc (2 + strlen (entry->pw_name)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2213 | |
| 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 Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2223 | #endif /* !__WIN32__ && !__OPENNT */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2226 | /* 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2228 | 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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2231 | static int |
| 2232 | complete_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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2240 | #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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2259 | |
| 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2265 | #if defined (HANDLE_MULTIBYTE) |
| 2266 | if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2267 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2268 | 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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2294 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2295 | 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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2311 | return 1; |
| 2312 | } |
| 2313 | else if (_rl_completion_case_fold) |
| 2314 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2315 | #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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2344 | 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2359 | /* 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. */ |
| 2363 | char * |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2364 | rl_filename_completion_function (text, state) |
| 2365 | const char *text; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2366 | int state; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2367 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2368 | static DIR *directory = (DIR *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2369 | static char *filename = (char *)NULL; |
| 2370 | static char *dirname = (char *)NULL; |
| 2371 | static char *users_dirname = (char *)NULL; |
| 2372 | static int filename_len; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2373 | char *temp, *dentry, *convfn; |
| 2374 | int dirlen, dentlen, convlen; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2375 | struct dirent *entry; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2376 | |
| 2377 | /* If we don't have any state, then do some initialization. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2378 | if (state == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2379 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2380 | /* 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2387 | FREE (dirname); |
| 2388 | FREE (filename); |
| 2389 | FREE (users_dirname); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2390 | |
| 2391 | filename = savestring (text); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2392 | if (*text == 0) |
| 2393 | text = "."; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2394 | dirname = savestring (text); |
| 2395 | |
| 2396 | temp = strrchr (dirname, '/'); |
| 2397 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2398 | #if defined (__MSDOS__) |
| 2399 | /* special hack for //X/... */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2400 | if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/') |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2401 | temp = strrchr (dirname + 3, '/'); |
| 2402 | #endif |
| 2403 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2404 | if (temp) |
| 2405 | { |
| 2406 | strcpy (filename, ++temp); |
| 2407 | *temp = '\0'; |
| 2408 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2409 | #if defined (__MSDOS__) |
| 2410 | /* searches from current directory on the drive */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2411 | else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':') |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2412 | { |
| 2413 | strcpy (filename, dirname + 2); |
| 2414 | dirname[2] = '\0'; |
| 2415 | } |
| 2416 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2417 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2418 | { |
| 2419 | dirname[0] = '.'; |
| 2420 | dirname[1] = '\0'; |
| 2421 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2422 | |
| 2423 | /* We aren't done yet. We also support the "~user" syntax. */ |
| 2424 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2425 | /* 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2431 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2432 | if (*dirname == '~') |
| 2433 | { |
| 2434 | temp = tilde_expand (dirname); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2435 | xfree (dirname); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2436 | dirname = temp; |
| 2437 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2438 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2439 | /* 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2448 | if (rl_directory_rewrite_hook) |
| 2449 | (*rl_directory_rewrite_hook) (&dirname); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2450 | else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2451 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2452 | xfree (users_dirname); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2453 | users_dirname = savestring (dirname); |
| 2454 | } |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2455 | else if (rl_completion_found_quote && rl_filename_dequoting_function) |
| 2456 | { |
| 2457 | /* delete single and double quotes */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2458 | xfree (dirname); |
| 2459 | dirname = savestring (users_dirname); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2460 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2461 | directory = opendir (dirname); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2462 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2463 | /* 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 Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2466 | { |
| 2467 | /* delete single and double quotes */ |
| 2468 | temp = (*rl_filename_dequoting_function) (filename, rl_completion_quote_character); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2469 | xfree (filename); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2470 | filename = temp; |
| 2471 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2472 | 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2485 | entry = (struct dirent *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2486 | while (directory && (entry = readdir (directory))) |
| 2487 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2488 | 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 Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2497 | /* 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2500 | if (filename_len == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2501 | { |
Chet Ramey | 30d188c | 2011-11-21 20:57:16 -0500 | [diff] [blame] | 2502 | if (_rl_match_hidden_files == 0 && HIDDEN_FILE (convfn)) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2503 | continue; |
| 2504 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2505 | if (convfn[0] != '.' || |
| 2506 | (convfn[1] && (convfn[1] != '.' || convfn[2]))) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2507 | break; |
| 2508 | } |
| 2509 | else |
| 2510 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2511 | if (complete_fncmp (convfn, convlen, filename, filename_len)) |
| 2512 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2513 | } |
| 2514 | } |
| 2515 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2516 | if (entry == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2517 | { |
| 2518 | if (directory) |
| 2519 | { |
| 2520 | closedir (directory); |
| 2521 | directory = (DIR *)NULL; |
| 2522 | } |
| 2523 | if (dirname) |
| 2524 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2525 | xfree (dirname); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2526 | dirname = (char *)NULL; |
| 2527 | } |
| 2528 | if (filename) |
| 2529 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2530 | xfree (filename); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2531 | filename = (char *)NULL; |
| 2532 | } |
| 2533 | if (users_dirname) |
| 2534 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2535 | xfree (users_dirname); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2536 | users_dirname = (char *)NULL; |
| 2537 | } |
| 2538 | |
| 2539 | return (char *)NULL; |
| 2540 | } |
| 2541 | else |
| 2542 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2543 | /* dirname && (strcmp (dirname, ".") != 0) */ |
| 2544 | if (dirname && (dirname[0] != '.' || dirname[1])) |
| 2545 | { |
| 2546 | if (rl_complete_with_tilde_expansion && *users_dirname == '~') |
| 2547 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2548 | dirlen = strlen (dirname); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2549 | temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2550 | strcpy (temp, dirname); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2551 | /* Canonicalization cuts off any final slash present. We |
| 2552 | may need to add it back. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2553 | if (dirname[dirlen - 1] != '/') |
| 2554 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2555 | temp[dirlen++] = '/'; |
| 2556 | temp[dirlen] = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | else |
| 2560 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2561 | dirlen = strlen (users_dirname); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2562 | temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2563 | strcpy (temp, users_dirname); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2564 | /* Make sure that temp has a trailing slash here. */ |
| 2565 | if (users_dirname[dirlen - 1] != '/') |
| 2566 | temp[dirlen++] = '/'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
Chet Ramey | 30d188c | 2011-11-21 20:57:16 -0500 | [diff] [blame] | 2569 | strcpy (temp + dirlen, convfn); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2570 | } |
| 2571 | else |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2572 | temp = savestring (convfn); |
| 2573 | |
| 2574 | if (convfn != dentry) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2575 | xfree (convfn); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2576 | |
| 2577 | return (temp); |
| 2578 | } |
| 2579 | } |
| 2580 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2581 | /* An initial implementation of a menu completion function a la tcsh. The |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2582 | first time (if the last readline command was not rl_old_menu_complete), we |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2583 | 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 Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2587 | second and subsequent consecutive calls to rl_old_menu_complete). When we |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2588 | hit the end of the match list, we restore the original unmatched text, |
| 2589 | ring the bell, and reset the counter to zero. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2590 | int |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2591 | rl_old_menu_complete (count, invoking_key) |
| 2592 | int count, invoking_key; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2593 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2594 | rl_compentry_func_t *our_func; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2595 | int matching_filenames, found_quote; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2596 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2597 | 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 Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2604 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2605 | /* The first time through, we generate the list of matches and set things |
| 2606 | up to insert them. */ |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 2607 | if (rl_last_func != rl_old_menu_complete) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2608 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2609 | /* Clean up from previous call, if any. */ |
| 2610 | FREE (orig_text); |
| 2611 | if (matches) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2612 | _rl_free_match_list (matches); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2613 | |
| 2614 | match_list_index = match_list_size = 0; |
| 2615 | matches = (char **)NULL; |
| 2616 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2617 | rl_completion_invoking_key = invoking_key; |
| 2618 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 2619 | RL_SETSTATE(RL_STATE_COMPLETING); |
| 2620 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2621 | /* Only the completion entry function can change these. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2622 | set_completion_defaults ('%'); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2623 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2624 | our_func = rl_menu_completion_entry_function; |
| 2625 | if (our_func == 0) |
| 2626 | our_func = rl_completion_entry_function |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2627 | ? rl_completion_entry_function |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2628 | : rl_filename_completion_function; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2629 | |
| 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2638 | quote_char = _rl_find_completion_word (&found_quote, &delimiter); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2639 | |
| 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 Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2647 | /* 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2649 | rl_filename_completion_function does this. */ |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2650 | matching_filenames = rl_filename_completion_desired; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2651 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2652 | if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2653 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2654 | rl_ding (); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2655 | FREE (matches); |
| 2656 | matches = (char **)0; |
| 2657 | FREE (orig_text); |
| 2658 | orig_text = (char *)0; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2659 | completion_changed_buffer = 0; |
| 2660 | RL_UNSETSTATE(RL_STATE_COMPLETING); |
| 2661 | return (0); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 2664 | RL_UNSETSTATE(RL_STATE_COMPLETING); |
| 2665 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2666 | 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 Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2670 | |
| 2671 | if (match_list_size > 1 && _rl_complete_show_all) |
| 2672 | display_matches (matches); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2673 | } |
| 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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2681 | rl_ding (); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2682 | FREE (matches); |
| 2683 | matches = (char **)0; |
| 2684 | completion_changed_buffer = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2685 | return (0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2686 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2687 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2688 | match_list_index += count; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2689 | if (match_list_index < 0) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2690 | { |
| 2691 | while (match_list_index < 0) |
| 2692 | match_list_index += match_list_size; |
| 2693 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2694 | else |
| 2695 | match_list_index %= match_list_size; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2696 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2697 | if (match_list_index == 0 && match_list_size > 1) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2698 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2699 | rl_ding (); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2700 | insert_match (orig_text, orig_start, MULT_MATCH, "e_char); |
| 2701 | } |
| 2702 | else |
| 2703 | { |
| 2704 | insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2705 | append_to_match (matches[match_list_index], delimiter, quote_char, |
| 2706 | strcmp (orig_text, matches[match_list_index])); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2709 | completion_changed_buffer = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2710 | return (0); |
| 2711 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2712 | |
| 2713 | int |
| 2714 | rl_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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2728 | static int delimiter, cstate; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2729 | |
| 2730 | /* The first time through, we generate the list of matches and set things |
| 2731 | up to insert them. */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2732 | if ((rl_last_func != rl_menu_complete && rl_last_func != rl_backward_menu_complete) || full_completion) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2733 | { |
| 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 Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 2744 | RL_SETSTATE(RL_STATE_COMPLETING); |
| 2745 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2746 | /* 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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2781 | rl_ding (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2782 | FREE (matches); |
| 2783 | matches = (char **)0; |
| 2784 | FREE (orig_text); |
| 2785 | orig_text = (char *)0; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2786 | completion_changed_buffer = 0; |
| 2787 | RL_UNSETSTATE(RL_STATE_COMPLETING); |
| 2788 | return (0); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 2791 | RL_UNSETSTATE(RL_STATE_COMPLETING); |
| 2792 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2793 | 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, "e_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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2829 | else if (_rl_menu_complete_prefix_first) |
| 2830 | { |
| 2831 | rl_ding (); |
| 2832 | return (0); |
| 2833 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2834 | } |
| 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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2841 | else if (_rl_menu_complete_prefix_first && match_list_size > 1) |
| 2842 | { |
| 2843 | rl_ding (); |
| 2844 | return (0); |
| 2845 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2846 | } |
| 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 Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 2863 | { |
| 2864 | while (match_list_index < 0) |
| 2865 | match_list_index += match_list_size; |
| 2866 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2867 | 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, "e_char); |
| 2874 | } |
| 2875 | else |
| 2876 | { |
| 2877 | insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_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 Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 2885 | |
| 2886 | int |
| 2887 | rl_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 | } |