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