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)
32 #if defined (HAVE_GRP_H)
37 #include "chartypes.h"
43 #include "execute_cmd.h"
46 #include "builtins/common.h"
47 #include <readline/rlconf.h>
48 #include <readline/readline.h>
49 #include <readline/history.h>
51 #include <glob/glob.h>
57 #if defined (PROGRAMMABLE_COMPLETION)
58 # include "pcomplete.h"
61 #if defined (BRACE_COMPLETION)
62 extern int bash_brace_completion __P((int, int));
63 #endif /* BRACE_COMPLETION */
65 /* Forward declarations */
67 /* Functions bound to keys in Readline for Bash users. */
68 static int shell_expand_line __P((int, int));
69 static int display_shell_version __P((int, int));
70 static int operate_and_get_next __P((int, int));
72 static int bash_ignore_filenames __P((char **));
73 static int bash_ignore_everything __P((char **));
75 #if defined (BANG_HISTORY)
76 static char *history_expand_line_internal __P((char *));
77 static int history_expand_line __P((int, int));
78 static int tcsh_magic_space __P((int, int));
79 #endif /* BANG_HISTORY */
81 static int alias_expand_line __P((int, int));
83 #if defined (BANG_HISTORY) && defined (ALIAS)
84 static int history_and_alias_expand_line __P((int, int));
87 /* Helper functions for Readline. */
88 static int bash_directory_completion_hook __P((char **));
89 static int filename_completion_ignore __P((char **));
90 static int bash_push_line __P((void));
92 static void cleanup_expansion_error __P((void));
93 static void maybe_make_readline_line __P((char *));
94 static void set_up_new_line __P((char *));
96 static int check_redir __P((int));
97 static char **attempt_shell_completion __P((const char *, int, int));
98 static char *variable_completion_function __P((const char *, int));
99 static char *hostname_completion_function __P((const char *, int));
100 static char *command_subst_completion_function __P((const char *, int));
102 static void build_history_completion_array __P((void));
103 static char *history_completion_generator __P((const char *, int));
104 static int dynamic_complete_history __P((int, int));
106 static void initialize_hostname_list __P((void));
107 static void add_host_name __P((char *));
108 static void snarf_hosts_from_file __P((char *));
109 static char **hostnames_matching __P((char *));
111 static void _ignore_completion_names __P((char **, sh_ignore_func_t *));
112 static int name_is_acceptable __P((const char *));
113 static int test_for_directory __P((const char *));
114 static int return_zero __P((const char *));
116 static char *bash_dequote_filename __P((char *, int));
117 static char *quote_word_break_chars __P((char *));
118 static char *bash_quote_filename __P((char *, int, char *));
120 static int bash_execute_unix_command __P((int, int));
121 static void init_unix_command_map __P((void));
122 static int isolate_sequence __P((char *, int, int, int *));
124 static int set_saved_history __P((void));
127 static int posix_edit_macros __P((int, int));
130 #if defined (PROGRAMMABLE_COMPLETION)
131 static int find_cmd_start __P((int));
132 static int find_cmd_end __P((int));
133 static char *find_cmd_name __P((int));
134 static char *prog_complete_return __P((const char *, int));
136 static char **prog_complete_matches;
139 /* Variables used here but defined in other files. */
140 extern int current_command_line_count;
141 extern int posixly_correct, no_symbolic_links;
142 extern char *current_prompt_string, *ps1_prompt;
143 extern STRING_INT_ALIST word_token_alist[];
145 /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
146 completion functions which indicate what type of completion should be
147 done (at or before point) that can be bound to key sequences with
148 the readline library. */
149 #define SPECIFIC_COMPLETION_FUNCTIONS
151 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
152 static int bash_specific_completion __P((int, rl_compentry_func_t *));
154 static int bash_complete_filename_internal __P((int));
155 static int bash_complete_username_internal __P((int));
156 static int bash_complete_hostname_internal __P((int));
157 static int bash_complete_variable_internal __P((int));
158 static int bash_complete_command_internal __P((int));
160 static int bash_complete_filename __P((int, int));
161 static int bash_possible_filename_completions __P((int, int));
162 static int bash_complete_username __P((int, int));
163 static int bash_possible_username_completions __P((int, int));
164 static int bash_complete_hostname __P((int, int));
165 static int bash_possible_hostname_completions __P((int, int));
166 static int bash_complete_variable __P((int, int));
167 static int bash_possible_variable_completions __P((int, int));
168 static int bash_complete_command __P((int, int));
169 static int bash_possible_command_completions __P((int, int));
171 static char *glob_complete_word __P((const char *, int));
172 static int bash_glob_completion_internal __P((int));
173 static int bash_glob_expand_word __P((int, int));
174 static int bash_glob_list_expansions __P((int, int));
175 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
177 #if defined (VI_MODE)
178 static int vi_edit_and_execute_command __P((int, int));
181 /* Non-zero once initalize_readline () has been called. */
182 int bash_readline_initialized = 0;
184 /* If non-zero, we do hostname completion, breaking words at `@' and
185 trying to complete the stuff after the `@' from our own internal
187 int perform_hostname_completion = 1;
189 /* If non-zero, we don't do command completion on an empty line. */
190 int no_empty_command_completion;
192 static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:";
193 static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:";
195 static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL;
197 /* What kind of quoting is performed by bash_quote_filename:
198 COMPLETE_DQUOTE = double-quoting the filename
199 COMPLETE_SQUOTE = single_quoting the filename
200 COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
202 #define COMPLETE_DQUOTE 1
203 #define COMPLETE_SQUOTE 2
204 #define COMPLETE_BSQUOTE 3
205 static int completion_quoting_style = COMPLETE_BSQUOTE;
207 /* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
208 Called when the shell is put into or out of `posix' mode. */
210 posix_readline_initialize (on_or_off)
214 rl_variable_bind ("comment-begin", "#");
215 #if defined (VI_MODE)
216 rl_bind_key_in_map (CTRL('I'), on_or_off ? rl_insert : rl_complete, vi_insertion_keymap);
221 enable_hostname_completion (on_or_off)
226 old_value = perform_hostname_completion;
230 perform_hostname_completion = 1;
231 rl_special_prefixes = "$@";
232 rl_completer_word_break_characters = bash_completer_word_break_characters;
236 perform_hostname_completion = 0;
237 rl_special_prefixes = "$";
238 rl_completer_word_break_characters = bash_nohostname_word_break_characters;
244 /* Called once from parse.y if we are going to use readline. */
246 initialize_readline ()
248 if (bash_readline_initialized)
251 rl_terminal_name = get_string_value ("TERM");
253 rl_outstream = stderr;
255 /* Allow conditional parsing of the ~/.inputrc file. */
256 rl_readline_name = "Bash";
258 /* Add bindable names before calling rl_initialize so they may be
259 referenced in the various inputrc files. */
260 rl_add_defun ("shell-expand-line", shell_expand_line, -1);
262 rl_add_defun ("history-expand-line", history_expand_line, -1);
263 rl_add_defun ("magic-space", tcsh_magic_space, -1);
267 rl_add_defun ("alias-expand-line", alias_expand_line, -1);
269 rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line, -1);
273 /* Backwards compatibility. */
274 rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1);
276 rl_add_defun ("operate-and-get-next", operate_and_get_next, -1);
277 rl_add_defun ("display-shell-version", display_shell_version, -1);
279 #if defined (BRACE_COMPLETION)
280 rl_add_defun ("complete-into-braces", bash_brace_completion, -1);
283 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
284 rl_add_defun ("complete-filename", bash_complete_filename, -1);
285 rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1);
286 rl_add_defun ("complete-username", bash_complete_username, -1);
287 rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1);
288 rl_add_defun ("complete-hostname", bash_complete_hostname, -1);
289 rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1);
290 rl_add_defun ("complete-variable", bash_complete_variable, -1);
291 rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1);
292 rl_add_defun ("complete-command", bash_complete_command, -1);
293 rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1);
294 rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1);
295 rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1);
298 rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1);
300 /* Bind defaults before binding our custom shell keybindings. */
301 if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0)
304 /* Bind up our special shell functions. */
305 rl_bind_key_in_map (CTRL('E'), shell_expand_line, emacs_meta_keymap);
307 /* Bind up our special shell functions. */
309 rl_bind_key_in_map ('^', history_expand_line, emacs_meta_keymap);
312 rl_bind_key_in_map (CTRL ('O'), operate_and_get_next, emacs_standard_keymap);
313 rl_bind_key_in_map (CTRL ('V'), display_shell_version, emacs_ctlx_keymap);
315 /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
316 so it is not necessary to allow C-M-j for context switching. Turn
317 off this occasionally confusing behaviour. */
318 rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
319 rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap);
320 #if defined (VI_MODE)
321 rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap);
324 #if defined (BRACE_COMPLETION)
325 rl_bind_key_in_map ('{', bash_brace_completion, emacs_meta_keymap);
326 #endif /* BRACE_COMPLETION */
328 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
329 rl_bind_key_in_map ('/', bash_complete_filename, emacs_meta_keymap);
330 rl_bind_key_in_map ('/', bash_possible_filename_completions, emacs_ctlx_keymap);
332 rl_bind_key_in_map ('~', bash_complete_username, emacs_meta_keymap);
333 rl_bind_key_in_map ('~', bash_possible_username_completions, emacs_ctlx_keymap);
335 rl_bind_key_in_map ('@', bash_complete_hostname, emacs_meta_keymap);
336 rl_bind_key_in_map ('@', bash_possible_hostname_completions, emacs_ctlx_keymap);
338 rl_bind_key_in_map ('$', bash_complete_variable, emacs_meta_keymap);
339 rl_bind_key_in_map ('$', bash_possible_variable_completions, emacs_ctlx_keymap);
341 rl_bind_key_in_map ('!', bash_complete_command, emacs_meta_keymap);
342 rl_bind_key_in_map ('!', bash_possible_command_completions, emacs_ctlx_keymap);
344 rl_bind_key_in_map ('*', bash_glob_expand_word, emacs_ctlx_keymap);
345 rl_bind_key_in_map ('g', bash_glob_list_expansions, emacs_ctlx_keymap);
347 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
349 rl_bind_key_in_map (TAB, dynamic_complete_history, emacs_meta_keymap);
351 /* Tell the completer that we want a crack first. */
352 rl_attempted_completion_function = attempt_shell_completion;
354 /* Tell the completer that we might want to follow symbolic links or
355 do other expansion on directory names. */
356 rl_directory_completion_hook = bash_directory_completion_hook;
358 /* Tell the filename completer we want a chance to ignore some names. */
359 rl_ignore_some_completions_function = filename_completion_ignore;
361 #if defined (VI_MODE)
362 rl_bind_key_in_map ('v', vi_edit_and_execute_command, vi_movement_keymap);
364 rl_bind_key_in_map ('@', posix_edit_macros, vi_movement_keymap);
368 rl_completer_quote_characters = "'\"";
370 /* This sets rl_completer_word_break_characters and rl_special_prefixes
371 to the appropriate values, depending on whether or not hostname
372 completion is enabled. */
373 enable_hostname_completion (perform_hostname_completion);
375 /* characters that need to be quoted when appearing in filenames. */
376 rl_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{"; /*}*/
377 rl_filename_quoting_function = bash_quote_filename;
378 rl_filename_dequoting_function = bash_dequote_filename;
379 rl_char_is_quoted_p = char_is_quoted;
382 posix_readline_initialize (1);
384 bash_readline_initialized = 1;
387 /* On Sun systems at least, rl_attempted_completion_function can end up
388 getting set to NULL, and rl_completion_entry_function set to do command
389 word completion if Bash is interrupted while trying to complete a command
390 word. This just resets all the completion functions to the right thing.
391 It's called from throw_to_top_level(). */
393 bashline_reinitialize ()
396 rl_attempted_completion_function = attempt_shell_completion;
397 rl_completion_entry_function = NULL;
398 rl_directory_completion_hook = bash_directory_completion_hook;
399 rl_ignore_some_completions_function = filename_completion_ignore;
402 /* Contains the line to push into readline. */
403 static char *push_to_readline = (char *)NULL;
405 /* Push the contents of push_to_readline into the
410 if (push_to_readline)
412 rl_insert_text (push_to_readline);
413 free (push_to_readline);
414 push_to_readline = (char *)NULL;
415 rl_startup_hook = old_rl_startup_hook;
420 /* Call this to set the initial text for the next line to read
426 FREE (push_to_readline);
428 push_to_readline = savestring (line);
429 old_rl_startup_hook = rl_startup_hook;
430 rl_startup_hook = bash_push_line;
436 display_shell_version (count, c)
440 show_shell_version (0);
441 putc ('\r', rl_outstream);
442 fflush (rl_outstream);
448 /* **************************************************************** */
452 /* **************************************************************** */
454 /* If the user requests hostname completion, then simply build a list
455 of hosts, and complete from that forever more, or at least until
456 HOSTFILE is unset. */
458 /* THIS SHOULD BE A STRINGLIST. */
459 /* The kept list of hostnames. */
460 static char **hostname_list = (char **)NULL;
462 /* The physical size of the above list. */
463 static int hostname_list_size;
465 /* The number of hostnames in the above list. */
466 static int hostname_list_length;
468 /* Whether or not HOSTNAME_LIST has been initialized. */
469 int hostname_list_initialized = 0;
471 /* Initialize the hostname completion table. */
473 initialize_hostname_list ()
477 temp = get_string_value ("HOSTFILE");
479 temp = get_string_value ("hostname_completion_file");
481 temp = DEFAULT_HOSTS_FILE;
483 snarf_hosts_from_file (temp);
486 hostname_list_initialized++;
489 /* Add NAME to the list of hosts. */
496 if (hostname_list_length + 2 > hostname_list_size)
498 hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32);
499 size = hostname_list_size * sizeof (char *);
500 hostname_list = (char **)xrealloc (hostname_list, size);
503 hostname_list[hostname_list_length++] = savestring (name);
504 hostname_list[hostname_list_length] = (char *)NULL;
507 #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
510 snarf_hosts_from_file (filename)
514 char *temp, buffer[256], name[256];
515 register int i, start;
517 file = fopen (filename, "r");
521 while (temp = fgets (buffer, 255, file))
523 /* Skip to first character. */
524 for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++)
527 /* If comment or blank line, ignore. */
528 if (buffer[i] == '\0' || buffer[i] == '#')
531 /* If `preprocessor' directive, do the include. */
532 if (strncmp (buffer + i, "$include ", 9) == 0)
536 /* Find start of filename. */
537 for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++)
540 /* Find end of filename. */
541 for (t = incfile; *t && cr_whitespace (*t) == 0; t++)
546 snarf_hosts_from_file (incfile);
550 /* Skip internet address if present. */
551 if (DIGIT (buffer[i]))
552 for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++);
554 /* Gobble up names. Each name is separated with whitespace. */
557 for (; cr_whitespace (buffer[i]); i++)
559 if (buffer[i] == '\0' || buffer[i] == '#')
562 /* Isolate the current word. */
563 for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++)
567 strncpy (name, buffer + start, i - start);
568 name[i - start] = '\0';
569 add_host_name (name);
575 /* Return the hostname list. */
579 if (hostname_list_initialized == 0)
580 initialize_hostname_list ();
581 return (hostname_list);
585 clear_hostname_list ()
589 if (hostname_list_initialized == 0)
591 for (i = 0; i < hostname_list_length; i++)
592 free (hostname_list[i]);
593 hostname_list_length = 0;
596 /* Return a NULL terminated list of hostnames which begin with TEXT.
597 Initialize the hostname list the first time if neccessary.
598 The array is malloc ()'ed, but not the individual strings. */
600 hostnames_matching (text)
603 register int i, len, nmatch, rsize;
606 if (hostname_list_initialized == 0)
607 initialize_hostname_list ();
609 if (hostname_list_initialized == 0)
610 return ((char **)NULL);
612 /* Special case. If TEXT consists of nothing, then the whole list is
616 result = alloc_array (1 + hostname_list_length);
617 for (i = 0; i < hostname_list_length; i++)
618 result[i] = hostname_list[i];
619 result[i] = (char *)NULL;
623 /* Scan until found, or failure. */
625 result = (char **)NULL;
626 for (i = nmatch = rsize = 0; i < hostname_list_length; i++)
628 if (STREQN (text, hostname_list[i], len) == 0)
631 /* OK, it matches. Add it to the list. */
632 if (nmatch >= (rsize - 1))
634 rsize = (rsize + 16) - (rsize % 16);
635 result = (char **)xrealloc (result, rsize * sizeof (char *));
638 result[nmatch++] = hostname_list[i];
641 result[nmatch] = (char *)NULL;
645 /* The equivalent of the Korn shell C-o operate-and-get-next-history-line
647 static int saved_history_line_to_use = -1;
652 if (saved_history_line_to_use >= 0)
653 rl_get_previous_history (history_length - saved_history_line_to_use, 0);
654 saved_history_line_to_use = -1;
655 rl_startup_hook = old_rl_startup_hook;
660 operate_and_get_next (count, c)
665 /* Accept the current line. */
668 /* Find the current line, and find the next line to use. */
669 where = where_history ();
671 if ((history_is_stifled () && (history_length >= history_max_entries)) ||
672 (where >= history_length - 1))
673 saved_history_line_to_use = where;
675 saved_history_line_to_use = where + 1;
677 old_rl_startup_hook = rl_startup_hook;
678 rl_startup_hook = set_saved_history;
683 #if defined (VI_MODE)
684 /* This vi mode command causes VI_EDIT_COMMAND to be run on the current
685 command being entered (if no explicit argument is given), otherwise on
686 a command from the history file. */
688 #define VI_EDIT_COMMAND "fc -e ${VISUAL:-${EDITOR:-vi}}"
691 vi_edit_and_execute_command (count, c)
697 rrs = rl_readline_state;
698 cclc = current_command_line_count;
700 /* Accept the current line. */
705 command = (char *)xmalloc (strlen (VI_EDIT_COMMAND) + 8);
706 sprintf (command, "%s %d", VI_EDIT_COMMAND, count);
710 /* Take the command we were just editing, add it to the history file,
711 then call fc to operate on it. We have to add a dummy command to
712 the end of the history because fc ignores the last command (assumes
713 it's supposed to deal with the command before the `fc'). */
715 bash_add_history (rl_line_buffer);
716 bash_add_history ("");
717 history_lines_this_session++;
719 command = savestring (VI_EDIT_COMMAND);
721 r = parse_and_execute (command, "v", SEVAL_NOHIST);
723 current_command_line_count = cclc;
725 /* Now erase the contents of the current line and undo the effects of the
726 rl_accept_line() above. We don't even want to make the text we just
727 executed available for undoing. */
728 rl_line_buffer[0] = '\0'; /* XXX */
729 rl_point = rl_end = 0;
731 rl_readline_state = rrs;
733 rl_forced_update_display ();
741 posix_edit_macros (count, key)
745 char alias_name[3], *alias_value, *macro;
750 alias_name[2] = '\0';
752 alias_value = get_alias_value (alias_name);
753 if (alias_value && *alias_value)
755 macro = savestring (alias_value);
756 rl_push_macro_input (macro);
762 /* **************************************************************** */
764 /* How To Do Shell Completion */
766 /* **************************************************************** */
768 #define COMMAND_SEPARATORS ";|&{(`"
774 register int this_char, prev_char;
776 /* Handle the two character tokens `>&', `<&', and `>|'.
777 We are not in a command position after one of these. */
778 this_char = rl_line_buffer[ti];
779 prev_char = rl_line_buffer[ti - 1];
781 if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) ||
782 (this_char == '|' && prev_char == '>'))
784 else if ((this_char == '{' && prev_char == '$') || /* } */
785 (char_is_quoted (rl_line_buffer, ti)))
790 #if defined (PROGRAMMABLE_COMPLETION)
792 * XXX - because of the <= start test, and setting os = s+1, this can
793 * potentially return os > start. This is probably not what we want to
794 * happen, but fix later after 2.05a-release.
797 find_cmd_start (start)
803 while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS)) <= start) &&
815 e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS);
820 find_cmd_name (start)
826 for (s = start; whitespace (rl_line_buffer[s]); s++)
829 /* skip until a shell break character */
830 e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n");
832 name = substring (rl_line_buffer, s, e);
838 prog_complete_return (text, matchnum)
847 if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0)
849 return (prog_complete_matches[ind++]);
852 #endif /* PROGRAMMABLE_COMPLETION */
854 /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
855 at START and END. Return an array of matches, or NULL if none. */
857 attempt_shell_completion (text, start, end)
861 int in_command_position, ti, saveti, qc;
862 char **matches, *command_separator_chars;
864 command_separator_chars = COMMAND_SEPARATORS;
865 matches = (char **)NULL;
866 rl_ignore_some_completions_function = filename_completion_ignore;
868 /* Determine if this could be a command word. It is if it appears at
869 the start of the line (ignoring preceding whitespace), or if it
870 appears after a character that separates commands. It cannot be a
871 command word if we aren't at the top-level prompt. */
875 while ((ti > -1) && (whitespace (rl_line_buffer[ti])))
879 /* If this is an open quote, maybe we're trying to complete a quoted
881 if (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\'')
883 qc = rl_line_buffer[ti];
885 while (ti > -1 && (whitespace (rl_line_buffer[ti])))
890 in_command_position = 0;
893 /* Only do command completion at the start of a line when we
894 are prompting at the top level. */
895 if (current_prompt_string == ps1_prompt)
896 in_command_position++;
898 else if (member (rl_line_buffer[ti], command_separator_chars))
900 in_command_position++;
902 if (check_redir (ti) == 1)
903 in_command_position = 0;
907 /* This still could be in command position. It is possible
908 that all of the previous words on the line are variable
912 /* Check that we haven't incorrectly flagged a closed command substitution
913 as indicating we're in a command position. */
914 if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' &&
915 *text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0)
916 in_command_position = 0;
918 /* Special handling for command substitution. If *TEXT is a backquote,
919 it can be the start or end of an old-style command substitution, or
920 unmatched. If it's unmatched, both calls to unclosed_pair will
923 (in_command_position || (unclosed_pair (rl_line_buffer, start, "`") &&
924 unclosed_pair (rl_line_buffer, end, "`"))))
925 matches = rl_completion_matches (text, command_subst_completion_function);
927 #if defined (PROGRAMMABLE_COMPLETION)
928 /* Attempt programmable completion. */
929 if (!matches && in_command_position == 0 && prog_completion_enabled &&
930 (num_progcomps () > 0) && current_prompt_string == ps1_prompt)
935 /* XXX - don't free the members */
936 if (prog_complete_matches)
937 free (prog_complete_matches);
938 prog_complete_matches = (char **)NULL;
940 s = find_cmd_start (start);
941 e = find_cmd_end (end);
942 n = find_cmd_name (s);
944 prog_complete_matches = programmable_completions (n, text, s, e, &foundcs);
948 /* XXX - if we found a COMPSPEC for the command, just return whatever
949 the programmable completion code returns, and disable the default
950 filename completion that readline will do unless the COPT_DEFAULT
951 option has been set with the `-o default' option to complete. */
954 /* If the user specified that the compspec returns filenames, make
955 sure that readline knows it. */
956 if (foundcs & COPT_FILENAMES)
957 rl_filename_completion_desired = 1;
958 /* Turn what the programmable completion code returns into what
959 readline wants. I should have made compute_lcd_of_matches
961 matches = rl_completion_matches (text, prog_complete_return);
962 if ((foundcs & COPT_DEFAULT) == 0)
963 rl_attempted_completion_over = 1; /* no default */
969 /* New posix-style command substitution or variable name? */
970 if (!matches && *text == '$')
972 if (qc != '\'' && text[1] == '(') /* ) */
973 matches = rl_completion_matches (text, command_subst_completion_function);
975 matches = rl_completion_matches (text, variable_completion_function);
978 /* If the word starts in `~', and there is no slash in the word, then
979 try completing this word as a username. */
980 if (!matches && *text == '~' && !strchr (text, '/'))
981 matches = rl_completion_matches (text, rl_username_completion_function);
983 /* Another one. Why not? If the word starts in '@', then look through
984 the world of known hostnames for completion first. */
985 if (!matches && perform_hostname_completion && *text == '@')
986 matches = rl_completion_matches (text, hostname_completion_function);
988 /* And last, (but not least) if this word is in a command position, then
989 complete over possible command names, including aliases, functions,
990 and command names. */
991 if (!matches && in_command_position)
993 if (start == 0 && end == 0 && text[0] == '\0' && no_empty_command_completion)
995 matches = (char **)NULL;
996 rl_ignore_some_completions_function = bash_ignore_everything;
1000 matches = rl_completion_matches (text, command_word_completion_function);
1001 /* If we are attempting command completion and nothing matches, we
1002 do not want readline to perform filename completion for us. We
1003 still want to be able to complete partial pathnames, so set the
1004 completion ignore function to something which will remove
1005 filenames and leave directories in the match list. */
1006 if (matches == (char **)NULL)
1007 rl_ignore_some_completions_function = bash_ignore_filenames;
1011 /* This could be a globbing pattern, so try to expand it using pathname
1013 if (!matches && glob_pattern_p (text))
1015 matches = rl_completion_matches (text, glob_complete_word);
1016 /* A glob expression that matches more than one filename is problematic.
1017 If we match more than one filename, punt. */
1018 if (matches && matches[1])
1020 free_array (matches);
1021 matches = (char **)0;
1028 /* This is the function to call when the word to complete is in a position
1029 where a command word can be found. It grovels $PATH, looking for commands
1030 that match. It also scans aliases, function names, and the shell_builtin
1033 command_word_completion_function (hint_text, state)
1034 const char *hint_text;
1037 static char *hint = (char *)NULL;
1038 static char *path = (char *)NULL;
1039 static char *val = (char *)NULL;
1040 static char *filename_hint = (char *)NULL;
1041 static int path_index, hint_len, istate;
1042 static int mapping_over, local_index;
1043 static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
1045 static alias_t **alias_list = (alias_t **)NULL;
1048 /* We have to map over the possibilities for command words. If we have
1049 no state, then make one just for that purpose. */
1058 /* If this is an absolute program name, do not check it against
1059 aliases, reserved words, functions or builtins. We must check
1060 whether or not it is unique, and, if so, whether that filename
1062 if (absolute_program (hint_text))
1064 /* Perform tilde expansion on what's passed, so we don't end up
1065 passing filenames with tildes directly to stat(). */
1066 if (*hint_text == '~')
1067 hint = bash_tilde_expand (hint_text);
1069 hint = savestring (hint_text);
1070 hint_len = strlen (hint);
1073 free (filename_hint);
1074 filename_hint = savestring (hint);
1081 hint = savestring (hint_text);
1082 hint_len = strlen (hint);
1084 path = get_string_value ("PATH");
1087 /* Initialize the variables for each type of command word. */
1093 varlist = all_visible_functions ();
1099 alias_list = all_aliases ();
1103 /* mapping_over says what we are currently hacking. Note that every case
1104 in this list must fall through when there are no more possibilities. */
1106 switch (mapping_over)
1108 case 0: /* Aliases come first. */
1110 while (alias_list && alias_list[local_index])
1112 register char *alias;
1114 alias = alias_list[local_index++]->name;
1116 if (STREQN (alias, hint, hint_len))
1117 return (savestring (alias));
1123 case 1: /* Then shell reserved words. */
1125 while (word_token_alist[local_index].word)
1127 register char *reserved_word;
1129 reserved_word = word_token_alist[local_index++].word;
1131 if (STREQN (reserved_word, hint, hint_len))
1132 return (savestring (reserved_word));
1138 case 2: /* Then function names. */
1139 while (varlist && varlist[local_index])
1141 register char *varname;
1143 varname = varlist[local_index++]->name;
1145 if (STREQN (varname, hint, hint_len))
1146 return (savestring (varname));
1151 case 3: /* Then shell builtins. */
1152 for (; local_index < num_shell_builtins; local_index++)
1154 /* Ignore it if it doesn't have a function pointer or if it
1155 is not currently enabled. */
1156 if (!shell_builtins[local_index].function ||
1157 (shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0)
1160 if (STREQN (shell_builtins[local_index].name, hint, hint_len))
1162 int i = local_index++;
1164 return (savestring (shell_builtins[i].name));
1171 /* Repeatedly call filename_completion_function while we have
1172 members of PATH left. Question: should we stat each file?
1173 Answer: we call executable_file () on each file. */
1176 istate = (val != (char *)NULL);
1182 /* Get the next directory from the path. If there is none, then we
1184 if (!path || !path[path_index] ||
1185 (current_path = extract_colon_unit (path, &path_index)) == 0)
1186 return ((char *)NULL);
1188 if (*current_path == 0)
1190 free (current_path);
1191 current_path = savestring (".");
1194 if (*current_path == '~')
1198 t = bash_tilde_expand (current_path);
1199 free (current_path);
1204 free (filename_hint);
1206 filename_hint = (char *)xmalloc (2 + strlen (current_path) + hint_len);
1207 sprintf (filename_hint, "%s/%s", current_path, hint);
1209 free (current_path);
1213 val = rl_filename_completion_function (filename_hint, istate);
1218 /* If the hint text is an absolute program, then don't bother
1219 searching through PATH. */
1220 if (absolute_program (hint))
1221 return ((char *)NULL);
1227 int match, freetemp;
1230 if (absolute_program (hint))
1232 match = strncmp (val, hint, hint_len) == 0;
1233 /* If we performed tilde expansion, restore the original
1235 if (*hint_text == '~')
1239 tl = strlen (hint_text);
1240 l = vl - hint_len; /* # of chars added */
1241 temp = (char *)xmalloc (l + 2 + tl);
1242 strcpy (temp, hint_text);
1243 strcpy (temp + tl, val + vl - l);
1246 temp = savestring (val);
1251 temp = strrchr (val, '/');
1256 freetemp = match = strncmp (temp, hint, hint_len) == 0;
1258 temp = savestring (temp);
1261 freetemp = match = 0;
1264 /* If we have found a match, and it is an executable file or a
1265 directory name, return it. */
1266 if (match && executable_or_directory (val))
1269 val = ""; /* So it won't be NULL. */
1282 /* Completion inside an unterminated command substitution. */
1284 command_subst_completion_function (text, state)
1288 static char **matches = (char **)NULL;
1289 static const char *orig_start;
1290 static char *filename_text = (char *)NULL;
1291 static int cmd_index, start_len;
1297 free (filename_text);
1301 else if (*text == '$' && text[1] == '(') /* ) */
1303 start_len = text - orig_start;
1304 filename_text = savestring (text);
1307 matches = rl_completion_matches (filename_text, command_word_completion_function);
1311 if (!matches || !matches[cmd_index])
1313 rl_filename_quoting_desired = 0; /* disable quoting */
1314 return ((char *)NULL);
1318 value = (char *)xmalloc (1 + start_len + strlen (matches[cmd_index]));
1321 value[0] = *orig_start;
1323 strncpy (value, orig_start, start_len);
1325 strcpy (value + start_len, matches[cmd_index]);
1332 /* Okay, now we write the entry_function for variable completion. */
1334 variable_completion_function (text, state)
1338 static char **varlist = (char **)NULL;
1339 static int varlist_index;
1340 static char *varname = (char *)NULL;
1342 static int first_char, first_char_loc;
1350 first_char = text[0];
1352 if (first_char == '$')
1355 if (text[first_char_loc] == '{')
1358 varname = savestring (text + first_char_loc);
1360 namelen = strlen (varname);
1362 free_array (varlist);
1364 varlist = all_variables_matching_prefix (varname);
1368 if (!varlist || !varlist[varlist_index])
1370 return ((char *)NULL);
1376 value = (char *)xmalloc (4 + strlen (varlist[varlist_index]));
1380 value[0] = first_char;
1381 if (first_char_loc == 2)
1385 strcpy (value + first_char_loc, varlist[varlist_index]);
1386 if (first_char_loc == 2)
1387 strcat (value, "}");
1394 /* How about a completion function for hostnames? */
1396 hostname_completion_function (text, state)
1400 static char **list = (char **)NULL;
1401 static int list_index = 0;
1402 static int first_char, first_char_loc;
1404 /* If we don't have any state, make some. */
1409 list = (char **)NULL;
1414 if (first_char == '@')
1417 list = hostnames_matching ((char *)text+first_char_loc);
1421 if (list && list[list_index])
1425 t = (char *)xmalloc (2 + strlen (list[list_index]));
1427 strcpy (t + first_char_loc, list[list_index]);
1432 return ((char *)NULL);
1436 bash_groupname_completion_function (text, state)
1440 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
1441 return ((char *)NULL);
1443 static char *gname = (char *)NULL;
1444 static struct group *grent;
1445 static int gnamelen;
1451 gname = savestring (text);
1452 gnamelen = strlen (gname);
1457 while (grent = getgrent ())
1459 if (gnamelen == 0 || (STREQN (gname, grent->gr_name, gnamelen)))
1466 return ((char *)NULL);
1469 value = savestring (grent->gr_name);
1474 /* Functions to perform history and alias expansions on the current line. */
1476 #if defined (BANG_HISTORY)
1477 /* Perform history expansion on the current line. If no history expansion
1478 is done, pre_process_line() returns what it was passed, so we need to
1479 allocate a new line here. */
1481 history_expand_line_internal (line)
1486 new_line = pre_process_line (line, 0, 0);
1487 return (new_line == line) ? savestring (line) : new_line;
1491 /* There was an error in expansion. Let the preprocessor print
1494 cleanup_expansion_error ()
1498 fprintf (rl_outstream, "\r\n");
1499 to_free = pre_process_line (rl_line_buffer, 1, 0);
1500 if (to_free != rl_line_buffer)
1502 putc ('\r', rl_outstream);
1503 rl_forced_update_display ();
1506 /* If NEW_LINE differs from what is in the readline line buffer, add an
1507 undo record to get from the readline line buffer contents to the new
1508 line and make NEW_LINE the current readline line. */
1510 maybe_make_readline_line (new_line)
1513 if (strcmp (new_line, rl_line_buffer) != 0)
1517 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
1518 rl_delete_text (0, rl_point);
1519 rl_point = rl_end = 0;
1520 rl_insert_text (new_line);
1521 rl_add_undo (UNDO_END, 0, 0, 0);
1525 /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
1527 set_up_new_line (new_line)
1530 int old_point, at_end;
1532 old_point = rl_point;
1533 at_end = rl_point == rl_end;
1535 /* If the line was history and alias expanded, then make that
1536 be one thing to undo. */
1537 maybe_make_readline_line (new_line);
1540 /* Place rl_point where we think it should go. */
1543 else if (old_point < rl_end)
1545 rl_point = old_point;
1546 if (!whitespace (rl_line_buffer[rl_point]))
1547 rl_forward_word (1, 0);
1552 /* Expand aliases in the current readline line. */
1554 alias_expand_line (count, ignore)
1559 new_line = alias_expand (rl_line_buffer);
1563 set_up_new_line (new_line);
1568 cleanup_expansion_error ();
1574 #if defined (BANG_HISTORY)
1575 /* History expand the line. */
1577 history_expand_line (count, ignore)
1582 new_line = history_expand_line_internal (rl_line_buffer);
1586 set_up_new_line (new_line);
1591 cleanup_expansion_error ();
1596 /* Expand history substitutions in the current line and then insert a
1597 space (hopefully close to where we were before). */
1599 tcsh_magic_space (count, ignore)
1602 int dist_from_end, old_point;
1604 old_point = rl_point;
1605 dist_from_end = rl_end - rl_point;
1606 if (history_expand_line (count, ignore) == 0)
1608 /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
1609 This works if all expansions were before rl_point or if no expansions
1611 rl_point = (old_point == 0) ? old_point : rl_end - dist_from_end;
1620 /* History and alias expand the line. */
1622 history_and_alias_expand_line (count, ignore)
1627 new_line = pre_process_line (rl_line_buffer, 0, 0);
1628 if (new_line == rl_line_buffer)
1629 new_line = savestring (new_line);
1636 alias_line = alias_expand (new_line);
1638 new_line = alias_line;
1644 set_up_new_line (new_line);
1649 cleanup_expansion_error ();
1654 /* History and alias expand the line, then perform the shell word
1655 expansions by calling expand_string. This can't use set_up_new_line()
1656 because we want the variable expansions as a separate undo'able
1657 set of operations. */
1659 shell_expand_line (count, ignore)
1663 WORD_LIST *expanded_string;
1665 new_line = pre_process_line (rl_line_buffer, 0, 0);
1666 if (new_line == rl_line_buffer)
1667 new_line = savestring (new_line);
1674 alias_line = alias_expand (new_line);
1676 new_line = alias_line;
1682 int old_point = rl_point;
1683 int at_end = rl_point == rl_end;
1685 /* If the line was history and alias expanded, then make that
1686 be one thing to undo. */
1687 maybe_make_readline_line (new_line);
1690 /* If there is variable expansion to perform, do that as a separate
1691 operation to be undone. */
1692 new_line = savestring (rl_line_buffer);
1693 expanded_string = expand_string (new_line, 0);
1695 if (expanded_string == 0)
1697 new_line = (char *)xmalloc (1);
1702 new_line = string_list (expanded_string);
1703 dispose_words (expanded_string);
1706 maybe_make_readline_line (new_line);
1709 /* Place rl_point where we think it should go. */
1712 else if (old_point < rl_end)
1714 rl_point = old_point;
1715 if (!whitespace (rl_line_buffer[rl_point]))
1716 rl_forward_word (1, 0);
1722 cleanup_expansion_error ();
1727 /* Define NO_FORCE_FIGNORE if you want to match filenames that would
1728 otherwise be ignored if they are the only possible matches. */
1729 /* #define NO_FORCE_FIGNORE */
1731 /* If FIGNORE is set, then don't match files with the given suffixes when
1732 completing filenames. If only one of the possibilities has an acceptable
1733 suffix, delete the others, else just return and let the completer
1734 signal an error. It is called by the completer when real
1735 completions are done on filenames by the completer's internal
1736 function, not for completion lists (M-?) and not on "other"
1737 completion types, such as hostnames or commands. */
1739 static struct ignorevar fignore =
1745 (sh_iv_item_func_t *) 0,
1749 _ignore_completion_names (names, name_func)
1751 sh_ignore_func_t *name_func;
1755 #ifdef NO_FORCE_FIGNORE
1760 /* If there is only one completion, see if it is acceptable. If it is
1761 not, free it up. In any case, short-circuit and return. This is a
1762 special case because names[0] is not the prefix of the list of names
1763 if there is only one completion; it is the completion itself. */
1764 if (names[1] == (char *)0)
1766 #ifndef NO_FORCE_FIGNORE
1767 if ((*name_func) (names[0]) == 0)
1770 names[0] = (char *)NULL;
1776 /* Allocate space for array to hold list of pointers to matching
1777 filenames. The pointers are copied back to NAMES when done. */
1778 for (nidx = 1; names[nidx]; nidx++)
1780 newnames = alloc_array (nidx + 1);
1781 #ifdef NO_FORCE_FIGNORE
1782 oldnames = alloc_array (nidx - 1);
1786 newnames[0] = names[0];
1787 for (idx = nidx = 1; names[idx]; idx++)
1789 if ((*name_func) (names[idx]))
1790 newnames[nidx++] = names[idx];
1792 #ifndef NO_FORCE_FIGNORE
1795 oldnames[oidx++] = names[idx];
1799 newnames[nidx] = (char *)NULL;
1801 /* If none are acceptable then let the completer handle it. */
1804 #ifndef NO_FORCE_FIGNORE
1806 names[0] = (char *)NULL;
1814 #ifdef NO_FORCE_FIGNORE
1816 free (oldnames[--oidx]);
1820 /* If only one is acceptable, copy it to names[0] and return. */
1824 names[0] = newnames[1];
1825 names[1] = (char *)NULL;
1830 /* Copy the acceptable names back to NAMES, set the new array end,
1832 for (nidx = 1; newnames[nidx]; nidx++)
1833 names[nidx] = newnames[nidx];
1834 names[nidx] = (char *)NULL;
1839 name_is_acceptable (name)
1845 for (nlen = strlen (name), p = fignore.ignores; p->val; p++)
1847 if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len]))
1856 ignore_dot_names (name)
1859 return (name[0] != '.');
1864 filename_completion_ignore (names)
1868 if (glob_dot_filenames == 0)
1869 _ignore_completion_names (names, ignore_dot_names);
1872 setup_ignore_patterns (&fignore);
1874 if (fignore.num_ignores == 0)
1877 _ignore_completion_names (names, name_is_acceptable);
1882 /* Return 1 if NAME is a directory. */
1884 test_for_directory (name)
1890 fn = bash_tilde_expand (name);
1891 if (stat (fn, &finfo) != 0)
1897 return (S_ISDIR (finfo.st_mode));
1900 /* Remove files from NAMES, leaving directories. */
1902 bash_ignore_filenames (names)
1905 _ignore_completion_names (names, test_for_directory);
1917 bash_ignore_everything (names)
1920 _ignore_completion_names (names, return_zero);
1924 /* Handle symbolic link references and other directory name
1925 expansions while hacking completion. */
1927 bash_directory_completion_hook (dirname)
1930 char *local_dirname, *new_dirname, *t;
1931 int return_value, should_expand_dirname;
1934 return_value = should_expand_dirname = 0;
1935 local_dirname = *dirname;
1938 should_expand_dirname = strchr (local_dirname, '$') || strchr (local_dirname, '`');
1940 if (strchr (local_dirname, '$'))
1941 should_expand_dirname = 1;
1944 t = strchr (local_dirname, '`');
1945 if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0)
1946 should_expand_dirname = 1;
1950 if (should_expand_dirname)
1952 new_dirname = savestring (local_dirname);
1953 wl = expand_string (new_dirname, 0);
1956 *dirname = string_list (wl);
1957 /* Tell the completer to replace the directory name only if we
1958 actually expanded something. */
1959 return_value = STREQ (local_dirname, *dirname) == 0;
1960 free (local_dirname);
1963 local_dirname = *dirname;
1968 free (local_dirname);
1969 *dirname = (char *)xmalloc (1);
1975 if (!no_symbolic_links && (local_dirname[0] != '.' || local_dirname[1]))
1977 char *temp1, *temp2;
1980 t = get_working_directory ("symlink-hook");
1981 temp1 = make_absolute (local_dirname, t);
1983 temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
1984 /* If we can't canonicalize, bail. */
1990 len1 = strlen (temp1);
1991 if (temp1[len1 - 1] == '/')
1993 len2 = strlen (temp2);
1994 temp2 = (char *)xrealloc (temp2, len2 + 2);
1996 temp2[len2 + 1] = '\0';
1998 free (local_dirname);
2002 return (return_value);
2005 static char **history_completion_array = (char **)NULL;
2006 static int harry_size;
2007 static int harry_len;
2010 build_history_completion_array ()
2016 /* First, clear out the current dynamic history completion list. */
2019 for (i = 0; history_completion_array[i]; i++)
2020 free (history_completion_array[i]);
2022 free (history_completion_array);
2024 history_completion_array = (char **)NULL;
2029 /* Next, grovel each line of history, making each shell-sized token
2030 a separate entry in the history_completion_array. */
2031 hlist = history_list ();
2035 for (i = 0; hlist[i]; i++)
2037 /* Separate each token, and place into an array. */
2038 tokens = history_tokenize (hlist[i]->line);
2040 for (j = 0; tokens && tokens[j]; j++)
2042 if (harry_len + 2 > harry_size)
2045 history_completion_array = (char **)xrealloc
2046 (history_completion_array, harry_size * sizeof (char *));
2049 history_completion_array[harry_len++] = tokens[j];
2050 history_completion_array[harry_len] = (char *)NULL;
2055 /* Sort the complete list of tokens. */
2056 qsort (history_completion_array, harry_len, sizeof (char *), (QSFUNC *)qsort_string_compare);
2061 history_completion_generator (hint_text, state)
2062 const char *hint_text;
2065 static int local_index, len;
2066 static const char *text;
2068 /* If this is the first call to the generator, then initialize the
2069 list of strings to complete over. */
2073 build_history_completion_array ();
2075 len = strlen (text);
2078 while (history_completion_array && history_completion_array[local_index])
2080 if (strncmp (text, history_completion_array[local_index++], len) == 0)
2081 return (savestring (history_completion_array[local_index - 1]));
2083 return ((char *)NULL);
2087 dynamic_complete_history (count, key)
2092 rl_compentry_func_t *orig_func;
2093 rl_completion_func_t *orig_attempt_func;
2095 orig_func = rl_completion_entry_function;
2096 orig_attempt_func = rl_attempted_completion_function;
2097 rl_completion_entry_function = history_completion_generator;
2098 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
2100 if (rl_last_func == dynamic_complete_history)
2101 r = rl_complete_internal ('?');
2103 r = rl_complete_internal (TAB);
2105 rl_completion_entry_function = orig_func;
2106 rl_attempted_completion_function = orig_attempt_func;
2110 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
2112 bash_complete_username (ignore, ignore2)
2113 int ignore, ignore2;
2115 return bash_complete_username_internal (TAB);
2119 bash_possible_username_completions (ignore, ignore2)
2120 int ignore, ignore2;
2122 return bash_complete_username_internal ('?');
2126 bash_complete_username_internal (what_to_do)
2129 return bash_specific_completion (what_to_do, rl_username_completion_function);
2133 bash_complete_filename (ignore, ignore2)
2134 int ignore, ignore2;
2136 return bash_complete_filename_internal (TAB);
2140 bash_possible_filename_completions (ignore, ignore2)
2141 int ignore, ignore2;
2143 return bash_complete_filename_internal ('?');
2147 bash_complete_filename_internal (what_to_do)
2150 rl_compentry_func_t *orig_func;
2151 rl_completion_func_t *orig_attempt_func;
2152 rl_icppfunc_t *orig_dir_func;
2153 const char *orig_rl_completer_word_break_characters;
2156 orig_func = rl_completion_entry_function;
2157 orig_attempt_func = rl_attempted_completion_function;
2158 orig_dir_func = rl_directory_completion_hook;
2159 orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
2160 rl_completion_entry_function = rl_filename_completion_function;
2161 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
2162 rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
2163 rl_completer_word_break_characters = " \t\n\"\'";
2165 r = rl_complete_internal (what_to_do);
2167 rl_completion_entry_function = orig_func;
2168 rl_attempted_completion_function = orig_attempt_func;
2169 rl_directory_completion_hook = orig_dir_func;
2170 rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
2176 bash_complete_hostname (ignore, ignore2)
2177 int ignore, ignore2;
2179 return bash_complete_hostname_internal (TAB);
2183 bash_possible_hostname_completions (ignore, ignore2)
2184 int ignore, ignore2;
2186 return bash_complete_hostname_internal ('?');
2190 bash_complete_variable (ignore, ignore2)
2191 int ignore, ignore2;
2193 return bash_complete_variable_internal (TAB);
2197 bash_possible_variable_completions (ignore, ignore2)
2198 int ignore, ignore2;
2200 return bash_complete_variable_internal ('?');
2204 bash_complete_command (ignore, ignore2)
2205 int ignore, ignore2;
2207 return bash_complete_command_internal (TAB);
2211 bash_possible_command_completions (ignore, ignore2)
2212 int ignore, ignore2;
2214 return bash_complete_command_internal ('?');
2218 bash_complete_hostname_internal (what_to_do)
2221 return bash_specific_completion (what_to_do, hostname_completion_function);
2225 bash_complete_variable_internal (what_to_do)
2228 return bash_specific_completion (what_to_do, variable_completion_function);
2232 bash_complete_command_internal (what_to_do)
2235 return bash_specific_completion (what_to_do, command_word_completion_function);
2239 glob_complete_word (text, state)
2243 static char **matches = (char **)NULL;
2249 rl_filename_completion_desired = 1;
2252 matches = shell_glob_filename (text);
2253 if (GLOB_FAILED (matches))
2254 matches = (char **)NULL;
2258 ret = matches ? matches[ind] : (char *)NULL;
2264 bash_glob_completion_internal (what_to_do)
2267 return bash_specific_completion (what_to_do, glob_complete_word);
2271 bash_glob_expand_word (count, key)
2274 return bash_glob_completion_internal ('*');
2278 bash_glob_list_expansions (count, key)
2281 return bash_glob_completion_internal ('?');
2285 bash_specific_completion (what_to_do, generator)
2287 rl_compentry_func_t *generator;
2289 rl_compentry_func_t *orig_func;
2290 rl_completion_func_t *orig_attempt_func;
2293 orig_func = rl_completion_entry_function;
2294 orig_attempt_func = rl_attempted_completion_function;
2295 rl_completion_entry_function = generator;
2296 rl_attempted_completion_function = NULL;
2298 r = rl_complete_internal (what_to_do);
2300 rl_completion_entry_function = orig_func;
2301 rl_attempted_completion_function = orig_attempt_func;
2306 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
2308 /* Filename quoting for completion. */
2309 /* A function to strip unquoted quote characters (single quotes, double
2310 quotes, and backslashes). It allows single quotes to appear
2311 within double quotes, and vice versa. It should be smarter. */
2313 bash_dequote_filename (text, quote_char)
2321 ret = (char *)xmalloc (l + 1);
2322 for (quoted = quote_char, p = text, r = ret; p && *p; p++)
2324 /* Allow backslash-quoted characters to pass through unscathed. */
2333 if (quoted && *p == quoted)
2339 if (quoted == 0 && (*p == '\'' || *p == '"'))
2350 /* Quote characters that the readline completion code would treat as
2351 word break characters with backslashes. Pass backslash-quoted
2352 characters through without examination. */
2354 quote_word_break_chars (text)
2361 ret = (char *)xmalloc ((2 * l) + 1);
2362 for (s = text, r = ret; *s; s++)
2364 /* Pass backslash-quoted characters through, including the backslash. */
2373 /* OK, we have an unquoted character. Check its presence in
2374 rl_completer_word_break_characters. */
2375 if (strchr (rl_completer_word_break_characters, *s))
2383 /* Quote a filename using double quotes, single quotes, or backslashes
2384 depending on the value of completion_quoting_style. If we're
2385 completing using backslashes, we need to quote some additional
2386 characters (those that readline treats as word breaks), so we call
2387 quote_word_break_chars on the result. */
2389 bash_quote_filename (s, rtype, qcp)
2394 char *rtext, *mtext, *ret;
2397 rtext = (char *)NULL;
2399 /* If RTYPE == MULT_MATCH, it means that there is
2400 more than one match. In this case, we do not add
2401 the closing quote or attempt to perform tilde
2402 expansion. If RTYPE == SINGLE_MATCH, we try
2403 to perform tilde expansion, because single and double
2404 quotes inhibit tilde expansion by the shell. */
2407 if (mtext[0] == '~' && rtype == SINGLE_MATCH)
2408 mtext = bash_tilde_expand (s);
2410 cs = completion_quoting_style;
2411 /* Might need to modify the default completion style based on *qcp,
2412 since it's set to any user-provided opening quote. We also change
2413 to single-quoting if there is no user-provided opening quote and
2414 the word being completed contains newlines, since those are not
2415 quoted correctly using backslashes (a backslash-newline pair is
2416 special to the shell parser). */
2417 if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && strchr (mtext, '\n'))
2418 cs = COMPLETE_SQUOTE;
2419 else if (*qcp == '"')
2420 cs = COMPLETE_DQUOTE;
2421 else if (*qcp == '\'')
2422 cs = COMPLETE_SQUOTE;
2423 #if defined (BANG_HISTORY)
2424 else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE &&
2425 history_expansion_inhibited == 0 && strchr (mtext, '!'))
2426 cs = COMPLETE_BSQUOTE;
2428 if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE &&
2429 history_expansion_inhibited == 0 && strchr (mtext, '!'))
2431 cs = COMPLETE_BSQUOTE;
2438 case COMPLETE_DQUOTE:
2439 rtext = sh_double_quote (mtext);
2441 case COMPLETE_SQUOTE:
2442 rtext = sh_single_quote (mtext);
2444 case COMPLETE_BSQUOTE:
2445 rtext = sh_backslash_quote (mtext);
2452 /* We may need to quote additional characters: those that readline treats
2453 as word breaks that are not quoted by backslash_quote. */
2454 if (rtext && cs == COMPLETE_BSQUOTE)
2456 mtext = quote_word_break_chars (rtext);
2461 /* Leave the opening quote intact. The readline completion code takes
2462 care of avoiding doubled opening quotes. */
2463 rlen = strlen (rtext);
2464 ret = (char *)xmalloc (rlen + 1);
2465 strcpy (ret, rtext);
2467 /* If there are multiple matches, cut off the closing quote. */
2468 if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE)
2469 ret[rlen - 1] = '\0';
2474 /* Support for binding readline key sequences to Unix commands. */
2475 static Keymap cmd_xmap;
2478 bash_execute_unix_command (count, key)
2479 int count; /* ignored */
2482 Keymap ckmap; /* current keymap */
2483 Keymap xkmap; /* unix command executing keymap */
2487 /* First, we need to find the right command to execute. This is tricky,
2488 because we might have already indirected into another keymap. */
2489 ckmap = rl_get_keymap ();
2490 if (ckmap != rl_executing_keymap)
2492 /* bogus. we have to search. only handle one level of indirection. */
2493 for (i = 0; i < KEYMAP_SIZE; i++)
2495 if (ckmap[i].type == ISKMAP && (Keymap)ckmap[i].function == rl_executing_keymap)
2498 if (i < KEYMAP_SIZE)
2499 xkmap = (Keymap)cmd_xmap[i].function;
2503 internal_error ("bash_execute_unix_command: cannot find keymap for command");
2504 rl_forced_update_display ();
2511 cmd = (char *)xkmap[key].function;
2519 rl_crlf (); /* move to a new line */
2521 cmd = savestring (cmd);
2522 parse_and_execute (cmd, "bash_execute_unix_command", 0);
2524 /* and restore the readline buffer and display after command execution. */
2525 rl_forced_update_display ();
2530 init_unix_command_map ()
2532 cmd_xmap = rl_make_bare_keymap ();
2536 isolate_sequence (string, ind, need_dquote, startp)
2538 int ind, need_dquote, *startp;
2541 int c, passc, delim;
2543 for (i = ind; string[i] && whitespace (string[i]); i++)
2545 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
2546 if (need_dquote && string[i] != '"')
2548 builtin_error ("%s: first non-whitespace character is not `\"'", string);
2552 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
2553 string to bind the key sequence to. */
2554 delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0;
2557 *startp = delim ? ++i : i;
2559 for (passc = 0; c = string[i]; i++)
2575 if (delim && string[i] != delim)
2577 builtin_error ("%s: no closing `%c'", string, delim);
2585 bind_keyseq_to_unix_command (line)
2593 init_unix_command_map ();
2595 kmap = rl_get_keymap ();
2597 /* We duplicate some of the work done by rl_parse_and_bind here, but
2598 this code only has to handle `"keyseq": ["]command["]' and can
2599 generate an error for anything else. */
2600 i = isolate_sequence (line, 0, 1, &kstart);
2604 /* Create the key sequence string to pass to rl_generic_bind */
2605 kseq = substring (line, kstart, i);
2607 for ( ; line[i] && line[i] != ':'; i++)
2611 builtin_error ("%s: missing colon separator", line);
2615 i = isolate_sequence (line, i + 1, 0, &kstart);
2619 /* Create the value string containing the command to execute. */
2620 value = substring (line, kstart, i);
2622 /* Save the command to execute and the key sequence in the CMD_XMAP */
2623 rl_generic_bind (ISMACR, kseq, value, cmd_xmap);
2625 /* and bind the key sequence in the current keymap to a function that
2626 understands how to execute from CMD_XMAP */
2627 rl_set_key (kseq, bash_execute_unix_command, kmap);
2632 /* Used by the programmable completion code. Complete TEXT as a filename,
2633 but return only directories as matches. Dequotes the filename before
2634 attempting to find matches. */
2636 bash_directory_completion_matches (text)
2643 qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
2644 dfn = bash_dequote_filename ((char *)text, qc);
2645 m1 = rl_completion_matches (dfn, rl_filename_completion_function);
2648 if (m1 == 0 || m1[0] == 0)
2650 /* We don't bother recomputing the lcd of the matches, because it will just
2651 get thrown away by the programmable completion code and recomputed
2653 (void)bash_ignore_filenames (m1);
2656 #endif /* READLINE */