blob: 9b49d0b486e5dc5511c6837cc0c587169cb24d0d [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* bashline.c -- Bash's interface to the readline library. */
2
Jari Aalto31859422009-01-12 13:36:28 +00003/* Copyright (C) 1987-2009 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
Jari Aalto31859422009-01-12 13:36:28 +00007 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000016
17 You should have received a copy of the GNU General Public License
Jari Aalto31859422009-01-12 13:36:28 +000018 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
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"
Jari Aalto31859422009-01-12 13:36:28 +000053#include "shmbutil.h"
54
Jari Aaltoccc6cda1996-12-23 17:02:34 +000055#include "builtins/common.h"
Jari Aalto31859422009-01-12 13:36:28 +000056
Jari Aalto726f6381996-08-26 18:22:31 +000057#include <readline/rlconf.h>
58#include <readline/readline.h>
59#include <readline/history.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000060
61#include <glob/glob.h>
Jari Aalto726f6381996-08-26 18:22:31 +000062
63#if defined (ALIAS)
64# include "alias.h"
65#endif
66
Jari Aaltobb706242000-03-17 21:46:59 +000067#if defined (PROGRAMMABLE_COMPLETION)
68# include "pcomplete.h"
69#endif
70
Jari Aalto7117c2d2002-07-17 14:10:11 +000071/* These should agree with the defines for emacs_mode and vi_mode in
72 rldefs.h, even though that's not a public readline header file. */
73#ifndef EMACS_EDITING_MODE
74# define NO_EDITING_MODE -1
75# define EMACS_EDITING_MODE 1
76# define VI_EDITING_MODE 0
77#endif
78
Jari Aalto31859422009-01-12 13:36:28 +000079#define RL_BOOLEAN_VARIABLE_VALUE(s) ((s)[0] == 'o' && (s)[1] == 'n' && (s)[2] == '\0')
80
Jari Aalto726f6381996-08-26 18:22:31 +000081#if defined (BRACE_COMPLETION)
Jari Aalto28ef6c32001-04-06 19:14:31 +000082extern int bash_brace_completion __P((int, int));
Jari Aalto726f6381996-08-26 18:22:31 +000083#endif /* BRACE_COMPLETION */
84
Chet Ramey00018032011-11-21 20:51:19 -050085/* To avoid including curses.h/term.h/termcap.h and that whole mess. */
86extern int tputs __P((const char *string, int nlines, int (*outx)(int)));
87
Jari Aalto28ef6c32001-04-06 19:14:31 +000088/* Forward declarations */
89
Jari Aalto726f6381996-08-26 18:22:31 +000090/* Functions bound to keys in Readline for Bash users. */
Jari Aalto28ef6c32001-04-06 19:14:31 +000091static int shell_expand_line __P((int, int));
92static int display_shell_version __P((int, int));
93static int operate_and_get_next __P((int, int));
94
95static int bash_ignore_filenames __P((char **));
96static int bash_ignore_everything __P((char **));
97
Jari Aaltocce855b1998-04-17 19:52:44 +000098#if defined (BANG_HISTORY)
Jari Aaltof73dda02001-11-13 17:56:06 +000099static char *history_expand_line_internal __P((char *));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000100static int history_expand_line __P((int, int));
101static int tcsh_magic_space __P((int, int));
Jari Aaltocce855b1998-04-17 19:52:44 +0000102#endif /* BANG_HISTORY */
103#ifdef ALIAS
Jari Aalto28ef6c32001-04-06 19:14:31 +0000104static int alias_expand_line __P((int, int));
Jari Aaltocce855b1998-04-17 19:52:44 +0000105#endif
106#if defined (BANG_HISTORY) && defined (ALIAS)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000107static int history_and_alias_expand_line __P((int, int));
Jari Aaltocce855b1998-04-17 19:52:44 +0000108#endif
109
Jari Aalto31859422009-01-12 13:36:28 +0000110static int bash_forward_shellword __P((int, int));
111static int bash_backward_shellword __P((int, int));
112static int bash_kill_shellword __P((int, int));
113static int bash_backward_kill_shellword __P((int, int));
114
Jari Aalto726f6381996-08-26 18:22:31 +0000115/* Helper functions for Readline. */
Jari Aalto31859422009-01-12 13:36:28 +0000116static char *restore_tilde __P((char *, char *));
117
Chet Ramey00018032011-11-21 20:51:19 -0500118static char *bash_filename_rewrite_hook __P((char *, int));
Jari Aalto95732b42005-12-07 14:08:12 +0000119static void bash_directory_expansion __P((char **));
Jari Aaltof73dda02001-11-13 17:56:06 +0000120static int bash_directory_completion_hook __P((char **));
121static int filename_completion_ignore __P((char **));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000122static int bash_push_line __P((void));
Jari Aalto726f6381996-08-26 18:22:31 +0000123
Jari Aaltof73dda02001-11-13 17:56:06 +0000124static void cleanup_expansion_error __P((void));
125static void maybe_make_readline_line __P((char *));
126static void set_up_new_line __P((char *));
127
128static int check_redir __P((int));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000129static char **attempt_shell_completion __P((const char *, int, int));
130static char *variable_completion_function __P((const char *, int));
131static char *hostname_completion_function __P((const char *, int));
132static char *command_subst_completion_function __P((const char *, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000133
Jari Aaltof73dda02001-11-13 17:56:06 +0000134static void build_history_completion_array __P((void));
135static char *history_completion_generator __P((const char *, int));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000136static int dynamic_complete_history __P((int, int));
Jari Aalto31859422009-01-12 13:36:28 +0000137static int bash_dabbrev_expand __P((int, int));
Jari Aalto726f6381996-08-26 18:22:31 +0000138
Jari Aaltof73dda02001-11-13 17:56:06 +0000139static void initialize_hostname_list __P((void));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000140static void add_host_name __P((char *));
Jari Aaltof73dda02001-11-13 17:56:06 +0000141static void snarf_hosts_from_file __P((char *));
142static char **hostnames_matching __P((char *));
143
144static void _ignore_completion_names __P((char **, sh_ignore_func_t *));
145static int name_is_acceptable __P((const char *));
146static int test_for_directory __P((const char *));
147static int return_zero __P((const char *));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000148
149static char *bash_dequote_filename __P((char *, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000150static char *quote_word_break_chars __P((char *));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000151static char *bash_quote_filename __P((char *, int, char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000152
Chet Ramey00018032011-11-21 20:51:19 -0500153static int putx __P((int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000154static int bash_execute_unix_command __P((int, int));
155static void init_unix_command_map __P((void));
156static int isolate_sequence __P((char *, int, int, int *));
157
158static int set_saved_history __P((void));
159
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000160#if defined (ALIAS)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000161static int posix_edit_macros __P((int, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000162#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000163
Jari Aaltobb706242000-03-17 21:46:59 +0000164#if defined (PROGRAMMABLE_COMPLETION)
Jari Aaltof73dda02001-11-13 17:56:06 +0000165static int find_cmd_start __P((int));
166static int find_cmd_end __P((int));
167static char *find_cmd_name __P((int));
168static char *prog_complete_return __P((const char *, int));
169
Jari Aaltobb706242000-03-17 21:46:59 +0000170static char **prog_complete_matches;
Jari Aaltobb706242000-03-17 21:46:59 +0000171#endif
172
Jari Aalto726f6381996-08-26 18:22:31 +0000173/* Variables used here but defined in other files. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000174#if defined (BANG_HISTORY)
175extern int hist_verify;
176#endif
177
178extern int current_command_line_count, last_command_exit_value;
Jari Aalto31859422009-01-12 13:36:28 +0000179extern int array_needs_making;
Jari Aalto726f6381996-08-26 18:22:31 +0000180extern int posixly_correct, no_symbolic_links;
Jari Aalto726f6381996-08-26 18:22:31 +0000181extern char *current_prompt_string, *ps1_prompt;
182extern STRING_INT_ALIST word_token_alist[];
Jari Aaltob80f6442004-07-27 13:29:18 +0000183extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
Jari Aalto726f6381996-08-26 18:22:31 +0000184
185/* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
186 completion functions which indicate what type of completion should be
187 done (at or before point) that can be bound to key sequences with
188 the readline library. */
189#define SPECIFIC_COMPLETION_FUNCTIONS
190
191#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000192static int bash_specific_completion __P((int, rl_compentry_func_t *));
193
194static int bash_complete_filename_internal __P((int));
195static int bash_complete_username_internal __P((int));
196static int bash_complete_hostname_internal __P((int));
197static int bash_complete_variable_internal __P((int));
198static int bash_complete_command_internal __P((int));
199
200static int bash_complete_filename __P((int, int));
201static int bash_possible_filename_completions __P((int, int));
202static int bash_complete_username __P((int, int));
203static int bash_possible_username_completions __P((int, int));
204static int bash_complete_hostname __P((int, int));
205static int bash_possible_hostname_completions __P((int, int));
206static int bash_complete_variable __P((int, int));
207static int bash_possible_variable_completions __P((int, int));
208static int bash_complete_command __P((int, int));
209static int bash_possible_command_completions __P((int, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000210
211static char *glob_complete_word __P((const char *, int));
212static int bash_glob_completion_internal __P((int));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000213static int bash_glob_complete_word __P((int, int));
Jari Aaltof73dda02001-11-13 17:56:06 +0000214static int bash_glob_expand_word __P((int, int));
215static int bash_glob_list_expansions __P((int, int));
Jari Aaltob80f6442004-07-27 13:29:18 +0000216
Jari Aalto726f6381996-08-26 18:22:31 +0000217#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
218
Jari Aalto7117c2d2002-07-17 14:10:11 +0000219static int edit_and_execute_command __P((int, int, int, char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000220#if defined (VI_MODE)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000221static int vi_edit_and_execute_command __P((int, int));
Jari Aaltob80f6442004-07-27 13:29:18 +0000222static int bash_vi_complete __P((int, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000223#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +0000224static int emacs_edit_and_execute_command __P((int, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000225
Jari Aalto726f6381996-08-26 18:22:31 +0000226/* Non-zero once initalize_readline () has been called. */
227int bash_readline_initialized = 0;
228
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000229/* If non-zero, we do hostname completion, breaking words at `@' and
230 trying to complete the stuff after the `@' from our own internal
231 host list. */
232int perform_hostname_completion = 1;
233
Jari Aaltobb706242000-03-17 21:46:59 +0000234/* If non-zero, we don't do command completion on an empty line. */
235int no_empty_command_completion;
236
Jari Aaltob80f6442004-07-27 13:29:18 +0000237/* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the
238 only possible matches. Set to 0 if you want to match filenames if they
239 are the only possible matches, even if FIGNORE says to. */
240int force_fignore = 1;
241
Jari Aalto31859422009-01-12 13:36:28 +0000242/* Perform spelling correction on directory names during word completion */
243int dircomplete_spelling = 0;
244
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000245static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:";
246static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:";
Jari Aaltob80f6442004-07-27 13:29:18 +0000247/* )) */
Jari Aalto726f6381996-08-26 18:22:31 +0000248
Jari Aalto28ef6c32001-04-06 19:14:31 +0000249static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000250
Jari Aalto95732b42005-12-07 14:08:12 +0000251static int dot_in_path = 0;
252
Chet Ramey00018032011-11-21 20:51:19 -0500253/* Set to non-zero when dabbrev-expand is running */
254static int dabbrev_expand_active = 0;
255
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000256/* What kind of quoting is performed by bash_quote_filename:
257 COMPLETE_DQUOTE = double-quoting the filename
258 COMPLETE_SQUOTE = single_quoting the filename
259 COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
260*/
261#define COMPLETE_DQUOTE 1
262#define COMPLETE_SQUOTE 2
263#define COMPLETE_BSQUOTE 3
264static int completion_quoting_style = COMPLETE_BSQUOTE;
265
Jari Aalto06285672006-10-10 14:15:34 +0000266/* Flag values for the final argument to bash_default_completion */
267#define DEFCOMP_CMDPOS 1
268
Jari Aalto726f6381996-08-26 18:22:31 +0000269/* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
270 Called when the shell is put into or out of `posix' mode. */
271void
272posix_readline_initialize (on_or_off)
273 int on_or_off;
274{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000275 if (on_or_off)
276 rl_variable_bind ("comment-begin", "#");
Jari Aalto726f6381996-08-26 18:22:31 +0000277#if defined (VI_MODE)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000278 rl_bind_key_in_map (CTRL ('I'), on_or_off ? rl_insert : rl_complete, vi_insertion_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000279#endif
280}
281
Jari Aalto31859422009-01-12 13:36:28 +0000282void
283reset_completer_word_break_chars ()
284{
285 rl_completer_word_break_characters = perform_hostname_completion ? savestring (bash_completer_word_break_characters) : savestring (bash_nohostname_word_break_characters);
286}
287
Jari Aaltob80f6442004-07-27 13:29:18 +0000288/* When this function returns, rl_completer_word_break_characters points to
289 dynamically allocated memory. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000290int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000291enable_hostname_completion (on_or_off)
292 int on_or_off;
293{
Jari Aaltof73dda02001-11-13 17:56:06 +0000294 int old_value;
Jari Aaltob80f6442004-07-27 13:29:18 +0000295 char *at, *nv, *nval;
Jari Aaltof73dda02001-11-13 17:56:06 +0000296
297 old_value = perform_hostname_completion;
298
Jari Aalto726f6381996-08-26 18:22:31 +0000299 if (on_or_off)
300 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000301 perform_hostname_completion = 1;
302 rl_special_prefixes = "$@";
Jari Aalto726f6381996-08-26 18:22:31 +0000303 }
304 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000305 {
306 perform_hostname_completion = 0;
307 rl_special_prefixes = "$";
Jari Aaltob80f6442004-07-27 13:29:18 +0000308 }
309
310 /* Now we need to figure out how to appropriately modify and assign
311 rl_completer_word_break_characters depending on whether we want
312 hostname completion on or off. */
313
314 /* If this is the first time this has been called
315 (bash_readline_initialized == 0), use the sames values as before, but
316 allocate new memory for rl_completer_word_break_characters. */
317
318 if (bash_readline_initialized == 0 &&
319 (rl_completer_word_break_characters == 0 ||
320 rl_completer_word_break_characters == rl_basic_word_break_characters))
321 {
322 if (on_or_off)
323 rl_completer_word_break_characters = savestring (bash_completer_word_break_characters);
324 else
325 rl_completer_word_break_characters = savestring (bash_nohostname_word_break_characters);
326 }
327 else
328 {
329 /* See if we have anything to do. */
330 at = strchr (rl_completer_word_break_characters, '@');
331 if ((at == 0 && on_or_off == 0) || (at != 0 && on_or_off != 0))
Jari Aaltoeb873672004-11-09 21:37:25 +0000332 return old_value;
Jari Aaltob80f6442004-07-27 13:29:18 +0000333
334 /* We have something to do. Do it. */
335 nval = (char *)xmalloc (strlen (rl_completer_word_break_characters) + 1 + on_or_off);
336
337 if (on_or_off == 0)
338 {
339 /* Turn it off -- just remove `@' from word break chars. We want
340 to remove all occurrences of `@' from the char list, so we loop
341 rather than just copy the rest of the list over AT. */
342 for (nv = nval, at = rl_completer_word_break_characters; *at; )
343 if (*at != '@')
344 *nv++ = *at++;
345 else
346 at++;
347 *nv = '\0';
348 }
349 else
350 {
351 nval[0] = '@';
352 strcpy (nval + 1, rl_completer_word_break_characters);
353 }
354
355 free (rl_completer_word_break_characters);
356 rl_completer_word_break_characters = nval;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000357 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000358
359 return (old_value);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000360}
Jari Aalto726f6381996-08-26 18:22:31 +0000361
362/* Called once from parse.y if we are going to use readline. */
363void
364initialize_readline ()
365{
Jari Aaltob80f6442004-07-27 13:29:18 +0000366 rl_command_func_t *func;
367 char kseq[2];
368
Jari Aalto726f6381996-08-26 18:22:31 +0000369 if (bash_readline_initialized)
370 return;
371
372 rl_terminal_name = get_string_value ("TERM");
373 rl_instream = stdin;
374 rl_outstream = stderr;
Jari Aalto726f6381996-08-26 18:22:31 +0000375
376 /* Allow conditional parsing of the ~/.inputrc file. */
377 rl_readline_name = "Bash";
378
Jari Aalto28ef6c32001-04-06 19:14:31 +0000379 /* Add bindable names before calling rl_initialize so they may be
380 referenced in the various inputrc files. */
381 rl_add_defun ("shell-expand-line", shell_expand_line, -1);
Jari Aaltocce855b1998-04-17 19:52:44 +0000382#ifdef BANG_HISTORY
Jari Aalto28ef6c32001-04-06 19:14:31 +0000383 rl_add_defun ("history-expand-line", history_expand_line, -1);
384 rl_add_defun ("magic-space", tcsh_magic_space, -1);
Jari Aaltocce855b1998-04-17 19:52:44 +0000385#endif
386
Jari Aalto31859422009-01-12 13:36:28 +0000387 rl_add_defun ("shell-forward-word", bash_forward_shellword, -1);
388 rl_add_defun ("shell-backward-word", bash_backward_shellword, -1);
389 rl_add_defun ("shell-kill-word", bash_kill_shellword, -1);
390 rl_add_defun ("shell-backward-kill-word", bash_backward_kill_shellword, -1);
391
Jari Aaltod166f041997-06-05 14:59:13 +0000392#ifdef ALIAS
Jari Aalto28ef6c32001-04-06 19:14:31 +0000393 rl_add_defun ("alias-expand-line", alias_expand_line, -1);
Jari Aaltobc4cd231998-07-23 14:37:54 +0000394# ifdef BANG_HISTORY
Jari Aalto28ef6c32001-04-06 19:14:31 +0000395 rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line, -1);
Jari Aaltobc4cd231998-07-23 14:37:54 +0000396# endif
Jari Aaltod166f041997-06-05 14:59:13 +0000397#endif
398
Jari Aalto726f6381996-08-26 18:22:31 +0000399 /* Backwards compatibility. */
400 rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1);
401
Jari Aalto28ef6c32001-04-06 19:14:31 +0000402 rl_add_defun ("operate-and-get-next", operate_and_get_next, -1);
403 rl_add_defun ("display-shell-version", display_shell_version, -1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000404 rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command, -1);
Jari Aalto726f6381996-08-26 18:22:31 +0000405
Jari Aalto28ef6c32001-04-06 19:14:31 +0000406#if defined (BRACE_COMPLETION)
407 rl_add_defun ("complete-into-braces", bash_brace_completion, -1);
408#endif
409
410#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
411 rl_add_defun ("complete-filename", bash_complete_filename, -1);
412 rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1);
413 rl_add_defun ("complete-username", bash_complete_username, -1);
414 rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1);
415 rl_add_defun ("complete-hostname", bash_complete_hostname, -1);
416 rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1);
417 rl_add_defun ("complete-variable", bash_complete_variable, -1);
418 rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1);
419 rl_add_defun ("complete-command", bash_complete_command, -1);
420 rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000421 rl_add_defun ("glob-complete-word", bash_glob_complete_word, -1);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000422 rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1);
423 rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1);
424#endif
425
426 rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1);
Jari Aalto31859422009-01-12 13:36:28 +0000427 rl_add_defun ("dabbrev-expand", bash_dabbrev_expand, -1);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000428
429 /* Bind defaults before binding our custom shell keybindings. */
430 if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0)
431 rl_initialize ();
432
433 /* Bind up our special shell functions. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000434 rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line, emacs_meta_keymap);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000435
Jari Aalto28ef6c32001-04-06 19:14:31 +0000436#ifdef BANG_HISTORY
Jari Aaltob80f6442004-07-27 13:29:18 +0000437 rl_bind_key_if_unbound_in_map ('^', history_expand_line, emacs_meta_keymap);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000438#endif
439
Jari Aaltob80f6442004-07-27 13:29:18 +0000440 rl_bind_key_if_unbound_in_map (CTRL ('O'), operate_and_get_next, emacs_standard_keymap);
441 rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000442
443 /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
444 so it is not necessary to allow C-M-j for context switching. Turn
445 off this occasionally confusing behaviour. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000446 kseq[0] = CTRL('J');
447 kseq[1] = '\0';
448 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
449 if (func == rl_vi_editing_mode)
450 rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
451 kseq[0] = CTRL('M');
452 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
453 if (func == rl_vi_editing_mode)
454 rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000455#if defined (VI_MODE)
456 rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap);
457#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000458
Jari Aalto726f6381996-08-26 18:22:31 +0000459#if defined (BRACE_COMPLETION)
Jari Aaltob80f6442004-07-27 13:29:18 +0000460 rl_bind_key_if_unbound_in_map ('{', bash_brace_completion, emacs_meta_keymap); /*}*/
Jari Aalto726f6381996-08-26 18:22:31 +0000461#endif /* BRACE_COMPLETION */
462
463#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
Jari Aaltob80f6442004-07-27 13:29:18 +0000464 rl_bind_key_if_unbound_in_map ('/', bash_complete_filename, emacs_meta_keymap);
465 rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000466
Jari Aaltob80f6442004-07-27 13:29:18 +0000467 /* Have to jump through hoops here because there is a default binding for
468 M-~ (rl_tilde_expand) */
469 kseq[0] = '~';
470 kseq[1] = '\0';
471 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
472 if (func == 0 || func == rl_tilde_expand)
473 rl_bind_keyseq_in_map (kseq, bash_complete_username, emacs_meta_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000474
Jari Aaltob80f6442004-07-27 13:29:18 +0000475 rl_bind_key_if_unbound_in_map ('~', bash_possible_username_completions, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000476
Jari Aaltob80f6442004-07-27 13:29:18 +0000477 rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname, emacs_meta_keymap);
478 rl_bind_key_if_unbound_in_map ('@', bash_possible_hostname_completions, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000479
Jari Aaltob80f6442004-07-27 13:29:18 +0000480 rl_bind_key_if_unbound_in_map ('$', bash_complete_variable, emacs_meta_keymap);
481 rl_bind_key_if_unbound_in_map ('$', bash_possible_variable_completions, emacs_ctlx_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000482
Jari Aaltob80f6442004-07-27 13:29:18 +0000483 rl_bind_key_if_unbound_in_map ('!', bash_complete_command, emacs_meta_keymap);
484 rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions, emacs_ctlx_keymap);
485
486 rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word, emacs_meta_keymap);
487 rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word, emacs_ctlx_keymap);
488 rl_bind_key_if_unbound_in_map ('g', bash_glob_list_expansions, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000489
490#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
491
Jari Aalto95732b42005-12-07 14:08:12 +0000492 kseq[0] = TAB;
493 kseq[1] = '\0';
494 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
495 if (func == 0 || func == rl_tab_insert)
496 rl_bind_key_in_map (TAB, dynamic_complete_history, emacs_meta_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000497
498 /* Tell the completer that we want a crack first. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000499 rl_attempted_completion_function = attempt_shell_completion;
Jari Aalto726f6381996-08-26 18:22:31 +0000500
501 /* Tell the completer that we might want to follow symbolic links or
502 do other expansion on directory names. */
503 rl_directory_completion_hook = bash_directory_completion_hook;
504
Chet Ramey00018032011-11-21 20:51:19 -0500505 rl_filename_rewrite_hook = bash_filename_rewrite_hook;
506
Jari Aalto726f6381996-08-26 18:22:31 +0000507 /* Tell the filename completer we want a chance to ignore some names. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000508 rl_ignore_some_completions_function = filename_completion_ignore;
Jari Aalto726f6381996-08-26 18:22:31 +0000509
Jari Aalto7117c2d2002-07-17 14:10:11 +0000510 /* Bind C-xC-e to invoke emacs and run result as commands. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000511 rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command, emacs_ctlx_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000512#if defined (VI_MODE)
Jari Aaltob80f6442004-07-27 13:29:18 +0000513 rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command, vi_movement_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000514# if defined (ALIAS)
Jari Aaltob80f6442004-07-27 13:29:18 +0000515 rl_bind_key_if_unbound_in_map ('@', posix_edit_macros, vi_movement_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000516# endif
Jari Aaltob80f6442004-07-27 13:29:18 +0000517
518 rl_bind_key_in_map ('\\', bash_vi_complete, vi_movement_keymap);
519 rl_bind_key_in_map ('*', bash_vi_complete, vi_movement_keymap);
520 rl_bind_key_in_map ('=', bash_vi_complete, vi_movement_keymap);
Jari Aalto726f6381996-08-26 18:22:31 +0000521#endif
522
523 rl_completer_quote_characters = "'\"";
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000524
525 /* This sets rl_completer_word_break_characters and rl_special_prefixes
526 to the appropriate values, depending on whether or not hostname
527 completion is enabled. */
528 enable_hostname_completion (perform_hostname_completion);
529
530 /* characters that need to be quoted when appearing in filenames. */
Jari Aalto31859422009-01-12 13:36:28 +0000531#if 0
Jari Aalto28ef6c32001-04-06 19:14:31 +0000532 rl_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{"; /*}*/
Jari Aalto31859422009-01-12 13:36:28 +0000533#else
534 rl_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{~"; /*}*/
535#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000536 rl_filename_quoting_function = bash_quote_filename;
537 rl_filename_dequoting_function = bash_dequote_filename;
538 rl_char_is_quoted_p = char_is_quoted;
Jari Aalto726f6381996-08-26 18:22:31 +0000539
Jari Aalto7117c2d2002-07-17 14:10:11 +0000540#if 0
541 /* This is superfluous and makes it impossible to use tab completion in
542 vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix()
543 should already have called posix_readline_initialize() when
544 posixly_correct was set. */
Jari Aalto726f6381996-08-26 18:22:31 +0000545 if (posixly_correct)
546 posix_readline_initialize (1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000547#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000548
549 bash_readline_initialized = 1;
550}
551
Jari Aalto31859422009-01-12 13:36:28 +0000552void
553bashline_reinitialize ()
554{
555 bash_readline_initialized = 0;
556}
557
Jari Aalto726f6381996-08-26 18:22:31 +0000558/* On Sun systems at least, rl_attempted_completion_function can end up
559 getting set to NULL, and rl_completion_entry_function set to do command
560 word completion if Bash is interrupted while trying to complete a command
561 word. This just resets all the completion functions to the right thing.
562 It's called from throw_to_top_level(). */
563void
Jari Aalto31859422009-01-12 13:36:28 +0000564bashline_reset ()
Jari Aalto726f6381996-08-26 18:22:31 +0000565{
566 tilde_initialize ();
567 rl_attempted_completion_function = attempt_shell_completion;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000568 rl_completion_entry_function = NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000569 rl_directory_completion_hook = bash_directory_completion_hook;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000570 rl_ignore_some_completions_function = filename_completion_ignore;
Jari Aalto726f6381996-08-26 18:22:31 +0000571}
572
573/* Contains the line to push into readline. */
574static char *push_to_readline = (char *)NULL;
575
576/* Push the contents of push_to_readline into the
577 readline buffer. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000578static int
Jari Aalto726f6381996-08-26 18:22:31 +0000579bash_push_line ()
580{
581 if (push_to_readline)
582 {
583 rl_insert_text (push_to_readline);
584 free (push_to_readline);
585 push_to_readline = (char *)NULL;
586 rl_startup_hook = old_rl_startup_hook;
587 }
Jari Aalto28ef6c32001-04-06 19:14:31 +0000588 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000589}
590
591/* Call this to set the initial text for the next line to read
592 from readline. */
593int
594bash_re_edit (line)
595 char *line;
596{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000597 FREE (push_to_readline);
Jari Aalto726f6381996-08-26 18:22:31 +0000598
599 push_to_readline = savestring (line);
600 old_rl_startup_hook = rl_startup_hook;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000601 rl_startup_hook = bash_push_line;
Jari Aalto726f6381996-08-26 18:22:31 +0000602
603 return (0);
604}
605
Jari Aalto28ef6c32001-04-06 19:14:31 +0000606static int
Jari Aalto726f6381996-08-26 18:22:31 +0000607display_shell_version (count, c)
608 int count, c;
609{
Jari Aalto28ef6c32001-04-06 19:14:31 +0000610 rl_crlf ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000611 show_shell_version (0);
Jari Aalto726f6381996-08-26 18:22:31 +0000612 putc ('\r', rl_outstream);
613 fflush (rl_outstream);
614 rl_on_new_line ();
615 rl_redisplay ();
Jari Aalto28ef6c32001-04-06 19:14:31 +0000616 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000617}
618
619/* **************************************************************** */
620/* */
621/* Readline Stuff */
622/* */
623/* **************************************************************** */
624
625/* If the user requests hostname completion, then simply build a list
Jari Aaltobb706242000-03-17 21:46:59 +0000626 of hosts, and complete from that forever more, or at least until
627 HOSTFILE is unset. */
Jari Aalto726f6381996-08-26 18:22:31 +0000628
Jari Aaltobb706242000-03-17 21:46:59 +0000629/* THIS SHOULD BE A STRINGLIST. */
Jari Aalto726f6381996-08-26 18:22:31 +0000630/* The kept list of hostnames. */
631static char **hostname_list = (char **)NULL;
632
633/* The physical size of the above list. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000634static int hostname_list_size;
Jari Aalto726f6381996-08-26 18:22:31 +0000635
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000636/* The number of hostnames in the above list. */
637static int hostname_list_length;
Jari Aalto726f6381996-08-26 18:22:31 +0000638
639/* Whether or not HOSTNAME_LIST has been initialized. */
640int hostname_list_initialized = 0;
641
Jari Aalto726f6381996-08-26 18:22:31 +0000642/* Initialize the hostname completion table. */
643static void
644initialize_hostname_list ()
645{
646 char *temp;
647
648 temp = get_string_value ("HOSTFILE");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000649 if (temp == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000650 temp = get_string_value ("hostname_completion_file");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000651 if (temp == 0)
652 temp = DEFAULT_HOSTS_FILE;
Jari Aalto726f6381996-08-26 18:22:31 +0000653
654 snarf_hosts_from_file (temp);
Jari Aalto726f6381996-08-26 18:22:31 +0000655
656 if (hostname_list)
657 hostname_list_initialized++;
658}
659
660/* Add NAME to the list of hosts. */
661static void
662add_host_name (name)
663 char *name;
664{
665 if (hostname_list_length + 2 > hostname_list_size)
666 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000667 hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000668 hostname_list = strvec_resize (hostname_list, hostname_list_size);
Jari Aalto726f6381996-08-26 18:22:31 +0000669 }
670
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000671 hostname_list[hostname_list_length++] = savestring (name);
672 hostname_list[hostname_list_length] = (char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000673}
674
675#define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
676
677static void
678snarf_hosts_from_file (filename)
679 char *filename;
680{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000681 FILE *file;
Jari Aalto726f6381996-08-26 18:22:31 +0000682 char *temp, buffer[256], name[256];
683 register int i, start;
684
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000685 file = fopen (filename, "r");
686 if (file == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000687 return;
688
689 while (temp = fgets (buffer, 255, file))
690 {
691 /* Skip to first character. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000692 for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++)
693 ;
Jari Aalto726f6381996-08-26 18:22:31 +0000694
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000695 /* If comment or blank line, ignore. */
696 if (buffer[i] == '\0' || buffer[i] == '#')
Jari Aalto726f6381996-08-26 18:22:31 +0000697 continue;
698
699 /* If `preprocessor' directive, do the include. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000700 if (strncmp (buffer + i, "$include ", 9) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000701 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000702 char *incfile, *t;
Jari Aalto726f6381996-08-26 18:22:31 +0000703
704 /* Find start of filename. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000705 for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++)
706 ;
Jari Aalto726f6381996-08-26 18:22:31 +0000707
708 /* Find end of filename. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000709 for (t = incfile; *t && cr_whitespace (*t) == 0; t++)
710 ;
Jari Aalto726f6381996-08-26 18:22:31 +0000711
712 *t = '\0';
713
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000714 snarf_hosts_from_file (incfile);
Jari Aalto726f6381996-08-26 18:22:31 +0000715 continue;
716 }
717
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000718 /* Skip internet address if present. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000719 if (DIGIT (buffer[i]))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000720 for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++);
Jari Aalto726f6381996-08-26 18:22:31 +0000721
722 /* Gobble up names. Each name is separated with whitespace. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000723 while (buffer[i])
Jari Aalto726f6381996-08-26 18:22:31 +0000724 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000725 for (; cr_whitespace (buffer[i]); i++)
726 ;
727 if (buffer[i] == '\0' || buffer[i] == '#')
728 break;
729
730 /* Isolate the current word. */
731 for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++)
732 ;
733 if (i == start)
Jari Aalto726f6381996-08-26 18:22:31 +0000734 continue;
735 strncpy (name, buffer + start, i - start);
736 name[i - start] = '\0';
737 add_host_name (name);
738 }
739 }
740 fclose (file);
741}
742
Jari Aaltobb706242000-03-17 21:46:59 +0000743/* Return the hostname list. */
744char **
745get_hostname_list ()
746{
747 if (hostname_list_initialized == 0)
748 initialize_hostname_list ();
749 return (hostname_list);
750}
751
752void
753clear_hostname_list ()
754{
755 register int i;
756
757 if (hostname_list_initialized == 0)
758 return;
759 for (i = 0; i < hostname_list_length; i++)
760 free (hostname_list[i]);
Chet Ramey00018032011-11-21 20:51:19 -0500761 hostname_list_length = hostname_list_initialized = 0;
Jari Aaltobb706242000-03-17 21:46:59 +0000762}
763
Jari Aalto726f6381996-08-26 18:22:31 +0000764/* Return a NULL terminated list of hostnames which begin with TEXT.
765 Initialize the hostname list the first time if neccessary.
766 The array is malloc ()'ed, but not the individual strings. */
767static char **
768hostnames_matching (text)
769 char *text;
770{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000771 register int i, len, nmatch, rsize;
772 char **result;
Jari Aalto726f6381996-08-26 18:22:31 +0000773
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000774 if (hostname_list_initialized == 0)
775 initialize_hostname_list ();
Jari Aalto726f6381996-08-26 18:22:31 +0000776
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000777 if (hostname_list_initialized == 0)
778 return ((char **)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000779
780 /* Special case. If TEXT consists of nothing, then the whole list is
781 what is desired. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000782 if (*text == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000783 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000784 result = strvec_create (1 + hostname_list_length);
Jari Aalto726f6381996-08-26 18:22:31 +0000785 for (i = 0; i < hostname_list_length; i++)
786 result[i] = hostname_list[i];
787 result[i] = (char *)NULL;
788 return (result);
789 }
790
791 /* Scan until found, or failure. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000792 len = strlen (text);
793 result = (char **)NULL;
794 for (i = nmatch = rsize = 0; i < hostname_list_length; i++)
Jari Aalto726f6381996-08-26 18:22:31 +0000795 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000796 if (STREQN (text, hostname_list[i], len) == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000797 continue;
Jari Aalto726f6381996-08-26 18:22:31 +0000798
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000799 /* OK, it matches. Add it to the list. */
Jari Aaltobc4cd231998-07-23 14:37:54 +0000800 if (nmatch >= (rsize - 1))
Jari Aalto726f6381996-08-26 18:22:31 +0000801 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000802 rsize = (rsize + 16) - (rsize % 16);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000803 result = strvec_resize (result, rsize);
Jari Aalto726f6381996-08-26 18:22:31 +0000804 }
805
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000806 result[nmatch++] = hostname_list[i];
Jari Aalto726f6381996-08-26 18:22:31 +0000807 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000808 if (nmatch)
809 result[nmatch] = (char *)NULL;
810 return (result);
Jari Aalto726f6381996-08-26 18:22:31 +0000811}
812
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000813/* The equivalent of the Korn shell C-o operate-and-get-next-history-line
Jari Aalto726f6381996-08-26 18:22:31 +0000814 editing command. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000815static int saved_history_line_to_use = -1;
Jari Aalto726f6381996-08-26 18:22:31 +0000816
Jari Aalto28ef6c32001-04-06 19:14:31 +0000817static int
Jari Aalto726f6381996-08-26 18:22:31 +0000818set_saved_history ()
819{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000820 if (saved_history_line_to_use >= 0)
Jari Aaltob72432f1999-02-19 17:11:39 +0000821 rl_get_previous_history (history_length - saved_history_line_to_use, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000822 saved_history_line_to_use = -1;
Jari Aalto726f6381996-08-26 18:22:31 +0000823 rl_startup_hook = old_rl_startup_hook;
Jari Aaltof73dda02001-11-13 17:56:06 +0000824 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000825}
Jari Aalto726f6381996-08-26 18:22:31 +0000826
Jari Aalto28ef6c32001-04-06 19:14:31 +0000827static int
Jari Aalto726f6381996-08-26 18:22:31 +0000828operate_and_get_next (count, c)
829 int count, c;
830{
831 int where;
832
833 /* Accept the current line. */
Jari Aaltob72432f1999-02-19 17:11:39 +0000834 rl_newline (1, c);
Jari Aalto726f6381996-08-26 18:22:31 +0000835
836 /* Find the current line, and find the next line to use. */
837 where = where_history ();
838
Jari Aalto28ef6c32001-04-06 19:14:31 +0000839 if ((history_is_stifled () && (history_length >= history_max_entries)) ||
Jari Aalto726f6381996-08-26 18:22:31 +0000840 (where >= history_length - 1))
841 saved_history_line_to_use = where;
842 else
843 saved_history_line_to_use = where + 1;
844
845 old_rl_startup_hook = rl_startup_hook;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000846 rl_startup_hook = set_saved_history;
847
848 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000849}
850
Jari Aalto726f6381996-08-26 18:22:31 +0000851/* This vi mode command causes VI_EDIT_COMMAND to be run on the current
852 command being entered (if no explicit argument is given), otherwise on
853 a command from the history file. */
854
Jari Aaltob80f6442004-07-27 13:29:18 +0000855#define VI_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-vi}}\""
856#define EMACS_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-emacs}}\""
Jari Aalto95732b42005-12-07 14:08:12 +0000857#define POSIX_VI_EDIT_COMMAND "fc -e vi"
Jari Aalto726f6381996-08-26 18:22:31 +0000858
Jari Aalto28ef6c32001-04-06 19:14:31 +0000859static int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000860edit_and_execute_command (count, c, editing_mode, edit_command)
861 int count, c, editing_mode;
862 char *edit_command;
Jari Aalto726f6381996-08-26 18:22:31 +0000863{
Jari Aalto31859422009-01-12 13:36:28 +0000864 char *command, *metaval;
865 int r, cclc, rrs, metaflag;
Jari Aaltof73dda02001-11-13 17:56:06 +0000866
867 rrs = rl_readline_state;
868 cclc = current_command_line_count;
Jari Aalto726f6381996-08-26 18:22:31 +0000869
870 /* Accept the current line. */
Jari Aaltob72432f1999-02-19 17:11:39 +0000871 rl_newline (1, c);
Jari Aalto726f6381996-08-26 18:22:31 +0000872
873 if (rl_explicit_arg)
874 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000875 command = (char *)xmalloc (strlen (edit_command) + 8);
876 sprintf (command, "%s %d", edit_command, count);
Jari Aalto726f6381996-08-26 18:22:31 +0000877 }
878 else
879 {
880 /* Take the command we were just editing, add it to the history file,
881 then call fc to operate on it. We have to add a dummy command to
882 the end of the history because fc ignores the last command (assumes
883 it's supposed to deal with the command before the `fc'). */
884 using_history ();
Jari Aaltod166f041997-06-05 14:59:13 +0000885 bash_add_history (rl_line_buffer);
886 bash_add_history ("");
Jari Aalto726f6381996-08-26 18:22:31 +0000887 history_lines_this_session++;
888 using_history ();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000889 command = savestring (edit_command);
Jari Aalto726f6381996-08-26 18:22:31 +0000890 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000891
Jari Aalto31859422009-01-12 13:36:28 +0000892 metaval = rl_variable_value ("input-meta");
893 metaflag = RL_BOOLEAN_VARIABLE_VALUE (metaval);
894
Jari Aalto7117c2d2002-07-17 14:10:11 +0000895 /* Now, POSIX.1-2001 and SUSv3 say that the commands executed from the
896 temporary file should be placed into the history. We don't do that
897 yet. */
Jari Aalto31859422009-01-12 13:36:28 +0000898 if (rl_deprep_term_function)
899 (*rl_deprep_term_function) ();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000900 r = parse_and_execute (command, (editing_mode == VI_EDITING_MODE) ? "v" : "C-xC-e", SEVAL_NOHIST);
Jari Aalto31859422009-01-12 13:36:28 +0000901 if (rl_prep_term_function)
902 (*rl_prep_term_function) (metaflag);
Jari Aaltof73dda02001-11-13 17:56:06 +0000903
904 current_command_line_count = cclc;
905
906 /* Now erase the contents of the current line and undo the effects of the
907 rl_accept_line() above. We don't even want to make the text we just
908 executed available for undoing. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000909 rl_line_buffer[0] = '\0'; /* XXX */
Jari Aaltof73dda02001-11-13 17:56:06 +0000910 rl_point = rl_end = 0;
911 rl_done = 0;
912 rl_readline_state = rrs;
913
914 rl_forced_update_display ();
Jari Aalto28ef6c32001-04-06 19:14:31 +0000915
916 return r;
Jari Aalto726f6381996-08-26 18:22:31 +0000917}
Jari Aalto7117c2d2002-07-17 14:10:11 +0000918
919#if defined (VI_MODE)
920static int
921vi_edit_and_execute_command (count, c)
922 int count, c;
923{
Jari Aalto95732b42005-12-07 14:08:12 +0000924 if (posixly_correct)
925 return (edit_and_execute_command (count, c, VI_EDITING_MODE, POSIX_VI_EDIT_COMMAND));
926 else
927 return (edit_and_execute_command (count, c, VI_EDITING_MODE, VI_EDIT_COMMAND));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000928}
Jari Aalto726f6381996-08-26 18:22:31 +0000929#endif /* VI_MODE */
930
Jari Aalto7117c2d2002-07-17 14:10:11 +0000931static int
932emacs_edit_and_execute_command (count, c)
933 int count, c;
934{
935 return (edit_and_execute_command (count, c, EMACS_EDITING_MODE, EMACS_EDIT_COMMAND));
936}
937
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000938#if defined (ALIAS)
939static int
940posix_edit_macros (count, key)
941 int count, key;
942{
943 int c;
944 char alias_name[3], *alias_value, *macro;
945
946 c = rl_read_key ();
947 alias_name[0] = '_';
948 alias_name[1] = c;
949 alias_name[2] = '\0';
950
951 alias_value = get_alias_value (alias_name);
952 if (alias_value && *alias_value)
953 {
954 macro = savestring (alias_value);
955 rl_push_macro_input (macro);
956 }
957 return 0;
958}
959#endif
960
Jari Aalto31859422009-01-12 13:36:28 +0000961/* Bindable commands that move `shell-words': that is, sequences of
962 non-unquoted-metacharacters. */
963
964#define WORDDELIM(c) (shellmeta(c) || shellblank(c))
965
966static int
967bash_forward_shellword (count, key)
968 int count, key;
969{
970 size_t slen;
971 int sindex, c, p;
972 DECLARE_MBSTATE;
973
974 if (count < 0)
975 return (bash_backward_shellword (-count, key));
976
977 /* The tricky part of this is deciding whether or not the first character
978 we're on is an unquoted metacharacter. Not completely handled yet. */
979 /* XXX - need to test this stuff with backslash-escaped shell
980 metacharacters and unclosed single- and double-quoted strings. */
981
982 p = rl_point;
983 slen = rl_end;
984
985 while (count)
986 {
987 if (p == rl_end)
988 {
989 rl_point = rl_end;
990 return 0;
991 }
992
993 /* Move forward until we hit a non-metacharacter. */
994 while (p < rl_end && (c = rl_line_buffer[p]) && WORDDELIM (c))
995 {
996 switch (c)
997 {
998 default:
999 ADVANCE_CHAR (rl_line_buffer, slen, p);
1000 continue; /* straight back to loop, don't increment p */
1001 case '\\':
1002 if (p < rl_end && rl_line_buffer[p])
1003 ADVANCE_CHAR (rl_line_buffer, slen, p);
1004 break;
1005 case '\'':
1006 p = skip_to_delim (rl_line_buffer, ++p, "'", SD_NOJMP);
1007 break;
1008 case '"':
1009 p = skip_to_delim (rl_line_buffer, ++p, "\"", SD_NOJMP);
1010 break;
1011 }
1012
1013 if (p < rl_end)
1014 p++;
1015 }
1016
1017 if (rl_line_buffer[p] == 0 || p == rl_end)
1018 {
1019 rl_point = rl_end;
1020 rl_ding ();
1021 return 0;
1022 }
1023
1024 /* Now move forward until we hit a non-quoted metacharacter or EOL */
1025 while (p < rl_end && (c = rl_line_buffer[p]) && WORDDELIM (c) == 0)
1026 {
1027 switch (c)
1028 {
1029 default:
1030 ADVANCE_CHAR (rl_line_buffer, slen, p);
1031 continue; /* straight back to loop, don't increment p */
1032 case '\\':
1033 if (p < rl_end && rl_line_buffer[p])
1034 ADVANCE_CHAR (rl_line_buffer, slen, p);
1035 break;
1036 case '\'':
1037 p = skip_to_delim (rl_line_buffer, ++p, "'", SD_NOJMP);
1038 break;
1039 case '"':
1040 p = skip_to_delim (rl_line_buffer, ++p, "\"", SD_NOJMP);
1041 break;
1042 }
1043
1044 if (p < rl_end)
1045 p++;
1046 }
1047
1048 if (p == rl_end || rl_line_buffer[p] == 0)
1049 {
1050 rl_point = rl_end;
1051 return (0);
1052 }
1053
1054 count--;
1055 }
1056
1057 rl_point = p;
1058 return (0);
1059}
1060
1061static int
1062bash_backward_shellword (count, key)
1063 int count, key;
1064{
1065 size_t slen;
1066 int sindex, c, p;
1067 DECLARE_MBSTATE;
1068
1069 if (count < 0)
1070 return (bash_forward_shellword (-count, key));
1071
1072 p = rl_point;
1073 slen = rl_end;
1074
1075 while (count)
1076 {
1077 if (p == 0)
1078 {
1079 rl_point = 0;
1080 return 0;
1081 }
1082
1083 /* Move backward until we hit a non-metacharacter. */
1084 while (p > 0)
1085 {
1086 c = rl_line_buffer[p];
1087 if (WORDDELIM (c) && char_is_quoted (rl_line_buffer, p) == 0)
1088 BACKUP_CHAR (rl_line_buffer, slen, p);
1089 break;
1090 }
1091
1092 if (p == 0)
1093 {
1094 rl_point = 0;
1095 return 0;
1096 }
1097
1098 /* Now move backward until we hit a metacharacter or BOL. */
1099 while (p > 0)
1100 {
1101 c = rl_line_buffer[p];
1102 if (WORDDELIM (c) && char_is_quoted (rl_line_buffer, p) == 0)
1103 break;
1104 BACKUP_CHAR (rl_line_buffer, slen, p);
1105 }
1106
1107 count--;
1108 }
1109
1110 rl_point = p;
1111 return 0;
1112}
1113
1114static int
1115bash_kill_shellword (count, key)
1116 int count, key;
1117{
1118 int p;
1119
1120 if (count < 0)
1121 return (bash_backward_kill_shellword (-count, key));
1122
1123 p = rl_point;
1124 bash_forward_shellword (count, key);
1125
1126 if (rl_point != p)
1127 rl_kill_text (p, rl_point);
1128
1129 rl_point = p;
1130 if (rl_editing_mode == 1) /* 1 == emacs_mode */
1131 rl_mark = rl_point;
1132
1133 return 0;
1134}
1135
1136static int
1137bash_backward_kill_shellword (count, key)
1138 int count, key;
1139{
1140 int p;
1141
1142 if (count < 0)
1143 return (bash_kill_shellword (-count, key));
1144
1145 p = rl_point;
1146 bash_backward_shellword (count, key);
1147
1148 if (rl_point != p)
1149 rl_kill_text (p, rl_point);
1150
1151 if (rl_editing_mode == 1) /* 1 == emacs_mode */
1152 rl_mark = rl_point;
1153
1154 return 0;
1155}
1156
1157
Jari Aalto726f6381996-08-26 18:22:31 +00001158/* **************************************************************** */
1159/* */
1160/* How To Do Shell Completion */
1161/* */
1162/* **************************************************************** */
1163
Jari Aaltobb706242000-03-17 21:46:59 +00001164#define COMMAND_SEPARATORS ";|&{(`"
Jari Aaltob80f6442004-07-27 13:29:18 +00001165/* )} */
Jari Aaltobb706242000-03-17 21:46:59 +00001166
1167static int
1168check_redir (ti)
1169 int ti;
1170{
1171 register int this_char, prev_char;
1172
1173 /* Handle the two character tokens `>&', `<&', and `>|'.
1174 We are not in a command position after one of these. */
1175 this_char = rl_line_buffer[ti];
1176 prev_char = rl_line_buffer[ti - 1];
1177
1178 if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) ||
1179 (this_char == '|' && prev_char == '>'))
1180 return (1);
1181 else if ((this_char == '{' && prev_char == '$') || /* } */
1182 (char_is_quoted (rl_line_buffer, ti)))
1183 return (1);
1184 return (0);
1185}
1186
1187#if defined (PROGRAMMABLE_COMPLETION)
Jari Aaltof73dda02001-11-13 17:56:06 +00001188/*
1189 * XXX - because of the <= start test, and setting os = s+1, this can
1190 * potentially return os > start. This is probably not what we want to
1191 * happen, but fix later after 2.05a-release.
1192 */
Jari Aaltobb706242000-03-17 21:46:59 +00001193static int
1194find_cmd_start (start)
1195 int start;
1196{
1197 register int s, os;
1198
1199 os = 0;
Chet Ramey00018032011-11-21 20:51:19 -05001200 while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS, SD_NOJMP|SD_NOSKIPCMD)) <= start) &&
Jari Aaltobb706242000-03-17 21:46:59 +00001201 rl_line_buffer[s])
1202 os = s+1;
1203 return os;
1204}
1205
1206static int
1207find_cmd_end (end)
1208 int end;
1209{
1210 register int e;
1211
Jari Aalto31859422009-01-12 13:36:28 +00001212 e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS, SD_NOJMP);
Jari Aaltobb706242000-03-17 21:46:59 +00001213 return e;
1214}
1215
1216static char *
1217find_cmd_name (start)
1218 int start;
1219{
1220 char *name;
1221 register int s, e;
1222
1223 for (s = start; whitespace (rl_line_buffer[s]); s++)
1224 ;
1225
1226 /* skip until a shell break character */
Jari Aalto31859422009-01-12 13:36:28 +00001227 e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n", SD_NOJMP);
Jari Aaltobb706242000-03-17 21:46:59 +00001228
1229 name = substring (rl_line_buffer, s, e);
1230
1231 return (name);
1232}
1233
1234static char *
1235prog_complete_return (text, matchnum)
Jari Aaltof73dda02001-11-13 17:56:06 +00001236 const char *text;
Jari Aaltobb706242000-03-17 21:46:59 +00001237 int matchnum;
1238{
1239 static int ind;
1240
1241 if (matchnum == 0)
1242 ind = 0;
1243
1244 if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0)
1245 return (char *)NULL;
1246 return (prog_complete_matches[ind++]);
1247}
1248
1249#endif /* PROGRAMMABLE_COMPLETION */
1250
Jari Aalto726f6381996-08-26 18:22:31 +00001251/* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
1252 at START and END. Return an array of matches, or NULL if none. */
1253static char **
1254attempt_shell_completion (text, start, end)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001255 const char *text;
Jari Aalto726f6381996-08-26 18:22:31 +00001256 int start, end;
1257{
Jari Aalto06285672006-10-10 14:15:34 +00001258 int in_command_position, ti, saveti, qc, dflags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001259 char **matches, *command_separator_chars;
Jari Aalto726f6381996-08-26 18:22:31 +00001260
Jari Aaltobb706242000-03-17 21:46:59 +00001261 command_separator_chars = COMMAND_SEPARATORS;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001262 matches = (char **)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001263 rl_ignore_some_completions_function = filename_completion_ignore;
Jari Aalto726f6381996-08-26 18:22:31 +00001264
1265 /* Determine if this could be a command word. It is if it appears at
1266 the start of the line (ignoring preceding whitespace), or if it
1267 appears after a character that separates commands. It cannot be a
1268 command word if we aren't at the top-level prompt. */
1269 ti = start - 1;
Jari Aaltobb706242000-03-17 21:46:59 +00001270 saveti = qc = -1;
Jari Aalto726f6381996-08-26 18:22:31 +00001271
1272 while ((ti > -1) && (whitespace (rl_line_buffer[ti])))
1273 ti--;
1274
Jari Aaltobb706242000-03-17 21:46:59 +00001275#if 1
1276 /* If this is an open quote, maybe we're trying to complete a quoted
1277 command name. */
Jari Aaltob80f6442004-07-27 13:29:18 +00001278 if (ti >= 0 && (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\''))
Jari Aaltobb706242000-03-17 21:46:59 +00001279 {
1280 qc = rl_line_buffer[ti];
1281 saveti = ti--;
1282 while (ti > -1 && (whitespace (rl_line_buffer[ti])))
Jari Aalto28ef6c32001-04-06 19:14:31 +00001283 ti--;
Jari Aaltobb706242000-03-17 21:46:59 +00001284 }
1285#endif
1286
Jari Aalto726f6381996-08-26 18:22:31 +00001287 in_command_position = 0;
1288 if (ti < 0)
1289 {
1290 /* Only do command completion at the start of a line when we
Jari Aalto28ef6c32001-04-06 19:14:31 +00001291 are prompting at the top level. */
Jari Aalto726f6381996-08-26 18:22:31 +00001292 if (current_prompt_string == ps1_prompt)
1293 in_command_position++;
1294 }
1295 else if (member (rl_line_buffer[ti], command_separator_chars))
1296 {
Jari Aalto726f6381996-08-26 18:22:31 +00001297 in_command_position++;
1298
Jari Aaltobb706242000-03-17 21:46:59 +00001299 if (check_redir (ti) == 1)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001300 in_command_position = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001301 }
1302 else
1303 {
1304 /* This still could be in command position. It is possible
1305 that all of the previous words on the line are variable
1306 assignments. */
1307 }
1308
Jari Aaltod166f041997-06-05 14:59:13 +00001309 /* Check that we haven't incorrectly flagged a closed command substitution
1310 as indicating we're in a command position. */
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001311 if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' &&
Jari Aaltobb706242000-03-17 21:46:59 +00001312 *text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0)
Jari Aaltod166f041997-06-05 14:59:13 +00001313 in_command_position = 0;
1314
1315 /* Special handling for command substitution. If *TEXT is a backquote,
1316 it can be the start or end of an old-style command substitution, or
1317 unmatched. If it's unmatched, both calls to unclosed_pair will
Chet Ramey00018032011-11-21 20:51:19 -05001318 succeed. Don't bother if readline found a single quote and we are
1319 completing on the substring. */
1320 if (*text == '`' && rl_completion_quote_character != '\'' &&
Jari Aaltobb706242000-03-17 21:46:59 +00001321 (in_command_position || (unclosed_pair (rl_line_buffer, start, "`") &&
1322 unclosed_pair (rl_line_buffer, end, "`"))))
Jari Aalto28ef6c32001-04-06 19:14:31 +00001323 matches = rl_completion_matches (text, command_subst_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00001324
Jari Aaltobb706242000-03-17 21:46:59 +00001325#if defined (PROGRAMMABLE_COMPLETION)
1326 /* Attempt programmable completion. */
Jari Aalto31859422009-01-12 13:36:28 +00001327 if (matches == 0 && (in_command_position == 0 || text[0] == '\0') &&
1328 prog_completion_enabled && (progcomp_size () > 0) &&
1329 current_prompt_string == ps1_prompt)
Jari Aaltobb706242000-03-17 21:46:59 +00001330 {
1331 int s, e, foundcs;
1332 char *n;
1333
1334 /* XXX - don't free the members */
1335 if (prog_complete_matches)
1336 free (prog_complete_matches);
1337 prog_complete_matches = (char **)NULL;
1338
1339 s = find_cmd_start (start);
1340 e = find_cmd_end (end);
1341 n = find_cmd_name (s);
Jari Aalto31859422009-01-12 13:36:28 +00001342 if (e == 0 && e == s && text[0] == '\0')
1343 prog_complete_matches = programmable_completions ("_EmptycmD_", text, s, e, &foundcs);
1344 else if (e > s && assignment (n, 0) == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +00001345 prog_complete_matches = programmable_completions (n, text, s, e, &foundcs);
1346 else
1347 foundcs = 0;
Jari Aaltobb706242000-03-17 21:46:59 +00001348 FREE (n);
1349 /* XXX - if we found a COMPSPEC for the command, just return whatever
1350 the programmable completion code returns, and disable the default
Jari Aalto28ef6c32001-04-06 19:14:31 +00001351 filename completion that readline will do unless the COPT_DEFAULT
Jari Aalto31859422009-01-12 13:36:28 +00001352 option has been set with the `-o default' option to complete or
1353 compopt. */
Jari Aaltobb706242000-03-17 21:46:59 +00001354 if (foundcs)
1355 {
Jari Aalto31859422009-01-12 13:36:28 +00001356 pcomp_set_readline_variables (foundcs, 1);
Jari Aaltobb706242000-03-17 21:46:59 +00001357 /* Turn what the programmable completion code returns into what
1358 readline wants. I should have made compute_lcd_of_matches
1359 external... */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001360 matches = rl_completion_matches (text, prog_complete_return);
1361 if ((foundcs & COPT_DEFAULT) == 0)
1362 rl_attempted_completion_over = 1; /* no default */
Jari Aaltob80f6442004-07-27 13:29:18 +00001363 if (matches || ((foundcs & COPT_BASHDEFAULT) == 0))
1364 return (matches);
Jari Aaltobb706242000-03-17 21:46:59 +00001365 }
1366 }
1367#endif
1368
Jari Aaltob80f6442004-07-27 13:29:18 +00001369 if (matches == 0)
Jari Aalto06285672006-10-10 14:15:34 +00001370 {
1371 dflags = 0;
1372 if (in_command_position)
1373 dflags |= DEFCOMP_CMDPOS;
1374 matches = bash_default_completion (text, start, end, qc, dflags);
1375 }
Jari Aaltob80f6442004-07-27 13:29:18 +00001376
1377 return matches;
1378}
1379
1380char **
Jari Aalto06285672006-10-10 14:15:34 +00001381bash_default_completion (text, start, end, qc, compflags)
Jari Aaltob80f6442004-07-27 13:29:18 +00001382 const char *text;
Jari Aalto06285672006-10-10 14:15:34 +00001383 int start, end, qc, compflags;
Jari Aaltob80f6442004-07-27 13:29:18 +00001384{
1385 char **matches;
1386
1387 matches = (char **)NULL;
1388
Jari Aaltobb706242000-03-17 21:46:59 +00001389 /* New posix-style command substitution or variable name? */
Jari Aalto726f6381996-08-26 18:22:31 +00001390 if (!matches && *text == '$')
Jari Aaltobb706242000-03-17 21:46:59 +00001391 {
1392 if (qc != '\'' && text[1] == '(') /* ) */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001393 matches = rl_completion_matches (text, command_subst_completion_function);
Jari Aaltobb706242000-03-17 21:46:59 +00001394 else
Jari Aalto28ef6c32001-04-06 19:14:31 +00001395 matches = rl_completion_matches (text, variable_completion_function);
Jari Aaltobb706242000-03-17 21:46:59 +00001396 }
Jari Aalto726f6381996-08-26 18:22:31 +00001397
1398 /* If the word starts in `~', and there is no slash in the word, then
1399 try completing this word as a username. */
Chet Ramey00018032011-11-21 20:51:19 -05001400 if (matches ==0 && *text == '~' && mbschr (text, '/') == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001401 matches = rl_completion_matches (text, rl_username_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00001402
1403 /* Another one. Why not? If the word starts in '@', then look through
1404 the world of known hostnames for completion first. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001405 if (!matches && perform_hostname_completion && *text == '@')
Jari Aalto28ef6c32001-04-06 19:14:31 +00001406 matches = rl_completion_matches (text, hostname_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00001407
1408 /* And last, (but not least) if this word is in a command position, then
1409 complete over possible command names, including aliases, functions,
1410 and command names. */
Jari Aalto06285672006-10-10 14:15:34 +00001411 if (matches == 0 && (compflags & DEFCOMP_CMDPOS))
Jari Aalto726f6381996-08-26 18:22:31 +00001412 {
Jari Aalto06285672006-10-10 14:15:34 +00001413 /* If END == START and text[0] == 0, we are trying to complete an empty
1414 command word. */
1415 if (no_empty_command_completion && end == start && text[0] == '\0')
Jari Aaltobb706242000-03-17 21:46:59 +00001416 {
1417 matches = (char **)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001418 rl_ignore_some_completions_function = bash_ignore_everything;
Jari Aaltobb706242000-03-17 21:46:59 +00001419 }
1420 else
1421 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001422#define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x))
1423
Jari Aalto95732b42005-12-07 14:08:12 +00001424 dot_in_path = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001425 matches = rl_completion_matches (text, command_word_completion_function);
Jari Aaltob80f6442004-07-27 13:29:18 +00001426
Jari Aaltobb706242000-03-17 21:46:59 +00001427 /* If we are attempting command completion and nothing matches, we
1428 do not want readline to perform filename completion for us. We
1429 still want to be able to complete partial pathnames, so set the
1430 completion ignore function to something which will remove
1431 filenames and leave directories in the match list. */
1432 if (matches == (char **)NULL)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001433 rl_ignore_some_completions_function = bash_ignore_filenames;
Jari Aalto95732b42005-12-07 14:08:12 +00001434 else if (matches[1] == 0 && CMD_IS_DIR(matches[0]) && dot_in_path == 0)
1435 /* If we found a single match, without looking in the current
1436 directory (because it's not in $PATH), but the found name is
1437 also a command in the current directory, suppress appending any
1438 terminating character, since it's ambiguous. */
1439 {
1440 rl_completion_suppress_append = 1;
1441 rl_filename_completion_desired = 0;
1442 }
Jari Aaltob80f6442004-07-27 13:29:18 +00001443 else if (matches[0] && matches[1] && STREQ (matches[0], matches[1]) && CMD_IS_DIR (matches[0]))
Jari Aalto7117c2d2002-07-17 14:10:11 +00001444 /* There are multiple instances of the same match (duplicate
1445 completions haven't yet been removed). In this case, all of
1446 the matches will be the same, and the duplicate removal code
Jari Aalto95732b42005-12-07 14:08:12 +00001447 will distill them all down to one. We turn on
1448 rl_completion_suppress_append for the same reason as above.
Jari Aalto7117c2d2002-07-17 14:10:11 +00001449 Remember: we only care if there's eventually a single unique
1450 completion. If there are multiple completions this won't
1451 make a difference and the problem won't occur. */
Jari Aalto95732b42005-12-07 14:08:12 +00001452 {
1453 rl_completion_suppress_append = 1;
1454 rl_filename_completion_desired = 0;
1455 }
Jari Aaltobb706242000-03-17 21:46:59 +00001456 }
Jari Aalto726f6381996-08-26 18:22:31 +00001457 }
1458
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001459 /* This could be a globbing pattern, so try to expand it using pathname
1460 expansion. */
Jari Aaltof73dda02001-11-13 17:56:06 +00001461 if (!matches && glob_pattern_p (text))
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001462 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00001463 matches = rl_completion_matches (text, glob_complete_word);
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001464 /* A glob expression that matches more than one filename is problematic.
1465 If we match more than one filename, punt. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001466 if (matches && matches[1] && rl_completion_type == TAB)
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001467 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00001468 strvec_dispose (matches);
Jari Aaltoe8ce7751997-09-22 20:22:27 +00001469 matches = (char **)0;
1470 }
1471 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001472
Jari Aalto726f6381996-08-26 18:22:31 +00001473 return (matches);
1474}
1475
1476/* This is the function to call when the word to complete is in a position
1477 where a command word can be found. It grovels $PATH, looking for commands
1478 that match. It also scans aliases, function names, and the shell_builtin
1479 table. */
Jari Aaltobb706242000-03-17 21:46:59 +00001480char *
Jari Aalto726f6381996-08-26 18:22:31 +00001481command_word_completion_function (hint_text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001482 const char *hint_text;
Jari Aalto726f6381996-08-26 18:22:31 +00001483 int state;
1484{
1485 static char *hint = (char *)NULL;
1486 static char *path = (char *)NULL;
1487 static char *val = (char *)NULL;
1488 static char *filename_hint = (char *)NULL;
Jari Aalto95732b42005-12-07 14:08:12 +00001489 static char *dequoted_hint = (char *)NULL;
Jari Aalto31859422009-01-12 13:36:28 +00001490 static char *directory_part = (char *)NULL;
1491 static char **glob_matches = (char **)NULL;
Jari Aalto95732b42005-12-07 14:08:12 +00001492 static int path_index, hint_len, dequoted_len, istate, igncase;
Jari Aalto06285672006-10-10 14:15:34 +00001493 static int mapping_over, local_index, searching_path, hint_is_dir;
Jari Aalto31859422009-01-12 13:36:28 +00001494 static int old_glob_ignore_case, globpat;
Jari Aalto726f6381996-08-26 18:22:31 +00001495 static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
1496#if defined (ALIAS)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001497 static alias_t **alias_list = (alias_t **)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00001498#endif /* ALIAS */
Jari Aalto95732b42005-12-07 14:08:12 +00001499 char *temp;
Jari Aalto726f6381996-08-26 18:22:31 +00001500
1501 /* We have to map over the possibilities for command words. If we have
1502 no state, then make one just for that purpose. */
Jari Aalto31859422009-01-12 13:36:28 +00001503 if (state == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00001504 {
Jari Aalto95732b42005-12-07 14:08:12 +00001505 if (dequoted_hint && dequoted_hint != hint)
1506 free (dequoted_hint);
Jari Aalto726f6381996-08-26 18:22:31 +00001507 if (hint)
1508 free (hint);
1509
Jari Aalto06285672006-10-10 14:15:34 +00001510 mapping_over = searching_path = 0;
1511 hint_is_dir = CMD_IS_DIR (hint_text);
Jari Aalto726f6381996-08-26 18:22:31 +00001512 val = (char *)NULL;
1513
Jari Aalto95732b42005-12-07 14:08:12 +00001514 temp = rl_variable_value ("completion-ignore-case");
Jari Aalto31859422009-01-12 13:36:28 +00001515 igncase = RL_BOOLEAN_VARIABLE_VALUE (temp);
1516
1517 if (glob_matches)
1518 {
1519 free (glob_matches);
1520 glob_matches = (char **)NULL;
1521 }
1522
1523 globpat = glob_pattern_p (hint_text);
Jari Aalto95732b42005-12-07 14:08:12 +00001524
Jari Aalto726f6381996-08-26 18:22:31 +00001525 /* If this is an absolute program name, do not check it against
1526 aliases, reserved words, functions or builtins. We must check
1527 whether or not it is unique, and, if so, whether that filename
1528 is executable. */
Jari Aalto31859422009-01-12 13:36:28 +00001529 if (globpat || absolute_program (hint_text))
Jari Aalto726f6381996-08-26 18:22:31 +00001530 {
1531 /* Perform tilde expansion on what's passed, so we don't end up
1532 passing filenames with tildes directly to stat(). */
1533 if (*hint_text == '~')
Jari Aalto31859422009-01-12 13:36:28 +00001534 {
1535 hint = bash_tilde_expand (hint_text, 0);
1536 directory_part = savestring (hint_text);
1537 temp = strchr (directory_part, '/');
1538 if (temp)
1539 *temp = 0;
1540 else
1541 {
1542 free (directory_part);
1543 directory_part = (char *)NULL;
1544 }
1545 }
Jari Aalto726f6381996-08-26 18:22:31 +00001546 else
1547 hint = savestring (hint_text);
Jari Aalto95732b42005-12-07 14:08:12 +00001548
1549 dequoted_hint = hint;
1550 /* If readline's completer found a quote character somewhere, but
1551 didn't set the quote character, there must have been a quote
1552 character embedded in the filename. It can't be at the start of
1553 the filename, so we need to dequote the filename before we look
1554 in the file system for it. */
1555 if (rl_completion_found_quote && rl_completion_quote_character == 0)
1556 {
1557 dequoted_hint = bash_dequote_filename (hint, 0);
1558 free (hint);
1559 hint = dequoted_hint;
1560 }
1561 dequoted_len = hint_len = strlen (hint);
Jari Aalto726f6381996-08-26 18:22:31 +00001562
1563 if (filename_hint)
1564 free (filename_hint);
Jari Aalto95732b42005-12-07 14:08:12 +00001565
Jari Aalto726f6381996-08-26 18:22:31 +00001566 filename_hint = savestring (hint);
1567
Jari Aalto726f6381996-08-26 18:22:31 +00001568 istate = 0;
Jari Aalto31859422009-01-12 13:36:28 +00001569
1570 if (globpat)
1571 {
1572 mapping_over = 5;
1573 goto globword;
1574 }
1575 else
1576 {
1577 mapping_over = 4;
1578 goto inner;
1579 }
Jari Aalto726f6381996-08-26 18:22:31 +00001580 }
1581
Jari Aalto95732b42005-12-07 14:08:12 +00001582 dequoted_hint = hint = savestring (hint_text);
1583 dequoted_len = hint_len = strlen (hint);
Jari Aalto726f6381996-08-26 18:22:31 +00001584
Jari Aalto95732b42005-12-07 14:08:12 +00001585 if (rl_completion_found_quote && rl_completion_quote_character == 0)
1586 {
1587 dequoted_hint = bash_dequote_filename (hint, 0);
1588 dequoted_len = strlen (dequoted_hint);
1589 }
1590
Jari Aalto726f6381996-08-26 18:22:31 +00001591 path = get_string_value ("PATH");
Jari Aalto95732b42005-12-07 14:08:12 +00001592 path_index = dot_in_path = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001593
1594 /* Initialize the variables for each type of command word. */
1595 local_index = 0;
1596
1597 if (varlist)
1598 free (varlist);
1599
1600 varlist = all_visible_functions ();
1601
1602#if defined (ALIAS)
1603 if (alias_list)
1604 free (alias_list);
1605
1606 alias_list = all_aliases ();
1607#endif /* ALIAS */
1608 }
1609
1610 /* mapping_over says what we are currently hacking. Note that every case
1611 in this list must fall through when there are no more possibilities. */
1612
1613 switch (mapping_over)
1614 {
1615 case 0: /* Aliases come first. */
1616#if defined (ALIAS)
1617 while (alias_list && alias_list[local_index])
1618 {
1619 register char *alias;
1620
1621 alias = alias_list[local_index++]->name;
1622
1623 if (STREQN (alias, hint, hint_len))
1624 return (savestring (alias));
1625 }
1626#endif /* ALIAS */
1627 local_index = 0;
1628 mapping_over++;
1629
1630 case 1: /* Then shell reserved words. */
1631 {
1632 while (word_token_alist[local_index].word)
1633 {
1634 register char *reserved_word;
1635
1636 reserved_word = word_token_alist[local_index++].word;
1637
1638 if (STREQN (reserved_word, hint, hint_len))
1639 return (savestring (reserved_word));
1640 }
1641 local_index = 0;
1642 mapping_over++;
1643 }
1644
1645 case 2: /* Then function names. */
1646 while (varlist && varlist[local_index])
1647 {
1648 register char *varname;
1649
1650 varname = varlist[local_index++]->name;
1651
1652 if (STREQN (varname, hint, hint_len))
1653 return (savestring (varname));
1654 }
1655 local_index = 0;
1656 mapping_over++;
1657
1658 case 3: /* Then shell builtins. */
1659 for (; local_index < num_shell_builtins; local_index++)
1660 {
1661 /* Ignore it if it doesn't have a function pointer or if it
1662 is not currently enabled. */
1663 if (!shell_builtins[local_index].function ||
1664 (shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0)
1665 continue;
1666
1667 if (STREQN (shell_builtins[local_index].name, hint, hint_len))
1668 {
1669 int i = local_index++;
1670
1671 return (savestring (shell_builtins[i].name));
1672 }
1673 }
1674 local_index = 0;
1675 mapping_over++;
1676 }
1677
Jari Aalto31859422009-01-12 13:36:28 +00001678globword:
1679 /* Limited support for completing command words with globbing chars. Only
1680 a single match (multiple matches that end up reducing the number of
1681 characters in the common prefix are bad) will ever be returned on
1682 regular completion. */
Chet Ramey30d188c2011-11-21 20:57:16 -05001683 if (globpat)
Jari Aalto31859422009-01-12 13:36:28 +00001684 {
1685 if (state == 0)
1686 {
1687 glob_ignore_case = igncase;
1688 glob_matches = shell_glob_filename (hint);
1689 glob_ignore_case = old_glob_ignore_case;
1690
1691 if (GLOB_FAILED (glob_matches) || glob_matches == 0)
1692 {
1693 glob_matches = (char **)NULL;
1694 return ((char *)NULL);
1695 }
1696
1697 local_index = 0;
1698
1699 if (glob_matches[1] && rl_completion_type == TAB) /* multiple matches are bad */
1700 return ((char *)NULL);
1701 }
1702
1703 while (val = glob_matches[local_index++])
1704 {
1705 if (executable_or_directory (val))
1706 {
1707 if (*hint_text == '~')
1708 {
1709 temp = restore_tilde (val, directory_part);
1710 free (val);
1711 val = temp;
1712 }
1713 return (val);
1714 }
1715 free (val);
1716 }
1717
1718 glob_ignore_case = old_glob_ignore_case;
1719 return ((char *)NULL);
1720 }
1721
Jari Aalto06285672006-10-10 14:15:34 +00001722 /* If the text passed is a directory in the current directory, return it
1723 as a possible match. Executables in directories in the current
1724 directory can be specified using relative pathnames and successfully
1725 executed even when `.' is not in $PATH. */
1726 if (hint_is_dir)
1727 {
1728 hint_is_dir = 0; /* only return the hint text once */
1729 return (savestring (hint_text));
1730 }
1731
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001732 /* Repeatedly call filename_completion_function while we have
Jari Aalto726f6381996-08-26 18:22:31 +00001733 members of PATH left. Question: should we stat each file?
1734 Answer: we call executable_file () on each file. */
1735 outer:
1736
1737 istate = (val != (char *)NULL);
1738
Jari Aalto31859422009-01-12 13:36:28 +00001739 if (istate == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00001740 {
1741 char *current_path;
1742
1743 /* Get the next directory from the path. If there is none, then we
1744 are all done. */
Jari Aalto31859422009-01-12 13:36:28 +00001745 if (path == 0 || path[path_index] == 0 ||
Jari Aalto726f6381996-08-26 18:22:31 +00001746 (current_path = extract_colon_unit (path, &path_index)) == 0)
1747 return ((char *)NULL);
1748
Jari Aalto06285672006-10-10 14:15:34 +00001749 searching_path = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001750 if (*current_path == 0)
1751 {
1752 free (current_path);
1753 current_path = savestring (".");
1754 }
1755
1756 if (*current_path == '~')
1757 {
1758 char *t;
1759
Jari Aalto7117c2d2002-07-17 14:10:11 +00001760 t = bash_tilde_expand (current_path, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00001761 free (current_path);
1762 current_path = t;
1763 }
1764
Jari Aalto95732b42005-12-07 14:08:12 +00001765 if (current_path[0] == '.' && current_path[1] == '\0')
1766 dot_in_path = 1;
1767
Jari Aalto726f6381996-08-26 18:22:31 +00001768 if (filename_hint)
1769 free (filename_hint);
1770
Jari Aalto7117c2d2002-07-17 14:10:11 +00001771 filename_hint = sh_makepath (current_path, hint, 0);
Jari Aalto31859422009-01-12 13:36:28 +00001772 free (current_path); /* XXX */
Jari Aalto726f6381996-08-26 18:22:31 +00001773 }
1774
1775 inner:
Jari Aalto28ef6c32001-04-06 19:14:31 +00001776 val = rl_filename_completion_function (filename_hint, istate);
Jari Aalto726f6381996-08-26 18:22:31 +00001777 istate = 1;
1778
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001779 if (val == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00001780 {
1781 /* If the hint text is an absolute program, then don't bother
1782 searching through PATH. */
1783 if (absolute_program (hint))
1784 return ((char *)NULL);
1785
1786 goto outer;
1787 }
1788 else
1789 {
Jari Aaltod166f041997-06-05 14:59:13 +00001790 int match, freetemp;
Jari Aalto726f6381996-08-26 18:22:31 +00001791
1792 if (absolute_program (hint))
1793 {
Jari Aalto95732b42005-12-07 14:08:12 +00001794 if (igncase == 0)
1795 match = strncmp (val, hint, hint_len) == 0;
1796 else
1797 match = strncasecmp (val, hint, hint_len) == 0;
1798
Jari Aalto726f6381996-08-26 18:22:31 +00001799 /* If we performed tilde expansion, restore the original
1800 filename. */
1801 if (*hint_text == '~')
Jari Aalto31859422009-01-12 13:36:28 +00001802 temp = restore_tilde (val, directory_part);
Jari Aalto726f6381996-08-26 18:22:31 +00001803 else
1804 temp = savestring (val);
Jari Aaltod166f041997-06-05 14:59:13 +00001805 freetemp = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001806 }
1807 else
1808 {
1809 temp = strrchr (val, '/');
1810
1811 if (temp)
1812 {
1813 temp++;
Jari Aalto95732b42005-12-07 14:08:12 +00001814 if (igncase == 0)
1815 freetemp = match = strncmp (temp, hint, hint_len) == 0;
1816 else
1817 freetemp = match = strncasecmp (temp, hint, hint_len) == 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001818 if (match)
1819 temp = savestring (temp);
1820 }
1821 else
Jari Aaltod166f041997-06-05 14:59:13 +00001822 freetemp = match = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001823 }
1824
Jari Aalto06285672006-10-10 14:15:34 +00001825#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001826 /* If we have found a match, and it is an executable file or a
1827 directory name, return it. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001828 if (match && executable_or_directory (val))
Jari Aalto06285672006-10-10 14:15:34 +00001829#else
1830 /* If we have found a match, and it is an executable file, return it.
1831 We don't return directory names when searching $PATH, since the
1832 bash execution code won't find executables in directories which
1833 appear in directories in $PATH when they're specified using
1834 relative pathnames. */
1835 if (match && (searching_path ? executable_file (val) : executable_or_directory (val)))
1836#endif
Jari Aalto726f6381996-08-26 18:22:31 +00001837 {
1838 free (val);
1839 val = ""; /* So it won't be NULL. */
1840 return (temp);
1841 }
1842 else
1843 {
Jari Aaltod166f041997-06-05 14:59:13 +00001844 if (freetemp)
1845 free (temp);
Jari Aalto726f6381996-08-26 18:22:31 +00001846 free (val);
1847 goto inner;
1848 }
1849 }
1850}
1851
Jari Aaltod166f041997-06-05 14:59:13 +00001852/* Completion inside an unterminated command substitution. */
Jari Aalto726f6381996-08-26 18:22:31 +00001853static char *
1854command_subst_completion_function (text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001855 const char *text;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001856 int state;
Jari Aalto726f6381996-08-26 18:22:31 +00001857{
1858 static char **matches = (char **)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +00001859 static const char *orig_start;
1860 static char *filename_text = (char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00001861 static int cmd_index, start_len;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001862 char *value;
Jari Aalto726f6381996-08-26 18:22:31 +00001863
1864 if (state == 0)
1865 {
1866 if (filename_text)
1867 free (filename_text);
1868 orig_start = text;
1869 if (*text == '`')
Jari Aalto28ef6c32001-04-06 19:14:31 +00001870 text++;
Jari Aaltocce855b1998-04-17 19:52:44 +00001871 else if (*text == '$' && text[1] == '(') /* ) */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001872 text += 2;
Jari Aaltob80f6442004-07-27 13:29:18 +00001873 /* If the text was quoted, suppress any quote character that the
1874 readline completion code would insert. */
1875 rl_completion_suppress_quote = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001876 start_len = text - orig_start;
1877 filename_text = savestring (text);
1878 if (matches)
1879 free (matches);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001880
1881 /*
1882 * At this point we can entertain the idea of re-parsing
1883 * `filename_text' into a (possibly incomplete) command name and
1884 * arguments, and doing completion based on that. This is
1885 * currently very rudimentary, but it is a small improvement.
1886 */
1887 for (value = filename_text + strlen (filename_text) - 1; value > filename_text; value--)
1888 if (whitespace (*value) || member (*value, COMMAND_SEPARATORS))
1889 break;
1890 if (value <= filename_text)
1891 matches = rl_completion_matches (filename_text, command_word_completion_function);
1892 else
1893 {
1894 value++;
1895 start_len += value - filename_text;
1896 if (whitespace (value[-1]))
1897 matches = rl_completion_matches (value, rl_filename_completion_function);
1898 else
1899 matches = rl_completion_matches (value, command_word_completion_function);
1900 }
1901
1902 /* If there is more than one match, rl_completion_matches has already
1903 put the lcd in matches[0]. Skip over it. */
1904 cmd_index = matches && matches[0] && matches[1];
Jari Aalto95732b42005-12-07 14:08:12 +00001905
1906 /* If there's a single match and it's a directory, set the append char
1907 to the expected `/'. Otherwise, don't append anything. */
1908 if (matches && matches[0] && matches[1] == 0 && test_for_directory (matches[0]))
1909 rl_completion_append_character = '/';
1910 else
1911 rl_completion_suppress_append = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001912 }
1913
1914 if (!matches || !matches[cmd_index])
1915 {
1916 rl_filename_quoting_desired = 0; /* disable quoting */
1917 return ((char *)NULL);
1918 }
1919 else
1920 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001921 value = (char *)xmalloc (1 + start_len + strlen (matches[cmd_index]));
Jari Aalto726f6381996-08-26 18:22:31 +00001922
1923 if (start_len == 1)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001924 value[0] = *orig_start;
Jari Aalto726f6381996-08-26 18:22:31 +00001925 else
Jari Aalto28ef6c32001-04-06 19:14:31 +00001926 strncpy (value, orig_start, start_len);
Jari Aalto726f6381996-08-26 18:22:31 +00001927
1928 strcpy (value + start_len, matches[cmd_index]);
1929
1930 cmd_index++;
1931 return (value);
1932 }
1933}
1934
1935/* Okay, now we write the entry_function for variable completion. */
1936static char *
1937variable_completion_function (text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001938 const char *text;
Jari Aalto726f6381996-08-26 18:22:31 +00001939 int state;
Jari Aalto726f6381996-08-26 18:22:31 +00001940{
Jari Aaltobb706242000-03-17 21:46:59 +00001941 static char **varlist = (char **)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00001942 static int varlist_index;
1943 static char *varname = (char *)NULL;
1944 static int namelen;
1945 static int first_char, first_char_loc;
1946
1947 if (!state)
1948 {
1949 if (varname)
1950 free (varname);
1951
1952 first_char_loc = 0;
1953 first_char = text[0];
1954
1955 if (first_char == '$')
1956 first_char_loc++;
1957
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001958 if (text[first_char_loc] == '{')
Jari Aalto28ef6c32001-04-06 19:14:31 +00001959 first_char_loc++;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001960
Jari Aalto726f6381996-08-26 18:22:31 +00001961 varname = savestring (text + first_char_loc);
1962
1963 namelen = strlen (varname);
1964 if (varlist)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001965 strvec_dispose (varlist);
Jari Aaltobb706242000-03-17 21:46:59 +00001966
1967 varlist = all_variables_matching_prefix (varname);
Jari Aalto726f6381996-08-26 18:22:31 +00001968 varlist_index = 0;
1969 }
1970
Jari Aalto726f6381996-08-26 18:22:31 +00001971 if (!varlist || !varlist[varlist_index])
1972 {
1973 return ((char *)NULL);
1974 }
1975 else
1976 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001977 char *value;
1978
1979 value = (char *)xmalloc (4 + strlen (varlist[varlist_index]));
Jari Aalto726f6381996-08-26 18:22:31 +00001980
1981 if (first_char_loc)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001982 {
1983 value[0] = first_char;
1984 if (first_char_loc == 2)
1985 value[1] = '{';
1986 }
Jari Aalto726f6381996-08-26 18:22:31 +00001987
Jari Aaltobb706242000-03-17 21:46:59 +00001988 strcpy (value + first_char_loc, varlist[varlist_index]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001989 if (first_char_loc == 2)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001990 strcat (value, "}");
Jari Aalto726f6381996-08-26 18:22:31 +00001991
1992 varlist_index++;
1993 return (value);
1994 }
1995}
1996
1997/* How about a completion function for hostnames? */
1998static char *
1999hostname_completion_function (text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002000 const char *text;
Jari Aalto726f6381996-08-26 18:22:31 +00002001 int state;
Jari Aalto726f6381996-08-26 18:22:31 +00002002{
2003 static char **list = (char **)NULL;
2004 static int list_index = 0;
2005 static int first_char, first_char_loc;
2006
2007 /* If we don't have any state, make some. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002008 if (state == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002009 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002010 FREE (list);
Jari Aalto726f6381996-08-26 18:22:31 +00002011
2012 list = (char **)NULL;
2013
2014 first_char_loc = 0;
2015 first_char = *text;
2016
2017 if (first_char == '@')
2018 first_char_loc++;
2019
Jari Aaltof73dda02001-11-13 17:56:06 +00002020 list = hostnames_matching ((char *)text+first_char_loc);
Jari Aalto726f6381996-08-26 18:22:31 +00002021 list_index = 0;
2022 }
2023
2024 if (list && list[list_index])
2025 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002026 char *t;
Jari Aalto726f6381996-08-26 18:22:31 +00002027
Jari Aaltof73dda02001-11-13 17:56:06 +00002028 t = (char *)xmalloc (2 + strlen (list[list_index]));
Jari Aalto726f6381996-08-26 18:22:31 +00002029 *t = first_char;
2030 strcpy (t + first_char_loc, list[list_index]);
2031 list_index++;
2032 return (t);
2033 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002034
2035 return ((char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +00002036}
2037
Jari Aalto7117c2d2002-07-17 14:10:11 +00002038/*
2039 * A completion function for service names from /etc/services (or wherever).
2040 */
2041char *
2042bash_servicename_completion_function (text, state)
2043 const char *text;
2044 int state;
2045{
2046#if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT)
2047 return ((char *)NULL);
2048#else
2049 static char *sname = (char *)NULL;
2050 static struct servent *srvent;
2051 static int snamelen, firstc;
2052 char *value;
2053 char **alist, *aentry;
2054 int afound;
2055
2056 if (state == 0)
2057 {
2058 FREE (sname);
2059 firstc = *text;
2060
2061 sname = savestring (text);
2062 snamelen = strlen (sname);
2063 setservent (0);
2064 }
2065
2066 while (srvent = getservent ())
2067 {
2068 afound = 0;
2069 if (snamelen == 0 || (STREQN (sname, srvent->s_name, snamelen)))
2070 break;
2071 /* Not primary, check aliases */
Jari Aalto06285672006-10-10 14:15:34 +00002072 for (alist = srvent->s_aliases; *alist; alist++)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002073 {
Jari Aalto06285672006-10-10 14:15:34 +00002074 aentry = *alist;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002075 if (STREQN (sname, aentry, snamelen))
2076 {
2077 afound = 1;
2078 break;
2079 }
2080 }
2081
2082 if (afound)
2083 break;
2084 }
2085
2086 if (srvent == 0)
2087 {
2088 endservent ();
2089 return ((char *)NULL);
2090 }
2091
2092 value = afound ? savestring (aentry) : savestring (srvent->s_name);
2093 return value;
2094#endif
2095}
2096
2097/*
2098 * A completion function for group names from /etc/group (or wherever).
2099 */
Jari Aaltof73dda02001-11-13 17:56:06 +00002100char *
2101bash_groupname_completion_function (text, state)
2102 const char *text;
2103 int state;
2104{
2105#if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
2106 return ((char *)NULL);
2107#else
2108 static char *gname = (char *)NULL;
2109 static struct group *grent;
2110 static int gnamelen;
2111 char *value;
2112
2113 if (state == 0)
2114 {
2115 FREE (gname);
2116 gname = savestring (text);
2117 gnamelen = strlen (gname);
2118
2119 setgrent ();
2120 }
2121
2122 while (grent = getgrent ())
2123 {
2124 if (gnamelen == 0 || (STREQN (gname, grent->gr_name, gnamelen)))
2125 break;
2126 }
2127
2128 if (grent == 0)
2129 {
2130 endgrent ();
2131 return ((char *)NULL);
2132 }
2133
2134 value = savestring (grent->gr_name);
2135 return (value);
2136#endif
2137}
2138
Jari Aaltocce855b1998-04-17 19:52:44 +00002139/* Functions to perform history and alias expansions on the current line. */
2140
2141#if defined (BANG_HISTORY)
2142/* Perform history expansion on the current line. If no history expansion
2143 is done, pre_process_line() returns what it was passed, so we need to
2144 allocate a new line here. */
Jari Aalto726f6381996-08-26 18:22:31 +00002145static char *
2146history_expand_line_internal (line)
2147 char *line;
2148{
2149 char *new_line;
Jari Aaltob80f6442004-07-27 13:29:18 +00002150 int old_verify;
Jari Aalto726f6381996-08-26 18:22:31 +00002151
Jari Aaltob80f6442004-07-27 13:29:18 +00002152 old_verify = hist_verify;
2153 hist_verify = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002154 new_line = pre_process_line (line, 0, 0);
Jari Aaltob80f6442004-07-27 13:29:18 +00002155 hist_verify = old_verify;
2156
Jari Aaltod166f041997-06-05 14:59:13 +00002157 return (new_line == line) ? savestring (line) : new_line;
Jari Aalto726f6381996-08-26 18:22:31 +00002158}
Jari Aalto726f6381996-08-26 18:22:31 +00002159#endif
2160
2161/* There was an error in expansion. Let the preprocessor print
2162 the error here. */
2163static void
2164cleanup_expansion_error ()
2165{
2166 char *to_free;
Jari Aaltob80f6442004-07-27 13:29:18 +00002167#if defined (BANG_HISTORY)
2168 int old_verify;
2169
2170 old_verify = hist_verify;
2171 hist_verify = 0;
2172#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002173
2174 fprintf (rl_outstream, "\r\n");
2175 to_free = pre_process_line (rl_line_buffer, 1, 0);
Jari Aaltob80f6442004-07-27 13:29:18 +00002176#if defined (BANG_HISTORY)
2177 hist_verify = old_verify;
2178#endif
Jari Aaltod166f041997-06-05 14:59:13 +00002179 if (to_free != rl_line_buffer)
Jari Aaltob80f6442004-07-27 13:29:18 +00002180 FREE (to_free);
Jari Aalto726f6381996-08-26 18:22:31 +00002181 putc ('\r', rl_outstream);
2182 rl_forced_update_display ();
2183}
2184
2185/* If NEW_LINE differs from what is in the readline line buffer, add an
2186 undo record to get from the readline line buffer contents to the new
2187 line and make NEW_LINE the current readline line. */
2188static void
2189maybe_make_readline_line (new_line)
2190 char *new_line;
2191{
2192 if (strcmp (new_line, rl_line_buffer) != 0)
2193 {
2194 rl_point = rl_end;
2195
2196 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2197 rl_delete_text (0, rl_point);
Jari Aalto7117c2d2002-07-17 14:10:11 +00002198 rl_point = rl_end = rl_mark = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002199 rl_insert_text (new_line);
2200 rl_add_undo (UNDO_END, 0, 0, 0);
2201 }
2202}
2203
2204/* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
2205static void
2206set_up_new_line (new_line)
2207 char *new_line;
2208{
Jari Aaltof73dda02001-11-13 17:56:06 +00002209 int old_point, at_end;
2210
2211 old_point = rl_point;
2212 at_end = rl_point == rl_end;
Jari Aalto726f6381996-08-26 18:22:31 +00002213
2214 /* If the line was history and alias expanded, then make that
2215 be one thing to undo. */
2216 maybe_make_readline_line (new_line);
2217 free (new_line);
2218
2219 /* Place rl_point where we think it should go. */
2220 if (at_end)
2221 rl_point = rl_end;
2222 else if (old_point < rl_end)
2223 {
2224 rl_point = old_point;
2225 if (!whitespace (rl_line_buffer[rl_point]))
Jari Aaltob72432f1999-02-19 17:11:39 +00002226 rl_forward_word (1, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00002227 }
2228}
2229
Jari Aaltocce855b1998-04-17 19:52:44 +00002230#if defined (ALIAS)
2231/* Expand aliases in the current readline line. */
2232static int
Jari Aalto28ef6c32001-04-06 19:14:31 +00002233alias_expand_line (count, ignore)
2234 int count, ignore;
Jari Aaltocce855b1998-04-17 19:52:44 +00002235{
2236 char *new_line;
2237
2238 new_line = alias_expand (rl_line_buffer);
2239
2240 if (new_line)
2241 {
2242 set_up_new_line (new_line);
2243 return (0);
2244 }
2245 else
2246 {
2247 cleanup_expansion_error ();
2248 return (1);
2249 }
2250}
2251#endif
2252
2253#if defined (BANG_HISTORY)
Jari Aalto726f6381996-08-26 18:22:31 +00002254/* History expand the line. */
Jari Aaltocce855b1998-04-17 19:52:44 +00002255static int
Jari Aalto28ef6c32001-04-06 19:14:31 +00002256history_expand_line (count, ignore)
2257 int count, ignore;
Jari Aalto726f6381996-08-26 18:22:31 +00002258{
2259 char *new_line;
2260
2261 new_line = history_expand_line_internal (rl_line_buffer);
2262
2263 if (new_line)
Jari Aaltocce855b1998-04-17 19:52:44 +00002264 {
2265 set_up_new_line (new_line);
2266 return (0);
2267 }
Jari Aalto726f6381996-08-26 18:22:31 +00002268 else
Jari Aaltocce855b1998-04-17 19:52:44 +00002269 {
2270 cleanup_expansion_error ();
2271 return (1);
2272 }
Jari Aalto726f6381996-08-26 18:22:31 +00002273}
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002274
Jari Aaltocce855b1998-04-17 19:52:44 +00002275/* Expand history substitutions in the current line and then insert a
Jari Aalto28ef6c32001-04-06 19:14:31 +00002276 space (hopefully close to where we were before). */
Jari Aaltocce855b1998-04-17 19:52:44 +00002277static int
Jari Aalto28ef6c32001-04-06 19:14:31 +00002278tcsh_magic_space (count, ignore)
2279 int count, ignore;
Jari Aaltocce855b1998-04-17 19:52:44 +00002280{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002281 int dist_from_end, old_point;
2282
2283 old_point = rl_point;
2284 dist_from_end = rl_end - rl_point;
2285 if (history_expand_line (count, ignore) == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00002286 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00002287 /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
2288 This works if all expansions were before rl_point or if no expansions
2289 were performed. */
2290 rl_point = (old_point == 0) ? old_point : rl_end - dist_from_end;
Jari Aaltocce855b1998-04-17 19:52:44 +00002291 rl_insert (1, ' ');
2292 return (0);
2293 }
2294 else
2295 return (1);
2296}
Jari Aalto95732b42005-12-07 14:08:12 +00002297#endif /* BANG_HISTORY */
Jari Aaltocce855b1998-04-17 19:52:44 +00002298
Jari Aalto726f6381996-08-26 18:22:31 +00002299/* History and alias expand the line. */
Jari Aaltocce855b1998-04-17 19:52:44 +00002300static int
Jari Aalto28ef6c32001-04-06 19:14:31 +00002301history_and_alias_expand_line (count, ignore)
2302 int count, ignore;
Jari Aalto726f6381996-08-26 18:22:31 +00002303{
2304 char *new_line;
2305
Jari Aalto95732b42005-12-07 14:08:12 +00002306 new_line = 0;
2307#if defined (BANG_HISTORY)
Jari Aaltob80f6442004-07-27 13:29:18 +00002308 new_line = history_expand_line_internal (rl_line_buffer);
Jari Aalto95732b42005-12-07 14:08:12 +00002309#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002310
2311#if defined (ALIAS)
2312 if (new_line)
2313 {
2314 char *alias_line;
2315
2316 alias_line = alias_expand (new_line);
2317 free (new_line);
2318 new_line = alias_line;
2319 }
2320#endif /* ALIAS */
2321
2322 if (new_line)
Jari Aaltocce855b1998-04-17 19:52:44 +00002323 {
2324 set_up_new_line (new_line);
2325 return (0);
2326 }
Jari Aalto726f6381996-08-26 18:22:31 +00002327 else
Jari Aaltocce855b1998-04-17 19:52:44 +00002328 {
2329 cleanup_expansion_error ();
2330 return (1);
2331 }
Jari Aalto726f6381996-08-26 18:22:31 +00002332}
2333
2334/* History and alias expand the line, then perform the shell word
Jari Aaltocce855b1998-04-17 19:52:44 +00002335 expansions by calling expand_string. This can't use set_up_new_line()
2336 because we want the variable expansions as a separate undo'able
2337 set of operations. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00002338static int
2339shell_expand_line (count, ignore)
2340 int count, ignore;
Jari Aalto726f6381996-08-26 18:22:31 +00002341{
2342 char *new_line;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002343 WORD_LIST *expanded_string;
Jari Aalto726f6381996-08-26 18:22:31 +00002344
Jari Aalto95732b42005-12-07 14:08:12 +00002345 new_line = 0;
2346#if defined (BANG_HISTORY)
Jari Aaltob80f6442004-07-27 13:29:18 +00002347 new_line = history_expand_line_internal (rl_line_buffer);
Jari Aalto95732b42005-12-07 14:08:12 +00002348#endif
Jari Aalto726f6381996-08-26 18:22:31 +00002349
2350#if defined (ALIAS)
2351 if (new_line)
2352 {
2353 char *alias_line;
2354
2355 alias_line = alias_expand (new_line);
2356 free (new_line);
2357 new_line = alias_line;
2358 }
2359#endif /* ALIAS */
2360
2361 if (new_line)
2362 {
2363 int old_point = rl_point;
2364 int at_end = rl_point == rl_end;
2365
2366 /* If the line was history and alias expanded, then make that
2367 be one thing to undo. */
2368 maybe_make_readline_line (new_line);
2369 free (new_line);
2370
2371 /* If there is variable expansion to perform, do that as a separate
2372 operation to be undone. */
Jari Aaltod166f041997-06-05 14:59:13 +00002373 new_line = savestring (rl_line_buffer);
2374 expanded_string = expand_string (new_line, 0);
2375 FREE (new_line);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002376 if (expanded_string == 0)
2377 {
Jari Aaltof73dda02001-11-13 17:56:06 +00002378 new_line = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002379 new_line[0] = '\0';
2380 }
2381 else
2382 {
2383 new_line = string_list (expanded_string);
2384 dispose_words (expanded_string);
2385 }
Jari Aalto726f6381996-08-26 18:22:31 +00002386
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002387 maybe_make_readline_line (new_line);
2388 free (new_line);
Jari Aalto726f6381996-08-26 18:22:31 +00002389
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002390 /* Place rl_point where we think it should go. */
2391 if (at_end)
2392 rl_point = rl_end;
2393 else if (old_point < rl_end)
2394 {
2395 rl_point = old_point;
2396 if (!whitespace (rl_line_buffer[rl_point]))
Jari Aaltob72432f1999-02-19 17:11:39 +00002397 rl_forward_word (1, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002398 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00002399 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002400 }
2401 else
Jari Aalto28ef6c32001-04-06 19:14:31 +00002402 {
2403 cleanup_expansion_error ();
2404 return 1;
2405 }
Jari Aalto726f6381996-08-26 18:22:31 +00002406}
2407
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002408/* If FIGNORE is set, then don't match files with the given suffixes when
2409 completing filenames. If only one of the possibilities has an acceptable
Jari Aalto726f6381996-08-26 18:22:31 +00002410 suffix, delete the others, else just return and let the completer
2411 signal an error. It is called by the completer when real
2412 completions are done on filenames by the completer's internal
2413 function, not for completion lists (M-?) and not on "other"
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002414 completion types, such as hostnames or commands. */
Jari Aalto726f6381996-08-26 18:22:31 +00002415
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002416static struct ignorevar fignore =
2417{
2418 "FIGNORE",
2419 (struct ign *)0,
2420 0,
2421 (char *)0,
Jari Aaltof73dda02001-11-13 17:56:06 +00002422 (sh_iv_item_func_t *) 0,
Jari Aalto726f6381996-08-26 18:22:31 +00002423};
2424
Jari Aalto726f6381996-08-26 18:22:31 +00002425static void
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002426_ignore_completion_names (names, name_func)
Jari Aalto726f6381996-08-26 18:22:31 +00002427 char **names;
Jari Aaltof73dda02001-11-13 17:56:06 +00002428 sh_ignore_func_t *name_func;
Jari Aalto726f6381996-08-26 18:22:31 +00002429{
2430 char **newnames;
2431 int idx, nidx;
Jari Aaltocce855b1998-04-17 19:52:44 +00002432 char **oldnames;
2433 int oidx;
Jari Aalto726f6381996-08-26 18:22:31 +00002434
2435 /* If there is only one completion, see if it is acceptable. If it is
2436 not, free it up. In any case, short-circuit and return. This is a
2437 special case because names[0] is not the prefix of the list of names
2438 if there is only one completion; it is the completion itself. */
2439 if (names[1] == (char *)0)
2440 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002441 if (force_fignore)
2442 if ((*name_func) (names[0]) == 0)
2443 {
2444 free (names[0]);
2445 names[0] = (char *)NULL;
2446 }
2447
Jari Aalto726f6381996-08-26 18:22:31 +00002448 return;
2449 }
2450
2451 /* Allocate space for array to hold list of pointers to matching
2452 filenames. The pointers are copied back to NAMES when done. */
2453 for (nidx = 1; names[nidx]; nidx++)
2454 ;
Jari Aalto7117c2d2002-07-17 14:10:11 +00002455 newnames = strvec_create (nidx + 1);
Jari Aaltob80f6442004-07-27 13:29:18 +00002456
2457 if (force_fignore == 0)
2458 {
2459 oldnames = strvec_create (nidx - 1);
2460 oidx = 0;
2461 }
Jari Aalto726f6381996-08-26 18:22:31 +00002462
2463 newnames[0] = names[0];
2464 for (idx = nidx = 1; names[idx]; idx++)
2465 {
2466 if ((*name_func) (names[idx]))
2467 newnames[nidx++] = names[idx];
Jari Aaltob80f6442004-07-27 13:29:18 +00002468 else if (force_fignore == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00002469 oldnames[oidx++] = names[idx];
Jari Aaltob80f6442004-07-27 13:29:18 +00002470 else
2471 free (names[idx]);
Jari Aalto726f6381996-08-26 18:22:31 +00002472 }
2473
2474 newnames[nidx] = (char *)NULL;
2475
2476 /* If none are acceptable then let the completer handle it. */
2477 if (nidx == 1)
2478 {
Jari Aaltob80f6442004-07-27 13:29:18 +00002479 if (force_fignore)
2480 {
2481 free (names[0]);
2482 names[0] = (char *)NULL;
2483 }
2484 else
2485 free (oldnames);
2486
Jari Aalto726f6381996-08-26 18:22:31 +00002487 free (newnames);
2488 return;
2489 }
2490
Jari Aaltob80f6442004-07-27 13:29:18 +00002491 if (force_fignore == 0)
2492 {
2493 while (oidx)
2494 free (oldnames[--oidx]);
2495 free (oldnames);
2496 }
Jari Aaltocce855b1998-04-17 19:52:44 +00002497
Jari Aalto726f6381996-08-26 18:22:31 +00002498 /* If only one is acceptable, copy it to names[0] and return. */
2499 if (nidx == 2)
2500 {
2501 free (names[0]);
2502 names[0] = newnames[1];
2503 names[1] = (char *)NULL;
2504 free (newnames);
2505 return;
2506 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002507
Jari Aalto726f6381996-08-26 18:22:31 +00002508 /* Copy the acceptable names back to NAMES, set the new array end,
2509 and return. */
2510 for (nidx = 1; newnames[nidx]; nidx++)
2511 names[nidx] = newnames[nidx];
2512 names[nidx] = (char *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002513 free (newnames);
2514}
2515
2516static int
2517name_is_acceptable (name)
Jari Aaltof73dda02001-11-13 17:56:06 +00002518 const char *name;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002519{
2520 struct ign *p;
2521 int nlen;
2522
2523 for (nlen = strlen (name), p = fignore.ignores; p->val; p++)
2524 {
2525 if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len]))
2526 return (0);
2527 }
2528
2529 return (1);
Jari Aalto726f6381996-08-26 18:22:31 +00002530}
2531
Jari Aaltob72432f1999-02-19 17:11:39 +00002532#if 0
2533static int
2534ignore_dot_names (name)
2535 char *name;
2536{
2537 return (name[0] != '.');
2538}
2539#endif
2540
Jari Aalto28ef6c32001-04-06 19:14:31 +00002541static int
Jari Aalto726f6381996-08-26 18:22:31 +00002542filename_completion_ignore (names)
2543 char **names;
2544{
Jari Aaltob72432f1999-02-19 17:11:39 +00002545#if 0
2546 if (glob_dot_filenames == 0)
2547 _ignore_completion_names (names, ignore_dot_names);
2548#endif
2549
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002550 setup_ignore_patterns (&fignore);
Jari Aalto726f6381996-08-26 18:22:31 +00002551
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002552 if (fignore.num_ignores == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002553 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002554
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002555 _ignore_completion_names (names, name_is_acceptable);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002556
2557 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002558}
2559
Jari Aalto31859422009-01-12 13:36:28 +00002560/* Return 1 if NAME is a directory. NAME undergoes tilde expansion. */
Jari Aalto726f6381996-08-26 18:22:31 +00002561static int
2562test_for_directory (name)
Jari Aaltof73dda02001-11-13 17:56:06 +00002563 const char *name;
Jari Aalto726f6381996-08-26 18:22:31 +00002564{
Jari Aalto726f6381996-08-26 18:22:31 +00002565 char *fn;
Jari Aalto31859422009-01-12 13:36:28 +00002566 int r;
Jari Aalto726f6381996-08-26 18:22:31 +00002567
Jari Aalto7117c2d2002-07-17 14:10:11 +00002568 fn = bash_tilde_expand (name, 0);
Jari Aalto31859422009-01-12 13:36:28 +00002569 r = file_isdir (fn);
Jari Aalto726f6381996-08-26 18:22:31 +00002570 free (fn);
Jari Aalto31859422009-01-12 13:36:28 +00002571
2572 return (r);
Jari Aalto726f6381996-08-26 18:22:31 +00002573}
2574
2575/* Remove files from NAMES, leaving directories. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00002576static int
Jari Aalto726f6381996-08-26 18:22:31 +00002577bash_ignore_filenames (names)
2578 char **names;
2579{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002580 _ignore_completion_names (names, test_for_directory);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002581 return 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002582}
2583
Jari Aaltobb706242000-03-17 21:46:59 +00002584static int
2585return_zero (name)
Jari Aaltof73dda02001-11-13 17:56:06 +00002586 const char *name;
Jari Aaltobb706242000-03-17 21:46:59 +00002587{
2588 return 0;
2589}
2590
Jari Aalto28ef6c32001-04-06 19:14:31 +00002591static int
Jari Aaltobb706242000-03-17 21:46:59 +00002592bash_ignore_everything (names)
2593 char **names;
2594{
2595 _ignore_completion_names (names, return_zero);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002596 return 0;
Jari Aaltobb706242000-03-17 21:46:59 +00002597}
2598
Jari Aalto31859422009-01-12 13:36:28 +00002599/* Replace a tilde-prefix in VAL with a `~', assuming the user typed it. VAL
2600 is an expanded filename. DIRECTORY_PART is the tilde-prefix portion
2601 of the un-tilde-expanded version of VAL (what the user typed). */
2602static char *
2603restore_tilde (val, directory_part)
2604 char *val, *directory_part;
2605{
2606 int l, vl, dl2, xl;
2607 char *dh2, *expdir, *ret;
2608
2609 vl = strlen (val);
2610
2611 /* We need to duplicate the expansions readline performs on the directory
2612 portion before passing it to our completion function. */
2613 dh2 = directory_part ? bash_dequote_filename (directory_part, 0) : 0;
2614 bash_directory_expansion (&dh2);
2615 dl2 = strlen (dh2);
2616
2617 expdir = bash_tilde_expand (directory_part, 0);
2618 xl = strlen (expdir);
2619 free (expdir);
2620
2621 /*
2622 dh2 = unexpanded but dequoted tilde-prefix
2623 dl2 = length of tilde-prefix
2624 expdir = tilde-expanded tilde-prefix
2625 xl = length of expanded tilde-prefix
2626 l = length of remainder after tilde-prefix
2627 */
2628 l = (vl - xl) + 1;
2629
2630 ret = (char *)xmalloc (dl2 + 2 + l);
2631 strcpy (ret, dh2);
2632 strcpy (ret + dl2, val + xl);
2633
2634 free (dh2);
2635 return (ret);
2636}
2637
Jari Aaltoeb873672004-11-09 21:37:25 +00002638/* Simulate the expansions that will be performed by
2639 rl_filename_completion_function. This must be called with the address of
2640 a pointer to malloc'd memory. */
Jari Aalto95732b42005-12-07 14:08:12 +00002641static void
Jari Aaltoeb873672004-11-09 21:37:25 +00002642bash_directory_expansion (dirname)
2643 char **dirname;
2644{
Jari Aalto06285672006-10-10 14:15:34 +00002645 char *d, *nd;
Jari Aaltoeb873672004-11-09 21:37:25 +00002646
2647 d = savestring (*dirname);
2648
2649 if (rl_directory_rewrite_hook)
2650 (*rl_directory_rewrite_hook) (&d);
2651
2652 if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&d))
2653 {
2654 free (*dirname);
2655 *dirname = d;
2656 }
Jari Aalto06285672006-10-10 14:15:34 +00002657 else if (rl_completion_found_quote)
2658 {
2659 nd = bash_dequote_filename (d, rl_completion_quote_character);
2660 free (*dirname);
2661 free (d);
2662 *dirname = nd;
2663 }
Jari Aaltoeb873672004-11-09 21:37:25 +00002664}
Jari Aalto31859422009-01-12 13:36:28 +00002665
Chet Ramey00018032011-11-21 20:51:19 -05002666/* If necessary, rewrite directory entry */
2667static char *
2668bash_filename_rewrite_hook (fname, fnlen)
2669 char *fname;
2670 int fnlen;
2671{
2672 char *conv;
2673
2674 conv = fnx_fromfs (fname, fnlen);
2675 if (conv != fname)
2676 conv = savestring (conv);
2677 return conv;
2678}
2679
Jari Aalto726f6381996-08-26 18:22:31 +00002680/* Handle symbolic link references and other directory name
2681 expansions while hacking completion. */
2682static int
2683bash_directory_completion_hook (dirname)
2684 char **dirname;
2685{
Jari Aaltob72432f1999-02-19 17:11:39 +00002686 char *local_dirname, *new_dirname, *t;
Jari Aaltobb706242000-03-17 21:46:59 +00002687 int return_value, should_expand_dirname;
Jari Aalto726f6381996-08-26 18:22:31 +00002688 WORD_LIST *wl;
Jari Aaltob80f6442004-07-27 13:29:18 +00002689 struct stat sb;
Jari Aalto726f6381996-08-26 18:22:31 +00002690
Jari Aaltobb706242000-03-17 21:46:59 +00002691 return_value = should_expand_dirname = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00002692 local_dirname = *dirname;
Jari Aaltobb706242000-03-17 21:46:59 +00002693
Chet Ramey00018032011-11-21 20:51:19 -05002694 if (mbschr (local_dirname, '$'))
Jari Aaltobb706242000-03-17 21:46:59 +00002695 should_expand_dirname = 1;
2696 else
Jari Aalto726f6381996-08-26 18:22:31 +00002697 {
Chet Ramey00018032011-11-21 20:51:19 -05002698 t = mbschr (local_dirname, '`');
Jari Aaltobb706242000-03-17 21:46:59 +00002699 if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0)
2700 should_expand_dirname = 1;
2701 }
Jari Aaltobb706242000-03-17 21:46:59 +00002702
Jari Aaltob80f6442004-07-27 13:29:18 +00002703#if defined (HAVE_LSTAT)
2704 if (should_expand_dirname && lstat (local_dirname, &sb) == 0)
2705#else
2706 if (should_expand_dirname && stat (local_dirname, &sb) == 0)
2707#endif
2708 should_expand_dirname = 0;
2709
Jari Aaltobb706242000-03-17 21:46:59 +00002710 if (should_expand_dirname)
2711 {
2712 new_dirname = savestring (local_dirname);
Jari Aaltof1be6662008-11-18 13:15:12 +00002713 wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB); /* does the right thing */
Jari Aalto726f6381996-08-26 18:22:31 +00002714 if (wl)
2715 {
2716 *dirname = string_list (wl);
2717 /* Tell the completer to replace the directory name only if we
2718 actually expanded something. */
2719 return_value = STREQ (local_dirname, *dirname) == 0;
2720 free (local_dirname);
Jari Aaltob72432f1999-02-19 17:11:39 +00002721 free (new_dirname);
Jari Aalto726f6381996-08-26 18:22:31 +00002722 dispose_words (wl);
2723 local_dirname = *dirname;
2724 }
2725 else
2726 {
Jari Aaltob72432f1999-02-19 17:11:39 +00002727 free (new_dirname);
Jari Aalto726f6381996-08-26 18:22:31 +00002728 free (local_dirname);
Jari Aaltof73dda02001-11-13 17:56:06 +00002729 *dirname = (char *)xmalloc (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002730 **dirname = '\0';
Jari Aalto726f6381996-08-26 18:22:31 +00002731 return 1;
2732 }
2733 }
Jari Aalto06285672006-10-10 14:15:34 +00002734 else
2735 {
2736 /* Dequote the filename even if we don't expand it. */
2737 new_dirname = bash_dequote_filename (local_dirname, rl_completion_quote_character);
2738 free (local_dirname);
2739 local_dirname = *dirname = new_dirname;
2740 }
Jari Aalto726f6381996-08-26 18:22:31 +00002741
Jari Aalto31859422009-01-12 13:36:28 +00002742 if (no_symbolic_links == 0 && (local_dirname[0] != '.' || local_dirname[1]))
Jari Aalto726f6381996-08-26 18:22:31 +00002743 {
2744 char *temp1, *temp2;
2745 int len1, len2;
2746
2747 t = get_working_directory ("symlink-hook");
2748 temp1 = make_absolute (local_dirname, t);
2749 free (t);
Jari Aalto28ef6c32001-04-06 19:14:31 +00002750 temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
Jari Aalto31859422009-01-12 13:36:28 +00002751
2752 /* Try spelling correction if initial canonicalization fails. */
2753 if (temp2 == 0 && dircomplete_spelling)
2754 {
2755 temp2 = dirspell (temp1);
2756 if (temp2)
2757 {
2758 free (temp1);
2759 temp1 = temp2;
2760 temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
2761 return_value = temp2 != 0;
2762 }
2763 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002764 /* If we can't canonicalize, bail. */
2765 if (temp2 == 0)
2766 {
2767 free (temp1);
2768 return 1;
2769 }
Jari Aalto726f6381996-08-26 18:22:31 +00002770 len1 = strlen (temp1);
2771 if (temp1[len1 - 1] == '/')
Jari Aalto28ef6c32001-04-06 19:14:31 +00002772 {
Jari Aalto726f6381996-08-26 18:22:31 +00002773 len2 = strlen (temp2);
Jari Aalto95732b42005-12-07 14:08:12 +00002774 if (len2 > 2) /* don't append `/' to `/' or `//' */
2775 {
2776 temp2 = (char *)xrealloc (temp2, len2 + 2);
2777 temp2[len2] = '/';
2778 temp2[len2 + 1] = '\0';
2779 }
Jari Aalto28ef6c32001-04-06 19:14:31 +00002780 }
Jari Aalto726f6381996-08-26 18:22:31 +00002781 free (local_dirname);
2782 *dirname = temp2;
2783 free (temp1);
2784 }
2785 return (return_value);
2786}
2787
Jari Aalto726f6381996-08-26 18:22:31 +00002788static char **history_completion_array = (char **)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002789static int harry_size;
2790static int harry_len;
Jari Aalto726f6381996-08-26 18:22:31 +00002791
2792static void
2793build_history_completion_array ()
2794{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002795 register int i, j;
2796 HIST_ENTRY **hlist;
2797 char **tokens;
Jari Aalto726f6381996-08-26 18:22:31 +00002798
2799 /* First, clear out the current dynamic history completion list. */
2800 if (harry_size)
2801 {
Jari Aalto7117c2d2002-07-17 14:10:11 +00002802 strvec_dispose (history_completion_array);
Jari Aalto726f6381996-08-26 18:22:31 +00002803 history_completion_array = (char **)NULL;
2804 harry_size = 0;
2805 harry_len = 0;
2806 }
2807
2808 /* Next, grovel each line of history, making each shell-sized token
2809 a separate entry in the history_completion_array. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002810 hlist = history_list ();
Jari Aalto726f6381996-08-26 18:22:31 +00002811
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002812 if (hlist)
2813 {
2814 for (i = 0; hlist[i]; i++)
Chet Ramey00018032011-11-21 20:51:19 -05002815 ;
2816 for ( --i; i >= 0; i--)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002817 {
2818 /* Separate each token, and place into an array. */
2819 tokens = history_tokenize (hlist[i]->line);
Jari Aalto726f6381996-08-26 18:22:31 +00002820
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002821 for (j = 0; tokens && tokens[j]; j++)
2822 {
2823 if (harry_len + 2 > harry_size)
Jari Aalto7117c2d2002-07-17 14:10:11 +00002824 history_completion_array = strvec_resize (history_completion_array, harry_size += 10);
Jari Aalto726f6381996-08-26 18:22:31 +00002825
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002826 history_completion_array[harry_len++] = tokens[j];
2827 history_completion_array[harry_len] = (char *)NULL;
2828 }
2829 free (tokens);
2830 }
Jari Aalto726f6381996-08-26 18:22:31 +00002831
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002832 /* Sort the complete list of tokens. */
Chet Ramey00018032011-11-21 20:51:19 -05002833 if (dabbrev_expand_active == 0)
2834 qsort (history_completion_array, harry_len, sizeof (char *), (QSFUNC *)strvec_strcmp);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002835 }
Jari Aalto726f6381996-08-26 18:22:31 +00002836}
2837
2838static char *
2839history_completion_generator (hint_text, state)
Jari Aaltof73dda02001-11-13 17:56:06 +00002840 const char *hint_text;
Jari Aalto726f6381996-08-26 18:22:31 +00002841 int state;
2842{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002843 static int local_index, len;
Jari Aaltof73dda02001-11-13 17:56:06 +00002844 static const char *text;
Jari Aalto726f6381996-08-26 18:22:31 +00002845
2846 /* If this is the first call to the generator, then initialize the
2847 list of strings to complete over. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00002848 if (state == 0)
Jari Aalto726f6381996-08-26 18:22:31 +00002849 {
Chet Ramey00018032011-11-21 20:51:19 -05002850 if (dabbrev_expand_active) /* This is kind of messy */
2851 rl_completion_suppress_append = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00002852 local_index = 0;
2853 build_history_completion_array ();
2854 text = hint_text;
2855 len = strlen (text);
2856 }
2857
2858 while (history_completion_array && history_completion_array[local_index])
2859 {
2860 if (strncmp (text, history_completion_array[local_index++], len) == 0)
2861 return (savestring (history_completion_array[local_index - 1]));
2862 }
2863 return ((char *)NULL);
2864}
2865
Jari Aalto28ef6c32001-04-06 19:14:31 +00002866static int
Jari Aalto726f6381996-08-26 18:22:31 +00002867dynamic_complete_history (count, key)
2868 int count, key;
2869{
Jari Aaltof73dda02001-11-13 17:56:06 +00002870 int r;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002871 rl_compentry_func_t *orig_func;
2872 rl_completion_func_t *orig_attempt_func;
Jari Aalto726f6381996-08-26 18:22:31 +00002873
2874 orig_func = rl_completion_entry_function;
2875 orig_attempt_func = rl_attempted_completion_function;
Jari Aalto31859422009-01-12 13:36:28 +00002876
Jari Aalto28ef6c32001-04-06 19:14:31 +00002877 rl_completion_entry_function = history_completion_generator;
2878 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00002879
Jari Aalto7117c2d2002-07-17 14:10:11 +00002880 /* XXX - use rl_completion_mode here? */
Jari Aalto28ef6c32001-04-06 19:14:31 +00002881 if (rl_last_func == dynamic_complete_history)
Jari Aaltof73dda02001-11-13 17:56:06 +00002882 r = rl_complete_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002883 else
Jari Aaltof73dda02001-11-13 17:56:06 +00002884 r = rl_complete_internal (TAB);
Jari Aalto726f6381996-08-26 18:22:31 +00002885
2886 rl_completion_entry_function = orig_func;
2887 rl_attempted_completion_function = orig_attempt_func;
Jari Aaltof73dda02001-11-13 17:56:06 +00002888 return r;
Jari Aalto726f6381996-08-26 18:22:31 +00002889}
2890
Jari Aalto31859422009-01-12 13:36:28 +00002891static int
2892bash_dabbrev_expand (count, key)
2893 int count, key;
2894{
Chet Ramey00018032011-11-21 20:51:19 -05002895 int r, orig_suppress, orig_sort;
Jari Aalto31859422009-01-12 13:36:28 +00002896 rl_compentry_func_t *orig_func;
2897 rl_completion_func_t *orig_attempt_func;
2898
2899 orig_func = rl_menu_completion_entry_function;
2900 orig_attempt_func = rl_attempted_completion_function;
Chet Ramey00018032011-11-21 20:51:19 -05002901 orig_suppress = rl_completion_suppress_append;
2902 orig_sort = rl_sort_completion_matches;
Jari Aalto31859422009-01-12 13:36:28 +00002903
2904 rl_menu_completion_entry_function = history_completion_generator;
2905 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
2906 rl_filename_completion_desired = 0;
Chet Ramey00018032011-11-21 20:51:19 -05002907 rl_completion_suppress_append = 1;
2908 rl_sort_completion_matches = 0;
Jari Aalto31859422009-01-12 13:36:28 +00002909
2910 /* XXX - use rl_completion_mode here? */
Chet Ramey00018032011-11-21 20:51:19 -05002911 dabbrev_expand_active = 1;
Jari Aalto31859422009-01-12 13:36:28 +00002912 if (rl_last_func == bash_dabbrev_expand)
2913 rl_last_func = rl_menu_complete;
2914 r = rl_menu_complete (count, key);
Chet Ramey00018032011-11-21 20:51:19 -05002915 dabbrev_expand_active = 0;
Jari Aalto31859422009-01-12 13:36:28 +00002916
2917 rl_last_func = bash_dabbrev_expand;
2918 rl_menu_completion_entry_function = orig_func;
2919 rl_attempted_completion_function = orig_attempt_func;
Chet Ramey00018032011-11-21 20:51:19 -05002920 rl_completion_suppress_append = orig_suppress;
2921 rl_sort_completion_matches = orig_sort;
Jari Aalto31859422009-01-12 13:36:28 +00002922
2923 return r;
2924}
2925
Jari Aalto726f6381996-08-26 18:22:31 +00002926#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
Jari Aalto28ef6c32001-04-06 19:14:31 +00002927static int
Jari Aalto726f6381996-08-26 18:22:31 +00002928bash_complete_username (ignore, ignore2)
2929 int ignore, ignore2;
2930{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002931 return bash_complete_username_internal (rl_completion_mode (bash_complete_username));
Jari Aalto726f6381996-08-26 18:22:31 +00002932}
2933
Jari Aalto28ef6c32001-04-06 19:14:31 +00002934static int
Jari Aalto726f6381996-08-26 18:22:31 +00002935bash_possible_username_completions (ignore, ignore2)
2936 int ignore, ignore2;
2937{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002938 return bash_complete_username_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002939}
2940
Jari Aalto28ef6c32001-04-06 19:14:31 +00002941static int
Jari Aalto726f6381996-08-26 18:22:31 +00002942bash_complete_username_internal (what_to_do)
2943 int what_to_do;
2944{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002945 return bash_specific_completion (what_to_do, rl_username_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00002946}
2947
Jari Aalto28ef6c32001-04-06 19:14:31 +00002948static int
Jari Aalto726f6381996-08-26 18:22:31 +00002949bash_complete_filename (ignore, ignore2)
2950 int ignore, ignore2;
2951{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002952 return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename));
Jari Aalto726f6381996-08-26 18:22:31 +00002953}
2954
Jari Aalto28ef6c32001-04-06 19:14:31 +00002955static int
Jari Aalto726f6381996-08-26 18:22:31 +00002956bash_possible_filename_completions (ignore, ignore2)
2957 int ignore, ignore2;
2958{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002959 return bash_complete_filename_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00002960}
2961
Jari Aalto28ef6c32001-04-06 19:14:31 +00002962static int
Jari Aalto726f6381996-08-26 18:22:31 +00002963bash_complete_filename_internal (what_to_do)
2964 int what_to_do;
2965{
Jari Aalto28ef6c32001-04-06 19:14:31 +00002966 rl_compentry_func_t *orig_func;
2967 rl_completion_func_t *orig_attempt_func;
2968 rl_icppfunc_t *orig_dir_func;
Jari Aaltob80f6442004-07-27 13:29:18 +00002969 /*const*/ char *orig_rl_completer_word_break_characters;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002970 int r;
Jari Aalto726f6381996-08-26 18:22:31 +00002971
2972 orig_func = rl_completion_entry_function;
2973 orig_attempt_func = rl_attempted_completion_function;
2974 orig_dir_func = rl_directory_completion_hook;
2975 orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002976 rl_completion_entry_function = rl_filename_completion_function;
2977 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
2978 rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00002979 rl_completer_word_break_characters = " \t\n\"\'";
2980
Jari Aalto28ef6c32001-04-06 19:14:31 +00002981 r = rl_complete_internal (what_to_do);
Jari Aalto726f6381996-08-26 18:22:31 +00002982
2983 rl_completion_entry_function = orig_func;
2984 rl_attempted_completion_function = orig_attempt_func;
2985 rl_directory_completion_hook = orig_dir_func;
2986 rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
Jari Aalto28ef6c32001-04-06 19:14:31 +00002987
2988 return r;
Jari Aalto726f6381996-08-26 18:22:31 +00002989}
2990
Jari Aalto28ef6c32001-04-06 19:14:31 +00002991static int
Jari Aalto726f6381996-08-26 18:22:31 +00002992bash_complete_hostname (ignore, ignore2)
2993 int ignore, ignore2;
2994{
Jari Aalto7117c2d2002-07-17 14:10:11 +00002995 return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname));
Jari Aalto726f6381996-08-26 18:22:31 +00002996}
2997
Jari Aalto28ef6c32001-04-06 19:14:31 +00002998static int
Jari Aalto726f6381996-08-26 18:22:31 +00002999bash_possible_hostname_completions (ignore, ignore2)
3000 int ignore, ignore2;
3001{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003002 return bash_complete_hostname_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00003003}
3004
Jari Aalto28ef6c32001-04-06 19:14:31 +00003005static int
Jari Aalto726f6381996-08-26 18:22:31 +00003006bash_complete_variable (ignore, ignore2)
3007 int ignore, ignore2;
3008{
Jari Aalto7117c2d2002-07-17 14:10:11 +00003009 return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable));
Jari Aalto726f6381996-08-26 18:22:31 +00003010}
3011
Jari Aalto28ef6c32001-04-06 19:14:31 +00003012static int
Jari Aalto726f6381996-08-26 18:22:31 +00003013bash_possible_variable_completions (ignore, ignore2)
3014 int ignore, ignore2;
3015{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003016 return bash_complete_variable_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00003017}
3018
Jari Aalto28ef6c32001-04-06 19:14:31 +00003019static int
Jari Aalto726f6381996-08-26 18:22:31 +00003020bash_complete_command (ignore, ignore2)
3021 int ignore, ignore2;
3022{
Jari Aalto7117c2d2002-07-17 14:10:11 +00003023 return bash_complete_command_internal (rl_completion_mode (bash_complete_command));
Jari Aalto726f6381996-08-26 18:22:31 +00003024}
3025
Jari Aalto28ef6c32001-04-06 19:14:31 +00003026static int
Jari Aalto726f6381996-08-26 18:22:31 +00003027bash_possible_command_completions (ignore, ignore2)
3028 int ignore, ignore2;
3029{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003030 return bash_complete_command_internal ('?');
Jari Aalto726f6381996-08-26 18:22:31 +00003031}
3032
Jari Aalto28ef6c32001-04-06 19:14:31 +00003033static int
Jari Aalto726f6381996-08-26 18:22:31 +00003034bash_complete_hostname_internal (what_to_do)
3035 int what_to_do;
3036{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003037 return bash_specific_completion (what_to_do, hostname_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00003038}
3039
Jari Aalto28ef6c32001-04-06 19:14:31 +00003040static int
Jari Aalto726f6381996-08-26 18:22:31 +00003041bash_complete_variable_internal (what_to_do)
3042 int what_to_do;
3043{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003044 return bash_specific_completion (what_to_do, variable_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00003045}
3046
Jari Aalto28ef6c32001-04-06 19:14:31 +00003047static int
Jari Aalto726f6381996-08-26 18:22:31 +00003048bash_complete_command_internal (what_to_do)
3049 int what_to_do;
3050{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003051 return bash_specific_completion (what_to_do, command_word_completion_function);
Jari Aalto726f6381996-08-26 18:22:31 +00003052}
3053
Jari Aalto7117c2d2002-07-17 14:10:11 +00003054static char *globtext;
3055static char *globorig;
3056
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003057static char *
3058glob_complete_word (text, state)
Jari Aalto28ef6c32001-04-06 19:14:31 +00003059 const char *text;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003060 int state;
3061{
3062 static char **matches = (char **)NULL;
3063 static int ind;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003064 int glen;
Jari Aaltoeb873672004-11-09 21:37:25 +00003065 char *ret, *ttext;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003066
3067 if (state == 0)
3068 {
Jari Aaltoe8ce7751997-09-22 20:22:27 +00003069 rl_filename_completion_desired = 1;
Jari Aalto7117c2d2002-07-17 14:10:11 +00003070 FREE (matches);
3071 if (globorig != globtext)
3072 FREE (globorig);
3073 FREE (globtext);
3074
Jari Aaltoeb873672004-11-09 21:37:25 +00003075 ttext = bash_tilde_expand (text, 0);
3076
Jari Aalto7117c2d2002-07-17 14:10:11 +00003077 if (rl_explicit_arg)
3078 {
Jari Aaltoeb873672004-11-09 21:37:25 +00003079 globorig = savestring (ttext);
3080 glen = strlen (ttext);
Jari Aalto7117c2d2002-07-17 14:10:11 +00003081 globtext = (char *)xmalloc (glen + 2);
Jari Aaltoeb873672004-11-09 21:37:25 +00003082 strcpy (globtext, ttext);
Jari Aalto7117c2d2002-07-17 14:10:11 +00003083 globtext[glen] = '*';
3084 globtext[glen+1] = '\0';
3085 }
3086 else
Jari Aaltoeb873672004-11-09 21:37:25 +00003087 globtext = globorig = savestring (ttext);
3088
3089 if (ttext != text)
3090 free (ttext);
Jari Aalto7117c2d2002-07-17 14:10:11 +00003091
3092 matches = shell_glob_filename (globtext);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003093 if (GLOB_FAILED (matches))
Jari Aalto28ef6c32001-04-06 19:14:31 +00003094 matches = (char **)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003095 ind = 0;
3096 }
3097
3098 ret = matches ? matches[ind] : (char *)NULL;
3099 ind++;
3100 return ret;
3101}
3102
Jari Aalto28ef6c32001-04-06 19:14:31 +00003103static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003104bash_glob_completion_internal (what_to_do)
3105 int what_to_do;
3106{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003107 return bash_specific_completion (what_to_do, glob_complete_word);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003108}
3109
Jari Aalto7117c2d2002-07-17 14:10:11 +00003110/* A special quoting function so we don't end up quoting globbing characters
3111 in the word if there are no matches or multiple matches. */
3112static char *
3113bash_glob_quote_filename (s, rtype, qcp)
3114 char *s;
3115 int rtype;
3116 char *qcp;
3117{
3118 if (globorig && qcp && *qcp == '\0' && STREQ (s, globorig))
3119 return (savestring (s));
3120 else
3121 return (bash_quote_filename (s, rtype, qcp));
3122}
3123
3124static int
3125bash_glob_complete_word (count, key)
3126 int count, key;
3127{
3128 int r;
3129 rl_quote_func_t *orig_quoting_function;
3130
Jari Aaltob80f6442004-07-27 13:29:18 +00003131 if (rl_editing_mode == EMACS_EDITING_MODE)
3132 rl_explicit_arg = 1; /* force `*' append */
Jari Aalto7117c2d2002-07-17 14:10:11 +00003133 orig_quoting_function = rl_filename_quoting_function;
3134 rl_filename_quoting_function = bash_glob_quote_filename;
3135
3136 r = bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word));
3137
3138 rl_filename_quoting_function = orig_quoting_function;
3139 return r;
3140}
3141
Jari Aalto28ef6c32001-04-06 19:14:31 +00003142static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003143bash_glob_expand_word (count, key)
3144 int count, key;
3145{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003146 return bash_glob_completion_internal ('*');
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003147}
3148
Jari Aalto28ef6c32001-04-06 19:14:31 +00003149static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003150bash_glob_list_expansions (count, key)
3151 int count, key;
3152{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003153 return bash_glob_completion_internal ('?');
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003154}
3155
Jari Aalto28ef6c32001-04-06 19:14:31 +00003156static int
Jari Aalto726f6381996-08-26 18:22:31 +00003157bash_specific_completion (what_to_do, generator)
3158 int what_to_do;
Jari Aalto28ef6c32001-04-06 19:14:31 +00003159 rl_compentry_func_t *generator;
Jari Aalto726f6381996-08-26 18:22:31 +00003160{
Jari Aalto28ef6c32001-04-06 19:14:31 +00003161 rl_compentry_func_t *orig_func;
3162 rl_completion_func_t *orig_attempt_func;
3163 int r;
Jari Aalto726f6381996-08-26 18:22:31 +00003164
3165 orig_func = rl_completion_entry_function;
3166 orig_attempt_func = rl_attempted_completion_function;
3167 rl_completion_entry_function = generator;
Jari Aalto28ef6c32001-04-06 19:14:31 +00003168 rl_attempted_completion_function = NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00003169
Jari Aalto28ef6c32001-04-06 19:14:31 +00003170 r = rl_complete_internal (what_to_do);
Jari Aalto726f6381996-08-26 18:22:31 +00003171
3172 rl_completion_entry_function = orig_func;
3173 rl_attempted_completion_function = orig_attempt_func;
Jari Aalto28ef6c32001-04-06 19:14:31 +00003174
3175 return r;
Jari Aalto726f6381996-08-26 18:22:31 +00003176}
3177
3178#endif /* SPECIFIC_COMPLETION_FUNCTIONS */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003179
Jari Aaltob80f6442004-07-27 13:29:18 +00003180#if defined (VI_MODE)
3181/* Completion, from vi mode's point of view. This is a modified version of
3182 rl_vi_complete which uses the bash globbing code to implement what POSIX
3183 specifies, which is to append a `*' and attempt filename generation (which
3184 has the side effect of expanding any globbing characters in the word). */
3185static int
3186bash_vi_complete (count, key)
3187 int count, key;
3188{
3189#if defined (SPECIFIC_COMPLETION_FUNCTIONS)
3190 int p, r;
3191 char *t;
3192
3193 if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
3194 {
3195 if (!whitespace (rl_line_buffer[rl_point + 1]))
3196 rl_vi_end_word (1, 'E');
3197 rl_point++;
3198 }
3199
3200 /* Find boundaries of current word, according to vi definition of a
3201 `bigword'. */
3202 t = 0;
3203 if (rl_point > 0)
3204 {
3205 p = rl_point;
3206 rl_vi_bWord (1, 'B');
3207 r = rl_point;
3208 rl_point = p;
3209 p = r;
3210
3211 t = substring (rl_line_buffer, p, rl_point);
3212 }
3213
3214 if (t && glob_pattern_p (t) == 0)
3215 rl_explicit_arg = 1; /* XXX - force glob_complete_word to append `*' */
3216 FREE (t);
3217
3218 if (key == '*') /* Expansion and replacement. */
3219 r = bash_glob_expand_word (count, key);
3220 else if (key == '=') /* List possible completions. */
3221 r = bash_glob_list_expansions (count, key);
3222 else if (key == '\\') /* Standard completion */
3223 r = bash_glob_complete_word (count, key);
3224 else
3225 r = rl_complete (0, key);
3226
3227 if (key == '*' || key == '\\')
3228 rl_vi_start_inserting (key, 1, 1);
3229
3230 return (r);
3231#else
3232 return rl_vi_complete (count, key);
3233#endif /* !SPECIFIC_COMPLETION_FUNCTIONS */
3234}
3235#endif /* VI_MODE */
3236
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003237/* Filename quoting for completion. */
Jari Aaltobb706242000-03-17 21:46:59 +00003238/* A function to strip unquoted quote characters (single quotes, double
3239 quotes, and backslashes). It allows single quotes to appear
3240 within double quotes, and vice versa. It should be smarter. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003241static char *
3242bash_dequote_filename (text, quote_char)
3243 char *text;
Jari Aalto28ef6c32001-04-06 19:14:31 +00003244 int quote_char;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003245{
3246 char *ret, *p, *r;
3247 int l, quoted;
3248
3249 l = strlen (text);
Jari Aaltof73dda02001-11-13 17:56:06 +00003250 ret = (char *)xmalloc (l + 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003251 for (quoted = quote_char, p = text, r = ret; p && *p; p++)
3252 {
Jari Aalto31859422009-01-12 13:36:28 +00003253 /* Allow backslash-escaped characters to pass through unscathed. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003254 if (*p == '\\')
3255 {
Jari Aalto31859422009-01-12 13:36:28 +00003256 /* Backslashes are preserved within single quotes. */
3257 if (quoted == '\'')
3258 *r++ = *p;
3259 /* Backslashes are preserved within double quotes unless the
3260 character is one that is defined to be escaped */
3261 else if (quoted == '"' && ((sh_syntaxtab[p[1]] & CBSDQUOTE) == 0))
3262 *r++ = *p;
3263
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003264 *r++ = *++p;
3265 if (*p == '\0')
Chet Ramey00018032011-11-21 20:51:19 -05003266 return ret; /* XXX - was break; */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003267 continue;
3268 }
3269 /* Close quote. */
3270 if (quoted && *p == quoted)
Jari Aalto28ef6c32001-04-06 19:14:31 +00003271 {
3272 quoted = 0;
3273 continue;
3274 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003275 /* Open quote. */
3276 if (quoted == 0 && (*p == '\'' || *p == '"'))
Jari Aalto28ef6c32001-04-06 19:14:31 +00003277 {
3278 quoted = *p;
3279 continue;
3280 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003281 *r++ = *p;
3282 }
3283 *r = '\0';
3284 return ret;
3285}
3286
Jari Aaltod166f041997-06-05 14:59:13 +00003287/* Quote characters that the readline completion code would treat as
3288 word break characters with backslashes. Pass backslash-quoted
3289 characters through without examination. */
3290static char *
3291quote_word_break_chars (text)
3292 char *text;
3293{
3294 char *ret, *r, *s;
3295 int l;
3296
3297 l = strlen (text);
Jari Aaltof73dda02001-11-13 17:56:06 +00003298 ret = (char *)xmalloc ((2 * l) + 1);
Jari Aaltod166f041997-06-05 14:59:13 +00003299 for (s = text, r = ret; *s; s++)
3300 {
3301 /* Pass backslash-quoted characters through, including the backslash. */
3302 if (*s == '\\')
3303 {
3304 *r++ = '\\';
3305 *r++ = *++s;
3306 if (*s == '\0')
3307 break;
3308 continue;
3309 }
3310 /* OK, we have an unquoted character. Check its presence in
3311 rl_completer_word_break_characters. */
Chet Ramey00018032011-11-21 20:51:19 -05003312 if (mbschr (rl_completer_word_break_characters, *s))
Jari Aalto28ef6c32001-04-06 19:14:31 +00003313 *r++ = '\\';
Jari Aalto31859422009-01-12 13:36:28 +00003314 /* XXX -- check for standalone tildes here and backslash-quote them */
3315 if (s == text && *s == '~' && file_exists (text))
3316 *r++ = '\\';
Jari Aaltod166f041997-06-05 14:59:13 +00003317 *r++ = *s;
3318 }
3319 *r = '\0';
3320 return ret;
3321}
3322
3323/* Quote a filename using double quotes, single quotes, or backslashes
3324 depending on the value of completion_quoting_style. If we're
3325 completing using backslashes, we need to quote some additional
3326 characters (those that readline treats as word breaks), so we call
Jari Aalto7117c2d2002-07-17 14:10:11 +00003327 quote_word_break_chars on the result. This returns newly-allocated
3328 memory. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003329static char *
3330bash_quote_filename (s, rtype, qcp)
3331 char *s;
3332 int rtype;
3333 char *qcp;
3334{
3335 char *rtext, *mtext, *ret;
3336 int rlen, cs;
3337
3338 rtext = (char *)NULL;
3339
3340 /* If RTYPE == MULT_MATCH, it means that there is
3341 more than one match. In this case, we do not add
3342 the closing quote or attempt to perform tilde
3343 expansion. If RTYPE == SINGLE_MATCH, we try
3344 to perform tilde expansion, because single and double
3345 quotes inhibit tilde expansion by the shell. */
3346
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003347 cs = completion_quoting_style;
3348 /* Might need to modify the default completion style based on *qcp,
Jari Aaltobb706242000-03-17 21:46:59 +00003349 since it's set to any user-provided opening quote. We also change
3350 to single-quoting if there is no user-provided opening quote and
3351 the word being completed contains newlines, since those are not
3352 quoted correctly using backslashes (a backslash-newline pair is
3353 special to the shell parser). */
Chet Ramey00018032011-11-21 20:51:19 -05003354 if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && mbschr (s, '\n'))
Jari Aaltobb706242000-03-17 21:46:59 +00003355 cs = COMPLETE_SQUOTE;
3356 else if (*qcp == '"')
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003357 cs = COMPLETE_DQUOTE;
3358 else if (*qcp == '\'')
3359 cs = COMPLETE_SQUOTE;
3360#if defined (BANG_HISTORY)
3361 else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE &&
Chet Ramey00018032011-11-21 20:51:19 -05003362 history_expansion_inhibited == 0 && mbschr (s, '!'))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003363 cs = COMPLETE_BSQUOTE;
Jari Aaltod166f041997-06-05 14:59:13 +00003364
3365 if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE &&
Chet Ramey00018032011-11-21 20:51:19 -05003366 history_expansion_inhibited == 0 && mbschr (s, '!'))
Jari Aaltod166f041997-06-05 14:59:13 +00003367 {
3368 cs = COMPLETE_BSQUOTE;
3369 *qcp = '\0';
3370 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003371#endif
3372
Jari Aalto95732b42005-12-07 14:08:12 +00003373 /* Don't tilde-expand backslash-quoted filenames, since only single and
3374 double quotes inhibit tilde expansion. */
3375 mtext = s;
3376 if (mtext[0] == '~' && rtype == SINGLE_MATCH && cs != COMPLETE_BSQUOTE)
3377 mtext = bash_tilde_expand (s, 0);
3378
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003379 switch (cs)
3380 {
3381 case COMPLETE_DQUOTE:
Jari Aalto28ef6c32001-04-06 19:14:31 +00003382 rtext = sh_double_quote (mtext);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003383 break;
3384 case COMPLETE_SQUOTE:
Jari Aalto28ef6c32001-04-06 19:14:31 +00003385 rtext = sh_single_quote (mtext);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003386 break;
3387 case COMPLETE_BSQUOTE:
Jari Aalto28ef6c32001-04-06 19:14:31 +00003388 rtext = sh_backslash_quote (mtext);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003389 break;
3390 }
3391
3392 if (mtext != s)
3393 free (mtext);
3394
Jari Aaltod166f041997-06-05 14:59:13 +00003395 /* We may need to quote additional characters: those that readline treats
3396 as word breaks that are not quoted by backslash_quote. */
3397 if (rtext && cs == COMPLETE_BSQUOTE)
3398 {
3399 mtext = quote_word_break_chars (rtext);
3400 free (rtext);
3401 rtext = mtext;
3402 }
3403
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003404 /* Leave the opening quote intact. The readline completion code takes
3405 care of avoiding doubled opening quotes. */
3406 rlen = strlen (rtext);
Jari Aaltof73dda02001-11-13 17:56:06 +00003407 ret = (char *)xmalloc (rlen + 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003408 strcpy (ret, rtext);
3409
3410 /* If there are multiple matches, cut off the closing quote. */
3411 if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE)
3412 ret[rlen - 1] = '\0';
3413 free (rtext);
3414 return ret;
3415}
3416
Jari Aaltobb706242000-03-17 21:46:59 +00003417/* Support for binding readline key sequences to Unix commands. */
3418static Keymap cmd_xmap;
3419
3420static int
Chet Ramey00018032011-11-21 20:51:19 -05003421putx(c)
3422 int c;
3423{
3424 putc (c, rl_outstream);
3425}
3426
3427static int
Jari Aaltobb706242000-03-17 21:46:59 +00003428bash_execute_unix_command (count, key)
3429 int count; /* ignored */
3430 int key;
3431{
3432 Keymap ckmap; /* current keymap */
3433 Keymap xkmap; /* unix command executing keymap */
Chet Ramey00018032011-11-21 20:51:19 -05003434 register int i, r;
Jari Aalto31859422009-01-12 13:36:28 +00003435 intmax_t mi;
Jari Aaltob80f6442004-07-27 13:29:18 +00003436 sh_parser_state_t ps;
Chet Ramey00018032011-11-21 20:51:19 -05003437 char *cmd, *value, *l, *l1, *ce;
Jari Aalto31859422009-01-12 13:36:28 +00003438 SHELL_VAR *v;
3439 char ibuf[INT_STRLEN_BOUND(int) + 1];
Jari Aaltobb706242000-03-17 21:46:59 +00003440
3441 /* First, we need to find the right command to execute. This is tricky,
3442 because we might have already indirected into another keymap. */
3443 ckmap = rl_get_keymap ();
3444 if (ckmap != rl_executing_keymap)
3445 {
3446 /* bogus. we have to search. only handle one level of indirection. */
3447 for (i = 0; i < KEYMAP_SIZE; i++)
3448 {
3449 if (ckmap[i].type == ISKMAP && (Keymap)ckmap[i].function == rl_executing_keymap)
3450 break;
3451 }
3452 if (i < KEYMAP_SIZE)
Jari Aalto28ef6c32001-04-06 19:14:31 +00003453 xkmap = (Keymap)cmd_xmap[i].function;
Jari Aaltobb706242000-03-17 21:46:59 +00003454 else
3455 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00003456 rl_crlf ();
Jari Aaltob80f6442004-07-27 13:29:18 +00003457 internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
Jari Aalto28ef6c32001-04-06 19:14:31 +00003458 rl_forced_update_display ();
3459 return 1;
Jari Aaltobb706242000-03-17 21:46:59 +00003460 }
3461 }
3462 else
3463 xkmap = cmd_xmap;
3464
3465 cmd = (char *)xkmap[key].function;
3466
3467 if (cmd == 0)
3468 {
Jari Aalto28ef6c32001-04-06 19:14:31 +00003469 rl_ding ();
Jari Aaltobb706242000-03-17 21:46:59 +00003470 return 1;
3471 }
3472
Chet Ramey00018032011-11-21 20:51:19 -05003473 ce = rl_get_termcap ("ce");
3474 if (ce) /* clear current line */
3475 {
3476 fprintf (rl_outstream, "\r");
3477 tputs (ce, 1, putx);
3478 fflush (rl_outstream);
3479 }
3480 else
3481 rl_crlf (); /* move to a new line */
Jari Aaltobb706242000-03-17 21:46:59 +00003482
Jari Aalto31859422009-01-12 13:36:28 +00003483 v = bind_variable ("READLINE_LINE", rl_line_buffer, 0);
3484 if (v)
3485 VSETATTR (v, att_exported);
Chet Ramey00018032011-11-21 20:51:19 -05003486 l = v ? value_cell (v) : 0;
Jari Aalto31859422009-01-12 13:36:28 +00003487 value = inttostr (rl_point, ibuf, sizeof (ibuf));
3488 v = bind_int_variable ("READLINE_POINT", value);
3489 if (v)
3490 VSETATTR (v, att_exported);
3491 array_needs_making = 1;
3492
Jari Aaltob80f6442004-07-27 13:29:18 +00003493 save_parser_state (&ps);
Chet Ramey00018032011-11-21 20:51:19 -05003494 r = parse_and_execute (cmd, "bash_execute_unix_command", SEVAL_NOHIST|SEVAL_NOFREE);
Jari Aaltob80f6442004-07-27 13:29:18 +00003495 restore_parser_state (&ps);
Jari Aaltobb706242000-03-17 21:46:59 +00003496
Jari Aalto31859422009-01-12 13:36:28 +00003497 v = find_variable ("READLINE_LINE");
Chet Ramey00018032011-11-21 20:51:19 -05003498 l1 = v ? value_cell (v) : 0;
3499 if (l1 != l)
Jari Aalto31859422009-01-12 13:36:28 +00003500 maybe_make_readline_line (value_cell (v));
3501 v = find_variable ("READLINE_POINT");
3502 if (v && legal_number (value_cell (v), &mi))
3503 {
3504 i = mi;
Chet Ramey89a92862011-11-21 20:49:12 -05003505 if (i != rl_point)
Jari Aalto31859422009-01-12 13:36:28 +00003506 {
3507 rl_point = i;
3508 if (rl_point > rl_end)
3509 rl_point = rl_end;
3510 else if (rl_point < 0)
3511 rl_point = 0;
3512 }
3513 }
3514
3515 unbind_variable ("READLINE_LINE");
3516 unbind_variable ("READLINE_POINT");
3517 array_needs_making = 1;
3518
Jari Aaltobb706242000-03-17 21:46:59 +00003519 /* and restore the readline buffer and display after command execution. */
3520 rl_forced_update_display ();
3521 return 0;
3522}
3523
3524static void
3525init_unix_command_map ()
3526{
3527 cmd_xmap = rl_make_bare_keymap ();
3528}
3529
3530static int
3531isolate_sequence (string, ind, need_dquote, startp)
3532 char *string;
3533 int ind, need_dquote, *startp;
3534{
3535 register int i;
3536 int c, passc, delim;
3537
3538 for (i = ind; string[i] && whitespace (string[i]); i++)
3539 ;
3540 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
3541 if (need_dquote && string[i] != '"')
3542 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003543 builtin_error (_("%s: first non-whitespace character is not `\"'"), string);
Jari Aaltobb706242000-03-17 21:46:59 +00003544 return -1;
3545 }
3546
3547 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
3548 string to bind the key sequence to. */
3549 delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0;
3550
3551 if (startp)
3552 *startp = delim ? ++i : i;
3553
3554 for (passc = 0; c = string[i]; i++)
3555 {
3556 if (passc)
3557 {
3558 passc = 0;
3559 continue;
3560 }
3561 if (c == '\\')
3562 {
3563 passc++;
3564 continue;
3565 }
3566 if (c == delim)
Jari Aalto28ef6c32001-04-06 19:14:31 +00003567 break;
Jari Aaltobb706242000-03-17 21:46:59 +00003568 }
3569
3570 if (delim && string[i] != delim)
3571 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003572 builtin_error (_("no closing `%c' in %s"), delim, string);
Jari Aaltobb706242000-03-17 21:46:59 +00003573 return -1;
3574 }
3575
3576 return i;
3577}
3578
3579int
3580bind_keyseq_to_unix_command (line)
3581 char *line;
3582{
3583 Keymap kmap;
3584 char *kseq, *value;
Jari Aaltof73dda02001-11-13 17:56:06 +00003585 int i, kstart;
Jari Aaltobb706242000-03-17 21:46:59 +00003586
3587 if (cmd_xmap == 0)
3588 init_unix_command_map ();
3589
3590 kmap = rl_get_keymap ();
3591
3592 /* We duplicate some of the work done by rl_parse_and_bind here, but
3593 this code only has to handle `"keyseq": ["]command["]' and can
3594 generate an error for anything else. */
3595 i = isolate_sequence (line, 0, 1, &kstart);
3596 if (i < 0)
3597 return -1;
3598
3599 /* Create the key sequence string to pass to rl_generic_bind */
3600 kseq = substring (line, kstart, i);
3601
3602 for ( ; line[i] && line[i] != ':'; i++)
3603 ;
3604 if (line[i] != ':')
3605 {
Jari Aaltob80f6442004-07-27 13:29:18 +00003606 builtin_error (_("%s: missing colon separator"), line);
Jari Aaltobb706242000-03-17 21:46:59 +00003607 return -1;
3608 }
3609
3610 i = isolate_sequence (line, i + 1, 0, &kstart);
3611 if (i < 0)
3612 return -1;
3613
3614 /* Create the value string containing the command to execute. */
3615 value = substring (line, kstart, i);
3616
3617 /* Save the command to execute and the key sequence in the CMD_XMAP */
3618 rl_generic_bind (ISMACR, kseq, value, cmd_xmap);
3619
3620 /* and bind the key sequence in the current keymap to a function that
3621 understands how to execute from CMD_XMAP */
Jari Aaltob80f6442004-07-27 13:29:18 +00003622 rl_bind_keyseq_in_map (kseq, bash_execute_unix_command, kmap);
Jari Aaltobb706242000-03-17 21:46:59 +00003623
3624 return 0;
3625}
3626
3627/* Used by the programmable completion code. Complete TEXT as a filename,
3628 but return only directories as matches. Dequotes the filename before
3629 attempting to find matches. */
3630char **
3631bash_directory_completion_matches (text)
Jari Aalto28ef6c32001-04-06 19:14:31 +00003632 const char *text;
Jari Aaltobb706242000-03-17 21:46:59 +00003633{
3634 char **m1;
3635 char *dfn;
3636 int qc;
3637
Jari Aaltob80f6442004-07-27 13:29:18 +00003638 qc = rl_dispatching ? rl_completion_quote_character : 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +00003639 dfn = bash_dequote_filename ((char *)text, qc);
3640 m1 = rl_completion_matches (dfn, rl_filename_completion_function);
Jari Aaltobb706242000-03-17 21:46:59 +00003641 free (dfn);
3642
3643 if (m1 == 0 || m1[0] == 0)
3644 return m1;
3645 /* We don't bother recomputing the lcd of the matches, because it will just
3646 get thrown away by the programmable completion code and recomputed
3647 later. */
3648 (void)bash_ignore_filenames (m1);
3649 return m1;
3650}
Jari Aaltob80f6442004-07-27 13:29:18 +00003651
3652char *
3653bash_dequote_text (text)
3654 const char *text;
3655{
3656 char *dtxt;
3657 int qc;
3658
3659 qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
3660 dtxt = bash_dequote_filename ((char *)text, qc);
3661 return (dtxt);
3662}
Jari Aaltoccc6cda1996-12-23 17:02:34 +00003663#endif /* READLINE */