]> git.ipfire.org Git - thirdparty/bash.git/blob - bashline.c
commit bash-20190531 snapshot
[thirdparty/bash.git] / bashline.c
1 /* bashline.c -- Bash's interface to the readline library. */
2
3 /* Copyright (C) 1987-2019 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
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.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #if defined (READLINE)
24
25 #include "bashtypes.h"
26 #include "posixstat.h"
27
28 #if defined (HAVE_UNISTD_H)
29 # include <unistd.h>
30 #endif
31
32 #if defined (HAVE_GRP_H)
33 # include <grp.h>
34 #endif
35
36 #if defined (HAVE_NETDB_H)
37 # include <netdb.h>
38 #endif
39
40 #include <signal.h>
41
42 #include <stdio.h>
43 #include "chartypes.h"
44 #include "bashansi.h"
45 #include "bashintl.h"
46
47 #include "shell.h"
48 #include "input.h"
49 #include "parser.h"
50 #include "builtins.h"
51 #include "bashhist.h"
52 #include "bashline.h"
53 #include "execute_cmd.h"
54 #include "findcmd.h"
55 #include "pathexp.h"
56 #include "shmbutil.h"
57 #include "trap.h"
58 #include "flags.h"
59
60 #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
61 # include <mbstr.h> /* mbschr */
62 #endif
63
64 #include "builtins/common.h"
65
66 #include <readline/rlconf.h>
67 #include <readline/readline.h>
68 #include <readline/history.h>
69 #include <readline/rlmbutil.h>
70
71 #include <glob/glob.h>
72
73 #if defined (ALIAS)
74 # include "alias.h"
75 #endif
76
77 #if defined (PROGRAMMABLE_COMPLETION)
78 # include "pcomplete.h"
79 #endif
80
81 /* These should agree with the defines for emacs_mode and vi_mode in
82 rldefs.h, even though that's not a public readline header file. */
83 #ifndef EMACS_EDITING_MODE
84 # define NO_EDITING_MODE -1
85 # define EMACS_EDITING_MODE 1
86 # define VI_EDITING_MODE 0
87 #endif
88
89 #define RL_BOOLEAN_VARIABLE_VALUE(s) ((s)[0] == 'o' && (s)[1] == 'n' && (s)[2] == '\0')
90
91 #if defined (BRACE_COMPLETION)
92 extern int bash_brace_completion __P((int, int));
93 #endif /* BRACE_COMPLETION */
94
95 /* To avoid including curses.h/term.h/termcap.h and that whole mess. */
96 #ifdef _MINIX
97 extern int tputs __P((const char *string, int nlines, void (*outx)(int)));
98 #else
99 extern int tputs __P((const char *string, int nlines, int (*outx)(int)));
100 #endif
101
102 /* Forward declarations */
103
104 /* Functions bound to keys in Readline for Bash users. */
105 static int shell_expand_line __P((int, int));
106 static int display_shell_version __P((int, int));
107 static int operate_and_get_next __P((int, int));
108
109 static int bash_ignore_filenames __P((char **));
110 static int bash_ignore_everything __P((char **));
111 static int bash_progcomp_ignore_filenames __P((char **));
112
113 #if defined (BANG_HISTORY)
114 static char *history_expand_line_internal __P((char *));
115 static int history_expand_line __P((int, int));
116 static int tcsh_magic_space __P((int, int));
117 #endif /* BANG_HISTORY */
118 #ifdef ALIAS
119 static int alias_expand_line __P((int, int));
120 #endif
121 #if defined (BANG_HISTORY) && defined (ALIAS)
122 static int history_and_alias_expand_line __P((int, int));
123 #endif
124
125 static int bash_forward_shellword __P((int, int));
126 static int bash_backward_shellword __P((int, int));
127 static int bash_kill_shellword __P((int, int));
128 static int bash_backward_kill_shellword __P((int, int));
129
130 /* Helper functions for Readline. */
131 static char *restore_tilde __P((char *, char *));
132 static char *maybe_restore_tilde __P((char *, char *));
133
134 static char *bash_filename_rewrite_hook __P((char *, int));
135
136 static void bash_directory_expansion __P((char **));
137 static int bash_filename_stat_hook __P((char **));
138 static int bash_command_name_stat_hook __P((char **));
139 static int bash_directory_completion_hook __P((char **));
140 static int filename_completion_ignore __P((char **));
141 static int bash_push_line __P((void));
142
143 static int executable_completion __P((const char *, int));
144
145 static rl_icppfunc_t *save_directory_hook __P((void));
146 static void restore_directory_hook __P((rl_icppfunc_t));
147
148 static int directory_exists __P((const char *, int));
149
150 static void cleanup_expansion_error __P((void));
151 static void maybe_make_readline_line __P((char *));
152 static void set_up_new_line __P((char *));
153
154 static int check_redir __P((int));
155 static char **attempt_shell_completion __P((const char *, int, int));
156 static char *variable_completion_function __P((const char *, int));
157 static char *hostname_completion_function __P((const char *, int));
158 static char *command_subst_completion_function __P((const char *, int));
159
160 static void build_history_completion_array __P((void));
161 static char *history_completion_generator __P((const char *, int));
162 static int dynamic_complete_history __P((int, int));
163 static int bash_dabbrev_expand __P((int, int));
164
165 static void initialize_hostname_list __P((void));
166 static void add_host_name __P((char *));
167 static void snarf_hosts_from_file __P((char *));
168 static char **hostnames_matching __P((char *));
169
170 static void _ignore_completion_names __P((char **, sh_ignore_func_t *));
171 static int name_is_acceptable __P((const char *));
172 static int test_for_directory __P((const char *));
173 static int test_for_canon_directory __P((const char *));
174 static int return_zero __P((const char *));
175
176 static char *bash_dequote_filename __P((char *, int));
177 static char *quote_word_break_chars __P((char *));
178 static void set_filename_bstab __P((const char *));
179 static char *bash_quote_filename __P((char *, int, char *));
180
181 #ifdef _MINIX
182 static void putx __P((int));
183 #else
184 static int putx __P((int));
185 #endif
186 static int bash_execute_unix_command __P((int, int));
187 static void init_unix_command_map __P((void));
188 static int isolate_sequence __P((char *, int, int, int *));
189
190 static int set_saved_history __P((void));
191
192 #if defined (ALIAS)
193 static int posix_edit_macros __P((int, int));
194 #endif
195
196 static int bash_event_hook __P((void));
197
198 #if defined (PROGRAMMABLE_COMPLETION)
199 static int find_cmd_start __P((int));
200 static int find_cmd_end __P((int));
201 static char *find_cmd_name __P((int, int *, int *));
202 static char *prog_complete_return __P((const char *, int));
203
204 static char **prog_complete_matches;
205 #endif
206
207 extern int no_symbolic_links;
208 extern STRING_INT_ALIST word_token_alist[];
209
210 /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
211 completion functions which indicate what type of completion should be
212 done (at or before point) that can be bound to key sequences with
213 the readline library. */
214 #define SPECIFIC_COMPLETION_FUNCTIONS
215
216 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
217 static int bash_specific_completion __P((int, rl_compentry_func_t *));
218
219 static int bash_complete_filename_internal __P((int));
220 static int bash_complete_username_internal __P((int));
221 static int bash_complete_hostname_internal __P((int));
222 static int bash_complete_variable_internal __P((int));
223 static int bash_complete_command_internal __P((int));
224
225 static int bash_complete_filename __P((int, int));
226 static int bash_possible_filename_completions __P((int, int));
227 static int bash_complete_username __P((int, int));
228 static int bash_possible_username_completions __P((int, int));
229 static int bash_complete_hostname __P((int, int));
230 static int bash_possible_hostname_completions __P((int, int));
231 static int bash_complete_variable __P((int, int));
232 static int bash_possible_variable_completions __P((int, int));
233 static int bash_complete_command __P((int, int));
234 static int bash_possible_command_completions __P((int, int));
235
236 static int completion_glob_pattern __P((char *));
237 static char *glob_complete_word __P((const char *, int));
238 static int bash_glob_completion_internal __P((int));
239 static int bash_glob_complete_word __P((int, int));
240 static int bash_glob_expand_word __P((int, int));
241 static int bash_glob_list_expansions __P((int, int));
242
243 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
244
245 static int edit_and_execute_command __P((int, int, int, char *));
246 #if defined (VI_MODE)
247 static int vi_edit_and_execute_command __P((int, int));
248 static int bash_vi_complete __P((int, int));
249 #endif
250 static int emacs_edit_and_execute_command __P((int, int));
251
252 /* Non-zero once initalize_readline () has been called. */
253 int bash_readline_initialized = 0;
254
255 /* If non-zero, we do hostname completion, breaking words at `@' and
256 trying to complete the stuff after the `@' from our own internal
257 host list. */
258 int perform_hostname_completion = 1;
259
260 /* If non-zero, we don't do command completion on an empty line. */
261 int no_empty_command_completion;
262
263 /* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the
264 only possible matches. Set to 0 if you want to match filenames if they
265 are the only possible matches, even if FIGNORE says to. */
266 int force_fignore = 1;
267
268 /* Perform spelling correction on directory names during word completion */
269 int dircomplete_spelling = 0;
270
271 /* Expand directory names during word/filename completion. */
272 #if DIRCOMPLETE_EXPAND_DEFAULT
273 int dircomplete_expand = 1;
274 int dircomplete_expand_relpath = 1;
275 #else
276 int dircomplete_expand = 0;
277 int dircomplete_expand_relpath = 0;
278 #endif
279
280 /* When non-zero, perform `normal' shell quoting on completed filenames
281 even when the completed name contains a directory name with a shell
282 variable referene, so dollar signs in a filename get quoted appropriately.
283 Set to zero to remove dollar sign (and braces or parens as needed) from
284 the set of characters that will be quoted. */
285 int complete_fullquote = 1;
286
287 static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:";
288 static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:";
289 /* )) */
290
291 static const char *default_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{~"; /*}*/
292 static char *custom_filename_quote_characters = 0;
293 static char filename_bstab[256];
294
295 static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL;
296
297 static int dot_in_path = 0;
298
299 /* Set to non-zero when dabbrev-expand is running */
300 static int dabbrev_expand_active = 0;
301
302 /* What kind of quoting is performed by bash_quote_filename:
303 COMPLETE_DQUOTE = double-quoting the filename
304 COMPLETE_SQUOTE = single_quoting the filename
305 COMPLETE_BSQUOTE = backslash-quoting special chars in the filename
306 */
307 #define COMPLETE_DQUOTE 1
308 #define COMPLETE_SQUOTE 2
309 #define COMPLETE_BSQUOTE 3
310 static int completion_quoting_style = COMPLETE_BSQUOTE;
311
312 /* Flag values for the final argument to bash_default_completion */
313 #define DEFCOMP_CMDPOS 1
314
315 static rl_command_func_t *vi_tab_binding = rl_complete;
316
317 /* Change the readline VI-mode keymaps into or out of Posix.2 compliance.
318 Called when the shell is put into or out of `posix' mode. */
319 void
320 posix_readline_initialize (on_or_off)
321 int on_or_off;
322 {
323 static char kseq[2] = { CTRL ('I'), 0 }; /* TAB */
324
325 if (on_or_off)
326 rl_variable_bind ("comment-begin", "#");
327 #if defined (VI_MODE)
328 if (on_or_off)
329 {
330 vi_tab_binding = rl_function_of_keyseq (kseq, vi_insertion_keymap, (int *)NULL);
331 rl_bind_key_in_map (CTRL ('I'), rl_insert, vi_insertion_keymap);
332 }
333 else
334 {
335 if (rl_function_of_keyseq (kseq, vi_insertion_keymap, (int *)NULL) == rl_insert)
336 rl_bind_key_in_map (CTRL ('I'), vi_tab_binding, vi_insertion_keymap);
337 }
338 #endif
339 }
340
341 void
342 reset_completer_word_break_chars ()
343 {
344 rl_completer_word_break_characters = perform_hostname_completion ? savestring (bash_completer_word_break_characters) : savestring (bash_nohostname_word_break_characters);
345 }
346
347 /* When this function returns, rl_completer_word_break_characters points to
348 dynamically allocated memory. */
349 int
350 enable_hostname_completion (on_or_off)
351 int on_or_off;
352 {
353 int old_value;
354 char *at, *nv, *nval;
355
356 old_value = perform_hostname_completion;
357
358 if (on_or_off)
359 {
360 perform_hostname_completion = 1;
361 rl_special_prefixes = "$@";
362 }
363 else
364 {
365 perform_hostname_completion = 0;
366 rl_special_prefixes = "$";
367 }
368
369 /* Now we need to figure out how to appropriately modify and assign
370 rl_completer_word_break_characters depending on whether we want
371 hostname completion on or off. */
372
373 /* If this is the first time this has been called
374 (bash_readline_initialized == 0), use the sames values as before, but
375 allocate new memory for rl_completer_word_break_characters. */
376
377 if (bash_readline_initialized == 0 &&
378 (rl_completer_word_break_characters == 0 ||
379 rl_completer_word_break_characters == rl_basic_word_break_characters))
380 {
381 if (on_or_off)
382 rl_completer_word_break_characters = savestring (bash_completer_word_break_characters);
383 else
384 rl_completer_word_break_characters = savestring (bash_nohostname_word_break_characters);
385 }
386 else
387 {
388 /* See if we have anything to do. */
389 at = strchr (rl_completer_word_break_characters, '@');
390 if ((at == 0 && on_or_off == 0) || (at != 0 && on_or_off != 0))
391 return old_value;
392
393 /* We have something to do. Do it. */
394 nval = (char *)xmalloc (strlen (rl_completer_word_break_characters) + 1 + on_or_off);
395
396 if (on_or_off == 0)
397 {
398 /* Turn it off -- just remove `@' from word break chars. We want
399 to remove all occurrences of `@' from the char list, so we loop
400 rather than just copy the rest of the list over AT. */
401 for (nv = nval, at = rl_completer_word_break_characters; *at; )
402 if (*at != '@')
403 *nv++ = *at++;
404 else
405 at++;
406 *nv = '\0';
407 }
408 else
409 {
410 nval[0] = '@';
411 strcpy (nval + 1, rl_completer_word_break_characters);
412 }
413
414 free (rl_completer_word_break_characters);
415 rl_completer_word_break_characters = nval;
416 }
417
418 return (old_value);
419 }
420
421 /* Called once from parse.y if we are going to use readline. */
422 void
423 initialize_readline ()
424 {
425 rl_command_func_t *func;
426 char kseq[2];
427
428 if (bash_readline_initialized)
429 return;
430
431 rl_terminal_name = get_string_value ("TERM");
432 rl_instream = stdin;
433 rl_outstream = stderr;
434
435 /* Allow conditional parsing of the ~/.inputrc file. */
436 rl_readline_name = "Bash";
437
438 /* Add bindable names before calling rl_initialize so they may be
439 referenced in the various inputrc files. */
440 rl_add_defun ("shell-expand-line", shell_expand_line, -1);
441 #ifdef BANG_HISTORY
442 rl_add_defun ("history-expand-line", history_expand_line, -1);
443 rl_add_defun ("magic-space", tcsh_magic_space, -1);
444 #endif
445
446 rl_add_defun ("shell-forward-word", bash_forward_shellword, -1);
447 rl_add_defun ("shell-backward-word", bash_backward_shellword, -1);
448 rl_add_defun ("shell-kill-word", bash_kill_shellword, -1);
449 rl_add_defun ("shell-backward-kill-word", bash_backward_kill_shellword, -1);
450
451 #ifdef ALIAS
452 rl_add_defun ("alias-expand-line", alias_expand_line, -1);
453 # ifdef BANG_HISTORY
454 rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line, -1);
455 # endif
456 #endif
457
458 /* Backwards compatibility. */
459 rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1);
460
461 rl_add_defun ("operate-and-get-next", operate_and_get_next, -1);
462 rl_add_defun ("display-shell-version", display_shell_version, -1);
463 rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command, -1);
464
465 #if defined (BRACE_COMPLETION)
466 rl_add_defun ("complete-into-braces", bash_brace_completion, -1);
467 #endif
468
469 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
470 rl_add_defun ("complete-filename", bash_complete_filename, -1);
471 rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1);
472 rl_add_defun ("complete-username", bash_complete_username, -1);
473 rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1);
474 rl_add_defun ("complete-hostname", bash_complete_hostname, -1);
475 rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1);
476 rl_add_defun ("complete-variable", bash_complete_variable, -1);
477 rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1);
478 rl_add_defun ("complete-command", bash_complete_command, -1);
479 rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1);
480 rl_add_defun ("glob-complete-word", bash_glob_complete_word, -1);
481 rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1);
482 rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1);
483 #endif
484
485 rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1);
486 rl_add_defun ("dabbrev-expand", bash_dabbrev_expand, -1);
487
488 /* Bind defaults before binding our custom shell keybindings. */
489 if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0)
490 rl_initialize ();
491
492 /* Bind up our special shell functions. */
493 rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line, emacs_meta_keymap);
494
495 #ifdef BANG_HISTORY
496 rl_bind_key_if_unbound_in_map ('^', history_expand_line, emacs_meta_keymap);
497 #endif
498
499 rl_bind_key_if_unbound_in_map (CTRL ('O'), operate_and_get_next, emacs_standard_keymap);
500 rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version, emacs_ctlx_keymap);
501
502 /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
503 so it is not necessary to allow C-M-j for context switching. Turn
504 off this occasionally confusing behaviour. */
505 kseq[0] = CTRL('J');
506 kseq[1] = '\0';
507 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
508 if (func == rl_vi_editing_mode)
509 rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
510 kseq[0] = CTRL('M');
511 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
512 if (func == rl_vi_editing_mode)
513 rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap);
514 #if defined (VI_MODE)
515 kseq[0] = CTRL('E');
516 func = rl_function_of_keyseq (kseq, vi_movement_keymap, (int *)NULL);
517 if (func == rl_emacs_editing_mode)
518 rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap);
519 #endif
520
521 #if defined (BRACE_COMPLETION)
522 rl_bind_key_if_unbound_in_map ('{', bash_brace_completion, emacs_meta_keymap); /*}*/
523 #endif /* BRACE_COMPLETION */
524
525 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
526 rl_bind_key_if_unbound_in_map ('/', bash_complete_filename, emacs_meta_keymap);
527 rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions, emacs_ctlx_keymap);
528
529 /* Have to jump through hoops here because there is a default binding for
530 M-~ (rl_tilde_expand) */
531 kseq[0] = '~';
532 kseq[1] = '\0';
533 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
534 if (func == 0 || func == rl_tilde_expand)
535 rl_bind_keyseq_in_map (kseq, bash_complete_username, emacs_meta_keymap);
536
537 rl_bind_key_if_unbound_in_map ('~', bash_possible_username_completions, emacs_ctlx_keymap);
538
539 rl_bind_key_if_unbound_in_map ('@', bash_complete_hostname, emacs_meta_keymap);
540 rl_bind_key_if_unbound_in_map ('@', bash_possible_hostname_completions, emacs_ctlx_keymap);
541
542 rl_bind_key_if_unbound_in_map ('$', bash_complete_variable, emacs_meta_keymap);
543 rl_bind_key_if_unbound_in_map ('$', bash_possible_variable_completions, emacs_ctlx_keymap);
544
545 rl_bind_key_if_unbound_in_map ('!', bash_complete_command, emacs_meta_keymap);
546 rl_bind_key_if_unbound_in_map ('!', bash_possible_command_completions, emacs_ctlx_keymap);
547
548 rl_bind_key_if_unbound_in_map ('g', bash_glob_complete_word, emacs_meta_keymap);
549 rl_bind_key_if_unbound_in_map ('*', bash_glob_expand_word, emacs_ctlx_keymap);
550 rl_bind_key_if_unbound_in_map ('g', bash_glob_list_expansions, emacs_ctlx_keymap);
551
552 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
553
554 kseq[0] = TAB;
555 kseq[1] = '\0';
556 func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);
557 if (func == 0 || func == rl_tab_insert)
558 rl_bind_key_in_map (TAB, dynamic_complete_history, emacs_meta_keymap);
559
560 /* Tell the completer that we want a crack first. */
561 rl_attempted_completion_function = attempt_shell_completion;
562
563 /* Tell the completer that we might want to follow symbolic links or
564 do other expansion on directory names. */
565 set_directory_hook ();
566
567 rl_filename_rewrite_hook = bash_filename_rewrite_hook;
568
569 rl_filename_stat_hook = bash_filename_stat_hook;
570
571 /* Tell the filename completer we want a chance to ignore some names. */
572 rl_ignore_some_completions_function = filename_completion_ignore;
573
574 /* Bind C-xC-e to invoke emacs and run result as commands. */
575 rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command, emacs_ctlx_keymap);
576 #if defined (VI_MODE)
577 rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command, vi_movement_keymap);
578 # if defined (ALIAS)
579 rl_bind_key_if_unbound_in_map ('@', posix_edit_macros, vi_movement_keymap);
580 # endif
581
582 rl_bind_key_in_map ('\\', bash_vi_complete, vi_movement_keymap);
583 rl_bind_key_in_map ('*', bash_vi_complete, vi_movement_keymap);
584 rl_bind_key_in_map ('=', bash_vi_complete, vi_movement_keymap);
585 #endif
586
587 rl_completer_quote_characters = "'\"";
588
589 /* This sets rl_completer_word_break_characters and rl_special_prefixes
590 to the appropriate values, depending on whether or not hostname
591 completion is enabled. */
592 enable_hostname_completion (perform_hostname_completion);
593
594 /* characters that need to be quoted when appearing in filenames. */
595 rl_filename_quote_characters = default_filename_quote_characters;
596 set_filename_bstab (rl_filename_quote_characters);
597
598 rl_filename_quoting_function = bash_quote_filename;
599 rl_filename_dequoting_function = bash_dequote_filename;
600 rl_char_is_quoted_p = char_is_quoted;
601
602 #if 0
603 /* This is superfluous and makes it impossible to use tab completion in
604 vi mode even when explicitly binding it in ~/.inputrc. sv_strict_posix()
605 should already have called posix_readline_initialize() when
606 posixly_correct was set. */
607 if (posixly_correct)
608 posix_readline_initialize (1);
609 #endif
610
611 bash_readline_initialized = 1;
612 }
613
614 void
615 bashline_reinitialize ()
616 {
617 bash_readline_initialized = 0;
618 }
619
620 void
621 bashline_set_event_hook ()
622 {
623 rl_signal_event_hook = bash_event_hook;
624 }
625
626 void
627 bashline_reset_event_hook ()
628 {
629 rl_signal_event_hook = 0;
630 }
631
632 /* On Sun systems at least, rl_attempted_completion_function can end up
633 getting set to NULL, and rl_completion_entry_function set to do command
634 word completion if Bash is interrupted while trying to complete a command
635 word. This just resets all the completion functions to the right thing.
636 It's called from throw_to_top_level(). */
637 void
638 bashline_reset ()
639 {
640 tilde_initialize ();
641 rl_attempted_completion_function = attempt_shell_completion;
642 rl_completion_entry_function = NULL;
643 rl_ignore_some_completions_function = filename_completion_ignore;
644 rl_filename_quote_characters = default_filename_quote_characters;
645 set_filename_bstab (rl_filename_quote_characters);
646
647 set_directory_hook ();
648 rl_filename_stat_hook = bash_filename_stat_hook;
649
650 bashline_reset_event_hook ();
651
652 rl_sort_completion_matches = 1;
653 }
654
655 /* Contains the line to push into readline. */
656 static char *push_to_readline = (char *)NULL;
657
658 /* Push the contents of push_to_readline into the
659 readline buffer. */
660 static int
661 bash_push_line ()
662 {
663 if (push_to_readline)
664 {
665 rl_insert_text (push_to_readline);
666 free (push_to_readline);
667 push_to_readline = (char *)NULL;
668 rl_startup_hook = old_rl_startup_hook;
669 }
670 return 0;
671 }
672
673 /* Call this to set the initial text for the next line to read
674 from readline. */
675 int
676 bash_re_edit (line)
677 char *line;
678 {
679 FREE (push_to_readline);
680
681 push_to_readline = savestring (line);
682 old_rl_startup_hook = rl_startup_hook;
683 rl_startup_hook = bash_push_line;
684
685 return (0);
686 }
687
688 static int
689 display_shell_version (count, c)
690 int count, c;
691 {
692 rl_crlf ();
693 show_shell_version (0);
694 putc ('\r', rl_outstream);
695 fflush (rl_outstream);
696 rl_on_new_line ();
697 rl_redisplay ();
698 return 0;
699 }
700
701 /* **************************************************************** */
702 /* */
703 /* Readline Stuff */
704 /* */
705 /* **************************************************************** */
706
707 /* If the user requests hostname completion, then simply build a list
708 of hosts, and complete from that forever more, or at least until
709 HOSTFILE is unset. */
710
711 /* THIS SHOULD BE A STRINGLIST. */
712 /* The kept list of hostnames. */
713 static char **hostname_list = (char **)NULL;
714
715 /* The physical size of the above list. */
716 static int hostname_list_size;
717
718 /* The number of hostnames in the above list. */
719 static int hostname_list_length;
720
721 /* Whether or not HOSTNAME_LIST has been initialized. */
722 int hostname_list_initialized = 0;
723
724 /* Initialize the hostname completion table. */
725 static void
726 initialize_hostname_list ()
727 {
728 char *temp;
729
730 temp = get_string_value ("HOSTFILE");
731 if (temp == 0)
732 temp = get_string_value ("hostname_completion_file");
733 if (temp == 0)
734 temp = DEFAULT_HOSTS_FILE;
735
736 snarf_hosts_from_file (temp);
737
738 if (hostname_list)
739 hostname_list_initialized++;
740 }
741
742 /* Add NAME to the list of hosts. */
743 static void
744 add_host_name (name)
745 char *name;
746 {
747 if (hostname_list_length + 2 > hostname_list_size)
748 {
749 hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32);
750 hostname_list = strvec_resize (hostname_list, hostname_list_size);
751 }
752
753 hostname_list[hostname_list_length++] = savestring (name);
754 hostname_list[hostname_list_length] = (char *)NULL;
755 }
756
757 #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
758
759 static void
760 snarf_hosts_from_file (filename)
761 char *filename;
762 {
763 FILE *file;
764 char *temp, buffer[256], name[256];
765 register int i, start;
766
767 file = fopen (filename, "r");
768 if (file == 0)
769 return;
770
771 while (temp = fgets (buffer, 255, file))
772 {
773 /* Skip to first character. */
774 for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++)
775 ;
776
777 /* If comment or blank line, ignore. */
778 if (buffer[i] == '\0' || buffer[i] == '#')
779 continue;
780
781 /* If `preprocessor' directive, do the include. */
782 if (strncmp (buffer + i, "$include ", 9) == 0)
783 {
784 char *incfile, *t;
785
786 /* Find start of filename. */
787 for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++)
788 ;
789
790 /* Find end of filename. */
791 for (t = incfile; *t && cr_whitespace (*t) == 0; t++)
792 ;
793
794 *t = '\0';
795
796 snarf_hosts_from_file (incfile);
797 continue;
798 }
799
800 /* Skip internet address if present. */
801 if (DIGIT (buffer[i]))
802 for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++);
803
804 /* Gobble up names. Each name is separated with whitespace. */
805 while (buffer[i])
806 {
807 for (; cr_whitespace (buffer[i]); i++)
808 ;
809 if (buffer[i] == '\0' || buffer[i] == '#')
810 break;
811
812 /* Isolate the current word. */
813 for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++)
814 ;
815 if (i == start)
816 continue;
817 strncpy (name, buffer + start, i - start);
818 name[i - start] = '\0';
819 add_host_name (name);
820 }
821 }
822 fclose (file);
823 }
824
825 /* Return the hostname list. */
826 char **
827 get_hostname_list ()
828 {
829 if (hostname_list_initialized == 0)
830 initialize_hostname_list ();
831 return (hostname_list);
832 }
833
834 void
835 clear_hostname_list ()
836 {
837 register int i;
838
839 if (hostname_list_initialized == 0)
840 return;
841 for (i = 0; i < hostname_list_length; i++)
842 free (hostname_list[i]);
843 hostname_list_length = hostname_list_initialized = 0;
844 }
845
846 /* Return a NULL terminated list of hostnames which begin with TEXT.
847 Initialize the hostname list the first time if necessary.
848 The array is malloc ()'ed, but not the individual strings. */
849 static char **
850 hostnames_matching (text)
851 char *text;
852 {
853 register int i, len, nmatch, rsize;
854 char **result;
855
856 if (hostname_list_initialized == 0)
857 initialize_hostname_list ();
858
859 if (hostname_list_initialized == 0)
860 return ((char **)NULL);
861
862 /* Special case. If TEXT consists of nothing, then the whole list is
863 what is desired. */
864 if (*text == '\0')
865 {
866 result = strvec_create (1 + hostname_list_length);
867 for (i = 0; i < hostname_list_length; i++)
868 result[i] = hostname_list[i];
869 result[i] = (char *)NULL;
870 return (result);
871 }
872
873 /* Scan until found, or failure. */
874 len = strlen (text);
875 result = (char **)NULL;
876 for (i = nmatch = rsize = 0; i < hostname_list_length; i++)
877 {
878 if (STREQN (text, hostname_list[i], len) == 0)
879 continue;
880
881 /* OK, it matches. Add it to the list. */
882 if (nmatch >= (rsize - 1))
883 {
884 rsize = (rsize + 16) - (rsize % 16);
885 result = strvec_resize (result, rsize);
886 }
887
888 result[nmatch++] = hostname_list[i];
889 }
890 if (nmatch)
891 result[nmatch] = (char *)NULL;
892 return (result);
893 }
894
895 /* The equivalent of the Korn shell C-o operate-and-get-next-history-line
896 editing command. */
897 static int saved_history_line_to_use = -1;
898 static int last_saved_history_line = -1;
899
900 #define HISTORY_FULL() (history_is_stifled () && history_length >= history_max_entries)
901
902 static int
903 set_saved_history ()
904 {
905 /* XXX - compensate for assumption that history was `shuffled' if it was
906 actually not. */
907 if (HISTORY_FULL () &&
908 hist_last_line_added == 0 &&
909 saved_history_line_to_use < history_length - 1)
910 saved_history_line_to_use++;
911
912 if (saved_history_line_to_use >= 0)
913 {
914 rl_get_previous_history (history_length - saved_history_line_to_use, 0);
915 last_saved_history_line = saved_history_line_to_use;
916 }
917 saved_history_line_to_use = -1;
918 rl_startup_hook = old_rl_startup_hook;
919 return (0);
920 }
921
922 static int
923 operate_and_get_next (count, c)
924 int count, c;
925 {
926 int where;
927
928 /* Accept the current line. */
929 rl_newline (1, c);
930
931 /* Find the current line, and find the next line to use. */
932 where = rl_explicit_arg ? count : where_history ();
933
934 if (HISTORY_FULL () || (where >= history_length - 1) || rl_explicit_arg)
935 saved_history_line_to_use = where;
936 else
937 saved_history_line_to_use = where + 1;
938
939 old_rl_startup_hook = rl_startup_hook;
940 rl_startup_hook = set_saved_history;
941
942 return 0;
943 }
944
945 /* This vi mode command causes VI_EDIT_COMMAND to be run on the current
946 command being entered (if no explicit argument is given), otherwise on
947 a command from the history file. */
948
949 #define VI_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-vi}}\""
950 #define EMACS_EDIT_COMMAND "fc -e \"${VISUAL:-${EDITOR:-emacs}}\""
951 #define POSIX_VI_EDIT_COMMAND "fc -e vi"
952
953 static int
954 edit_and_execute_command (count, c, editing_mode, edit_command)
955 int count, c, editing_mode;
956 char *edit_command;
957 {
958 char *command, *metaval;
959 int r, rrs, metaflag;
960 sh_parser_state_t ps;
961
962 rrs = rl_readline_state;
963 saved_command_line_count = current_command_line_count;
964
965 /* Accept the current line. */
966 rl_newline (1, c);
967
968 if (rl_explicit_arg)
969 {
970 command = (char *)xmalloc (strlen (edit_command) + 8);
971 sprintf (command, "%s %d", edit_command, count);
972 }
973 else
974 {
975 /* Take the command we were just editing, add it to the history file,
976 then call fc to operate on it. We have to add a dummy command to
977 the end of the history because fc ignores the last command (assumes
978 it's supposed to deal with the command before the `fc'). */
979 /* This breaks down when using command-oriented history and are not
980 finished with the command, so we should not ignore the last command */
981 using_history ();
982 current_command_line_count++; /* for rl_newline above */
983 bash_add_history (rl_line_buffer);
984 current_command_line_count = 0; /* for dummy history entry */
985 bash_add_history ("");
986 history_lines_this_session++;
987 using_history ();
988 command = savestring (edit_command);
989 }
990
991 metaval = rl_variable_value ("input-meta");
992 metaflag = RL_BOOLEAN_VARIABLE_VALUE (metaval);
993
994 if (rl_deprep_term_function)
995 (*rl_deprep_term_function) ();
996 save_parser_state (&ps);
997 r = parse_and_execute (command, (editing_mode == VI_EDITING_MODE) ? "v" : "C-xC-e", SEVAL_NOHIST);
998 restore_parser_state (&ps);
999 if (rl_prep_term_function)
1000 (*rl_prep_term_function) (metaflag);
1001
1002 current_command_line_count = saved_command_line_count;
1003
1004 /* Now erase the contents of the current line and undo the effects of the
1005 rl_accept_line() above. We don't even want to make the text we just
1006 executed available for undoing. */
1007 rl_line_buffer[0] = '\0'; /* XXX */
1008 rl_point = rl_end = 0;
1009 rl_done = 0;
1010 rl_readline_state = rrs;
1011
1012 #if defined (VI_MODE)
1013 if (editing_mode == VI_EDITING_MODE)
1014 rl_vi_insertion_mode (1, c);
1015 #endif
1016
1017 rl_forced_update_display ();
1018
1019 return r;
1020 }
1021
1022 #if defined (VI_MODE)
1023 static int
1024 vi_edit_and_execute_command (count, c)
1025 int count, c;
1026 {
1027 if (posixly_correct)
1028 return (edit_and_execute_command (count, c, VI_EDITING_MODE, POSIX_VI_EDIT_COMMAND));
1029 else
1030 return (edit_and_execute_command (count, c, VI_EDITING_MODE, VI_EDIT_COMMAND));
1031 }
1032 #endif /* VI_MODE */
1033
1034 static int
1035 emacs_edit_and_execute_command (count, c)
1036 int count, c;
1037 {
1038 return (edit_and_execute_command (count, c, EMACS_EDITING_MODE, EMACS_EDIT_COMMAND));
1039 }
1040
1041 #if defined (ALIAS)
1042 static int
1043 posix_edit_macros (count, key)
1044 int count, key;
1045 {
1046 int c;
1047 char alias_name[3], *alias_value, *macro;
1048
1049 c = rl_read_key ();
1050 alias_name[0] = '_';
1051 alias_name[1] = c;
1052 alias_name[2] = '\0';
1053
1054 alias_value = get_alias_value (alias_name);
1055 if (alias_value && *alias_value)
1056 {
1057 macro = savestring (alias_value);
1058 rl_push_macro_input (macro);
1059 }
1060 return 0;
1061 }
1062 #endif
1063
1064 /* Bindable commands that move `shell-words': that is, sequences of
1065 non-unquoted-metacharacters. */
1066
1067 #define WORDDELIM(c) (shellmeta(c) || shellblank(c))
1068
1069 static int
1070 bash_forward_shellword (count, key)
1071 int count, key;
1072 {
1073 size_t slen;
1074 int c, p;
1075 DECLARE_MBSTATE;
1076
1077 if (count < 0)
1078 return (bash_backward_shellword (-count, key));
1079
1080 /* The tricky part of this is deciding whether or not the first character
1081 we're on is an unquoted metacharacter. Not completely handled yet. */
1082 /* XXX - need to test this stuff with backslash-escaped shell
1083 metacharacters and unclosed single- and double-quoted strings. */
1084
1085 p = rl_point;
1086 slen = rl_end;
1087
1088 while (count)
1089 {
1090 if (p == rl_end)
1091 {
1092 rl_point = rl_end;
1093 return 0;
1094 }
1095
1096 /* Are we in a quoted string? If we are, move to the end of the quoted
1097 string and continue the outer loop. We only want quoted strings, not
1098 backslash-escaped characters, but char_is_quoted doesn't
1099 differentiate. */
1100 if (char_is_quoted (rl_line_buffer, p) && p > 0 && rl_line_buffer[p-1] != '\\')
1101 {
1102 do
1103 ADVANCE_CHAR (rl_line_buffer, slen, p);
1104 while (p < rl_end && char_is_quoted (rl_line_buffer, p));
1105 count--;
1106 continue;
1107 }
1108
1109 /* Rest of code assumes we are not in a quoted string. */
1110 /* Move forward until we hit a non-metacharacter. */
1111 while (p < rl_end && (c = rl_line_buffer[p]) && WORDDELIM (c))
1112 {
1113 switch (c)
1114 {
1115 default:
1116 ADVANCE_CHAR (rl_line_buffer, slen, p);
1117 continue; /* straight back to loop, don't increment p */
1118 case '\\':
1119 if (p < rl_end && rl_line_buffer[p])
1120 ADVANCE_CHAR (rl_line_buffer, slen, p);
1121 break;
1122 case '\'':
1123 p = skip_to_delim (rl_line_buffer, ++p, "'", SD_NOJMP);
1124 break;
1125 case '"':
1126 p = skip_to_delim (rl_line_buffer, ++p, "\"", SD_NOJMP);
1127 break;
1128 }
1129
1130 if (p < rl_end)
1131 p++;
1132 }
1133
1134 if (rl_line_buffer[p] == 0 || p == rl_end)
1135 {
1136 rl_point = rl_end;
1137 rl_ding ();
1138 return 0;
1139 }
1140
1141 /* Now move forward until we hit a non-quoted metacharacter or EOL */
1142 while (p < rl_end && (c = rl_line_buffer[p]) && WORDDELIM (c) == 0)
1143 {
1144 switch (c)
1145 {
1146 default:
1147 ADVANCE_CHAR (rl_line_buffer, slen, p);
1148 continue; /* straight back to loop, don't increment p */
1149 case '\\':
1150 if (p < rl_end && rl_line_buffer[p])
1151 ADVANCE_CHAR (rl_line_buffer, slen, p);
1152 break;
1153 case '\'':
1154 p = skip_to_delim (rl_line_buffer, ++p, "'", SD_NOJMP);
1155 break;
1156 case '"':
1157 p = skip_to_delim (rl_line_buffer, ++p, "\"", SD_NOJMP);
1158 break;
1159 }
1160
1161 if (p < rl_end)
1162 p++;
1163 }
1164
1165 if (p == rl_end || rl_line_buffer[p] == 0)
1166 {
1167 rl_point = rl_end;
1168 return (0);
1169 }
1170
1171 count--;
1172 }
1173
1174 rl_point = p;
1175 return (0);
1176 }
1177
1178 static int
1179 bash_backward_shellword (count, key)
1180 int count, key;
1181 {
1182 size_t slen;
1183 int c, p;
1184 DECLARE_MBSTATE;
1185
1186 if (count < 0)
1187 return (bash_forward_shellword (-count, key));
1188
1189 p = rl_point;
1190 slen = rl_end;
1191
1192 if (p == rl_end && p > 0)
1193 p--;
1194
1195 while (count)
1196 {
1197 if (p == 0)
1198 {
1199 rl_point = 0;
1200 return 0;
1201 }
1202
1203 /* Move backward until we hit a non-metacharacter. */
1204 while (p > 0)
1205 {
1206 c = rl_line_buffer[p];
1207 if (WORDDELIM (c) == 0 || char_is_quoted (rl_line_buffer, p))
1208 break;
1209 BACKUP_CHAR (rl_line_buffer, slen, p);
1210 }
1211
1212 if (p == 0)
1213 {
1214 rl_point = 0;
1215 return 0;
1216 }
1217
1218 /* Now move backward until we hit a metacharacter or BOL. */
1219 while (p > 0)
1220 {
1221 c = rl_line_buffer[p];
1222 if (WORDDELIM (c) && char_is_quoted (rl_line_buffer, p) == 0)
1223 break;
1224 BACKUP_CHAR (rl_line_buffer, slen, p);
1225 }
1226
1227 count--;
1228 }
1229
1230 rl_point = p;
1231 return 0;
1232 }
1233
1234 static int
1235 bash_kill_shellword (count, key)
1236 int count, key;
1237 {
1238 int p;
1239
1240 if (count < 0)
1241 return (bash_backward_kill_shellword (-count, key));
1242
1243 p = rl_point;
1244 bash_forward_shellword (count, key);
1245
1246 if (rl_point != p)
1247 rl_kill_text (p, rl_point);
1248
1249 rl_point = p;
1250 if (rl_editing_mode == EMACS_EDITING_MODE) /* 1 == emacs_mode */
1251 rl_mark = rl_point;
1252
1253 return 0;
1254 }
1255
1256 static int
1257 bash_backward_kill_shellword (count, key)
1258 int count, key;
1259 {
1260 int p;
1261
1262 if (count < 0)
1263 return (bash_kill_shellword (-count, key));
1264
1265 p = rl_point;
1266 bash_backward_shellword (count, key);
1267
1268 if (rl_point != p)
1269 rl_kill_text (p, rl_point);
1270
1271 if (rl_editing_mode == EMACS_EDITING_MODE) /* 1 == emacs_mode */
1272 rl_mark = rl_point;
1273
1274 return 0;
1275 }
1276
1277
1278 /* **************************************************************** */
1279 /* */
1280 /* How To Do Shell Completion */
1281 /* */
1282 /* **************************************************************** */
1283
1284 #define COMMAND_SEPARATORS ";|&{(`"
1285 /* )} */
1286 #define COMMAND_SEPARATORS_PLUS_WS ";|&{(` \t"
1287 /* )} */
1288
1289 /* check for redirections and other character combinations that are not
1290 command separators */
1291 static int
1292 check_redir (ti)
1293 int ti;
1294 {
1295 register int this_char, prev_char;
1296
1297 /* Handle the two character tokens `>&', `<&', and `>|'.
1298 We are not in a command position after one of these. */
1299 this_char = rl_line_buffer[ti];
1300 prev_char = (ti > 0) ? rl_line_buffer[ti - 1] : 0;
1301
1302 if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) ||
1303 (this_char == '|' && prev_char == '>'))
1304 return (1);
1305 else if (this_char == '{' && prev_char == '$') /*}*/
1306 return (1);
1307 #if 0 /* Not yet */
1308 else if (this_char == '(' && prev_char == '$') /*)*/
1309 return (1);
1310 else if (this_char == '(' && prev_char == '<') /*)*/
1311 return (1);
1312 #if defined (EXTENDED_GLOB)
1313 else if (extended_glob && this_char == '(' && prev_char == '!') /*)*/
1314 return (1);
1315 #endif
1316 #endif
1317 else if (char_is_quoted (rl_line_buffer, ti))
1318 return (1);
1319 return (0);
1320 }
1321
1322 #if defined (PROGRAMMABLE_COMPLETION)
1323 /*
1324 * XXX - because of the <= start test, and setting os = s+1, this can
1325 * potentially return os > start. This is probably not what we want to
1326 * happen, but fix later after 2.05a-release.
1327 */
1328 static int
1329 find_cmd_start (start)
1330 int start;
1331 {
1332 register int s, os, ns;
1333
1334 os = 0;
1335 /* Flags == SD_NOJMP only because we want to skip over command substitutions
1336 in assignment statements. Have to test whether this affects `standalone'
1337 command substitutions as individual words. */
1338 while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE/*|SD_NOSKIPCMD*/)) <= start) &&
1339 rl_line_buffer[s])
1340 {
1341 /* Handle >| token crudely; treat as > not | */
1342 if (rl_line_buffer[s] == '|' && rl_line_buffer[s-1] == '>')
1343 {
1344 ns = skip_to_delim (rl_line_buffer, s+1, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE/*|SD_NOSKIPCMD*/);
1345 if (ns > start || rl_line_buffer[ns] == 0)
1346 return os;
1347 os = ns+1;
1348 continue;
1349 }
1350 os = s+1;
1351 }
1352 return os;
1353 }
1354
1355 static int
1356 find_cmd_end (end)
1357 int end;
1358 {
1359 register int e;
1360
1361 e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS, SD_NOJMP|SD_COMPLETE);
1362 return e;
1363 }
1364
1365 static char *
1366 find_cmd_name (start, sp, ep)
1367 int start;
1368 int *sp, *ep;
1369 {
1370 char *name;
1371 register int s, e;
1372
1373 for (s = start; whitespace (rl_line_buffer[s]); s++)
1374 ;
1375
1376 /* skip until a shell break character */
1377 e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n", SD_NOJMP|SD_COMPLETE);
1378
1379 name = substring (rl_line_buffer, s, e);
1380
1381 if (sp)
1382 *sp = s;
1383 if (ep)
1384 *ep = e;
1385
1386 return (name);
1387 }
1388
1389 static char *
1390 prog_complete_return (text, matchnum)
1391 const char *text;
1392 int matchnum;
1393 {
1394 static int ind;
1395
1396 if (matchnum == 0)
1397 ind = 0;
1398
1399 if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0)
1400 return (char *)NULL;
1401 return (prog_complete_matches[ind++]);
1402 }
1403
1404 #endif /* PROGRAMMABLE_COMPLETION */
1405
1406 /* Try and catch completion attempts that are syntax errors or otherwise
1407 invalid. */
1408 static int
1409 invalid_completion (text, ind)
1410 const char *text;
1411 int ind;
1412 {
1413 int pind;
1414
1415 /* If we don't catch these here, the next clause will */
1416 if (ind > 0 && rl_line_buffer[ind] == '(' && /*)*/
1417 member (rl_line_buffer[ind-1], "$<>"))
1418 return 0;
1419
1420 pind = ind - 1;
1421 while (pind > 0 && whitespace (rl_line_buffer[pind]))
1422 pind--;
1423 /* If we have only whitespace preceding a paren, it's valid */
1424 if (ind >= 0 && pind <= 0 && rl_line_buffer[ind] == '(') /*)*/
1425 return 0;
1426 /* Flag the invalid completions, which are mostly syntax errors */
1427 if (ind > 0 && rl_line_buffer[ind] == '(' && /*)*/
1428 member (rl_line_buffer[pind], COMMAND_SEPARATORS) == 0)
1429 return 1;
1430
1431 return 0;
1432 }
1433
1434 /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are
1435 at START and END. Return an array of matches, or NULL if none. */
1436 static char **
1437 attempt_shell_completion (text, start, end)
1438 const char *text;
1439 int start, end;
1440 {
1441 int in_command_position, ti, qc, dflags;
1442 char **matches, *command_separator_chars;
1443 #if defined (PROGRAMMABLE_COMPLETION)
1444 int have_progcomps, was_assignment;
1445 COMPSPEC *iw_compspec;
1446 #endif
1447
1448 command_separator_chars = COMMAND_SEPARATORS;
1449 matches = (char **)NULL;
1450 rl_ignore_some_completions_function = filename_completion_ignore;
1451
1452 rl_filename_quote_characters = default_filename_quote_characters;
1453 set_filename_bstab (rl_filename_quote_characters);
1454 set_directory_hook ();
1455 rl_filename_stat_hook = bash_filename_stat_hook;
1456
1457 rl_sort_completion_matches = 1; /* sort by default */
1458
1459 /* Determine if this could be a command word. It is if it appears at
1460 the start of the line (ignoring preceding whitespace), or if it
1461 appears after a character that separates commands. It cannot be a
1462 command word if we aren't at the top-level prompt. */
1463 ti = start - 1;
1464 qc = -1;
1465
1466 while ((ti > -1) && (whitespace (rl_line_buffer[ti])))
1467 ti--;
1468
1469 #if 1
1470 /* If this is an open quote, maybe we're trying to complete a quoted
1471 command name. */
1472 if (ti >= 0 && (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\''))
1473 {
1474 qc = rl_line_buffer[ti];
1475 ti--;
1476 while (ti > -1 && (whitespace (rl_line_buffer[ti])))
1477 ti--;
1478 }
1479 #endif
1480
1481 in_command_position = 0;
1482 if (ti < 0)
1483 {
1484 /* Only do command completion at the start of a line when we
1485 are prompting at the top level. */
1486 if (current_prompt_string == ps1_prompt)
1487 in_command_position++;
1488 else if (parser_in_command_position ())
1489 in_command_position++;
1490 }
1491 else if (member (rl_line_buffer[ti], command_separator_chars))
1492 {
1493 in_command_position++;
1494
1495 if (check_redir (ti) == 1)
1496 in_command_position = 0;
1497 }
1498 else
1499 {
1500 /* This still could be in command position. It is possible
1501 that all of the previous words on the line are variable
1502 assignments. */
1503 }
1504
1505 if (in_command_position && invalid_completion (text, ti))
1506 {
1507 rl_attempted_completion_over = 1;
1508 return ((char **)NULL);
1509 }
1510
1511 /* Check that we haven't incorrectly flagged a closed command substitution
1512 as indicating we're in a command position. */
1513 if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' &&
1514 *text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0)
1515 in_command_position = 0;
1516
1517 /* Special handling for command substitution. If *TEXT is a backquote,
1518 it can be the start or end of an old-style command substitution, or
1519 unmatched. If it's unmatched, both calls to unclosed_pair will
1520 succeed. Don't bother if readline found a single quote and we are
1521 completing on the substring. */
1522 if (*text == '`' && rl_completion_quote_character != '\'' &&
1523 (in_command_position || (unclosed_pair (rl_line_buffer, start, "`") &&
1524 unclosed_pair (rl_line_buffer, end, "`"))))
1525 matches = rl_completion_matches (text, command_subst_completion_function);
1526
1527 #if defined (PROGRAMMABLE_COMPLETION)
1528 /* Attempt programmable completion. */
1529 have_progcomps = prog_completion_enabled && (progcomp_size () > 0);
1530 iw_compspec = progcomp_search (INITIALWORD);
1531 if (matches == 0 &&
1532 (in_command_position == 0 || text[0] == '\0' || (in_command_position && iw_compspec)) &&
1533 current_prompt_string == ps1_prompt)
1534 {
1535 int s, e, s1, e1, os, foundcs;
1536 char *n;
1537
1538 /* XXX - don't free the members */
1539 if (prog_complete_matches)
1540 free (prog_complete_matches);
1541 prog_complete_matches = (char **)NULL;
1542
1543 os = start;
1544 n = 0;
1545 was_assignment = 0;
1546 s = find_cmd_start (os);
1547 e = find_cmd_end (end);
1548 do
1549 {
1550 /* Don't read past the end of rl_line_buffer */
1551 if (s > rl_end)
1552 {
1553 s1 = s = e1;
1554 break;
1555 }
1556 /* Or past point if point is within an assignment statement */
1557 else if (was_assignment && s > rl_point)
1558 {
1559 s1 = s = e1;
1560 break;
1561 }
1562 /* Skip over assignment statements preceding a command name. If we
1563 don't find a command name at all, we can perform command name
1564 completion. If we find a partial command name, we should perform
1565 command name completion on it. */
1566 FREE (n);
1567 n = find_cmd_name (s, &s1, &e1);
1568 s = e1 + 1;
1569 }
1570 while (was_assignment = assignment (n, 0));
1571 s = s1; /* reset to index where name begins */
1572
1573 /* s == index of where command name begins (reset above)
1574 e == end of current command, may be end of line
1575 s1 = index of where command name begins
1576 e1 == index of where command name ends
1577 start == index of where word to be completed begins
1578 end == index of where word to be completed ends
1579 if (s == start) we are doing command word completion for sure
1580 if (e1 == end) we are at the end of the command name and completing it */
1581 if (start == 0 && end == 0 && e != 0 && text[0] == '\0') /* beginning of non-empty line */
1582 foundcs = 0;
1583 else if (start == end && start == s1 && e != 0 && e1 > end) /* beginning of command name, leading whitespace */
1584 foundcs = 0;
1585 else if (e == 0 && e == s && text[0] == '\0' && have_progcomps) /* beginning of empty line */
1586 prog_complete_matches = programmable_completions (EMPTYCMD, text, s, e, &foundcs);
1587 else if (start == end && text[0] == '\0' && s1 > start && whitespace (rl_line_buffer[start]))
1588 foundcs = 0; /* whitespace before command name */
1589 else if (e > s && was_assignment == 0 && e1 == end && rl_line_buffer[e] == 0 && whitespace (rl_line_buffer[e-1]) == 0)
1590 {
1591 /* not assignment statement, but still want to perform command
1592 completion if we are composing command word. */
1593 foundcs = 0;
1594 in_command_position = s == start && STREQ (n, text); /* XXX */
1595 }
1596 else if (e > s && was_assignment == 0 && have_progcomps)
1597 {
1598 prog_complete_matches = programmable_completions (n, text, s, e, &foundcs);
1599 /* command completion if programmable completion fails */
1600 /* If we have a completion for the initial word, we can prefer that */
1601 in_command_position = s == start && (iw_compspec || STREQ (n, text)); /* XXX */
1602 if (iw_compspec && in_command_position)
1603 foundcs = 0;
1604 }
1605 /* empty command name following command separator */
1606 else if (s >= e && n[0] == '\0' && text[0] == '\0' && start > 0 &&
1607 was_assignment == 0 && member (rl_line_buffer[start-1], COMMAND_SEPARATORS))
1608 {
1609 foundcs = 0;
1610 in_command_position = 1;
1611 }
1612 else if (s >= e && n[0] == '\0' && text[0] == '\0' && start > 0)
1613 {
1614 foundcs = 0; /* empty command name following optional assignments */
1615 in_command_position += was_assignment;
1616 }
1617 else if (s == start && e == end && STREQ (n, text) && start > 0)
1618 {
1619 foundcs = 0; /* partial command name following assignments */
1620 in_command_position = 1;
1621 }
1622 else
1623 foundcs = 0;
1624
1625 /* If we have defined a compspec for the initial (command) word, call
1626 it and process the results like any other programmable completion. */
1627 if (in_command_position && have_progcomps && foundcs == 0 && iw_compspec)
1628 prog_complete_matches = programmable_completions (INITIALWORD, text, s, e, &foundcs);
1629
1630 FREE (n);
1631 /* XXX - if we found a COMPSPEC for the command, just return whatever
1632 the programmable completion code returns, and disable the default
1633 filename completion that readline will do unless the COPT_DEFAULT
1634 option has been set with the `-o default' option to complete or
1635 compopt. */
1636 if (foundcs)
1637 {
1638 pcomp_set_readline_variables (foundcs, 1);
1639 /* Turn what the programmable completion code returns into what
1640 readline wants. I should have made compute_lcd_of_matches
1641 external... */
1642 matches = rl_completion_matches (text, prog_complete_return);
1643 if ((foundcs & COPT_DEFAULT) == 0)
1644 rl_attempted_completion_over = 1; /* no default */
1645 if (matches || ((foundcs & COPT_BASHDEFAULT) == 0))
1646 return (matches);
1647 }
1648 }
1649 #endif
1650
1651 if (matches == 0)
1652 {
1653 dflags = 0;
1654 if (in_command_position)
1655 dflags |= DEFCOMP_CMDPOS;
1656 matches = bash_default_completion (text, start, end, qc, dflags);
1657 }
1658
1659 return matches;
1660 }
1661
1662 char **
1663 bash_default_completion (text, start, end, qc, compflags)
1664 const char *text;
1665 int start, end, qc, compflags;
1666 {
1667 char **matches, *t;
1668
1669 matches = (char **)NULL;
1670
1671 /* New posix-style command substitution or variable name? */
1672 if (*text == '$')
1673 {
1674 if (qc != '\'' && text[1] == '(') /* ) */
1675 matches = rl_completion_matches (text, command_subst_completion_function);
1676 else
1677 {
1678 matches = rl_completion_matches (text, variable_completion_function);
1679 /* If a single match, see if it expands to a directory name and append
1680 a slash if it does. This requires us to expand the variable name,
1681 so we don't want to display errors if the variable is unset. This
1682 can happen with dynamic variables whose value has never been
1683 requested. */
1684 if (matches && matches[0] && matches[1] == 0)
1685 {
1686 t = savestring (matches[0]);
1687 bash_filename_stat_hook (&t);
1688 /* doesn't use test_for_directory because that performs tilde
1689 expansion */
1690 if (file_isdir (t))
1691 rl_completion_append_character = '/';
1692 free (t);
1693 }
1694 }
1695 }
1696
1697 /* If the word starts in `~', and there is no slash in the word, then
1698 try completing this word as a username. */
1699 if (matches == 0 && *text == '~' && mbschr (text, '/') == 0)
1700 matches = rl_completion_matches (text, rl_username_completion_function);
1701
1702 /* Another one. Why not? If the word starts in '@', then look through
1703 the world of known hostnames for completion first. */
1704 if (matches == 0 && perform_hostname_completion && *text == '@')
1705 matches = rl_completion_matches (text, hostname_completion_function);
1706
1707 /* And last, (but not least) if this word is in a command position, then
1708 complete over possible command names, including aliases, functions,
1709 and command names. */
1710 if (matches == 0 && (compflags & DEFCOMP_CMDPOS))
1711 {
1712 /* If END == START and text[0] == 0, we are trying to complete an empty
1713 command word. */
1714 if (no_empty_command_completion && end == start && text[0] == '\0')
1715 {
1716 matches = (char **)NULL;
1717 rl_ignore_some_completions_function = bash_ignore_everything;
1718 }
1719 else
1720 {
1721 #define CMD_IS_DIR(x) (absolute_pathname(x) == 0 && absolute_program(x) == 0 && *(x) != '~' && test_for_directory (x))
1722
1723 dot_in_path = 0;
1724 matches = rl_completion_matches (text, command_word_completion_function);
1725
1726 /* If we are attempting command completion and nothing matches, we
1727 do not want readline to perform filename completion for us. We
1728 still want to be able to complete partial pathnames, so set the
1729 completion ignore function to something which will remove
1730 filenames and leave directories in the match list. */
1731 if (matches == (char **)NULL)
1732 rl_ignore_some_completions_function = bash_ignore_filenames;
1733 else if (matches[1] == 0 && CMD_IS_DIR(matches[0]) && dot_in_path == 0)
1734 /* If we found a single match, without looking in the current
1735 directory (because it's not in $PATH), but the found name is
1736 also a command in the current directory, suppress appending any
1737 terminating character, since it's ambiguous. */
1738 {
1739 rl_completion_suppress_append = 1;
1740 rl_filename_completion_desired = 0;
1741 }
1742 else if (matches[0] && matches[1] && STREQ (matches[0], matches[1]) && CMD_IS_DIR (matches[0]))
1743 /* There are multiple instances of the same match (duplicate
1744 completions haven't yet been removed). In this case, all of
1745 the matches will be the same, and the duplicate removal code
1746 will distill them all down to one. We turn on
1747 rl_completion_suppress_append for the same reason as above.
1748 Remember: we only care if there's eventually a single unique
1749 completion. If there are multiple completions this won't
1750 make a difference and the problem won't occur. */
1751 {
1752 rl_completion_suppress_append = 1;
1753 rl_filename_completion_desired = 0;
1754 }
1755 }
1756 }
1757
1758 /* This could be a globbing pattern, so try to expand it using pathname
1759 expansion. */
1760 if (!matches && completion_glob_pattern ((char *)text))
1761 {
1762 matches = rl_completion_matches (text, glob_complete_word);
1763 /* A glob expression that matches more than one filename is problematic.
1764 If we match more than one filename, punt. */
1765 if (matches && matches[1] && rl_completion_type == TAB)
1766 {
1767 strvec_dispose (matches);
1768 matches = (char **)0;
1769 }
1770 else if (matches && matches[1] && rl_completion_type == '!')
1771 {
1772 rl_completion_suppress_append = 1;
1773 rl_filename_completion_desired = 0;
1774 }
1775 }
1776
1777 return (matches);
1778 }
1779
1780 static int
1781 bash_command_name_stat_hook (name)
1782 char **name;
1783 {
1784 char *cname, *result;
1785
1786 /* If it's not something we're going to look up in $PATH, just call the
1787 normal filename stat hook. */
1788 if (absolute_program (*name))
1789 return (bash_filename_stat_hook (name));
1790
1791 cname = *name;
1792 /* XXX - we could do something here with converting aliases, builtins,
1793 and functions into something that came out as executable, but we don't. */
1794 result = search_for_command (cname, 0);
1795 if (result)
1796 {
1797 *name = result;
1798 return 1;
1799 }
1800 return 0;
1801 }
1802
1803 static int
1804 executable_completion (filename, searching_path)
1805 const char *filename;
1806 int searching_path;
1807 {
1808 char *f;
1809 int r;
1810
1811 f = savestring (filename);
1812 bash_directory_completion_hook (&f);
1813
1814 r = searching_path ? executable_file (f) : executable_or_directory (f);
1815 free (f);
1816 return r;
1817 }
1818
1819 /* This is the function to call when the word to complete is in a position
1820 where a command word can be found. It grovels $PATH, looking for commands
1821 that match. It also scans aliases, function names, and the shell_builtin
1822 table. */
1823 char *
1824 command_word_completion_function (hint_text, state)
1825 const char *hint_text;
1826 int state;
1827 {
1828 static char *hint = (char *)NULL;
1829 static char *path = (char *)NULL;
1830 static char *val = (char *)NULL;
1831 static char *filename_hint = (char *)NULL;
1832 static char *fnhint = (char *)NULL;
1833 static char *dequoted_hint = (char *)NULL;
1834 static char *directory_part = (char *)NULL;
1835 static char **glob_matches = (char **)NULL;
1836 static int path_index, hint_len, istate, igncase;
1837 static int mapping_over, local_index, searching_path, hint_is_dir;
1838 static int old_glob_ignore_case, globpat;
1839 static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
1840 #if defined (ALIAS)
1841 static alias_t **alias_list = (alias_t **)NULL;
1842 #endif /* ALIAS */
1843 char *temp, *cval;
1844
1845 /* We have to map over the possibilities for command words. If we have
1846 no state, then make one just for that purpose. */
1847 if (state == 0)
1848 {
1849 rl_filename_stat_hook = bash_command_name_stat_hook;
1850
1851 if (dequoted_hint && dequoted_hint != hint)
1852 free (dequoted_hint);
1853 if (hint)
1854 free (hint);
1855
1856 mapping_over = searching_path = 0;
1857 hint_is_dir = CMD_IS_DIR (hint_text);
1858 val = (char *)NULL;
1859
1860 temp = rl_variable_value ("completion-ignore-case");
1861 igncase = RL_BOOLEAN_VARIABLE_VALUE (temp);
1862
1863 if (glob_matches)
1864 {
1865 free (glob_matches);
1866 glob_matches = (char **)NULL;
1867 }
1868
1869 globpat = completion_glob_pattern ((char *)hint_text);
1870
1871 /* If this is an absolute program name, do not check it against
1872 aliases, reserved words, functions or builtins. We must check
1873 whether or not it is unique, and, if so, whether that filename
1874 is executable. */
1875 if (globpat || absolute_program (hint_text))
1876 {
1877 /* Perform tilde expansion on what's passed, so we don't end up
1878 passing filenames with tildes directly to stat(). The rest of
1879 the shell doesn't do variable expansion on the word following
1880 the tilde, so we don't do it here even if direxpand is set. */
1881 if (*hint_text == '~')
1882 {
1883 hint = bash_tilde_expand (hint_text, 0);
1884 directory_part = savestring (hint_text);
1885 temp = strchr (directory_part, '/');
1886 if (temp)
1887 *temp = 0;
1888 else
1889 {
1890 free (directory_part);
1891 directory_part = (char *)NULL;
1892 }
1893 }
1894 else if (dircomplete_expand)
1895 {
1896 hint = savestring (hint_text);
1897 bash_directory_completion_hook (&hint);
1898 }
1899 else
1900 hint = savestring (hint_text);
1901
1902 dequoted_hint = hint;
1903 /* If readline's completer found a quote character somewhere, but
1904 didn't set the quote character, there must have been a quote
1905 character embedded in the filename. It can't be at the start of
1906 the filename, so we need to dequote the filename before we look
1907 in the file system for it. */
1908 if (rl_completion_found_quote && rl_completion_quote_character == 0)
1909 {
1910 dequoted_hint = bash_dequote_filename (hint, 0);
1911 free (hint);
1912 hint = dequoted_hint;
1913 }
1914 hint_len = strlen (hint);
1915
1916 if (filename_hint)
1917 free (filename_hint);
1918
1919 fnhint = filename_hint = savestring (hint);
1920
1921 istate = 0;
1922
1923 if (globpat)
1924 {
1925 mapping_over = 5;
1926 goto globword;
1927 }
1928 else
1929 {
1930 if (dircomplete_expand && path_dot_or_dotdot (filename_hint))
1931 {
1932 dircomplete_expand = 0;
1933 set_directory_hook ();
1934 dircomplete_expand = 1;
1935 }
1936 mapping_over = 4;
1937 goto inner;
1938 }
1939 }
1940
1941 dequoted_hint = hint = savestring (hint_text);
1942 hint_len = strlen (hint);
1943
1944 if (rl_completion_found_quote && rl_completion_quote_character == 0)
1945 dequoted_hint = bash_dequote_filename (hint, 0);
1946
1947 path = get_string_value ("PATH");
1948 path_index = dot_in_path = 0;
1949
1950 /* Initialize the variables for each type of command word. */
1951 local_index = 0;
1952
1953 if (varlist)
1954 free (varlist);
1955
1956 varlist = all_visible_functions ();
1957
1958 #if defined (ALIAS)
1959 if (alias_list)
1960 free (alias_list);
1961
1962 alias_list = all_aliases ();
1963 #endif /* ALIAS */
1964 }
1965
1966 /* mapping_over says what we are currently hacking. Note that every case
1967 in this list must fall through when there are no more possibilities. */
1968
1969 switch (mapping_over)
1970 {
1971 case 0: /* Aliases come first. */
1972 #if defined (ALIAS)
1973 while (alias_list && alias_list[local_index])
1974 {
1975 register char *alias;
1976
1977 alias = alias_list[local_index++]->name;
1978
1979 if (igncase == 0 && (STREQN (alias, hint, hint_len)))
1980 return (savestring (alias));
1981 else if (igncase && strncasecmp (alias, hint, hint_len) == 0)
1982 return (savestring (alias));
1983 }
1984 #endif /* ALIAS */
1985 local_index = 0;
1986 mapping_over++;
1987
1988 case 1: /* Then shell reserved words. */
1989 {
1990 while (word_token_alist[local_index].word)
1991 {
1992 register char *reserved_word;
1993
1994 reserved_word = word_token_alist[local_index++].word;
1995
1996 if (STREQN (reserved_word, hint, hint_len))
1997 return (savestring (reserved_word));
1998 }
1999 local_index = 0;
2000 mapping_over++;
2001 }
2002
2003 case 2: /* Then function names. */
2004 while (varlist && varlist[local_index])
2005 {
2006 register char *varname;
2007
2008 varname = varlist[local_index++]->name;
2009
2010 /* Honor completion-ignore-case for shell function names. */
2011 if (igncase == 0 && (STREQN (varname, hint, hint_len)))
2012 return (savestring (varname));
2013 else if (igncase && strncasecmp (varname, hint, hint_len) == 0)
2014 return (savestring (varname));
2015 }
2016 local_index = 0;
2017 mapping_over++;
2018
2019 case 3: /* Then shell builtins. */
2020 for (; local_index < num_shell_builtins; local_index++)
2021 {
2022 /* Ignore it if it doesn't have a function pointer or if it
2023 is not currently enabled. */
2024 if (!shell_builtins[local_index].function ||
2025 (shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0)
2026 continue;
2027
2028 if (STREQN (shell_builtins[local_index].name, hint, hint_len))
2029 {
2030 int i = local_index++;
2031
2032 return (savestring (shell_builtins[i].name));
2033 }
2034 }
2035 local_index = 0;
2036 mapping_over++;
2037 }
2038
2039 globword:
2040 /* Limited support for completing command words with globbing chars. Only
2041 a single match (multiple matches that end up reducing the number of
2042 characters in the common prefix are bad) will ever be returned on
2043 regular completion. */
2044 if (globpat)
2045 {
2046 if (state == 0)
2047 {
2048 glob_ignore_case = igncase;
2049 glob_matches = shell_glob_filename (hint);
2050 glob_ignore_case = old_glob_ignore_case;
2051
2052 if (GLOB_FAILED (glob_matches) || glob_matches == 0)
2053 {
2054 glob_matches = (char **)NULL;
2055 return ((char *)NULL);
2056 }
2057
2058 local_index = 0;
2059
2060 if (glob_matches[1] && rl_completion_type == TAB) /* multiple matches are bad */
2061 return ((char *)NULL);
2062 }
2063
2064 while (val = glob_matches[local_index++])
2065 {
2066 if (executable_or_directory (val))
2067 {
2068 if (*hint_text == '~' && directory_part)
2069 {
2070 temp = maybe_restore_tilde (val, directory_part);
2071 free (val);
2072 val = temp;
2073 }
2074 return (val);
2075 }
2076 free (val);
2077 }
2078
2079 glob_ignore_case = old_glob_ignore_case;
2080 return ((char *)NULL);
2081 }
2082
2083 /* If the text passed is a directory in the current directory, return it
2084 as a possible match. Executables in directories in the current
2085 directory can be specified using relative pathnames and successfully
2086 executed even when `.' is not in $PATH. */
2087 if (hint_is_dir)
2088 {
2089 hint_is_dir = 0; /* only return the hint text once */
2090 return (savestring (hint_text));
2091 }
2092
2093 /* Repeatedly call filename_completion_function while we have
2094 members of PATH left. Question: should we stat each file?
2095 Answer: we call executable_file () on each file. */
2096 outer:
2097
2098 istate = (val != (char *)NULL);
2099
2100 if (istate == 0)
2101 {
2102 char *current_path;
2103
2104 /* Get the next directory from the path. If there is none, then we
2105 are all done. */
2106 if (path == 0 || path[path_index] == 0 ||
2107 (current_path = extract_colon_unit (path, &path_index)) == 0)
2108 return ((char *)NULL);
2109
2110 searching_path = 1;
2111 if (*current_path == 0)
2112 {
2113 free (current_path);
2114 current_path = savestring (".");
2115 }
2116
2117 if (*current_path == '~')
2118 {
2119 char *t;
2120
2121 t = bash_tilde_expand (current_path, 0);
2122 free (current_path);
2123 current_path = t;
2124 }
2125
2126 if (current_path[0] == '.' && current_path[1] == '\0')
2127 dot_in_path = 1;
2128
2129 if (fnhint && fnhint != filename_hint)
2130 free (fnhint);
2131 if (filename_hint)
2132 free (filename_hint);
2133
2134 filename_hint = sh_makepath (current_path, hint, 0);
2135 /* Need a quoted version (though it doesn't matter much in most
2136 cases) because rl_filename_completion_function dequotes the
2137 filename it gets, assuming that it's been quoted as part of
2138 the input line buffer. */
2139 if (strpbrk (filename_hint, "\"'\\"))
2140 fnhint = sh_backslash_quote (filename_hint, filename_bstab, 0);
2141 else
2142 fnhint = filename_hint;
2143 free (current_path); /* XXX */
2144 }
2145
2146 inner:
2147 val = rl_filename_completion_function (fnhint, istate);
2148 if (mapping_over == 4 && dircomplete_expand)
2149 set_directory_hook ();
2150
2151 istate = 1;
2152
2153 if (val == 0)
2154 {
2155 /* If the hint text is an absolute program, then don't bother
2156 searching through PATH. */
2157 if (absolute_program (hint))
2158 return ((char *)NULL);
2159
2160 goto outer;
2161 }
2162 else
2163 {
2164 int match, freetemp;
2165
2166 if (absolute_program (hint))
2167 {
2168 if (igncase == 0)
2169 match = strncmp (val, hint, hint_len) == 0;
2170 else
2171 match = strncasecmp (val, hint, hint_len) == 0;
2172
2173 /* If we performed tilde expansion, restore the original
2174 filename. */
2175 if (*hint_text == '~')
2176 temp = maybe_restore_tilde (val, directory_part);
2177 else
2178 temp = savestring (val);
2179 freetemp = 1;
2180 }
2181 else
2182 {
2183 temp = strrchr (val, '/');
2184
2185 if (temp)
2186 {
2187 temp++;
2188 if (igncase == 0)
2189 freetemp = match = strncmp (temp, hint, hint_len) == 0;
2190 else
2191 freetemp = match = strncasecmp (temp, hint, hint_len) == 0;
2192 if (match)
2193 temp = savestring (temp);
2194 }
2195 else
2196 freetemp = match = 0;
2197 }
2198
2199 /* If we have found a match, and it is an executable file, return it.
2200 We don't return directory names when searching $PATH, since the
2201 bash execution code won't find executables in directories which
2202 appear in directories in $PATH when they're specified using
2203 relative pathnames. */
2204 #if 0
2205 /* If we're not searching $PATH and we have a relative pathname, we
2206 need to re-canonicalize it before testing whether or not it's an
2207 executable or a directory so the shell treats .. relative to $PWD
2208 according to the physical/logical option. The shell already
2209 canonicalizes the directory name in order to tell readline where
2210 to look, so not doing it here will be inconsistent. */
2211 /* XXX -- currently not used -- will introduce more inconsistency,
2212 since shell does not canonicalize ../foo before passing it to
2213 shell_execve(). */
2214 if (match && searching_path == 0 && *val == '.')
2215 {
2216 char *t, *t1;
2217
2218 t = get_working_directory ("command-word-completion");
2219 t1 = make_absolute (val, t);
2220 free (t);
2221 cval = sh_canonpath (t1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
2222 }
2223 else
2224 #endif
2225 cval = val;
2226
2227 if (match && executable_completion ((searching_path ? val : cval), searching_path))
2228 {
2229 if (cval != val)
2230 free (cval);
2231 free (val);
2232 val = ""; /* So it won't be NULL. */
2233 return (temp);
2234 }
2235 else
2236 {
2237 if (freetemp)
2238 free (temp);
2239 if (cval != val)
2240 free (cval);
2241 free (val);
2242 goto inner;
2243 }
2244 }
2245 }
2246
2247 /* Completion inside an unterminated command substitution. */
2248 static char *
2249 command_subst_completion_function (text, state)
2250 const char *text;
2251 int state;
2252 {
2253 static char **matches = (char **)NULL;
2254 static const char *orig_start;
2255 static char *filename_text = (char *)NULL;
2256 static int cmd_index, start_len;
2257 char *value;
2258
2259 if (state == 0)
2260 {
2261 if (filename_text)
2262 free (filename_text);
2263 orig_start = text;
2264 if (*text == '`')
2265 text++;
2266 else if (*text == '$' && text[1] == '(') /* ) */
2267 text += 2;
2268 /* If the text was quoted, suppress any quote character that the
2269 readline completion code would insert. */
2270 rl_completion_suppress_quote = 1;
2271 start_len = text - orig_start;
2272 filename_text = savestring (text);
2273 if (matches)
2274 free (matches);
2275
2276 /*
2277 * At this point we can entertain the idea of re-parsing
2278 * `filename_text' into a (possibly incomplete) command name and
2279 * arguments, and doing completion based on that. This is
2280 * currently very rudimentary, but it is a small improvement.
2281 */
2282 for (value = filename_text + strlen (filename_text) - 1; value > filename_text; value--)
2283 if (whitespace (*value) || member (*value, COMMAND_SEPARATORS))
2284 break;
2285 if (value <= filename_text)
2286 matches = rl_completion_matches (filename_text, command_word_completion_function);
2287 else
2288 {
2289 value++;
2290 start_len += value - filename_text;
2291 if (whitespace (value[-1]))
2292 matches = rl_completion_matches (value, rl_filename_completion_function);
2293 else
2294 matches = rl_completion_matches (value, command_word_completion_function);
2295 }
2296
2297 /* If there is more than one match, rl_completion_matches has already
2298 put the lcd in matches[0]. Skip over it. */
2299 cmd_index = matches && matches[0] && matches[1];
2300
2301 /* If there's a single match and it's a directory, set the append char
2302 to the expected `/'. Otherwise, don't append anything. */
2303 if (matches && matches[0] && matches[1] == 0 && test_for_directory (matches[0]))
2304 rl_completion_append_character = '/';
2305 else
2306 rl_completion_suppress_append = 1;
2307 }
2308
2309 if (matches == 0 || matches[cmd_index] == 0)
2310 {
2311 rl_filename_quoting_desired = 0; /* disable quoting */
2312 return ((char *)NULL);
2313 }
2314 else
2315 {
2316 value = (char *)xmalloc (1 + start_len + strlen (matches[cmd_index]));
2317
2318 if (start_len == 1)
2319 value[0] = *orig_start;
2320 else
2321 strncpy (value, orig_start, start_len);
2322
2323 strcpy (value + start_len, matches[cmd_index]);
2324
2325 cmd_index++;
2326 return (value);
2327 }
2328 }
2329
2330 /* Okay, now we write the entry_function for variable completion. */
2331 static char *
2332 variable_completion_function (text, state)
2333 const char *text;
2334 int state;
2335 {
2336 static char **varlist = (char **)NULL;
2337 static int varlist_index;
2338 static char *varname = (char *)NULL;
2339 static int first_char, first_char_loc;
2340
2341 if (!state)
2342 {
2343 if (varname)
2344 free (varname);
2345
2346 first_char_loc = 0;
2347 first_char = text[0];
2348
2349 if (first_char == '$')
2350 first_char_loc++;
2351
2352 if (text[first_char_loc] == '{')
2353 first_char_loc++;
2354
2355 varname = savestring (text + first_char_loc);
2356
2357 if (varlist)
2358 strvec_dispose (varlist);
2359
2360 varlist = all_variables_matching_prefix (varname);
2361 varlist_index = 0;
2362 }
2363
2364 if (!varlist || !varlist[varlist_index])
2365 {
2366 return ((char *)NULL);
2367 }
2368 else
2369 {
2370 char *value;
2371
2372 value = (char *)xmalloc (4 + strlen (varlist[varlist_index]));
2373
2374 if (first_char_loc)
2375 {
2376 value[0] = first_char;
2377 if (first_char_loc == 2)
2378 value[1] = '{';
2379 }
2380
2381 strcpy (value + first_char_loc, varlist[varlist_index]);
2382 if (first_char_loc == 2)
2383 strcat (value, "}");
2384
2385 varlist_index++;
2386 return (value);
2387 }
2388 }
2389
2390 /* How about a completion function for hostnames? */
2391 static char *
2392 hostname_completion_function (text, state)
2393 const char *text;
2394 int state;
2395 {
2396 static char **list = (char **)NULL;
2397 static int list_index = 0;
2398 static int first_char, first_char_loc;
2399
2400 /* If we don't have any state, make some. */
2401 if (state == 0)
2402 {
2403 FREE (list);
2404
2405 list = (char **)NULL;
2406
2407 first_char_loc = 0;
2408 first_char = *text;
2409
2410 if (first_char == '@')
2411 first_char_loc++;
2412
2413 list = hostnames_matching ((char *)text+first_char_loc);
2414 list_index = 0;
2415 }
2416
2417 if (list && list[list_index])
2418 {
2419 char *t;
2420
2421 t = (char *)xmalloc (2 + strlen (list[list_index]));
2422 *t = first_char;
2423 strcpy (t + first_char_loc, list[list_index]);
2424 list_index++;
2425 return (t);
2426 }
2427
2428 return ((char *)NULL);
2429 }
2430
2431 /*
2432 * A completion function for service names from /etc/services (or wherever).
2433 */
2434 char *
2435 bash_servicename_completion_function (text, state)
2436 const char *text;
2437 int state;
2438 {
2439 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GETSERVENT)
2440 return ((char *)NULL);
2441 #else
2442 static char *sname = (char *)NULL;
2443 static struct servent *srvent;
2444 static int snamelen;
2445 char *value;
2446 char **alist, *aentry;
2447 int afound;
2448
2449 if (state == 0)
2450 {
2451 FREE (sname);
2452
2453 sname = savestring (text);
2454 snamelen = strlen (sname);
2455 setservent (0);
2456 }
2457
2458 while (srvent = getservent ())
2459 {
2460 afound = 0;
2461 if (snamelen == 0 || (STREQN (sname, srvent->s_name, snamelen)))
2462 break;
2463 /* Not primary, check aliases */
2464 for (alist = srvent->s_aliases; *alist; alist++)
2465 {
2466 aentry = *alist;
2467 if (STREQN (sname, aentry, snamelen))
2468 {
2469 afound = 1;
2470 break;
2471 }
2472 }
2473
2474 if (afound)
2475 break;
2476 }
2477
2478 if (srvent == 0)
2479 {
2480 endservent ();
2481 return ((char *)NULL);
2482 }
2483
2484 value = afound ? savestring (aentry) : savestring (srvent->s_name);
2485 return value;
2486 #endif
2487 }
2488
2489 /*
2490 * A completion function for group names from /etc/group (or wherever).
2491 */
2492 char *
2493 bash_groupname_completion_function (text, state)
2494 const char *text;
2495 int state;
2496 {
2497 #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H)
2498 return ((char *)NULL);
2499 #else
2500 static char *gname = (char *)NULL;
2501 static struct group *grent;
2502 static int gnamelen;
2503 char *value;
2504
2505 if (state == 0)
2506 {
2507 FREE (gname);
2508 gname = savestring (text);
2509 gnamelen = strlen (gname);
2510
2511 setgrent ();
2512 }
2513
2514 while (grent = getgrent ())
2515 {
2516 if (gnamelen == 0 || (STREQN (gname, grent->gr_name, gnamelen)))
2517 break;
2518 }
2519
2520 if (grent == 0)
2521 {
2522 endgrent ();
2523 return ((char *)NULL);
2524 }
2525
2526 value = savestring (grent->gr_name);
2527 return (value);
2528 #endif
2529 }
2530
2531 /* Functions to perform history and alias expansions on the current line. */
2532
2533 #if defined (BANG_HISTORY)
2534 /* Perform history expansion on the current line. If no history expansion
2535 is done, pre_process_line() returns what it was passed, so we need to
2536 allocate a new line here. */
2537 static char *
2538 history_expand_line_internal (line)
2539 char *line;
2540 {
2541 char *new_line;
2542 int old_verify;
2543
2544 old_verify = hist_verify;
2545 hist_verify = 0;
2546 new_line = pre_process_line (line, 0, 0);
2547 hist_verify = old_verify;
2548
2549 return (new_line == line) ? savestring (line) : new_line;
2550 }
2551 #endif
2552
2553 /* There was an error in expansion. Let the preprocessor print
2554 the error here. */
2555 static void
2556 cleanup_expansion_error ()
2557 {
2558 char *to_free;
2559 #if defined (BANG_HISTORY)
2560 int old_verify;
2561
2562 old_verify = hist_verify;
2563 hist_verify = 0;
2564 #endif
2565
2566 fprintf (rl_outstream, "\r\n");
2567 to_free = pre_process_line (rl_line_buffer, 1, 0);
2568 #if defined (BANG_HISTORY)
2569 hist_verify = old_verify;
2570 #endif
2571 if (to_free != rl_line_buffer)
2572 FREE (to_free);
2573 putc ('\r', rl_outstream);
2574 rl_forced_update_display ();
2575 }
2576
2577 /* If NEW_LINE differs from what is in the readline line buffer, add an
2578 undo record to get from the readline line buffer contents to the new
2579 line and make NEW_LINE the current readline line. */
2580 static void
2581 maybe_make_readline_line (new_line)
2582 char *new_line;
2583 {
2584 if (new_line && strcmp (new_line, rl_line_buffer) != 0)
2585 {
2586 rl_point = rl_end;
2587
2588 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2589 rl_delete_text (0, rl_point);
2590 rl_point = rl_end = rl_mark = 0;
2591 rl_insert_text (new_line);
2592 rl_add_undo (UNDO_END, 0, 0, 0);
2593 }
2594 }
2595
2596 /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */
2597 static void
2598 set_up_new_line (new_line)
2599 char *new_line;
2600 {
2601 int old_point, at_end;
2602
2603 old_point = rl_point;
2604 at_end = rl_point == rl_end;
2605
2606 /* If the line was history and alias expanded, then make that
2607 be one thing to undo. */
2608 maybe_make_readline_line (new_line);
2609 free (new_line);
2610
2611 /* Place rl_point where we think it should go. */
2612 if (at_end)
2613 rl_point = rl_end;
2614 else if (old_point < rl_end)
2615 {
2616 rl_point = old_point;
2617 if (!whitespace (rl_line_buffer[rl_point]))
2618 rl_forward_word (1, 0);
2619 }
2620 }
2621
2622 #if defined (ALIAS)
2623 /* Expand aliases in the current readline line. */
2624 static int
2625 alias_expand_line (count, ignore)
2626 int count, ignore;
2627 {
2628 char *new_line;
2629
2630 new_line = alias_expand (rl_line_buffer);
2631
2632 if (new_line)
2633 {
2634 set_up_new_line (new_line);
2635 return (0);
2636 }
2637 else
2638 {
2639 cleanup_expansion_error ();
2640 return (1);
2641 }
2642 }
2643 #endif
2644
2645 #if defined (BANG_HISTORY)
2646 /* History expand the line. */
2647 static int
2648 history_expand_line (count, ignore)
2649 int count, ignore;
2650 {
2651 char *new_line;
2652
2653 new_line = history_expand_line_internal (rl_line_buffer);
2654
2655 if (new_line)
2656 {
2657 set_up_new_line (new_line);
2658 return (0);
2659 }
2660 else
2661 {
2662 cleanup_expansion_error ();
2663 return (1);
2664 }
2665 }
2666
2667 /* Expand history substitutions in the current line and then insert a
2668 space (hopefully close to where we were before). */
2669 static int
2670 tcsh_magic_space (count, ignore)
2671 int count, ignore;
2672 {
2673 int dist_from_end, old_point;
2674
2675 old_point = rl_point;
2676 dist_from_end = rl_end - rl_point;
2677 if (history_expand_line (count, ignore) == 0)
2678 {
2679 /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>.
2680 This works if all expansions were before rl_point or if no expansions
2681 were performed. */
2682 rl_point = (old_point == 0) ? old_point : rl_end - dist_from_end;
2683 rl_insert (1, ' ');
2684 return (0);
2685 }
2686 else
2687 return (1);
2688 }
2689 #endif /* BANG_HISTORY */
2690
2691 /* History and alias expand the line. */
2692 static int
2693 history_and_alias_expand_line (count, ignore)
2694 int count, ignore;
2695 {
2696 char *new_line;
2697
2698 new_line = 0;
2699 #if defined (BANG_HISTORY)
2700 new_line = history_expand_line_internal (rl_line_buffer);
2701 #endif
2702
2703 #if defined (ALIAS)
2704 if (new_line)
2705 {
2706 char *alias_line;
2707
2708 alias_line = alias_expand (new_line);
2709 free (new_line);
2710 new_line = alias_line;
2711 }
2712 #endif /* ALIAS */
2713
2714 if (new_line)
2715 {
2716 set_up_new_line (new_line);
2717 return (0);
2718 }
2719 else
2720 {
2721 cleanup_expansion_error ();
2722 return (1);
2723 }
2724 }
2725
2726 /* History and alias expand the line, then perform the shell word
2727 expansions by calling expand_string. This can't use set_up_new_line()
2728 because we want the variable expansions as a separate undo'able
2729 set of operations. */
2730 static int
2731 shell_expand_line (count, ignore)
2732 int count, ignore;
2733 {
2734 char *new_line;
2735 WORD_LIST *expanded_string;
2736 WORD_DESC *w;
2737
2738 new_line = 0;
2739 #if defined (BANG_HISTORY)
2740 new_line = history_expand_line_internal (rl_line_buffer);
2741 #endif
2742
2743 #if defined (ALIAS)
2744 if (new_line)
2745 {
2746 char *alias_line;
2747
2748 alias_line = alias_expand (new_line);
2749 free (new_line);
2750 new_line = alias_line;
2751 }
2752 #endif /* ALIAS */
2753
2754 if (new_line)
2755 {
2756 int old_point = rl_point;
2757 int at_end = rl_point == rl_end;
2758
2759 /* If the line was history and alias expanded, then make that
2760 be one thing to undo. */
2761 maybe_make_readline_line (new_line);
2762 free (new_line);
2763
2764 /* If there is variable expansion to perform, do that as a separate
2765 operation to be undone. */
2766
2767 #if 1
2768 w = alloc_word_desc ();
2769 w->word = savestring (rl_line_buffer);
2770 w->flags = rl_explicit_arg ? (W_NOPROCSUB|W_NOCOMSUB) : 0;
2771 expanded_string = expand_word (w, rl_explicit_arg ? Q_HERE_DOCUMENT : 0);
2772 dispose_word (w);
2773 #else
2774 new_line = savestring (rl_line_buffer);
2775 expanded_string = expand_string (new_line, 0);
2776 FREE (new_line);
2777 #endif
2778
2779 if (expanded_string == 0)
2780 {
2781 new_line = (char *)xmalloc (1);
2782 new_line[0] = '\0';
2783 }
2784 else
2785 {
2786 new_line = string_list (expanded_string);
2787 dispose_words (expanded_string);
2788 }
2789
2790 maybe_make_readline_line (new_line);
2791 free (new_line);
2792
2793 /* Place rl_point where we think it should go. */
2794 if (at_end)
2795 rl_point = rl_end;
2796 else if (old_point < rl_end)
2797 {
2798 rl_point = old_point;
2799 if (!whitespace (rl_line_buffer[rl_point]))
2800 rl_forward_word (1, 0);
2801 }
2802 return 0;
2803 }
2804 else
2805 {
2806 cleanup_expansion_error ();
2807 return 1;
2808 }
2809 }
2810
2811 /* If FIGNORE is set, then don't match files with the given suffixes when
2812 completing filenames. If only one of the possibilities has an acceptable
2813 suffix, delete the others, else just return and let the completer
2814 signal an error. It is called by the completer when real
2815 completions are done on filenames by the completer's internal
2816 function, not for completion lists (M-?) and not on "other"
2817 completion types, such as hostnames or commands. */
2818
2819 static struct ignorevar fignore =
2820 {
2821 "FIGNORE",
2822 (struct ign *)0,
2823 0,
2824 (char *)0,
2825 (sh_iv_item_func_t *) 0,
2826 };
2827
2828 static void
2829 _ignore_completion_names (names, name_func)
2830 char **names;
2831 sh_ignore_func_t *name_func;
2832 {
2833 char **newnames;
2834 int idx, nidx;
2835 char **oldnames;
2836 int oidx;
2837
2838 /* If there is only one completion, see if it is acceptable. If it is
2839 not, free it up. In any case, short-circuit and return. This is a
2840 special case because names[0] is not the prefix of the list of names
2841 if there is only one completion; it is the completion itself. */
2842 if (names[1] == (char *)0)
2843 {
2844 if (force_fignore)
2845 if ((*name_func) (names[0]) == 0)
2846 {
2847 free (names[0]);
2848 names[0] = (char *)NULL;
2849 }
2850
2851 return;
2852 }
2853
2854 /* Allocate space for array to hold list of pointers to matching
2855 filenames. The pointers are copied back to NAMES when done. */
2856 for (nidx = 1; names[nidx]; nidx++)
2857 ;
2858 newnames = strvec_create (nidx + 1);
2859
2860 if (force_fignore == 0)
2861 {
2862 oldnames = strvec_create (nidx - 1);
2863 oidx = 0;
2864 }
2865
2866 newnames[0] = names[0];
2867 for (idx = nidx = 1; names[idx]; idx++)
2868 {
2869 if ((*name_func) (names[idx]))
2870 newnames[nidx++] = names[idx];
2871 else if (force_fignore == 0)
2872 oldnames[oidx++] = names[idx];
2873 else
2874 free (names[idx]);
2875 }
2876
2877 newnames[nidx] = (char *)NULL;
2878
2879 /* If none are acceptable then let the completer handle it. */
2880 if (nidx == 1)
2881 {
2882 if (force_fignore)
2883 {
2884 free (names[0]);
2885 names[0] = (char *)NULL;
2886 }
2887 else
2888 free (oldnames);
2889
2890 free (newnames);
2891 return;
2892 }
2893
2894 if (force_fignore == 0)
2895 {
2896 while (oidx)
2897 free (oldnames[--oidx]);
2898 free (oldnames);
2899 }
2900
2901 /* If only one is acceptable, copy it to names[0] and return. */
2902 if (nidx == 2)
2903 {
2904 free (names[0]);
2905 names[0] = newnames[1];
2906 names[1] = (char *)NULL;
2907 free (newnames);
2908 return;
2909 }
2910
2911 /* Copy the acceptable names back to NAMES, set the new array end,
2912 and return. */
2913 for (nidx = 1; newnames[nidx]; nidx++)
2914 names[nidx] = newnames[nidx];
2915 names[nidx] = (char *)NULL;
2916 free (newnames);
2917 }
2918
2919 static int
2920 name_is_acceptable (name)
2921 const char *name;
2922 {
2923 struct ign *p;
2924 int nlen;
2925
2926 for (nlen = strlen (name), p = fignore.ignores; p->val; p++)
2927 {
2928 if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len]))
2929 return (0);
2930 }
2931
2932 return (1);
2933 }
2934
2935 #if 0
2936 static int
2937 ignore_dot_names (name)
2938 char *name;
2939 {
2940 return (name[0] != '.');
2941 }
2942 #endif
2943
2944 static int
2945 filename_completion_ignore (names)
2946 char **names;
2947 {
2948 #if 0
2949 if (glob_dot_filenames == 0)
2950 _ignore_completion_names (names, ignore_dot_names);
2951 #endif
2952
2953 setup_ignore_patterns (&fignore);
2954
2955 if (fignore.num_ignores == 0)
2956 return 0;
2957
2958 _ignore_completion_names (names, name_is_acceptable);
2959
2960 return 0;
2961 }
2962
2963 /* Return 1 if NAME is a directory. NAME undergoes tilde expansion. */
2964 static int
2965 test_for_directory (name)
2966 const char *name;
2967 {
2968 char *fn;
2969 int r;
2970
2971 fn = bash_tilde_expand (name, 0);
2972 r = file_isdir (fn);
2973 free (fn);
2974
2975 return (r);
2976 }
2977
2978 static int
2979 test_for_canon_directory (name)
2980 const char *name;
2981 {
2982 char *fn;
2983 int r;
2984
2985 fn = (*name == '~') ? bash_tilde_expand (name, 0) : savestring (name);
2986 bash_filename_stat_hook (&fn);
2987 r = file_isdir (fn);
2988 free (fn);
2989
2990 return (r);
2991 }
2992
2993 /* Remove files from NAMES, leaving directories. */
2994 static int
2995 bash_ignore_filenames (names)
2996 char **names;
2997 {
2998 _ignore_completion_names (names, test_for_directory);
2999 return 0;
3000 }
3001
3002 static int
3003 bash_progcomp_ignore_filenames (names)
3004 char **names;
3005 {
3006 _ignore_completion_names (names, test_for_canon_directory);
3007 return 0;
3008 }
3009
3010 static int
3011 return_zero (name)
3012 const char *name;
3013 {
3014 return 0;
3015 }
3016
3017 static int
3018 bash_ignore_everything (names)
3019 char **names;
3020 {
3021 _ignore_completion_names (names, return_zero);
3022 return 0;
3023 }
3024
3025 /* Replace a tilde-prefix in VAL with a `~', assuming the user typed it. VAL
3026 is an expanded filename. DIRECTORY_PART is the tilde-prefix portion
3027 of the un-tilde-expanded version of VAL (what the user typed). */
3028 static char *
3029 restore_tilde (val, directory_part)
3030 char *val, *directory_part;
3031 {
3032 int l, vl, dl2, xl;
3033 char *dh2, *expdir, *ret, *v;
3034
3035 vl = strlen (val);
3036
3037 /* We need to duplicate the expansions readline performs on the directory
3038 portion before passing it to our completion function. */
3039 dh2 = directory_part ? bash_dequote_filename (directory_part, 0) : 0;
3040 bash_directory_expansion (&dh2);
3041 dl2 = strlen (dh2);
3042
3043 expdir = bash_tilde_expand (directory_part, 0);
3044 xl = strlen (expdir);
3045 if (*directory_part == '~' && STREQ (directory_part, expdir))
3046 {
3047 /* tilde expansion failed, so what should we return? we use what the
3048 user typed. */
3049 v = mbschr (val, '/');
3050 vl = STRLEN (v);
3051 ret = (char *)xmalloc (xl + vl + 2);
3052 strcpy (ret, directory_part);
3053 if (v && *v)
3054 strcpy (ret + xl, v);
3055
3056 free (dh2);
3057 free (expdir);
3058
3059 return ret;
3060 }
3061 free (expdir);
3062
3063 /*
3064 dh2 = unexpanded but dequoted tilde-prefix
3065 dl2 = length of tilde-prefix
3066 expdir = tilde-expanded tilde-prefix
3067 xl = length of expanded tilde-prefix
3068 l = length of remainder after tilde-prefix
3069 */
3070 l = (vl - xl) + 1;
3071 if (l <= 0)
3072 {
3073 free (dh2);
3074 return (savestring (val)); /* XXX - just punt */
3075 }
3076
3077 ret = (char *)xmalloc (dl2 + 2 + l);
3078 strcpy (ret, dh2);
3079 strcpy (ret + dl2, val + xl);
3080
3081 free (dh2);
3082 return (ret);
3083 }
3084
3085 static char *
3086 maybe_restore_tilde (val, directory_part)
3087 char *val, *directory_part;
3088 {
3089 rl_icppfunc_t *save;
3090 char *ret;
3091
3092 save = (dircomplete_expand == 0) ? save_directory_hook () : (rl_icppfunc_t *)0;
3093 ret = restore_tilde (val, directory_part);
3094 if (save)
3095 restore_directory_hook (save);
3096 return ret;
3097 }
3098
3099 /* Simulate the expansions that will be performed by
3100 rl_filename_completion_function. This must be called with the address of
3101 a pointer to malloc'd memory. */
3102 static void
3103 bash_directory_expansion (dirname)
3104 char **dirname;
3105 {
3106 char *d, *nd;
3107
3108 d = savestring (*dirname);
3109
3110 if ((rl_directory_rewrite_hook) && (*rl_directory_rewrite_hook) (&d))
3111 {
3112 free (*dirname);
3113 *dirname = d;
3114 }
3115 else if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&d))
3116 {
3117 free (*dirname);
3118 *dirname = d;
3119 }
3120 else if (rl_completion_found_quote)
3121 {
3122 nd = bash_dequote_filename (d, rl_completion_quote_character);
3123 free (*dirname);
3124 free (d);
3125 *dirname = nd;
3126 }
3127 }
3128
3129 /* If necessary, rewrite directory entry */
3130 static char *
3131 bash_filename_rewrite_hook (fname, fnlen)
3132 char *fname;
3133 int fnlen;
3134 {
3135 char *conv;
3136
3137 conv = fnx_fromfs (fname, fnlen);
3138 if (conv != fname)
3139 conv = savestring (conv);
3140 return conv;
3141 }
3142
3143 /* Functions to save and restore the appropriate directory hook */
3144 /* This is not static so the shopt code can call it */
3145 void
3146 set_directory_hook ()
3147 {
3148 if (dircomplete_expand)
3149 {
3150 rl_directory_completion_hook = bash_directory_completion_hook;
3151 rl_directory_rewrite_hook = (rl_icppfunc_t *)0;
3152 }
3153 else
3154 {
3155 rl_directory_rewrite_hook = bash_directory_completion_hook;
3156 rl_directory_completion_hook = (rl_icppfunc_t *)0;
3157 }
3158 }
3159
3160 static rl_icppfunc_t *
3161 save_directory_hook ()
3162 {
3163 rl_icppfunc_t *ret;
3164
3165 if (dircomplete_expand)
3166 {
3167 ret = rl_directory_completion_hook;
3168 rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
3169 }
3170 else
3171 {
3172 ret = rl_directory_rewrite_hook;
3173 rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
3174 }
3175
3176 return ret;
3177 }
3178
3179 static void
3180 restore_directory_hook (hookf)
3181 rl_icppfunc_t *hookf;
3182 {
3183 if (dircomplete_expand)
3184 rl_directory_completion_hook = hookf;
3185 else
3186 rl_directory_rewrite_hook = hookf;
3187 }
3188
3189 /* Check whether not DIRNAME, with any trailing slash removed, exists. If
3190 SHOULD_DEQUOTE is non-zero, we dequote the directory name first. */
3191 static int
3192 directory_exists (dirname, should_dequote)
3193 const char *dirname;
3194 int should_dequote;
3195 {
3196 char *new_dirname;
3197 int dirlen, r;
3198 struct stat sb;
3199
3200 /* We save the string and chop the trailing slash because stat/lstat behave
3201 inconsistently if one is present. */
3202 new_dirname = should_dequote ? bash_dequote_filename ((char *)dirname, rl_completion_quote_character) : savestring (dirname);
3203 dirlen = STRLEN (new_dirname);
3204 if (new_dirname[dirlen - 1] == '/')
3205 new_dirname[dirlen - 1] = '\0';
3206 #if defined (HAVE_LSTAT)
3207 r = lstat (new_dirname, &sb) == 0;
3208 #else
3209 r = stat (new_dirname, &sb) == 0;
3210 #endif
3211 free (new_dirname);
3212 return (r);
3213 }
3214
3215 /* Expand a filename before the readline completion code passes it to stat(2).
3216 The filename will already have had tilde expansion performed. */
3217 static int
3218 bash_filename_stat_hook (dirname)
3219 char **dirname;
3220 {
3221 char *local_dirname, *new_dirname, *t;
3222 int should_expand_dirname, return_value;
3223 int global_nounset;
3224 WORD_LIST *wl;
3225
3226 local_dirname = *dirname;
3227 should_expand_dirname = return_value = 0;
3228 if (t = mbschr (local_dirname, '$'))
3229 should_expand_dirname = '$';
3230 else if (t = mbschr (local_dirname, '`')) /* XXX */
3231 should_expand_dirname = '`';
3232
3233 if (should_expand_dirname && directory_exists (local_dirname, 0))
3234 should_expand_dirname = 0;
3235
3236 if (should_expand_dirname)
3237 {
3238 new_dirname = savestring (local_dirname);
3239 /* no error messages, and expand_prompt_string doesn't longjmp so we don't
3240 have to worry about restoring this setting. */
3241 global_nounset = unbound_vars_is_error;
3242 unbound_vars_is_error = 0;
3243 wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB|W_NOPROCSUB|W_COMPLETE); /* does the right thing */
3244 unbound_vars_is_error = global_nounset;
3245 if (wl)
3246 {
3247 free (new_dirname);
3248 new_dirname = string_list (wl);
3249 /* Tell the completer we actually expanded something and change
3250 *dirname only if we expanded to something non-null -- stat
3251 behaves unpredictably when passed null or empty strings */
3252 if (new_dirname && *new_dirname)
3253 {
3254 free (local_dirname); /* XXX */
3255 local_dirname = *dirname = new_dirname;
3256 return_value = STREQ (local_dirname, *dirname) == 0;
3257 }
3258 else
3259 free (new_dirname);
3260 dispose_words (wl);
3261 }
3262 else
3263 free (new_dirname);
3264 }
3265
3266 /* This is very similar to the code in bash_directory_completion_hook below,
3267 but without spelling correction and not worrying about whether or not
3268 we change relative pathnames. */
3269 if (no_symbolic_links == 0 && (local_dirname[0] != '.' || local_dirname[1]))
3270 {
3271 char *temp1, *temp2;
3272
3273 t = get_working_directory ("symlink-hook");
3274 temp1 = make_absolute (local_dirname, t);
3275 free (t);
3276 temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
3277
3278 /* If we can't canonicalize, bail. */
3279 if (temp2 == 0)
3280 {
3281 free (temp1);
3282 return return_value;
3283 }
3284
3285 free (local_dirname);
3286 *dirname = temp2;
3287 free (temp1);
3288 }
3289
3290 return (return_value);
3291 }
3292
3293 /* Handle symbolic link references and other directory name
3294 expansions while hacking completion. This should return 1 if it modifies
3295 the DIRNAME argument, 0 otherwise. It should make sure not to modify
3296 DIRNAME if it returns 0. */
3297 static int
3298 bash_directory_completion_hook (dirname)
3299 char **dirname;
3300 {
3301 char *local_dirname, *new_dirname, *t;
3302 int return_value, should_expand_dirname, nextch, closer;
3303 WORD_LIST *wl;
3304
3305 return_value = should_expand_dirname = nextch = closer = 0;
3306 local_dirname = *dirname;
3307
3308 if (t = mbschr (local_dirname, '$'))
3309 {
3310 should_expand_dirname = '$';
3311 nextch = t[1];
3312 /* Deliberately does not handle the deprecated $[...] arithmetic
3313 expansion syntax */
3314 if (nextch == '(')
3315 closer = ')';
3316 else if (nextch == '{')
3317 closer = '}';
3318 else
3319 nextch = 0;
3320
3321 if (closer)
3322 {
3323 int p;
3324 char delims[2];
3325
3326 delims[0] = closer; delims[1] = 0;
3327 p = skip_to_delim (t, 1, delims, SD_NOJMP|SD_COMPLETE);
3328 if (t[p] != closer)
3329 should_expand_dirname = 0;
3330 }
3331 }
3332 else if (local_dirname[0] == '~')
3333 should_expand_dirname = '~';
3334 else
3335 {
3336 t = mbschr (local_dirname, '`');
3337 if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0)
3338 should_expand_dirname = '`';
3339 }
3340
3341 if (should_expand_dirname && directory_exists (local_dirname, 1))
3342 should_expand_dirname = 0;
3343
3344 if (should_expand_dirname)
3345 {
3346 new_dirname = savestring (local_dirname);
3347 wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB|W_NOPROCSUB|W_COMPLETE); /* does the right thing */
3348 if (wl)
3349 {
3350 *dirname = string_list (wl);
3351 /* Tell the completer to replace the directory name only if we
3352 actually expanded something. */
3353 return_value = STREQ (local_dirname, *dirname) == 0;
3354 free (local_dirname);
3355 free (new_dirname);
3356 dispose_words (wl);
3357 local_dirname = *dirname;
3358 /* XXX - change rl_filename_quote_characters here based on
3359 should_expand_dirname/nextch/closer. This is the only place
3360 custom_filename_quote_characters is modified. */
3361 if (rl_filename_quote_characters && *rl_filename_quote_characters)
3362 {
3363 int i, j, c;
3364 i = strlen (default_filename_quote_characters);
3365 custom_filename_quote_characters = xrealloc (custom_filename_quote_characters, i+1);
3366 for (i = j = 0; c = default_filename_quote_characters[i]; i++)
3367 {
3368 if (c == should_expand_dirname || c == nextch || c == closer)
3369 continue;
3370 custom_filename_quote_characters[j++] = c;
3371 }
3372 custom_filename_quote_characters[j] = '\0';
3373 rl_filename_quote_characters = custom_filename_quote_characters;
3374 set_filename_bstab (rl_filename_quote_characters);
3375 }
3376 }
3377 else
3378 {
3379 free (new_dirname);
3380 free (local_dirname);
3381 *dirname = (char *)xmalloc (1);
3382 **dirname = '\0';
3383 return 1;
3384 }
3385 }
3386 else
3387 {
3388 /* Dequote the filename even if we don't expand it. */
3389 new_dirname = bash_dequote_filename (local_dirname, rl_completion_quote_character);
3390 return_value = STREQ (local_dirname, new_dirname) == 0;
3391 free (local_dirname);
3392 local_dirname = *dirname = new_dirname;
3393 }
3394
3395 /* no_symbolic_links == 0 -> use (default) logical view of the file system.
3396 local_dirname[0] == '.' && local_dirname[1] == '/' means files in the
3397 current directory (./).
3398 local_dirname[0] == '.' && local_dirname[1] == 0 means relative pathnames
3399 in the current directory (e.g., lib/sh).
3400 XXX - should we do spelling correction on these? */
3401
3402 /* This is test as it was in bash-4.2: skip relative pathnames in current
3403 directory. Change test to
3404 (local_dirname[0] != '.' || (local_dirname[1] && local_dirname[1] != '/'))
3405 if we want to skip paths beginning with ./ also. */
3406 if (no_symbolic_links == 0 && (local_dirname[0] != '.' || local_dirname[1]))
3407 {
3408 char *temp1, *temp2;
3409 int len1, len2;
3410
3411 /* If we have a relative path
3412 (local_dirname[0] != '/' && local_dirname[0] != '.')
3413 that is canonical after appending it to the current directory, then
3414 temp1 = temp2+'/'
3415 That is,
3416 strcmp (temp1, temp2) == 0
3417 after adding a slash to temp2 below. It should be safe to not
3418 change those.
3419 */
3420 t = get_working_directory ("symlink-hook");
3421 temp1 = make_absolute (local_dirname, t);
3422 free (t);
3423 temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
3424
3425 /* Try spelling correction if initial canonicalization fails. Make
3426 sure we are set to replace the directory name with the results so
3427 subsequent directory checks don't fail. */
3428 if (temp2 == 0 && dircomplete_spelling && dircomplete_expand)
3429 {
3430 temp2 = dirspell (temp1);
3431 if (temp2)
3432 {
3433 free (temp1);
3434 temp1 = temp2;
3435 temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
3436 return_value |= temp2 != 0;
3437 }
3438 }
3439 /* If we can't canonicalize, bail. */
3440 if (temp2 == 0)
3441 {
3442 free (temp1);
3443 return return_value;
3444 }
3445 len1 = strlen (temp1);
3446 if (temp1[len1 - 1] == '/')
3447 {
3448 len2 = strlen (temp2);
3449 if (len2 > 2) /* don't append `/' to `/' or `//' */
3450 {
3451 temp2 = (char *)xrealloc (temp2, len2 + 2);
3452 temp2[len2] = '/';
3453 temp2[len2 + 1] = '\0';
3454 }
3455 }
3456
3457 /* dircomplete_expand_relpath == 0 means we want to leave relative
3458 pathnames that are unchanged by canonicalization alone.
3459 *local_dirname != '/' && *local_dirname != '.' == relative pathname
3460 (consistent with general.c:absolute_pathname())
3461 temp1 == temp2 (after appending a slash to temp2) means the pathname
3462 is not changed by canonicalization as described above. */
3463 if (dircomplete_expand_relpath || ((local_dirname[0] != '/' && local_dirname[0] != '.') && STREQ (temp1, temp2) == 0))
3464 return_value |= STREQ (local_dirname, temp2) == 0;
3465 free (local_dirname);
3466 *dirname = temp2;
3467 free (temp1);
3468 }
3469
3470 return (return_value);
3471 }
3472
3473 static char **history_completion_array = (char **)NULL;
3474 static int harry_size;
3475 static int harry_len;
3476
3477 static void
3478 build_history_completion_array ()
3479 {
3480 register int i, j;
3481 HIST_ENTRY **hlist;
3482 char **tokens;
3483
3484 /* First, clear out the current dynamic history completion list. */
3485 if (harry_size)
3486 {
3487 strvec_dispose (history_completion_array);
3488 history_completion_array = (char **)NULL;
3489 harry_size = 0;
3490 harry_len = 0;
3491 }
3492
3493 /* Next, grovel each line of history, making each shell-sized token
3494 a separate entry in the history_completion_array. */
3495 hlist = history_list ();
3496
3497 if (hlist)
3498 {
3499 for (i = 0; hlist[i]; i++)
3500 ;
3501 for ( --i; i >= 0; i--)
3502 {
3503 /* Separate each token, and place into an array. */
3504 tokens = history_tokenize (hlist[i]->line);
3505
3506 for (j = 0; tokens && tokens[j]; j++)
3507 {
3508 if (harry_len + 2 > harry_size)
3509 history_completion_array = strvec_resize (history_completion_array, harry_size += 10);
3510
3511 history_completion_array[harry_len++] = tokens[j];
3512 history_completion_array[harry_len] = (char *)NULL;
3513 }
3514 free (tokens);
3515 }
3516
3517 /* Sort the complete list of tokens. */
3518 if (dabbrev_expand_active == 0)
3519 qsort (history_completion_array, harry_len, sizeof (char *), (QSFUNC *)strvec_strcmp);
3520 }
3521 }
3522
3523 static char *
3524 history_completion_generator (hint_text, state)
3525 const char *hint_text;
3526 int state;
3527 {
3528 static int local_index, len;
3529 static const char *text;
3530
3531 /* If this is the first call to the generator, then initialize the
3532 list of strings to complete over. */
3533 if (state == 0)
3534 {
3535 if (dabbrev_expand_active) /* This is kind of messy */
3536 rl_completion_suppress_append = 1;
3537 local_index = 0;
3538 build_history_completion_array ();
3539 text = hint_text;
3540 len = strlen (text);
3541 }
3542
3543 while (history_completion_array && history_completion_array[local_index])
3544 {
3545 /* XXX - should this use completion-ignore-case? */
3546 if (strncmp (text, history_completion_array[local_index++], len) == 0)
3547 return (savestring (history_completion_array[local_index - 1]));
3548 }
3549 return ((char *)NULL);
3550 }
3551
3552 static int
3553 dynamic_complete_history (count, key)
3554 int count, key;
3555 {
3556 int r;
3557 rl_compentry_func_t *orig_func;
3558 rl_completion_func_t *orig_attempt_func;
3559 rl_compignore_func_t *orig_ignore_func;
3560
3561 orig_func = rl_completion_entry_function;
3562 orig_attempt_func = rl_attempted_completion_function;
3563 orig_ignore_func = rl_ignore_some_completions_function;
3564
3565 rl_completion_entry_function = history_completion_generator;
3566 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
3567 rl_ignore_some_completions_function = filename_completion_ignore;
3568
3569 /* XXX - use rl_completion_mode here? */
3570 if (rl_last_func == dynamic_complete_history)
3571 r = rl_complete_internal ('?');
3572 else
3573 r = rl_complete_internal (TAB);
3574
3575 rl_completion_entry_function = orig_func;
3576 rl_attempted_completion_function = orig_attempt_func;
3577 rl_ignore_some_completions_function = orig_ignore_func;
3578
3579 return r;
3580 }
3581
3582 static int
3583 bash_dabbrev_expand (count, key)
3584 int count, key;
3585 {
3586 int r, orig_suppress, orig_sort;
3587 rl_compentry_func_t *orig_func;
3588 rl_completion_func_t *orig_attempt_func;
3589 rl_compignore_func_t *orig_ignore_func;
3590
3591 orig_func = rl_menu_completion_entry_function;
3592 orig_attempt_func = rl_attempted_completion_function;
3593 orig_ignore_func = rl_ignore_some_completions_function;
3594 orig_suppress = rl_completion_suppress_append;
3595 orig_sort = rl_sort_completion_matches;
3596
3597 rl_menu_completion_entry_function = history_completion_generator;
3598 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
3599 rl_ignore_some_completions_function = filename_completion_ignore;
3600 rl_filename_completion_desired = 0;
3601 rl_completion_suppress_append = 1;
3602 rl_sort_completion_matches = 0;
3603
3604 /* XXX - use rl_completion_mode here? */
3605 dabbrev_expand_active = 1;
3606 if (rl_last_func == bash_dabbrev_expand)
3607 rl_last_func = rl_menu_complete;
3608 r = rl_menu_complete (count, key);
3609 dabbrev_expand_active = 0;
3610
3611 rl_last_func = bash_dabbrev_expand;
3612 rl_menu_completion_entry_function = orig_func;
3613 rl_attempted_completion_function = orig_attempt_func;
3614 rl_ignore_some_completions_function = orig_ignore_func;
3615 rl_completion_suppress_append = orig_suppress;
3616 rl_sort_completion_matches = orig_sort;
3617
3618 return r;
3619 }
3620
3621 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
3622 static int
3623 bash_complete_username (ignore, ignore2)
3624 int ignore, ignore2;
3625 {
3626 return bash_complete_username_internal (rl_completion_mode (bash_complete_username));
3627 }
3628
3629 static int
3630 bash_possible_username_completions (ignore, ignore2)
3631 int ignore, ignore2;
3632 {
3633 return bash_complete_username_internal ('?');
3634 }
3635
3636 static int
3637 bash_complete_username_internal (what_to_do)
3638 int what_to_do;
3639 {
3640 return bash_specific_completion (what_to_do, rl_username_completion_function);
3641 }
3642
3643 static int
3644 bash_complete_filename (ignore, ignore2)
3645 int ignore, ignore2;
3646 {
3647 return bash_complete_filename_internal (rl_completion_mode (bash_complete_filename));
3648 }
3649
3650 static int
3651 bash_possible_filename_completions (ignore, ignore2)
3652 int ignore, ignore2;
3653 {
3654 return bash_complete_filename_internal ('?');
3655 }
3656
3657 static int
3658 bash_complete_filename_internal (what_to_do)
3659 int what_to_do;
3660 {
3661 rl_compentry_func_t *orig_func;
3662 rl_completion_func_t *orig_attempt_func;
3663 rl_icppfunc_t *orig_dir_func;
3664 rl_compignore_func_t *orig_ignore_func;
3665 /*const*/ char *orig_rl_completer_word_break_characters;
3666 int r;
3667
3668 orig_func = rl_completion_entry_function;
3669 orig_attempt_func = rl_attempted_completion_function;
3670 orig_ignore_func = rl_ignore_some_completions_function;
3671 orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
3672
3673 orig_dir_func = save_directory_hook ();
3674
3675 rl_completion_entry_function = rl_filename_completion_function;
3676 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
3677 rl_ignore_some_completions_function = filename_completion_ignore;
3678 rl_completer_word_break_characters = " \t\n\"\'";
3679
3680 r = rl_complete_internal (what_to_do);
3681
3682 rl_completion_entry_function = orig_func;
3683 rl_attempted_completion_function = orig_attempt_func;
3684 rl_ignore_some_completions_function = orig_ignore_func;
3685 rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
3686
3687 restore_directory_hook (orig_dir_func);
3688
3689 return r;
3690 }
3691
3692 static int
3693 bash_complete_hostname (ignore, ignore2)
3694 int ignore, ignore2;
3695 {
3696 return bash_complete_hostname_internal (rl_completion_mode (bash_complete_hostname));
3697 }
3698
3699 static int
3700 bash_possible_hostname_completions (ignore, ignore2)
3701 int ignore, ignore2;
3702 {
3703 return bash_complete_hostname_internal ('?');
3704 }
3705
3706 static int
3707 bash_complete_variable (ignore, ignore2)
3708 int ignore, ignore2;
3709 {
3710 return bash_complete_variable_internal (rl_completion_mode (bash_complete_variable));
3711 }
3712
3713 static int
3714 bash_possible_variable_completions (ignore, ignore2)
3715 int ignore, ignore2;
3716 {
3717 return bash_complete_variable_internal ('?');
3718 }
3719
3720 static int
3721 bash_complete_command (ignore, ignore2)
3722 int ignore, ignore2;
3723 {
3724 return bash_complete_command_internal (rl_completion_mode (bash_complete_command));
3725 }
3726
3727 static int
3728 bash_possible_command_completions (ignore, ignore2)
3729 int ignore, ignore2;
3730 {
3731 return bash_complete_command_internal ('?');
3732 }
3733
3734 static int
3735 bash_complete_hostname_internal (what_to_do)
3736 int what_to_do;
3737 {
3738 return bash_specific_completion (what_to_do, hostname_completion_function);
3739 }
3740
3741 static int
3742 bash_complete_variable_internal (what_to_do)
3743 int what_to_do;
3744 {
3745 return bash_specific_completion (what_to_do, variable_completion_function);
3746 }
3747
3748 static int
3749 bash_complete_command_internal (what_to_do)
3750 int what_to_do;
3751 {
3752 return bash_specific_completion (what_to_do, command_word_completion_function);
3753 }
3754
3755 static int
3756 completion_glob_pattern (string)
3757 char *string;
3758 {
3759 register int c;
3760 char *send;
3761 int open;
3762
3763 DECLARE_MBSTATE;
3764
3765 open = 0;
3766 send = string + strlen (string);
3767
3768 while (c = *string++)
3769 {
3770 switch (c)
3771 {
3772 case '?':
3773 case '*':
3774 return (1);
3775
3776 case '[':
3777 open++;
3778 continue;
3779
3780 case ']':
3781 if (open)
3782 return (1);
3783 continue;
3784
3785 case '+':
3786 case '@':
3787 case '!':
3788 if (*string == '(') /*)*/
3789 return (1);
3790 continue;
3791
3792 case '\\':
3793 if (*string++ == 0)
3794 return (0);
3795 }
3796
3797 /* Advance one fewer byte than an entire multibyte character to
3798 account for the auto-increment in the loop above. */
3799 #ifdef HANDLE_MULTIBYTE
3800 string--;
3801 ADVANCE_CHAR_P (string, send - string);
3802 string++;
3803 #else
3804 ADVANCE_CHAR_P (string, send - string);
3805 #endif
3806 }
3807 return (0);
3808 }
3809
3810 static char *globtext;
3811 static char *globorig;
3812
3813 static char *
3814 glob_complete_word (text, state)
3815 const char *text;
3816 int state;
3817 {
3818 static char **matches = (char **)NULL;
3819 static int ind;
3820 int glen;
3821 char *ret, *ttext;
3822
3823 if (state == 0)
3824 {
3825 rl_filename_completion_desired = 1;
3826 FREE (matches);
3827 if (globorig != globtext)
3828 FREE (globorig);
3829 FREE (globtext);
3830
3831 ttext = bash_tilde_expand (text, 0);
3832
3833 if (rl_explicit_arg)
3834 {
3835 globorig = savestring (ttext);
3836 glen = strlen (ttext);
3837 globtext = (char *)xmalloc (glen + 2);
3838 strcpy (globtext, ttext);
3839 globtext[glen] = '*';
3840 globtext[glen+1] = '\0';
3841 }
3842 else
3843 globtext = globorig = savestring (ttext);
3844
3845 if (ttext != text)
3846 free (ttext);
3847
3848 matches = shell_glob_filename (globtext);
3849 if (GLOB_FAILED (matches))
3850 matches = (char **)NULL;
3851 ind = 0;
3852 }
3853
3854 ret = matches ? matches[ind] : (char *)NULL;
3855 ind++;
3856 return ret;
3857 }
3858
3859 static int
3860 bash_glob_completion_internal (what_to_do)
3861 int what_to_do;
3862 {
3863 return bash_specific_completion (what_to_do, glob_complete_word);
3864 }
3865
3866 /* A special quoting function so we don't end up quoting globbing characters
3867 in the word if there are no matches or multiple matches. */
3868 static char *
3869 bash_glob_quote_filename (s, rtype, qcp)
3870 char *s;
3871 int rtype;
3872 char *qcp;
3873 {
3874 if (globorig && qcp && *qcp == '\0' && STREQ (s, globorig))
3875 return (savestring (s));
3876 else
3877 return (bash_quote_filename (s, rtype, qcp));
3878 }
3879
3880 static int
3881 bash_glob_complete_word (count, key)
3882 int count, key;
3883 {
3884 int r;
3885 rl_quote_func_t *orig_quoting_function;
3886
3887 if (rl_editing_mode == EMACS_EDITING_MODE)
3888 rl_explicit_arg = 1; /* force `*' append */
3889 orig_quoting_function = rl_filename_quoting_function;
3890 rl_filename_quoting_function = bash_glob_quote_filename;
3891
3892 r = bash_glob_completion_internal (rl_completion_mode (bash_glob_complete_word));
3893
3894 rl_filename_quoting_function = orig_quoting_function;
3895 return r;
3896 }
3897
3898 static int
3899 bash_glob_expand_word (count, key)
3900 int count, key;
3901 {
3902 return bash_glob_completion_internal ('*');
3903 }
3904
3905 static int
3906 bash_glob_list_expansions (count, key)
3907 int count, key;
3908 {
3909 return bash_glob_completion_internal ('?');
3910 }
3911
3912 static int
3913 bash_specific_completion (what_to_do, generator)
3914 int what_to_do;
3915 rl_compentry_func_t *generator;
3916 {
3917 rl_compentry_func_t *orig_func;
3918 rl_completion_func_t *orig_attempt_func;
3919 rl_compignore_func_t *orig_ignore_func;
3920 int r;
3921
3922 orig_func = rl_completion_entry_function;
3923 orig_attempt_func = rl_attempted_completion_function;
3924 orig_ignore_func = rl_ignore_some_completions_function;
3925 rl_completion_entry_function = generator;
3926 rl_attempted_completion_function = NULL;
3927 rl_ignore_some_completions_function = orig_ignore_func;
3928
3929 r = rl_complete_internal (what_to_do);
3930
3931 rl_completion_entry_function = orig_func;
3932 rl_attempted_completion_function = orig_attempt_func;
3933 rl_ignore_some_completions_function = orig_ignore_func;
3934
3935 return r;
3936 }
3937
3938 #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
3939
3940 #if defined (VI_MODE)
3941 /* Completion, from vi mode's point of view. This is a modified version of
3942 rl_vi_complete which uses the bash globbing code to implement what POSIX
3943 specifies, which is to append a `*' and attempt filename generation (which
3944 has the side effect of expanding any globbing characters in the word). */
3945 static int
3946 bash_vi_complete (count, key)
3947 int count, key;
3948 {
3949 #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
3950 int p, r;
3951 char *t;
3952
3953 if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
3954 {
3955 if (!whitespace (rl_line_buffer[rl_point + 1]))
3956 rl_vi_end_word (1, 'E');
3957 rl_point++;
3958 }
3959
3960 /* Find boundaries of current word, according to vi definition of a
3961 `bigword'. */
3962 t = 0;
3963 if (rl_point > 0)
3964 {
3965 p = rl_point;
3966 rl_vi_bWord (1, 'B');
3967 r = rl_point;
3968 rl_point = p;
3969 p = r;
3970
3971 t = substring (rl_line_buffer, p, rl_point);
3972 }
3973
3974 if (t && completion_glob_pattern (t) == 0)
3975 rl_explicit_arg = 1; /* XXX - force glob_complete_word to append `*' */
3976 FREE (t);
3977
3978 if (key == '*') /* Expansion and replacement. */
3979 r = bash_glob_expand_word (count, key);
3980 else if (key == '=') /* List possible completions. */
3981 r = bash_glob_list_expansions (count, key);
3982 else if (key == '\\') /* Standard completion */
3983 r = bash_glob_complete_word (count, key);
3984 else
3985 r = rl_complete (0, key);
3986
3987 if (key == '*' || key == '\\')
3988 rl_vi_start_inserting (key, 1, 1);
3989
3990 return (r);
3991 #else
3992 return rl_vi_complete (count, key);
3993 #endif /* !SPECIFIC_COMPLETION_FUNCTIONS */
3994 }
3995 #endif /* VI_MODE */
3996
3997 /* Filename quoting for completion. */
3998 /* A function to strip unquoted quote characters (single quotes, double
3999 quotes, and backslashes). It allows single quotes to appear
4000 within double quotes, and vice versa. It should be smarter. */
4001 static char *
4002 bash_dequote_filename (text, quote_char)
4003 char *text;
4004 int quote_char;
4005 {
4006 char *ret, *p, *r;
4007 int l, quoted;
4008
4009 l = strlen (text);
4010 ret = (char *)xmalloc (l + 1);
4011 for (quoted = quote_char, p = text, r = ret; p && *p; p++)
4012 {
4013 /* Allow backslash-escaped characters to pass through unscathed. */
4014 if (*p == '\\')
4015 {
4016 /* Backslashes are preserved within single quotes. */
4017 if (quoted == '\'')
4018 *r++ = *p;
4019 /* Backslashes are preserved within double quotes unless the
4020 character is one that is defined to be escaped */
4021 else if (quoted == '"' && ((sh_syntaxtab[(unsigned char)p[1]] & CBSDQUOTE) == 0))
4022 *r++ = *p;
4023
4024 *r++ = *++p;
4025 if (*p == '\0')
4026 return ret; /* XXX - was break; */
4027 continue;
4028 }
4029 /* Close quote. */
4030 if (quoted && *p == quoted)
4031 {
4032 quoted = 0;
4033 continue;
4034 }
4035 /* Open quote. */
4036 if (quoted == 0 && (*p == '\'' || *p == '"'))
4037 {
4038 quoted = *p;
4039 continue;
4040 }
4041 *r++ = *p;
4042 }
4043 *r = '\0';
4044 return ret;
4045 }
4046
4047 /* Quote characters that the readline completion code would treat as
4048 word break characters with backslashes. Pass backslash-quoted
4049 characters through without examination. */
4050 static char *
4051 quote_word_break_chars (text)
4052 char *text;
4053 {
4054 char *ret, *r, *s;
4055 int l;
4056
4057 l = strlen (text);
4058 ret = (char *)xmalloc ((2 * l) + 1);
4059 for (s = text, r = ret; *s; s++)
4060 {
4061 /* Pass backslash-quoted characters through, including the backslash. */
4062 if (*s == '\\')
4063 {
4064 *r++ = '\\';
4065 *r++ = *++s;
4066 if (*s == '\0')
4067 break;
4068 continue;
4069 }
4070 /* OK, we have an unquoted character. Check its presence in
4071 rl_completer_word_break_characters. */
4072 if (mbschr (rl_completer_word_break_characters, *s))
4073 *r++ = '\\';
4074 /* XXX -- check for standalone tildes here and backslash-quote them */
4075 if (s == text && *s == '~' && file_exists (text))
4076 *r++ = '\\';
4077 *r++ = *s;
4078 }
4079 *r = '\0';
4080 return ret;
4081 }
4082
4083 /* Use characters in STRING to populate the table of characters that should
4084 be backslash-quoted. The table will be used for sh_backslash_quote from
4085 this file. */
4086 static void
4087 set_filename_bstab (string)
4088 const char *string;
4089 {
4090 const char *s;
4091
4092 memset (filename_bstab, 0, sizeof (filename_bstab));
4093 for (s = string; s && *s; s++)
4094 filename_bstab[*s] = 1;
4095 }
4096
4097 /* Quote a filename using double quotes, single quotes, or backslashes
4098 depending on the value of completion_quoting_style. If we're
4099 completing using backslashes, we need to quote some additional
4100 characters (those that readline treats as word breaks), so we call
4101 quote_word_break_chars on the result. This returns newly-allocated
4102 memory. */
4103 static char *
4104 bash_quote_filename (s, rtype, qcp)
4105 char *s;
4106 int rtype;
4107 char *qcp;
4108 {
4109 char *rtext, *mtext, *ret;
4110 int rlen, cs;
4111
4112 rtext = (char *)NULL;
4113
4114 /* If RTYPE == MULT_MATCH, it means that there is
4115 more than one match. In this case, we do not add
4116 the closing quote or attempt to perform tilde
4117 expansion. If RTYPE == SINGLE_MATCH, we try
4118 to perform tilde expansion, because single and double
4119 quotes inhibit tilde expansion by the shell. */
4120
4121 cs = completion_quoting_style;
4122 /* Might need to modify the default completion style based on *qcp,
4123 since it's set to any user-provided opening quote. We also change
4124 to single-quoting if there is no user-provided opening quote and
4125 the word being completed contains newlines, since those are not
4126 quoted correctly using backslashes (a backslash-newline pair is
4127 special to the shell parser). */
4128 if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && mbschr (s, '\n'))
4129 cs = COMPLETE_SQUOTE;
4130 else if (*qcp == '"')
4131 cs = COMPLETE_DQUOTE;
4132 else if (*qcp == '\'')
4133 cs = COMPLETE_SQUOTE;
4134 #if defined (BANG_HISTORY)
4135 else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE &&
4136 history_expansion_inhibited == 0 && mbschr (s, '!'))
4137 cs = COMPLETE_BSQUOTE;
4138
4139 if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE &&
4140 history_expansion_inhibited == 0 && mbschr (s, '!'))
4141 {
4142 cs = COMPLETE_BSQUOTE;
4143 *qcp = '\0';
4144 }
4145 #endif
4146
4147 /* Don't tilde-expand backslash-quoted filenames, since only single and
4148 double quotes inhibit tilde expansion. */
4149 mtext = s;
4150 if (mtext[0] == '~' && rtype == SINGLE_MATCH && cs != COMPLETE_BSQUOTE)
4151 mtext = bash_tilde_expand (s, 0);
4152
4153 switch (cs)
4154 {
4155 case COMPLETE_DQUOTE:
4156 rtext = sh_double_quote (mtext);
4157 break;
4158 case COMPLETE_SQUOTE:
4159 rtext = sh_single_quote (mtext);
4160 break;
4161 case COMPLETE_BSQUOTE:
4162 rtext = sh_backslash_quote (mtext, complete_fullquote ? 0 : filename_bstab, 0);
4163 break;
4164 }
4165
4166 if (mtext != s)
4167 free (mtext);
4168
4169 /* We may need to quote additional characters: those that readline treats
4170 as word breaks that are not quoted by backslash_quote. */
4171 if (rtext && cs == COMPLETE_BSQUOTE)
4172 {
4173 mtext = quote_word_break_chars (rtext);
4174 free (rtext);
4175 rtext = mtext;
4176 }
4177
4178 /* Leave the opening quote intact. The readline completion code takes
4179 care of avoiding doubled opening quotes. */
4180 if (rtext)
4181 {
4182 rlen = strlen (rtext);
4183 ret = (char *)xmalloc (rlen + 1);
4184 strcpy (ret, rtext);
4185 }
4186 else
4187 {
4188 ret = (char *)xmalloc (rlen = 1);
4189 ret[0] = '\0';
4190 }
4191
4192 /* If there are multiple matches, cut off the closing quote. */
4193 if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE)
4194 ret[rlen - 1] = '\0';
4195 free (rtext);
4196 return ret;
4197 }
4198
4199 /* Support for binding readline key sequences to Unix commands. */
4200 static Keymap cmd_xmap;
4201
4202 #ifdef _MINIX
4203 static void
4204 #else
4205 static int
4206 #endif
4207 putx(c)
4208 int c;
4209 {
4210 int x;
4211 x = putc (c, rl_outstream);
4212 #ifndef _MINIX
4213 return x;
4214 #endif
4215 }
4216
4217 static int
4218 bash_execute_unix_command (count, key)
4219 int count; /* ignored */
4220 int key;
4221 {
4222 int type;
4223 register int i, r;
4224 intmax_t mi;
4225 sh_parser_state_t ps;
4226 char *cmd, *value, *ce, old_ch;
4227 SHELL_VAR *v;
4228 char ibuf[INT_STRLEN_BOUND(int) + 1];
4229
4230 /* First, we need to find the right command to execute. This is tricky,
4231 because we might have already indirected into another keymap, so we
4232 have to walk cmd_xmap using the entire key sequence. */
4233 cmd = (char *)rl_function_of_keyseq_len (rl_executing_keyseq, rl_key_sequence_length, cmd_xmap, &type);
4234
4235 if (cmd == 0 || type != ISMACR)
4236 {
4237 rl_crlf ();
4238 internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
4239 rl_forced_update_display ();
4240 return 1;
4241 }
4242
4243 ce = rl_get_termcap ("ce");
4244 if (ce) /* clear current line */
4245 {
4246 #if 0
4247 fprintf (rl_outstream, "\r");
4248 tputs (ce, 1, putx);
4249 #else
4250 rl_clear_visible_line ();
4251 #endif
4252 fflush (rl_outstream);
4253 }
4254 else
4255 rl_crlf (); /* move to a new line */
4256
4257 v = bind_variable ("READLINE_LINE", rl_line_buffer, 0);
4258 if (v)
4259 VSETATTR (v, att_exported);
4260 i = rl_point;
4261 #if defined (HANDLE_MULTIBYTE)
4262 if (MB_CUR_MAX > 1)
4263 {
4264 old_ch = rl_line_buffer[rl_point];
4265 rl_line_buffer[rl_point] = '\0';
4266 i = MB_STRLEN (rl_line_buffer);
4267 rl_line_buffer[rl_point] = old_ch;
4268 }
4269 #endif
4270 value = inttostr (i, ibuf, sizeof (ibuf));
4271 v = bind_int_variable ("READLINE_POINT", value, 0);
4272 if (v)
4273 VSETATTR (v, att_exported);
4274 array_needs_making = 1;
4275
4276 save_parser_state (&ps);
4277 r = parse_and_execute (savestring (cmd), "bash_execute_unix_command", SEVAL_NOHIST|SEVAL_NOFREE);
4278 restore_parser_state (&ps);
4279
4280 v = find_variable ("READLINE_LINE");
4281 maybe_make_readline_line (v ? value_cell (v) : 0);
4282
4283 v = find_variable ("READLINE_POINT");
4284 if (v && legal_number (value_cell (v), &mi))
4285 {
4286 i = mi;
4287 #if defined (HANDLE_MULTIBYTE)
4288 if (i > 0 && MB_CUR_MAX > 1)
4289 i = _rl_find_next_mbchar (rl_line_buffer, 0, i, 0);
4290 #endif
4291 if (i != rl_point)
4292 {
4293 rl_point = i;
4294 if (rl_point > rl_end)
4295 rl_point = rl_end;
4296 else if (rl_point < 0)
4297 rl_point = 0;
4298 }
4299 }
4300
4301 check_unbind_variable ("READLINE_LINE");
4302 check_unbind_variable ("READLINE_POINT");
4303 array_needs_making = 1;
4304
4305 /* and restore the readline buffer and display after command execution. */
4306 /* If we clear the last line of the prompt above, redraw only that last
4307 line. If the command returns 124, we redraw unconditionally as in
4308 previous versions. */
4309 if (ce && r != 124)
4310 rl_redraw_prompt_last_line ();
4311 else
4312 rl_forced_update_display ();
4313
4314 return 0;
4315 }
4316
4317 int
4318 print_unix_command_map ()
4319 {
4320 Keymap save;
4321
4322 save = rl_get_keymap ();
4323 rl_set_keymap (cmd_xmap);
4324 rl_macro_dumper (1);
4325 rl_set_keymap (save);
4326 return 0;
4327 }
4328
4329 static void
4330 init_unix_command_map ()
4331 {
4332 cmd_xmap = rl_make_bare_keymap ();
4333 }
4334
4335 static int
4336 isolate_sequence (string, ind, need_dquote, startp)
4337 char *string;
4338 int ind, need_dquote, *startp;
4339 {
4340 register int i;
4341 int c, passc, delim;
4342
4343 for (i = ind; string[i] && whitespace (string[i]); i++)
4344 ;
4345 /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
4346 if (need_dquote && string[i] != '"')
4347 {
4348 builtin_error (_("%s: first non-whitespace character is not `\"'"), string);
4349 return -1;
4350 }
4351
4352 /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
4353 string to bind the key sequence to. */
4354 delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0;
4355
4356 if (startp)
4357 *startp = delim ? ++i : i;
4358
4359 for (passc = 0; c = string[i]; i++)
4360 {
4361 if (passc)
4362 {
4363 passc = 0;
4364 continue;
4365 }
4366 if (c == '\\')
4367 {
4368 passc++;
4369 continue;
4370 }
4371 if (c == delim)
4372 break;
4373 }
4374
4375 if (delim && string[i] != delim)
4376 {
4377 builtin_error (_("no closing `%c' in %s"), delim, string);
4378 return -1;
4379 }
4380
4381 return i;
4382 }
4383
4384 int
4385 bind_keyseq_to_unix_command (line)
4386 char *line;
4387 {
4388 Keymap kmap;
4389 char *kseq, *value;
4390 int i, kstart;
4391
4392 if (cmd_xmap == 0)
4393 init_unix_command_map ();
4394
4395 kmap = rl_get_keymap ();
4396
4397 /* We duplicate some of the work done by rl_parse_and_bind here, but
4398 this code only has to handle `"keyseq": ["]command["]' and can
4399 generate an error for anything else. */
4400 i = isolate_sequence (line, 0, 1, &kstart);
4401 if (i < 0)
4402 return -1;
4403
4404 /* Create the key sequence string to pass to rl_generic_bind */
4405 kseq = substring (line, kstart, i);
4406
4407 for ( ; line[i] && line[i] != ':'; i++)
4408 ;
4409 if (line[i] != ':')
4410 {
4411 builtin_error (_("%s: missing colon separator"), line);
4412 FREE (kseq);
4413 return -1;
4414 }
4415
4416 i = isolate_sequence (line, i + 1, 0, &kstart);
4417 if (i < 0)
4418 {
4419 FREE (kseq);
4420 return -1;
4421 }
4422
4423 /* Create the value string containing the command to execute. */
4424 value = substring (line, kstart, i);
4425
4426 /* Save the command to execute and the key sequence in the CMD_XMAP */
4427 rl_generic_bind (ISMACR, kseq, value, cmd_xmap);
4428
4429 /* and bind the key sequence in the current keymap to a function that
4430 understands how to execute from CMD_XMAP */
4431 rl_bind_keyseq_in_map (kseq, bash_execute_unix_command, kmap);
4432
4433 free (kseq);
4434 return 0;
4435 }
4436
4437 /* Used by the programmable completion code. Complete TEXT as a filename,
4438 but return only directories as matches. Dequotes the filename before
4439 attempting to find matches. */
4440 char **
4441 bash_directory_completion_matches (text)
4442 const char *text;
4443 {
4444 char **m1;
4445 char *dfn;
4446 int qc;
4447
4448 qc = rl_dispatching ? rl_completion_quote_character : 0;
4449 /* If rl_completion_found_quote != 0, rl_completion_matches will call the
4450 filename dequoting function, causing the directory name to be dequoted
4451 twice. */
4452 if (rl_dispatching && rl_completion_found_quote == 0)
4453 dfn = bash_dequote_filename ((char *)text, qc);
4454 else
4455 dfn = (char *)text;
4456 m1 = rl_completion_matches (dfn, rl_filename_completion_function);
4457 if (dfn != text)
4458 free (dfn);
4459
4460 if (m1 == 0 || m1[0] == 0)
4461 return m1;
4462 /* We don't bother recomputing the lcd of the matches, because it will just
4463 get thrown away by the programmable completion code and recomputed
4464 later. */
4465 (void)bash_progcomp_ignore_filenames (m1);
4466 return m1;
4467 }
4468
4469 char *
4470 bash_dequote_text (text)
4471 const char *text;
4472 {
4473 char *dtxt;
4474 int qc;
4475
4476 qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
4477 dtxt = bash_dequote_filename ((char *)text, qc);
4478 return (dtxt);
4479 }
4480
4481 /* This event hook is designed to be called after readline receives a signal
4482 that interrupts read(2). It gives reasonable responsiveness to interrupts
4483 and fatal signals without executing too much code in a signal handler
4484 context. */
4485 static int
4486 bash_event_hook ()
4487 {
4488 /* If we're going to longjmp to top_level, make sure we clean up readline.
4489 check_signals will call QUIT, which will eventually longjmp to top_level,
4490 calling run_interrupt_trap along the way. The check for sigalrm_seen is
4491 to clean up the read builtin's state. */
4492 if (terminating_signal || interrupt_state || sigalrm_seen)
4493 rl_cleanup_after_signal ();
4494 bashline_reset_event_hook ();
4495 check_signals_and_traps (); /* XXX */
4496 return 0;
4497 }
4498
4499 #endif /* READLINE */