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