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