]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | /* bashline.c -- Bash's interface to the readline library. */ |
2 | ||
3 | /* Copyright (C) 1987,1991 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 it | |
8 | under the terms of the GNU General Public License as published by | |
bb70624e | 9 | the Free Software Foundation; either version 2, or (at your option) |
726f6388 JA |
10 | any later version. |
11 | ||
12 | Bash is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
15 | License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with Bash; see the file COPYING. If not, write to the Free | |
bb70624e | 19 | Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
726f6388 | 20 | |
ccc6cda3 JA |
21 | #include "config.h" |
22 | ||
23 | #if defined (READLINE) | |
24 | ||
726f6388 JA |
25 | #include "bashtypes.h" |
26 | #include "posixstat.h" | |
27 | ||
ccc6cda3 JA |
28 | #if defined (HAVE_UNISTD_H) |
29 | # include <unistd.h> | |
30 | #endif | |
31 | ||
f73dda09 JA |
32 | #if defined (HAVE_GRP_H) |
33 | # include <grp.h> | |
34 | #endif | |
35 | ||
726f6388 | 36 | #include <stdio.h> |
f73dda09 | 37 | #include "chartypes.h" |
726f6388 | 38 | #include "bashansi.h" |
726f6388 JA |
39 | #include "shell.h" |
40 | #include "builtins.h" | |
726f6388 | 41 | #include "bashhist.h" |
ccc6cda3 | 42 | #include "bashline.h" |
726f6388 | 43 | #include "execute_cmd.h" |
cce855bc | 44 | #include "findcmd.h" |
ccc6cda3 JA |
45 | #include "pathexp.h" |
46 | #include "builtins/common.h" | |
47 | #include <readline/rlconf.h> | |
48 | #include <readline/readline.h> | |
49 | #include <readline/history.h> | |
50 | ||
51 | #include <glob/glob.h> | |
726f6388 JA |
52 | |
53 | #if defined (ALIAS) | |
54 | # include "alias.h" | |
55 | #endif | |
56 | ||
bb70624e JA |
57 | #if defined (PROGRAMMABLE_COMPLETION) |
58 | # include "pcomplete.h" | |
59 | #endif | |
60 | ||
726f6388 | 61 | #if defined (BRACE_COMPLETION) |
28ef6c31 | 62 | extern int bash_brace_completion __P((int, int)); |
726f6388 JA |
63 | #endif /* BRACE_COMPLETION */ |
64 | ||
28ef6c31 JA |
65 | /* Forward declarations */ |
66 | ||
726f6388 | 67 | /* Functions bound to keys in Readline for Bash users. */ |
28ef6c31 JA |
68 | static int shell_expand_line __P((int, int)); |
69 | static int display_shell_version __P((int, int)); | |
70 | static int operate_and_get_next __P((int, int)); | |
71 | ||
72 | static int bash_ignore_filenames __P((char **)); | |
73 | static int bash_ignore_everything __P((char **)); | |
74 | ||
cce855bc | 75 | #if defined (BANG_HISTORY) |
f73dda09 | 76 | static char *history_expand_line_internal __P((char *)); |
28ef6c31 JA |
77 | static int history_expand_line __P((int, int)); |
78 | static int tcsh_magic_space __P((int, int)); | |
cce855bc | 79 | #endif /* BANG_HISTORY */ |
d166f048 | 80 | #ifdef ALIAS |
28ef6c31 | 81 | static int alias_expand_line __P((int, int)); |
cce855bc JA |
82 | #endif |
83 | #if defined (BANG_HISTORY) && defined (ALIAS) | |
28ef6c31 | 84 | static int history_and_alias_expand_line __P((int, int)); |
d166f048 | 85 | #endif |
726f6388 JA |
86 | |
87 | /* Helper functions for Readline. */ | |
f73dda09 JA |
88 | static int bash_directory_completion_hook __P((char **)); |
89 | static int filename_completion_ignore __P((char **)); | |
28ef6c31 | 90 | static int bash_push_line __P((void)); |
726f6388 | 91 | |
f73dda09 JA |
92 | static void cleanup_expansion_error __P((void)); |
93 | static void maybe_make_readline_line __P((char *)); | |
94 | static void set_up_new_line __P((char *)); | |
95 | ||
96 | static int check_redir __P((int)); | |
28ef6c31 JA |
97 | static char **attempt_shell_completion __P((const char *, int, int)); |
98 | static char *variable_completion_function __P((const char *, int)); | |
99 | static char *hostname_completion_function __P((const char *, int)); | |
100 | static char *command_subst_completion_function __P((const char *, int)); | |
ccc6cda3 | 101 | |
f73dda09 JA |
102 | static void build_history_completion_array __P((void)); |
103 | static char *history_completion_generator __P((const char *, int)); | |
28ef6c31 | 104 | static int dynamic_complete_history __P((int, int)); |
726f6388 | 105 | |
f73dda09 | 106 | static void initialize_hostname_list __P((void)); |
28ef6c31 | 107 | static void add_host_name __P((char *)); |
f73dda09 JA |
108 | static void snarf_hosts_from_file __P((char *)); |
109 | static char **hostnames_matching __P((char *)); | |
110 | ||
111 | static void _ignore_completion_names __P((char **, sh_ignore_func_t *)); | |
112 | static int name_is_acceptable __P((const char *)); | |
113 | static int test_for_directory __P((const char *)); | |
114 | static int return_zero __P((const char *)); | |
28ef6c31 JA |
115 | |
116 | static char *bash_dequote_filename __P((char *, int)); | |
f73dda09 | 117 | static char *quote_word_break_chars __P((char *)); |
28ef6c31 | 118 | static char *bash_quote_filename __P((char *, int, char *)); |
ccc6cda3 | 119 | |
f73dda09 JA |
120 | static int bash_execute_unix_command __P((int, int)); |
121 | static void init_unix_command_map __P((void)); | |
122 | static int isolate_sequence __P((char *, int, int, int *)); | |
123 | ||
124 | static int set_saved_history __P((void)); | |
125 | ||
ccc6cda3 | 126 | #if defined (ALIAS) |
28ef6c31 | 127 | static int posix_edit_macros __P((int, int)); |
ccc6cda3 | 128 | #endif |
726f6388 | 129 | |
bb70624e | 130 | #if defined (PROGRAMMABLE_COMPLETION) |
f73dda09 JA |
131 | static int find_cmd_start __P((int)); |
132 | static int find_cmd_end __P((int)); | |
133 | static char *find_cmd_name __P((int)); | |
134 | static char *prog_complete_return __P((const char *, int)); | |
135 | ||
bb70624e | 136 | static char **prog_complete_matches; |
bb70624e JA |
137 | #endif |
138 | ||
726f6388 | 139 | /* Variables used here but defined in other files. */ |
f73dda09 | 140 | extern int current_command_line_count; |
726f6388 | 141 | extern int posixly_correct, no_symbolic_links; |
726f6388 JA |
142 | extern char *current_prompt_string, *ps1_prompt; |
143 | extern STRING_INT_ALIST word_token_alist[]; | |
726f6388 JA |
144 | |
145 | /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual | |
146 | completion functions which indicate what type of completion should be | |
147 | done (at or before point) that can be bound to key sequences with | |
148 | the readline library. */ | |
149 | #define SPECIFIC_COMPLETION_FUNCTIONS | |
150 | ||
151 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) | |
28ef6c31 JA |
152 | static int bash_specific_completion __P((int, rl_compentry_func_t *)); |
153 | ||
154 | static int bash_complete_filename_internal __P((int)); | |
155 | static int bash_complete_username_internal __P((int)); | |
156 | static int bash_complete_hostname_internal __P((int)); | |
157 | static int bash_complete_variable_internal __P((int)); | |
158 | static int bash_complete_command_internal __P((int)); | |
159 | ||
160 | static int bash_complete_filename __P((int, int)); | |
161 | static int bash_possible_filename_completions __P((int, int)); | |
162 | static int bash_complete_username __P((int, int)); | |
163 | static int bash_possible_username_completions __P((int, int)); | |
164 | static int bash_complete_hostname __P((int, int)); | |
165 | static int bash_possible_hostname_completions __P((int, int)); | |
166 | static int bash_complete_variable __P((int, int)); | |
167 | static int bash_possible_variable_completions __P((int, int)); | |
168 | static int bash_complete_command __P((int, int)); | |
169 | static int bash_possible_command_completions __P((int, int)); | |
f73dda09 JA |
170 | |
171 | static char *glob_complete_word __P((const char *, int)); | |
172 | static int bash_glob_completion_internal __P((int)); | |
173 | static int bash_glob_expand_word __P((int, int)); | |
174 | static int bash_glob_list_expansions __P((int, int)); | |
726f6388 JA |
175 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ |
176 | ||
726f6388 | 177 | #if defined (VI_MODE) |
28ef6c31 | 178 | static int vi_edit_and_execute_command __P((int, int)); |
726f6388 JA |
179 | #endif |
180 | ||
ccc6cda3 JA |
181 | /* Non-zero once initalize_readline () has been called. */ |
182 | int bash_readline_initialized = 0; | |
183 | ||
184 | /* If non-zero, we do hostname completion, breaking words at `@' and | |
185 | trying to complete the stuff after the `@' from our own internal | |
186 | host list. */ | |
187 | int perform_hostname_completion = 1; | |
188 | ||
bb70624e JA |
189 | /* If non-zero, we don't do command completion on an empty line. */ |
190 | int no_empty_command_completion; | |
191 | ||
ccc6cda3 JA |
192 | static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:"; |
193 | static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:"; | |
194 | ||
28ef6c31 | 195 | static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL; |
726f6388 | 196 | |
ccc6cda3 JA |
197 | /* What kind of quoting is performed by bash_quote_filename: |
198 | COMPLETE_DQUOTE = double-quoting the filename | |
199 | COMPLETE_SQUOTE = single_quoting the filename | |
200 | COMPLETE_BSQUOTE = backslash-quoting special chars in the filename | |
201 | */ | |
202 | #define COMPLETE_DQUOTE 1 | |
203 | #define COMPLETE_SQUOTE 2 | |
204 | #define COMPLETE_BSQUOTE 3 | |
205 | static int completion_quoting_style = COMPLETE_BSQUOTE; | |
206 | ||
726f6388 JA |
207 | /* Change the readline VI-mode keymaps into or out of Posix.2 compliance. |
208 | Called when the shell is put into or out of `posix' mode. */ | |
209 | void | |
210 | posix_readline_initialize (on_or_off) | |
211 | int on_or_off; | |
212 | { | |
ccc6cda3 JA |
213 | if (on_or_off) |
214 | rl_variable_bind ("comment-begin", "#"); | |
726f6388 | 215 | #if defined (VI_MODE) |
ccc6cda3 JA |
216 | rl_bind_key_in_map (CTRL('I'), on_or_off ? rl_insert : rl_complete, vi_insertion_keymap); |
217 | #endif | |
218 | } | |
219 | ||
f73dda09 | 220 | int |
ccc6cda3 JA |
221 | enable_hostname_completion (on_or_off) |
222 | int on_or_off; | |
223 | { | |
f73dda09 JA |
224 | int old_value; |
225 | ||
226 | old_value = perform_hostname_completion; | |
227 | ||
726f6388 JA |
228 | if (on_or_off) |
229 | { | |
ccc6cda3 JA |
230 | perform_hostname_completion = 1; |
231 | rl_special_prefixes = "$@"; | |
232 | rl_completer_word_break_characters = bash_completer_word_break_characters; | |
726f6388 JA |
233 | } |
234 | else | |
ccc6cda3 JA |
235 | { |
236 | perform_hostname_completion = 0; | |
237 | rl_special_prefixes = "$"; | |
238 | rl_completer_word_break_characters = bash_nohostname_word_break_characters; | |
239 | } | |
f73dda09 JA |
240 | |
241 | return (old_value); | |
ccc6cda3 | 242 | } |
726f6388 JA |
243 | |
244 | /* Called once from parse.y if we are going to use readline. */ | |
245 | void | |
246 | initialize_readline () | |
247 | { | |
248 | if (bash_readline_initialized) | |
249 | return; | |
250 | ||
251 | rl_terminal_name = get_string_value ("TERM"); | |
252 | rl_instream = stdin; | |
253 | rl_outstream = stderr; | |
726f6388 JA |
254 | |
255 | /* Allow conditional parsing of the ~/.inputrc file. */ | |
256 | rl_readline_name = "Bash"; | |
257 | ||
28ef6c31 JA |
258 | /* Add bindable names before calling rl_initialize so they may be |
259 | referenced in the various inputrc files. */ | |
260 | rl_add_defun ("shell-expand-line", shell_expand_line, -1); | |
cce855bc | 261 | #ifdef BANG_HISTORY |
28ef6c31 JA |
262 | rl_add_defun ("history-expand-line", history_expand_line, -1); |
263 | rl_add_defun ("magic-space", tcsh_magic_space, -1); | |
cce855bc JA |
264 | #endif |
265 | ||
d166f048 | 266 | #ifdef ALIAS |
28ef6c31 | 267 | rl_add_defun ("alias-expand-line", alias_expand_line, -1); |
bc4cd23c | 268 | # ifdef BANG_HISTORY |
28ef6c31 | 269 | rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line, -1); |
bc4cd23c | 270 | # endif |
d166f048 JA |
271 | #endif |
272 | ||
726f6388 JA |
273 | /* Backwards compatibility. */ |
274 | rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1); | |
275 | ||
28ef6c31 JA |
276 | rl_add_defun ("operate-and-get-next", operate_and_get_next, -1); |
277 | rl_add_defun ("display-shell-version", display_shell_version, -1); | |
278 | ||
279 | #if defined (BRACE_COMPLETION) | |
280 | rl_add_defun ("complete-into-braces", bash_brace_completion, -1); | |
281 | #endif | |
282 | ||
283 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) | |
284 | rl_add_defun ("complete-filename", bash_complete_filename, -1); | |
285 | rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1); | |
286 | rl_add_defun ("complete-username", bash_complete_username, -1); | |
287 | rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1); | |
288 | rl_add_defun ("complete-hostname", bash_complete_hostname, -1); | |
289 | rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1); | |
290 | rl_add_defun ("complete-variable", bash_complete_variable, -1); | |
291 | rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1); | |
292 | rl_add_defun ("complete-command", bash_complete_command, -1); | |
293 | rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1); | |
294 | rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1); | |
295 | rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1); | |
296 | #endif | |
297 | ||
298 | rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1); | |
726f6388 | 299 | |
28ef6c31 JA |
300 | /* Bind defaults before binding our custom shell keybindings. */ |
301 | if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0) | |
302 | rl_initialize (); | |
303 | ||
304 | /* Bind up our special shell functions. */ | |
305 | rl_bind_key_in_map (CTRL('E'), shell_expand_line, emacs_meta_keymap); | |
306 | ||
307 | /* Bind up our special shell functions. */ | |
308 | #ifdef BANG_HISTORY | |
309 | rl_bind_key_in_map ('^', history_expand_line, emacs_meta_keymap); | |
310 | #endif | |
311 | ||
312 | rl_bind_key_in_map (CTRL ('O'), operate_and_get_next, emacs_standard_keymap); | |
313 | rl_bind_key_in_map (CTRL ('V'), display_shell_version, emacs_ctlx_keymap); | |
726f6388 JA |
314 | |
315 | /* In Bash, the user can switch editing modes with "set -o [vi emacs]", | |
316 | so it is not necessary to allow C-M-j for context switching. Turn | |
317 | off this occasionally confusing behaviour. */ | |
318 | rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap); | |
319 | rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap); | |
320 | #if defined (VI_MODE) | |
321 | rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap); | |
322 | #endif | |
ccc6cda3 | 323 | |
726f6388 | 324 | #if defined (BRACE_COMPLETION) |
28ef6c31 | 325 | rl_bind_key_in_map ('{', bash_brace_completion, emacs_meta_keymap); |
726f6388 JA |
326 | #endif /* BRACE_COMPLETION */ |
327 | ||
328 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) | |
28ef6c31 JA |
329 | rl_bind_key_in_map ('/', bash_complete_filename, emacs_meta_keymap); |
330 | rl_bind_key_in_map ('/', bash_possible_filename_completions, emacs_ctlx_keymap); | |
331 | ||
332 | rl_bind_key_in_map ('~', bash_complete_username, emacs_meta_keymap); | |
333 | rl_bind_key_in_map ('~', bash_possible_username_completions, emacs_ctlx_keymap); | |
334 | ||
335 | rl_bind_key_in_map ('@', bash_complete_hostname, emacs_meta_keymap); | |
336 | rl_bind_key_in_map ('@', bash_possible_hostname_completions, emacs_ctlx_keymap); | |
337 | ||
338 | rl_bind_key_in_map ('$', bash_complete_variable, emacs_meta_keymap); | |
339 | rl_bind_key_in_map ('$', bash_possible_variable_completions, emacs_ctlx_keymap); | |
340 | ||
341 | rl_bind_key_in_map ('!', bash_complete_command, emacs_meta_keymap); | |
342 | rl_bind_key_in_map ('!', bash_possible_command_completions, emacs_ctlx_keymap); | |
343 | ||
344 | rl_bind_key_in_map ('*', bash_glob_expand_word, emacs_ctlx_keymap); | |
345 | rl_bind_key_in_map ('g', bash_glob_list_expansions, emacs_ctlx_keymap); | |
726f6388 JA |
346 | |
347 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ | |
348 | ||
28ef6c31 | 349 | rl_bind_key_in_map (TAB, dynamic_complete_history, emacs_meta_keymap); |
726f6388 JA |
350 | |
351 | /* Tell the completer that we want a crack first. */ | |
28ef6c31 | 352 | rl_attempted_completion_function = attempt_shell_completion; |
726f6388 JA |
353 | |
354 | /* Tell the completer that we might want to follow symbolic links or | |
355 | do other expansion on directory names. */ | |
356 | rl_directory_completion_hook = bash_directory_completion_hook; | |
357 | ||
358 | /* Tell the filename completer we want a chance to ignore some names. */ | |
28ef6c31 | 359 | rl_ignore_some_completions_function = filename_completion_ignore; |
726f6388 JA |
360 | |
361 | #if defined (VI_MODE) | |
28ef6c31 | 362 | rl_bind_key_in_map ('v', vi_edit_and_execute_command, vi_movement_keymap); |
ccc6cda3 | 363 | # if defined (ALIAS) |
28ef6c31 | 364 | rl_bind_key_in_map ('@', posix_edit_macros, vi_movement_keymap); |
ccc6cda3 | 365 | # endif |
726f6388 JA |
366 | #endif |
367 | ||
368 | rl_completer_quote_characters = "'\""; | |
ccc6cda3 JA |
369 | |
370 | /* This sets rl_completer_word_break_characters and rl_special_prefixes | |
371 | to the appropriate values, depending on whether or not hostname | |
372 | completion is enabled. */ | |
373 | enable_hostname_completion (perform_hostname_completion); | |
374 | ||
375 | /* characters that need to be quoted when appearing in filenames. */ | |
28ef6c31 | 376 | rl_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{"; /*}*/ |
ccc6cda3 JA |
377 | rl_filename_quoting_function = bash_quote_filename; |
378 | rl_filename_dequoting_function = bash_dequote_filename; | |
379 | rl_char_is_quoted_p = char_is_quoted; | |
726f6388 JA |
380 | |
381 | if (posixly_correct) | |
382 | posix_readline_initialize (1); | |
383 | ||
384 | bash_readline_initialized = 1; | |
385 | } | |
386 | ||
387 | /* On Sun systems at least, rl_attempted_completion_function can end up | |
388 | getting set to NULL, and rl_completion_entry_function set to do command | |
389 | word completion if Bash is interrupted while trying to complete a command | |
390 | word. This just resets all the completion functions to the right thing. | |
391 | It's called from throw_to_top_level(). */ | |
392 | void | |
393 | bashline_reinitialize () | |
394 | { | |
395 | tilde_initialize (); | |
396 | rl_attempted_completion_function = attempt_shell_completion; | |
28ef6c31 | 397 | rl_completion_entry_function = NULL; |
726f6388 | 398 | rl_directory_completion_hook = bash_directory_completion_hook; |
28ef6c31 | 399 | rl_ignore_some_completions_function = filename_completion_ignore; |
726f6388 JA |
400 | } |
401 | ||
402 | /* Contains the line to push into readline. */ | |
403 | static char *push_to_readline = (char *)NULL; | |
404 | ||
405 | /* Push the contents of push_to_readline into the | |
406 | readline buffer. */ | |
28ef6c31 | 407 | static int |
726f6388 JA |
408 | bash_push_line () |
409 | { | |
410 | if (push_to_readline) | |
411 | { | |
412 | rl_insert_text (push_to_readline); | |
413 | free (push_to_readline); | |
414 | push_to_readline = (char *)NULL; | |
415 | rl_startup_hook = old_rl_startup_hook; | |
416 | } | |
28ef6c31 | 417 | return 0; |
726f6388 JA |
418 | } |
419 | ||
420 | /* Call this to set the initial text for the next line to read | |
421 | from readline. */ | |
422 | int | |
423 | bash_re_edit (line) | |
424 | char *line; | |
425 | { | |
ccc6cda3 | 426 | FREE (push_to_readline); |
726f6388 JA |
427 | |
428 | push_to_readline = savestring (line); | |
429 | old_rl_startup_hook = rl_startup_hook; | |
28ef6c31 | 430 | rl_startup_hook = bash_push_line; |
726f6388 JA |
431 | |
432 | return (0); | |
433 | } | |
434 | ||
28ef6c31 | 435 | static int |
726f6388 JA |
436 | display_shell_version (count, c) |
437 | int count, c; | |
438 | { | |
28ef6c31 | 439 | rl_crlf (); |
ccc6cda3 | 440 | show_shell_version (0); |
726f6388 JA |
441 | putc ('\r', rl_outstream); |
442 | fflush (rl_outstream); | |
443 | rl_on_new_line (); | |
444 | rl_redisplay (); | |
28ef6c31 | 445 | return 0; |
726f6388 JA |
446 | } |
447 | ||
448 | /* **************************************************************** */ | |
449 | /* */ | |
450 | /* Readline Stuff */ | |
451 | /* */ | |
452 | /* **************************************************************** */ | |
453 | ||
454 | /* If the user requests hostname completion, then simply build a list | |
bb70624e JA |
455 | of hosts, and complete from that forever more, or at least until |
456 | HOSTFILE is unset. */ | |
726f6388 | 457 | |
bb70624e | 458 | /* THIS SHOULD BE A STRINGLIST. */ |
726f6388 JA |
459 | /* The kept list of hostnames. */ |
460 | static char **hostname_list = (char **)NULL; | |
461 | ||
462 | /* The physical size of the above list. */ | |
ccc6cda3 | 463 | static int hostname_list_size; |
726f6388 | 464 | |
ccc6cda3 JA |
465 | /* The number of hostnames in the above list. */ |
466 | static int hostname_list_length; | |
726f6388 JA |
467 | |
468 | /* Whether or not HOSTNAME_LIST has been initialized. */ | |
469 | int hostname_list_initialized = 0; | |
470 | ||
726f6388 JA |
471 | /* Initialize the hostname completion table. */ |
472 | static void | |
473 | initialize_hostname_list () | |
474 | { | |
475 | char *temp; | |
476 | ||
477 | temp = get_string_value ("HOSTFILE"); | |
ccc6cda3 | 478 | if (temp == 0) |
726f6388 | 479 | temp = get_string_value ("hostname_completion_file"); |
ccc6cda3 JA |
480 | if (temp == 0) |
481 | temp = DEFAULT_HOSTS_FILE; | |
726f6388 JA |
482 | |
483 | snarf_hosts_from_file (temp); | |
726f6388 JA |
484 | |
485 | if (hostname_list) | |
486 | hostname_list_initialized++; | |
487 | } | |
488 | ||
489 | /* Add NAME to the list of hosts. */ | |
490 | static void | |
491 | add_host_name (name) | |
492 | char *name; | |
493 | { | |
f73dda09 | 494 | size_t size; |
ccc6cda3 | 495 | |
726f6388 JA |
496 | if (hostname_list_length + 2 > hostname_list_size) |
497 | { | |
ccc6cda3 JA |
498 | hostname_list_size = (hostname_list_size + 32) - (hostname_list_size % 32); |
499 | size = hostname_list_size * sizeof (char *); | |
500 | hostname_list = (char **)xrealloc (hostname_list, size); | |
726f6388 JA |
501 | } |
502 | ||
ccc6cda3 JA |
503 | hostname_list[hostname_list_length++] = savestring (name); |
504 | hostname_list[hostname_list_length] = (char *)NULL; | |
726f6388 JA |
505 | } |
506 | ||
507 | #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c)) | |
508 | ||
509 | static void | |
510 | snarf_hosts_from_file (filename) | |
511 | char *filename; | |
512 | { | |
ccc6cda3 | 513 | FILE *file; |
726f6388 JA |
514 | char *temp, buffer[256], name[256]; |
515 | register int i, start; | |
516 | ||
ccc6cda3 JA |
517 | file = fopen (filename, "r"); |
518 | if (file == 0) | |
726f6388 JA |
519 | return; |
520 | ||
521 | while (temp = fgets (buffer, 255, file)) | |
522 | { | |
523 | /* Skip to first character. */ | |
ccc6cda3 JA |
524 | for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++) |
525 | ; | |
726f6388 | 526 | |
ccc6cda3 JA |
527 | /* If comment or blank line, ignore. */ |
528 | if (buffer[i] == '\0' || buffer[i] == '#') | |
726f6388 JA |
529 | continue; |
530 | ||
531 | /* If `preprocessor' directive, do the include. */ | |
ccc6cda3 | 532 | if (strncmp (buffer + i, "$include ", 9) == 0) |
726f6388 | 533 | { |
ccc6cda3 | 534 | char *incfile, *t; |
726f6388 JA |
535 | |
536 | /* Find start of filename. */ | |
ccc6cda3 JA |
537 | for (incfile = buffer + i + 9; *incfile && whitespace (*incfile); incfile++) |
538 | ; | |
726f6388 JA |
539 | |
540 | /* Find end of filename. */ | |
ccc6cda3 JA |
541 | for (t = incfile; *t && cr_whitespace (*t) == 0; t++) |
542 | ; | |
726f6388 JA |
543 | |
544 | *t = '\0'; | |
545 | ||
ccc6cda3 | 546 | snarf_hosts_from_file (incfile); |
726f6388 JA |
547 | continue; |
548 | } | |
549 | ||
ccc6cda3 | 550 | /* Skip internet address if present. */ |
f73dda09 | 551 | if (DIGIT (buffer[i])) |
ccc6cda3 | 552 | for (; buffer[i] && cr_whitespace (buffer[i]) == 0; i++); |
726f6388 JA |
553 | |
554 | /* Gobble up names. Each name is separated with whitespace. */ | |
ccc6cda3 | 555 | while (buffer[i]) |
726f6388 | 556 | { |
ccc6cda3 JA |
557 | for (; cr_whitespace (buffer[i]); i++) |
558 | ; | |
559 | if (buffer[i] == '\0' || buffer[i] == '#') | |
560 | break; | |
561 | ||
562 | /* Isolate the current word. */ | |
563 | for (start = i; buffer[i] && cr_whitespace (buffer[i]) == 0; i++) | |
564 | ; | |
565 | if (i == start) | |
726f6388 JA |
566 | continue; |
567 | strncpy (name, buffer + start, i - start); | |
568 | name[i - start] = '\0'; | |
569 | add_host_name (name); | |
570 | } | |
571 | } | |
572 | fclose (file); | |
573 | } | |
574 | ||
bb70624e JA |
575 | /* Return the hostname list. */ |
576 | char ** | |
577 | get_hostname_list () | |
578 | { | |
579 | if (hostname_list_initialized == 0) | |
580 | initialize_hostname_list (); | |
581 | return (hostname_list); | |
582 | } | |
583 | ||
584 | void | |
585 | clear_hostname_list () | |
586 | { | |
587 | register int i; | |
588 | ||
589 | if (hostname_list_initialized == 0) | |
590 | return; | |
591 | for (i = 0; i < hostname_list_length; i++) | |
592 | free (hostname_list[i]); | |
593 | hostname_list_length = 0; | |
594 | } | |
595 | ||
726f6388 JA |
596 | /* Return a NULL terminated list of hostnames which begin with TEXT. |
597 | Initialize the hostname list the first time if neccessary. | |
598 | The array is malloc ()'ed, but not the individual strings. */ | |
599 | static char ** | |
600 | hostnames_matching (text) | |
601 | char *text; | |
602 | { | |
ccc6cda3 JA |
603 | register int i, len, nmatch, rsize; |
604 | char **result; | |
726f6388 | 605 | |
ccc6cda3 JA |
606 | if (hostname_list_initialized == 0) |
607 | initialize_hostname_list (); | |
726f6388 | 608 | |
ccc6cda3 JA |
609 | if (hostname_list_initialized == 0) |
610 | return ((char **)NULL); | |
726f6388 JA |
611 | |
612 | /* Special case. If TEXT consists of nothing, then the whole list is | |
613 | what is desired. */ | |
ccc6cda3 | 614 | if (*text == '\0') |
726f6388 | 615 | { |
bb70624e | 616 | result = alloc_array (1 + hostname_list_length); |
726f6388 JA |
617 | for (i = 0; i < hostname_list_length; i++) |
618 | result[i] = hostname_list[i]; | |
619 | result[i] = (char *)NULL; | |
620 | return (result); | |
621 | } | |
622 | ||
623 | /* Scan until found, or failure. */ | |
ccc6cda3 JA |
624 | len = strlen (text); |
625 | result = (char **)NULL; | |
626 | for (i = nmatch = rsize = 0; i < hostname_list_length; i++) | |
726f6388 | 627 | { |
ccc6cda3 | 628 | if (STREQN (text, hostname_list[i], len) == 0) |
28ef6c31 | 629 | continue; |
726f6388 | 630 | |
ccc6cda3 | 631 | /* OK, it matches. Add it to the list. */ |
bc4cd23c | 632 | if (nmatch >= (rsize - 1)) |
726f6388 | 633 | { |
ccc6cda3 JA |
634 | rsize = (rsize + 16) - (rsize % 16); |
635 | result = (char **)xrealloc (result, rsize * sizeof (char *)); | |
726f6388 JA |
636 | } |
637 | ||
ccc6cda3 | 638 | result[nmatch++] = hostname_list[i]; |
726f6388 | 639 | } |
ccc6cda3 JA |
640 | if (nmatch) |
641 | result[nmatch] = (char *)NULL; | |
642 | return (result); | |
726f6388 JA |
643 | } |
644 | ||
ccc6cda3 | 645 | /* The equivalent of the Korn shell C-o operate-and-get-next-history-line |
726f6388 | 646 | editing command. */ |
ccc6cda3 | 647 | static int saved_history_line_to_use = -1; |
726f6388 | 648 | |
28ef6c31 | 649 | static int |
726f6388 JA |
650 | set_saved_history () |
651 | { | |
ccc6cda3 | 652 | if (saved_history_line_to_use >= 0) |
b72432fd | 653 | rl_get_previous_history (history_length - saved_history_line_to_use, 0); |
ccc6cda3 | 654 | saved_history_line_to_use = -1; |
726f6388 | 655 | rl_startup_hook = old_rl_startup_hook; |
f73dda09 | 656 | return (0); |
ccc6cda3 | 657 | } |
726f6388 | 658 | |
28ef6c31 | 659 | static int |
726f6388 JA |
660 | operate_and_get_next (count, c) |
661 | int count, c; | |
662 | { | |
663 | int where; | |
664 | ||
665 | /* Accept the current line. */ | |
b72432fd | 666 | rl_newline (1, c); |
726f6388 JA |
667 | |
668 | /* Find the current line, and find the next line to use. */ | |
669 | where = where_history (); | |
670 | ||
28ef6c31 | 671 | if ((history_is_stifled () && (history_length >= history_max_entries)) || |
726f6388 JA |
672 | (where >= history_length - 1)) |
673 | saved_history_line_to_use = where; | |
674 | else | |
675 | saved_history_line_to_use = where + 1; | |
676 | ||
677 | old_rl_startup_hook = rl_startup_hook; | |
28ef6c31 JA |
678 | rl_startup_hook = set_saved_history; |
679 | ||
680 | return 0; | |
726f6388 JA |
681 | } |
682 | ||
683 | #if defined (VI_MODE) | |
684 | /* This vi mode command causes VI_EDIT_COMMAND to be run on the current | |
685 | command being entered (if no explicit argument is given), otherwise on | |
686 | a command from the history file. */ | |
687 | ||
688 | #define VI_EDIT_COMMAND "fc -e ${VISUAL:-${EDITOR:-vi}}" | |
689 | ||
28ef6c31 | 690 | static int |
726f6388 | 691 | vi_edit_and_execute_command (count, c) |
ccc6cda3 | 692 | int count, c; |
726f6388 JA |
693 | { |
694 | char *command; | |
f73dda09 JA |
695 | int r, cclc, rrs; |
696 | ||
697 | rrs = rl_readline_state; | |
698 | cclc = current_command_line_count; | |
726f6388 JA |
699 | |
700 | /* Accept the current line. */ | |
b72432fd | 701 | rl_newline (1, c); |
726f6388 JA |
702 | |
703 | if (rl_explicit_arg) | |
704 | { | |
f73dda09 | 705 | command = (char *)xmalloc (strlen (VI_EDIT_COMMAND) + 8); |
726f6388 JA |
706 | sprintf (command, "%s %d", VI_EDIT_COMMAND, count); |
707 | } | |
708 | else | |
709 | { | |
710 | /* Take the command we were just editing, add it to the history file, | |
711 | then call fc to operate on it. We have to add a dummy command to | |
712 | the end of the history because fc ignores the last command (assumes | |
713 | it's supposed to deal with the command before the `fc'). */ | |
714 | using_history (); | |
d166f048 JA |
715 | bash_add_history (rl_line_buffer); |
716 | bash_add_history (""); | |
726f6388 JA |
717 | history_lines_this_session++; |
718 | using_history (); | |
719 | command = savestring (VI_EDIT_COMMAND); | |
720 | } | |
28ef6c31 | 721 | r = parse_and_execute (command, "v", SEVAL_NOHIST); |
f73dda09 JA |
722 | |
723 | current_command_line_count = cclc; | |
724 | ||
725 | /* Now erase the contents of the current line and undo the effects of the | |
726 | rl_accept_line() above. We don't even want to make the text we just | |
727 | executed available for undoing. */ | |
ccc6cda3 | 728 | rl_line_buffer[0] = '\0'; /* XXX */ |
f73dda09 JA |
729 | rl_point = rl_end = 0; |
730 | rl_done = 0; | |
731 | rl_readline_state = rrs; | |
732 | ||
733 | rl_forced_update_display (); | |
28ef6c31 JA |
734 | |
735 | return r; | |
726f6388 JA |
736 | } |
737 | #endif /* VI_MODE */ | |
738 | ||
ccc6cda3 JA |
739 | #if defined (ALIAS) |
740 | static int | |
741 | posix_edit_macros (count, key) | |
742 | int count, key; | |
743 | { | |
744 | int c; | |
745 | char alias_name[3], *alias_value, *macro; | |
746 | ||
747 | c = rl_read_key (); | |
748 | alias_name[0] = '_'; | |
749 | alias_name[1] = c; | |
750 | alias_name[2] = '\0'; | |
751 | ||
752 | alias_value = get_alias_value (alias_name); | |
753 | if (alias_value && *alias_value) | |
754 | { | |
755 | macro = savestring (alias_value); | |
756 | rl_push_macro_input (macro); | |
757 | } | |
758 | return 0; | |
759 | } | |
760 | #endif | |
761 | ||
726f6388 JA |
762 | /* **************************************************************** */ |
763 | /* */ | |
764 | /* How To Do Shell Completion */ | |
765 | /* */ | |
766 | /* **************************************************************** */ | |
767 | ||
bb70624e JA |
768 | #define COMMAND_SEPARATORS ";|&{(`" |
769 | ||
770 | static int | |
771 | check_redir (ti) | |
772 | int ti; | |
773 | { | |
774 | register int this_char, prev_char; | |
775 | ||
776 | /* Handle the two character tokens `>&', `<&', and `>|'. | |
777 | We are not in a command position after one of these. */ | |
778 | this_char = rl_line_buffer[ti]; | |
779 | prev_char = rl_line_buffer[ti - 1]; | |
780 | ||
781 | if ((this_char == '&' && (prev_char == '<' || prev_char == '>')) || | |
782 | (this_char == '|' && prev_char == '>')) | |
783 | return (1); | |
784 | else if ((this_char == '{' && prev_char == '$') || /* } */ | |
785 | (char_is_quoted (rl_line_buffer, ti))) | |
786 | return (1); | |
787 | return (0); | |
788 | } | |
789 | ||
790 | #if defined (PROGRAMMABLE_COMPLETION) | |
f73dda09 JA |
791 | /* |
792 | * XXX - because of the <= start test, and setting os = s+1, this can | |
793 | * potentially return os > start. This is probably not what we want to | |
794 | * happen, but fix later after 2.05a-release. | |
795 | */ | |
bb70624e JA |
796 | static int |
797 | find_cmd_start (start) | |
798 | int start; | |
799 | { | |
800 | register int s, os; | |
801 | ||
802 | os = 0; | |
803 | while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS)) <= start) && | |
804 | rl_line_buffer[s]) | |
805 | os = s+1; | |
806 | return os; | |
807 | } | |
808 | ||
809 | static int | |
810 | find_cmd_end (end) | |
811 | int end; | |
812 | { | |
813 | register int e; | |
814 | ||
815 | e = skip_to_delim (rl_line_buffer, end, COMMAND_SEPARATORS); | |
816 | return e; | |
817 | } | |
818 | ||
819 | static char * | |
820 | find_cmd_name (start) | |
821 | int start; | |
822 | { | |
823 | char *name; | |
824 | register int s, e; | |
825 | ||
826 | for (s = start; whitespace (rl_line_buffer[s]); s++) | |
827 | ; | |
828 | ||
829 | /* skip until a shell break character */ | |
830 | e = skip_to_delim (rl_line_buffer, s, "()<>;&| \t\n"); | |
831 | ||
832 | name = substring (rl_line_buffer, s, e); | |
833 | ||
834 | return (name); | |
835 | } | |
836 | ||
837 | static char * | |
838 | prog_complete_return (text, matchnum) | |
f73dda09 | 839 | const char *text; |
bb70624e JA |
840 | int matchnum; |
841 | { | |
842 | static int ind; | |
843 | ||
844 | if (matchnum == 0) | |
845 | ind = 0; | |
846 | ||
847 | if (prog_complete_matches == 0 || prog_complete_matches[ind] == 0) | |
848 | return (char *)NULL; | |
849 | return (prog_complete_matches[ind++]); | |
850 | } | |
851 | ||
852 | #endif /* PROGRAMMABLE_COMPLETION */ | |
853 | ||
726f6388 JA |
854 | /* Do some completion on TEXT. The indices of TEXT in RL_LINE_BUFFER are |
855 | at START and END. Return an array of matches, or NULL if none. */ | |
856 | static char ** | |
857 | attempt_shell_completion (text, start, end) | |
28ef6c31 | 858 | const char *text; |
726f6388 JA |
859 | int start, end; |
860 | { | |
bb70624e | 861 | int in_command_position, ti, saveti, qc; |
ccc6cda3 | 862 | char **matches, *command_separator_chars; |
726f6388 | 863 | |
bb70624e | 864 | command_separator_chars = COMMAND_SEPARATORS; |
ccc6cda3 | 865 | matches = (char **)NULL; |
28ef6c31 | 866 | rl_ignore_some_completions_function = filename_completion_ignore; |
726f6388 JA |
867 | |
868 | /* Determine if this could be a command word. It is if it appears at | |
869 | the start of the line (ignoring preceding whitespace), or if it | |
870 | appears after a character that separates commands. It cannot be a | |
871 | command word if we aren't at the top-level prompt. */ | |
872 | ti = start - 1; | |
bb70624e | 873 | saveti = qc = -1; |
726f6388 JA |
874 | |
875 | while ((ti > -1) && (whitespace (rl_line_buffer[ti]))) | |
876 | ti--; | |
877 | ||
bb70624e JA |
878 | #if 1 |
879 | /* If this is an open quote, maybe we're trying to complete a quoted | |
880 | command name. */ | |
881 | if (rl_line_buffer[ti] == '"' || rl_line_buffer[ti] == '\'') | |
882 | { | |
883 | qc = rl_line_buffer[ti]; | |
884 | saveti = ti--; | |
885 | while (ti > -1 && (whitespace (rl_line_buffer[ti]))) | |
28ef6c31 | 886 | ti--; |
bb70624e JA |
887 | } |
888 | #endif | |
889 | ||
726f6388 JA |
890 | in_command_position = 0; |
891 | if (ti < 0) | |
892 | { | |
893 | /* Only do command completion at the start of a line when we | |
28ef6c31 | 894 | are prompting at the top level. */ |
726f6388 JA |
895 | if (current_prompt_string == ps1_prompt) |
896 | in_command_position++; | |
897 | } | |
898 | else if (member (rl_line_buffer[ti], command_separator_chars)) | |
899 | { | |
726f6388 JA |
900 | in_command_position++; |
901 | ||
bb70624e | 902 | if (check_redir (ti) == 1) |
28ef6c31 | 903 | in_command_position = 0; |
726f6388 JA |
904 | } |
905 | else | |
906 | { | |
907 | /* This still could be in command position. It is possible | |
908 | that all of the previous words on the line are variable | |
909 | assignments. */ | |
910 | } | |
911 | ||
d166f048 JA |
912 | /* Check that we haven't incorrectly flagged a closed command substitution |
913 | as indicating we're in a command position. */ | |
e8ce775d | 914 | if (in_command_position && ti >= 0 && rl_line_buffer[ti] == '`' && |
bb70624e | 915 | *text != '`' && unclosed_pair (rl_line_buffer, end, "`") == 0) |
d166f048 JA |
916 | in_command_position = 0; |
917 | ||
918 | /* Special handling for command substitution. If *TEXT is a backquote, | |
919 | it can be the start or end of an old-style command substitution, or | |
920 | unmatched. If it's unmatched, both calls to unclosed_pair will | |
921 | succeed. */ | |
bb70624e JA |
922 | if (*text == '`' && |
923 | (in_command_position || (unclosed_pair (rl_line_buffer, start, "`") && | |
924 | unclosed_pair (rl_line_buffer, end, "`")))) | |
28ef6c31 | 925 | matches = rl_completion_matches (text, command_subst_completion_function); |
726f6388 | 926 | |
bb70624e JA |
927 | #if defined (PROGRAMMABLE_COMPLETION) |
928 | /* Attempt programmable completion. */ | |
929 | if (!matches && in_command_position == 0 && prog_completion_enabled && | |
930 | (num_progcomps () > 0) && current_prompt_string == ps1_prompt) | |
931 | { | |
932 | int s, e, foundcs; | |
933 | char *n; | |
934 | ||
935 | /* XXX - don't free the members */ | |
936 | if (prog_complete_matches) | |
937 | free (prog_complete_matches); | |
938 | prog_complete_matches = (char **)NULL; | |
939 | ||
940 | s = find_cmd_start (start); | |
941 | e = find_cmd_end (end); | |
942 | n = find_cmd_name (s); | |
f73dda09 JA |
943 | if (e > s) |
944 | prog_complete_matches = programmable_completions (n, text, s, e, &foundcs); | |
945 | else | |
946 | foundcs = 0; | |
bb70624e JA |
947 | FREE (n); |
948 | /* XXX - if we found a COMPSPEC for the command, just return whatever | |
949 | the programmable completion code returns, and disable the default | |
28ef6c31 JA |
950 | filename completion that readline will do unless the COPT_DEFAULT |
951 | option has been set with the `-o default' option to complete. */ | |
bb70624e JA |
952 | if (foundcs) |
953 | { | |
28ef6c31 | 954 | /* If the user specified that the compspec returns filenames, make |
f73dda09 | 955 | sure that readline knows it. */ |
28ef6c31 JA |
956 | if (foundcs & COPT_FILENAMES) |
957 | rl_filename_completion_desired = 1; | |
bb70624e JA |
958 | /* Turn what the programmable completion code returns into what |
959 | readline wants. I should have made compute_lcd_of_matches | |
960 | external... */ | |
28ef6c31 JA |
961 | matches = rl_completion_matches (text, prog_complete_return); |
962 | if ((foundcs & COPT_DEFAULT) == 0) | |
963 | rl_attempted_completion_over = 1; /* no default */ | |
bb70624e JA |
964 | return (matches); |
965 | } | |
966 | } | |
967 | #endif | |
968 | ||
969 | /* New posix-style command substitution or variable name? */ | |
726f6388 | 970 | if (!matches && *text == '$') |
bb70624e JA |
971 | { |
972 | if (qc != '\'' && text[1] == '(') /* ) */ | |
28ef6c31 | 973 | matches = rl_completion_matches (text, command_subst_completion_function); |
bb70624e | 974 | else |
28ef6c31 | 975 | matches = rl_completion_matches (text, variable_completion_function); |
bb70624e | 976 | } |
726f6388 JA |
977 | |
978 | /* If the word starts in `~', and there is no slash in the word, then | |
979 | try completing this word as a username. */ | |
980 | if (!matches && *text == '~' && !strchr (text, '/')) | |
28ef6c31 | 981 | matches = rl_completion_matches (text, rl_username_completion_function); |
726f6388 JA |
982 | |
983 | /* Another one. Why not? If the word starts in '@', then look through | |
984 | the world of known hostnames for completion first. */ | |
ccc6cda3 | 985 | if (!matches && perform_hostname_completion && *text == '@') |
28ef6c31 | 986 | matches = rl_completion_matches (text, hostname_completion_function); |
726f6388 JA |
987 | |
988 | /* And last, (but not least) if this word is in a command position, then | |
989 | complete over possible command names, including aliases, functions, | |
990 | and command names. */ | |
991 | if (!matches && in_command_position) | |
992 | { | |
bb70624e JA |
993 | if (start == 0 && end == 0 && text[0] == '\0' && no_empty_command_completion) |
994 | { | |
995 | matches = (char **)NULL; | |
28ef6c31 | 996 | rl_ignore_some_completions_function = bash_ignore_everything; |
bb70624e JA |
997 | } |
998 | else | |
999 | { | |
28ef6c31 | 1000 | matches = rl_completion_matches (text, command_word_completion_function); |
bb70624e JA |
1001 | /* If we are attempting command completion and nothing matches, we |
1002 | do not want readline to perform filename completion for us. We | |
1003 | still want to be able to complete partial pathnames, so set the | |
1004 | completion ignore function to something which will remove | |
1005 | filenames and leave directories in the match list. */ | |
1006 | if (matches == (char **)NULL) | |
28ef6c31 | 1007 | rl_ignore_some_completions_function = bash_ignore_filenames; |
bb70624e | 1008 | } |
726f6388 JA |
1009 | } |
1010 | ||
ccc6cda3 JA |
1011 | /* This could be a globbing pattern, so try to expand it using pathname |
1012 | expansion. */ | |
f73dda09 | 1013 | if (!matches && glob_pattern_p (text)) |
e8ce775d | 1014 | { |
28ef6c31 | 1015 | matches = rl_completion_matches (text, glob_complete_word); |
e8ce775d JA |
1016 | /* A glob expression that matches more than one filename is problematic. |
1017 | If we match more than one filename, punt. */ | |
1018 | if (matches && matches[1]) | |
1019 | { | |
1020 | free_array (matches); | |
1021 | matches = (char **)0; | |
1022 | } | |
1023 | } | |
ccc6cda3 | 1024 | |
726f6388 JA |
1025 | return (matches); |
1026 | } | |
1027 | ||
1028 | /* This is the function to call when the word to complete is in a position | |
1029 | where a command word can be found. It grovels $PATH, looking for commands | |
1030 | that match. It also scans aliases, function names, and the shell_builtin | |
1031 | table. */ | |
bb70624e | 1032 | char * |
726f6388 | 1033 | command_word_completion_function (hint_text, state) |
28ef6c31 | 1034 | const char *hint_text; |
726f6388 JA |
1035 | int state; |
1036 | { | |
1037 | static char *hint = (char *)NULL; | |
1038 | static char *path = (char *)NULL; | |
1039 | static char *val = (char *)NULL; | |
1040 | static char *filename_hint = (char *)NULL; | |
1041 | static int path_index, hint_len, istate; | |
1042 | static int mapping_over, local_index; | |
1043 | static SHELL_VAR **varlist = (SHELL_VAR **)NULL; | |
1044 | #if defined (ALIAS) | |
ccc6cda3 | 1045 | static alias_t **alias_list = (alias_t **)NULL; |
726f6388 JA |
1046 | #endif /* ALIAS */ |
1047 | ||
1048 | /* We have to map over the possibilities for command words. If we have | |
1049 | no state, then make one just for that purpose. */ | |
726f6388 JA |
1050 | if (!state) |
1051 | { | |
1052 | if (hint) | |
1053 | free (hint); | |
1054 | ||
1055 | mapping_over = 0; | |
1056 | val = (char *)NULL; | |
1057 | ||
1058 | /* If this is an absolute program name, do not check it against | |
1059 | aliases, reserved words, functions or builtins. We must check | |
1060 | whether or not it is unique, and, if so, whether that filename | |
1061 | is executable. */ | |
f73dda09 | 1062 | if (absolute_program (hint_text)) |
726f6388 JA |
1063 | { |
1064 | /* Perform tilde expansion on what's passed, so we don't end up | |
1065 | passing filenames with tildes directly to stat(). */ | |
1066 | if (*hint_text == '~') | |
f73dda09 | 1067 | hint = bash_tilde_expand (hint_text); |
726f6388 JA |
1068 | else |
1069 | hint = savestring (hint_text); | |
1070 | hint_len = strlen (hint); | |
1071 | ||
1072 | if (filename_hint) | |
1073 | free (filename_hint); | |
1074 | filename_hint = savestring (hint); | |
1075 | ||
1076 | mapping_over = 4; | |
1077 | istate = 0; | |
1078 | goto inner; | |
1079 | } | |
1080 | ||
1081 | hint = savestring (hint_text); | |
1082 | hint_len = strlen (hint); | |
1083 | ||
1084 | path = get_string_value ("PATH"); | |
1085 | path_index = 0; | |
1086 | ||
1087 | /* Initialize the variables for each type of command word. */ | |
1088 | local_index = 0; | |
1089 | ||
1090 | if (varlist) | |
1091 | free (varlist); | |
1092 | ||
1093 | varlist = all_visible_functions (); | |
1094 | ||
1095 | #if defined (ALIAS) | |
1096 | if (alias_list) | |
1097 | free (alias_list); | |
1098 | ||
1099 | alias_list = all_aliases (); | |
1100 | #endif /* ALIAS */ | |
1101 | } | |
1102 | ||
1103 | /* mapping_over says what we are currently hacking. Note that every case | |
1104 | in this list must fall through when there are no more possibilities. */ | |
1105 | ||
1106 | switch (mapping_over) | |
1107 | { | |
1108 | case 0: /* Aliases come first. */ | |
1109 | #if defined (ALIAS) | |
1110 | while (alias_list && alias_list[local_index]) | |
1111 | { | |
1112 | register char *alias; | |
1113 | ||
1114 | alias = alias_list[local_index++]->name; | |
1115 | ||
1116 | if (STREQN (alias, hint, hint_len)) | |
1117 | return (savestring (alias)); | |
1118 | } | |
1119 | #endif /* ALIAS */ | |
1120 | local_index = 0; | |
1121 | mapping_over++; | |
1122 | ||
1123 | case 1: /* Then shell reserved words. */ | |
1124 | { | |
1125 | while (word_token_alist[local_index].word) | |
1126 | { | |
1127 | register char *reserved_word; | |
1128 | ||
1129 | reserved_word = word_token_alist[local_index++].word; | |
1130 | ||
1131 | if (STREQN (reserved_word, hint, hint_len)) | |
1132 | return (savestring (reserved_word)); | |
1133 | } | |
1134 | local_index = 0; | |
1135 | mapping_over++; | |
1136 | } | |
1137 | ||
1138 | case 2: /* Then function names. */ | |
1139 | while (varlist && varlist[local_index]) | |
1140 | { | |
1141 | register char *varname; | |
1142 | ||
1143 | varname = varlist[local_index++]->name; | |
1144 | ||
1145 | if (STREQN (varname, hint, hint_len)) | |
1146 | return (savestring (varname)); | |
1147 | } | |
1148 | local_index = 0; | |
1149 | mapping_over++; | |
1150 | ||
1151 | case 3: /* Then shell builtins. */ | |
1152 | for (; local_index < num_shell_builtins; local_index++) | |
1153 | { | |
1154 | /* Ignore it if it doesn't have a function pointer or if it | |
1155 | is not currently enabled. */ | |
1156 | if (!shell_builtins[local_index].function || | |
1157 | (shell_builtins[local_index].flags & BUILTIN_ENABLED) == 0) | |
1158 | continue; | |
1159 | ||
1160 | if (STREQN (shell_builtins[local_index].name, hint, hint_len)) | |
1161 | { | |
1162 | int i = local_index++; | |
1163 | ||
1164 | return (savestring (shell_builtins[i].name)); | |
1165 | } | |
1166 | } | |
1167 | local_index = 0; | |
1168 | mapping_over++; | |
1169 | } | |
1170 | ||
ccc6cda3 | 1171 | /* Repeatedly call filename_completion_function while we have |
726f6388 JA |
1172 | members of PATH left. Question: should we stat each file? |
1173 | Answer: we call executable_file () on each file. */ | |
1174 | outer: | |
1175 | ||
1176 | istate = (val != (char *)NULL); | |
1177 | ||
1178 | if (!istate) | |
1179 | { | |
1180 | char *current_path; | |
1181 | ||
1182 | /* Get the next directory from the path. If there is none, then we | |
1183 | are all done. */ | |
1184 | if (!path || !path[path_index] || | |
1185 | (current_path = extract_colon_unit (path, &path_index)) == 0) | |
1186 | return ((char *)NULL); | |
1187 | ||
1188 | if (*current_path == 0) | |
1189 | { | |
1190 | free (current_path); | |
1191 | current_path = savestring ("."); | |
1192 | } | |
1193 | ||
1194 | if (*current_path == '~') | |
1195 | { | |
1196 | char *t; | |
1197 | ||
ccc6cda3 | 1198 | t = bash_tilde_expand (current_path); |
726f6388 JA |
1199 | free (current_path); |
1200 | current_path = t; | |
1201 | } | |
1202 | ||
1203 | if (filename_hint) | |
1204 | free (filename_hint); | |
1205 | ||
f73dda09 | 1206 | filename_hint = (char *)xmalloc (2 + strlen (current_path) + hint_len); |
726f6388 JA |
1207 | sprintf (filename_hint, "%s/%s", current_path, hint); |
1208 | ||
1209 | free (current_path); | |
1210 | } | |
1211 | ||
1212 | inner: | |
28ef6c31 | 1213 | val = rl_filename_completion_function (filename_hint, istate); |
726f6388 JA |
1214 | istate = 1; |
1215 | ||
ccc6cda3 | 1216 | if (val == 0) |
726f6388 JA |
1217 | { |
1218 | /* If the hint text is an absolute program, then don't bother | |
1219 | searching through PATH. */ | |
1220 | if (absolute_program (hint)) | |
1221 | return ((char *)NULL); | |
1222 | ||
1223 | goto outer; | |
1224 | } | |
1225 | else | |
1226 | { | |
d166f048 | 1227 | int match, freetemp; |
726f6388 JA |
1228 | char *temp; |
1229 | ||
1230 | if (absolute_program (hint)) | |
1231 | { | |
1232 | match = strncmp (val, hint, hint_len) == 0; | |
1233 | /* If we performed tilde expansion, restore the original | |
1234 | filename. */ | |
1235 | if (*hint_text == '~') | |
1236 | { | |
1237 | int l, tl, vl; | |
1238 | vl = strlen (val); | |
1239 | tl = strlen (hint_text); | |
1240 | l = vl - hint_len; /* # of chars added */ | |
f73dda09 | 1241 | temp = (char *)xmalloc (l + 2 + tl); |
726f6388 JA |
1242 | strcpy (temp, hint_text); |
1243 | strcpy (temp + tl, val + vl - l); | |
1244 | } | |
1245 | else | |
1246 | temp = savestring (val); | |
d166f048 | 1247 | freetemp = 1; |
726f6388 JA |
1248 | } |
1249 | else | |
1250 | { | |
1251 | temp = strrchr (val, '/'); | |
1252 | ||
1253 | if (temp) | |
1254 | { | |
1255 | temp++; | |
d166f048 | 1256 | freetemp = match = strncmp (temp, hint, hint_len) == 0; |
726f6388 JA |
1257 | if (match) |
1258 | temp = savestring (temp); | |
1259 | } | |
1260 | else | |
d166f048 | 1261 | freetemp = match = 0; |
726f6388 JA |
1262 | } |
1263 | ||
ccc6cda3 JA |
1264 | /* If we have found a match, and it is an executable file or a |
1265 | directory name, return it. */ | |
28ef6c31 | 1266 | if (match && executable_or_directory (val)) |
726f6388 JA |
1267 | { |
1268 | free (val); | |
1269 | val = ""; /* So it won't be NULL. */ | |
1270 | return (temp); | |
1271 | } | |
1272 | else | |
1273 | { | |
d166f048 JA |
1274 | if (freetemp) |
1275 | free (temp); | |
726f6388 JA |
1276 | free (val); |
1277 | goto inner; | |
1278 | } | |
1279 | } | |
1280 | } | |
1281 | ||
d166f048 | 1282 | /* Completion inside an unterminated command substitution. */ |
726f6388 JA |
1283 | static char * |
1284 | command_subst_completion_function (text, state) | |
28ef6c31 | 1285 | const char *text; |
ccc6cda3 | 1286 | int state; |
726f6388 JA |
1287 | { |
1288 | static char **matches = (char **)NULL; | |
28ef6c31 JA |
1289 | static const char *orig_start; |
1290 | static char *filename_text = (char *)NULL; | |
726f6388 | 1291 | static int cmd_index, start_len; |
ccc6cda3 | 1292 | char *value; |
726f6388 JA |
1293 | |
1294 | if (state == 0) | |
1295 | { | |
1296 | if (filename_text) | |
1297 | free (filename_text); | |
1298 | orig_start = text; | |
1299 | if (*text == '`') | |
28ef6c31 | 1300 | text++; |
cce855bc | 1301 | else if (*text == '$' && text[1] == '(') /* ) */ |
28ef6c31 | 1302 | text += 2; |
726f6388 JA |
1303 | start_len = text - orig_start; |
1304 | filename_text = savestring (text); | |
1305 | if (matches) | |
1306 | free (matches); | |
28ef6c31 | 1307 | matches = rl_completion_matches (filename_text, command_word_completion_function); |
726f6388 JA |
1308 | cmd_index = 0; |
1309 | } | |
1310 | ||
1311 | if (!matches || !matches[cmd_index]) | |
1312 | { | |
1313 | rl_filename_quoting_desired = 0; /* disable quoting */ | |
1314 | return ((char *)NULL); | |
1315 | } | |
1316 | else | |
1317 | { | |
f73dda09 | 1318 | value = (char *)xmalloc (1 + start_len + strlen (matches[cmd_index])); |
726f6388 JA |
1319 | |
1320 | if (start_len == 1) | |
28ef6c31 | 1321 | value[0] = *orig_start; |
726f6388 | 1322 | else |
28ef6c31 | 1323 | strncpy (value, orig_start, start_len); |
726f6388 JA |
1324 | |
1325 | strcpy (value + start_len, matches[cmd_index]); | |
1326 | ||
1327 | cmd_index++; | |
1328 | return (value); | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | /* Okay, now we write the entry_function for variable completion. */ | |
1333 | static char * | |
1334 | variable_completion_function (text, state) | |
28ef6c31 | 1335 | const char *text; |
726f6388 | 1336 | int state; |
726f6388 | 1337 | { |
bb70624e | 1338 | static char **varlist = (char **)NULL; |
726f6388 JA |
1339 | static int varlist_index; |
1340 | static char *varname = (char *)NULL; | |
1341 | static int namelen; | |
1342 | static int first_char, first_char_loc; | |
1343 | ||
1344 | if (!state) | |
1345 | { | |
1346 | if (varname) | |
1347 | free (varname); | |
1348 | ||
1349 | first_char_loc = 0; | |
1350 | first_char = text[0]; | |
1351 | ||
1352 | if (first_char == '$') | |
1353 | first_char_loc++; | |
1354 | ||
ccc6cda3 | 1355 | if (text[first_char_loc] == '{') |
28ef6c31 | 1356 | first_char_loc++; |
ccc6cda3 | 1357 | |
726f6388 JA |
1358 | varname = savestring (text + first_char_loc); |
1359 | ||
1360 | namelen = strlen (varname); | |
1361 | if (varlist) | |
bb70624e | 1362 | free_array (varlist); |
726f6388 | 1363 | |
bb70624e JA |
1364 | varlist = all_variables_matching_prefix (varname); |
1365 | varlist_index = 0; | |
726f6388 JA |
1366 | } |
1367 | ||
1368 | if (!varlist || !varlist[varlist_index]) | |
1369 | { | |
1370 | return ((char *)NULL); | |
1371 | } | |
1372 | else | |
1373 | { | |
f73dda09 JA |
1374 | char *value; |
1375 | ||
1376 | value = (char *)xmalloc (4 + strlen (varlist[varlist_index])); | |
726f6388 JA |
1377 | |
1378 | if (first_char_loc) | |
ccc6cda3 JA |
1379 | { |
1380 | value[0] = first_char; | |
1381 | if (first_char_loc == 2) | |
1382 | value[1] = '{'; | |
1383 | } | |
726f6388 | 1384 | |
bb70624e | 1385 | strcpy (value + first_char_loc, varlist[varlist_index]); |
ccc6cda3 | 1386 | if (first_char_loc == 2) |
28ef6c31 | 1387 | strcat (value, "}"); |
726f6388 JA |
1388 | |
1389 | varlist_index++; | |
1390 | return (value); | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | /* How about a completion function for hostnames? */ | |
1395 | static char * | |
1396 | hostname_completion_function (text, state) | |
28ef6c31 | 1397 | const char *text; |
726f6388 | 1398 | int state; |
726f6388 JA |
1399 | { |
1400 | static char **list = (char **)NULL; | |
1401 | static int list_index = 0; | |
1402 | static int first_char, first_char_loc; | |
1403 | ||
1404 | /* If we don't have any state, make some. */ | |
ccc6cda3 | 1405 | if (state == 0) |
726f6388 | 1406 | { |
ccc6cda3 | 1407 | FREE (list); |
726f6388 JA |
1408 | |
1409 | list = (char **)NULL; | |
1410 | ||
1411 | first_char_loc = 0; | |
1412 | first_char = *text; | |
1413 | ||
1414 | if (first_char == '@') | |
1415 | first_char_loc++; | |
1416 | ||
f73dda09 | 1417 | list = hostnames_matching ((char *)text+first_char_loc); |
726f6388 JA |
1418 | list_index = 0; |
1419 | } | |
1420 | ||
1421 | if (list && list[list_index]) | |
1422 | { | |
ccc6cda3 | 1423 | char *t; |
726f6388 | 1424 | |
f73dda09 | 1425 | t = (char *)xmalloc (2 + strlen (list[list_index])); |
726f6388 JA |
1426 | *t = first_char; |
1427 | strcpy (t + first_char_loc, list[list_index]); | |
1428 | list_index++; | |
1429 | return (t); | |
1430 | } | |
ccc6cda3 JA |
1431 | |
1432 | return ((char *)NULL); | |
726f6388 JA |
1433 | } |
1434 | ||
f73dda09 JA |
1435 | char * |
1436 | bash_groupname_completion_function (text, state) | |
1437 | const char *text; | |
1438 | int state; | |
1439 | { | |
1440 | #if defined (__WIN32__) || defined (__OPENNT) || !defined (HAVE_GRP_H) | |
1441 | return ((char *)NULL); | |
1442 | #else | |
1443 | static char *gname = (char *)NULL; | |
1444 | static struct group *grent; | |
1445 | static int gnamelen; | |
1446 | char *value; | |
1447 | ||
1448 | if (state == 0) | |
1449 | { | |
1450 | FREE (gname); | |
1451 | gname = savestring (text); | |
1452 | gnamelen = strlen (gname); | |
1453 | ||
1454 | setgrent (); | |
1455 | } | |
1456 | ||
1457 | while (grent = getgrent ()) | |
1458 | { | |
1459 | if (gnamelen == 0 || (STREQN (gname, grent->gr_name, gnamelen))) | |
1460 | break; | |
1461 | } | |
1462 | ||
1463 | if (grent == 0) | |
1464 | { | |
1465 | endgrent (); | |
1466 | return ((char *)NULL); | |
1467 | } | |
1468 | ||
1469 | value = savestring (grent->gr_name); | |
1470 | return (value); | |
1471 | #endif | |
1472 | } | |
1473 | ||
cce855bc JA |
1474 | /* Functions to perform history and alias expansions on the current line. */ |
1475 | ||
1476 | #if defined (BANG_HISTORY) | |
1477 | /* Perform history expansion on the current line. If no history expansion | |
1478 | is done, pre_process_line() returns what it was passed, so we need to | |
1479 | allocate a new line here. */ | |
726f6388 JA |
1480 | static char * |
1481 | history_expand_line_internal (line) | |
1482 | char *line; | |
1483 | { | |
1484 | char *new_line; | |
1485 | ||
1486 | new_line = pre_process_line (line, 0, 0); | |
d166f048 | 1487 | return (new_line == line) ? savestring (line) : new_line; |
726f6388 | 1488 | } |
726f6388 JA |
1489 | #endif |
1490 | ||
1491 | /* There was an error in expansion. Let the preprocessor print | |
1492 | the error here. */ | |
1493 | static void | |
1494 | cleanup_expansion_error () | |
1495 | { | |
1496 | char *to_free; | |
1497 | ||
1498 | fprintf (rl_outstream, "\r\n"); | |
1499 | to_free = pre_process_line (rl_line_buffer, 1, 0); | |
d166f048 JA |
1500 | if (to_free != rl_line_buffer) |
1501 | free (to_free); | |
726f6388 JA |
1502 | putc ('\r', rl_outstream); |
1503 | rl_forced_update_display (); | |
1504 | } | |
1505 | ||
1506 | /* If NEW_LINE differs from what is in the readline line buffer, add an | |
1507 | undo record to get from the readline line buffer contents to the new | |
1508 | line and make NEW_LINE the current readline line. */ | |
1509 | static void | |
1510 | maybe_make_readline_line (new_line) | |
1511 | char *new_line; | |
1512 | { | |
1513 | if (strcmp (new_line, rl_line_buffer) != 0) | |
1514 | { | |
1515 | rl_point = rl_end; | |
1516 | ||
1517 | rl_add_undo (UNDO_BEGIN, 0, 0, 0); | |
1518 | rl_delete_text (0, rl_point); | |
1519 | rl_point = rl_end = 0; | |
1520 | rl_insert_text (new_line); | |
1521 | rl_add_undo (UNDO_END, 0, 0, 0); | |
1522 | } | |
1523 | } | |
1524 | ||
1525 | /* Make NEW_LINE be the current readline line. This frees NEW_LINE. */ | |
1526 | static void | |
1527 | set_up_new_line (new_line) | |
1528 | char *new_line; | |
1529 | { | |
f73dda09 JA |
1530 | int old_point, at_end; |
1531 | ||
1532 | old_point = rl_point; | |
1533 | at_end = rl_point == rl_end; | |
726f6388 JA |
1534 | |
1535 | /* If the line was history and alias expanded, then make that | |
1536 | be one thing to undo. */ | |
1537 | maybe_make_readline_line (new_line); | |
1538 | free (new_line); | |
1539 | ||
1540 | /* Place rl_point where we think it should go. */ | |
1541 | if (at_end) | |
1542 | rl_point = rl_end; | |
1543 | else if (old_point < rl_end) | |
1544 | { | |
1545 | rl_point = old_point; | |
1546 | if (!whitespace (rl_line_buffer[rl_point])) | |
b72432fd | 1547 | rl_forward_word (1, 0); |
726f6388 JA |
1548 | } |
1549 | } | |
1550 | ||
cce855bc JA |
1551 | #if defined (ALIAS) |
1552 | /* Expand aliases in the current readline line. */ | |
1553 | static int | |
28ef6c31 JA |
1554 | alias_expand_line (count, ignore) |
1555 | int count, ignore; | |
cce855bc JA |
1556 | { |
1557 | char *new_line; | |
1558 | ||
1559 | new_line = alias_expand (rl_line_buffer); | |
1560 | ||
1561 | if (new_line) | |
1562 | { | |
1563 | set_up_new_line (new_line); | |
1564 | return (0); | |
1565 | } | |
1566 | else | |
1567 | { | |
1568 | cleanup_expansion_error (); | |
1569 | return (1); | |
1570 | } | |
1571 | } | |
1572 | #endif | |
1573 | ||
1574 | #if defined (BANG_HISTORY) | |
726f6388 | 1575 | /* History expand the line. */ |
cce855bc | 1576 | static int |
28ef6c31 JA |
1577 | history_expand_line (count, ignore) |
1578 | int count, ignore; | |
726f6388 JA |
1579 | { |
1580 | char *new_line; | |
1581 | ||
1582 | new_line = history_expand_line_internal (rl_line_buffer); | |
1583 | ||
1584 | if (new_line) | |
cce855bc JA |
1585 | { |
1586 | set_up_new_line (new_line); | |
1587 | return (0); | |
1588 | } | |
726f6388 | 1589 | else |
cce855bc JA |
1590 | { |
1591 | cleanup_expansion_error (); | |
1592 | return (1); | |
1593 | } | |
1594 | } | |
1595 | ||
1596 | /* Expand history substitutions in the current line and then insert a | |
28ef6c31 | 1597 | space (hopefully close to where we were before). */ |
cce855bc | 1598 | static int |
28ef6c31 JA |
1599 | tcsh_magic_space (count, ignore) |
1600 | int count, ignore; | |
cce855bc | 1601 | { |
28ef6c31 JA |
1602 | int dist_from_end, old_point; |
1603 | ||
1604 | old_point = rl_point; | |
1605 | dist_from_end = rl_end - rl_point; | |
1606 | if (history_expand_line (count, ignore) == 0) | |
cce855bc | 1607 | { |
28ef6c31 JA |
1608 | /* Try a simple heuristic from Stephen Gildea <gildea@intouchsys.com>. |
1609 | This works if all expansions were before rl_point or if no expansions | |
1610 | were performed. */ | |
1611 | rl_point = (old_point == 0) ? old_point : rl_end - dist_from_end; | |
cce855bc JA |
1612 | rl_insert (1, ' '); |
1613 | return (0); | |
1614 | } | |
1615 | else | |
1616 | return (1); | |
726f6388 | 1617 | } |
cce855bc | 1618 | #endif |
ccc6cda3 | 1619 | |
726f6388 | 1620 | /* History and alias expand the line. */ |
cce855bc | 1621 | static int |
28ef6c31 JA |
1622 | history_and_alias_expand_line (count, ignore) |
1623 | int count, ignore; | |
726f6388 JA |
1624 | { |
1625 | char *new_line; | |
1626 | ||
1627 | new_line = pre_process_line (rl_line_buffer, 0, 0); | |
d166f048 JA |
1628 | if (new_line == rl_line_buffer) |
1629 | new_line = savestring (new_line); | |
726f6388 JA |
1630 | |
1631 | #if defined (ALIAS) | |
1632 | if (new_line) | |
1633 | { | |
1634 | char *alias_line; | |
1635 | ||
1636 | alias_line = alias_expand (new_line); | |
1637 | free (new_line); | |
1638 | new_line = alias_line; | |
1639 | } | |
1640 | #endif /* ALIAS */ | |
1641 | ||
1642 | if (new_line) | |
cce855bc JA |
1643 | { |
1644 | set_up_new_line (new_line); | |
1645 | return (0); | |
1646 | } | |
726f6388 | 1647 | else |
cce855bc JA |
1648 | { |
1649 | cleanup_expansion_error (); | |
1650 | return (1); | |
1651 | } | |
726f6388 JA |
1652 | } |
1653 | ||
1654 | /* History and alias expand the line, then perform the shell word | |
cce855bc JA |
1655 | expansions by calling expand_string. This can't use set_up_new_line() |
1656 | because we want the variable expansions as a separate undo'able | |
1657 | set of operations. */ | |
28ef6c31 JA |
1658 | static int |
1659 | shell_expand_line (count, ignore) | |
1660 | int count, ignore; | |
726f6388 JA |
1661 | { |
1662 | char *new_line; | |
ccc6cda3 | 1663 | WORD_LIST *expanded_string; |
726f6388 JA |
1664 | |
1665 | new_line = pre_process_line (rl_line_buffer, 0, 0); | |
d166f048 JA |
1666 | if (new_line == rl_line_buffer) |
1667 | new_line = savestring (new_line); | |
726f6388 JA |
1668 | |
1669 | #if defined (ALIAS) | |
1670 | if (new_line) | |
1671 | { | |
1672 | char *alias_line; | |
1673 | ||
1674 | alias_line = alias_expand (new_line); | |
1675 | free (new_line); | |
1676 | new_line = alias_line; | |
1677 | } | |
1678 | #endif /* ALIAS */ | |
1679 | ||
1680 | if (new_line) | |
1681 | { | |
1682 | int old_point = rl_point; | |
1683 | int at_end = rl_point == rl_end; | |
1684 | ||
1685 | /* If the line was history and alias expanded, then make that | |
1686 | be one thing to undo. */ | |
1687 | maybe_make_readline_line (new_line); | |
1688 | free (new_line); | |
1689 | ||
1690 | /* If there is variable expansion to perform, do that as a separate | |
1691 | operation to be undone. */ | |
d166f048 JA |
1692 | new_line = savestring (rl_line_buffer); |
1693 | expanded_string = expand_string (new_line, 0); | |
1694 | FREE (new_line); | |
ccc6cda3 JA |
1695 | if (expanded_string == 0) |
1696 | { | |
f73dda09 | 1697 | new_line = (char *)xmalloc (1); |
ccc6cda3 JA |
1698 | new_line[0] = '\0'; |
1699 | } | |
1700 | else | |
1701 | { | |
1702 | new_line = string_list (expanded_string); | |
1703 | dispose_words (expanded_string); | |
1704 | } | |
726f6388 | 1705 | |
ccc6cda3 JA |
1706 | maybe_make_readline_line (new_line); |
1707 | free (new_line); | |
726f6388 | 1708 | |
ccc6cda3 JA |
1709 | /* Place rl_point where we think it should go. */ |
1710 | if (at_end) | |
1711 | rl_point = rl_end; | |
1712 | else if (old_point < rl_end) | |
1713 | { | |
1714 | rl_point = old_point; | |
1715 | if (!whitespace (rl_line_buffer[rl_point])) | |
b72432fd | 1716 | rl_forward_word (1, 0); |
ccc6cda3 | 1717 | } |
28ef6c31 | 1718 | return 0; |
726f6388 JA |
1719 | } |
1720 | else | |
28ef6c31 JA |
1721 | { |
1722 | cleanup_expansion_error (); | |
1723 | return 1; | |
1724 | } | |
726f6388 JA |
1725 | } |
1726 | ||
cce855bc JA |
1727 | /* Define NO_FORCE_FIGNORE if you want to match filenames that would |
1728 | otherwise be ignored if they are the only possible matches. */ | |
1729 | /* #define NO_FORCE_FIGNORE */ | |
1730 | ||
ccc6cda3 JA |
1731 | /* If FIGNORE is set, then don't match files with the given suffixes when |
1732 | completing filenames. If only one of the possibilities has an acceptable | |
726f6388 JA |
1733 | suffix, delete the others, else just return and let the completer |
1734 | signal an error. It is called by the completer when real | |
1735 | completions are done on filenames by the completer's internal | |
1736 | function, not for completion lists (M-?) and not on "other" | |
ccc6cda3 | 1737 | completion types, such as hostnames or commands. */ |
726f6388 | 1738 | |
ccc6cda3 | 1739 | static struct ignorevar fignore = |
726f6388 | 1740 | { |
ccc6cda3 JA |
1741 | "FIGNORE", |
1742 | (struct ign *)0, | |
1743 | 0, | |
1744 | (char *)0, | |
f73dda09 | 1745 | (sh_iv_item_func_t *) 0, |
ccc6cda3 | 1746 | }; |
726f6388 | 1747 | |
726f6388 | 1748 | static void |
ccc6cda3 | 1749 | _ignore_completion_names (names, name_func) |
726f6388 | 1750 | char **names; |
f73dda09 | 1751 | sh_ignore_func_t *name_func; |
726f6388 JA |
1752 | { |
1753 | char **newnames; | |
1754 | int idx, nidx; | |
cce855bc JA |
1755 | #ifdef NO_FORCE_FIGNORE |
1756 | char **oldnames; | |
1757 | int oidx; | |
1758 | #endif | |
726f6388 JA |
1759 | |
1760 | /* If there is only one completion, see if it is acceptable. If it is | |
1761 | not, free it up. In any case, short-circuit and return. This is a | |
1762 | special case because names[0] is not the prefix of the list of names | |
1763 | if there is only one completion; it is the completion itself. */ | |
1764 | if (names[1] == (char *)0) | |
1765 | { | |
cce855bc | 1766 | #ifndef NO_FORCE_FIGNORE |
726f6388 | 1767 | if ((*name_func) (names[0]) == 0) |
28ef6c31 JA |
1768 | { |
1769 | free (names[0]); | |
1770 | names[0] = (char *)NULL; | |
1771 | } | |
cce855bc | 1772 | #endif |
726f6388 JA |
1773 | return; |
1774 | } | |
1775 | ||
1776 | /* Allocate space for array to hold list of pointers to matching | |
1777 | filenames. The pointers are copied back to NAMES when done. */ | |
1778 | for (nidx = 1; names[nidx]; nidx++) | |
1779 | ; | |
bb70624e | 1780 | newnames = alloc_array (nidx + 1); |
cce855bc | 1781 | #ifdef NO_FORCE_FIGNORE |
bb70624e | 1782 | oldnames = alloc_array (nidx - 1); |
cce855bc JA |
1783 | oidx = 0; |
1784 | #endif | |
726f6388 JA |
1785 | |
1786 | newnames[0] = names[0]; | |
1787 | for (idx = nidx = 1; names[idx]; idx++) | |
1788 | { | |
1789 | if ((*name_func) (names[idx])) | |
1790 | newnames[nidx++] = names[idx]; | |
1791 | else | |
cce855bc | 1792 | #ifndef NO_FORCE_FIGNORE |
28ef6c31 | 1793 | free (names[idx]); |
cce855bc JA |
1794 | #else |
1795 | oldnames[oidx++] = names[idx]; | |
1796 | #endif | |
726f6388 JA |
1797 | } |
1798 | ||
1799 | newnames[nidx] = (char *)NULL; | |
1800 | ||
1801 | /* If none are acceptable then let the completer handle it. */ | |
1802 | if (nidx == 1) | |
1803 | { | |
cce855bc | 1804 | #ifndef NO_FORCE_FIGNORE |
726f6388 JA |
1805 | free (names[0]); |
1806 | names[0] = (char *)NULL; | |
cce855bc JA |
1807 | #else |
1808 | free (oldnames); | |
1809 | #endif | |
726f6388 JA |
1810 | free (newnames); |
1811 | return; | |
1812 | } | |
1813 | ||
cce855bc JA |
1814 | #ifdef NO_FORCE_FIGNORE |
1815 | while (oidx) | |
1816 | free (oldnames[--oidx]); | |
1817 | free (oldnames); | |
1818 | #endif | |
1819 | ||
726f6388 JA |
1820 | /* If only one is acceptable, copy it to names[0] and return. */ |
1821 | if (nidx == 2) | |
1822 | { | |
1823 | free (names[0]); | |
1824 | names[0] = newnames[1]; | |
1825 | names[1] = (char *)NULL; | |
1826 | free (newnames); | |
1827 | return; | |
1828 | } | |
ccc6cda3 | 1829 | |
726f6388 JA |
1830 | /* Copy the acceptable names back to NAMES, set the new array end, |
1831 | and return. */ | |
1832 | for (nidx = 1; newnames[nidx]; nidx++) | |
1833 | names[nidx] = newnames[nidx]; | |
1834 | names[nidx] = (char *)NULL; | |
ccc6cda3 JA |
1835 | free (newnames); |
1836 | } | |
1837 | ||
1838 | static int | |
1839 | name_is_acceptable (name) | |
f73dda09 | 1840 | const char *name; |
ccc6cda3 JA |
1841 | { |
1842 | struct ign *p; | |
1843 | int nlen; | |
1844 | ||
1845 | for (nlen = strlen (name), p = fignore.ignores; p->val; p++) | |
1846 | { | |
1847 | if (nlen > p->len && p->len > 0 && STREQ (p->val, &name[nlen - p->len])) | |
1848 | return (0); | |
1849 | } | |
1850 | ||
1851 | return (1); | |
726f6388 JA |
1852 | } |
1853 | ||
b72432fd JA |
1854 | #if 0 |
1855 | static int | |
1856 | ignore_dot_names (name) | |
1857 | char *name; | |
1858 | { | |
1859 | return (name[0] != '.'); | |
1860 | } | |
1861 | #endif | |
1862 | ||
28ef6c31 | 1863 | static int |
726f6388 JA |
1864 | filename_completion_ignore (names) |
1865 | char **names; | |
1866 | { | |
b72432fd JA |
1867 | #if 0 |
1868 | if (glob_dot_filenames == 0) | |
1869 | _ignore_completion_names (names, ignore_dot_names); | |
1870 | #endif | |
1871 | ||
ccc6cda3 | 1872 | setup_ignore_patterns (&fignore); |
726f6388 | 1873 | |
ccc6cda3 | 1874 | if (fignore.num_ignores == 0) |
28ef6c31 | 1875 | return 0; |
726f6388 | 1876 | |
ccc6cda3 | 1877 | _ignore_completion_names (names, name_is_acceptable); |
28ef6c31 JA |
1878 | |
1879 | return 0; | |
726f6388 JA |
1880 | } |
1881 | ||
1882 | /* Return 1 if NAME is a directory. */ | |
1883 | static int | |
1884 | test_for_directory (name) | |
f73dda09 | 1885 | const char *name; |
726f6388 JA |
1886 | { |
1887 | struct stat finfo; | |
1888 | char *fn; | |
1889 | ||
ccc6cda3 | 1890 | fn = bash_tilde_expand (name); |
726f6388 JA |
1891 | if (stat (fn, &finfo) != 0) |
1892 | { | |
1893 | free (fn); | |
1894 | return 0; | |
1895 | } | |
1896 | free (fn); | |
1897 | return (S_ISDIR (finfo.st_mode)); | |
1898 | } | |
1899 | ||
1900 | /* Remove files from NAMES, leaving directories. */ | |
28ef6c31 | 1901 | static int |
726f6388 JA |
1902 | bash_ignore_filenames (names) |
1903 | char **names; | |
1904 | { | |
ccc6cda3 | 1905 | _ignore_completion_names (names, test_for_directory); |
28ef6c31 | 1906 | return 0; |
726f6388 JA |
1907 | } |
1908 | ||
bb70624e JA |
1909 | static int |
1910 | return_zero (name) | |
f73dda09 | 1911 | const char *name; |
bb70624e JA |
1912 | { |
1913 | return 0; | |
1914 | } | |
1915 | ||
28ef6c31 | 1916 | static int |
bb70624e JA |
1917 | bash_ignore_everything (names) |
1918 | char **names; | |
1919 | { | |
1920 | _ignore_completion_names (names, return_zero); | |
28ef6c31 | 1921 | return 0; |
bb70624e JA |
1922 | } |
1923 | ||
726f6388 JA |
1924 | /* Handle symbolic link references and other directory name |
1925 | expansions while hacking completion. */ | |
1926 | static int | |
1927 | bash_directory_completion_hook (dirname) | |
1928 | char **dirname; | |
1929 | { | |
b72432fd | 1930 | char *local_dirname, *new_dirname, *t; |
bb70624e | 1931 | int return_value, should_expand_dirname; |
726f6388 JA |
1932 | WORD_LIST *wl; |
1933 | ||
bb70624e | 1934 | return_value = should_expand_dirname = 0; |
726f6388 | 1935 | local_dirname = *dirname; |
bb70624e JA |
1936 | |
1937 | #if 0 | |
1938 | should_expand_dirname = strchr (local_dirname, '$') || strchr (local_dirname, '`'); | |
1939 | #else | |
1940 | if (strchr (local_dirname, '$')) | |
1941 | should_expand_dirname = 1; | |
1942 | else | |
1943 | { | |
1944 | t = strchr (local_dirname, '`'); | |
1945 | if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0) | |
1946 | should_expand_dirname = 1; | |
1947 | } | |
1948 | #endif | |
1949 | ||
1950 | if (should_expand_dirname) | |
726f6388 | 1951 | { |
bb70624e | 1952 | new_dirname = savestring (local_dirname); |
b72432fd | 1953 | wl = expand_string (new_dirname, 0); |
726f6388 JA |
1954 | if (wl) |
1955 | { | |
1956 | *dirname = string_list (wl); | |
1957 | /* Tell the completer to replace the directory name only if we | |
1958 | actually expanded something. */ | |
1959 | return_value = STREQ (local_dirname, *dirname) == 0; | |
1960 | free (local_dirname); | |
b72432fd | 1961 | free (new_dirname); |
726f6388 JA |
1962 | dispose_words (wl); |
1963 | local_dirname = *dirname; | |
1964 | } | |
1965 | else | |
1966 | { | |
b72432fd | 1967 | free (new_dirname); |
726f6388 | 1968 | free (local_dirname); |
f73dda09 | 1969 | *dirname = (char *)xmalloc (1); |
ccc6cda3 | 1970 | **dirname = '\0'; |
726f6388 JA |
1971 | return 1; |
1972 | } | |
1973 | } | |
1974 | ||
1975 | if (!no_symbolic_links && (local_dirname[0] != '.' || local_dirname[1])) | |
1976 | { | |
1977 | char *temp1, *temp2; | |
1978 | int len1, len2; | |
1979 | ||
1980 | t = get_working_directory ("symlink-hook"); | |
1981 | temp1 = make_absolute (local_dirname, t); | |
1982 | free (t); | |
28ef6c31 | 1983 | temp2 = sh_canonpath (temp1, PATH_CHECKDOTDOT|PATH_CHECKEXISTS); |
ccc6cda3 JA |
1984 | /* If we can't canonicalize, bail. */ |
1985 | if (temp2 == 0) | |
1986 | { | |
1987 | free (temp1); | |
1988 | return 1; | |
1989 | } | |
726f6388 JA |
1990 | len1 = strlen (temp1); |
1991 | if (temp1[len1 - 1] == '/') | |
28ef6c31 | 1992 | { |
726f6388 | 1993 | len2 = strlen (temp2); |
f73dda09 | 1994 | temp2 = (char *)xrealloc (temp2, len2 + 2); |
28ef6c31 JA |
1995 | temp2[len2] = '/'; |
1996 | temp2[len2 + 1] = '\0'; | |
1997 | } | |
726f6388 JA |
1998 | free (local_dirname); |
1999 | *dirname = temp2; | |
2000 | free (temp1); | |
2001 | } | |
2002 | return (return_value); | |
2003 | } | |
2004 | ||
726f6388 | 2005 | static char **history_completion_array = (char **)NULL; |
ccc6cda3 JA |
2006 | static int harry_size; |
2007 | static int harry_len; | |
726f6388 JA |
2008 | |
2009 | static void | |
2010 | build_history_completion_array () | |
2011 | { | |
ccc6cda3 JA |
2012 | register int i, j; |
2013 | HIST_ENTRY **hlist; | |
2014 | char **tokens; | |
726f6388 JA |
2015 | |
2016 | /* First, clear out the current dynamic history completion list. */ | |
2017 | if (harry_size) | |
2018 | { | |
2019 | for (i = 0; history_completion_array[i]; i++) | |
2020 | free (history_completion_array[i]); | |
2021 | ||
2022 | free (history_completion_array); | |
2023 | ||
2024 | history_completion_array = (char **)NULL; | |
2025 | harry_size = 0; | |
2026 | harry_len = 0; | |
2027 | } | |
2028 | ||
2029 | /* Next, grovel each line of history, making each shell-sized token | |
2030 | a separate entry in the history_completion_array. */ | |
ccc6cda3 | 2031 | hlist = history_list (); |
726f6388 | 2032 | |
ccc6cda3 JA |
2033 | if (hlist) |
2034 | { | |
2035 | for (i = 0; hlist[i]; i++) | |
2036 | { | |
2037 | /* Separate each token, and place into an array. */ | |
2038 | tokens = history_tokenize (hlist[i]->line); | |
726f6388 | 2039 | |
ccc6cda3 JA |
2040 | for (j = 0; tokens && tokens[j]; j++) |
2041 | { | |
2042 | if (harry_len + 2 > harry_size) | |
2043 | { | |
2044 | harry_size += 10; | |
f73dda09 | 2045 | history_completion_array = (char **)xrealloc |
ccc6cda3 JA |
2046 | (history_completion_array, harry_size * sizeof (char *)); |
2047 | } | |
726f6388 | 2048 | |
ccc6cda3 JA |
2049 | history_completion_array[harry_len++] = tokens[j]; |
2050 | history_completion_array[harry_len] = (char *)NULL; | |
2051 | } | |
2052 | free (tokens); | |
2053 | } | |
726f6388 | 2054 | |
ccc6cda3 | 2055 | /* Sort the complete list of tokens. */ |
f73dda09 | 2056 | qsort (history_completion_array, harry_len, sizeof (char *), (QSFUNC *)qsort_string_compare); |
ccc6cda3 | 2057 | } |
726f6388 JA |
2058 | } |
2059 | ||
2060 | static char * | |
2061 | history_completion_generator (hint_text, state) | |
f73dda09 | 2062 | const char *hint_text; |
726f6388 JA |
2063 | int state; |
2064 | { | |
ccc6cda3 | 2065 | static int local_index, len; |
f73dda09 | 2066 | static const char *text; |
726f6388 JA |
2067 | |
2068 | /* If this is the first call to the generator, then initialize the | |
2069 | list of strings to complete over. */ | |
ccc6cda3 | 2070 | if (state == 0) |
726f6388 JA |
2071 | { |
2072 | local_index = 0; | |
2073 | build_history_completion_array (); | |
2074 | text = hint_text; | |
2075 | len = strlen (text); | |
2076 | } | |
2077 | ||
2078 | while (history_completion_array && history_completion_array[local_index]) | |
2079 | { | |
2080 | if (strncmp (text, history_completion_array[local_index++], len) == 0) | |
2081 | return (savestring (history_completion_array[local_index - 1])); | |
2082 | } | |
2083 | return ((char *)NULL); | |
2084 | } | |
2085 | ||
28ef6c31 | 2086 | static int |
726f6388 JA |
2087 | dynamic_complete_history (count, key) |
2088 | int count, key; | |
2089 | { | |
f73dda09 JA |
2090 | int r; |
2091 | ||
28ef6c31 JA |
2092 | rl_compentry_func_t *orig_func; |
2093 | rl_completion_func_t *orig_attempt_func; | |
726f6388 JA |
2094 | |
2095 | orig_func = rl_completion_entry_function; | |
2096 | orig_attempt_func = rl_attempted_completion_function; | |
28ef6c31 JA |
2097 | rl_completion_entry_function = history_completion_generator; |
2098 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; | |
726f6388 | 2099 | |
28ef6c31 | 2100 | if (rl_last_func == dynamic_complete_history) |
f73dda09 | 2101 | r = rl_complete_internal ('?'); |
726f6388 | 2102 | else |
f73dda09 | 2103 | r = rl_complete_internal (TAB); |
726f6388 JA |
2104 | |
2105 | rl_completion_entry_function = orig_func; | |
2106 | rl_attempted_completion_function = orig_attempt_func; | |
f73dda09 | 2107 | return r; |
726f6388 JA |
2108 | } |
2109 | ||
726f6388 | 2110 | #if defined (SPECIFIC_COMPLETION_FUNCTIONS) |
28ef6c31 | 2111 | static int |
726f6388 JA |
2112 | bash_complete_username (ignore, ignore2) |
2113 | int ignore, ignore2; | |
2114 | { | |
28ef6c31 | 2115 | return bash_complete_username_internal (TAB); |
726f6388 JA |
2116 | } |
2117 | ||
28ef6c31 | 2118 | static int |
726f6388 JA |
2119 | bash_possible_username_completions (ignore, ignore2) |
2120 | int ignore, ignore2; | |
2121 | { | |
28ef6c31 | 2122 | return bash_complete_username_internal ('?'); |
726f6388 JA |
2123 | } |
2124 | ||
28ef6c31 | 2125 | static int |
726f6388 JA |
2126 | bash_complete_username_internal (what_to_do) |
2127 | int what_to_do; | |
2128 | { | |
28ef6c31 | 2129 | return bash_specific_completion (what_to_do, rl_username_completion_function); |
726f6388 JA |
2130 | } |
2131 | ||
28ef6c31 | 2132 | static int |
726f6388 JA |
2133 | bash_complete_filename (ignore, ignore2) |
2134 | int ignore, ignore2; | |
2135 | { | |
28ef6c31 | 2136 | return bash_complete_filename_internal (TAB); |
726f6388 JA |
2137 | } |
2138 | ||
28ef6c31 | 2139 | static int |
726f6388 JA |
2140 | bash_possible_filename_completions (ignore, ignore2) |
2141 | int ignore, ignore2; | |
2142 | { | |
28ef6c31 | 2143 | return bash_complete_filename_internal ('?'); |
726f6388 JA |
2144 | } |
2145 | ||
28ef6c31 | 2146 | static int |
726f6388 JA |
2147 | bash_complete_filename_internal (what_to_do) |
2148 | int what_to_do; | |
2149 | { | |
28ef6c31 JA |
2150 | rl_compentry_func_t *orig_func; |
2151 | rl_completion_func_t *orig_attempt_func; | |
2152 | rl_icppfunc_t *orig_dir_func; | |
2153 | const char *orig_rl_completer_word_break_characters; | |
2154 | int r; | |
726f6388 JA |
2155 | |
2156 | orig_func = rl_completion_entry_function; | |
2157 | orig_attempt_func = rl_attempted_completion_function; | |
2158 | orig_dir_func = rl_directory_completion_hook; | |
2159 | orig_rl_completer_word_break_characters = rl_completer_word_break_characters; | |
28ef6c31 JA |
2160 | rl_completion_entry_function = rl_filename_completion_function; |
2161 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; | |
2162 | rl_directory_completion_hook = (rl_icppfunc_t *)NULL; | |
726f6388 JA |
2163 | rl_completer_word_break_characters = " \t\n\"\'"; |
2164 | ||
28ef6c31 | 2165 | r = rl_complete_internal (what_to_do); |
726f6388 JA |
2166 | |
2167 | rl_completion_entry_function = orig_func; | |
2168 | rl_attempted_completion_function = orig_attempt_func; | |
2169 | rl_directory_completion_hook = orig_dir_func; | |
2170 | rl_completer_word_break_characters = orig_rl_completer_word_break_characters; | |
28ef6c31 JA |
2171 | |
2172 | return r; | |
726f6388 JA |
2173 | } |
2174 | ||
28ef6c31 | 2175 | static int |
726f6388 JA |
2176 | bash_complete_hostname (ignore, ignore2) |
2177 | int ignore, ignore2; | |
2178 | { | |
28ef6c31 | 2179 | return bash_complete_hostname_internal (TAB); |
726f6388 JA |
2180 | } |
2181 | ||
28ef6c31 | 2182 | static int |
726f6388 JA |
2183 | bash_possible_hostname_completions (ignore, ignore2) |
2184 | int ignore, ignore2; | |
2185 | { | |
28ef6c31 | 2186 | return bash_complete_hostname_internal ('?'); |
726f6388 JA |
2187 | } |
2188 | ||
28ef6c31 | 2189 | static int |
726f6388 JA |
2190 | bash_complete_variable (ignore, ignore2) |
2191 | int ignore, ignore2; | |
2192 | { | |
28ef6c31 | 2193 | return bash_complete_variable_internal (TAB); |
726f6388 JA |
2194 | } |
2195 | ||
28ef6c31 | 2196 | static int |
726f6388 JA |
2197 | bash_possible_variable_completions (ignore, ignore2) |
2198 | int ignore, ignore2; | |
2199 | { | |
28ef6c31 | 2200 | return bash_complete_variable_internal ('?'); |
726f6388 JA |
2201 | } |
2202 | ||
28ef6c31 | 2203 | static int |
726f6388 JA |
2204 | bash_complete_command (ignore, ignore2) |
2205 | int ignore, ignore2; | |
2206 | { | |
28ef6c31 | 2207 | return bash_complete_command_internal (TAB); |
726f6388 JA |
2208 | } |
2209 | ||
28ef6c31 | 2210 | static int |
726f6388 JA |
2211 | bash_possible_command_completions (ignore, ignore2) |
2212 | int ignore, ignore2; | |
2213 | { | |
28ef6c31 | 2214 | return bash_complete_command_internal ('?'); |
726f6388 JA |
2215 | } |
2216 | ||
28ef6c31 | 2217 | static int |
726f6388 JA |
2218 | bash_complete_hostname_internal (what_to_do) |
2219 | int what_to_do; | |
2220 | { | |
28ef6c31 | 2221 | return bash_specific_completion (what_to_do, hostname_completion_function); |
726f6388 JA |
2222 | } |
2223 | ||
28ef6c31 | 2224 | static int |
726f6388 JA |
2225 | bash_complete_variable_internal (what_to_do) |
2226 | int what_to_do; | |
2227 | { | |
28ef6c31 | 2228 | return bash_specific_completion (what_to_do, variable_completion_function); |
726f6388 JA |
2229 | } |
2230 | ||
28ef6c31 | 2231 | static int |
726f6388 JA |
2232 | bash_complete_command_internal (what_to_do) |
2233 | int what_to_do; | |
2234 | { | |
28ef6c31 | 2235 | return bash_specific_completion (what_to_do, command_word_completion_function); |
726f6388 JA |
2236 | } |
2237 | ||
ccc6cda3 JA |
2238 | static char * |
2239 | glob_complete_word (text, state) | |
28ef6c31 | 2240 | const char *text; |
ccc6cda3 JA |
2241 | int state; |
2242 | { | |
2243 | static char **matches = (char **)NULL; | |
2244 | static int ind; | |
2245 | char *ret; | |
2246 | ||
2247 | if (state == 0) | |
2248 | { | |
e8ce775d | 2249 | rl_filename_completion_desired = 1; |
ccc6cda3 | 2250 | if (matches) |
28ef6c31 | 2251 | free (matches); |
ccc6cda3 JA |
2252 | matches = shell_glob_filename (text); |
2253 | if (GLOB_FAILED (matches)) | |
28ef6c31 | 2254 | matches = (char **)NULL; |
ccc6cda3 JA |
2255 | ind = 0; |
2256 | } | |
2257 | ||
2258 | ret = matches ? matches[ind] : (char *)NULL; | |
2259 | ind++; | |
2260 | return ret; | |
2261 | } | |
2262 | ||
28ef6c31 | 2263 | static int |
ccc6cda3 JA |
2264 | bash_glob_completion_internal (what_to_do) |
2265 | int what_to_do; | |
2266 | { | |
28ef6c31 | 2267 | return bash_specific_completion (what_to_do, glob_complete_word); |
ccc6cda3 JA |
2268 | } |
2269 | ||
28ef6c31 | 2270 | static int |
ccc6cda3 JA |
2271 | bash_glob_expand_word (count, key) |
2272 | int count, key; | |
2273 | { | |
28ef6c31 | 2274 | return bash_glob_completion_internal ('*'); |
ccc6cda3 JA |
2275 | } |
2276 | ||
28ef6c31 | 2277 | static int |
ccc6cda3 JA |
2278 | bash_glob_list_expansions (count, key) |
2279 | int count, key; | |
2280 | { | |
28ef6c31 | 2281 | return bash_glob_completion_internal ('?'); |
ccc6cda3 JA |
2282 | } |
2283 | ||
28ef6c31 | 2284 | static int |
726f6388 JA |
2285 | bash_specific_completion (what_to_do, generator) |
2286 | int what_to_do; | |
28ef6c31 | 2287 | rl_compentry_func_t *generator; |
726f6388 | 2288 | { |
28ef6c31 JA |
2289 | rl_compentry_func_t *orig_func; |
2290 | rl_completion_func_t *orig_attempt_func; | |
2291 | int r; | |
726f6388 JA |
2292 | |
2293 | orig_func = rl_completion_entry_function; | |
2294 | orig_attempt_func = rl_attempted_completion_function; | |
2295 | rl_completion_entry_function = generator; | |
28ef6c31 | 2296 | rl_attempted_completion_function = NULL; |
726f6388 | 2297 | |
28ef6c31 | 2298 | r = rl_complete_internal (what_to_do); |
726f6388 JA |
2299 | |
2300 | rl_completion_entry_function = orig_func; | |
2301 | rl_attempted_completion_function = orig_attempt_func; | |
28ef6c31 JA |
2302 | |
2303 | return r; | |
726f6388 JA |
2304 | } |
2305 | ||
2306 | #endif /* SPECIFIC_COMPLETION_FUNCTIONS */ | |
ccc6cda3 JA |
2307 | |
2308 | /* Filename quoting for completion. */ | |
bb70624e JA |
2309 | /* A function to strip unquoted quote characters (single quotes, double |
2310 | quotes, and backslashes). It allows single quotes to appear | |
2311 | within double quotes, and vice versa. It should be smarter. */ | |
ccc6cda3 JA |
2312 | static char * |
2313 | bash_dequote_filename (text, quote_char) | |
2314 | char *text; | |
28ef6c31 | 2315 | int quote_char; |
ccc6cda3 JA |
2316 | { |
2317 | char *ret, *p, *r; | |
2318 | int l, quoted; | |
2319 | ||
2320 | l = strlen (text); | |
f73dda09 | 2321 | ret = (char *)xmalloc (l + 1); |
ccc6cda3 JA |
2322 | for (quoted = quote_char, p = text, r = ret; p && *p; p++) |
2323 | { | |
2324 | /* Allow backslash-quoted characters to pass through unscathed. */ | |
2325 | if (*p == '\\') | |
2326 | { | |
2327 | *r++ = *++p; | |
2328 | if (*p == '\0') | |
2329 | break; | |
2330 | continue; | |
2331 | } | |
2332 | /* Close quote. */ | |
2333 | if (quoted && *p == quoted) | |
28ef6c31 JA |
2334 | { |
2335 | quoted = 0; | |
2336 | continue; | |
2337 | } | |
ccc6cda3 JA |
2338 | /* Open quote. */ |
2339 | if (quoted == 0 && (*p == '\'' || *p == '"')) | |
28ef6c31 JA |
2340 | { |
2341 | quoted = *p; | |
2342 | continue; | |
2343 | } | |
ccc6cda3 JA |
2344 | *r++ = *p; |
2345 | } | |
2346 | *r = '\0'; | |
2347 | return ret; | |
2348 | } | |
2349 | ||
d166f048 JA |
2350 | /* Quote characters that the readline completion code would treat as |
2351 | word break characters with backslashes. Pass backslash-quoted | |
2352 | characters through without examination. */ | |
2353 | static char * | |
2354 | quote_word_break_chars (text) | |
2355 | char *text; | |
2356 | { | |
2357 | char *ret, *r, *s; | |
2358 | int l; | |
2359 | ||
2360 | l = strlen (text); | |
f73dda09 | 2361 | ret = (char *)xmalloc ((2 * l) + 1); |
d166f048 JA |
2362 | for (s = text, r = ret; *s; s++) |
2363 | { | |
2364 | /* Pass backslash-quoted characters through, including the backslash. */ | |
2365 | if (*s == '\\') | |
2366 | { | |
2367 | *r++ = '\\'; | |
2368 | *r++ = *++s; | |
2369 | if (*s == '\0') | |
2370 | break; | |
2371 | continue; | |
2372 | } | |
2373 | /* OK, we have an unquoted character. Check its presence in | |
2374 | rl_completer_word_break_characters. */ | |
2375 | if (strchr (rl_completer_word_break_characters, *s)) | |
28ef6c31 | 2376 | *r++ = '\\'; |
d166f048 JA |
2377 | *r++ = *s; |
2378 | } | |
2379 | *r = '\0'; | |
2380 | return ret; | |
2381 | } | |
2382 | ||
2383 | /* Quote a filename using double quotes, single quotes, or backslashes | |
2384 | depending on the value of completion_quoting_style. If we're | |
2385 | completing using backslashes, we need to quote some additional | |
2386 | characters (those that readline treats as word breaks), so we call | |
2387 | quote_word_break_chars on the result. */ | |
ccc6cda3 JA |
2388 | static char * |
2389 | bash_quote_filename (s, rtype, qcp) | |
2390 | char *s; | |
2391 | int rtype; | |
2392 | char *qcp; | |
2393 | { | |
2394 | char *rtext, *mtext, *ret; | |
2395 | int rlen, cs; | |
2396 | ||
2397 | rtext = (char *)NULL; | |
2398 | ||
2399 | /* If RTYPE == MULT_MATCH, it means that there is | |
2400 | more than one match. In this case, we do not add | |
2401 | the closing quote or attempt to perform tilde | |
2402 | expansion. If RTYPE == SINGLE_MATCH, we try | |
2403 | to perform tilde expansion, because single and double | |
2404 | quotes inhibit tilde expansion by the shell. */ | |
2405 | ||
2406 | mtext = s; | |
2407 | if (mtext[0] == '~' && rtype == SINGLE_MATCH) | |
2408 | mtext = bash_tilde_expand (s); | |
2409 | ||
2410 | cs = completion_quoting_style; | |
2411 | /* Might need to modify the default completion style based on *qcp, | |
bb70624e JA |
2412 | since it's set to any user-provided opening quote. We also change |
2413 | to single-quoting if there is no user-provided opening quote and | |
2414 | the word being completed contains newlines, since those are not | |
2415 | quoted correctly using backslashes (a backslash-newline pair is | |
2416 | special to the shell parser). */ | |
2417 | if (*qcp == '\0' && cs == COMPLETE_BSQUOTE && strchr (mtext, '\n')) | |
2418 | cs = COMPLETE_SQUOTE; | |
2419 | else if (*qcp == '"') | |
ccc6cda3 JA |
2420 | cs = COMPLETE_DQUOTE; |
2421 | else if (*qcp == '\'') | |
2422 | cs = COMPLETE_SQUOTE; | |
2423 | #if defined (BANG_HISTORY) | |
2424 | else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE && | |
2425 | history_expansion_inhibited == 0 && strchr (mtext, '!')) | |
2426 | cs = COMPLETE_BSQUOTE; | |
d166f048 JA |
2427 | |
2428 | if (*qcp == '"' && history_expansion && cs == COMPLETE_DQUOTE && | |
28ef6c31 | 2429 | history_expansion_inhibited == 0 && strchr (mtext, '!')) |
d166f048 JA |
2430 | { |
2431 | cs = COMPLETE_BSQUOTE; | |
2432 | *qcp = '\0'; | |
2433 | } | |
ccc6cda3 JA |
2434 | #endif |
2435 | ||
2436 | switch (cs) | |
2437 | { | |
2438 | case COMPLETE_DQUOTE: | |
28ef6c31 | 2439 | rtext = sh_double_quote (mtext); |
ccc6cda3 JA |
2440 | break; |
2441 | case COMPLETE_SQUOTE: | |
28ef6c31 | 2442 | rtext = sh_single_quote (mtext); |
ccc6cda3 JA |
2443 | break; |
2444 | case COMPLETE_BSQUOTE: | |
28ef6c31 | 2445 | rtext = sh_backslash_quote (mtext); |
ccc6cda3 JA |
2446 | break; |
2447 | } | |
2448 | ||
2449 | if (mtext != s) | |
2450 | free (mtext); | |
2451 | ||
d166f048 JA |
2452 | /* We may need to quote additional characters: those that readline treats |
2453 | as word breaks that are not quoted by backslash_quote. */ | |
2454 | if (rtext && cs == COMPLETE_BSQUOTE) | |
2455 | { | |
2456 | mtext = quote_word_break_chars (rtext); | |
2457 | free (rtext); | |
2458 | rtext = mtext; | |
2459 | } | |
2460 | ||
ccc6cda3 JA |
2461 | /* Leave the opening quote intact. The readline completion code takes |
2462 | care of avoiding doubled opening quotes. */ | |
2463 | rlen = strlen (rtext); | |
f73dda09 | 2464 | ret = (char *)xmalloc (rlen + 1); |
ccc6cda3 JA |
2465 | strcpy (ret, rtext); |
2466 | ||
2467 | /* If there are multiple matches, cut off the closing quote. */ | |
2468 | if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE) | |
2469 | ret[rlen - 1] = '\0'; | |
2470 | free (rtext); | |
2471 | return ret; | |
2472 | } | |
2473 | ||
bb70624e JA |
2474 | /* Support for binding readline key sequences to Unix commands. */ |
2475 | static Keymap cmd_xmap; | |
2476 | ||
2477 | static int | |
2478 | bash_execute_unix_command (count, key) | |
2479 | int count; /* ignored */ | |
2480 | int key; | |
2481 | { | |
2482 | Keymap ckmap; /* current keymap */ | |
2483 | Keymap xkmap; /* unix command executing keymap */ | |
2484 | register int i; | |
2485 | char *cmd; | |
2486 | ||
2487 | /* First, we need to find the right command to execute. This is tricky, | |
2488 | because we might have already indirected into another keymap. */ | |
2489 | ckmap = rl_get_keymap (); | |
2490 | if (ckmap != rl_executing_keymap) | |
2491 | { | |
2492 | /* bogus. we have to search. only handle one level of indirection. */ | |
2493 | for (i = 0; i < KEYMAP_SIZE; i++) | |
2494 | { | |
2495 | if (ckmap[i].type == ISKMAP && (Keymap)ckmap[i].function == rl_executing_keymap) | |
2496 | break; | |
2497 | } | |
2498 | if (i < KEYMAP_SIZE) | |
28ef6c31 | 2499 | xkmap = (Keymap)cmd_xmap[i].function; |
bb70624e JA |
2500 | else |
2501 | { | |
28ef6c31 JA |
2502 | rl_crlf (); |
2503 | internal_error ("bash_execute_unix_command: cannot find keymap for command"); | |
2504 | rl_forced_update_display (); | |
2505 | return 1; | |
bb70624e JA |
2506 | } |
2507 | } | |
2508 | else | |
2509 | xkmap = cmd_xmap; | |
2510 | ||
2511 | cmd = (char *)xkmap[key].function; | |
2512 | ||
2513 | if (cmd == 0) | |
2514 | { | |
28ef6c31 | 2515 | rl_ding (); |
bb70624e JA |
2516 | return 1; |
2517 | } | |
2518 | ||
28ef6c31 | 2519 | rl_crlf (); /* move to a new line */ |
bb70624e JA |
2520 | |
2521 | cmd = savestring (cmd); | |
2522 | parse_and_execute (cmd, "bash_execute_unix_command", 0); | |
2523 | ||
2524 | /* and restore the readline buffer and display after command execution. */ | |
2525 | rl_forced_update_display (); | |
2526 | return 0; | |
2527 | } | |
2528 | ||
2529 | static void | |
2530 | init_unix_command_map () | |
2531 | { | |
2532 | cmd_xmap = rl_make_bare_keymap (); | |
2533 | } | |
2534 | ||
2535 | static int | |
2536 | isolate_sequence (string, ind, need_dquote, startp) | |
2537 | char *string; | |
2538 | int ind, need_dquote, *startp; | |
2539 | { | |
2540 | register int i; | |
2541 | int c, passc, delim; | |
2542 | ||
2543 | for (i = ind; string[i] && whitespace (string[i]); i++) | |
2544 | ; | |
2545 | /* NEED_DQUOTE means that the first non-white character *must* be `"'. */ | |
2546 | if (need_dquote && string[i] != '"') | |
2547 | { | |
2548 | builtin_error ("%s: first non-whitespace character is not `\"'", string); | |
2549 | return -1; | |
2550 | } | |
2551 | ||
2552 | /* We can have delimited strings even if NEED_DQUOTE == 0, like the command | |
2553 | string to bind the key sequence to. */ | |
2554 | delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0; | |
2555 | ||
2556 | if (startp) | |
2557 | *startp = delim ? ++i : i; | |
2558 | ||
2559 | for (passc = 0; c = string[i]; i++) | |
2560 | { | |
2561 | if (passc) | |
2562 | { | |
2563 | passc = 0; | |
2564 | continue; | |
2565 | } | |
2566 | if (c == '\\') | |
2567 | { | |
2568 | passc++; | |
2569 | continue; | |
2570 | } | |
2571 | if (c == delim) | |
28ef6c31 | 2572 | break; |
bb70624e JA |
2573 | } |
2574 | ||
2575 | if (delim && string[i] != delim) | |
2576 | { | |
2577 | builtin_error ("%s: no closing `%c'", string, delim); | |
2578 | return -1; | |
2579 | } | |
2580 | ||
2581 | return i; | |
2582 | } | |
2583 | ||
2584 | int | |
2585 | bind_keyseq_to_unix_command (line) | |
2586 | char *line; | |
2587 | { | |
2588 | Keymap kmap; | |
2589 | char *kseq, *value; | |
f73dda09 | 2590 | int i, kstart; |
bb70624e JA |
2591 | |
2592 | if (cmd_xmap == 0) | |
2593 | init_unix_command_map (); | |
2594 | ||
2595 | kmap = rl_get_keymap (); | |
2596 | ||
2597 | /* We duplicate some of the work done by rl_parse_and_bind here, but | |
2598 | this code only has to handle `"keyseq": ["]command["]' and can | |
2599 | generate an error for anything else. */ | |
2600 | i = isolate_sequence (line, 0, 1, &kstart); | |
2601 | if (i < 0) | |
2602 | return -1; | |
2603 | ||
2604 | /* Create the key sequence string to pass to rl_generic_bind */ | |
2605 | kseq = substring (line, kstart, i); | |
2606 | ||
2607 | for ( ; line[i] && line[i] != ':'; i++) | |
2608 | ; | |
2609 | if (line[i] != ':') | |
2610 | { | |
2611 | builtin_error ("%s: missing colon separator", line); | |
2612 | return -1; | |
2613 | } | |
2614 | ||
2615 | i = isolate_sequence (line, i + 1, 0, &kstart); | |
2616 | if (i < 0) | |
2617 | return -1; | |
2618 | ||
2619 | /* Create the value string containing the command to execute. */ | |
2620 | value = substring (line, kstart, i); | |
2621 | ||
2622 | /* Save the command to execute and the key sequence in the CMD_XMAP */ | |
2623 | rl_generic_bind (ISMACR, kseq, value, cmd_xmap); | |
2624 | ||
2625 | /* and bind the key sequence in the current keymap to a function that | |
2626 | understands how to execute from CMD_XMAP */ | |
28ef6c31 | 2627 | rl_set_key (kseq, bash_execute_unix_command, kmap); |
bb70624e JA |
2628 | |
2629 | return 0; | |
2630 | } | |
2631 | ||
2632 | /* Used by the programmable completion code. Complete TEXT as a filename, | |
2633 | but return only directories as matches. Dequotes the filename before | |
2634 | attempting to find matches. */ | |
2635 | char ** | |
2636 | bash_directory_completion_matches (text) | |
28ef6c31 | 2637 | const char *text; |
bb70624e JA |
2638 | { |
2639 | char **m1; | |
2640 | char *dfn; | |
2641 | int qc; | |
2642 | ||
2643 | qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0; | |
28ef6c31 JA |
2644 | dfn = bash_dequote_filename ((char *)text, qc); |
2645 | m1 = rl_completion_matches (dfn, rl_filename_completion_function); | |
bb70624e JA |
2646 | free (dfn); |
2647 | ||
2648 | if (m1 == 0 || m1[0] == 0) | |
2649 | return m1; | |
2650 | /* We don't bother recomputing the lcd of the matches, because it will just | |
2651 | get thrown away by the programmable completion code and recomputed | |
2652 | later. */ | |
2653 | (void)bash_ignore_filenames (m1); | |
2654 | return m1; | |
2655 | } | |
ccc6cda3 | 2656 | #endif /* READLINE */ |