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 ((char *)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
);
1995 old_glob_ignore_case
= glob_ignore_case
;
1999 free (glob_matches
);
2000 glob_matches
= (char **)NULL
;
2003 globpat
= completion_glob_pattern ((char *)hint_text
);
2005 /* If this is an absolute program name, do not check it against
2006 aliases, reserved words, functions or builtins. We must check
2007 whether or not it is unique, and, if so, whether that filename
2009 if (globpat
|| absolute_program (hint_text
))
2011 /* Perform tilde expansion on what's passed, so we don't end up
2012 passing filenames with tildes directly to stat(). The rest of
2013 the shell doesn't do variable expansion on the word following
2014 the tilde, so we don't do it here even if direxpand is set. */
2015 if (*hint_text
== '~')
2017 hint
= bash_tilde_expand (hint_text
, 0);
2018 directory_part
= savestring (hint_text
);
2019 temp
= strchr (directory_part
, '/');
2024 free (directory_part
);
2025 directory_part
= (char *)NULL
;
2028 else if (dircomplete_expand
)
2030 hint
= savestring (hint_text
);
2031 bash_directory_completion_hook (&hint
);
2034 hint
= savestring (hint_text
);
2036 dequoted_hint
= hint
;
2037 /* If readline's completer found a quote character somewhere, but
2038 didn't set the quote character, there must have been a quote
2039 character embedded in the filename. It can't be at the start of
2040 the filename, so we need to dequote the filename before we look
2041 in the file system for it. */
2042 if (rl_completion_found_quote
&& rl_completion_quote_character
== 0)
2044 dequoted_hint
= bash_dequote_filename (hint
, 0);
2046 hint
= dequoted_hint
;
2048 hint_len
= strlen (hint
);
2051 free (filename_hint
);
2053 fnhint
= filename_hint
= savestring (hint
);
2064 if (dircomplete_expand
&& path_dot_or_dotdot (filename_hint
))
2066 dircomplete_expand
= 0;
2067 set_directory_hook ();
2068 dircomplete_expand
= 1;
2075 dequoted_hint
= hint
= savestring (hint_text
);
2076 hint_len
= strlen (hint
);
2078 if (rl_completion_found_quote
&& rl_completion_quote_character
== 0)
2079 dequoted_hint
= bash_dequote_filename (hint
, 0);
2081 path
= get_string_value ("PATH");
2082 path_index
= dot_in_path
= 0;
2084 /* Initialize the variables for each type of command word. */
2090 varlist
= all_visible_functions ();
2096 alias_list
= all_aliases ();
2100 /* mapping_over says what we are currently hacking. Note that every case
2101 in this list must fall through when there are no more possibilities. */
2103 switch (mapping_over
)
2105 case 0: /* Aliases come first. */
2107 while (alias_list
&& alias_list
[local_index
])
2109 register char *alias
;
2111 alias
= alias_list
[local_index
++]->name
;
2113 if (igncase
== 0 && (STREQN (alias
, hint
, hint_len
)))
2114 return (savestring (alias
));
2115 else if (igncase
&& strncasecmp (alias
, hint
, hint_len
) == 0)
2116 return (savestring (alias
));
2122 case 1: /* Then shell reserved words. */
2124 while (word_token_alist
[local_index
].word
)
2126 register char *reserved_word
;
2128 reserved_word
= word_token_alist
[local_index
++].word
;
2130 if (STREQN (reserved_word
, hint
, hint_len
))
2131 return (savestring (reserved_word
));
2137 case 2: /* Then function names. */
2138 while (varlist
&& varlist
[local_index
])
2140 register char *varname
;
2142 varname
= varlist
[local_index
++]->name
;
2144 /* Honor completion-ignore-case for shell function names. */
2145 if (igncase
== 0 && (STREQN (varname
, hint
, hint_len
)))
2146 return (savestring (varname
));
2147 else if (igncase
&& strncasecmp (varname
, hint
, hint_len
) == 0)
2148 return (savestring (varname
));
2153 case 3: /* Then shell builtins. */
2154 for (; local_index
< num_shell_builtins
; local_index
++)
2156 /* Ignore it if it doesn't have a function pointer or if it
2157 is not currently enabled. */
2158 if (!shell_builtins
[local_index
].function
||
2159 (shell_builtins
[local_index
].flags
& BUILTIN_ENABLED
) == 0)
2162 if (STREQN (shell_builtins
[local_index
].name
, hint
, hint_len
))
2164 int i
= local_index
++;
2166 return (savestring (shell_builtins
[i
].name
));
2174 /* Limited support for completing command words with globbing chars. Only
2175 a single match (multiple matches that end up reducing the number of
2176 characters in the common prefix are bad) will ever be returned on
2177 regular completion. */
2182 rl_filename_completion_desired
= 1;
2184 glob_ignore_case
= igncase
;
2185 glob_matches
= shell_glob_filename (hint
, 0);
2186 glob_ignore_case
= old_glob_ignore_case
;
2188 if (GLOB_FAILED (glob_matches
) || glob_matches
== 0)
2190 glob_matches
= (char **)NULL
;
2191 return ((char *)NULL
);
2196 if (glob_matches
[1] && rl_completion_type
== TAB
) /* multiple matches are bad */
2197 return ((char *)NULL
);
2200 while (val
= glob_matches
[local_index
++])
2202 if (executable_or_directory (val
))
2204 if (*hint_text
== '~' && directory_part
)
2206 temp
= maybe_restore_tilde (val
, directory_part
);
2215 glob_ignore_case
= old_glob_ignore_case
;
2216 return ((char *)NULL
);
2219 /* If the text passed is a directory in the current directory, return it
2220 as a possible match. Executables in directories in the current
2221 directory can be specified using relative pathnames and successfully
2222 executed even when `.' is not in $PATH. */
2225 hint_is_dir
= 0; /* only return the hint text once */
2226 return (savestring (hint_text
));
2229 /* Repeatedly call filename_completion_function while we have
2230 members of PATH left. Question: should we stat each file?
2231 Answer: we call executable_file () on each file. */
2234 istate
= (val
!= (char *)NULL
);
2240 /* Get the next directory from the path. If there is none, then we
2242 if (path
== 0 || path
[path_index
] == 0 ||
2243 (current_path
= extract_colon_unit (path
, &path_index
)) == 0)
2244 return ((char *)NULL
);
2247 if (*current_path
== 0)
2249 free (current_path
);
2250 current_path
= savestring (".");
2253 if (*current_path
== '~')
2257 t
= bash_tilde_expand (current_path
, 0);
2258 free (current_path
);
2262 if (current_path
[0] == '.' && current_path
[1] == '\0')
2265 if (fnhint
&& fnhint
!= filename_hint
)
2268 free (filename_hint
);
2270 filename_hint
= sh_makepath (current_path
, hint
, 0);
2271 /* Need a quoted version (though it doesn't matter much in most
2272 cases) because rl_filename_completion_function dequotes the
2273 filename it gets, assuming that it's been quoted as part of
2274 the input line buffer. */
2275 if (strpbrk (filename_hint
, "\"'\\"))
2276 fnhint
= sh_backslash_quote (filename_hint
, filename_bstab
, 0);
2278 fnhint
= filename_hint
;
2279 free (current_path
); /* XXX */
2283 val
= rl_filename_completion_function (fnhint
, istate
);
2284 if (mapping_over
== 4 && dircomplete_expand
)
2285 set_directory_hook ();
2291 /* If the hint text is an absolute program, then don't bother
2292 searching through PATH. */
2293 if (absolute_program (hint
))
2294 return ((char *)NULL
);
2300 int match
, freetemp
;
2302 if (absolute_program (hint
))
2305 match
= strncmp (val
, hint
, hint_len
) == 0;
2307 match
= strncasecmp (val
, hint
, hint_len
) == 0;
2309 /* If we performed tilde expansion, restore the original
2311 if (*hint_text
== '~')
2312 temp
= maybe_restore_tilde (val
, directory_part
);
2314 temp
= savestring (val
);
2319 temp
= strrchr (val
, '/');
2325 freetemp
= match
= strncmp (temp
, hint
, hint_len
) == 0;
2327 freetemp
= match
= strncasecmp (temp
, hint
, hint_len
) == 0;
2329 temp
= savestring (temp
);
2332 freetemp
= match
= 0;
2335 /* If we have found a match, and it is an executable file, return it.
2336 We don't return directory names when searching $PATH, since the
2337 bash execution code won't find executables in directories which
2338 appear in directories in $PATH when they're specified using
2339 relative pathnames. */
2341 /* If we're not searching $PATH and we have a relative pathname, we
2342 need to re-canonicalize it before testing whether or not it's an
2343 executable or a directory so the shell treats .. relative to $PWD
2344 according to the physical/logical option. The shell already
2345 canonicalizes the directory name in order to tell readline where
2346 to look, so not doing it here will be inconsistent. */
2347 /* XXX -- currently not used -- will introduce more inconsistency,
2348 since shell does not canonicalize ../foo before passing it to
2350 if (match
&& searching_path
== 0 && *val
== '.')
2354 t
= get_working_directory ("command-word-completion");
2355 t1
= make_absolute (val
, t
);
2357 cval
= sh_canonpath (t1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
2363 if (match
&& executable_completion ((searching_path
? val
: cval
), searching_path
))
2368 val
= ""; /* So it won't be NULL. */
2383 /* Completion inside an unterminated command substitution. */
2385 command_subst_completion_function (text
, state
)
2389 static char **matches
= (char **)NULL
;
2390 static const char *orig_start
;
2391 static char *filename_text
= (char *)NULL
;
2392 static int cmd_index
, start_len
;
2398 free (filename_text
);
2402 else if (*text
== '$' && text
[1] == '(') /* ) */
2404 /* If the text was quoted, suppress any quote character that the
2405 readline completion code would insert. */
2406 rl_completion_suppress_quote
= 1;
2407 start_len
= text
- orig_start
;
2408 filename_text
= savestring (text
);
2413 * At this point we can entertain the idea of re-parsing
2414 * `filename_text' into a (possibly incomplete) command name and
2415 * arguments, and doing completion based on that. This is
2416 * currently very rudimentary, but it is a small improvement.
2418 for (value
= filename_text
+ strlen (filename_text
) - 1; value
> filename_text
; value
--)
2419 if (whitespace (*value
) || member (*value
, COMMAND_SEPARATORS
))
2421 if (value
<= filename_text
)
2422 matches
= rl_completion_matches (filename_text
, command_word_completion_function
);
2426 start_len
+= value
- filename_text
;
2427 if (whitespace (value
[-1]))
2428 matches
= rl_completion_matches (value
, rl_filename_completion_function
);
2430 matches
= rl_completion_matches (value
, command_word_completion_function
);
2433 /* If there is more than one match, rl_completion_matches has already
2434 put the lcd in matches[0]. Skip over it. */
2435 cmd_index
= matches
&& matches
[0] && matches
[1];
2437 /* If there's a single match and it's a directory, set the append char
2438 to the expected `/'. Otherwise, don't append anything. */
2439 if (matches
&& matches
[0] && matches
[1] == 0 && test_for_directory (matches
[0]))
2440 rl_completion_append_character
= '/';
2442 rl_completion_suppress_append
= 1;
2445 if (matches
== 0 || matches
[cmd_index
] == 0)
2447 rl_filename_quoting_desired
= 0; /* disable quoting */
2448 return ((char *)NULL
);
2452 value
= (char *)xmalloc (1 + start_len
+ strlen (matches
[cmd_index
]));
2455 value
[0] = *orig_start
;
2457 strncpy (value
, orig_start
, start_len
);
2459 strcpy (value
+ start_len
, matches
[cmd_index
]);
2466 /* Okay, now we write the entry_function for variable completion. */
2468 variable_completion_function (text
, state
)
2472 static char **varlist
= (char **)NULL
;
2473 static int varlist_index
;
2474 static char *varname
= (char *)NULL
;
2475 static int first_char
, first_char_loc
;
2483 first_char
= text
[0];
2485 if (first_char
== '$')
2488 if (text
[first_char_loc
] == '{')
2491 varname
= savestring (text
+ first_char_loc
);
2494 strvec_dispose (varlist
);
2496 varlist
= all_variables_matching_prefix (varname
);
2500 if (!varlist
|| !varlist
[varlist_index
])
2502 return ((char *)NULL
);
2508 value
= (char *)xmalloc (4 + strlen (varlist
[varlist_index
]));
2512 value
[0] = first_char
;
2513 if (first_char_loc
== 2)
2517 strcpy (value
+ first_char_loc
, varlist
[varlist_index
]);
2518 if (first_char_loc
== 2)
2519 strcat (value
, "}");
2526 /* How about a completion function for hostnames? */
2528 hostname_completion_function (text
, state
)
2532 static char **list
= (char **)NULL
;
2533 static int list_index
= 0;
2534 static int first_char
, first_char_loc
;
2536 /* If we don't have any state, make some. */
2541 list
= (char **)NULL
;
2546 if (first_char
== '@')
2549 list
= hostnames_matching ((char *)text
+first_char_loc
);
2553 if (list
&& list
[list_index
])
2557 t
= (char *)xmalloc (2 + strlen (list
[list_index
]));
2559 strcpy (t
+ first_char_loc
, list
[list_index
]);
2564 return ((char *)NULL
);
2568 * A completion function for service names from /etc/services (or wherever).
2571 bash_servicename_completion_function (text
, state
)
2575 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT)
2576 return ((char *)NULL
);
2578 static char *sname
= (char *)NULL
;
2579 static struct servent
*srvent
;
2580 static int snamelen
;
2582 char **alist
, *aentry
;
2589 sname
= savestring (text
);
2590 snamelen
= strlen (sname
);
2594 while (srvent
= getservent ())
2597 if (snamelen
== 0 || (STREQN (sname
, srvent
->s_name
, snamelen
)))
2599 /* Not primary, check aliases */
2600 for (alist
= srvent
->s_aliases
; *alist
; alist
++)
2603 if (STREQN (sname
, aentry
, snamelen
))
2617 return ((char *)NULL
);
2620 value
= afound
? savestring (aentry
) : savestring (srvent
->s_name
);
2626 * A completion function for group names from /etc/group (or wherever).
2629 bash_groupname_completion_function (text
, state
)
2633 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
2634 return ((char *)NULL
);
2636 static char *gname
= (char *)NULL
;
2637 static struct group
*grent
;
2638 static int gnamelen
;
2644 gname
= savestring (text
);
2645 gnamelen
= strlen (gname
);
2650 while (grent
= getgrent ())
2652 if (gnamelen
== 0 || (STREQN (gname
, grent
->gr_name
, gnamelen
)))
2659 return ((char *)NULL
);
2662 value
= savestring (grent
->gr_name
);
2667 /* Functions to perform history and alias expansions on the current line. */
2669 #if defined (BANG_HISTORY)
2670 /* Perform history expansion on the current line. If no history expansion
2671 is done, pre_process_line() returns what it was passed, so we need to
2672 allocate a new line here. */
2674 history_expand_line_internal (line
)
2680 old_verify
= hist_verify
;
2682 new_line
= pre_process_line (line
, 0, 0);
2683 hist_verify
= old_verify
;
2685 return (new_line
== line
) ? savestring (line
) : new_line
;
2689 /* There was an error in expansion. Let the preprocessor print
2692 cleanup_expansion_error ()
2695 #if defined (BANG_HISTORY)
2698 old_verify
= hist_verify
;
2702 fprintf (rl_outstream
, "\r\n");
2703 to_free
= pre_process_line (rl_line_buffer
, 1, 0);
2704 #if defined (BANG_HISTORY)
2705 hist_verify
= old_verify
;
2707 if (to_free
!= rl_line_buffer
)
2709 putc ('\r', rl_outstream
);
2710 rl_forced_update_display ();
2713 /* If NEW_LINE differs from what is in the readline line buffer, add an
2714 undo record to get from the readline line buffer contents to the new
2715 line and make NEW_LINE the current readline line. */
2717 maybe_make_readline_line (new_line
)
2720 if (new_line
&& strcmp (new_line
, rl_line_buffer
) != 0)
2724 rl_add_undo (UNDO_BEGIN
, 0, 0, 0);
2725 rl_delete_text (0, rl_point
);
2726 rl_point
= rl_end
= rl_mark
= 0;
2727 rl_insert_text (new_line
);
2728 rl_add_undo (UNDO_END
, 0, 0, 0);
2732 /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
2734 set_up_new_line (new_line
)
2737 int old_point
, at_end
;
2739 old_point
= rl_point
;
2740 at_end
= rl_point
== rl_end
;
2742 /* If the line was history and alias expanded, then make that
2743 be one thing to undo. */
2744 maybe_make_readline_line (new_line
);
2747 /* Place rl_point where we think it should go. */
2750 else if (old_point
< rl_end
)
2752 rl_point
= old_point
;
2753 if (!whitespace (rl_line_buffer
[rl_point
]))
2754 rl_forward_word (1, 0);
2759 /* Expand aliases in the current readline line. */
2761 alias_expand_line (count
, ignore
)
2766 new_line
= alias_expand (rl_line_buffer
);
2770 set_up_new_line (new_line
);
2775 cleanup_expansion_error ();
2781 #if defined (BANG_HISTORY)
2782 /* History expand the line. */
2784 history_expand_line (count
, ignore
)
2789 new_line
= history_expand_line_internal (rl_line_buffer
);
2793 set_up_new_line (new_line
);
2798 cleanup_expansion_error ();
2803 /* Expand history substitutions in the current line and then insert a
2804 space (hopefully close to where we were before). */
2806 tcsh_magic_space (count
, ignore
)
2809 int dist_from_end
, old_point
;
2811 old_point
= rl_point
;
2812 dist_from_end
= rl_end
- rl_point
;
2813 if (history_expand_line (count
, ignore
) == 0)
2815 /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
2816 This works if all expansions were before rl_point or if no expansions
2818 rl_point
= (old_point
== 0) ? old_point
: rl_end
- dist_from_end
;
2825 #endif /* BANG_HISTORY */
2827 /* History and alias expand the line. */
2829 history_and_alias_expand_line (count
, ignore
)
2835 #if defined (BANG_HISTORY)
2836 new_line
= history_expand_line_internal (rl_line_buffer
);
2839 t
= expand_string_dollar_quote (new_line
? new_line
: rl_line_buffer
, 0);
2848 alias_line
= alias_expand (new_line
);
2850 new_line
= alias_line
;
2856 set_up_new_line (new_line
);
2861 cleanup_expansion_error ();
2866 /* History and alias expand the line, then perform the shell word
2867 expansions by calling expand_string. This can't use set_up_new_line()
2868 because we want the variable expansions as a separate undo'able
2869 set of operations. */
2871 shell_expand_line (count
, ignore
)
2875 WORD_LIST
*expanded_string
;
2879 #if defined (BANG_HISTORY)
2880 new_line
= history_expand_line_internal (rl_line_buffer
);
2883 t
= expand_string_dollar_quote (new_line
? new_line
: rl_line_buffer
, 0);
2892 alias_line
= alias_expand (new_line
);
2894 new_line
= alias_line
;
2900 int old_point
= rl_point
;
2901 int at_end
= rl_point
== rl_end
;
2903 /* If the line was history and alias expanded, then make that
2904 be one thing to undo. */
2905 maybe_make_readline_line (new_line
);
2908 /* If there is variable expansion to perform, do that as a separate
2909 operation to be undone. */
2912 w
= alloc_word_desc ();
2913 w
->word
= savestring (rl_line_buffer
);
2914 w
->flags
= rl_explicit_arg
? (W_NOPROCSUB
|W_NOCOMSUB
) : 0;
2915 expanded_string
= expand_word (w
, rl_explicit_arg
? Q_HERE_DOCUMENT
: 0);
2918 new_line
= savestring (rl_line_buffer
);
2919 expanded_string
= expand_string (new_line
, 0);
2923 if (expanded_string
== 0)
2925 new_line
= (char *)xmalloc (1);
2930 new_line
= string_list (expanded_string
);
2931 dispose_words (expanded_string
);
2934 maybe_make_readline_line (new_line
);
2937 /* Place rl_point where we think it should go. */
2940 else if (old_point
< rl_end
)
2942 rl_point
= old_point
;
2943 if (!whitespace (rl_line_buffer
[rl_point
]))
2944 rl_forward_word (1, 0);
2950 cleanup_expansion_error ();
2955 /* If FIGNORE is set, then don't match files with the given suffixes when
2956 completing filenames. If only one of the possibilities has an acceptable
2957 suffix, delete the others, else just return and let the completer
2958 signal an error. It is called by the completer when real
2959 completions are done on filenames by the completer's internal
2960 function, not for completion lists (M-?) and not on "other"
2961 completion types, such as hostnames or commands. */
2963 static struct ignorevar fignore
=
2969 (sh_iv_item_func_t
*) 0,
2973 _ignore_completion_names (names
, name_func
)
2975 sh_ignore_func_t
*name_func
;
2982 /* If there is only one completion, see if it is acceptable. If it is
2983 not, free it up. In any case, short-circuit and return. This is a
2984 special case because names[0] is not the prefix of the list of names
2985 if there is only one completion; it is the completion itself. */
2986 if (names
[1] == (char *)0)
2989 if ((*name_func
) (names
[0]) == 0)
2992 names
[0] = (char *)NULL
;
2998 /* Allocate space for array to hold list of pointers to matching
2999 filenames. The pointers are copied back to NAMES when done. */
3000 for (nidx
= 1; names
[nidx
]; nidx
++)
3002 newnames
= strvec_create (nidx
+ 1);
3004 if (force_fignore
== 0)
3006 oldnames
= strvec_create (nidx
- 1);
3010 newnames
[0] = names
[0];
3011 for (idx
= nidx
= 1; names
[idx
]; idx
++)
3013 if ((*name_func
) (names
[idx
]))
3014 newnames
[nidx
++] = names
[idx
];
3015 else if (force_fignore
== 0)
3016 oldnames
[oidx
++] = names
[idx
];
3021 newnames
[nidx
] = (char *)NULL
;
3023 /* If none are acceptable then let the completer handle it. */
3029 names
[0] = (char *)NULL
;
3038 if (force_fignore
== 0)
3041 free (oldnames
[--oidx
]);
3045 /* If only one is acceptable, copy it to names[0] and return. */
3049 names
[0] = newnames
[1];
3050 names
[1] = (char *)NULL
;
3055 /* Copy the acceptable names back to NAMES, set the new array end,
3057 for (nidx
= 1; newnames
[nidx
]; nidx
++)
3058 names
[nidx
] = newnames
[nidx
];
3059 names
[nidx
] = (char *)NULL
;
3064 name_is_acceptable (name
)
3070 for (nlen
= strlen (name
), p
= fignore
.ignores
; p
->val
; p
++)
3072 if (nlen
> p
->len
&& p
->len
> 0 && STREQ (p
->val
, &name
[nlen
- p
->len
]))
3081 ignore_dot_names (name
)
3084 return (name
[0] != '.');
3089 filename_completion_ignore (names
)
3093 if (glob_dot_filenames
== 0)
3094 _ignore_completion_names (names
, ignore_dot_names
);
3097 setup_ignore_patterns (&fignore
);
3099 if (fignore
.num_ignores
== 0)
3102 _ignore_completion_names (names
, name_is_acceptable
);
3107 /* Return 1 if NAME is a directory. NAME undergoes tilde expansion. */
3109 test_for_directory (name
)
3115 fn
= bash_tilde_expand (name
, 0);
3116 r
= file_isdir (fn
);
3123 test_for_canon_directory (name
)
3129 fn
= (*name
== '~') ? bash_tilde_expand (name
, 0) : savestring (name
);
3130 bash_filename_stat_hook (&fn
);
3131 r
= file_isdir (fn
);
3137 /* Remove files from NAMES, leaving directories. */
3139 bash_ignore_filenames (names
)
3142 _ignore_completion_names (names
, test_for_directory
);
3147 bash_progcomp_ignore_filenames (names
)
3150 _ignore_completion_names (names
, test_for_canon_directory
);
3162 bash_ignore_everything (names
)
3165 _ignore_completion_names (names
, return_zero
);
3169 /* Replace a tilde-prefix in VAL with a `~', assuming the user typed it. VAL
3170 is an expanded filename. DIRECTORY_PART is the tilde-prefix portion
3171 of the un-tilde-expanded version of VAL (what the user typed). */
3173 restore_tilde (val
, directory_part
)
3174 char *val
, *directory_part
;
3177 char *dh2
, *expdir
, *ret
, *v
;
3181 /* We need to duplicate the expansions readline performs on the directory
3182 portion before passing it to our completion function. */
3183 dh2
= directory_part
? bash_dequote_filename (directory_part
, 0) : 0;
3184 bash_directory_expansion (&dh2
);
3187 expdir
= bash_tilde_expand (directory_part
, 0);
3188 xl
= strlen (expdir
);
3189 if (*directory_part
== '~' && STREQ (directory_part
, expdir
))
3191 /* tilde expansion failed, so what should we return? we use what the
3193 v
= mbschr (val
, '/');
3195 ret
= (char *)xmalloc (xl
+ vl
+ 2);
3196 strcpy (ret
, directory_part
);
3198 strcpy (ret
+ xl
, v
);
3208 dh2 = unexpanded but dequoted tilde-prefix
3209 dl2 = length of tilde-prefix
3210 expdir = tilde-expanded tilde-prefix
3211 xl = length of expanded tilde-prefix
3212 l = length of remainder after tilde-prefix
3218 return (savestring (val
)); /* XXX - just punt */
3221 ret
= (char *)xmalloc (dl2
+ 2 + l
);
3223 strcpy (ret
+ dl2
, val
+ xl
);
3230 maybe_restore_tilde (val
, directory_part
)
3231 char *val
, *directory_part
;
3233 rl_icppfunc_t
*save
;
3236 save
= (dircomplete_expand
== 0) ? save_directory_hook () : (rl_icppfunc_t
*)0;
3237 ret
= restore_tilde (val
, directory_part
);
3239 restore_directory_hook (save
);
3243 /* Simulate the expansions that will be performed by
3244 rl_filename_completion_function. This must be called with the address of
3245 a pointer to malloc'd memory. */
3247 bash_directory_expansion (dirname
)
3252 d
= savestring (*dirname
);
3254 if ((rl_directory_rewrite_hook
) && (*rl_directory_rewrite_hook
) (&d
))
3259 else if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&d
))
3264 else if (rl_completion_found_quote
)
3266 nd
= bash_dequote_filename (d
, rl_completion_quote_character
);
3273 /* If necessary, rewrite directory entry */
3275 bash_filename_rewrite_hook (fname
, fnlen
)
3281 conv
= fnx_fromfs (fname
, fnlen
);
3283 conv
= savestring (conv
);
3287 /* Functions to save and restore the appropriate directory hook */
3288 /* This is not static so the shopt code can call it */
3290 set_directory_hook ()
3292 if (dircomplete_expand
)
3294 rl_directory_completion_hook
= bash_directory_completion_hook
;
3295 rl_directory_rewrite_hook
= (rl_icppfunc_t
*)0;
3299 rl_directory_rewrite_hook
= bash_directory_completion_hook
;
3300 rl_directory_completion_hook
= (rl_icppfunc_t
*)0;
3304 static rl_icppfunc_t
*
3305 save_directory_hook ()
3309 if (dircomplete_expand
)
3311 ret
= rl_directory_completion_hook
;
3312 rl_directory_completion_hook
= (rl_icppfunc_t
*)NULL
;
3316 ret
= rl_directory_rewrite_hook
;
3317 rl_directory_rewrite_hook
= (rl_icppfunc_t
*)NULL
;
3324 restore_directory_hook (hookf
)
3325 rl_icppfunc_t
*hookf
;
3327 if (dircomplete_expand
)
3328 rl_directory_completion_hook
= hookf
;
3330 rl_directory_rewrite_hook
= hookf
;
3333 /* Check whether not DIRNAME, with any trailing slash removed, exists. If
3334 SHOULD_DEQUOTE is non-zero, we dequote the directory name first. */
3336 directory_exists (dirname
, should_dequote
)
3337 const char *dirname
;
3344 /* We save the string and chop the trailing slash because stat/lstat behave
3345 inconsistently if one is present. */
3346 new_dirname
= should_dequote
? bash_dequote_filename ((char *)dirname
, rl_completion_quote_character
) : savestring (dirname
);
3347 dirlen
= STRLEN (new_dirname
);
3348 if (new_dirname
[dirlen
- 1] == '/')
3349 new_dirname
[dirlen
- 1] = '\0';
3350 #if defined (HAVE_LSTAT)
3351 r
= lstat (new_dirname
, &sb
) == 0;
3353 r
= stat (new_dirname
, &sb
) == 0;
3359 /* Expand a filename before the readline completion code passes it to stat(2).
3360 The filename will already have had tilde expansion performed. */
3362 bash_filename_stat_hook (dirname
)
3365 char *local_dirname
, *new_dirname
, *t
;
3366 int should_expand_dirname
, return_value
;
3370 local_dirname
= *dirname
;
3371 should_expand_dirname
= return_value
= 0;
3372 if (t
= mbschr (local_dirname
, '$'))
3373 should_expand_dirname
= '$';
3374 else if (t
= mbschr (local_dirname
, '`')) /* XXX */
3375 should_expand_dirname
= '`';
3377 if (should_expand_dirname
&& directory_exists (local_dirname
, 0))
3378 should_expand_dirname
= 0;
3380 if (should_expand_dirname
)
3382 new_dirname
= savestring (local_dirname
);
3383 /* no error messages, and expand_prompt_string doesn't longjmp so we don't
3384 have to worry about restoring this setting. */
3385 global_nounset
= unbound_vars_is_error
;
3386 unbound_vars_is_error
= 0;
3387 wl
= expand_prompt_string (new_dirname
, 0, W_NOCOMSUB
|W_NOPROCSUB
|W_COMPLETE
); /* does the right thing */
3388 unbound_vars_is_error
= global_nounset
;
3392 new_dirname
= string_list (wl
);
3393 /* Tell the completer we actually expanded something and change
3394 *dirname only if we expanded to something non-null -- stat
3395 behaves unpredictably when passed null or empty strings */
3396 if (new_dirname
&& *new_dirname
)
3398 free (local_dirname
); /* XXX */
3399 local_dirname
= *dirname
= new_dirname
;
3400 return_value
= STREQ (local_dirname
, *dirname
) == 0;
3410 /* This is very similar to the code in bash_directory_completion_hook below,
3411 but without spelling correction and not worrying about whether or not
3412 we change relative pathnames. */
3413 if (no_symbolic_links
== 0 && (local_dirname
[0] != '.' || local_dirname
[1]))
3415 char *temp1
, *temp2
;
3417 t
= get_working_directory ("symlink-hook");
3418 temp1
= make_absolute (local_dirname
, t
);
3420 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3422 /* If we can't canonicalize, bail. */
3426 return return_value
;
3429 free (local_dirname
);
3434 return (return_value
);
3437 /* Handle symbolic link references and other directory name
3438 expansions while hacking completion. This should return 1 if it modifies
3439 the DIRNAME argument, 0 otherwise. It should make sure not to modify
3440 DIRNAME if it returns 0. */
3442 bash_directory_completion_hook (dirname
)
3445 char *local_dirname
, *new_dirname
, *t
;
3446 int return_value
, should_expand_dirname
, nextch
, closer
;
3449 return_value
= should_expand_dirname
= nextch
= closer
= 0;
3450 local_dirname
= *dirname
;
3452 if (t
= mbschr (local_dirname
, '$'))
3454 should_expand_dirname
= '$';
3456 /* Deliberately does not handle the deprecated $[...] arithmetic
3460 else if (nextch
== '{')
3470 delims
[0] = closer
; delims
[1] = 0;
3471 p
= skip_to_delim (t
, 1, delims
, SD_NOJMP
|SD_COMPLETE
);
3473 should_expand_dirname
= 0;
3476 else if (local_dirname
[0] == '~')
3477 should_expand_dirname
= '~';
3480 t
= mbschr (local_dirname
, '`');
3481 if (t
&& unclosed_pair (local_dirname
, strlen (local_dirname
), "`") == 0)
3482 should_expand_dirname
= '`';
3485 if (should_expand_dirname
&& directory_exists (local_dirname
, 1))
3486 should_expand_dirname
= 0;
3488 if (should_expand_dirname
)
3490 new_dirname
= savestring (local_dirname
);
3491 wl
= expand_prompt_string (new_dirname
, 0, W_NOCOMSUB
|W_NOPROCSUB
|W_COMPLETE
); /* does the right thing */
3494 *dirname
= string_list (wl
);
3495 /* Tell the completer to replace the directory name only if we
3496 actually expanded something. */
3497 return_value
= STREQ (local_dirname
, *dirname
) == 0;
3498 free (local_dirname
);
3501 local_dirname
= *dirname
;
3502 /* XXX - change rl_filename_quote_characters here based on
3503 should_expand_dirname/nextch/closer. This is the only place
3504 custom_filename_quote_characters is modified. */
3505 if (rl_filename_quote_characters
&& *rl_filename_quote_characters
)
3508 i
= strlen (default_filename_quote_characters
);
3509 custom_filename_quote_characters
= xrealloc (custom_filename_quote_characters
, i
+1);
3510 for (i
= j
= 0; c
= default_filename_quote_characters
[i
]; i
++)
3512 if (c
== should_expand_dirname
|| c
== nextch
|| c
== closer
)
3514 custom_filename_quote_characters
[j
++] = c
;
3516 custom_filename_quote_characters
[j
] = '\0';
3517 rl_filename_quote_characters
= custom_filename_quote_characters
;
3518 set_filename_bstab (rl_filename_quote_characters
);
3524 free (local_dirname
);
3525 *dirname
= (char *)xmalloc (1);
3532 /* Dequote the filename even if we don't expand it. */
3533 new_dirname
= bash_dequote_filename (local_dirname
, rl_completion_quote_character
);
3534 return_value
= STREQ (local_dirname
, new_dirname
) == 0;
3535 free (local_dirname
);
3536 local_dirname
= *dirname
= new_dirname
;
3539 /* no_symbolic_links == 0 -> use (default) logical view of the file system.
3540 local_dirname[0] == '.' && local_dirname[1] == '/' means files in the
3541 current directory (./).
3542 local_dirname[0] == '.' && local_dirname[1] == 0 means relative pathnames
3543 in the current directory (e.g., lib/sh).
3544 XXX - should we do spelling correction on these? */
3546 /* This is test as it was in bash-4.2: skip relative pathnames in current
3547 directory. Change test to
3548 (local_dirname[0] != '.' || (local_dirname[1] && local_dirname[1] != '/'))
3549 if we want to skip paths beginning with ./ also. */
3550 if (no_symbolic_links
== 0 && (local_dirname
[0] != '.' || local_dirname
[1]))
3552 char *temp1
, *temp2
;
3555 /* If we have a relative path
3556 (local_dirname[0] != '/' && local_dirname[0] != '.')
3557 that is canonical after appending it to the current directory, then
3560 strcmp (temp1, temp2) == 0
3561 after adding a slash to temp2 below. It should be safe to not
3564 t
= get_working_directory ("symlink-hook");
3565 temp1
= make_absolute (local_dirname
, t
);
3567 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3569 /* Try spelling correction if initial canonicalization fails. Make
3570 sure we are set to replace the directory name with the results so
3571 subsequent directory checks don't fail. */
3572 if (temp2
== 0 && dircomplete_spelling
&& dircomplete_expand
)
3574 temp2
= dirspell (temp1
);
3579 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3580 return_value
|= temp2
!= 0;
3583 /* If we can't canonicalize, bail. */
3587 return return_value
;
3589 len1
= strlen (temp1
);
3590 if (temp1
[len1
- 1] == '/')
3592 len2
= strlen (temp2
);
3593 if (len2
> 2) /* don't append `/' to `/' or `//' */
3595 temp2
= (char *)xrealloc (temp2
, len2
+ 2);
3597 temp2
[len2
+ 1] = '\0';
3601 /* dircomplete_expand_relpath == 0 means we want to leave relative
3602 pathnames that are unchanged by canonicalization alone.
3603 *local_dirname != '/' && *local_dirname != '.' == relative pathname
3604 (consistent with general.c:absolute_pathname())
3605 temp1 == temp2 (after appending a slash to temp2) means the pathname
3606 is not changed by canonicalization as described above. */
3607 if (dircomplete_expand_relpath
|| ((local_dirname
[0] != '/' && local_dirname
[0] != '.') && STREQ (temp1
, temp2
) == 0))
3608 return_value
|= STREQ (local_dirname
, temp2
) == 0;
3609 free (local_dirname
);
3614 return (return_value
);
3617 static char **history_completion_array
= (char **)NULL
;
3618 static int harry_size
;
3619 static int harry_len
;
3622 build_history_completion_array ()
3628 /* First, clear out the current dynamic history completion list. */
3631 strvec_dispose (history_completion_array
);
3632 history_completion_array
= (char **)NULL
;
3637 /* Next, grovel each line of history, making each shell-sized token
3638 a separate entry in the history_completion_array. */
3639 hlist
= history_list ();
3643 for (i
= 0; hlist
[i
]; i
++)
3645 for ( --i
; i
>= 0; i
--)
3647 /* Separate each token, and place into an array. */
3648 tokens
= history_tokenize (hlist
[i
]->line
);
3650 for (j
= 0; tokens
&& tokens
[j
]; j
++)
3652 if (harry_len
+ 2 > harry_size
)
3653 history_completion_array
= strvec_resize (history_completion_array
, harry_size
+= 10);
3655 history_completion_array
[harry_len
++] = tokens
[j
];
3656 history_completion_array
[harry_len
] = (char *)NULL
;
3661 /* Sort the complete list of tokens. */
3662 if (dabbrev_expand_active
== 0)
3663 qsort (history_completion_array
, harry_len
, sizeof (char *), (QSFUNC
*)strvec_strcmp
);
3668 history_completion_generator (hint_text
, state
)
3669 const char *hint_text
;
3672 static int local_index
, len
;
3673 static const char *text
;
3675 /* If this is the first call to the generator, then initialize the
3676 list of strings to complete over. */
3679 if (dabbrev_expand_active
) /* This is kind of messy */
3680 rl_completion_suppress_append
= 1;
3682 build_history_completion_array ();
3684 len
= strlen (text
);
3687 while (history_completion_array
&& history_completion_array
[local_index
])
3689 /* XXX - should this use completion-ignore-case? */
3690 if (strncmp (text
, history_completion_array
[local_index
++], len
) == 0)
3691 return (savestring (history_completion_array
[local_index
- 1]));
3693 return ((char *)NULL
);
3697 dynamic_complete_history (count
, key
)
3701 rl_compentry_func_t
*orig_func
;
3702 rl_completion_func_t
*orig_attempt_func
;
3703 rl_compignore_func_t
*orig_ignore_func
;
3705 orig_func
= rl_completion_entry_function
;
3706 orig_attempt_func
= rl_attempted_completion_function
;
3707 orig_ignore_func
= rl_ignore_some_completions_function
;
3709 rl_completion_entry_function
= history_completion_generator
;
3710 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3711 rl_ignore_some_completions_function
= filename_completion_ignore
;
3713 /* XXX - use rl_completion_mode here? */
3714 if (rl_last_func
== dynamic_complete_history
)
3715 r
= rl_complete_internal ('?');
3717 r
= rl_complete_internal (TAB
);
3719 rl_completion_entry_function
= orig_func
;
3720 rl_attempted_completion_function
= orig_attempt_func
;
3721 rl_ignore_some_completions_function
= orig_ignore_func
;
3727 bash_dabbrev_expand (count
, key
)
3730 int r
, orig_suppress
, orig_sort
;
3731 rl_compentry_func_t
*orig_func
;
3732 rl_completion_func_t
*orig_attempt_func
;
3733 rl_compignore_func_t
*orig_ignore_func
;
3735 orig_func
= rl_menu_completion_entry_function
;
3736 orig_attempt_func
= rl_attempted_completion_function
;
3737 orig_ignore_func
= rl_ignore_some_completions_function
;
3738 orig_suppress
= rl_completion_suppress_append
;
3739 orig_sort
= rl_sort_completion_matches
;
3741 rl_menu_completion_entry_function
= history_completion_generator
;
3742 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3743 rl_ignore_some_completions_function
= filename_completion_ignore
;
3744 rl_filename_completion_desired
= 0;
3745 rl_completion_suppress_append
= 1;
3746 rl_sort_completion_matches
= 0;
3748 /* XXX - use rl_completion_mode here? */
3749 dabbrev_expand_active
= 1;
3750 if (rl_last_func
== bash_dabbrev_expand
)
3751 rl_last_func
= rl_menu_complete
;
3752 r
= rl_menu_complete (count
, key
);
3753 dabbrev_expand_active
= 0;
3755 rl_last_func
= bash_dabbrev_expand
;
3756 rl_menu_completion_entry_function
= orig_func
;
3757 rl_attempted_completion_function
= orig_attempt_func
;
3758 rl_ignore_some_completions_function
= orig_ignore_func
;
3759 rl_completion_suppress_append
= orig_suppress
;
3760 rl_sort_completion_matches
= orig_sort
;
3765 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
3767 bash_complete_username (ignore
, ignore2
)
3768 int ignore
, ignore2
;
3770 return bash_complete_username_internal (rl_completion_mode (bash_complete_username
));
3774 bash_possible_username_completions (ignore
, ignore2
)
3775 int ignore
, ignore2
;
3777 return bash_complete_username_internal ('?');
3781 bash_complete_username_internal (what_to_do
)
3784 return bash_specific_completion (what_to_do
, rl_username_completion_function
);
3788 bash_complete_filename (ignore
, ignore2
)
3789 int ignore
, ignore2
;
3791 return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename
));
3795 bash_possible_filename_completions (ignore
, ignore2
)
3796 int ignore
, ignore2
;
3798 return bash_complete_filename_internal ('?');
3802 bash_complete_filename_internal (what_to_do
)
3805 rl_compentry_func_t
*orig_func
;
3806 rl_completion_func_t
*orig_attempt_func
;
3807 rl_icppfunc_t
*orig_dir_func
;
3808 rl_compignore_func_t
*orig_ignore_func
;
3809 /*const*/ char *orig_rl_completer_word_break_characters
;
3812 orig_func
= rl_completion_entry_function
;
3813 orig_attempt_func
= rl_attempted_completion_function
;
3814 orig_ignore_func
= rl_ignore_some_completions_function
;
3815 orig_rl_completer_word_break_characters
= rl_completer_word_break_characters
;
3817 orig_dir_func
= save_directory_hook ();
3819 rl_completion_entry_function
= rl_filename_completion_function
;
3820 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3821 rl_ignore_some_completions_function
= filename_completion_ignore
;
3822 rl_completer_word_break_characters
= " \t\n\"\'";
3824 r
= rl_complete_internal (what_to_do
);
3826 rl_completion_entry_function
= orig_func
;
3827 rl_attempted_completion_function
= orig_attempt_func
;
3828 rl_ignore_some_completions_function
= orig_ignore_func
;
3829 rl_completer_word_break_characters
= orig_rl_completer_word_break_characters
;
3831 restore_directory_hook (orig_dir_func
);
3837 bash_complete_hostname (ignore
, ignore2
)
3838 int ignore
, ignore2
;
3840 return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname
));
3844 bash_possible_hostname_completions (ignore
, ignore2
)
3845 int ignore
, ignore2
;
3847 return bash_complete_hostname_internal ('?');
3851 bash_complete_variable (ignore
, ignore2
)
3852 int ignore
, ignore2
;
3854 return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable
));
3858 bash_possible_variable_completions (ignore
, ignore2
)
3859 int ignore
, ignore2
;
3861 return bash_complete_variable_internal ('?');
3865 bash_complete_command (ignore
, ignore2
)
3866 int ignore
, ignore2
;
3868 return bash_complete_command_internal (rl_completion_mode (bash_complete_command
));
3872 bash_possible_command_completions (ignore
, ignore2
)
3873 int ignore
, ignore2
;
3875 return bash_complete_command_internal ('?');
3879 bash_complete_hostname_internal (what_to_do
)
3882 return bash_specific_completion (what_to_do
, hostname_completion_function
);
3886 bash_complete_variable_internal (what_to_do
)
3889 return bash_specific_completion (what_to_do
, variable_completion_function
);
3893 bash_complete_command_internal (what_to_do
)
3896 return bash_specific_completion (what_to_do
, command_word_completion_function
);
3900 completion_glob_pattern (string
)
3903 return (glob_pattern_p (string
) == 1);
3906 static char *globtext
;
3907 static char *globorig
;
3910 glob_complete_word (text
, state
)
3914 static char **matches
= (char **)NULL
;
3921 rl_filename_completion_desired
= 1;
3923 if (globorig
!= globtext
)
3927 ttext
= bash_tilde_expand (text
, 0);
3929 if (rl_explicit_arg
)
3931 globorig
= savestring (ttext
);
3932 glen
= strlen (ttext
);
3933 globtext
= (char *)xmalloc (glen
+ 2);
3934 strcpy (globtext
, ttext
);
3935 globtext
[glen
] = '*';
3936 globtext
[glen
+1] = '\0';
3939 globtext
= globorig
= savestring (ttext
);
3944 matches
= shell_glob_filename (globtext
, 0);
3945 if (GLOB_FAILED (matches
))
3946 matches
= (char **)NULL
;
3950 ret
= matches
? matches
[ind
] : (char *)NULL
;
3956 bash_glob_completion_internal (what_to_do
)
3959 return bash_specific_completion (what_to_do
, glob_complete_word
);
3962 /* A special quoting function so we don't end up quoting globbing characters
3963 in the word if there are no matches or multiple matches. */
3965 bash_glob_quote_filename (s
, rtype
, qcp
)
3970 if (globorig
&& qcp
&& *qcp
== '\0' && STREQ (s
, globorig
))
3971 return (savestring (s
));
3973 return (bash_quote_filename (s
, rtype
, qcp
));
3977 bash_glob_complete_word (count
, key
)
3981 rl_quote_func_t
*orig_quoting_function
;
3983 if (rl_editing_mode
== EMACS_EDITING_MODE
)
3984 rl_explicit_arg
= 1; /* force `*' append */
3985 orig_quoting_function
= rl_filename_quoting_function
;
3986 rl_filename_quoting_function
= bash_glob_quote_filename
;
3988 r
= bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word
));
3990 rl_filename_quoting_function
= orig_quoting_function
;
3995 bash_glob_expand_word (count
, key
)
3998 return bash_glob_completion_internal ('*');
4002 bash_glob_list_expansions (count
, key
)
4005 return bash_glob_completion_internal ('?');
4009 bash_specific_completion (what_to_do
, generator
)
4011 rl_compentry_func_t
*generator
;
4013 rl_compentry_func_t
*orig_func
;
4014 rl_completion_func_t
*orig_attempt_func
;
4015 rl_compignore_func_t
*orig_ignore_func
;
4018 orig_func
= rl_completion_entry_function
;
4019 orig_attempt_func
= rl_attempted_completion_function
;
4020 orig_ignore_func
= rl_ignore_some_completions_function
;
4021 rl_completion_entry_function
= generator
;
4022 rl_attempted_completion_function
= NULL
;
4023 rl_ignore_some_completions_function
= orig_ignore_func
;
4025 r
= rl_complete_internal (what_to_do
);
4027 rl_completion_entry_function
= orig_func
;
4028 rl_attempted_completion_function
= orig_attempt_func
;
4029 rl_ignore_some_completions_function
= orig_ignore_func
;
4034 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
4036 #if defined (VI_MODE)
4037 /* Completion, from vi mode's point of view. This is a modified version of
4038 rl_vi_complete which uses the bash globbing code to implement what POSIX
4039 specifies, which is to append a `*' and attempt filename generation (which
4040 has the side effect of expanding any globbing characters in the word). */
4042 bash_vi_complete (count
, key
)
4045 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
4049 if ((rl_point
< rl_end
) && (!whitespace (rl_line_buffer
[rl_point
])))
4051 if (!whitespace (rl_line_buffer
[rl_point
+ 1]))
4052 rl_vi_end_word (1, 'E');
4056 /* Find boundaries of current word, according to vi definition of a
4062 rl_vi_bWord (1, 'B');
4067 t
= substring (rl_line_buffer
, p
, rl_point
);
4070 if (t
&& completion_glob_pattern (t
) == 0)
4071 rl_explicit_arg
= 1; /* XXX - force glob_complete_word to append `*' */
4074 if (key
== '*') /* Expansion and replacement. */
4075 r
= bash_glob_expand_word (count
, key
);
4076 else if (key
== '=') /* List possible completions. */
4077 r
= bash_glob_list_expansions (count
, key
);
4078 else if (key
== '\\') /* Standard completion */
4079 r
= bash_glob_complete_word (count
, key
);
4081 r
= rl_complete (0, key
);
4083 if (key
== '*' || key
== '\\')
4084 rl_vi_start_inserting (key
, 1, 1);
4088 return rl_vi_complete (count
, key
);
4089 #endif /* !SPECIFIC_COMPLETION_FUNCTIONS */
4091 #endif /* VI_MODE */
4093 /* Filename quoting for completion. */
4094 /* A function to strip unquoted quote characters (single quotes, double
4095 quotes, and backslashes). It allows single quotes to appear
4096 within double quotes, and vice versa. It should be smarter. */
4098 bash_dequote_filename (text
, quote_char
)
4106 ret
= (char *)xmalloc (l
+ 1);
4107 for (quoted
= quote_char
, p
= text
, r
= ret
; p
&& *p
; p
++)
4109 /* Allow backslash-escaped characters to pass through unscathed. */
4112 /* Backslashes are preserved within single quotes. */
4115 /* Backslashes are preserved within double quotes unless the
4116 character is one that is defined to be escaped */
4117 else if (quoted
== '"' && ((sh_syntaxtab
[(unsigned char)p
[1]] & CBSDQUOTE
) == 0))
4122 return ret
; /* XXX - was break; */
4126 if (quoted
&& *p
== quoted
)
4132 if (quoted
== 0 && (*p
== '\'' || *p
== '"'))
4143 /* Quote characters that the readline completion code would treat as
4144 word break characters with backslashes. Pass backslash-quoted
4145 characters through without examination. */
4147 quote_word_break_chars (text
)
4154 ret
= (char *)xmalloc ((2 * l
) + 1);
4155 for (s
= text
, r
= ret
; *s
; s
++)
4157 /* Pass backslash-quoted characters through, including the backslash. */
4166 /* OK, we have an unquoted character. Check its presence in
4167 rl_completer_word_break_characters. */
4168 if (mbschr (rl_completer_word_break_characters
, *s
))
4170 /* XXX -- check for standalone tildes here and backslash-quote them */
4171 if (s
== text
&& *s
== '~' && file_exists (text
))
4179 /* Use characters in STRING to populate the table of characters that should
4180 be backslash-quoted. The table will be used for sh_backslash_quote from
4183 set_filename_bstab (string
)
4188 memset (filename_bstab
, 0, sizeof (filename_bstab
));
4189 for (s
= string
; s
&& *s
; s
++)
4190 filename_bstab
[(unsigned char)*s
] = 1;
4193 /* Quote a filename using double quotes, single quotes, or backslashes
4194 depending on the value of completion_quoting_style. If we're
4195 completing using backslashes, we need to quote some additional
4196 characters (those that readline treats as word breaks), so we call
4197 quote_word_break_chars on the result. This returns newly-allocated
4200 bash_quote_filename (s
, rtype
, qcp
)
4205 char *rtext
, *mtext
, *ret
;
4208 rtext
= (char *)NULL
;
4210 /* If RTYPE == MULT_MATCH, it means that there is
4211 more than one match. In this case, we do not add
4212 the closing quote or attempt to perform tilde
4213 expansion. If RTYPE == SINGLE_MATCH, we try
4214 to perform tilde expansion, because single and double
4215 quotes inhibit tilde expansion by the shell. */
4217 cs
= completion_quoting_style
;
4218 /* Might need to modify the default completion style based on *qcp,
4219 since it's set to any user-provided opening quote. We also change
4220 to single-quoting if there is no user-provided opening quote and
4221 the word being completed contains newlines, since those are not
4222 quoted correctly using backslashes (a backslash-newline pair is
4223 special to the shell parser). */
4224 if (*qcp
== '\0' && cs
== COMPLETE_BSQUOTE
&& mbschr (s
, '\n'))
4225 cs
= COMPLETE_SQUOTE
;
4226 else if (*qcp
== '"')
4227 cs
= COMPLETE_DQUOTE
;
4228 else if (*qcp
== '\'')
4229 cs
= COMPLETE_SQUOTE
;
4230 #if defined (BANG_HISTORY)
4231 else if (*qcp
== '\0' && history_expansion
&& cs
== COMPLETE_DQUOTE
&&
4232 history_expansion_inhibited
== 0 && mbschr (s
, '!'))
4233 cs
= COMPLETE_BSQUOTE
;
4235 if (*qcp
== '"' && history_expansion
&& cs
== COMPLETE_DQUOTE
&&
4236 history_expansion_inhibited
== 0 && mbschr (s
, '!'))
4238 cs
= COMPLETE_BSQUOTE
;
4243 /* Don't tilde-expand backslash-quoted filenames, since only single and
4244 double quotes inhibit tilde expansion. */
4246 if (mtext
[0] == '~' && rtype
== SINGLE_MATCH
&& cs
!= COMPLETE_BSQUOTE
)
4247 mtext
= bash_tilde_expand (s
, 0);
4251 case COMPLETE_DQUOTE
:
4252 rtext
= sh_double_quote (mtext
);
4254 case COMPLETE_SQUOTE
:
4255 rtext
= sh_single_quote (mtext
);
4257 case COMPLETE_BSQUOTE
:
4258 rtext
= sh_backslash_quote (mtext
, complete_fullquote
? 0 : filename_bstab
, 0);
4265 /* We may need to quote additional characters: those that readline treats
4266 as word breaks that are not quoted by backslash_quote. */
4267 if (rtext
&& cs
== COMPLETE_BSQUOTE
)
4269 mtext
= quote_word_break_chars (rtext
);
4274 /* Leave the opening quote intact. The readline completion code takes
4275 care of avoiding doubled opening quotes. */
4278 rlen
= strlen (rtext
);
4279 ret
= (char *)xmalloc (rlen
+ 1);
4280 strcpy (ret
, rtext
);
4284 ret
= (char *)xmalloc (rlen
= 1);
4288 /* If there are multiple matches, cut off the closing quote. */
4289 if (rtype
== MULT_MATCH
&& cs
!= COMPLETE_BSQUOTE
)
4290 ret
[rlen
- 1] = '\0';
4295 /* Support for binding readline key sequences to Unix commands. Each editing
4296 mode has a separate Unix command keymap. */
4298 static Keymap emacs_std_cmd_xmap
;
4299 #if defined (VI_MODE)
4300 static Keymap vi_insert_cmd_xmap
;
4301 static Keymap vi_movement_cmd_xmap
;
4313 x
= putc (c
, rl_outstream
);
4320 readline_get_char_offset (ind
)
4326 #if defined (HANDLE_MULTIBYTE)
4327 if (locale_mb_cur_max
> 1)
4329 old_ch
= rl_line_buffer
[ind
];
4330 rl_line_buffer
[ind
] = '\0';
4331 r
= MB_STRLEN (rl_line_buffer
);
4332 rl_line_buffer
[ind
] = old_ch
;
4339 readline_set_char_offset (ind
, varp
)
4347 #if defined (HANDLE_MULTIBYTE)
4348 if (i
> 0 && locale_mb_cur_max
> 1)
4349 i
= _rl_find_next_mbchar (rl_line_buffer
, 0, i
, 0); /* XXX */
4362 bash_execute_unix_command (count
, key
)
4363 int count
; /* ignored */
4369 sh_parser_state_t ps
;
4370 char *cmd
, *value
, *ce
, old_ch
;
4372 char ibuf
[INT_STRLEN_BOUND(int) + 1];
4375 /* First, we need to find the right command to execute. This is tricky,
4376 because we might have already indirected into another keymap, so we
4377 have to walk cmd_xmap using the entire key sequence. */
4378 cmd_xmap
= get_cmd_xmap_from_keymap (rl_get_keymap ());
4379 cmd
= (char *)rl_function_of_keyseq_len (rl_executing_keyseq
, rl_key_sequence_length
, cmd_xmap
, &type
);
4381 if (type
== ISKMAP
&& (type
= ((Keymap
) cmd
)[ANYOTHERKEY
].type
) == ISMACR
)
4382 cmd
= (char*)((Keymap
) cmd
)[ANYOTHERKEY
].function
;
4384 if (cmd
== 0 || type
!= ISMACR
)
4387 internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
4388 rl_forced_update_display ();
4392 ce
= rl_get_termcap ("ce");
4393 if (ce
) /* clear current line */
4395 rl_clear_visible_line ();
4396 fflush (rl_outstream
);
4399 rl_crlf (); /* move to a new line */
4401 v
= bind_variable ("READLINE_LINE", rl_line_buffer
, 0);
4403 VSETATTR (v
, att_exported
);
4405 i
= readline_get_char_offset (rl_point
);
4406 value
= inttostr (i
, ibuf
, sizeof (ibuf
));
4407 v
= bind_int_variable ("READLINE_POINT", value
, 0);
4409 VSETATTR (v
, att_exported
);
4411 i
= readline_get_char_offset (rl_mark
);
4412 value
= inttostr (i
, ibuf
, sizeof (ibuf
));
4413 v
= bind_int_variable ("READLINE_MARK", value
, 0);
4415 VSETATTR (v
, att_exported
);
4416 array_needs_making
= 1;
4418 save_parser_state (&ps
);
4419 rl_clear_signals ();
4420 r
= parse_and_execute (savestring (cmd
), "bash_execute_unix_command", SEVAL_NOHIST
);
4422 restore_parser_state (&ps
);
4424 v
= find_variable ("READLINE_LINE");
4425 maybe_make_readline_line (v
? value_cell (v
) : 0);
4427 v
= find_variable ("READLINE_POINT");
4428 if (v
&& legal_number (value_cell (v
), &mi
))
4429 readline_set_char_offset (mi
, &rl_point
);
4431 v
= find_variable ("READLINE_MARK");
4432 if (v
&& legal_number (value_cell (v
), &mi
))
4433 readline_set_char_offset (mi
, &rl_mark
);
4435 check_unbind_variable ("READLINE_LINE");
4436 check_unbind_variable ("READLINE_POINT");
4437 check_unbind_variable ("READLINE_MARK");
4438 array_needs_making
= 1;
4440 /* and restore the readline buffer and display after command execution. */
4441 /* If we clear the last line of the prompt above, redraw only that last
4442 line. If the command returns 124, we redraw unconditionally as in
4443 previous versions. */
4445 rl_redraw_prompt_last_line ();
4447 rl_forced_update_display ();
4453 print_unix_command_map ()
4455 Keymap save
, cmd_xmap
;
4457 save
= rl_get_keymap ();
4458 cmd_xmap
= get_cmd_xmap_from_keymap (save
);
4459 rl_set_keymap (cmd_xmap
);
4460 rl_macro_dumper (1);
4461 rl_set_keymap (save
);
4466 init_unix_command_map ()
4468 emacs_std_cmd_xmap
= rl_make_bare_keymap ();
4470 emacs_std_cmd_xmap
[CTRL('X')].type
= ISKMAP
;
4471 emacs_std_cmd_xmap
[CTRL('X')].function
= KEYMAP_TO_FUNCTION (rl_make_bare_keymap ());
4472 emacs_std_cmd_xmap
[ESC
].type
= ISKMAP
;
4473 emacs_std_cmd_xmap
[ESC
].function
= KEYMAP_TO_FUNCTION (rl_make_bare_keymap ());
4475 #if defined (VI_MODE)
4476 vi_insert_cmd_xmap
= rl_make_bare_keymap ();
4477 vi_movement_cmd_xmap
= rl_make_bare_keymap ();
4482 get_cmd_xmap_from_edit_mode ()
4484 if (emacs_std_cmd_xmap
== 0)
4485 init_unix_command_map ();
4487 switch (rl_editing_mode
)
4489 case EMACS_EDITING_MODE
:
4490 return emacs_std_cmd_xmap
;
4491 #if defined (VI_MODE)
4492 case VI_EDITING_MODE
:
4493 return (get_cmd_xmap_from_keymap (rl_get_keymap ()));
4496 return (Keymap
)NULL
;
4501 get_cmd_xmap_from_keymap (kmap
)
4504 if (emacs_std_cmd_xmap
== 0)
4505 init_unix_command_map ();
4507 if (kmap
== emacs_standard_keymap
)
4508 return emacs_std_cmd_xmap
;
4509 else if (kmap
== emacs_meta_keymap
)
4510 return (FUNCTION_TO_KEYMAP (emacs_std_cmd_xmap
, ESC
));
4511 else if (kmap
== emacs_ctlx_keymap
)
4512 return (FUNCTION_TO_KEYMAP (emacs_std_cmd_xmap
, CTRL('X')));
4513 #if defined (VI_MODE)
4514 else if (kmap
== vi_insertion_keymap
)
4515 return vi_insert_cmd_xmap
;
4516 else if (kmap
== vi_movement_keymap
)
4517 return vi_movement_cmd_xmap
;
4520 return (Keymap
)NULL
;
4524 isolate_sequence (string
, ind
, need_dquote
, startp
)
4526 int ind
, need_dquote
, *startp
;
4529 int c
, passc
, delim
;
4531 for (i
= ind
; string
[i
] && whitespace (string
[i
]); i
++)
4533 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
4534 if (need_dquote
&& string
[i
] != '"')
4536 builtin_error (_("%s: first non-whitespace character is not `\"'"), string
);
4540 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
4541 string to bind the key sequence to. */
4542 delim
= (string
[i
] == '"' || string
[i
] == '\'') ? string
[i
] : 0;
4545 *startp
= delim
? ++i
: i
;
4547 for (passc
= 0; c
= string
[i
]; i
++)
4563 if (delim
&& string
[i
] != delim
)
4565 builtin_error (_("no closing `%c' in %s"), delim
, string
);
4573 bind_keyseq_to_unix_command (line
)
4576 Keymap kmap
, cmd_xmap
;
4580 kmap
= rl_get_keymap ();
4582 /* We duplicate some of the work done by rl_parse_and_bind here, but
4583 this code only has to handle `"keyseq": ["]command["]' and can
4584 generate an error for anything else. */
4585 i
= isolate_sequence (line
, 0, 1, &kstart
);
4589 /* Create the key sequence string to pass to rl_generic_bind */
4590 kseq
= substring (line
, kstart
, i
);
4592 for ( ; line
[i
] && line
[i
] != ':'; i
++)
4596 builtin_error (_("%s: missing colon separator"), line
);
4601 i
= isolate_sequence (line
, i
+ 1, 0, &kstart
);
4608 /* Create the value string containing the command to execute. */
4609 value
= substring (line
, kstart
, i
);
4611 /* Save the command to execute and the key sequence in the CMD_XMAP */
4612 cmd_xmap
= get_cmd_xmap_from_keymap (kmap
);
4613 rl_generic_bind (ISMACR
, kseq
, value
, cmd_xmap
);
4615 /* and bind the key sequence in the current keymap to a function that
4616 understands how to execute from CMD_XMAP */
4617 rl_bind_keyseq_in_map (kseq
, bash_execute_unix_command
, kmap
);
4624 unbind_unix_command (kseq
)
4629 cmd_xmap
= get_cmd_xmap_from_keymap (rl_get_keymap ());
4630 if (rl_bind_keyseq_in_map (kseq
, (rl_command_func_t
*)NULL
, cmd_xmap
) != 0)
4632 builtin_error (_("`%s': cannot unbind in command keymap"), kseq
);
4638 /* Used by the programmable completion code. Complete TEXT as a filename,
4639 but return only directories as matches. Dequotes the filename before
4640 attempting to find matches. */
4642 bash_directory_completion_matches (text
)
4649 qc
= rl_dispatching
? rl_completion_quote_character
: 0;
4650 /* If rl_completion_found_quote != 0, rl_completion_matches will call the
4651 filename dequoting function, causing the directory name to be dequoted
4653 if (rl_dispatching
&& rl_completion_found_quote
== 0)
4654 dfn
= bash_dequote_filename ((char *)text
, qc
);
4657 m1
= rl_completion_matches (dfn
, rl_filename_completion_function
);
4661 if (m1
== 0 || m1
[0] == 0)
4663 /* We don't bother recomputing the lcd of the matches, because it will just
4664 get thrown away by the programmable completion code and recomputed
4666 (void)bash_progcomp_ignore_filenames (m1
);
4671 bash_dequote_text (text
)
4677 qc
= (text
[0] == '"' || text
[0] == '\'') ? text
[0] : 0;
4678 dtxt
= bash_dequote_filename ((char *)text
, qc
);
4682 /* This event hook is designed to be called after readline receives a signal
4683 that interrupts read(2). It gives reasonable responsiveness to interrupts
4684 and fatal signals without executing too much code in a signal handler
4691 /* XXX - see if we need to do anything here if sigterm_received == 1,
4692 we probably don't want to reset the event hook since we will not be
4693 jumping to the top level */
4694 if (sigterm_received
)
4696 /* RESET_SIGTERM; */
4701 if (terminating_signal
)
4702 sig
= terminating_signal
;
4703 else if (interrupt_state
)
4705 else if (read_timeout
&& read_timeout
->alrmflag
)
4707 else if (RL_ISSTATE (RL_STATE_TIMEOUT
)) /* just in case */
4711 read_timeout
->alrmflag
= 1;
4714 sig
= first_pending_trap ();
4716 /* If we're going to longjmp to top_level, make sure we clean up readline.
4717 check_signals will call QUIT, which will eventually longjmp to top_level,
4718 calling run_interrupt_trap along the way. The check against read_timeout
4719 is so we can clean up the read builtin's state. */
4720 if (terminating_signal
|| interrupt_state
|| (read_timeout
&& read_timeout
->alrmflag
))
4721 rl_cleanup_after_signal ();
4722 bashline_reset_event_hook ();
4724 RL_UNSETSTATE (RL_STATE_TIMEOUT
); /* XXX */
4726 /* posix mode SIGINT during read -e. We only get here if SIGINT is trapped. */
4727 if (posixly_correct
&& this_shell_builtin
== read_builtin
&& sig
== SIGINT
)
4729 last_command_exit_value
= 128|SIGINT
;
4730 throw_to_top_level ();
4733 check_signals_and_traps (); /* XXX */
4737 #endif /* READLINE */