blob: 854195a0fd96c6929dc9bd507e77cb6cfa1c62d5 [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* bashline.c -- Bash's interface to the readline library. */
2
Jari Aaltob80f6442004-07-27 13:29:18 +00003/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
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
Jari Aaltobb706242000-03-17 21:46:59 +00009 the Free Software Foundation; either version 2, or (at your option)
Jari Aalto726f6381996-08-26 18:22:31 +000010 any later version.
11
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.
16
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
Jari Aaltobb706242000-03-17 21:46:59 +000019 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
Jari Aalto726f6381996-08-26 18:22:31 +000020
Jari Aaltoccc6cda1996-12-23 17:02:34 +000021#include "config.h"
22
23#if defined (READLINE)
24
Jari Aalto726f6381996-08-26 18:22:31 +000025#include "bashtypes.h"
26#include "posixstat.h"
27
Jari Aaltoccc6cda1996-12-23 17:02:34 +000028#if defined (HAVE_UNISTD_H)
29# include <unistd.h>
30#endif
31
Jari Aaltof73dda02001-11-13 17:56:06 +000032#if defined (HAVE_GRP_H)
33# include <grp.h>
34#endif
35
Jari Aalto7117c2d2002-07-17 14:10:11 +000036#if defined (HAVE_NETDB_H)
37# include <netdb.h>
38#endif
39
Jari Aalto726f6381996-08-26 18:22:31 +000040#include <stdio.h>
Jari Aaltof73dda02001-11-13 17:56:06 +000041#include "chartypes.h"
Jari Aalto726f6381996-08-26 18:22:31 +000042#include "bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000043#include "bashintl.h"
44
Jari Aaltoccc6cda1996-12-23 17:02:34 +000045#include "shell.h"
Jari Aalto7117c2d2002-07-17 14:10:11 +000046#include "input.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000047#include "builtins.h"
48#include "bashhist.h"
49#include "bashline.h"
50#include "execute_cmd.h"
Jari Aaltocce855b1998-04-17 19:52:44 +000051#include "findcmd.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000052#include "pathexp.h"
53#include "builtins/common.h"
Jari Aalto726f6381996-08-26 18:22:31 +000054#include <readline/rlconf.h>
55#include <readline/readline.h>
56#include <readline/history.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000057
58#include <glob/glob.h>
Jari Aalto726f6381996-08-26 18:22:31 +000059
60#if defined (ALIAS)
61# include "alias.h"
62#endif
63
Jari Aaltobb706242000-03-17 21:46:59 +000064#if defined (PROGRAMMABLE_COMPLETION)
65# include "pcomplete.h"
66#endif
67
Jari Aalto7117c2d2002-07-17 14:10:11 +000068/* These should agree with the defines for emacs_mode and vi_mode in
69 rldefs.h, even though that's not a public readline header file. */
70#ifndef EMACS_EDITING_MODE
71# define NO_EDITING_MODE -1
72# define EMACS_EDITING_MODE 1
73# define VI_EDITING_MODE 0
74#endif
75
Jari Aalto726f6381996-08-26 18:22:31 +000076#if defined (BRACE_COMPLETION)
Jari Aalto28ef6c32001-04-06 19:14:31 +000077extern int bash_brace_completion __P((int, int));
Jari Aalto726f6381996-08-26 18:22:31 +000078#endif /* BRACE_COMPLETION */
79
Jari Aalto28ef6c32001-04-06 19:14:31 +000080/* Forward declarations */
81
Jari Aalto726f6381996-08-26 18:22:31 +000082/* Functions bound to keys in Readline for Bash users. */
Jari Aalto28ef6c32001-04-06 19:14:31 +000083static int shell_expand_line __P((int, int));
84static int display_shell_version __P((int, int));
85static int operate_and_get_next __P((int, int));
86
87static int bash_ignore_filenames __P((char **));
88static int bash_ignore_everything __P((char **));
89
Jari Aaltocce855b1998-04-17 19:52:44 +000090#if defined (BANG_HISTORY)
Jari Aaltof73dda02001-11-13 17:56:06 +000091static char *history_expand_line_internal __P((char *));
Jari Aalto28ef6c32001-04-06 19:14:31 +000092static int history_expand_line __P((int, int));
93static int tcsh_magic_space __P((int, int));
Jari Aaltocce855b1998-04-17 19:52:44 +000094#endif /* BANG_HISTORY */
95#ifdef ALIAS
Jari Aalto28ef6c32001-04-06 19:14:31 +000096static int alias_expand_line __P((int, int));
Jari Aaltocce855b1998-04-17 19:52:44 +000097#endif
98#if defined (BANG_HISTORY) && defined (ALIAS)
Jari Aalto28ef6c32001-04-06 19:14:31 +000099static int history_and_alias_expand_line __P((int, int));
Jari Aaltocce855b1998-04-17 19:52:44 +0000100#endif
101
Jari Aalto726f6381996-08-26 18:22:31 +0000102/* Helper functions for Readline. */
Jari Aaltoeb873672004-11-09 21:37:25 +0000103static int bash_directory_expansion __P((char **));
Jari Aaltof73dda02001-11-13 17:56:06 +0000104static int bash_directory_completion_hook __P((char **));
105static int filename_completion_ignore __P((char **));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000106static int bash_push_line __P((void));
Jari Aalto726f6381996-08-26 18:22:31 +0000107
Jari Aaltof73dda02001-11-13 17:56:06 +0000108static void cleanup_expansion_error __P((void));
109static void maybe_make_readline_line __P((char *));
110static void set_up_new_line __P((char *));
111
112static int check_redir __P((int));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000113static char **attempt_shell_completion __P((const char *, int, int));
114static char *variable_completion_function __P((const char *, int));
115static char *hostname_completion_function __P((const char *, int));
116static char *command_subst_completion_function __P((const char *, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000117
Jari Aaltof73dda02001-11-13 17:56:06 +0000118static void build_history_completion_array __P((void));
119static char *history_completion_generator __P((const char *, int));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000120static int dynamic_complete_history __P((int, int));
Jari Aalto726f6381996-08-26 18:22:31 +0000121
Jari Aaltof73dda02001-11-13 17:56:06 +0000122static void initialize_hostname_list __P((void));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000123static void add_host_name __P((char *));
Jari Aaltof73dda02001-11-13 17:56:06 +0000124static void snarf_hosts_from_file __P((char *));
125static char **hostnames_matching __P((char *));
126
127static void _ignore_completion_names __P((char **, sh_ignore_func_t *));
128static int name_is_acceptable __P((const char *));
129static int test_for_directory __P((const char *));
130static int return_zero __P((const char *));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000131
132static char *bash_dequote_filename __P((char *, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000133static char *quote_word_break_chars __P((char *));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000134static char *bash_quote_filename __P((char *, int, char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000135
Jari Aaltof73dda02001-11-13 17:56:06 +0000136static int bash_execute_unix_command __P((int, int));
137static void init_unix_command_map __P((void));
138static int isolate_sequence __P((char *, int, int, int *));
139
140static int set_saved_history __P((void));
141
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000142#if defined (ALIAS)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000143static int posix_edit_macros __P((int, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000144#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000145
Jari Aaltobb706242000-03-17 21:46:59 +0000146#if defined (PROGRAMMABLE_COMPLETION)
Jari Aaltof73dda02001-11-13 17:56:06 +0000147static int find_cmd_start __P((int));
148static int find_cmd_end __P((int));
149static char *find_cmd_name __P((int));
150static char *prog_complete_return __P((const char *, int));
151
Jari Aaltobb706242000-03-17 21:46:59 +0000152static char **prog_complete_matches;
Jari Aaltobb706242000-03-17 21:46:59 +0000153#endif
154
Jari Aalto726f6381996-08-26 18:22:31 +0000155/* Variables used here but defined in other files. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000156#if defined (BANG_HISTORY)
157extern int hist_verify;
158#endif
159
160extern int current_command_line_count, last_command_exit_value;
Jari Aalto726f6381996-08-26 18:22:31 +0000161extern int posixly_correct, no_symbolic_links;
Jari Aalto726f6381996-08-26 18:22:31 +0000162extern char *current_prompt_string, *ps1_prompt;
163extern STRING_INT_ALIST word_token_alist[];
Jari Aaltob80f6442004-07-27 13:29:18 +0000164extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
Jari Aalto726f6381996-08-26 18:22:31 +0000165
166/* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
167 completion functions which indicate what type of completion should be
168 done (at or before point) that can be bound to key sequences with
169 the readline library. */
170#define SPECIFIC_COMPLETION_FUNCTIONS
171
172#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000173static int bash_specific_completion __P((int, rl_compentry_func_t *));
174
175static int bash_complete_filename_internal __P((int));
176static int bash_complete_username_internal __P((int));
177static int bash_complete_hostname_internal __P((int));
178static int bash_complete_variable_internal __P((int));
179static int bash_complete_command_internal __P((int));
180
181static int bash_complete_filename __P((int, int));
182static int bash_possible_filename_completions __P((int, int));
183static int bash_complete_username __P((int, int));
184static int bash_possible_username_completions __P((int, int));
185static int bash_complete_hostname __P((int, int));
186static int bash_possible_hostname_completions __P((int, int));
187static int bash_complete_variable __P((int, int));
188static int bash_possible_variable_completions __P((int, int));
189static int bash_complete_command __P((int, int));
190static int bash_possible_command_completions __P((int, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000191
192static char *glob_complete_word __P((const char *, int));
193static int bash_glob_completion_internal __P((int));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000194static int bash_glob_complete_word __P((int, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000195static int bash_glob_expand_word __P((int, int));
196static int bash_glob_list_expansions __P((int, int));
Jari Aaltob80f6442004-07-27 13:29:18 +0000197
Jari Aalto726f6381996-08-26 18:22:31 +0000198#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
199
Jari Aalto7117c2d2002-07-17 14:10:11 +0000200static int edit_and_execute_command __P((int, int, int, char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000201#if defined (VI_MODE)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000202static int vi_edit_and_execute_command __P((int, int));
Jari Aaltob80f6442004-07-27 13:29:18 +0000203static int bash_vi_complete __P((int, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000204#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +0000205static int emacs_edit_and_execute_command __P((int, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000206
Jari Aalto726f6381996-08-26 18:22:31 +0000207/* Non-zero once initalize_readline () has been called. */
208int bash_readline_initialized = 0;
209
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000210/* If non-zero, we do hostname completion, breaking words at `@' and
211 trying to complete the stuff after the `@' from our own internal
212 host list. */
213int perform_hostname_completion = 1;
214
Jari Aaltobb706242000-03-17 21:46:59 +0000215/* If non-zero, we don't do command completion on an empty line. */
216int no_empty_command_completion;
217
Jari Aaltob80f6442004-07-27 13:29:18 +0000218/* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the
219 only possible matches. Set to 0 if you want to match filenames if they
220 are the only possible matches, even if FIGNORE says to. */
221int force_fignore = 1;
222
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000223static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:";
224static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:";
Jari Aaltob80f6442004-07-27 13:29:18 +0000225/* )) */
Jari Aalto726f6381996-08-26 18:22:31 +0000226
Jari Aalto28ef6c32001-04-06 19:14:31 +0000227static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000228
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000229/* What kind of quoting is performed by bash_quote_filename:
230 COMPLETE_DQUOTE = double-quoting the filename
231 COMPLETE_SQUOTE = single_quoting the filename
232 COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
233*/
234#define COMPLETE_DQUOTE 1
235#define COMPLETE_SQUOTE 2
236#define COMPLETE_BSQUOTE 3
237static int completion_quoting_style = COMPLETE_BSQUOTE;
238
Jari Aalto726f6381996-08-26 18:22:31 +0000239/* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
240 Called when the shell is put into or out of `posix' mode. */
241void
242posix_readline_initialize (on_or_off)
243 int on_or_off;
244{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000245 if (on_or_off)
246 rl_variable_bind ("comment-begin", "#");
Jari Aalto726f6381996-08-26 18:22:31 +0000247#if defined (VI_MODE)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000248 rl_bind_key_in_map (CTRL ('I'), on_or_off ? rl_insert : rl_complete, vi_insertion_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000249#endif
250}
251
Jari Aaltob80f6442004-07-27 13:29:18 +0000252/* When this function returns, rl_completer_word_break_characters points to
253 dynamically allocated memory. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000254int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000255enable_hostname_completion (on_or_off)
256 int on_or_off;
257{
Jari Aaltof73dda02001-11-13 17:56:06 +0000258 int old_value;
Jari Aaltob80f6442004-07-27 13:29:18 +0000259 char *at, *nv, *nval;
Jari Aaltof73dda02001-11-13 17:56:06 +0000260
261 old_value = perform_hostname_completion;
262
Jari Aalto726f6381996-08-26 18:22:31 +0000263 if (on_or_off)
264 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000265 perform_hostname_completion = 1;
266 rl_special_prefixes = "$@";
Jari Aalto726f6381996-08-26 18:22:31 +0000267 }
268 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000269 {
270 perform_hostname_completion = 0;
271 rl_special_prefixes = "$";
Jari Aaltob80f6442004-07-27 13:29:18 +0000272 }
273
274 /* Now we need to figure out how to appropriately modify and assign
275 rl_completer_word_break_characters depending on whether we want
276 hostname completion on or off. */
277
278 /* If this is the first time this has been called
279 (bash_readline_initialized == 0), use the sames values as before, but
280 allocate new memory for rl_completer_word_break_characters. */
281
282 if (bash_readline_initialized == 0 &&
283 (rl_completer_word_break_characters == 0 ||
284 rl_completer_word_break_characters == rl_basic_word_break_characters))
285 {
286 if (on_or_off)
287 rl_completer_word_break_characters = savestring (bash_completer_word_break_characters);
288 else
289 rl_completer_word_break_characters = savestring (bash_nohostname_word_break_characters);
290 }
291 else
292 {
293 /* See if we have anything to do. */
294 at = strchr (rl_completer_word_break_characters, '@');
295 if ((at == 0 && on_or_off == 0) || (at != 0 && on_or_off != 0))
Jari Aaltoeb873672004-11-09 21:37:25 +0000296 return old_value;
Jari Aaltob80f6442004-07-27 13:29:18 +0000297
298 /* We have something to do. Do it. */
299 nval = (char *)xmalloc (strlen (rl_completer_word_break_characters) + 1 + on_or_off);
300
301 if (on_or_off == 0)
302 {
303 /* Turn it off -- just remove `@' from word break chars. We want
304 to remove all occurrences of `@' from the char list, so we loop
305 rather than just copy the rest of the list over AT. */
306 for (nv = nval, at = rl_completer_word_break_characters; *at; )
307 if (*at != '@')
308 *nv++ = *at++;
309 else
310 at++;
311 *nv = '\0';
312 }
313 else
314 {
315 nval[0] = '@';
316 strcpy (nval + 1, rl_completer_word_break_characters);
317 }
318
319 free (rl_completer_word_break_characters);
320 rl_completer_word_break_characters = nval;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000321 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000322
323 return (old_value);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000324}
Jari Aalto726f6381996-08-26 18:22:31 +0000325
326/* Called once from parse.y if we are going to use readline. */
327void
328initialize_readline ()
329{
Jari Aaltob80f6442004-07-27 13:29:18 +0000330 rl_command_func_t *func;
331 char kseq[2];
332
Jari Aalto726f6381996-08-26 18:22:31 +0000333 if (bash_readline_initialized)
334 return;
335
336 rl_terminal_name = get_string_value ("TERM");
337 rl_instream = stdin;
338 rl_outstream = stderr;
Jari Aalto726f6381996-08-26 18:22:31 +0000339
340 /* Allow conditional parsing of the ~/.inputrc file. */
341 rl_readline_name = "Bash";
342
Jari Aalto28ef6c32001-04-06 19:14:31 +0000343 /* Add bindable names before calling rl_initialize so they may be
344 referenced in the various inputrc files. */
345 rl_add_defun ("shell-expand-line", shell_expand_line, -1);
Jari Aaltocce855b1998-04-17 19:52:44 +0000346#ifdef BANG_HISTORY
Jari Aalto28ef6c32001-04-06 19:14:31 +0000347 rl_add_defun ("history-expand-line", history_expand_line, -1);
348 rl_add_defun ("magic-space", tcsh_magic_space, -1);
Jari Aaltocce855b1998-04-17 19:52:44 +0000349#endif
350
Jari Aaltod166f041997-06-05 14:59:13 +0000351#ifdef ALIAS
Jari Aalto28ef6c32001-04-06 19:14:31 +0000352 rl_add_defun ("alias-expand-line", alias_expand_line, -1);
Jari Aaltobc4cd231998-07-23 14:37:54 +0000353# ifdef BANG_HISTORY
Jari Aalto28ef6c32001-04-06 19:14:31 +0000354 rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line, -1);
Jari Aaltobc4cd231998-07-23 14:37:54 +0000355# endif
Jari Aaltod166f041997-06-05 14:59:13 +0000356#endif
357
Jari Aalto726f6381996-08-26 18:22:31 +0000358 /* Backwards compatibility. */
359 rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1);
360
Jari Aalto28ef6c32001-04-06 19:14:31 +0000361 rl_add_defun ("operate-and-get-next", operate_and_get_next, -1);
362 rl_add_defun ("display-shell-version", display_shell_version, -1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000363 rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command, -1);
Jari Aalto726f6381996-08-26 18:22:31 +0000364
Jari Aalto28ef6c32001-04-06 19:14:31 +0000365#if defined (BRACE_COMPLETION)
366 rl_add_defun ("complete-into-braces", bash_brace_completion, -1);
367#endif
368
369#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
370 rl_add_defun ("complete-filename", bash_complete_filename, -1);
371 rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1);
372 rl_add_defun ("complete-username", bash_complete_username, -1);
373 rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1);
374 rl_add_defun ("complete-hostname", bash_complete_hostname, -1);
375 rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1);
376 rl_add_defun ("complete-variable", bash_complete_variable, -1);
377 rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1);
378 rl_add_defun ("complete-command", bash_complete_command, -1);
379 rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000380 rl_add_defun ("glob-complete-word", bash_glob_complete_word, -1);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000381 rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1);
382 rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1);
383#endif
384
385 rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1);
386
387 /* Bind defaults before binding our custom shell keybindings. */
388 if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0)
389 rl_initialize ();
390
391 /* Bind up our special shell functions. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000392 rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line, emacs_meta_keymap);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000393
Jari Aalto28ef6c32001-04-06 19:14:31 +0000394#ifdef BANG_HISTORY
Jari Aaltob80f6442004-07-27 13:29:18 +0000395 rl_bind_key_if_unbound_in_map ('^', history_expand_line, emacs_meta_keymap);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000396#endif
397
Jari Aaltob80f6442004-07-27 13:29:18 +0000398 rl_bind_key_if_unbound_in_map (CTRL ('O'), operate_and_get_next, emacs_standard_keymap);
399 rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000400
401 /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
402 so it is not necessary to allow C-M-j for context switching. Turn
403 off this occasionally confusing behaviour. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000404 kseq[0] = CTRL('J');
405 kseq[1] = '\0';
406 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
407 if (func == rl_vi_editing_mode)
408 rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
409 kseq[0] = CTRL('M');
410 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
411 if (func == rl_vi_editing_mode)
412 rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000413#if defined (VI_MODE)
414 rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap);
415#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000416
Jari Aalto726f6381996-08-26 18:22:31 +0000417#if defined (BRACE_COMPLETION)
Jari Aaltob80f6442004-07-27 13:29:18 +0000418 rl_bind_key_if_unbound_in_map ('{', bash_brace_completion, emacs_meta_keymap); /*}*/
Jari Aalto726f6381996-08-26 18:22:31 +0000419#endif /* BRACE_COMPLETION */
420
421#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
Jari Aaltob80f6442004-07-27 13:29:18 +0000422 rl_bind_key_if_unbound_in_map ('/', bash_complete_filename, emacs_meta_keymap);
423 rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000424
Jari Aaltob80f6442004-07-27 13:29:18 +0000425 /* Have to jump through hoops here because there is a default binding for
426 M-~ (rl_tilde_expand) */
427 kseq[0] = '~';
428 kseq[1] = '\0';
429 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
430 if (func == 0 || func == rl_tilde_expand)
431 rl_bind_keyseq_in_map (kseq, bash_complete_username, emacs_meta_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000432
Jari Aaltob80f6442004-07-27 13:29:18 +0000433 rl_bind_key_if_unbound_in_map ('~', bash_possible_username_completions, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000434
Jari Aaltob80f6442004-07-27 13:29:18 +0000435 rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname, emacs_meta_keymap);
436 rl_bind_key_if_unbound_in_map ('@', bash_possible_hostname_completions, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000437
Jari Aaltob80f6442004-07-27 13:29:18 +0000438 rl_bind_key_if_unbound_in_map ('$', bash_complete_variable, emacs_meta_keymap);
439 rl_bind_key_if_unbound_in_map ('$', bash_possible_variable_completions, emacs_ctlx_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000440
Jari Aaltob80f6442004-07-27 13:29:18 +0000441 rl_bind_key_if_unbound_in_map ('!', bash_complete_command, emacs_meta_keymap);
442 rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions, emacs_ctlx_keymap);
443
444 rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word, emacs_meta_keymap);
445 rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word, emacs_ctlx_keymap);
446 rl_bind_key_if_unbound_in_map ('g', bash_glob_list_expansions, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000447
448#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
449
Jari Aaltob80f6442004-07-27 13:29:18 +0000450 rl_bind_key_if_unbound_in_map (TAB, dynamic_complete_history, emacs_meta_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000451
452 /* Tell the completer that we want a crack first. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000453 rl_attempted_completion_function = attempt_shell_completion;
Jari Aalto726f6381996-08-26 18:22:31 +0000454
455 /* Tell the completer that we might want to follow symbolic links or
456 do other expansion on directory names. */
457 rl_directory_completion_hook = bash_directory_completion_hook;
458
459 /* Tell the filename completer we want a chance to ignore some names. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000460 rl_ignore_some_completions_function = filename_completion_ignore;
Jari Aalto726f6381996-08-26 18:22:31 +0000461
Jari Aalto7117c2d2002-07-17 14:10:11 +0000462 /* Bind C-xC-e to invoke emacs and run result as commands. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000463 rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000464#if defined (VI_MODE)
Jari Aaltob80f6442004-07-27 13:29:18 +0000465 rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command, vi_movement_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000466# if defined (ALIAS)
Jari Aaltob80f6442004-07-27 13:29:18 +0000467 rl_bind_key_if_unbound_in_map ('@', posix_edit_macros, vi_movement_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000468# endif
Jari Aaltob80f6442004-07-27 13:29:18 +0000469
470 rl_bind_key_in_map ('\\', bash_vi_complete, vi_movement_keymap);
471 rl_bind_key_in_map ('*', bash_vi_complete, vi_movement_keymap);
472 rl_bind_key_in_map ('=', bash_vi_complete, vi_movement_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000473#endif
474
475 rl_completer_quote_characters = "'\"";
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000476
477 /* This sets rl_completer_word_break_characters and rl_special_prefixes
478 to the appropriate values, depending on whether or not hostname
479 completion is enabled. */
480 enable_hostname_completion (perform_hostname_completion);
481
482 /* characters that need to be quoted when appearing in filenames. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000483 rl_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{"; /*}*/
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000484 rl_filename_quoting_function = bash_quote_filename;
485 rl_filename_dequoting_function = bash_dequote_filename;
486 rl_char_is_quoted_p = char_is_quoted;
Jari Aalto726f6381996-08-26 18:22:31 +0000487
Jari Aalto7117c2d2002-07-17 14:10:11 +0000488#if 0
489 /* This is superfluous and makes it impossible to use tab completion in
490 vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix()
491 should already have called posix_readline_initialize() when
492 posixly_correct was set. */
Jari Aalto726f6381996-08-26 18:22:31 +0000493 if (posixly_correct)
494 posix_readline_initialize (1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000495#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000496
497 bash_readline_initialized = 1;
498}
499
500/* On Sun systems at least, rl_attempted_completion_function can end up
501 getting set to NULL, and rl_completion_entry_function set to do command
502 word completion if Bash is interrupted while trying to complete a command
503 word. This just resets all the completion functions to the right thing.
504 It's called from throw_to_top_level(). */
505void
506bashline_reinitialize ()
507{
508 tilde_initialize ();
509 rl_attempted_completion_function = attempt_shell_completion;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000510 rl_completion_entry_function = NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000511 rl_directory_completion_hook = bash_directory_completion_hook;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000512 rl_ignore_some_completions_function = filename_completion_ignore;
Jari Aalto726f6381996-08-26 18:22:31 +0000513}
514
515/* Contains the line to push into readline. */
516static char *push_to_readline = (char *)NULL;
517
518/* Push the contents of push_to_readline into the
519 readline buffer. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000520static int
Jari Aalto726f6381996-08-26 18:22:31 +0000521bash_push_line ()
522{
523 if (push_to_readline)
524 {
525 rl_insert_text (push_to_readline);
526 free (push_to_readline);
527 push_to_readline = (char *)NULL;
528 rl_startup_hook = old_rl_startup_hook;
529 }
Jari Aalto28ef6c32001-04-06 19:14:31 +0000530 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000531}
532
533/* Call this to set the initial text for the next line to read
534 from readline. */
535int
536bash_re_edit (line)
537 char *line;
538{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000539 FREE (push_to_readline);
Jari Aalto726f6381996-08-26 18:22:31 +0000540
541 push_to_readline = savestring (line);
542 old_rl_startup_hook = rl_startup_hook;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000543 rl_startup_hook = bash_push_line;
Jari Aalto726f6381996-08-26 18:22:31 +0000544
545 return (0);
546}
547
Jari Aalto28ef6c32001-04-06 19:14:31 +0000548static int
Jari Aalto726f6381996-08-26 18:22:31 +0000549display_shell_version (count, c)
550 int count, c;
551{
Jari Aalto28ef6c32001-04-06 19:14:31 +0000552 rl_crlf ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000553 show_shell_version (0);
Jari Aalto726f6381996-08-26 18:22:31 +0000554 putc ('\r', rl_outstream);
555 fflush (rl_outstream);
556 rl_on_new_line ();
557 rl_redisplay ();
Jari Aalto28ef6c32001-04-06 19:14:31 +0000558 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000559}
560
561/* **************************************************************** */
562/* */
563/* Readline Stuff */
564/* */
565/* **************************************************************** */
566
567/* If the user requests hostname completion, then simply build a list
Jari Aaltobb706242000-03-17 21:46:59 +0000568 of hosts, and complete from that forever more, or at least until
569 HOSTFILE is unset. */
Jari Aalto726f6381996-08-26 18:22:31 +0000570
Jari Aaltobb706242000-03-17 21:46:59 +0000571/* THIS SHOULD BE A STRINGLIST. */
Jari Aalto726f6381996-08-26 18:22:31 +0000572/* The kept list of hostnames. */
573static char **hostname_list = (char **)NULL;
574
575/* The physical size of the above list. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000576static int hostname_list_size;
Jari Aalto726f6381996-08-26 18:22:31 +0000577
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000578/* The number of hostnames in the above list. */
579static int hostname_list_length;
Jari Aalto726f6381996-08-26 18:22:31 +0000580
581/* Whether or not HOSTNAME_LIST has been initialized. */
582int hostname_list_initialized = 0;
583
Jari Aalto726f6381996-08-26 18:22:31 +0000584/* Initialize the hostname completion table. */
585static void
586initialize_hostname_list ()
587{
588 char *temp;
589
590 temp = get_string_value ("HOSTFILE");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000591 if (temp == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000592 temp = get_string_value ("hostname_completion_file");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000593 if (temp == 0)
594 temp = DEFAULT_HOSTS_FILE;
Jari Aalto726f6381996-08-26 18:22:31 +0000595
596 snarf_hosts_from_file (temp);
Jari Aalto726f6381996-08-26 18:22:31 +0000597
598 if (hostname_list)
599 hostname_list_initialized++;
600}
601
602/* Add NAME to the list of hosts. */
603static void
604add_host_name (name)
605 char *name;
606{
607 if (hostname_list_length + 2 > hostname_list_size)
608 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000609 hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000610 hostname_list = strvec_resize (hostname_list, hostname_list_size);
Jari Aalto726f6381996-08-26 18:22:31 +0000611 }
612
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000613 hostname_list[hostname_list_length++] = savestring (name);
614 hostname_list[hostname_list_length] = (char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000615}
616
617#define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
618
619static void
620snarf_hosts_from_file (filename)
621 char *filename;
622{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000623 FILE *file;
Jari Aalto726f6381996-08-26 18:22:31 +0000624 char *temp, buffer[256], name[256];
625 register int i, start;
626
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000627 file = fopen (filename, "r");
628 if (file == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000629 return;
630
631 while (temp = fgets (buffer, 255, file))
632 {
633 /* Skip to first character. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000634 for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++)
635 ;
Jari Aalto726f6381996-08-26 18:22:31 +0000636
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000637 /* If comment or blank line, ignore. */
638 if (buffer[i] == '\0' || buffer[i] == '#')
Jari Aalto726f6381996-08-26 18:22:31 +0000639 continue;
640
641 /* If `preprocessor' directive, do the include. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000642 if (strncmp (buffer + i, "$include ", 9) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000643 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000644 char *incfile, *t;
Jari Aalto726f6381996-08-26 18:22:31 +0000645
646 /* Find start of filename. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000647 for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++)
648 ;
Jari Aalto726f6381996-08-26 18:22:31 +0000649
650 /* Find end of filename. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000651 for (t = incfile; *t && cr_whitespace (*t) == 0; t++)
652 ;
Jari Aalto726f6381996-08-26 18:22:31 +0000653
654 *t = '\0';
655
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000656 snarf_hosts_from_file (incfile);
Jari Aalto726f6381996-08-26 18:22:31 +0000657 continue;
658 }
659
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000660 /* Skip internet address if present. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000661 if (DIGIT (buffer[i]))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000662 for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++);
Jari Aalto726f6381996-08-26 18:22:31 +0000663
664 /* Gobble up names. Each name is separated with whitespace. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000665 while (buffer[i])
Jari Aalto726f6381996-08-26 18:22:31 +0000666 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000667 for (; cr_whitespace (buffer[i]); i++)
668 ;
669 if (buffer[i] == '\0' || buffer[i] == '#')
670 break;
671
672 /* Isolate the current word. */
673 for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++)
674 ;
675 if (i == start)
Jari Aalto726f6381996-08-26 18:22:31 +0000676 continue;
677 strncpy (name, buffer + start, i - start);
678 name[i - start] = '\0';
679 add_host_name (name);
680 }
681 }
682 fclose (file);
683}
684
Jari Aaltobb706242000-03-17 21:46:59 +0000685/* Return the hostname list. */
686char **
687get_hostname_list ()
688{
689 if (hostname_list_initialized == 0)
690 initialize_hostname_list ();
691 return (hostname_list);
692}
693
694void
695clear_hostname_list ()
696{
697 register int i;
698
699 if (hostname_list_initialized == 0)
700 return;
701 for (i = 0; i < hostname_list_length; i++)
702 free (hostname_list[i]);
703 hostname_list_length = 0;
704}
705
Jari Aalto726f6381996-08-26 18:22:31 +0000706/* Return a NULL terminated list of hostnames which begin with TEXT.
707 Initialize the hostname list the first time if neccessary.
708 The array is malloc ()'ed, but not the individual strings. */
709static char **
710hostnames_matching (text)
711 char *text;
712{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000713 register int i, len, nmatch, rsize;
714 char **result;
Jari Aalto726f6381996-08-26 18:22:31 +0000715
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000716 if (hostname_list_initialized == 0)
717 initialize_hostname_list ();
Jari Aalto726f6381996-08-26 18:22:31 +0000718
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000719 if (hostname_list_initialized == 0)
720 return ((char **)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000721
722 /* Special case. If TEXT consists of nothing, then the whole list is
723 what is desired. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000724 if (*text == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000725 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000726 result = strvec_create (1 + hostname_list_length);
Jari Aalto726f6381996-08-26 18:22:31 +0000727 for (i = 0; i < hostname_list_length; i++)
728 result[i] = hostname_list[i];
729 result[i] = (char *)NULL;
730 return (result);
731 }
732
733 /* Scan until found, or failure. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000734 len = strlen (text);
735 result = (char **)NULL;
736 for (i = nmatch = rsize = 0; i < hostname_list_length; i++)
Jari Aalto726f6381996-08-26 18:22:31 +0000737 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000738 if (STREQN (text, hostname_list[i], len) == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000739 continue;
Jari Aalto726f6381996-08-26 18:22:31 +0000740
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000741 /* OK, it matches. Add it to the list. */
Jari Aaltobc4cd231998-07-23 14:37:54 +0000742 if (nmatch >= (rsize - 1))
Jari Aalto726f6381996-08-26 18:22:31 +0000743 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000744 rsize = (rsize + 16) - (rsize % 16);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000745 result = strvec_resize (result, rsize);
Jari Aalto726f6381996-08-26 18:22:31 +0000746 }
747
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000748 result[nmatch++] = hostname_list[i];
Jari Aalto726f6381996-08-26 18:22:31 +0000749 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000750 if (nmatch)
751 result[nmatch] = (char *)NULL;
752 return (result);
Jari Aalto726f6381996-08-26 18:22:31 +0000753}
754
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000755/* The equivalent of the Korn shell C-o operate-and-get-next-history-line
Jari Aalto726f6381996-08-26 18:22:31 +0000756 editing command. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000757static int saved_history_line_to_use = -1;
Jari Aalto726f6381996-08-26 18:22:31 +0000758
Jari Aalto28ef6c32001-04-06 19:14:31 +0000759static int
Jari Aalto726f6381996-08-26 18:22:31 +0000760set_saved_history ()
761{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000762 if (saved_history_line_to_use >= 0)
Jari Aaltob72432f1999-02-19 17:11:39 +0000763 rl_get_previous_history (history_length - saved_history_line_to_use, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000764 saved_history_line_to_use = -1;
Jari Aalto726f6381996-08-26 18:22:31 +0000765 rl_startup_hook = old_rl_startup_hook;
Jari Aaltof73dda02001-11-13 17:56:06 +0000766 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000767}
Jari Aalto726f6381996-08-26 18:22:31 +0000768
Jari Aalto28ef6c32001-04-06 19:14:31 +0000769static int
Jari Aalto726f6381996-08-26 18:22:31 +0000770operate_and_get_next (count, c)
771 int count, c;
772{
773 int where;
774
775 /* Accept the current line. */
Jari Aaltob72432f1999-02-19 17:11:39 +0000776 rl_newline (1, c);
Jari Aalto726f6381996-08-26 18:22:31 +0000777
778 /* Find the current line, and find the next line to use. */
779 where = where_history ();
780
Jari Aalto28ef6c32001-04-06 19:14:31 +0000781 if ((history_is_stifled () && (history_length >= history_max_entries)) ||
Jari Aalto726f6381996-08-26 18:22:31 +0000782 (where >= history_length - 1))
783 saved_history_line_to_use = where;
784 else
785 saved_history_line_to_use = where + 1;
786
787 old_rl_startup_hook = rl_startup_hook;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000788 rl_startup_hook = set_saved_history;
789
790 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000791}
792
Jari Aalto726f6381996-08-26 18:22:31 +0000793/* This vi mode command causes VI_EDIT_COMMAND to be run on the current
794 command being entered (if no explicit argument is given), otherwise on
795 a command from the history file. */
796
Jari Aaltob80f6442004-07-27 13:29:18 +0000797#define VI_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-vi}}\""
798#define EMACS_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-emacs}}\""
Jari Aalto726f6381996-08-26 18:22:31 +0000799
Jari Aalto28ef6c32001-04-06 19:14:31 +0000800static int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000801edit_and_execute_command (count, c, editing_mode, edit_command)
802 int count, c, editing_mode;
803 char *edit_command;
Jari Aalto726f6381996-08-26 18:22:31 +0000804{
805 char *command;
Jari Aaltof73dda02001-11-13 17:56:06 +0000806 int r, cclc, rrs;
807
808 rrs = rl_readline_state;
809 cclc = current_command_line_count;
Jari Aalto726f6381996-08-26 18:22:31 +0000810
811 /* Accept the current line. */
Jari Aaltob72432f1999-02-19 17:11:39 +0000812 rl_newline (1, c);
Jari Aalto726f6381996-08-26 18:22:31 +0000813
814 if (rl_explicit_arg)
815 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000816 command = (char *)xmalloc (strlen (edit_command) + 8);
817 sprintf (command, "%s %d", edit_command, count);
Jari Aalto726f6381996-08-26 18:22:31 +0000818 }
819 else
820 {
821 /* Take the command we were just editing, add it to the history file,
822 then call fc to operate on it. We have to add a dummy command to
823 the end of the history because fc ignores the last command (assumes
824 it's supposed to deal with the command before the `fc'). */
825 using_history ();
Jari Aaltod166f041997-06-05 14:59:13 +0000826 bash_add_history (rl_line_buffer);
827 bash_add_history ("");
Jari Aalto726f6381996-08-26 18:22:31 +0000828 history_lines_this_session++;
829 using_history ();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000830 command = savestring (edit_command);
Jari Aalto726f6381996-08-26 18:22:31 +0000831 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000832
833 /* Now, POSIX.1-2001 and SUSv3 say that the commands executed from the
834 temporary file should be placed into the history. We don't do that
835 yet. */
836 r = parse_and_execute (command, (editing_mode == VI_EDITING_MODE) ? "v" : "C-xC-e", SEVAL_NOHIST);
Jari Aaltof73dda02001-11-13 17:56:06 +0000837
838 current_command_line_count = cclc;
839
840 /* Now erase the contents of the current line and undo the effects of the
841 rl_accept_line() above. We don't even want to make the text we just
842 executed available for undoing. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000843 rl_line_buffer[0] = '\0'; /* XXX */
Jari Aaltof73dda02001-11-13 17:56:06 +0000844 rl_point = rl_end = 0;
845 rl_done = 0;
846 rl_readline_state = rrs;
847
848 rl_forced_update_display ();
Jari Aalto28ef6c32001-04-06 19:14:31 +0000849
850 return r;
Jari Aalto726f6381996-08-26 18:22:31 +0000851}
Jari Aalto7117c2d2002-07-17 14:10:11 +0000852
853#if defined (VI_MODE)
854static int
855vi_edit_and_execute_command (count, c)
856 int count, c;
857{
858 return (edit_and_execute_command (count, c, VI_EDITING_MODE, VI_EDIT_COMMAND));
859}
Jari Aalto726f6381996-08-26 18:22:31 +0000860#endif /* VI_MODE */
861
Jari Aalto7117c2d2002-07-17 14:10:11 +0000862static int
863emacs_edit_and_execute_command (count, c)
864 int count, c;
865{
866 return (edit_and_execute_command (count, c, EMACS_EDITING_MODE, EMACS_EDIT_COMMAND));
867}
868
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000869#if defined (ALIAS)
870static int
871posix_edit_macros (count, key)
872 int count, key;
873{
874 int c;
875 char alias_name[3], *alias_value, *macro;
876
877 c = rl_read_key ();
878 alias_name[0] = '_';
879 alias_name[1] = c;
880 alias_name[2] = '\0';
881
882 alias_value = get_alias_value (alias_name);
883 if (alias_value && *alias_value)
884 {
885 macro = savestring (alias_value);
886 rl_push_macro_input (macro);
887 }
888 return 0;
889}
890#endif
891
Jari Aalto726f6381996-08-26 18:22:31 +0000892/* **************************************************************** */
893/* */
894/* How To Do Shell Completion */
895/* */
896/* **************************************************************** */
897
Jari Aaltobb706242000-03-17 21:46:59 +0000898#define COMMAND_SEPARATORS ";|&{(`"
Jari Aaltob80f6442004-07-27 13:29:18 +0000899/* )} */
Jari Aaltobb706242000-03-17 21:46:59 +0000900
901static int
902check_redir (ti)
903 int ti;
904{
905 register int this_char, prev_char;
906
907 /* Handle the two character tokens `>&', `<&', and `>|'.
908 We are not in a command position after one of these. */
909 this_char = rl_line_buffer[ti];
910 prev_char = rl_line_buffer[ti - 1];
911
912 if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) ||
913 (this_char == '|' && prev_char == '>'))
914 return (1);
915 else if ((this_char == '{' && prev_char == '$') || /* } */
916 (char_is_quoted (rl_line_buffer, ti)))
917 return (1);
918 return (0);
919}
920
921#if defined (PROGRAMMABLE_COMPLETION)
Jari Aaltof73dda02001-11-13 17:56:06 +0000922/*
923 * XXX - because of the <= start test, and setting os = s+1, this can
924 * potentially return os > start. This is probably not what we want to
925 * happen, but fix later after 2.05a-release.
926 */
Jari Aaltobb706242000-03-17 21:46:59 +0000927static int
928find_cmd_start (start)
929 int start;
930{
931 register int s, os;
932
933 os = 0;
934 while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS)) <= start) &&
935 rl_line_buffer[s])
936 os = s+1;
937 return os;
938}
939
940static int
941find_cmd_end (end)
942 int end;
943{
944 register int e;
945
946 e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS);
947 return e;
948}
949
950static char *
951find_cmd_name (start)
952 int start;
953{
954 char *name;
955 register int s, e;
956
957 for (s = start; whitespace (rl_line_buffer[s]); s++)
958 ;
959
960 /* skip until a shell break character */
961 e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n");
962
963 name = substring (rl_line_buffer, s, e);
964
965 return (name);
966}
967
968static char *
969prog_complete_return (text, matchnum)
Jari Aaltof73dda02001-11-13 17:56:06 +0000970 const char *text;
Jari Aaltobb706242000-03-17 21:46:59 +0000971 int matchnum;
972{
973 static int ind;
974
975 if (matchnum == 0)
976 ind = 0;
977
978 if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0)
979 return (char *)NULL;
980 return (prog_complete_matches[ind++]);
981}
982
983#endif /* PROGRAMMABLE_COMPLETION */
984
Jari Aalto726f6381996-08-26 18:22:31 +0000985/* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
986 at START and END. Return an array of matches, or NULL if none. */
987static char **
988attempt_shell_completion (text, start, end)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000989 const char *text;
Jari Aalto726f6381996-08-26 18:22:31 +0000990 int start, end;
991{
Jari Aaltobb706242000-03-17 21:46:59 +0000992 int in_command_position, ti, saveti, qc;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000993 char **matches, *command_separator_chars;
Jari Aalto726f6381996-08-26 18:22:31 +0000994
Jari Aaltobb706242000-03-17 21:46:59 +0000995 command_separator_chars = COMMAND_SEPARATORS;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000996 matches = (char **)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000997 rl_ignore_some_completions_function = filename_completion_ignore;
Jari Aalto726f6381996-08-26 18:22:31 +0000998
999 /* Determine if this could be a command word. It is if it appears at
1000 the start of the line (ignoring preceding whitespace), or if it
1001 appears after a character that separates commands. It cannot be a
1002 command word if we aren't at the top-level prompt. */
1003 ti = start - 1;
Jari Aaltobb706242000-03-17 21:46:59 +00001004 saveti = qc = -1;
Jari Aalto726f6381996-08-26 18:22:31 +00001005
1006 while ((ti > -1) && (whitespace (rl_line_buffer[ti])))
1007 ti--;
1008
Jari Aaltobb706242000-03-17 21:46:59 +00001009#if 1
1010 /* If this is an open quote, maybe we're trying to complete a quoted
1011 command name. */
Jari Aaltob80f6442004-07-27 13:29:18 +00001012 if (ti >= 0 && (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\''))
Jari Aaltobb706242000-03-17 21:46:59 +00001013 {
1014 qc = rl_line_buffer[ti];
1015 saveti = ti--;
1016 while (ti > -1 && (whitespace (rl_line_buffer[ti])))
Jari Aalto28ef6c32001-04-06 19:14:31 +00001017 ti--;
Jari Aaltobb706242000-03-17 21:46:59 +00001018 }
1019#endif
1020
Jari Aalto726f6381996-08-26 18:22:31 +00001021 in_command_position = 0;
1022 if (ti < 0)
1023 {
1024 /* Only do command completion at the start of a line when we
Jari Aalto28ef6c32001-04-06 19:14:31 +00001025 are prompting at the top level. */
Jari Aalto726f6381996-08-26 18:22:31 +00001026 if (current_prompt_string == ps1_prompt)
1027 in_command_position++;
1028 }
1029 else if (member (rl_line_buffer[ti], command_separator_chars))
1030 {
Jari Aalto726f6381996-08-26 18:22:31 +00001031 in_command_position++;
1032
Jari Aaltobb706242000-03-17 21:46:59 +00001033 if (check_redir (ti) == 1)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001034 in_command_position = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001035 }
1036 else
1037 {
1038 /* This still could be in command position. It is possible
1039 that all of the previous words on the line are variable
1040 assignments. */
1041 }
1042
Jari Aaltod166f041997-06-05 14:59:13 +00001043 /* Check that we haven't incorrectly flagged a closed command substitution
1044 as indicating we're in a command position. */
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001045 if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' &&
Jari Aaltobb706242000-03-17 21:46:59 +00001046 *text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0)
Jari Aaltod166f041997-06-05 14:59:13 +00001047 in_command_position = 0;
1048
1049 /* Special handling for command substitution. If *TEXT is a backquote,
1050 it can be the start or end of an old-style command substitution, or
1051 unmatched. If it's unmatched, both calls to unclosed_pair will
1052 succeed. */
Jari Aaltobb706242000-03-17 21:46:59 +00001053 if (*text == '`' &&
1054 (in_command_position || (unclosed_pair (rl_line_buffer, start, "`") &&
1055 unclosed_pair (rl_line_buffer, end, "`"))))
Jari Aalto28ef6c32001-04-06 19:14:31 +00001056 matches = rl_completion_matches (text, command_subst_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00001057
Jari Aaltobb706242000-03-17 21:46:59 +00001058#if defined (PROGRAMMABLE_COMPLETION)
1059 /* Attempt programmable completion. */
1060 if (!matches && in_command_position == 0 && prog_completion_enabled &&
Jari Aalto7117c2d2002-07-17 14:10:11 +00001061 (progcomp_size () > 0) && current_prompt_string == ps1_prompt)
Jari Aaltobb706242000-03-17 21:46:59 +00001062 {
1063 int s, e, foundcs;
1064 char *n;
1065
1066 /* XXX - don't free the members */
1067 if (prog_complete_matches)
1068 free (prog_complete_matches);
1069 prog_complete_matches = (char **)NULL;
1070
1071 s = find_cmd_start (start);
1072 e = find_cmd_end (end);
1073 n = find_cmd_name (s);
Jari Aaltof73dda02001-11-13 17:56:06 +00001074 if (e > s)
1075 prog_complete_matches = programmable_completions (n, text, s, e, &foundcs);
1076 else
1077 foundcs = 0;
Jari Aaltobb706242000-03-17 21:46:59 +00001078 FREE (n);
1079 /* XXX - if we found a COMPSPEC for the command, just return whatever
1080 the programmable completion code returns, and disable the default
Jari Aalto28ef6c32001-04-06 19:14:31 +00001081 filename completion that readline will do unless the COPT_DEFAULT
1082 option has been set with the `-o default' option to complete. */
Jari Aaltobb706242000-03-17 21:46:59 +00001083 if (foundcs)
1084 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00001085 /* If the user specified that the compspec returns filenames, make
Jari Aaltof73dda02001-11-13 17:56:06 +00001086 sure that readline knows it. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001087 if (foundcs & COPT_FILENAMES)
1088 rl_filename_completion_desired = 1;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001089 /* If the user doesn't want a space appended, tell readline. */
1090 if (foundcs & COPT_NOSPACE)
1091 rl_completion_suppress_append = 1;
Jari Aaltobb706242000-03-17 21:46:59 +00001092 /* Turn what the programmable completion code returns into what
1093 readline wants. I should have made compute_lcd_of_matches
1094 external... */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001095 matches = rl_completion_matches (text, prog_complete_return);
1096 if ((foundcs & COPT_DEFAULT) == 0)
1097 rl_attempted_completion_over = 1; /* no default */
Jari Aaltob80f6442004-07-27 13:29:18 +00001098 if (matches || ((foundcs & COPT_BASHDEFAULT) == 0))
1099 return (matches);
Jari Aaltobb706242000-03-17 21:46:59 +00001100 }
1101 }
1102#endif
1103
Jari Aaltob80f6442004-07-27 13:29:18 +00001104 if (matches == 0)
1105 matches = bash_default_completion (text, start, end, qc, in_command_position);
1106
1107 return matches;
1108}
1109
1110char **
1111bash_default_completion (text, start, end, qc, in_command_position)
1112 const char *text;
1113 int start, end, qc, in_command_position;
1114{
1115 char **matches;
1116
1117 matches = (char **)NULL;
1118
Jari Aaltobb706242000-03-17 21:46:59 +00001119 /* New posix-style command substitution or variable name? */
Jari Aalto726f6381996-08-26 18:22:31 +00001120 if (!matches && *text == '$')
Jari Aaltobb706242000-03-17 21:46:59 +00001121 {
1122 if (qc != '\'' && text[1] == '(') /* ) */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001123 matches = rl_completion_matches (text, command_subst_completion_function);
Jari Aaltobb706242000-03-17 21:46:59 +00001124 else
Jari Aalto28ef6c32001-04-06 19:14:31 +00001125 matches = rl_completion_matches (text, variable_completion_function);
Jari Aaltobb706242000-03-17 21:46:59 +00001126 }
Jari Aalto726f6381996-08-26 18:22:31 +00001127
1128 /* If the word starts in `~', and there is no slash in the word, then
1129 try completing this word as a username. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001130 if (!matches && *text == '~' && !xstrchr (text, '/'))
Jari Aalto28ef6c32001-04-06 19:14:31 +00001131 matches = rl_completion_matches (text, rl_username_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00001132
1133 /* Another one. Why not? If the word starts in '@', then look through
1134 the world of known hostnames for completion first. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001135 if (!matches && perform_hostname_completion && *text == '@')
Jari Aalto28ef6c32001-04-06 19:14:31 +00001136 matches = rl_completion_matches (text, hostname_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00001137
1138 /* And last, (but not least) if this word is in a command position, then
1139 complete over possible command names, including aliases, functions,
1140 and command names. */
1141 if (!matches && in_command_position)
1142 {
Jari Aaltobb706242000-03-17 21:46:59 +00001143 if (start == 0 && end == 0 && text[0] == '\0' && no_empty_command_completion)
1144 {
1145 matches = (char **)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001146 rl_ignore_some_completions_function = bash_ignore_everything;
Jari Aaltobb706242000-03-17 21:46:59 +00001147 }
1148 else
1149 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001150#define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x))
1151
Jari Aalto28ef6c32001-04-06 19:14:31 +00001152 matches = rl_completion_matches (text, command_word_completion_function);
Jari Aaltob80f6442004-07-27 13:29:18 +00001153
Jari Aaltobb706242000-03-17 21:46:59 +00001154 /* If we are attempting command completion and nothing matches, we
1155 do not want readline to perform filename completion for us. We
1156 still want to be able to complete partial pathnames, so set the
1157 completion ignore function to something which will remove
1158 filenames and leave directories in the match list. */
1159 if (matches == (char **)NULL)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001160 rl_ignore_some_completions_function = bash_ignore_filenames;
Jari Aaltob80f6442004-07-27 13:29:18 +00001161#if 0
1162 else if (matches[1] == 0 && CMD_IS_DIR(matches[0]))
Jari Aalto7117c2d2002-07-17 14:10:11 +00001163 /* Turn off rl_filename_completion_desired so readline doesn't
1164 append a slash if there is a directory with the same name
1165 in the current directory, or other filename-specific things.
1166 If the name begins with a slash, we're either completing a
1167 full pathname or a directory pathname, and readline won't be
1168 looking in the current directory anyway, so there's no
1169 conflict. */
1170 rl_filename_completion_desired = 0;
Jari Aaltob80f6442004-07-27 13:29:18 +00001171 else if (matches[0] && matches[1] && STREQ (matches[0], matches[1]) && CMD_IS_DIR (matches[0]))
Jari Aalto7117c2d2002-07-17 14:10:11 +00001172 /* There are multiple instances of the same match (duplicate
1173 completions haven't yet been removed). In this case, all of
1174 the matches will be the same, and the duplicate removal code
1175 will distill them all down to one. We turn off
1176 rl_filename_completion_desired for the same reason as above.
1177 Remember: we only care if there's eventually a single unique
1178 completion. If there are multiple completions this won't
1179 make a difference and the problem won't occur. */
1180 rl_filename_completion_desired = 0;
Jari Aaltob80f6442004-07-27 13:29:18 +00001181#endif
Jari Aaltobb706242000-03-17 21:46:59 +00001182 }
Jari Aalto726f6381996-08-26 18:22:31 +00001183 }
1184
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001185 /* This could be a globbing pattern, so try to expand it using pathname
1186 expansion. */
Jari Aaltof73dda02001-11-13 17:56:06 +00001187 if (!matches && glob_pattern_p (text))
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001188 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00001189 matches = rl_completion_matches (text, glob_complete_word);
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001190 /* A glob expression that matches more than one filename is problematic.
1191 If we match more than one filename, punt. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001192 if (matches && matches[1] && rl_completion_type == TAB)
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001193 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00001194 strvec_dispose (matches);
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001195 matches = (char **)0;
1196 }
1197 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001198
Jari Aalto726f6381996-08-26 18:22:31 +00001199 return (matches);
1200}
1201
1202/* This is the function to call when the word to complete is in a position
1203 where a command word can be found. It grovels $PATH, looking for commands
1204 that match. It also scans aliases, function names, and the shell_builtin
1205 table. */
Jari Aaltobb706242000-03-17 21:46:59 +00001206char *
Jari Aalto726f6381996-08-26 18:22:31 +00001207command_word_completion_function (hint_text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001208 const char *hint_text;
Jari Aalto726f6381996-08-26 18:22:31 +00001209 int state;
1210{
1211 static char *hint = (char *)NULL;
1212 static char *path = (char *)NULL;
1213 static char *val = (char *)NULL;
1214 static char *filename_hint = (char *)NULL;
1215 static int path_index, hint_len, istate;
1216 static int mapping_over, local_index;
1217 static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
1218#if defined (ALIAS)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001219 static alias_t **alias_list = (alias_t **)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00001220#endif /* ALIAS */
1221
1222 /* We have to map over the possibilities for command words. If we have
1223 no state, then make one just for that purpose. */
Jari Aalto726f6381996-08-26 18:22:31 +00001224 if (!state)
1225 {
1226 if (hint)
1227 free (hint);
1228
1229 mapping_over = 0;
1230 val = (char *)NULL;
1231
1232 /* If this is an absolute program name, do not check it against
1233 aliases, reserved words, functions or builtins. We must check
1234 whether or not it is unique, and, if so, whether that filename
1235 is executable. */
Jari Aaltof73dda02001-11-13 17:56:06 +00001236 if (absolute_program (hint_text))
Jari Aalto726f6381996-08-26 18:22:31 +00001237 {
1238 /* Perform tilde expansion on what's passed, so we don't end up
1239 passing filenames with tildes directly to stat(). */
1240 if (*hint_text == '~')
Jari Aalto7117c2d2002-07-17 14:10:11 +00001241 hint = bash_tilde_expand (hint_text, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00001242 else
1243 hint = savestring (hint_text);
1244 hint_len = strlen (hint);
1245
1246 if (filename_hint)
1247 free (filename_hint);
1248 filename_hint = savestring (hint);
1249
1250 mapping_over = 4;
1251 istate = 0;
1252 goto inner;
1253 }
1254
1255 hint = savestring (hint_text);
1256 hint_len = strlen (hint);
1257
1258 path = get_string_value ("PATH");
1259 path_index = 0;
1260
1261 /* Initialize the variables for each type of command word. */
1262 local_index = 0;
1263
1264 if (varlist)
1265 free (varlist);
1266
1267 varlist = all_visible_functions ();
1268
1269#if defined (ALIAS)
1270 if (alias_list)
1271 free (alias_list);
1272
1273 alias_list = all_aliases ();
1274#endif /* ALIAS */
1275 }
1276
1277 /* mapping_over says what we are currently hacking. Note that every case
1278 in this list must fall through when there are no more possibilities. */
1279
1280 switch (mapping_over)
1281 {
1282 case 0: /* Aliases come first. */
1283#if defined (ALIAS)
1284 while (alias_list && alias_list[local_index])
1285 {
1286 register char *alias;
1287
1288 alias = alias_list[local_index++]->name;
1289
1290 if (STREQN (alias, hint, hint_len))
1291 return (savestring (alias));
1292 }
1293#endif /* ALIAS */
1294 local_index = 0;
1295 mapping_over++;
1296
1297 case 1: /* Then shell reserved words. */
1298 {
1299 while (word_token_alist[local_index].word)
1300 {
1301 register char *reserved_word;
1302
1303 reserved_word = word_token_alist[local_index++].word;
1304
1305 if (STREQN (reserved_word, hint, hint_len))
1306 return (savestring (reserved_word));
1307 }
1308 local_index = 0;
1309 mapping_over++;
1310 }
1311
1312 case 2: /* Then function names. */
1313 while (varlist && varlist[local_index])
1314 {
1315 register char *varname;
1316
1317 varname = varlist[local_index++]->name;
1318
1319 if (STREQN (varname, hint, hint_len))
1320 return (savestring (varname));
1321 }
1322 local_index = 0;
1323 mapping_over++;
1324
1325 case 3: /* Then shell builtins. */
1326 for (; local_index < num_shell_builtins; local_index++)
1327 {
1328 /* Ignore it if it doesn't have a function pointer or if it
1329 is not currently enabled. */
1330 if (!shell_builtins[local_index].function ||
1331 (shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0)
1332 continue;
1333
1334 if (STREQN (shell_builtins[local_index].name, hint, hint_len))
1335 {
1336 int i = local_index++;
1337
1338 return (savestring (shell_builtins[i].name));
1339 }
1340 }
1341 local_index = 0;
1342 mapping_over++;
1343 }
1344
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001345 /* Repeatedly call filename_completion_function while we have
Jari Aalto726f6381996-08-26 18:22:31 +00001346 members of PATH left. Question: should we stat each file?
1347 Answer: we call executable_file () on each file. */
1348 outer:
1349
1350 istate = (val != (char *)NULL);
1351
1352 if (!istate)
1353 {
1354 char *current_path;
1355
1356 /* Get the next directory from the path. If there is none, then we
1357 are all done. */
1358 if (!path || !path[path_index] ||
1359 (current_path = extract_colon_unit (path, &path_index)) == 0)
1360 return ((char *)NULL);
1361
1362 if (*current_path == 0)
1363 {
1364 free (current_path);
1365 current_path = savestring (".");
1366 }
1367
1368 if (*current_path == '~')
1369 {
1370 char *t;
1371
Jari Aalto7117c2d2002-07-17 14:10:11 +00001372 t = bash_tilde_expand (current_path, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00001373 free (current_path);
1374 current_path = t;
1375 }
1376
1377 if (filename_hint)
1378 free (filename_hint);
1379
Jari Aalto7117c2d2002-07-17 14:10:11 +00001380 filename_hint = sh_makepath (current_path, hint, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00001381
1382 free (current_path);
1383 }
1384
1385 inner:
Jari Aalto28ef6c32001-04-06 19:14:31 +00001386 val = rl_filename_completion_function (filename_hint, istate);
Jari Aalto726f6381996-08-26 18:22:31 +00001387 istate = 1;
1388
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001389 if (val == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00001390 {
1391 /* If the hint text is an absolute program, then don't bother
1392 searching through PATH. */
1393 if (absolute_program (hint))
1394 return ((char *)NULL);
1395
1396 goto outer;
1397 }
1398 else
1399 {
Jari Aaltod166f041997-06-05 14:59:13 +00001400 int match, freetemp;
Jari Aalto726f6381996-08-26 18:22:31 +00001401 char *temp;
1402
1403 if (absolute_program (hint))
1404 {
1405 match = strncmp (val, hint, hint_len) == 0;
1406 /* If we performed tilde expansion, restore the original
1407 filename. */
1408 if (*hint_text == '~')
1409 {
Jari Aaltoeb873672004-11-09 21:37:25 +00001410 int l, tl, vl, dl;
1411 char *rd;
Jari Aalto726f6381996-08-26 18:22:31 +00001412 vl = strlen (val);
1413 tl = strlen (hint_text);
Jari Aaltoeb873672004-11-09 21:37:25 +00001414#if 0
Jari Aalto726f6381996-08-26 18:22:31 +00001415 l = vl - hint_len; /* # of chars added */
Jari Aaltoeb873672004-11-09 21:37:25 +00001416#else
1417 rd = savestring (filename_hint);
1418 bash_directory_expansion (&rd);
1419 dl = strlen (rd);
1420 l = vl - dl; /* # of chars added */
1421 free (rd);
1422#endif
Jari Aaltof73dda02001-11-13 17:56:06 +00001423 temp = (char *)xmalloc (l + 2 + tl);
Jari Aalto726f6381996-08-26 18:22:31 +00001424 strcpy (temp, hint_text);
1425 strcpy (temp + tl, val + vl - l);
1426 }
1427 else
1428 temp = savestring (val);
Jari Aaltod166f041997-06-05 14:59:13 +00001429 freetemp = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001430 }
1431 else
1432 {
1433 temp = strrchr (val, '/');
1434
1435 if (temp)
1436 {
1437 temp++;
Jari Aaltod166f041997-06-05 14:59:13 +00001438 freetemp = match = strncmp (temp, hint, hint_len) == 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001439 if (match)
1440 temp = savestring (temp);
1441 }
1442 else
Jari Aaltod166f041997-06-05 14:59:13 +00001443 freetemp = match = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001444 }
1445
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001446 /* If we have found a match, and it is an executable file or a
1447 directory name, return it. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001448 if (match && executable_or_directory (val))
Jari Aalto726f6381996-08-26 18:22:31 +00001449 {
1450 free (val);
1451 val = ""; /* So it won't be NULL. */
1452 return (temp);
1453 }
1454 else
1455 {
Jari Aaltod166f041997-06-05 14:59:13 +00001456 if (freetemp)
1457 free (temp);
Jari Aalto726f6381996-08-26 18:22:31 +00001458 free (val);
1459 goto inner;
1460 }
1461 }
1462}
1463
Jari Aaltod166f041997-06-05 14:59:13 +00001464/* Completion inside an unterminated command substitution. */
Jari Aalto726f6381996-08-26 18:22:31 +00001465static char *
1466command_subst_completion_function (text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001467 const char *text;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001468 int state;
Jari Aalto726f6381996-08-26 18:22:31 +00001469{
1470 static char **matches = (char **)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001471 static const char *orig_start;
1472 static char *filename_text = (char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00001473 static int cmd_index, start_len;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001474 char *value;
Jari Aalto726f6381996-08-26 18:22:31 +00001475
1476 if (state == 0)
1477 {
1478 if (filename_text)
1479 free (filename_text);
1480 orig_start = text;
1481 if (*text == '`')
Jari Aalto28ef6c32001-04-06 19:14:31 +00001482 text++;
Jari Aaltocce855b1998-04-17 19:52:44 +00001483 else if (*text == '$' && text[1] == '(') /* ) */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001484 text += 2;
Jari Aaltob80f6442004-07-27 13:29:18 +00001485 /* If the text was quoted, suppress any quote character that the
1486 readline completion code would insert. */
1487 rl_completion_suppress_quote = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001488 start_len = text - orig_start;
1489 filename_text = savestring (text);
1490 if (matches)
1491 free (matches);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001492
1493 /*
1494 * At this point we can entertain the idea of re-parsing
1495 * `filename_text' into a (possibly incomplete) command name and
1496 * arguments, and doing completion based on that. This is
1497 * currently very rudimentary, but it is a small improvement.
1498 */
1499 for (value = filename_text + strlen (filename_text) - 1; value > filename_text; value--)
1500 if (whitespace (*value) || member (*value, COMMAND_SEPARATORS))
1501 break;
1502 if (value <= filename_text)
1503 matches = rl_completion_matches (filename_text, command_word_completion_function);
1504 else
1505 {
1506 value++;
1507 start_len += value - filename_text;
1508 if (whitespace (value[-1]))
1509 matches = rl_completion_matches (value, rl_filename_completion_function);
1510 else
1511 matches = rl_completion_matches (value, command_word_completion_function);
1512 }
1513
1514 /* If there is more than one match, rl_completion_matches has already
1515 put the lcd in matches[0]. Skip over it. */
1516 cmd_index = matches && matches[0] && matches[1];
Jari Aalto726f6381996-08-26 18:22:31 +00001517 }
1518
1519 if (!matches || !matches[cmd_index])
1520 {
1521 rl_filename_quoting_desired = 0; /* disable quoting */
1522 return ((char *)NULL);
1523 }
1524 else
1525 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001526 value = (char *)xmalloc (1 + start_len + strlen (matches[cmd_index]));
Jari Aalto726f6381996-08-26 18:22:31 +00001527
1528 if (start_len == 1)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001529 value[0] = *orig_start;
Jari Aalto726f6381996-08-26 18:22:31 +00001530 else
Jari Aalto28ef6c32001-04-06 19:14:31 +00001531 strncpy (value, orig_start, start_len);
Jari Aalto726f6381996-08-26 18:22:31 +00001532
1533 strcpy (value + start_len, matches[cmd_index]);
1534
1535 cmd_index++;
1536 return (value);
1537 }
1538}
1539
1540/* Okay, now we write the entry_function for variable completion. */
1541static char *
1542variable_completion_function (text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001543 const char *text;
Jari Aalto726f6381996-08-26 18:22:31 +00001544 int state;
Jari Aalto726f6381996-08-26 18:22:31 +00001545{
Jari Aaltobb706242000-03-17 21:46:59 +00001546 static char **varlist = (char **)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00001547 static int varlist_index;
1548 static char *varname = (char *)NULL;
1549 static int namelen;
1550 static int first_char, first_char_loc;
1551
1552 if (!state)
1553 {
1554 if (varname)
1555 free (varname);
1556
1557 first_char_loc = 0;
1558 first_char = text[0];
1559
1560 if (first_char == '$')
1561 first_char_loc++;
1562
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001563 if (text[first_char_loc] == '{')
Jari Aalto28ef6c32001-04-06 19:14:31 +00001564 first_char_loc++;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001565
Jari Aalto726f6381996-08-26 18:22:31 +00001566 varname = savestring (text + first_char_loc);
1567
1568 namelen = strlen (varname);
1569 if (varlist)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001570 strvec_dispose (varlist);
Jari Aaltobb706242000-03-17 21:46:59 +00001571
1572 varlist = all_variables_matching_prefix (varname);
Jari Aalto726f6381996-08-26 18:22:31 +00001573 varlist_index = 0;
1574 }
1575
Jari Aalto726f6381996-08-26 18:22:31 +00001576 if (!varlist || !varlist[varlist_index])
1577 {
1578 return ((char *)NULL);
1579 }
1580 else
1581 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001582 char *value;
1583
1584 value = (char *)xmalloc (4 + strlen (varlist[varlist_index]));
Jari Aalto726f6381996-08-26 18:22:31 +00001585
1586 if (first_char_loc)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001587 {
1588 value[0] = first_char;
1589 if (first_char_loc == 2)
1590 value[1] = '{';
1591 }
Jari Aalto726f6381996-08-26 18:22:31 +00001592
Jari Aaltobb706242000-03-17 21:46:59 +00001593 strcpy (value + first_char_loc, varlist[varlist_index]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001594 if (first_char_loc == 2)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001595 strcat (value, "}");
Jari Aalto726f6381996-08-26 18:22:31 +00001596
1597 varlist_index++;
1598 return (value);
1599 }
1600}
1601
1602/* How about a completion function for hostnames? */
1603static char *
1604hostname_completion_function (text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001605 const char *text;
Jari Aalto726f6381996-08-26 18:22:31 +00001606 int state;
Jari Aalto726f6381996-08-26 18:22:31 +00001607{
1608 static char **list = (char **)NULL;
1609 static int list_index = 0;
1610 static int first_char, first_char_loc;
1611
1612 /* If we don't have any state, make some. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001613 if (state == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00001614 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001615 FREE (list);
Jari Aalto726f6381996-08-26 18:22:31 +00001616
1617 list = (char **)NULL;
1618
1619 first_char_loc = 0;
1620 first_char = *text;
1621
1622 if (first_char == '@')
1623 first_char_loc++;
1624
Jari Aaltof73dda02001-11-13 17:56:06 +00001625 list = hostnames_matching ((char *)text+first_char_loc);
Jari Aalto726f6381996-08-26 18:22:31 +00001626 list_index = 0;
1627 }
1628
1629 if (list && list[list_index])
1630 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001631 char *t;
Jari Aalto726f6381996-08-26 18:22:31 +00001632
Jari Aaltof73dda02001-11-13 17:56:06 +00001633 t = (char *)xmalloc (2 + strlen (list[list_index]));
Jari Aalto726f6381996-08-26 18:22:31 +00001634 *t = first_char;
1635 strcpy (t + first_char_loc, list[list_index]);
1636 list_index++;
1637 return (t);
1638 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001639
1640 return ((char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +00001641}
1642
Jari Aalto7117c2d2002-07-17 14:10:11 +00001643/*
1644 * A completion function for service names from /etc/services (or wherever).
1645 */
1646char *
1647bash_servicename_completion_function (text, state)
1648 const char *text;
1649 int state;
1650{
1651#if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT)
1652 return ((char *)NULL);
1653#else
1654 static char *sname = (char *)NULL;
1655 static struct servent *srvent;
1656 static int snamelen, firstc;
1657 char *value;
1658 char **alist, *aentry;
1659 int afound;
1660
1661 if (state == 0)
1662 {
1663 FREE (sname);
1664 firstc = *text;
1665
1666 sname = savestring (text);
1667 snamelen = strlen (sname);
1668 setservent (0);
1669 }
1670
1671 while (srvent = getservent ())
1672 {
1673 afound = 0;
1674 if (snamelen == 0 || (STREQN (sname, srvent->s_name, snamelen)))
1675 break;
1676 /* Not primary, check aliases */
1677 for (alist = srvent->s_aliases; aentry = *alist; alist++)
1678 {
1679 if (STREQN (sname, aentry, snamelen))
1680 {
1681 afound = 1;
1682 break;
1683 }
1684 }
1685
1686 if (afound)
1687 break;
1688 }
1689
1690 if (srvent == 0)
1691 {
1692 endservent ();
1693 return ((char *)NULL);
1694 }
1695
1696 value = afound ? savestring (aentry) : savestring (srvent->s_name);
1697 return value;
1698#endif
1699}
1700
1701/*
1702 * A completion function for group names from /etc/group (or wherever).
1703 */
Jari Aaltof73dda02001-11-13 17:56:06 +00001704char *
1705bash_groupname_completion_function (text, state)
1706 const char *text;
1707 int state;
1708{
1709#if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
1710 return ((char *)NULL);
1711#else
1712 static char *gname = (char *)NULL;
1713 static struct group *grent;
1714 static int gnamelen;
1715 char *value;
1716
1717 if (state == 0)
1718 {
1719 FREE (gname);
1720 gname = savestring (text);
1721 gnamelen = strlen (gname);
1722
1723 setgrent ();
1724 }
1725
1726 while (grent = getgrent ())
1727 {
1728 if (gnamelen == 0 || (STREQN (gname, grent->gr_name, gnamelen)))
1729 break;
1730 }
1731
1732 if (grent == 0)
1733 {
1734 endgrent ();
1735 return ((char *)NULL);
1736 }
1737
1738 value = savestring (grent->gr_name);
1739 return (value);
1740#endif
1741}
1742
Jari Aaltocce855b1998-04-17 19:52:44 +00001743/* Functions to perform history and alias expansions on the current line. */
1744
1745#if defined (BANG_HISTORY)
1746/* Perform history expansion on the current line. If no history expansion
1747 is done, pre_process_line() returns what it was passed, so we need to
1748 allocate a new line here. */
Jari Aalto726f6381996-08-26 18:22:31 +00001749static char *
1750history_expand_line_internal (line)
1751 char *line;
1752{
1753 char *new_line;
Jari Aaltob80f6442004-07-27 13:29:18 +00001754 int old_verify;
Jari Aalto726f6381996-08-26 18:22:31 +00001755
Jari Aaltob80f6442004-07-27 13:29:18 +00001756 old_verify = hist_verify;
1757 hist_verify = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001758 new_line = pre_process_line (line, 0, 0);
Jari Aaltob80f6442004-07-27 13:29:18 +00001759 hist_verify = old_verify;
1760
Jari Aaltod166f041997-06-05 14:59:13 +00001761 return (new_line == line) ? savestring (line) : new_line;
Jari Aalto726f6381996-08-26 18:22:31 +00001762}
Jari Aalto726f6381996-08-26 18:22:31 +00001763#endif
1764
1765/* There was an error in expansion. Let the preprocessor print
1766 the error here. */
1767static void
1768cleanup_expansion_error ()
1769{
1770 char *to_free;
Jari Aaltob80f6442004-07-27 13:29:18 +00001771#if defined (BANG_HISTORY)
1772 int old_verify;
1773
1774 old_verify = hist_verify;
1775 hist_verify = 0;
1776#endif
Jari Aalto726f6381996-08-26 18:22:31 +00001777
1778 fprintf (rl_outstream, "\r\n");
1779 to_free = pre_process_line (rl_line_buffer, 1, 0);
Jari Aaltob80f6442004-07-27 13:29:18 +00001780#if defined (BANG_HISTORY)
1781 hist_verify = old_verify;
1782#endif
Jari Aaltod166f041997-06-05 14:59:13 +00001783 if (to_free != rl_line_buffer)
Jari Aaltob80f6442004-07-27 13:29:18 +00001784 FREE (to_free);
Jari Aalto726f6381996-08-26 18:22:31 +00001785 putc ('\r', rl_outstream);
1786 rl_forced_update_display ();
1787}
1788
1789/* If NEW_LINE differs from what is in the readline line buffer, add an
1790 undo record to get from the readline line buffer contents to the new
1791 line and make NEW_LINE the current readline line. */
1792static void
1793maybe_make_readline_line (new_line)
1794 char *new_line;
1795{
1796 if (strcmp (new_line, rl_line_buffer) != 0)
1797 {
1798 rl_point = rl_end;
1799
1800 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
1801 rl_delete_text (0, rl_point);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001802 rl_point = rl_end = rl_mark = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001803 rl_insert_text (new_line);
1804 rl_add_undo (UNDO_END, 0, 0, 0);
1805 }
1806}
1807
1808/* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
1809static void
1810set_up_new_line (new_line)
1811 char *new_line;
1812{
Jari Aaltof73dda02001-11-13 17:56:06 +00001813 int old_point, at_end;
1814
1815 old_point = rl_point;
1816 at_end = rl_point == rl_end;
Jari Aalto726f6381996-08-26 18:22:31 +00001817
1818 /* If the line was history and alias expanded, then make that
1819 be one thing to undo. */
1820 maybe_make_readline_line (new_line);
1821 free (new_line);
1822
1823 /* Place rl_point where we think it should go. */
1824 if (at_end)
1825 rl_point = rl_end;
1826 else if (old_point < rl_end)
1827 {
1828 rl_point = old_point;
1829 if (!whitespace (rl_line_buffer[rl_point]))
Jari Aaltob72432f1999-02-19 17:11:39 +00001830 rl_forward_word (1, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00001831 }
1832}
1833
Jari Aaltocce855b1998-04-17 19:52:44 +00001834#if defined (ALIAS)
1835/* Expand aliases in the current readline line. */
1836static int
Jari Aalto28ef6c32001-04-06 19:14:31 +00001837alias_expand_line (count, ignore)
1838 int count, ignore;
Jari Aaltocce855b1998-04-17 19:52:44 +00001839{
1840 char *new_line;
1841
1842 new_line = alias_expand (rl_line_buffer);
1843
1844 if (new_line)
1845 {
1846 set_up_new_line (new_line);
1847 return (0);
1848 }
1849 else
1850 {
1851 cleanup_expansion_error ();
1852 return (1);
1853 }
1854}
1855#endif
1856
1857#if defined (BANG_HISTORY)
Jari Aalto726f6381996-08-26 18:22:31 +00001858/* History expand the line. */
Jari Aaltocce855b1998-04-17 19:52:44 +00001859static int
Jari Aalto28ef6c32001-04-06 19:14:31 +00001860history_expand_line (count, ignore)
1861 int count, ignore;
Jari Aalto726f6381996-08-26 18:22:31 +00001862{
1863 char *new_line;
1864
1865 new_line = history_expand_line_internal (rl_line_buffer);
1866
1867 if (new_line)
Jari Aaltocce855b1998-04-17 19:52:44 +00001868 {
1869 set_up_new_line (new_line);
1870 return (0);
1871 }
Jari Aalto726f6381996-08-26 18:22:31 +00001872 else
Jari Aaltocce855b1998-04-17 19:52:44 +00001873 {
1874 cleanup_expansion_error ();
1875 return (1);
1876 }
Jari Aalto726f6381996-08-26 18:22:31 +00001877}
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001878
Jari Aaltocce855b1998-04-17 19:52:44 +00001879/* Expand history substitutions in the current line and then insert a
Jari Aalto28ef6c32001-04-06 19:14:31 +00001880 space (hopefully close to where we were before). */
Jari Aaltocce855b1998-04-17 19:52:44 +00001881static int
Jari Aalto28ef6c32001-04-06 19:14:31 +00001882tcsh_magic_space (count, ignore)
1883 int count, ignore;
Jari Aaltocce855b1998-04-17 19:52:44 +00001884{
Jari Aalto28ef6c32001-04-06 19:14:31 +00001885 int dist_from_end, old_point;
1886
1887 old_point = rl_point;
1888 dist_from_end = rl_end - rl_point;
1889 if (history_expand_line (count, ignore) == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00001890 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00001891 /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
1892 This works if all expansions were before rl_point or if no expansions
1893 were performed. */
1894 rl_point = (old_point == 0) ? old_point : rl_end - dist_from_end;
Jari Aaltocce855b1998-04-17 19:52:44 +00001895 rl_insert (1, ' ');
1896 return (0);
1897 }
1898 else
1899 return (1);
1900}
1901#endif
1902
Jari Aalto726f6381996-08-26 18:22:31 +00001903/* History and alias expand the line. */
Jari Aaltocce855b1998-04-17 19:52:44 +00001904static int
Jari Aalto28ef6c32001-04-06 19:14:31 +00001905history_and_alias_expand_line (count, ignore)
1906 int count, ignore;
Jari Aalto726f6381996-08-26 18:22:31 +00001907{
1908 char *new_line;
1909
Jari Aaltob80f6442004-07-27 13:29:18 +00001910 new_line = history_expand_line_internal (rl_line_buffer);
Jari Aalto726f6381996-08-26 18:22:31 +00001911
1912#if defined (ALIAS)
1913 if (new_line)
1914 {
1915 char *alias_line;
1916
1917 alias_line = alias_expand (new_line);
1918 free (new_line);
1919 new_line = alias_line;
1920 }
1921#endif /* ALIAS */
1922
1923 if (new_line)
Jari Aaltocce855b1998-04-17 19:52:44 +00001924 {
1925 set_up_new_line (new_line);
1926 return (0);
1927 }
Jari Aalto726f6381996-08-26 18:22:31 +00001928 else
Jari Aaltocce855b1998-04-17 19:52:44 +00001929 {
1930 cleanup_expansion_error ();
1931 return (1);
1932 }
Jari Aalto726f6381996-08-26 18:22:31 +00001933}
1934
1935/* History and alias expand the line, then perform the shell word
Jari Aaltocce855b1998-04-17 19:52:44 +00001936 expansions by calling expand_string. This can't use set_up_new_line()
1937 because we want the variable expansions as a separate undo'able
1938 set of operations. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001939static int
1940shell_expand_line (count, ignore)
1941 int count, ignore;
Jari Aalto726f6381996-08-26 18:22:31 +00001942{
1943 char *new_line;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001944 WORD_LIST *expanded_string;
Jari Aalto726f6381996-08-26 18:22:31 +00001945
Jari Aaltob80f6442004-07-27 13:29:18 +00001946 new_line = history_expand_line_internal (rl_line_buffer);
Jari Aalto726f6381996-08-26 18:22:31 +00001947
1948#if defined (ALIAS)
1949 if (new_line)
1950 {
1951 char *alias_line;
1952
1953 alias_line = alias_expand (new_line);
1954 free (new_line);
1955 new_line = alias_line;
1956 }
1957#endif /* ALIAS */
1958
1959 if (new_line)
1960 {
1961 int old_point = rl_point;
1962 int at_end = rl_point == rl_end;
1963
1964 /* If the line was history and alias expanded, then make that
1965 be one thing to undo. */
1966 maybe_make_readline_line (new_line);
1967 free (new_line);
1968
1969 /* If there is variable expansion to perform, do that as a separate
1970 operation to be undone. */
Jari Aaltod166f041997-06-05 14:59:13 +00001971 new_line = savestring (rl_line_buffer);
1972 expanded_string = expand_string (new_line, 0);
1973 FREE (new_line);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001974 if (expanded_string == 0)
1975 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001976 new_line = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001977 new_line[0] = '\0';
1978 }
1979 else
1980 {
1981 new_line = string_list (expanded_string);
1982 dispose_words (expanded_string);
1983 }
Jari Aalto726f6381996-08-26 18:22:31 +00001984
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001985 maybe_make_readline_line (new_line);
1986 free (new_line);
Jari Aalto726f6381996-08-26 18:22:31 +00001987
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001988 /* Place rl_point where we think it should go. */
1989 if (at_end)
1990 rl_point = rl_end;
1991 else if (old_point < rl_end)
1992 {
1993 rl_point = old_point;
1994 if (!whitespace (rl_line_buffer[rl_point]))
Jari Aaltob72432f1999-02-19 17:11:39 +00001995 rl_forward_word (1, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001996 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00001997 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001998 }
1999 else
Jari Aalto28ef6c32001-04-06 19:14:31 +00002000 {
2001 cleanup_expansion_error ();
2002 return 1;
2003 }
Jari Aalto726f6381996-08-26 18:22:31 +00002004}
2005
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002006/* If FIGNORE is set, then don't match files with the given suffixes when
2007 completing filenames. If only one of the possibilities has an acceptable
Jari Aalto726f6381996-08-26 18:22:31 +00002008 suffix, delete the others, else just return and let the completer
2009 signal an error. It is called by the completer when real
2010 completions are done on filenames by the completer's internal
2011 function, not for completion lists (M-?) and not on "other"
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002012 completion types, such as hostnames or commands. */
Jari Aalto726f6381996-08-26 18:22:31 +00002013
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002014static struct ignorevar fignore =
2015{
2016 "FIGNORE",
2017 (struct ign *)0,
2018 0,
2019 (char *)0,
Jari Aaltof73dda02001-11-13 17:56:06 +00002020 (sh_iv_item_func_t *) 0,
Jari Aalto726f6381996-08-26 18:22:31 +00002021};
2022
Jari Aalto726f6381996-08-26 18:22:31 +00002023static void
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002024_ignore_completion_names (names, name_func)
Jari Aalto726f6381996-08-26 18:22:31 +00002025 char **names;
Jari Aaltof73dda02001-11-13 17:56:06 +00002026 sh_ignore_func_t *name_func;
Jari Aalto726f6381996-08-26 18:22:31 +00002027{
2028 char **newnames;
2029 int idx, nidx;
Jari Aaltocce855b1998-04-17 19:52:44 +00002030 char **oldnames;
2031 int oidx;
Jari Aalto726f6381996-08-26 18:22:31 +00002032
2033 /* If there is only one completion, see if it is acceptable. If it is
2034 not, free it up. In any case, short-circuit and return. This is a
2035 special case because names[0] is not the prefix of the list of names
2036 if there is only one completion; it is the completion itself. */
2037 if (names[1] == (char *)0)
2038 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002039 if (force_fignore)
2040 if ((*name_func) (names[0]) == 0)
2041 {
2042 free (names[0]);
2043 names[0] = (char *)NULL;
2044 }
2045
Jari Aalto726f6381996-08-26 18:22:31 +00002046 return;
2047 }
2048
2049 /* Allocate space for array to hold list of pointers to matching
2050 filenames. The pointers are copied back to NAMES when done. */
2051 for (nidx = 1; names[nidx]; nidx++)
2052 ;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002053 newnames = strvec_create (nidx + 1);
Jari Aaltob80f6442004-07-27 13:29:18 +00002054
2055 if (force_fignore == 0)
2056 {
2057 oldnames = strvec_create (nidx - 1);
2058 oidx = 0;
2059 }
Jari Aalto726f6381996-08-26 18:22:31 +00002060
2061 newnames[0] = names[0];
2062 for (idx = nidx = 1; names[idx]; idx++)
2063 {
2064 if ((*name_func) (names[idx]))
2065 newnames[nidx++] = names[idx];
Jari Aaltob80f6442004-07-27 13:29:18 +00002066 else if (force_fignore == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00002067 oldnames[oidx++] = names[idx];
Jari Aaltob80f6442004-07-27 13:29:18 +00002068 else
2069 free (names[idx]);
Jari Aalto726f6381996-08-26 18:22:31 +00002070 }
2071
2072 newnames[nidx] = (char *)NULL;
2073
2074 /* If none are acceptable then let the completer handle it. */
2075 if (nidx == 1)
2076 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002077 if (force_fignore)
2078 {
2079 free (names[0]);
2080 names[0] = (char *)NULL;
2081 }
2082 else
2083 free (oldnames);
2084
Jari Aalto726f6381996-08-26 18:22:31 +00002085 free (newnames);
2086 return;
2087 }
2088
Jari Aaltob80f6442004-07-27 13:29:18 +00002089 if (force_fignore == 0)
2090 {
2091 while (oidx)
2092 free (oldnames[--oidx]);
2093 free (oldnames);
2094 }
Jari Aaltocce855b1998-04-17 19:52:44 +00002095
Jari Aalto726f6381996-08-26 18:22:31 +00002096 /* If only one is acceptable, copy it to names[0] and return. */
2097 if (nidx == 2)
2098 {
2099 free (names[0]);
2100 names[0] = newnames[1];
2101 names[1] = (char *)NULL;
2102 free (newnames);
2103 return;
2104 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002105
Jari Aalto726f6381996-08-26 18:22:31 +00002106 /* Copy the acceptable names back to NAMES, set the new array end,
2107 and return. */
2108 for (nidx = 1; newnames[nidx]; nidx++)
2109 names[nidx] = newnames[nidx];
2110 names[nidx] = (char *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002111 free (newnames);
2112}
2113
2114static int
2115name_is_acceptable (name)
Jari Aaltof73dda02001-11-13 17:56:06 +00002116 const char *name;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002117{
2118 struct ign *p;
2119 int nlen;
2120
2121 for (nlen = strlen (name), p = fignore.ignores; p->val; p++)
2122 {
2123 if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len]))
2124 return (0);
2125 }
2126
2127 return (1);
Jari Aalto726f6381996-08-26 18:22:31 +00002128}
2129
Jari Aaltob72432f1999-02-19 17:11:39 +00002130#if 0
2131static int
2132ignore_dot_names (name)
2133 char *name;
2134{
2135 return (name[0] != '.');
2136}
2137#endif
2138
Jari Aalto28ef6c32001-04-06 19:14:31 +00002139static int
Jari Aalto726f6381996-08-26 18:22:31 +00002140filename_completion_ignore (names)
2141 char **names;
2142{
Jari Aaltob72432f1999-02-19 17:11:39 +00002143#if 0
2144 if (glob_dot_filenames == 0)
2145 _ignore_completion_names (names, ignore_dot_names);
2146#endif
2147
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002148 setup_ignore_patterns (&fignore);
Jari Aalto726f6381996-08-26 18:22:31 +00002149
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002150 if (fignore.num_ignores == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002151 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002152
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002153 _ignore_completion_names (names, name_is_acceptable);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002154
2155 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002156}
2157
2158/* Return 1 if NAME is a directory. */
2159static int
2160test_for_directory (name)
Jari Aaltof73dda02001-11-13 17:56:06 +00002161 const char *name;
Jari Aalto726f6381996-08-26 18:22:31 +00002162{
2163 struct stat finfo;
2164 char *fn;
2165
Jari Aalto7117c2d2002-07-17 14:10:11 +00002166 fn = bash_tilde_expand (name, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00002167 if (stat (fn, &finfo) != 0)
2168 {
2169 free (fn);
2170 return 0;
2171 }
2172 free (fn);
2173 return (S_ISDIR (finfo.st_mode));
2174}
2175
2176/* Remove files from NAMES, leaving directories. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00002177static int
Jari Aalto726f6381996-08-26 18:22:31 +00002178bash_ignore_filenames (names)
2179 char **names;
2180{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002181 _ignore_completion_names (names, test_for_directory);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002182 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002183}
2184
Jari Aaltobb706242000-03-17 21:46:59 +00002185static int
2186return_zero (name)
Jari Aaltof73dda02001-11-13 17:56:06 +00002187 const char *name;
Jari Aaltobb706242000-03-17 21:46:59 +00002188{
2189 return 0;
2190}
2191
Jari Aalto28ef6c32001-04-06 19:14:31 +00002192static int
Jari Aaltobb706242000-03-17 21:46:59 +00002193bash_ignore_everything (names)
2194 char **names;
2195{
2196 _ignore_completion_names (names, return_zero);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002197 return 0;
Jari Aaltobb706242000-03-17 21:46:59 +00002198}
2199
Jari Aaltoeb873672004-11-09 21:37:25 +00002200/* Simulate the expansions that will be performed by
2201 rl_filename_completion_function. This must be called with the address of
2202 a pointer to malloc'd memory. */
2203static int
2204bash_directory_expansion (dirname)
2205 char **dirname;
2206{
2207 char *d;
2208
2209 d = savestring (*dirname);
2210
2211 if (rl_directory_rewrite_hook)
2212 (*rl_directory_rewrite_hook) (&d);
2213
2214 if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&d))
2215 {
2216 free (*dirname);
2217 *dirname = d;
2218 }
2219}
2220
Jari Aalto726f6381996-08-26 18:22:31 +00002221/* Handle symbolic link references and other directory name
2222 expansions while hacking completion. */
2223static int
2224bash_directory_completion_hook (dirname)
2225 char **dirname;
2226{
Jari Aaltob72432f1999-02-19 17:11:39 +00002227 char *local_dirname, *new_dirname, *t;
Jari Aaltobb706242000-03-17 21:46:59 +00002228 int return_value, should_expand_dirname;
Jari Aalto726f6381996-08-26 18:22:31 +00002229 WORD_LIST *wl;
Jari Aaltob80f6442004-07-27 13:29:18 +00002230 struct stat sb;
Jari Aalto726f6381996-08-26 18:22:31 +00002231
Jari Aaltobb706242000-03-17 21:46:59 +00002232 return_value = should_expand_dirname = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002233 local_dirname = *dirname;
Jari Aaltobb706242000-03-17 21:46:59 +00002234
2235#if 0
Jari Aalto7117c2d2002-07-17 14:10:11 +00002236 should_expand_dirname = xstrchr (local_dirname, '$') || xstrchr (local_dirname, '`');
Jari Aaltobb706242000-03-17 21:46:59 +00002237#else
Jari Aalto7117c2d2002-07-17 14:10:11 +00002238 if (xstrchr (local_dirname, '$'))
Jari Aaltobb706242000-03-17 21:46:59 +00002239 should_expand_dirname = 1;
2240 else
Jari Aalto726f6381996-08-26 18:22:31 +00002241 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00002242 t = xstrchr (local_dirname, '`');
Jari Aaltobb706242000-03-17 21:46:59 +00002243 if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0)
2244 should_expand_dirname = 1;
2245 }
2246#endif
2247
Jari Aaltob80f6442004-07-27 13:29:18 +00002248#if defined (HAVE_LSTAT)
2249 if (should_expand_dirname && lstat (local_dirname, &sb) == 0)
2250#else
2251 if (should_expand_dirname && stat (local_dirname, &sb) == 0)
2252#endif
2253 should_expand_dirname = 0;
2254
Jari Aaltobb706242000-03-17 21:46:59 +00002255 if (should_expand_dirname)
2256 {
2257 new_dirname = savestring (local_dirname);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002258 wl = expand_prompt_string (new_dirname, 0); /* does the right thing */
Jari Aalto726f6381996-08-26 18:22:31 +00002259 if (wl)
2260 {
2261 *dirname = string_list (wl);
2262 /* Tell the completer to replace the directory name only if we
2263 actually expanded something. */
2264 return_value = STREQ (local_dirname, *dirname) == 0;
2265 free (local_dirname);
Jari Aaltob72432f1999-02-19 17:11:39 +00002266 free (new_dirname);
Jari Aalto726f6381996-08-26 18:22:31 +00002267 dispose_words (wl);
2268 local_dirname = *dirname;
2269 }
2270 else
2271 {
Jari Aaltob72432f1999-02-19 17:11:39 +00002272 free (new_dirname);
Jari Aalto726f6381996-08-26 18:22:31 +00002273 free (local_dirname);
Jari Aaltof73dda02001-11-13 17:56:06 +00002274 *dirname = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002275 **dirname = '\0';
Jari Aalto726f6381996-08-26 18:22:31 +00002276 return 1;
2277 }
2278 }
2279
2280 if (!no_symbolic_links && (local_dirname[0] != '.' || local_dirname[1]))
2281 {
2282 char *temp1, *temp2;
2283 int len1, len2;
2284
2285 t = get_working_directory ("symlink-hook");
2286 temp1 = make_absolute (local_dirname, t);
2287 free (t);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002288 temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002289 /* If we can't canonicalize, bail. */
2290 if (temp2 == 0)
2291 {
2292 free (temp1);
2293 return 1;
2294 }
Jari Aalto726f6381996-08-26 18:22:31 +00002295 len1 = strlen (temp1);
2296 if (temp1[len1 - 1] == '/')
Jari Aalto28ef6c32001-04-06 19:14:31 +00002297 {
Jari Aalto726f6381996-08-26 18:22:31 +00002298 len2 = strlen (temp2);
Jari Aaltof73dda02001-11-13 17:56:06 +00002299 temp2 = (char *)xrealloc (temp2, len2 + 2);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002300 temp2[len2] = '/';
2301 temp2[len2 + 1] = '\0';
2302 }
Jari Aalto726f6381996-08-26 18:22:31 +00002303 free (local_dirname);
2304 *dirname = temp2;
2305 free (temp1);
2306 }
2307 return (return_value);
2308}
2309
Jari Aalto726f6381996-08-26 18:22:31 +00002310static char **history_completion_array = (char **)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002311static int harry_size;
2312static int harry_len;
Jari Aalto726f6381996-08-26 18:22:31 +00002313
2314static void
2315build_history_completion_array ()
2316{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002317 register int i, j;
2318 HIST_ENTRY **hlist;
2319 char **tokens;
Jari Aalto726f6381996-08-26 18:22:31 +00002320
2321 /* First, clear out the current dynamic history completion list. */
2322 if (harry_size)
2323 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00002324 strvec_dispose (history_completion_array);
Jari Aalto726f6381996-08-26 18:22:31 +00002325 history_completion_array = (char **)NULL;
2326 harry_size = 0;
2327 harry_len = 0;
2328 }
2329
2330 /* Next, grovel each line of history, making each shell-sized token
2331 a separate entry in the history_completion_array. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002332 hlist = history_list ();
Jari Aalto726f6381996-08-26 18:22:31 +00002333
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002334 if (hlist)
2335 {
2336 for (i = 0; hlist[i]; i++)
2337 {
2338 /* Separate each token, and place into an array. */
2339 tokens = history_tokenize (hlist[i]->line);
Jari Aalto726f6381996-08-26 18:22:31 +00002340
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002341 for (j = 0; tokens && tokens[j]; j++)
2342 {
2343 if (harry_len + 2 > harry_size)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002344 history_completion_array = strvec_resize (history_completion_array, harry_size += 10);
Jari Aalto726f6381996-08-26 18:22:31 +00002345
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002346 history_completion_array[harry_len++] = tokens[j];
2347 history_completion_array[harry_len] = (char *)NULL;
2348 }
2349 free (tokens);
2350 }
Jari Aalto726f6381996-08-26 18:22:31 +00002351
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002352 /* Sort the complete list of tokens. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002353 qsort (history_completion_array, harry_len, sizeof (char *), (QSFUNC *)strvec_strcmp);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002354 }
Jari Aalto726f6381996-08-26 18:22:31 +00002355}
2356
2357static char *
2358history_completion_generator (hint_text, state)
Jari Aaltof73dda02001-11-13 17:56:06 +00002359 const char *hint_text;
Jari Aalto726f6381996-08-26 18:22:31 +00002360 int state;
2361{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002362 static int local_index, len;
Jari Aaltof73dda02001-11-13 17:56:06 +00002363 static const char *text;
Jari Aalto726f6381996-08-26 18:22:31 +00002364
2365 /* If this is the first call to the generator, then initialize the
2366 list of strings to complete over. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002367 if (state == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002368 {
2369 local_index = 0;
2370 build_history_completion_array ();
2371 text = hint_text;
2372 len = strlen (text);
2373 }
2374
2375 while (history_completion_array && history_completion_array[local_index])
2376 {
2377 if (strncmp (text, history_completion_array[local_index++], len) == 0)
2378 return (savestring (history_completion_array[local_index - 1]));
2379 }
2380 return ((char *)NULL);
2381}
2382
Jari Aalto28ef6c32001-04-06 19:14:31 +00002383static int
Jari Aalto726f6381996-08-26 18:22:31 +00002384dynamic_complete_history (count, key)
2385 int count, key;
2386{
Jari Aaltof73dda02001-11-13 17:56:06 +00002387 int r;
2388
Jari Aalto28ef6c32001-04-06 19:14:31 +00002389 rl_compentry_func_t *orig_func;
2390 rl_completion_func_t *orig_attempt_func;
Jari Aalto726f6381996-08-26 18:22:31 +00002391
2392 orig_func = rl_completion_entry_function;
2393 orig_attempt_func = rl_attempted_completion_function;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002394 rl_completion_entry_function = history_completion_generator;
2395 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00002396
Jari Aalto7117c2d2002-07-17 14:10:11 +00002397 /* XXX - use rl_completion_mode here? */
Jari Aalto28ef6c32001-04-06 19:14:31 +00002398 if (rl_last_func == dynamic_complete_history)
Jari Aaltof73dda02001-11-13 17:56:06 +00002399 r = rl_complete_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002400 else
Jari Aaltof73dda02001-11-13 17:56:06 +00002401 r = rl_complete_internal (TAB);
Jari Aalto726f6381996-08-26 18:22:31 +00002402
2403 rl_completion_entry_function = orig_func;
2404 rl_attempted_completion_function = orig_attempt_func;
Jari Aaltof73dda02001-11-13 17:56:06 +00002405 return r;
Jari Aalto726f6381996-08-26 18:22:31 +00002406}
2407
Jari Aalto726f6381996-08-26 18:22:31 +00002408#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002409static int
Jari Aalto726f6381996-08-26 18:22:31 +00002410bash_complete_username (ignore, ignore2)
2411 int ignore, ignore2;
2412{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002413 return bash_complete_username_internal (rl_completion_mode (bash_complete_username));
Jari Aalto726f6381996-08-26 18:22:31 +00002414}
2415
Jari Aalto28ef6c32001-04-06 19:14:31 +00002416static int
Jari Aalto726f6381996-08-26 18:22:31 +00002417bash_possible_username_completions (ignore, ignore2)
2418 int ignore, ignore2;
2419{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002420 return bash_complete_username_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002421}
2422
Jari Aalto28ef6c32001-04-06 19:14:31 +00002423static int
Jari Aalto726f6381996-08-26 18:22:31 +00002424bash_complete_username_internal (what_to_do)
2425 int what_to_do;
2426{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002427 return bash_specific_completion (what_to_do, rl_username_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00002428}
2429
Jari Aalto28ef6c32001-04-06 19:14:31 +00002430static int
Jari Aalto726f6381996-08-26 18:22:31 +00002431bash_complete_filename (ignore, ignore2)
2432 int ignore, ignore2;
2433{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002434 return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename));
Jari Aalto726f6381996-08-26 18:22:31 +00002435}
2436
Jari Aalto28ef6c32001-04-06 19:14:31 +00002437static int
Jari Aalto726f6381996-08-26 18:22:31 +00002438bash_possible_filename_completions (ignore, ignore2)
2439 int ignore, ignore2;
2440{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002441 return bash_complete_filename_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002442}
2443
Jari Aalto28ef6c32001-04-06 19:14:31 +00002444static int
Jari Aalto726f6381996-08-26 18:22:31 +00002445bash_complete_filename_internal (what_to_do)
2446 int what_to_do;
2447{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002448 rl_compentry_func_t *orig_func;
2449 rl_completion_func_t *orig_attempt_func;
2450 rl_icppfunc_t *orig_dir_func;
Jari Aaltob80f6442004-07-27 13:29:18 +00002451 /*const*/ char *orig_rl_completer_word_break_characters;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002452 int r;
Jari Aalto726f6381996-08-26 18:22:31 +00002453
2454 orig_func = rl_completion_entry_function;
2455 orig_attempt_func = rl_attempted_completion_function;
2456 orig_dir_func = rl_directory_completion_hook;
2457 orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002458 rl_completion_entry_function = rl_filename_completion_function;
2459 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
2460 rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00002461 rl_completer_word_break_characters = " \t\n\"\'";
2462
Jari Aalto28ef6c32001-04-06 19:14:31 +00002463 r = rl_complete_internal (what_to_do);
Jari Aalto726f6381996-08-26 18:22:31 +00002464
2465 rl_completion_entry_function = orig_func;
2466 rl_attempted_completion_function = orig_attempt_func;
2467 rl_directory_completion_hook = orig_dir_func;
2468 rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002469
2470 return r;
Jari Aalto726f6381996-08-26 18:22:31 +00002471}
2472
Jari Aalto28ef6c32001-04-06 19:14:31 +00002473static int
Jari Aalto726f6381996-08-26 18:22:31 +00002474bash_complete_hostname (ignore, ignore2)
2475 int ignore, ignore2;
2476{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002477 return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname));
Jari Aalto726f6381996-08-26 18:22:31 +00002478}
2479
Jari Aalto28ef6c32001-04-06 19:14:31 +00002480static int
Jari Aalto726f6381996-08-26 18:22:31 +00002481bash_possible_hostname_completions (ignore, ignore2)
2482 int ignore, ignore2;
2483{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002484 return bash_complete_hostname_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002485}
2486
Jari Aalto28ef6c32001-04-06 19:14:31 +00002487static int
Jari Aalto726f6381996-08-26 18:22:31 +00002488bash_complete_variable (ignore, ignore2)
2489 int ignore, ignore2;
2490{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002491 return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable));
Jari Aalto726f6381996-08-26 18:22:31 +00002492}
2493
Jari Aalto28ef6c32001-04-06 19:14:31 +00002494static int
Jari Aalto726f6381996-08-26 18:22:31 +00002495bash_possible_variable_completions (ignore, ignore2)
2496 int ignore, ignore2;
2497{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002498 return bash_complete_variable_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002499}
2500
Jari Aalto28ef6c32001-04-06 19:14:31 +00002501static int
Jari Aalto726f6381996-08-26 18:22:31 +00002502bash_complete_command (ignore, ignore2)
2503 int ignore, ignore2;
2504{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002505 return bash_complete_command_internal (rl_completion_mode (bash_complete_command));
Jari Aalto726f6381996-08-26 18:22:31 +00002506}
2507
Jari Aalto28ef6c32001-04-06 19:14:31 +00002508static int
Jari Aalto726f6381996-08-26 18:22:31 +00002509bash_possible_command_completions (ignore, ignore2)
2510 int ignore, ignore2;
2511{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002512 return bash_complete_command_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002513}
2514
Jari Aalto28ef6c32001-04-06 19:14:31 +00002515static int
Jari Aalto726f6381996-08-26 18:22:31 +00002516bash_complete_hostname_internal (what_to_do)
2517 int what_to_do;
2518{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002519 return bash_specific_completion (what_to_do, hostname_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00002520}
2521
Jari Aalto28ef6c32001-04-06 19:14:31 +00002522static int
Jari Aalto726f6381996-08-26 18:22:31 +00002523bash_complete_variable_internal (what_to_do)
2524 int what_to_do;
2525{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002526 return bash_specific_completion (what_to_do, variable_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00002527}
2528
Jari Aalto28ef6c32001-04-06 19:14:31 +00002529static int
Jari Aalto726f6381996-08-26 18:22:31 +00002530bash_complete_command_internal (what_to_do)
2531 int what_to_do;
2532{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002533 return bash_specific_completion (what_to_do, command_word_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00002534}
2535
Jari Aalto7117c2d2002-07-17 14:10:11 +00002536static char *globtext;
2537static char *globorig;
2538
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002539static char *
2540glob_complete_word (text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002541 const char *text;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002542 int state;
2543{
2544 static char **matches = (char **)NULL;
2545 static int ind;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002546 int glen;
Jari Aaltoeb873672004-11-09 21:37:25 +00002547 char *ret, *ttext;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002548
2549 if (state == 0)
2550 {
Jari Aaltoe8ce7751997-09-22 20:22:27 +00002551 rl_filename_completion_desired = 1;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002552 FREE (matches);
2553 if (globorig != globtext)
2554 FREE (globorig);
2555 FREE (globtext);
2556
Jari Aaltoeb873672004-11-09 21:37:25 +00002557 ttext = bash_tilde_expand (text, 0);
2558
Jari Aalto7117c2d2002-07-17 14:10:11 +00002559 if (rl_explicit_arg)
2560 {
Jari Aaltoeb873672004-11-09 21:37:25 +00002561 globorig = savestring (ttext);
2562 glen = strlen (ttext);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002563 globtext = (char *)xmalloc (glen + 2);
Jari Aaltoeb873672004-11-09 21:37:25 +00002564 strcpy (globtext, ttext);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002565 globtext[glen] = '*';
2566 globtext[glen+1] = '\0';
2567 }
2568 else
Jari Aaltoeb873672004-11-09 21:37:25 +00002569 globtext = globorig = savestring (ttext);
2570
2571 if (ttext != text)
2572 free (ttext);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002573
2574 matches = shell_glob_filename (globtext);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002575 if (GLOB_FAILED (matches))
Jari Aalto28ef6c32001-04-06 19:14:31 +00002576 matches = (char **)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002577 ind = 0;
2578 }
2579
2580 ret = matches ? matches[ind] : (char *)NULL;
2581 ind++;
2582 return ret;
2583}
2584
Jari Aalto28ef6c32001-04-06 19:14:31 +00002585static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002586bash_glob_completion_internal (what_to_do)
2587 int what_to_do;
2588{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002589 return bash_specific_completion (what_to_do, glob_complete_word);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002590}
2591
Jari Aalto7117c2d2002-07-17 14:10:11 +00002592/* A special quoting function so we don't end up quoting globbing characters
2593 in the word if there are no matches or multiple matches. */
2594static char *
2595bash_glob_quote_filename (s, rtype, qcp)
2596 char *s;
2597 int rtype;
2598 char *qcp;
2599{
2600 if (globorig && qcp && *qcp == '\0' && STREQ (s, globorig))
2601 return (savestring (s));
2602 else
2603 return (bash_quote_filename (s, rtype, qcp));
2604}
2605
2606static int
2607bash_glob_complete_word (count, key)
2608 int count, key;
2609{
2610 int r;
2611 rl_quote_func_t *orig_quoting_function;
2612
Jari Aaltob80f6442004-07-27 13:29:18 +00002613 if (rl_editing_mode == EMACS_EDITING_MODE)
2614 rl_explicit_arg = 1; /* force `*' append */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002615 orig_quoting_function = rl_filename_quoting_function;
2616 rl_filename_quoting_function = bash_glob_quote_filename;
2617
2618 r = bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word));
2619
2620 rl_filename_quoting_function = orig_quoting_function;
2621 return r;
2622}
2623
Jari Aalto28ef6c32001-04-06 19:14:31 +00002624static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002625bash_glob_expand_word (count, key)
2626 int count, key;
2627{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002628 return bash_glob_completion_internal ('*');
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002629}
2630
Jari Aalto28ef6c32001-04-06 19:14:31 +00002631static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002632bash_glob_list_expansions (count, key)
2633 int count, key;
2634{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002635 return bash_glob_completion_internal ('?');
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002636}
2637
Jari Aalto28ef6c32001-04-06 19:14:31 +00002638static int
Jari Aalto726f6381996-08-26 18:22:31 +00002639bash_specific_completion (what_to_do, generator)
2640 int what_to_do;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002641 rl_compentry_func_t *generator;
Jari Aalto726f6381996-08-26 18:22:31 +00002642{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002643 rl_compentry_func_t *orig_func;
2644 rl_completion_func_t *orig_attempt_func;
2645 int r;
Jari Aalto726f6381996-08-26 18:22:31 +00002646
2647 orig_func = rl_completion_entry_function;
2648 orig_attempt_func = rl_attempted_completion_function;
2649 rl_completion_entry_function = generator;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002650 rl_attempted_completion_function = NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00002651
Jari Aalto28ef6c32001-04-06 19:14:31 +00002652 r = rl_complete_internal (what_to_do);
Jari Aalto726f6381996-08-26 18:22:31 +00002653
2654 rl_completion_entry_function = orig_func;
2655 rl_attempted_completion_function = orig_attempt_func;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002656
2657 return r;
Jari Aalto726f6381996-08-26 18:22:31 +00002658}
2659
2660#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002661
Jari Aaltob80f6442004-07-27 13:29:18 +00002662#if defined (VI_MODE)
2663/* Completion, from vi mode's point of view. This is a modified version of
2664 rl_vi_complete which uses the bash globbing code to implement what POSIX
2665 specifies, which is to append a `*' and attempt filename generation (which
2666 has the side effect of expanding any globbing characters in the word). */
2667static int
2668bash_vi_complete (count, key)
2669 int count, key;
2670{
2671#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
2672 int p, r;
2673 char *t;
2674
2675 if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
2676 {
2677 if (!whitespace (rl_line_buffer[rl_point + 1]))
2678 rl_vi_end_word (1, 'E');
2679 rl_point++;
2680 }
2681
2682 /* Find boundaries of current word, according to vi definition of a
2683 `bigword'. */
2684 t = 0;
2685 if (rl_point > 0)
2686 {
2687 p = rl_point;
2688 rl_vi_bWord (1, 'B');
2689 r = rl_point;
2690 rl_point = p;
2691 p = r;
2692
2693 t = substring (rl_line_buffer, p, rl_point);
2694 }
2695
2696 if (t && glob_pattern_p (t) == 0)
2697 rl_explicit_arg = 1; /* XXX - force glob_complete_word to append `*' */
2698 FREE (t);
2699
2700 if (key == '*') /* Expansion and replacement. */
2701 r = bash_glob_expand_word (count, key);
2702 else if (key == '=') /* List possible completions. */
2703 r = bash_glob_list_expansions (count, key);
2704 else if (key == '\\') /* Standard completion */
2705 r = bash_glob_complete_word (count, key);
2706 else
2707 r = rl_complete (0, key);
2708
2709 if (key == '*' || key == '\\')
2710 rl_vi_start_inserting (key, 1, 1);
2711
2712 return (r);
2713#else
2714 return rl_vi_complete (count, key);
2715#endif /* !SPECIFIC_COMPLETION_FUNCTIONS */
2716}
2717#endif /* VI_MODE */
2718
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002719/* Filename quoting for completion. */
Jari Aaltobb706242000-03-17 21:46:59 +00002720/* A function to strip unquoted quote characters (single quotes, double
2721 quotes, and backslashes). It allows single quotes to appear
2722 within double quotes, and vice versa. It should be smarter. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002723static char *
2724bash_dequote_filename (text, quote_char)
2725 char *text;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002726 int quote_char;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002727{
2728 char *ret, *p, *r;
2729 int l, quoted;
2730
2731 l = strlen (text);
Jari Aaltof73dda02001-11-13 17:56:06 +00002732 ret = (char *)xmalloc (l + 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002733 for (quoted = quote_char, p = text, r = ret; p && *p; p++)
2734 {
2735 /* Allow backslash-quoted characters to pass through unscathed. */
2736 if (*p == '\\')
2737 {
2738 *r++ = *++p;
2739 if (*p == '\0')
2740 break;
2741 continue;
2742 }
2743 /* Close quote. */
2744 if (quoted && *p == quoted)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002745 {
2746 quoted = 0;
2747 continue;
2748 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002749 /* Open quote. */
2750 if (quoted == 0 && (*p == '\'' || *p == '"'))
Jari Aalto28ef6c32001-04-06 19:14:31 +00002751 {
2752 quoted = *p;
2753 continue;
2754 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002755 *r++ = *p;
2756 }
2757 *r = '\0';
2758 return ret;
2759}
2760
Jari Aaltod166f041997-06-05 14:59:13 +00002761/* Quote characters that the readline completion code would treat as
2762 word break characters with backslashes. Pass backslash-quoted
2763 characters through without examination. */
2764static char *
2765quote_word_break_chars (text)
2766 char *text;
2767{
2768 char *ret, *r, *s;
2769 int l;
2770
2771 l = strlen (text);
Jari Aaltof73dda02001-11-13 17:56:06 +00002772 ret = (char *)xmalloc ((2 * l) + 1);
Jari Aaltod166f041997-06-05 14:59:13 +00002773 for (s = text, r = ret; *s; s++)
2774 {
2775 /* Pass backslash-quoted characters through, including the backslash. */
2776 if (*s == '\\')
2777 {
2778 *r++ = '\\';
2779 *r++ = *++s;
2780 if (*s == '\0')
2781 break;
2782 continue;
2783 }
2784 /* OK, we have an unquoted character. Check its presence in
2785 rl_completer_word_break_characters. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002786 if (xstrchr (rl_completer_word_break_characters, *s))
Jari Aalto28ef6c32001-04-06 19:14:31 +00002787 *r++ = '\\';
Jari Aaltod166f041997-06-05 14:59:13 +00002788 *r++ = *s;
2789 }
2790 *r = '\0';
2791 return ret;
2792}
2793
2794/* Quote a filename using double quotes, single quotes, or backslashes
2795 depending on the value of completion_quoting_style. If we're
2796 completing using backslashes, we need to quote some additional
2797 characters (those that readline treats as word breaks), so we call
Jari Aalto7117c2d2002-07-17 14:10:11 +00002798 quote_word_break_chars on the result. This returns newly-allocated
2799 memory. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002800static char *
2801bash_quote_filename (s, rtype, qcp)
2802 char *s;
2803 int rtype;
2804 char *qcp;
2805{
2806 char *rtext, *mtext, *ret;
2807 int rlen, cs;
2808
2809 rtext = (char *)NULL;
2810
2811 /* If RTYPE == MULT_MATCH, it means that there is
2812 more than one match. In this case, we do not add
2813 the closing quote or attempt to perform tilde
2814 expansion. If RTYPE == SINGLE_MATCH, we try
2815 to perform tilde expansion, because single and double
2816 quotes inhibit tilde expansion by the shell. */
2817
2818 mtext = s;
2819 if (mtext[0] == '~' && rtype == SINGLE_MATCH)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002820 mtext = bash_tilde_expand (s, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002821
2822 cs = completion_quoting_style;
2823 /* Might need to modify the default completion style based on *qcp,
Jari Aaltobb706242000-03-17 21:46:59 +00002824 since it's set to any user-provided opening quote. We also change
2825 to single-quoting if there is no user-provided opening quote and
2826 the word being completed contains newlines, since those are not
2827 quoted correctly using backslashes (a backslash-newline pair is
2828 special to the shell parser). */
Jari Aalto7117c2d2002-07-17 14:10:11 +00002829 if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && xstrchr (mtext, '\n'))
Jari Aaltobb706242000-03-17 21:46:59 +00002830 cs = COMPLETE_SQUOTE;
2831 else if (*qcp == '"')
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002832 cs = COMPLETE_DQUOTE;
2833 else if (*qcp == '\'')
2834 cs = COMPLETE_SQUOTE;
2835#if defined (BANG_HISTORY)
2836 else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE &&
Jari Aalto7117c2d2002-07-17 14:10:11 +00002837 history_expansion_inhibited == 0 && xstrchr (mtext, '!'))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002838 cs = COMPLETE_BSQUOTE;
Jari Aaltod166f041997-06-05 14:59:13 +00002839
2840 if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE &&
Jari Aalto7117c2d2002-07-17 14:10:11 +00002841 history_expansion_inhibited == 0 && xstrchr (mtext, '!'))
Jari Aaltod166f041997-06-05 14:59:13 +00002842 {
2843 cs = COMPLETE_BSQUOTE;
2844 *qcp = '\0';
2845 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002846#endif
2847
2848 switch (cs)
2849 {
2850 case COMPLETE_DQUOTE:
Jari Aalto28ef6c32001-04-06 19:14:31 +00002851 rtext = sh_double_quote (mtext);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002852 break;
2853 case COMPLETE_SQUOTE:
Jari Aalto28ef6c32001-04-06 19:14:31 +00002854 rtext = sh_single_quote (mtext);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002855 break;
2856 case COMPLETE_BSQUOTE:
Jari Aalto28ef6c32001-04-06 19:14:31 +00002857 rtext = sh_backslash_quote (mtext);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002858 break;
2859 }
2860
2861 if (mtext != s)
2862 free (mtext);
2863
Jari Aaltod166f041997-06-05 14:59:13 +00002864 /* We may need to quote additional characters: those that readline treats
2865 as word breaks that are not quoted by backslash_quote. */
2866 if (rtext && cs == COMPLETE_BSQUOTE)
2867 {
2868 mtext = quote_word_break_chars (rtext);
2869 free (rtext);
2870 rtext = mtext;
2871 }
2872
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002873 /* Leave the opening quote intact. The readline completion code takes
2874 care of avoiding doubled opening quotes. */
2875 rlen = strlen (rtext);
Jari Aaltof73dda02001-11-13 17:56:06 +00002876 ret = (char *)xmalloc (rlen + 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002877 strcpy (ret, rtext);
2878
2879 /* If there are multiple matches, cut off the closing quote. */
2880 if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE)
2881 ret[rlen - 1] = '\0';
2882 free (rtext);
2883 return ret;
2884}
2885
Jari Aaltobb706242000-03-17 21:46:59 +00002886/* Support for binding readline key sequences to Unix commands. */
2887static Keymap cmd_xmap;
2888
2889static int
2890bash_execute_unix_command (count, key)
2891 int count; /* ignored */
2892 int key;
2893{
2894 Keymap ckmap; /* current keymap */
2895 Keymap xkmap; /* unix command executing keymap */
2896 register int i;
2897 char *cmd;
Jari Aaltob80f6442004-07-27 13:29:18 +00002898 sh_parser_state_t ps;
Jari Aaltobb706242000-03-17 21:46:59 +00002899
2900 /* First, we need to find the right command to execute. This is tricky,
2901 because we might have already indirected into another keymap. */
2902 ckmap = rl_get_keymap ();
2903 if (ckmap != rl_executing_keymap)
2904 {
2905 /* bogus. we have to search. only handle one level of indirection. */
2906 for (i = 0; i < KEYMAP_SIZE; i++)
2907 {
2908 if (ckmap[i].type == ISKMAP && (Keymap)ckmap[i].function == rl_executing_keymap)
2909 break;
2910 }
2911 if (i < KEYMAP_SIZE)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002912 xkmap = (Keymap)cmd_xmap[i].function;
Jari Aaltobb706242000-03-17 21:46:59 +00002913 else
2914 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00002915 rl_crlf ();
Jari Aaltob80f6442004-07-27 13:29:18 +00002916 internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
Jari Aalto28ef6c32001-04-06 19:14:31 +00002917 rl_forced_update_display ();
2918 return 1;
Jari Aaltobb706242000-03-17 21:46:59 +00002919 }
2920 }
2921 else
2922 xkmap = cmd_xmap;
2923
2924 cmd = (char *)xkmap[key].function;
2925
2926 if (cmd == 0)
2927 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00002928 rl_ding ();
Jari Aaltobb706242000-03-17 21:46:59 +00002929 return 1;
2930 }
2931
Jari Aalto28ef6c32001-04-06 19:14:31 +00002932 rl_crlf (); /* move to a new line */
Jari Aaltobb706242000-03-17 21:46:59 +00002933
Jari Aaltob80f6442004-07-27 13:29:18 +00002934 save_parser_state (&ps);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002935
Jari Aaltobb706242000-03-17 21:46:59 +00002936 cmd = savestring (cmd);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002937 parse_and_execute (cmd, "bash_execute_unix_command", SEVAL_NOHIST);
2938
Jari Aaltob80f6442004-07-27 13:29:18 +00002939 restore_parser_state (&ps);
Jari Aaltobb706242000-03-17 21:46:59 +00002940
2941 /* and restore the readline buffer and display after command execution. */
2942 rl_forced_update_display ();
2943 return 0;
2944}
2945
2946static void
2947init_unix_command_map ()
2948{
2949 cmd_xmap = rl_make_bare_keymap ();
2950}
2951
2952static int
2953isolate_sequence (string, ind, need_dquote, startp)
2954 char *string;
2955 int ind, need_dquote, *startp;
2956{
2957 register int i;
2958 int c, passc, delim;
2959
2960 for (i = ind; string[i] && whitespace (string[i]); i++)
2961 ;
2962 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
2963 if (need_dquote && string[i] != '"')
2964 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002965 builtin_error (_("%s: first non-whitespace character is not `\"'"), string);
Jari Aaltobb706242000-03-17 21:46:59 +00002966 return -1;
2967 }
2968
2969 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
2970 string to bind the key sequence to. */
2971 delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0;
2972
2973 if (startp)
2974 *startp = delim ? ++i : i;
2975
2976 for (passc = 0; c = string[i]; i++)
2977 {
2978 if (passc)
2979 {
2980 passc = 0;
2981 continue;
2982 }
2983 if (c == '\\')
2984 {
2985 passc++;
2986 continue;
2987 }
2988 if (c == delim)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002989 break;
Jari Aaltobb706242000-03-17 21:46:59 +00002990 }
2991
2992 if (delim && string[i] != delim)
2993 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002994 builtin_error (_("no closing `%c' in %s"), delim, string);
Jari Aaltobb706242000-03-17 21:46:59 +00002995 return -1;
2996 }
2997
2998 return i;
2999}
3000
3001int
3002bind_keyseq_to_unix_command (line)
3003 char *line;
3004{
3005 Keymap kmap;
3006 char *kseq, *value;
Jari Aaltof73dda02001-11-13 17:56:06 +00003007 int i, kstart;
Jari Aaltobb706242000-03-17 21:46:59 +00003008
3009 if (cmd_xmap == 0)
3010 init_unix_command_map ();
3011
3012 kmap = rl_get_keymap ();
3013
3014 /* We duplicate some of the work done by rl_parse_and_bind here, but
3015 this code only has to handle `"keyseq": ["]command["]' and can
3016 generate an error for anything else. */
3017 i = isolate_sequence (line, 0, 1, &kstart);
3018 if (i < 0)
3019 return -1;
3020
3021 /* Create the key sequence string to pass to rl_generic_bind */
3022 kseq = substring (line, kstart, i);
3023
3024 for ( ; line[i] && line[i] != ':'; i++)
3025 ;
3026 if (line[i] != ':')
3027 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003028 builtin_error (_("%s: missing colon separator"), line);
Jari Aaltobb706242000-03-17 21:46:59 +00003029 return -1;
3030 }
3031
3032 i = isolate_sequence (line, i + 1, 0, &kstart);
3033 if (i < 0)
3034 return -1;
3035
3036 /* Create the value string containing the command to execute. */
3037 value = substring (line, kstart, i);
3038
3039 /* Save the command to execute and the key sequence in the CMD_XMAP */
3040 rl_generic_bind (ISMACR, kseq, value, cmd_xmap);
3041
3042 /* and bind the key sequence in the current keymap to a function that
3043 understands how to execute from CMD_XMAP */
Jari Aaltob80f6442004-07-27 13:29:18 +00003044 rl_bind_keyseq_in_map (kseq, bash_execute_unix_command, kmap);
Jari Aaltobb706242000-03-17 21:46:59 +00003045
3046 return 0;
3047}
3048
3049/* Used by the programmable completion code. Complete TEXT as a filename,
3050 but return only directories as matches. Dequotes the filename before
3051 attempting to find matches. */
3052char **
3053bash_directory_completion_matches (text)
Jari Aalto28ef6c32001-04-06 19:14:31 +00003054 const char *text;
Jari Aaltobb706242000-03-17 21:46:59 +00003055{
3056 char **m1;
3057 char *dfn;
3058 int qc;
3059
Jari Aaltob80f6442004-07-27 13:29:18 +00003060#if 0
Jari Aaltobb706242000-03-17 21:46:59 +00003061 qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
Jari Aaltob80f6442004-07-27 13:29:18 +00003062#else
3063 qc = rl_dispatching ? rl_completion_quote_character : 0;
3064#endif
Jari Aalto28ef6c32001-04-06 19:14:31 +00003065 dfn = bash_dequote_filename ((char *)text, qc);
3066 m1 = rl_completion_matches (dfn, rl_filename_completion_function);
Jari Aaltobb706242000-03-17 21:46:59 +00003067 free (dfn);
3068
3069 if (m1 == 0 || m1[0] == 0)
3070 return m1;
3071 /* We don't bother recomputing the lcd of the matches, because it will just
3072 get thrown away by the programmable completion code and recomputed
3073 later. */
3074 (void)bash_ignore_filenames (m1);
3075 return m1;
3076}
Jari Aaltob80f6442004-07-27 13:29:18 +00003077
3078char *
3079bash_dequote_text (text)
3080 const char *text;
3081{
3082 char *dtxt;
3083 int qc;
3084
3085 qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
3086 dtxt = bash_dequote_filename ((char *)text, qc);
3087 return (dtxt);
3088}
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003089#endif /* READLINE */