1 /* bashline.c -- Bash's interface to the readline library. */
3 /* Copyright (C) 1987,1991 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 it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bash is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #if defined (READLINE)
25 #include "bashtypes.h"
26 #include "posixstat.h"
28 #if defined (HAVE_UNISTD_H)
38 #include "execute_cmd.h"
41 #include "builtins/common.h"
42 #include <readline/rlconf.h>
43 #include <readline/readline.h>
44 #include <readline/history.h>
46 #include <glob/glob.h>
52 #if defined (PROGRAMMABLE_COMPLETION)
53 # include "pcomplete.h"
56 #if defined (BRACE_COMPLETION)
57 extern void bash_brace_completion ();
58 #endif /* BRACE_COMPLETION */
60 /* Functions bound to keys in Readline for Bash users. */
61 static void shell_expand_line ();
62 static void display_shell_version (), operate_and_get_next ();
63 static void bash_ignore_filenames ();
64 static void bash_ignore_everything ();
65 static void cleanup_expansion_error (), set_up_new_line ();
67 #if defined (BANG_HISTORY)
68 static int history_expand_line ();
69 static int tcsh_magic_space ();
70 #endif /* BANG_HISTORY */
72 static int alias_expand_line ();
74 #if defined (BANG_HISTORY) && defined (ALIAS)
75 static int history_and_alias_expand_line ();
78 /* Helper functions for Readline. */
79 static int bash_directory_completion_hook ();
80 static void filename_completion_ignore ();
81 static void bash_push_line ();
83 static char **attempt_shell_completion ();
84 static char *variable_completion_function ();
85 static char *hostname_completion_function ();
86 static char *command_subst_completion_function ();
87 static void dynamic_complete_history ();
89 static char *glob_complete_word ();
90 static void bash_glob_expand_word ();
91 static void bash_glob_list_expansions ();
93 static void snarf_hosts_from_file ();
94 static void add_host_name ();
96 static char *bash_dequote_filename ();
97 static char *bash_quote_filename ();
100 static int posix_edit_macros ();
103 #if defined (PROGRAMMABLE_COMPLETION)
104 static char **prog_complete_matches;
105 static int old_rl_completion_append_character;
108 /* Variables used here but defined in other files. */
109 extern int posixly_correct, no_symbolic_links;
110 extern int rl_explicit_arg;
111 extern char *current_prompt_string, *ps1_prompt;
112 extern STRING_INT_ALIST word_token_alist[];
113 extern Function *rl_last_func;
114 extern int rl_filename_completion_desired;
116 /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
117 completion functions which indicate what type of completion should be
118 done (at or before point) that can be bound to key sequences with
119 the readline library. */
120 #define SPECIFIC_COMPLETION_FUNCTIONS
122 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
123 static void bash_specific_completion ();
124 static void bash_complete_filename (), bash_possible_filename_completions ();
125 static void bash_complete_filename_internal ();
126 static void bash_complete_username (), bash_possible_username_completions ();
127 static void bash_complete_username_internal ();
128 static void bash_complete_hostname (), bash_possible_hostname_completions ();
129 static void bash_complete_hostname_internal ();
130 static void bash_complete_variable (), bash_possible_variable_completions ();
131 static void bash_complete_variable_internal ();
132 static void bash_complete_command (), bash_possible_command_completions ();
133 static void bash_complete_command_internal ();
134 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
136 #if defined (VI_MODE)
137 static void vi_edit_and_execute_command ();
140 /* Non-zero once initalize_readline () has been called. */
141 int bash_readline_initialized = 0;
143 /* If non-zero, we do hostname completion, breaking words at `@' and
144 trying to complete the stuff after the `@' from our own internal
146 int perform_hostname_completion = 1;
148 /* If non-zero, we don't do command completion on an empty line. */
149 int no_empty_command_completion;
151 static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:";
152 static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:";
154 static Function *old_rl_startup_hook = (Function *) NULL;
156 /* What kind of quoting is performed by bash_quote_filename:
157 COMPLETE_DQUOTE = double-quoting the filename
158 COMPLETE_SQUOTE = single_quoting the filename
159 COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
161 #define COMPLETE_DQUOTE 1
162 #define COMPLETE_SQUOTE 2
163 #define COMPLETE_BSQUOTE 3
164 static int completion_quoting_style = COMPLETE_BSQUOTE;
166 /* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
167 Called when the shell is put into or out of `posix' mode. */
169 posix_readline_initialize (on_or_off)
173 rl_variable_bind ("comment-begin", "#");
174 #if defined (VI_MODE)
175 rl_bind_key_in_map (CTRL('I'), on_or_off ? rl_insert : rl_complete, vi_insertion_keymap);
180 enable_hostname_completion (on_or_off)
185 perform_hostname_completion = 1;
186 rl_special_prefixes = "$@";
187 rl_completer_word_break_characters = bash_completer_word_break_characters;
191 perform_hostname_completion = 0;
192 rl_special_prefixes = "$";
193 rl_completer_word_break_characters = bash_nohostname_word_break_characters;
197 /* Called once from parse.y if we are going to use readline. */
199 initialize_readline ()
201 if (bash_readline_initialized)
204 rl_terminal_name = get_string_value ("TERM");
206 rl_outstream = stderr;
208 /* Allow conditional parsing of the ~/.inputrc file. */
209 rl_readline_name = "Bash";
211 /* Bind up our special shell functions. */
212 rl_add_defun ("shell-expand-line", (Function *)shell_expand_line, -1);
213 rl_bind_key_in_map (CTRL('E'), (Function *)shell_expand_line, emacs_meta_keymap);
215 /* Bind up our special shell functions. */
217 rl_add_defun ("history-expand-line", (Function *)history_expand_line, -1);
218 rl_bind_key_in_map ('^', (Function *)history_expand_line, emacs_meta_keymap);
220 rl_add_defun ("magic-space", (Function *)tcsh_magic_space, -1);
224 rl_add_defun ("alias-expand-line", (Function *)alias_expand_line, -1);
226 rl_add_defun ("history-and-alias-expand-line", (Function *)history_and_alias_expand_line, -1);
230 /* Backwards compatibility. */
231 rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1);
234 ("operate-and-get-next", (Function *)operate_and_get_next, CTRL('O'));
237 ("display-shell-version", (Function *)display_shell_version, -1);
239 (CTRL ('V'), (Function *)display_shell_version, emacs_ctlx_keymap);
241 /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
242 so it is not necessary to allow C-M-j for context switching. Turn
243 off this occasionally confusing behaviour. */
244 rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
245 rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap);
246 #if defined (VI_MODE)
247 rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap);
250 #if defined (BRACE_COMPLETION)
251 rl_add_defun ("complete-into-braces", (Function *)bash_brace_completion, -1);
252 rl_bind_key_in_map ('{', (Function *)bash_brace_completion, emacs_meta_keymap);
253 #endif /* BRACE_COMPLETION */
255 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
256 rl_add_defun ("complete-filename", (Function *)bash_complete_filename, -1);
257 rl_bind_key_in_map ('/', (Function *)bash_complete_filename, emacs_meta_keymap);
258 rl_add_defun ("possible-filename-completions",
259 (Function *)bash_possible_filename_completions, -1);
260 rl_bind_key_in_map ('/', (Function *)bash_possible_filename_completions, emacs_ctlx_keymap);
262 rl_add_defun ("complete-username", (Function *)bash_complete_username, -1);
263 rl_bind_key_in_map ('~', (Function *)bash_complete_username, emacs_meta_keymap);
264 rl_add_defun ("possible-username-completions",
265 (Function *)bash_possible_username_completions, -1);
266 rl_bind_key_in_map ('~', (Function *)bash_possible_username_completions, emacs_ctlx_keymap);
268 rl_add_defun ("complete-hostname", (Function *)bash_complete_hostname, -1);
269 rl_bind_key_in_map ('@', (Function *)bash_complete_hostname, emacs_meta_keymap);
270 rl_add_defun ("possible-hostname-completions",
271 (Function *)bash_possible_hostname_completions, -1);
272 rl_bind_key_in_map ('@', (Function *)bash_possible_hostname_completions, emacs_ctlx_keymap);
274 rl_add_defun ("complete-variable", (Function *)bash_complete_variable, -1);
275 rl_bind_key_in_map ('$', (Function *)bash_complete_variable, emacs_meta_keymap);
276 rl_add_defun ("possible-variable-completions",
277 (Function *)bash_possible_variable_completions, -1);
278 rl_bind_key_in_map ('$', (Function *)bash_possible_variable_completions, emacs_ctlx_keymap);
280 rl_add_defun ("complete-command", (Function *)bash_complete_command, -1);
281 rl_bind_key_in_map ('!', (Function *)bash_complete_command, emacs_meta_keymap);
282 rl_add_defun ("possible-command-completions",
283 (Function *)bash_possible_command_completions, -1);
284 rl_bind_key_in_map ('!', (Function *)bash_possible_command_completions, emacs_ctlx_keymap);
286 rl_add_defun ("glob-expand-word", (Function *)bash_glob_expand_word, -1);
287 rl_add_defun ("glob-list-expansions", (Function *)bash_glob_list_expansions, -1);
288 rl_bind_key_in_map ('*', (Function *)bash_glob_expand_word, emacs_ctlx_keymap);
289 rl_bind_key_in_map ('g', (Function *)bash_glob_list_expansions, emacs_ctlx_keymap);
291 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
293 rl_add_defun ("dynamic-complete-history", (Function *)dynamic_complete_history, -1);
294 rl_bind_key_in_map (TAB, (Function *)dynamic_complete_history, emacs_meta_keymap);
296 /* Tell the completer that we want a crack first. */
297 rl_attempted_completion_function = (CPPFunction *)attempt_shell_completion;
299 /* Tell the completer that we might want to follow symbolic links or
300 do other expansion on directory names. */
301 rl_directory_completion_hook = bash_directory_completion_hook;
303 /* Tell the filename completer we want a chance to ignore some names. */
304 rl_ignore_some_completions_function = (Function *)filename_completion_ignore;
306 #if defined (VI_MODE)
307 rl_bind_key_in_map ('v', (Function *)vi_edit_and_execute_command, vi_movement_keymap);
309 rl_bind_key_in_map ('@', (Function *)posix_edit_macros, vi_movement_keymap);
313 rl_completer_quote_characters = "'\"";
315 /* This sets rl_completer_word_break_characters and rl_special_prefixes
316 to the appropriate values, depending on whether or not hostname
317 completion is enabled. */
318 enable_hostname_completion (perform_hostname_completion);
320 /* characters that need to be quoted when appearing in filenames. */
321 rl_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:";
322 rl_filename_quoting_function = bash_quote_filename;
323 rl_filename_dequoting_function = bash_dequote_filename;
324 rl_char_is_quoted_p = char_is_quoted;
327 posix_readline_initialize (1);
329 bash_readline_initialized = 1;
332 /* On Sun systems at least, rl_attempted_completion_function can end up
333 getting set to NULL, and rl_completion_entry_function set to do command
334 word completion if Bash is interrupted while trying to complete a command
335 word. This just resets all the completion functions to the right thing.
336 It's called from throw_to_top_level(). */
338 bashline_reinitialize ()
341 rl_attempted_completion_function = attempt_shell_completion;
342 rl_completion_entry_function = (Function *)NULL;
343 rl_directory_completion_hook = bash_directory_completion_hook;
344 rl_ignore_some_completions_function = (Function *)filename_completion_ignore;
347 /* Contains the line to push into readline. */
348 static char *push_to_readline = (char *)NULL;
350 /* Push the contents of push_to_readline into the
355 if (push_to_readline)
357 rl_insert_text (push_to_readline);
358 free (push_to_readline);
359 push_to_readline = (char *)NULL;
360 rl_startup_hook = old_rl_startup_hook;
364 /* Call this to set the initial text for the next line to read
370 FREE (push_to_readline);
372 push_to_readline = savestring (line);
373 old_rl_startup_hook = rl_startup_hook;
374 rl_startup_hook = (Function *)bash_push_line;
380 display_shell_version (count, c)
384 show_shell_version (0);
385 putc ('\r', rl_outstream);
386 fflush (rl_outstream);
391 /* **************************************************************** */
395 /* **************************************************************** */
397 /* If the user requests hostname completion, then simply build a list
398 of hosts, and complete from that forever more, or at least until
399 HOSTFILE is unset. */
401 /* THIS SHOULD BE A STRINGLIST. */
402 /* The kept list of hostnames. */
403 static char **hostname_list = (char **)NULL;
405 /* The physical size of the above list. */
406 static int hostname_list_size;
408 /* The number of hostnames in the above list. */
409 static int hostname_list_length;
411 /* Whether or not HOSTNAME_LIST has been initialized. */
412 int hostname_list_initialized = 0;
414 /* Initialize the hostname completion table. */
416 initialize_hostname_list ()
420 temp = get_string_value ("HOSTFILE");
422 temp = get_string_value ("hostname_completion_file");
424 temp = DEFAULT_HOSTS_FILE;
426 snarf_hosts_from_file (temp);
429 hostname_list_initialized++;
432 /* Add NAME to the list of hosts. */
439 if (hostname_list_length + 2 > hostname_list_size)
441 hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32);
442 size = hostname_list_size * sizeof (char *);
443 hostname_list = (char **)xrealloc (hostname_list, size);
446 hostname_list[hostname_list_length++] = savestring (name);
447 hostname_list[hostname_list_length] = (char *)NULL;
450 #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
453 snarf_hosts_from_file (filename)
457 char *temp, buffer[256], name[256];
458 register int i, start;
460 file = fopen (filename, "r");
464 while (temp = fgets (buffer, 255, file))
466 /* Skip to first character. */
467 for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++)
470 /* If comment or blank line, ignore. */
471 if (buffer[i] == '\0' || buffer[i] == '#')
474 /* If `preprocessor' directive, do the include. */
475 if (strncmp (buffer + i, "$include ", 9) == 0)
479 /* Find start of filename. */
480 for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++)
483 /* Find end of filename. */
484 for (t = incfile; *t && cr_whitespace (*t) == 0; t++)
489 snarf_hosts_from_file (incfile);
493 /* Skip internet address if present. */
494 if (digit (buffer[i]))
495 for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++);
497 /* Gobble up names. Each name is separated with whitespace. */
500 for (; cr_whitespace (buffer[i]); i++)
502 if (buffer[i] == '\0' || buffer[i] == '#')
505 /* Isolate the current word. */
506 for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++)
510 strncpy (name, buffer + start, i - start);
511 name[i - start] = '\0';
512 add_host_name (name);
518 /* Return the hostname list. */
522 if (hostname_list_initialized == 0)
523 initialize_hostname_list ();
524 return (hostname_list);
528 clear_hostname_list ()
532 if (hostname_list_initialized == 0)
534 for (i = 0; i < hostname_list_length; i++)
535 free (hostname_list[i]);
536 hostname_list_length = 0;
539 /* Return a NULL terminated list of hostnames which begin with TEXT.
540 Initialize the hostname list the first time if neccessary.
541 The array is malloc ()'ed, but not the individual strings. */
543 hostnames_matching (text)
546 register int i, len, nmatch, rsize;
549 if (hostname_list_initialized == 0)
550 initialize_hostname_list ();
552 if (hostname_list_initialized == 0)
553 return ((char **)NULL);
555 /* Special case. If TEXT consists of nothing, then the whole list is
559 result = alloc_array (1 + hostname_list_length);
560 for (i = 0; i < hostname_list_length; i++)
561 result[i] = hostname_list[i];
562 result[i] = (char *)NULL;
566 /* Scan until found, or failure. */
568 result = (char **)NULL;
569 for (i = nmatch = rsize = 0; i < hostname_list_length; i++)
571 if (STREQN (text, hostname_list[i], len) == 0)
574 /* OK, it matches. Add it to the list. */
575 if (nmatch >= (rsize - 1))
577 rsize = (rsize + 16) - (rsize % 16);
578 result = (char **)xrealloc (result, rsize * sizeof (char *));
581 result[nmatch++] = hostname_list[i];
584 result[nmatch] = (char *)NULL;
588 /* The equivalent of the Korn shell C-o operate-and-get-next-history-line
590 static int saved_history_line_to_use = -1;
595 if (saved_history_line_to_use >= 0)
596 rl_get_previous_history (history_length - saved_history_line_to_use, 0);
597 saved_history_line_to_use = -1;
598 rl_startup_hook = old_rl_startup_hook;
602 operate_and_get_next (count, c)
607 /* Accept the current line. */
610 /* Find the current line, and find the next line to use. */
611 where = where_history ();
613 if ((history_is_stifled () && (history_length >= max_input_history)) ||
614 (where >= history_length - 1))
615 saved_history_line_to_use = where;
617 saved_history_line_to_use = where + 1;
619 old_rl_startup_hook = rl_startup_hook;
620 rl_startup_hook = (Function *)set_saved_history;
623 #if defined (VI_MODE)
624 /* This vi mode command causes VI_EDIT_COMMAND to be run on the current
625 command being entered (if no explicit argument is given), otherwise on
626 a command from the history file. */
628 #define VI_EDIT_COMMAND "fc -e ${VISUAL:-${EDITOR:-vi}}"
631 vi_edit_and_execute_command (count, c)
636 /* Accept the current line. */
641 command = xmalloc (strlen (VI_EDIT_COMMAND) + 8);
642 sprintf (command, "%s %d", VI_EDIT_COMMAND, count);
646 /* Take the command we were just editing, add it to the history file,
647 then call fc to operate on it. We have to add a dummy command to
648 the end of the history because fc ignores the last command (assumes
649 it's supposed to deal with the command before the `fc'). */
651 bash_add_history (rl_line_buffer);
652 bash_add_history ("");
653 history_lines_this_session++;
655 command = savestring (VI_EDIT_COMMAND);
657 parse_and_execute (command, "v", SEVAL_NOHIST);
658 rl_line_buffer[0] = '\0'; /* XXX */
664 posix_edit_macros (count, key)
668 char alias_name[3], *alias_value, *macro;
673 alias_name[2] = '\0';
675 alias_value = get_alias_value (alias_name);
676 if (alias_value && *alias_value)
678 macro = savestring (alias_value);
679 rl_push_macro_input (macro);
685 /* **************************************************************** */
687 /* How To Do Shell Completion */
689 /* **************************************************************** */
691 #define COMMAND_SEPARATORS ";|&{(`"
697 register int this_char, prev_char;
699 /* Handle the two character tokens `>&', `<&', and `>|'.
700 We are not in a command position after one of these. */
701 this_char = rl_line_buffer[ti];
702 prev_char = rl_line_buffer[ti - 1];
704 if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) ||
705 (this_char == '|' && prev_char == '>'))
707 else if ((this_char == '{' && prev_char == '$') || /* } */
708 (char_is_quoted (rl_line_buffer, ti)))
713 #if defined (PROGRAMMABLE_COMPLETION)
715 find_cmd_start (start)
721 while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS)) <= start) &&
733 e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS);
738 find_cmd_name (start)
744 for (s = start; whitespace (rl_line_buffer[s]); s++)
747 /* skip until a shell break character */
748 e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n");
750 name = substring (rl_line_buffer, s, e);
756 prog_complete_return (text, matchnum)
765 if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0)
767 return (prog_complete_matches[ind++]);
770 #endif /* PROGRAMMABLE_COMPLETION */
772 /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
773 at START and END. Return an array of matches, or NULL if none. */
775 attempt_shell_completion (text, start, end)
779 int in_command_position, ti, saveti, qc;
780 char **matches, *command_separator_chars;
782 command_separator_chars = COMMAND_SEPARATORS;
783 matches = (char **)NULL;
784 rl_ignore_some_completions_function = (Function *)filename_completion_ignore;
786 /* Determine if this could be a command word. It is if it appears at
787 the start of the line (ignoring preceding whitespace), or if it
788 appears after a character that separates commands. It cannot be a
789 command word if we aren't at the top-level prompt. */
793 while ((ti > -1) && (whitespace (rl_line_buffer[ti])))
797 /* If this is an open quote, maybe we're trying to complete a quoted
799 if (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\'')
801 qc = rl_line_buffer[ti];
803 while (ti > -1 && (whitespace (rl_line_buffer[ti])))
808 in_command_position = 0;
811 /* Only do command completion at the start of a line when we
812 are prompting at the top level. */
813 if (current_prompt_string == ps1_prompt)
814 in_command_position++;
816 else if (member (rl_line_buffer[ti], command_separator_chars))
818 in_command_position++;
820 if (check_redir (ti) == 1)
821 in_command_position = 0;
825 /* This still could be in command position. It is possible
826 that all of the previous words on the line are variable
830 /* Check that we haven't incorrectly flagged a closed command substitution
831 as indicating we're in a command position. */
832 if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' &&
833 *text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0)
834 in_command_position = 0;
836 /* Special handling for command substitution. If *TEXT is a backquote,
837 it can be the start or end of an old-style command substitution, or
838 unmatched. If it's unmatched, both calls to unclosed_pair will
841 (in_command_position || (unclosed_pair (rl_line_buffer, start, "`") &&
842 unclosed_pair (rl_line_buffer, end, "`"))))
843 matches = completion_matches (text, command_subst_completion_function);
845 #if defined (PROGRAMMABLE_COMPLETION)
846 /* Attempt programmable completion. */
847 if (!matches && in_command_position == 0 && prog_completion_enabled &&
848 (num_progcomps () > 0) && current_prompt_string == ps1_prompt)
853 /* XXX - don't free the members */
854 if (prog_complete_matches)
855 free (prog_complete_matches);
856 prog_complete_matches = (char **)NULL;
858 s = find_cmd_start (start);
859 e = find_cmd_end (end);
860 n = find_cmd_name (s);
861 prog_complete_matches = programmable_completions (n, text, s, e, &foundcs);
863 /* XXX - if we found a COMPSPEC for the command, just return whatever
864 the programmable completion code returns, and disable the default
865 filename completion that readline will do. */
868 /* Turn what the programmable completion code returns into what
869 readline wants. I should have made compute_lcd_of_matches
871 matches = completion_matches (text, prog_complete_return);
872 rl_attempted_completion_over = 1; /* no default */
878 /* New posix-style command substitution or variable name? */
879 if (!matches && *text == '$')
881 if (qc != '\'' && text[1] == '(') /* ) */
882 matches = completion_matches (text, command_subst_completion_function);
884 matches = completion_matches (text, variable_completion_function);
887 /* If the word starts in `~', and there is no slash in the word, then
888 try completing this word as a username. */
889 if (!matches && *text == '~' && !strchr (text, '/'))
890 matches = completion_matches (text, username_completion_function);
892 /* Another one. Why not? If the word starts in '@', then look through
893 the world of known hostnames for completion first. */
894 if (!matches && perform_hostname_completion && *text == '@')
895 matches = completion_matches (text, hostname_completion_function);
897 /* And last, (but not least) if this word is in a command position, then
898 complete over possible command names, including aliases, functions,
899 and command names. */
900 if (!matches && in_command_position)
902 if (start == 0 && end == 0 && text[0] == '\0' && no_empty_command_completion)
904 matches = (char **)NULL;
905 rl_ignore_some_completions_function = (Function *)bash_ignore_everything;
909 matches = completion_matches (text, command_word_completion_function);
910 /* If we are attempting command completion and nothing matches, we
911 do not want readline to perform filename completion for us. We
912 still want to be able to complete partial pathnames, so set the
913 completion ignore function to something which will remove
914 filenames and leave directories in the match list. */
915 if (matches == (char **)NULL)
916 rl_ignore_some_completions_function = (Function *)bash_ignore_filenames;
920 /* This could be a globbing pattern, so try to expand it using pathname
922 if (!matches && glob_pattern_p (text))
924 matches = completion_matches (text, glob_complete_word);
925 /* A glob expression that matches more than one filename is problematic.
926 If we match more than one filename, punt. */
927 if (matches && matches[1])
929 free_array (matches);
930 matches = (char **)0;
937 /* This is the function to call when the word to complete is in a position
938 where a command word can be found. It grovels $PATH, looking for commands
939 that match. It also scans aliases, function names, and the shell_builtin
942 command_word_completion_function (hint_text, state)
946 static char *hint = (char *)NULL;
947 static char *path = (char *)NULL;
948 static char *val = (char *)NULL;
949 static char *filename_hint = (char *)NULL;
950 static int path_index, hint_len, istate;
951 static int mapping_over, local_index;
952 static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
954 static alias_t **alias_list = (alias_t **)NULL;
957 /* We have to map over the possibilities for command words. If we have
958 no state, then make one just for that purpose. */
967 /* If this is an absolute program name, do not check it against
968 aliases, reserved words, functions or builtins. We must check
969 whether or not it is unique, and, if so, whether that filename
971 if (absolute_program (hint_text))
973 /* Perform tilde expansion on what's passed, so we don't end up
974 passing filenames with tildes directly to stat(). */
975 if (*hint_text == '~')
976 hint = bash_tilde_expand (hint_text);
978 hint = savestring (hint_text);
979 hint_len = strlen (hint);
982 free (filename_hint);
983 filename_hint = savestring (hint);
990 hint = savestring (hint_text);
991 hint_len = strlen (hint);
993 path = get_string_value ("PATH");
996 /* Initialize the variables for each type of command word. */
1002 varlist = all_visible_functions ();
1008 alias_list = all_aliases ();
1012 /* mapping_over says what we are currently hacking. Note that every case
1013 in this list must fall through when there are no more possibilities. */
1015 switch (mapping_over)
1017 case 0: /* Aliases come first. */
1019 while (alias_list && alias_list[local_index])
1021 register char *alias;
1023 alias = alias_list[local_index++]->name;
1025 if (STREQN (alias, hint, hint_len))
1026 return (savestring (alias));
1032 case 1: /* Then shell reserved words. */
1034 while (word_token_alist[local_index].word)
1036 register char *reserved_word;
1038 reserved_word = word_token_alist[local_index++].word;
1040 if (STREQN (reserved_word, hint, hint_len))
1041 return (savestring (reserved_word));
1047 case 2: /* Then function names. */
1048 while (varlist && varlist[local_index])
1050 register char *varname;
1052 varname = varlist[local_index++]->name;
1054 if (STREQN (varname, hint, hint_len))
1055 return (savestring (varname));
1060 case 3: /* Then shell builtins. */
1061 for (; local_index < num_shell_builtins; local_index++)
1063 /* Ignore it if it doesn't have a function pointer or if it
1064 is not currently enabled. */
1065 if (!shell_builtins[local_index].function ||
1066 (shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0)
1069 if (STREQN (shell_builtins[local_index].name, hint, hint_len))
1071 int i = local_index++;
1073 return (savestring (shell_builtins[i].name));
1080 /* Repeatedly call filename_completion_function while we have
1081 members of PATH left. Question: should we stat each file?
1082 Answer: we call executable_file () on each file. */
1085 istate = (val != (char *)NULL);
1091 /* Get the next directory from the path. If there is none, then we
1093 if (!path || !path[path_index] ||
1094 (current_path = extract_colon_unit (path, &path_index)) == 0)
1095 return ((char *)NULL);
1097 if (*current_path == 0)
1099 free (current_path);
1100 current_path = savestring (".");
1103 if (*current_path == '~')
1107 t = bash_tilde_expand (current_path);
1108 free (current_path);
1113 free (filename_hint);
1115 filename_hint = xmalloc (2 + strlen (current_path) + hint_len);
1116 sprintf (filename_hint, "%s/%s", current_path, hint);
1118 free (current_path);
1122 val = filename_completion_function (filename_hint, istate);
1127 /* If the hint text is an absolute program, then don't bother
1128 searching through PATH. */
1129 if (absolute_program (hint))
1130 return ((char *)NULL);
1136 int match, freetemp;
1139 if (absolute_program (hint))
1141 match = strncmp (val, hint, hint_len) == 0;
1142 /* If we performed tilde expansion, restore the original
1144 if (*hint_text == '~')
1148 tl = strlen (hint_text);
1149 l = vl - hint_len; /* # of chars added */
1150 temp = xmalloc (l + 2 + tl);
1151 strcpy (temp, hint_text);
1152 strcpy (temp + tl, val + vl - l);
1155 temp = savestring (val);
1160 temp = strrchr (val, '/');
1165 freetemp = match = strncmp (temp, hint, hint_len) == 0;
1167 temp = savestring (temp);
1170 freetemp = match = 0;
1173 /* If we have found a match, and it is an executable file or a
1174 directory name, return it. */
1175 if (match && (executable_file (val) || is_directory (val)))
1178 val = ""; /* So it won't be NULL. */
1191 /* Completion inside an unterminated command substitution. */
1193 command_subst_completion_function (text, state)
1197 static char **matches = (char **)NULL;
1198 static char *orig_start, *filename_text = (char *)NULL;
1199 static int cmd_index, start_len;
1205 free (filename_text);
1209 else if (*text == '$' && text[1] == '(') /* ) */
1211 start_len = text - orig_start;
1212 filename_text = savestring (text);
1215 matches = completion_matches (filename_text, command_word_completion_function);
1219 if (!matches || !matches[cmd_index])
1221 rl_filename_quoting_desired = 0; /* disable quoting */
1222 return ((char *)NULL);
1226 value = xmalloc (1 + start_len + strlen (matches[cmd_index]));
1229 value[0] = *orig_start;
1231 strncpy (value, orig_start, start_len);
1233 strcpy (value + start_len, matches[cmd_index]);
1240 /* Okay, now we write the entry_function for variable completion. */
1242 variable_completion_function (text, state)
1246 static char **varlist = (char **)NULL;
1247 static int varlist_index;
1248 static char *varname = (char *)NULL;
1250 static int first_char, first_char_loc;
1258 first_char = text[0];
1260 if (first_char == '$')
1263 if (text[first_char_loc] == '{')
1266 varname = savestring (text + first_char_loc);
1268 namelen = strlen (varname);
1270 free_array (varlist);
1272 varlist = all_variables_matching_prefix (varname);
1276 if (!varlist || !varlist[varlist_index])
1278 return ((char *)NULL);
1282 char *value = xmalloc (4 + strlen (varlist[varlist_index]));
1286 value[0] = first_char;
1287 if (first_char_loc == 2)
1291 strcpy (value + first_char_loc, varlist[varlist_index]);
1292 if (first_char_loc == 2)
1293 strcat (value, "}");
1300 /* How about a completion function for hostnames? */
1302 hostname_completion_function (text, state)
1306 static char **list = (char **)NULL;
1307 static int list_index = 0;
1308 static int first_char, first_char_loc;
1310 /* If we don't have any state, make some. */
1315 list = (char **)NULL;
1320 if (first_char == '@')
1323 list = hostnames_matching (&text[first_char_loc]);
1327 if (list && list[list_index])
1331 t = xmalloc (2 + strlen (list[list_index]));
1333 strcpy (t + first_char_loc, list[list_index]);
1338 return ((char *)NULL);
1341 /* Functions to perform history and alias expansions on the current line. */
1343 #if defined (BANG_HISTORY)
1344 /* Perform history expansion on the current line. If no history expansion
1345 is done, pre_process_line() returns what it was passed, so we need to
1346 allocate a new line here. */
1348 history_expand_line_internal (line)
1353 new_line = pre_process_line (line, 0, 0);
1354 return (new_line == line) ? savestring (line) : new_line;
1358 /* There was an error in expansion. Let the preprocessor print
1361 cleanup_expansion_error ()
1365 fprintf (rl_outstream, "\r\n");
1366 to_free = pre_process_line (rl_line_buffer, 1, 0);
1367 if (to_free != rl_line_buffer)
1369 putc ('\r', rl_outstream);
1370 rl_forced_update_display ();
1373 /* If NEW_LINE differs from what is in the readline line buffer, add an
1374 undo record to get from the readline line buffer contents to the new
1375 line and make NEW_LINE the current readline line. */
1377 maybe_make_readline_line (new_line)
1380 if (strcmp (new_line, rl_line_buffer) != 0)
1384 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
1385 rl_delete_text (0, rl_point);
1386 rl_point = rl_end = 0;
1387 rl_insert_text (new_line);
1388 rl_add_undo (UNDO_END, 0, 0, 0);
1392 /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
1394 set_up_new_line (new_line)
1397 int old_point = rl_point;
1398 int at_end = rl_point == rl_end;
1400 /* If the line was history and alias expanded, then make that
1401 be one thing to undo. */
1402 maybe_make_readline_line (new_line);
1405 /* Place rl_point where we think it should go. */
1408 else if (old_point < rl_end)
1410 rl_point = old_point;
1411 if (!whitespace (rl_line_buffer[rl_point]))
1412 rl_forward_word (1, 0);
1417 /* Expand aliases in the current readline line. */
1419 alias_expand_line (ignore)
1424 new_line = alias_expand (rl_line_buffer);
1428 set_up_new_line (new_line);
1433 cleanup_expansion_error ();
1439 #if defined (BANG_HISTORY)
1440 /* History expand the line. */
1442 history_expand_line (ignore)
1447 new_line = history_expand_line_internal (rl_line_buffer);
1451 set_up_new_line (new_line);
1456 cleanup_expansion_error ();
1461 /* Expand history substitutions in the current line and then insert a
1462 space wherever set_up_new_line decided to put rl_point. */
1464 tcsh_magic_space (ignore)
1467 if (history_expand_line (ignore) == 0)
1477 /* History and alias expand the line. */
1479 history_and_alias_expand_line (ignore)
1484 new_line = pre_process_line (rl_line_buffer, 0, 0);
1485 if (new_line == rl_line_buffer)
1486 new_line = savestring (new_line);
1493 alias_line = alias_expand (new_line);
1495 new_line = alias_line;
1501 set_up_new_line (new_line);
1506 cleanup_expansion_error ();
1511 /* History and alias expand the line, then perform the shell word
1512 expansions by calling expand_string. This can't use set_up_new_line()
1513 because we want the variable expansions as a separate undo'able
1514 set of operations. */
1516 shell_expand_line (ignore)
1520 WORD_LIST *expanded_string;
1522 new_line = pre_process_line (rl_line_buffer, 0, 0);
1523 if (new_line == rl_line_buffer)
1524 new_line = savestring (new_line);
1531 alias_line = alias_expand (new_line);
1533 new_line = alias_line;
1539 int old_point = rl_point;
1540 int at_end = rl_point == rl_end;
1542 /* If the line was history and alias expanded, then make that
1543 be one thing to undo. */
1544 maybe_make_readline_line (new_line);
1547 /* If there is variable expansion to perform, do that as a separate
1548 operation to be undone. */
1549 new_line = savestring (rl_line_buffer);
1550 expanded_string = expand_string (new_line, 0);
1552 if (expanded_string == 0)
1554 new_line = xmalloc (1);
1559 new_line = string_list (expanded_string);
1560 dispose_words (expanded_string);
1563 maybe_make_readline_line (new_line);
1566 /* Place rl_point where we think it should go. */
1569 else if (old_point < rl_end)
1571 rl_point = old_point;
1572 if (!whitespace (rl_line_buffer[rl_point]))
1573 rl_forward_word (1, 0);
1577 cleanup_expansion_error ();
1580 /* Define NO_FORCE_FIGNORE if you want to match filenames that would
1581 otherwise be ignored if they are the only possible matches. */
1582 /* #define NO_FORCE_FIGNORE */
1584 /* If FIGNORE is set, then don't match files with the given suffixes when
1585 completing filenames. If only one of the possibilities has an acceptable
1586 suffix, delete the others, else just return and let the completer
1587 signal an error. It is called by the completer when real
1588 completions are done on filenames by the completer's internal
1589 function, not for completion lists (M-?) and not on "other"
1590 completion types, such as hostnames or commands. */
1592 static struct ignorevar fignore =
1602 _ignore_completion_names (names, name_func)
1604 Function *name_func;
1608 #ifdef NO_FORCE_FIGNORE
1613 /* If there is only one completion, see if it is acceptable. If it is
1614 not, free it up. In any case, short-circuit and return. This is a
1615 special case because names[0] is not the prefix of the list of names
1616 if there is only one completion; it is the completion itself. */
1617 if (names[1] == (char *)0)
1619 #ifndef NO_FORCE_FIGNORE
1620 if ((*name_func) (names[0]) == 0)
1623 names[0] = (char *)NULL;
1629 /* Allocate space for array to hold list of pointers to matching
1630 filenames. The pointers are copied back to NAMES when done. */
1631 for (nidx = 1; names[nidx]; nidx++)
1633 newnames = alloc_array (nidx + 1);
1634 #ifdef NO_FORCE_FIGNORE
1635 oldnames = alloc_array (nidx - 1);
1639 newnames[0] = names[0];
1640 for (idx = nidx = 1; names[idx]; idx++)
1642 if ((*name_func) (names[idx]))
1643 newnames[nidx++] = names[idx];
1645 #ifndef NO_FORCE_FIGNORE
1648 oldnames[oidx++] = names[idx];
1652 newnames[nidx] = (char *)NULL;
1654 /* If none are acceptable then let the completer handle it. */
1657 #ifndef NO_FORCE_FIGNORE
1659 names[0] = (char *)NULL;
1667 #ifdef NO_FORCE_FIGNORE
1669 free (oldnames[--oidx]);
1673 /* If only one is acceptable, copy it to names[0] and return. */
1677 names[0] = newnames[1];
1678 names[1] = (char *)NULL;
1683 /* Copy the acceptable names back to NAMES, set the new array end,
1685 for (nidx = 1; newnames[nidx]; nidx++)
1686 names[nidx] = newnames[nidx];
1687 names[nidx] = (char *)NULL;
1692 name_is_acceptable (name)
1698 for (nlen = strlen (name), p = fignore.ignores; p->val; p++)
1700 if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len]))
1709 ignore_dot_names (name)
1712 return (name[0] != '.');
1717 filename_completion_ignore (names)
1721 if (glob_dot_filenames == 0)
1722 _ignore_completion_names (names, ignore_dot_names);
1725 setup_ignore_patterns (&fignore);
1727 if (fignore.num_ignores == 0)
1730 _ignore_completion_names (names, name_is_acceptable);
1733 /* Return 1 if NAME is a directory. */
1735 test_for_directory (name)
1741 fn = bash_tilde_expand (name);
1742 if (stat (fn, &finfo) != 0)
1748 return (S_ISDIR (finfo.st_mode));
1751 /* Remove files from NAMES, leaving directories. */
1753 bash_ignore_filenames (names)
1756 _ignore_completion_names (names, test_for_directory);
1767 bash_ignore_everything (names)
1770 _ignore_completion_names (names, return_zero);
1773 /* Handle symbolic link references and other directory name
1774 expansions while hacking completion. */
1776 bash_directory_completion_hook (dirname)
1779 char *local_dirname, *new_dirname, *t;
1780 int return_value, should_expand_dirname;
1783 return_value = should_expand_dirname = 0;
1784 local_dirname = *dirname;
1787 should_expand_dirname = strchr (local_dirname, '$') || strchr (local_dirname, '`');
1789 if (strchr (local_dirname, '$'))
1790 should_expand_dirname = 1;
1793 t = strchr (local_dirname, '`');
1794 if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0)
1795 should_expand_dirname = 1;
1799 if (should_expand_dirname)
1801 new_dirname = savestring (local_dirname);
1802 wl = expand_string (new_dirname, 0);
1805 *dirname = string_list (wl);
1806 /* Tell the completer to replace the directory name only if we
1807 actually expanded something. */
1808 return_value = STREQ (local_dirname, *dirname) == 0;
1809 free (local_dirname);
1812 local_dirname = *dirname;
1817 free (local_dirname);
1818 *dirname = xmalloc (1);
1824 if (!no_symbolic_links && (local_dirname[0] != '.' || local_dirname[1]))
1826 char *temp1, *temp2;
1829 t = get_working_directory ("symlink-hook");
1830 temp1 = make_absolute (local_dirname, t);
1832 temp2 = canonicalize_pathname (temp1);
1833 /* If we can't canonicalize, bail. */
1839 len1 = strlen (temp1);
1840 if (temp1[len1 - 1] == '/')
1842 len2 = strlen (temp2);
1843 temp2 = xrealloc (temp2, len2 + 2);
1845 temp2[len2 + 1] = '\0';
1847 free (local_dirname);
1851 return (return_value);
1854 static char **history_completion_array = (char **)NULL;
1855 static int harry_size;
1856 static int harry_len;
1859 build_history_completion_array ()
1865 /* First, clear out the current dynamic history completion list. */
1868 for (i = 0; history_completion_array[i]; i++)
1869 free (history_completion_array[i]);
1871 free (history_completion_array);
1873 history_completion_array = (char **)NULL;
1878 /* Next, grovel each line of history, making each shell-sized token
1879 a separate entry in the history_completion_array. */
1880 hlist = history_list ();
1884 for (i = 0; hlist[i]; i++)
1886 /* Separate each token, and place into an array. */
1887 tokens = history_tokenize (hlist[i]->line);
1889 for (j = 0; tokens && tokens[j]; j++)
1891 if (harry_len + 2 > harry_size)
1894 history_completion_array = (char **) xrealloc
1895 (history_completion_array, harry_size * sizeof (char *));
1898 history_completion_array[harry_len++] = tokens[j];
1899 history_completion_array[harry_len] = (char *)NULL;
1904 /* Sort the complete list of tokens. */
1905 qsort (history_completion_array, harry_len, sizeof (char *), (Function *)qsort_string_compare);
1910 history_completion_generator (hint_text, state)
1914 static int local_index, len;
1917 /* If this is the first call to the generator, then initialize the
1918 list of strings to complete over. */
1922 build_history_completion_array ();
1924 len = strlen (text);
1927 while (history_completion_array && history_completion_array[local_index])
1929 if (strncmp (text, history_completion_array[local_index++], len) == 0)
1930 return (savestring (history_completion_array[local_index - 1]));
1932 return ((char *)NULL);
1936 dynamic_complete_history (count, key)
1939 Function *orig_func;
1940 CPPFunction *orig_attempt_func;
1942 orig_func = rl_completion_entry_function;
1943 orig_attempt_func = rl_attempted_completion_function;
1944 rl_completion_entry_function = (Function *)history_completion_generator;
1945 rl_attempted_completion_function = (CPPFunction *)NULL;
1947 if (rl_last_func == (Function *)dynamic_complete_history)
1948 rl_complete_internal ('?');
1950 rl_complete_internal (TAB);
1952 rl_completion_entry_function = orig_func;
1953 rl_attempted_completion_function = orig_attempt_func;
1956 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
1958 bash_complete_username (ignore, ignore2)
1959 int ignore, ignore2;
1961 bash_complete_username_internal (TAB);
1965 bash_possible_username_completions (ignore, ignore2)
1966 int ignore, ignore2;
1968 bash_complete_username_internal ('?');
1972 bash_complete_username_internal (what_to_do)
1975 bash_specific_completion
1976 (what_to_do, (Function *)username_completion_function);
1980 bash_complete_filename (ignore, ignore2)
1981 int ignore, ignore2;
1983 bash_complete_filename_internal (TAB);
1987 bash_possible_filename_completions (ignore, ignore2)
1988 int ignore, ignore2;
1990 bash_complete_filename_internal ('?');
1994 bash_complete_filename_internal (what_to_do)
1997 Function *orig_func, *orig_dir_func;
1998 CPPFunction *orig_attempt_func;
1999 char *orig_rl_completer_word_break_characters;
2001 orig_func = rl_completion_entry_function;
2002 orig_attempt_func = rl_attempted_completion_function;
2003 orig_dir_func = rl_directory_completion_hook;
2004 orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
2005 rl_completion_entry_function = (Function *)filename_completion_function;
2006 rl_attempted_completion_function = (CPPFunction *)NULL;
2007 rl_directory_completion_hook = (Function *)NULL;
2008 rl_completer_word_break_characters = " \t\n\"\'";
2010 rl_complete_internal (what_to_do);
2012 rl_completion_entry_function = orig_func;
2013 rl_attempted_completion_function = orig_attempt_func;
2014 rl_directory_completion_hook = orig_dir_func;
2015 rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
2019 bash_complete_hostname (ignore, ignore2)
2020 int ignore, ignore2;
2022 bash_complete_hostname_internal (TAB);
2026 bash_possible_hostname_completions (ignore, ignore2)
2027 int ignore, ignore2;
2029 bash_complete_hostname_internal ('?');
2033 bash_complete_variable (ignore, ignore2)
2034 int ignore, ignore2;
2036 bash_complete_variable_internal (TAB);
2040 bash_possible_variable_completions (ignore, ignore2)
2041 int ignore, ignore2;
2043 bash_complete_variable_internal ('?');
2047 bash_complete_command (ignore, ignore2)
2048 int ignore, ignore2;
2050 bash_complete_command_internal (TAB);
2054 bash_possible_command_completions (ignore, ignore2)
2055 int ignore, ignore2;
2057 bash_complete_command_internal ('?');
2061 bash_complete_hostname_internal (what_to_do)
2064 bash_specific_completion
2065 (what_to_do, (Function *)hostname_completion_function);
2069 bash_complete_variable_internal (what_to_do)
2072 bash_specific_completion
2073 (what_to_do, (Function *)variable_completion_function);
2077 bash_complete_command_internal (what_to_do)
2080 bash_specific_completion
2081 (what_to_do, (Function *)command_word_completion_function);
2085 glob_complete_word (text, state)
2089 static char **matches = (char **)NULL;
2095 rl_filename_completion_desired = 1;
2098 matches = shell_glob_filename (text);
2099 if (GLOB_FAILED (matches))
2100 matches = (char **)NULL;
2104 ret = matches ? matches[ind] : (char *)NULL;
2110 bash_glob_completion_internal (what_to_do)
2113 bash_specific_completion (what_to_do, (Function *)glob_complete_word);
2117 bash_glob_expand_word (count, key)
2120 bash_glob_completion_internal ('*');
2124 bash_glob_list_expansions (count, key)
2127 bash_glob_completion_internal ('?');
2131 bash_specific_completion (what_to_do, generator)
2133 Function *generator;
2135 Function *orig_func;
2136 CPPFunction *orig_attempt_func;
2138 orig_func = rl_completion_entry_function;
2139 orig_attempt_func = rl_attempted_completion_function;
2140 rl_completion_entry_function = generator;
2141 rl_attempted_completion_function = (CPPFunction *)NULL;
2143 rl_complete_internal (what_to_do);
2145 rl_completion_entry_function = orig_func;
2146 rl_attempted_completion_function = orig_attempt_func;
2149 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
2151 /* Filename quoting for completion. */
2152 /* A function to strip unquoted quote characters (single quotes, double
2153 quotes, and backslashes). It allows single quotes to appear
2154 within double quotes, and vice versa. It should be smarter. */
2156 bash_dequote_filename (text, quote_char)
2163 ret = xmalloc (l + 1);
2164 for (quoted = quote_char, p = text, r = ret; p && *p; p++)
2166 /* Allow backslash-quoted characters to pass through unscathed. */
2175 if (quoted && *p == quoted)
2181 if (quoted == 0 && (*p == '\'' || *p == '"'))
2192 /* Quote characters that the readline completion code would treat as
2193 word break characters with backslashes. Pass backslash-quoted
2194 characters through without examination. */
2196 quote_word_break_chars (text)
2203 ret = xmalloc ((2 * l) + 1);
2204 for (s = text, r = ret; *s; s++)
2206 /* Pass backslash-quoted characters through, including the backslash. */
2215 /* OK, we have an unquoted character. Check its presence in
2216 rl_completer_word_break_characters. */
2217 if (strchr (rl_completer_word_break_characters, *s))
2225 /* Quote a filename using double quotes, single quotes, or backslashes
2226 depending on the value of completion_quoting_style. If we're
2227 completing using backslashes, we need to quote some additional
2228 characters (those that readline treats as word breaks), so we call
2229 quote_word_break_chars on the result. */
2231 bash_quote_filename (s, rtype, qcp)
2236 char *rtext, *mtext, *ret;
2239 rtext = (char *)NULL;
2241 /* If RTYPE == MULT_MATCH, it means that there is
2242 more than one match. In this case, we do not add
2243 the closing quote or attempt to perform tilde
2244 expansion. If RTYPE == SINGLE_MATCH, we try
2245 to perform tilde expansion, because single and double
2246 quotes inhibit tilde expansion by the shell. */
2249 if (mtext[0] == '~' && rtype == SINGLE_MATCH)
2250 mtext = bash_tilde_expand (s);
2252 cs = completion_quoting_style;
2253 /* Might need to modify the default completion style based on *qcp,
2254 since it's set to any user-provided opening quote. We also change
2255 to single-quoting if there is no user-provided opening quote and
2256 the word being completed contains newlines, since those are not
2257 quoted correctly using backslashes (a backslash-newline pair is
2258 special to the shell parser). */
2259 if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && strchr (mtext, '\n'))
2260 cs = COMPLETE_SQUOTE;
2261 else if (*qcp == '"')
2262 cs = COMPLETE_DQUOTE;
2263 else if (*qcp == '\'')
2264 cs = COMPLETE_SQUOTE;
2265 #if defined (BANG_HISTORY)
2266 else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE &&
2267 history_expansion_inhibited == 0 && strchr (mtext, '!'))
2268 cs = COMPLETE_BSQUOTE;
2270 if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE &&
2271 history_expansion_inhibited == 0 && strchr (mtext, '!'))
2273 cs = COMPLETE_BSQUOTE;
2280 case COMPLETE_DQUOTE:
2281 rtext = double_quote (mtext);
2283 case COMPLETE_SQUOTE:
2284 rtext = single_quote (mtext);
2286 case COMPLETE_BSQUOTE:
2287 rtext = backslash_quote (mtext);
2294 /* We may need to quote additional characters: those that readline treats
2295 as word breaks that are not quoted by backslash_quote. */
2296 if (rtext && cs == COMPLETE_BSQUOTE)
2298 mtext = quote_word_break_chars (rtext);
2303 /* Leave the opening quote intact. The readline completion code takes
2304 care of avoiding doubled opening quotes. */
2305 rlen = strlen (rtext);
2306 ret = xmalloc (rlen + 1);
2307 strcpy (ret, rtext);
2309 /* If there are multiple matches, cut off the closing quote. */
2310 if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE)
2311 ret[rlen - 1] = '\0';
2316 /* Support for binding readline key sequences to Unix commands. */
2317 static Keymap cmd_xmap;
2320 bash_execute_unix_command (count, key)
2321 int count; /* ignored */
2324 Keymap ckmap; /* current keymap */
2325 Keymap xkmap; /* unix command executing keymap */
2329 /* First, we need to find the right command to execute. This is tricky,
2330 because we might have already indirected into another keymap. */
2331 ckmap = rl_get_keymap ();
2332 if (ckmap != rl_executing_keymap)
2334 /* bogus. we have to search. only handle one level of indirection. */
2335 for (i = 0; i < KEYMAP_SIZE; i++)
2337 if (ckmap[i].type == ISKMAP && (Keymap)ckmap[i].function == rl_executing_keymap)
2340 if (i < KEYMAP_SIZE)
2341 xkmap = (Keymap)cmd_xmap[i].function;
2345 internal_error ("bash_execute_unix_command: cannot find keymap for command");
2346 rl_forced_update_display ();
2353 cmd = (char *)xkmap[key].function;
2361 crlf (); /* move to a new line */
2363 cmd = savestring (cmd);
2364 parse_and_execute (cmd, "bash_execute_unix_command", 0);
2366 /* and restore the readline buffer and display after command execution. */
2367 rl_forced_update_display ();
2372 init_unix_command_map ()
2374 cmd_xmap = rl_make_bare_keymap ();
2378 isolate_sequence (string, ind, need_dquote, startp)
2380 int ind, need_dquote, *startp;
2383 int c, passc, delim;
2385 for (i = ind; string[i] && whitespace (string[i]); i++)
2387 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
2388 if (need_dquote && string[i] != '"')
2390 builtin_error ("%s: first non-whitespace character is not `\"'", string);
2394 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
2395 string to bind the key sequence to. */
2396 delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0;
2399 *startp = delim ? ++i : i;
2401 for (passc = 0; c = string[i]; i++)
2417 if (delim && string[i] != delim)
2419 builtin_error ("%s: no closing `%c'", string, delim);
2427 bind_keyseq_to_unix_command (line)
2432 int i, kstart, len, ok;
2435 init_unix_command_map ();
2437 kmap = rl_get_keymap ();
2439 /* We duplicate some of the work done by rl_parse_and_bind here, but
2440 this code only has to handle `"keyseq": ["]command["]' and can
2441 generate an error for anything else. */
2442 i = isolate_sequence (line, 0, 1, &kstart);
2446 /* Create the key sequence string to pass to rl_generic_bind */
2447 kseq = substring (line, kstart, i);
2449 for ( ; line[i] && line[i] != ':'; i++)
2453 builtin_error ("%s: missing colon separator", line);
2457 i = isolate_sequence (line, i + 1, 0, &kstart);
2461 /* Create the value string containing the command to execute. */
2462 value = substring (line, kstart, i);
2464 /* Save the command to execute and the key sequence in the CMD_XMAP */
2465 rl_generic_bind (ISMACR, kseq, value, cmd_xmap);
2467 /* and bind the key sequence in the current keymap to a function that
2468 understands how to execute from CMD_XMAP */
2469 rl_set_key (kseq, (Function *)bash_execute_unix_command, kmap);
2474 /* Used by the programmable completion code. Complete TEXT as a filename,
2475 but return only directories as matches. Dequotes the filename before
2476 attempting to find matches. */
2478 bash_directory_completion_matches (text)
2485 qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
2486 dfn = bash_dequote_filename (text, qc);
2487 m1 = completion_matches (dfn, filename_completion_function);
2490 if (m1 == 0 || m1[0] == 0)
2492 /* We don't bother recomputing the lcd of the matches, because it will just
2493 get thrown away by the programmable completion code and recomputed
2495 (void)bash_ignore_filenames (m1);
2498 #endif /* READLINE */