1 /* bashline.c -- Bash's interface to the readline library. */
3 /* Copyright (C) 1987-2022 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 int bash_check_expchar
PARAMS((char *, int, int *, int *));
196 static void set_filename_quote_chars
PARAMS((int, int, int));
197 static void set_filename_bstab
PARAMS((const char *));
198 static char *bash_quote_filename
PARAMS((char *, int, char *));
201 static void putx
PARAMS((int));
203 static int putx
PARAMS((int));
205 static int readline_get_char_offset
PARAMS((int));
206 static void readline_set_char_offset
PARAMS((int, int *));
208 static Keymap get_cmd_xmap_from_edit_mode
PARAMS((void));
209 static Keymap get_cmd_xmap_from_keymap
PARAMS((Keymap
));
211 static void init_unix_command_map
PARAMS((void));
212 static int isolate_sequence
PARAMS((char *, int, int, int *));
214 static int set_saved_history
PARAMS((void));
217 static int posix_edit_macros
PARAMS((int, int));
220 static int bash_event_hook
PARAMS((void));
222 #if defined (PROGRAMMABLE_COMPLETION)
223 static int find_cmd_start
PARAMS((int));
224 static int find_cmd_end
PARAMS((int));
225 static char *find_cmd_name
PARAMS((int, int *, int *));
226 static char *prog_complete_return
PARAMS((const char *, int));
228 static char **prog_complete_matches
;
231 extern int no_symbolic_links
;
232 extern STRING_INT_ALIST word_token_alist
[];
233 extern sh_timer
*read_timeout
;
235 /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
236 completion functions which indicate what type of completion should be
237 done (at or before point) that can be bound to key sequences with
238 the readline library. */
239 #define SPECIFIC_COMPLETION_FUNCTIONS
241 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
242 static int bash_specific_completion
PARAMS((int, rl_compentry_func_t
*));
244 static int bash_complete_filename_internal
PARAMS((int));
245 static int bash_complete_username_internal
PARAMS((int));
246 static int bash_complete_hostname_internal
PARAMS((int));
247 static int bash_complete_variable_internal
PARAMS((int));
248 static int bash_complete_command_internal
PARAMS((int));
250 static int bash_complete_filename
PARAMS((int, int));
251 static int bash_possible_filename_completions
PARAMS((int, int));
252 static int bash_complete_username
PARAMS((int, int));
253 static int bash_possible_username_completions
PARAMS((int, int));
254 static int bash_complete_hostname
PARAMS((int, int));
255 static int bash_possible_hostname_completions
PARAMS((int, int));
256 static int bash_complete_variable
PARAMS((int, int));
257 static int bash_possible_variable_completions
PARAMS((int, int));
258 static int bash_complete_command
PARAMS((int, int));
259 static int bash_possible_command_completions
PARAMS((int, int));
261 static int completion_glob_pattern
PARAMS((char *));
262 static char *glob_complete_word
PARAMS((const char *, int));
263 static int bash_glob_completion_internal
PARAMS((int));
264 static int bash_glob_complete_word
PARAMS((int, int));
265 static int bash_glob_expand_word
PARAMS((int, int));
266 static int bash_glob_list_expansions
PARAMS((int, int));
268 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
270 static int edit_and_execute_command
PARAMS((int, int, int, char *));
271 #if defined (VI_MODE)
272 static int vi_edit_and_execute_command
PARAMS((int, int));
273 static int bash_vi_complete
PARAMS((int, int));
275 static int emacs_edit_and_execute_command
PARAMS((int, int));
277 /* Non-zero once initialize_readline () has been called. */
278 int bash_readline_initialized
= 0;
280 /* If non-zero, we do hostname completion, breaking words at `@' and
281 trying to complete the stuff after the `@' from our own internal
283 int perform_hostname_completion
= 1;
285 /* If non-zero, we don't do command completion on an empty line. */
286 int no_empty_command_completion
;
288 /* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the
289 only possible matches. Set to 0 if you want to match filenames if they
290 are the only possible matches, even if FIGNORE says to. */
291 int force_fignore
= 1;
293 /* Perform spelling correction on directory names during word completion */
294 int dircomplete_spelling
= 0;
296 /* Expand directory names during word/filename completion. */
297 #if DIRCOMPLETE_EXPAND_DEFAULT
298 int dircomplete_expand
= 1;
299 int dircomplete_expand_relpath
= 1;
301 int dircomplete_expand
= 0;
302 int dircomplete_expand_relpath
= 0;
305 /* When non-zero, perform `normal' shell quoting on completed filenames
306 even when the completed name contains a directory name with a shell
307 variable reference, so dollar signs in a filename get quoted appropriately.
308 Set to zero to remove dollar sign (and braces or parens as needed) from
309 the set of characters that will be quoted. */
310 int complete_fullquote
= 1;
312 static char *bash_completer_word_break_characters
= " \t\n\"'@><=;|&(:";
313 static char *bash_nohostname_word_break_characters
= " \t\n\"'><=;|&(:";
316 static const char *default_filename_quote_characters
= " \t\n\\\"'@<>=;|&()#$`?*[!:{~"; /*}*/
317 static char *custom_filename_quote_characters
= 0;
318 static char filename_bstab
[256];
320 static rl_hook_func_t
*old_rl_startup_hook
= (rl_hook_func_t
*)NULL
;
322 static int dot_in_path
= 0;
324 /* Set to non-zero when dabbrev-expand is running */
325 static int dabbrev_expand_active
= 0;
327 /* What kind of quoting is performed by bash_quote_filename:
328 COMPLETE_DQUOTE = double-quoting the filename
329 COMPLETE_SQUOTE = single_quoting the filename
330 COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
332 #define COMPLETE_DQUOTE 1
333 #define COMPLETE_SQUOTE 2
334 #define COMPLETE_BSQUOTE 3
335 static int completion_quoting_style
= COMPLETE_BSQUOTE
;
337 /* Flag values for the final argument to bash_default_completion */
338 #define DEFCOMP_CMDPOS 1
340 static rl_command_func_t
*vi_tab_binding
= rl_complete
;
342 /* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
343 Called when the shell is put into or out of `posix' mode. */
345 posix_readline_initialize (on_or_off
)
348 static char kseq
[2] = { CTRL ('I'), 0 }; /* TAB */
351 rl_variable_bind ("comment-begin", "#");
352 #if defined (VI_MODE)
355 vi_tab_binding
= rl_function_of_keyseq (kseq
, vi_insertion_keymap
, (int *)NULL
);
356 rl_bind_key_in_map (CTRL ('I'), rl_insert
, vi_insertion_keymap
);
360 if (rl_function_of_keyseq (kseq
, vi_insertion_keymap
, (int *)NULL
) == rl_insert
)
361 rl_bind_key_in_map (CTRL ('I'), vi_tab_binding
, vi_insertion_keymap
);
367 reset_completer_word_break_chars ()
369 rl_completer_word_break_characters
= perform_hostname_completion
? savestring (bash_completer_word_break_characters
) : savestring (bash_nohostname_word_break_characters
);
372 /* When this function returns, rl_completer_word_break_characters points to
373 dynamically allocated memory. */
375 enable_hostname_completion (on_or_off
)
382 old_value
= perform_hostname_completion
;
386 perform_hostname_completion
= 1;
387 rl_special_prefixes
= "$@";
391 perform_hostname_completion
= 0;
392 rl_special_prefixes
= "$";
395 /* Now we need to figure out how to appropriately modify and assign
396 rl_completer_word_break_characters depending on whether we want
397 hostname completion on or off. */
399 /* If this is the first time this has been called
400 (bash_readline_initialized == 0), use the sames values as before, but
401 allocate new memory for rl_completer_word_break_characters. */
403 if (bash_readline_initialized
== 0 &&
404 (rl_completer_word_break_characters
== 0 ||
405 rl_completer_word_break_characters
== rl_basic_word_break_characters
))
408 rl_completer_word_break_characters
= savestring (bash_completer_word_break_characters
);
410 rl_completer_word_break_characters
= savestring (bash_nohostname_word_break_characters
);
414 /* See if we have anything to do. */
415 at
= strchr (rl_completer_word_break_characters
, '@');
416 if ((at
== 0 && on_or_off
== 0) || (at
!= 0 && on_or_off
!= 0))
419 /* We have something to do. Do it. */
420 nval
= (char *)xmalloc (strlen (rl_completer_word_break_characters
) + 1 + on_or_off
);
424 /* Turn it off -- just remove `@' from word break chars. We want
425 to remove all occurrences of `@' from the char list, so we loop
426 rather than just copy the rest of the list over AT. */
427 for (nv
= nval
, at
= rl_completer_word_break_characters
; *at
; )
437 strcpy (nval
+ 1, rl_completer_word_break_characters
);
440 free ((void *)rl_completer_word_break_characters
);
441 rl_completer_word_break_characters
= nval
;
447 /* Called once from parse.y if we are going to use readline. */
449 initialize_readline ()
451 rl_command_func_t
*func
;
454 if (bash_readline_initialized
)
457 rl_terminal_name
= get_string_value ("TERM");
459 rl_outstream
= stderr
;
461 /* Allow conditional parsing of the ~/.inputrc file. */
462 rl_readline_name
= "Bash";
464 /* Add bindable names before calling rl_initialize so they may be
465 referenced in the various inputrc files. */
466 rl_add_defun ("shell-expand-line", shell_expand_line
, -1);
468 rl_add_defun ("history-expand-line", history_expand_line
, -1);
469 rl_add_defun ("magic-space", tcsh_magic_space
, -1);
472 rl_add_defun ("shell-forward-word", bash_forward_shellword
, -1);
473 rl_add_defun ("shell-backward-word", bash_backward_shellword
, -1);
474 rl_add_defun ("shell-kill-word", bash_kill_shellword
, -1);
475 rl_add_defun ("shell-backward-kill-word", bash_backward_kill_shellword
, -1);
476 rl_add_defun ("shell-transpose-words", bash_transpose_shellwords
, -1);
478 rl_add_defun ("spell-correct-word", bash_spell_correct_shellword
, -1);
479 rl_bind_key_if_unbound_in_map ('s', bash_spell_correct_shellword
, emacs_ctlx_keymap
);
482 rl_add_defun ("alias-expand-line", alias_expand_line
, -1);
484 rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line
, -1);
488 /* Backwards compatibility. */
489 rl_add_defun ("insert-last-argument", rl_yank_last_arg
, -1);
491 rl_add_defun ("display-shell-version", display_shell_version
, -1);
492 rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command
, -1);
493 #if defined (VI_MODE)
494 rl_add_defun ("vi-edit-and-execute-command", vi_edit_and_execute_command
, -1);
497 #if defined (BRACE_COMPLETION)
498 rl_add_defun ("complete-into-braces", bash_brace_completion
, -1);
501 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
502 rl_add_defun ("complete-filename", bash_complete_filename
, -1);
503 rl_add_defun ("possible-filename-completions", bash_possible_filename_completions
, -1);
504 rl_add_defun ("complete-username", bash_complete_username
, -1);
505 rl_add_defun ("possible-username-completions", bash_possible_username_completions
, -1);
506 rl_add_defun ("complete-hostname", bash_complete_hostname
, -1);
507 rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions
, -1);
508 rl_add_defun ("complete-variable", bash_complete_variable
, -1);
509 rl_add_defun ("possible-variable-completions", bash_possible_variable_completions
, -1);
510 rl_add_defun ("complete-command", bash_complete_command
, -1);
511 rl_add_defun ("possible-command-completions", bash_possible_command_completions
, -1);
512 rl_add_defun ("glob-complete-word", bash_glob_complete_word
, -1);
513 rl_add_defun ("glob-expand-word", bash_glob_expand_word
, -1);
514 rl_add_defun ("glob-list-expansions", bash_glob_list_expansions
, -1);
517 rl_add_defun ("dynamic-complete-history", dynamic_complete_history
, -1);
518 rl_add_defun ("dabbrev-expand", bash_dabbrev_expand
, -1);
520 /* Bind defaults before binding our custom shell keybindings. */
521 if (RL_ISSTATE(RL_STATE_INITIALIZED
) == 0)
524 /* Bind up our special shell functions. */
525 rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line
, emacs_meta_keymap
);
528 rl_bind_key_if_unbound_in_map ('^', history_expand_line
, emacs_meta_keymap
);
531 rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version
, emacs_ctlx_keymap
);
533 /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
534 so it is not necessary to allow C-M-j for context switching. Turn
535 off this occasionally confusing behaviour. */
538 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
539 if (func
== rl_vi_editing_mode
)
540 rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap
);
542 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
543 if (func
== rl_vi_editing_mode
)
544 rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap
);
545 #if defined (VI_MODE)
547 func
= rl_function_of_keyseq (kseq
, vi_movement_keymap
, (int *)NULL
);
548 if (func
== rl_emacs_editing_mode
)
549 rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap
);
552 #if defined (BRACE_COMPLETION)
553 rl_bind_key_if_unbound_in_map ('{', bash_brace_completion
, emacs_meta_keymap
); /*}*/
554 #endif /* BRACE_COMPLETION */
556 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
557 rl_bind_key_if_unbound_in_map ('/', bash_complete_filename
, emacs_meta_keymap
);
558 rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions
, emacs_ctlx_keymap
);
560 /* Have to jump through hoops here because there is a default binding for
561 M-~ (rl_tilde_expand) */
564 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
565 if (func
== 0 || func
== rl_tilde_expand
)
566 rl_bind_keyseq_in_map (kseq
, bash_complete_username
, emacs_meta_keymap
);
568 rl_bind_key_if_unbound_in_map ('~', bash_possible_username_completions
, emacs_ctlx_keymap
);
570 rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname
, emacs_meta_keymap
);
571 rl_bind_key_if_unbound_in_map ('@', bash_possible_hostname_completions
, emacs_ctlx_keymap
);
573 rl_bind_key_if_unbound_in_map ('$', bash_complete_variable
, emacs_meta_keymap
);
574 rl_bind_key_if_unbound_in_map ('$', bash_possible_variable_completions
, emacs_ctlx_keymap
);
576 rl_bind_key_if_unbound_in_map ('!', bash_complete_command
, emacs_meta_keymap
);
577 rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions
, emacs_ctlx_keymap
);
579 rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word
, emacs_meta_keymap
);
580 rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word
, emacs_ctlx_keymap
);
581 rl_bind_key_if_unbound_in_map ('g', bash_glob_list_expansions
, emacs_ctlx_keymap
);
583 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
587 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
588 if (func
== 0 || func
== rl_tab_insert
)
589 rl_bind_key_in_map (TAB
, dynamic_complete_history
, emacs_meta_keymap
);
591 /* Tell the completer that we want a crack first. */
592 rl_attempted_completion_function
= attempt_shell_completion
;
594 /* Tell the completer that we might want to follow symbolic links or
595 do other expansion on directory names. */
596 set_directory_hook ();
598 rl_filename_rewrite_hook
= bash_filename_rewrite_hook
;
600 rl_filename_stat_hook
= bash_filename_stat_hook
;
602 /* Tell the filename completer we want a chance to ignore some names. */
603 rl_ignore_some_completions_function
= filename_completion_ignore
;
605 /* Bind C-xC-e to invoke emacs and run result as commands. */
606 rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command
, emacs_ctlx_keymap
);
607 #if defined (VI_MODE)
608 rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command
, vi_movement_keymap
);
610 rl_bind_key_if_unbound_in_map ('@', posix_edit_macros
, vi_movement_keymap
);
613 rl_bind_key_in_map ('\\', bash_vi_complete
, vi_movement_keymap
);
614 rl_bind_key_in_map ('*', bash_vi_complete
, vi_movement_keymap
);
615 rl_bind_key_in_map ('=', bash_vi_complete
, vi_movement_keymap
);
618 rl_completer_quote_characters
= "'\"";
620 /* This sets rl_completer_word_break_characters and rl_special_prefixes
621 to the appropriate values, depending on whether or not hostname
622 completion is enabled. */
623 enable_hostname_completion (perform_hostname_completion
);
625 /* characters that need to be quoted when appearing in filenames. */
626 rl_filename_quote_characters
= default_filename_quote_characters
;
627 set_filename_bstab (rl_filename_quote_characters
);
629 rl_filename_quoting_function
= bash_quote_filename
;
630 rl_filename_dequoting_function
= bash_dequote_filename
;
631 rl_char_is_quoted_p
= char_is_quoted
;
633 /* Add some default bindings for the "shellwords" functions, roughly
634 parallelling the default word bindings in emacs mode. */
635 rl_bind_key_if_unbound_in_map (CTRL('B'), bash_backward_shellword
, emacs_meta_keymap
);
636 rl_bind_key_if_unbound_in_map (CTRL('D'), bash_kill_shellword
, emacs_meta_keymap
);
637 rl_bind_key_if_unbound_in_map (CTRL('F'), bash_forward_shellword
, emacs_meta_keymap
);
638 rl_bind_key_if_unbound_in_map (CTRL('T'), bash_transpose_shellwords
, emacs_meta_keymap
);
641 /* This is superfluous and makes it impossible to use tab completion in
642 vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix()
643 should already have called posix_readline_initialize() when
644 posixly_correct was set. */
646 posix_readline_initialize (1);
649 bash_readline_initialized
= 1;
653 bashline_reinitialize ()
655 bash_readline_initialized
= 0;
659 bashline_set_event_hook ()
661 rl_signal_event_hook
= bash_event_hook
;
665 bashline_reset_event_hook ()
667 rl_signal_event_hook
= 0;
670 /* On Sun systems at least, rl_attempted_completion_function can end up
671 getting set to NULL, and rl_completion_entry_function set to do command
672 word completion if Bash is interrupted while trying to complete a command
673 word. This just resets all the completion functions to the right thing.
674 It's called from throw_to_top_level(). */
679 rl_attempted_completion_function
= attempt_shell_completion
;
680 rl_completion_entry_function
= NULL
;
681 rl_ignore_some_completions_function
= filename_completion_ignore
;
683 complete_fullquote
= 1;
684 rl_filename_quote_characters
= default_filename_quote_characters
;
685 set_filename_bstab (rl_filename_quote_characters
);
687 set_directory_hook ();
688 rl_filename_stat_hook
= bash_filename_stat_hook
;
690 bashline_reset_event_hook ();
692 rl_sort_completion_matches
= 1;
695 /* Contains the line to push into readline. */
696 static char *push_to_readline
= (char *)NULL
;
698 /* Push the contents of push_to_readline into the
703 if (push_to_readline
)
705 rl_insert_text (push_to_readline
);
706 free (push_to_readline
);
707 push_to_readline
= (char *)NULL
;
708 rl_startup_hook
= old_rl_startup_hook
;
713 /* Call this to set the initial text for the next line to read
719 FREE (push_to_readline
);
721 push_to_readline
= savestring (line
);
722 old_rl_startup_hook
= rl_startup_hook
;
723 rl_startup_hook
= bash_push_line
;
729 display_shell_version (count
, c
)
733 show_shell_version (0);
734 putc ('\r', rl_outstream
);
735 fflush (rl_outstream
);
741 /* **************************************************************** */
745 /* **************************************************************** */
747 /* If the user requests hostname completion, then simply build a list
748 of hosts, and complete from that forever more, or at least until
749 HOSTFILE is unset. */
751 /* THIS SHOULD BE A STRINGLIST. */
752 /* The kept list of hostnames. */
753 static char **hostname_list
= (char **)NULL
;
755 /* The physical size of the above list. */
756 static int hostname_list_size
;
758 /* The number of hostnames in the above list. */
759 static int hostname_list_length
;
761 /* Whether or not HOSTNAME_LIST has been initialized. */
762 int hostname_list_initialized
= 0;
764 /* Initialize the hostname completion table. */
766 initialize_hostname_list ()
770 temp
= get_string_value ("HOSTFILE");
772 temp
= get_string_value ("hostname_completion_file");
774 temp
= DEFAULT_HOSTS_FILE
;
776 snarf_hosts_from_file (temp
);
779 hostname_list_initialized
++;
782 /* Add NAME to the list of hosts. */
787 if (hostname_list_length
+ 2 > hostname_list_size
)
789 hostname_list_size
= (hostname_list_size
+ 32) - (hostname_list_size
% 32);
790 hostname_list
= strvec_resize (hostname_list
, hostname_list_size
);
793 hostname_list
[hostname_list_length
++] = savestring (name
);
794 hostname_list
[hostname_list_length
] = (char *)NULL
;
797 #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
800 snarf_hosts_from_file (filename
)
804 char *temp
, buffer
[256], name
[256];
805 register int i
, start
;
807 file
= fopen (filename
, "r");
811 while (temp
= fgets (buffer
, 255, file
))
813 /* Skip to first character. */
814 for (i
= 0; buffer
[i
] && cr_whitespace (buffer
[i
]); i
++)
817 /* If comment or blank line, ignore. */
818 if (buffer
[i
] == '\0' || buffer
[i
] == '#')
821 /* If `preprocessor' directive, do the include. */
822 if (strncmp (buffer
+ i
, "$include ", 9) == 0)
826 /* Find start of filename. */
827 for (incfile
= buffer
+ i
+ 9; *incfile
&& whitespace (*incfile
); incfile
++)
830 /* Find end of filename. */
831 for (t
= incfile
; *t
&& cr_whitespace (*t
) == 0; t
++)
836 snarf_hosts_from_file (incfile
);
840 /* Skip internet address if present. */
841 if (DIGIT (buffer
[i
]))
842 for (; buffer
[i
] && cr_whitespace (buffer
[i
]) == 0; i
++);
844 /* Gobble up names. Each name is separated with whitespace. */
847 for (; cr_whitespace (buffer
[i
]); i
++)
849 if (buffer
[i
] == '\0' || buffer
[i
] == '#')
852 /* Isolate the current word. */
853 for (start
= i
; buffer
[i
] && cr_whitespace (buffer
[i
]) == 0; i
++)
857 strncpy (name
, buffer
+ start
, i
- start
);
858 name
[i
- start
] = '\0';
859 add_host_name (name
);
865 /* Return the hostname list. */
869 if (hostname_list_initialized
== 0)
870 initialize_hostname_list ();
871 return (hostname_list
);
875 clear_hostname_list ()
879 if (hostname_list_initialized
== 0)
881 for (i
= 0; i
< hostname_list_length
; i
++)
882 free (hostname_list
[i
]);
883 hostname_list_length
= hostname_list_initialized
= 0;
886 /* Return a NULL terminated list of hostnames which begin with TEXT.
887 Initialize the hostname list the first time if necessary.
888 The array is malloc ()'ed, but not the individual strings. */
890 hostnames_matching (text
)
893 register int i
, len
, nmatch
, rsize
;
896 if (hostname_list_initialized
== 0)
897 initialize_hostname_list ();
899 if (hostname_list_initialized
== 0)
900 return ((char **)NULL
);
902 /* Special case. If TEXT consists of nothing, then the whole list is
906 result
= strvec_create (1 + hostname_list_length
);
907 for (i
= 0; i
< hostname_list_length
; i
++)
908 result
[i
] = hostname_list
[i
];
909 result
[i
] = (char *)NULL
;
913 /* Scan until found, or failure. */
915 result
= (char **)NULL
;
916 for (i
= nmatch
= rsize
= 0; i
< hostname_list_length
; i
++)
918 if (STREQN (text
, hostname_list
[i
], len
) == 0)
921 /* OK, it matches. Add it to the list. */
922 if (nmatch
>= (rsize
- 1))
924 rsize
= (rsize
+ 16) - (rsize
% 16);
925 result
= strvec_resize (result
, rsize
);
928 result
[nmatch
++] = hostname_list
[i
];
931 result
[nmatch
] = (char *)NULL
;
935 /* This vi mode command causes VI_EDIT_COMMAND to be run on the current
936 command being entered (if no explicit argument is given), otherwise on
937 a command from the history file. */
939 #define VI_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-vi}}\""
940 #define EMACS_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-emacs}}\""
941 #define POSIX_VI_EDIT_COMMAND "fc -e vi"
944 edit_and_execute_command (count
, c
, editing_mode
, edit_command
)
945 int count
, c
, editing_mode
;
948 char *command
, *metaval
;
949 int r
, rrs
, metaflag
;
950 sh_parser_state_t ps
;
952 rrs
= rl_readline_state
;
953 saved_command_line_count
= current_command_line_count
;
955 /* Accept the current line. */
960 command
= (char *)xmalloc (strlen (edit_command
) + 8);
961 sprintf (command
, "%s %d", edit_command
, count
);
965 /* Take the command we were just editing, add it to the history file,
966 then call fc to operate on it. We have to add a dummy command to
967 the end of the history because fc ignores the last command (assumes
968 it's supposed to deal with the command before the `fc'). */
969 /* This breaks down when using command-oriented history and are not
970 finished with the command, so we should not ignore the last command */
972 current_command_line_count
++; /* for rl_newline above */
973 bash_add_history (rl_line_buffer
);
974 current_command_line_count
= 0; /* for dummy history entry */
975 bash_add_history ("");
976 history_lines_this_session
++;
978 command
= savestring (edit_command
);
981 metaval
= rl_variable_value ("input-meta");
982 metaflag
= RL_BOOLEAN_VARIABLE_VALUE (metaval
);
984 if (rl_deprep_term_function
)
985 (*rl_deprep_term_function
) ();
987 save_parser_state (&ps
);
988 r
= parse_and_execute (command
, (editing_mode
== VI_EDITING_MODE
) ? "v" : "C-xC-e", SEVAL_NOHIST
);
989 restore_parser_state (&ps
);
991 /* if some kind of reset_parser was called, undo it. */
992 reset_readahead_token ();
994 if (rl_prep_term_function
)
995 (*rl_prep_term_function
) (metaflag
);
998 current_command_line_count
= saved_command_line_count
;
1000 /* Now erase the contents of the current line and undo the effects of the
1001 rl_accept_line() above. We don't even want to make the text we just
1002 executed available for undoing. */
1003 rl_line_buffer
[0] = '\0'; /* XXX */
1004 rl_point
= rl_end
= 0;
1006 rl_readline_state
= rrs
;
1008 #if defined (VI_MODE)
1009 if (editing_mode
== VI_EDITING_MODE
)
1010 rl_vi_insertion_mode (1, c
);
1013 rl_forced_update_display ();
1018 #if defined (VI_MODE)
1020 vi_edit_and_execute_command (count
, c
)
1023 if (posixly_correct
)
1024 return (edit_and_execute_command (count
, c
, VI_EDITING_MODE
, POSIX_VI_EDIT_COMMAND
));
1026 return (edit_and_execute_command (count
, c
, VI_EDITING_MODE
, VI_EDIT_COMMAND
));
1028 #endif /* VI_MODE */
1031 emacs_edit_and_execute_command (count
, c
)
1034 return (edit_and_execute_command (count
, c
, EMACS_EDITING_MODE
, EMACS_EDIT_COMMAND
));
1039 posix_edit_macros (count
, key
)
1043 char alias_name
[3], *alias_value
, *macro
;
1048 alias_name
[0] = '_';
1050 alias_name
[2] = '\0';
1052 alias_value
= get_alias_value (alias_name
);
1053 if (alias_value
&& *alias_value
)
1055 macro
= savestring (alias_value
);
1056 rl_push_macro_input (macro
);
1062 /* Bindable commands that move `shell-words': that is, sequences of
1063 non-unquoted-metacharacters. */
1065 #define WORDDELIM(c) (shellmeta(c) || shellblank(c))
1068 bash_forward_shellword (count
, key
)
1076 return (bash_backward_shellword (-count
, key
));
1078 /* The tricky part of this is deciding whether or not the first character
1079 we're on is an unquoted metacharacter. Not completely handled yet. */
1080 /* XXX - need to test this stuff with backslash-escaped shell
1081 metacharacters and unclosed single- and double-quoted strings. */
1094 /* Are we in a quoted string? If we are, move to the end of the quoted
1095 string and continue the outer loop. We only want quoted strings, not
1096 backslash-escaped characters, but char_is_quoted doesn't
1098 if (char_is_quoted (rl_line_buffer
, p
) && p
> 0 && rl_line_buffer
[p
-1] != '\\')
1101 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1102 while (p
< rl_end
&& char_is_quoted (rl_line_buffer
, p
));
1107 /* Rest of code assumes we are not in a quoted string. */
1108 /* Move forward until we hit a non-metacharacter. */
1109 while (p
< rl_end
&& (c
= rl_line_buffer
[p
]) && WORDDELIM (c
))
1114 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1115 continue; /* straight back to loop, don't increment p */
1117 if (p
< rl_end
&& rl_line_buffer
[p
])
1118 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1121 p
= skip_to_delim (rl_line_buffer
, ++p
, "'", SD_NOJMP
);
1124 p
= skip_to_delim (rl_line_buffer
, ++p
, "\"", SD_NOJMP
);
1132 if (rl_line_buffer
[p
] == 0 || p
== rl_end
)
1139 /* Now move forward until we hit a non-quoted metacharacter or EOL */
1140 while (p
< rl_end
&& (c
= rl_line_buffer
[p
]) && WORDDELIM (c
) == 0)
1145 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1146 continue; /* straight back to loop, don't increment p */
1148 if (p
< rl_end
&& rl_line_buffer
[p
])
1149 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1152 p
= skip_to_delim (rl_line_buffer
, ++p
, "'", SD_NOJMP
);
1155 p
= skip_to_delim (rl_line_buffer
, ++p
, "\"", SD_NOJMP
);
1163 if (p
== rl_end
|| rl_line_buffer
[p
] == 0)
1177 bash_backward_shellword (count
, key
)
1185 return (bash_forward_shellword (-count
, key
));
1198 /* Move backward until we hit a non-metacharacter. We want to deal
1199 with the characters before point, so we move off a word if we're
1200 at its first character. */
1201 BACKUP_CHAR (rl_line_buffer
, slen
, p
);
1204 c
= rl_line_buffer
[p
];
1205 if (WORDDELIM (c
) == 0 || char_is_quoted (rl_line_buffer
, p
))
1207 BACKUP_CHAR (rl_line_buffer
, slen
, p
);
1216 /* Now move backward until we hit a metacharacter or BOL. Leave point
1217 at the start of the shellword or at BOL. */
1221 c
= rl_line_buffer
[p
];
1222 if (WORDDELIM (c
) && char_is_quoted (rl_line_buffer
, p
) == 0)
1228 BACKUP_CHAR (rl_line_buffer
, slen
, p
);
1239 bash_kill_shellword (count
, key
)
1245 return (bash_backward_kill_shellword (-count
, key
));
1248 bash_forward_shellword (count
, key
);
1251 rl_kill_text (p
, rl_point
);
1254 if (rl_editing_mode
== EMACS_EDITING_MODE
) /* 1 == emacs_mode */
1261 bash_backward_kill_shellword (count
, key
)
1267 return (bash_kill_shellword (-count
, key
));
1270 bash_backward_shellword (count
, key
);
1273 rl_kill_text (p
, rl_point
);
1275 if (rl_editing_mode
== EMACS_EDITING_MODE
) /* 1 == emacs_mode */
1282 bash_transpose_shellwords (count
, key
)
1285 char *word1
, *word2
;
1286 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1287 int orig_point
= rl_point
;
1292 /* Find the two shell words. */
1293 bash_forward_shellword (count
, key
);
1295 bash_backward_shellword (1, key
);
1297 bash_backward_shellword (count
, key
);
1299 bash_forward_shellword (1, key
);
1302 /* check that there really are two words. */
1303 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1306 rl_point
= orig_point
;
1310 /* Get the text of the words. */
1311 word1
= rl_copy_text (w1_beg
, w1_end
);
1312 word2
= rl_copy_text (w2_beg
, w2_end
);
1314 /* We are about to do many insertions and deletions. Remember them
1315 as one operation. */
1316 rl_begin_undo_group ();
1318 /* Do the stuff at word2 first, so that we don't have to worry
1319 about word1 moving. */
1321 rl_delete_text (w2_beg
, w2_end
);
1322 rl_insert_text (word1
);
1325 rl_delete_text (w1_beg
, w1_end
);
1326 rl_insert_text (word2
);
1328 /* This is exactly correct since the text before this point has not
1329 changed in length. */
1332 /* I think that does it. */
1333 rl_end_undo_group ();
1340 /* Directory name spelling correction on the current word (not shellword).
1341 COUNT > 1 is not exactly correct yet. */
1343 bash_spell_correct_shellword (count
, key
)
1346 int opoint
, wbeg
, wend
;
1347 char *text
, *newdir
;
1352 bash_backward_shellword (1, key
);
1354 bash_forward_shellword (1, key
);
1360 text
= rl_copy_text (wbeg
, wend
);
1362 newdir
= dirspell (text
);
1365 rl_begin_undo_group ();
1366 rl_delete_text (wbeg
, wend
);
1369 rl_insert_text (newdir
);
1371 rl_end_undo_group ();
1377 if (rl_point
>= rl_end
)
1383 bash_forward_shellword (1, key
); /* XXX */
1389 /* **************************************************************** */
1391 /* How To Do Shell Completion */
1393 /* **************************************************************** */
1395 #define COMMAND_SEPARATORS ";|&{(`"
1397 #define COMMAND_SEPARATORS_PLUS_WS ";|&{(` \t"
1400 /* check for redirections and other character combinations that are not
1401 command separators */
1406 register int this_char
, prev_char
;
1408 /* Handle the two character tokens `>&', `<&', and `>|'.
1409 We are not in a command position after one of these. */
1410 this_char
= rl_line_buffer
[ti
];
1411 prev_char
= (ti
> 0) ? rl_line_buffer
[ti
- 1] : 0;
1413 if ((this_char
== '&' && (prev_char
== '<' || prev_char
== '>')) ||
1414 (this_char
== '|' && prev_char
== '>'))
1416 else if (this_char
== '{' && prev_char
== '$') /*}*/
1419 else if (this_char
== '(' && prev_char
== '$') /*)*/
1421 else if (this_char
== '(' && prev_char
== '<') /*)*/
1423 #if defined (EXTENDED_GLOB)
1424 else if (extended_glob
&& this_char
== '(' && prev_char
== '!') /*)*/
1428 else if (char_is_quoted (rl_line_buffer
, ti
))
1433 #if defined (PROGRAMMABLE_COMPLETION)
1435 * XXX - because of the <= start test, and setting os = s+1, this can
1436 * potentially return os > start. This is probably not what we want to
1437 * happen, but fix later after 2.05a-release.
1440 find_cmd_start (start
)
1443 register int s
, os
, ns
;
1446 /* Flags == SD_NOJMP only because we want to skip over command substitutions
1447 in assignment statements. Have to test whether this affects `standalone'
1448 command substitutions as individual words. */
1449 while (((s
= skip_to_delim (rl_line_buffer
, os
, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
/*|SD_NOSKIPCMD*/)) <= start
) &&
1452 /* Handle >| token crudely; treat as > not | */
1453 if (s
> 0 && rl_line_buffer
[s
] == '|' && rl_line_buffer
[s
-1] == '>')
1455 ns
= skip_to_delim (rl_line_buffer
, s
+1, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
/*|SD_NOSKIPCMD*/);
1456 if (ns
> start
|| rl_line_buffer
[ns
] == 0)
1461 /* The only reserved word in COMMAND_SEPARATORS is `{', so handle that
1462 specially, making sure it's in a spot acceptable for reserved words */
1463 if (s
>= os
&& rl_line_buffer
[s
] == '{')
1465 int pc
, nc
; /* index of previous non-whitespace, next char */
1466 for (pc
= (s
> os
) ? s
- 1 : os
; pc
> os
&& whitespace(rl_line_buffer
[pc
]); pc
--)
1468 nc
= rl_line_buffer
[s
+1];
1469 /* must be preceded by a command separator or be the first non-
1470 whitespace character since the last command separator, and
1471 followed by a shell break character (not another `{') to be a reserved word. */
1472 if ((pc
> os
&& (rl_line_buffer
[s
-1] == '{' || strchr (COMMAND_SEPARATORS
, rl_line_buffer
[pc
]) == 0)) ||
1473 (shellbreak(nc
) == 0)) /* }} */
1475 /* Not a reserved word, look for another delim */
1476 ns
= skip_to_delim (rl_line_buffer
, s
+1, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
/*|SD_NOSKIPCMD*/);
1477 if (ns
> start
|| rl_line_buffer
[ns
] == 0)
1494 e
= skip_to_delim (rl_line_buffer
, end
, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
);
1499 find_cmd_name (start
, sp
, ep
)
1506 for (s
= start
; whitespace (rl_line_buffer
[s
]); s
++)
1509 /* skip until a shell break character */
1510 e
= skip_to_delim (rl_line_buffer
, s
, "()<>;&| \t\n", SD_NOJMP
|SD_COMPLETE
);
1512 name
= substring (rl_line_buffer
, s
, e
);
1523 prog_complete_return (text
, matchnum
)
1532 if (prog_complete_matches
== 0 || prog_complete_matches
[ind
] == 0)
1533 return (char *)NULL
;
1534 return (prog_complete_matches
[ind
++]);
1537 #endif /* PROGRAMMABLE_COMPLETION */
1539 /* Try and catch completion attempts that are syntax errors or otherwise
1542 invalid_completion (text
, ind
)
1548 /* If we don't catch these here, the next clause will */
1549 if (ind
> 0 && rl_line_buffer
[ind
] == '(' && /*)*/
1550 member (rl_line_buffer
[ind
-1], "$<>"))
1554 while (pind
> 0 && whitespace (rl_line_buffer
[pind
]))
1556 /* If we have only whitespace preceding a paren, it's valid */
1557 if (ind
>= 0 && pind
<= 0 && rl_line_buffer
[ind
] == '(') /*)*/
1559 /* Flag the invalid completions, which are mostly syntax errors */
1560 if (ind
> 0 && rl_line_buffer
[ind
] == '(' && /*)*/
1561 member (rl_line_buffer
[pind
], COMMAND_SEPARATORS
) == 0)
1567 /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
1568 at START and END. Return an array of matches, or NULL if none. */
1570 attempt_shell_completion (text
, start
, end
)
1574 int in_command_position
, ti
, qc
, dflags
;
1575 char **matches
, *command_separator_chars
;
1576 #if defined (PROGRAMMABLE_COMPLETION)
1577 int have_progcomps
, was_assignment
;
1578 COMPSPEC
*iw_compspec
;
1581 command_separator_chars
= COMMAND_SEPARATORS
;
1582 matches
= (char **)NULL
;
1583 rl_ignore_some_completions_function
= filename_completion_ignore
;
1585 complete_fullquote
= 1; /* full filename quoting by default */
1586 rl_filename_quote_characters
= default_filename_quote_characters
;
1587 set_filename_bstab (rl_filename_quote_characters
);
1588 set_directory_hook ();
1589 rl_filename_stat_hook
= bash_filename_stat_hook
;
1591 rl_sort_completion_matches
= 1; /* sort by default */
1593 /* Determine if this could be a command word. It is if it appears at
1594 the start of the line (ignoring preceding whitespace), or if it
1595 appears after a character that separates commands. It cannot be a
1596 command word if we aren't at the top-level prompt. */
1600 while ((ti
> -1) && (whitespace (rl_line_buffer
[ti
])))
1604 /* If this is an open quote, maybe we're trying to complete a quoted
1606 if (ti
>= 0 && (rl_line_buffer
[ti
] == '"' || rl_line_buffer
[ti
] == '\''))
1608 qc
= rl_line_buffer
[ti
];
1610 while (ti
> -1 && (whitespace (rl_line_buffer
[ti
])))
1615 in_command_position
= 0;
1618 /* Only do command completion at the start of a line when we
1619 are prompting at the top level. */
1620 if (current_prompt_string
== ps1_prompt
)
1621 in_command_position
++;
1622 else if (parser_in_command_position ())
1623 in_command_position
++;
1625 else if (member (rl_line_buffer
[ti
], command_separator_chars
))
1627 in_command_position
++;
1629 if (check_redir (ti
) == 1)
1630 in_command_position
= -1; /* sentinel that we're not the first word on the line */
1634 /* This still could be in command position. It is possible
1635 that all of the previous words on the line are variable
1639 if (in_command_position
> 0 && invalid_completion (text
, ti
))
1641 rl_attempted_completion_over
= 1;
1642 return ((char **)NULL
);
1645 /* Check that we haven't incorrectly flagged a closed command substitution
1646 as indicating we're in a command position. */
1647 if (in_command_position
> 0 && ti
>= 0 && rl_line_buffer
[ti
] == '`' &&
1648 *text
!= '`' && unclosed_pair (rl_line_buffer
, end
, "`") == 0)
1649 in_command_position
= -1; /* not following a command separator */
1651 /* Special handling for command substitution. If *TEXT is a backquote,
1652 it can be the start or end of an old-style command substitution, or
1653 unmatched. If it's unmatched, both calls to unclosed_pair will
1654 succeed. Don't bother if readline found a single quote and we are
1655 completing on the substring. */
1656 if (*text
== '`' && rl_completion_quote_character
!= '\'' &&
1657 (in_command_position
> 0 || (unclosed_pair (rl_line_buffer
, start
, "`") &&
1658 unclosed_pair (rl_line_buffer
, end
, "`"))))
1659 matches
= rl_completion_matches (text
, command_subst_completion_function
);
1661 #if defined (PROGRAMMABLE_COMPLETION)
1662 /* Attempt programmable completion. */
1663 have_progcomps
= prog_completion_enabled
&& (progcomp_size () > 0);
1664 iw_compspec
= progcomp_search (INITIALWORD
);
1666 (in_command_position
== 0 || text
[0] == '\0' || (in_command_position
> 0 && iw_compspec
)) &&
1667 current_prompt_string
== ps1_prompt
)
1669 int s
, e
, s1
, e1
, os
, foundcs
;
1672 /* XXX - don't free the members */
1673 if (prog_complete_matches
)
1674 free (prog_complete_matches
);
1675 prog_complete_matches
= (char **)NULL
;
1680 s
= find_cmd_start (os
);
1681 e
= find_cmd_end (end
);
1684 /* Don't read past the end of rl_line_buffer */
1690 /* Or past point if point is within an assignment statement */
1691 else if (was_assignment
&& s
> rl_point
)
1696 /* Skip over assignment statements preceding a command name. If we
1697 don't find a command name at all, we can perform command name
1698 completion. If we find a partial command name, we should perform
1699 command name completion on it. */
1701 n
= find_cmd_name (s
, &s1
, &e1
);
1704 while (was_assignment
= assignment (n
, 0));
1705 s
= s1
; /* reset to index where name begins */
1707 /* s == index of where command name begins (reset above)
1708 e == end of current command, may be end of line
1709 s1 = index of where command name begins
1710 e1 == index of where command name ends
1711 start == index of where word to be completed begins
1712 end == index of where word to be completed ends
1713 if (s == start) we are doing command word completion for sure
1714 if (e1 == end) we are at the end of the command name and completing it */
1715 if (start
== 0 && end
== 0 && e
!= 0 && text
[0] == '\0') /* beginning of non-empty line */
1717 else if (start
== end
&& start
== s1
&& e
!= 0 && e1
> end
) /* beginning of command name, leading whitespace */
1719 else if (e
== 0 && e
== s
&& text
[0] == '\0' && have_progcomps
) /* beginning of empty line */
1720 prog_complete_matches
= programmable_completions (EMPTYCMD
, text
, s
, e
, &foundcs
);
1721 else if (start
== end
&& text
[0] == '\0' && s1
> start
&& whitespace (rl_line_buffer
[start
]))
1722 foundcs
= 0; /* whitespace before command name */
1723 else if (e
> s
&& was_assignment
== 0 && e1
== end
&& rl_line_buffer
[e
] == 0 && whitespace (rl_line_buffer
[e
-1]) == 0)
1725 /* not assignment statement, but still want to perform command
1726 completion if we are composing command word. */
1728 in_command_position
= s
== start
&& STREQ (n
, text
); /* XXX */
1730 else if (e
> s
&& was_assignment
== 0 && have_progcomps
)
1732 prog_complete_matches
= programmable_completions (n
, text
, s
, e
, &foundcs
);
1733 /* command completion if programmable completion fails */
1734 /* If we have a completion for the initial word, we can prefer that */
1735 in_command_position
= s
== start
&& (iw_compspec
|| STREQ (n
, text
)); /* XXX */
1736 if (iw_compspec
&& in_command_position
)
1739 /* empty command name following command separator */
1740 else if (s
>= e
&& n
[0] == '\0' && text
[0] == '\0' && start
> 0 &&
1741 was_assignment
== 0 && member (rl_line_buffer
[start
-1], COMMAND_SEPARATORS
))
1744 in_command_position
= 1;
1746 else if (s
>= e
&& n
[0] == '\0' && text
[0] == '\0' && start
> 0)
1748 foundcs
= 0; /* empty command name following optional assignments */
1749 in_command_position
+= was_assignment
;
1751 else if (s
== start
&& e
== end
&& STREQ (n
, text
) && start
> 0)
1753 foundcs
= 0; /* partial command name following assignments */
1754 in_command_position
= 1;
1759 /* If we have defined a compspec for the initial (command) word, call
1760 it and process the results like any other programmable completion. */
1761 if (in_command_position
&& have_progcomps
&& foundcs
== 0 && iw_compspec
)
1762 prog_complete_matches
= programmable_completions (INITIALWORD
, text
, s
, e
, &foundcs
);
1765 /* XXX - if we found a COMPSPEC for the command, just return whatever
1766 the programmable completion code returns, and disable the default
1767 filename completion that readline will do unless the COPT_DEFAULT
1768 option has been set with the `-o default' option to complete or
1772 pcomp_set_readline_variables (foundcs
, 1);
1773 /* Turn what the programmable completion code returns into what
1774 readline wants. I should have made compute_lcd_of_matches
1776 matches
= rl_completion_matches (text
, prog_complete_return
);
1777 if ((foundcs
& COPT_DEFAULT
) == 0)
1778 rl_attempted_completion_over
= 1; /* no default */
1779 if (matches
|| ((foundcs
& COPT_BASHDEFAULT
) == 0))
1788 if (in_command_position
> 0)
1789 dflags
|= DEFCOMP_CMDPOS
;
1790 matches
= bash_default_completion (text
, start
, end
, qc
, dflags
);
1797 bash_default_completion (text
, start
, end
, qc
, compflags
)
1799 int start
, end
, qc
, compflags
;
1803 matches
= (char **)NULL
;
1805 /* New posix-style command substitution or variable name? */
1808 if (qc
!= '\'' && text
[1] == '(') /* ) */
1809 matches
= rl_completion_matches (text
, command_subst_completion_function
);
1812 matches
= rl_completion_matches (text
, variable_completion_function
);
1813 /* If a single match, see if it expands to a directory name and append
1814 a slash if it does. This requires us to expand the variable name,
1815 so we don't want to display errors if the variable is unset. This
1816 can happen with dynamic variables whose value has never been
1818 if (matches
&& matches
[0] && matches
[1] == 0)
1820 t
= savestring (matches
[0]);
1821 bash_filename_stat_hook (&t
);
1822 /* doesn't use test_for_directory because that performs tilde
1825 rl_completion_append_character
= '/';
1831 /* If the word starts in `~', and there is no slash in the word, then
1832 try completing this word as a username. */
1833 if (matches
== 0 && *text
== '~' && mbschr (text
, '/') == 0)
1834 matches
= rl_completion_matches (text
, rl_username_completion_function
);
1836 /* Another one. Why not? If the word starts in '@', then look through
1837 the world of known hostnames for completion first. */
1838 if (matches
== 0 && perform_hostname_completion
&& *text
== '@')
1839 matches
= rl_completion_matches (text
, hostname_completion_function
);
1841 /* And last, (but not least) if this word is in a command position, then
1842 complete over possible command names, including aliases, functions,
1843 and command names. */
1844 if (matches
== 0 && (compflags
& DEFCOMP_CMDPOS
))
1846 /* If END == START and text[0] == 0, we are trying to complete an empty
1848 if (no_empty_command_completion
&& end
== start
&& text
[0] == '\0')
1850 matches
= (char **)NULL
;
1851 rl_ignore_some_completions_function
= bash_ignore_everything
;
1855 #define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x))
1858 matches
= rl_completion_matches (text
, command_word_completion_function
);
1860 /* If we are attempting command completion and nothing matches, we
1861 do not want readline to perform filename completion for us. We
1862 still want to be able to complete partial pathnames, so set the
1863 completion ignore function to something which will remove
1864 filenames and leave directories in the match list. */
1865 if (matches
== (char **)NULL
)
1866 rl_ignore_some_completions_function
= bash_ignore_filenames
;
1867 else if (matches
[1] == 0 && CMD_IS_DIR(matches
[0]) && dot_in_path
== 0)
1868 /* If we found a single match, without looking in the current
1869 directory (because it's not in $PATH), but the found name is
1870 also a command in the current directory, suppress appending any
1871 terminating character, since it's ambiguous. */
1873 rl_completion_suppress_append
= 1;
1874 rl_filename_completion_desired
= 0;
1876 else if (matches
[0] && matches
[1] && STREQ (matches
[0], matches
[1]) && CMD_IS_DIR (matches
[0]))
1877 /* There are multiple instances of the same match (duplicate
1878 completions haven't yet been removed). In this case, all of
1879 the matches will be the same, and the duplicate removal code
1880 will distill them all down to one. We turn on
1881 rl_completion_suppress_append for the same reason as above.
1882 Remember: we only care if there's eventually a single unique
1883 completion. If there are multiple completions this won't
1884 make a difference and the problem won't occur. */
1886 rl_completion_suppress_append
= 1;
1887 rl_filename_completion_desired
= 0;
1892 /* This could be a globbing pattern, so try to expand it using pathname
1894 if (!matches
&& completion_glob_pattern ((char *)text
))
1896 matches
= rl_completion_matches (text
, glob_complete_word
);
1897 /* A glob expression that matches more than one filename is problematic.
1898 If we match more than one filename, punt. */
1899 if (matches
&& matches
[1] && rl_completion_type
== TAB
)
1901 strvec_dispose (matches
);
1902 matches
= (char **)0;
1904 else if (matches
&& matches
[1] && rl_completion_type
== '!')
1906 rl_completion_suppress_append
= 1;
1907 rl_filename_completion_desired
= 0;
1915 bash_command_name_stat_hook (name
)
1918 char *cname
, *result
;
1920 /* If it's not something we're going to look up in $PATH, just call the
1921 normal filename stat hook. */
1922 if (absolute_program (*name
))
1923 return (bash_filename_stat_hook (name
));
1926 /* XXX - we could do something here with converting aliases, builtins,
1927 and functions into something that came out as executable, but we don't. */
1928 result
= search_for_command (cname
, 0);
1938 executable_completion (filename
, searching_path
)
1939 const char *filename
;
1945 /* This gets an unquoted filename, so we need to quote special characters
1946 in the filename before the completion hook gets it. */
1948 f
= savestring (filename
);
1951 f
= bash_quote_filename ((char *)filename
, SINGLE_MATCH
, &c
);
1953 bash_directory_completion_hook (&f
);
1955 r
= searching_path
? executable_file (f
) : executable_or_directory (f
);
1960 /* This is the function to call when the word to complete is in a position
1961 where a command word can be found. It grovels $PATH, looking for commands
1962 that match. It also scans aliases, function names, and the shell_builtin
1965 command_word_completion_function (hint_text
, state
)
1966 const char *hint_text
;
1969 static char *hint
= (char *)NULL
;
1970 static char *path
= (char *)NULL
;
1971 static char *val
= (char *)NULL
;
1972 static char *filename_hint
= (char *)NULL
;
1973 static char *fnhint
= (char *)NULL
;
1974 static char *dequoted_hint
= (char *)NULL
;
1975 static char *directory_part
= (char *)NULL
;
1976 static char **glob_matches
= (char **)NULL
;
1977 static int path_index
, hint_len
, istate
, igncase
;
1978 static int mapping_over
, local_index
, searching_path
, hint_is_dir
;
1979 static int old_glob_ignore_case
, globpat
;
1980 static SHELL_VAR
**varlist
= (SHELL_VAR
**)NULL
;
1982 static alias_t
**alias_list
= (alias_t
**)NULL
;
1986 /* We have to map over the possibilities for command words. If we have
1987 no state, then make one just for that purpose. */
1990 rl_filename_stat_hook
= bash_command_name_stat_hook
;
1992 if (dequoted_hint
&& dequoted_hint
!= hint
)
1993 free (dequoted_hint
);
1997 mapping_over
= searching_path
= 0;
1998 hint_is_dir
= CMD_IS_DIR (hint_text
);
2001 temp
= rl_variable_value ("completion-ignore-case");
2002 igncase
= RL_BOOLEAN_VARIABLE_VALUE (temp
);
2004 old_glob_ignore_case
= glob_ignore_case
;
2008 free (glob_matches
);
2009 glob_matches
= (char **)NULL
;
2012 globpat
= completion_glob_pattern ((char *)hint_text
);
2014 /* If this is an absolute program name, do not check it against
2015 aliases, reserved words, functions or builtins. We must check
2016 whether or not it is unique, and, if so, whether that filename
2018 if (globpat
|| absolute_program (hint_text
))
2020 /* Perform tilde expansion on what's passed, so we don't end up
2021 passing filenames with tildes directly to stat(). The rest of
2022 the shell doesn't do variable expansion on the word following
2023 the tilde, so we don't do it here even if direxpand is set. */
2024 if (*hint_text
== '~')
2026 hint
= bash_tilde_expand (hint_text
, 0);
2027 directory_part
= savestring (hint_text
);
2028 temp
= strchr (directory_part
, '/');
2033 free (directory_part
);
2034 directory_part
= (char *)NULL
;
2037 else if (dircomplete_expand
)
2039 hint
= savestring (hint_text
);
2040 bash_directory_completion_hook (&hint
);
2043 hint
= savestring (hint_text
);
2045 dequoted_hint
= hint
;
2046 /* If readline's completer found a quote character somewhere, but
2047 didn't set the quote character, there must have been a quote
2048 character embedded in the filename. It can't be at the start of
2049 the filename, so we need to dequote the filename before we look
2050 in the file system for it. */
2051 if (rl_completion_found_quote
&& rl_completion_quote_character
== 0)
2053 dequoted_hint
= bash_dequote_filename (hint
, 0);
2055 hint
= dequoted_hint
;
2057 hint_len
= strlen (hint
);
2060 free (filename_hint
);
2062 fnhint
= filename_hint
= savestring (hint
);
2073 if (dircomplete_expand
&& path_dot_or_dotdot (filename_hint
))
2075 dircomplete_expand
= 0;
2076 set_directory_hook ();
2077 dircomplete_expand
= 1;
2084 dequoted_hint
= hint
= savestring (hint_text
);
2085 hint_len
= strlen (hint
);
2087 if (rl_completion_found_quote
&& rl_completion_quote_character
== 0)
2088 dequoted_hint
= bash_dequote_filename (hint
, 0);
2090 path
= get_string_value ("PATH");
2091 path_index
= dot_in_path
= 0;
2093 /* Initialize the variables for each type of command word. */
2099 varlist
= all_visible_functions ();
2105 alias_list
= all_aliases ();
2109 /* mapping_over says what we are currently hacking. Note that every case
2110 in this list must fall through when there are no more possibilities. */
2112 switch (mapping_over
)
2114 case 0: /* Aliases come first. */
2116 while (alias_list
&& alias_list
[local_index
])
2118 register char *alias
;
2120 alias
= alias_list
[local_index
++]->name
;
2122 if (igncase
== 0 && (STREQN (alias
, hint
, hint_len
)))
2123 return (savestring (alias
));
2124 else if (igncase
&& strncasecmp (alias
, hint
, hint_len
) == 0)
2125 return (savestring (alias
));
2131 case 1: /* Then shell reserved words. */
2133 while (word_token_alist
[local_index
].word
)
2135 register char *reserved_word
;
2137 reserved_word
= word_token_alist
[local_index
++].word
;
2139 if (STREQN (reserved_word
, hint
, hint_len
))
2140 return (savestring (reserved_word
));
2146 case 2: /* Then function names. */
2147 while (varlist
&& varlist
[local_index
])
2149 register char *varname
;
2151 varname
= varlist
[local_index
++]->name
;
2153 /* Honor completion-ignore-case for shell function names. */
2154 if (igncase
== 0 && (STREQN (varname
, hint
, hint_len
)))
2155 return (savestring (varname
));
2156 else if (igncase
&& strncasecmp (varname
, hint
, hint_len
) == 0)
2157 return (savestring (varname
));
2162 case 3: /* Then shell builtins. */
2163 for (; local_index
< num_shell_builtins
; local_index
++)
2165 /* Ignore it if it doesn't have a function pointer or if it
2166 is not currently enabled. */
2167 if (!shell_builtins
[local_index
].function
||
2168 (shell_builtins
[local_index
].flags
& BUILTIN_ENABLED
) == 0)
2171 if (STREQN (shell_builtins
[local_index
].name
, hint
, hint_len
))
2173 int i
= local_index
++;
2175 return (savestring (shell_builtins
[i
].name
));
2183 /* Limited support for completing command words with globbing chars. Only
2184 a single match (multiple matches that end up reducing the number of
2185 characters in the common prefix are bad) will ever be returned on
2186 regular completion. */
2191 rl_filename_completion_desired
= 1;
2193 glob_ignore_case
= igncase
;
2194 glob_matches
= shell_glob_filename (hint
, 0);
2195 glob_ignore_case
= old_glob_ignore_case
;
2197 if (GLOB_FAILED (glob_matches
) || glob_matches
== 0)
2199 glob_matches
= (char **)NULL
;
2200 return ((char *)NULL
);
2205 if (glob_matches
[1] && rl_completion_type
== TAB
) /* multiple matches are bad */
2206 return ((char *)NULL
);
2209 while (val
= glob_matches
[local_index
++])
2211 if (executable_or_directory (val
))
2213 if (*hint_text
== '~' && directory_part
)
2215 temp
= maybe_restore_tilde (val
, directory_part
);
2224 glob_ignore_case
= old_glob_ignore_case
;
2225 return ((char *)NULL
);
2228 /* If the text passed is a directory in the current directory, return it
2229 as a possible match. Executables in directories in the current
2230 directory can be specified using relative pathnames and successfully
2231 executed even when `.' is not in $PATH. */
2234 hint_is_dir
= 0; /* only return the hint text once */
2235 return (savestring (hint_text
));
2238 /* Repeatedly call filename_completion_function while we have
2239 members of PATH left. Question: should we stat each file?
2240 Answer: we call executable_file () on each file. */
2243 istate
= (val
!= (char *)NULL
);
2249 /* Get the next directory from the path. If there is none, then we
2251 if (path
== 0 || path
[path_index
] == 0 ||
2252 (current_path
= extract_colon_unit (path
, &path_index
)) == 0)
2253 return ((char *)NULL
);
2256 if (*current_path
== 0)
2258 free (current_path
);
2259 current_path
= savestring (".");
2262 if (*current_path
== '~')
2266 t
= bash_tilde_expand (current_path
, 0);
2267 free (current_path
);
2271 if (current_path
[0] == '.' && current_path
[1] == '\0')
2274 if (fnhint
&& fnhint
!= filename_hint
)
2277 free (filename_hint
);
2279 filename_hint
= sh_makepath (current_path
, hint
, 0);
2280 /* Need a quoted version (though it doesn't matter much in most
2281 cases) because rl_filename_completion_function dequotes the
2282 filename it gets, assuming that it's been quoted as part of
2283 the input line buffer. */
2284 if (strpbrk (filename_hint
, "\"'\\"))
2285 fnhint
= sh_backslash_quote (filename_hint
, filename_bstab
, 0);
2287 fnhint
= filename_hint
;
2288 free (current_path
); /* XXX */
2292 val
= rl_filename_completion_function (fnhint
, istate
);
2293 if (mapping_over
== 4 && dircomplete_expand
)
2294 set_directory_hook ();
2300 /* If the hint text is an absolute program, then don't bother
2301 searching through PATH. */
2302 if (absolute_program (hint
))
2303 return ((char *)NULL
);
2309 int match
, freetemp
;
2311 if (absolute_program (hint
))
2315 match
= strncmp (val
, hint
, hint_len
) == 0;
2317 match
= strncasecmp (val
, hint
, hint_len
) == 0;
2319 /* Why duplicate the comparison rl_filename_completion_function
2320 already performs? */
2324 /* If we performed tilde expansion, restore the original
2326 if (*hint_text
== '~')
2327 temp
= maybe_restore_tilde (val
, directory_part
);
2329 temp
= savestring (val
);
2334 temp
= strrchr (val
, '/');
2340 freetemp
= match
= strncmp (temp
, hint
, hint_len
) == 0;
2342 freetemp
= match
= strncasecmp (temp
, hint
, hint_len
) == 0;
2344 temp
= savestring (temp
);
2347 freetemp
= match
= 0;
2350 /* If we have found a match, and it is an executable file, return it.
2351 We don't return directory names when searching $PATH, since the
2352 bash execution code won't find executables in directories which
2353 appear in directories in $PATH when they're specified using
2354 relative pathnames. */
2356 /* If we're not searching $PATH and we have a relative pathname, we
2357 need to re-canonicalize it before testing whether or not it's an
2358 executable or a directory so the shell treats .. relative to $PWD
2359 according to the physical/logical option. The shell already
2360 canonicalizes the directory name in order to tell readline where
2361 to look, so not doing it here will be inconsistent. */
2362 /* XXX -- currently not used -- will introduce more inconsistency,
2363 since shell does not canonicalize ../foo before passing it to
2365 if (match
&& searching_path
== 0 && *val
== '.')
2369 t
= get_working_directory ("command-word-completion");
2370 t1
= make_absolute (val
, t
);
2372 cval
= sh_canonpath (t1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
2378 if (match
&& executable_completion ((searching_path
? val
: cval
), searching_path
))
2383 val
= ""; /* So it won't be NULL. */
2398 /* Completion inside an unterminated command substitution. */
2400 command_subst_completion_function (text
, state
)
2404 static char **matches
= (char **)NULL
;
2405 static const char *orig_start
;
2406 static char *filename_text
= (char *)NULL
;
2407 static int cmd_index
, start_len
;
2413 free (filename_text
);
2417 else if (*text
== '$' && text
[1] == '(') /* ) */
2419 /* If the text was quoted, suppress any quote character that the
2420 readline completion code would insert. */
2421 rl_completion_suppress_quote
= 1;
2422 start_len
= text
- orig_start
;
2423 filename_text
= savestring (text
);
2428 * At this point we can entertain the idea of re-parsing
2429 * `filename_text' into a (possibly incomplete) command name and
2430 * arguments, and doing completion based on that. This is
2431 * currently very rudimentary, but it is a small improvement.
2433 for (value
= filename_text
+ strlen (filename_text
) - 1; value
> filename_text
; value
--)
2434 if (whitespace (*value
) || member (*value
, COMMAND_SEPARATORS
))
2436 if (value
<= filename_text
)
2437 matches
= rl_completion_matches (filename_text
, command_word_completion_function
);
2441 start_len
+= value
- filename_text
;
2442 if (whitespace (value
[-1]))
2443 matches
= rl_completion_matches (value
, rl_filename_completion_function
);
2445 matches
= rl_completion_matches (value
, command_word_completion_function
);
2448 /* If there is more than one match, rl_completion_matches has already
2449 put the lcd in matches[0]. Skip over it. */
2450 cmd_index
= matches
&& matches
[0] && matches
[1];
2452 /* If there's a single match and it's a directory, set the append char
2453 to the expected `/'. Otherwise, don't append anything. */
2454 if (matches
&& matches
[0] && matches
[1] == 0 && test_for_directory (matches
[0]))
2455 rl_completion_append_character
= '/';
2457 rl_completion_suppress_append
= 1;
2460 if (matches
== 0 || matches
[cmd_index
] == 0)
2462 rl_filename_quoting_desired
= 0; /* disable quoting */
2463 return ((char *)NULL
);
2467 value
= (char *)xmalloc (1 + start_len
+ strlen (matches
[cmd_index
]));
2470 value
[0] = *orig_start
;
2472 strncpy (value
, orig_start
, start_len
);
2474 strcpy (value
+ start_len
, matches
[cmd_index
]);
2481 /* Okay, now we write the entry_function for variable completion. */
2483 variable_completion_function (text
, state
)
2487 static char **varlist
= (char **)NULL
;
2488 static int varlist_index
;
2489 static char *varname
= (char *)NULL
;
2490 static int first_char
, first_char_loc
;
2498 first_char
= text
[0];
2500 if (first_char
== '$')
2503 if (text
[first_char_loc
] == '{')
2506 varname
= savestring (text
+ first_char_loc
);
2509 strvec_dispose (varlist
);
2511 varlist
= all_variables_matching_prefix (varname
);
2515 if (!varlist
|| !varlist
[varlist_index
])
2517 return ((char *)NULL
);
2523 value
= (char *)xmalloc (4 + strlen (varlist
[varlist_index
]));
2527 value
[0] = first_char
;
2528 if (first_char_loc
== 2)
2532 strcpy (value
+ first_char_loc
, varlist
[varlist_index
]);
2533 if (first_char_loc
== 2)
2534 strcat (value
, "}");
2541 /* How about a completion function for hostnames? */
2543 hostname_completion_function (text
, state
)
2547 static char **list
= (char **)NULL
;
2548 static int list_index
= 0;
2549 static int first_char
, first_char_loc
;
2551 /* If we don't have any state, make some. */
2556 list
= (char **)NULL
;
2561 if (first_char
== '@')
2564 list
= hostnames_matching ((char *)text
+first_char_loc
);
2568 if (list
&& list
[list_index
])
2572 t
= (char *)xmalloc (2 + strlen (list
[list_index
]));
2574 strcpy (t
+ first_char_loc
, list
[list_index
]);
2579 return ((char *)NULL
);
2583 * A completion function for service names from /etc/services (or wherever).
2586 bash_servicename_completion_function (text
, state
)
2590 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT)
2591 return ((char *)NULL
);
2593 static char *sname
= (char *)NULL
;
2594 static struct servent
*srvent
;
2595 static int snamelen
;
2597 char **alist
, *aentry
;
2604 sname
= savestring (text
);
2605 snamelen
= strlen (sname
);
2609 while (srvent
= getservent ())
2612 if (snamelen
== 0 || (STREQN (sname
, srvent
->s_name
, snamelen
)))
2614 /* Not primary, check aliases */
2615 for (alist
= srvent
->s_aliases
; *alist
; alist
++)
2618 if (STREQN (sname
, aentry
, snamelen
))
2632 return ((char *)NULL
);
2635 value
= afound
? savestring (aentry
) : savestring (srvent
->s_name
);
2641 * A completion function for group names from /etc/group (or wherever).
2644 bash_groupname_completion_function (text
, state
)
2648 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
2649 return ((char *)NULL
);
2651 static char *gname
= (char *)NULL
;
2652 static struct group
*grent
;
2653 static int gnamelen
;
2659 gname
= savestring (text
);
2660 gnamelen
= strlen (gname
);
2665 while (grent
= getgrent ())
2667 if (gnamelen
== 0 || (STREQN (gname
, grent
->gr_name
, gnamelen
)))
2674 return ((char *)NULL
);
2677 value
= savestring (grent
->gr_name
);
2682 /* Functions to perform history and alias expansions on the current line. */
2684 #if defined (BANG_HISTORY)
2685 /* Perform history expansion on the current line. If no history expansion
2686 is done, pre_process_line() returns what it was passed, so we need to
2687 allocate a new line here. */
2689 history_expand_line_internal (line
)
2695 old_verify
= hist_verify
;
2697 new_line
= pre_process_line (line
, 0, 0);
2698 hist_verify
= old_verify
;
2700 return (new_line
== line
) ? savestring (line
) : new_line
;
2704 /* There was an error in expansion. Let the preprocessor print
2707 cleanup_expansion_error ()
2710 #if defined (BANG_HISTORY)
2713 old_verify
= hist_verify
;
2717 fprintf (rl_outstream
, "\r\n");
2718 to_free
= pre_process_line (rl_line_buffer
, 1, 0);
2719 #if defined (BANG_HISTORY)
2720 hist_verify
= old_verify
;
2722 if (to_free
!= rl_line_buffer
)
2724 putc ('\r', rl_outstream
);
2725 rl_forced_update_display ();
2728 /* If NEW_LINE differs from what is in the readline line buffer, add an
2729 undo record to get from the readline line buffer contents to the new
2730 line and make NEW_LINE the current readline line. */
2732 maybe_make_readline_line (new_line
)
2735 if (new_line
&& strcmp (new_line
, rl_line_buffer
) != 0)
2739 rl_add_undo (UNDO_BEGIN
, 0, 0, 0);
2740 rl_delete_text (0, rl_point
);
2741 rl_point
= rl_end
= rl_mark
= 0;
2742 rl_insert_text (new_line
);
2743 rl_add_undo (UNDO_END
, 0, 0, 0);
2747 /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
2749 set_up_new_line (new_line
)
2752 int old_point
, at_end
;
2754 old_point
= rl_point
;
2755 at_end
= rl_point
== rl_end
;
2757 /* If the line was history and alias expanded, then make that
2758 be one thing to undo. */
2759 maybe_make_readline_line (new_line
);
2762 /* Place rl_point where we think it should go. */
2765 else if (old_point
< rl_end
)
2767 rl_point
= old_point
;
2768 if (!whitespace (rl_line_buffer
[rl_point
]))
2769 rl_forward_word (1, 0);
2774 /* Expand aliases in the current readline line. */
2776 alias_expand_line (count
, ignore
)
2781 new_line
= alias_expand (rl_line_buffer
);
2785 set_up_new_line (new_line
);
2790 cleanup_expansion_error ();
2796 #if defined (BANG_HISTORY)
2797 /* History expand the line. */
2799 history_expand_line (count
, ignore
)
2804 new_line
= history_expand_line_internal (rl_line_buffer
);
2808 set_up_new_line (new_line
);
2813 cleanup_expansion_error ();
2818 /* Expand history substitutions in the current line and then insert a
2819 space (hopefully close to where we were before). */
2821 tcsh_magic_space (count
, ignore
)
2824 int dist_from_end
, old_point
;
2826 old_point
= rl_point
;
2827 dist_from_end
= rl_end
- rl_point
;
2828 if (history_expand_line (count
, ignore
) == 0)
2830 /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
2831 This works if all expansions were before rl_point or if no expansions
2833 rl_point
= (old_point
== 0) ? old_point
: rl_end
- dist_from_end
;
2840 #endif /* BANG_HISTORY */
2842 /* History and alias expand the line. */
2844 history_and_alias_expand_line (count
, ignore
)
2850 #if defined (BANG_HISTORY)
2851 new_line
= history_expand_line_internal (rl_line_buffer
);
2859 alias_line
= alias_expand (new_line
);
2861 new_line
= alias_line
;
2867 set_up_new_line (new_line
);
2872 cleanup_expansion_error ();
2877 /* History and alias expand the line, then perform the shell word
2878 expansions by calling expand_string. This can't use set_up_new_line()
2879 because we want the variable expansions as a separate undo'able
2880 set of operations. */
2882 shell_expand_line (count
, ignore
)
2886 WORD_LIST
*expanded_string
;
2890 #if defined (BANG_HISTORY)
2891 new_line
= history_expand_line_internal (rl_line_buffer
);
2894 t
= expand_string_dollar_quote (new_line
? new_line
: rl_line_buffer
, 0);
2903 alias_line
= alias_expand (new_line
);
2905 new_line
= alias_line
;
2911 int old_point
= rl_point
;
2912 int at_end
= rl_point
== rl_end
;
2914 /* If the line was history and alias expanded, then make that
2915 be one thing to undo. */
2916 maybe_make_readline_line (new_line
);
2919 /* If there is variable expansion to perform, do that as a separate
2920 operation to be undone. */
2923 w
= alloc_word_desc ();
2924 w
->word
= savestring (rl_line_buffer
);
2925 w
->flags
= rl_explicit_arg
? (W_NOPROCSUB
|W_NOCOMSUB
) : 0;
2926 expanded_string
= expand_word (w
, rl_explicit_arg
? Q_HERE_DOCUMENT
: 0);
2929 new_line
= savestring (rl_line_buffer
);
2930 expanded_string
= expand_string (new_line
, 0);
2934 if (expanded_string
== 0)
2936 new_line
= (char *)xmalloc (1);
2941 new_line
= string_list (expanded_string
);
2942 dispose_words (expanded_string
);
2945 maybe_make_readline_line (new_line
);
2948 /* Place rl_point where we think it should go. */
2951 else if (old_point
< rl_end
)
2953 rl_point
= old_point
;
2954 if (!whitespace (rl_line_buffer
[rl_point
]))
2955 rl_forward_word (1, 0);
2961 cleanup_expansion_error ();
2966 /* If FIGNORE is set, then don't match files with the given suffixes when
2967 completing filenames. If only one of the possibilities has an acceptable
2968 suffix, delete the others, else just return and let the completer
2969 signal an error. It is called by the completer when real
2970 completions are done on filenames by the completer's internal
2971 function, not for completion lists (M-?) and not on "other"
2972 completion types, such as hostnames or commands. */
2974 static struct ignorevar fignore
=
2980 (sh_iv_item_func_t
*) 0,
2984 _ignore_completion_names (names
, name_func
)
2986 sh_ignore_func_t
*name_func
;
2993 /* If there is only one completion, see if it is acceptable. If it is
2994 not, free it up. In any case, short-circuit and return. This is a
2995 special case because names[0] is not the prefix of the list of names
2996 if there is only one completion; it is the completion itself. */
2997 if (names
[1] == (char *)0)
3000 if ((*name_func
) (names
[0]) == 0)
3003 names
[0] = (char *)NULL
;
3009 /* Allocate space for array to hold list of pointers to matching
3010 filenames. The pointers are copied back to NAMES when done. */
3011 for (nidx
= 1; names
[nidx
]; nidx
++)
3013 newnames
= strvec_create (nidx
+ 1);
3015 if (force_fignore
== 0)
3017 oldnames
= strvec_create (nidx
- 1);
3021 newnames
[0] = names
[0];
3022 for (idx
= nidx
= 1; names
[idx
]; idx
++)
3024 if ((*name_func
) (names
[idx
]))
3025 newnames
[nidx
++] = names
[idx
];
3026 else if (force_fignore
== 0)
3027 oldnames
[oidx
++] = names
[idx
];
3032 newnames
[nidx
] = (char *)NULL
;
3034 /* If none are acceptable then let the completer handle it. */
3040 names
[0] = (char *)NULL
;
3049 if (force_fignore
== 0)
3052 free (oldnames
[--oidx
]);
3056 /* If only one is acceptable, copy it to names[0] and return. */
3060 names
[0] = newnames
[1];
3061 names
[1] = (char *)NULL
;
3066 /* Copy the acceptable names back to NAMES, set the new array end,
3068 for (nidx
= 1; newnames
[nidx
]; nidx
++)
3069 names
[nidx
] = newnames
[nidx
];
3070 names
[nidx
] = (char *)NULL
;
3075 name_is_acceptable (name
)
3081 for (nlen
= strlen (name
), p
= fignore
.ignores
; p
->val
; p
++)
3083 if (nlen
> p
->len
&& p
->len
> 0 && STREQ (p
->val
, &name
[nlen
- p
->len
]))
3092 ignore_dot_names (name
)
3095 return (name
[0] != '.');
3100 filename_completion_ignore (names
)
3104 if (glob_dot_filenames
== 0)
3105 _ignore_completion_names (names
, ignore_dot_names
);
3108 setup_ignore_patterns (&fignore
);
3110 if (fignore
.num_ignores
== 0)
3113 _ignore_completion_names (names
, name_is_acceptable
);
3118 /* Return 1 if NAME is a directory. NAME undergoes tilde expansion. */
3120 test_for_directory (name
)
3126 fn
= bash_tilde_expand (name
, 0);
3127 r
= file_isdir (fn
);
3134 test_for_canon_directory (name
)
3140 fn
= (*name
== '~') ? bash_tilde_expand (name
, 0) : savestring (name
);
3141 bash_filename_stat_hook (&fn
);
3142 r
= file_isdir (fn
);
3148 /* Remove files from NAMES, leaving directories. */
3150 bash_ignore_filenames (names
)
3153 _ignore_completion_names (names
, test_for_directory
);
3158 bash_progcomp_ignore_filenames (names
)
3161 _ignore_completion_names (names
, test_for_canon_directory
);
3173 bash_ignore_everything (names
)
3176 _ignore_completion_names (names
, return_zero
);
3180 /* Replace a tilde-prefix in VAL with a `~', assuming the user typed it. VAL
3181 is an expanded filename. DIRECTORY_PART is the tilde-prefix portion
3182 of the un-tilde-expanded version of VAL (what the user typed). */
3184 restore_tilde (val
, directory_part
)
3185 char *val
, *directory_part
;
3188 char *dh2
, *expdir
, *ret
, *v
;
3192 /* We need to duplicate the expansions readline performs on the directory
3193 portion before passing it to our completion function. */
3194 dh2
= directory_part
? bash_dequote_filename (directory_part
, 0) : 0;
3195 bash_directory_expansion (&dh2
);
3198 expdir
= bash_tilde_expand (directory_part
, 0);
3199 xl
= strlen (expdir
);
3200 if (*directory_part
== '~' && STREQ (directory_part
, expdir
))
3202 /* tilde expansion failed, so what should we return? we use what the
3204 v
= mbschr (val
, '/');
3206 ret
= (char *)xmalloc (xl
+ vl
+ 2);
3207 strcpy (ret
, directory_part
);
3209 strcpy (ret
+ xl
, v
);
3219 dh2 = unexpanded but dequoted tilde-prefix
3220 dl2 = length of tilde-prefix
3221 expdir = tilde-expanded tilde-prefix
3222 xl = length of expanded tilde-prefix
3223 l = length of remainder after tilde-prefix
3229 return (savestring (val
)); /* XXX - just punt */
3232 ret
= (char *)xmalloc (dl2
+ 2 + l
);
3234 strcpy (ret
+ dl2
, val
+ xl
);
3241 maybe_restore_tilde (val
, directory_part
)
3242 char *val
, *directory_part
;
3244 rl_icppfunc_t
*save
;
3247 save
= (dircomplete_expand
== 0) ? save_directory_hook () : (rl_icppfunc_t
*)0;
3248 ret
= restore_tilde (val
, directory_part
);
3250 restore_directory_hook (save
);
3254 /* Simulate the expansions that will be performed by
3255 rl_filename_completion_function. This must be called with the address of
3256 a pointer to malloc'd memory. */
3258 bash_directory_expansion (dirname
)
3263 d
= savestring (*dirname
);
3265 if ((rl_directory_rewrite_hook
) && (*rl_directory_rewrite_hook
) (&d
))
3270 else if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&d
))
3275 else if (rl_completion_found_quote
)
3277 nd
= bash_dequote_filename (d
, rl_completion_quote_character
);
3286 /* If necessary, rewrite directory entry */
3288 bash_filename_rewrite_hook (fname
, fnlen
)
3294 conv
= fnx_fromfs (fname
, fnlen
);
3296 conv
= savestring (conv
);
3300 /* Functions to save and restore the appropriate directory hook */
3301 /* This is not static so the shopt code can call it */
3303 set_directory_hook ()
3305 if (dircomplete_expand
)
3307 rl_directory_completion_hook
= bash_directory_completion_hook
;
3308 rl_directory_rewrite_hook
= (rl_icppfunc_t
*)0;
3312 rl_directory_rewrite_hook
= bash_directory_completion_hook
;
3313 rl_directory_completion_hook
= (rl_icppfunc_t
*)0;
3317 static rl_icppfunc_t
*
3318 save_directory_hook ()
3322 if (dircomplete_expand
)
3324 ret
= rl_directory_completion_hook
;
3325 rl_directory_completion_hook
= (rl_icppfunc_t
*)NULL
;
3329 ret
= rl_directory_rewrite_hook
;
3330 rl_directory_rewrite_hook
= (rl_icppfunc_t
*)NULL
;
3337 restore_directory_hook (hookf
)
3338 rl_icppfunc_t
*hookf
;
3340 if (dircomplete_expand
)
3341 rl_directory_completion_hook
= hookf
;
3343 rl_directory_rewrite_hook
= hookf
;
3346 /* Check whether not DIRNAME, with any trailing slash removed, exists. If
3347 SHOULD_DEQUOTE is non-zero, we dequote the directory name first. */
3349 directory_exists (dirname
, should_dequote
)
3350 const char *dirname
;
3357 /* We save the string and chop the trailing slash because stat/lstat behave
3358 inconsistently if one is present. */
3359 new_dirname
= should_dequote
? bash_dequote_filename ((char *)dirname
, rl_completion_quote_character
) : savestring (dirname
);
3360 dirlen
= STRLEN (new_dirname
);
3361 if (new_dirname
[dirlen
- 1] == '/')
3362 new_dirname
[dirlen
- 1] = '\0';
3363 #if defined (HAVE_LSTAT)
3364 r
= lstat (new_dirname
, &sb
) == 0;
3366 r
= stat (new_dirname
, &sb
) == 0;
3372 /* Expand a filename before the readline completion code passes it to stat(2).
3373 The filename will already have had tilde expansion performed. */
3375 bash_filename_stat_hook (dirname
)
3378 char *local_dirname
, *new_dirname
, *t
;
3379 int should_expand_dirname
, return_value
;
3383 local_dirname
= *dirname
;
3384 should_expand_dirname
= return_value
= 0;
3385 if (t
= mbschr (local_dirname
, '$'))
3386 should_expand_dirname
= '$';
3387 else if (t
= mbschr (local_dirname
, '`')) /* XXX */
3388 should_expand_dirname
= '`';
3390 if (should_expand_dirname
&& directory_exists (local_dirname
, 0))
3391 should_expand_dirname
= 0;
3393 if (should_expand_dirname
)
3395 new_dirname
= savestring (local_dirname
);
3396 /* no error messages, and expand_prompt_string doesn't longjmp so we don't
3397 have to worry about restoring this setting. */
3398 global_nounset
= unbound_vars_is_error
;
3399 unbound_vars_is_error
= 0;
3400 wl
= expand_prompt_string (new_dirname
, 0, W_NOCOMSUB
|W_NOPROCSUB
|W_COMPLETE
); /* does the right thing */
3401 unbound_vars_is_error
= global_nounset
;
3405 new_dirname
= string_list (wl
);
3406 /* Tell the completer we actually expanded something and change
3407 *dirname only if we expanded to something non-null -- stat
3408 behaves unpredictably when passed null or empty strings */
3409 if (new_dirname
&& *new_dirname
)
3411 free (local_dirname
); /* XXX */
3412 local_dirname
= *dirname
= new_dirname
;
3413 return_value
= STREQ (local_dirname
, *dirname
) == 0;
3423 /* This is very similar to the code in bash_directory_completion_hook below,
3424 but without spelling correction and not worrying about whether or not
3425 we change relative pathnames. */
3426 if (no_symbolic_links
== 0 && (local_dirname
[0] != '.' || local_dirname
[1]))
3428 char *temp1
, *temp2
;
3430 t
= get_working_directory ("symlink-hook");
3431 temp1
= make_absolute (local_dirname
, t
);
3433 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3435 /* If we can't canonicalize, bail. */
3439 return return_value
;
3442 free (local_dirname
);
3447 return (return_value
);
3450 /* Handle symbolic link references and other directory name
3451 expansions while hacking completion. This should return 1 if it modifies
3452 the DIRNAME argument, 0 otherwise. It should make sure not to modify
3453 DIRNAME if it returns 0. */
3455 bash_directory_completion_hook (dirname
)
3458 char *local_dirname
, *new_dirname
, *t
;
3459 int return_value
, should_expand_dirname
, nextch
, closer
;
3462 return_value
= should_expand_dirname
= nextch
= closer
= 0;
3463 local_dirname
= *dirname
;
3465 should_expand_dirname
= bash_check_expchar (local_dirname
, 1, &nextch
, &closer
);
3467 if (should_expand_dirname
&& directory_exists (local_dirname
, 1))
3468 should_expand_dirname
= 0;
3470 if (should_expand_dirname
)
3472 new_dirname
= savestring (local_dirname
);
3473 wl
= expand_prompt_string (new_dirname
, 0, W_NOCOMSUB
|W_NOPROCSUB
|W_COMPLETE
); /* does the right thing */
3476 *dirname
= string_list (wl
);
3477 /* Tell the completer to replace the directory name only if we
3478 actually expanded something. */
3479 return_value
= STREQ (local_dirname
, *dirname
) == 0;
3480 free (local_dirname
);
3483 local_dirname
= *dirname
;
3485 set_filename_quote_chars (should_expand_dirname
, nextch
, closer
);
3490 free (local_dirname
);
3491 *dirname
= (char *)xmalloc (1);
3498 /* Dequote the filename even if we don't expand it. */
3499 new_dirname
= bash_dequote_filename (local_dirname
, rl_completion_quote_character
);
3500 return_value
= STREQ (local_dirname
, new_dirname
) == 0;
3501 free (local_dirname
);
3502 local_dirname
= *dirname
= new_dirname
;
3505 /* no_symbolic_links == 0 -> use (default) logical view of the file system.
3506 local_dirname[0] == '.' && local_dirname[1] == '/' means files in the
3507 current directory (./).
3508 local_dirname[0] == '.' && local_dirname[1] == 0 means relative pathnames
3509 in the current directory (e.g., lib/sh).
3510 XXX - should we do spelling correction on these? */
3512 /* This is test as it was in bash-4.2: skip relative pathnames in current
3513 directory. Change test to
3514 (local_dirname[0] != '.' || (local_dirname[1] && local_dirname[1] != '/'))
3515 if we want to skip paths beginning with ./ also. */
3516 if (no_symbolic_links
== 0 && (local_dirname
[0] != '.' || local_dirname
[1]))
3518 char *temp1
, *temp2
;
3521 /* If we have a relative path
3522 (local_dirname[0] != '/' && local_dirname[0] != '.')
3523 that is canonical after appending it to the current directory, then
3526 strcmp (temp1, temp2) == 0
3527 after adding a slash to temp2 below. It should be safe to not
3530 t
= get_working_directory ("symlink-hook");
3531 temp1
= make_absolute (local_dirname
, t
);
3533 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3535 /* Try spelling correction if initial canonicalization fails. Make
3536 sure we are set to replace the directory name with the results so
3537 subsequent directory checks don't fail. */
3538 if (temp2
== 0 && dircomplete_spelling
&& dircomplete_expand
)
3542 temp2
= dirspell (temp1
);
3543 l2
= STRLEN (temp2
);
3544 /* Don't take matches if they are shorter than the original path */
3545 if (temp2
&& l2
< strlen (temp1
) && STREQN (temp1
, temp2
, l2
))
3554 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3555 return_value
|= temp2
!= 0;
3558 /* If we can't canonicalize, bail. */
3562 return return_value
;
3564 len1
= strlen (temp1
);
3565 if (temp1
[len1
- 1] == '/')
3567 len2
= strlen (temp2
);
3568 if (len2
> 2) /* don't append `/' to `/' or `//' */
3570 temp2
= (char *)xrealloc (temp2
, len2
+ 2);
3572 temp2
[len2
+ 1] = '\0';
3576 /* dircomplete_expand_relpath == 0 means we want to leave relative
3577 pathnames that are unchanged by canonicalization alone.
3578 *local_dirname != '/' && *local_dirname != '.' == relative pathname
3579 (consistent with general.c:absolute_pathname())
3580 temp1 == temp2 (after appending a slash to temp2) means the pathname
3581 is not changed by canonicalization as described above. */
3582 if (dircomplete_expand_relpath
|| ((local_dirname
[0] != '/' && local_dirname
[0] != '.') && STREQ (temp1
, temp2
) == 0))
3583 return_value
|= STREQ (local_dirname
, temp2
) == 0;
3584 free (local_dirname
);
3589 return (return_value
);
3592 static char **history_completion_array
= (char **)NULL
;
3593 static int harry_size
;
3594 static int harry_len
;
3597 build_history_completion_array ()
3603 /* First, clear out the current dynamic history completion list. */
3606 strvec_dispose (history_completion_array
);
3607 history_completion_array
= (char **)NULL
;
3612 /* Next, grovel each line of history, making each shell-sized token
3613 a separate entry in the history_completion_array. */
3614 hlist
= history_list ();
3618 for (i
= 0; hlist
[i
]; i
++)
3620 for ( --i
; i
>= 0; i
--)
3622 /* Separate each token, and place into an array. */
3623 tokens
= history_tokenize (hlist
[i
]->line
);
3625 for (j
= 0; tokens
&& tokens
[j
]; j
++)
3627 if (harry_len
+ 2 > harry_size
)
3628 history_completion_array
= strvec_resize (history_completion_array
, harry_size
+= 10);
3630 history_completion_array
[harry_len
++] = tokens
[j
];
3631 history_completion_array
[harry_len
] = (char *)NULL
;
3636 /* Sort the complete list of tokens. */
3637 if (dabbrev_expand_active
== 0)
3638 qsort (history_completion_array
, harry_len
, sizeof (char *), (QSFUNC
*)strvec_strcmp
);
3643 history_completion_generator (hint_text
, state
)
3644 const char *hint_text
;
3647 static int local_index
, len
;
3648 static const char *text
;
3650 /* If this is the first call to the generator, then initialize the
3651 list of strings to complete over. */
3654 if (dabbrev_expand_active
) /* This is kind of messy */
3655 rl_completion_suppress_append
= 1;
3657 build_history_completion_array ();
3659 len
= strlen (text
);
3662 while (history_completion_array
&& history_completion_array
[local_index
])
3664 /* XXX - should this use completion-ignore-case? */
3665 if (strncmp (text
, history_completion_array
[local_index
++], len
) == 0)
3666 return (savestring (history_completion_array
[local_index
- 1]));
3668 return ((char *)NULL
);
3672 dynamic_complete_history (count
, key
)
3676 rl_compentry_func_t
*orig_func
;
3677 rl_completion_func_t
*orig_attempt_func
;
3678 rl_compignore_func_t
*orig_ignore_func
;
3680 orig_func
= rl_completion_entry_function
;
3681 orig_attempt_func
= rl_attempted_completion_function
;
3682 orig_ignore_func
= rl_ignore_some_completions_function
;
3684 rl_completion_entry_function
= history_completion_generator
;
3685 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3686 rl_ignore_some_completions_function
= filename_completion_ignore
;
3688 /* XXX - use rl_completion_mode here? */
3689 if (rl_last_func
== dynamic_complete_history
)
3690 r
= rl_complete_internal ('?');
3692 r
= rl_complete_internal (TAB
);
3694 rl_completion_entry_function
= orig_func
;
3695 rl_attempted_completion_function
= orig_attempt_func
;
3696 rl_ignore_some_completions_function
= orig_ignore_func
;
3702 bash_dabbrev_expand (count
, key
)
3705 int r
, orig_suppress
, orig_sort
;
3706 rl_compentry_func_t
*orig_func
;
3707 rl_completion_func_t
*orig_attempt_func
;
3708 rl_compignore_func_t
*orig_ignore_func
;
3710 orig_func
= rl_menu_completion_entry_function
;
3711 orig_attempt_func
= rl_attempted_completion_function
;
3712 orig_ignore_func
= rl_ignore_some_completions_function
;
3713 orig_suppress
= rl_completion_suppress_append
;
3714 orig_sort
= rl_sort_completion_matches
;
3716 rl_menu_completion_entry_function
= history_completion_generator
;
3717 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3718 rl_ignore_some_completions_function
= filename_completion_ignore
;
3719 rl_filename_completion_desired
= 0;
3720 rl_completion_suppress_append
= 1;
3721 rl_sort_completion_matches
= 0;
3723 /* XXX - use rl_completion_mode here? */
3724 dabbrev_expand_active
= 1;
3725 if (rl_last_func
== bash_dabbrev_expand
)
3726 rl_last_func
= rl_menu_complete
;
3727 r
= rl_menu_complete (count
, key
);
3728 dabbrev_expand_active
= 0;
3730 rl_last_func
= bash_dabbrev_expand
;
3731 rl_menu_completion_entry_function
= orig_func
;
3732 rl_attempted_completion_function
= orig_attempt_func
;
3733 rl_ignore_some_completions_function
= orig_ignore_func
;
3734 rl_completion_suppress_append
= orig_suppress
;
3735 rl_sort_completion_matches
= orig_sort
;
3740 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
3742 bash_complete_username (ignore
, ignore2
)
3743 int ignore
, ignore2
;
3745 return bash_complete_username_internal (rl_completion_mode (bash_complete_username
));
3749 bash_possible_username_completions (ignore
, ignore2
)
3750 int ignore
, ignore2
;
3752 return bash_complete_username_internal ('?');
3756 bash_complete_username_internal (what_to_do
)
3759 return bash_specific_completion (what_to_do
, rl_username_completion_function
);
3763 bash_complete_filename (ignore
, ignore2
)
3764 int ignore
, ignore2
;
3766 return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename
));
3770 bash_possible_filename_completions (ignore
, ignore2
)
3771 int ignore
, ignore2
;
3773 return bash_complete_filename_internal ('?');
3777 bash_complete_filename_internal (what_to_do
)
3780 rl_compentry_func_t
*orig_func
;
3781 rl_completion_func_t
*orig_attempt_func
;
3782 rl_icppfunc_t
*orig_dir_func
;
3783 rl_compignore_func_t
*orig_ignore_func
;
3784 const char *orig_rl_completer_word_break_characters
;
3787 orig_func
= rl_completion_entry_function
;
3788 orig_attempt_func
= rl_attempted_completion_function
;
3789 orig_ignore_func
= rl_ignore_some_completions_function
;
3790 orig_rl_completer_word_break_characters
= rl_completer_word_break_characters
;
3792 orig_dir_func
= save_directory_hook ();
3794 rl_completion_entry_function
= rl_filename_completion_function
;
3795 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3796 rl_ignore_some_completions_function
= filename_completion_ignore
;
3797 rl_completer_word_break_characters
= " \t\n\"\'";
3799 r
= rl_complete_internal (what_to_do
);
3801 rl_completion_entry_function
= orig_func
;
3802 rl_attempted_completion_function
= orig_attempt_func
;
3803 rl_ignore_some_completions_function
= orig_ignore_func
;
3804 rl_completer_word_break_characters
= orig_rl_completer_word_break_characters
;
3806 restore_directory_hook (orig_dir_func
);
3812 bash_complete_hostname (ignore
, ignore2
)
3813 int ignore
, ignore2
;
3815 return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname
));
3819 bash_possible_hostname_completions (ignore
, ignore2
)
3820 int ignore
, ignore2
;
3822 return bash_complete_hostname_internal ('?');
3826 bash_complete_variable (ignore
, ignore2
)
3827 int ignore
, ignore2
;
3829 return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable
));
3833 bash_possible_variable_completions (ignore
, ignore2
)
3834 int ignore
, ignore2
;
3836 return bash_complete_variable_internal ('?');
3840 bash_complete_command (ignore
, ignore2
)
3841 int ignore
, ignore2
;
3843 return bash_complete_command_internal (rl_completion_mode (bash_complete_command
));
3847 bash_possible_command_completions (ignore
, ignore2
)
3848 int ignore
, ignore2
;
3850 return bash_complete_command_internal ('?');
3854 bash_complete_hostname_internal (what_to_do
)
3857 return bash_specific_completion (what_to_do
, hostname_completion_function
);
3861 bash_complete_variable_internal (what_to_do
)
3864 return bash_specific_completion (what_to_do
, variable_completion_function
);
3868 bash_complete_command_internal (what_to_do
)
3871 return bash_specific_completion (what_to_do
, command_word_completion_function
);
3875 completion_glob_pattern (string
)
3878 return (glob_pattern_p (string
) == 1);
3881 static char *globtext
;
3882 static char *globorig
;
3885 glob_complete_word (text
, state
)
3889 static char **matches
= (char **)NULL
;
3896 rl_filename_completion_desired
= 1;
3898 if (globorig
!= globtext
)
3902 ttext
= bash_tilde_expand (text
, 0);
3904 if (rl_explicit_arg
)
3906 globorig
= savestring (ttext
);
3907 glen
= strlen (ttext
);
3908 globtext
= (char *)xmalloc (glen
+ 2);
3909 strcpy (globtext
, ttext
);
3910 globtext
[glen
] = '*';
3911 globtext
[glen
+1] = '\0';
3914 globtext
= globorig
= savestring (ttext
);
3919 matches
= shell_glob_filename (globtext
, 0);
3920 if (GLOB_FAILED (matches
))
3921 matches
= (char **)NULL
;
3925 ret
= matches
? matches
[ind
] : (char *)NULL
;
3931 bash_glob_completion_internal (what_to_do
)
3934 return bash_specific_completion (what_to_do
, glob_complete_word
);
3937 /* A special quoting function so we don't end up quoting globbing characters
3938 in the word if there are no matches or multiple matches. */
3940 bash_glob_quote_filename (s
, rtype
, qcp
)
3945 if (globorig
&& qcp
&& *qcp
== '\0' && STREQ (s
, globorig
))
3946 return (savestring (s
));
3948 return (bash_quote_filename (s
, rtype
, qcp
));
3952 bash_glob_complete_word (count
, key
)
3956 rl_quote_func_t
*orig_quoting_function
;
3958 if (rl_editing_mode
== EMACS_EDITING_MODE
)
3959 rl_explicit_arg
= 1; /* force `*' append */
3960 orig_quoting_function
= rl_filename_quoting_function
;
3961 rl_filename_quoting_function
= bash_glob_quote_filename
;
3963 r
= bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word
));
3965 rl_filename_quoting_function
= orig_quoting_function
;
3970 bash_glob_expand_word (count
, key
)
3973 return bash_glob_completion_internal ('*');
3977 bash_glob_list_expansions (count
, key
)
3980 return bash_glob_completion_internal ('?');
3984 bash_specific_completion (what_to_do
, generator
)
3986 rl_compentry_func_t
*generator
;
3988 rl_compentry_func_t
*orig_func
;
3989 rl_completion_func_t
*orig_attempt_func
;
3990 rl_compignore_func_t
*orig_ignore_func
;
3993 orig_func
= rl_completion_entry_function
;
3994 orig_attempt_func
= rl_attempted_completion_function
;
3995 orig_ignore_func
= rl_ignore_some_completions_function
;
3996 rl_completion_entry_function
= generator
;
3997 rl_attempted_completion_function
= NULL
;
3998 rl_ignore_some_completions_function
= orig_ignore_func
;
4000 r
= rl_complete_internal (what_to_do
);
4002 rl_completion_entry_function
= orig_func
;
4003 rl_attempted_completion_function
= orig_attempt_func
;
4004 rl_ignore_some_completions_function
= orig_ignore_func
;
4009 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
4011 #if defined (VI_MODE)
4012 /* Completion, from vi mode's point of view. This is a modified version of
4013 rl_vi_complete which uses the bash globbing code to implement what POSIX
4014 specifies, which is to append a `*' and attempt filename generation (which
4015 has the side effect of expanding any globbing characters in the word). */
4017 bash_vi_complete (count
, key
)
4020 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
4024 if ((rl_point
< rl_end
) && (!whitespace (rl_line_buffer
[rl_point
])))
4026 if (!whitespace (rl_line_buffer
[rl_point
+ 1]))
4027 rl_vi_end_word (1, 'E');
4031 /* Find boundaries of current word, according to vi definition of a
4037 rl_vi_bWord (1, 'B');
4042 t
= substring (rl_line_buffer
, p
, rl_point
);
4045 if (t
&& completion_glob_pattern (t
) == 0)
4046 rl_explicit_arg
= 1; /* XXX - force glob_complete_word to append `*' */
4049 if (key
== '*') /* Expansion and replacement. */
4050 r
= bash_glob_expand_word (count
, key
);
4051 else if (key
== '=') /* List possible completions. */
4052 r
= bash_glob_list_expansions (count
, key
);
4053 else if (key
== '\\') /* Standard completion */
4054 r
= bash_glob_complete_word (count
, key
);
4056 r
= rl_complete (0, key
);
4058 if (key
== '*' || key
== '\\')
4059 rl_vi_start_inserting (key
, 1, 1);
4063 return rl_vi_complete (count
, key
);
4064 #endif /* !SPECIFIC_COMPLETION_FUNCTIONS */
4066 #endif /* VI_MODE */
4068 /* Filename quoting for completion. */
4069 /* A function to strip unquoted quote characters (single quotes, double
4070 quotes, and backslashes). It allows single quotes to appear
4071 within double quotes, and vice versa. It should be smarter. */
4073 bash_dequote_filename (text
, quote_char
)
4081 ret
= (char *)xmalloc (l
+ 1);
4082 for (quoted
= quote_char
, p
= text
, r
= ret
; p
&& *p
; p
++)
4084 /* Allow backslash-escaped characters to pass through unscathed. */
4087 /* Backslashes are preserved within single quotes. */
4090 /* Backslashes are preserved within double quotes unless the
4091 character is one that is defined to be escaped */
4092 else if (quoted
== '"' && ((sh_syntaxtab
[(unsigned char)p
[1]] & CBSDQUOTE
) == 0))
4097 return ret
; /* XXX - was break; */
4101 if (quoted
&& *p
== quoted
)
4107 if (quoted
== 0 && (*p
== '\'' || *p
== '"'))
4118 /* Quote characters that the readline completion code would treat as
4119 word break characters with backslashes. Pass backslash-quoted
4120 characters through without examination. */
4122 quote_word_break_chars (text
)
4129 ret
= (char *)xmalloc ((2 * l
) + 1);
4130 for (s
= text
, r
= ret
; *s
; s
++)
4132 /* Pass backslash-quoted characters through, including the backslash. */
4141 /* OK, we have an unquoted character. Check its presence in
4142 rl_completer_word_break_characters. */
4143 if (mbschr (rl_completer_word_break_characters
, *s
))
4145 /* XXX -- check for standalone tildes here and backslash-quote them */
4146 if (s
== text
&& *s
== '~' && file_exists (text
))
4154 /* Return a character in DIRNAME that will cause shell expansion to be
4155 performed. If NEXTP is non-null, *NEXTP gets the expansion character that
4156 follows RET (e.g., '{' or `(' for `$'). If CLOSERP is non-null, *CLOSERP
4157 gets the character that should close <RET><NEXTP>. If NEED_CLOSER is non-
4158 zero, any expansion pair that isn't closed causes this function to
4159 return 0, which indicates that we didn't find an expansion character. It's
4160 used in case DIRNAME is going to be expanded. If DIRNAME is just going to
4161 be quoted, NEED_CLOSER will be 0. */
4163 bash_check_expchar (dirname
, need_closer
, nextp
, closerp
)
4166 int *nextp
, *closerp
;
4172 if (t
= mbschr (dirname
, '$'))
4176 /* Deliberately does not handle the deprecated $[...] arithmetic
4185 if (c
&& need_closer
) /* XXX */
4190 delims
[0] = c
; delims
[1] = 0;
4191 p
= skip_to_delim (t
, 1, delims
, SD_NOJMP
|SD_COMPLETE
);
4196 else if (dirname
[0] == '~')
4200 t
= mbschr (dirname
, '`');
4203 if (need_closer
== 0)
4205 else if (unclosed_pair (dirname
, strlen (dirname
), "`") == 0)
4218 /* Make sure EXPCHAR and, if non-zero, NEXTCH and CLOSER are not in the set
4219 of characters to be backslash-escaped. This is the only place
4220 custom_filename_quote_characters is modified. */
4222 set_filename_quote_chars (expchar
, nextch
, closer
)
4223 int expchar
, nextch
, closer
;
4227 if (rl_filename_quote_characters
&& *rl_filename_quote_characters
)
4229 i
= strlen (default_filename_quote_characters
);
4230 custom_filename_quote_characters
= xrealloc (custom_filename_quote_characters
, i
+1);
4231 for (i
= j
= 0; c
= default_filename_quote_characters
[i
]; i
++)
4233 if (c
== expchar
|| c
== nextch
|| c
== closer
)
4235 custom_filename_quote_characters
[j
++] = c
;
4237 custom_filename_quote_characters
[j
] = '\0';
4238 rl_filename_quote_characters
= custom_filename_quote_characters
;
4239 set_filename_bstab (rl_filename_quote_characters
);
4243 /* Use characters in STRING to populate the table of characters that should
4244 be backslash-quoted. The table will be used for sh_backslash_quote from
4247 set_filename_bstab (string
)
4252 memset (filename_bstab
, 0, sizeof (filename_bstab
));
4253 for (s
= string
; s
&& *s
; s
++)
4254 filename_bstab
[(unsigned char)*s
] = 1;
4257 /* Quote a filename using double quotes, single quotes, or backslashes
4258 depending on the value of completion_quoting_style. If we're
4259 completing using backslashes, we need to quote some additional
4260 characters (those that readline treats as word breaks), so we call
4261 quote_word_break_chars on the result. This returns newly-allocated
4264 bash_quote_filename (s
, rtype
, qcp
)
4269 char *rtext
, *mtext
, *ret
;
4271 int expchar
, nextch
, closer
;
4273 rtext
= (char *)NULL
;
4275 /* If RTYPE == MULT_MATCH, it means that there is
4276 more than one match. In this case, we do not add
4277 the closing quote or attempt to perform tilde
4278 expansion. If RTYPE == SINGLE_MATCH, we try
4279 to perform tilde expansion, because single and double
4280 quotes inhibit tilde expansion by the shell. */
4282 cs
= completion_quoting_style
;
4283 /* Might need to modify the default completion style based on *qcp,
4284 since it's set to any user-provided opening quote. We also change
4285 to single-quoting if there is no user-provided opening quote and
4286 the word being completed contains newlines, since those are not
4287 quoted correctly using backslashes (a backslash-newline pair is
4288 special to the shell parser). */
4289 expchar
= nextch
= closer
= 0;
4290 if (*qcp
== '\0' && cs
== COMPLETE_BSQUOTE
&& dircomplete_expand
== 0 &&
4291 (expchar
= bash_check_expchar (s
, 0, &nextch
, &closer
)) &&
4292 file_exists (s
) == 0)
4294 /* Usually this will have been set by bash_directory_completion_hook,
4295 but there are cases where it will not be. */
4296 if (rl_filename_quote_characters
!= custom_filename_quote_characters
)
4297 set_filename_quote_chars (expchar
, nextch
, closer
);
4298 complete_fullquote
= 0;
4300 else if (*qcp
== '\0' && cs
== COMPLETE_BSQUOTE
&& mbschr (s
, '\n'))
4301 cs
= COMPLETE_SQUOTE
;
4302 else if (*qcp
== '"')
4303 cs
= COMPLETE_DQUOTE
;
4304 else if (*qcp
== '\'')
4305 cs
= COMPLETE_SQUOTE
;
4306 #if defined (BANG_HISTORY)
4307 else if (*qcp
== '\0' && history_expansion
&& cs
== COMPLETE_DQUOTE
&&
4308 history_expansion_inhibited
== 0 && mbschr (s
, '!'))
4309 cs
= COMPLETE_BSQUOTE
;
4311 if (*qcp
== '"' && history_expansion
&& cs
== COMPLETE_DQUOTE
&&
4312 history_expansion_inhibited
== 0 && mbschr (s
, '!'))
4314 cs
= COMPLETE_BSQUOTE
;
4319 /* Don't tilde-expand backslash-quoted filenames, since only single and
4320 double quotes inhibit tilde expansion. */
4322 if (mtext
[0] == '~' && rtype
== SINGLE_MATCH
&& cs
!= COMPLETE_BSQUOTE
)
4323 mtext
= bash_tilde_expand (s
, 0);
4327 case COMPLETE_DQUOTE
:
4328 rtext
= sh_double_quote (mtext
);
4330 case COMPLETE_SQUOTE
:
4331 rtext
= sh_single_quote (mtext
);
4333 case COMPLETE_BSQUOTE
:
4334 rtext
= sh_backslash_quote (mtext
, complete_fullquote
? 0 : filename_bstab
, 0);
4341 /* We may need to quote additional characters: those that readline treats
4342 as word breaks that are not quoted by backslash_quote. */
4343 /* XXX - test complete_fullquote here? */
4344 if (rtext
&& cs
== COMPLETE_BSQUOTE
&& rl_completer_word_break_characters
)
4346 mtext
= quote_word_break_chars (rtext
);
4351 /* Leave the opening quote intact. The readline completion code takes
4352 care of avoiding doubled opening quotes. */
4355 rlen
= strlen (rtext
);
4356 ret
= (char *)xmalloc (rlen
+ 1);
4357 strcpy (ret
, rtext
);
4361 ret
= (char *)xmalloc (rlen
= 1);
4365 /* If there are multiple matches, cut off the closing quote. */
4366 if (rtype
== MULT_MATCH
&& cs
!= COMPLETE_BSQUOTE
)
4367 ret
[rlen
- 1] = '\0';
4372 /* Support for binding readline key sequences to Unix commands. Each editing
4373 mode has a separate Unix command keymap. */
4375 static Keymap emacs_std_cmd_xmap
;
4376 #if defined (VI_MODE)
4377 static Keymap vi_insert_cmd_xmap
;
4378 static Keymap vi_movement_cmd_xmap
;
4390 x
= putc (c
, rl_outstream
);
4397 readline_get_char_offset (ind
)
4403 #if defined (HANDLE_MULTIBYTE)
4404 if (locale_mb_cur_max
> 1)
4406 old_ch
= rl_line_buffer
[ind
];
4407 rl_line_buffer
[ind
] = '\0';
4408 r
= MB_STRLEN (rl_line_buffer
);
4409 rl_line_buffer
[ind
] = old_ch
;
4416 readline_set_char_offset (ind
, varp
)
4424 #if defined (HANDLE_MULTIBYTE)
4425 if (i
> 0 && locale_mb_cur_max
> 1)
4426 i
= _rl_find_next_mbchar (rl_line_buffer
, 0, i
, 0); /* XXX */
4439 bash_execute_unix_command (count
, key
)
4440 int count
; /* ignored */
4446 sh_parser_state_t ps
;
4447 char *cmd
, *value
, *ce
, old_ch
;
4449 char ibuf
[INT_STRLEN_BOUND(int) + 1];
4454 kseq
= rl_executing_keyseq
;
4455 kslen
= rl_key_sequence_length
;
4457 /* If we have a numeric argument, chop it off the front of the key sequence */
4458 if (count
> 1 || rl_explicit_arg
)
4460 i
= rl_trim_arg_from_keyseq (rl_executing_keyseq
, rl_key_sequence_length
, rl_get_keymap ());
4463 kseq
= rl_executing_keyseq
+ i
;
4464 kslen
= rl_key_sequence_length
- i
;
4468 /* First, we need to find the right command to execute. This is tricky,
4469 because we might have already indirected into another keymap, so we
4470 have to walk cmd_xmap using the entire key sequence. */
4471 cmd_xmap
= get_cmd_xmap_from_keymap (rl_get_keymap ());
4472 cmd
= (char *)rl_function_of_keyseq_len (kseq
, kslen
, cmd_xmap
, &type
);
4474 if (type
== ISKMAP
&& (type
= ((Keymap
) cmd
)[ANYOTHERKEY
].type
) == ISMACR
)
4475 cmd
= (char*)((Keymap
) cmd
)[ANYOTHERKEY
].function
;
4477 if (cmd
== 0 || type
!= ISMACR
)
4480 internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
4481 rl_forced_update_display ();
4485 ce
= rl_get_termcap ("ce");
4486 if (ce
) /* clear current line */
4488 rl_clear_visible_line ();
4489 fflush (rl_outstream
);
4492 rl_crlf (); /* move to a new line */
4494 v
= bind_variable ("READLINE_LINE", rl_line_buffer
, 0);
4496 VSETATTR (v
, att_exported
);
4498 i
= readline_get_char_offset (rl_point
);
4499 value
= inttostr (i
, ibuf
, sizeof (ibuf
));
4500 v
= bind_int_variable ("READLINE_POINT", value
, 0);
4502 VSETATTR (v
, att_exported
);
4504 i
= readline_get_char_offset (rl_mark
);
4505 value
= inttostr (i
, ibuf
, sizeof (ibuf
));
4506 v
= bind_int_variable ("READLINE_MARK", value
, 0);
4508 VSETATTR (v
, att_exported
);
4510 if (count
> 1 || rl_explicit_arg
)
4512 value
= inttostr (count
, ibuf
, sizeof (ibuf
));
4513 v
= bind_int_variable ("READLINE_ARGUMENT", value
, 0);
4515 VSETATTR (v
, att_exported
);
4517 array_needs_making
= 1;
4519 save_parser_state (&ps
);
4520 rl_clear_signals ();
4521 r
= parse_and_execute (savestring (cmd
), "bash_execute_unix_command", SEVAL_NOHIST
);
4523 restore_parser_state (&ps
);
4525 v
= find_variable ("READLINE_LINE");
4526 maybe_make_readline_line (v
? value_cell (v
) : 0);
4528 v
= find_variable ("READLINE_POINT");
4529 if (v
&& legal_number (value_cell (v
), &mi
))
4530 readline_set_char_offset (mi
, &rl_point
);
4532 v
= find_variable ("READLINE_MARK");
4533 if (v
&& legal_number (value_cell (v
), &mi
))
4534 readline_set_char_offset (mi
, &rl_mark
);
4536 check_unbind_variable ("READLINE_LINE");
4537 check_unbind_variable ("READLINE_POINT");
4538 check_unbind_variable ("READLINE_MARK");
4539 check_unbind_variable ("READLINE_ARGUMENT");
4540 array_needs_making
= 1;
4542 /* and restore the readline buffer and display after command execution. */
4543 /* If we clear the last line of the prompt above, redraw only that last
4544 line. If the command returns 124, we redraw unconditionally as in
4545 previous versions. */
4547 rl_redraw_prompt_last_line ();
4549 rl_forced_update_display ();
4555 print_unix_command_map ()
4557 Keymap save
, cmd_xmap
;
4559 save
= rl_get_keymap ();
4560 cmd_xmap
= get_cmd_xmap_from_keymap (save
);
4561 rl_set_keymap (cmd_xmap
);
4562 rl_macro_dumper (1);
4563 rl_set_keymap (save
);
4568 init_unix_command_map ()
4570 emacs_std_cmd_xmap
= rl_make_bare_keymap ();
4572 emacs_std_cmd_xmap
[CTRL('X')].type
= ISKMAP
;
4573 emacs_std_cmd_xmap
[CTRL('X')].function
= KEYMAP_TO_FUNCTION (rl_make_bare_keymap ());
4574 emacs_std_cmd_xmap
[ESC
].type
= ISKMAP
;
4575 emacs_std_cmd_xmap
[ESC
].function
= KEYMAP_TO_FUNCTION (rl_make_bare_keymap ());
4577 #if defined (VI_MODE)
4578 vi_insert_cmd_xmap
= rl_make_bare_keymap ();
4579 vi_movement_cmd_xmap
= rl_make_bare_keymap ();
4584 get_cmd_xmap_from_edit_mode ()
4586 if (emacs_std_cmd_xmap
== 0)
4587 init_unix_command_map ();
4589 switch (rl_editing_mode
)
4591 case EMACS_EDITING_MODE
:
4592 return emacs_std_cmd_xmap
;
4593 #if defined (VI_MODE)
4594 case VI_EDITING_MODE
:
4595 return (get_cmd_xmap_from_keymap (rl_get_keymap ()));
4598 return (Keymap
)NULL
;
4603 get_cmd_xmap_from_keymap (kmap
)
4606 if (emacs_std_cmd_xmap
== 0)
4607 init_unix_command_map ();
4609 if (kmap
== emacs_standard_keymap
)
4610 return emacs_std_cmd_xmap
;
4611 else if (kmap
== emacs_meta_keymap
)
4612 return (FUNCTION_TO_KEYMAP (emacs_std_cmd_xmap
, ESC
));
4613 else if (kmap
== emacs_ctlx_keymap
)
4614 return (FUNCTION_TO_KEYMAP (emacs_std_cmd_xmap
, CTRL('X')));
4615 #if defined (VI_MODE)
4616 else if (kmap
== vi_insertion_keymap
)
4617 return vi_insert_cmd_xmap
;
4618 else if (kmap
== vi_movement_keymap
)
4619 return vi_movement_cmd_xmap
;
4622 return (Keymap
)NULL
;
4626 isolate_sequence (string
, ind
, need_dquote
, startp
)
4628 int ind
, need_dquote
, *startp
;
4631 int c
, passc
, delim
;
4633 for (i
= ind
; string
[i
] && whitespace (string
[i
]); i
++)
4635 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
4636 if (need_dquote
&& string
[i
] != '"')
4638 builtin_error (_("%s: first non-whitespace character is not `\"'"), string
);
4642 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
4643 string to bind the key sequence to. */
4644 delim
= (string
[i
] == '"' || string
[i
] == '\'') ? string
[i
] : 0;
4647 *startp
= delim
? ++i
: i
;
4649 for (passc
= 0; c
= string
[i
]; i
++)
4665 if (delim
&& string
[i
] != delim
)
4667 builtin_error (_("no closing `%c' in %s"), delim
, string
);
4675 bind_keyseq_to_unix_command (line
)
4678 Keymap kmap
, cmd_xmap
;
4682 kmap
= rl_get_keymap ();
4684 /* We duplicate some of the work done by rl_parse_and_bind here, but
4685 this code only has to handle `"keyseq": ["]command["]' and can
4686 generate an error for anything else. */
4687 i
= isolate_sequence (line
, 0, 1, &kstart
);
4691 /* Create the key sequence string to pass to rl_generic_bind */
4692 kseq
= substring (line
, kstart
, i
);
4694 for ( ; line
[i
] && line
[i
] != ':'; i
++)
4698 builtin_error (_("%s: missing colon separator"), line
);
4703 i
= isolate_sequence (line
, i
+ 1, 0, &kstart
);
4710 /* Create the value string containing the command to execute. */
4711 value
= substring (line
, kstart
, i
);
4713 /* Save the command to execute and the key sequence in the CMD_XMAP */
4714 cmd_xmap
= get_cmd_xmap_from_keymap (kmap
);
4715 rl_generic_bind (ISMACR
, kseq
, value
, cmd_xmap
);
4717 /* and bind the key sequence in the current keymap to a function that
4718 understands how to execute from CMD_XMAP */
4719 rl_bind_keyseq_in_map (kseq
, bash_execute_unix_command
, kmap
);
4726 unbind_unix_command (kseq
)
4731 cmd_xmap
= get_cmd_xmap_from_keymap (rl_get_keymap ());
4732 if (rl_bind_keyseq_in_map (kseq
, (rl_command_func_t
*)NULL
, cmd_xmap
) != 0)
4734 builtin_error (_("`%s': cannot unbind in command keymap"), kseq
);
4740 /* Used by the programmable completion code. Complete TEXT as a filename,
4741 but return only directories as matches. Dequotes the filename before
4742 attempting to find matches. */
4744 bash_directory_completion_matches (text
)
4751 qc
= rl_dispatching
? rl_completion_quote_character
: 0;
4752 /* If rl_completion_found_quote != 0, rl_completion_matches will call the
4753 filename dequoting function, causing the directory name to be dequoted
4755 if (rl_dispatching
&& rl_completion_found_quote
== 0)
4756 dfn
= bash_dequote_filename ((char *)text
, qc
);
4759 m1
= rl_completion_matches (dfn
, rl_filename_completion_function
);
4763 if (m1
== 0 || m1
[0] == 0)
4765 /* We don't bother recomputing the lcd of the matches, because it will just
4766 get thrown away by the programmable completion code and recomputed
4768 (void)bash_progcomp_ignore_filenames (m1
);
4773 bash_dequote_text (text
)
4779 qc
= (text
[0] == '"' || text
[0] == '\'') ? text
[0] : 0;
4780 dtxt
= bash_dequote_filename ((char *)text
, qc
);
4784 /* This event hook is designed to be called after readline receives a signal
4785 that interrupts read(2). It gives reasonable responsiveness to interrupts
4786 and fatal signals without executing too much code in a signal handler
4793 /* XXX - see if we need to do anything here if sigterm_received == 1,
4794 we probably don't want to reset the event hook since we will not be
4795 jumping to the top level */
4796 if (sigterm_received
)
4798 /* RESET_SIGTERM; */
4803 if (terminating_signal
)
4804 sig
= terminating_signal
;
4805 else if (interrupt_state
)
4807 else if (read_timeout
&& read_timeout
->alrmflag
)
4809 else if (RL_ISSTATE (RL_STATE_TIMEOUT
)) /* just in case */
4813 read_timeout
->alrmflag
= 1;
4816 sig
= first_pending_trap ();
4818 /* If we're going to longjmp to top_level, make sure we clean up readline.
4819 check_signals will call QUIT, which will eventually longjmp to top_level,
4820 calling run_interrupt_trap along the way. The check against read_timeout
4821 is so we can clean up the read builtin's state. */
4822 if (terminating_signal
|| interrupt_state
|| (read_timeout
&& read_timeout
->alrmflag
))
4823 rl_cleanup_after_signal ();
4824 bashline_reset_event_hook ();
4826 RL_UNSETSTATE (RL_STATE_TIMEOUT
); /* XXX */
4828 /* posix mode SIGINT during read -e. We only get here if SIGINT is trapped. */
4829 if (posixly_correct
&& this_shell_builtin
== read_builtin
&& sig
== SIGINT
)
4831 last_command_exit_value
= 128|SIGINT
;
4832 throw_to_top_level ();
4835 check_signals_and_traps (); /* XXX */
4839 #endif /* READLINE */