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