| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* bashline.c -- Bash's interface to the readline library. */ |
| 2 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1987-2017 Free Software Foundation, Inc. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
| 23 | #if defined (READLINE) |
| 24 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 25 | #include "bashtypes.h" |
| 26 | #include "posixstat.h" |
| 27 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 28 | #if defined (HAVE_UNISTD_H) |
| 29 | # include <unistd.h> |
| 30 | #endif |
| 31 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 32 | #if defined (HAVE_GRP_H) |
| 33 | # include <grp.h> |
| 34 | #endif |
| 35 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 36 | #if defined (HAVE_NETDB_H) |
| 37 | # include <netdb.h> |
| 38 | #endif |
| 39 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 40 | #include <signal.h> |
| 41 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 42 | #include <stdio.h> |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 43 | #include "chartypes.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 44 | #include "bashansi.h" |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 45 | #include "bashintl.h" |
| 46 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 47 | #include "shell.h" |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 48 | #include "input.h" |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 49 | #include "parser.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | #include "builtins.h" |
| 51 | #include "bashhist.h" |
| 52 | #include "bashline.h" |
| 53 | #include "execute_cmd.h" |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 54 | #include "findcmd.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 55 | #include "pathexp.h" |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 56 | #include "shmbutil.h" |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 57 | #include "trap.h" |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 58 | #include "flags.h" |
| 59 | |
| 60 | #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR) |
| 61 | # include <mbstr.h> /* mbschr */ |
| 62 | #endif |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 63 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 64 | #include "builtins/common.h" |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 65 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 66 | #include <readline/rlconf.h> |
| 67 | #include <readline/readline.h> |
| 68 | #include <readline/history.h> |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 69 | #include <readline/rlmbutil.h> |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | |
| 71 | #include <glob/glob.h> |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 72 | |
| 73 | #if defined (ALIAS) |
| 74 | # include "alias.h" |
| 75 | #endif |
| 76 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 77 | #if defined (PROGRAMMABLE_COMPLETION) |
| 78 | # include "pcomplete.h" |
| 79 | #endif |
| 80 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 81 | /* These should agree with the defines for emacs_mode and vi_mode in |
| 82 | rldefs.h, even though that's not a public readline header file. */ |
| 83 | #ifndef EMACS_EDITING_MODE |
| 84 | # define NO_EDITING_MODE -1 |
| 85 | # define EMACS_EDITING_MODE 1 |
| 86 | # define VI_EDITING_MODE 0 |
| 87 | #endif |
| 88 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 89 | #define RL_BOOLEAN_VARIABLE_VALUE(s) ((s)[0] == 'o' && (s)[1] == 'n' && (s)[2] == '\0') |
| 90 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 91 | #if defined (BRACE_COMPLETION) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 92 | extern int bash_brace_completion __P((int, int)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 93 | #endif /* BRACE_COMPLETION */ |
| 94 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 95 | /* To avoid including curses.h/term.h/termcap.h and that whole mess. */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 96 | #ifdef _MINIX |
| 97 | extern int tputs __P((const char *string, int nlines, void (*outx)(int))); |
| 98 | #else |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 99 | extern int tputs __P((const char *string, int nlines, int (*outx)(int))); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 100 | #endif |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 101 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 102 | /* Forward declarations */ |
| 103 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 104 | /* Functions bound to keys in Readline for Bash users. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 105 | static int shell_expand_line __P((int, int)); |
| 106 | static int display_shell_version __P((int, int)); |
| 107 | static int operate_and_get_next __P((int, int)); |
| 108 | |
| 109 | static int bash_ignore_filenames __P((char **)); |
| 110 | static int bash_ignore_everything __P((char **)); |
| 111 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 112 | #if defined (BANG_HISTORY) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 113 | static char *history_expand_line_internal __P((char *)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 114 | static int history_expand_line __P((int, int)); |
| 115 | static int tcsh_magic_space __P((int, int)); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 116 | #endif /* BANG_HISTORY */ |
| 117 | #ifdef ALIAS |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 118 | static int alias_expand_line __P((int, int)); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 119 | #endif |
| 120 | #if defined (BANG_HISTORY) && defined (ALIAS) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 121 | static int history_and_alias_expand_line __P((int, int)); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 124 | static int bash_forward_shellword __P((int, int)); |
| 125 | static int bash_backward_shellword __P((int, int)); |
| 126 | static int bash_kill_shellword __P((int, int)); |
| 127 | static int bash_backward_kill_shellword __P((int, int)); |
| 128 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 129 | /* Helper functions for Readline. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 130 | static char *restore_tilde __P((char *, char *)); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 131 | static char *maybe_restore_tilde __P((char *, char *)); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 132 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 133 | static char *bash_filename_rewrite_hook __P((char *, int)); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 134 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 135 | static void bash_directory_expansion __P((char **)); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 136 | static int bash_filename_stat_hook __P((char **)); |
| 137 | static int bash_command_name_stat_hook __P((char **)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 138 | static int bash_directory_completion_hook __P((char **)); |
| 139 | static int filename_completion_ignore __P((char **)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 140 | static int bash_push_line __P((void)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 141 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 142 | static int executable_completion __P((const char *, int)); |
| 143 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 144 | static rl_icppfunc_t *save_directory_hook __P((void)); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 145 | static void restore_directory_hook __P((rl_icppfunc_t)); |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 146 | |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 147 | static int directory_exists __P((const char *, int)); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 148 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 149 | static void cleanup_expansion_error __P((void)); |
| 150 | static void maybe_make_readline_line __P((char *)); |
| 151 | static void set_up_new_line __P((char *)); |
| 152 | |
| 153 | static int check_redir __P((int)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 154 | static char **attempt_shell_completion __P((const char *, int, int)); |
| 155 | static char *variable_completion_function __P((const char *, int)); |
| 156 | static char *hostname_completion_function __P((const char *, int)); |
| 157 | static char *command_subst_completion_function __P((const char *, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 158 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 159 | static void build_history_completion_array __P((void)); |
| 160 | static char *history_completion_generator __P((const char *, int)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 161 | static int dynamic_complete_history __P((int, int)); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 162 | static int bash_dabbrev_expand __P((int, int)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 163 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 164 | static void initialize_hostname_list __P((void)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 165 | static void add_host_name __P((char *)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 166 | static void snarf_hosts_from_file __P((char *)); |
| 167 | static char **hostnames_matching __P((char *)); |
| 168 | |
| 169 | static void _ignore_completion_names __P((char **, sh_ignore_func_t *)); |
| 170 | static int name_is_acceptable __P((const char *)); |
| 171 | static int test_for_directory __P((const char *)); |
| 172 | static int return_zero __P((const char *)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 173 | |
| 174 | static char *bash_dequote_filename __P((char *, int)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 175 | static char *quote_word_break_chars __P((char *)); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 176 | static void set_filename_bstab __P((const char *)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 177 | static char *bash_quote_filename __P((char *, int, char *)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 178 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 179 | #ifdef _MINIX |
| 180 | static void putx __P((int)); |
| 181 | #else |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 182 | static int putx __P((int)); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 183 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 184 | static int bash_execute_unix_command __P((int, int)); |
| 185 | static void init_unix_command_map __P((void)); |
| 186 | static int isolate_sequence __P((char *, int, int, int *)); |
| 187 | |
| 188 | static int set_saved_history __P((void)); |
| 189 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 190 | #if defined (ALIAS) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 191 | static int posix_edit_macros __P((int, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 192 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 193 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 194 | static int bash_event_hook __P((void)); |
| 195 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 196 | #if defined (PROGRAMMABLE_COMPLETION) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 197 | static int find_cmd_start __P((int)); |
| 198 | static int find_cmd_end __P((int)); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 199 | static char *find_cmd_name __P((int, int *, int *)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 200 | static char *prog_complete_return __P((const char *, int)); |
| 201 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 202 | static char **prog_complete_matches; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 203 | #endif |
| 204 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 205 | extern int no_symbolic_links; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 206 | extern STRING_INT_ALIST word_token_alist[]; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 207 | |
| 208 | /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual |
| 209 | completion functions which indicate what type of completion should be |
| 210 | done (at or before point) that can be bound to key sequences with |
| 211 | the readline library. */ |
| 212 | #define SPECIFIC_COMPLETION_FUNCTIONS |
| 213 | |
| 214 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 215 | static int bash_specific_completion __P((int, rl_compentry_func_t *)); |
| 216 | |
| 217 | static int bash_complete_filename_internal __P((int)); |
| 218 | static int bash_complete_username_internal __P((int)); |
| 219 | static int bash_complete_hostname_internal __P((int)); |
| 220 | static int bash_complete_variable_internal __P((int)); |
| 221 | static int bash_complete_command_internal __P((int)); |
| 222 | |
| 223 | static int bash_complete_filename __P((int, int)); |
| 224 | static int bash_possible_filename_completions __P((int, int)); |
| 225 | static int bash_complete_username __P((int, int)); |
| 226 | static int bash_possible_username_completions __P((int, int)); |
| 227 | static int bash_complete_hostname __P((int, int)); |
| 228 | static int bash_possible_hostname_completions __P((int, int)); |
| 229 | static int bash_complete_variable __P((int, int)); |
| 230 | static int bash_possible_variable_completions __P((int, int)); |
| 231 | static int bash_complete_command __P((int, int)); |
| 232 | static int bash_possible_command_completions __P((int, int)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 233 | |
| Chet Ramey | 4d2e315 | 2019-01-18 15:12:37 -0500 | [diff] [blame^] | 234 | static int completion_glob_pattern __P((char *)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 235 | static char *glob_complete_word __P((const char *, int)); |
| 236 | static int bash_glob_completion_internal __P((int)); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 237 | static int bash_glob_complete_word __P((int, int)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 238 | static int bash_glob_expand_word __P((int, int)); |
| 239 | static int bash_glob_list_expansions __P((int, int)); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 240 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 241 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ |
| 242 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 243 | static int edit_and_execute_command __P((int, int, int, char *)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 244 | #if defined (VI_MODE) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 245 | static int vi_edit_and_execute_command __P((int, int)); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 246 | static int bash_vi_complete __P((int, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 247 | #endif |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 248 | static int emacs_edit_and_execute_command __P((int, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 249 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 250 | /* Non-zero once initalize_readline () has been called. */ |
| 251 | int bash_readline_initialized = 0; |
| 252 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 253 | /* If non-zero, we do hostname completion, breaking words at `@' and |
| 254 | trying to complete the stuff after the `@' from our own internal |
| 255 | host list. */ |
| 256 | int perform_hostname_completion = 1; |
| 257 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 258 | /* If non-zero, we don't do command completion on an empty line. */ |
| 259 | int no_empty_command_completion; |
| 260 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 261 | /* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the |
| 262 | only possible matches. Set to 0 if you want to match filenames if they |
| 263 | are the only possible matches, even if FIGNORE says to. */ |
| 264 | int force_fignore = 1; |
| 265 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 266 | /* Perform spelling correction on directory names during word completion */ |
| 267 | int dircomplete_spelling = 0; |
| 268 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 269 | /* Expand directory names during word/filename completion. */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 270 | #if DIRCOMPLETE_EXPAND_DEFAULT |
| 271 | int dircomplete_expand = 1; |
| 272 | int dircomplete_expand_relpath = 1; |
| 273 | #else |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 274 | int dircomplete_expand = 0; |
| 275 | int dircomplete_expand_relpath = 0; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 276 | #endif |
| 277 | |
| 278 | /* When non-zero, perform `normal' shell quoting on completed filenames |
| 279 | even when the completed name contains a directory name with a shell |
| 280 | variable referene, so dollar signs in a filename get quoted appropriately. |
| 281 | Set to zero to remove dollar sign (and braces or parens as needed) from |
| 282 | the set of characters that will be quoted. */ |
| 283 | int complete_fullquote = 1; |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 284 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 285 | static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:"; |
| 286 | static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:"; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 287 | /* )) */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 288 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 289 | static const char *default_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{~"; /*}*/ |
| 290 | static char *custom_filename_quote_characters = 0; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 291 | static char filename_bstab[256]; |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 292 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 293 | static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 294 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 295 | static int dot_in_path = 0; |
| 296 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 297 | /* Set to non-zero when dabbrev-expand is running */ |
| 298 | static int dabbrev_expand_active = 0; |
| 299 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 300 | /* What kind of quoting is performed by bash_quote_filename: |
| 301 | COMPLETE_DQUOTE = double-quoting the filename |
| 302 | COMPLETE_SQUOTE = single_quoting the filename |
| 303 | COMPLETE_BSQUOTE = backslash-quoting special chars in the filename |
| 304 | */ |
| 305 | #define COMPLETE_DQUOTE 1 |
| 306 | #define COMPLETE_SQUOTE 2 |
| 307 | #define COMPLETE_BSQUOTE 3 |
| 308 | static int completion_quoting_style = COMPLETE_BSQUOTE; |
| 309 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 310 | /* Flag values for the final argument to bash_default_completion */ |
| 311 | #define DEFCOMP_CMDPOS 1 |
| 312 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 313 | /* Change the readline VI-mode keymaps into or out of Posix.2 compliance. |
| 314 | Called when the shell is put into or out of `posix' mode. */ |
| 315 | void |
| 316 | posix_readline_initialize (on_or_off) |
| 317 | int on_or_off; |
| 318 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 319 | if (on_or_off) |
| 320 | rl_variable_bind ("comment-begin", "#"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 321 | #if defined (VI_MODE) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 322 | rl_bind_key_in_map (CTRL ('I'), on_or_off ? rl_insert : rl_complete, vi_insertion_keymap); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 323 | #endif |
| 324 | } |
| 325 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 326 | void |
| 327 | reset_completer_word_break_chars () |
| 328 | { |
| 329 | rl_completer_word_break_characters = perform_hostname_completion ? savestring (bash_completer_word_break_characters) : savestring (bash_nohostname_word_break_characters); |
| 330 | } |
| 331 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 332 | /* When this function returns, rl_completer_word_break_characters points to |
| 333 | dynamically allocated memory. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 334 | int |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 335 | enable_hostname_completion (on_or_off) |
| 336 | int on_or_off; |
| 337 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 338 | int old_value; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 339 | char *at, *nv, *nval; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 340 | |
| 341 | old_value = perform_hostname_completion; |
| 342 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 343 | if (on_or_off) |
| 344 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 345 | perform_hostname_completion = 1; |
| 346 | rl_special_prefixes = "$@"; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 347 | } |
| 348 | else |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 349 | { |
| 350 | perform_hostname_completion = 0; |
| 351 | rl_special_prefixes = "$"; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* Now we need to figure out how to appropriately modify and assign |
| 355 | rl_completer_word_break_characters depending on whether we want |
| 356 | hostname completion on or off. */ |
| 357 | |
| 358 | /* If this is the first time this has been called |
| 359 | (bash_readline_initialized == 0), use the sames values as before, but |
| 360 | allocate new memory for rl_completer_word_break_characters. */ |
| 361 | |
| 362 | if (bash_readline_initialized == 0 && |
| 363 | (rl_completer_word_break_characters == 0 || |
| 364 | rl_completer_word_break_characters == rl_basic_word_break_characters)) |
| 365 | { |
| 366 | if (on_or_off) |
| 367 | rl_completer_word_break_characters = savestring (bash_completer_word_break_characters); |
| 368 | else |
| 369 | rl_completer_word_break_characters = savestring (bash_nohostname_word_break_characters); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | /* See if we have anything to do. */ |
| 374 | at = strchr (rl_completer_word_break_characters, '@'); |
| 375 | if ((at == 0 && on_or_off == 0) || (at != 0 && on_or_off != 0)) |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 376 | return old_value; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 377 | |
| 378 | /* We have something to do. Do it. */ |
| 379 | nval = (char *)xmalloc (strlen (rl_completer_word_break_characters) + 1 + on_or_off); |
| 380 | |
| 381 | if (on_or_off == 0) |
| 382 | { |
| 383 | /* Turn it off -- just remove `@' from word break chars. We want |
| 384 | to remove all occurrences of `@' from the char list, so we loop |
| 385 | rather than just copy the rest of the list over AT. */ |
| 386 | for (nv = nval, at = rl_completer_word_break_characters; *at; ) |
| 387 | if (*at != '@') |
| 388 | *nv++ = *at++; |
| 389 | else |
| 390 | at++; |
| 391 | *nv = '\0'; |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | nval[0] = '@'; |
| 396 | strcpy (nval + 1, rl_completer_word_break_characters); |
| 397 | } |
| 398 | |
| 399 | free (rl_completer_word_break_characters); |
| 400 | rl_completer_word_break_characters = nval; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 401 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 402 | |
| 403 | return (old_value); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 404 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 405 | |
| 406 | /* Called once from parse.y if we are going to use readline. */ |
| 407 | void |
| 408 | initialize_readline () |
| 409 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 410 | rl_command_func_t *func; |
| 411 | char kseq[2]; |
| 412 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 413 | if (bash_readline_initialized) |
| 414 | return; |
| 415 | |
| 416 | rl_terminal_name = get_string_value ("TERM"); |
| 417 | rl_instream = stdin; |
| 418 | rl_outstream = stderr; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 419 | |
| 420 | /* Allow conditional parsing of the ~/.inputrc file. */ |
| 421 | rl_readline_name = "Bash"; |
| 422 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 423 | /* Add bindable names before calling rl_initialize so they may be |
| 424 | referenced in the various inputrc files. */ |
| 425 | rl_add_defun ("shell-expand-line", shell_expand_line, -1); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 426 | #ifdef BANG_HISTORY |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 427 | rl_add_defun ("history-expand-line", history_expand_line, -1); |
| 428 | rl_add_defun ("magic-space", tcsh_magic_space, -1); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 429 | #endif |
| 430 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 431 | rl_add_defun ("shell-forward-word", bash_forward_shellword, -1); |
| 432 | rl_add_defun ("shell-backward-word", bash_backward_shellword, -1); |
| 433 | rl_add_defun ("shell-kill-word", bash_kill_shellword, -1); |
| 434 | rl_add_defun ("shell-backward-kill-word", bash_backward_kill_shellword, -1); |
| 435 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 436 | #ifdef ALIAS |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 437 | rl_add_defun ("alias-expand-line", alias_expand_line, -1); |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 438 | # ifdef BANG_HISTORY |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 439 | rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line, -1); |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 440 | # endif |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 441 | #endif |
| 442 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 443 | /* Backwards compatibility. */ |
| 444 | rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1); |
| 445 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 446 | rl_add_defun ("operate-and-get-next", operate_and_get_next, -1); |
| 447 | rl_add_defun ("display-shell-version", display_shell_version, -1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 448 | rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command, -1); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 449 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 450 | #if defined (BRACE_COMPLETION) |
| 451 | rl_add_defun ("complete-into-braces", bash_brace_completion, -1); |
| 452 | #endif |
| 453 | |
| 454 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| 455 | rl_add_defun ("complete-filename", bash_complete_filename, -1); |
| 456 | rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1); |
| 457 | rl_add_defun ("complete-username", bash_complete_username, -1); |
| 458 | rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1); |
| 459 | rl_add_defun ("complete-hostname", bash_complete_hostname, -1); |
| 460 | rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1); |
| 461 | rl_add_defun ("complete-variable", bash_complete_variable, -1); |
| 462 | rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1); |
| 463 | rl_add_defun ("complete-command", bash_complete_command, -1); |
| 464 | rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 465 | rl_add_defun ("glob-complete-word", bash_glob_complete_word, -1); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 466 | rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1); |
| 467 | rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1); |
| 468 | #endif |
| 469 | |
| 470 | rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 471 | rl_add_defun ("dabbrev-expand", bash_dabbrev_expand, -1); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 472 | |
| 473 | /* Bind defaults before binding our custom shell keybindings. */ |
| 474 | if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0) |
| 475 | rl_initialize (); |
| 476 | |
| 477 | /* Bind up our special shell functions. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 478 | rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line, emacs_meta_keymap); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 479 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 480 | #ifdef BANG_HISTORY |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 481 | rl_bind_key_if_unbound_in_map ('^', history_expand_line, emacs_meta_keymap); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 482 | #endif |
| 483 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 484 | rl_bind_key_if_unbound_in_map (CTRL ('O'), operate_and_get_next, emacs_standard_keymap); |
| 485 | rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version, emacs_ctlx_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 486 | |
| 487 | /* In Bash, the user can switch editing modes with "set -o [vi emacs]", |
| 488 | so it is not necessary to allow C-M-j for context switching. Turn |
| 489 | off this occasionally confusing behaviour. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 490 | kseq[0] = CTRL('J'); |
| 491 | kseq[1] = '\0'; |
| 492 | func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL); |
| 493 | if (func == rl_vi_editing_mode) |
| 494 | rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap); |
| 495 | kseq[0] = CTRL('M'); |
| 496 | func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL); |
| 497 | if (func == rl_vi_editing_mode) |
| 498 | rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 499 | #if defined (VI_MODE) |
| 500 | rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap); |
| 501 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 502 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 503 | #if defined (BRACE_COMPLETION) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 504 | rl_bind_key_if_unbound_in_map ('{', bash_brace_completion, emacs_meta_keymap); /*}*/ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 505 | #endif /* BRACE_COMPLETION */ |
| 506 | |
| 507 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 508 | rl_bind_key_if_unbound_in_map ('/', bash_complete_filename, emacs_meta_keymap); |
| 509 | rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions, emacs_ctlx_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 510 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 511 | /* Have to jump through hoops here because there is a default binding for |
| 512 | M-~ (rl_tilde_expand) */ |
| 513 | kseq[0] = '~'; |
| 514 | kseq[1] = '\0'; |
| 515 | func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL); |
| 516 | if (func == 0 || func == rl_tilde_expand) |
| 517 | rl_bind_keyseq_in_map (kseq, bash_complete_username, emacs_meta_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 518 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 519 | rl_bind_key_if_unbound_in_map ('~', bash_possible_username_completions, emacs_ctlx_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 520 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 521 | rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname, emacs_meta_keymap); |
| 522 | rl_bind_key_if_unbound_in_map ('@', bash_possible_hostname_completions, emacs_ctlx_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 523 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 524 | rl_bind_key_if_unbound_in_map ('$', bash_complete_variable, emacs_meta_keymap); |
| 525 | rl_bind_key_if_unbound_in_map ('$', bash_possible_variable_completions, emacs_ctlx_keymap); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 526 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 527 | rl_bind_key_if_unbound_in_map ('!', bash_complete_command, emacs_meta_keymap); |
| 528 | rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions, emacs_ctlx_keymap); |
| 529 | |
| 530 | rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word, emacs_meta_keymap); |
| 531 | rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word, emacs_ctlx_keymap); |
| 532 | rl_bind_key_if_unbound_in_map ('g', bash_glob_list_expansions, emacs_ctlx_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 533 | |
| 534 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ |
| 535 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 536 | kseq[0] = TAB; |
| 537 | kseq[1] = '\0'; |
| 538 | func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL); |
| 539 | if (func == 0 || func == rl_tab_insert) |
| 540 | rl_bind_key_in_map (TAB, dynamic_complete_history, emacs_meta_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 541 | |
| 542 | /* Tell the completer that we want a crack first. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 543 | rl_attempted_completion_function = attempt_shell_completion; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 544 | |
| 545 | /* Tell the completer that we might want to follow symbolic links or |
| 546 | do other expansion on directory names. */ |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 547 | set_directory_hook (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 548 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 549 | rl_filename_rewrite_hook = bash_filename_rewrite_hook; |
| 550 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 551 | rl_filename_stat_hook = bash_filename_stat_hook; |
| 552 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 553 | /* Tell the filename completer we want a chance to ignore some names. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 554 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 555 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 556 | /* Bind C-xC-e to invoke emacs and run result as commands. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 557 | rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command, emacs_ctlx_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 558 | #if defined (VI_MODE) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 559 | rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command, vi_movement_keymap); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 560 | # if defined (ALIAS) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 561 | rl_bind_key_if_unbound_in_map ('@', posix_edit_macros, vi_movement_keymap); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 562 | # endif |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 563 | |
| 564 | rl_bind_key_in_map ('\\', bash_vi_complete, vi_movement_keymap); |
| 565 | rl_bind_key_in_map ('*', bash_vi_complete, vi_movement_keymap); |
| 566 | rl_bind_key_in_map ('=', bash_vi_complete, vi_movement_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 567 | #endif |
| 568 | |
| 569 | rl_completer_quote_characters = "'\""; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 570 | |
| 571 | /* This sets rl_completer_word_break_characters and rl_special_prefixes |
| 572 | to the appropriate values, depending on whether or not hostname |
| 573 | completion is enabled. */ |
| 574 | enable_hostname_completion (perform_hostname_completion); |
| 575 | |
| 576 | /* characters that need to be quoted when appearing in filenames. */ |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 577 | rl_filename_quote_characters = default_filename_quote_characters; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 578 | set_filename_bstab (rl_filename_quote_characters); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 579 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 580 | rl_filename_quoting_function = bash_quote_filename; |
| 581 | rl_filename_dequoting_function = bash_dequote_filename; |
| 582 | rl_char_is_quoted_p = char_is_quoted; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 583 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 584 | #if 0 |
| 585 | /* This is superfluous and makes it impossible to use tab completion in |
| 586 | vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix() |
| 587 | should already have called posix_readline_initialize() when |
| 588 | posixly_correct was set. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 589 | if (posixly_correct) |
| 590 | posix_readline_initialize (1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 591 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 592 | |
| 593 | bash_readline_initialized = 1; |
| 594 | } |
| 595 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 596 | void |
| 597 | bashline_reinitialize () |
| 598 | { |
| 599 | bash_readline_initialized = 0; |
| 600 | } |
| 601 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 602 | void |
| 603 | bashline_set_event_hook () |
| 604 | { |
| 605 | rl_signal_event_hook = bash_event_hook; |
| 606 | } |
| 607 | |
| 608 | void |
| 609 | bashline_reset_event_hook () |
| 610 | { |
| 611 | rl_signal_event_hook = 0; |
| 612 | } |
| 613 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 614 | /* On Sun systems at least, rl_attempted_completion_function can end up |
| 615 | getting set to NULL, and rl_completion_entry_function set to do command |
| 616 | word completion if Bash is interrupted while trying to complete a command |
| 617 | word. This just resets all the completion functions to the right thing. |
| 618 | It's called from throw_to_top_level(). */ |
| 619 | void |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 620 | bashline_reset () |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 621 | { |
| 622 | tilde_initialize (); |
| 623 | rl_attempted_completion_function = attempt_shell_completion; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 624 | rl_completion_entry_function = NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 625 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 626 | rl_filename_quote_characters = default_filename_quote_characters; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 627 | set_filename_bstab (rl_filename_quote_characters); |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 628 | |
| 629 | set_directory_hook (); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 630 | rl_filename_stat_hook = bash_filename_stat_hook; |
| 631 | |
| 632 | bashline_reset_event_hook (); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 633 | |
| 634 | rl_sort_completion_matches = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* Contains the line to push into readline. */ |
| 638 | static char *push_to_readline = (char *)NULL; |
| 639 | |
| 640 | /* Push the contents of push_to_readline into the |
| 641 | readline buffer. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 642 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 643 | bash_push_line () |
| 644 | { |
| 645 | if (push_to_readline) |
| 646 | { |
| 647 | rl_insert_text (push_to_readline); |
| 648 | free (push_to_readline); |
| 649 | push_to_readline = (char *)NULL; |
| 650 | rl_startup_hook = old_rl_startup_hook; |
| 651 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 652 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /* Call this to set the initial text for the next line to read |
| 656 | from readline. */ |
| 657 | int |
| 658 | bash_re_edit (line) |
| 659 | char *line; |
| 660 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 661 | FREE (push_to_readline); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 662 | |
| 663 | push_to_readline = savestring (line); |
| 664 | old_rl_startup_hook = rl_startup_hook; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 665 | rl_startup_hook = bash_push_line; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 666 | |
| 667 | return (0); |
| 668 | } |
| 669 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 670 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 671 | display_shell_version (count, c) |
| 672 | int count, c; |
| 673 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 674 | rl_crlf (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 675 | show_shell_version (0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 676 | putc ('\r', rl_outstream); |
| 677 | fflush (rl_outstream); |
| 678 | rl_on_new_line (); |
| 679 | rl_redisplay (); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 680 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* **************************************************************** */ |
| 684 | /* */ |
| 685 | /* Readline Stuff */ |
| 686 | /* */ |
| 687 | /* **************************************************************** */ |
| 688 | |
| 689 | /* If the user requests hostname completion, then simply build a list |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 690 | of hosts, and complete from that forever more, or at least until |
| 691 | HOSTFILE is unset. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 692 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 693 | /* THIS SHOULD BE A STRINGLIST. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 694 | /* The kept list of hostnames. */ |
| 695 | static char **hostname_list = (char **)NULL; |
| 696 | |
| 697 | /* The physical size of the above list. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 698 | static int hostname_list_size; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 699 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 700 | /* The number of hostnames in the above list. */ |
| 701 | static int hostname_list_length; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 702 | |
| 703 | /* Whether or not HOSTNAME_LIST has been initialized. */ |
| 704 | int hostname_list_initialized = 0; |
| 705 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 706 | /* Initialize the hostname completion table. */ |
| 707 | static void |
| 708 | initialize_hostname_list () |
| 709 | { |
| 710 | char *temp; |
| 711 | |
| 712 | temp = get_string_value ("HOSTFILE"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 713 | if (temp == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 714 | temp = get_string_value ("hostname_completion_file"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 715 | if (temp == 0) |
| 716 | temp = DEFAULT_HOSTS_FILE; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 717 | |
| 718 | snarf_hosts_from_file (temp); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 719 | |
| 720 | if (hostname_list) |
| 721 | hostname_list_initialized++; |
| 722 | } |
| 723 | |
| 724 | /* Add NAME to the list of hosts. */ |
| 725 | static void |
| 726 | add_host_name (name) |
| 727 | char *name; |
| 728 | { |
| 729 | if (hostname_list_length + 2 > hostname_list_size) |
| 730 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 731 | hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 732 | hostname_list = strvec_resize (hostname_list, hostname_list_size); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 735 | hostname_list[hostname_list_length++] = savestring (name); |
| 736 | hostname_list[hostname_list_length] = (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c)) |
| 740 | |
| 741 | static void |
| 742 | snarf_hosts_from_file (filename) |
| 743 | char *filename; |
| 744 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 745 | FILE *file; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 746 | char *temp, buffer[256], name[256]; |
| 747 | register int i, start; |
| 748 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 749 | file = fopen (filename, "r"); |
| 750 | if (file == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 751 | return; |
| 752 | |
| 753 | while (temp = fgets (buffer, 255, file)) |
| 754 | { |
| 755 | /* Skip to first character. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 756 | for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++) |
| 757 | ; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 758 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 759 | /* If comment or blank line, ignore. */ |
| 760 | if (buffer[i] == '\0' || buffer[i] == '#') |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 761 | continue; |
| 762 | |
| 763 | /* If `preprocessor' directive, do the include. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 764 | if (strncmp (buffer + i, "$include ", 9) == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 765 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 766 | char *incfile, *t; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 767 | |
| 768 | /* Find start of filename. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 769 | for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++) |
| 770 | ; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 771 | |
| 772 | /* Find end of filename. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 773 | for (t = incfile; *t && cr_whitespace (*t) == 0; t++) |
| 774 | ; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 775 | |
| 776 | *t = '\0'; |
| 777 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 778 | snarf_hosts_from_file (incfile); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 779 | continue; |
| 780 | } |
| 781 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 782 | /* Skip internet address if present. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 783 | if (DIGIT (buffer[i])) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 784 | for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 785 | |
| 786 | /* Gobble up names. Each name is separated with whitespace. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 787 | while (buffer[i]) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 788 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 789 | for (; cr_whitespace (buffer[i]); i++) |
| 790 | ; |
| 791 | if (buffer[i] == '\0' || buffer[i] == '#') |
| 792 | break; |
| 793 | |
| 794 | /* Isolate the current word. */ |
| 795 | for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++) |
| 796 | ; |
| 797 | if (i == start) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 798 | continue; |
| 799 | strncpy (name, buffer + start, i - start); |
| 800 | name[i - start] = '\0'; |
| 801 | add_host_name (name); |
| 802 | } |
| 803 | } |
| 804 | fclose (file); |
| 805 | } |
| 806 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 807 | /* Return the hostname list. */ |
| 808 | char ** |
| 809 | get_hostname_list () |
| 810 | { |
| 811 | if (hostname_list_initialized == 0) |
| 812 | initialize_hostname_list (); |
| 813 | return (hostname_list); |
| 814 | } |
| 815 | |
| 816 | void |
| 817 | clear_hostname_list () |
| 818 | { |
| 819 | register int i; |
| 820 | |
| 821 | if (hostname_list_initialized == 0) |
| 822 | return; |
| 823 | for (i = 0; i < hostname_list_length; i++) |
| 824 | free (hostname_list[i]); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 825 | hostname_list_length = hostname_list_initialized = 0; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 828 | /* Return a NULL terminated list of hostnames which begin with TEXT. |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 829 | Initialize the hostname list the first time if necessary. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 830 | The array is malloc ()'ed, but not the individual strings. */ |
| 831 | static char ** |
| 832 | hostnames_matching (text) |
| 833 | char *text; |
| 834 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 835 | register int i, len, nmatch, rsize; |
| 836 | char **result; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 837 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 838 | if (hostname_list_initialized == 0) |
| 839 | initialize_hostname_list (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 840 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 841 | if (hostname_list_initialized == 0) |
| 842 | return ((char **)NULL); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 843 | |
| 844 | /* Special case. If TEXT consists of nothing, then the whole list is |
| 845 | what is desired. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 846 | if (*text == '\0') |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 847 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 848 | result = strvec_create (1 + hostname_list_length); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 849 | for (i = 0; i < hostname_list_length; i++) |
| 850 | result[i] = hostname_list[i]; |
| 851 | result[i] = (char *)NULL; |
| 852 | return (result); |
| 853 | } |
| 854 | |
| 855 | /* Scan until found, or failure. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 856 | len = strlen (text); |
| 857 | result = (char **)NULL; |
| 858 | for (i = nmatch = rsize = 0; i < hostname_list_length; i++) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 859 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 860 | if (STREQN (text, hostname_list[i], len) == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 861 | continue; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 862 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 863 | /* OK, it matches. Add it to the list. */ |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 864 | if (nmatch >= (rsize - 1)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 865 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 866 | rsize = (rsize + 16) - (rsize % 16); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 867 | result = strvec_resize (result, rsize); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 870 | result[nmatch++] = hostname_list[i]; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 871 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 872 | if (nmatch) |
| 873 | result[nmatch] = (char *)NULL; |
| 874 | return (result); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 877 | /* The equivalent of the Korn shell C-o operate-and-get-next-history-line |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 878 | editing command. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 879 | static int saved_history_line_to_use = -1; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 880 | static int last_saved_history_line = -1; |
| 881 | |
| 882 | #define HISTORY_FULL() (history_is_stifled () && history_length >= history_max_entries) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 883 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 884 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 885 | set_saved_history () |
| 886 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 887 | /* XXX - compensate for assumption that history was `shuffled' if it was |
| 888 | actually not. */ |
| 889 | if (HISTORY_FULL () && |
| 890 | hist_last_line_added == 0 && |
| 891 | saved_history_line_to_use < history_length - 1) |
| 892 | saved_history_line_to_use++; |
| 893 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 894 | if (saved_history_line_to_use >= 0) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 895 | { |
| 896 | rl_get_previous_history (history_length - saved_history_line_to_use, 0); |
| 897 | last_saved_history_line = saved_history_line_to_use; |
| 898 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 899 | saved_history_line_to_use = -1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 900 | rl_startup_hook = old_rl_startup_hook; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 901 | return (0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 902 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 903 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 904 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 905 | operate_and_get_next (count, c) |
| 906 | int count, c; |
| 907 | { |
| 908 | int where; |
| 909 | |
| 910 | /* Accept the current line. */ |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 911 | rl_newline (1, c); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 912 | |
| 913 | /* Find the current line, and find the next line to use. */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 914 | where = rl_explicit_arg ? count : where_history (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 915 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 916 | if (HISTORY_FULL () || (where >= history_length - 1) || rl_explicit_arg) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 917 | saved_history_line_to_use = where; |
| 918 | else |
| 919 | saved_history_line_to_use = where + 1; |
| 920 | |
| 921 | old_rl_startup_hook = rl_startup_hook; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 922 | rl_startup_hook = set_saved_history; |
| 923 | |
| 924 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 927 | /* This vi mode command causes VI_EDIT_COMMAND to be run on the current |
| 928 | command being entered (if no explicit argument is given), otherwise on |
| 929 | a command from the history file. */ |
| 930 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 931 | #define VI_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-vi}}\"" |
| 932 | #define EMACS_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-emacs}}\"" |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 933 | #define POSIX_VI_EDIT_COMMAND "fc -e vi" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 934 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 935 | static int |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 936 | edit_and_execute_command (count, c, editing_mode, edit_command) |
| 937 | int count, c, editing_mode; |
| 938 | char *edit_command; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 939 | { |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 940 | char *command, *metaval; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 941 | int r, rrs, metaflag; |
| 942 | sh_parser_state_t ps; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 943 | |
| 944 | rrs = rl_readline_state; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 945 | saved_command_line_count = current_command_line_count; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 946 | |
| 947 | /* Accept the current line. */ |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 948 | rl_newline (1, c); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 949 | |
| 950 | if (rl_explicit_arg) |
| 951 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 952 | command = (char *)xmalloc (strlen (edit_command) + 8); |
| 953 | sprintf (command, "%s %d", edit_command, count); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 954 | } |
| 955 | else |
| 956 | { |
| 957 | /* Take the command we were just editing, add it to the history file, |
| 958 | then call fc to operate on it. We have to add a dummy command to |
| 959 | the end of the history because fc ignores the last command (assumes |
| 960 | it's supposed to deal with the command before the `fc'). */ |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 961 | /* This breaks down when using command-oriented history and are not |
| 962 | finished with the command, so we should not ignore the last command */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 963 | using_history (); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 964 | if (rl_line_buffer[0]) |
| 965 | { |
| 966 | current_command_line_count++; /* for rl_newline above */ |
| 967 | bash_add_history (rl_line_buffer); |
| 968 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 969 | current_command_line_count = 0; /* for dummy history entry */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 970 | bash_add_history (""); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 971 | history_lines_this_session++; |
| 972 | using_history (); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 973 | command = savestring (edit_command); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 974 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 975 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 976 | metaval = rl_variable_value ("input-meta"); |
| 977 | metaflag = RL_BOOLEAN_VARIABLE_VALUE (metaval); |
| 978 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 979 | if (rl_deprep_term_function) |
| 980 | (*rl_deprep_term_function) (); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 981 | save_parser_state (&ps); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 982 | r = parse_and_execute (command, (editing_mode == VI_EDITING_MODE) ? "v" : "C-xC-e", SEVAL_NOHIST); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 983 | restore_parser_state (&ps); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 984 | if (rl_prep_term_function) |
| 985 | (*rl_prep_term_function) (metaflag); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 986 | |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 987 | current_command_line_count = saved_command_line_count; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 988 | |
| 989 | /* Now erase the contents of the current line and undo the effects of the |
| 990 | rl_accept_line() above. We don't even want to make the text we just |
| 991 | executed available for undoing. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 992 | rl_line_buffer[0] = '\0'; /* XXX */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 993 | rl_point = rl_end = 0; |
| 994 | rl_done = 0; |
| 995 | rl_readline_state = rrs; |
| 996 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 997 | #if defined (VI_MODE) |
| 998 | if (editing_mode == VI_EDITING_MODE) |
| 999 | rl_vi_insertion_mode (1, c); |
| 1000 | #endif |
| 1001 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1002 | rl_forced_update_display (); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1003 | |
| 1004 | return r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1005 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1006 | |
| 1007 | #if defined (VI_MODE) |
| 1008 | static int |
| 1009 | vi_edit_and_execute_command (count, c) |
| 1010 | int count, c; |
| 1011 | { |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1012 | if (posixly_correct) |
| 1013 | return (edit_and_execute_command (count, c, VI_EDITING_MODE, POSIX_VI_EDIT_COMMAND)); |
| 1014 | else |
| 1015 | return (edit_and_execute_command (count, c, VI_EDITING_MODE, VI_EDIT_COMMAND)); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1016 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1017 | #endif /* VI_MODE */ |
| 1018 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1019 | static int |
| 1020 | emacs_edit_and_execute_command (count, c) |
| 1021 | int count, c; |
| 1022 | { |
| 1023 | return (edit_and_execute_command (count, c, EMACS_EDITING_MODE, EMACS_EDIT_COMMAND)); |
| 1024 | } |
| 1025 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1026 | #if defined (ALIAS) |
| 1027 | static int |
| 1028 | posix_edit_macros (count, key) |
| 1029 | int count, key; |
| 1030 | { |
| 1031 | int c; |
| 1032 | char alias_name[3], *alias_value, *macro; |
| 1033 | |
| 1034 | c = rl_read_key (); |
| 1035 | alias_name[0] = '_'; |
| 1036 | alias_name[1] = c; |
| 1037 | alias_name[2] = '\0'; |
| 1038 | |
| 1039 | alias_value = get_alias_value (alias_name); |
| 1040 | if (alias_value && *alias_value) |
| 1041 | { |
| 1042 | macro = savestring (alias_value); |
| 1043 | rl_push_macro_input (macro); |
| 1044 | } |
| 1045 | return 0; |
| 1046 | } |
| 1047 | #endif |
| 1048 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1049 | /* Bindable commands that move `shell-words': that is, sequences of |
| 1050 | non-unquoted-metacharacters. */ |
| 1051 | |
| 1052 | #define WORDDELIM(c) (shellmeta(c) || shellblank(c)) |
| 1053 | |
| 1054 | static int |
| 1055 | bash_forward_shellword (count, key) |
| 1056 | int count, key; |
| 1057 | { |
| 1058 | size_t slen; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1059 | int c, p; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1060 | DECLARE_MBSTATE; |
| 1061 | |
| 1062 | if (count < 0) |
| 1063 | return (bash_backward_shellword (-count, key)); |
| 1064 | |
| 1065 | /* The tricky part of this is deciding whether or not the first character |
| 1066 | we're on is an unquoted metacharacter. Not completely handled yet. */ |
| 1067 | /* XXX - need to test this stuff with backslash-escaped shell |
| 1068 | metacharacters and unclosed single- and double-quoted strings. */ |
| 1069 | |
| 1070 | p = rl_point; |
| 1071 | slen = rl_end; |
| 1072 | |
| 1073 | while (count) |
| 1074 | { |
| 1075 | if (p == rl_end) |
| 1076 | { |
| 1077 | rl_point = rl_end; |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1081 | /* Are we in a quoted string? If we are, move to the end of the quoted |
| 1082 | string and continue the outer loop. We only want quoted strings, not |
| 1083 | backslash-escaped characters, but char_is_quoted doesn't |
| 1084 | differentiate. */ |
| 1085 | if (char_is_quoted (rl_line_buffer, p) && p > 0 && rl_line_buffer[p-1] != '\\') |
| 1086 | { |
| 1087 | do |
| 1088 | ADVANCE_CHAR (rl_line_buffer, slen, p); |
| 1089 | while (p < rl_end && char_is_quoted (rl_line_buffer, p)); |
| 1090 | count--; |
| 1091 | continue; |
| 1092 | } |
| 1093 | |
| 1094 | /* Rest of code assumes we are not in a quoted string. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1095 | /* Move forward until we hit a non-metacharacter. */ |
| 1096 | while (p < rl_end && (c = rl_line_buffer[p]) && WORDDELIM (c)) |
| 1097 | { |
| 1098 | switch (c) |
| 1099 | { |
| 1100 | default: |
| 1101 | ADVANCE_CHAR (rl_line_buffer, slen, p); |
| 1102 | continue; /* straight back to loop, don't increment p */ |
| 1103 | case '\\': |
| 1104 | if (p < rl_end && rl_line_buffer[p]) |
| 1105 | ADVANCE_CHAR (rl_line_buffer, slen, p); |
| 1106 | break; |
| 1107 | case '\'': |
| 1108 | p = skip_to_delim (rl_line_buffer, ++p, "'", SD_NOJMP); |
| 1109 | break; |
| 1110 | case '"': |
| 1111 | p = skip_to_delim (rl_line_buffer, ++p, "\"", SD_NOJMP); |
| 1112 | break; |
| 1113 | } |
| 1114 | |
| 1115 | if (p < rl_end) |
| 1116 | p++; |
| 1117 | } |
| 1118 | |
| 1119 | if (rl_line_buffer[p] == 0 || p == rl_end) |
| 1120 | { |
| 1121 | rl_point = rl_end; |
| 1122 | rl_ding (); |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | /* Now move forward until we hit a non-quoted metacharacter or EOL */ |
| 1127 | while (p < rl_end && (c = rl_line_buffer[p]) && WORDDELIM (c) == 0) |
| 1128 | { |
| 1129 | switch (c) |
| 1130 | { |
| 1131 | default: |
| 1132 | ADVANCE_CHAR (rl_line_buffer, slen, p); |
| 1133 | continue; /* straight back to loop, don't increment p */ |
| 1134 | case '\\': |
| 1135 | if (p < rl_end && rl_line_buffer[p]) |
| 1136 | ADVANCE_CHAR (rl_line_buffer, slen, p); |
| 1137 | break; |
| 1138 | case '\'': |
| 1139 | p = skip_to_delim (rl_line_buffer, ++p, "'", SD_NOJMP); |
| 1140 | break; |
| 1141 | case '"': |
| 1142 | p = skip_to_delim (rl_line_buffer, ++p, "\"", SD_NOJMP); |
| 1143 | break; |
| 1144 | } |
| 1145 | |
| 1146 | if (p < rl_end) |
| 1147 | p++; |
| 1148 | } |
| 1149 | |
| 1150 | if (p == rl_end || rl_line_buffer[p] == 0) |
| 1151 | { |
| 1152 | rl_point = rl_end; |
| 1153 | return (0); |
| 1154 | } |
| 1155 | |
| 1156 | count--; |
| 1157 | } |
| 1158 | |
| 1159 | rl_point = p; |
| 1160 | return (0); |
| 1161 | } |
| 1162 | |
| 1163 | static int |
| 1164 | bash_backward_shellword (count, key) |
| 1165 | int count, key; |
| 1166 | { |
| 1167 | size_t slen; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1168 | int c, p; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1169 | DECLARE_MBSTATE; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1170 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1171 | if (count < 0) |
| 1172 | return (bash_forward_shellword (-count, key)); |
| 1173 | |
| 1174 | p = rl_point; |
| 1175 | slen = rl_end; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1176 | |
| 1177 | if (p == rl_end && p > 0) |
| 1178 | p--; |
| 1179 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1180 | while (count) |
| 1181 | { |
| 1182 | if (p == 0) |
| 1183 | { |
| 1184 | rl_point = 0; |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | /* Move backward until we hit a non-metacharacter. */ |
| 1189 | while (p > 0) |
| 1190 | { |
| 1191 | c = rl_line_buffer[p]; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1192 | if (WORDDELIM (c) == 0 || char_is_quoted (rl_line_buffer, p)) |
| 1193 | break; |
| 1194 | BACKUP_CHAR (rl_line_buffer, slen, p); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | if (p == 0) |
| 1198 | { |
| 1199 | rl_point = 0; |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | /* Now move backward until we hit a metacharacter or BOL. */ |
| 1204 | while (p > 0) |
| 1205 | { |
| 1206 | c = rl_line_buffer[p]; |
| 1207 | if (WORDDELIM (c) && char_is_quoted (rl_line_buffer, p) == 0) |
| 1208 | break; |
| 1209 | BACKUP_CHAR (rl_line_buffer, slen, p); |
| 1210 | } |
| 1211 | |
| 1212 | count--; |
| 1213 | } |
| 1214 | |
| 1215 | rl_point = p; |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | static int |
| 1220 | bash_kill_shellword (count, key) |
| 1221 | int count, key; |
| 1222 | { |
| 1223 | int p; |
| 1224 | |
| 1225 | if (count < 0) |
| 1226 | return (bash_backward_kill_shellword (-count, key)); |
| 1227 | |
| 1228 | p = rl_point; |
| 1229 | bash_forward_shellword (count, key); |
| 1230 | |
| 1231 | if (rl_point != p) |
| 1232 | rl_kill_text (p, rl_point); |
| 1233 | |
| 1234 | rl_point = p; |
| 1235 | if (rl_editing_mode == 1) /* 1 == emacs_mode */ |
| 1236 | rl_mark = rl_point; |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static int |
| 1242 | bash_backward_kill_shellword (count, key) |
| 1243 | int count, key; |
| 1244 | { |
| 1245 | int p; |
| 1246 | |
| 1247 | if (count < 0) |
| 1248 | return (bash_kill_shellword (-count, key)); |
| 1249 | |
| 1250 | p = rl_point; |
| 1251 | bash_backward_shellword (count, key); |
| 1252 | |
| 1253 | if (rl_point != p) |
| 1254 | rl_kill_text (p, rl_point); |
| 1255 | |
| 1256 | if (rl_editing_mode == 1) /* 1 == emacs_mode */ |
| 1257 | rl_mark = rl_point; |
| 1258 | |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1263 | /* **************************************************************** */ |
| 1264 | /* */ |
| 1265 | /* How To Do Shell Completion */ |
| 1266 | /* */ |
| 1267 | /* **************************************************************** */ |
| 1268 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1269 | #define COMMAND_SEPARATORS ";|&{(`" |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1270 | /* )} */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1271 | #define COMMAND_SEPARATORS_PLUS_WS ";|&{(` \t" |
| 1272 | /* )} */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1273 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1274 | /* check for redirections and other character combinations that are not |
| 1275 | command separators */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1276 | static int |
| 1277 | check_redir (ti) |
| 1278 | int ti; |
| 1279 | { |
| 1280 | register int this_char, prev_char; |
| 1281 | |
| 1282 | /* Handle the two character tokens `>&', `<&', and `>|'. |
| 1283 | We are not in a command position after one of these. */ |
| 1284 | this_char = rl_line_buffer[ti]; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1285 | prev_char = (ti > 0) ? rl_line_buffer[ti - 1] : 0; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1286 | |
| 1287 | if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) || |
| 1288 | (this_char == '|' && prev_char == '>')) |
| 1289 | return (1); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1290 | else if (this_char == '{' && prev_char == '$') /*}*/ |
| 1291 | return (1); |
| 1292 | #if 0 /* Not yet */ |
| 1293 | else if (this_char == '(' && prev_char == '$') /*)*/ |
| 1294 | return (1); |
| 1295 | else if (this_char == '(' && prev_char == '<') /*)*/ |
| 1296 | return (1); |
| 1297 | #if defined (EXTENDED_GLOB) |
| 1298 | else if (extended_glob && this_char == '(' && prev_char == '!') /*)*/ |
| 1299 | return (1); |
| 1300 | #endif |
| 1301 | #endif |
| 1302 | else if (char_is_quoted (rl_line_buffer, ti)) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1303 | return (1); |
| 1304 | return (0); |
| 1305 | } |
| 1306 | |
| 1307 | #if defined (PROGRAMMABLE_COMPLETION) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1308 | /* |
| 1309 | * XXX - because of the <= start test, and setting os = s+1, this can |
| 1310 | * potentially return os > start. This is probably not what we want to |
| 1311 | * happen, but fix later after 2.05a-release. |
| 1312 | */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1313 | static int |
| 1314 | find_cmd_start (start) |
| 1315 | int start; |
| 1316 | { |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1317 | register int s, os, ns; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1318 | |
| 1319 | os = 0; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1320 | /* Flags == SD_NOJMP only because we want to skip over command substitutions |
| 1321 | in assignment statements. Have to test whether this affects `standalone' |
| 1322 | command substitutions as individual words. */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1323 | while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE/*|SD_NOSKIPCMD*/)) <= start) && |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1324 | rl_line_buffer[s]) |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1325 | { |
| 1326 | /* Handle >| token crudely; treat as > not | */ |
| 1327 | if (rl_line_buffer[s] == '|' && rl_line_buffer[s-1] == '>') |
| 1328 | { |
| 1329 | ns = skip_to_delim (rl_line_buffer, s+1, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE/*|SD_NOSKIPCMD*/); |
| 1330 | if (ns > start || rl_line_buffer[ns] == 0) |
| 1331 | return os; |
| 1332 | os = ns+1; |
| 1333 | continue; |
| 1334 | } |
| 1335 | os = s+1; |
| 1336 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1337 | return os; |
| 1338 | } |
| 1339 | |
| 1340 | static int |
| 1341 | find_cmd_end (end) |
| 1342 | int end; |
| 1343 | { |
| 1344 | register int e; |
| 1345 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1346 | e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1347 | return e; |
| 1348 | } |
| 1349 | |
| 1350 | static char * |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1351 | find_cmd_name (start, sp, ep) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1352 | int start; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1353 | int *sp, *ep; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1354 | { |
| 1355 | char *name; |
| 1356 | register int s, e; |
| 1357 | |
| 1358 | for (s = start; whitespace (rl_line_buffer[s]); s++) |
| 1359 | ; |
| 1360 | |
| 1361 | /* skip until a shell break character */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1362 | e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n", SD_NOJMP|SD_COMPLETE); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1363 | |
| 1364 | name = substring (rl_line_buffer, s, e); |
| 1365 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1366 | if (sp) |
| 1367 | *sp = s; |
| 1368 | if (ep) |
| 1369 | *ep = e; |
| 1370 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1371 | return (name); |
| 1372 | } |
| 1373 | |
| 1374 | static char * |
| 1375 | prog_complete_return (text, matchnum) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1376 | const char *text; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1377 | int matchnum; |
| 1378 | { |
| 1379 | static int ind; |
| 1380 | |
| 1381 | if (matchnum == 0) |
| 1382 | ind = 0; |
| 1383 | |
| 1384 | if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0) |
| 1385 | return (char *)NULL; |
| 1386 | return (prog_complete_matches[ind++]); |
| 1387 | } |
| 1388 | |
| 1389 | #endif /* PROGRAMMABLE_COMPLETION */ |
| 1390 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1391 | /* Try and catch completion attempts that are syntax errors or otherwise |
| 1392 | invalid. */ |
| 1393 | static int |
| 1394 | invalid_completion (text, ind) |
| 1395 | const char *text; |
| 1396 | int ind; |
| 1397 | { |
| 1398 | int pind; |
| 1399 | |
| 1400 | /* If we don't catch these here, the next clause will */ |
| 1401 | if (ind > 0 && rl_line_buffer[ind] == '(' && /*)*/ |
| 1402 | member (rl_line_buffer[ind-1], "$<>")) |
| 1403 | return 0; |
| 1404 | |
| 1405 | pind = ind - 1; |
| 1406 | while (pind > 0 && whitespace (rl_line_buffer[pind])) |
| 1407 | pind--; |
| 1408 | /* If we have only whitespace preceding a paren, it's valid */ |
| 1409 | if (ind >= 0 && pind <= 0 && rl_line_buffer[ind] == '(') /*)*/ |
| 1410 | return 0; |
| 1411 | /* Flag the invalid completions, which are mostly syntax errors */ |
| 1412 | if (ind > 0 && rl_line_buffer[ind] == '(' && /*)*/ |
| 1413 | member (rl_line_buffer[pind], COMMAND_SEPARATORS) == 0) |
| 1414 | return 1; |
| 1415 | |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1419 | /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are |
| 1420 | at START and END. Return an array of matches, or NULL if none. */ |
| 1421 | static char ** |
| 1422 | attempt_shell_completion (text, start, end) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1423 | const char *text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1424 | int start, end; |
| 1425 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1426 | int in_command_position, ti, qc, dflags; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1427 | char **matches, *command_separator_chars; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1428 | #if defined (PROGRAMMABLE_COMPLETION) |
| 1429 | int have_progcomps, was_assignment; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1430 | COMPSPEC *iw_compspec; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1431 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1432 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1433 | command_separator_chars = COMMAND_SEPARATORS; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1434 | matches = (char **)NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1435 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1436 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 1437 | rl_filename_quote_characters = default_filename_quote_characters; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1438 | set_filename_bstab (rl_filename_quote_characters); |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 1439 | set_directory_hook (); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1440 | rl_filename_stat_hook = bash_filename_stat_hook; |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 1441 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1442 | rl_sort_completion_matches = 1; /* sort by default */ |
| 1443 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1444 | /* Determine if this could be a command word. It is if it appears at |
| 1445 | the start of the line (ignoring preceding whitespace), or if it |
| 1446 | appears after a character that separates commands. It cannot be a |
| 1447 | command word if we aren't at the top-level prompt. */ |
| 1448 | ti = start - 1; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1449 | qc = -1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1450 | |
| 1451 | while ((ti > -1) && (whitespace (rl_line_buffer[ti]))) |
| 1452 | ti--; |
| 1453 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1454 | #if 1 |
| 1455 | /* If this is an open quote, maybe we're trying to complete a quoted |
| 1456 | command name. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1457 | if (ti >= 0 && (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\'')) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1458 | { |
| 1459 | qc = rl_line_buffer[ti]; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1460 | ti--; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1461 | while (ti > -1 && (whitespace (rl_line_buffer[ti]))) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1462 | ti--; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1463 | } |
| 1464 | #endif |
| 1465 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1466 | in_command_position = 0; |
| 1467 | if (ti < 0) |
| 1468 | { |
| 1469 | /* Only do command completion at the start of a line when we |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1470 | are prompting at the top level. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1471 | if (current_prompt_string == ps1_prompt) |
| 1472 | in_command_position++; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1473 | else if (parser_in_command_position ()) |
| 1474 | in_command_position++; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1475 | } |
| 1476 | else if (member (rl_line_buffer[ti], command_separator_chars)) |
| 1477 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1478 | in_command_position++; |
| 1479 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1480 | if (check_redir (ti) == 1) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1481 | in_command_position = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1482 | } |
| 1483 | else |
| 1484 | { |
| 1485 | /* This still could be in command position. It is possible |
| 1486 | that all of the previous words on the line are variable |
| 1487 | assignments. */ |
| 1488 | } |
| 1489 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1490 | if (in_command_position && invalid_completion (text, ti)) |
| 1491 | { |
| 1492 | rl_attempted_completion_over = 1; |
| 1493 | return ((char **)NULL); |
| 1494 | } |
| 1495 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1496 | /* Check that we haven't incorrectly flagged a closed command substitution |
| 1497 | as indicating we're in a command position. */ |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1498 | if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' && |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1499 | *text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1500 | in_command_position = 0; |
| 1501 | |
| 1502 | /* Special handling for command substitution. If *TEXT is a backquote, |
| 1503 | it can be the start or end of an old-style command substitution, or |
| 1504 | unmatched. If it's unmatched, both calls to unclosed_pair will |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1505 | succeed. Don't bother if readline found a single quote and we are |
| 1506 | completing on the substring. */ |
| 1507 | if (*text == '`' && rl_completion_quote_character != '\'' && |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1508 | (in_command_position || (unclosed_pair (rl_line_buffer, start, "`") && |
| 1509 | unclosed_pair (rl_line_buffer, end, "`")))) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1510 | matches = rl_completion_matches (text, command_subst_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1511 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1512 | #if defined (PROGRAMMABLE_COMPLETION) |
| 1513 | /* Attempt programmable completion. */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1514 | have_progcomps = prog_completion_enabled && (progcomp_size () > 0); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1515 | iw_compspec = progcomp_search (INITIALWORD); |
| 1516 | if (matches == 0 && |
| 1517 | (in_command_position == 0 || text[0] == '\0' || (in_command_position && iw_compspec)) && |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1518 | current_prompt_string == ps1_prompt) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1519 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1520 | int s, e, s1, e1, os, foundcs; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1521 | char *n; |
| 1522 | |
| 1523 | /* XXX - don't free the members */ |
| 1524 | if (prog_complete_matches) |
| 1525 | free (prog_complete_matches); |
| 1526 | prog_complete_matches = (char **)NULL; |
| 1527 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1528 | os = start; |
| 1529 | n = 0; |
| Chet Ramey | 2b3ca7e | 2015-08-13 15:38:31 -0400 | [diff] [blame] | 1530 | was_assignment = 0; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1531 | s = find_cmd_start (os); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1532 | e = find_cmd_end (end); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1533 | do |
| 1534 | { |
| Chet Ramey | 2b3ca7e | 2015-08-13 15:38:31 -0400 | [diff] [blame] | 1535 | /* Don't read past the end of rl_line_buffer */ |
| 1536 | if (s > rl_end) |
| 1537 | { |
| 1538 | s1 = s = e1; |
| 1539 | break; |
| 1540 | } |
| 1541 | /* Or past point if point is within an assignment statement */ |
| 1542 | else if (was_assignment && s > rl_point) |
| 1543 | { |
| 1544 | s1 = s = e1; |
| 1545 | break; |
| 1546 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1547 | /* Skip over assignment statements preceding a command name. If we |
| 1548 | don't find a command name at all, we can perform command name |
| 1549 | completion. If we find a partial command name, we should perform |
| 1550 | command name completion on it. */ |
| 1551 | FREE (n); |
| 1552 | n = find_cmd_name (s, &s1, &e1); |
| 1553 | s = e1 + 1; |
| 1554 | } |
| 1555 | while (was_assignment = assignment (n, 0)); |
| 1556 | s = s1; /* reset to index where name begins */ |
| 1557 | |
| 1558 | /* s == index of where command name begins (reset above) |
| 1559 | e == end of current command, may be end of line |
| 1560 | s1 = index of where command name begins |
| 1561 | e1 == index of where command name ends |
| 1562 | start == index of where word to be completed begins |
| 1563 | end == index of where word to be completed ends |
| 1564 | if (s == start) we are doing command word completion for sure |
| 1565 | if (e1 == end) we are at the end of the command name and completing it */ |
| 1566 | if (start == 0 && end == 0 && e != 0 && text[0] == '\0') /* beginning of non-empty line */ |
| 1567 | foundcs = 0; |
| 1568 | else if (start == end && start == s1 && e != 0 && e1 > end) /* beginning of command name, leading whitespace */ |
| 1569 | foundcs = 0; |
| 1570 | else if (e == 0 && e == s && text[0] == '\0' && have_progcomps) /* beginning of empty line */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1571 | prog_complete_matches = programmable_completions (EMPTYCMD, text, s, e, &foundcs); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1572 | else if (start == end && text[0] == '\0' && s1 > start && whitespace (rl_line_buffer[start])) |
| 1573 | foundcs = 0; /* whitespace before command name */ |
| 1574 | else if (e > s && was_assignment == 0 && e1 == end && rl_line_buffer[e] == 0 && whitespace (rl_line_buffer[e-1]) == 0) |
| 1575 | { |
| 1576 | /* not assignment statement, but still want to perform command |
| 1577 | completion if we are composing command word. */ |
| 1578 | foundcs = 0; |
| 1579 | in_command_position = s == start && STREQ (n, text); /* XXX */ |
| 1580 | } |
| 1581 | else if (e > s && was_assignment == 0 && have_progcomps) |
| 1582 | { |
| 1583 | prog_complete_matches = programmable_completions (n, text, s, e, &foundcs); |
| 1584 | /* command completion if programmable completion fails */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1585 | /* If we have a completion for the initial word, we can prefer that */ |
| 1586 | in_command_position = s == start && (iw_compspec || STREQ (n, text)); /* XXX */ |
| 1587 | if (iw_compspec && in_command_position) |
| 1588 | foundcs = 0; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1589 | } |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1590 | /* empty command name following command separator */ |
| 1591 | else if (s >= e && n[0] == '\0' && text[0] == '\0' && start > 0 && |
| 1592 | was_assignment == 0 && member (rl_line_buffer[start-1], COMMAND_SEPARATORS)) |
| 1593 | { |
| 1594 | foundcs = 0; |
| 1595 | in_command_position = 1; |
| 1596 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1597 | else if (s >= e && n[0] == '\0' && text[0] == '\0' && start > 0) |
| 1598 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1599 | foundcs = 0; /* empty command name following optional assignments */ |
| 1600 | in_command_position += was_assignment; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1601 | } |
| 1602 | else if (s == start && e == end && STREQ (n, text) && start > 0) |
| 1603 | { |
| 1604 | foundcs = 0; /* partial command name following assignments */ |
| 1605 | in_command_position = 1; |
| 1606 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1607 | else |
| 1608 | foundcs = 0; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1609 | |
| 1610 | /* If we have defined a compspec for the initial (command) word, call |
| 1611 | it and process the results like any other programmable completion. */ |
| 1612 | if (in_command_position && have_progcomps && foundcs == 0 && iw_compspec) |
| 1613 | prog_complete_matches = programmable_completions (INITIALWORD, text, s, e, &foundcs); |
| 1614 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1615 | FREE (n); |
| 1616 | /* XXX - if we found a COMPSPEC for the command, just return whatever |
| 1617 | the programmable completion code returns, and disable the default |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1618 | filename completion that readline will do unless the COPT_DEFAULT |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1619 | option has been set with the `-o default' option to complete or |
| 1620 | compopt. */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1621 | if (foundcs) |
| 1622 | { |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1623 | pcomp_set_readline_variables (foundcs, 1); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1624 | /* Turn what the programmable completion code returns into what |
| 1625 | readline wants. I should have made compute_lcd_of_matches |
| 1626 | external... */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1627 | matches = rl_completion_matches (text, prog_complete_return); |
| 1628 | if ((foundcs & COPT_DEFAULT) == 0) |
| 1629 | rl_attempted_completion_over = 1; /* no default */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1630 | if (matches || ((foundcs & COPT_BASHDEFAULT) == 0)) |
| 1631 | return (matches); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | #endif |
| 1635 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1636 | if (matches == 0) |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1637 | { |
| 1638 | dflags = 0; |
| 1639 | if (in_command_position) |
| 1640 | dflags |= DEFCOMP_CMDPOS; |
| 1641 | matches = bash_default_completion (text, start, end, qc, dflags); |
| 1642 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1643 | |
| 1644 | return matches; |
| 1645 | } |
| 1646 | |
| 1647 | char ** |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1648 | bash_default_completion (text, start, end, qc, compflags) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1649 | const char *text; |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1650 | int start, end, qc, compflags; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1651 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1652 | char **matches, *t; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1653 | |
| 1654 | matches = (char **)NULL; |
| 1655 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1656 | /* New posix-style command substitution or variable name? */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1657 | if (*text == '$') |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1658 | { |
| 1659 | if (qc != '\'' && text[1] == '(') /* ) */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1660 | matches = rl_completion_matches (text, command_subst_completion_function); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1661 | else |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1662 | { |
| 1663 | matches = rl_completion_matches (text, variable_completion_function); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1664 | /* If a single match, see if it expands to a directory name and append |
| 1665 | a slash if it does. This requires us to expand the variable name, |
| 1666 | so we don't want to display errors if the variable is unset. This |
| 1667 | can happen with dynamic variables whose value has never been |
| 1668 | requested. */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1669 | if (matches && matches[0] && matches[1] == 0) |
| 1670 | { |
| 1671 | t = savestring (matches[0]); |
| 1672 | bash_filename_stat_hook (&t); |
| 1673 | /* doesn't use test_for_directory because that performs tilde |
| 1674 | expansion */ |
| 1675 | if (file_isdir (t)) |
| 1676 | rl_completion_append_character = '/'; |
| 1677 | free (t); |
| 1678 | } |
| 1679 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1680 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1681 | |
| 1682 | /* If the word starts in `~', and there is no slash in the word, then |
| 1683 | try completing this word as a username. */ |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1684 | if (matches == 0 && *text == '~' && mbschr (text, '/') == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1685 | matches = rl_completion_matches (text, rl_username_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1686 | |
| 1687 | /* Another one. Why not? If the word starts in '@', then look through |
| 1688 | the world of known hostnames for completion first. */ |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1689 | if (matches == 0 && perform_hostname_completion && *text == '@') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1690 | matches = rl_completion_matches (text, hostname_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1691 | |
| 1692 | /* And last, (but not least) if this word is in a command position, then |
| 1693 | complete over possible command names, including aliases, functions, |
| 1694 | and command names. */ |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1695 | if (matches == 0 && (compflags & DEFCOMP_CMDPOS)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1696 | { |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1697 | /* If END == START and text[0] == 0, we are trying to complete an empty |
| 1698 | command word. */ |
| 1699 | if (no_empty_command_completion && end == start && text[0] == '\0') |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1700 | { |
| 1701 | matches = (char **)NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1702 | rl_ignore_some_completions_function = bash_ignore_everything; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1703 | } |
| 1704 | else |
| 1705 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1706 | #define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x)) |
| 1707 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1708 | dot_in_path = 0; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1709 | matches = rl_completion_matches (text, command_word_completion_function); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1710 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1711 | /* If we are attempting command completion and nothing matches, we |
| 1712 | do not want readline to perform filename completion for us. We |
| 1713 | still want to be able to complete partial pathnames, so set the |
| 1714 | completion ignore function to something which will remove |
| 1715 | filenames and leave directories in the match list. */ |
| 1716 | if (matches == (char **)NULL) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1717 | rl_ignore_some_completions_function = bash_ignore_filenames; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1718 | else if (matches[1] == 0 && CMD_IS_DIR(matches[0]) && dot_in_path == 0) |
| 1719 | /* If we found a single match, without looking in the current |
| 1720 | directory (because it's not in $PATH), but the found name is |
| 1721 | also a command in the current directory, suppress appending any |
| 1722 | terminating character, since it's ambiguous. */ |
| 1723 | { |
| 1724 | rl_completion_suppress_append = 1; |
| 1725 | rl_filename_completion_desired = 0; |
| 1726 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1727 | else if (matches[0] && matches[1] && STREQ (matches[0], matches[1]) && CMD_IS_DIR (matches[0])) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1728 | /* There are multiple instances of the same match (duplicate |
| 1729 | completions haven't yet been removed). In this case, all of |
| 1730 | the matches will be the same, and the duplicate removal code |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1731 | will distill them all down to one. We turn on |
| 1732 | rl_completion_suppress_append for the same reason as above. |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1733 | Remember: we only care if there's eventually a single unique |
| 1734 | completion. If there are multiple completions this won't |
| 1735 | make a difference and the problem won't occur. */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1736 | { |
| 1737 | rl_completion_suppress_append = 1; |
| 1738 | rl_filename_completion_desired = 0; |
| 1739 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1740 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1743 | /* This could be a globbing pattern, so try to expand it using pathname |
| 1744 | expansion. */ |
| Chet Ramey | 4d2e315 | 2019-01-18 15:12:37 -0500 | [diff] [blame^] | 1745 | if (!matches && completion_glob_pattern ((char *)text)) |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1746 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1747 | matches = rl_completion_matches (text, glob_complete_word); |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1748 | /* A glob expression that matches more than one filename is problematic. |
| 1749 | If we match more than one filename, punt. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1750 | if (matches && matches[1] && rl_completion_type == TAB) |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1751 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1752 | strvec_dispose (matches); |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1753 | matches = (char **)0; |
| 1754 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1755 | else if (matches && matches[1] && rl_completion_type == '!') |
| 1756 | { |
| 1757 | rl_completion_suppress_append = 1; |
| 1758 | rl_filename_completion_desired = 0; |
| 1759 | } |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1760 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1761 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1762 | return (matches); |
| 1763 | } |
| 1764 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1765 | static int |
| 1766 | bash_command_name_stat_hook (name) |
| 1767 | char **name; |
| 1768 | { |
| 1769 | char *cname, *result; |
| 1770 | |
| 1771 | /* If it's not something we're going to look up in $PATH, just call the |
| 1772 | normal filename stat hook. */ |
| 1773 | if (absolute_program (*name)) |
| 1774 | return (bash_filename_stat_hook (name)); |
| 1775 | |
| 1776 | cname = *name; |
| 1777 | /* XXX - we could do something here with converting aliases, builtins, |
| 1778 | and functions into something that came out as executable, but we don't. */ |
| 1779 | result = search_for_command (cname, 0); |
| 1780 | if (result) |
| 1781 | { |
| 1782 | *name = result; |
| 1783 | return 1; |
| 1784 | } |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
| 1788 | static int |
| 1789 | executable_completion (filename, searching_path) |
| 1790 | const char *filename; |
| 1791 | int searching_path; |
| 1792 | { |
| 1793 | char *f; |
| 1794 | int r; |
| 1795 | |
| 1796 | f = savestring (filename); |
| 1797 | bash_directory_completion_hook (&f); |
| 1798 | |
| 1799 | r = searching_path ? executable_file (f) : executable_or_directory (f); |
| 1800 | free (f); |
| 1801 | return r; |
| 1802 | } |
| 1803 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1804 | /* This is the function to call when the word to complete is in a position |
| 1805 | where a command word can be found. It grovels $PATH, looking for commands |
| 1806 | that match. It also scans aliases, function names, and the shell_builtin |
| 1807 | table. */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1808 | char * |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1809 | command_word_completion_function (hint_text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1810 | const char *hint_text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1811 | int state; |
| 1812 | { |
| 1813 | static char *hint = (char *)NULL; |
| 1814 | static char *path = (char *)NULL; |
| 1815 | static char *val = (char *)NULL; |
| 1816 | static char *filename_hint = (char *)NULL; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1817 | static char *fnhint = (char *)NULL; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1818 | static char *dequoted_hint = (char *)NULL; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1819 | static char *directory_part = (char *)NULL; |
| 1820 | static char **glob_matches = (char **)NULL; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1821 | static int path_index, hint_len, istate, igncase; |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1822 | static int mapping_over, local_index, searching_path, hint_is_dir; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1823 | static int old_glob_ignore_case, globpat; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1824 | static SHELL_VAR **varlist = (SHELL_VAR **)NULL; |
| 1825 | #if defined (ALIAS) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1826 | static alias_t **alias_list = (alias_t **)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1827 | #endif /* ALIAS */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1828 | char *temp, *cval; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1829 | |
| 1830 | /* We have to map over the possibilities for command words. If we have |
| 1831 | no state, then make one just for that purpose. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1832 | if (state == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1833 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1834 | rl_filename_stat_hook = bash_command_name_stat_hook; |
| 1835 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1836 | if (dequoted_hint && dequoted_hint != hint) |
| 1837 | free (dequoted_hint); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1838 | if (hint) |
| 1839 | free (hint); |
| 1840 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1841 | mapping_over = searching_path = 0; |
| 1842 | hint_is_dir = CMD_IS_DIR (hint_text); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1843 | val = (char *)NULL; |
| 1844 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1845 | temp = rl_variable_value ("completion-ignore-case"); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1846 | igncase = RL_BOOLEAN_VARIABLE_VALUE (temp); |
| 1847 | |
| 1848 | if (glob_matches) |
| 1849 | { |
| 1850 | free (glob_matches); |
| 1851 | glob_matches = (char **)NULL; |
| 1852 | } |
| 1853 | |
| Chet Ramey | 4d2e315 | 2019-01-18 15:12:37 -0500 | [diff] [blame^] | 1854 | globpat = completion_glob_pattern ((char *)hint_text); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1855 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1856 | /* If this is an absolute program name, do not check it against |
| 1857 | aliases, reserved words, functions or builtins. We must check |
| 1858 | whether or not it is unique, and, if so, whether that filename |
| 1859 | is executable. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1860 | if (globpat || absolute_program (hint_text)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1861 | { |
| 1862 | /* Perform tilde expansion on what's passed, so we don't end up |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1863 | passing filenames with tildes directly to stat(). The rest of |
| 1864 | the shell doesn't do variable expansion on the word following |
| 1865 | the tilde, so we don't do it here even if direxpand is set. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1866 | if (*hint_text == '~') |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1867 | { |
| 1868 | hint = bash_tilde_expand (hint_text, 0); |
| 1869 | directory_part = savestring (hint_text); |
| 1870 | temp = strchr (directory_part, '/'); |
| 1871 | if (temp) |
| 1872 | *temp = 0; |
| 1873 | else |
| 1874 | { |
| 1875 | free (directory_part); |
| 1876 | directory_part = (char *)NULL; |
| 1877 | } |
| 1878 | } |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 1879 | else if (dircomplete_expand) |
| 1880 | { |
| 1881 | hint = savestring (hint_text); |
| 1882 | bash_directory_completion_hook (&hint); |
| 1883 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1884 | else |
| 1885 | hint = savestring (hint_text); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1886 | |
| 1887 | dequoted_hint = hint; |
| 1888 | /* If readline's completer found a quote character somewhere, but |
| 1889 | didn't set the quote character, there must have been a quote |
| 1890 | character embedded in the filename. It can't be at the start of |
| 1891 | the filename, so we need to dequote the filename before we look |
| 1892 | in the file system for it. */ |
| 1893 | if (rl_completion_found_quote && rl_completion_quote_character == 0) |
| 1894 | { |
| 1895 | dequoted_hint = bash_dequote_filename (hint, 0); |
| 1896 | free (hint); |
| 1897 | hint = dequoted_hint; |
| 1898 | } |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1899 | hint_len = strlen (hint); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1900 | |
| 1901 | if (filename_hint) |
| 1902 | free (filename_hint); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1903 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1904 | fnhint = filename_hint = savestring (hint); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1905 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1906 | istate = 0; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1907 | |
| 1908 | if (globpat) |
| 1909 | { |
| 1910 | mapping_over = 5; |
| 1911 | goto globword; |
| 1912 | } |
| 1913 | else |
| 1914 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1915 | if (dircomplete_expand && path_dot_or_dotdot (filename_hint)) |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 1916 | { |
| 1917 | dircomplete_expand = 0; |
| 1918 | set_directory_hook (); |
| 1919 | dircomplete_expand = 1; |
| 1920 | } |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1921 | mapping_over = 4; |
| 1922 | goto inner; |
| 1923 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1926 | dequoted_hint = hint = savestring (hint_text); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1927 | hint_len = strlen (hint); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1928 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1929 | if (rl_completion_found_quote && rl_completion_quote_character == 0) |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1930 | dequoted_hint = bash_dequote_filename (hint, 0); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1931 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1932 | path = get_string_value ("PATH"); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1933 | path_index = dot_in_path = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1934 | |
| 1935 | /* Initialize the variables for each type of command word. */ |
| 1936 | local_index = 0; |
| 1937 | |
| 1938 | if (varlist) |
| 1939 | free (varlist); |
| 1940 | |
| 1941 | varlist = all_visible_functions (); |
| 1942 | |
| 1943 | #if defined (ALIAS) |
| 1944 | if (alias_list) |
| 1945 | free (alias_list); |
| 1946 | |
| 1947 | alias_list = all_aliases (); |
| 1948 | #endif /* ALIAS */ |
| 1949 | } |
| 1950 | |
| 1951 | /* mapping_over says what we are currently hacking. Note that every case |
| 1952 | in this list must fall through when there are no more possibilities. */ |
| 1953 | |
| 1954 | switch (mapping_over) |
| 1955 | { |
| 1956 | case 0: /* Aliases come first. */ |
| 1957 | #if defined (ALIAS) |
| 1958 | while (alias_list && alias_list[local_index]) |
| 1959 | { |
| 1960 | register char *alias; |
| 1961 | |
| 1962 | alias = alias_list[local_index++]->name; |
| 1963 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1964 | if (igncase == 0 && (STREQN (alias, hint, hint_len))) |
| 1965 | return (savestring (alias)); |
| 1966 | else if (igncase && strncasecmp (alias, hint, hint_len) == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1967 | return (savestring (alias)); |
| 1968 | } |
| 1969 | #endif /* ALIAS */ |
| 1970 | local_index = 0; |
| 1971 | mapping_over++; |
| 1972 | |
| 1973 | case 1: /* Then shell reserved words. */ |
| 1974 | { |
| 1975 | while (word_token_alist[local_index].word) |
| 1976 | { |
| 1977 | register char *reserved_word; |
| 1978 | |
| 1979 | reserved_word = word_token_alist[local_index++].word; |
| 1980 | |
| 1981 | if (STREQN (reserved_word, hint, hint_len)) |
| 1982 | return (savestring (reserved_word)); |
| 1983 | } |
| 1984 | local_index = 0; |
| 1985 | mapping_over++; |
| 1986 | } |
| 1987 | |
| 1988 | case 2: /* Then function names. */ |
| 1989 | while (varlist && varlist[local_index]) |
| 1990 | { |
| 1991 | register char *varname; |
| 1992 | |
| 1993 | varname = varlist[local_index++]->name; |
| 1994 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 1995 | /* Honor completion-ignore-case for shell function names. */ |
| 1996 | if (igncase == 0 && (STREQN (varname, hint, hint_len))) |
| 1997 | return (savestring (varname)); |
| 1998 | else if (igncase && strncasecmp (varname, hint, hint_len) == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1999 | return (savestring (varname)); |
| 2000 | } |
| 2001 | local_index = 0; |
| 2002 | mapping_over++; |
| 2003 | |
| 2004 | case 3: /* Then shell builtins. */ |
| 2005 | for (; local_index < num_shell_builtins; local_index++) |
| 2006 | { |
| 2007 | /* Ignore it if it doesn't have a function pointer or if it |
| 2008 | is not currently enabled. */ |
| 2009 | if (!shell_builtins[local_index].function || |
| 2010 | (shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0) |
| 2011 | continue; |
| 2012 | |
| 2013 | if (STREQN (shell_builtins[local_index].name, hint, hint_len)) |
| 2014 | { |
| 2015 | int i = local_index++; |
| 2016 | |
| 2017 | return (savestring (shell_builtins[i].name)); |
| 2018 | } |
| 2019 | } |
| 2020 | local_index = 0; |
| 2021 | mapping_over++; |
| 2022 | } |
| 2023 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2024 | globword: |
| 2025 | /* Limited support for completing command words with globbing chars. Only |
| 2026 | a single match (multiple matches that end up reducing the number of |
| 2027 | characters in the common prefix are bad) will ever be returned on |
| 2028 | regular completion. */ |
| Chet Ramey | 30d188c | 2011-11-21 20:57:16 -0500 | [diff] [blame] | 2029 | if (globpat) |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2030 | { |
| 2031 | if (state == 0) |
| 2032 | { |
| 2033 | glob_ignore_case = igncase; |
| 2034 | glob_matches = shell_glob_filename (hint); |
| 2035 | glob_ignore_case = old_glob_ignore_case; |
| 2036 | |
| 2037 | if (GLOB_FAILED (glob_matches) || glob_matches == 0) |
| 2038 | { |
| 2039 | glob_matches = (char **)NULL; |
| 2040 | return ((char *)NULL); |
| 2041 | } |
| 2042 | |
| 2043 | local_index = 0; |
| 2044 | |
| 2045 | if (glob_matches[1] && rl_completion_type == TAB) /* multiple matches are bad */ |
| 2046 | return ((char *)NULL); |
| 2047 | } |
| 2048 | |
| 2049 | while (val = glob_matches[local_index++]) |
| 2050 | { |
| 2051 | if (executable_or_directory (val)) |
| 2052 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2053 | if (*hint_text == '~' && directory_part) |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2054 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2055 | temp = maybe_restore_tilde (val, directory_part); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2056 | free (val); |
| 2057 | val = temp; |
| 2058 | } |
| 2059 | return (val); |
| 2060 | } |
| 2061 | free (val); |
| 2062 | } |
| 2063 | |
| 2064 | glob_ignore_case = old_glob_ignore_case; |
| 2065 | return ((char *)NULL); |
| 2066 | } |
| 2067 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2068 | /* If the text passed is a directory in the current directory, return it |
| 2069 | as a possible match. Executables in directories in the current |
| 2070 | directory can be specified using relative pathnames and successfully |
| 2071 | executed even when `.' is not in $PATH. */ |
| 2072 | if (hint_is_dir) |
| 2073 | { |
| 2074 | hint_is_dir = 0; /* only return the hint text once */ |
| 2075 | return (savestring (hint_text)); |
| 2076 | } |
| 2077 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2078 | /* Repeatedly call filename_completion_function while we have |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2079 | members of PATH left. Question: should we stat each file? |
| 2080 | Answer: we call executable_file () on each file. */ |
| 2081 | outer: |
| 2082 | |
| 2083 | istate = (val != (char *)NULL); |
| 2084 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2085 | if (istate == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2086 | { |
| 2087 | char *current_path; |
| 2088 | |
| 2089 | /* Get the next directory from the path. If there is none, then we |
| 2090 | are all done. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2091 | if (path == 0 || path[path_index] == 0 || |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2092 | (current_path = extract_colon_unit (path, &path_index)) == 0) |
| 2093 | return ((char *)NULL); |
| 2094 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2095 | searching_path = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2096 | if (*current_path == 0) |
| 2097 | { |
| 2098 | free (current_path); |
| 2099 | current_path = savestring ("."); |
| 2100 | } |
| 2101 | |
| 2102 | if (*current_path == '~') |
| 2103 | { |
| 2104 | char *t; |
| 2105 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2106 | t = bash_tilde_expand (current_path, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2107 | free (current_path); |
| 2108 | current_path = t; |
| 2109 | } |
| 2110 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2111 | if (current_path[0] == '.' && current_path[1] == '\0') |
| 2112 | dot_in_path = 1; |
| 2113 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2114 | if (fnhint && fnhint != filename_hint) |
| 2115 | free (fnhint); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2116 | if (filename_hint) |
| 2117 | free (filename_hint); |
| 2118 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2119 | filename_hint = sh_makepath (current_path, hint, 0); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2120 | /* Need a quoted version (though it doesn't matter much in most |
| 2121 | cases) because rl_filename_completion_function dequotes the |
| 2122 | filename it gets, assuming that it's been quoted as part of |
| 2123 | the input line buffer. */ |
| 2124 | if (strpbrk (filename_hint, "\"'\\")) |
| 2125 | fnhint = sh_backslash_quote (filename_hint, filename_bstab, 0); |
| 2126 | else |
| 2127 | fnhint = filename_hint; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2128 | free (current_path); /* XXX */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | inner: |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2132 | val = rl_filename_completion_function (fnhint, istate); |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 2133 | if (mapping_over == 4 && dircomplete_expand) |
| 2134 | set_directory_hook (); |
| 2135 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2136 | istate = 1; |
| 2137 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2138 | if (val == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2139 | { |
| 2140 | /* If the hint text is an absolute program, then don't bother |
| 2141 | searching through PATH. */ |
| 2142 | if (absolute_program (hint)) |
| 2143 | return ((char *)NULL); |
| 2144 | |
| 2145 | goto outer; |
| 2146 | } |
| 2147 | else |
| 2148 | { |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2149 | int match, freetemp; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2150 | |
| 2151 | if (absolute_program (hint)) |
| 2152 | { |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2153 | if (igncase == 0) |
| 2154 | match = strncmp (val, hint, hint_len) == 0; |
| 2155 | else |
| 2156 | match = strncasecmp (val, hint, hint_len) == 0; |
| 2157 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2158 | /* If we performed tilde expansion, restore the original |
| 2159 | filename. */ |
| 2160 | if (*hint_text == '~') |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2161 | temp = maybe_restore_tilde (val, directory_part); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2162 | else |
| 2163 | temp = savestring (val); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2164 | freetemp = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2165 | } |
| 2166 | else |
| 2167 | { |
| 2168 | temp = strrchr (val, '/'); |
| 2169 | |
| 2170 | if (temp) |
| 2171 | { |
| 2172 | temp++; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2173 | if (igncase == 0) |
| 2174 | freetemp = match = strncmp (temp, hint, hint_len) == 0; |
| 2175 | else |
| 2176 | freetemp = match = strncasecmp (temp, hint, hint_len) == 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2177 | if (match) |
| 2178 | temp = savestring (temp); |
| 2179 | } |
| 2180 | else |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2181 | freetemp = match = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2184 | /* If we have found a match, and it is an executable file, return it. |
| 2185 | We don't return directory names when searching $PATH, since the |
| 2186 | bash execution code won't find executables in directories which |
| 2187 | appear in directories in $PATH when they're specified using |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2188 | relative pathnames. */ |
| 2189 | #if 0 |
| 2190 | /* If we're not searching $PATH and we have a relative pathname, we |
| 2191 | need to re-canonicalize it before testing whether or not it's an |
| 2192 | executable or a directory so the shell treats .. relative to $PWD |
| 2193 | according to the physical/logical option. The shell already |
| 2194 | canonicalizes the directory name in order to tell readline where |
| 2195 | to look, so not doing it here will be inconsistent. */ |
| 2196 | /* XXX -- currently not used -- will introduce more inconsistency, |
| 2197 | since shell does not canonicalize ../foo before passing it to |
| 2198 | shell_execve(). */ |
| 2199 | if (match && searching_path == 0 && *val == '.') |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2200 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2201 | char *t, *t1; |
| 2202 | |
| 2203 | t = get_working_directory ("command-word-completion"); |
| 2204 | t1 = make_absolute (val, t); |
| 2205 | free (t); |
| 2206 | cval = sh_canonpath (t1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS); |
| 2207 | } |
| 2208 | else |
| 2209 | #endif |
| 2210 | cval = val; |
| 2211 | |
| 2212 | if (match && executable_completion ((searching_path ? val : cval), searching_path)) |
| 2213 | { |
| 2214 | if (cval != val) |
| 2215 | free (cval); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2216 | free (val); |
| 2217 | val = ""; /* So it won't be NULL. */ |
| 2218 | return (temp); |
| 2219 | } |
| 2220 | else |
| 2221 | { |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2222 | if (freetemp) |
| 2223 | free (temp); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2224 | if (cval != val) |
| 2225 | free (cval); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2226 | free (val); |
| 2227 | goto inner; |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2232 | /* Completion inside an unterminated command substitution. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2233 | static char * |
| 2234 | command_subst_completion_function (text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2235 | const char *text; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2236 | int state; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2237 | { |
| 2238 | static char **matches = (char **)NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2239 | static const char *orig_start; |
| 2240 | static char *filename_text = (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2241 | static int cmd_index, start_len; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2242 | char *value; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2243 | |
| 2244 | if (state == 0) |
| 2245 | { |
| 2246 | if (filename_text) |
| 2247 | free (filename_text); |
| 2248 | orig_start = text; |
| 2249 | if (*text == '`') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2250 | text++; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2251 | else if (*text == '$' && text[1] == '(') /* ) */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2252 | text += 2; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2253 | /* If the text was quoted, suppress any quote character that the |
| 2254 | readline completion code would insert. */ |
| 2255 | rl_completion_suppress_quote = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2256 | start_len = text - orig_start; |
| 2257 | filename_text = savestring (text); |
| 2258 | if (matches) |
| 2259 | free (matches); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2260 | |
| 2261 | /* |
| 2262 | * At this point we can entertain the idea of re-parsing |
| 2263 | * `filename_text' into a (possibly incomplete) command name and |
| 2264 | * arguments, and doing completion based on that. This is |
| 2265 | * currently very rudimentary, but it is a small improvement. |
| 2266 | */ |
| 2267 | for (value = filename_text + strlen (filename_text) - 1; value > filename_text; value--) |
| 2268 | if (whitespace (*value) || member (*value, COMMAND_SEPARATORS)) |
| 2269 | break; |
| 2270 | if (value <= filename_text) |
| 2271 | matches = rl_completion_matches (filename_text, command_word_completion_function); |
| 2272 | else |
| 2273 | { |
| 2274 | value++; |
| 2275 | start_len += value - filename_text; |
| 2276 | if (whitespace (value[-1])) |
| 2277 | matches = rl_completion_matches (value, rl_filename_completion_function); |
| 2278 | else |
| 2279 | matches = rl_completion_matches (value, command_word_completion_function); |
| 2280 | } |
| 2281 | |
| 2282 | /* If there is more than one match, rl_completion_matches has already |
| 2283 | put the lcd in matches[0]. Skip over it. */ |
| 2284 | cmd_index = matches && matches[0] && matches[1]; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2285 | |
| 2286 | /* If there's a single match and it's a directory, set the append char |
| 2287 | to the expected `/'. Otherwise, don't append anything. */ |
| 2288 | if (matches && matches[0] && matches[1] == 0 && test_for_directory (matches[0])) |
| 2289 | rl_completion_append_character = '/'; |
| 2290 | else |
| 2291 | rl_completion_suppress_append = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 2294 | if (matches == 0 || matches[cmd_index] == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2295 | { |
| 2296 | rl_filename_quoting_desired = 0; /* disable quoting */ |
| 2297 | return ((char *)NULL); |
| 2298 | } |
| 2299 | else |
| 2300 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2301 | value = (char *)xmalloc (1 + start_len + strlen (matches[cmd_index])); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2302 | |
| 2303 | if (start_len == 1) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2304 | value[0] = *orig_start; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2305 | else |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2306 | strncpy (value, orig_start, start_len); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2307 | |
| 2308 | strcpy (value + start_len, matches[cmd_index]); |
| 2309 | |
| 2310 | cmd_index++; |
| 2311 | return (value); |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | /* Okay, now we write the entry_function for variable completion. */ |
| 2316 | static char * |
| 2317 | variable_completion_function (text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2318 | const char *text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2319 | int state; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2320 | { |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2321 | static char **varlist = (char **)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2322 | static int varlist_index; |
| 2323 | static char *varname = (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2324 | static int first_char, first_char_loc; |
| 2325 | |
| 2326 | if (!state) |
| 2327 | { |
| 2328 | if (varname) |
| 2329 | free (varname); |
| 2330 | |
| 2331 | first_char_loc = 0; |
| 2332 | first_char = text[0]; |
| 2333 | |
| 2334 | if (first_char == '$') |
| 2335 | first_char_loc++; |
| 2336 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2337 | if (text[first_char_loc] == '{') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2338 | first_char_loc++; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2339 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2340 | varname = savestring (text + first_char_loc); |
| 2341 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2342 | if (varlist) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2343 | strvec_dispose (varlist); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2344 | |
| 2345 | varlist = all_variables_matching_prefix (varname); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2346 | varlist_index = 0; |
| 2347 | } |
| 2348 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2349 | if (!varlist || !varlist[varlist_index]) |
| 2350 | { |
| 2351 | return ((char *)NULL); |
| 2352 | } |
| 2353 | else |
| 2354 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2355 | char *value; |
| 2356 | |
| 2357 | value = (char *)xmalloc (4 + strlen (varlist[varlist_index])); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2358 | |
| 2359 | if (first_char_loc) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2360 | { |
| 2361 | value[0] = first_char; |
| 2362 | if (first_char_loc == 2) |
| 2363 | value[1] = '{'; |
| 2364 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2365 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2366 | strcpy (value + first_char_loc, varlist[varlist_index]); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2367 | if (first_char_loc == 2) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2368 | strcat (value, "}"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2369 | |
| 2370 | varlist_index++; |
| 2371 | return (value); |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | /* How about a completion function for hostnames? */ |
| 2376 | static char * |
| 2377 | hostname_completion_function (text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2378 | const char *text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2379 | int state; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2380 | { |
| 2381 | static char **list = (char **)NULL; |
| 2382 | static int list_index = 0; |
| 2383 | static int first_char, first_char_loc; |
| 2384 | |
| 2385 | /* If we don't have any state, make some. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2386 | if (state == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2387 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2388 | FREE (list); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2389 | |
| 2390 | list = (char **)NULL; |
| 2391 | |
| 2392 | first_char_loc = 0; |
| 2393 | first_char = *text; |
| 2394 | |
| 2395 | if (first_char == '@') |
| 2396 | first_char_loc++; |
| 2397 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2398 | list = hostnames_matching ((char *)text+first_char_loc); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2399 | list_index = 0; |
| 2400 | } |
| 2401 | |
| 2402 | if (list && list[list_index]) |
| 2403 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2404 | char *t; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2405 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2406 | t = (char *)xmalloc (2 + strlen (list[list_index])); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2407 | *t = first_char; |
| 2408 | strcpy (t + first_char_loc, list[list_index]); |
| 2409 | list_index++; |
| 2410 | return (t); |
| 2411 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2412 | |
| 2413 | return ((char *)NULL); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2416 | /* |
| 2417 | * A completion function for service names from /etc/services (or wherever). |
| 2418 | */ |
| 2419 | char * |
| 2420 | bash_servicename_completion_function (text, state) |
| 2421 | const char *text; |
| 2422 | int state; |
| 2423 | { |
| 2424 | #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT) |
| 2425 | return ((char *)NULL); |
| 2426 | #else |
| 2427 | static char *sname = (char *)NULL; |
| 2428 | static struct servent *srvent; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 2429 | static int snamelen; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2430 | char *value; |
| 2431 | char **alist, *aentry; |
| 2432 | int afound; |
| 2433 | |
| 2434 | if (state == 0) |
| 2435 | { |
| 2436 | FREE (sname); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2437 | |
| 2438 | sname = savestring (text); |
| 2439 | snamelen = strlen (sname); |
| 2440 | setservent (0); |
| 2441 | } |
| 2442 | |
| 2443 | while (srvent = getservent ()) |
| 2444 | { |
| 2445 | afound = 0; |
| 2446 | if (snamelen == 0 || (STREQN (sname, srvent->s_name, snamelen))) |
| 2447 | break; |
| 2448 | /* Not primary, check aliases */ |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2449 | for (alist = srvent->s_aliases; *alist; alist++) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2450 | { |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2451 | aentry = *alist; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2452 | if (STREQN (sname, aentry, snamelen)) |
| 2453 | { |
| 2454 | afound = 1; |
| 2455 | break; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | if (afound) |
| 2460 | break; |
| 2461 | } |
| 2462 | |
| 2463 | if (srvent == 0) |
| 2464 | { |
| 2465 | endservent (); |
| 2466 | return ((char *)NULL); |
| 2467 | } |
| 2468 | |
| 2469 | value = afound ? savestring (aentry) : savestring (srvent->s_name); |
| 2470 | return value; |
| 2471 | #endif |
| 2472 | } |
| 2473 | |
| 2474 | /* |
| 2475 | * A completion function for group names from /etc/group (or wherever). |
| 2476 | */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2477 | char * |
| 2478 | bash_groupname_completion_function (text, state) |
| 2479 | const char *text; |
| 2480 | int state; |
| 2481 | { |
| 2482 | #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H) |
| 2483 | return ((char *)NULL); |
| 2484 | #else |
| 2485 | static char *gname = (char *)NULL; |
| 2486 | static struct group *grent; |
| 2487 | static int gnamelen; |
| 2488 | char *value; |
| 2489 | |
| 2490 | if (state == 0) |
| 2491 | { |
| 2492 | FREE (gname); |
| 2493 | gname = savestring (text); |
| 2494 | gnamelen = strlen (gname); |
| 2495 | |
| 2496 | setgrent (); |
| 2497 | } |
| 2498 | |
| 2499 | while (grent = getgrent ()) |
| 2500 | { |
| 2501 | if (gnamelen == 0 || (STREQN (gname, grent->gr_name, gnamelen))) |
| 2502 | break; |
| 2503 | } |
| 2504 | |
| 2505 | if (grent == 0) |
| 2506 | { |
| 2507 | endgrent (); |
| 2508 | return ((char *)NULL); |
| 2509 | } |
| 2510 | |
| 2511 | value = savestring (grent->gr_name); |
| 2512 | return (value); |
| 2513 | #endif |
| 2514 | } |
| 2515 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2516 | /* Functions to perform history and alias expansions on the current line. */ |
| 2517 | |
| 2518 | #if defined (BANG_HISTORY) |
| 2519 | /* Perform history expansion on the current line. If no history expansion |
| 2520 | is done, pre_process_line() returns what it was passed, so we need to |
| 2521 | allocate a new line here. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2522 | static char * |
| 2523 | history_expand_line_internal (line) |
| 2524 | char *line; |
| 2525 | { |
| 2526 | char *new_line; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2527 | int old_verify; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2528 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2529 | old_verify = hist_verify; |
| 2530 | hist_verify = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2531 | new_line = pre_process_line (line, 0, 0); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2532 | hist_verify = old_verify; |
| 2533 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2534 | return (new_line == line) ? savestring (line) : new_line; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2535 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2536 | #endif |
| 2537 | |
| 2538 | /* There was an error in expansion. Let the preprocessor print |
| 2539 | the error here. */ |
| 2540 | static void |
| 2541 | cleanup_expansion_error () |
| 2542 | { |
| 2543 | char *to_free; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2544 | #if defined (BANG_HISTORY) |
| 2545 | int old_verify; |
| 2546 | |
| 2547 | old_verify = hist_verify; |
| 2548 | hist_verify = 0; |
| 2549 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2550 | |
| 2551 | fprintf (rl_outstream, "\r\n"); |
| 2552 | to_free = pre_process_line (rl_line_buffer, 1, 0); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2553 | #if defined (BANG_HISTORY) |
| 2554 | hist_verify = old_verify; |
| 2555 | #endif |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2556 | if (to_free != rl_line_buffer) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2557 | FREE (to_free); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2558 | putc ('\r', rl_outstream); |
| 2559 | rl_forced_update_display (); |
| 2560 | } |
| 2561 | |
| 2562 | /* If NEW_LINE differs from what is in the readline line buffer, add an |
| 2563 | undo record to get from the readline line buffer contents to the new |
| 2564 | line and make NEW_LINE the current readline line. */ |
| 2565 | static void |
| 2566 | maybe_make_readline_line (new_line) |
| 2567 | char *new_line; |
| 2568 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 2569 | if (new_line && strcmp (new_line, rl_line_buffer) != 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2570 | { |
| 2571 | rl_point = rl_end; |
| 2572 | |
| 2573 | rl_add_undo (UNDO_BEGIN, 0, 0, 0); |
| 2574 | rl_delete_text (0, rl_point); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2575 | rl_point = rl_end = rl_mark = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2576 | rl_insert_text (new_line); |
| 2577 | rl_add_undo (UNDO_END, 0, 0, 0); |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */ |
| 2582 | static void |
| 2583 | set_up_new_line (new_line) |
| 2584 | char *new_line; |
| 2585 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2586 | int old_point, at_end; |
| 2587 | |
| 2588 | old_point = rl_point; |
| 2589 | at_end = rl_point == rl_end; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2590 | |
| 2591 | /* If the line was history and alias expanded, then make that |
| 2592 | be one thing to undo. */ |
| 2593 | maybe_make_readline_line (new_line); |
| 2594 | free (new_line); |
| 2595 | |
| 2596 | /* Place rl_point where we think it should go. */ |
| 2597 | if (at_end) |
| 2598 | rl_point = rl_end; |
| 2599 | else if (old_point < rl_end) |
| 2600 | { |
| 2601 | rl_point = old_point; |
| 2602 | if (!whitespace (rl_line_buffer[rl_point])) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2603 | rl_forward_word (1, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2607 | #if defined (ALIAS) |
| 2608 | /* Expand aliases in the current readline line. */ |
| 2609 | static int |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2610 | alias_expand_line (count, ignore) |
| 2611 | int count, ignore; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2612 | { |
| 2613 | char *new_line; |
| 2614 | |
| 2615 | new_line = alias_expand (rl_line_buffer); |
| 2616 | |
| 2617 | if (new_line) |
| 2618 | { |
| 2619 | set_up_new_line (new_line); |
| 2620 | return (0); |
| 2621 | } |
| 2622 | else |
| 2623 | { |
| 2624 | cleanup_expansion_error (); |
| 2625 | return (1); |
| 2626 | } |
| 2627 | } |
| 2628 | #endif |
| 2629 | |
| 2630 | #if defined (BANG_HISTORY) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2631 | /* History expand the line. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2632 | static int |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2633 | history_expand_line (count, ignore) |
| 2634 | int count, ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2635 | { |
| 2636 | char *new_line; |
| 2637 | |
| 2638 | new_line = history_expand_line_internal (rl_line_buffer); |
| 2639 | |
| 2640 | if (new_line) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2641 | { |
| 2642 | set_up_new_line (new_line); |
| 2643 | return (0); |
| 2644 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2645 | else |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2646 | { |
| 2647 | cleanup_expansion_error (); |
| 2648 | return (1); |
| 2649 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2650 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2651 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2652 | /* Expand history substitutions in the current line and then insert a |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2653 | space (hopefully close to where we were before). */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2654 | static int |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2655 | tcsh_magic_space (count, ignore) |
| 2656 | int count, ignore; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2657 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2658 | int dist_from_end, old_point; |
| 2659 | |
| 2660 | old_point = rl_point; |
| 2661 | dist_from_end = rl_end - rl_point; |
| 2662 | if (history_expand_line (count, ignore) == 0) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2663 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2664 | /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>. |
| 2665 | This works if all expansions were before rl_point or if no expansions |
| 2666 | were performed. */ |
| 2667 | rl_point = (old_point == 0) ? old_point : rl_end - dist_from_end; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2668 | rl_insert (1, ' '); |
| 2669 | return (0); |
| 2670 | } |
| 2671 | else |
| 2672 | return (1); |
| 2673 | } |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2674 | #endif /* BANG_HISTORY */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2675 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2676 | /* History and alias expand the line. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2677 | static int |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2678 | history_and_alias_expand_line (count, ignore) |
| 2679 | int count, ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2680 | { |
| 2681 | char *new_line; |
| 2682 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2683 | new_line = 0; |
| 2684 | #if defined (BANG_HISTORY) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2685 | new_line = history_expand_line_internal (rl_line_buffer); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2686 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2687 | |
| 2688 | #if defined (ALIAS) |
| 2689 | if (new_line) |
| 2690 | { |
| 2691 | char *alias_line; |
| 2692 | |
| 2693 | alias_line = alias_expand (new_line); |
| 2694 | free (new_line); |
| 2695 | new_line = alias_line; |
| 2696 | } |
| 2697 | #endif /* ALIAS */ |
| 2698 | |
| 2699 | if (new_line) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2700 | { |
| 2701 | set_up_new_line (new_line); |
| 2702 | return (0); |
| 2703 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2704 | else |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2705 | { |
| 2706 | cleanup_expansion_error (); |
| 2707 | return (1); |
| 2708 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | /* History and alias expand the line, then perform the shell word |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2712 | expansions by calling expand_string. This can't use set_up_new_line() |
| 2713 | because we want the variable expansions as a separate undo'able |
| 2714 | set of operations. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2715 | static int |
| 2716 | shell_expand_line (count, ignore) |
| 2717 | int count, ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2718 | { |
| 2719 | char *new_line; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2720 | WORD_LIST *expanded_string; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 2721 | WORD_DESC *w; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2722 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2723 | new_line = 0; |
| 2724 | #if defined (BANG_HISTORY) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2725 | new_line = history_expand_line_internal (rl_line_buffer); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2726 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2727 | |
| 2728 | #if defined (ALIAS) |
| 2729 | if (new_line) |
| 2730 | { |
| 2731 | char *alias_line; |
| 2732 | |
| 2733 | alias_line = alias_expand (new_line); |
| 2734 | free (new_line); |
| 2735 | new_line = alias_line; |
| 2736 | } |
| 2737 | #endif /* ALIAS */ |
| 2738 | |
| 2739 | if (new_line) |
| 2740 | { |
| 2741 | int old_point = rl_point; |
| 2742 | int at_end = rl_point == rl_end; |
| 2743 | |
| 2744 | /* If the line was history and alias expanded, then make that |
| 2745 | be one thing to undo. */ |
| 2746 | maybe_make_readline_line (new_line); |
| 2747 | free (new_line); |
| 2748 | |
| 2749 | /* If there is variable expansion to perform, do that as a separate |
| 2750 | operation to be undone. */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 2751 | |
| 2752 | #if 1 |
| 2753 | w = alloc_word_desc (); |
| 2754 | w->word = savestring (rl_line_buffer); |
| 2755 | w->flags = rl_explicit_arg ? (W_NOPROCSUB|W_NOCOMSUB) : 0; |
| 2756 | expanded_string = expand_word (w, rl_explicit_arg ? Q_HERE_DOCUMENT : 0); |
| 2757 | dispose_word (w); |
| 2758 | #else |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2759 | new_line = savestring (rl_line_buffer); |
| 2760 | expanded_string = expand_string (new_line, 0); |
| 2761 | FREE (new_line); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 2762 | #endif |
| 2763 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2764 | if (expanded_string == 0) |
| 2765 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2766 | new_line = (char *)xmalloc (1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2767 | new_line[0] = '\0'; |
| 2768 | } |
| 2769 | else |
| 2770 | { |
| 2771 | new_line = string_list (expanded_string); |
| 2772 | dispose_words (expanded_string); |
| 2773 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2774 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2775 | maybe_make_readline_line (new_line); |
| 2776 | free (new_line); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2777 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2778 | /* Place rl_point where we think it should go. */ |
| 2779 | if (at_end) |
| 2780 | rl_point = rl_end; |
| 2781 | else if (old_point < rl_end) |
| 2782 | { |
| 2783 | rl_point = old_point; |
| 2784 | if (!whitespace (rl_line_buffer[rl_point])) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2785 | rl_forward_word (1, 0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2786 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2787 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2788 | } |
| 2789 | else |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2790 | { |
| 2791 | cleanup_expansion_error (); |
| 2792 | return 1; |
| 2793 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2796 | /* If FIGNORE is set, then don't match files with the given suffixes when |
| 2797 | completing filenames. If only one of the possibilities has an acceptable |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2798 | suffix, delete the others, else just return and let the completer |
| 2799 | signal an error. It is called by the completer when real |
| 2800 | completions are done on filenames by the completer's internal |
| 2801 | function, not for completion lists (M-?) and not on "other" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2802 | completion types, such as hostnames or commands. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2803 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2804 | static struct ignorevar fignore = |
| 2805 | { |
| 2806 | "FIGNORE", |
| 2807 | (struct ign *)0, |
| 2808 | 0, |
| 2809 | (char *)0, |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2810 | (sh_iv_item_func_t *) 0, |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2811 | }; |
| 2812 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2813 | static void |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2814 | _ignore_completion_names (names, name_func) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2815 | char **names; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2816 | sh_ignore_func_t *name_func; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2817 | { |
| 2818 | char **newnames; |
| 2819 | int idx, nidx; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2820 | char **oldnames; |
| 2821 | int oidx; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2822 | |
| 2823 | /* If there is only one completion, see if it is acceptable. If it is |
| 2824 | not, free it up. In any case, short-circuit and return. This is a |
| 2825 | special case because names[0] is not the prefix of the list of names |
| 2826 | if there is only one completion; it is the completion itself. */ |
| 2827 | if (names[1] == (char *)0) |
| 2828 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2829 | if (force_fignore) |
| 2830 | if ((*name_func) (names[0]) == 0) |
| 2831 | { |
| 2832 | free (names[0]); |
| 2833 | names[0] = (char *)NULL; |
| 2834 | } |
| 2835 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2836 | return; |
| 2837 | } |
| 2838 | |
| 2839 | /* Allocate space for array to hold list of pointers to matching |
| 2840 | filenames. The pointers are copied back to NAMES when done. */ |
| 2841 | for (nidx = 1; names[nidx]; nidx++) |
| 2842 | ; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2843 | newnames = strvec_create (nidx + 1); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2844 | |
| 2845 | if (force_fignore == 0) |
| 2846 | { |
| 2847 | oldnames = strvec_create (nidx - 1); |
| 2848 | oidx = 0; |
| 2849 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2850 | |
| 2851 | newnames[0] = names[0]; |
| 2852 | for (idx = nidx = 1; names[idx]; idx++) |
| 2853 | { |
| 2854 | if ((*name_func) (names[idx])) |
| 2855 | newnames[nidx++] = names[idx]; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2856 | else if (force_fignore == 0) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2857 | oldnames[oidx++] = names[idx]; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2858 | else |
| 2859 | free (names[idx]); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | newnames[nidx] = (char *)NULL; |
| 2863 | |
| 2864 | /* If none are acceptable then let the completer handle it. */ |
| 2865 | if (nidx == 1) |
| 2866 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2867 | if (force_fignore) |
| 2868 | { |
| 2869 | free (names[0]); |
| 2870 | names[0] = (char *)NULL; |
| 2871 | } |
| 2872 | else |
| 2873 | free (oldnames); |
| 2874 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2875 | free (newnames); |
| 2876 | return; |
| 2877 | } |
| 2878 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2879 | if (force_fignore == 0) |
| 2880 | { |
| 2881 | while (oidx) |
| 2882 | free (oldnames[--oidx]); |
| 2883 | free (oldnames); |
| 2884 | } |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2885 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2886 | /* If only one is acceptable, copy it to names[0] and return. */ |
| 2887 | if (nidx == 2) |
| 2888 | { |
| 2889 | free (names[0]); |
| 2890 | names[0] = newnames[1]; |
| 2891 | names[1] = (char *)NULL; |
| 2892 | free (newnames); |
| 2893 | return; |
| 2894 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2895 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2896 | /* Copy the acceptable names back to NAMES, set the new array end, |
| 2897 | and return. */ |
| 2898 | for (nidx = 1; newnames[nidx]; nidx++) |
| 2899 | names[nidx] = newnames[nidx]; |
| 2900 | names[nidx] = (char *)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2901 | free (newnames); |
| 2902 | } |
| 2903 | |
| 2904 | static int |
| 2905 | name_is_acceptable (name) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2906 | const char *name; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2907 | { |
| 2908 | struct ign *p; |
| 2909 | int nlen; |
| 2910 | |
| 2911 | for (nlen = strlen (name), p = fignore.ignores; p->val; p++) |
| 2912 | { |
| 2913 | if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len])) |
| 2914 | return (0); |
| 2915 | } |
| 2916 | |
| 2917 | return (1); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2920 | #if 0 |
| 2921 | static int |
| 2922 | ignore_dot_names (name) |
| 2923 | char *name; |
| 2924 | { |
| 2925 | return (name[0] != '.'); |
| 2926 | } |
| 2927 | #endif |
| 2928 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2929 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2930 | filename_completion_ignore (names) |
| 2931 | char **names; |
| 2932 | { |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2933 | #if 0 |
| 2934 | if (glob_dot_filenames == 0) |
| 2935 | _ignore_completion_names (names, ignore_dot_names); |
| 2936 | #endif |
| 2937 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2938 | setup_ignore_patterns (&fignore); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2939 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2940 | if (fignore.num_ignores == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2941 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2942 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2943 | _ignore_completion_names (names, name_is_acceptable); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2944 | |
| 2945 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2948 | /* Return 1 if NAME is a directory. NAME undergoes tilde expansion. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2949 | static int |
| 2950 | test_for_directory (name) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2951 | const char *name; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2952 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2953 | char *fn; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2954 | int r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2955 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2956 | fn = bash_tilde_expand (name, 0); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2957 | r = file_isdir (fn); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2958 | free (fn); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2959 | |
| 2960 | return (r); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | /* Remove files from NAMES, leaving directories. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2964 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2965 | bash_ignore_filenames (names) |
| 2966 | char **names; |
| 2967 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2968 | _ignore_completion_names (names, test_for_directory); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2969 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2972 | static int |
| 2973 | return_zero (name) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2974 | const char *name; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2975 | { |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2979 | static int |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2980 | bash_ignore_everything (names) |
| 2981 | char **names; |
| 2982 | { |
| 2983 | _ignore_completion_names (names, return_zero); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2984 | return 0; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2987 | /* Replace a tilde-prefix in VAL with a `~', assuming the user typed it. VAL |
| 2988 | is an expanded filename. DIRECTORY_PART is the tilde-prefix portion |
| 2989 | of the un-tilde-expanded version of VAL (what the user typed). */ |
| 2990 | static char * |
| 2991 | restore_tilde (val, directory_part) |
| 2992 | char *val, *directory_part; |
| 2993 | { |
| 2994 | int l, vl, dl2, xl; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 2995 | char *dh2, *expdir, *ret, *v; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2996 | |
| 2997 | vl = strlen (val); |
| 2998 | |
| 2999 | /* We need to duplicate the expansions readline performs on the directory |
| 3000 | portion before passing it to our completion function. */ |
| 3001 | dh2 = directory_part ? bash_dequote_filename (directory_part, 0) : 0; |
| 3002 | bash_directory_expansion (&dh2); |
| 3003 | dl2 = strlen (dh2); |
| 3004 | |
| 3005 | expdir = bash_tilde_expand (directory_part, 0); |
| 3006 | xl = strlen (expdir); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 3007 | if (*directory_part == '~' && STREQ (directory_part, expdir)) |
| 3008 | { |
| 3009 | /* tilde expansion failed, so what should we return? we use what the |
| 3010 | user typed. */ |
| 3011 | v = mbschr (val, '/'); |
| 3012 | vl = STRLEN (v); |
| 3013 | ret = (char *)xmalloc (xl + vl + 2); |
| 3014 | strcpy (ret, directory_part); |
| 3015 | if (v && *v) |
| 3016 | strcpy (ret + xl, v); |
| 3017 | |
| 3018 | free (dh2); |
| 3019 | free (expdir); |
| 3020 | |
| 3021 | return ret; |
| 3022 | } |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3023 | free (expdir); |
| 3024 | |
| 3025 | /* |
| 3026 | dh2 = unexpanded but dequoted tilde-prefix |
| 3027 | dl2 = length of tilde-prefix |
| 3028 | expdir = tilde-expanded tilde-prefix |
| 3029 | xl = length of expanded tilde-prefix |
| 3030 | l = length of remainder after tilde-prefix |
| 3031 | */ |
| 3032 | l = (vl - xl) + 1; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 3033 | if (l <= 0) |
| 3034 | { |
| 3035 | free (dh2); |
| 3036 | return (savestring (val)); /* XXX - just punt */ |
| 3037 | } |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3038 | |
| 3039 | ret = (char *)xmalloc (dl2 + 2 + l); |
| 3040 | strcpy (ret, dh2); |
| 3041 | strcpy (ret + dl2, val + xl); |
| 3042 | |
| 3043 | free (dh2); |
| 3044 | return (ret); |
| 3045 | } |
| 3046 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3047 | static char * |
| 3048 | maybe_restore_tilde (val, directory_part) |
| 3049 | char *val, *directory_part; |
| 3050 | { |
| 3051 | rl_icppfunc_t *save; |
| 3052 | char *ret; |
| 3053 | |
| 3054 | save = (dircomplete_expand == 0) ? save_directory_hook () : (rl_icppfunc_t *)0; |
| 3055 | ret = restore_tilde (val, directory_part); |
| 3056 | if (save) |
| 3057 | restore_directory_hook (save); |
| 3058 | return ret; |
| 3059 | } |
| 3060 | |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3061 | /* Simulate the expansions that will be performed by |
| 3062 | rl_filename_completion_function. This must be called with the address of |
| 3063 | a pointer to malloc'd memory. */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3064 | static void |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3065 | bash_directory_expansion (dirname) |
| 3066 | char **dirname; |
| 3067 | { |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3068 | char *d, *nd; |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3069 | |
| 3070 | d = savestring (*dirname); |
| 3071 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3072 | if ((rl_directory_rewrite_hook) && (*rl_directory_rewrite_hook) (&d)) |
| 3073 | { |
| 3074 | free (*dirname); |
| 3075 | *dirname = d; |
| 3076 | } |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3077 | else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&d)) |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3078 | { |
| 3079 | free (*dirname); |
| 3080 | *dirname = d; |
| 3081 | } |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3082 | else if (rl_completion_found_quote) |
| 3083 | { |
| 3084 | nd = bash_dequote_filename (d, rl_completion_quote_character); |
| 3085 | free (*dirname); |
| 3086 | free (d); |
| 3087 | *dirname = nd; |
| 3088 | } |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3089 | } |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3090 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3091 | /* If necessary, rewrite directory entry */ |
| 3092 | static char * |
| 3093 | bash_filename_rewrite_hook (fname, fnlen) |
| 3094 | char *fname; |
| 3095 | int fnlen; |
| 3096 | { |
| 3097 | char *conv; |
| 3098 | |
| 3099 | conv = fnx_fromfs (fname, fnlen); |
| 3100 | if (conv != fname) |
| 3101 | conv = savestring (conv); |
| 3102 | return conv; |
| 3103 | } |
| 3104 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3105 | /* Functions to save and restore the appropriate directory hook */ |
| 3106 | /* This is not static so the shopt code can call it */ |
| 3107 | void |
| 3108 | set_directory_hook () |
| 3109 | { |
| 3110 | if (dircomplete_expand) |
| 3111 | { |
| 3112 | rl_directory_completion_hook = bash_directory_completion_hook; |
| 3113 | rl_directory_rewrite_hook = (rl_icppfunc_t *)0; |
| 3114 | } |
| 3115 | else |
| 3116 | { |
| 3117 | rl_directory_rewrite_hook = bash_directory_completion_hook; |
| 3118 | rl_directory_completion_hook = (rl_icppfunc_t *)0; |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | static rl_icppfunc_t * |
| 3123 | save_directory_hook () |
| 3124 | { |
| 3125 | rl_icppfunc_t *ret; |
| 3126 | |
| 3127 | if (dircomplete_expand) |
| 3128 | { |
| 3129 | ret = rl_directory_completion_hook; |
| 3130 | rl_directory_completion_hook = (rl_icppfunc_t *)NULL; |
| 3131 | } |
| 3132 | else |
| 3133 | { |
| 3134 | ret = rl_directory_rewrite_hook; |
| 3135 | rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL; |
| 3136 | } |
| 3137 | |
| 3138 | return ret; |
| 3139 | } |
| 3140 | |
| 3141 | static void |
| 3142 | restore_directory_hook (hookf) |
| 3143 | rl_icppfunc_t *hookf; |
| 3144 | { |
| 3145 | if (dircomplete_expand) |
| 3146 | rl_directory_completion_hook = hookf; |
| 3147 | else |
| 3148 | rl_directory_rewrite_hook = hookf; |
| 3149 | } |
| 3150 | |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 3151 | /* Check whether not DIRNAME, with any trailing slash removed, exists. If |
| 3152 | SHOULD_DEQUOTE is non-zero, we dequote the directory name first. */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3153 | static int |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 3154 | directory_exists (dirname, should_dequote) |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3155 | const char *dirname; |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 3156 | int should_dequote; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3157 | { |
| 3158 | char *new_dirname; |
| 3159 | int dirlen, r; |
| 3160 | struct stat sb; |
| 3161 | |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 3162 | /* We save the string and chop the trailing slash because stat/lstat behave |
| 3163 | inconsistently if one is present. */ |
| 3164 | new_dirname = should_dequote ? bash_dequote_filename ((char *)dirname, rl_completion_quote_character) : savestring (dirname); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3165 | dirlen = STRLEN (new_dirname); |
| 3166 | if (new_dirname[dirlen - 1] == '/') |
| 3167 | new_dirname[dirlen - 1] = '\0'; |
| 3168 | #if defined (HAVE_LSTAT) |
| 3169 | r = lstat (new_dirname, &sb) == 0; |
| 3170 | #else |
| 3171 | r = stat (new_dirname, &sb) == 0; |
| 3172 | #endif |
| 3173 | free (new_dirname); |
| 3174 | return (r); |
| 3175 | } |
| 3176 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3177 | /* Expand a filename before the readline completion code passes it to stat(2). |
| 3178 | The filename will already have had tilde expansion performed. */ |
| 3179 | static int |
| 3180 | bash_filename_stat_hook (dirname) |
| 3181 | char **dirname; |
| 3182 | { |
| 3183 | char *local_dirname, *new_dirname, *t; |
| 3184 | int should_expand_dirname, return_value; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3185 | int global_nounset; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3186 | WORD_LIST *wl; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3187 | |
| 3188 | local_dirname = *dirname; |
| 3189 | should_expand_dirname = return_value = 0; |
| 3190 | if (t = mbschr (local_dirname, '$')) |
| 3191 | should_expand_dirname = '$'; |
| 3192 | else if (t = mbschr (local_dirname, '`')) /* XXX */ |
| 3193 | should_expand_dirname = '`'; |
| 3194 | |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 3195 | if (should_expand_dirname && directory_exists (local_dirname, 0)) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3196 | should_expand_dirname = 0; |
| 3197 | |
| 3198 | if (should_expand_dirname) |
| 3199 | { |
| 3200 | new_dirname = savestring (local_dirname); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3201 | /* no error messages, and expand_prompt_string doesn't longjmp so we don't |
| 3202 | have to worry about restoring this setting. */ |
| 3203 | global_nounset = unbound_vars_is_error; |
| 3204 | unbound_vars_is_error = 0; |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 3205 | wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB|W_NOPROCSUB|W_COMPLETE); /* does the right thing */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3206 | unbound_vars_is_error = global_nounset; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3207 | if (wl) |
| 3208 | { |
| 3209 | free (new_dirname); |
| 3210 | new_dirname = string_list (wl); |
| 3211 | /* Tell the completer we actually expanded something and change |
| 3212 | *dirname only if we expanded to something non-null -- stat |
| 3213 | behaves unpredictably when passed null or empty strings */ |
| 3214 | if (new_dirname && *new_dirname) |
| 3215 | { |
| 3216 | free (local_dirname); /* XXX */ |
| 3217 | local_dirname = *dirname = new_dirname; |
| 3218 | return_value = STREQ (local_dirname, *dirname) == 0; |
| 3219 | } |
| 3220 | else |
| 3221 | free (new_dirname); |
| 3222 | dispose_words (wl); |
| 3223 | } |
| 3224 | else |
| 3225 | free (new_dirname); |
| 3226 | } |
| 3227 | |
| 3228 | /* This is very similar to the code in bash_directory_completion_hook below, |
| 3229 | but without spelling correction and not worrying about whether or not |
| 3230 | we change relative pathnames. */ |
| 3231 | if (no_symbolic_links == 0 && (local_dirname[0] != '.' || local_dirname[1])) |
| 3232 | { |
| 3233 | char *temp1, *temp2; |
| 3234 | |
| 3235 | t = get_working_directory ("symlink-hook"); |
| 3236 | temp1 = make_absolute (local_dirname, t); |
| 3237 | free (t); |
| 3238 | temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS); |
| 3239 | |
| 3240 | /* If we can't canonicalize, bail. */ |
| 3241 | if (temp2 == 0) |
| 3242 | { |
| 3243 | free (temp1); |
| 3244 | return return_value; |
| 3245 | } |
| 3246 | |
| 3247 | free (local_dirname); |
| 3248 | *dirname = temp2; |
| 3249 | free (temp1); |
| 3250 | } |
| 3251 | |
| 3252 | return (return_value); |
| 3253 | } |
| 3254 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3255 | /* Handle symbolic link references and other directory name |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3256 | expansions while hacking completion. This should return 1 if it modifies |
| 3257 | the DIRNAME argument, 0 otherwise. It should make sure not to modify |
| 3258 | DIRNAME if it returns 0. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3259 | static int |
| 3260 | bash_directory_completion_hook (dirname) |
| 3261 | char **dirname; |
| 3262 | { |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3263 | char *local_dirname, *new_dirname, *t; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 3264 | int return_value, should_expand_dirname, nextch, closer; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3265 | WORD_LIST *wl; |
| 3266 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3267 | return_value = should_expand_dirname = nextch = closer = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3268 | local_dirname = *dirname; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3269 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3270 | if (t = mbschr (local_dirname, '$')) |
| 3271 | { |
| 3272 | should_expand_dirname = '$'; |
| 3273 | nextch = t[1]; |
| 3274 | /* Deliberately does not handle the deprecated $[...] arithmetic |
| 3275 | expansion syntax */ |
| 3276 | if (nextch == '(') |
| 3277 | closer = ')'; |
| 3278 | else if (nextch == '{') |
| 3279 | closer = '}'; |
| 3280 | else |
| 3281 | nextch = 0; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 3282 | |
| 3283 | if (closer) |
| 3284 | { |
| 3285 | int p; |
| 3286 | char delims[2]; |
| 3287 | |
| 3288 | delims[0] = closer; delims[1] = 0; |
| 3289 | p = skip_to_delim (t, 1, delims, SD_NOJMP|SD_COMPLETE); |
| 3290 | if (t[p] != closer) |
| 3291 | should_expand_dirname = 0; |
| 3292 | } |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3293 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3294 | else if (local_dirname[0] == '~') |
| 3295 | should_expand_dirname = '~'; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3296 | else |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3297 | { |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3298 | t = mbschr (local_dirname, '`'); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3299 | if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0) |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3300 | should_expand_dirname = '`'; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3301 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3302 | |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 3303 | if (should_expand_dirname && directory_exists (local_dirname, 1)) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3304 | should_expand_dirname = 0; |
| 3305 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3306 | if (should_expand_dirname) |
| 3307 | { |
| 3308 | new_dirname = savestring (local_dirname); |
| Chet Ramey | 4f747ed | 2017-01-20 11:47:55 -0500 | [diff] [blame] | 3309 | wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB|W_NOPROCSUB|W_COMPLETE); /* does the right thing */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3310 | if (wl) |
| 3311 | { |
| 3312 | *dirname = string_list (wl); |
| 3313 | /* Tell the completer to replace the directory name only if we |
| 3314 | actually expanded something. */ |
| 3315 | return_value = STREQ (local_dirname, *dirname) == 0; |
| 3316 | free (local_dirname); |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3317 | free (new_dirname); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3318 | dispose_words (wl); |
| 3319 | local_dirname = *dirname; |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3320 | /* XXX - change rl_filename_quote_characters here based on |
| 3321 | should_expand_dirname/nextch/closer. This is the only place |
| 3322 | custom_filename_quote_characters is modified. */ |
| 3323 | if (rl_filename_quote_characters && *rl_filename_quote_characters) |
| 3324 | { |
| 3325 | int i, j, c; |
| 3326 | i = strlen (default_filename_quote_characters); |
| 3327 | custom_filename_quote_characters = xrealloc (custom_filename_quote_characters, i+1); |
| 3328 | for (i = j = 0; c = default_filename_quote_characters[i]; i++) |
| 3329 | { |
| 3330 | if (c == should_expand_dirname || c == nextch || c == closer) |
| 3331 | continue; |
| 3332 | custom_filename_quote_characters[j++] = c; |
| 3333 | } |
| 3334 | custom_filename_quote_characters[j] = '\0'; |
| 3335 | rl_filename_quote_characters = custom_filename_quote_characters; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3336 | set_filename_bstab (rl_filename_quote_characters); |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3337 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3338 | } |
| 3339 | else |
| 3340 | { |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 3341 | free (new_dirname); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3342 | free (local_dirname); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3343 | *dirname = (char *)xmalloc (1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3344 | **dirname = '\0'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3345 | return 1; |
| 3346 | } |
| 3347 | } |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3348 | else |
| 3349 | { |
| 3350 | /* Dequote the filename even if we don't expand it. */ |
| 3351 | new_dirname = bash_dequote_filename (local_dirname, rl_completion_quote_character); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3352 | return_value = STREQ (local_dirname, new_dirname) == 0; |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3353 | free (local_dirname); |
| 3354 | local_dirname = *dirname = new_dirname; |
| 3355 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3356 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3357 | /* no_symbolic_links == 0 -> use (default) logical view of the file system. |
| 3358 | local_dirname[0] == '.' && local_dirname[1] == '/' means files in the |
| 3359 | current directory (./). |
| 3360 | local_dirname[0] == '.' && local_dirname[1] == 0 means relative pathnames |
| 3361 | in the current directory (e.g., lib/sh). |
| 3362 | XXX - should we do spelling correction on these? */ |
| 3363 | |
| 3364 | /* This is test as it was in bash-4.2: skip relative pathnames in current |
| 3365 | directory. Change test to |
| 3366 | (local_dirname[0] != '.' || (local_dirname[1] && local_dirname[1] != '/')) |
| 3367 | if we want to skip paths beginning with ./ also. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3368 | if (no_symbolic_links == 0 && (local_dirname[0] != '.' || local_dirname[1])) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3369 | { |
| 3370 | char *temp1, *temp2; |
| 3371 | int len1, len2; |
| 3372 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3373 | /* If we have a relative path |
| 3374 | (local_dirname[0] != '/' && local_dirname[0] != '.') |
| 3375 | that is canonical after appending it to the current directory, then |
| 3376 | temp1 = temp2+'/' |
| 3377 | That is, |
| 3378 | strcmp (temp1, temp2) == 0 |
| 3379 | after adding a slash to temp2 below. It should be safe to not |
| 3380 | change those. |
| 3381 | */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3382 | t = get_working_directory ("symlink-hook"); |
| 3383 | temp1 = make_absolute (local_dirname, t); |
| 3384 | free (t); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3385 | temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3386 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3387 | /* Try spelling correction if initial canonicalization fails. Make |
| 3388 | sure we are set to replace the directory name with the results so |
| 3389 | subsequent directory checks don't fail. */ |
| 3390 | if (temp2 == 0 && dircomplete_spelling && dircomplete_expand) |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3391 | { |
| 3392 | temp2 = dirspell (temp1); |
| 3393 | if (temp2) |
| 3394 | { |
| 3395 | free (temp1); |
| 3396 | temp1 = temp2; |
| 3397 | temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3398 | return_value |= temp2 != 0; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3399 | } |
| 3400 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3401 | /* If we can't canonicalize, bail. */ |
| 3402 | if (temp2 == 0) |
| 3403 | { |
| 3404 | free (temp1); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3405 | return return_value; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3406 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3407 | len1 = strlen (temp1); |
| 3408 | if (temp1[len1 - 1] == '/') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3409 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3410 | len2 = strlen (temp2); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3411 | if (len2 > 2) /* don't append `/' to `/' or `//' */ |
| 3412 | { |
| 3413 | temp2 = (char *)xrealloc (temp2, len2 + 2); |
| 3414 | temp2[len2] = '/'; |
| 3415 | temp2[len2 + 1] = '\0'; |
| 3416 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3417 | } |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3418 | |
| 3419 | /* dircomplete_expand_relpath == 0 means we want to leave relative |
| 3420 | pathnames that are unchanged by canonicalization alone. |
| 3421 | *local_dirname != '/' && *local_dirname != '.' == relative pathname |
| 3422 | (consistent with general.c:absolute_pathname()) |
| 3423 | temp1 == temp2 (after appending a slash to temp2) means the pathname |
| 3424 | is not changed by canonicalization as described above. */ |
| 3425 | if (dircomplete_expand_relpath || ((local_dirname[0] != '/' && local_dirname[0] != '.') && STREQ (temp1, temp2) == 0)) |
| 3426 | return_value |= STREQ (local_dirname, temp2) == 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3427 | free (local_dirname); |
| 3428 | *dirname = temp2; |
| 3429 | free (temp1); |
| 3430 | } |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3431 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3432 | return (return_value); |
| 3433 | } |
| 3434 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3435 | static char **history_completion_array = (char **)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3436 | static int harry_size; |
| 3437 | static int harry_len; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3438 | |
| 3439 | static void |
| 3440 | build_history_completion_array () |
| 3441 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3442 | register int i, j; |
| 3443 | HIST_ENTRY **hlist; |
| 3444 | char **tokens; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3445 | |
| 3446 | /* First, clear out the current dynamic history completion list. */ |
| 3447 | if (harry_size) |
| 3448 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3449 | strvec_dispose (history_completion_array); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3450 | history_completion_array = (char **)NULL; |
| 3451 | harry_size = 0; |
| 3452 | harry_len = 0; |
| 3453 | } |
| 3454 | |
| 3455 | /* Next, grovel each line of history, making each shell-sized token |
| 3456 | a separate entry in the history_completion_array. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3457 | hlist = history_list (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3458 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3459 | if (hlist) |
| 3460 | { |
| 3461 | for (i = 0; hlist[i]; i++) |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3462 | ; |
| 3463 | for ( --i; i >= 0; i--) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3464 | { |
| 3465 | /* Separate each token, and place into an array. */ |
| 3466 | tokens = history_tokenize (hlist[i]->line); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3467 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3468 | for (j = 0; tokens && tokens[j]; j++) |
| 3469 | { |
| 3470 | if (harry_len + 2 > harry_size) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3471 | history_completion_array = strvec_resize (history_completion_array, harry_size += 10); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3472 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3473 | history_completion_array[harry_len++] = tokens[j]; |
| 3474 | history_completion_array[harry_len] = (char *)NULL; |
| 3475 | } |
| 3476 | free (tokens); |
| 3477 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3478 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3479 | /* Sort the complete list of tokens. */ |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3480 | if (dabbrev_expand_active == 0) |
| 3481 | qsort (history_completion_array, harry_len, sizeof (char *), (QSFUNC *)strvec_strcmp); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3482 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | static char * |
| 3486 | history_completion_generator (hint_text, state) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3487 | const char *hint_text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3488 | int state; |
| 3489 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3490 | static int local_index, len; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3491 | static const char *text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3492 | |
| 3493 | /* If this is the first call to the generator, then initialize the |
| 3494 | list of strings to complete over. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3495 | if (state == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3496 | { |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3497 | if (dabbrev_expand_active) /* This is kind of messy */ |
| 3498 | rl_completion_suppress_append = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3499 | local_index = 0; |
| 3500 | build_history_completion_array (); |
| 3501 | text = hint_text; |
| 3502 | len = strlen (text); |
| 3503 | } |
| 3504 | |
| 3505 | while (history_completion_array && history_completion_array[local_index]) |
| 3506 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 3507 | /* XXX - should this use completion-ignore-case? */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3508 | if (strncmp (text, history_completion_array[local_index++], len) == 0) |
| 3509 | return (savestring (history_completion_array[local_index - 1])); |
| 3510 | } |
| 3511 | return ((char *)NULL); |
| 3512 | } |
| 3513 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3514 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3515 | dynamic_complete_history (count, key) |
| 3516 | int count, key; |
| 3517 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3518 | int r; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3519 | rl_compentry_func_t *orig_func; |
| 3520 | rl_completion_func_t *orig_attempt_func; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3521 | rl_compignore_func_t *orig_ignore_func; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3522 | |
| 3523 | orig_func = rl_completion_entry_function; |
| 3524 | orig_attempt_func = rl_attempted_completion_function; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3525 | orig_ignore_func = rl_ignore_some_completions_function; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3526 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3527 | rl_completion_entry_function = history_completion_generator; |
| 3528 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3529 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3530 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3531 | /* XXX - use rl_completion_mode here? */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3532 | if (rl_last_func == dynamic_complete_history) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3533 | r = rl_complete_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3534 | else |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3535 | r = rl_complete_internal (TAB); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3536 | |
| 3537 | rl_completion_entry_function = orig_func; |
| 3538 | rl_attempted_completion_function = orig_attempt_func; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3539 | rl_ignore_some_completions_function = orig_ignore_func; |
| 3540 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3541 | return r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3544 | static int |
| 3545 | bash_dabbrev_expand (count, key) |
| 3546 | int count, key; |
| 3547 | { |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3548 | int r, orig_suppress, orig_sort; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3549 | rl_compentry_func_t *orig_func; |
| 3550 | rl_completion_func_t *orig_attempt_func; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3551 | rl_compignore_func_t *orig_ignore_func; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3552 | |
| 3553 | orig_func = rl_menu_completion_entry_function; |
| 3554 | orig_attempt_func = rl_attempted_completion_function; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3555 | orig_ignore_func = rl_ignore_some_completions_function; |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3556 | orig_suppress = rl_completion_suppress_append; |
| 3557 | orig_sort = rl_sort_completion_matches; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3558 | |
| 3559 | rl_menu_completion_entry_function = history_completion_generator; |
| 3560 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3561 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3562 | rl_filename_completion_desired = 0; |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3563 | rl_completion_suppress_append = 1; |
| 3564 | rl_sort_completion_matches = 0; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3565 | |
| 3566 | /* XXX - use rl_completion_mode here? */ |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3567 | dabbrev_expand_active = 1; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3568 | if (rl_last_func == bash_dabbrev_expand) |
| 3569 | rl_last_func = rl_menu_complete; |
| 3570 | r = rl_menu_complete (count, key); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3571 | dabbrev_expand_active = 0; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3572 | |
| 3573 | rl_last_func = bash_dabbrev_expand; |
| 3574 | rl_menu_completion_entry_function = orig_func; |
| 3575 | rl_attempted_completion_function = orig_attempt_func; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3576 | rl_ignore_some_completions_function = orig_ignore_func; |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3577 | rl_completion_suppress_append = orig_suppress; |
| 3578 | rl_sort_completion_matches = orig_sort; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3579 | |
| 3580 | return r; |
| 3581 | } |
| 3582 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3583 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3584 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3585 | bash_complete_username (ignore, ignore2) |
| 3586 | int ignore, ignore2; |
| 3587 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3588 | return bash_complete_username_internal (rl_completion_mode (bash_complete_username)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3591 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3592 | bash_possible_username_completions (ignore, ignore2) |
| 3593 | int ignore, ignore2; |
| 3594 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3595 | return bash_complete_username_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3598 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3599 | bash_complete_username_internal (what_to_do) |
| 3600 | int what_to_do; |
| 3601 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3602 | return bash_specific_completion (what_to_do, rl_username_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3605 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3606 | bash_complete_filename (ignore, ignore2) |
| 3607 | int ignore, ignore2; |
| 3608 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3609 | return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3612 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3613 | bash_possible_filename_completions (ignore, ignore2) |
| 3614 | int ignore, ignore2; |
| 3615 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3616 | return bash_complete_filename_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3619 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3620 | bash_complete_filename_internal (what_to_do) |
| 3621 | int what_to_do; |
| 3622 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3623 | rl_compentry_func_t *orig_func; |
| 3624 | rl_completion_func_t *orig_attempt_func; |
| 3625 | rl_icppfunc_t *orig_dir_func; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3626 | rl_compignore_func_t *orig_ignore_func; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3627 | /*const*/ char *orig_rl_completer_word_break_characters; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3628 | int r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3629 | |
| 3630 | orig_func = rl_completion_entry_function; |
| 3631 | orig_attempt_func = rl_attempted_completion_function; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3632 | orig_ignore_func = rl_ignore_some_completions_function; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3633 | orig_rl_completer_word_break_characters = rl_completer_word_break_characters; |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3634 | |
| 3635 | orig_dir_func = save_directory_hook (); |
| 3636 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3637 | rl_completion_entry_function = rl_filename_completion_function; |
| 3638 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3639 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3640 | rl_completer_word_break_characters = " \t\n\"\'"; |
| 3641 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3642 | r = rl_complete_internal (what_to_do); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3643 | |
| 3644 | rl_completion_entry_function = orig_func; |
| 3645 | rl_attempted_completion_function = orig_attempt_func; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3646 | rl_ignore_some_completions_function = orig_ignore_func; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3647 | rl_completer_word_break_characters = orig_rl_completer_word_break_characters; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3648 | |
| Chet Ramey | 16b2d7f | 2012-05-31 15:11:45 -0400 | [diff] [blame] | 3649 | restore_directory_hook (orig_dir_func); |
| 3650 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3651 | return r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3654 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3655 | bash_complete_hostname (ignore, ignore2) |
| 3656 | int ignore, ignore2; |
| 3657 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3658 | return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3661 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3662 | bash_possible_hostname_completions (ignore, ignore2) |
| 3663 | int ignore, ignore2; |
| 3664 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3665 | return bash_complete_hostname_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3668 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3669 | bash_complete_variable (ignore, ignore2) |
| 3670 | int ignore, ignore2; |
| 3671 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3672 | return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3675 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3676 | bash_possible_variable_completions (ignore, ignore2) |
| 3677 | int ignore, ignore2; |
| 3678 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3679 | return bash_complete_variable_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3682 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3683 | bash_complete_command (ignore, ignore2) |
| 3684 | int ignore, ignore2; |
| 3685 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3686 | return bash_complete_command_internal (rl_completion_mode (bash_complete_command)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3689 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3690 | bash_possible_command_completions (ignore, ignore2) |
| 3691 | int ignore, ignore2; |
| 3692 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3693 | return bash_complete_command_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3696 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3697 | bash_complete_hostname_internal (what_to_do) |
| 3698 | int what_to_do; |
| 3699 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3700 | return bash_specific_completion (what_to_do, hostname_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3703 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3704 | bash_complete_variable_internal (what_to_do) |
| 3705 | int what_to_do; |
| 3706 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3707 | return bash_specific_completion (what_to_do, variable_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3710 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3711 | bash_complete_command_internal (what_to_do) |
| 3712 | int what_to_do; |
| 3713 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3714 | return bash_specific_completion (what_to_do, command_word_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
| Chet Ramey | 4d2e315 | 2019-01-18 15:12:37 -0500 | [diff] [blame^] | 3717 | static int |
| 3718 | completion_glob_pattern (string) |
| 3719 | char *string; |
| 3720 | { |
| 3721 | register int c; |
| 3722 | char *send; |
| 3723 | int open; |
| 3724 | |
| 3725 | DECLARE_MBSTATE; |
| 3726 | |
| 3727 | open = 0; |
| 3728 | send = string + strlen (string); |
| 3729 | |
| 3730 | while (c = *string++) |
| 3731 | { |
| 3732 | switch (c) |
| 3733 | { |
| 3734 | case '?': |
| 3735 | case '*': |
| 3736 | return (1); |
| 3737 | |
| 3738 | case '[': |
| 3739 | open++; |
| 3740 | continue; |
| 3741 | |
| 3742 | case ']': |
| 3743 | if (open) |
| 3744 | return (1); |
| 3745 | continue; |
| 3746 | |
| 3747 | case '+': |
| 3748 | case '@': |
| 3749 | case '!': |
| 3750 | if (*string == '(') /*)*/ |
| 3751 | return (1); |
| 3752 | continue; |
| 3753 | |
| 3754 | case '\\': |
| 3755 | if (*string == 0) |
| 3756 | return (0); |
| 3757 | } |
| 3758 | |
| 3759 | /* Advance one fewer byte than an entire multibyte character to |
| 3760 | account for the auto-increment in the loop above. */ |
| 3761 | #ifdef HANDLE_MULTIBYTE |
| 3762 | string--; |
| 3763 | ADVANCE_CHAR_P (string, send - string); |
| 3764 | string++; |
| 3765 | #else |
| 3766 | ADVANCE_CHAR_P (string, send - string); |
| 3767 | #endif |
| 3768 | } |
| 3769 | return (0); |
| 3770 | } |
| 3771 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3772 | static char *globtext; |
| 3773 | static char *globorig; |
| 3774 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3775 | static char * |
| 3776 | glob_complete_word (text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3777 | const char *text; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3778 | int state; |
| 3779 | { |
| 3780 | static char **matches = (char **)NULL; |
| 3781 | static int ind; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3782 | int glen; |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3783 | char *ret, *ttext; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3784 | |
| 3785 | if (state == 0) |
| 3786 | { |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 3787 | rl_filename_completion_desired = 1; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3788 | FREE (matches); |
| 3789 | if (globorig != globtext) |
| 3790 | FREE (globorig); |
| 3791 | FREE (globtext); |
| 3792 | |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3793 | ttext = bash_tilde_expand (text, 0); |
| 3794 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3795 | if (rl_explicit_arg) |
| 3796 | { |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3797 | globorig = savestring (ttext); |
| 3798 | glen = strlen (ttext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3799 | globtext = (char *)xmalloc (glen + 2); |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3800 | strcpy (globtext, ttext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3801 | globtext[glen] = '*'; |
| 3802 | globtext[glen+1] = '\0'; |
| 3803 | } |
| 3804 | else |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 3805 | globtext = globorig = savestring (ttext); |
| 3806 | |
| 3807 | if (ttext != text) |
| 3808 | free (ttext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3809 | |
| 3810 | matches = shell_glob_filename (globtext); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3811 | if (GLOB_FAILED (matches)) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3812 | matches = (char **)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3813 | ind = 0; |
| 3814 | } |
| 3815 | |
| 3816 | ret = matches ? matches[ind] : (char *)NULL; |
| 3817 | ind++; |
| 3818 | return ret; |
| 3819 | } |
| 3820 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3821 | static int |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3822 | bash_glob_completion_internal (what_to_do) |
| 3823 | int what_to_do; |
| 3824 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3825 | return bash_specific_completion (what_to_do, glob_complete_word); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3828 | /* A special quoting function so we don't end up quoting globbing characters |
| 3829 | in the word if there are no matches or multiple matches. */ |
| 3830 | static char * |
| 3831 | bash_glob_quote_filename (s, rtype, qcp) |
| 3832 | char *s; |
| 3833 | int rtype; |
| 3834 | char *qcp; |
| 3835 | { |
| 3836 | if (globorig && qcp && *qcp == '\0' && STREQ (s, globorig)) |
| 3837 | return (savestring (s)); |
| 3838 | else |
| 3839 | return (bash_quote_filename (s, rtype, qcp)); |
| 3840 | } |
| 3841 | |
| 3842 | static int |
| 3843 | bash_glob_complete_word (count, key) |
| 3844 | int count, key; |
| 3845 | { |
| 3846 | int r; |
| 3847 | rl_quote_func_t *orig_quoting_function; |
| 3848 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3849 | if (rl_editing_mode == EMACS_EDITING_MODE) |
| 3850 | rl_explicit_arg = 1; /* force `*' append */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3851 | orig_quoting_function = rl_filename_quoting_function; |
| 3852 | rl_filename_quoting_function = bash_glob_quote_filename; |
| 3853 | |
| 3854 | r = bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word)); |
| 3855 | |
| 3856 | rl_filename_quoting_function = orig_quoting_function; |
| 3857 | return r; |
| 3858 | } |
| 3859 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3860 | static int |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3861 | bash_glob_expand_word (count, key) |
| 3862 | int count, key; |
| 3863 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3864 | return bash_glob_completion_internal ('*'); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3867 | static int |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3868 | bash_glob_list_expansions (count, key) |
| 3869 | int count, key; |
| 3870 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3871 | return bash_glob_completion_internal ('?'); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3874 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3875 | bash_specific_completion (what_to_do, generator) |
| 3876 | int what_to_do; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3877 | rl_compentry_func_t *generator; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3878 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3879 | rl_compentry_func_t *orig_func; |
| 3880 | rl_completion_func_t *orig_attempt_func; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3881 | rl_compignore_func_t *orig_ignore_func; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3882 | int r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3883 | |
| 3884 | orig_func = rl_completion_entry_function; |
| 3885 | orig_attempt_func = rl_attempted_completion_function; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3886 | orig_ignore_func = rl_ignore_some_completions_function; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3887 | rl_completion_entry_function = generator; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3888 | rl_attempted_completion_function = NULL; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3889 | rl_ignore_some_completions_function = orig_ignore_func; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3890 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3891 | r = rl_complete_internal (what_to_do); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3892 | |
| 3893 | rl_completion_entry_function = orig_func; |
| 3894 | rl_attempted_completion_function = orig_attempt_func; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3895 | rl_ignore_some_completions_function = orig_ignore_func; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3896 | |
| 3897 | return r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3901 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3902 | #if defined (VI_MODE) |
| 3903 | /* Completion, from vi mode's point of view. This is a modified version of |
| 3904 | rl_vi_complete which uses the bash globbing code to implement what POSIX |
| 3905 | specifies, which is to append a `*' and attempt filename generation (which |
| 3906 | has the side effect of expanding any globbing characters in the word). */ |
| 3907 | static int |
| 3908 | bash_vi_complete (count, key) |
| 3909 | int count, key; |
| 3910 | { |
| 3911 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| 3912 | int p, r; |
| 3913 | char *t; |
| 3914 | |
| 3915 | if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point]))) |
| 3916 | { |
| 3917 | if (!whitespace (rl_line_buffer[rl_point + 1])) |
| 3918 | rl_vi_end_word (1, 'E'); |
| 3919 | rl_point++; |
| 3920 | } |
| 3921 | |
| 3922 | /* Find boundaries of current word, according to vi definition of a |
| 3923 | `bigword'. */ |
| 3924 | t = 0; |
| 3925 | if (rl_point > 0) |
| 3926 | { |
| 3927 | p = rl_point; |
| 3928 | rl_vi_bWord (1, 'B'); |
| 3929 | r = rl_point; |
| 3930 | rl_point = p; |
| 3931 | p = r; |
| 3932 | |
| 3933 | t = substring (rl_line_buffer, p, rl_point); |
| 3934 | } |
| 3935 | |
| Chet Ramey | 4d2e315 | 2019-01-18 15:12:37 -0500 | [diff] [blame^] | 3936 | if (t && completion_glob_pattern (t) == 0) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3937 | rl_explicit_arg = 1; /* XXX - force glob_complete_word to append `*' */ |
| 3938 | FREE (t); |
| 3939 | |
| 3940 | if (key == '*') /* Expansion and replacement. */ |
| 3941 | r = bash_glob_expand_word (count, key); |
| 3942 | else if (key == '=') /* List possible completions. */ |
| 3943 | r = bash_glob_list_expansions (count, key); |
| 3944 | else if (key == '\\') /* Standard completion */ |
| 3945 | r = bash_glob_complete_word (count, key); |
| 3946 | else |
| 3947 | r = rl_complete (0, key); |
| 3948 | |
| 3949 | if (key == '*' || key == '\\') |
| 3950 | rl_vi_start_inserting (key, 1, 1); |
| 3951 | |
| 3952 | return (r); |
| 3953 | #else |
| 3954 | return rl_vi_complete (count, key); |
| 3955 | #endif /* !SPECIFIC_COMPLETION_FUNCTIONS */ |
| 3956 | } |
| 3957 | #endif /* VI_MODE */ |
| 3958 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3959 | /* Filename quoting for completion. */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3960 | /* A function to strip unquoted quote characters (single quotes, double |
| 3961 | quotes, and backslashes). It allows single quotes to appear |
| 3962 | within double quotes, and vice versa. It should be smarter. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3963 | static char * |
| 3964 | bash_dequote_filename (text, quote_char) |
| 3965 | char *text; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3966 | int quote_char; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3967 | { |
| 3968 | char *ret, *p, *r; |
| 3969 | int l, quoted; |
| 3970 | |
| 3971 | l = strlen (text); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3972 | ret = (char *)xmalloc (l + 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3973 | for (quoted = quote_char, p = text, r = ret; p && *p; p++) |
| 3974 | { |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3975 | /* Allow backslash-escaped characters to pass through unscathed. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3976 | if (*p == '\\') |
| 3977 | { |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3978 | /* Backslashes are preserved within single quotes. */ |
| 3979 | if (quoted == '\'') |
| 3980 | *r++ = *p; |
| 3981 | /* Backslashes are preserved within double quotes unless the |
| 3982 | character is one that is defined to be escaped */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 3983 | else if (quoted == '"' && ((sh_syntaxtab[(unsigned char)p[1]] & CBSDQUOTE) == 0)) |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3984 | *r++ = *p; |
| 3985 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3986 | *r++ = *++p; |
| 3987 | if (*p == '\0') |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3988 | return ret; /* XXX - was break; */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3989 | continue; |
| 3990 | } |
| 3991 | /* Close quote. */ |
| 3992 | if (quoted && *p == quoted) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3993 | { |
| 3994 | quoted = 0; |
| 3995 | continue; |
| 3996 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3997 | /* Open quote. */ |
| 3998 | if (quoted == 0 && (*p == '\'' || *p == '"')) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3999 | { |
| 4000 | quoted = *p; |
| 4001 | continue; |
| 4002 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4003 | *r++ = *p; |
| 4004 | } |
| 4005 | *r = '\0'; |
| 4006 | return ret; |
| 4007 | } |
| 4008 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4009 | /* Quote characters that the readline completion code would treat as |
| 4010 | word break characters with backslashes. Pass backslash-quoted |
| 4011 | characters through without examination. */ |
| 4012 | static char * |
| 4013 | quote_word_break_chars (text) |
| 4014 | char *text; |
| 4015 | { |
| 4016 | char *ret, *r, *s; |
| 4017 | int l; |
| 4018 | |
| 4019 | l = strlen (text); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4020 | ret = (char *)xmalloc ((2 * l) + 1); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4021 | for (s = text, r = ret; *s; s++) |
| 4022 | { |
| 4023 | /* Pass backslash-quoted characters through, including the backslash. */ |
| 4024 | if (*s == '\\') |
| 4025 | { |
| 4026 | *r++ = '\\'; |
| 4027 | *r++ = *++s; |
| 4028 | if (*s == '\0') |
| 4029 | break; |
| 4030 | continue; |
| 4031 | } |
| 4032 | /* OK, we have an unquoted character. Check its presence in |
| 4033 | rl_completer_word_break_characters. */ |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4034 | if (mbschr (rl_completer_word_break_characters, *s)) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4035 | *r++ = '\\'; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4036 | /* XXX -- check for standalone tildes here and backslash-quote them */ |
| 4037 | if (s == text && *s == '~' && file_exists (text)) |
| 4038 | *r++ = '\\'; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4039 | *r++ = *s; |
| 4040 | } |
| 4041 | *r = '\0'; |
| 4042 | return ret; |
| 4043 | } |
| 4044 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4045 | /* Use characters in STRING to populate the table of characters that should |
| 4046 | be backslash-quoted. The table will be used for sh_backslash_quote from |
| 4047 | this file. */ |
| 4048 | static void |
| 4049 | set_filename_bstab (string) |
| 4050 | const char *string; |
| 4051 | { |
| 4052 | const char *s; |
| 4053 | |
| 4054 | memset (filename_bstab, 0, sizeof (filename_bstab)); |
| 4055 | for (s = string; s && *s; s++) |
| 4056 | filename_bstab[*s] = 1; |
| 4057 | } |
| 4058 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4059 | /* Quote a filename using double quotes, single quotes, or backslashes |
| 4060 | depending on the value of completion_quoting_style. If we're |
| 4061 | completing using backslashes, we need to quote some additional |
| 4062 | characters (those that readline treats as word breaks), so we call |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4063 | quote_word_break_chars on the result. This returns newly-allocated |
| 4064 | memory. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4065 | static char * |
| 4066 | bash_quote_filename (s, rtype, qcp) |
| 4067 | char *s; |
| 4068 | int rtype; |
| 4069 | char *qcp; |
| 4070 | { |
| 4071 | char *rtext, *mtext, *ret; |
| 4072 | int rlen, cs; |
| 4073 | |
| 4074 | rtext = (char *)NULL; |
| 4075 | |
| 4076 | /* If RTYPE == MULT_MATCH, it means that there is |
| 4077 | more than one match. In this case, we do not add |
| 4078 | the closing quote or attempt to perform tilde |
| 4079 | expansion. If RTYPE == SINGLE_MATCH, we try |
| 4080 | to perform tilde expansion, because single and double |
| 4081 | quotes inhibit tilde expansion by the shell. */ |
| 4082 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4083 | cs = completion_quoting_style; |
| 4084 | /* Might need to modify the default completion style based on *qcp, |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4085 | since it's set to any user-provided opening quote. We also change |
| 4086 | to single-quoting if there is no user-provided opening quote and |
| 4087 | the word being completed contains newlines, since those are not |
| 4088 | quoted correctly using backslashes (a backslash-newline pair is |
| 4089 | special to the shell parser). */ |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4090 | if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && mbschr (s, '\n')) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4091 | cs = COMPLETE_SQUOTE; |
| 4092 | else if (*qcp == '"') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4093 | cs = COMPLETE_DQUOTE; |
| 4094 | else if (*qcp == '\'') |
| 4095 | cs = COMPLETE_SQUOTE; |
| 4096 | #if defined (BANG_HISTORY) |
| 4097 | else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE && |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4098 | history_expansion_inhibited == 0 && mbschr (s, '!')) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4099 | cs = COMPLETE_BSQUOTE; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4100 | |
| 4101 | if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE && |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4102 | history_expansion_inhibited == 0 && mbschr (s, '!')) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4103 | { |
| 4104 | cs = COMPLETE_BSQUOTE; |
| 4105 | *qcp = '\0'; |
| 4106 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4107 | #endif |
| 4108 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4109 | /* Don't tilde-expand backslash-quoted filenames, since only single and |
| 4110 | double quotes inhibit tilde expansion. */ |
| 4111 | mtext = s; |
| 4112 | if (mtext[0] == '~' && rtype == SINGLE_MATCH && cs != COMPLETE_BSQUOTE) |
| 4113 | mtext = bash_tilde_expand (s, 0); |
| 4114 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4115 | switch (cs) |
| 4116 | { |
| 4117 | case COMPLETE_DQUOTE: |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4118 | rtext = sh_double_quote (mtext); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4119 | break; |
| 4120 | case COMPLETE_SQUOTE: |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4121 | rtext = sh_single_quote (mtext); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4122 | break; |
| 4123 | case COMPLETE_BSQUOTE: |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4124 | rtext = sh_backslash_quote (mtext, complete_fullquote ? 0 : filename_bstab, 0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4125 | break; |
| 4126 | } |
| 4127 | |
| 4128 | if (mtext != s) |
| 4129 | free (mtext); |
| 4130 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 4131 | /* We may need to quote additional characters: those that readline treats |
| 4132 | as word breaks that are not quoted by backslash_quote. */ |
| 4133 | if (rtext && cs == COMPLETE_BSQUOTE) |
| 4134 | { |
| 4135 | mtext = quote_word_break_chars (rtext); |
| 4136 | free (rtext); |
| 4137 | rtext = mtext; |
| 4138 | } |
| 4139 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4140 | /* Leave the opening quote intact. The readline completion code takes |
| 4141 | care of avoiding doubled opening quotes. */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4142 | if (rtext) |
| 4143 | { |
| 4144 | rlen = strlen (rtext); |
| 4145 | ret = (char *)xmalloc (rlen + 1); |
| 4146 | strcpy (ret, rtext); |
| 4147 | } |
| 4148 | else |
| 4149 | { |
| 4150 | ret = (char *)xmalloc (rlen = 1); |
| 4151 | ret[0] = '\0'; |
| 4152 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4153 | |
| 4154 | /* If there are multiple matches, cut off the closing quote. */ |
| 4155 | if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE) |
| 4156 | ret[rlen - 1] = '\0'; |
| 4157 | free (rtext); |
| 4158 | return ret; |
| 4159 | } |
| 4160 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4161 | /* Support for binding readline key sequences to Unix commands. */ |
| 4162 | static Keymap cmd_xmap; |
| 4163 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4164 | #ifdef _MINIX |
| 4165 | static void |
| 4166 | #else |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4167 | static int |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4168 | #endif |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4169 | putx(c) |
| 4170 | int c; |
| 4171 | { |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 4172 | int x; |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 4173 | x = putc (c, rl_outstream); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4174 | #ifndef _MINIX |
| 4175 | return x; |
| 4176 | #endif |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | static int |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4180 | bash_execute_unix_command (count, key) |
| 4181 | int count; /* ignored */ |
| 4182 | int key; |
| 4183 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4184 | int type; |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4185 | register int i, r; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4186 | intmax_t mi; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4187 | sh_parser_state_t ps; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 4188 | char *cmd, *value, *ce, old_ch; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4189 | SHELL_VAR *v; |
| 4190 | char ibuf[INT_STRLEN_BOUND(int) + 1]; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4191 | |
| 4192 | /* First, we need to find the right command to execute. This is tricky, |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4193 | because we might have already indirected into another keymap, so we |
| 4194 | have to walk cmd_xmap using the entire key sequence. */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 4195 | cmd = (char *)rl_function_of_keyseq_len (rl_executing_keyseq, rl_key_sequence_length, cmd_xmap, &type); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4196 | |
| 4197 | if (cmd == 0 || type != ISMACR) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4198 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4199 | rl_crlf (); |
| 4200 | internal_error (_("bash_execute_unix_command: cannot find keymap for command")); |
| 4201 | rl_forced_update_display (); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4202 | return 1; |
| 4203 | } |
| 4204 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4205 | ce = rl_get_termcap ("ce"); |
| 4206 | if (ce) /* clear current line */ |
| 4207 | { |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 4208 | #if 0 |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4209 | fprintf (rl_outstream, "\r"); |
| 4210 | tputs (ce, 1, putx); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 4211 | #else |
| 4212 | rl_clear_visible_line (); |
| 4213 | #endif |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 4214 | fflush (rl_outstream); |
| 4215 | } |
| 4216 | else |
| 4217 | rl_crlf (); /* move to a new line */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4218 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4219 | v = bind_variable ("READLINE_LINE", rl_line_buffer, 0); |
| 4220 | if (v) |
| 4221 | VSETATTR (v, att_exported); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 4222 | i = rl_point; |
| 4223 | #if defined (HANDLE_MULTIBYTE) |
| 4224 | if (MB_CUR_MAX > 1) |
| 4225 | { |
| 4226 | old_ch = rl_line_buffer[rl_point]; |
| 4227 | rl_line_buffer[rl_point] = '\0'; |
| 4228 | i = MB_STRLEN (rl_line_buffer); |
| 4229 | rl_line_buffer[rl_point] = old_ch; |
| 4230 | } |
| 4231 | #endif |
| 4232 | value = inttostr (i, ibuf, sizeof (ibuf)); |
| 4233 | v = bind_int_variable ("READLINE_POINT", value, 0); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4234 | if (v) |
| 4235 | VSETATTR (v, att_exported); |
| 4236 | array_needs_making = 1; |
| 4237 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4238 | save_parser_state (&ps); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 4239 | r = parse_and_execute (savestring (cmd), "bash_execute_unix_command", SEVAL_NOHIST|SEVAL_NOFREE); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4240 | restore_parser_state (&ps); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4241 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4242 | v = find_variable ("READLINE_LINE"); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 4243 | maybe_make_readline_line (v ? value_cell (v) : 0); |
| 4244 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4245 | v = find_variable ("READLINE_POINT"); |
| 4246 | if (v && legal_number (value_cell (v), &mi)) |
| 4247 | { |
| 4248 | i = mi; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 4249 | #if defined (HANDLE_MULTIBYTE) |
| 4250 | if (i > 0 && MB_CUR_MAX > 1) |
| 4251 | i = _rl_find_next_mbchar (rl_line_buffer, 0, i, 0); |
| 4252 | #endif |
| Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 4253 | if (i != rl_point) |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4254 | { |
| 4255 | rl_point = i; |
| 4256 | if (rl_point > rl_end) |
| 4257 | rl_point = rl_end; |
| 4258 | else if (rl_point < 0) |
| 4259 | rl_point = 0; |
| 4260 | } |
| 4261 | } |
| 4262 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 4263 | check_unbind_variable ("READLINE_LINE"); |
| 4264 | check_unbind_variable ("READLINE_POINT"); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4265 | array_needs_making = 1; |
| 4266 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4267 | /* and restore the readline buffer and display after command execution. */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 4268 | /* If we clear the last line of the prompt above, redraw only that last |
| 4269 | line. If the command returns 124, we redraw unconditionally as in |
| 4270 | previous versions. */ |
| 4271 | if (ce && r != 124) |
| 4272 | rl_redraw_prompt_last_line (); |
| 4273 | else |
| 4274 | rl_forced_update_display (); |
| 4275 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4276 | return 0; |
| 4277 | } |
| 4278 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4279 | int |
| 4280 | print_unix_command_map () |
| 4281 | { |
| 4282 | Keymap save; |
| 4283 | |
| 4284 | save = rl_get_keymap (); |
| 4285 | rl_set_keymap (cmd_xmap); |
| 4286 | rl_macro_dumper (1); |
| 4287 | rl_set_keymap (save); |
| 4288 | return 0; |
| 4289 | } |
| 4290 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4291 | static void |
| 4292 | init_unix_command_map () |
| 4293 | { |
| 4294 | cmd_xmap = rl_make_bare_keymap (); |
| 4295 | } |
| 4296 | |
| 4297 | static int |
| 4298 | isolate_sequence (string, ind, need_dquote, startp) |
| 4299 | char *string; |
| 4300 | int ind, need_dquote, *startp; |
| 4301 | { |
| 4302 | register int i; |
| 4303 | int c, passc, delim; |
| 4304 | |
| 4305 | for (i = ind; string[i] && whitespace (string[i]); i++) |
| 4306 | ; |
| 4307 | /* NEED_DQUOTE means that the first non-white character *must* be `"'. */ |
| 4308 | if (need_dquote && string[i] != '"') |
| 4309 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4310 | builtin_error (_("%s: first non-whitespace character is not `\"'"), string); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4311 | return -1; |
| 4312 | } |
| 4313 | |
| 4314 | /* We can have delimited strings even if NEED_DQUOTE == 0, like the command |
| 4315 | string to bind the key sequence to. */ |
| 4316 | delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0; |
| 4317 | |
| 4318 | if (startp) |
| 4319 | *startp = delim ? ++i : i; |
| 4320 | |
| 4321 | for (passc = 0; c = string[i]; i++) |
| 4322 | { |
| 4323 | if (passc) |
| 4324 | { |
| 4325 | passc = 0; |
| 4326 | continue; |
| 4327 | } |
| 4328 | if (c == '\\') |
| 4329 | { |
| 4330 | passc++; |
| 4331 | continue; |
| 4332 | } |
| 4333 | if (c == delim) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4334 | break; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | if (delim && string[i] != delim) |
| 4338 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4339 | builtin_error (_("no closing `%c' in %s"), delim, string); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4340 | return -1; |
| 4341 | } |
| 4342 | |
| 4343 | return i; |
| 4344 | } |
| 4345 | |
| 4346 | int |
| 4347 | bind_keyseq_to_unix_command (line) |
| 4348 | char *line; |
| 4349 | { |
| 4350 | Keymap kmap; |
| 4351 | char *kseq, *value; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 4352 | int i, kstart; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4353 | |
| 4354 | if (cmd_xmap == 0) |
| 4355 | init_unix_command_map (); |
| 4356 | |
| 4357 | kmap = rl_get_keymap (); |
| 4358 | |
| 4359 | /* We duplicate some of the work done by rl_parse_and_bind here, but |
| 4360 | this code only has to handle `"keyseq": ["]command["]' and can |
| 4361 | generate an error for anything else. */ |
| 4362 | i = isolate_sequence (line, 0, 1, &kstart); |
| 4363 | if (i < 0) |
| 4364 | return -1; |
| 4365 | |
| 4366 | /* Create the key sequence string to pass to rl_generic_bind */ |
| 4367 | kseq = substring (line, kstart, i); |
| 4368 | |
| 4369 | for ( ; line[i] && line[i] != ':'; i++) |
| 4370 | ; |
| 4371 | if (line[i] != ':') |
| 4372 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4373 | builtin_error (_("%s: missing colon separator"), line); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4374 | FREE (kseq); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4375 | return -1; |
| 4376 | } |
| 4377 | |
| 4378 | i = isolate_sequence (line, i + 1, 0, &kstart); |
| 4379 | if (i < 0) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4380 | { |
| 4381 | FREE (kseq); |
| 4382 | return -1; |
| 4383 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4384 | |
| 4385 | /* Create the value string containing the command to execute. */ |
| 4386 | value = substring (line, kstart, i); |
| 4387 | |
| 4388 | /* Save the command to execute and the key sequence in the CMD_XMAP */ |
| 4389 | rl_generic_bind (ISMACR, kseq, value, cmd_xmap); |
| 4390 | |
| 4391 | /* and bind the key sequence in the current keymap to a function that |
| 4392 | understands how to execute from CMD_XMAP */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4393 | rl_bind_keyseq_in_map (kseq, bash_execute_unix_command, kmap); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4394 | |
| 4395 | free (kseq); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4396 | return 0; |
| 4397 | } |
| 4398 | |
| 4399 | /* Used by the programmable completion code. Complete TEXT as a filename, |
| 4400 | but return only directories as matches. Dequotes the filename before |
| 4401 | attempting to find matches. */ |
| 4402 | char ** |
| 4403 | bash_directory_completion_matches (text) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4404 | const char *text; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4405 | { |
| 4406 | char **m1; |
| 4407 | char *dfn; |
| 4408 | int qc; |
| 4409 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4410 | qc = rl_dispatching ? rl_completion_quote_character : 0; |
| Chet Ramey | c6dcdf4 | 2014-05-16 14:18:15 -0400 | [diff] [blame] | 4411 | /* If rl_completion_found_quote != 0, rl_completion_matches will call the |
| 4412 | filename dequoting function, causing the directory name to be dequoted |
| 4413 | twice. */ |
| 4414 | if (rl_dispatching && rl_completion_found_quote == 0) |
| 4415 | dfn = bash_dequote_filename ((char *)text, qc); |
| 4416 | else |
| 4417 | dfn = (char *)text; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 4418 | m1 = rl_completion_matches (dfn, rl_filename_completion_function); |
| Chet Ramey | c6dcdf4 | 2014-05-16 14:18:15 -0400 | [diff] [blame] | 4419 | if (dfn != text) |
| 4420 | free (dfn); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 4421 | |
| 4422 | if (m1 == 0 || m1[0] == 0) |
| 4423 | return m1; |
| 4424 | /* We don't bother recomputing the lcd of the matches, because it will just |
| 4425 | get thrown away by the programmable completion code and recomputed |
| 4426 | later. */ |
| 4427 | (void)bash_ignore_filenames (m1); |
| 4428 | return m1; |
| 4429 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 4430 | |
| 4431 | char * |
| 4432 | bash_dequote_text (text) |
| 4433 | const char *text; |
| 4434 | { |
| 4435 | char *dtxt; |
| 4436 | int qc; |
| 4437 | |
| 4438 | qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0; |
| 4439 | dtxt = bash_dequote_filename ((char *)text, qc); |
| 4440 | return (dtxt); |
| 4441 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4442 | |
| 4443 | /* This event hook is designed to be called after readline receives a signal |
| 4444 | that interrupts read(2). It gives reasonable responsiveness to interrupts |
| 4445 | and fatal signals without executing too much code in a signal handler |
| 4446 | context. */ |
| 4447 | static int |
| 4448 | bash_event_hook () |
| 4449 | { |
| 4450 | /* If we're going to longjmp to top_level, make sure we clean up readline. |
| 4451 | check_signals will call QUIT, which will eventually longjmp to top_level, |
| Chet Ramey | 84c617e | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 4452 | calling run_interrupt_trap along the way. The check for sigalrm_seen is |
| 4453 | to clean up the read builtin's state. */ |
| 4454 | if (terminating_signal || interrupt_state || sigalrm_seen) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4455 | rl_cleanup_after_signal (); |
| 4456 | bashline_reset_event_hook (); |
| 4457 | check_signals_and_traps (); /* XXX */ |
| 4458 | return 0; |
| 4459 | } |
| 4460 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4461 | #endif /* READLINE */ |