1 /* bashline.c -- Bash's interface to the readline library. */
3 /* Copyright (C) 1987-2016 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"
52 #include "execute_cmd.h"
59 #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
60 # include <mbstr.h> /* mbschr */
63 #include "builtins/common.h"
65 #include <readline/rlconf.h>
66 #include <readline/readline.h>
67 #include <readline/history.h>
69 #include <glob/glob.h>
75 #if defined (PROGRAMMABLE_COMPLETION)
76 # include "pcomplete.h"
79 /* These should agree with the defines for emacs_mode and vi_mode in
80 rldefs.h, even though that's not a public readline header file. */
81 #ifndef EMACS_EDITING_MODE
82 # define NO_EDITING_MODE -1
83 # define EMACS_EDITING_MODE 1
84 # define VI_EDITING_MODE 0
87 #define RL_BOOLEAN_VARIABLE_VALUE(s) ((s)[0] == 'o' && (s)[1] == 'n' && (s)[2] == '\0')
89 #if defined (BRACE_COMPLETION)
90 extern int bash_brace_completion
__P((int, int));
91 #endif /* BRACE_COMPLETION */
93 /* To avoid including curses.h/term.h/termcap.h and that whole mess. */
95 extern int tputs
__P((const char *string
, int nlines
, void (*outx
)(int)));
97 extern int tputs
__P((const char *string
, int nlines
, int (*outx
)(int)));
100 /* Forward declarations */
102 /* Functions bound to keys in Readline for Bash users. */
103 static int shell_expand_line
__P((int, int));
104 static int display_shell_version
__P((int, int));
105 static int operate_and_get_next
__P((int, int));
107 static int bash_ignore_filenames
__P((char **));
108 static int bash_ignore_everything
__P((char **));
110 #if defined (BANG_HISTORY)
111 static char *history_expand_line_internal
__P((char *));
112 static int history_expand_line
__P((int, int));
113 static int tcsh_magic_space
__P((int, int));
114 #endif /* BANG_HISTORY */
116 static int alias_expand_line
__P((int, int));
118 #if defined (BANG_HISTORY) && defined (ALIAS)
119 static int history_and_alias_expand_line
__P((int, int));
122 static int bash_forward_shellword
__P((int, int));
123 static int bash_backward_shellword
__P((int, int));
124 static int bash_kill_shellword
__P((int, int));
125 static int bash_backward_kill_shellword
__P((int, int));
127 /* Helper functions for Readline. */
128 static char *restore_tilde
__P((char *, char *));
129 static char *maybe_restore_tilde
__P((char *, char *));
131 static char *bash_filename_rewrite_hook
__P((char *, int));
133 static void bash_directory_expansion
__P((char **));
134 static int bash_filename_stat_hook
__P((char **));
135 static int bash_command_name_stat_hook
__P((char **));
136 static int bash_directory_completion_hook
__P((char **));
137 static int filename_completion_ignore
__P((char **));
138 static int bash_push_line
__P((void));
140 static int executable_completion
__P((const char *, int));
142 static rl_icppfunc_t
*save_directory_hook
__P((void));
143 static void restore_directory_hook
__P((rl_icppfunc_t
));
145 static int directory_exists
__P((const char *));
147 static void cleanup_expansion_error
__P((void));
148 static void maybe_make_readline_line
__P((char *));
149 static void set_up_new_line
__P((char *));
151 static int check_redir
__P((int));
152 static char **attempt_shell_completion
__P((const char *, int, int));
153 static char *variable_completion_function
__P((const char *, int));
154 static char *hostname_completion_function
__P((const char *, int));
155 static char *command_subst_completion_function
__P((const char *, int));
157 static void build_history_completion_array
__P((void));
158 static char *history_completion_generator
__P((const char *, int));
159 static int dynamic_complete_history
__P((int, int));
160 static int bash_dabbrev_expand
__P((int, int));
162 static void initialize_hostname_list
__P((void));
163 static void add_host_name
__P((char *));
164 static void snarf_hosts_from_file
__P((char *));
165 static char **hostnames_matching
__P((char *));
167 static void _ignore_completion_names
__P((char **, sh_ignore_func_t
*));
168 static int name_is_acceptable
__P((const char *));
169 static int test_for_directory
__P((const char *));
170 static int return_zero
__P((const char *));
172 static char *bash_dequote_filename
__P((char *, int));
173 static char *quote_word_break_chars
__P((char *));
174 static void set_filename_bstab
__P((const char *));
175 static char *bash_quote_filename
__P((char *, int, char *));
178 static void putx
__P((int));
180 static int putx
__P((int));
182 static int bash_execute_unix_command
__P((int, int));
183 static void init_unix_command_map
__P((void));
184 static int isolate_sequence
__P((char *, int, int, int *));
186 static int set_saved_history
__P((void));
189 static int posix_edit_macros
__P((int, int));
192 static int bash_event_hook
__P((void));
194 #if defined (PROGRAMMABLE_COMPLETION)
195 static int find_cmd_start
__P((int));
196 static int find_cmd_end
__P((int));
197 static char *find_cmd_name
__P((int, int *, int *));
198 static char *prog_complete_return
__P((const char *, int));
200 static char **prog_complete_matches
;
203 /* Variables used here but defined in other files. */
204 #if defined (BANG_HISTORY)
205 extern int hist_verify
;
208 extern int current_command_line_count
, saved_command_line_count
;
209 extern int last_command_exit_value
;
210 extern int array_needs_making
;
211 extern int posixly_correct
, no_symbolic_links
;
212 extern int sigalrm_seen
;
213 extern char *current_prompt_string
, *ps1_prompt
;
214 extern STRING_INT_ALIST word_token_alist
[];
215 extern sh_builtin_func_t
*last_shell_builtin
, *this_shell_builtin
;
217 /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
218 completion functions which indicate what type of completion should be
219 done (at or before point) that can be bound to key sequences with
220 the readline library. */
221 #define SPECIFIC_COMPLETION_FUNCTIONS
223 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
224 static int bash_specific_completion
__P((int, rl_compentry_func_t
*));
226 static int bash_complete_filename_internal
__P((int));
227 static int bash_complete_username_internal
__P((int));
228 static int bash_complete_hostname_internal
__P((int));
229 static int bash_complete_variable_internal
__P((int));
230 static int bash_complete_command_internal
__P((int));
232 static int bash_complete_filename
__P((int, int));
233 static int bash_possible_filename_completions
__P((int, int));
234 static int bash_complete_username
__P((int, int));
235 static int bash_possible_username_completions
__P((int, int));
236 static int bash_complete_hostname
__P((int, int));
237 static int bash_possible_hostname_completions
__P((int, int));
238 static int bash_complete_variable
__P((int, int));
239 static int bash_possible_variable_completions
__P((int, int));
240 static int bash_complete_command
__P((int, int));
241 static int bash_possible_command_completions
__P((int, int));
243 static char *glob_complete_word
__P((const char *, int));
244 static int bash_glob_completion_internal
__P((int));
245 static int bash_glob_complete_word
__P((int, int));
246 static int bash_glob_expand_word
__P((int, int));
247 static int bash_glob_list_expansions
__P((int, int));
249 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
251 static int edit_and_execute_command
__P((int, int, int, char *));
252 #if defined (VI_MODE)
253 static int vi_edit_and_execute_command
__P((int, int));
254 static int bash_vi_complete
__P((int, int));
256 static int emacs_edit_and_execute_command
__P((int, int));
258 /* Non-zero once initalize_readline () has been called. */
259 int bash_readline_initialized
= 0;
261 /* If non-zero, we do hostname completion, breaking words at `@' and
262 trying to complete the stuff after the `@' from our own internal
264 int perform_hostname_completion
= 1;
266 /* If non-zero, we don't do command completion on an empty line. */
267 int no_empty_command_completion
;
269 /* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the
270 only possible matches. Set to 0 if you want to match filenames if they
271 are the only possible matches, even if FIGNORE says to. */
272 int force_fignore
= 1;
274 /* Perform spelling correction on directory names during word completion */
275 int dircomplete_spelling
= 0;
277 /* Expand directory names during word/filename completion. */
278 #if DIRCOMPLETE_EXPAND_DEFAULT
279 int dircomplete_expand
= 1;
280 int dircomplete_expand_relpath
= 1;
282 int dircomplete_expand
= 0;
283 int dircomplete_expand_relpath
= 0;
286 /* When non-zero, perform `normal' shell quoting on completed filenames
287 even when the completed name contains a directory name with a shell
288 variable referene, so dollar signs in a filename get quoted appropriately.
289 Set to zero to remove dollar sign (and braces or parens as needed) from
290 the set of characters that will be quoted. */
291 int complete_fullquote
= 1;
293 static char *bash_completer_word_break_characters
= " \t\n\"'@><=;|&(:";
294 static char *bash_nohostname_word_break_characters
= " \t\n\"'><=;|&(:";
297 static const char *default_filename_quote_characters
= " \t\n\\\"'@<>=;|&()#$`?*[!:{~"; /*}*/
298 static char *custom_filename_quote_characters
= 0;
299 static char filename_bstab
[256];
301 static rl_hook_func_t
*old_rl_startup_hook
= (rl_hook_func_t
*)NULL
;
303 static int dot_in_path
= 0;
305 /* Set to non-zero when dabbrev-expand is running */
306 static int dabbrev_expand_active
= 0;
308 /* What kind of quoting is performed by bash_quote_filename:
309 COMPLETE_DQUOTE = double-quoting the filename
310 COMPLETE_SQUOTE = single_quoting the filename
311 COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
313 #define COMPLETE_DQUOTE 1
314 #define COMPLETE_SQUOTE 2
315 #define COMPLETE_BSQUOTE 3
316 static int completion_quoting_style
= COMPLETE_BSQUOTE
;
318 /* Flag values for the final argument to bash_default_completion */
319 #define DEFCOMP_CMDPOS 1
321 /* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
322 Called when the shell is put into or out of `posix' mode. */
324 posix_readline_initialize (on_or_off
)
328 rl_variable_bind ("comment-begin", "#");
329 #if defined (VI_MODE)
330 rl_bind_key_in_map (CTRL ('I'), on_or_off
? rl_insert
: rl_complete
, vi_insertion_keymap
);
335 reset_completer_word_break_chars ()
337 rl_completer_word_break_characters
= perform_hostname_completion
? savestring (bash_completer_word_break_characters
) : savestring (bash_nohostname_word_break_characters
);
340 /* When this function returns, rl_completer_word_break_characters points to
341 dynamically allocated memory. */
343 enable_hostname_completion (on_or_off
)
347 char *at
, *nv
, *nval
;
349 old_value
= perform_hostname_completion
;
353 perform_hostname_completion
= 1;
354 rl_special_prefixes
= "$@";
358 perform_hostname_completion
= 0;
359 rl_special_prefixes
= "$";
362 /* Now we need to figure out how to appropriately modify and assign
363 rl_completer_word_break_characters depending on whether we want
364 hostname completion on or off. */
366 /* If this is the first time this has been called
367 (bash_readline_initialized == 0), use the sames values as before, but
368 allocate new memory for rl_completer_word_break_characters. */
370 if (bash_readline_initialized
== 0 &&
371 (rl_completer_word_break_characters
== 0 ||
372 rl_completer_word_break_characters
== rl_basic_word_break_characters
))
375 rl_completer_word_break_characters
= savestring (bash_completer_word_break_characters
);
377 rl_completer_word_break_characters
= savestring (bash_nohostname_word_break_characters
);
381 /* See if we have anything to do. */
382 at
= strchr (rl_completer_word_break_characters
, '@');
383 if ((at
== 0 && on_or_off
== 0) || (at
!= 0 && on_or_off
!= 0))
386 /* We have something to do. Do it. */
387 nval
= (char *)xmalloc (strlen (rl_completer_word_break_characters
) + 1 + on_or_off
);
391 /* Turn it off -- just remove `@' from word break chars. We want
392 to remove all occurrences of `@' from the char list, so we loop
393 rather than just copy the rest of the list over AT. */
394 for (nv
= nval
, at
= rl_completer_word_break_characters
; *at
; )
404 strcpy (nval
+ 1, rl_completer_word_break_characters
);
407 free (rl_completer_word_break_characters
);
408 rl_completer_word_break_characters
= nval
;
414 /* Called once from parse.y if we are going to use readline. */
416 initialize_readline ()
418 rl_command_func_t
*func
;
421 if (bash_readline_initialized
)
424 rl_terminal_name
= get_string_value ("TERM");
426 rl_outstream
= stderr
;
428 /* Allow conditional parsing of the ~/.inputrc file. */
429 rl_readline_name
= "Bash";
431 /* Add bindable names before calling rl_initialize so they may be
432 referenced in the various inputrc files. */
433 rl_add_defun ("shell-expand-line", shell_expand_line
, -1);
435 rl_add_defun ("history-expand-line", history_expand_line
, -1);
436 rl_add_defun ("magic-space", tcsh_magic_space
, -1);
439 rl_add_defun ("shell-forward-word", bash_forward_shellword
, -1);
440 rl_add_defun ("shell-backward-word", bash_backward_shellword
, -1);
441 rl_add_defun ("shell-kill-word", bash_kill_shellword
, -1);
442 rl_add_defun ("shell-backward-kill-word", bash_backward_kill_shellword
, -1);
445 rl_add_defun ("alias-expand-line", alias_expand_line
, -1);
447 rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line
, -1);
451 /* Backwards compatibility. */
452 rl_add_defun ("insert-last-argument", rl_yank_last_arg
, -1);
454 rl_add_defun ("operate-and-get-next", operate_and_get_next
, -1);
455 rl_add_defun ("display-shell-version", display_shell_version
, -1);
456 rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command
, -1);
458 #if defined (BRACE_COMPLETION)
459 rl_add_defun ("complete-into-braces", bash_brace_completion
, -1);
462 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
463 rl_add_defun ("complete-filename", bash_complete_filename
, -1);
464 rl_add_defun ("possible-filename-completions", bash_possible_filename_completions
, -1);
465 rl_add_defun ("complete-username", bash_complete_username
, -1);
466 rl_add_defun ("possible-username-completions", bash_possible_username_completions
, -1);
467 rl_add_defun ("complete-hostname", bash_complete_hostname
, -1);
468 rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions
, -1);
469 rl_add_defun ("complete-variable", bash_complete_variable
, -1);
470 rl_add_defun ("possible-variable-completions", bash_possible_variable_completions
, -1);
471 rl_add_defun ("complete-command", bash_complete_command
, -1);
472 rl_add_defun ("possible-command-completions", bash_possible_command_completions
, -1);
473 rl_add_defun ("glob-complete-word", bash_glob_complete_word
, -1);
474 rl_add_defun ("glob-expand-word", bash_glob_expand_word
, -1);
475 rl_add_defun ("glob-list-expansions", bash_glob_list_expansions
, -1);
478 rl_add_defun ("dynamic-complete-history", dynamic_complete_history
, -1);
479 rl_add_defun ("dabbrev-expand", bash_dabbrev_expand
, -1);
481 /* Bind defaults before binding our custom shell keybindings. */
482 if (RL_ISSTATE(RL_STATE_INITIALIZED
) == 0)
485 /* Bind up our special shell functions. */
486 rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line
, emacs_meta_keymap
);
489 rl_bind_key_if_unbound_in_map ('^', history_expand_line
, emacs_meta_keymap
);
492 rl_bind_key_if_unbound_in_map (CTRL ('O'), operate_and_get_next
, emacs_standard_keymap
);
493 rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version
, emacs_ctlx_keymap
);
495 /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
496 so it is not necessary to allow C-M-j for context switching. Turn
497 off this occasionally confusing behaviour. */
500 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
501 if (func
== rl_vi_editing_mode
)
502 rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap
);
504 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
505 if (func
== rl_vi_editing_mode
)
506 rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap
);
507 #if defined (VI_MODE)
508 rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap
);
511 #if defined (BRACE_COMPLETION)
512 rl_bind_key_if_unbound_in_map ('{', bash_brace_completion
, emacs_meta_keymap
); /*}*/
513 #endif /* BRACE_COMPLETION */
515 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
516 rl_bind_key_if_unbound_in_map ('/', bash_complete_filename
, emacs_meta_keymap
);
517 rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions
, emacs_ctlx_keymap
);
519 /* Have to jump through hoops here because there is a default binding for
520 M-~ (rl_tilde_expand) */
523 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
524 if (func
== 0 || func
== rl_tilde_expand
)
525 rl_bind_keyseq_in_map (kseq
, bash_complete_username
, emacs_meta_keymap
);
527 rl_bind_key_if_unbound_in_map ('~', bash_possible_username_completions
, emacs_ctlx_keymap
);
529 rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname
, emacs_meta_keymap
);
530 rl_bind_key_if_unbound_in_map ('@', bash_possible_hostname_completions
, emacs_ctlx_keymap
);
532 rl_bind_key_if_unbound_in_map ('$', bash_complete_variable
, emacs_meta_keymap
);
533 rl_bind_key_if_unbound_in_map ('$', bash_possible_variable_completions
, emacs_ctlx_keymap
);
535 rl_bind_key_if_unbound_in_map ('!', bash_complete_command
, emacs_meta_keymap
);
536 rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions
, emacs_ctlx_keymap
);
538 rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word
, emacs_meta_keymap
);
539 rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word
, emacs_ctlx_keymap
);
540 rl_bind_key_if_unbound_in_map ('g', bash_glob_list_expansions
, emacs_ctlx_keymap
);
542 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
546 func
= rl_function_of_keyseq (kseq
, emacs_meta_keymap
, (int *)NULL
);
547 if (func
== 0 || func
== rl_tab_insert
)
548 rl_bind_key_in_map (TAB
, dynamic_complete_history
, emacs_meta_keymap
);
550 /* Tell the completer that we want a crack first. */
551 rl_attempted_completion_function
= attempt_shell_completion
;
553 /* Tell the completer that we might want to follow symbolic links or
554 do other expansion on directory names. */
555 set_directory_hook ();
557 rl_filename_rewrite_hook
= bash_filename_rewrite_hook
;
559 rl_filename_stat_hook
= bash_filename_stat_hook
;
561 /* Tell the filename completer we want a chance to ignore some names. */
562 rl_ignore_some_completions_function
= filename_completion_ignore
;
564 /* Bind C-xC-e to invoke emacs and run result as commands. */
565 rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command
, emacs_ctlx_keymap
);
566 #if defined (VI_MODE)
567 rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command
, vi_movement_keymap
);
569 rl_bind_key_if_unbound_in_map ('@', posix_edit_macros
, vi_movement_keymap
);
572 rl_bind_key_in_map ('\\', bash_vi_complete
, vi_movement_keymap
);
573 rl_bind_key_in_map ('*', bash_vi_complete
, vi_movement_keymap
);
574 rl_bind_key_in_map ('=', bash_vi_complete
, vi_movement_keymap
);
577 rl_completer_quote_characters
= "'\"";
579 /* This sets rl_completer_word_break_characters and rl_special_prefixes
580 to the appropriate values, depending on whether or not hostname
581 completion is enabled. */
582 enable_hostname_completion (perform_hostname_completion
);
584 /* characters that need to be quoted when appearing in filenames. */
585 rl_filename_quote_characters
= default_filename_quote_characters
;
586 set_filename_bstab (rl_filename_quote_characters
);
588 rl_filename_quoting_function
= bash_quote_filename
;
589 rl_filename_dequoting_function
= bash_dequote_filename
;
590 rl_char_is_quoted_p
= char_is_quoted
;
593 /* This is superfluous and makes it impossible to use tab completion in
594 vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix()
595 should already have called posix_readline_initialize() when
596 posixly_correct was set. */
598 posix_readline_initialize (1);
601 bash_readline_initialized
= 1;
605 bashline_reinitialize ()
607 bash_readline_initialized
= 0;
611 bashline_set_event_hook ()
613 rl_signal_event_hook
= bash_event_hook
;
617 bashline_reset_event_hook ()
619 rl_signal_event_hook
= 0;
622 /* On Sun systems at least, rl_attempted_completion_function can end up
623 getting set to NULL, and rl_completion_entry_function set to do command
624 word completion if Bash is interrupted while trying to complete a command
625 word. This just resets all the completion functions to the right thing.
626 It's called from throw_to_top_level(). */
631 rl_attempted_completion_function
= attempt_shell_completion
;
632 rl_completion_entry_function
= NULL
;
633 rl_ignore_some_completions_function
= filename_completion_ignore
;
634 rl_filename_quote_characters
= default_filename_quote_characters
;
635 set_filename_bstab (rl_filename_quote_characters
);
637 set_directory_hook ();
638 rl_filename_stat_hook
= bash_filename_stat_hook
;
640 bashline_reset_event_hook ();
642 rl_sort_completion_matches
= 1;
645 /* Contains the line to push into readline. */
646 static char *push_to_readline
= (char *)NULL
;
648 /* Push the contents of push_to_readline into the
653 if (push_to_readline
)
655 rl_insert_text (push_to_readline
);
656 free (push_to_readline
);
657 push_to_readline
= (char *)NULL
;
658 rl_startup_hook
= old_rl_startup_hook
;
663 /* Call this to set the initial text for the next line to read
669 FREE (push_to_readline
);
671 push_to_readline
= savestring (line
);
672 old_rl_startup_hook
= rl_startup_hook
;
673 rl_startup_hook
= bash_push_line
;
679 display_shell_version (count
, c
)
683 show_shell_version (0);
684 putc ('\r', rl_outstream
);
685 fflush (rl_outstream
);
691 /* **************************************************************** */
695 /* **************************************************************** */
697 /* If the user requests hostname completion, then simply build a list
698 of hosts, and complete from that forever more, or at least until
699 HOSTFILE is unset. */
701 /* THIS SHOULD BE A STRINGLIST. */
702 /* The kept list of hostnames. */
703 static char **hostname_list
= (char **)NULL
;
705 /* The physical size of the above list. */
706 static int hostname_list_size
;
708 /* The number of hostnames in the above list. */
709 static int hostname_list_length
;
711 /* Whether or not HOSTNAME_LIST has been initialized. */
712 int hostname_list_initialized
= 0;
714 /* Initialize the hostname completion table. */
716 initialize_hostname_list ()
720 temp
= get_string_value ("HOSTFILE");
722 temp
= get_string_value ("hostname_completion_file");
724 temp
= DEFAULT_HOSTS_FILE
;
726 snarf_hosts_from_file (temp
);
729 hostname_list_initialized
++;
732 /* Add NAME to the list of hosts. */
737 if (hostname_list_length
+ 2 > hostname_list_size
)
739 hostname_list_size
= (hostname_list_size
+ 32) - (hostname_list_size
% 32);
740 hostname_list
= strvec_resize (hostname_list
, hostname_list_size
);
743 hostname_list
[hostname_list_length
++] = savestring (name
);
744 hostname_list
[hostname_list_length
] = (char *)NULL
;
747 #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
750 snarf_hosts_from_file (filename
)
754 char *temp
, buffer
[256], name
[256];
755 register int i
, start
;
757 file
= fopen (filename
, "r");
761 while (temp
= fgets (buffer
, 255, file
))
763 /* Skip to first character. */
764 for (i
= 0; buffer
[i
] && cr_whitespace (buffer
[i
]); i
++)
767 /* If comment or blank line, ignore. */
768 if (buffer
[i
] == '\0' || buffer
[i
] == '#')
771 /* If `preprocessor' directive, do the include. */
772 if (strncmp (buffer
+ i
, "$include ", 9) == 0)
776 /* Find start of filename. */
777 for (incfile
= buffer
+ i
+ 9; *incfile
&& whitespace (*incfile
); incfile
++)
780 /* Find end of filename. */
781 for (t
= incfile
; *t
&& cr_whitespace (*t
) == 0; t
++)
786 snarf_hosts_from_file (incfile
);
790 /* Skip internet address if present. */
791 if (DIGIT (buffer
[i
]))
792 for (; buffer
[i
] && cr_whitespace (buffer
[i
]) == 0; i
++);
794 /* Gobble up names. Each name is separated with whitespace. */
797 for (; cr_whitespace (buffer
[i
]); i
++)
799 if (buffer
[i
] == '\0' || buffer
[i
] == '#')
802 /* Isolate the current word. */
803 for (start
= i
; buffer
[i
] && cr_whitespace (buffer
[i
]) == 0; i
++)
807 strncpy (name
, buffer
+ start
, i
- start
);
808 name
[i
- start
] = '\0';
809 add_host_name (name
);
815 /* Return the hostname list. */
819 if (hostname_list_initialized
== 0)
820 initialize_hostname_list ();
821 return (hostname_list
);
825 clear_hostname_list ()
829 if (hostname_list_initialized
== 0)
831 for (i
= 0; i
< hostname_list_length
; i
++)
832 free (hostname_list
[i
]);
833 hostname_list_length
= hostname_list_initialized
= 0;
836 /* Return a NULL terminated list of hostnames which begin with TEXT.
837 Initialize the hostname list the first time if necessary.
838 The array is malloc ()'ed, but not the individual strings. */
840 hostnames_matching (text
)
843 register int i
, len
, nmatch
, rsize
;
846 if (hostname_list_initialized
== 0)
847 initialize_hostname_list ();
849 if (hostname_list_initialized
== 0)
850 return ((char **)NULL
);
852 /* Special case. If TEXT consists of nothing, then the whole list is
856 result
= strvec_create (1 + hostname_list_length
);
857 for (i
= 0; i
< hostname_list_length
; i
++)
858 result
[i
] = hostname_list
[i
];
859 result
[i
] = (char *)NULL
;
863 /* Scan until found, or failure. */
865 result
= (char **)NULL
;
866 for (i
= nmatch
= rsize
= 0; i
< hostname_list_length
; i
++)
868 if (STREQN (text
, hostname_list
[i
], len
) == 0)
871 /* OK, it matches. Add it to the list. */
872 if (nmatch
>= (rsize
- 1))
874 rsize
= (rsize
+ 16) - (rsize
% 16);
875 result
= strvec_resize (result
, rsize
);
878 result
[nmatch
++] = hostname_list
[i
];
881 result
[nmatch
] = (char *)NULL
;
885 /* The equivalent of the Korn shell C-o operate-and-get-next-history-line
887 static int saved_history_line_to_use
= -1;
888 static int last_saved_history_line
= -1;
890 #define HISTORY_FULL() (history_is_stifled () && history_length >= history_max_entries)
895 /* XXX - compensate for assumption that history was `shuffled' if it was
897 if (HISTORY_FULL () &&
898 hist_last_line_added
== 0 &&
899 saved_history_line_to_use
< history_length
- 1)
900 saved_history_line_to_use
++;
902 if (saved_history_line_to_use
>= 0)
904 rl_get_previous_history (history_length
- saved_history_line_to_use
, 0);
905 last_saved_history_line
= saved_history_line_to_use
;
907 saved_history_line_to_use
= -1;
908 rl_startup_hook
= old_rl_startup_hook
;
913 operate_and_get_next (count
, c
)
918 /* Accept the current line. */
921 /* Find the current line, and find the next line to use. */
922 where
= where_history ();
924 if (HISTORY_FULL () || (where
>= history_length
- 1))
925 saved_history_line_to_use
= where
;
927 saved_history_line_to_use
= where
+ 1;
929 old_rl_startup_hook
= rl_startup_hook
;
930 rl_startup_hook
= set_saved_history
;
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
) ();
986 save_parser_state (&ps
);
987 r
= parse_and_execute (command
, (editing_mode
== VI_EDITING_MODE
) ? "v" : "C-xC-e", SEVAL_NOHIST
);
988 restore_parser_state (&ps
);
989 if (rl_prep_term_function
)
990 (*rl_prep_term_function
) (metaflag
);
992 current_command_line_count
= saved_command_line_count
;
994 /* Now erase the contents of the current line and undo the effects of the
995 rl_accept_line() above. We don't even want to make the text we just
996 executed available for undoing. */
997 rl_line_buffer
[0] = '\0'; /* XXX */
998 rl_point
= rl_end
= 0;
1000 rl_readline_state
= rrs
;
1002 rl_forced_update_display ();
1007 #if defined (VI_MODE)
1009 vi_edit_and_execute_command (count
, c
)
1012 if (posixly_correct
)
1013 return (edit_and_execute_command (count
, c
, VI_EDITING_MODE
, POSIX_VI_EDIT_COMMAND
));
1015 return (edit_and_execute_command (count
, c
, VI_EDITING_MODE
, VI_EDIT_COMMAND
));
1017 #endif /* VI_MODE */
1020 emacs_edit_and_execute_command (count
, c
)
1023 return (edit_and_execute_command (count
, c
, EMACS_EDITING_MODE
, EMACS_EDIT_COMMAND
));
1028 posix_edit_macros (count
, key
)
1032 char alias_name
[3], *alias_value
, *macro
;
1035 alias_name
[0] = '_';
1037 alias_name
[2] = '\0';
1039 alias_value
= get_alias_value (alias_name
);
1040 if (alias_value
&& *alias_value
)
1042 macro
= savestring (alias_value
);
1043 rl_push_macro_input (macro
);
1049 /* Bindable commands that move `shell-words': that is, sequences of
1050 non-unquoted-metacharacters. */
1052 #define WORDDELIM(c) (shellmeta(c) || shellblank(c))
1055 bash_forward_shellword (count
, key
)
1063 return (bash_backward_shellword (-count
, key
));
1065 /* The tricky part of this is deciding whether or not the first character
1066 we're on is an unquoted metacharacter. Not completely handled yet. */
1067 /* XXX - need to test this stuff with backslash-escaped shell
1068 metacharacters and unclosed single- and double-quoted strings. */
1081 /* Are we in a quoted string? If we are, move to the end of the quoted
1082 string and continue the outer loop. We only want quoted strings, not
1083 backslash-escaped characters, but char_is_quoted doesn't
1085 if (char_is_quoted (rl_line_buffer
, p
) && p
> 0 && rl_line_buffer
[p
-1] != '\\')
1088 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1089 while (p
< rl_end
&& char_is_quoted (rl_line_buffer
, p
));
1094 /* Rest of code assumes we are not in a quoted string. */
1095 /* Move forward until we hit a non-metacharacter. */
1096 while (p
< rl_end
&& (c
= rl_line_buffer
[p
]) && WORDDELIM (c
))
1101 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1102 continue; /* straight back to loop, don't increment p */
1104 if (p
< rl_end
&& rl_line_buffer
[p
])
1105 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1108 p
= skip_to_delim (rl_line_buffer
, ++p
, "'", SD_NOJMP
);
1111 p
= skip_to_delim (rl_line_buffer
, ++p
, "\"", SD_NOJMP
);
1119 if (rl_line_buffer
[p
] == 0 || p
== rl_end
)
1126 /* Now move forward until we hit a non-quoted metacharacter or EOL */
1127 while (p
< rl_end
&& (c
= rl_line_buffer
[p
]) && WORDDELIM (c
) == 0)
1132 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1133 continue; /* straight back to loop, don't increment p */
1135 if (p
< rl_end
&& rl_line_buffer
[p
])
1136 ADVANCE_CHAR (rl_line_buffer
, slen
, p
);
1139 p
= skip_to_delim (rl_line_buffer
, ++p
, "'", SD_NOJMP
);
1142 p
= skip_to_delim (rl_line_buffer
, ++p
, "\"", SD_NOJMP
);
1150 if (p
== rl_end
|| rl_line_buffer
[p
] == 0)
1164 bash_backward_shellword (count
, key
)
1172 return (bash_forward_shellword (-count
, key
));
1185 /* Move backward until we hit a non-metacharacter. */
1188 c
= rl_line_buffer
[p
];
1189 if (WORDDELIM (c
) && char_is_quoted (rl_line_buffer
, p
) == 0)
1190 BACKUP_CHAR (rl_line_buffer
, slen
, p
);
1200 /* Now move backward until we hit a metacharacter or BOL. */
1203 c
= rl_line_buffer
[p
];
1204 if (WORDDELIM (c
) && char_is_quoted (rl_line_buffer
, p
) == 0)
1206 BACKUP_CHAR (rl_line_buffer
, slen
, p
);
1217 bash_kill_shellword (count
, key
)
1223 return (bash_backward_kill_shellword (-count
, key
));
1226 bash_forward_shellword (count
, key
);
1229 rl_kill_text (p
, rl_point
);
1232 if (rl_editing_mode
== 1) /* 1 == emacs_mode */
1239 bash_backward_kill_shellword (count
, key
)
1245 return (bash_kill_shellword (-count
, key
));
1248 bash_backward_shellword (count
, key
);
1251 rl_kill_text (p
, rl_point
);
1253 if (rl_editing_mode
== 1) /* 1 == emacs_mode */
1260 /* **************************************************************** */
1262 /* How To Do Shell Completion */
1264 /* **************************************************************** */
1266 #define COMMAND_SEPARATORS ";|&{(`"
1268 #define COMMAND_SEPARATORS_PLUS_WS ";|&{(` \t"
1271 /* check for redirections and other character combinations that are not
1272 command separators */
1277 register int this_char
, prev_char
;
1279 /* Handle the two character tokens `>&', `<&', and `>|'.
1280 We are not in a command position after one of these. */
1281 this_char
= rl_line_buffer
[ti
];
1282 prev_char
= (ti
> 0) ? rl_line_buffer
[ti
- 1] : 0;
1284 if ((this_char
== '&' && (prev_char
== '<' || prev_char
== '>')) ||
1285 (this_char
== '|' && prev_char
== '>'))
1287 else if (this_char
== '{' && prev_char
== '$') /*}*/
1290 else if (this_char
== '(' && prev_char
== '$') /*)*/
1292 else if (this_char
== '(' && prev_char
== '<') /*)*/
1294 #if defined (EXTENDED_GLOB)
1295 else if (extended_glob
&& this_char
== '(' && prev_char
== '!') /*)*/
1299 else if (char_is_quoted (rl_line_buffer
, ti
))
1304 #if defined (PROGRAMMABLE_COMPLETION)
1306 * XXX - because of the <= start test, and setting os = s+1, this can
1307 * potentially return os > start. This is probably not what we want to
1308 * happen, but fix later after 2.05a-release.
1311 find_cmd_start (start
)
1314 register int s
, os
, ns
;
1317 /* Flags == SD_NOJMP only because we want to skip over command substitutions
1318 in assignment statements. Have to test whether this affects `standalone'
1319 command substitutions as individual words. */
1320 while (((s
= skip_to_delim (rl_line_buffer
, os
, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
/*|SD_NOSKIPCMD*/)) <= start
) &&
1323 /* Handle >| token crudely; treat as > not | */
1324 if (rl_line_buffer
[s
] == '|' && rl_line_buffer
[s
-1] == '>')
1326 ns
= skip_to_delim (rl_line_buffer
, s
+1, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
/*|SD_NOSKIPCMD*/);
1327 if (ns
> start
|| rl_line_buffer
[ns
] == 0)
1343 e
= skip_to_delim (rl_line_buffer
, end
, COMMAND_SEPARATORS
, SD_NOJMP
|SD_COMPLETE
);
1348 find_cmd_name (start
, sp
, ep
)
1355 for (s
= start
; whitespace (rl_line_buffer
[s
]); s
++)
1358 /* skip until a shell break character */
1359 e
= skip_to_delim (rl_line_buffer
, s
, "()<>;&| \t\n", SD_NOJMP
|SD_COMPLETE
);
1361 name
= substring (rl_line_buffer
, s
, e
);
1372 prog_complete_return (text
, matchnum
)
1381 if (prog_complete_matches
== 0 || prog_complete_matches
[ind
] == 0)
1382 return (char *)NULL
;
1383 return (prog_complete_matches
[ind
++]);
1386 #endif /* PROGRAMMABLE_COMPLETION */
1388 /* Try and catch completion attempts that are syntax errors or otherwise
1391 invalid_completion (text
, ind
)
1397 /* If we don't catch these here, the next clause will */
1398 if (ind
> 0 && rl_line_buffer
[ind
] == '(' && /*)*/
1399 member (rl_line_buffer
[ind
-1], "$<>"))
1403 while (pind
> 0 && whitespace (rl_line_buffer
[pind
]))
1405 /* If we have only whitespace preceding a paren, it's valid */
1406 if (ind
>= 0 && pind
<= 0 && rl_line_buffer
[ind
] == '(') /*)*/
1408 /* Flag the invalid completions, which are mostly syntax errors */
1409 if (ind
> 0 && rl_line_buffer
[ind
] == '(' && /*)*/
1410 member (rl_line_buffer
[pind
], COMMAND_SEPARATORS
) == 0)
1416 /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
1417 at START and END. Return an array of matches, or NULL if none. */
1419 attempt_shell_completion (text
, start
, end
)
1423 int in_command_position
, ti
, saveti
, qc
, dflags
;
1424 char **matches
, *command_separator_chars
;
1425 #if defined (PROGRAMMABLE_COMPLETION)
1426 int have_progcomps
, was_assignment
;
1429 command_separator_chars
= COMMAND_SEPARATORS
;
1430 matches
= (char **)NULL
;
1431 rl_ignore_some_completions_function
= filename_completion_ignore
;
1433 rl_filename_quote_characters
= default_filename_quote_characters
;
1434 set_filename_bstab (rl_filename_quote_characters
);
1435 set_directory_hook ();
1436 rl_filename_stat_hook
= bash_filename_stat_hook
;
1438 rl_sort_completion_matches
= 1; /* sort by default */
1440 /* Determine if this could be a command word. It is if it appears at
1441 the start of the line (ignoring preceding whitespace), or if it
1442 appears after a character that separates commands. It cannot be a
1443 command word if we aren't at the top-level prompt. */
1447 while ((ti
> -1) && (whitespace (rl_line_buffer
[ti
])))
1451 /* If this is an open quote, maybe we're trying to complete a quoted
1453 if (ti
>= 0 && (rl_line_buffer
[ti
] == '"' || rl_line_buffer
[ti
] == '\''))
1455 qc
= rl_line_buffer
[ti
];
1457 while (ti
> -1 && (whitespace (rl_line_buffer
[ti
])))
1462 in_command_position
= 0;
1465 /* Only do command completion at the start of a line when we
1466 are prompting at the top level. */
1467 if (current_prompt_string
== ps1_prompt
)
1468 in_command_position
++;
1469 else if (parser_in_command_position ())
1470 in_command_position
++;
1472 else if (member (rl_line_buffer
[ti
], command_separator_chars
))
1474 in_command_position
++;
1476 if (check_redir (ti
) == 1)
1477 in_command_position
= 0;
1481 /* This still could be in command position. It is possible
1482 that all of the previous words on the line are variable
1486 if (in_command_position
&& invalid_completion (text
, ti
))
1488 rl_attempted_completion_over
= 1;
1489 return ((char **)NULL
);
1492 /* Check that we haven't incorrectly flagged a closed command substitution
1493 as indicating we're in a command position. */
1494 if (in_command_position
&& ti
>= 0 && rl_line_buffer
[ti
] == '`' &&
1495 *text
!= '`' && unclosed_pair (rl_line_buffer
, end
, "`") == 0)
1496 in_command_position
= 0;
1498 /* Special handling for command substitution. If *TEXT is a backquote,
1499 it can be the start or end of an old-style command substitution, or
1500 unmatched. If it's unmatched, both calls to unclosed_pair will
1501 succeed. Don't bother if readline found a single quote and we are
1502 completing on the substring. */
1503 if (*text
== '`' && rl_completion_quote_character
!= '\'' &&
1504 (in_command_position
|| (unclosed_pair (rl_line_buffer
, start
, "`") &&
1505 unclosed_pair (rl_line_buffer
, end
, "`"))))
1506 matches
= rl_completion_matches (text
, command_subst_completion_function
);
1508 #if defined (PROGRAMMABLE_COMPLETION)
1509 /* Attempt programmable completion. */
1510 have_progcomps
= prog_completion_enabled
&& (progcomp_size () > 0);
1511 if (matches
== 0 && (in_command_position
== 0 || text
[0] == '\0') &&
1512 current_prompt_string
== ps1_prompt
)
1514 int s
, e
, s1
, e1
, os
, foundcs
;
1517 /* XXX - don't free the members */
1518 if (prog_complete_matches
)
1519 free (prog_complete_matches
);
1520 prog_complete_matches
= (char **)NULL
;
1525 s
= find_cmd_start (os
);
1526 e
= find_cmd_end (end
);
1529 /* Don't read past the end of rl_line_buffer */
1535 /* Or past point if point is within an assignment statement */
1536 else if (was_assignment
&& s
> rl_point
)
1541 /* Skip over assignment statements preceding a command name. If we
1542 don't find a command name at all, we can perform command name
1543 completion. If we find a partial command name, we should perform
1544 command name completion on it. */
1546 n
= find_cmd_name (s
, &s1
, &e1
);
1549 while (was_assignment
= assignment (n
, 0));
1550 s
= s1
; /* reset to index where name begins */
1552 /* s == index of where command name begins (reset above)
1553 e == end of current command, may be end of line
1554 s1 = index of where command name begins
1555 e1 == index of where command name ends
1556 start == index of where word to be completed begins
1557 end == index of where word to be completed ends
1558 if (s == start) we are doing command word completion for sure
1559 if (e1 == end) we are at the end of the command name and completing it */
1560 if (start
== 0 && end
== 0 && e
!= 0 && text
[0] == '\0') /* beginning of non-empty line */
1562 else if (start
== end
&& start
== s1
&& e
!= 0 && e1
> end
) /* beginning of command name, leading whitespace */
1564 else if (e
== 0 && e
== s
&& text
[0] == '\0' && have_progcomps
) /* beginning of empty line */
1565 prog_complete_matches
= programmable_completions ("_EmptycmD_", text
, s
, e
, &foundcs
);
1566 else if (start
== end
&& text
[0] == '\0' && s1
> start
&& whitespace (rl_line_buffer
[start
]))
1567 foundcs
= 0; /* whitespace before command name */
1568 else if (e
> s
&& was_assignment
== 0 && e1
== end
&& rl_line_buffer
[e
] == 0 && whitespace (rl_line_buffer
[e
-1]) == 0)
1570 /* not assignment statement, but still want to perform command
1571 completion if we are composing command word. */
1573 in_command_position
= s
== start
&& STREQ (n
, text
); /* XXX */
1575 else if (e
> s
&& was_assignment
== 0 && have_progcomps
)
1577 prog_complete_matches
= programmable_completions (n
, text
, s
, e
, &foundcs
);
1578 /* command completion if programmable completion fails */
1579 in_command_position
= s
== start
&& STREQ (n
, text
); /* XXX */
1581 /* empty command name following command separator */
1582 else if (s
>= e
&& n
[0] == '\0' && text
[0] == '\0' && start
> 0 &&
1583 was_assignment
== 0 && member (rl_line_buffer
[start
-1], COMMAND_SEPARATORS
))
1586 in_command_position
= 1;
1588 else if (s
>= e
&& n
[0] == '\0' && text
[0] == '\0' && start
> 0)
1590 foundcs
= 0; /* empty command name following assignments */
1591 in_command_position
= was_assignment
;
1593 else if (s
== start
&& e
== end
&& STREQ (n
, text
) && start
> 0)
1595 foundcs
= 0; /* partial command name following assignments */
1596 in_command_position
= 1;
1601 /* XXX - if we found a COMPSPEC for the command, just return whatever
1602 the programmable completion code returns, and disable the default
1603 filename completion that readline will do unless the COPT_DEFAULT
1604 option has been set with the `-o default' option to complete or
1608 pcomp_set_readline_variables (foundcs
, 1);
1609 /* Turn what the programmable completion code returns into what
1610 readline wants. I should have made compute_lcd_of_matches
1612 matches
= rl_completion_matches (text
, prog_complete_return
);
1613 if ((foundcs
& COPT_DEFAULT
) == 0)
1614 rl_attempted_completion_over
= 1; /* no default */
1615 if (matches
|| ((foundcs
& COPT_BASHDEFAULT
) == 0))
1624 if (in_command_position
)
1625 dflags
|= DEFCOMP_CMDPOS
;
1626 matches
= bash_default_completion (text
, start
, end
, qc
, dflags
);
1633 bash_default_completion (text
, start
, end
, qc
, compflags
)
1635 int start
, end
, qc
, compflags
;
1639 matches
= (char **)NULL
;
1641 /* New posix-style command substitution or variable name? */
1642 if (!matches
&& *text
== '$')
1644 if (qc
!= '\'' && text
[1] == '(') /* ) */
1645 matches
= rl_completion_matches (text
, command_subst_completion_function
);
1648 matches
= rl_completion_matches (text
, variable_completion_function
);
1649 /* If a single match, see if it expands to a directory name and append
1650 a slash if it does. This requires us to expand the variable name,
1651 so we don't want to display errors if the variable is unset. This
1652 can happen with dynamic variables whose value has never been
1654 if (matches
&& matches
[0] && matches
[1] == 0)
1656 t
= savestring (matches
[0]);
1657 bash_filename_stat_hook (&t
);
1658 /* doesn't use test_for_directory because that performs tilde
1661 rl_completion_append_character
= '/';
1667 /* If the word starts in `~', and there is no slash in the word, then
1668 try completing this word as a username. */
1669 if (matches
== 0 && *text
== '~' && mbschr (text
, '/') == 0)
1670 matches
= rl_completion_matches (text
, rl_username_completion_function
);
1672 /* Another one. Why not? If the word starts in '@', then look through
1673 the world of known hostnames for completion first. */
1674 if (matches
== 0 && perform_hostname_completion
&& *text
== '@')
1675 matches
= rl_completion_matches (text
, hostname_completion_function
);
1677 /* And last, (but not least) if this word is in a command position, then
1678 complete over possible command names, including aliases, functions,
1679 and command names. */
1680 if (matches
== 0 && (compflags
& DEFCOMP_CMDPOS
))
1682 /* If END == START and text[0] == 0, we are trying to complete an empty
1684 if (no_empty_command_completion
&& end
== start
&& text
[0] == '\0')
1686 matches
= (char **)NULL
;
1687 rl_ignore_some_completions_function
= bash_ignore_everything
;
1691 #define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x))
1694 matches
= rl_completion_matches (text
, command_word_completion_function
);
1696 /* If we are attempting command completion and nothing matches, we
1697 do not want readline to perform filename completion for us. We
1698 still want to be able to complete partial pathnames, so set the
1699 completion ignore function to something which will remove
1700 filenames and leave directories in the match list. */
1701 if (matches
== (char **)NULL
)
1702 rl_ignore_some_completions_function
= bash_ignore_filenames
;
1703 else if (matches
[1] == 0 && CMD_IS_DIR(matches
[0]) && dot_in_path
== 0)
1704 /* If we found a single match, without looking in the current
1705 directory (because it's not in $PATH), but the found name is
1706 also a command in the current directory, suppress appending any
1707 terminating character, since it's ambiguous. */
1709 rl_completion_suppress_append
= 1;
1710 rl_filename_completion_desired
= 0;
1712 else if (matches
[0] && matches
[1] && STREQ (matches
[0], matches
[1]) && CMD_IS_DIR (matches
[0]))
1713 /* There are multiple instances of the same match (duplicate
1714 completions haven't yet been removed). In this case, all of
1715 the matches will be the same, and the duplicate removal code
1716 will distill them all down to one. We turn on
1717 rl_completion_suppress_append for the same reason as above.
1718 Remember: we only care if there's eventually a single unique
1719 completion. If there are multiple completions this won't
1720 make a difference and the problem won't occur. */
1722 rl_completion_suppress_append
= 1;
1723 rl_filename_completion_desired
= 0;
1728 /* This could be a globbing pattern, so try to expand it using pathname
1730 if (!matches
&& glob_pattern_p (text
))
1732 matches
= rl_completion_matches (text
, glob_complete_word
);
1733 /* A glob expression that matches more than one filename is problematic.
1734 If we match more than one filename, punt. */
1735 if (matches
&& matches
[1] && rl_completion_type
== TAB
)
1737 strvec_dispose (matches
);
1738 matches
= (char **)0;
1740 else if (matches
&& matches
[1] && rl_completion_type
== '!')
1742 rl_completion_suppress_append
= 1;
1743 rl_filename_completion_desired
= 0;
1751 bash_command_name_stat_hook (name
)
1754 char *cname
, *result
;
1756 /* If it's not something we're going to look up in $PATH, just call the
1757 normal filename stat hook. */
1758 if (absolute_program (*name
))
1759 return (bash_filename_stat_hook (name
));
1762 /* XXX - we could do something here with converting aliases, builtins,
1763 and functions into something that came out as executable, but we don't. */
1764 result
= search_for_command (cname
, 0);
1774 executable_completion (filename
, searching_path
)
1775 const char *filename
;
1781 f
= savestring (filename
);
1782 bash_directory_completion_hook (&f
);
1784 r
= searching_path
? executable_file (f
) : executable_or_directory (f
);
1789 /* This is the function to call when the word to complete is in a position
1790 where a command word can be found. It grovels $PATH, looking for commands
1791 that match. It also scans aliases, function names, and the shell_builtin
1794 command_word_completion_function (hint_text
, state
)
1795 const char *hint_text
;
1798 static char *hint
= (char *)NULL
;
1799 static char *path
= (char *)NULL
;
1800 static char *val
= (char *)NULL
;
1801 static char *filename_hint
= (char *)NULL
;
1802 static char *fnhint
= (char *)NULL
;
1803 static char *dequoted_hint
= (char *)NULL
;
1804 static char *directory_part
= (char *)NULL
;
1805 static char **glob_matches
= (char **)NULL
;
1806 static int path_index
, hint_len
, dequoted_len
, istate
, igncase
;
1807 static int mapping_over
, local_index
, searching_path
, hint_is_dir
;
1808 static int old_glob_ignore_case
, globpat
;
1809 static SHELL_VAR
**varlist
= (SHELL_VAR
**)NULL
;
1811 static alias_t
**alias_list
= (alias_t
**)NULL
;
1815 /* We have to map over the possibilities for command words. If we have
1816 no state, then make one just for that purpose. */
1819 rl_filename_stat_hook
= bash_command_name_stat_hook
;
1821 if (dequoted_hint
&& dequoted_hint
!= hint
)
1822 free (dequoted_hint
);
1826 mapping_over
= searching_path
= 0;
1827 hint_is_dir
= CMD_IS_DIR (hint_text
);
1830 temp
= rl_variable_value ("completion-ignore-case");
1831 igncase
= RL_BOOLEAN_VARIABLE_VALUE (temp
);
1835 free (glob_matches
);
1836 glob_matches
= (char **)NULL
;
1839 globpat
= glob_pattern_p (hint_text
);
1841 /* If this is an absolute program name, do not check it against
1842 aliases, reserved words, functions or builtins. We must check
1843 whether or not it is unique, and, if so, whether that filename
1845 if (globpat
|| absolute_program (hint_text
))
1847 /* Perform tilde expansion on what's passed, so we don't end up
1848 passing filenames with tildes directly to stat(). The rest of
1849 the shell doesn't do variable expansion on the word following
1850 the tilde, so we don't do it here even if direxpand is set. */
1851 if (*hint_text
== '~')
1853 hint
= bash_tilde_expand (hint_text
, 0);
1854 directory_part
= savestring (hint_text
);
1855 temp
= strchr (directory_part
, '/');
1860 free (directory_part
);
1861 directory_part
= (char *)NULL
;
1864 else if (dircomplete_expand
)
1866 hint
= savestring (hint_text
);
1867 bash_directory_completion_hook (&hint
);
1870 hint
= savestring (hint_text
);
1872 dequoted_hint
= hint
;
1873 /* If readline's completer found a quote character somewhere, but
1874 didn't set the quote character, there must have been a quote
1875 character embedded in the filename. It can't be at the start of
1876 the filename, so we need to dequote the filename before we look
1877 in the file system for it. */
1878 if (rl_completion_found_quote
&& rl_completion_quote_character
== 0)
1880 dequoted_hint
= bash_dequote_filename (hint
, 0);
1882 hint
= dequoted_hint
;
1884 dequoted_len
= hint_len
= strlen (hint
);
1887 free (filename_hint
);
1889 fnhint
= filename_hint
= savestring (hint
);
1900 if (dircomplete_expand
&& path_dot_or_dotdot (filename_hint
))
1902 dircomplete_expand
= 0;
1903 set_directory_hook ();
1904 dircomplete_expand
= 1;
1911 dequoted_hint
= hint
= savestring (hint_text
);
1912 dequoted_len
= hint_len
= strlen (hint
);
1914 if (rl_completion_found_quote
&& rl_completion_quote_character
== 0)
1916 dequoted_hint
= bash_dequote_filename (hint
, 0);
1917 dequoted_len
= strlen (dequoted_hint
);
1920 path
= get_string_value ("PATH");
1921 path_index
= dot_in_path
= 0;
1923 /* Initialize the variables for each type of command word. */
1929 varlist
= all_visible_functions ();
1935 alias_list
= all_aliases ();
1939 /* mapping_over says what we are currently hacking. Note that every case
1940 in this list must fall through when there are no more possibilities. */
1942 switch (mapping_over
)
1944 case 0: /* Aliases come first. */
1946 while (alias_list
&& alias_list
[local_index
])
1948 register char *alias
;
1950 alias
= alias_list
[local_index
++]->name
;
1952 if (STREQN (alias
, hint
, hint_len
))
1953 return (savestring (alias
));
1959 case 1: /* Then shell reserved words. */
1961 while (word_token_alist
[local_index
].word
)
1963 register char *reserved_word
;
1965 reserved_word
= word_token_alist
[local_index
++].word
;
1967 if (STREQN (reserved_word
, hint
, hint_len
))
1968 return (savestring (reserved_word
));
1974 case 2: /* Then function names. */
1975 while (varlist
&& varlist
[local_index
])
1977 register char *varname
;
1979 varname
= varlist
[local_index
++]->name
;
1981 if (STREQN (varname
, hint
, hint_len
))
1982 return (savestring (varname
));
1987 case 3: /* Then shell builtins. */
1988 for (; local_index
< num_shell_builtins
; local_index
++)
1990 /* Ignore it if it doesn't have a function pointer or if it
1991 is not currently enabled. */
1992 if (!shell_builtins
[local_index
].function
||
1993 (shell_builtins
[local_index
].flags
& BUILTIN_ENABLED
) == 0)
1996 if (STREQN (shell_builtins
[local_index
].name
, hint
, hint_len
))
1998 int i
= local_index
++;
2000 return (savestring (shell_builtins
[i
].name
));
2008 /* Limited support for completing command words with globbing chars. Only
2009 a single match (multiple matches that end up reducing the number of
2010 characters in the common prefix are bad) will ever be returned on
2011 regular completion. */
2016 glob_ignore_case
= igncase
;
2017 glob_matches
= shell_glob_filename (hint
);
2018 glob_ignore_case
= old_glob_ignore_case
;
2020 if (GLOB_FAILED (glob_matches
) || glob_matches
== 0)
2022 glob_matches
= (char **)NULL
;
2023 return ((char *)NULL
);
2028 if (glob_matches
[1] && rl_completion_type
== TAB
) /* multiple matches are bad */
2029 return ((char *)NULL
);
2032 while (val
= glob_matches
[local_index
++])
2034 if (executable_or_directory (val
))
2036 if (*hint_text
== '~' && directory_part
)
2038 temp
= maybe_restore_tilde (val
, directory_part
);
2047 glob_ignore_case
= old_glob_ignore_case
;
2048 return ((char *)NULL
);
2051 /* If the text passed is a directory in the current directory, return it
2052 as a possible match. Executables in directories in the current
2053 directory can be specified using relative pathnames and successfully
2054 executed even when `.' is not in $PATH. */
2057 hint_is_dir
= 0; /* only return the hint text once */
2058 return (savestring (hint_text
));
2061 /* Repeatedly call filename_completion_function while we have
2062 members of PATH left. Question: should we stat each file?
2063 Answer: we call executable_file () on each file. */
2066 istate
= (val
!= (char *)NULL
);
2072 /* Get the next directory from the path. If there is none, then we
2074 if (path
== 0 || path
[path_index
] == 0 ||
2075 (current_path
= extract_colon_unit (path
, &path_index
)) == 0)
2076 return ((char *)NULL
);
2079 if (*current_path
== 0)
2081 free (current_path
);
2082 current_path
= savestring (".");
2085 if (*current_path
== '~')
2089 t
= bash_tilde_expand (current_path
, 0);
2090 free (current_path
);
2094 if (current_path
[0] == '.' && current_path
[1] == '\0')
2097 if (fnhint
&& fnhint
!= filename_hint
)
2100 free (filename_hint
);
2102 filename_hint
= sh_makepath (current_path
, hint
, 0);
2103 /* Need a quoted version (though it doesn't matter much in most
2104 cases) because rl_filename_completion_function dequotes the
2105 filename it gets, assuming that it's been quoted as part of
2106 the input line buffer. */
2107 if (strpbrk (filename_hint
, "\"'\\"))
2108 fnhint
= sh_backslash_quote (filename_hint
, filename_bstab
, 0);
2110 fnhint
= filename_hint
;
2111 free (current_path
); /* XXX */
2115 val
= rl_filename_completion_function (fnhint
, istate
);
2116 if (mapping_over
== 4 && dircomplete_expand
)
2117 set_directory_hook ();
2123 /* If the hint text is an absolute program, then don't bother
2124 searching through PATH. */
2125 if (absolute_program (hint
))
2126 return ((char *)NULL
);
2132 int match
, freetemp
;
2134 if (absolute_program (hint
))
2137 match
= strncmp (val
, hint
, hint_len
) == 0;
2139 match
= strncasecmp (val
, hint
, hint_len
) == 0;
2141 /* If we performed tilde expansion, restore the original
2143 if (*hint_text
== '~')
2144 temp
= maybe_restore_tilde (val
, directory_part
);
2146 temp
= savestring (val
);
2151 temp
= strrchr (val
, '/');
2157 freetemp
= match
= strncmp (temp
, hint
, hint_len
) == 0;
2159 freetemp
= match
= strncasecmp (temp
, hint
, hint_len
) == 0;
2161 temp
= savestring (temp
);
2164 freetemp
= match
= 0;
2167 /* If we have found a match, and it is an executable file, return it.
2168 We don't return directory names when searching $PATH, since the
2169 bash execution code won't find executables in directories which
2170 appear in directories in $PATH when they're specified using
2171 relative pathnames. */
2173 /* If we're not searching $PATH and we have a relative pathname, we
2174 need to re-canonicalize it before testing whether or not it's an
2175 executable or a directory so the shell treats .. relative to $PWD
2176 according to the physical/logical option. The shell already
2177 canonicalizes the directory name in order to tell readline where
2178 to look, so not doing it here will be inconsistent. */
2179 /* XXX -- currently not used -- will introduce more inconsistency,
2180 since shell does not canonicalize ../foo before passing it to
2182 if (match
&& searching_path
== 0 && *val
== '.')
2186 t
= get_working_directory ("command-word-completion");
2187 t1
= make_absolute (val
, t
);
2189 cval
= sh_canonpath (t1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
2195 if (match
&& executable_completion ((searching_path
? val
: cval
), searching_path
))
2200 val
= ""; /* So it won't be NULL. */
2215 /* Completion inside an unterminated command substitution. */
2217 command_subst_completion_function (text
, state
)
2221 static char **matches
= (char **)NULL
;
2222 static const char *orig_start
;
2223 static char *filename_text
= (char *)NULL
;
2224 static int cmd_index
, start_len
;
2230 free (filename_text
);
2234 else if (*text
== '$' && text
[1] == '(') /* ) */
2236 /* If the text was quoted, suppress any quote character that the
2237 readline completion code would insert. */
2238 rl_completion_suppress_quote
= 1;
2239 start_len
= text
- orig_start
;
2240 filename_text
= savestring (text
);
2245 * At this point we can entertain the idea of re-parsing
2246 * `filename_text' into a (possibly incomplete) command name and
2247 * arguments, and doing completion based on that. This is
2248 * currently very rudimentary, but it is a small improvement.
2250 for (value
= filename_text
+ strlen (filename_text
) - 1; value
> filename_text
; value
--)
2251 if (whitespace (*value
) || member (*value
, COMMAND_SEPARATORS
))
2253 if (value
<= filename_text
)
2254 matches
= rl_completion_matches (filename_text
, command_word_completion_function
);
2258 start_len
+= value
- filename_text
;
2259 if (whitespace (value
[-1]))
2260 matches
= rl_completion_matches (value
, rl_filename_completion_function
);
2262 matches
= rl_completion_matches (value
, command_word_completion_function
);
2265 /* If there is more than one match, rl_completion_matches has already
2266 put the lcd in matches[0]. Skip over it. */
2267 cmd_index
= matches
&& matches
[0] && matches
[1];
2269 /* If there's a single match and it's a directory, set the append char
2270 to the expected `/'. Otherwise, don't append anything. */
2271 if (matches
&& matches
[0] && matches
[1] == 0 && test_for_directory (matches
[0]))
2272 rl_completion_append_character
= '/';
2274 rl_completion_suppress_append
= 1;
2277 if (matches
== 0 || matches
[cmd_index
] == 0)
2279 rl_filename_quoting_desired
= 0; /* disable quoting */
2280 return ((char *)NULL
);
2284 value
= (char *)xmalloc (1 + start_len
+ strlen (matches
[cmd_index
]));
2287 value
[0] = *orig_start
;
2289 strncpy (value
, orig_start
, start_len
);
2291 strcpy (value
+ start_len
, matches
[cmd_index
]);
2298 /* Okay, now we write the entry_function for variable completion. */
2300 variable_completion_function (text
, state
)
2304 static char **varlist
= (char **)NULL
;
2305 static int varlist_index
;
2306 static char *varname
= (char *)NULL
;
2308 static int first_char
, first_char_loc
;
2316 first_char
= text
[0];
2318 if (first_char
== '$')
2321 if (text
[first_char_loc
] == '{')
2324 varname
= savestring (text
+ first_char_loc
);
2326 namelen
= strlen (varname
);
2328 strvec_dispose (varlist
);
2330 varlist
= all_variables_matching_prefix (varname
);
2334 if (!varlist
|| !varlist
[varlist_index
])
2336 return ((char *)NULL
);
2342 value
= (char *)xmalloc (4 + strlen (varlist
[varlist_index
]));
2346 value
[0] = first_char
;
2347 if (first_char_loc
== 2)
2351 strcpy (value
+ first_char_loc
, varlist
[varlist_index
]);
2352 if (first_char_loc
== 2)
2353 strcat (value
, "}");
2360 /* How about a completion function for hostnames? */
2362 hostname_completion_function (text
, state
)
2366 static char **list
= (char **)NULL
;
2367 static int list_index
= 0;
2368 static int first_char
, first_char_loc
;
2370 /* If we don't have any state, make some. */
2375 list
= (char **)NULL
;
2380 if (first_char
== '@')
2383 list
= hostnames_matching ((char *)text
+first_char_loc
);
2387 if (list
&& list
[list_index
])
2391 t
= (char *)xmalloc (2 + strlen (list
[list_index
]));
2393 strcpy (t
+ first_char_loc
, list
[list_index
]);
2398 return ((char *)NULL
);
2402 * A completion function for service names from /etc/services (or wherever).
2405 bash_servicename_completion_function (text
, state
)
2409 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT)
2410 return ((char *)NULL
);
2412 static char *sname
= (char *)NULL
;
2413 static struct servent
*srvent
;
2414 static int snamelen
, firstc
;
2416 char **alist
, *aentry
;
2424 sname
= savestring (text
);
2425 snamelen
= strlen (sname
);
2429 while (srvent
= getservent ())
2432 if (snamelen
== 0 || (STREQN (sname
, srvent
->s_name
, snamelen
)))
2434 /* Not primary, check aliases */
2435 for (alist
= srvent
->s_aliases
; *alist
; alist
++)
2438 if (STREQN (sname
, aentry
, snamelen
))
2452 return ((char *)NULL
);
2455 value
= afound
? savestring (aentry
) : savestring (srvent
->s_name
);
2461 * A completion function for group names from /etc/group (or wherever).
2464 bash_groupname_completion_function (text
, state
)
2468 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
2469 return ((char *)NULL
);
2471 static char *gname
= (char *)NULL
;
2472 static struct group
*grent
;
2473 static int gnamelen
;
2479 gname
= savestring (text
);
2480 gnamelen
= strlen (gname
);
2485 while (grent
= getgrent ())
2487 if (gnamelen
== 0 || (STREQN (gname
, grent
->gr_name
, gnamelen
)))
2494 return ((char *)NULL
);
2497 value
= savestring (grent
->gr_name
);
2502 /* Functions to perform history and alias expansions on the current line. */
2504 #if defined (BANG_HISTORY)
2505 /* Perform history expansion on the current line. If no history expansion
2506 is done, pre_process_line() returns what it was passed, so we need to
2507 allocate a new line here. */
2509 history_expand_line_internal (line
)
2515 old_verify
= hist_verify
;
2517 new_line
= pre_process_line (line
, 0, 0);
2518 hist_verify
= old_verify
;
2520 return (new_line
== line
) ? savestring (line
) : new_line
;
2524 /* There was an error in expansion. Let the preprocessor print
2527 cleanup_expansion_error ()
2530 #if defined (BANG_HISTORY)
2533 old_verify
= hist_verify
;
2537 fprintf (rl_outstream
, "\r\n");
2538 to_free
= pre_process_line (rl_line_buffer
, 1, 0);
2539 #if defined (BANG_HISTORY)
2540 hist_verify
= old_verify
;
2542 if (to_free
!= rl_line_buffer
)
2544 putc ('\r', rl_outstream
);
2545 rl_forced_update_display ();
2548 /* If NEW_LINE differs from what is in the readline line buffer, add an
2549 undo record to get from the readline line buffer contents to the new
2550 line and make NEW_LINE the current readline line. */
2552 maybe_make_readline_line (new_line
)
2555 if (strcmp (new_line
, rl_line_buffer
) != 0)
2559 rl_add_undo (UNDO_BEGIN
, 0, 0, 0);
2560 rl_delete_text (0, rl_point
);
2561 rl_point
= rl_end
= rl_mark
= 0;
2562 rl_insert_text (new_line
);
2563 rl_add_undo (UNDO_END
, 0, 0, 0);
2567 /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
2569 set_up_new_line (new_line
)
2572 int old_point
, at_end
;
2574 old_point
= rl_point
;
2575 at_end
= rl_point
== rl_end
;
2577 /* If the line was history and alias expanded, then make that
2578 be one thing to undo. */
2579 maybe_make_readline_line (new_line
);
2582 /* Place rl_point where we think it should go. */
2585 else if (old_point
< rl_end
)
2587 rl_point
= old_point
;
2588 if (!whitespace (rl_line_buffer
[rl_point
]))
2589 rl_forward_word (1, 0);
2594 /* Expand aliases in the current readline line. */
2596 alias_expand_line (count
, ignore
)
2601 new_line
= alias_expand (rl_line_buffer
);
2605 set_up_new_line (new_line
);
2610 cleanup_expansion_error ();
2616 #if defined (BANG_HISTORY)
2617 /* History expand the line. */
2619 history_expand_line (count
, ignore
)
2624 new_line
= history_expand_line_internal (rl_line_buffer
);
2628 set_up_new_line (new_line
);
2633 cleanup_expansion_error ();
2638 /* Expand history substitutions in the current line and then insert a
2639 space (hopefully close to where we were before). */
2641 tcsh_magic_space (count
, ignore
)
2644 int dist_from_end
, old_point
;
2646 old_point
= rl_point
;
2647 dist_from_end
= rl_end
- rl_point
;
2648 if (history_expand_line (count
, ignore
) == 0)
2650 /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
2651 This works if all expansions were before rl_point or if no expansions
2653 rl_point
= (old_point
== 0) ? old_point
: rl_end
- dist_from_end
;
2660 #endif /* BANG_HISTORY */
2662 /* History and alias expand the line. */
2664 history_and_alias_expand_line (count
, ignore
)
2670 #if defined (BANG_HISTORY)
2671 new_line
= history_expand_line_internal (rl_line_buffer
);
2679 alias_line
= alias_expand (new_line
);
2681 new_line
= alias_line
;
2687 set_up_new_line (new_line
);
2692 cleanup_expansion_error ();
2697 /* History and alias expand the line, then perform the shell word
2698 expansions by calling expand_string. This can't use set_up_new_line()
2699 because we want the variable expansions as a separate undo'able
2700 set of operations. */
2702 shell_expand_line (count
, ignore
)
2706 WORD_LIST
*expanded_string
;
2709 #if defined (BANG_HISTORY)
2710 new_line
= history_expand_line_internal (rl_line_buffer
);
2718 alias_line
= alias_expand (new_line
);
2720 new_line
= alias_line
;
2726 int old_point
= rl_point
;
2727 int at_end
= rl_point
== rl_end
;
2729 /* If the line was history and alias expanded, then make that
2730 be one thing to undo. */
2731 maybe_make_readline_line (new_line
);
2734 /* If there is variable expansion to perform, do that as a separate
2735 operation to be undone. */
2736 new_line
= savestring (rl_line_buffer
);
2737 expanded_string
= expand_string (new_line
, 0);
2739 if (expanded_string
== 0)
2741 new_line
= (char *)xmalloc (1);
2746 new_line
= string_list (expanded_string
);
2747 dispose_words (expanded_string
);
2750 maybe_make_readline_line (new_line
);
2753 /* Place rl_point where we think it should go. */
2756 else if (old_point
< rl_end
)
2758 rl_point
= old_point
;
2759 if (!whitespace (rl_line_buffer
[rl_point
]))
2760 rl_forward_word (1, 0);
2766 cleanup_expansion_error ();
2771 /* If FIGNORE is set, then don't match files with the given suffixes when
2772 completing filenames. If only one of the possibilities has an acceptable
2773 suffix, delete the others, else just return and let the completer
2774 signal an error. It is called by the completer when real
2775 completions are done on filenames by the completer's internal
2776 function, not for completion lists (M-?) and not on "other"
2777 completion types, such as hostnames or commands. */
2779 static struct ignorevar fignore
=
2785 (sh_iv_item_func_t
*) 0,
2789 _ignore_completion_names (names
, name_func
)
2791 sh_ignore_func_t
*name_func
;
2798 /* If there is only one completion, see if it is acceptable. If it is
2799 not, free it up. In any case, short-circuit and return. This is a
2800 special case because names[0] is not the prefix of the list of names
2801 if there is only one completion; it is the completion itself. */
2802 if (names
[1] == (char *)0)
2805 if ((*name_func
) (names
[0]) == 0)
2808 names
[0] = (char *)NULL
;
2814 /* Allocate space for array to hold list of pointers to matching
2815 filenames. The pointers are copied back to NAMES when done. */
2816 for (nidx
= 1; names
[nidx
]; nidx
++)
2818 newnames
= strvec_create (nidx
+ 1);
2820 if (force_fignore
== 0)
2822 oldnames
= strvec_create (nidx
- 1);
2826 newnames
[0] = names
[0];
2827 for (idx
= nidx
= 1; names
[idx
]; idx
++)
2829 if ((*name_func
) (names
[idx
]))
2830 newnames
[nidx
++] = names
[idx
];
2831 else if (force_fignore
== 0)
2832 oldnames
[oidx
++] = names
[idx
];
2837 newnames
[nidx
] = (char *)NULL
;
2839 /* If none are acceptable then let the completer handle it. */
2845 names
[0] = (char *)NULL
;
2854 if (force_fignore
== 0)
2857 free (oldnames
[--oidx
]);
2861 /* If only one is acceptable, copy it to names[0] and return. */
2865 names
[0] = newnames
[1];
2866 names
[1] = (char *)NULL
;
2871 /* Copy the acceptable names back to NAMES, set the new array end,
2873 for (nidx
= 1; newnames
[nidx
]; nidx
++)
2874 names
[nidx
] = newnames
[nidx
];
2875 names
[nidx
] = (char *)NULL
;
2880 name_is_acceptable (name
)
2886 for (nlen
= strlen (name
), p
= fignore
.ignores
; p
->val
; p
++)
2888 if (nlen
> p
->len
&& p
->len
> 0 && STREQ (p
->val
, &name
[nlen
- p
->len
]))
2897 ignore_dot_names (name
)
2900 return (name
[0] != '.');
2905 filename_completion_ignore (names
)
2909 if (glob_dot_filenames
== 0)
2910 _ignore_completion_names (names
, ignore_dot_names
);
2913 setup_ignore_patterns (&fignore
);
2915 if (fignore
.num_ignores
== 0)
2918 _ignore_completion_names (names
, name_is_acceptable
);
2923 /* Return 1 if NAME is a directory. NAME undergoes tilde expansion. */
2925 test_for_directory (name
)
2931 fn
= bash_tilde_expand (name
, 0);
2932 r
= file_isdir (fn
);
2938 /* Remove files from NAMES, leaving directories. */
2940 bash_ignore_filenames (names
)
2943 _ignore_completion_names (names
, test_for_directory
);
2955 bash_ignore_everything (names
)
2958 _ignore_completion_names (names
, return_zero
);
2962 /* Replace a tilde-prefix in VAL with a `~', assuming the user typed it. VAL
2963 is an expanded filename. DIRECTORY_PART is the tilde-prefix portion
2964 of the un-tilde-expanded version of VAL (what the user typed). */
2966 restore_tilde (val
, directory_part
)
2967 char *val
, *directory_part
;
2970 char *dh2
, *expdir
, *ret
;
2974 /* We need to duplicate the expansions readline performs on the directory
2975 portion before passing it to our completion function. */
2976 dh2
= directory_part
? bash_dequote_filename (directory_part
, 0) : 0;
2977 bash_directory_expansion (&dh2
);
2980 expdir
= bash_tilde_expand (directory_part
, 0);
2981 xl
= strlen (expdir
);
2985 dh2 = unexpanded but dequoted tilde-prefix
2986 dl2 = length of tilde-prefix
2987 expdir = tilde-expanded tilde-prefix
2988 xl = length of expanded tilde-prefix
2989 l = length of remainder after tilde-prefix
2993 ret
= (char *)xmalloc (dl2
+ 2 + l
);
2995 strcpy (ret
+ dl2
, val
+ xl
);
3002 maybe_restore_tilde (val
, directory_part
)
3003 char *val
, *directory_part
;
3005 rl_icppfunc_t
*save
;
3008 save
= (dircomplete_expand
== 0) ? save_directory_hook () : (rl_icppfunc_t
*)0;
3009 ret
= restore_tilde (val
, directory_part
);
3011 restore_directory_hook (save
);
3015 /* Simulate the expansions that will be performed by
3016 rl_filename_completion_function. This must be called with the address of
3017 a pointer to malloc'd memory. */
3019 bash_directory_expansion (dirname
)
3024 d
= savestring (*dirname
);
3026 if ((rl_directory_rewrite_hook
) && (*rl_directory_rewrite_hook
) (&d
))
3031 else if (rl_directory_completion_hook
&& (*rl_directory_completion_hook
) (&d
))
3036 else if (rl_completion_found_quote
)
3038 nd
= bash_dequote_filename (d
, rl_completion_quote_character
);
3045 /* If necessary, rewrite directory entry */
3047 bash_filename_rewrite_hook (fname
, fnlen
)
3053 conv
= fnx_fromfs (fname
, fnlen
);
3055 conv
= savestring (conv
);
3059 /* Functions to save and restore the appropriate directory hook */
3060 /* This is not static so the shopt code can call it */
3062 set_directory_hook ()
3064 if (dircomplete_expand
)
3066 rl_directory_completion_hook
= bash_directory_completion_hook
;
3067 rl_directory_rewrite_hook
= (rl_icppfunc_t
*)0;
3071 rl_directory_rewrite_hook
= bash_directory_completion_hook
;
3072 rl_directory_completion_hook
= (rl_icppfunc_t
*)0;
3076 static rl_icppfunc_t
*
3077 save_directory_hook ()
3081 if (dircomplete_expand
)
3083 ret
= rl_directory_completion_hook
;
3084 rl_directory_completion_hook
= (rl_icppfunc_t
*)NULL
;
3088 ret
= rl_directory_rewrite_hook
;
3089 rl_directory_rewrite_hook
= (rl_icppfunc_t
*)NULL
;
3096 restore_directory_hook (hookf
)
3097 rl_icppfunc_t
*hookf
;
3099 if (dircomplete_expand
)
3100 rl_directory_completion_hook
= hookf
;
3102 rl_directory_rewrite_hook
= hookf
;
3105 /* Check whether not the (dequoted) version of DIRNAME, with any trailing slash
3108 directory_exists (dirname
)
3109 const char *dirname
;
3115 /* First, dequote the directory name */
3116 new_dirname
= bash_dequote_filename ((char *)dirname
, rl_completion_quote_character
);
3117 dirlen
= STRLEN (new_dirname
);
3118 if (new_dirname
[dirlen
- 1] == '/')
3119 new_dirname
[dirlen
- 1] = '\0';
3120 #if defined (HAVE_LSTAT)
3121 r
= lstat (new_dirname
, &sb
) == 0;
3123 r
= stat (new_dirname
, &sb
) == 0;
3129 /* Expand a filename before the readline completion code passes it to stat(2).
3130 The filename will already have had tilde expansion performed. */
3132 bash_filename_stat_hook (dirname
)
3135 char *local_dirname
, *new_dirname
, *t
;
3136 int should_expand_dirname
, return_value
;
3141 local_dirname
= *dirname
;
3142 should_expand_dirname
= return_value
= 0;
3143 if (t
= mbschr (local_dirname
, '$'))
3144 should_expand_dirname
= '$';
3145 else if (t
= mbschr (local_dirname
, '`')) /* XXX */
3146 should_expand_dirname
= '`';
3148 if (should_expand_dirname
&& directory_exists (local_dirname
))
3149 should_expand_dirname
= 0;
3151 if (should_expand_dirname
)
3153 new_dirname
= savestring (local_dirname
);
3154 /* no error messages, and expand_prompt_string doesn't longjmp so we don't
3155 have to worry about restoring this setting. */
3156 global_nounset
= unbound_vars_is_error
;
3157 unbound_vars_is_error
= 0;
3158 wl
= expand_prompt_string (new_dirname
, 0, W_NOCOMSUB
|W_COMPLETE
); /* does the right thing */
3159 unbound_vars_is_error
= global_nounset
;
3163 new_dirname
= string_list (wl
);
3164 /* Tell the completer we actually expanded something and change
3165 *dirname only if we expanded to something non-null -- stat
3166 behaves unpredictably when passed null or empty strings */
3167 if (new_dirname
&& *new_dirname
)
3169 free (local_dirname
); /* XXX */
3170 local_dirname
= *dirname
= new_dirname
;
3171 return_value
= STREQ (local_dirname
, *dirname
) == 0;
3181 /* This is very similar to the code in bash_directory_completion_hook below,
3182 but without spelling correction and not worrying about whether or not
3183 we change relative pathnames. */
3184 if (no_symbolic_links
== 0 && (local_dirname
[0] != '.' || local_dirname
[1]))
3186 char *temp1
, *temp2
;
3188 t
= get_working_directory ("symlink-hook");
3189 temp1
= make_absolute (local_dirname
, t
);
3191 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3193 /* If we can't canonicalize, bail. */
3197 return return_value
;
3200 free (local_dirname
);
3205 return (return_value
);
3208 /* Handle symbolic link references and other directory name
3209 expansions while hacking completion. This should return 1 if it modifies
3210 the DIRNAME argument, 0 otherwise. It should make sure not to modify
3211 DIRNAME if it returns 0. */
3213 bash_directory_completion_hook (dirname
)
3216 char *local_dirname
, *new_dirname
, *t
;
3217 int return_value
, should_expand_dirname
, nextch
, closer
, changed
;
3218 size_t local_dirlen
;
3222 return_value
= should_expand_dirname
= nextch
= closer
= 0;
3223 local_dirname
= *dirname
;
3225 if (t
= mbschr (local_dirname
, '$'))
3227 should_expand_dirname
= '$';
3229 /* Deliberately does not handle the deprecated $[...] arithmetic
3233 else if (nextch
== '{')
3238 else if (local_dirname
[0] == '~')
3239 should_expand_dirname
= '~';
3242 t
= mbschr (local_dirname
, '`');
3243 if (t
&& unclosed_pair (local_dirname
, strlen (local_dirname
), "`") == 0)
3244 should_expand_dirname
= '`';
3247 if (should_expand_dirname
&& directory_exists (local_dirname
))
3248 should_expand_dirname
= 0;
3250 if (should_expand_dirname
)
3252 new_dirname
= savestring (local_dirname
);
3253 wl
= expand_prompt_string (new_dirname
, 0, W_NOCOMSUB
|W_COMPLETE
); /* does the right thing */
3256 *dirname
= string_list (wl
);
3257 /* Tell the completer to replace the directory name only if we
3258 actually expanded something. */
3259 return_value
= STREQ (local_dirname
, *dirname
) == 0;
3260 free (local_dirname
);
3263 local_dirname
= *dirname
;
3264 /* XXX - change rl_filename_quote_characters here based on
3265 should_expand_dirname/nextch/closer. This is the only place
3266 custom_filename_quote_characters is modified. */
3267 if (rl_filename_quote_characters
&& *rl_filename_quote_characters
)
3270 i
= strlen (default_filename_quote_characters
);
3271 custom_filename_quote_characters
= xrealloc (custom_filename_quote_characters
, i
+1);
3272 for (i
= j
= 0; c
= default_filename_quote_characters
[i
]; i
++)
3274 if (c
== should_expand_dirname
|| c
== nextch
|| c
== closer
)
3276 custom_filename_quote_characters
[j
++] = c
;
3278 custom_filename_quote_characters
[j
] = '\0';
3279 rl_filename_quote_characters
= custom_filename_quote_characters
;
3280 set_filename_bstab (rl_filename_quote_characters
);
3286 free (local_dirname
);
3287 *dirname
= (char *)xmalloc (1);
3294 /* Dequote the filename even if we don't expand it. */
3295 new_dirname
= bash_dequote_filename (local_dirname
, rl_completion_quote_character
);
3296 return_value
= STREQ (local_dirname
, new_dirname
) == 0;
3297 free (local_dirname
);
3298 local_dirname
= *dirname
= new_dirname
;
3301 /* no_symbolic_links == 0 -> use (default) logical view of the file system.
3302 local_dirname[0] == '.' && local_dirname[1] == '/' means files in the
3303 current directory (./).
3304 local_dirname[0] == '.' && local_dirname[1] == 0 means relative pathnames
3305 in the current directory (e.g., lib/sh).
3306 XXX - should we do spelling correction on these? */
3308 /* This is test as it was in bash-4.2: skip relative pathnames in current
3309 directory. Change test to
3310 (local_dirname[0] != '.' || (local_dirname[1] && local_dirname[1] != '/'))
3311 if we want to skip paths beginning with ./ also. */
3312 if (no_symbolic_links
== 0 && (local_dirname
[0] != '.' || local_dirname
[1]))
3314 char *temp1
, *temp2
;
3317 /* If we have a relative path
3318 (local_dirname[0] != '/' && local_dirname[0] != '.')
3319 that is canonical after appending it to the current directory, then
3322 strcmp (temp1, temp2) == 0
3323 after adding a slash to temp2 below. It should be safe to not
3326 t
= get_working_directory ("symlink-hook");
3327 temp1
= make_absolute (local_dirname
, t
);
3329 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3331 /* Try spelling correction if initial canonicalization fails. Make
3332 sure we are set to replace the directory name with the results so
3333 subsequent directory checks don't fail. */
3334 if (temp2
== 0 && dircomplete_spelling
&& dircomplete_expand
)
3336 temp2
= dirspell (temp1
);
3341 temp2
= sh_canonpath (temp1
, PATH_CHECKDOTDOT
|PATH_CHECKEXISTS
);
3342 return_value
|= temp2
!= 0;
3345 /* If we can't canonicalize, bail. */
3349 return return_value
;
3351 len1
= strlen (temp1
);
3352 if (temp1
[len1
- 1] == '/')
3354 len2
= strlen (temp2
);
3355 if (len2
> 2) /* don't append `/' to `/' or `//' */
3357 temp2
= (char *)xrealloc (temp2
, len2
+ 2);
3359 temp2
[len2
+ 1] = '\0';
3363 /* dircomplete_expand_relpath == 0 means we want to leave relative
3364 pathnames that are unchanged by canonicalization alone.
3365 *local_dirname != '/' && *local_dirname != '.' == relative pathname
3366 (consistent with general.c:absolute_pathname())
3367 temp1 == temp2 (after appending a slash to temp2) means the pathname
3368 is not changed by canonicalization as described above. */
3369 if (dircomplete_expand_relpath
|| ((local_dirname
[0] != '/' && local_dirname
[0] != '.') && STREQ (temp1
, temp2
) == 0))
3370 return_value
|= STREQ (local_dirname
, temp2
) == 0;
3371 free (local_dirname
);
3376 return (return_value
);
3379 static char **history_completion_array
= (char **)NULL
;
3380 static int harry_size
;
3381 static int harry_len
;
3384 build_history_completion_array ()
3390 /* First, clear out the current dynamic history completion list. */
3393 strvec_dispose (history_completion_array
);
3394 history_completion_array
= (char **)NULL
;
3399 /* Next, grovel each line of history, making each shell-sized token
3400 a separate entry in the history_completion_array. */
3401 hlist
= history_list ();
3405 for (i
= 0; hlist
[i
]; i
++)
3407 for ( --i
; i
>= 0; i
--)
3409 /* Separate each token, and place into an array. */
3410 tokens
= history_tokenize (hlist
[i
]->line
);
3412 for (j
= 0; tokens
&& tokens
[j
]; j
++)
3414 if (harry_len
+ 2 > harry_size
)
3415 history_completion_array
= strvec_resize (history_completion_array
, harry_size
+= 10);
3417 history_completion_array
[harry_len
++] = tokens
[j
];
3418 history_completion_array
[harry_len
] = (char *)NULL
;
3423 /* Sort the complete list of tokens. */
3424 if (dabbrev_expand_active
== 0)
3425 qsort (history_completion_array
, harry_len
, sizeof (char *), (QSFUNC
*)strvec_strcmp
);
3430 history_completion_generator (hint_text
, state
)
3431 const char *hint_text
;
3434 static int local_index
, len
;
3435 static const char *text
;
3437 /* If this is the first call to the generator, then initialize the
3438 list of strings to complete over. */
3441 if (dabbrev_expand_active
) /* This is kind of messy */
3442 rl_completion_suppress_append
= 1;
3444 build_history_completion_array ();
3446 len
= strlen (text
);
3449 while (history_completion_array
&& history_completion_array
[local_index
])
3451 if (strncmp (text
, history_completion_array
[local_index
++], len
) == 0)
3452 return (savestring (history_completion_array
[local_index
- 1]));
3454 return ((char *)NULL
);
3458 dynamic_complete_history (count
, key
)
3462 rl_compentry_func_t
*orig_func
;
3463 rl_completion_func_t
*orig_attempt_func
;
3464 rl_compignore_func_t
*orig_ignore_func
;
3466 orig_func
= rl_completion_entry_function
;
3467 orig_attempt_func
= rl_attempted_completion_function
;
3468 orig_ignore_func
= rl_ignore_some_completions_function
;
3470 rl_completion_entry_function
= history_completion_generator
;
3471 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3472 rl_ignore_some_completions_function
= filename_completion_ignore
;
3474 /* XXX - use rl_completion_mode here? */
3475 if (rl_last_func
== dynamic_complete_history
)
3476 r
= rl_complete_internal ('?');
3478 r
= rl_complete_internal (TAB
);
3480 rl_completion_entry_function
= orig_func
;
3481 rl_attempted_completion_function
= orig_attempt_func
;
3482 rl_ignore_some_completions_function
= orig_ignore_func
;
3488 bash_dabbrev_expand (count
, key
)
3491 int r
, orig_suppress
, orig_sort
;
3492 rl_compentry_func_t
*orig_func
;
3493 rl_completion_func_t
*orig_attempt_func
;
3494 rl_compignore_func_t
*orig_ignore_func
;
3496 orig_func
= rl_menu_completion_entry_function
;
3497 orig_attempt_func
= rl_attempted_completion_function
;
3498 orig_ignore_func
= rl_ignore_some_completions_function
;
3499 orig_suppress
= rl_completion_suppress_append
;
3500 orig_sort
= rl_sort_completion_matches
;
3502 rl_menu_completion_entry_function
= history_completion_generator
;
3503 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3504 rl_ignore_some_completions_function
= filename_completion_ignore
;
3505 rl_filename_completion_desired
= 0;
3506 rl_completion_suppress_append
= 1;
3507 rl_sort_completion_matches
= 0;
3509 /* XXX - use rl_completion_mode here? */
3510 dabbrev_expand_active
= 1;
3511 if (rl_last_func
== bash_dabbrev_expand
)
3512 rl_last_func
= rl_menu_complete
;
3513 r
= rl_menu_complete (count
, key
);
3514 dabbrev_expand_active
= 0;
3516 rl_last_func
= bash_dabbrev_expand
;
3517 rl_menu_completion_entry_function
= orig_func
;
3518 rl_attempted_completion_function
= orig_attempt_func
;
3519 rl_ignore_some_completions_function
= orig_ignore_func
;
3520 rl_completion_suppress_append
= orig_suppress
;
3521 rl_sort_completion_matches
= orig_sort
;
3526 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
3528 bash_complete_username (ignore
, ignore2
)
3529 int ignore
, ignore2
;
3531 return bash_complete_username_internal (rl_completion_mode (bash_complete_username
));
3535 bash_possible_username_completions (ignore
, ignore2
)
3536 int ignore
, ignore2
;
3538 return bash_complete_username_internal ('?');
3542 bash_complete_username_internal (what_to_do
)
3545 return bash_specific_completion (what_to_do
, rl_username_completion_function
);
3549 bash_complete_filename (ignore
, ignore2
)
3550 int ignore
, ignore2
;
3552 return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename
));
3556 bash_possible_filename_completions (ignore
, ignore2
)
3557 int ignore
, ignore2
;
3559 return bash_complete_filename_internal ('?');
3563 bash_complete_filename_internal (what_to_do
)
3566 rl_compentry_func_t
*orig_func
;
3567 rl_completion_func_t
*orig_attempt_func
;
3568 rl_icppfunc_t
*orig_dir_func
;
3569 rl_compignore_func_t
*orig_ignore_func
;
3570 /*const*/ char *orig_rl_completer_word_break_characters
;
3573 orig_func
= rl_completion_entry_function
;
3574 orig_attempt_func
= rl_attempted_completion_function
;
3575 orig_ignore_func
= rl_ignore_some_completions_function
;
3576 orig_rl_completer_word_break_characters
= rl_completer_word_break_characters
;
3578 orig_dir_func
= save_directory_hook ();
3580 rl_completion_entry_function
= rl_filename_completion_function
;
3581 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
3582 rl_ignore_some_completions_function
= filename_completion_ignore
;
3583 rl_completer_word_break_characters
= " \t\n\"\'";
3585 r
= rl_complete_internal (what_to_do
);
3587 rl_completion_entry_function
= orig_func
;
3588 rl_attempted_completion_function
= orig_attempt_func
;
3589 rl_ignore_some_completions_function
= orig_ignore_func
;
3590 rl_completer_word_break_characters
= orig_rl_completer_word_break_characters
;
3592 restore_directory_hook (orig_dir_func
);
3598 bash_complete_hostname (ignore
, ignore2
)
3599 int ignore
, ignore2
;
3601 return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname
));
3605 bash_possible_hostname_completions (ignore
, ignore2
)
3606 int ignore
, ignore2
;
3608 return bash_complete_hostname_internal ('?');
3612 bash_complete_variable (ignore
, ignore2
)
3613 int ignore
, ignore2
;
3615 return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable
));
3619 bash_possible_variable_completions (ignore
, ignore2
)
3620 int ignore
, ignore2
;
3622 return bash_complete_variable_internal ('?');
3626 bash_complete_command (ignore
, ignore2
)
3627 int ignore
, ignore2
;
3629 return bash_complete_command_internal (rl_completion_mode (bash_complete_command
));
3633 bash_possible_command_completions (ignore
, ignore2
)
3634 int ignore
, ignore2
;
3636 return bash_complete_command_internal ('?');
3640 bash_complete_hostname_internal (what_to_do
)
3643 return bash_specific_completion (what_to_do
, hostname_completion_function
);
3647 bash_complete_variable_internal (what_to_do
)
3650 return bash_specific_completion (what_to_do
, variable_completion_function
);
3654 bash_complete_command_internal (what_to_do
)
3657 return bash_specific_completion (what_to_do
, command_word_completion_function
);
3660 static char *globtext
;
3661 static char *globorig
;
3664 glob_complete_word (text
, state
)
3668 static char **matches
= (char **)NULL
;
3675 rl_filename_completion_desired
= 1;
3677 if (globorig
!= globtext
)
3681 ttext
= bash_tilde_expand (text
, 0);
3683 if (rl_explicit_arg
)
3685 globorig
= savestring (ttext
);
3686 glen
= strlen (ttext
);
3687 globtext
= (char *)xmalloc (glen
+ 2);
3688 strcpy (globtext
, ttext
);
3689 globtext
[glen
] = '*';
3690 globtext
[glen
+1] = '\0';
3693 globtext
= globorig
= savestring (ttext
);
3698 matches
= shell_glob_filename (globtext
);
3699 if (GLOB_FAILED (matches
))
3700 matches
= (char **)NULL
;
3704 ret
= matches
? matches
[ind
] : (char *)NULL
;
3710 bash_glob_completion_internal (what_to_do
)
3713 return bash_specific_completion (what_to_do
, glob_complete_word
);
3716 /* A special quoting function so we don't end up quoting globbing characters
3717 in the word if there are no matches or multiple matches. */
3719 bash_glob_quote_filename (s
, rtype
, qcp
)
3724 if (globorig
&& qcp
&& *qcp
== '\0' && STREQ (s
, globorig
))
3725 return (savestring (s
));
3727 return (bash_quote_filename (s
, rtype
, qcp
));
3731 bash_glob_complete_word (count
, key
)
3735 rl_quote_func_t
*orig_quoting_function
;
3737 if (rl_editing_mode
== EMACS_EDITING_MODE
)
3738 rl_explicit_arg
= 1; /* force `*' append */
3739 orig_quoting_function
= rl_filename_quoting_function
;
3740 rl_filename_quoting_function
= bash_glob_quote_filename
;
3742 r
= bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word
));
3744 rl_filename_quoting_function
= orig_quoting_function
;
3749 bash_glob_expand_word (count
, key
)
3752 return bash_glob_completion_internal ('*');
3756 bash_glob_list_expansions (count
, key
)
3759 return bash_glob_completion_internal ('?');
3763 bash_specific_completion (what_to_do
, generator
)
3765 rl_compentry_func_t
*generator
;
3767 rl_compentry_func_t
*orig_func
;
3768 rl_completion_func_t
*orig_attempt_func
;
3769 rl_compignore_func_t
*orig_ignore_func
;
3772 orig_func
= rl_completion_entry_function
;
3773 orig_attempt_func
= rl_attempted_completion_function
;
3774 orig_ignore_func
= rl_ignore_some_completions_function
;
3775 rl_completion_entry_function
= generator
;
3776 rl_attempted_completion_function
= NULL
;
3777 rl_ignore_some_completions_function
= orig_ignore_func
;
3779 r
= rl_complete_internal (what_to_do
);
3781 rl_completion_entry_function
= orig_func
;
3782 rl_attempted_completion_function
= orig_attempt_func
;
3783 rl_ignore_some_completions_function
= orig_ignore_func
;
3788 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
3790 #if defined (VI_MODE)
3791 /* Completion, from vi mode's point of view. This is a modified version of
3792 rl_vi_complete which uses the bash globbing code to implement what POSIX
3793 specifies, which is to append a `*' and attempt filename generation (which
3794 has the side effect of expanding any globbing characters in the word). */
3796 bash_vi_complete (count
, key
)
3799 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
3803 if ((rl_point
< rl_end
) && (!whitespace (rl_line_buffer
[rl_point
])))
3805 if (!whitespace (rl_line_buffer
[rl_point
+ 1]))
3806 rl_vi_end_word (1, 'E');
3810 /* Find boundaries of current word, according to vi definition of a
3816 rl_vi_bWord (1, 'B');
3821 t
= substring (rl_line_buffer
, p
, rl_point
);
3824 if (t
&& glob_pattern_p (t
) == 0)
3825 rl_explicit_arg
= 1; /* XXX - force glob_complete_word to append `*' */
3828 if (key
== '*') /* Expansion and replacement. */
3829 r
= bash_glob_expand_word (count
, key
);
3830 else if (key
== '=') /* List possible completions. */
3831 r
= bash_glob_list_expansions (count
, key
);
3832 else if (key
== '\\') /* Standard completion */
3833 r
= bash_glob_complete_word (count
, key
);
3835 r
= rl_complete (0, key
);
3837 if (key
== '*' || key
== '\\')
3838 rl_vi_start_inserting (key
, 1, 1);
3842 return rl_vi_complete (count
, key
);
3843 #endif /* !SPECIFIC_COMPLETION_FUNCTIONS */
3845 #endif /* VI_MODE */
3847 /* Filename quoting for completion. */
3848 /* A function to strip unquoted quote characters (single quotes, double
3849 quotes, and backslashes). It allows single quotes to appear
3850 within double quotes, and vice versa. It should be smarter. */
3852 bash_dequote_filename (text
, quote_char
)
3860 ret
= (char *)xmalloc (l
+ 1);
3861 for (quoted
= quote_char
, p
= text
, r
= ret
; p
&& *p
; p
++)
3863 /* Allow backslash-escaped characters to pass through unscathed. */
3866 /* Backslashes are preserved within single quotes. */
3869 /* Backslashes are preserved within double quotes unless the
3870 character is one that is defined to be escaped */
3871 else if (quoted
== '"' && ((sh_syntaxtab
[p
[1]] & CBSDQUOTE
) == 0))
3876 return ret
; /* XXX - was break; */
3880 if (quoted
&& *p
== quoted
)
3886 if (quoted
== 0 && (*p
== '\'' || *p
== '"'))
3897 /* Quote characters that the readline completion code would treat as
3898 word break characters with backslashes. Pass backslash-quoted
3899 characters through without examination. */
3901 quote_word_break_chars (text
)
3908 ret
= (char *)xmalloc ((2 * l
) + 1);
3909 for (s
= text
, r
= ret
; *s
; s
++)
3911 /* Pass backslash-quoted characters through, including the backslash. */
3920 /* OK, we have an unquoted character. Check its presence in
3921 rl_completer_word_break_characters. */
3922 if (mbschr (rl_completer_word_break_characters
, *s
))
3924 /* XXX -- check for standalone tildes here and backslash-quote them */
3925 if (s
== text
&& *s
== '~' && file_exists (text
))
3933 /* Use characters in STRING to populate the table of characters that should
3934 be backslash-quoted. The table will be used for sh_backslash_quote from
3937 set_filename_bstab (string
)
3942 memset (filename_bstab
, 0, sizeof (filename_bstab
));
3943 for (s
= string
; s
&& *s
; s
++)
3944 filename_bstab
[*s
] = 1;
3947 /* Quote a filename using double quotes, single quotes, or backslashes
3948 depending on the value of completion_quoting_style. If we're
3949 completing using backslashes, we need to quote some additional
3950 characters (those that readline treats as word breaks), so we call
3951 quote_word_break_chars on the result. This returns newly-allocated
3954 bash_quote_filename (s
, rtype
, qcp
)
3959 char *rtext
, *mtext
, *ret
;
3962 rtext
= (char *)NULL
;
3964 /* If RTYPE == MULT_MATCH, it means that there is
3965 more than one match. In this case, we do not add
3966 the closing quote or attempt to perform tilde
3967 expansion. If RTYPE == SINGLE_MATCH, we try
3968 to perform tilde expansion, because single and double
3969 quotes inhibit tilde expansion by the shell. */
3971 cs
= completion_quoting_style
;
3972 /* Might need to modify the default completion style based on *qcp,
3973 since it's set to any user-provided opening quote. We also change
3974 to single-quoting if there is no user-provided opening quote and
3975 the word being completed contains newlines, since those are not
3976 quoted correctly using backslashes (a backslash-newline pair is
3977 special to the shell parser). */
3978 if (*qcp
== '\0' && cs
== COMPLETE_BSQUOTE
&& mbschr (s
, '\n'))
3979 cs
= COMPLETE_SQUOTE
;
3980 else if (*qcp
== '"')
3981 cs
= COMPLETE_DQUOTE
;
3982 else if (*qcp
== '\'')
3983 cs
= COMPLETE_SQUOTE
;
3984 #if defined (BANG_HISTORY)
3985 else if (*qcp
== '\0' && history_expansion
&& cs
== COMPLETE_DQUOTE
&&
3986 history_expansion_inhibited
== 0 && mbschr (s
, '!'))
3987 cs
= COMPLETE_BSQUOTE
;
3989 if (*qcp
== '"' && history_expansion
&& cs
== COMPLETE_DQUOTE
&&
3990 history_expansion_inhibited
== 0 && mbschr (s
, '!'))
3992 cs
= COMPLETE_BSQUOTE
;
3997 /* Don't tilde-expand backslash-quoted filenames, since only single and
3998 double quotes inhibit tilde expansion. */
4000 if (mtext
[0] == '~' && rtype
== SINGLE_MATCH
&& cs
!= COMPLETE_BSQUOTE
)
4001 mtext
= bash_tilde_expand (s
, 0);
4005 case COMPLETE_DQUOTE
:
4006 rtext
= sh_double_quote (mtext
);
4008 case COMPLETE_SQUOTE
:
4009 rtext
= sh_single_quote (mtext
);
4011 case COMPLETE_BSQUOTE
:
4012 rtext
= sh_backslash_quote (mtext
, complete_fullquote
? 0 : filename_bstab
, 0);
4019 /* We may need to quote additional characters: those that readline treats
4020 as word breaks that are not quoted by backslash_quote. */
4021 if (rtext
&& cs
== COMPLETE_BSQUOTE
)
4023 mtext
= quote_word_break_chars (rtext
);
4028 /* Leave the opening quote intact. The readline completion code takes
4029 care of avoiding doubled opening quotes. */
4032 rlen
= strlen (rtext
);
4033 ret
= (char *)xmalloc (rlen
+ 1);
4034 strcpy (ret
, rtext
);
4038 ret
= (char *)xmalloc (rlen
= 1);
4042 /* If there are multiple matches, cut off the closing quote. */
4043 if (rtype
== MULT_MATCH
&& cs
!= COMPLETE_BSQUOTE
)
4044 ret
[rlen
- 1] = '\0';
4049 /* Support for binding readline key sequences to Unix commands. */
4050 static Keymap cmd_xmap
;
4061 x
= putc (c
, rl_outstream
);
4068 bash_execute_unix_command (count
, key
)
4069 int count
; /* ignored */
4072 Keymap ckmap
; /* current keymap */
4073 Keymap xkmap
; /* unix command executing keymap */
4074 rl_command_func_t
*func
;
4078 sh_parser_state_t ps
;
4079 char *cmd
, *value
, *l
, *l1
, *ce
;
4081 char ibuf
[INT_STRLEN_BOUND(int) + 1];
4083 /* First, we need to find the right command to execute. This is tricky,
4084 because we might have already indirected into another keymap, so we
4085 have to walk cmd_xmap using the entire key sequence. */
4086 cmd
= (char *)rl_function_of_keyseq (rl_executing_keyseq
, cmd_xmap
, &type
);
4088 if (cmd
== 0 || type
!= ISMACR
)
4091 internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
4092 rl_forced_update_display ();
4096 ce
= rl_get_termcap ("ce");
4097 if (ce
) /* clear current line */
4100 fprintf (rl_outstream
, "\r");
4101 tputs (ce
, 1, putx
);
4103 rl_clear_visible_line ();
4105 fflush (rl_outstream
);
4108 rl_crlf (); /* move to a new line */
4110 v
= bind_variable ("READLINE_LINE", rl_line_buffer
, 0);
4112 VSETATTR (v
, att_exported
);
4113 l
= v
? value_cell (v
) : 0;
4114 value
= inttostr (rl_point
, ibuf
, sizeof (ibuf
));
4115 v
= bind_int_variable ("READLINE_POINT", value
);
4117 VSETATTR (v
, att_exported
);
4118 array_needs_making
= 1;
4120 save_parser_state (&ps
);
4121 r
= parse_and_execute (cmd
, "bash_execute_unix_command", SEVAL_NOHIST
|SEVAL_NOFREE
);
4122 restore_parser_state (&ps
);
4124 v
= find_variable ("READLINE_LINE");
4125 l1
= v
? value_cell (v
) : 0;
4127 maybe_make_readline_line (value_cell (v
));
4128 v
= find_variable ("READLINE_POINT");
4129 if (v
&& legal_number (value_cell (v
), &mi
))
4135 if (rl_point
> rl_end
)
4137 else if (rl_point
< 0)
4142 check_unbind_variable ("READLINE_LINE");
4143 check_unbind_variable ("READLINE_POINT");
4144 array_needs_making
= 1;
4146 /* and restore the readline buffer and display after command execution. */
4147 /* If we clear the last line of the prompt above, redraw only that last
4148 line. If the command returns 124, we redraw unconditionally as in
4149 previous versions. */
4151 rl_redraw_prompt_last_line ();
4153 rl_forced_update_display ();
4159 print_unix_command_map ()
4163 save
= rl_get_keymap ();
4164 rl_set_keymap (cmd_xmap
);
4165 rl_macro_dumper (1);
4166 rl_set_keymap (save
);
4171 init_unix_command_map ()
4173 cmd_xmap
= rl_make_bare_keymap ();
4177 isolate_sequence (string
, ind
, need_dquote
, startp
)
4179 int ind
, need_dquote
, *startp
;
4182 int c
, passc
, delim
;
4184 for (i
= ind
; string
[i
] && whitespace (string
[i
]); i
++)
4186 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
4187 if (need_dquote
&& string
[i
] != '"')
4189 builtin_error (_("%s: first non-whitespace character is not `\"'"), string
);
4193 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
4194 string to bind the key sequence to. */
4195 delim
= (string
[i
] == '"' || string
[i
] == '\'') ? string
[i
] : 0;
4198 *startp
= delim
? ++i
: i
;
4200 for (passc
= 0; c
= string
[i
]; i
++)
4216 if (delim
&& string
[i
] != delim
)
4218 builtin_error (_("no closing `%c' in %s"), delim
, string
);
4226 bind_keyseq_to_unix_command (line
)
4234 init_unix_command_map ();
4236 kmap
= rl_get_keymap ();
4238 /* We duplicate some of the work done by rl_parse_and_bind here, but
4239 this code only has to handle `"keyseq": ["]command["]' and can
4240 generate an error for anything else. */
4241 i
= isolate_sequence (line
, 0, 1, &kstart
);
4245 /* Create the key sequence string to pass to rl_generic_bind */
4246 kseq
= substring (line
, kstart
, i
);
4248 for ( ; line
[i
] && line
[i
] != ':'; i
++)
4252 builtin_error (_("%s: missing colon separator"), line
);
4257 i
= isolate_sequence (line
, i
+ 1, 0, &kstart
);
4264 /* Create the value string containing the command to execute. */
4265 value
= substring (line
, kstart
, i
);
4267 /* Save the command to execute and the key sequence in the CMD_XMAP */
4268 rl_generic_bind (ISMACR
, kseq
, value
, cmd_xmap
);
4270 /* and bind the key sequence in the current keymap to a function that
4271 understands how to execute from CMD_XMAP */
4272 rl_bind_keyseq_in_map (kseq
, bash_execute_unix_command
, kmap
);
4278 /* Used by the programmable completion code. Complete TEXT as a filename,
4279 but return only directories as matches. Dequotes the filename before
4280 attempting to find matches. */
4282 bash_directory_completion_matches (text
)
4289 qc
= rl_dispatching
? rl_completion_quote_character
: 0;
4290 /* If rl_completion_found_quote != 0, rl_completion_matches will call the
4291 filename dequoting function, causing the directory name to be dequoted
4293 if (rl_dispatching
&& rl_completion_found_quote
== 0)
4294 dfn
= bash_dequote_filename ((char *)text
, qc
);
4297 m1
= rl_completion_matches (dfn
, rl_filename_completion_function
);
4301 if (m1
== 0 || m1
[0] == 0)
4303 /* We don't bother recomputing the lcd of the matches, because it will just
4304 get thrown away by the programmable completion code and recomputed
4306 (void)bash_ignore_filenames (m1
);
4311 bash_dequote_text (text
)
4317 qc
= (text
[0] == '"' || text
[0] == '\'') ? text
[0] : 0;
4318 dtxt
= bash_dequote_filename ((char *)text
, qc
);
4322 /* This event hook is designed to be called after readline receives a signal
4323 that interrupts read(2). It gives reasonable responsiveness to interrupts
4324 and fatal signals without executing too much code in a signal handler
4329 /* If we're going to longjmp to top_level, make sure we clean up readline.
4330 check_signals will call QUIT, which will eventually longjmp to top_level,
4331 calling run_interrupt_trap along the way. The check for sigalrm_seen is
4332 to clean up the read builtin's state. */
4333 if (terminating_signal
|| interrupt_state
|| sigalrm_seen
)
4334 rl_cleanup_after_signal ();
4335 bashline_reset_event_hook ();
4336 check_signals_and_traps (); /* XXX */
4340 #endif /* READLINE */