| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* bashline.c -- Bash's interface to the readline library. */ |
| 2 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1987-2006 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 | |
| 7 | Bash is free software; you can redistribute it and/or modify it |
| 8 | under the terms of the GNU General Public License as published by |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 9 | the Free Software Foundation; either version 2, or (at your option) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 10 | any later version. |
| 11 | |
| 12 | Bash is distributed in the hope that it will be useful, but WITHOUT |
| 13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 15 | License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Bash; see the file COPYING. If not, write to the Free |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 19 | Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
| 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 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 41 | #include "chartypes.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 42 | #include "bashansi.h" |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 43 | #include "bashintl.h" |
| 44 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 45 | #include "shell.h" |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 46 | #include "input.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 47 | #include "builtins.h" |
| 48 | #include "bashhist.h" |
| 49 | #include "bashline.h" |
| 50 | #include "execute_cmd.h" |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 51 | #include "findcmd.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 52 | #include "pathexp.h" |
| 53 | #include "builtins/common.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 54 | #include <readline/rlconf.h> |
| 55 | #include <readline/readline.h> |
| 56 | #include <readline/history.h> |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 57 | |
| 58 | #include <glob/glob.h> |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 59 | |
| 60 | #if defined (ALIAS) |
| 61 | # include "alias.h" |
| 62 | #endif |
| 63 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 64 | #if defined (PROGRAMMABLE_COMPLETION) |
| 65 | # include "pcomplete.h" |
| 66 | #endif |
| 67 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 68 | /* These should agree with the defines for emacs_mode and vi_mode in |
| 69 | rldefs.h, even though that's not a public readline header file. */ |
| 70 | #ifndef EMACS_EDITING_MODE |
| 71 | # define NO_EDITING_MODE -1 |
| 72 | # define EMACS_EDITING_MODE 1 |
| 73 | # define VI_EDITING_MODE 0 |
| 74 | #endif |
| 75 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 76 | #if defined (BRACE_COMPLETION) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 77 | extern int bash_brace_completion __P((int, int)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 78 | #endif /* BRACE_COMPLETION */ |
| 79 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 80 | /* Forward declarations */ |
| 81 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 82 | /* Functions bound to keys in Readline for Bash users. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 83 | static int shell_expand_line __P((int, int)); |
| 84 | static int display_shell_version __P((int, int)); |
| 85 | static int operate_and_get_next __P((int, int)); |
| 86 | |
| 87 | static int bash_ignore_filenames __P((char **)); |
| 88 | static int bash_ignore_everything __P((char **)); |
| 89 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 90 | #if defined (BANG_HISTORY) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 91 | static char *history_expand_line_internal __P((char *)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 92 | static int history_expand_line __P((int, int)); |
| 93 | static int tcsh_magic_space __P((int, int)); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 94 | #endif /* BANG_HISTORY */ |
| 95 | #ifdef ALIAS |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 96 | static int alias_expand_line __P((int, int)); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 97 | #endif |
| 98 | #if defined (BANG_HISTORY) && defined (ALIAS) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 99 | static int history_and_alias_expand_line __P((int, int)); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 102 | /* Helper functions for Readline. */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 103 | static void bash_directory_expansion __P((char **)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 104 | static int bash_directory_completion_hook __P((char **)); |
| 105 | static int filename_completion_ignore __P((char **)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 106 | static int bash_push_line __P((void)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 107 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 108 | static void cleanup_expansion_error __P((void)); |
| 109 | static void maybe_make_readline_line __P((char *)); |
| 110 | static void set_up_new_line __P((char *)); |
| 111 | |
| 112 | static int check_redir __P((int)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 113 | static char **attempt_shell_completion __P((const char *, int, int)); |
| 114 | static char *variable_completion_function __P((const char *, int)); |
| 115 | static char *hostname_completion_function __P((const char *, int)); |
| 116 | static char *command_subst_completion_function __P((const char *, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 118 | static void build_history_completion_array __P((void)); |
| 119 | static char *history_completion_generator __P((const char *, int)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 120 | static int dynamic_complete_history __P((int, int)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 121 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 122 | static void initialize_hostname_list __P((void)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 123 | static void add_host_name __P((char *)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 124 | static void snarf_hosts_from_file __P((char *)); |
| 125 | static char **hostnames_matching __P((char *)); |
| 126 | |
| 127 | static void _ignore_completion_names __P((char **, sh_ignore_func_t *)); |
| 128 | static int name_is_acceptable __P((const char *)); |
| 129 | static int test_for_directory __P((const char *)); |
| 130 | static int return_zero __P((const char *)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 131 | |
| 132 | static char *bash_dequote_filename __P((char *, int)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 133 | static char *quote_word_break_chars __P((char *)); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 134 | static char *bash_quote_filename __P((char *, int, char *)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 135 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 136 | static int bash_execute_unix_command __P((int, int)); |
| 137 | static void init_unix_command_map __P((void)); |
| 138 | static int isolate_sequence __P((char *, int, int, int *)); |
| 139 | |
| 140 | static int set_saved_history __P((void)); |
| 141 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 142 | #if defined (ALIAS) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 143 | static int posix_edit_macros __P((int, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 144 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 145 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 146 | #if defined (PROGRAMMABLE_COMPLETION) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 147 | static int find_cmd_start __P((int)); |
| 148 | static int find_cmd_end __P((int)); |
| 149 | static char *find_cmd_name __P((int)); |
| 150 | static char *prog_complete_return __P((const char *, int)); |
| 151 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 152 | static char **prog_complete_matches; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 153 | #endif |
| 154 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 155 | /* Variables used here but defined in other files. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 156 | #if defined (BANG_HISTORY) |
| 157 | extern int hist_verify; |
| 158 | #endif |
| 159 | |
| 160 | extern int current_command_line_count, last_command_exit_value; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 161 | extern int posixly_correct, no_symbolic_links; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 162 | extern char *current_prompt_string, *ps1_prompt; |
| 163 | extern STRING_INT_ALIST word_token_alist[]; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 164 | extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 165 | |
| 166 | /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual |
| 167 | completion functions which indicate what type of completion should be |
| 168 | done (at or before point) that can be bound to key sequences with |
| 169 | the readline library. */ |
| 170 | #define SPECIFIC_COMPLETION_FUNCTIONS |
| 171 | |
| 172 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 173 | static int bash_specific_completion __P((int, rl_compentry_func_t *)); |
| 174 | |
| 175 | static int bash_complete_filename_internal __P((int)); |
| 176 | static int bash_complete_username_internal __P((int)); |
| 177 | static int bash_complete_hostname_internal __P((int)); |
| 178 | static int bash_complete_variable_internal __P((int)); |
| 179 | static int bash_complete_command_internal __P((int)); |
| 180 | |
| 181 | static int bash_complete_filename __P((int, int)); |
| 182 | static int bash_possible_filename_completions __P((int, int)); |
| 183 | static int bash_complete_username __P((int, int)); |
| 184 | static int bash_possible_username_completions __P((int, int)); |
| 185 | static int bash_complete_hostname __P((int, int)); |
| 186 | static int bash_possible_hostname_completions __P((int, int)); |
| 187 | static int bash_complete_variable __P((int, int)); |
| 188 | static int bash_possible_variable_completions __P((int, int)); |
| 189 | static int bash_complete_command __P((int, int)); |
| 190 | static int bash_possible_command_completions __P((int, int)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 191 | |
| 192 | static char *glob_complete_word __P((const char *, int)); |
| 193 | static int bash_glob_completion_internal __P((int)); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 194 | static int bash_glob_complete_word __P((int, int)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 195 | static int bash_glob_expand_word __P((int, int)); |
| 196 | static int bash_glob_list_expansions __P((int, int)); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 197 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 198 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ |
| 199 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 200 | static int edit_and_execute_command __P((int, int, int, char *)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 201 | #if defined (VI_MODE) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 202 | static int vi_edit_and_execute_command __P((int, int)); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 203 | static int bash_vi_complete __P((int, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 204 | #endif |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 205 | static int emacs_edit_and_execute_command __P((int, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 206 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 207 | /* Non-zero once initalize_readline () has been called. */ |
| 208 | int bash_readline_initialized = 0; |
| 209 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 210 | /* If non-zero, we do hostname completion, breaking words at `@' and |
| 211 | trying to complete the stuff after the `@' from our own internal |
| 212 | host list. */ |
| 213 | int perform_hostname_completion = 1; |
| 214 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 215 | /* If non-zero, we don't do command completion on an empty line. */ |
| 216 | int no_empty_command_completion; |
| 217 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 218 | /* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the |
| 219 | only possible matches. Set to 0 if you want to match filenames if they |
| 220 | are the only possible matches, even if FIGNORE says to. */ |
| 221 | int force_fignore = 1; |
| 222 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 223 | static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:"; |
| 224 | static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:"; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 225 | /* )) */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 226 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 227 | 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] | 228 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 229 | static int dot_in_path = 0; |
| 230 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 231 | /* What kind of quoting is performed by bash_quote_filename: |
| 232 | COMPLETE_DQUOTE = double-quoting the filename |
| 233 | COMPLETE_SQUOTE = single_quoting the filename |
| 234 | COMPLETE_BSQUOTE = backslash-quoting special chars in the filename |
| 235 | */ |
| 236 | #define COMPLETE_DQUOTE 1 |
| 237 | #define COMPLETE_SQUOTE 2 |
| 238 | #define COMPLETE_BSQUOTE 3 |
| 239 | static int completion_quoting_style = COMPLETE_BSQUOTE; |
| 240 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 241 | /* Flag values for the final argument to bash_default_completion */ |
| 242 | #define DEFCOMP_CMDPOS 1 |
| 243 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 244 | /* Change the readline VI-mode keymaps into or out of Posix.2 compliance. |
| 245 | Called when the shell is put into or out of `posix' mode. */ |
| 246 | void |
| 247 | posix_readline_initialize (on_or_off) |
| 248 | int on_or_off; |
| 249 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 250 | if (on_or_off) |
| 251 | rl_variable_bind ("comment-begin", "#"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 252 | #if defined (VI_MODE) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 253 | 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] | 254 | #endif |
| 255 | } |
| 256 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 257 | /* When this function returns, rl_completer_word_break_characters points to |
| 258 | dynamically allocated memory. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 259 | int |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 260 | enable_hostname_completion (on_or_off) |
| 261 | int on_or_off; |
| 262 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 263 | int old_value; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 264 | char *at, *nv, *nval; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 265 | |
| 266 | old_value = perform_hostname_completion; |
| 267 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 268 | if (on_or_off) |
| 269 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 270 | perform_hostname_completion = 1; |
| 271 | rl_special_prefixes = "$@"; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 272 | } |
| 273 | else |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 274 | { |
| 275 | perform_hostname_completion = 0; |
| 276 | rl_special_prefixes = "$"; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* Now we need to figure out how to appropriately modify and assign |
| 280 | rl_completer_word_break_characters depending on whether we want |
| 281 | hostname completion on or off. */ |
| 282 | |
| 283 | /* If this is the first time this has been called |
| 284 | (bash_readline_initialized == 0), use the sames values as before, but |
| 285 | allocate new memory for rl_completer_word_break_characters. */ |
| 286 | |
| 287 | if (bash_readline_initialized == 0 && |
| 288 | (rl_completer_word_break_characters == 0 || |
| 289 | rl_completer_word_break_characters == rl_basic_word_break_characters)) |
| 290 | { |
| 291 | if (on_or_off) |
| 292 | rl_completer_word_break_characters = savestring (bash_completer_word_break_characters); |
| 293 | else |
| 294 | rl_completer_word_break_characters = savestring (bash_nohostname_word_break_characters); |
| 295 | } |
| 296 | else |
| 297 | { |
| 298 | /* See if we have anything to do. */ |
| 299 | at = strchr (rl_completer_word_break_characters, '@'); |
| 300 | 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] | 301 | return old_value; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 302 | |
| 303 | /* We have something to do. Do it. */ |
| 304 | nval = (char *)xmalloc (strlen (rl_completer_word_break_characters) + 1 + on_or_off); |
| 305 | |
| 306 | if (on_or_off == 0) |
| 307 | { |
| 308 | /* Turn it off -- just remove `@' from word break chars. We want |
| 309 | to remove all occurrences of `@' from the char list, so we loop |
| 310 | rather than just copy the rest of the list over AT. */ |
| 311 | for (nv = nval, at = rl_completer_word_break_characters; *at; ) |
| 312 | if (*at != '@') |
| 313 | *nv++ = *at++; |
| 314 | else |
| 315 | at++; |
| 316 | *nv = '\0'; |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | nval[0] = '@'; |
| 321 | strcpy (nval + 1, rl_completer_word_break_characters); |
| 322 | } |
| 323 | |
| 324 | free (rl_completer_word_break_characters); |
| 325 | rl_completer_word_break_characters = nval; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 326 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 327 | |
| 328 | return (old_value); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 329 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 330 | |
| 331 | /* Called once from parse.y if we are going to use readline. */ |
| 332 | void |
| 333 | initialize_readline () |
| 334 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 335 | rl_command_func_t *func; |
| 336 | char kseq[2]; |
| 337 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 338 | if (bash_readline_initialized) |
| 339 | return; |
| 340 | |
| 341 | rl_terminal_name = get_string_value ("TERM"); |
| 342 | rl_instream = stdin; |
| 343 | rl_outstream = stderr; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 344 | |
| 345 | /* Allow conditional parsing of the ~/.inputrc file. */ |
| 346 | rl_readline_name = "Bash"; |
| 347 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 348 | /* Add bindable names before calling rl_initialize so they may be |
| 349 | referenced in the various inputrc files. */ |
| 350 | rl_add_defun ("shell-expand-line", shell_expand_line, -1); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 351 | #ifdef BANG_HISTORY |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 352 | rl_add_defun ("history-expand-line", history_expand_line, -1); |
| 353 | rl_add_defun ("magic-space", tcsh_magic_space, -1); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 354 | #endif |
| 355 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 356 | #ifdef ALIAS |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 357 | rl_add_defun ("alias-expand-line", alias_expand_line, -1); |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 358 | # ifdef BANG_HISTORY |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 359 | 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] | 360 | # endif |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 361 | #endif |
| 362 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 363 | /* Backwards compatibility. */ |
| 364 | rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1); |
| 365 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 366 | rl_add_defun ("operate-and-get-next", operate_and_get_next, -1); |
| 367 | rl_add_defun ("display-shell-version", display_shell_version, -1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 368 | 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] | 369 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 370 | #if defined (BRACE_COMPLETION) |
| 371 | rl_add_defun ("complete-into-braces", bash_brace_completion, -1); |
| 372 | #endif |
| 373 | |
| 374 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| 375 | rl_add_defun ("complete-filename", bash_complete_filename, -1); |
| 376 | rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1); |
| 377 | rl_add_defun ("complete-username", bash_complete_username, -1); |
| 378 | rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1); |
| 379 | rl_add_defun ("complete-hostname", bash_complete_hostname, -1); |
| 380 | rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1); |
| 381 | rl_add_defun ("complete-variable", bash_complete_variable, -1); |
| 382 | rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1); |
| 383 | rl_add_defun ("complete-command", bash_complete_command, -1); |
| 384 | rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 385 | rl_add_defun ("glob-complete-word", bash_glob_complete_word, -1); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 386 | rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1); |
| 387 | rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1); |
| 388 | #endif |
| 389 | |
| 390 | rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1); |
| 391 | |
| 392 | /* Bind defaults before binding our custom shell keybindings. */ |
| 393 | if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0) |
| 394 | rl_initialize (); |
| 395 | |
| 396 | /* Bind up our special shell functions. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 397 | 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] | 398 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 399 | #ifdef BANG_HISTORY |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 400 | 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] | 401 | #endif |
| 402 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 403 | rl_bind_key_if_unbound_in_map (CTRL ('O'), operate_and_get_next, emacs_standard_keymap); |
| 404 | 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] | 405 | |
| 406 | /* In Bash, the user can switch editing modes with "set -o [vi emacs]", |
| 407 | so it is not necessary to allow C-M-j for context switching. Turn |
| 408 | off this occasionally confusing behaviour. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 409 | kseq[0] = CTRL('J'); |
| 410 | kseq[1] = '\0'; |
| 411 | func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL); |
| 412 | if (func == rl_vi_editing_mode) |
| 413 | rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap); |
| 414 | kseq[0] = CTRL('M'); |
| 415 | func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL); |
| 416 | if (func == rl_vi_editing_mode) |
| 417 | rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 418 | #if defined (VI_MODE) |
| 419 | rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap); |
| 420 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 421 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 422 | #if defined (BRACE_COMPLETION) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 423 | 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] | 424 | #endif /* BRACE_COMPLETION */ |
| 425 | |
| 426 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 427 | rl_bind_key_if_unbound_in_map ('/', bash_complete_filename, emacs_meta_keymap); |
| 428 | 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] | 429 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 430 | /* Have to jump through hoops here because there is a default binding for |
| 431 | M-~ (rl_tilde_expand) */ |
| 432 | kseq[0] = '~'; |
| 433 | kseq[1] = '\0'; |
| 434 | func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL); |
| 435 | if (func == 0 || func == rl_tilde_expand) |
| 436 | rl_bind_keyseq_in_map (kseq, bash_complete_username, emacs_meta_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 437 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 438 | 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] | 439 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 440 | rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname, emacs_meta_keymap); |
| 441 | 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] | 442 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 443 | rl_bind_key_if_unbound_in_map ('$', bash_complete_variable, emacs_meta_keymap); |
| 444 | 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] | 445 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 446 | rl_bind_key_if_unbound_in_map ('!', bash_complete_command, emacs_meta_keymap); |
| 447 | rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions, emacs_ctlx_keymap); |
| 448 | |
| 449 | rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word, emacs_meta_keymap); |
| 450 | rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word, emacs_ctlx_keymap); |
| 451 | 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] | 452 | |
| 453 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ |
| 454 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 455 | kseq[0] = TAB; |
| 456 | kseq[1] = '\0'; |
| 457 | func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL); |
| 458 | if (func == 0 || func == rl_tab_insert) |
| 459 | rl_bind_key_in_map (TAB, dynamic_complete_history, emacs_meta_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 460 | |
| 461 | /* Tell the completer that we want a crack first. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 462 | rl_attempted_completion_function = attempt_shell_completion; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 463 | |
| 464 | /* Tell the completer that we might want to follow symbolic links or |
| 465 | do other expansion on directory names. */ |
| 466 | rl_directory_completion_hook = bash_directory_completion_hook; |
| 467 | |
| 468 | /* Tell the filename completer we want a chance to ignore some names. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 469 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 470 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 471 | /* Bind C-xC-e to invoke emacs and run result as commands. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 472 | 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] | 473 | #if defined (VI_MODE) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 474 | 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] | 475 | # if defined (ALIAS) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 476 | 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] | 477 | # endif |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 478 | |
| 479 | rl_bind_key_in_map ('\\', bash_vi_complete, vi_movement_keymap); |
| 480 | rl_bind_key_in_map ('*', bash_vi_complete, vi_movement_keymap); |
| 481 | rl_bind_key_in_map ('=', bash_vi_complete, vi_movement_keymap); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 482 | #endif |
| 483 | |
| 484 | rl_completer_quote_characters = "'\""; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 485 | |
| 486 | /* This sets rl_completer_word_break_characters and rl_special_prefixes |
| 487 | to the appropriate values, depending on whether or not hostname |
| 488 | completion is enabled. */ |
| 489 | enable_hostname_completion (perform_hostname_completion); |
| 490 | |
| 491 | /* characters that need to be quoted when appearing in filenames. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 492 | rl_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{"; /*}*/ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 493 | rl_filename_quoting_function = bash_quote_filename; |
| 494 | rl_filename_dequoting_function = bash_dequote_filename; |
| 495 | rl_char_is_quoted_p = char_is_quoted; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 496 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 497 | #if 0 |
| 498 | /* This is superfluous and makes it impossible to use tab completion in |
| 499 | vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix() |
| 500 | should already have called posix_readline_initialize() when |
| 501 | posixly_correct was set. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 502 | if (posixly_correct) |
| 503 | posix_readline_initialize (1); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 504 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 505 | |
| 506 | bash_readline_initialized = 1; |
| 507 | } |
| 508 | |
| 509 | /* On Sun systems at least, rl_attempted_completion_function can end up |
| 510 | getting set to NULL, and rl_completion_entry_function set to do command |
| 511 | word completion if Bash is interrupted while trying to complete a command |
| 512 | word. This just resets all the completion functions to the right thing. |
| 513 | It's called from throw_to_top_level(). */ |
| 514 | void |
| 515 | bashline_reinitialize () |
| 516 | { |
| 517 | tilde_initialize (); |
| 518 | rl_attempted_completion_function = attempt_shell_completion; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 519 | rl_completion_entry_function = NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 520 | rl_directory_completion_hook = bash_directory_completion_hook; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 521 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* Contains the line to push into readline. */ |
| 525 | static char *push_to_readline = (char *)NULL; |
| 526 | |
| 527 | /* Push the contents of push_to_readline into the |
| 528 | readline buffer. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 529 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 530 | bash_push_line () |
| 531 | { |
| 532 | if (push_to_readline) |
| 533 | { |
| 534 | rl_insert_text (push_to_readline); |
| 535 | free (push_to_readline); |
| 536 | push_to_readline = (char *)NULL; |
| 537 | rl_startup_hook = old_rl_startup_hook; |
| 538 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 539 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* Call this to set the initial text for the next line to read |
| 543 | from readline. */ |
| 544 | int |
| 545 | bash_re_edit (line) |
| 546 | char *line; |
| 547 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 548 | FREE (push_to_readline); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 549 | |
| 550 | push_to_readline = savestring (line); |
| 551 | old_rl_startup_hook = rl_startup_hook; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 552 | rl_startup_hook = bash_push_line; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 553 | |
| 554 | return (0); |
| 555 | } |
| 556 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 557 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 558 | display_shell_version (count, c) |
| 559 | int count, c; |
| 560 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 561 | rl_crlf (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 562 | show_shell_version (0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 563 | putc ('\r', rl_outstream); |
| 564 | fflush (rl_outstream); |
| 565 | rl_on_new_line (); |
| 566 | rl_redisplay (); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 567 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* **************************************************************** */ |
| 571 | /* */ |
| 572 | /* Readline Stuff */ |
| 573 | /* */ |
| 574 | /* **************************************************************** */ |
| 575 | |
| 576 | /* If the user requests hostname completion, then simply build a list |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 577 | of hosts, and complete from that forever more, or at least until |
| 578 | HOSTFILE is unset. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 579 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 580 | /* THIS SHOULD BE A STRINGLIST. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 581 | /* The kept list of hostnames. */ |
| 582 | static char **hostname_list = (char **)NULL; |
| 583 | |
| 584 | /* The physical size of the above list. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 585 | static int hostname_list_size; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 586 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 587 | /* The number of hostnames in the above list. */ |
| 588 | static int hostname_list_length; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 589 | |
| 590 | /* Whether or not HOSTNAME_LIST has been initialized. */ |
| 591 | int hostname_list_initialized = 0; |
| 592 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 593 | /* Initialize the hostname completion table. */ |
| 594 | static void |
| 595 | initialize_hostname_list () |
| 596 | { |
| 597 | char *temp; |
| 598 | |
| 599 | temp = get_string_value ("HOSTFILE"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 600 | if (temp == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 601 | temp = get_string_value ("hostname_completion_file"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 602 | if (temp == 0) |
| 603 | temp = DEFAULT_HOSTS_FILE; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 604 | |
| 605 | snarf_hosts_from_file (temp); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 606 | |
| 607 | if (hostname_list) |
| 608 | hostname_list_initialized++; |
| 609 | } |
| 610 | |
| 611 | /* Add NAME to the list of hosts. */ |
| 612 | static void |
| 613 | add_host_name (name) |
| 614 | char *name; |
| 615 | { |
| 616 | if (hostname_list_length + 2 > hostname_list_size) |
| 617 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 618 | hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 619 | hostname_list = strvec_resize (hostname_list, hostname_list_size); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 622 | hostname_list[hostname_list_length++] = savestring (name); |
| 623 | hostname_list[hostname_list_length] = (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c)) |
| 627 | |
| 628 | static void |
| 629 | snarf_hosts_from_file (filename) |
| 630 | char *filename; |
| 631 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 632 | FILE *file; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 633 | char *temp, buffer[256], name[256]; |
| 634 | register int i, start; |
| 635 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 636 | file = fopen (filename, "r"); |
| 637 | if (file == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 638 | return; |
| 639 | |
| 640 | while (temp = fgets (buffer, 255, file)) |
| 641 | { |
| 642 | /* Skip to first character. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 643 | for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++) |
| 644 | ; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 645 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 646 | /* If comment or blank line, ignore. */ |
| 647 | if (buffer[i] == '\0' || buffer[i] == '#') |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 648 | continue; |
| 649 | |
| 650 | /* If `preprocessor' directive, do the include. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 651 | if (strncmp (buffer + i, "$include ", 9) == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 652 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 653 | char *incfile, *t; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 654 | |
| 655 | /* Find start of filename. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 656 | for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++) |
| 657 | ; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 658 | |
| 659 | /* Find end of filename. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 660 | for (t = incfile; *t && cr_whitespace (*t) == 0; t++) |
| 661 | ; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 662 | |
| 663 | *t = '\0'; |
| 664 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 665 | snarf_hosts_from_file (incfile); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 666 | continue; |
| 667 | } |
| 668 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 669 | /* Skip internet address if present. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 670 | if (DIGIT (buffer[i])) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 671 | for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 672 | |
| 673 | /* Gobble up names. Each name is separated with whitespace. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 674 | while (buffer[i]) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 675 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 676 | for (; cr_whitespace (buffer[i]); i++) |
| 677 | ; |
| 678 | if (buffer[i] == '\0' || buffer[i] == '#') |
| 679 | break; |
| 680 | |
| 681 | /* Isolate the current word. */ |
| 682 | for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++) |
| 683 | ; |
| 684 | if (i == start) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 685 | continue; |
| 686 | strncpy (name, buffer + start, i - start); |
| 687 | name[i - start] = '\0'; |
| 688 | add_host_name (name); |
| 689 | } |
| 690 | } |
| 691 | fclose (file); |
| 692 | } |
| 693 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 694 | /* Return the hostname list. */ |
| 695 | char ** |
| 696 | get_hostname_list () |
| 697 | { |
| 698 | if (hostname_list_initialized == 0) |
| 699 | initialize_hostname_list (); |
| 700 | return (hostname_list); |
| 701 | } |
| 702 | |
| 703 | void |
| 704 | clear_hostname_list () |
| 705 | { |
| 706 | register int i; |
| 707 | |
| 708 | if (hostname_list_initialized == 0) |
| 709 | return; |
| 710 | for (i = 0; i < hostname_list_length; i++) |
| 711 | free (hostname_list[i]); |
| 712 | hostname_list_length = 0; |
| 713 | } |
| 714 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 715 | /* Return a NULL terminated list of hostnames which begin with TEXT. |
| 716 | Initialize the hostname list the first time if neccessary. |
| 717 | The array is malloc ()'ed, but not the individual strings. */ |
| 718 | static char ** |
| 719 | hostnames_matching (text) |
| 720 | char *text; |
| 721 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 722 | register int i, len, nmatch, rsize; |
| 723 | char **result; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 724 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 725 | if (hostname_list_initialized == 0) |
| 726 | initialize_hostname_list (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 727 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 728 | if (hostname_list_initialized == 0) |
| 729 | return ((char **)NULL); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 730 | |
| 731 | /* Special case. If TEXT consists of nothing, then the whole list is |
| 732 | what is desired. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 733 | if (*text == '\0') |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 734 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 735 | result = strvec_create (1 + hostname_list_length); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 736 | for (i = 0; i < hostname_list_length; i++) |
| 737 | result[i] = hostname_list[i]; |
| 738 | result[i] = (char *)NULL; |
| 739 | return (result); |
| 740 | } |
| 741 | |
| 742 | /* Scan until found, or failure. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 743 | len = strlen (text); |
| 744 | result = (char **)NULL; |
| 745 | for (i = nmatch = rsize = 0; i < hostname_list_length; i++) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 746 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 747 | if (STREQN (text, hostname_list[i], len) == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 748 | continue; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 749 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 750 | /* OK, it matches. Add it to the list. */ |
| Jari Aalto | bc4cd23 | 1998-07-23 14:37:54 +0000 | [diff] [blame] | 751 | if (nmatch >= (rsize - 1)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 752 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 753 | rsize = (rsize + 16) - (rsize % 16); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 754 | result = strvec_resize (result, rsize); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 757 | result[nmatch++] = hostname_list[i]; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 758 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 759 | if (nmatch) |
| 760 | result[nmatch] = (char *)NULL; |
| 761 | return (result); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 764 | /* 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] | 765 | editing command. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 766 | static int saved_history_line_to_use = -1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 767 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 768 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 769 | set_saved_history () |
| 770 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 771 | if (saved_history_line_to_use >= 0) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 772 | rl_get_previous_history (history_length - saved_history_line_to_use, 0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 773 | saved_history_line_to_use = -1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 774 | rl_startup_hook = old_rl_startup_hook; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 775 | return (0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 776 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 777 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 778 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 779 | operate_and_get_next (count, c) |
| 780 | int count, c; |
| 781 | { |
| 782 | int where; |
| 783 | |
| 784 | /* Accept the current line. */ |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 785 | rl_newline (1, c); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 786 | |
| 787 | /* Find the current line, and find the next line to use. */ |
| 788 | where = where_history (); |
| 789 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 790 | if ((history_is_stifled () && (history_length >= history_max_entries)) || |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 791 | (where >= history_length - 1)) |
| 792 | saved_history_line_to_use = where; |
| 793 | else |
| 794 | saved_history_line_to_use = where + 1; |
| 795 | |
| 796 | old_rl_startup_hook = rl_startup_hook; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 797 | rl_startup_hook = set_saved_history; |
| 798 | |
| 799 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 802 | /* This vi mode command causes VI_EDIT_COMMAND to be run on the current |
| 803 | command being entered (if no explicit argument is given), otherwise on |
| 804 | a command from the history file. */ |
| 805 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 806 | #define VI_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-vi}}\"" |
| 807 | #define EMACS_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-emacs}}\"" |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 808 | #define POSIX_VI_EDIT_COMMAND "fc -e vi" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 809 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 810 | static int |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 811 | edit_and_execute_command (count, c, editing_mode, edit_command) |
| 812 | int count, c, editing_mode; |
| 813 | char *edit_command; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 814 | { |
| 815 | char *command; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 816 | int r, cclc, rrs; |
| 817 | |
| 818 | rrs = rl_readline_state; |
| 819 | cclc = current_command_line_count; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 820 | |
| 821 | /* Accept the current line. */ |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 822 | rl_newline (1, c); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 823 | |
| 824 | if (rl_explicit_arg) |
| 825 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 826 | command = (char *)xmalloc (strlen (edit_command) + 8); |
| 827 | sprintf (command, "%s %d", edit_command, count); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 828 | } |
| 829 | else |
| 830 | { |
| 831 | /* Take the command we were just editing, add it to the history file, |
| 832 | then call fc to operate on it. We have to add a dummy command to |
| 833 | the end of the history because fc ignores the last command (assumes |
| 834 | it's supposed to deal with the command before the `fc'). */ |
| 835 | using_history (); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 836 | bash_add_history (rl_line_buffer); |
| 837 | bash_add_history (""); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 838 | history_lines_this_session++; |
| 839 | using_history (); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 840 | command = savestring (edit_command); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 841 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 842 | |
| 843 | /* Now, POSIX.1-2001 and SUSv3 say that the commands executed from the |
| 844 | temporary file should be placed into the history. We don't do that |
| 845 | yet. */ |
| 846 | r = parse_and_execute (command, (editing_mode == VI_EDITING_MODE) ? "v" : "C-xC-e", SEVAL_NOHIST); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 847 | |
| 848 | current_command_line_count = cclc; |
| 849 | |
| 850 | /* Now erase the contents of the current line and undo the effects of the |
| 851 | rl_accept_line() above. We don't even want to make the text we just |
| 852 | executed available for undoing. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 853 | rl_line_buffer[0] = '\0'; /* XXX */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 854 | rl_point = rl_end = 0; |
| 855 | rl_done = 0; |
| 856 | rl_readline_state = rrs; |
| 857 | |
| 858 | rl_forced_update_display (); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 859 | |
| 860 | return r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 861 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 862 | |
| 863 | #if defined (VI_MODE) |
| 864 | static int |
| 865 | vi_edit_and_execute_command (count, c) |
| 866 | int count, c; |
| 867 | { |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 868 | if (posixly_correct) |
| 869 | return (edit_and_execute_command (count, c, VI_EDITING_MODE, POSIX_VI_EDIT_COMMAND)); |
| 870 | else |
| 871 | 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] | 872 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 873 | #endif /* VI_MODE */ |
| 874 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 875 | static int |
| 876 | emacs_edit_and_execute_command (count, c) |
| 877 | int count, c; |
| 878 | { |
| 879 | return (edit_and_execute_command (count, c, EMACS_EDITING_MODE, EMACS_EDIT_COMMAND)); |
| 880 | } |
| 881 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 882 | #if defined (ALIAS) |
| 883 | static int |
| 884 | posix_edit_macros (count, key) |
| 885 | int count, key; |
| 886 | { |
| 887 | int c; |
| 888 | char alias_name[3], *alias_value, *macro; |
| 889 | |
| 890 | c = rl_read_key (); |
| 891 | alias_name[0] = '_'; |
| 892 | alias_name[1] = c; |
| 893 | alias_name[2] = '\0'; |
| 894 | |
| 895 | alias_value = get_alias_value (alias_name); |
| 896 | if (alias_value && *alias_value) |
| 897 | { |
| 898 | macro = savestring (alias_value); |
| 899 | rl_push_macro_input (macro); |
| 900 | } |
| 901 | return 0; |
| 902 | } |
| 903 | #endif |
| 904 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 905 | /* **************************************************************** */ |
| 906 | /* */ |
| 907 | /* How To Do Shell Completion */ |
| 908 | /* */ |
| 909 | /* **************************************************************** */ |
| 910 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 911 | #define COMMAND_SEPARATORS ";|&{(`" |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 912 | /* )} */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 913 | |
| 914 | static int |
| 915 | check_redir (ti) |
| 916 | int ti; |
| 917 | { |
| 918 | register int this_char, prev_char; |
| 919 | |
| 920 | /* Handle the two character tokens `>&', `<&', and `>|'. |
| 921 | We are not in a command position after one of these. */ |
| 922 | this_char = rl_line_buffer[ti]; |
| 923 | prev_char = rl_line_buffer[ti - 1]; |
| 924 | |
| 925 | if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) || |
| 926 | (this_char == '|' && prev_char == '>')) |
| 927 | return (1); |
| 928 | else if ((this_char == '{' && prev_char == '$') || /* } */ |
| 929 | (char_is_quoted (rl_line_buffer, ti))) |
| 930 | return (1); |
| 931 | return (0); |
| 932 | } |
| 933 | |
| 934 | #if defined (PROGRAMMABLE_COMPLETION) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 935 | /* |
| 936 | * XXX - because of the <= start test, and setting os = s+1, this can |
| 937 | * potentially return os > start. This is probably not what we want to |
| 938 | * happen, but fix later after 2.05a-release. |
| 939 | */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 940 | static int |
| 941 | find_cmd_start (start) |
| 942 | int start; |
| 943 | { |
| 944 | register int s, os; |
| 945 | |
| 946 | os = 0; |
| 947 | while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS)) <= start) && |
| 948 | rl_line_buffer[s]) |
| 949 | os = s+1; |
| 950 | return os; |
| 951 | } |
| 952 | |
| 953 | static int |
| 954 | find_cmd_end (end) |
| 955 | int end; |
| 956 | { |
| 957 | register int e; |
| 958 | |
| 959 | e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS); |
| 960 | return e; |
| 961 | } |
| 962 | |
| 963 | static char * |
| 964 | find_cmd_name (start) |
| 965 | int start; |
| 966 | { |
| 967 | char *name; |
| 968 | register int s, e; |
| 969 | |
| 970 | for (s = start; whitespace (rl_line_buffer[s]); s++) |
| 971 | ; |
| 972 | |
| 973 | /* skip until a shell break character */ |
| 974 | e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n"); |
| 975 | |
| 976 | name = substring (rl_line_buffer, s, e); |
| 977 | |
| 978 | return (name); |
| 979 | } |
| 980 | |
| 981 | static char * |
| 982 | prog_complete_return (text, matchnum) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 983 | const char *text; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 984 | int matchnum; |
| 985 | { |
| 986 | static int ind; |
| 987 | |
| 988 | if (matchnum == 0) |
| 989 | ind = 0; |
| 990 | |
| 991 | if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0) |
| 992 | return (char *)NULL; |
| 993 | return (prog_complete_matches[ind++]); |
| 994 | } |
| 995 | |
| 996 | #endif /* PROGRAMMABLE_COMPLETION */ |
| 997 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 998 | /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are |
| 999 | at START and END. Return an array of matches, or NULL if none. */ |
| 1000 | static char ** |
| 1001 | attempt_shell_completion (text, start, end) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1002 | const char *text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1003 | int start, end; |
| 1004 | { |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1005 | int in_command_position, ti, saveti, qc, dflags; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1006 | char **matches, *command_separator_chars; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1007 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1008 | command_separator_chars = COMMAND_SEPARATORS; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1009 | matches = (char **)NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1010 | rl_ignore_some_completions_function = filename_completion_ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1011 | |
| 1012 | /* Determine if this could be a command word. It is if it appears at |
| 1013 | the start of the line (ignoring preceding whitespace), or if it |
| 1014 | appears after a character that separates commands. It cannot be a |
| 1015 | command word if we aren't at the top-level prompt. */ |
| 1016 | ti = start - 1; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1017 | saveti = qc = -1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1018 | |
| 1019 | while ((ti > -1) && (whitespace (rl_line_buffer[ti]))) |
| 1020 | ti--; |
| 1021 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1022 | #if 1 |
| 1023 | /* If this is an open quote, maybe we're trying to complete a quoted |
| 1024 | command name. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1025 | if (ti >= 0 && (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\'')) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1026 | { |
| 1027 | qc = rl_line_buffer[ti]; |
| 1028 | saveti = ti--; |
| 1029 | while (ti > -1 && (whitespace (rl_line_buffer[ti]))) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1030 | ti--; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1031 | } |
| 1032 | #endif |
| 1033 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1034 | in_command_position = 0; |
| 1035 | if (ti < 0) |
| 1036 | { |
| 1037 | /* Only do command completion at the start of a line when we |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1038 | are prompting at the top level. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1039 | if (current_prompt_string == ps1_prompt) |
| 1040 | in_command_position++; |
| 1041 | } |
| 1042 | else if (member (rl_line_buffer[ti], command_separator_chars)) |
| 1043 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1044 | in_command_position++; |
| 1045 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1046 | if (check_redir (ti) == 1) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1047 | in_command_position = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1048 | } |
| 1049 | else |
| 1050 | { |
| 1051 | /* This still could be in command position. It is possible |
| 1052 | that all of the previous words on the line are variable |
| 1053 | assignments. */ |
| 1054 | } |
| 1055 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1056 | /* Check that we haven't incorrectly flagged a closed command substitution |
| 1057 | as indicating we're in a command position. */ |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1058 | if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' && |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1059 | *text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1060 | in_command_position = 0; |
| 1061 | |
| 1062 | /* Special handling for command substitution. If *TEXT is a backquote, |
| 1063 | it can be the start or end of an old-style command substitution, or |
| 1064 | unmatched. If it's unmatched, both calls to unclosed_pair will |
| 1065 | succeed. */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1066 | if (*text == '`' && |
| 1067 | (in_command_position || (unclosed_pair (rl_line_buffer, start, "`") && |
| 1068 | unclosed_pair (rl_line_buffer, end, "`")))) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1069 | matches = rl_completion_matches (text, command_subst_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1070 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1071 | #if defined (PROGRAMMABLE_COMPLETION) |
| 1072 | /* Attempt programmable completion. */ |
| 1073 | if (!matches && in_command_position == 0 && prog_completion_enabled && |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1074 | (progcomp_size () > 0) && current_prompt_string == ps1_prompt) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1075 | { |
| 1076 | int s, e, foundcs; |
| 1077 | char *n; |
| 1078 | |
| 1079 | /* XXX - don't free the members */ |
| 1080 | if (prog_complete_matches) |
| 1081 | free (prog_complete_matches); |
| 1082 | prog_complete_matches = (char **)NULL; |
| 1083 | |
| 1084 | s = find_cmd_start (start); |
| 1085 | e = find_cmd_end (end); |
| 1086 | n = find_cmd_name (s); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1087 | if (e > s && assignment (n, 0) == 0) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1088 | prog_complete_matches = programmable_completions (n, text, s, e, &foundcs); |
| 1089 | else |
| 1090 | foundcs = 0; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1091 | FREE (n); |
| 1092 | /* XXX - if we found a COMPSPEC for the command, just return whatever |
| 1093 | the programmable completion code returns, and disable the default |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1094 | filename completion that readline will do unless the COPT_DEFAULT |
| 1095 | option has been set with the `-o default' option to complete. */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1096 | if (foundcs) |
| 1097 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1098 | /* If the user specified that the compspec returns filenames, make |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1099 | sure that readline knows it. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1100 | if (foundcs & COPT_FILENAMES) |
| 1101 | rl_filename_completion_desired = 1; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1102 | /* If the user doesn't want a space appended, tell readline. */ |
| 1103 | if (foundcs & COPT_NOSPACE) |
| 1104 | rl_completion_suppress_append = 1; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1105 | /* Turn what the programmable completion code returns into what |
| 1106 | readline wants. I should have made compute_lcd_of_matches |
| 1107 | external... */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1108 | matches = rl_completion_matches (text, prog_complete_return); |
| 1109 | if ((foundcs & COPT_DEFAULT) == 0) |
| 1110 | rl_attempted_completion_over = 1; /* no default */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1111 | if (matches || ((foundcs & COPT_BASHDEFAULT) == 0)) |
| 1112 | return (matches); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | #endif |
| 1116 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1117 | if (matches == 0) |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1118 | { |
| 1119 | dflags = 0; |
| 1120 | if (in_command_position) |
| 1121 | dflags |= DEFCOMP_CMDPOS; |
| 1122 | matches = bash_default_completion (text, start, end, qc, dflags); |
| 1123 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1124 | |
| 1125 | return matches; |
| 1126 | } |
| 1127 | |
| 1128 | char ** |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1129 | bash_default_completion (text, start, end, qc, compflags) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1130 | const char *text; |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1131 | int start, end, qc, compflags; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1132 | { |
| 1133 | char **matches; |
| 1134 | |
| 1135 | matches = (char **)NULL; |
| 1136 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1137 | /* New posix-style command substitution or variable name? */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1138 | if (!matches && *text == '$') |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1139 | { |
| 1140 | if (qc != '\'' && text[1] == '(') /* ) */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1141 | matches = rl_completion_matches (text, command_subst_completion_function); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1142 | else |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1143 | matches = rl_completion_matches (text, variable_completion_function); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1144 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1145 | |
| 1146 | /* If the word starts in `~', and there is no slash in the word, then |
| 1147 | try completing this word as a username. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1148 | if (!matches && *text == '~' && !xstrchr (text, '/')) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1149 | matches = rl_completion_matches (text, rl_username_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1150 | |
| 1151 | /* Another one. Why not? If the word starts in '@', then look through |
| 1152 | the world of known hostnames for completion first. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1153 | if (!matches && perform_hostname_completion && *text == '@') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1154 | matches = rl_completion_matches (text, hostname_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1155 | |
| 1156 | /* And last, (but not least) if this word is in a command position, then |
| 1157 | complete over possible command names, including aliases, functions, |
| 1158 | and command names. */ |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1159 | if (matches == 0 && (compflags & DEFCOMP_CMDPOS)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1160 | { |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1161 | /* If END == START and text[0] == 0, we are trying to complete an empty |
| 1162 | command word. */ |
| 1163 | if (no_empty_command_completion && end == start && text[0] == '\0') |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1164 | { |
| 1165 | matches = (char **)NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1166 | rl_ignore_some_completions_function = bash_ignore_everything; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1167 | } |
| 1168 | else |
| 1169 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1170 | #define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x)) |
| 1171 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1172 | dot_in_path = 0; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1173 | matches = rl_completion_matches (text, command_word_completion_function); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1174 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1175 | /* If we are attempting command completion and nothing matches, we |
| 1176 | do not want readline to perform filename completion for us. We |
| 1177 | still want to be able to complete partial pathnames, so set the |
| 1178 | completion ignore function to something which will remove |
| 1179 | filenames and leave directories in the match list. */ |
| 1180 | if (matches == (char **)NULL) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1181 | rl_ignore_some_completions_function = bash_ignore_filenames; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1182 | else if (matches[1] == 0 && CMD_IS_DIR(matches[0]) && dot_in_path == 0) |
| 1183 | /* If we found a single match, without looking in the current |
| 1184 | directory (because it's not in $PATH), but the found name is |
| 1185 | also a command in the current directory, suppress appending any |
| 1186 | terminating character, since it's ambiguous. */ |
| 1187 | { |
| 1188 | rl_completion_suppress_append = 1; |
| 1189 | rl_filename_completion_desired = 0; |
| 1190 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1191 | 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] | 1192 | /* There are multiple instances of the same match (duplicate |
| 1193 | completions haven't yet been removed). In this case, all of |
| 1194 | the matches will be the same, and the duplicate removal code |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1195 | will distill them all down to one. We turn on |
| 1196 | rl_completion_suppress_append for the same reason as above. |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1197 | Remember: we only care if there's eventually a single unique |
| 1198 | completion. If there are multiple completions this won't |
| 1199 | make a difference and the problem won't occur. */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1200 | { |
| 1201 | rl_completion_suppress_append = 1; |
| 1202 | rl_filename_completion_desired = 0; |
| 1203 | } |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1204 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1207 | /* This could be a globbing pattern, so try to expand it using pathname |
| 1208 | expansion. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1209 | if (!matches && glob_pattern_p (text)) |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1210 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1211 | matches = rl_completion_matches (text, glob_complete_word); |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1212 | /* A glob expression that matches more than one filename is problematic. |
| 1213 | If we match more than one filename, punt. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1214 | if (matches && matches[1] && rl_completion_type == TAB) |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1215 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1216 | strvec_dispose (matches); |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 1217 | matches = (char **)0; |
| 1218 | } |
| 1219 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1220 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1221 | return (matches); |
| 1222 | } |
| 1223 | |
| 1224 | /* This is the function to call when the word to complete is in a position |
| 1225 | where a command word can be found. It grovels $PATH, looking for commands |
| 1226 | that match. It also scans aliases, function names, and the shell_builtin |
| 1227 | table. */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1228 | char * |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1229 | command_word_completion_function (hint_text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1230 | const char *hint_text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1231 | int state; |
| 1232 | { |
| 1233 | static char *hint = (char *)NULL; |
| 1234 | static char *path = (char *)NULL; |
| 1235 | static char *val = (char *)NULL; |
| 1236 | static char *filename_hint = (char *)NULL; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1237 | static char *dequoted_hint = (char *)NULL; |
| 1238 | static int path_index, hint_len, dequoted_len, istate, igncase; |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1239 | static int mapping_over, local_index, searching_path, hint_is_dir; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1240 | static SHELL_VAR **varlist = (SHELL_VAR **)NULL; |
| 1241 | #if defined (ALIAS) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1242 | static alias_t **alias_list = (alias_t **)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1243 | #endif /* ALIAS */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1244 | char *temp; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1245 | |
| 1246 | /* We have to map over the possibilities for command words. If we have |
| 1247 | no state, then make one just for that purpose. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1248 | if (!state) |
| 1249 | { |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1250 | if (dequoted_hint && dequoted_hint != hint) |
| 1251 | free (dequoted_hint); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1252 | if (hint) |
| 1253 | free (hint); |
| 1254 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1255 | mapping_over = searching_path = 0; |
| 1256 | hint_is_dir = CMD_IS_DIR (hint_text); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1257 | val = (char *)NULL; |
| 1258 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1259 | temp = rl_variable_value ("completion-ignore-case"); |
| 1260 | igncase = strcmp (temp, "on") == 0; |
| 1261 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1262 | /* If this is an absolute program name, do not check it against |
| 1263 | aliases, reserved words, functions or builtins. We must check |
| 1264 | whether or not it is unique, and, if so, whether that filename |
| 1265 | is executable. */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1266 | if (absolute_program (hint_text)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1267 | { |
| 1268 | /* Perform tilde expansion on what's passed, so we don't end up |
| 1269 | passing filenames with tildes directly to stat(). */ |
| 1270 | if (*hint_text == '~') |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1271 | hint = bash_tilde_expand (hint_text, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1272 | else |
| 1273 | hint = savestring (hint_text); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1274 | |
| 1275 | dequoted_hint = hint; |
| 1276 | /* If readline's completer found a quote character somewhere, but |
| 1277 | didn't set the quote character, there must have been a quote |
| 1278 | character embedded in the filename. It can't be at the start of |
| 1279 | the filename, so we need to dequote the filename before we look |
| 1280 | in the file system for it. */ |
| 1281 | if (rl_completion_found_quote && rl_completion_quote_character == 0) |
| 1282 | { |
| 1283 | dequoted_hint = bash_dequote_filename (hint, 0); |
| 1284 | free (hint); |
| 1285 | hint = dequoted_hint; |
| 1286 | } |
| 1287 | dequoted_len = hint_len = strlen (hint); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1288 | |
| 1289 | if (filename_hint) |
| 1290 | free (filename_hint); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1291 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1292 | filename_hint = savestring (hint); |
| 1293 | |
| 1294 | mapping_over = 4; |
| 1295 | istate = 0; |
| 1296 | goto inner; |
| 1297 | } |
| 1298 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1299 | dequoted_hint = hint = savestring (hint_text); |
| 1300 | dequoted_len = hint_len = strlen (hint); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1301 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1302 | if (rl_completion_found_quote && rl_completion_quote_character == 0) |
| 1303 | { |
| 1304 | dequoted_hint = bash_dequote_filename (hint, 0); |
| 1305 | dequoted_len = strlen (dequoted_hint); |
| 1306 | } |
| 1307 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1308 | path = get_string_value ("PATH"); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1309 | path_index = dot_in_path = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1310 | |
| 1311 | /* Initialize the variables for each type of command word. */ |
| 1312 | local_index = 0; |
| 1313 | |
| 1314 | if (varlist) |
| 1315 | free (varlist); |
| 1316 | |
| 1317 | varlist = all_visible_functions (); |
| 1318 | |
| 1319 | #if defined (ALIAS) |
| 1320 | if (alias_list) |
| 1321 | free (alias_list); |
| 1322 | |
| 1323 | alias_list = all_aliases (); |
| 1324 | #endif /* ALIAS */ |
| 1325 | } |
| 1326 | |
| 1327 | /* mapping_over says what we are currently hacking. Note that every case |
| 1328 | in this list must fall through when there are no more possibilities. */ |
| 1329 | |
| 1330 | switch (mapping_over) |
| 1331 | { |
| 1332 | case 0: /* Aliases come first. */ |
| 1333 | #if defined (ALIAS) |
| 1334 | while (alias_list && alias_list[local_index]) |
| 1335 | { |
| 1336 | register char *alias; |
| 1337 | |
| 1338 | alias = alias_list[local_index++]->name; |
| 1339 | |
| 1340 | if (STREQN (alias, hint, hint_len)) |
| 1341 | return (savestring (alias)); |
| 1342 | } |
| 1343 | #endif /* ALIAS */ |
| 1344 | local_index = 0; |
| 1345 | mapping_over++; |
| 1346 | |
| 1347 | case 1: /* Then shell reserved words. */ |
| 1348 | { |
| 1349 | while (word_token_alist[local_index].word) |
| 1350 | { |
| 1351 | register char *reserved_word; |
| 1352 | |
| 1353 | reserved_word = word_token_alist[local_index++].word; |
| 1354 | |
| 1355 | if (STREQN (reserved_word, hint, hint_len)) |
| 1356 | return (savestring (reserved_word)); |
| 1357 | } |
| 1358 | local_index = 0; |
| 1359 | mapping_over++; |
| 1360 | } |
| 1361 | |
| 1362 | case 2: /* Then function names. */ |
| 1363 | while (varlist && varlist[local_index]) |
| 1364 | { |
| 1365 | register char *varname; |
| 1366 | |
| 1367 | varname = varlist[local_index++]->name; |
| 1368 | |
| 1369 | if (STREQN (varname, hint, hint_len)) |
| 1370 | return (savestring (varname)); |
| 1371 | } |
| 1372 | local_index = 0; |
| 1373 | mapping_over++; |
| 1374 | |
| 1375 | case 3: /* Then shell builtins. */ |
| 1376 | for (; local_index < num_shell_builtins; local_index++) |
| 1377 | { |
| 1378 | /* Ignore it if it doesn't have a function pointer or if it |
| 1379 | is not currently enabled. */ |
| 1380 | if (!shell_builtins[local_index].function || |
| 1381 | (shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0) |
| 1382 | continue; |
| 1383 | |
| 1384 | if (STREQN (shell_builtins[local_index].name, hint, hint_len)) |
| 1385 | { |
| 1386 | int i = local_index++; |
| 1387 | |
| 1388 | return (savestring (shell_builtins[i].name)); |
| 1389 | } |
| 1390 | } |
| 1391 | local_index = 0; |
| 1392 | mapping_over++; |
| 1393 | } |
| 1394 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1395 | /* If the text passed is a directory in the current directory, return it |
| 1396 | as a possible match. Executables in directories in the current |
| 1397 | directory can be specified using relative pathnames and successfully |
| 1398 | executed even when `.' is not in $PATH. */ |
| 1399 | if (hint_is_dir) |
| 1400 | { |
| 1401 | hint_is_dir = 0; /* only return the hint text once */ |
| 1402 | return (savestring (hint_text)); |
| 1403 | } |
| 1404 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1405 | /* Repeatedly call filename_completion_function while we have |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1406 | members of PATH left. Question: should we stat each file? |
| 1407 | Answer: we call executable_file () on each file. */ |
| 1408 | outer: |
| 1409 | |
| 1410 | istate = (val != (char *)NULL); |
| 1411 | |
| 1412 | if (!istate) |
| 1413 | { |
| 1414 | char *current_path; |
| 1415 | |
| 1416 | /* Get the next directory from the path. If there is none, then we |
| 1417 | are all done. */ |
| 1418 | if (!path || !path[path_index] || |
| 1419 | (current_path = extract_colon_unit (path, &path_index)) == 0) |
| 1420 | return ((char *)NULL); |
| 1421 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1422 | searching_path = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1423 | if (*current_path == 0) |
| 1424 | { |
| 1425 | free (current_path); |
| 1426 | current_path = savestring ("."); |
| 1427 | } |
| 1428 | |
| 1429 | if (*current_path == '~') |
| 1430 | { |
| 1431 | char *t; |
| 1432 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1433 | t = bash_tilde_expand (current_path, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1434 | free (current_path); |
| 1435 | current_path = t; |
| 1436 | } |
| 1437 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1438 | if (current_path[0] == '.' && current_path[1] == '\0') |
| 1439 | dot_in_path = 1; |
| 1440 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1441 | if (filename_hint) |
| 1442 | free (filename_hint); |
| 1443 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1444 | filename_hint = sh_makepath (current_path, hint, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1445 | free (current_path); |
| 1446 | } |
| 1447 | |
| 1448 | inner: |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1449 | val = rl_filename_completion_function (filename_hint, istate); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1450 | istate = 1; |
| 1451 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1452 | if (val == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1453 | { |
| 1454 | /* If the hint text is an absolute program, then don't bother |
| 1455 | searching through PATH. */ |
| 1456 | if (absolute_program (hint)) |
| 1457 | return ((char *)NULL); |
| 1458 | |
| 1459 | goto outer; |
| 1460 | } |
| 1461 | else |
| 1462 | { |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1463 | int match, freetemp; |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1464 | #if 0 |
| 1465 | char *temp; /* shadows previous declaration */ |
| 1466 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1467 | |
| 1468 | if (absolute_program (hint)) |
| 1469 | { |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1470 | if (igncase == 0) |
| 1471 | match = strncmp (val, hint, hint_len) == 0; |
| 1472 | else |
| 1473 | match = strncasecmp (val, hint, hint_len) == 0; |
| 1474 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1475 | /* If we performed tilde expansion, restore the original |
| 1476 | filename. */ |
| 1477 | if (*hint_text == '~') |
| 1478 | { |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 1479 | int l, tl, vl, dl; |
| 1480 | char *rd; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1481 | vl = strlen (val); |
| 1482 | tl = strlen (hint_text); |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 1483 | #if 0 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1484 | l = vl - hint_len; /* # of chars added */ |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 1485 | #else |
| 1486 | rd = savestring (filename_hint); |
| 1487 | bash_directory_expansion (&rd); |
| 1488 | dl = strlen (rd); |
| 1489 | l = vl - dl; /* # of chars added */ |
| 1490 | free (rd); |
| 1491 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1492 | temp = (char *)xmalloc (l + 2 + tl); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1493 | strcpy (temp, hint_text); |
| 1494 | strcpy (temp + tl, val + vl - l); |
| 1495 | } |
| 1496 | else |
| 1497 | temp = savestring (val); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1498 | freetemp = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1499 | } |
| 1500 | else |
| 1501 | { |
| 1502 | temp = strrchr (val, '/'); |
| 1503 | |
| 1504 | if (temp) |
| 1505 | { |
| 1506 | temp++; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1507 | if (igncase == 0) |
| 1508 | freetemp = match = strncmp (temp, hint, hint_len) == 0; |
| 1509 | else |
| 1510 | freetemp = match = strncasecmp (temp, hint, hint_len) == 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1511 | if (match) |
| 1512 | temp = savestring (temp); |
| 1513 | } |
| 1514 | else |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1515 | freetemp = match = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1518 | #if 0 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1519 | /* If we have found a match, and it is an executable file or a |
| 1520 | directory name, return it. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1521 | if (match && executable_or_directory (val)) |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1522 | #else |
| 1523 | /* If we have found a match, and it is an executable file, return it. |
| 1524 | We don't return directory names when searching $PATH, since the |
| 1525 | bash execution code won't find executables in directories which |
| 1526 | appear in directories in $PATH when they're specified using |
| 1527 | relative pathnames. */ |
| 1528 | if (match && (searching_path ? executable_file (val) : executable_or_directory (val))) |
| 1529 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1530 | { |
| 1531 | free (val); |
| 1532 | val = ""; /* So it won't be NULL. */ |
| 1533 | return (temp); |
| 1534 | } |
| 1535 | else |
| 1536 | { |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1537 | if (freetemp) |
| 1538 | free (temp); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1539 | free (val); |
| 1540 | goto inner; |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1545 | /* Completion inside an unterminated command substitution. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1546 | static char * |
| 1547 | command_subst_completion_function (text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1548 | const char *text; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1549 | int state; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1550 | { |
| 1551 | static char **matches = (char **)NULL; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1552 | static const char *orig_start; |
| 1553 | static char *filename_text = (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1554 | static int cmd_index, start_len; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1555 | char *value; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1556 | |
| 1557 | if (state == 0) |
| 1558 | { |
| 1559 | if (filename_text) |
| 1560 | free (filename_text); |
| 1561 | orig_start = text; |
| 1562 | if (*text == '`') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1563 | text++; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1564 | else if (*text == '$' && text[1] == '(') /* ) */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1565 | text += 2; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1566 | /* If the text was quoted, suppress any quote character that the |
| 1567 | readline completion code would insert. */ |
| 1568 | rl_completion_suppress_quote = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1569 | start_len = text - orig_start; |
| 1570 | filename_text = savestring (text); |
| 1571 | if (matches) |
| 1572 | free (matches); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1573 | |
| 1574 | /* |
| 1575 | * At this point we can entertain the idea of re-parsing |
| 1576 | * `filename_text' into a (possibly incomplete) command name and |
| 1577 | * arguments, and doing completion based on that. This is |
| 1578 | * currently very rudimentary, but it is a small improvement. |
| 1579 | */ |
| 1580 | for (value = filename_text + strlen (filename_text) - 1; value > filename_text; value--) |
| 1581 | if (whitespace (*value) || member (*value, COMMAND_SEPARATORS)) |
| 1582 | break; |
| 1583 | if (value <= filename_text) |
| 1584 | matches = rl_completion_matches (filename_text, command_word_completion_function); |
| 1585 | else |
| 1586 | { |
| 1587 | value++; |
| 1588 | start_len += value - filename_text; |
| 1589 | if (whitespace (value[-1])) |
| 1590 | matches = rl_completion_matches (value, rl_filename_completion_function); |
| 1591 | else |
| 1592 | matches = rl_completion_matches (value, command_word_completion_function); |
| 1593 | } |
| 1594 | |
| 1595 | /* If there is more than one match, rl_completion_matches has already |
| 1596 | put the lcd in matches[0]. Skip over it. */ |
| 1597 | cmd_index = matches && matches[0] && matches[1]; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1598 | |
| 1599 | /* If there's a single match and it's a directory, set the append char |
| 1600 | to the expected `/'. Otherwise, don't append anything. */ |
| 1601 | if (matches && matches[0] && matches[1] == 0 && test_for_directory (matches[0])) |
| 1602 | rl_completion_append_character = '/'; |
| 1603 | else |
| 1604 | rl_completion_suppress_append = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | if (!matches || !matches[cmd_index]) |
| 1608 | { |
| 1609 | rl_filename_quoting_desired = 0; /* disable quoting */ |
| 1610 | return ((char *)NULL); |
| 1611 | } |
| 1612 | else |
| 1613 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1614 | value = (char *)xmalloc (1 + start_len + strlen (matches[cmd_index])); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (start_len == 1) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1617 | value[0] = *orig_start; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1618 | else |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1619 | strncpy (value, orig_start, start_len); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1620 | |
| 1621 | strcpy (value + start_len, matches[cmd_index]); |
| 1622 | |
| 1623 | cmd_index++; |
| 1624 | return (value); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | /* Okay, now we write the entry_function for variable completion. */ |
| 1629 | static char * |
| 1630 | variable_completion_function (text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1631 | const char *text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1632 | int state; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1633 | { |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1634 | static char **varlist = (char **)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1635 | static int varlist_index; |
| 1636 | static char *varname = (char *)NULL; |
| 1637 | static int namelen; |
| 1638 | static int first_char, first_char_loc; |
| 1639 | |
| 1640 | if (!state) |
| 1641 | { |
| 1642 | if (varname) |
| 1643 | free (varname); |
| 1644 | |
| 1645 | first_char_loc = 0; |
| 1646 | first_char = text[0]; |
| 1647 | |
| 1648 | if (first_char == '$') |
| 1649 | first_char_loc++; |
| 1650 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1651 | if (text[first_char_loc] == '{') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1652 | first_char_loc++; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1653 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1654 | varname = savestring (text + first_char_loc); |
| 1655 | |
| 1656 | namelen = strlen (varname); |
| 1657 | if (varlist) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1658 | strvec_dispose (varlist); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1659 | |
| 1660 | varlist = all_variables_matching_prefix (varname); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1661 | varlist_index = 0; |
| 1662 | } |
| 1663 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1664 | if (!varlist || !varlist[varlist_index]) |
| 1665 | { |
| 1666 | return ((char *)NULL); |
| 1667 | } |
| 1668 | else |
| 1669 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1670 | char *value; |
| 1671 | |
| 1672 | value = (char *)xmalloc (4 + strlen (varlist[varlist_index])); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1673 | |
| 1674 | if (first_char_loc) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1675 | { |
| 1676 | value[0] = first_char; |
| 1677 | if (first_char_loc == 2) |
| 1678 | value[1] = '{'; |
| 1679 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1680 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1681 | strcpy (value + first_char_loc, varlist[varlist_index]); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1682 | if (first_char_loc == 2) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1683 | strcat (value, "}"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1684 | |
| 1685 | varlist_index++; |
| 1686 | return (value); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | /* How about a completion function for hostnames? */ |
| 1691 | static char * |
| 1692 | hostname_completion_function (text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1693 | const char *text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1694 | int state; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1695 | { |
| 1696 | static char **list = (char **)NULL; |
| 1697 | static int list_index = 0; |
| 1698 | static int first_char, first_char_loc; |
| 1699 | |
| 1700 | /* If we don't have any state, make some. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1701 | if (state == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1702 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1703 | FREE (list); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1704 | |
| 1705 | list = (char **)NULL; |
| 1706 | |
| 1707 | first_char_loc = 0; |
| 1708 | first_char = *text; |
| 1709 | |
| 1710 | if (first_char == '@') |
| 1711 | first_char_loc++; |
| 1712 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1713 | list = hostnames_matching ((char *)text+first_char_loc); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1714 | list_index = 0; |
| 1715 | } |
| 1716 | |
| 1717 | if (list && list[list_index]) |
| 1718 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1719 | char *t; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1720 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1721 | t = (char *)xmalloc (2 + strlen (list[list_index])); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1722 | *t = first_char; |
| 1723 | strcpy (t + first_char_loc, list[list_index]); |
| 1724 | list_index++; |
| 1725 | return (t); |
| 1726 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1727 | |
| 1728 | return ((char *)NULL); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1731 | /* |
| 1732 | * A completion function for service names from /etc/services (or wherever). |
| 1733 | */ |
| 1734 | char * |
| 1735 | bash_servicename_completion_function (text, state) |
| 1736 | const char *text; |
| 1737 | int state; |
| 1738 | { |
| 1739 | #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT) |
| 1740 | return ((char *)NULL); |
| 1741 | #else |
| 1742 | static char *sname = (char *)NULL; |
| 1743 | static struct servent *srvent; |
| 1744 | static int snamelen, firstc; |
| 1745 | char *value; |
| 1746 | char **alist, *aentry; |
| 1747 | int afound; |
| 1748 | |
| 1749 | if (state == 0) |
| 1750 | { |
| 1751 | FREE (sname); |
| 1752 | firstc = *text; |
| 1753 | |
| 1754 | sname = savestring (text); |
| 1755 | snamelen = strlen (sname); |
| 1756 | setservent (0); |
| 1757 | } |
| 1758 | |
| 1759 | while (srvent = getservent ()) |
| 1760 | { |
| 1761 | afound = 0; |
| 1762 | if (snamelen == 0 || (STREQN (sname, srvent->s_name, snamelen))) |
| 1763 | break; |
| 1764 | /* Not primary, check aliases */ |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1765 | for (alist = srvent->s_aliases; *alist; alist++) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1766 | { |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1767 | aentry = *alist; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1768 | if (STREQN (sname, aentry, snamelen)) |
| 1769 | { |
| 1770 | afound = 1; |
| 1771 | break; |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | if (afound) |
| 1776 | break; |
| 1777 | } |
| 1778 | |
| 1779 | if (srvent == 0) |
| 1780 | { |
| 1781 | endservent (); |
| 1782 | return ((char *)NULL); |
| 1783 | } |
| 1784 | |
| 1785 | value = afound ? savestring (aentry) : savestring (srvent->s_name); |
| 1786 | return value; |
| 1787 | #endif |
| 1788 | } |
| 1789 | |
| 1790 | /* |
| 1791 | * A completion function for group names from /etc/group (or wherever). |
| 1792 | */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1793 | char * |
| 1794 | bash_groupname_completion_function (text, state) |
| 1795 | const char *text; |
| 1796 | int state; |
| 1797 | { |
| 1798 | #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H) |
| 1799 | return ((char *)NULL); |
| 1800 | #else |
| 1801 | static char *gname = (char *)NULL; |
| 1802 | static struct group *grent; |
| 1803 | static int gnamelen; |
| 1804 | char *value; |
| 1805 | |
| 1806 | if (state == 0) |
| 1807 | { |
| 1808 | FREE (gname); |
| 1809 | gname = savestring (text); |
| 1810 | gnamelen = strlen (gname); |
| 1811 | |
| 1812 | setgrent (); |
| 1813 | } |
| 1814 | |
| 1815 | while (grent = getgrent ()) |
| 1816 | { |
| 1817 | if (gnamelen == 0 || (STREQN (gname, grent->gr_name, gnamelen))) |
| 1818 | break; |
| 1819 | } |
| 1820 | |
| 1821 | if (grent == 0) |
| 1822 | { |
| 1823 | endgrent (); |
| 1824 | return ((char *)NULL); |
| 1825 | } |
| 1826 | |
| 1827 | value = savestring (grent->gr_name); |
| 1828 | return (value); |
| 1829 | #endif |
| 1830 | } |
| 1831 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1832 | /* Functions to perform history and alias expansions on the current line. */ |
| 1833 | |
| 1834 | #if defined (BANG_HISTORY) |
| 1835 | /* Perform history expansion on the current line. If no history expansion |
| 1836 | is done, pre_process_line() returns what it was passed, so we need to |
| 1837 | allocate a new line here. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1838 | static char * |
| 1839 | history_expand_line_internal (line) |
| 1840 | char *line; |
| 1841 | { |
| 1842 | char *new_line; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1843 | int old_verify; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1844 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1845 | old_verify = hist_verify; |
| 1846 | hist_verify = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1847 | new_line = pre_process_line (line, 0, 0); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1848 | hist_verify = old_verify; |
| 1849 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1850 | return (new_line == line) ? savestring (line) : new_line; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1851 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1852 | #endif |
| 1853 | |
| 1854 | /* There was an error in expansion. Let the preprocessor print |
| 1855 | the error here. */ |
| 1856 | static void |
| 1857 | cleanup_expansion_error () |
| 1858 | { |
| 1859 | char *to_free; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1860 | #if defined (BANG_HISTORY) |
| 1861 | int old_verify; |
| 1862 | |
| 1863 | old_verify = hist_verify; |
| 1864 | hist_verify = 0; |
| 1865 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1866 | |
| 1867 | fprintf (rl_outstream, "\r\n"); |
| 1868 | to_free = pre_process_line (rl_line_buffer, 1, 0); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1869 | #if defined (BANG_HISTORY) |
| 1870 | hist_verify = old_verify; |
| 1871 | #endif |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1872 | if (to_free != rl_line_buffer) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1873 | FREE (to_free); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1874 | putc ('\r', rl_outstream); |
| 1875 | rl_forced_update_display (); |
| 1876 | } |
| 1877 | |
| 1878 | /* If NEW_LINE differs from what is in the readline line buffer, add an |
| 1879 | undo record to get from the readline line buffer contents to the new |
| 1880 | line and make NEW_LINE the current readline line. */ |
| 1881 | static void |
| 1882 | maybe_make_readline_line (new_line) |
| 1883 | char *new_line; |
| 1884 | { |
| 1885 | if (strcmp (new_line, rl_line_buffer) != 0) |
| 1886 | { |
| 1887 | rl_point = rl_end; |
| 1888 | |
| 1889 | rl_add_undo (UNDO_BEGIN, 0, 0, 0); |
| 1890 | rl_delete_text (0, rl_point); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1891 | rl_point = rl_end = rl_mark = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1892 | rl_insert_text (new_line); |
| 1893 | rl_add_undo (UNDO_END, 0, 0, 0); |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */ |
| 1898 | static void |
| 1899 | set_up_new_line (new_line) |
| 1900 | char *new_line; |
| 1901 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1902 | int old_point, at_end; |
| 1903 | |
| 1904 | old_point = rl_point; |
| 1905 | at_end = rl_point == rl_end; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1906 | |
| 1907 | /* If the line was history and alias expanded, then make that |
| 1908 | be one thing to undo. */ |
| 1909 | maybe_make_readline_line (new_line); |
| 1910 | free (new_line); |
| 1911 | |
| 1912 | /* Place rl_point where we think it should go. */ |
| 1913 | if (at_end) |
| 1914 | rl_point = rl_end; |
| 1915 | else if (old_point < rl_end) |
| 1916 | { |
| 1917 | rl_point = old_point; |
| 1918 | if (!whitespace (rl_line_buffer[rl_point])) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1919 | rl_forward_word (1, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1923 | #if defined (ALIAS) |
| 1924 | /* Expand aliases in the current readline line. */ |
| 1925 | static int |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1926 | alias_expand_line (count, ignore) |
| 1927 | int count, ignore; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1928 | { |
| 1929 | char *new_line; |
| 1930 | |
| 1931 | new_line = alias_expand (rl_line_buffer); |
| 1932 | |
| 1933 | if (new_line) |
| 1934 | { |
| 1935 | set_up_new_line (new_line); |
| 1936 | return (0); |
| 1937 | } |
| 1938 | else |
| 1939 | { |
| 1940 | cleanup_expansion_error (); |
| 1941 | return (1); |
| 1942 | } |
| 1943 | } |
| 1944 | #endif |
| 1945 | |
| 1946 | #if defined (BANG_HISTORY) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1947 | /* History expand the line. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1948 | static int |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1949 | history_expand_line (count, ignore) |
| 1950 | int count, ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1951 | { |
| 1952 | char *new_line; |
| 1953 | |
| 1954 | new_line = history_expand_line_internal (rl_line_buffer); |
| 1955 | |
| 1956 | if (new_line) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1957 | { |
| 1958 | set_up_new_line (new_line); |
| 1959 | return (0); |
| 1960 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1961 | else |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1962 | { |
| 1963 | cleanup_expansion_error (); |
| 1964 | return (1); |
| 1965 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1966 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1967 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1968 | /* Expand history substitutions in the current line and then insert a |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1969 | space (hopefully close to where we were before). */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1970 | static int |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1971 | tcsh_magic_space (count, ignore) |
| 1972 | int count, ignore; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1973 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1974 | int dist_from_end, old_point; |
| 1975 | |
| 1976 | old_point = rl_point; |
| 1977 | dist_from_end = rl_end - rl_point; |
| 1978 | if (history_expand_line (count, ignore) == 0) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1979 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1980 | /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>. |
| 1981 | This works if all expansions were before rl_point or if no expansions |
| 1982 | were performed. */ |
| 1983 | rl_point = (old_point == 0) ? old_point : rl_end - dist_from_end; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1984 | rl_insert (1, ' '); |
| 1985 | return (0); |
| 1986 | } |
| 1987 | else |
| 1988 | return (1); |
| 1989 | } |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1990 | #endif /* BANG_HISTORY */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1991 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1992 | /* History and alias expand the line. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1993 | static int |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1994 | history_and_alias_expand_line (count, ignore) |
| 1995 | int count, ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1996 | { |
| 1997 | char *new_line; |
| 1998 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1999 | new_line = 0; |
| 2000 | #if defined (BANG_HISTORY) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2001 | new_line = history_expand_line_internal (rl_line_buffer); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2002 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2003 | |
| 2004 | #if defined (ALIAS) |
| 2005 | if (new_line) |
| 2006 | { |
| 2007 | char *alias_line; |
| 2008 | |
| 2009 | alias_line = alias_expand (new_line); |
| 2010 | free (new_line); |
| 2011 | new_line = alias_line; |
| 2012 | } |
| 2013 | #endif /* ALIAS */ |
| 2014 | |
| 2015 | if (new_line) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2016 | { |
| 2017 | set_up_new_line (new_line); |
| 2018 | return (0); |
| 2019 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2020 | else |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2021 | { |
| 2022 | cleanup_expansion_error (); |
| 2023 | return (1); |
| 2024 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | /* History and alias expand the line, then perform the shell word |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2028 | expansions by calling expand_string. This can't use set_up_new_line() |
| 2029 | because we want the variable expansions as a separate undo'able |
| 2030 | set of operations. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2031 | static int |
| 2032 | shell_expand_line (count, ignore) |
| 2033 | int count, ignore; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2034 | { |
| 2035 | char *new_line; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2036 | WORD_LIST *expanded_string; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2037 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2038 | new_line = 0; |
| 2039 | #if defined (BANG_HISTORY) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2040 | new_line = history_expand_line_internal (rl_line_buffer); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2041 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2042 | |
| 2043 | #if defined (ALIAS) |
| 2044 | if (new_line) |
| 2045 | { |
| 2046 | char *alias_line; |
| 2047 | |
| 2048 | alias_line = alias_expand (new_line); |
| 2049 | free (new_line); |
| 2050 | new_line = alias_line; |
| 2051 | } |
| 2052 | #endif /* ALIAS */ |
| 2053 | |
| 2054 | if (new_line) |
| 2055 | { |
| 2056 | int old_point = rl_point; |
| 2057 | int at_end = rl_point == rl_end; |
| 2058 | |
| 2059 | /* If the line was history and alias expanded, then make that |
| 2060 | be one thing to undo. */ |
| 2061 | maybe_make_readline_line (new_line); |
| 2062 | free (new_line); |
| 2063 | |
| 2064 | /* If there is variable expansion to perform, do that as a separate |
| 2065 | operation to be undone. */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2066 | new_line = savestring (rl_line_buffer); |
| 2067 | expanded_string = expand_string (new_line, 0); |
| 2068 | FREE (new_line); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2069 | if (expanded_string == 0) |
| 2070 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2071 | new_line = (char *)xmalloc (1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2072 | new_line[0] = '\0'; |
| 2073 | } |
| 2074 | else |
| 2075 | { |
| 2076 | new_line = string_list (expanded_string); |
| 2077 | dispose_words (expanded_string); |
| 2078 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2079 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2080 | maybe_make_readline_line (new_line); |
| 2081 | free (new_line); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2082 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2083 | /* Place rl_point where we think it should go. */ |
| 2084 | if (at_end) |
| 2085 | rl_point = rl_end; |
| 2086 | else if (old_point < rl_end) |
| 2087 | { |
| 2088 | rl_point = old_point; |
| 2089 | if (!whitespace (rl_line_buffer[rl_point])) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2090 | rl_forward_word (1, 0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2091 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2092 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2093 | } |
| 2094 | else |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2095 | { |
| 2096 | cleanup_expansion_error (); |
| 2097 | return 1; |
| 2098 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2101 | /* If FIGNORE is set, then don't match files with the given suffixes when |
| 2102 | completing filenames. If only one of the possibilities has an acceptable |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2103 | suffix, delete the others, else just return and let the completer |
| 2104 | signal an error. It is called by the completer when real |
| 2105 | completions are done on filenames by the completer's internal |
| 2106 | function, not for completion lists (M-?) and not on "other" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2107 | completion types, such as hostnames or commands. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2108 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2109 | static struct ignorevar fignore = |
| 2110 | { |
| 2111 | "FIGNORE", |
| 2112 | (struct ign *)0, |
| 2113 | 0, |
| 2114 | (char *)0, |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2115 | (sh_iv_item_func_t *) 0, |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2116 | }; |
| 2117 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2118 | static void |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2119 | _ignore_completion_names (names, name_func) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2120 | char **names; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2121 | sh_ignore_func_t *name_func; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2122 | { |
| 2123 | char **newnames; |
| 2124 | int idx, nidx; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2125 | char **oldnames; |
| 2126 | int oidx; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2127 | |
| 2128 | /* If there is only one completion, see if it is acceptable. If it is |
| 2129 | not, free it up. In any case, short-circuit and return. This is a |
| 2130 | special case because names[0] is not the prefix of the list of names |
| 2131 | if there is only one completion; it is the completion itself. */ |
| 2132 | if (names[1] == (char *)0) |
| 2133 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2134 | if (force_fignore) |
| 2135 | if ((*name_func) (names[0]) == 0) |
| 2136 | { |
| 2137 | free (names[0]); |
| 2138 | names[0] = (char *)NULL; |
| 2139 | } |
| 2140 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2141 | return; |
| 2142 | } |
| 2143 | |
| 2144 | /* Allocate space for array to hold list of pointers to matching |
| 2145 | filenames. The pointers are copied back to NAMES when done. */ |
| 2146 | for (nidx = 1; names[nidx]; nidx++) |
| 2147 | ; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2148 | newnames = strvec_create (nidx + 1); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2149 | |
| 2150 | if (force_fignore == 0) |
| 2151 | { |
| 2152 | oldnames = strvec_create (nidx - 1); |
| 2153 | oidx = 0; |
| 2154 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2155 | |
| 2156 | newnames[0] = names[0]; |
| 2157 | for (idx = nidx = 1; names[idx]; idx++) |
| 2158 | { |
| 2159 | if ((*name_func) (names[idx])) |
| 2160 | newnames[nidx++] = names[idx]; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2161 | else if (force_fignore == 0) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2162 | oldnames[oidx++] = names[idx]; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2163 | else |
| 2164 | free (names[idx]); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | newnames[nidx] = (char *)NULL; |
| 2168 | |
| 2169 | /* If none are acceptable then let the completer handle it. */ |
| 2170 | if (nidx == 1) |
| 2171 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2172 | if (force_fignore) |
| 2173 | { |
| 2174 | free (names[0]); |
| 2175 | names[0] = (char *)NULL; |
| 2176 | } |
| 2177 | else |
| 2178 | free (oldnames); |
| 2179 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2180 | free (newnames); |
| 2181 | return; |
| 2182 | } |
| 2183 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2184 | if (force_fignore == 0) |
| 2185 | { |
| 2186 | while (oidx) |
| 2187 | free (oldnames[--oidx]); |
| 2188 | free (oldnames); |
| 2189 | } |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2190 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2191 | /* If only one is acceptable, copy it to names[0] and return. */ |
| 2192 | if (nidx == 2) |
| 2193 | { |
| 2194 | free (names[0]); |
| 2195 | names[0] = newnames[1]; |
| 2196 | names[1] = (char *)NULL; |
| 2197 | free (newnames); |
| 2198 | return; |
| 2199 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2200 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2201 | /* Copy the acceptable names back to NAMES, set the new array end, |
| 2202 | and return. */ |
| 2203 | for (nidx = 1; newnames[nidx]; nidx++) |
| 2204 | names[nidx] = newnames[nidx]; |
| 2205 | names[nidx] = (char *)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2206 | free (newnames); |
| 2207 | } |
| 2208 | |
| 2209 | static int |
| 2210 | name_is_acceptable (name) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2211 | const char *name; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2212 | { |
| 2213 | struct ign *p; |
| 2214 | int nlen; |
| 2215 | |
| 2216 | for (nlen = strlen (name), p = fignore.ignores; p->val; p++) |
| 2217 | { |
| 2218 | if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len])) |
| 2219 | return (0); |
| 2220 | } |
| 2221 | |
| 2222 | return (1); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2225 | #if 0 |
| 2226 | static int |
| 2227 | ignore_dot_names (name) |
| 2228 | char *name; |
| 2229 | { |
| 2230 | return (name[0] != '.'); |
| 2231 | } |
| 2232 | #endif |
| 2233 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2234 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2235 | filename_completion_ignore (names) |
| 2236 | char **names; |
| 2237 | { |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2238 | #if 0 |
| 2239 | if (glob_dot_filenames == 0) |
| 2240 | _ignore_completion_names (names, ignore_dot_names); |
| 2241 | #endif |
| 2242 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2243 | setup_ignore_patterns (&fignore); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2244 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2245 | if (fignore.num_ignores == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2246 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2247 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2248 | _ignore_completion_names (names, name_is_acceptable); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2249 | |
| 2250 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | /* Return 1 if NAME is a directory. */ |
| 2254 | static int |
| 2255 | test_for_directory (name) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2256 | const char *name; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2257 | { |
| 2258 | struct stat finfo; |
| 2259 | char *fn; |
| 2260 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2261 | fn = bash_tilde_expand (name, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2262 | if (stat (fn, &finfo) != 0) |
| 2263 | { |
| 2264 | free (fn); |
| 2265 | return 0; |
| 2266 | } |
| 2267 | free (fn); |
| 2268 | return (S_ISDIR (finfo.st_mode)); |
| 2269 | } |
| 2270 | |
| 2271 | /* Remove files from NAMES, leaving directories. */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2272 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2273 | bash_ignore_filenames (names) |
| 2274 | char **names; |
| 2275 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2276 | _ignore_completion_names (names, test_for_directory); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2277 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2280 | static int |
| 2281 | return_zero (name) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2282 | const char *name; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2283 | { |
| 2284 | return 0; |
| 2285 | } |
| 2286 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2287 | static int |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2288 | bash_ignore_everything (names) |
| 2289 | char **names; |
| 2290 | { |
| 2291 | _ignore_completion_names (names, return_zero); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2292 | return 0; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2295 | /* Simulate the expansions that will be performed by |
| 2296 | rl_filename_completion_function. This must be called with the address of |
| 2297 | a pointer to malloc'd memory. */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2298 | static void |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2299 | bash_directory_expansion (dirname) |
| 2300 | char **dirname; |
| 2301 | { |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2302 | char *d, *nd; |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2303 | |
| 2304 | d = savestring (*dirname); |
| 2305 | |
| 2306 | if (rl_directory_rewrite_hook) |
| 2307 | (*rl_directory_rewrite_hook) (&d); |
| 2308 | |
| 2309 | if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&d)) |
| 2310 | { |
| 2311 | free (*dirname); |
| 2312 | *dirname = d; |
| 2313 | } |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2314 | else if (rl_completion_found_quote) |
| 2315 | { |
| 2316 | nd = bash_dequote_filename (d, rl_completion_quote_character); |
| 2317 | free (*dirname); |
| 2318 | free (d); |
| 2319 | *dirname = nd; |
| 2320 | } |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2323 | /* Handle symbolic link references and other directory name |
| 2324 | expansions while hacking completion. */ |
| 2325 | static int |
| 2326 | bash_directory_completion_hook (dirname) |
| 2327 | char **dirname; |
| 2328 | { |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2329 | char *local_dirname, *new_dirname, *t; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2330 | int return_value, should_expand_dirname; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2331 | WORD_LIST *wl; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2332 | struct stat sb; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2333 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2334 | return_value = should_expand_dirname = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2335 | local_dirname = *dirname; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2336 | |
| 2337 | #if 0 |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2338 | should_expand_dirname = xstrchr (local_dirname, '$') || xstrchr (local_dirname, '`'); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2339 | #else |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2340 | if (xstrchr (local_dirname, '$')) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2341 | should_expand_dirname = 1; |
| 2342 | else |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2343 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2344 | t = xstrchr (local_dirname, '`'); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2345 | if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0) |
| 2346 | should_expand_dirname = 1; |
| 2347 | } |
| 2348 | #endif |
| 2349 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2350 | #if defined (HAVE_LSTAT) |
| 2351 | if (should_expand_dirname && lstat (local_dirname, &sb) == 0) |
| 2352 | #else |
| 2353 | if (should_expand_dirname && stat (local_dirname, &sb) == 0) |
| 2354 | #endif |
| 2355 | should_expand_dirname = 0; |
| 2356 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2357 | if (should_expand_dirname) |
| 2358 | { |
| 2359 | new_dirname = savestring (local_dirname); |
| Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame^] | 2360 | 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] | 2361 | if (wl) |
| 2362 | { |
| 2363 | *dirname = string_list (wl); |
| 2364 | /* Tell the completer to replace the directory name only if we |
| 2365 | actually expanded something. */ |
| 2366 | return_value = STREQ (local_dirname, *dirname) == 0; |
| 2367 | free (local_dirname); |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2368 | free (new_dirname); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2369 | dispose_words (wl); |
| 2370 | local_dirname = *dirname; |
| 2371 | } |
| 2372 | else |
| 2373 | { |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 2374 | free (new_dirname); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2375 | free (local_dirname); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2376 | *dirname = (char *)xmalloc (1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2377 | **dirname = '\0'; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2378 | return 1; |
| 2379 | } |
| 2380 | } |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 2381 | else |
| 2382 | { |
| 2383 | /* Dequote the filename even if we don't expand it. */ |
| 2384 | new_dirname = bash_dequote_filename (local_dirname, rl_completion_quote_character); |
| 2385 | free (local_dirname); |
| 2386 | local_dirname = *dirname = new_dirname; |
| 2387 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2388 | |
| 2389 | if (!no_symbolic_links && (local_dirname[0] != '.' || local_dirname[1])) |
| 2390 | { |
| 2391 | char *temp1, *temp2; |
| 2392 | int len1, len2; |
| 2393 | |
| 2394 | t = get_working_directory ("symlink-hook"); |
| 2395 | temp1 = make_absolute (local_dirname, t); |
| 2396 | free (t); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2397 | temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2398 | /* If we can't canonicalize, bail. */ |
| 2399 | if (temp2 == 0) |
| 2400 | { |
| 2401 | free (temp1); |
| 2402 | return 1; |
| 2403 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2404 | len1 = strlen (temp1); |
| 2405 | if (temp1[len1 - 1] == '/') |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2406 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2407 | len2 = strlen (temp2); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2408 | if (len2 > 2) /* don't append `/' to `/' or `//' */ |
| 2409 | { |
| 2410 | temp2 = (char *)xrealloc (temp2, len2 + 2); |
| 2411 | temp2[len2] = '/'; |
| 2412 | temp2[len2 + 1] = '\0'; |
| 2413 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2414 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2415 | free (local_dirname); |
| 2416 | *dirname = temp2; |
| 2417 | free (temp1); |
| 2418 | } |
| 2419 | return (return_value); |
| 2420 | } |
| 2421 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2422 | static char **history_completion_array = (char **)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2423 | static int harry_size; |
| 2424 | static int harry_len; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2425 | |
| 2426 | static void |
| 2427 | build_history_completion_array () |
| 2428 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2429 | register int i, j; |
| 2430 | HIST_ENTRY **hlist; |
| 2431 | char **tokens; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2432 | |
| 2433 | /* First, clear out the current dynamic history completion list. */ |
| 2434 | if (harry_size) |
| 2435 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2436 | strvec_dispose (history_completion_array); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2437 | history_completion_array = (char **)NULL; |
| 2438 | harry_size = 0; |
| 2439 | harry_len = 0; |
| 2440 | } |
| 2441 | |
| 2442 | /* Next, grovel each line of history, making each shell-sized token |
| 2443 | a separate entry in the history_completion_array. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2444 | hlist = history_list (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2445 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2446 | if (hlist) |
| 2447 | { |
| 2448 | for (i = 0; hlist[i]; i++) |
| 2449 | { |
| 2450 | /* Separate each token, and place into an array. */ |
| 2451 | tokens = history_tokenize (hlist[i]->line); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2452 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2453 | for (j = 0; tokens && tokens[j]; j++) |
| 2454 | { |
| 2455 | if (harry_len + 2 > harry_size) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2456 | history_completion_array = strvec_resize (history_completion_array, harry_size += 10); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2457 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2458 | history_completion_array[harry_len++] = tokens[j]; |
| 2459 | history_completion_array[harry_len] = (char *)NULL; |
| 2460 | } |
| 2461 | free (tokens); |
| 2462 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2463 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2464 | /* Sort the complete list of tokens. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2465 | qsort (history_completion_array, harry_len, sizeof (char *), (QSFUNC *)strvec_strcmp); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2466 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | static char * |
| 2470 | history_completion_generator (hint_text, state) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2471 | const char *hint_text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2472 | int state; |
| 2473 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2474 | static int local_index, len; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2475 | static const char *text; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2476 | |
| 2477 | /* If this is the first call to the generator, then initialize the |
| 2478 | list of strings to complete over. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2479 | if (state == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2480 | { |
| 2481 | local_index = 0; |
| 2482 | build_history_completion_array (); |
| 2483 | text = hint_text; |
| 2484 | len = strlen (text); |
| 2485 | } |
| 2486 | |
| 2487 | while (history_completion_array && history_completion_array[local_index]) |
| 2488 | { |
| 2489 | if (strncmp (text, history_completion_array[local_index++], len) == 0) |
| 2490 | return (savestring (history_completion_array[local_index - 1])); |
| 2491 | } |
| 2492 | return ((char *)NULL); |
| 2493 | } |
| 2494 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2495 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2496 | dynamic_complete_history (count, key) |
| 2497 | int count, key; |
| 2498 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2499 | int r; |
| 2500 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2501 | rl_compentry_func_t *orig_func; |
| 2502 | rl_completion_func_t *orig_attempt_func; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2503 | |
| 2504 | orig_func = rl_completion_entry_function; |
| 2505 | orig_attempt_func = rl_attempted_completion_function; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2506 | rl_completion_entry_function = history_completion_generator; |
| 2507 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2508 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2509 | /* XXX - use rl_completion_mode here? */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2510 | if (rl_last_func == dynamic_complete_history) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2511 | r = rl_complete_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2512 | else |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2513 | r = rl_complete_internal (TAB); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2514 | |
| 2515 | rl_completion_entry_function = orig_func; |
| 2516 | rl_attempted_completion_function = orig_attempt_func; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2517 | return r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2520 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2521 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2522 | bash_complete_username (ignore, ignore2) |
| 2523 | int ignore, ignore2; |
| 2524 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2525 | return bash_complete_username_internal (rl_completion_mode (bash_complete_username)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2528 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2529 | bash_possible_username_completions (ignore, ignore2) |
| 2530 | int ignore, ignore2; |
| 2531 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2532 | return bash_complete_username_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2535 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2536 | bash_complete_username_internal (what_to_do) |
| 2537 | int what_to_do; |
| 2538 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2539 | return bash_specific_completion (what_to_do, rl_username_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2542 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2543 | bash_complete_filename (ignore, ignore2) |
| 2544 | int ignore, ignore2; |
| 2545 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2546 | return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2549 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2550 | bash_possible_filename_completions (ignore, ignore2) |
| 2551 | int ignore, ignore2; |
| 2552 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2553 | return bash_complete_filename_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2556 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2557 | bash_complete_filename_internal (what_to_do) |
| 2558 | int what_to_do; |
| 2559 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2560 | rl_compentry_func_t *orig_func; |
| 2561 | rl_completion_func_t *orig_attempt_func; |
| 2562 | rl_icppfunc_t *orig_dir_func; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2563 | /*const*/ char *orig_rl_completer_word_break_characters; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2564 | int r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2565 | |
| 2566 | orig_func = rl_completion_entry_function; |
| 2567 | orig_attempt_func = rl_attempted_completion_function; |
| 2568 | orig_dir_func = rl_directory_completion_hook; |
| 2569 | orig_rl_completer_word_break_characters = rl_completer_word_break_characters; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2570 | rl_completion_entry_function = rl_filename_completion_function; |
| 2571 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; |
| 2572 | rl_directory_completion_hook = (rl_icppfunc_t *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2573 | rl_completer_word_break_characters = " \t\n\"\'"; |
| 2574 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2575 | r = rl_complete_internal (what_to_do); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2576 | |
| 2577 | rl_completion_entry_function = orig_func; |
| 2578 | rl_attempted_completion_function = orig_attempt_func; |
| 2579 | rl_directory_completion_hook = orig_dir_func; |
| 2580 | rl_completer_word_break_characters = orig_rl_completer_word_break_characters; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2581 | |
| 2582 | return r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2585 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2586 | bash_complete_hostname (ignore, ignore2) |
| 2587 | int ignore, ignore2; |
| 2588 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2589 | return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2592 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2593 | bash_possible_hostname_completions (ignore, ignore2) |
| 2594 | int ignore, ignore2; |
| 2595 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2596 | return bash_complete_hostname_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2599 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2600 | bash_complete_variable (ignore, ignore2) |
| 2601 | int ignore, ignore2; |
| 2602 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2603 | return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2606 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2607 | bash_possible_variable_completions (ignore, ignore2) |
| 2608 | int ignore, ignore2; |
| 2609 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2610 | return bash_complete_variable_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2613 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2614 | bash_complete_command (ignore, ignore2) |
| 2615 | int ignore, ignore2; |
| 2616 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2617 | return bash_complete_command_internal (rl_completion_mode (bash_complete_command)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2620 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2621 | bash_possible_command_completions (ignore, ignore2) |
| 2622 | int ignore, ignore2; |
| 2623 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2624 | return bash_complete_command_internal ('?'); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2627 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2628 | bash_complete_hostname_internal (what_to_do) |
| 2629 | int what_to_do; |
| 2630 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2631 | return bash_specific_completion (what_to_do, hostname_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2634 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2635 | bash_complete_variable_internal (what_to_do) |
| 2636 | int what_to_do; |
| 2637 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2638 | return bash_specific_completion (what_to_do, variable_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2641 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2642 | bash_complete_command_internal (what_to_do) |
| 2643 | int what_to_do; |
| 2644 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2645 | return bash_specific_completion (what_to_do, command_word_completion_function); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2648 | static char *globtext; |
| 2649 | static char *globorig; |
| 2650 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2651 | static char * |
| 2652 | glob_complete_word (text, state) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2653 | const char *text; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2654 | int state; |
| 2655 | { |
| 2656 | static char **matches = (char **)NULL; |
| 2657 | static int ind; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2658 | int glen; |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2659 | char *ret, *ttext; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2660 | |
| 2661 | if (state == 0) |
| 2662 | { |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 2663 | rl_filename_completion_desired = 1; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2664 | FREE (matches); |
| 2665 | if (globorig != globtext) |
| 2666 | FREE (globorig); |
| 2667 | FREE (globtext); |
| 2668 | |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2669 | ttext = bash_tilde_expand (text, 0); |
| 2670 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2671 | if (rl_explicit_arg) |
| 2672 | { |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2673 | globorig = savestring (ttext); |
| 2674 | glen = strlen (ttext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2675 | globtext = (char *)xmalloc (glen + 2); |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2676 | strcpy (globtext, ttext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2677 | globtext[glen] = '*'; |
| 2678 | globtext[glen+1] = '\0'; |
| 2679 | } |
| 2680 | else |
| Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 2681 | globtext = globorig = savestring (ttext); |
| 2682 | |
| 2683 | if (ttext != text) |
| 2684 | free (ttext); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2685 | |
| 2686 | matches = shell_glob_filename (globtext); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2687 | if (GLOB_FAILED (matches)) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2688 | matches = (char **)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2689 | ind = 0; |
| 2690 | } |
| 2691 | |
| 2692 | ret = matches ? matches[ind] : (char *)NULL; |
| 2693 | ind++; |
| 2694 | return ret; |
| 2695 | } |
| 2696 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2697 | static int |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2698 | bash_glob_completion_internal (what_to_do) |
| 2699 | int what_to_do; |
| 2700 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2701 | return bash_specific_completion (what_to_do, glob_complete_word); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2704 | /* A special quoting function so we don't end up quoting globbing characters |
| 2705 | in the word if there are no matches or multiple matches. */ |
| 2706 | static char * |
| 2707 | bash_glob_quote_filename (s, rtype, qcp) |
| 2708 | char *s; |
| 2709 | int rtype; |
| 2710 | char *qcp; |
| 2711 | { |
| 2712 | if (globorig && qcp && *qcp == '\0' && STREQ (s, globorig)) |
| 2713 | return (savestring (s)); |
| 2714 | else |
| 2715 | return (bash_quote_filename (s, rtype, qcp)); |
| 2716 | } |
| 2717 | |
| 2718 | static int |
| 2719 | bash_glob_complete_word (count, key) |
| 2720 | int count, key; |
| 2721 | { |
| 2722 | int r; |
| 2723 | rl_quote_func_t *orig_quoting_function; |
| 2724 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2725 | if (rl_editing_mode == EMACS_EDITING_MODE) |
| 2726 | rl_explicit_arg = 1; /* force `*' append */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2727 | orig_quoting_function = rl_filename_quoting_function; |
| 2728 | rl_filename_quoting_function = bash_glob_quote_filename; |
| 2729 | |
| 2730 | r = bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word)); |
| 2731 | |
| 2732 | rl_filename_quoting_function = orig_quoting_function; |
| 2733 | return r; |
| 2734 | } |
| 2735 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2736 | static int |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2737 | bash_glob_expand_word (count, key) |
| 2738 | int count, key; |
| 2739 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2740 | return bash_glob_completion_internal ('*'); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2743 | static int |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2744 | bash_glob_list_expansions (count, key) |
| 2745 | int count, key; |
| 2746 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2747 | return bash_glob_completion_internal ('?'); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2750 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2751 | bash_specific_completion (what_to_do, generator) |
| 2752 | int what_to_do; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2753 | rl_compentry_func_t *generator; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2754 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2755 | rl_compentry_func_t *orig_func; |
| 2756 | rl_completion_func_t *orig_attempt_func; |
| 2757 | int r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2758 | |
| 2759 | orig_func = rl_completion_entry_function; |
| 2760 | orig_attempt_func = rl_attempted_completion_function; |
| 2761 | rl_completion_entry_function = generator; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2762 | rl_attempted_completion_function = NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2763 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2764 | r = rl_complete_internal (what_to_do); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2765 | |
| 2766 | rl_completion_entry_function = orig_func; |
| 2767 | rl_attempted_completion_function = orig_attempt_func; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2768 | |
| 2769 | return r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2773 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 2774 | #if defined (VI_MODE) |
| 2775 | /* Completion, from vi mode's point of view. This is a modified version of |
| 2776 | rl_vi_complete which uses the bash globbing code to implement what POSIX |
| 2777 | specifies, which is to append a `*' and attempt filename generation (which |
| 2778 | has the side effect of expanding any globbing characters in the word). */ |
| 2779 | static int |
| 2780 | bash_vi_complete (count, key) |
| 2781 | int count, key; |
| 2782 | { |
| 2783 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
| 2784 | int p, r; |
| 2785 | char *t; |
| 2786 | |
| 2787 | if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point]))) |
| 2788 | { |
| 2789 | if (!whitespace (rl_line_buffer[rl_point + 1])) |
| 2790 | rl_vi_end_word (1, 'E'); |
| 2791 | rl_point++; |
| 2792 | } |
| 2793 | |
| 2794 | /* Find boundaries of current word, according to vi definition of a |
| 2795 | `bigword'. */ |
| 2796 | t = 0; |
| 2797 | if (rl_point > 0) |
| 2798 | { |
| 2799 | p = rl_point; |
| 2800 | rl_vi_bWord (1, 'B'); |
| 2801 | r = rl_point; |
| 2802 | rl_point = p; |
| 2803 | p = r; |
| 2804 | |
| 2805 | t = substring (rl_line_buffer, p, rl_point); |
| 2806 | } |
| 2807 | |
| 2808 | if (t && glob_pattern_p (t) == 0) |
| 2809 | rl_explicit_arg = 1; /* XXX - force glob_complete_word to append `*' */ |
| 2810 | FREE (t); |
| 2811 | |
| 2812 | if (key == '*') /* Expansion and replacement. */ |
| 2813 | r = bash_glob_expand_word (count, key); |
| 2814 | else if (key == '=') /* List possible completions. */ |
| 2815 | r = bash_glob_list_expansions (count, key); |
| 2816 | else if (key == '\\') /* Standard completion */ |
| 2817 | r = bash_glob_complete_word (count, key); |
| 2818 | else |
| 2819 | r = rl_complete (0, key); |
| 2820 | |
| 2821 | if (key == '*' || key == '\\') |
| 2822 | rl_vi_start_inserting (key, 1, 1); |
| 2823 | |
| 2824 | return (r); |
| 2825 | #else |
| 2826 | return rl_vi_complete (count, key); |
| 2827 | #endif /* !SPECIFIC_COMPLETION_FUNCTIONS */ |
| 2828 | } |
| 2829 | #endif /* VI_MODE */ |
| 2830 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2831 | /* Filename quoting for completion. */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2832 | /* A function to strip unquoted quote characters (single quotes, double |
| 2833 | quotes, and backslashes). It allows single quotes to appear |
| 2834 | within double quotes, and vice versa. It should be smarter. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2835 | static char * |
| 2836 | bash_dequote_filename (text, quote_char) |
| 2837 | char *text; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2838 | int quote_char; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2839 | { |
| 2840 | char *ret, *p, *r; |
| 2841 | int l, quoted; |
| 2842 | |
| 2843 | l = strlen (text); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2844 | ret = (char *)xmalloc (l + 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2845 | for (quoted = quote_char, p = text, r = ret; p && *p; p++) |
| 2846 | { |
| 2847 | /* Allow backslash-quoted characters to pass through unscathed. */ |
| 2848 | if (*p == '\\') |
| 2849 | { |
| 2850 | *r++ = *++p; |
| 2851 | if (*p == '\0') |
| 2852 | break; |
| 2853 | continue; |
| 2854 | } |
| 2855 | /* Close quote. */ |
| 2856 | if (quoted && *p == quoted) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2857 | { |
| 2858 | quoted = 0; |
| 2859 | continue; |
| 2860 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2861 | /* Open quote. */ |
| 2862 | if (quoted == 0 && (*p == '\'' || *p == '"')) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2863 | { |
| 2864 | quoted = *p; |
| 2865 | continue; |
| 2866 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2867 | *r++ = *p; |
| 2868 | } |
| 2869 | *r = '\0'; |
| 2870 | return ret; |
| 2871 | } |
| 2872 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2873 | /* Quote characters that the readline completion code would treat as |
| 2874 | word break characters with backslashes. Pass backslash-quoted |
| 2875 | characters through without examination. */ |
| 2876 | static char * |
| 2877 | quote_word_break_chars (text) |
| 2878 | char *text; |
| 2879 | { |
| 2880 | char *ret, *r, *s; |
| 2881 | int l; |
| 2882 | |
| 2883 | l = strlen (text); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2884 | ret = (char *)xmalloc ((2 * l) + 1); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2885 | for (s = text, r = ret; *s; s++) |
| 2886 | { |
| 2887 | /* Pass backslash-quoted characters through, including the backslash. */ |
| 2888 | if (*s == '\\') |
| 2889 | { |
| 2890 | *r++ = '\\'; |
| 2891 | *r++ = *++s; |
| 2892 | if (*s == '\0') |
| 2893 | break; |
| 2894 | continue; |
| 2895 | } |
| 2896 | /* OK, we have an unquoted character. Check its presence in |
| 2897 | rl_completer_word_break_characters. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2898 | if (xstrchr (rl_completer_word_break_characters, *s)) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2899 | *r++ = '\\'; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2900 | *r++ = *s; |
| 2901 | } |
| 2902 | *r = '\0'; |
| 2903 | return ret; |
| 2904 | } |
| 2905 | |
| 2906 | /* Quote a filename using double quotes, single quotes, or backslashes |
| 2907 | depending on the value of completion_quoting_style. If we're |
| 2908 | completing using backslashes, we need to quote some additional |
| 2909 | characters (those that readline treats as word breaks), so we call |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 2910 | quote_word_break_chars on the result. This returns newly-allocated |
| 2911 | memory. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2912 | static char * |
| 2913 | bash_quote_filename (s, rtype, qcp) |
| 2914 | char *s; |
| 2915 | int rtype; |
| 2916 | char *qcp; |
| 2917 | { |
| 2918 | char *rtext, *mtext, *ret; |
| 2919 | int rlen, cs; |
| 2920 | |
| 2921 | rtext = (char *)NULL; |
| 2922 | |
| 2923 | /* If RTYPE == MULT_MATCH, it means that there is |
| 2924 | more than one match. In this case, we do not add |
| 2925 | the closing quote or attempt to perform tilde |
| 2926 | expansion. If RTYPE == SINGLE_MATCH, we try |
| 2927 | to perform tilde expansion, because single and double |
| 2928 | quotes inhibit tilde expansion by the shell. */ |
| 2929 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2930 | cs = completion_quoting_style; |
| 2931 | /* Might need to modify the default completion style based on *qcp, |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2932 | since it's set to any user-provided opening quote. We also change |
| 2933 | to single-quoting if there is no user-provided opening quote and |
| 2934 | the word being completed contains newlines, since those are not |
| 2935 | quoted correctly using backslashes (a backslash-newline pair is |
| 2936 | special to the shell parser). */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2937 | if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && xstrchr (s, '\n')) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 2938 | cs = COMPLETE_SQUOTE; |
| 2939 | else if (*qcp == '"') |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2940 | cs = COMPLETE_DQUOTE; |
| 2941 | else if (*qcp == '\'') |
| 2942 | cs = COMPLETE_SQUOTE; |
| 2943 | #if defined (BANG_HISTORY) |
| 2944 | else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE && |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2945 | history_expansion_inhibited == 0 && xstrchr (s, '!')) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2946 | cs = COMPLETE_BSQUOTE; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2947 | |
| 2948 | if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE && |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2949 | history_expansion_inhibited == 0 && xstrchr (s, '!')) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2950 | { |
| 2951 | cs = COMPLETE_BSQUOTE; |
| 2952 | *qcp = '\0'; |
| 2953 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2954 | #endif |
| 2955 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 2956 | /* Don't tilde-expand backslash-quoted filenames, since only single and |
| 2957 | double quotes inhibit tilde expansion. */ |
| 2958 | mtext = s; |
| 2959 | if (mtext[0] == '~' && rtype == SINGLE_MATCH && cs != COMPLETE_BSQUOTE) |
| 2960 | mtext = bash_tilde_expand (s, 0); |
| 2961 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2962 | switch (cs) |
| 2963 | { |
| 2964 | case COMPLETE_DQUOTE: |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2965 | rtext = sh_double_quote (mtext); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2966 | break; |
| 2967 | case COMPLETE_SQUOTE: |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2968 | rtext = sh_single_quote (mtext); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2969 | break; |
| 2970 | case COMPLETE_BSQUOTE: |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 2971 | rtext = sh_backslash_quote (mtext); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2972 | break; |
| 2973 | } |
| 2974 | |
| 2975 | if (mtext != s) |
| 2976 | free (mtext); |
| 2977 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 2978 | /* We may need to quote additional characters: those that readline treats |
| 2979 | as word breaks that are not quoted by backslash_quote. */ |
| 2980 | if (rtext && cs == COMPLETE_BSQUOTE) |
| 2981 | { |
| 2982 | mtext = quote_word_break_chars (rtext); |
| 2983 | free (rtext); |
| 2984 | rtext = mtext; |
| 2985 | } |
| 2986 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2987 | /* Leave the opening quote intact. The readline completion code takes |
| 2988 | care of avoiding doubled opening quotes. */ |
| 2989 | rlen = strlen (rtext); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2990 | ret = (char *)xmalloc (rlen + 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2991 | strcpy (ret, rtext); |
| 2992 | |
| 2993 | /* If there are multiple matches, cut off the closing quote. */ |
| 2994 | if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE) |
| 2995 | ret[rlen - 1] = '\0'; |
| 2996 | free (rtext); |
| 2997 | return ret; |
| 2998 | } |
| 2999 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3000 | /* Support for binding readline key sequences to Unix commands. */ |
| 3001 | static Keymap cmd_xmap; |
| 3002 | |
| 3003 | static int |
| 3004 | bash_execute_unix_command (count, key) |
| 3005 | int count; /* ignored */ |
| 3006 | int key; |
| 3007 | { |
| 3008 | Keymap ckmap; /* current keymap */ |
| 3009 | Keymap xkmap; /* unix command executing keymap */ |
| 3010 | register int i; |
| 3011 | char *cmd; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3012 | sh_parser_state_t ps; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3013 | |
| 3014 | /* First, we need to find the right command to execute. This is tricky, |
| 3015 | because we might have already indirected into another keymap. */ |
| 3016 | ckmap = rl_get_keymap (); |
| 3017 | if (ckmap != rl_executing_keymap) |
| 3018 | { |
| 3019 | /* bogus. we have to search. only handle one level of indirection. */ |
| 3020 | for (i = 0; i < KEYMAP_SIZE; i++) |
| 3021 | { |
| 3022 | if (ckmap[i].type == ISKMAP && (Keymap)ckmap[i].function == rl_executing_keymap) |
| 3023 | break; |
| 3024 | } |
| 3025 | if (i < KEYMAP_SIZE) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3026 | xkmap = (Keymap)cmd_xmap[i].function; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3027 | else |
| 3028 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3029 | rl_crlf (); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3030 | internal_error (_("bash_execute_unix_command: cannot find keymap for command")); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3031 | rl_forced_update_display (); |
| 3032 | return 1; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3033 | } |
| 3034 | } |
| 3035 | else |
| 3036 | xkmap = cmd_xmap; |
| 3037 | |
| 3038 | cmd = (char *)xkmap[key].function; |
| 3039 | |
| 3040 | if (cmd == 0) |
| 3041 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3042 | rl_ding (); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3043 | return 1; |
| 3044 | } |
| 3045 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3046 | rl_crlf (); /* move to a new line */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3047 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3048 | save_parser_state (&ps); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3049 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3050 | cmd = savestring (cmd); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3051 | parse_and_execute (cmd, "bash_execute_unix_command", SEVAL_NOHIST); |
| 3052 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3053 | restore_parser_state (&ps); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3054 | |
| 3055 | /* and restore the readline buffer and display after command execution. */ |
| 3056 | rl_forced_update_display (); |
| 3057 | return 0; |
| 3058 | } |
| 3059 | |
| 3060 | static void |
| 3061 | init_unix_command_map () |
| 3062 | { |
| 3063 | cmd_xmap = rl_make_bare_keymap (); |
| 3064 | } |
| 3065 | |
| 3066 | static int |
| 3067 | isolate_sequence (string, ind, need_dquote, startp) |
| 3068 | char *string; |
| 3069 | int ind, need_dquote, *startp; |
| 3070 | { |
| 3071 | register int i; |
| 3072 | int c, passc, delim; |
| 3073 | |
| 3074 | for (i = ind; string[i] && whitespace (string[i]); i++) |
| 3075 | ; |
| 3076 | /* NEED_DQUOTE means that the first non-white character *must* be `"'. */ |
| 3077 | if (need_dquote && string[i] != '"') |
| 3078 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3079 | builtin_error (_("%s: first non-whitespace character is not `\"'"), string); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3080 | return -1; |
| 3081 | } |
| 3082 | |
| 3083 | /* We can have delimited strings even if NEED_DQUOTE == 0, like the command |
| 3084 | string to bind the key sequence to. */ |
| 3085 | delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0; |
| 3086 | |
| 3087 | if (startp) |
| 3088 | *startp = delim ? ++i : i; |
| 3089 | |
| 3090 | for (passc = 0; c = string[i]; i++) |
| 3091 | { |
| 3092 | if (passc) |
| 3093 | { |
| 3094 | passc = 0; |
| 3095 | continue; |
| 3096 | } |
| 3097 | if (c == '\\') |
| 3098 | { |
| 3099 | passc++; |
| 3100 | continue; |
| 3101 | } |
| 3102 | if (c == delim) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3103 | break; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | if (delim && string[i] != delim) |
| 3107 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3108 | builtin_error (_("no closing `%c' in %s"), delim, string); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3109 | return -1; |
| 3110 | } |
| 3111 | |
| 3112 | return i; |
| 3113 | } |
| 3114 | |
| 3115 | int |
| 3116 | bind_keyseq_to_unix_command (line) |
| 3117 | char *line; |
| 3118 | { |
| 3119 | Keymap kmap; |
| 3120 | char *kseq, *value; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 3121 | int i, kstart; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3122 | |
| 3123 | if (cmd_xmap == 0) |
| 3124 | init_unix_command_map (); |
| 3125 | |
| 3126 | kmap = rl_get_keymap (); |
| 3127 | |
| 3128 | /* We duplicate some of the work done by rl_parse_and_bind here, but |
| 3129 | this code only has to handle `"keyseq": ["]command["]' and can |
| 3130 | generate an error for anything else. */ |
| 3131 | i = isolate_sequence (line, 0, 1, &kstart); |
| 3132 | if (i < 0) |
| 3133 | return -1; |
| 3134 | |
| 3135 | /* Create the key sequence string to pass to rl_generic_bind */ |
| 3136 | kseq = substring (line, kstart, i); |
| 3137 | |
| 3138 | for ( ; line[i] && line[i] != ':'; i++) |
| 3139 | ; |
| 3140 | if (line[i] != ':') |
| 3141 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3142 | builtin_error (_("%s: missing colon separator"), line); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3143 | return -1; |
| 3144 | } |
| 3145 | |
| 3146 | i = isolate_sequence (line, i + 1, 0, &kstart); |
| 3147 | if (i < 0) |
| 3148 | return -1; |
| 3149 | |
| 3150 | /* Create the value string containing the command to execute. */ |
| 3151 | value = substring (line, kstart, i); |
| 3152 | |
| 3153 | /* Save the command to execute and the key sequence in the CMD_XMAP */ |
| 3154 | rl_generic_bind (ISMACR, kseq, value, cmd_xmap); |
| 3155 | |
| 3156 | /* and bind the key sequence in the current keymap to a function that |
| 3157 | understands how to execute from CMD_XMAP */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3158 | rl_bind_keyseq_in_map (kseq, bash_execute_unix_command, kmap); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3159 | |
| 3160 | return 0; |
| 3161 | } |
| 3162 | |
| 3163 | /* Used by the programmable completion code. Complete TEXT as a filename, |
| 3164 | but return only directories as matches. Dequotes the filename before |
| 3165 | attempting to find matches. */ |
| 3166 | char ** |
| 3167 | bash_directory_completion_matches (text) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3168 | const char *text; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3169 | { |
| 3170 | char **m1; |
| 3171 | char *dfn; |
| 3172 | int qc; |
| 3173 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3174 | qc = rl_dispatching ? rl_completion_quote_character : 0; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 3175 | dfn = bash_dequote_filename ((char *)text, qc); |
| 3176 | m1 = rl_completion_matches (dfn, rl_filename_completion_function); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3177 | free (dfn); |
| 3178 | |
| 3179 | if (m1 == 0 || m1[0] == 0) |
| 3180 | return m1; |
| 3181 | /* We don't bother recomputing the lcd of the matches, because it will just |
| 3182 | get thrown away by the programmable completion code and recomputed |
| 3183 | later. */ |
| 3184 | (void)bash_ignore_filenames (m1); |
| 3185 | return m1; |
| 3186 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 3187 | |
| 3188 | char * |
| 3189 | bash_dequote_text (text) |
| 3190 | const char *text; |
| 3191 | { |
| 3192 | char *dtxt; |
| 3193 | int qc; |
| 3194 | |
| 3195 | qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0; |
| 3196 | dtxt = bash_dequote_filename ((char *)text, qc); |
| 3197 | return (dtxt); |
| 3198 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 3199 | #endif /* READLINE */ |