1 /* bashline.c -- Bash's interface to the readline library. */
3 /* Copyright (C) 1987-2021 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (READLINE)
25 #include "bashtypes.h"
26 #include "posixstat.h"
28 #if defined (HAVE_UNISTD_H)
32 #if defined (HAVE_GRP_H)
36 #if defined (HAVE_NETDB_H)
43 #include "chartypes.h"
53 #include "execute_cmd.h"
61 #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
62 # include <mbstr.h> /* mbschr */
65 #include "builtins/common.h"
66 #include "builtins/builtext.h" /* for read_builtin */
68 #include <readline/rlconf.h>
69 #include <readline/readline.h>
70 #include <readline/history.h>
71 #include <readline/rlmbutil.h>
73 #include <glob/glob.h>
79 #if defined (PROGRAMMABLE_COMPLETION)
80 # include "pcomplete.h"
83 /* These should agree with the defines for emacs_mode and vi_mode in
84 rldefs.h, even though that's not a public readline header file. */
85 #ifndef EMACS_EDITING_MODE
86 # define NO_EDITING_MODE -1
87 # define EMACS_EDITING_MODE 1
88 # define VI_EDITING_MODE 0
91 /* Copied from rldefs.h, since that's not a public readline header file. */
92 #ifndef FUNCTION_TO_KEYMAP
95 # define FUNCTION_TO_KEYMAP(map, key) (Keymap)((int)map[key].function)
96 # define KEYMAP_TO_FUNCTION(data) (rl_command_func_t *)((int)(data))
98 # define FUNCTION_TO_KEYMAP(map, key) (Keymap)(map[key].function)
99 # define KEYMAP_TO_FUNCTION(data) (rl_command_func_t *)(data)
104 #define RL_BOOLEAN_VARIABLE_VALUE(s) ((s)[0] == 'o' && (s)[1] == 'n' && (s)[2] == '\0')
106 #if defined (BRACE_COMPLETION)
107 extern int bash_brace_completion
PARAMS((int, int));
108 #endif /* BRACE_COMPLETION */
110 /* To avoid including curses.h/term.h/termcap.h and that whole mess. */
112 extern int tputs
PARAMS((const char *string
, int nlines
, void (*outx
)(int)));
114 extern int tputs
PARAMS((const char *string
, int nlines
, int (*outx
)(int)));
117 /* Forward declarations */
119 /* Functions bound to keys in Readline for Bash users. */
120 static int shell_expand_line
PARAMS((int, int));
121 static int display_shell_version
PARAMS((int, int));
123 static int bash_ignore_filenames
PARAMS((char **));
124 static int bash_ignore_everything
PARAMS((char **));
125 static int bash_progcomp_ignore_filenames
PARAMS((char **));
127 #if defined (BANG_HISTORY)
128 static char *history_expand_line_internal
PARAMS((char *));
129 static int history_expand_line
PARAMS((int, int));
130 static int tcsh_magic_space
PARAMS((int, int));
131 #endif /* BANG_HISTORY */
133 static int alias_expand_line
PARAMS((int, int));
135 #if defined (BANG_HISTORY) && defined (ALIAS)
136 static int history_and_alias_expand_line
PARAMS((int, int));
139 static int bash_forward_shellword
PARAMS((int, int));
140 static int bash_backward_shellword
PARAMS((int, int));
141 static int bash_kill_shellword
PARAMS((int, int));
142 static int bash_backward_kill_shellword
PARAMS((int, int));
143 static int bash_transpose_shellwords
PARAMS((int, int));
145 static int bash_spell_correct_shellword
PARAMS((int, int));
147 /* Helper functions for Readline. */
148 static char *restore_tilde
PARAMS((char *, char *));
149 static char *maybe_restore_tilde
PARAMS((char *, char *));
151 static char *bash_filename_rewrite_hook
PARAMS((char *, int));
153 static void bash_directory_expansion
PARAMS((char **));
154 static int bash_filename_stat_hook
PARAMS((char **));
155 static int bash_command_name_stat_hook
PARAMS((char **));
156 static int bash_directory_completion_hook
PARAMS((char **));
157 static int filename_completion_ignore
PARAMS((char **));
158 static int bash_push_line
PARAMS((void));
160 static int executable_completion
PARAMS((const char *, int));
162 static rl_icppfunc_t
*save_directory_hook
PARAMS((void));
163 static void restore_directory_hook
PARAMS((rl_icppfunc_t
));
165 static int directory_exists
PARAMS((const char *, int));
167 static void cleanup_expansion_error
PARAMS((void));
168 static void maybe_make_readline_line
PARAMS((char *));
169 static void set_up_new_line
PARAMS((char *));
171 static int check_redir
PARAMS((int));
172 static char **attempt_shell_completion
PARAMS((const char *, int, int));
173 static char *variable_completion_function
PARAMS((const char *, int));
174 static char *hostname_completion_function
PARAMS((const char *, int));
175 static char *command_subst_completion_function
PARAMS((const char *, int));
177 static void build_history_completion_array
PARAMS((void));
178 static char *history_completion_generator
PARAMS((const char *, int));
179 static int dynamic_complete_history
PARAMS((int, int));
180 static int bash_dabbrev_expand
PARAMS((int, int));
182 static void initialize_hostname_list
PARAMS((void));
183 static void add_host_name
PARAMS((char *));
184 static void snarf_hosts_from_file
PARAMS((char *));
185 static char **hostnames_matching
PARAMS((char *));
187 static void _ignore_completion_names
PARAMS((char **, sh_ignore_func_t
*));
188 static int name_is_acceptable
PARAMS((const char *));
189 static int test_for_directory
PARAMS((const char *));
190 static int test_for_canon_directory
PARAMS((const char *));
191 static int return_zero
PARAMS((const char *));
193 static char *bash_dequote_filename
PARAMS((char *, int));
194 static char *quote_word_break_chars
PARAMS((char *));
195 static void set_filename_bstab
PARAMS((const char *));
196 static char *bash_quote_filename
PARAMS((char *, int, char *));
199 static void putx
PARAMS((int));
201 static int putx
PARAMS((int));
203 static int readline_get_char_offset
PARAMS((int));
204 static void readline_set_char_offset
PARAMS((int, int *));
206 static Keymap get_cmd_xmap_from_edit_mode
PARAMS((void));
207 static Keymap get_cmd_xmap_from_keymap
PARAMS((Keymap
));
209 static void init_unix_command_map
PARAMS((void));
210 static int isolate_sequence
PARAMS((char *, int, int, int *));
212 static int set_saved_history
PARAMS((void));
215 static int posix_edit_macros
PARAMS((int, int));
218 static int bash_event_hook
PARAMS((void));
220 #if defined (PROGRAMMABLE_COMPLETION)
221 static int find_cmd_start
PARAMS((int));
222 static int find_cmd_end
PARAMS((int));
223 static char *find_cmd_name
PARAMS((int, int *, int *));
224 static char *prog_complete_return
PARAMS((const char *, int));
226 static char **prog_complete_matches
;
229 extern int no_symbolic_links
;
230 extern STRING_INT_ALIST word_token_alist
[];
231 extern sh_timer
*read_timeout
;
233 /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
234 completion functions which indicate what type of completion should be
235 done (at or before point) that can be bound to key sequences with
236 the readline library. */
237 #define SPECIFIC_COMPLETION_FUNCTIONS
239 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
240 static int bash_specific_completion
PARAMS((int, rl_compentry_func_t
*));
242 static int bash_complete_filename_internal
PARAMS((int));
243 static int bash_complete_username_internal
PARAMS((int));
244 static int bash_complete_hostname_internal
PARAMS((int));
245 static int bash_complete_variable_internal
PARAMS((int));
246 static int bash_complete_command_internal
PARAMS((int));
248 static int bash_complete_filename
PARAMS((int, int));
249 static int bash_possible_filename_completions
PARAMS((int, int));
250 static int bash_complete_username
PARAMS((int, int));
251 static int bash_possible_username_completions
PARAMS((int, int));
252 static int bash_complete_hostname
PARAMS((int, int));
253 static int bash_possible_hostname_completions
PARAMS((int, int));
254 static int bash_complete_variable
PARAMS((int, int));
255 static int bash_possible_variable_completions
PARAMS((int, int));
256 static int bash_complete_command
PARAMS((int, int));
257 static int bash_possible_command_completions
PARAMS((int, int));
259 static int completion_glob_pattern
PARAMS((char *));
260 static char *glob_complete_word
PARAMS((const char *, int));
261 static int bash_glob_completion_internal
PARAMS((int));
262 static int bash_glob_complete_word
PARAMS((int, int));
263 static int bash_glob_expand_word
PARAMS((int, int));
264 static int bash_glob_list_expansions
PARAMS((int, int));
266 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
268 static int edit_and_execute_command
PARAMS((int, int, int, char *));
269 #if defined (VI_MODE)
270 static int vi_edit_and_execute_command
PARAMS((int, int));
271 static int bash_vi_complete
PARAMS((int, int));
273 static int emacs_edit_and_execute_command
PARAMS((int, int));
275 /* Non-zero once initialize_readline () has been called. */
276 int bash_readline_initialized
= 0;
278 /* If non-zero, we do hostname completion, breaking words at `@' and
279 trying to complete the stuff after the `@' from our own internal
281 int perform_hostname_completion
= 1;
283 /* If non-zero, we don't do command completion on an empty line. */
284 int no_empty_command_completion
;
286 /* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the
287 only possible matches. Set to 0 if you want to match filenames if they
288 are the only possible matches, even if FIGNORE says to. */
289 int force_fignore
= 1;
291 /* Perform spelling correction on directory names during word completion */
292 int dircomplete_spelling
= 0;
294 /* Expand directory names during word/filename completion. */
295 #if DIRCOMPLETE_EXPAND_DEFAULT
296 int dircomplete_expand
= 1;
297 int dircomplete_expand_relpath
= 1;
299 int dircomplete_expand
= 0;
300 int dircomplete_expand_relpath
= 0;
303 /* When non-zero, perform `normal' shell quoting on completed filenames
304 even when the completed name contains a directory name with a shell
305 variable reference, so dollar signs in a filename get quoted appropriately.
306 Set to zero to remove dollar sign (and braces or parens as needed) from
307 the set of characters that will be quoted. */
308 int complete_fullquote
= 1;
310 static char *bash_completer_word_break_characters
= " \t\n\"'@><=;|&(:";
311 static char *bash_nohostname_word_break_characters
= " \t\n\"'><=;|&(:";
314 static const char *default_filename_quote_characters
= " \t\n\\\"'@<>=;|&()#$`?*[!:{~"; /*}*/
315 static char *custom_filename_quote_characters
= 0;
316 static char filename_bstab
[256];
318 static rl_hook_func_t
*old_rl_startup_hook
= (rl_hook_func_t
*)NULL
;
320 static int dot_in_path
= 0;
322 /* Set to non-zero when dabbrev-expand is running */
323 static int dabbrev_expand_active
= 0;
325 /* What kind of quoting is performed by bash_quote_filename:
326 COMPLETE_DQUOTE = double-quoting the filename
327 COMPLETE_SQUOTE = single_quoting the filename
328 COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
330 #define COMPLETE_DQUOTE 1
331 #define COMPLETE_SQUOTE 2
332 #define COMPLETE_BSQUOTE 3
333 static int completion_quoting_style
= COMPLETE_BSQUOTE
;
335 /* Flag values for the final argument to bash_default_completion */
336 #define DEFCOMP_CMDPOS 1
338 static rl_command_func_t
*vi_tab_binding
= rl_complete
;
340 /* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
341 Called when the shell is put into or out of `posix' mode. */
343 posix_readline_initialize (on_or_off
)
346 static char kseq
[2] = { CTRL ('I'), 0 }; /* TAB */
349 rl_variable_bind ("comment-begin", "#");
350 #if defined (VI_MODE)
353 vi_tab_binding
= rl_function_of_keyseq (kseq
, vi_insertion_keymap
, (int *)NULL
);
354 rl_bind_key_in_map (CTRL ('I'), rl_insert
, vi_insertion_keymap
);
358 if (rl_function_of_keyseq (kseq
, vi_insertion_keymap
, (int *)NULL
) == rl_insert
)
359 rl_bind_key_in_map (CTRL ('I'), vi_tab_binding
, vi_insertion_keymap
);
365 reset_completer_word_break_chars ()
367 rl_completer_word_break_characters
= perform_hostname_completion
? savestring (bash_completer_word_break_characters
) : savestring (bash_nohostname_word_break_characters
);
370 /* When this function returns, rl_completer_word_break_characters points to
371 dynamically allocated memory. */
373 enable_hostname_completion (on_or_off
)
377 char *at
, *nv
, *nval
;
379 old_value
= perform_hostname_completion
;
383 perform_hostname_completion
= 1;
384 rl_special_prefixes
= "$@";
388 perform_hostname_completion
= 0;
389 rl_special_prefixes
= "$";
392 /* Now we need to figure out how to appropriately modify and assign
393 rl_completer_word_break_characters depending on whether we want
394 hostname completion on or off. */
396 /* If this is the first time this has been called
397 (bash_readline_initialized == 0), use the sames values as before, but
398 allocate new memory for rl_completer_word_break_characters. */
400 if (bash_readline_initialized
== 0 &&
401 (rl_completer_word_break_characters
== 0 ||
402 rl_completer_word_break_characters
== rl_basic_word_break_characters
))
405 rl_completer_word_break_characters
= savestring (bash_completer_word_break_characters
);
407 rl_completer_word_break_characters
= savestring (bash_nohostname_word_break_characters
);
411 /* See if we have anything to do. */
412 at
= strchr (rl_completer_word_break_characters
, '@');
413 if ((at
== 0 && on_or_off
== 0) || (at
!= 0 && on_or_off
!= 0))
416 /* We have something to do. Do it. */
417 nval
= (char *)xmalloc (strlen (rl_completer_word_break_characters
) + 1 + on_or_off
);
421 /* Turn it off -- just remove `@' from word break chars. We want
422 to remove all occurrences of `@' from the char list, so we loop
423 rather than just copy the rest of the list over AT. */
424 for (nv
= nval
, at
= rl_completer_word_break_characters
; *at
; )
434 strcpy (nval
+ 1, rl_completer_word_break_characters
);
437 free (rl_completer_word_break_characters
);
438 rl_completer_word_break_characters
= nval
;
444 /* Called once from parse.y if we are going to use readline. */
446 initialize_readline ()
448 rl_command_func_t
*func
;
451 if (bash_readline_initialized
)
454 rl_terminal_name
= get_string_value ("TERM");
456 rl_outstream
= stderr
;
458 /* Allow conditional parsing of the ~/.inputrc file. */
459 rl_readline_name
= "Bash";
461 /* Add bindable names before calling rl_initialize so they may be
462 referenced in the various inputrc files. */
463 rl_add_defun ("shell-expand-line", shell_expand_line
, -1);
465 rl_add_defun ("history-expand-line", history_expand_line
, -1);
466 rl_add_defun ("magic-space", tcsh_magic_space
, -1);
469 rl_add_defun ("shell-forward-word", bash_forward_shellword
, -1);
470 rl_add_defun ("shell-backward-word", bash_backward_shellword
, -1);
471 rl_add_defun ("shell-kill-word", bash_kill_shellword
, -1);
472 rl_add_defun ("shell-backward-kill-word", bash_backward_kill_shellword
, -1);
473 rl_add_defun ("shell-transpose-words", bash_transpose_shellwords
, -1);
475 rl_add_defun ("spell-correct-word", bash_spell_correct_shellword
, -1);
476 rl_bind_key_if_unbound_in_map ('s', bash_spell_correct_shellword
, emacs_ctlx_keymap
);
479 rl_add_defun ("alias-expand-line", alias_expand_line
, -1);
481 rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line
, -1);
485 /* Backwards compatibility. */
486 rl_add_defun ("insert-last-argument", rl_yank_last_arg
, -1);
488 rl_add_defun ("display-shell-version", display_shell_version
, -1);
489 rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command
, -1);
491 #if defined (BRACE_COMPLETION)
492 rl_add_defun ("complete-into-braces", bash_brace_completion
, -1);
495 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
496 rl_add_defun ("complete-filename", bash_complete_filename
, -1);
497 rl_add_defun ("possible-filename-completions", bash_possible_filename_completions
, -1);
498 rl_add_defun ("complete-username", bash_complete_username
, -1);
499 rl_add_defun ("possible-username-completions", bash_possible_username_completions
, -1);
500 rl_add_defun ("complete-hostname", bash_complete_hostname
, -1);
501 rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions
, -1);
502 rl_add_defun ("complete-variable", bash_complete_variable
, -1);
503 rl_add_defun ("possible-variable-completions", bash_possible_variable_completions
, -1);
504 rl_add_defun ("complete-command", bash_complete_command
, -1);
505 rl_add_defun ("possible-command-completions", bash_possible_command_completions
, -1);
506 rl_add_defun ("glob-complete-word", bash_glob_complete_word
, -1);
507 rl_add_defun ("glob-expand-word", bash_glob_expand_word
, -1);
508 rl_add_defun ("glob-list-expansions", bash_glob_list_expansions
, -1);
511 rl_add_defun ("dynamic-complete-history", dynamic_complete_history
, -1);
512 rl_add_defun ("dabbrev-expand", bash_dabbrev_expand
, -1);
514 /* Bind defaults before binding our custom shell keybindings. */
515 if (RL_ISSTATE(RL_STATE_INITIALIZED
) == 0)
518 /* Bind up our special shell functions. */
519 rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line
, emacs_meta_keymap
);
522 rl_bind_key_if_unbound_in_map ('^', history_expand_line
, emacs_meta_keymap
);
525 rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version
, emacs_ctlx_keymap
);
527 /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
528 so it is not necessary to allow C-M-j for context switching. Turn
529 off this occasionally confusing behaviour. */
532 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
533 if (func
== rl_vi_editing_mode
)
534 rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap
);
536 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
537 if (func
== rl_vi_editing_mode
)
538 rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap
);
539 #if defined (VI_MODE)
541 func
= rl_function_of_keyseq (kseq
, vi_movement_keymap
, (int *)NULL
);
542 if (func
== rl_emacs_editing_mode
)
543 rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap
);
546 #if defined (BRACE_COMPLETION)
547 rl_bind_key_if_unbound_in_map ('{', bash_brace_completion
, emacs_meta_keymap
); /*}*/
548 #endif /* BRACE_COMPLETION */
550 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
551 rl_bind_key_if_unbound_in_map ('/', bash_complete_filename
, emacs_meta_keymap
);
552 rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions
, emacs_ctlx_keymap
);
554 /* Have to jump through hoops here because there is a default binding for
555 M-~ (rl_tilde_expand) */
558 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
559 if (func
== 0 || func
== rl_tilde_expand
)
560 rl_bind_keyseq_in_map (kseq
, bash_complete_username
, emacs_meta_keymap
);
562 rl_bind_key_if_unbound_in_map ('~', bash_possible_username_completions
, emacs_ctlx_keymap
);
564 rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname
, emacs_meta_keymap
);
565 rl_bind_key_if_unbound_in_map ('@', bash_possible_hostname_completions
, emacs_ctlx_keymap
);
567 rl_bind_key_if_unbound_in_map ('$', bash_complete_variable
, emacs_meta_keymap
);
568 rl_bind_key_if_unbound_in_map ('$', bash_possible_variable_completions
, emacs_ctlx_keymap
);
570 rl_bind_key_if_unbound_in_map ('!', bash_complete_command
, emacs_meta_keymap
);
571 rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions
, emacs_ctlx_keymap
);
573 rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word
, emacs_meta_keymap
);
574 rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word
, emacs_ctlx_keymap
);
575 rl_bind_key_if_unbound_in_map ('g', bash_glob_list_expansions
, emacs_ctlx_keymap
);
577 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
581 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
582 if (func
== 0 || func
== rl_tab_insert
)
583 rl_bind_key_in_map (TAB
, dynamic_complete_history
, emacs_meta_keymap
);
585 /* Tell the completer that we want a crack first. */
586 rl_attempted_completion_function
= attempt_shell_completion
;
588 /* Tell the completer that we might want to follow symbolic links or
589 do other expansion on directory names. */
590 set_directory_hook ();
592 rl_filename_rewrite_hook
= bash_filename_rewrite_hook
;
594 rl_filename_stat_hook
= bash_filename_stat_hook
;
596 /* Tell the filename completer we want a chance to ignore some names. */
597 rl_ignore_some_completions_function
= filename_completion_ignore
;
599 /* Bind C-xC-e to invoke emacs and run result as commands. */
600 rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command
, emacs_ctlx_keymap
);
601 #if defined (VI_MODE)
602 rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command
, vi_movement_keymap
);
604 rl_bind_key_if_unbound_in_map ('@', posix_edit_macros
, vi_movement_keymap
);
607 rl_bind_key_in_map ('\\', bash_vi_complete
, vi_movement_keymap
);
608 rl_bind_key_in_map ('*', bash_vi_complete
, vi_movement_keymap
);
609 rl_bind_key_in_map ('=', bash_vi_complete
, vi_movement_keymap
);
612 rl_completer_quote_characters
= "'\"";
614 /* This sets rl_completer_word_break_characters and rl_special_prefixes
615 to the appropriate values, depending on whether or not hostname
616 completion is enabled. */
617 enable_hostname_completion (perform_hostname_completion
);
619 /* characters that need to be quoted when appearing in filenames. */
620 rl_filename_quote_characters
= default_filename_quote_characters
;
621 set_filename_bstab (rl_filename_quote_characters
);
623 rl_filename_quoting_function
= bash_quote_filename
;
624 rl_filename_dequoting_function
= bash_dequote_filename
;
625 rl_char_is_quoted_p
= char_is_quoted
;
627 /* Add some default bindings for the "shellwords" functions, roughly
628 parallelling the default word bindings in emacs mode. */
629 rl_bind_key_if_unbound_in_map (CTRL('B'), bash_backward_shellword
, emacs_meta_keymap
);
630 rl_bind_key_if_unbound_in_map (CTRL('D'), bash_kill_shellword
, emacs_meta_keymap
);
631 rl_bind_key_if_unbound_in_map (CTRL('F'), bash_forward_shellword
, emacs_meta_keymap
);
632 rl_bind_key_if_unbound_in_map (CTRL('T'), bash_transpose_shellwords
, emacs_meta_keymap
);
635 /* This is superfluous and makes it impossible to use tab completion in
636 vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix()
637 should already have called posix_readline_initialize() when
638 posixly_correct was set. */
640 posix_readline_initialize (1);
643 bash_readline_initialized
= 1;
647 bashline_reinitialize ()
649 bash_readline_initialized
= 0;
653 bashline_set_event_hook ()
655 rl_signal_event_hook
= bash_event_hook
;
659 bashline_reset_event_hook ()
661 rl_signal_event_hook
= 0;
664 /* On Sun systems at least, rl_attempted_completion_function can end up
665 getting set to NULL, and rl_completion_entry_function set to do command
666 word completion if Bash is interrupted while trying to complete a command
667 word. This just resets all the completion functions to the right thing.
668 It's called from throw_to_top_level(). */
673 rl_attempted_completion_function
= attempt_shell_completion
;
674 rl_completion_entry_function
= NULL
;
675 rl_ignore_some_completions_function
= filename_completion_ignore
;
676 rl_filename_quote_characters
= default_filename_quote_characters
;
677 set_filename_bstab (rl_filename_quote_characters
);
679 set_directory_hook ();
680 rl_filename_stat_hook
= bash_filename_stat_hook
;
682 bashline_reset_event_hook ();
684 rl_sort_completion_matches
= 1;
687 /* Contains the line to push into readline. */
688 static char *push_to_readline
= (char *)NULL
;
690 /* Push the contents of push_to_readline into the
695 if (push_to_readline
)
697 rl_insert_text (push_to_readline
);
698 free (push_to_readline
);
699 push_to_readline
= (char *)NULL
;
700 rl_startup_hook
= old_rl_startup_hook
;
705 /* Call this to set the initial text for the next line to read
711 FREE (push_to_readline
);
713 push_to_readline
= savestring (line
);
714 old_rl_startup_hook
= rl_startup_hook
;
715 rl_startup_hook
= bash_push_line
;
721 display_shell_version (count
, c
)
725 show_shell_version (0);
726 putc ('\r', rl_outstream
);
727 fflush (rl_outstream
);
733 /* **************************************************************** */
737 /* **************************************************************** */
739 /* If the user requests hostname completion, then simply build a list
740 of hosts, and complete from that forever more, or at least until
741 HOSTFILE is unset. */
743 /* THIS SHOULD BE A STRINGLIST. */
744 /* The kept list of hostnames. */
745 static char **hostname_list
= (char **)NULL
;
747 /* The physical size of the above list. */
748 static int hostname_list_size
;
750 /* The number of hostnames in the above list. */
751 static int hostname_list_length
;
753 /* Whether or not HOSTNAME_LIST has been initialized. */
754 int hostname_list_initialized
= 0;
756 /* Initialize the hostname completion table. */
758 initialize_hostname_list ()
762 temp
= get_string_value ("HOSTFILE");
764 temp
= get_string_value ("hostname_completion_file");
766 temp
= DEFAULT_HOSTS_FILE
;
768 snarf_hosts_from_file (temp
);
771 hostname_list_initialized
++;
774 /* Add NAME to the list of hosts. */
779 if (hostname_list_length
+ 2 > hostname_list_size
)
781 hostname_list_size
= (hostname_list_size
+ 32) - (hostname_list_size
% 32);
782 hostname_list
= strvec_resize (hostname_list
, hostname_list_size
);
785 hostname_list
[hostname_list_length
++] = savestring (name
);
786 hostname_list
[hostname_list_length
] = (char *)NULL
;
789 #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
792 snarf_hosts_from_file (filename
)
796 char *temp
, buffer
[256], name
[256];
797 register int i
, start
;
799 file
= fopen (filename
, "r");
803 while (temp
= fgets (buffer
, 255, file
))
805 /* Skip to first character. */
806 for (i
= 0; buffer
[i
] && cr_whitespace (buffer
[i
]); i
++)
809 /* If comment or blank line, ignore. */
810 if (buffer
[i
] == '\0' || buffer
[i
] == '#')
813 /* If `preprocessor' directive, do the include. */
814 if (strncmp (buffer
+ i
, "$include ", 9) == 0)
818 /* Find start of filename. */
819 for (incfile
= buffer
+ i
+ 9; *incfile
&& whitespace (*incfile
); incfile
++)
822 /* Find end of filename. */
823 for (t
= incfile
; *t
&& cr_whitespace (*t
) == 0; t
++)
828 snarf_hosts_from_file (incfile
);
832 /* Skip internet address if present. */
833 if (DIGIT (buffer
[i
]))
834 for (; buffer
[i
] && cr_whitespace (buffer
[i
]) == 0; i
++);
836 /* Gobble up names. Each name is separated with whitespace. */
839 for (; cr_whitespace (buffer
[i
]); i
++)
841 if (buffer
[i
] == '\0' || buffer
[i
] == '#')
844 /* Isolate the current word. */
845 for (start
= i
; buffer
[i
] && cr_whitespace (buffer
[i
]) == 0; i
++)
849 strncpy (name
, buffer
+ start
, i
- start
);
850 name
[i
- start
] = '\0';
851 add_host_name (name
);
857 /* Return the hostname list. */
861 if (hostname_list_initialized
== 0)
862 initialize_hostname_list ();
863 return (hostname_list
);
867 clear_hostname_list ()
871 if (hostname_list_initialized
== 0)
873 for (i
= 0; i
< hostname_list_length
; i
++)
874 free (hostname_list
[i
]);
875 hostname_list_length
= hostname_list_initialized
= 0;
878 /* Return a NULL terminated list of hostnames which begin with TEXT.
879 Initialize the hostname list the first time if necessary.
880 The array is malloc ()'ed, but not the individual strings. */
882 hostnames_matching (text
)
885 register int i
, len
, nmatch
, rsize
;
888 if (hostname_list_initialized
== 0)
889 initialize_hostname_list ();
891 if (hostname_list_initialized
== 0)
892 return ((char **)NULL
);
894 /* Special case. If TEXT consists of nothing, then the whole list is
898 result
= strvec_create (1 + hostname_list_length
);
899 for (i
= 0; i
< hostname_list_length
; i
++)
900 result
[i
] = hostname_list
[i
];
901 result
[i
] = (char *)NULL
;
905 /* Scan until found, or failure. */
907 result
= (char **)NULL
;
908 for (i
= nmatch
= rsize
= 0; i
< hostname_list_length
; i
++)
910 if (STREQN (text
, hostname_list
[i
], len
) == 0)
913 /* OK, it matches. Add it to the list. */
914 if (nmatch
>= (rsize
- 1))
916 rsize
= (rsize
+ 16) - (rsize
% 16);
917 result
= strvec_resize (result
, rsize
);
920 result
[nmatch
++] = hostname_list
[i
];
923 result
[nmatch
] = (char *)NULL
;
927 /* This vi mode command causes VI_EDIT_COMMAND to be run on the current
928 command being entered (if no explicit argument is given), otherwise on
929 a command from the history file. */
931 #define VI_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-vi}}\""
932 #define EMACS_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-emacs}}\""
933 #define POSIX_VI_EDIT_COMMAND "fc -e vi"
936 edit_and_execute_command (count
, c
, editing_mode
, edit_command
)
937 int count
, c
, editing_mode
;
940 char *command
, *metaval
;
941 int r
, rrs
, metaflag
;
942 sh_parser_state_t ps
;
944 rrs
= rl_readline_state
;
945 saved_command_line_count
= current_command_line_count
;
947 /* Accept the current line. */
952 command
= (char *)xmalloc (strlen (edit_command
) + 8);
953 sprintf (command
, "%s %d", edit_command
, count
);
957 /* Take the command we were just editing, add it to the history file,
958 then call fc to operate on it. We have to add a dummy command to
959 the end of the history because fc ignores the last command (assumes
960 it's supposed to deal with the command before the `fc'). */
961 /* This breaks down when using command-oriented history and are not
962 finished with the command, so we should not ignore the last command */
964 current_command_line_count
++; /* for rl_newline above */
965 bash_add_history (rl_line_buffer
);
966 current_command_line_count
= 0; /* for dummy history entry */
967 bash_add_history ("");
968 history_lines_this_session
++;
970 command
= savestring (edit_command
);
973 metaval
= rl_variable_value ("input-meta");
974 metaflag
= RL_BOOLEAN_VARIABLE_VALUE (metaval
);
976 if (rl_deprep_term_function
)
977 (*rl_deprep_term_function
) ();
979 save_parser_state (&ps
);
980 r
= parse_and_execute (command
, (editing_mode
== VI_EDITING_MODE
) ? "v" : "C-xC-e", SEVAL_NOHIST
);
981 restore_parser_state (&ps
);
983 /* if some kind of reset_parser was called, undo it. */
984 reset_readahead_token ();
986 if (rl_prep_term_function
)
987 (*rl_prep_term_function
) (metaflag
);
990 current_command_line_count
= saved_command_line_count
;
992 /* Now erase the contents of the current line and undo the effects of the
993 rl_accept_line() above. We don't even want to make the text we just
994 executed available for undoing. */
995 rl_line_buffer
[0] = '\0'; /* XXX */
996 rl_point
= rl_end
= 0;
998 rl_readline_state
= rrs
;
1000 #if defined (VI_MODE)
1001 if (editing_mode
== VI_EDITING_MODE
)
1002 rl_vi_insertion_mode (1, c
);
1005 rl_forced_update_display ();
1010 #if defined (VI_MODE)
1012 vi_edit_and_execute_command (count
, c
)
1015 if (posixly_correct
)
1016 return (edit_and_execute_command (count
, c
, VI_EDITING_MODE
, POSIX_VI_EDIT_COMMAND
));
1018 return (edit_and_execute_command (count
, c
, VI_EDITING_MODE
, VI_EDIT_COMMAND
));
1020 #endif /* VI_MODE */
1023 emacs_edit_and_execute_command (count
, c
)
1026 return (edit_and_execute_command (count
, c
, EMACS_EDITING_MODE
, EMACS_EDIT_COMMAND
));
1031 posix_edit_macros (count
, key
)
1035 char alias_name
[3], *alias_value
, *macro
;
1040 alias_name
[0] = '_';
1042 alias_name
[2] = '\0';
1044 alias_value
= get_alias_value (alias_name
);
1045 if (alias_value
&& *alias_value
)
1047 macro
= savestring (alias_value
);
1048 rl_push_macro_input (macro
);
1054 /* Bindable commands that move `shell-words': that is, sequences of
1055 non-unquoted-metacharacters. */
1057 #define WORDDELIM(c) (shellmeta(c) || shellblank(c))
1060 bash_forward_shellword (count
, key
)
1068 return (bash_backward_shellword (-count
, key
));
1070 /* The tricky part of this is deciding whether or not the first character
1071 we're on is an unquoted metacharacter. Not completely handled yet. */
1072 /* XXX - need to test this stuff with backslash-escaped shell
1073 metacharacters and unclosed single- and double-quoted strings. */
1086 /* Are we in a quoted string? If we are, move to the end of the quoted
1087 string and continue the outer loop. We only want quoted strings, not
1088 backslash-escaped characters, but char_is_quoted doesn't
1090 if (char_is_quoted (rl_line_buffer
, p
) && p
> 0 && rl_line_buffer
[p
-1] != '\\')
1093 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1094 while (p
< rl_end
&& char_is_quoted (rl_line_buffer
, p
));
1099 /* Rest of code assumes we are not in a quoted string. */
1100 /* Move forward until we hit a non-metacharacter. */
1101 while (p
< rl_end
&& (c
= rl_line_buffer
[p
]) && WORDDELIM (c
))
1106 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1107 continue; /* straight back to loop, don't increment p */
1109 if (p
< rl_end
&& rl_line_buffer
[p
])
1110 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1113 p
= skip_to_delim (rl_line_buffer
, ++p
, "'", SD_NOJMP
);
1116 p
= skip_to_delim (rl_line_buffer
, ++p
, "\"", SD_NOJMP
);
1124 if (rl_line_buffer
[p
] == 0 || p
== rl_end
)
1131 /* Now move forward until we hit a non-quoted metacharacter or EOL */
1132 while (p
< rl_end
&& (c
= rl_line_buffer
[p
]) && WORDDELIM (c
) == 0)
1137 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1138 continue; /* straight back to loop, don't increment p */
1140 if (p
< rl_end
&& rl_line_buffer
[p
])
1141 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1144 p
= skip_to_delim (rl_line_buffer
, ++p
, "'", SD_NOJMP
);
1147 p
= skip_to_delim (rl_line_buffer
, ++p
, "\"", SD_NOJMP
);
1155 if (p
== rl_end
|| rl_line_buffer
[p
] == 0)
1169 bash_backward_shellword (count
, key
)
1177 return (bash_forward_shellword (-count
, key
));
1190 /* Move backward until we hit a non-metacharacter. We want to deal
1191 with the characters before point, so we move off a word if we're
1192 at its first character. */
1193 BACKUP_CHAR (rl_line_buffer
, slen
, p
);
1196 c
= rl_line_buffer
[p
];
1197 if (WORDDELIM (c
) == 0 || char_is_quoted (rl_line_buffer
, p
))
1199 BACKUP_CHAR (rl_line_buffer
, slen
, p
);
1208 /* Now move backward until we hit a metacharacter or BOL. Leave point
1209 at the start of the shellword or at BOL. */
1213 c
= rl_line_buffer
[p
];
1214 if (WORDDELIM (c
) && char_is_quoted (rl_line_buffer
, p
) == 0)
1220 BACKUP_CHAR (rl_line_buffer
, slen
, p
);
1231 bash_kill_shellword (count
, key
)
1237 return (bash_backward_kill_shellword (-count
, key
));
1240 bash_forward_shellword (count
, key
);
1243 rl_kill_text (p
, rl_point
);
1246 if (rl_editing_mode
== EMACS_EDITING_MODE
) /* 1 == emacs_mode */
1253 bash_backward_kill_shellword (count
, key
)
1259 return (bash_kill_shellword (-count
, key
));
1262 bash_backward_shellword (count
, key
);
1265 rl_kill_text (p
, rl_point
);
1267 if (rl_editing_mode
== EMACS_EDITING_MODE
) /* 1 == emacs_mode */
1274 bash_transpose_shellwords (count
, key
)
1277 char *word1
, *word2
;
1278 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1279 int orig_point
= rl_point
;
1284 /* Find the two shell words. */
1285 bash_forward_shellword (count
, key
);
1287 bash_backward_shellword (1, key
);
1289 bash_backward_shellword (count
, key
);
1291 bash_forward_shellword (1, key
);
1294 /* check that there really are two words. */
1295 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1298 rl_point
= orig_point
;
1302 /* Get the text of the words. */
1303 word1
= rl_copy_text (w1_beg
, w1_end
);
1304 word2
= rl_copy_text (w2_beg
, w2_end
);
1306 /* We are about to do many insertions and deletions. Remember them
1307 as one operation. */
1308 rl_begin_undo_group ();
1310 /* Do the stuff at word2 first, so that we don't have to worry
1311 about word1 moving. */
1313 rl_delete_text (w2_beg
, w2_end
);
1314 rl_insert_text (word1
);
1317 rl_delete_text (w1_beg
, w1_end
);
1318 rl_insert_text (word2
);
1320 /* This is exactly correct since the text before this point has not
1321 changed in length. */
1324 /* I think that does it. */
1325 rl_end_undo_group ();
1332 /* Directory name spelling correction on the current word (not shellword).
1333 COUNT > 1 is not exactly correct yet. */
1335 bash_spell_correct_shellword (count
, key
)
1338 int opoint
, wbeg
, wend
;
1339 char *text
, *newdir
;
1344 bash_backward_shellword (1, key
);
1346 bash_forward_shellword (1, key
);
1352 text
= rl_copy_text (wbeg
, wend
);
1354 newdir
= dirspell (text
);
1357 rl_begin_undo_group ();
1358 rl_delete_text (wbeg
, wend
);
1361 rl_insert_text (newdir
);
1363 rl_end_undo_group ();
1369 if (rl_point
>= rl_end
)
1375 bash_forward_shellword (1, key
); /* XXX */
1381 /* **************************************************************** */
1383 /* How To Do Shell Completion */
1385 /* **************************************************************** */
1387 #define COMMAND_SEPARATORS ";|&{(`"
1389 #define COMMAND_SEPARATORS_PLUS_WS ";|&{(` \t"
1392 /* check for redirections and other character combinations that are not
1393 command separators */
1398 register int this_char
, prev_char
;
1400 /* Handle the two character tokens `>&', `<&', and `>|'.
1401 We are not in a command position after one of these. */
1402 this_char
= rl_line_buffer
[ti
];
1403 prev_char
= (ti
> 0) ? rl_line_buffer
[ti
- 1] : 0;
1405 if ((this_char
== '&' && (prev_char
== '<' || prev_char
== '>')) ||
1406 (this_char
== '|' && prev_char
== '>'))
1408 else if (this_char
== '{' && prev_char
== '$') /*}*/
1411 else if (this_char
== '(' && prev_char
== '$') /*)*/
1413 else if (this_char
== '(' && prev_char
== '<') /*)*/
1415 #if defined (EXTENDED_GLOB)
1416 else if (extended_glob
&& this_char
== '(' && prev_char
== '!') /*)*/
1420 else if (char_is_quoted (rl_line_buffer
, ti
))
1425 #if defined (PROGRAMMABLE_COMPLETION)
1427 * XXX - because of the <= start test, and setting os = s+1, this can
1428 * potentially return os > start. This is probably not what we want to
1429 * happen, but fix later after 2.05a-release.
1432 find_cmd_start (start
)
1435 register int s
, os
, ns
;
1438 /* Flags == SD_NOJMP only because we want to skip over command substitutions
1439 in assignment statements. Have to test whether this affects `standalone'
1440 command substitutions as individual words. */
1441 while (((s
= skip_to_delim (rl_line_buffer
, os
, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
/*|SD_NOSKIPCMD*/)) <= start
) &&
1444 /* Handle >| token crudely; treat as > not | */
1445 if (s
> 0 && rl_line_buffer
[s
] == '|' && rl_line_buffer
[s
-1] == '>')
1447 ns
= skip_to_delim (rl_line_buffer
, s
+1, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
/*|SD_NOSKIPCMD*/);
1448 if (ns
> start
|| rl_line_buffer
[ns
] == 0)
1453 /* The only reserved word in COMMAND_SEPARATORS is `{', so handle that
1454 specially, making sure it's in a spot acceptable for reserved words */
1455 if (s
>= os
&& rl_line_buffer
[s
] == '{')
1457 int pc
, nc
; /* index of previous non-whitespace, next char */
1458 for (pc
= (s
> os
) ? s
- 1 : os
; pc
> os
&& whitespace(rl_line_buffer
[pc
]); pc
--)
1460 nc
= rl_line_buffer
[s
+1];
1461 /* must be preceded by a command separator or be the first non-
1462 whitespace character since the last command separator, and
1463 followed by a shell break character (not another `{') to be a reserved word. */
1464 if ((pc
> os
&& (rl_line_buffer
[s
-1] == '{' || strchr (COMMAND_SEPARATORS
, rl_line_buffer
[pc
]) == 0)) ||
1465 (shellbreak(nc
) == 0)) /* }} */
1467 /* Not a reserved word, look for another delim */
1468 ns
= skip_to_delim (rl_line_buffer
, s
+1, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
/*|SD_NOSKIPCMD*/);
1469 if (ns
> start
|| rl_line_buffer
[ns
] == 0)
1486 e
= skip_to_delim (rl_line_buffer
, end
, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
);
1491 find_cmd_name (start
, sp
, ep
)
1498 for (s
= start
; whitespace (rl_line_buffer
[s
]); s
++)
1501 /* skip until a shell break character */
1502 e
= skip_to_delim (rl_line_buffer
, s
, "()<>;&| \t\n", SD_NOJMP
|SD_COMPLETE
);
1504 name
= substring (rl_line_buffer
, s
, e
);
1515 prog_complete_return (text
, matchnum
)
1524 if (prog_complete_matches
== 0 || prog_complete_matches
[ind
] == 0)
1525 return (char *)NULL
;
1526 return (prog_complete_matches
[ind
++]);
1529 #endif /* PROGRAMMABLE_COMPLETION */
1531 /* Try and catch completion attempts that are syntax errors or otherwise
1534 invalid_completion (text
, ind
)
1540 /* If we don't catch these here, the next clause will */
1541 if (ind
> 0 && rl_line_buffer
[ind
] == '(' && /*)*/
1542 member (rl_line_buffer
[ind
-1], "$<>"))
1546 while (pind
> 0 && whitespace (rl_line_buffer
[pind
]))
1548 /* If we have only whitespace preceding a paren, it's valid */
1549 if (ind
>= 0 && pind
<= 0 && rl_line_buffer
[ind
] == '(') /*)*/
1551 /* Flag the invalid completions, which are mostly syntax errors */
1552 if (ind
> 0 && rl_line_buffer
[ind
] == '(' && /*)*/
1553 member (rl_line_buffer
[pind
], COMMAND_SEPARATORS
) == 0)
1559 /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
1560 at START and END. Return an array of matches, or NULL if none. */
1562 attempt_shell_completion (text
, start
, end
)
1566 int in_command_position
, ti
, qc
, dflags
;
1567 char **matches
, *command_separator_chars
;
1568 #if defined (PROGRAMMABLE_COMPLETION)
1569 int have_progcomps
, was_assignment
;
1570 COMPSPEC
*iw_compspec
;
1573 command_separator_chars
= COMMAND_SEPARATORS
;
1574 matches
= (char **)NULL
;
1575 rl_ignore_some_completions_function
= filename_completion_ignore
;
1577 rl_filename_quote_characters
= default_filename_quote_characters
;
1578 set_filename_bstab (rl_filename_quote_characters
);
1579 set_directory_hook ();
1580 rl_filename_stat_hook
= bash_filename_stat_hook
;
1582 rl_sort_completion_matches
= 1; /* sort by default */
1584 /* Determine if this could be a command word. It is if it appears at
1585 the start of the line (ignoring preceding whitespace), or if it
1586 appears after a character that separates commands. It cannot be a
1587 command word if we aren't at the top-level prompt. */
1591 while ((ti
> -1) && (whitespace (rl_line_buffer
[ti
])))
1595 /* If this is an open quote, maybe we're trying to complete a quoted
1597 if (ti
>= 0 && (rl_line_buffer
[ti
] == '"' || rl_line_buffer
[ti
] == '\''))
1599 qc
= rl_line_buffer
[ti
];
1601 while (ti
> -1 && (whitespace (rl_line_buffer
[ti
])))
1606 in_command_position
= 0;
1609 /* Only do command completion at the start of a line when we
1610 are prompting at the top level. */
1611 if (current_prompt_string
== ps1_prompt
)
1612 in_command_position
++;
1613 else if (parser_in_command_position ())
1614 in_command_position
++;
1616 else if (member (rl_line_buffer
[ti
], command_separator_chars
))
1618 in_command_position
++;
1620 if (check_redir (ti
) == 1)
1621 in_command_position
= -1; /* sentinel that we're not the first word on the line */
1625 /* This still could be in command position. It is possible
1626 that all of the previous words on the line are variable
1630 if (in_command_position
> 0 && invalid_completion (text
, ti
))
1632 rl_attempted_completion_over
= 1;
1633 return ((char **)NULL
);
1636 /* Check that we haven't incorrectly flagged a closed command substitution
1637 as indicating we're in a command position. */
1638 if (in_command_position
> 0 && ti
>= 0 && rl_line_buffer
[ti
] == '`' &&
1639 *text
!= '`' && unclosed_pair (rl_line_buffer
, end
, "`") == 0)
1640 in_command_position
= -1; /* not following a command separator */
1642 /* Special handling for command substitution. If *TEXT is a backquote,
1643 it can be the start or end of an old-style command substitution, or
1644 unmatched. If it's unmatched, both calls to unclosed_pair will
1645 succeed. Don't bother if readline found a single quote and we are
1646 completing on the substring. */
1647 if (*text
== '`' && rl_completion_quote_character
!= '\'' &&
1648 (in_command_position
> 0 || (unclosed_pair (rl_line_buffer
, start
, "`") &&
1649 unclosed_pair (rl_line_buffer
, end
, "`"))))
1650 matches
= rl_completion_matches (text
, command_subst_completion_function
);
1652 #if defined (PROGRAMMABLE_COMPLETION)
1653 /* Attempt programmable completion. */
1654 have_progcomps
= prog_completion_enabled
&& (progcomp_size () > 0);
1655 iw_compspec
= progcomp_search (INITIALWORD
);
1657 (in_command_position
== 0 || text
[0] == '\0' || (in_command_position
> 0 && iw_compspec
)) &&
1658 current_prompt_string
== ps1_prompt
)
1660 int s
, e
, s1
, e1
, os
, foundcs
;
1663 /* XXX - don't free the members */
1664 if (prog_complete_matches
)
1665 free (prog_complete_matches
);
1666 prog_complete_matches
= (char **)NULL
;
1671 s
= find_cmd_start (os
);
1672 e
= find_cmd_end (end
);
1675 /* Don't read past the end of rl_line_buffer */
1681 /* Or past point if point is within an assignment statement */
1682 else if (was_assignment
&& s
> rl_point
)
1687 /* Skip over assignment statements preceding a command name. If we
1688 don't find a command name at all, we can perform command name
1689 completion. If we find a partial command name, we should perform
1690 command name completion on it. */
1692 n
= find_cmd_name (s
, &s1
, &e1
);
1695 while (was_assignment
= assignment (n
, 0));
1696 s
= s1
; /* reset to index where name begins */
1698 /* s == index of where command name begins (reset above)
1699 e == end of current command, may be end of line
1700 s1 = index of where command name begins
1701 e1 == index of where command name ends
1702 start == index of where word to be completed begins
1703 end == index of where word to be completed ends
1704 if (s == start) we are doing command word completion for sure
1705 if (e1 == end) we are at the end of the command name and completing it */
1706 if (start
== 0 && end
== 0 && e
!= 0 && text
[0] == '\0') /* beginning of non-empty line */
1708 else if (start
== end
&& start
== s1
&& e
!= 0 && e1
> end
) /* beginning of command name, leading whitespace */
1710 else if (e
== 0 && e
== s
&& text
[0] == '\0' && have_progcomps
) /* beginning of empty line */
1711 prog_complete_matches
= programmable_completions (EMPTYCMD
, text
, s
, e
, &foundcs
);
1712 else if (start
== end
&& text
[0] == '\0' && s1
> start
&& whitespace (rl_line_buffer
[start
]))
1713 foundcs
= 0; /* whitespace before command name */
1714 else if (e
> s
&& was_assignment
== 0 && e1
== end
&& rl_line_buffer
[e
] == 0 && whitespace (rl_line_buffer
[e
-1]) == 0)
1716 /* not assignment statement, but still want to perform command
1717 completion if we are composing command word. */
1719 in_command_position
= s
== start
&& STREQ (n
, text
); /* XXX */
1721 else if (e
> s
&& was_assignment
== 0 && have_progcomps
)
1723 prog_complete_matches
= programmable_completions (n
, text
, s
, e
, &foundcs
);
1724 /* command completion if programmable completion fails */
1725 /* If we have a completion for the initial word, we can prefer that */
1726 in_command_position
= s
== start
&& (iw_compspec
|| STREQ (n
, text
)); /* XXX */
1727 if (iw_compspec
&& in_command_position
)
1730 /* empty command name following command separator */
1731 else if (s
>= e
&& n
[0] == '\0' && text
[0] == '\0' && start
> 0 &&
1732 was_assignment
== 0 && member (rl_line_buffer
[start
-1], COMMAND_SEPARATORS
))
1735 in_command_position
= 1;
1737 else if (s
>= e
&& n
[0] == '\0' && text
[0] == '\0' && start
> 0)
1739 foundcs
= 0; /* empty command name following optional assignments */
1740 in_command_position
+= was_assignment
;
1742 else if (s
== start
&& e
== end
&& STREQ (n
, text
) && start
> 0)
1744 foundcs
= 0; /* partial command name following assignments */
1745 in_command_position
= 1;
1750 /* If we have defined a compspec for the initial (command) word, call
1751 it and process the results like any other programmable completion. */
1752 if (in_command_position
&& have_progcomps
&& foundcs
== 0 && iw_compspec
)
1753 prog_complete_matches
= programmable_completions (INITIALWORD
, text
, s
, e
, &foundcs
);
1756 /* XXX - if we found a COMPSPEC for the command, just return whatever
1757 the programmable completion code returns, and disable the default
1758 filename completion that readline will do unless the COPT_DEFAULT
1759 option has been set with the `-o default' option to complete or
1763 pcomp_set_readline_variables (foundcs
, 1);
1764 /* Turn what the programmable completion code returns into what
1765 readline wants. I should have made compute_lcd_of_matches
1767 matches
= rl_completion_matches (text
, prog_complete_return
);
1768 if ((foundcs
& COPT_DEFAULT
) == 0)
1769 rl_attempted_completion_over
= 1; /* no default */
1770 if (matches
|| ((foundcs
& COPT_BASHDEFAULT
) == 0))
1779 if (in_command_position
> 0)
1780 dflags
|= DEFCOMP_CMDPOS
;
1781 matches
= bash_default_completion (text
, start
, end
, qc
, dflags
);
1788 bash_default_completion (text
, start
, end
, qc
, compflags
)
1790 int start
, end
, qc
, compflags
;
1794 matches
= (char **)NULL
;
1796 /* New posix-style command substitution or variable name? */
1799 if (qc
!= '\'' && text
[1] == '(') /* ) */
1800 matches
= rl_completion_matches (text
, command_subst_completion_function
);
1803 matches
= rl_completion_matches (text
, variable_completion_function
);
1804 /* If a single match, see if it expands to a directory name and append
1805 a slash if it does. This requires us to expand the variable name,
1806 so we don't want to display errors if the variable is unset. This
1807 can happen with dynamic variables whose value has never been
1809 if (matches
&& matches
[0] && matches
[1] == 0)
1811 t
= savestring (matches
[0]);
1812 bash_filename_stat_hook (&t
);
1813 /* doesn't use test_for_directory because that performs tilde
1816 rl_completion_append_character
= '/';
1822 /* If the word starts in `~', and there is no slash in the word, then
1823 try completing this word as a username. */
1824 if (matches
== 0 && *text
== '~' && mbschr (text
, '/') == 0)
1825 matches
= rl_completion_matches (text
, rl_username_completion_function
);
1827 /* Another one. Why not? If the word starts in '@', then look through
1828 the world of known hostnames for completion first. */
1829 if (matches
== 0 && perform_hostname_completion
&& *text
== '@')
1830 matches
= rl_completion_matches (text
, hostname_completion_function
);
1832 /* And last, (but not least) if this word is in a command position, then
1833 complete over possible command names, including aliases, functions,
1834 and command names. */
1835 if (matches
== 0 && (compflags
& DEFCOMP_CMDPOS
))
1837 /* If END == START and text[0] == 0, we are trying to complete an empty
1839 if (no_empty_command_completion
&& end
== start
&& text
[0] == '\0')
1841 matches
= (char **)NULL
;
1842 rl_ignore_some_completions_function
= bash_ignore_everything
;
1846 #define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x))
1849 matches
= rl_completion_matches (text
, command_word_completion_function
);
1851 /* If we are attempting command completion and nothing matches, we
1852 do not want readline to perform filename completion for us. We
1853 still want to be able to complete partial pathnames, so set the
1854 completion ignore function to something which will remove
1855 filenames and leave directories in the match list. */
1856 if (matches
== (char **)NULL
)
1857 rl_ignore_some_completions_function
= bash_ignore_filenames
;
1858 else if (matches
[1] == 0 && CMD_IS_DIR(matches
[0]) && dot_in_path
== 0)
1859 /* If we found a single match, without looking in the current
1860 directory (because it's not in $PATH), but the found name is
1861 also a command in the current directory, suppress appending any
1862 terminating character, since it's ambiguous. */
1864 rl_completion_suppress_append
= 1;
1865 rl_filename_completion_desired
= 0;
1867 else if (matches
[0] && matches
[1] && STREQ (matches
[0], matches
[1]) && CMD_IS_DIR (matches
[0]))
1868 /* There are multiple instances of the same match (duplicate
1869 completions haven't yet been removed). In this case, all of
1870 the matches will be the same, and the duplicate removal code
1871 will distill them all down to one. We turn on
1872 rl_completion_suppress_append for the same reason as above.
1873 Remember: we only care if there's eventually a single unique
1874 completion. If there are multiple completions this won't
1875 make a difference and the problem won't occur. */
1877 rl_completion_suppress_append
= 1;
1878 rl_filename_completion_desired
= 0;
1883 /* This could be a globbing pattern, so try to expand it using pathname
1885 if (!matches
&& completion_glob_pattern ((char *)text
))
1887 matches
= rl_completion_matches (text
, glob_complete_word
);
1888 /* A glob expression that matches more than one filename is problematic.
1889 If we match more than one filename, punt. */
1890 if (matches
&& matches
[1] && rl_completion_type
== TAB
)
1892 strvec_dispose (matches
);
1893 matches
= (char **)0;
1895 else if (matches
&& matches
[1] && rl_completion_type
== '!')
1897 rl_completion_suppress_append
= 1;
1898 rl_filename_completion_desired
= 0;
1906 bash_command_name_stat_hook (name
)
1909 char *cname
, *result
;
1911 /* If it's not something we're going to look up in $PATH, just call the
1912 normal filename stat hook. */
1913 if (absolute_program (*name
))
1914 return (bash_filename_stat_hook (name
));
1917 /* XXX - we could do something here with converting aliases, builtins,
1918 and functions into something that came out as executable, but we don't. */
1919 result
= search_for_command (cname
, 0);
1929 executable_completion (filename
, searching_path
)
1930 const char *filename
;
1936 /* This gets an unquoted filename, so we need to quote special characters
1937 in the filename before the completion hook gets it. */
1939 f
= savestring (filename
);
1942 f
= bash_quote_filename (filename
, SINGLE_MATCH
, &c
);
1944 bash_directory_completion_hook (&f
);
1946 r
= searching_path
? executable_file (f
) : executable_or_directory (f
);
1951 /* This is the function to call when the word to complete is in a position
1952 where a command word can be found. It grovels $PATH, looking for commands
1953 that match. It also scans aliases, function names, and the shell_builtin
1956 command_word_completion_function (hint_text
, state
)
1957 const char *hint_text
;
1960 static char *hint
= (char *)NULL
;
1961 static char *path
= (char *)NULL
;
1962 static char *val
= (char *)NULL
;
1963 static char *filename_hint
= (char *)NULL
;
1964 static char *fnhint
= (char *)NULL
;
1965 static char *dequoted_hint
= (char *)NULL
;
1966 static char *directory_part
= (char *)NULL
;
1967 static char **glob_matches
= (char **)NULL
;
1968 static int path_index
, hint_len
, istate
, igncase
;
1969 static int mapping_over
, local_index
, searching_path
, hint_is_dir
;
1970 static int old_glob_ignore_case
, globpat
;
1971 static SHELL_VAR
**varlist
= (SHELL_VAR
**)NULL
;
1973 static alias_t
**alias_list
= (alias_t
**)NULL
;
1977 /* We have to map over the possibilities for command words. If we have
1978 no state, then make one just for that purpose. */
1981 rl_filename_stat_hook
= bash_command_name_stat_hook
;
1983 if (dequoted_hint
&& dequoted_hint
!= hint
)
1984 free (dequoted_hint
);
1988 mapping_over
= searching_path
= 0;
1989 hint_is_dir
= CMD_IS_DIR (hint_text
);
1992 temp
= rl_variable_value ("completion-ignore-case");
1993 igncase
= RL_BOOLEAN_VARIABLE_VALUE (temp
);
1997 free (glob_matches
);
1998 glob_matches
= (char **)NULL
;
2001 globpat
= completion_glob_pattern ((char *)hint_text
);
2003 /* If this is an absolute program name, do not check it against
2004 aliases, reserved words, functions or builtins. We must check
2005 whether or not it is unique, and, if so, whether that filename
2007 if (globpat
|| absolute_program (hint_text
))
2009 /* Perform tilde expansion on what's passed, so we don't end up
2010 passing filenames with tildes directly to stat(). The rest of
2011 the shell doesn't do variable expansion on the word following
2012 the tilde, so we don't do it here even if direxpand is set. */
2013 if (*hint_text
== '~')
2015 hint
= bash_tilde_expand (hint_text
, 0);
2016 directory_part
= savestring (hint_text
);
2017 temp
= strchr (directory_part
, '/');
2022 free (directory_part
);
2023 directory_part
= (char *)NULL
;
2026 else if (dircomplete_expand
)
2028 hint
= savestring (hint_text
);
2029 bash_directory_completion_hook (&hint
);
2032 hint
= savestring (hint_text
);
2034 dequoted_hint
= hint
;
2035 /* If readline's completer found a quote character somewhere, but
2036 didn't set the quote character, there must have been a quote
2037 character embedded in the filename. It can't be at the start of
2038 the filename, so we need to dequote the filename before we look
2039 in the file system for it. */
2040 if (rl_completion_found_quote
&& rl_completion_quote_character
== 0)
2042 dequoted_hint
= bash_dequote_filename (hint
, 0);
2044 hint
= dequoted_hint
;
2046 hint_len
= strlen (hint
);
2049 free (filename_hint
);
2051 fnhint
= filename_hint
= savestring (hint
);
2062 if (dircomplete_expand
&& path_dot_or_dotdot (filename_hint
))
2064 dircomplete_expand
= 0;
2065 set_directory_hook ();
2066 dircomplete_expand
= 1;
2073 dequoted_hint
= hint
= savestring (hint_text
);
2074 hint_len
= strlen (hint
);
2076 if (rl_completion_found_quote
&& rl_completion_quote_character
== 0)
2077 dequoted_hint
= bash_dequote_filename (hint
, 0);
2079 path
= get_string_value ("PATH");
2080 path_index
= dot_in_path
= 0;
2082 /* Initialize the variables for each type of command word. */
2088 varlist
= all_visible_functions ();
2094 alias_list
= all_aliases ();
2098 /* mapping_over says what we are currently hacking. Note that every case
2099 in this list must fall through when there are no more possibilities. */
2101 switch (mapping_over
)
2103 case 0: /* Aliases come first. */
2105 while (alias_list
&& alias_list
[local_index
])
2107 register char *alias
;
2109 alias
= alias_list
[local_index
++]->name
;
2111 if (igncase
== 0 && (STREQN (alias
, hint
, hint_len
)))
2112 return (savestring (alias
));
2113 else if (igncase
&& strncasecmp (alias
, hint
, hint_len
) == 0)
2114 return (savestring (alias
));
2120 case 1: /* Then shell reserved words. */
2122 while (word_token_alist
[local_index
].word
)
2124 register char *reserved_word
;
2126 reserved_word
= word_token_alist
[local_index
++].word
;
2128 if (STREQN (reserved_word
, hint
, hint_len
))
2129 return (savestring (reserved_word
));
2135 case 2: /* Then function names. */
2136 while (varlist
&& varlist
[local_index
])
2138 register char *varname
;
2140 varname
= varlist
[local_index
++]->name
;
2142 /* Honor completion-ignore-case for shell function names. */
2143 if (igncase
== 0 && (STREQN (varname
, hint
, hint_len
)))
2144 return (savestring (varname
));
2145 else if (igncase
&& strncasecmp (varname
, hint
, hint_len
) == 0)
2146 return (savestring (varname
));
2151 case 3: /* Then shell builtins. */
2152 for (; local_index
< num_shell_builtins
; local_index
++)
2154 /* Ignore it if it doesn't have a function pointer or if it
2155 is not currently enabled. */
2156 if (!shell_builtins
[local_index
].function
||
2157 (shell_builtins
[local_index
].flags
& BUILTIN_ENABLED
) == 0)
2160 if (STREQN (shell_builtins
[local_index
].name
, hint
, hint_len
))
2162 int i
= local_index
++;
2164 return (savestring (shell_builtins
[i
].name
));
2172 /* Limited support for completing command words with globbing chars. Only
2173 a single match (multiple matches that end up reducing the number of
2174 characters in the common prefix are bad) will ever be returned on
2175 regular completion. */
2180 glob_ignore_case
= igncase
;
2181 glob_matches
= shell_glob_filename (hint
, 0);
2182 glob_ignore_case
= old_glob_ignore_case
;
2184 if (GLOB_FAILED (glob_matches
) || glob_matches
== 0)
2186 glob_matches
= (char **)NULL
;
2187 return ((char *)NULL
);
2192 if (glob_matches
[1] && rl_completion_type
== TAB
) /* multiple matches are bad */
2193 return ((char *)NULL
);
2196 while (val
= glob_matches
[local_index
++])
2198 if (executable_or_directory (val
))
2200 if (*hint_text
== '~' && directory_part
)
2202 temp
= maybe_restore_tilde (val
, directory_part
);
2211 glob_ignore_case
= old_glob_ignore_case
;
2212 return ((char *)NULL
);
2215 /* If the text passed is a directory in the current directory, return it
2216 as a possible match. Executables in directories in the current
2217 directory can be specified using relative pathnames and successfully
2218 executed even when `.' is not in $PATH. */
2221 hint_is_dir
= 0; /* only return the hint text once */
2222 return (savestring (hint_text
));
2225 /* Repeatedly call filename_completion_function while we have
2226 members of PATH left. Question: should we stat each file?
2227 Answer: we call executable_file () on each file. */
2230 istate
= (val
!= (char *)NULL
);
2236 /* Get the next directory from the path. If there is none, then we
2238 if (path
== 0 || path
[path_index
] == 0 ||
2239 (current_path
= extract_colon_unit (path
, &path_index
)) == 0)
2240 return ((char *)NULL
);
2243 if (*current_path
== 0)
2245 free (current_path
);
2246 current_path
= savestring (".");
2249 if (*current_path
== '~')
2253 t
= bash_tilde_expand (current_path
, 0);
2254 free (current_path
);
2258 if (current_path
[0] == '.' && current_path
[1] == '\0')
2261 if (fnhint
&& fnhint
!= filename_hint
)
2264 free (filename_hint
);
2266 filename_hint
= sh_makepath (current_path
, hint
, 0);
2267 /* Need a quoted version (though it doesn't matter much in most
2268 cases) because rl_filename_completion_function dequotes the
2269 filename it gets, assuming that it's been quoted as part of
2270 the input line buffer. */
2271 if (strpbrk (filename_hint
, "\"'\\"))
2272 fnhint
= sh_backslash_quote (filename_hint
, filename_bstab
, 0);
2274 fnhint
= filename_hint
;
2275 free (current_path
); /* XXX */
2279 val
= rl_filename_completion_function (fnhint
, istate
);
2280 if (mapping_over
== 4 && dircomplete_expand
)
2281 set_directory_hook ();
2287 /* If the hint text is an absolute program, then don't bother
2288 searching through PATH. */
2289 if (absolute_program (hint
))
2290 return ((char *)NULL
);
2296 int match
, freetemp
;
2298 if (absolute_program (hint
))
2301 match
= strncmp (val
, hint
, hint_len
) == 0;
2303 match
= strncasecmp (val
, hint
, hint_len
) == 0;
2305 /* If we performed tilde expansion, restore the original
2307 if (*hint_text
== '~')
2308 temp
= maybe_restore_tilde (val
, directory_part
);
2310 temp
= savestring (val
);
2315 temp
= strrchr (val
, '/');
2321 freetemp
= match
= strncmp (temp
, hint
, hint_len
) == 0;
2323 freetemp
= match
= strncasecmp (temp
, hint
, hint_len
) == 0;
2325 temp
= savestring (temp
);
2328 freetemp
= match
= 0;
2331 /* If we have found a match, and it is an executable file, return it.
2332 We don't return directory names when searching $PATH, since the
2333 bash execution code won't find executables in directories which
2334 appear in directories in $PATH when they're specified using
2335 relative pathnames. */
2337 /* If we're not searching $PATH and we have a relative pathname, we
2338 need to re-canonicalize it before testing whether or not it's an
2339 executable or a directory so the shell treats .. relative to $PWD
2340 according to the physical/logical option. The shell already
2341 canonicalizes the directory name in order to tell readline where
2342 to look, so not doing it here will be inconsistent. */
2343 /* XXX -- currently not used -- will introduce more inconsistency,
2344 since shell does not canonicalize ../foo before passing it to
2346 if (match
&& searching_path
== 0 && *val
== '.')
2350 t
= get_working_directory ("command-word-completion");
2351 t1
= make_absolute (val
, t
);
2353 cval
= sh_canonpath (t1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
2359 if (match
&& executable_completion ((searching_path
? val
: cval
), searching_path
))
2364 val
= ""; /* So it won't be NULL. */
2379 /* Completion inside an unterminated command substitution. */
2381 command_subst_completion_function (text
, state
)
2385 static char **matches
= (char **)NULL
;
2386 static const char *orig_start
;
2387 static char *filename_text
= (char *)NULL
;
2388 static int cmd_index
, start_len
;
2394 free (filename_text
);
2398 else if (*text
== '$' && text
[1] == '(') /* ) */
2400 /* If the text was quoted, suppress any quote character that the
2401 readline completion code would insert. */
2402 rl_completion_suppress_quote
= 1;
2403 start_len
= text
- orig_start
;
2404 filename_text
= savestring (text
);
2409 * At this point we can entertain the idea of re-parsing
2410 * `filename_text' into a (possibly incomplete) command name and
2411 * arguments, and doing completion based on that. This is
2412 * currently very rudimentary, but it is a small improvement.
2414 for (value
= filename_text
+ strlen (filename_text
) - 1; value
> filename_text
; value
--)
2415 if (whitespace (*value
) || member (*value
, COMMAND_SEPARATORS
))
2417 if (value
<= filename_text
)
2418 matches
= rl_completion_matches (filename_text
, command_word_completion_function
);
2422 start_len
+= value
- filename_text
;
2423 if (whitespace (value
[-1]))
2424 matches
= rl_completion_matches (value
, rl_filename_completion_function
);
2426 matches
= rl_completion_matches (value
, command_word_completion_function
);
2429 /* If there is more than one match, rl_completion_matches has already
2430 put the lcd in matches[0]. Skip over it. */
2431 cmd_index
= matches
&& matches
[0] && matches
[1];
2433 /* If there's a single match and it's a directory, set the append char
2434 to the expected `/'. Otherwise, don't append anything. */
2435 if (matches
&& matches
[0] && matches
[1] == 0 && test_for_directory (matches
[0]))
2436 rl_completion_append_character
= '/';
2438 rl_completion_suppress_append
= 1;
2441 if (matches
== 0 || matches
[cmd_index
] == 0)
2443 rl_filename_quoting_desired
= 0; /* disable quoting */
2444 return ((char *)NULL
);
2448 value
= (char *)xmalloc (1 + start_len
+ strlen (matches
[cmd_index
]));
2451 value
[0] = *orig_start
;
2453 strncpy (value
, orig_start
, start_len
);
2455 strcpy (value
+ start_len
, matches
[cmd_index
]);
2462 /* Okay, now we write the entry_function for variable completion. */
2464 variable_completion_function (text
, state
)
2468 static char **varlist
= (char **)NULL
;
2469 static int varlist_index
;
2470 static char *varname
= (char *)NULL
;
2471 static int first_char
, first_char_loc
;
2479 first_char
= text
[0];
2481 if (first_char
== '$')
2484 if (text
[first_char_loc
] == '{')
2487 varname
= savestring (text
+ first_char_loc
);
2490 strvec_dispose (varlist
);
2492 varlist
= all_variables_matching_prefix (varname
);
2496 if (!varlist
|| !varlist
[varlist_index
])
2498 return ((char *)NULL
);
2504 value
= (char *)xmalloc (4 + strlen (varlist
[varlist_index
]));
2508 value
[0] = first_char
;
2509 if (first_char_loc
== 2)
2513 strcpy (value
+ first_char_loc
, varlist
[varlist_index
]);
2514 if (first_char_loc
== 2)
2515 strcat (value
, "}");
2522 /* How about a completion function for hostnames? */
2524 hostname_completion_function (text
, state
)
2528 static char **list
= (char **)NULL
;
2529 static int list_index
= 0;
2530 static int first_char
, first_char_loc
;
2532 /* If we don't have any state, make some. */
2537 list
= (char **)NULL
;
2542 if (first_char
== '@')
2545 list
= hostnames_matching ((char *)text
+first_char_loc
);
2549 if (list
&& list
[list_index
])
2553 t
= (char *)xmalloc (2 + strlen (list
[list_index
]));
2555 strcpy (t
+ first_char_loc
, list
[list_index
]);
2560 return ((char *)NULL
);
2564 * A completion function for service names from /etc/services (or wherever).
2567 bash_servicename_completion_function (text
, state
)
2571 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT)
2572 return ((char *)NULL
);
2574 static char *sname
= (char *)NULL
;
2575 static struct servent
*srvent
;
2576 static int snamelen
;
2578 char **alist
, *aentry
;
2585 sname
= savestring (text
);
2586 snamelen
= strlen (sname
);
2590 while (srvent
= getservent ())
2593 if (snamelen
== 0 || (STREQN (sname
, srvent
->s_name
, snamelen
)))
2595 /* Not primary, check aliases */
2596 for (alist
= srvent
->s_aliases
; *alist
; alist
++)
2599 if (STREQN (sname
, aentry
, snamelen
))
2613 return ((char *)NULL
);
2616 value
= afound
? savestring (aentry
) : savestring (srvent
->s_name
);
2622 * A completion function for group names from /etc/group (or wherever).
2625 bash_groupname_completion_function (text
, state
)
2629 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
2630 return ((char *)NULL
);
2632 static char *gname
= (char *)NULL
;
2633 static struct group
*grent
;
2634 static int gnamelen
;
2640 gname
= savestring (text
);
2641 gnamelen
= strlen (gname
);
2646 while (grent
= getgrent ())
2648 if (gnamelen
== 0 || (STREQN (gname
, grent
->gr_name
, gnamelen
)))
2655 return ((char *)NULL
);
2658 value
= savestring (grent
->gr_name
);
2663 /* Functions to perform history and alias expansions on the current line. */
2665 #if defined (BANG_HISTORY)
2666 /* Perform history expansion on the current line. If no history expansion
2667 is done, pre_process_line() returns what it was passed, so we need to
2668 allocate a new line here. */
2670 history_expand_line_internal (line
)
2676 old_verify
= hist_verify
;
2678 new_line
= pre_process_line (line
, 0, 0);
2679 hist_verify
= old_verify
;
2681 return (new_line
== line
) ? savestring (line
) : new_line
;
2685 /* There was an error in expansion. Let the preprocessor print
2688 cleanup_expansion_error ()
2691 #if defined (BANG_HISTORY)
2694 old_verify
= hist_verify
;
2698 fprintf (rl_outstream
, "\r\n");
2699 to_free
= pre_process_line (rl_line_buffer
, 1, 0);
2700 #if defined (BANG_HISTORY)
2701 hist_verify
= old_verify
;
2703 if (to_free
!= rl_line_buffer
)
2705 putc ('\r', rl_outstream
);
2706 rl_forced_update_display ();
2709 /* If NEW_LINE differs from what is in the readline line buffer, add an
2710 undo record to get from the readline line buffer contents to the new
2711 line and make NEW_LINE the current readline line. */
2713 maybe_make_readline_line (new_line
)
2716 if (new_line
&& strcmp (new_line
, rl_line_buffer
) != 0)
2720 rl_add_undo (UNDO_BEGIN
, 0, 0, 0);
2721 rl_delete_text (0, rl_point
);
2722 rl_point
= rl_end
= rl_mark
= 0;
2723 rl_insert_text (new_line
);
2724 rl_add_undo (UNDO_END
, 0, 0, 0);
2728 /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
2730 set_up_new_line (new_line
)
2733 int old_point
, at_end
;
2735 old_point
= rl_point
;
2736 at_end
= rl_point
== rl_end
;
2738 /* If the line was history and alias expanded, then make that
2739 be one thing to undo. */
2740 maybe_make_readline_line (new_line
);
2743 /* Place rl_point where we think it should go. */
2746 else if (old_point
< rl_end
)
2748 rl_point
= old_point
;
2749 if (!whitespace (rl_line_buffer
[rl_point
]))
2750 rl_forward_word (1, 0);
2755 /* Expand aliases in the current readline line. */
2757 alias_expand_line (count
, ignore
)
2762 new_line
= alias_expand (rl_line_buffer
);
2766 set_up_new_line (new_line
);
2771 cleanup_expansion_error ();
2777 #if defined (BANG_HISTORY)
2778 /* History expand the line. */
2780 history_expand_line (count
, ignore
)
2785 new_line
= history_expand_line_internal (rl_line_buffer
);
2789 set_up_new_line (new_line
);
2794 cleanup_expansion_error ();
2799 /* Expand history substitutions in the current line and then insert a
2800 space (hopefully close to where we were before). */
2802 tcsh_magic_space (count
, ignore
)
2805 int dist_from_end
, old_point
;
2807 old_point
= rl_point
;
2808 dist_from_end
= rl_end
- rl_point
;
2809 if (history_expand_line (count
, ignore
) == 0)
2811 /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
2812 This works if all expansions were before rl_point or if no expansions
2814 rl_point
= (old_point
== 0) ? old_point
: rl_end
- dist_from_end
;
2821 #endif /* BANG_HISTORY */
2823 /* History and alias expand the line. */
2825 history_and_alias_expand_line (count
, ignore
)
2831 #if defined (BANG_HISTORY)
2832 new_line
= history_expand_line_internal (rl_line_buffer
);
2835 t
= expand_string_dollar_quote (new_line
? new_line
: rl_line_buffer
, 0);
2844 alias_line
= alias_expand (new_line
);
2846 new_line
= alias_line
;
2852 set_up_new_line (new_line
);
2857 cleanup_expansion_error ();
2862 /* History and alias expand the line, then perform the shell word
2863 expansions by calling expand_string. This can't use set_up_new_line()
2864 because we want the variable expansions as a separate undo'able
2865 set of operations. */
2867 shell_expand_line (count
, ignore
)
2871 WORD_LIST
*expanded_string
;
2875 #if defined (BANG_HISTORY)
2876 new_line
= history_expand_line_internal (rl_line_buffer
);
2879 t
= expand_string_dollar_quote (new_line
? new_line
: rl_line_buffer
, 0);
2888 alias_line
= alias_expand (new_line
);
2890 new_line
= alias_line
;
2896 int old_point
= rl_point
;
2897 int at_end
= rl_point
== rl_end
;
2899 /* If the line was history and alias expanded, then make that
2900 be one thing to undo. */
2901 maybe_make_readline_line (new_line
);
2904 /* If there is variable expansion to perform, do that as a separate
2905 operation to be undone. */
2908 w
= alloc_word_desc ();
2909 w
->word
= savestring (rl_line_buffer
);
2910 w
->flags
= rl_explicit_arg
? (W_NOPROCSUB
|W_NOCOMSUB
) : 0;
2911 expanded_string
= expand_word (w
, rl_explicit_arg
? Q_HERE_DOCUMENT
: 0);
2914 new_line
= savestring (rl_line_buffer
);
2915 expanded_string
= expand_string (new_line
, 0);
2919 if (expanded_string
== 0)
2921 new_line
= (char *)xmalloc (1);
2926 new_line
= string_list (expanded_string
);
2927 dispose_words (expanded_string
);
2930 maybe_make_readline_line (new_line
);
2933 /* Place rl_point where we think it should go. */
2936 else if (old_point
< rl_end
)
2938 rl_point
= old_point
;
2939 if (!whitespace (rl_line_buffer
[rl_point
]))
2940 rl_forward_word (1, 0);
2946 cleanup_expansion_error ();
2951 /* If FIGNORE is set, then don't match files with the given suffixes when
2952 completing filenames. If only one of the possibilities has an acceptable
2953 suffix, delete the others, else just return and let the completer
2954 signal an error. It is called by the completer when real
2955 completions are done on filenames by the completer's internal
2956 function, not for completion lists (M-?) and not on "other"
2957 completion types, such as hostnames or commands. */
2959 static struct ignorevar fignore
=
2965 (sh_iv_item_func_t
*) 0,
2969 _ignore_completion_names (names
, name_func
)
2971 sh_ignore_func_t
*name_func
;
2978 /* If there is only one completion, see if it is acceptable. If it is
2979 not, free it up. In any case, short-circuit and return. This is a
2980 special case because names[0] is not the prefix of the list of names
2981 if there is only one completion; it is the completion itself. */
2982 if (names
[1] == (char *)0)
2985 if ((*name_func
) (names
[0]) == 0)
2988 names
[0] = (char *)NULL
;
2994 /* Allocate space for array to hold list of pointers to matching
2995 filenames. The pointers are copied back to NAMES when done. */
2996 for (nidx
= 1; names
[nidx
]; nidx
++)
2998 newnames
= strvec_create (nidx
+ 1);
3000 if (force_fignore
== 0)
3002 oldnames
= strvec_create (nidx
- 1);
3006 newnames
[0] = names
[0];
3007 for (idx
= nidx
= 1; names
[idx
]; idx
++)
3009 if ((*name_func
) (names
[idx
]))
3010 newnames
[nidx
++] = names
[idx
];
3011 else if (force_fignore
== 0)
3012 oldnames
[oidx
++] = names
[idx
];
3017 newnames
[nidx
] = (char *)NULL
;
3019 /* If none are acceptable then let the completer handle it. */
3025 names
[0] = (char *)NULL
;
3034 if (force_fignore
== 0)
3037 free (oldnames
[--oidx
]);
3041 /* If only one is acceptable, copy it to names[0] and return. */
3045 names
[0] = newnames
[1];
3046 names
[1] = (char *)NULL
;
3051 /* Copy the acceptable names back to NAMES, set the new array end,
3053 for (nidx
= 1; newnames
[nidx
]; nidx
++)
3054 names
[nidx
] = newnames
[nidx
];
3055 names
[nidx
] = (char *)NULL
;
3060 name_is_acceptable (name
)
3066 for (nlen
= strlen (name
), p
= fignore
.ignores
; p
->val
; p
++)
3068 if (nlen
> p
->len
&& p
->len
> 0 && STREQ (p
->val
, &name
[nlen
- p
->len
]))
3077 ignore_dot_names (name
)
3080 return (name
[0] != '.');
3085 filename_completion_ignore (names
)
3089 if (glob_dot_filenames
== 0)
3090 _ignore_completion_names (names
, ignore_dot_names
);
3093 setup_ignore_patterns (&fignore
);
3095 if (fignore
.num_ignores
== 0)
3098 _ignore_completion_names (names
, name_is_acceptable
);
3103 /* Return 1 if NAME is a directory. NAME undergoes tilde expansion. */
3105 test_for_directory (name
)
3111 fn
= bash_tilde_expand (name
, 0);
3112 r
= file_isdir (fn
);
3119 test_for_canon_directory (name
)
3125 fn
= (*name
== '~') ? bash_tilde_expand (name
, 0) : savestring (name
);
3126 bash_filename_stat_hook (&fn
);
3127 r
= file_isdir (fn
);
3133 /* Remove files from NAMES, leaving directories. */
3135 bash_ignore_filenames (names
)
3138 _ignore_completion_names (names
, test_for_directory
);
3143 bash_progcomp_ignore_filenames (names
)
3146 _ignore_completion_names (names
, test_for_canon_directory
);
3158 bash_ignore_everything (names
)
3161 _ignore_completion_names (names
, return_zero
);
3165 /* Replace a tilde-prefix in VAL with a `~', assuming the user typed it. VAL
3166 is an expanded filename. DIRECTORY_PART is the tilde-prefix portion
3167 of the un-tilde-expanded version of VAL (what the user typed). */
3169 restore_tilde (val
, directory_part
)
3170 char *val
, *directory_part
;
3173 char *dh2
, *expdir
, *ret
, *v
;
3177 /* We need to duplicate the expansions readline performs on the directory
3178 portion before passing it to our completion function. */
3179 dh2
= directory_part
? bash_dequote_filename (directory_part
, 0) : 0;
3180 bash_directory_expansion (&dh2
);
3183 expdir
= bash_tilde_expand (directory_part
, 0);
3184 xl
= strlen (expdir
);
3185 if (*directory_part
== '~' && STREQ (directory_part
, expdir
))
3187 /* tilde expansion failed, so what should we return? we use what the
3189 v
= mbschr (val
, '/');
3191 ret
= (char *)xmalloc (xl
+ vl
+ 2);
3192 strcpy (ret
, directory_part
);
3194 strcpy (ret
+ xl
, v
);
3204 dh2 = unexpanded but dequoted tilde-prefix
3205 dl2 = length of tilde-prefix
3206 expdir = tilde-expanded tilde-prefix
3207 xl = length of expanded tilde-prefix
3208 l = length of remainder after tilde-prefix
3214 return (savestring (val
)); /* XXX - just punt */
3217 ret
= (char *)xmalloc (dl2
+ 2 + l
);
3219 strcpy (ret
+ dl2
, val
+ xl
);
3226 maybe_restore_tilde (val
, directory_part
)
3227 char *val
, *directory_part
;
3229 rl_icppfunc_t
*save
;
3232 save
= (dircomplete_expand
== 0) ? save_directory_hook () : (rl_icppfunc_t
*)0;
3233 ret
= restore_tilde (val
, directory_part
);
3235 restore_directory_hook (save
);
3239 /* Simulate the expansions that will be performed by
3240 rl_filename_completion_function. This must be called with the address of
3241 a pointer to malloc'd memory. */
3243 bash_directory_expansion (dirname
)
3248 d
= savestring (*dirname
);
3250 if ((rl_directory_rewrite_hook
) && (*rl_directory_rewrite_hook
) (&d
))
3255 else if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&d
))
3260 else if (rl_completion_found_quote
)
3262 nd
= bash_dequote_filename (d
, rl_completion_quote_character
);
3269 /* If necessary, rewrite directory entry */
3271 bash_filename_rewrite_hook (fname
, fnlen
)
3277 conv
= fnx_fromfs (fname
, fnlen
);
3279 conv
= savestring (conv
);
3283 /* Functions to save and restore the appropriate directory hook */
3284 /* This is not static so the shopt code can call it */
3286 set_directory_hook ()
3288 if (dircomplete_expand
)
3290 rl_directory_completion_hook
= bash_directory_completion_hook
;
3291 rl_directory_rewrite_hook
= (rl_icppfunc_t
*)0;
3295 rl_directory_rewrite_hook
= bash_directory_completion_hook
;
3296 rl_directory_completion_hook
= (rl_icppfunc_t
*)0;
3300 static rl_icppfunc_t
*
3301 save_directory_hook ()
3305 if (dircomplete_expand
)
3307 ret
= rl_directory_completion_hook
;
3308 rl_directory_completion_hook
= (rl_icppfunc_t
*)NULL
;
3312 ret
= rl_directory_rewrite_hook
;
3313 rl_directory_rewrite_hook
= (rl_icppfunc_t
*)NULL
;
3320 restore_directory_hook (hookf
)
3321 rl_icppfunc_t
*hookf
;
3323 if (dircomplete_expand
)
3324 rl_directory_completion_hook
= hookf
;
3326 rl_directory_rewrite_hook
= hookf
;
3329 /* Check whether not DIRNAME, with any trailing slash removed, exists. If
3330 SHOULD_DEQUOTE is non-zero, we dequote the directory name first. */
3332 directory_exists (dirname
, should_dequote
)
3333 const char *dirname
;
3340 /* We save the string and chop the trailing slash because stat/lstat behave
3341 inconsistently if one is present. */
3342 new_dirname
= should_dequote
? bash_dequote_filename ((char *)dirname
, rl_completion_quote_character
) : savestring (dirname
);
3343 dirlen
= STRLEN (new_dirname
);
3344 if (new_dirname
[dirlen
- 1] == '/')
3345 new_dirname
[dirlen
- 1] = '\0';
3346 #if defined (HAVE_LSTAT)
3347 r
= lstat (new_dirname
, &sb
) == 0;
3349 r
= stat (new_dirname
, &sb
) == 0;
3355 /* Expand a filename before the readline completion code passes it to stat(2).
3356 The filename will already have had tilde expansion performed. */
3358 bash_filename_stat_hook (dirname
)
3361 char *local_dirname
, *new_dirname
, *t
;
3362 int should_expand_dirname
, return_value
;
3366 local_dirname
= *dirname
;
3367 should_expand_dirname
= return_value
= 0;
3368 if (t
= mbschr (local_dirname
, '$'))
3369 should_expand_dirname
= '$';
3370 else if (t
= mbschr (local_dirname
, '`')) /* XXX */
3371 should_expand_dirname
= '`';
3373 if (should_expand_dirname
&& directory_exists (local_dirname
, 0))
3374 should_expand_dirname
= 0;
3376 if (should_expand_dirname
)
3378 new_dirname
= savestring (local_dirname
);
3379 /* no error messages, and expand_prompt_string doesn't longjmp so we don't
3380 have to worry about restoring this setting. */
3381 global_nounset
= unbound_vars_is_error
;
3382 unbound_vars_is_error
= 0;
3383 wl
= expand_prompt_string (new_dirname
, 0, W_NOCOMSUB
|W_NOPROCSUB
|W_COMPLETE
); /* does the right thing */
3384 unbound_vars_is_error
= global_nounset
;
3388 new_dirname
= string_list (wl
);
3389 /* Tell the completer we actually expanded something and change
3390 *dirname only if we expanded to something non-null -- stat
3391 behaves unpredictably when passed null or empty strings */
3392 if (new_dirname
&& *new_dirname
)
3394 free (local_dirname
); /* XXX */
3395 local_dirname
= *dirname
= new_dirname
;
3396 return_value
= STREQ (local_dirname
, *dirname
) == 0;
3406 /* This is very similar to the code in bash_directory_completion_hook below,
3407 but without spelling correction and not worrying about whether or not
3408 we change relative pathnames. */
3409 if (no_symbolic_links
== 0 && (local_dirname
[0] != '.' || local_dirname
[1]))
3411 char *temp1
, *temp2
;
3413 t
= get_working_directory ("symlink-hook");
3414 temp1
= make_absolute (local_dirname
, t
);
3416 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3418 /* If we can't canonicalize, bail. */
3422 return return_value
;
3425 free (local_dirname
);
3430 return (return_value
);
3433 /* Handle symbolic link references and other directory name
3434 expansions while hacking completion. This should return 1 if it modifies
3435 the DIRNAME argument, 0 otherwise. It should make sure not to modify
3436 DIRNAME if it returns 0. */
3438 bash_directory_completion_hook (dirname
)
3441 char *local_dirname
, *new_dirname
, *t
;
3442 int return_value
, should_expand_dirname
, nextch
, closer
;
3445 return_value
= should_expand_dirname
= nextch
= closer
= 0;
3446 local_dirname
= *dirname
;
3448 if (t
= mbschr (local_dirname
, '$'))
3450 should_expand_dirname
= '$';
3452 /* Deliberately does not handle the deprecated $[...] arithmetic
3456 else if (nextch
== '{')
3466 delims
[0] = closer
; delims
[1] = 0;
3467 p
= skip_to_delim (t
, 1, delims
, SD_NOJMP
|SD_COMPLETE
);
3469 should_expand_dirname
= 0;
3472 else if (local_dirname
[0] == '~')
3473 should_expand_dirname
= '~';
3476 t
= mbschr (local_dirname
, '`');
3477 if (t
&& unclosed_pair (local_dirname
, strlen (local_dirname
), "`") == 0)
3478 should_expand_dirname
= '`';
3481 if (should_expand_dirname
&& directory_exists (local_dirname
, 1))
3482 should_expand_dirname
= 0;
3484 if (should_expand_dirname
)
3486 new_dirname
= savestring (local_dirname
);
3487 wl
= expand_prompt_string (new_dirname
, 0, W_NOCOMSUB
|W_NOPROCSUB
|W_COMPLETE
); /* does the right thing */
3490 *dirname
= string_list (wl
);
3491 /* Tell the completer to replace the directory name only if we
3492 actually expanded something. */
3493 return_value
= STREQ (local_dirname
, *dirname
) == 0;
3494 free (local_dirname
);
3497 local_dirname
= *dirname
;
3498 /* XXX - change rl_filename_quote_characters here based on
3499 should_expand_dirname/nextch/closer. This is the only place
3500 custom_filename_quote_characters is modified. */
3501 if (rl_filename_quote_characters
&& *rl_filename_quote_characters
)
3504 i
= strlen (default_filename_quote_characters
);
3505 custom_filename_quote_characters
= xrealloc (custom_filename_quote_characters
, i
+1);
3506 for (i
= j
= 0; c
= default_filename_quote_characters
[i
]; i
++)
3508 if (c
== should_expand_dirname
|| c
== nextch
|| c
== closer
)
3510 custom_filename_quote_characters
[j
++] = c
;
3512 custom_filename_quote_characters
[j
] = '\0';
3513 rl_filename_quote_characters
= custom_filename_quote_characters
;
3514 set_filename_bstab (rl_filename_quote_characters
);
3520 free (local_dirname
);
3521 *dirname
= (char *)xmalloc (1);
3528 /* Dequote the filename even if we don't expand it. */
3529 new_dirname
= bash_dequote_filename (local_dirname
, rl_completion_quote_character
);
3530 return_value
= STREQ (local_dirname
, new_dirname
) == 0;
3531 free (local_dirname
);
3532 local_dirname
= *dirname
= new_dirname
;
3535 /* no_symbolic_links == 0 -> use (default) logical view of the file system.
3536 local_dirname[0] == '.' && local_dirname[1] == '/' means files in the
3537 current directory (./).
3538 local_dirname[0] == '.' && local_dirname[1] == 0 means relative pathnames
3539 in the current directory (e.g., lib/sh).
3540 XXX - should we do spelling correction on these? */
3542 /* This is test as it was in bash-4.2: skip relative pathnames in current
3543 directory. Change test to
3544 (local_dirname[0] != '.' || (local_dirname[1] && local_dirname[1] != '/'))
3545 if we want to skip paths beginning with ./ also. */
3546 if (no_symbolic_links
== 0 && (local_dirname
[0] != '.' || local_dirname
[1]))
3548 char *temp1
, *temp2
;
3551 /* If we have a relative path
3552 (local_dirname[0] != '/' && local_dirname[0] != '.')
3553 that is canonical after appending it to the current directory, then
3556 strcmp (temp1, temp2) == 0
3557 after adding a slash to temp2 below. It should be safe to not
3560 t
= get_working_directory ("symlink-hook");
3561 temp1
= make_absolute (local_dirname
, t
);
3563 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3565 /* Try spelling correction if initial canonicalization fails. Make
3566 sure we are set to replace the directory name with the results so
3567 subsequent directory checks don't fail. */
3568 if (temp2
== 0 && dircomplete_spelling
&& dircomplete_expand
)
3570 temp2
= dirspell (temp1
);
3575 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3576 return_value
|= temp2
!= 0;
3579 /* If we can't canonicalize, bail. */
3583 return return_value
;
3585 len1
= strlen (temp1
);
3586 if (temp1
[len1
- 1] == '/')
3588 len2
= strlen (temp2
);
3589 if (len2
> 2) /* don't append `/' to `/' or `//' */
3591 temp2
= (char *)xrealloc (temp2
, len2
+ 2);
3593 temp2
[len2
+ 1] = '\0';
3597 /* dircomplete_expand_relpath == 0 means we want to leave relative
3598 pathnames that are unchanged by canonicalization alone.
3599 *local_dirname != '/' && *local_dirname != '.' == relative pathname
3600 (consistent with general.c:absolute_pathname())
3601 temp1 == temp2 (after appending a slash to temp2) means the pathname
3602 is not changed by canonicalization as described above. */
3603 if (dircomplete_expand_relpath
|| ((local_dirname
[0] != '/' && local_dirname
[0] != '.') && STREQ (temp1
, temp2
) == 0))
3604 return_value
|= STREQ (local_dirname
, temp2
) == 0;
3605 free (local_dirname
);
3610 return (return_value
);
3613 static char **history_completion_array
= (char **)NULL
;
3614 static int harry_size
;
3615 static int harry_len
;
3618 build_history_completion_array ()
3624 /* First, clear out the current dynamic history completion list. */
3627 strvec_dispose (history_completion_array
);
3628 history_completion_array
= (char **)NULL
;
3633 /* Next, grovel each line of history, making each shell-sized token
3634 a separate entry in the history_completion_array. */
3635 hlist
= history_list ();
3639 for (i
= 0; hlist
[i
]; i
++)
3641 for ( --i
; i
>= 0; i
--)
3643 /* Separate each token, and place into an array. */
3644 tokens
= history_tokenize (hlist
[i
]->line
);
3646 for (j
= 0; tokens
&& tokens
[j
]; j
++)
3648 if (harry_len
+ 2 > harry_size
)
3649 history_completion_array
= strvec_resize (history_completion_array
, harry_size
+= 10);
3651 history_completion_array
[harry_len
++] = tokens
[j
];
3652 history_completion_array
[harry_len
] = (char *)NULL
;
3657 /* Sort the complete list of tokens. */
3658 if (dabbrev_expand_active
== 0)
3659 qsort (history_completion_array
, harry_len
, sizeof (char *), (QSFUNC
*)strvec_strcmp
);
3664 history_completion_generator (hint_text
, state
)
3665 const char *hint_text
;
3668 static int local_index
, len
;
3669 static const char *text
;
3671 /* If this is the first call to the generator, then initialize the
3672 list of strings to complete over. */
3675 if (dabbrev_expand_active
) /* This is kind of messy */
3676 rl_completion_suppress_append
= 1;
3678 build_history_completion_array ();
3680 len
= strlen (text
);
3683 while (history_completion_array
&& history_completion_array
[local_index
])
3685 /* XXX - should this use completion-ignore-case? */
3686 if (strncmp (text
, history_completion_array
[local_index
++], len
) == 0)
3687 return (savestring (history_completion_array
[local_index
- 1]));
3689 return ((char *)NULL
);
3693 dynamic_complete_history (count
, key
)
3697 rl_compentry_func_t
*orig_func
;
3698 rl_completion_func_t
*orig_attempt_func
;
3699 rl_compignore_func_t
*orig_ignore_func
;
3701 orig_func
= rl_completion_entry_function
;
3702 orig_attempt_func
= rl_attempted_completion_function
;
3703 orig_ignore_func
= rl_ignore_some_completions_function
;
3705 rl_completion_entry_function
= history_completion_generator
;
3706 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3707 rl_ignore_some_completions_function
= filename_completion_ignore
;
3709 /* XXX - use rl_completion_mode here? */
3710 if (rl_last_func
== dynamic_complete_history
)
3711 r
= rl_complete_internal ('?');
3713 r
= rl_complete_internal (TAB
);
3715 rl_completion_entry_function
= orig_func
;
3716 rl_attempted_completion_function
= orig_attempt_func
;
3717 rl_ignore_some_completions_function
= orig_ignore_func
;
3723 bash_dabbrev_expand (count
, key
)
3726 int r
, orig_suppress
, orig_sort
;
3727 rl_compentry_func_t
*orig_func
;
3728 rl_completion_func_t
*orig_attempt_func
;
3729 rl_compignore_func_t
*orig_ignore_func
;
3731 orig_func
= rl_menu_completion_entry_function
;
3732 orig_attempt_func
= rl_attempted_completion_function
;
3733 orig_ignore_func
= rl_ignore_some_completions_function
;
3734 orig_suppress
= rl_completion_suppress_append
;
3735 orig_sort
= rl_sort_completion_matches
;
3737 rl_menu_completion_entry_function
= history_completion_generator
;
3738 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3739 rl_ignore_some_completions_function
= filename_completion_ignore
;
3740 rl_filename_completion_desired
= 0;
3741 rl_completion_suppress_append
= 1;
3742 rl_sort_completion_matches
= 0;
3744 /* XXX - use rl_completion_mode here? */
3745 dabbrev_expand_active
= 1;
3746 if (rl_last_func
== bash_dabbrev_expand
)
3747 rl_last_func
= rl_menu_complete
;
3748 r
= rl_menu_complete (count
, key
);
3749 dabbrev_expand_active
= 0;
3751 rl_last_func
= bash_dabbrev_expand
;
3752 rl_menu_completion_entry_function
= orig_func
;
3753 rl_attempted_completion_function
= orig_attempt_func
;
3754 rl_ignore_some_completions_function
= orig_ignore_func
;
3755 rl_completion_suppress_append
= orig_suppress
;
3756 rl_sort_completion_matches
= orig_sort
;
3761 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
3763 bash_complete_username (ignore
, ignore2
)
3764 int ignore
, ignore2
;
3766 return bash_complete_username_internal (rl_completion_mode (bash_complete_username
));
3770 bash_possible_username_completions (ignore
, ignore2
)
3771 int ignore
, ignore2
;
3773 return bash_complete_username_internal ('?');
3777 bash_complete_username_internal (what_to_do
)
3780 return bash_specific_completion (what_to_do
, rl_username_completion_function
);
3784 bash_complete_filename (ignore
, ignore2
)
3785 int ignore
, ignore2
;
3787 return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename
));
3791 bash_possible_filename_completions (ignore
, ignore2
)
3792 int ignore
, ignore2
;
3794 return bash_complete_filename_internal ('?');
3798 bash_complete_filename_internal (what_to_do
)
3801 rl_compentry_func_t
*orig_func
;
3802 rl_completion_func_t
*orig_attempt_func
;
3803 rl_icppfunc_t
*orig_dir_func
;
3804 rl_compignore_func_t
*orig_ignore_func
;
3805 /*const*/ char *orig_rl_completer_word_break_characters
;
3808 orig_func
= rl_completion_entry_function
;
3809 orig_attempt_func
= rl_attempted_completion_function
;
3810 orig_ignore_func
= rl_ignore_some_completions_function
;
3811 orig_rl_completer_word_break_characters
= rl_completer_word_break_characters
;
3813 orig_dir_func
= save_directory_hook ();
3815 rl_completion_entry_function
= rl_filename_completion_function
;
3816 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3817 rl_ignore_some_completions_function
= filename_completion_ignore
;
3818 rl_completer_word_break_characters
= " \t\n\"\'";
3820 r
= rl_complete_internal (what_to_do
);
3822 rl_completion_entry_function
= orig_func
;
3823 rl_attempted_completion_function
= orig_attempt_func
;
3824 rl_ignore_some_completions_function
= orig_ignore_func
;
3825 rl_completer_word_break_characters
= orig_rl_completer_word_break_characters
;
3827 restore_directory_hook (orig_dir_func
);
3833 bash_complete_hostname (ignore
, ignore2
)
3834 int ignore
, ignore2
;
3836 return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname
));
3840 bash_possible_hostname_completions (ignore
, ignore2
)
3841 int ignore
, ignore2
;
3843 return bash_complete_hostname_internal ('?');
3847 bash_complete_variable (ignore
, ignore2
)
3848 int ignore
, ignore2
;
3850 return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable
));
3854 bash_possible_variable_completions (ignore
, ignore2
)
3855 int ignore
, ignore2
;
3857 return bash_complete_variable_internal ('?');
3861 bash_complete_command (ignore
, ignore2
)
3862 int ignore
, ignore2
;
3864 return bash_complete_command_internal (rl_completion_mode (bash_complete_command
));
3868 bash_possible_command_completions (ignore
, ignore2
)
3869 int ignore
, ignore2
;
3871 return bash_complete_command_internal ('?');
3875 bash_complete_hostname_internal (what_to_do
)
3878 return bash_specific_completion (what_to_do
, hostname_completion_function
);
3882 bash_complete_variable_internal (what_to_do
)
3885 return bash_specific_completion (what_to_do
, variable_completion_function
);
3889 bash_complete_command_internal (what_to_do
)
3892 return bash_specific_completion (what_to_do
, command_word_completion_function
);
3896 completion_glob_pattern (string
)
3899 return (glob_pattern_p (string
) == 1);
3902 static char *globtext
;
3903 static char *globorig
;
3906 glob_complete_word (text
, state
)
3910 static char **matches
= (char **)NULL
;
3917 rl_filename_completion_desired
= 1;
3919 if (globorig
!= globtext
)
3923 ttext
= bash_tilde_expand (text
, 0);
3925 if (rl_explicit_arg
)
3927 globorig
= savestring (ttext
);
3928 glen
= strlen (ttext
);
3929 globtext
= (char *)xmalloc (glen
+ 2);
3930 strcpy (globtext
, ttext
);
3931 globtext
[glen
] = '*';
3932 globtext
[glen
+1] = '\0';
3935 globtext
= globorig
= savestring (ttext
);
3940 matches
= shell_glob_filename (globtext
, 0);
3941 if (GLOB_FAILED (matches
))
3942 matches
= (char **)NULL
;
3946 ret
= matches
? matches
[ind
] : (char *)NULL
;
3952 bash_glob_completion_internal (what_to_do
)
3955 return bash_specific_completion (what_to_do
, glob_complete_word
);
3958 /* A special quoting function so we don't end up quoting globbing characters
3959 in the word if there are no matches or multiple matches. */
3961 bash_glob_quote_filename (s
, rtype
, qcp
)
3966 if (globorig
&& qcp
&& *qcp
== '\0' && STREQ (s
, globorig
))
3967 return (savestring (s
));
3969 return (bash_quote_filename (s
, rtype
, qcp
));
3973 bash_glob_complete_word (count
, key
)
3977 rl_quote_func_t
*orig_quoting_function
;
3979 if (rl_editing_mode
== EMACS_EDITING_MODE
)
3980 rl_explicit_arg
= 1; /* force `*' append */
3981 orig_quoting_function
= rl_filename_quoting_function
;
3982 rl_filename_quoting_function
= bash_glob_quote_filename
;
3984 r
= bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word
));
3986 rl_filename_quoting_function
= orig_quoting_function
;
3991 bash_glob_expand_word (count
, key
)
3994 return bash_glob_completion_internal ('*');
3998 bash_glob_list_expansions (count
, key
)
4001 return bash_glob_completion_internal ('?');
4005 bash_specific_completion (what_to_do
, generator
)
4007 rl_compentry_func_t
*generator
;
4009 rl_compentry_func_t
*orig_func
;
4010 rl_completion_func_t
*orig_attempt_func
;
4011 rl_compignore_func_t
*orig_ignore_func
;
4014 orig_func
= rl_completion_entry_function
;
4015 orig_attempt_func
= rl_attempted_completion_function
;
4016 orig_ignore_func
= rl_ignore_some_completions_function
;
4017 rl_completion_entry_function
= generator
;
4018 rl_attempted_completion_function
= NULL
;
4019 rl_ignore_some_completions_function
= orig_ignore_func
;
4021 r
= rl_complete_internal (what_to_do
);
4023 rl_completion_entry_function
= orig_func
;
4024 rl_attempted_completion_function
= orig_attempt_func
;
4025 rl_ignore_some_completions_function
= orig_ignore_func
;
4030 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
4032 #if defined (VI_MODE)
4033 /* Completion, from vi mode's point of view. This is a modified version of
4034 rl_vi_complete which uses the bash globbing code to implement what POSIX
4035 specifies, which is to append a `*' and attempt filename generation (which
4036 has the side effect of expanding any globbing characters in the word). */
4038 bash_vi_complete (count
, key
)
4041 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
4045 if ((rl_point
< rl_end
) && (!whitespace (rl_line_buffer
[rl_point
])))
4047 if (!whitespace (rl_line_buffer
[rl_point
+ 1]))
4048 rl_vi_end_word (1, 'E');
4052 /* Find boundaries of current word, according to vi definition of a
4058 rl_vi_bWord (1, 'B');
4063 t
= substring (rl_line_buffer
, p
, rl_point
);
4066 if (t
&& completion_glob_pattern (t
) == 0)
4067 rl_explicit_arg
= 1; /* XXX - force glob_complete_word to append `*' */
4070 if (key
== '*') /* Expansion and replacement. */
4071 r
= bash_glob_expand_word (count
, key
);
4072 else if (key
== '=') /* List possible completions. */
4073 r
= bash_glob_list_expansions (count
, key
);
4074 else if (key
== '\\') /* Standard completion */
4075 r
= bash_glob_complete_word (count
, key
);
4077 r
= rl_complete (0, key
);
4079 if (key
== '*' || key
== '\\')
4080 rl_vi_start_inserting (key
, 1, 1);
4084 return rl_vi_complete (count
, key
);
4085 #endif /* !SPECIFIC_COMPLETION_FUNCTIONS */
4087 #endif /* VI_MODE */
4089 /* Filename quoting for completion. */
4090 /* A function to strip unquoted quote characters (single quotes, double
4091 quotes, and backslashes). It allows single quotes to appear
4092 within double quotes, and vice versa. It should be smarter. */
4094 bash_dequote_filename (text
, quote_char
)
4102 ret
= (char *)xmalloc (l
+ 1);
4103 for (quoted
= quote_char
, p
= text
, r
= ret
; p
&& *p
; p
++)
4105 /* Allow backslash-escaped characters to pass through unscathed. */
4108 /* Backslashes are preserved within single quotes. */
4111 /* Backslashes are preserved within double quotes unless the
4112 character is one that is defined to be escaped */
4113 else if (quoted
== '"' && ((sh_syntaxtab
[(unsigned char)p
[1]] & CBSDQUOTE
) == 0))
4118 return ret
; /* XXX - was break; */
4122 if (quoted
&& *p
== quoted
)
4128 if (quoted
== 0 && (*p
== '\'' || *p
== '"'))
4139 /* Quote characters that the readline completion code would treat as
4140 word break characters with backslashes. Pass backslash-quoted
4141 characters through without examination. */
4143 quote_word_break_chars (text
)
4150 ret
= (char *)xmalloc ((2 * l
) + 1);
4151 for (s
= text
, r
= ret
; *s
; s
++)
4153 /* Pass backslash-quoted characters through, including the backslash. */
4162 /* OK, we have an unquoted character. Check its presence in
4163 rl_completer_word_break_characters. */
4164 if (mbschr (rl_completer_word_break_characters
, *s
))
4166 /* XXX -- check for standalone tildes here and backslash-quote them */
4167 if (s
== text
&& *s
== '~' && file_exists (text
))
4175 /* Use characters in STRING to populate the table of characters that should
4176 be backslash-quoted. The table will be used for sh_backslash_quote from
4179 set_filename_bstab (string
)
4184 memset (filename_bstab
, 0, sizeof (filename_bstab
));
4185 for (s
= string
; s
&& *s
; s
++)
4186 filename_bstab
[(unsigned char)*s
] = 1;
4189 /* Quote a filename using double quotes, single quotes, or backslashes
4190 depending on the value of completion_quoting_style. If we're
4191 completing using backslashes, we need to quote some additional
4192 characters (those that readline treats as word breaks), so we call
4193 quote_word_break_chars on the result. This returns newly-allocated
4196 bash_quote_filename (s
, rtype
, qcp
)
4201 char *rtext
, *mtext
, *ret
;
4204 rtext
= (char *)NULL
;
4206 /* If RTYPE == MULT_MATCH, it means that there is
4207 more than one match. In this case, we do not add
4208 the closing quote or attempt to perform tilde
4209 expansion. If RTYPE == SINGLE_MATCH, we try
4210 to perform tilde expansion, because single and double
4211 quotes inhibit tilde expansion by the shell. */
4213 cs
= completion_quoting_style
;
4214 /* Might need to modify the default completion style based on *qcp,
4215 since it's set to any user-provided opening quote. We also change
4216 to single-quoting if there is no user-provided opening quote and
4217 the word being completed contains newlines, since those are not
4218 quoted correctly using backslashes (a backslash-newline pair is
4219 special to the shell parser). */
4220 if (*qcp
== '\0' && cs
== COMPLETE_BSQUOTE
&& mbschr (s
, '\n'))
4221 cs
= COMPLETE_SQUOTE
;
4222 else if (*qcp
== '"')
4223 cs
= COMPLETE_DQUOTE
;
4224 else if (*qcp
== '\'')
4225 cs
= COMPLETE_SQUOTE
;
4226 #if defined (BANG_HISTORY)
4227 else if (*qcp
== '\0' && history_expansion
&& cs
== COMPLETE_DQUOTE
&&
4228 history_expansion_inhibited
== 0 && mbschr (s
, '!'))
4229 cs
= COMPLETE_BSQUOTE
;
4231 if (*qcp
== '"' && history_expansion
&& cs
== COMPLETE_DQUOTE
&&
4232 history_expansion_inhibited
== 0 && mbschr (s
, '!'))
4234 cs
= COMPLETE_BSQUOTE
;
4239 /* Don't tilde-expand backslash-quoted filenames, since only single and
4240 double quotes inhibit tilde expansion. */
4242 if (mtext
[0] == '~' && rtype
== SINGLE_MATCH
&& cs
!= COMPLETE_BSQUOTE
)
4243 mtext
= bash_tilde_expand (s
, 0);
4247 case COMPLETE_DQUOTE
:
4248 rtext
= sh_double_quote (mtext
);
4250 case COMPLETE_SQUOTE
:
4251 rtext
= sh_single_quote (mtext
);
4253 case COMPLETE_BSQUOTE
:
4254 rtext
= sh_backslash_quote (mtext
, complete_fullquote
? 0 : filename_bstab
, 0);
4261 /* We may need to quote additional characters: those that readline treats
4262 as word breaks that are not quoted by backslash_quote. */
4263 if (rtext
&& cs
== COMPLETE_BSQUOTE
)
4265 mtext
= quote_word_break_chars (rtext
);
4270 /* Leave the opening quote intact. The readline completion code takes
4271 care of avoiding doubled opening quotes. */
4274 rlen
= strlen (rtext
);
4275 ret
= (char *)xmalloc (rlen
+ 1);
4276 strcpy (ret
, rtext
);
4280 ret
= (char *)xmalloc (rlen
= 1);
4284 /* If there are multiple matches, cut off the closing quote. */
4285 if (rtype
== MULT_MATCH
&& cs
!= COMPLETE_BSQUOTE
)
4286 ret
[rlen
- 1] = '\0';
4291 /* Support for binding readline key sequences to Unix commands. Each editing
4292 mode has a separate Unix command keymap. */
4294 static Keymap emacs_std_cmd_xmap
;
4295 #if defined (VI_MODE)
4296 static Keymap vi_insert_cmd_xmap
;
4297 static Keymap vi_movement_cmd_xmap
;
4309 x
= putc (c
, rl_outstream
);
4316 readline_get_char_offset (ind
)
4322 #if defined (HANDLE_MULTIBYTE)
4323 if (locale_mb_cur_max
> 1)
4325 old_ch
= rl_line_buffer
[ind
];
4326 rl_line_buffer
[ind
] = '\0';
4327 r
= MB_STRLEN (rl_line_buffer
);
4328 rl_line_buffer
[ind
] = old_ch
;
4335 readline_set_char_offset (ind
, varp
)
4343 #if defined (HANDLE_MULTIBYTE)
4344 if (i
> 0 && locale_mb_cur_max
> 1)
4345 i
= _rl_find_next_mbchar (rl_line_buffer
, 0, i
, 0); /* XXX */
4358 bash_execute_unix_command (count
, key
)
4359 int count
; /* ignored */
4365 sh_parser_state_t ps
;
4366 char *cmd
, *value
, *ce
, old_ch
;
4368 char ibuf
[INT_STRLEN_BOUND(int) + 1];
4371 /* First, we need to find the right command to execute. This is tricky,
4372 because we might have already indirected into another keymap, so we
4373 have to walk cmd_xmap using the entire key sequence. */
4374 cmd_xmap
= get_cmd_xmap_from_keymap (rl_get_keymap ());
4375 cmd
= (char *)rl_function_of_keyseq_len (rl_executing_keyseq
, rl_key_sequence_length
, cmd_xmap
, &type
);
4377 if (type
== ISKMAP
&& (type
= ((Keymap
) cmd
)[ANYOTHERKEY
].type
) == ISMACR
)
4378 cmd
= (char*)((Keymap
) cmd
)[ANYOTHERKEY
].function
;
4380 if (cmd
== 0 || type
!= ISMACR
)
4383 internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
4384 rl_forced_update_display ();
4388 ce
= rl_get_termcap ("ce");
4389 if (ce
) /* clear current line */
4391 rl_clear_visible_line ();
4392 fflush (rl_outstream
);
4395 rl_crlf (); /* move to a new line */
4397 v
= bind_variable ("READLINE_LINE", rl_line_buffer
, 0);
4399 VSETATTR (v
, att_exported
);
4401 i
= readline_get_char_offset (rl_point
);
4402 value
= inttostr (i
, ibuf
, sizeof (ibuf
));
4403 v
= bind_int_variable ("READLINE_POINT", value
, 0);
4405 VSETATTR (v
, att_exported
);
4407 i
= readline_get_char_offset (rl_mark
);
4408 value
= inttostr (i
, ibuf
, sizeof (ibuf
));
4409 v
= bind_int_variable ("READLINE_MARK", value
, 0);
4411 VSETATTR (v
, att_exported
);
4412 array_needs_making
= 1;
4414 save_parser_state (&ps
);
4415 rl_clear_signals ();
4416 r
= parse_and_execute (savestring (cmd
), "bash_execute_unix_command", SEVAL_NOHIST
);
4418 restore_parser_state (&ps
);
4420 v
= find_variable ("READLINE_LINE");
4421 maybe_make_readline_line (v
? value_cell (v
) : 0);
4423 v
= find_variable ("READLINE_POINT");
4424 if (v
&& legal_number (value_cell (v
), &mi
))
4425 readline_set_char_offset (mi
, &rl_point
);
4427 v
= find_variable ("READLINE_MARK");
4428 if (v
&& legal_number (value_cell (v
), &mi
))
4429 readline_set_char_offset (mi
, &rl_mark
);
4431 check_unbind_variable ("READLINE_LINE");
4432 check_unbind_variable ("READLINE_POINT");
4433 check_unbind_variable ("READLINE_MARK");
4434 array_needs_making
= 1;
4436 /* and restore the readline buffer and display after command execution. */
4437 /* If we clear the last line of the prompt above, redraw only that last
4438 line. If the command returns 124, we redraw unconditionally as in
4439 previous versions. */
4441 rl_redraw_prompt_last_line ();
4443 rl_forced_update_display ();
4449 print_unix_command_map ()
4451 Keymap save
, cmd_xmap
;
4453 save
= rl_get_keymap ();
4454 cmd_xmap
= get_cmd_xmap_from_keymap (save
);
4455 rl_set_keymap (cmd_xmap
);
4456 rl_macro_dumper (1);
4457 rl_set_keymap (save
);
4462 init_unix_command_map ()
4464 emacs_std_cmd_xmap
= rl_make_bare_keymap ();
4466 emacs_std_cmd_xmap
[CTRL('X')].type
= ISKMAP
;
4467 emacs_std_cmd_xmap
[CTRL('X')].function
= KEYMAP_TO_FUNCTION (rl_make_bare_keymap ());
4468 emacs_std_cmd_xmap
[ESC
].type
= ISKMAP
;
4469 emacs_std_cmd_xmap
[ESC
].function
= KEYMAP_TO_FUNCTION (rl_make_bare_keymap ());
4471 #if defined (VI_MODE)
4472 vi_insert_cmd_xmap
= rl_make_bare_keymap ();
4473 vi_movement_cmd_xmap
= rl_make_bare_keymap ();
4478 get_cmd_xmap_from_edit_mode ()
4480 if (emacs_std_cmd_xmap
== 0)
4481 init_unix_command_map ();
4483 switch (rl_editing_mode
)
4485 case EMACS_EDITING_MODE
:
4486 return emacs_std_cmd_xmap
;
4487 #if defined (VI_MODE)
4488 case VI_EDITING_MODE
:
4489 return (get_cmd_xmap_from_keymap (rl_get_keymap ()));
4492 return (Keymap
)NULL
;
4497 get_cmd_xmap_from_keymap (kmap
)
4500 if (emacs_std_cmd_xmap
== 0)
4501 init_unix_command_map ();
4503 if (kmap
== emacs_standard_keymap
)
4504 return emacs_std_cmd_xmap
;
4505 else if (kmap
== emacs_meta_keymap
)
4506 return (FUNCTION_TO_KEYMAP (emacs_std_cmd_xmap
, ESC
));
4507 else if (kmap
== emacs_ctlx_keymap
)
4508 return (FUNCTION_TO_KEYMAP (emacs_std_cmd_xmap
, CTRL('X')));
4509 #if defined (VI_MODE)
4510 else if (kmap
== vi_insertion_keymap
)
4511 return vi_insert_cmd_xmap
;
4512 else if (kmap
== vi_movement_keymap
)
4513 return vi_movement_cmd_xmap
;
4516 return (Keymap
)NULL
;
4520 isolate_sequence (string
, ind
, need_dquote
, startp
)
4522 int ind
, need_dquote
, *startp
;
4525 int c
, passc
, delim
;
4527 for (i
= ind
; string
[i
] && whitespace (string
[i
]); i
++)
4529 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
4530 if (need_dquote
&& string
[i
] != '"')
4532 builtin_error (_("%s: first non-whitespace character is not `\"'"), string
);
4536 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
4537 string to bind the key sequence to. */
4538 delim
= (string
[i
] == '"' || string
[i
] == '\'') ? string
[i
] : 0;
4541 *startp
= delim
? ++i
: i
;
4543 for (passc
= 0; c
= string
[i
]; i
++)
4559 if (delim
&& string
[i
] != delim
)
4561 builtin_error (_("no closing `%c' in %s"), delim
, string
);
4569 bind_keyseq_to_unix_command (line
)
4572 Keymap kmap
, cmd_xmap
;
4576 kmap
= rl_get_keymap ();
4578 /* We duplicate some of the work done by rl_parse_and_bind here, but
4579 this code only has to handle `"keyseq": ["]command["]' and can
4580 generate an error for anything else. */
4581 i
= isolate_sequence (line
, 0, 1, &kstart
);
4585 /* Create the key sequence string to pass to rl_generic_bind */
4586 kseq
= substring (line
, kstart
, i
);
4588 for ( ; line
[i
] && line
[i
] != ':'; i
++)
4592 builtin_error (_("%s: missing colon separator"), line
);
4597 i
= isolate_sequence (line
, i
+ 1, 0, &kstart
);
4604 /* Create the value string containing the command to execute. */
4605 value
= substring (line
, kstart
, i
);
4607 /* Save the command to execute and the key sequence in the CMD_XMAP */
4608 cmd_xmap
= get_cmd_xmap_from_keymap (kmap
);
4609 rl_generic_bind (ISMACR
, kseq
, value
, cmd_xmap
);
4611 /* and bind the key sequence in the current keymap to a function that
4612 understands how to execute from CMD_XMAP */
4613 rl_bind_keyseq_in_map (kseq
, bash_execute_unix_command
, kmap
);
4620 unbind_unix_command (kseq
)
4625 cmd_xmap
= get_cmd_xmap_from_keymap (rl_get_keymap ());
4626 if (rl_bind_keyseq_in_map (kseq
, (rl_command_func_t
*)NULL
, cmd_xmap
) != 0)
4628 builtin_error (_("`%s': cannot unbind in command keymap"), kseq
);
4634 /* Used by the programmable completion code. Complete TEXT as a filename,
4635 but return only directories as matches. Dequotes the filename before
4636 attempting to find matches. */
4638 bash_directory_completion_matches (text
)
4645 qc
= rl_dispatching
? rl_completion_quote_character
: 0;
4646 /* If rl_completion_found_quote != 0, rl_completion_matches will call the
4647 filename dequoting function, causing the directory name to be dequoted
4649 if (rl_dispatching
&& rl_completion_found_quote
== 0)
4650 dfn
= bash_dequote_filename ((char *)text
, qc
);
4653 m1
= rl_completion_matches (dfn
, rl_filename_completion_function
);
4657 if (m1
== 0 || m1
[0] == 0)
4659 /* We don't bother recomputing the lcd of the matches, because it will just
4660 get thrown away by the programmable completion code and recomputed
4662 (void)bash_progcomp_ignore_filenames (m1
);
4667 bash_dequote_text (text
)
4673 qc
= (text
[0] == '"' || text
[0] == '\'') ? text
[0] : 0;
4674 dtxt
= bash_dequote_filename ((char *)text
, qc
);
4678 /* This event hook is designed to be called after readline receives a signal
4679 that interrupts read(2). It gives reasonable responsiveness to interrupts
4680 and fatal signals without executing too much code in a signal handler
4687 /* XXX - see if we need to do anything here if sigterm_received == 1,
4688 we probably don't want to reset the event hook since we will not be
4689 jumping to the top level */
4690 if (sigterm_received
)
4692 /* RESET_SIGTERM; */
4697 if (terminating_signal
)
4698 sig
= terminating_signal
;
4699 else if (interrupt_state
)
4701 else if (read_timeout
&& read_timeout
->alrmflag
)
4703 else if (RL_ISSTATE (RL_STATE_TIMEOUT
)) /* just in case */
4707 read_timeout
->alrmflag
= 1;
4710 sig
= first_pending_trap ();
4712 /* If we're going to longjmp to top_level, make sure we clean up readline.
4713 check_signals will call QUIT, which will eventually longjmp to top_level,
4714 calling run_interrupt_trap along the way. The check against read_timeout
4715 is so we can clean up the read builtin's state. */
4716 if (terminating_signal
|| interrupt_state
|| (read_timeout
&& read_timeout
->alrmflag
))
4717 rl_cleanup_after_signal ();
4718 bashline_reset_event_hook ();
4720 RL_UNSETSTATE (RL_STATE_TIMEOUT
); /* XXX */
4722 /* posix mode SIGINT during read -e. We only get here if SIGINT is trapped. */
4723 if (posixly_correct
&& this_shell_builtin
== read_builtin
&& sig
== SIGINT
)
4725 last_command_exit_value
= 128|SIGINT
;
4726 throw_to_top_level ();
4729 check_signals_and_traps (); /* XXX */
4733 #endif /* READLINE */