1 | /* complete.c -- filename completion for readline. */ |
---|
2 | |
---|
3 | /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc. |
---|
4 | |
---|
5 | This file is part of the GNU Readline Library, a library for |
---|
6 | reading lines of text with interactive input and history editing. |
---|
7 | |
---|
8 | The GNU Readline Library is free software; you can redistribute it |
---|
9 | and/or modify it under the terms of the GNU General Public License |
---|
10 | as published by the Free Software Foundation; either version 2, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | The GNU Readline Library is distributed in the hope that it will be |
---|
14 | useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
---|
15 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | GNU General Public License for more details. |
---|
17 | |
---|
18 | The GNU General Public License is often shipped with GNU software, and |
---|
19 | is generally kept in a file called COPYING or LICENSE. If you do not |
---|
20 | have a copy of the license, write to the Free Software Foundation, |
---|
21 | 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
---|
22 | #define READLINE_LIBRARY |
---|
23 | |
---|
24 | #if defined (HAVE_CONFIG_H) |
---|
25 | # include <config.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <sys/types.h> |
---|
29 | #include <fcntl.h> |
---|
30 | #if defined (HAVE_SYS_FILE_H) |
---|
31 | #include <sys/file.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | #if defined (HAVE_UNISTD_H) |
---|
35 | # include <unistd.h> |
---|
36 | #endif /* HAVE_UNISTD_H */ |
---|
37 | |
---|
38 | #if defined (HAVE_STDLIB_H) |
---|
39 | # include <stdlib.h> |
---|
40 | #else |
---|
41 | # include "ansi_stdlib.h" |
---|
42 | #endif /* HAVE_STDLIB_H */ |
---|
43 | |
---|
44 | #include <stdio.h> |
---|
45 | |
---|
46 | #include <errno.h> |
---|
47 | #if !defined (errno) |
---|
48 | extern int errno; |
---|
49 | #endif /* !errno */ |
---|
50 | |
---|
51 | #include <pwd.h> |
---|
52 | #if !defined (HAVE_GETPW_DECLS) |
---|
53 | extern struct passwd *getpwent (); |
---|
54 | #endif /* USG && !HAVE_GETPW_DECLS */ |
---|
55 | |
---|
56 | /* ISC systems don't define getpwent() if _POSIX_SOURCE is defined. */ |
---|
57 | #if defined (isc386) && defined (_POSIX_SOURCE) |
---|
58 | # if defined (__STDC__) |
---|
59 | extern struct passwd *getpwent (void); |
---|
60 | # else |
---|
61 | extern struct passwd *getpwent (); |
---|
62 | # endif /* !__STDC__ */ |
---|
63 | #endif /* isc386 && _POSIX_SOURCE */ |
---|
64 | |
---|
65 | #include "posixdir.h" |
---|
66 | #include "posixstat.h" |
---|
67 | |
---|
68 | /* System-specific feature definitions and include files. */ |
---|
69 | #include "rldefs.h" |
---|
70 | |
---|
71 | /* Some standard library routines. */ |
---|
72 | #include "readline.h" |
---|
73 | #include "xmalloc.h" |
---|
74 | #include "rlprivate.h" |
---|
75 | |
---|
76 | #ifdef __STDC__ |
---|
77 | typedef int QSFUNC (const void *, const void *); |
---|
78 | #else |
---|
79 | typedef int QSFUNC (); |
---|
80 | #endif |
---|
81 | |
---|
82 | /* If non-zero, then this is the address of a function to call when |
---|
83 | completing a word would normally display the list of possible matches. |
---|
84 | This function is called instead of actually doing the display. |
---|
85 | It takes three arguments: (char **matches, int num_matches, int max_length) |
---|
86 | where MATCHES is the array of strings that matched, NUM_MATCHES is the |
---|
87 | number of strings in that array, and MAX_LENGTH is the length of the |
---|
88 | longest string in that array. */ |
---|
89 | VFunction *rl_completion_display_matches_hook = (VFunction *)NULL; |
---|
90 | |
---|
91 | /* Forward declarations for functions defined and used in this file. */ |
---|
92 | char *filename_completion_function __P((char *, int)); |
---|
93 | char **completion_matches __P((char *, CPFunction *)); |
---|
94 | |
---|
95 | #if defined (VISIBLE_STATS) |
---|
96 | # if !defined (X_OK) |
---|
97 | # define X_OK 1 |
---|
98 | # endif |
---|
99 | static int stat_char __P((char *)); |
---|
100 | #endif |
---|
101 | |
---|
102 | static char *rl_quote_filename __P((char *, int, char *)); |
---|
103 | static char *rl_strpbrk __P((char *, char *)); |
---|
104 | |
---|
105 | static char **remove_duplicate_matches __P((char **)); |
---|
106 | static void insert_match __P((char *, int, int, char *)); |
---|
107 | static int append_to_match __P((char *, int, int)); |
---|
108 | static void insert_all_matches __P((char **, int, char *)); |
---|
109 | static void display_matches __P((char **)); |
---|
110 | static int compute_lcd_of_matches __P((char **, int, char *)); |
---|
111 | |
---|
112 | /* **************************************************************** */ |
---|
113 | /* */ |
---|
114 | /* Completion matching, from readline's point of view. */ |
---|
115 | /* */ |
---|
116 | /* **************************************************************** */ |
---|
117 | |
---|
118 | /* Variables known only to the readline library. */ |
---|
119 | |
---|
120 | /* If non-zero, non-unique completions always show the list of matches. */ |
---|
121 | int _rl_complete_show_all = 0; |
---|
122 | |
---|
123 | /* If non-zero, completed directory names have a slash appended. */ |
---|
124 | int _rl_complete_mark_directories = 1; |
---|
125 | |
---|
126 | /* If non-zero, completions are printed horizontally in alphabetical order, |
---|
127 | like `ls -x'. */ |
---|
128 | int _rl_print_completions_horizontally; |
---|
129 | |
---|
130 | /* Non-zero means that case is not significant in filename completion. */ |
---|
131 | #if defined (__MSDOS__) && !defined (__DJGPP__) |
---|
132 | int _rl_completion_case_fold = 1; |
---|
133 | #else |
---|
134 | int _rl_completion_case_fold; |
---|
135 | #endif |
---|
136 | |
---|
137 | /* Global variables available to applications using readline. */ |
---|
138 | |
---|
139 | #if defined (VISIBLE_STATS) |
---|
140 | /* Non-zero means add an additional character to each filename displayed |
---|
141 | during listing completion iff rl_filename_completion_desired which helps |
---|
142 | to indicate the type of file being listed. */ |
---|
143 | int rl_visible_stats = 0; |
---|
144 | #endif /* VISIBLE_STATS */ |
---|
145 | |
---|
146 | /* If non-zero, then this is the address of a function to call when |
---|
147 | completing on a directory name. The function is called with |
---|
148 | the address of a string (the current directory name) as an arg. */ |
---|
149 | Function *rl_directory_completion_hook = (Function *)NULL; |
---|
150 | |
---|
151 | /* Non-zero means readline completion functions perform tilde expansion. */ |
---|
152 | int rl_complete_with_tilde_expansion = 0; |
---|
153 | |
---|
154 | /* Pointer to the generator function for completion_matches (). |
---|
155 | NULL means to use filename_completion_function (), the default filename |
---|
156 | completer. */ |
---|
157 | Function *rl_completion_entry_function = (Function *)NULL; |
---|
158 | |
---|
159 | /* Pointer to alternative function to create matches. |
---|
160 | Function is called with TEXT, START, and END. |
---|
161 | START and END are indices in RL_LINE_BUFFER saying what the boundaries |
---|
162 | of TEXT are. |
---|
163 | If this function exists and returns NULL then call the value of |
---|
164 | rl_completion_entry_function to try to match, otherwise use the |
---|
165 | array of strings returned. */ |
---|
166 | CPPFunction *rl_attempted_completion_function = (CPPFunction *)NULL; |
---|
167 | |
---|
168 | /* Non-zero means to suppress normal filename completion after the |
---|
169 | user-specified completion function has been called. */ |
---|
170 | int rl_attempted_completion_over = 0; |
---|
171 | |
---|
172 | /* Set to a character indicating the type of completion being performed |
---|
173 | by rl_complete_internal, available for use by application completion |
---|
174 | functions. */ |
---|
175 | int rl_completion_type = 0; |
---|
176 | |
---|
177 | /* Up to this many items will be displayed in response to a |
---|
178 | possible-completions call. After that, we ask the user if |
---|
179 | she is sure she wants to see them all. */ |
---|
180 | int rl_completion_query_items = 100; |
---|
181 | |
---|
182 | /* The basic list of characters that signal a break between words for the |
---|
183 | completer routine. The contents of this variable is what breaks words |
---|
184 | in the shell, i.e. " \t\n\"\\'`@$><=" */ |
---|
185 | char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; |
---|
186 | |
---|
187 | /* List of basic quoting characters. */ |
---|
188 | char *rl_basic_quote_characters = "\"'"; |
---|
189 | |
---|
190 | /* The list of characters that signal a break between words for |
---|
191 | rl_complete_internal. The default list is the contents of |
---|
192 | rl_basic_word_break_characters. */ |
---|
193 | char *rl_completer_word_break_characters = (char *)NULL; |
---|
194 | |
---|
195 | /* List of characters which can be used to quote a substring of the line. |
---|
196 | Completion occurs on the entire substring, and within the substring |
---|
197 | rl_completer_word_break_characters are treated as any other character, |
---|
198 | unless they also appear within this list. */ |
---|
199 | char *rl_completer_quote_characters = (char *)NULL; |
---|
200 | |
---|
201 | /* List of characters that should be quoted in filenames by the completer. */ |
---|
202 | char *rl_filename_quote_characters = (char *)NULL; |
---|
203 | |
---|
204 | /* List of characters that are word break characters, but should be left |
---|
205 | in TEXT when it is passed to the completion function. The shell uses |
---|
206 | this to help determine what kind of completing to do. */ |
---|
207 | char *rl_special_prefixes = (char *)NULL; |
---|
208 | |
---|
209 | /* If non-zero, then disallow duplicates in the matches. */ |
---|
210 | int rl_ignore_completion_duplicates = 1; |
---|
211 | |
---|
212 | /* Non-zero means that the results of the matches are to be treated |
---|
213 | as filenames. This is ALWAYS zero on entry, and can only be changed |
---|
214 | within a completion entry finder function. */ |
---|
215 | int rl_filename_completion_desired = 0; |
---|
216 | |
---|
217 | /* Non-zero means that the results of the matches are to be quoted using |
---|
218 | double quotes (or an application-specific quoting mechanism) if the |
---|
219 | filename contains any characters in rl_filename_quote_chars. This is |
---|
220 | ALWAYS non-zero on entry, and can only be changed within a completion |
---|
221 | entry finder function. */ |
---|
222 | int rl_filename_quoting_desired = 1; |
---|
223 | |
---|
224 | /* This function, if defined, is called by the completer when real |
---|
225 | filename completion is done, after all the matching names have been |
---|
226 | generated. It is passed a (char**) known as matches in the code below. |
---|
227 | It consists of a NULL-terminated array of pointers to potential |
---|
228 | matching strings. The 1st element (matches[0]) is the maximal |
---|
229 | substring that is common to all matches. This function can re-arrange |
---|
230 | the list of matches as required, but all elements of the array must be |
---|
231 | free()'d if they are deleted. The main intent of this function is |
---|
232 | to implement FIGNORE a la SunOS csh. */ |
---|
233 | Function *rl_ignore_some_completions_function = (Function *)NULL; |
---|
234 | |
---|
235 | /* Set to a function to quote a filename in an application-specific fashion. |
---|
236 | Called with the text to quote, the type of match found (single or multiple) |
---|
237 | and a pointer to the quoting character to be used, which the function can |
---|
238 | reset if desired. */ |
---|
239 | CPFunction *rl_filename_quoting_function = rl_quote_filename; |
---|
240 | |
---|
241 | /* Function to call to remove quoting characters from a filename. Called |
---|
242 | before completion is attempted, so the embedded quotes do not interfere |
---|
243 | with matching names in the file system. Readline doesn't do anything |
---|
244 | with this; it's set only by applications. */ |
---|
245 | CPFunction *rl_filename_dequoting_function = (CPFunction *)NULL; |
---|
246 | |
---|
247 | /* Function to call to decide whether or not a word break character is |
---|
248 | quoted. If a character is quoted, it does not break words for the |
---|
249 | completer. */ |
---|
250 | Function *rl_char_is_quoted_p = (Function *)NULL; |
---|
251 | |
---|
252 | /* Character appended to completed words when at the end of the line. The |
---|
253 | default is a space. */ |
---|
254 | int rl_completion_append_character = ' '; |
---|
255 | |
---|
256 | /* If non-zero, inhibit completion (temporarily). */ |
---|
257 | int rl_inhibit_completion; |
---|
258 | |
---|
259 | /* Variables local to this file. */ |
---|
260 | |
---|
261 | /* Local variable states what happened during the last completion attempt. */ |
---|
262 | static int completion_changed_buffer; |
---|
263 | |
---|
264 | /*************************************/ |
---|
265 | /* */ |
---|
266 | /* Bindable completion functions */ |
---|
267 | /* */ |
---|
268 | /*************************************/ |
---|
269 | |
---|
270 | /* Complete the word at or before point. You have supplied the function |
---|
271 | that does the initial simple matching selection algorithm (see |
---|
272 | completion_matches ()). The default is to do filename completion. */ |
---|
273 | int |
---|
274 | rl_complete (ignore, invoking_key) |
---|
275 | int ignore, invoking_key; |
---|
276 | { |
---|
277 | if (rl_inhibit_completion) |
---|
278 | return (rl_insert (ignore, invoking_key)); |
---|
279 | else if (rl_last_func == rl_complete && !completion_changed_buffer) |
---|
280 | return (rl_complete_internal ('?')); |
---|
281 | else if (_rl_complete_show_all) |
---|
282 | return (rl_complete_internal ('!')); |
---|
283 | else |
---|
284 | return (rl_complete_internal (TAB)); |
---|
285 | } |
---|
286 | |
---|
287 | /* List the possible completions. See description of rl_complete (). */ |
---|
288 | int |
---|
289 | rl_possible_completions (ignore, invoking_key) |
---|
290 | int ignore, invoking_key; |
---|
291 | { |
---|
292 | return (rl_complete_internal ('?')); |
---|
293 | } |
---|
294 | |
---|
295 | int |
---|
296 | rl_insert_completions (ignore, invoking_key) |
---|
297 | int ignore, invoking_key; |
---|
298 | { |
---|
299 | return (rl_complete_internal ('*')); |
---|
300 | } |
---|
301 | |
---|
302 | /************************************/ |
---|
303 | /* */ |
---|
304 | /* Completion utility functions */ |
---|
305 | /* */ |
---|
306 | /************************************/ |
---|
307 | |
---|
308 | /* Find the first occurrence in STRING1 of any character from STRING2. |
---|
309 | Return a pointer to the character in STRING1. */ |
---|
310 | static char * |
---|
311 | rl_strpbrk (string1, string2) |
---|
312 | char *string1, *string2; |
---|
313 | { |
---|
314 | register char *scan; |
---|
315 | |
---|
316 | for (; *string1; string1++) |
---|
317 | { |
---|
318 | for (scan = string2; *scan; scan++) |
---|
319 | { |
---|
320 | if (*string1 == *scan) |
---|
321 | { |
---|
322 | return (string1); |
---|
323 | } |
---|
324 | } |
---|
325 | } |
---|
326 | return ((char *)NULL); |
---|
327 | } |
---|
328 | |
---|
329 | /* The user must press "y" or "n". Non-zero return means "y" pressed. */ |
---|
330 | static int |
---|
331 | get_y_or_n () |
---|
332 | { |
---|
333 | int c; |
---|
334 | |
---|
335 | for (;;) |
---|
336 | { |
---|
337 | c = rl_read_key (); |
---|
338 | if (c == 'y' || c == 'Y' || c == ' ') |
---|
339 | return (1); |
---|
340 | if (c == 'n' || c == 'N' || c == RUBOUT) |
---|
341 | return (0); |
---|
342 | if (c == ABORT_CHAR) |
---|
343 | _rl_abort_internal (); |
---|
344 | ding (); |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | #if defined (VISIBLE_STATS) |
---|
349 | /* Return the character which best describes FILENAME. |
---|
350 | `@' for symbolic links |
---|
351 | `/' for directories |
---|
352 | `*' for executables |
---|
353 | `=' for sockets |
---|
354 | `|' for FIFOs |
---|
355 | `%' for character special devices |
---|
356 | `#' for block special devices */ |
---|
357 | static int |
---|
358 | stat_char (filename) |
---|
359 | char *filename; |
---|
360 | { |
---|
361 | struct stat finfo; |
---|
362 | int character, r; |
---|
363 | |
---|
364 | #if defined (HAVE_LSTAT) && defined (S_ISLNK) |
---|
365 | r = lstat (filename, &finfo); |
---|
366 | #else |
---|
367 | r = stat (filename, &finfo); |
---|
368 | #endif |
---|
369 | |
---|
370 | if (r == -1) |
---|
371 | return (0); |
---|
372 | |
---|
373 | character = 0; |
---|
374 | if (S_ISDIR (finfo.st_mode)) |
---|
375 | character = '/'; |
---|
376 | #if defined (S_ISCHR) |
---|
377 | else if (S_ISCHR (finfo.st_mode)) |
---|
378 | character = '%'; |
---|
379 | #endif /* S_ISCHR */ |
---|
380 | #if defined (S_ISBLK) |
---|
381 | else if (S_ISBLK (finfo.st_mode)) |
---|
382 | character = '#'; |
---|
383 | #endif /* S_ISBLK */ |
---|
384 | #if defined (S_ISLNK) |
---|
385 | else if (S_ISLNK (finfo.st_mode)) |
---|
386 | character = '@'; |
---|
387 | #endif /* S_ISLNK */ |
---|
388 | #if defined (S_ISSOCK) |
---|
389 | else if (S_ISSOCK (finfo.st_mode)) |
---|
390 | character = '='; |
---|
391 | #endif /* S_ISSOCK */ |
---|
392 | #if defined (S_ISFIFO) |
---|
393 | else if (S_ISFIFO (finfo.st_mode)) |
---|
394 | character = '|'; |
---|
395 | #endif |
---|
396 | else if (S_ISREG (finfo.st_mode)) |
---|
397 | { |
---|
398 | if (access (filename, X_OK) == 0) |
---|
399 | character = '*'; |
---|
400 | } |
---|
401 | return (character); |
---|
402 | } |
---|
403 | #endif /* VISIBLE_STATS */ |
---|
404 | |
---|
405 | /* Return the portion of PATHNAME that should be output when listing |
---|
406 | possible completions. If we are hacking filename completion, we |
---|
407 | are only interested in the basename, the portion following the |
---|
408 | final slash. Otherwise, we return what we were passed. */ |
---|
409 | static char * |
---|
410 | printable_part (pathname) |
---|
411 | char *pathname; |
---|
412 | { |
---|
413 | char *temp; |
---|
414 | |
---|
415 | temp = rl_filename_completion_desired ? strrchr (pathname, '/') : (char *)NULL; |
---|
416 | #if defined (__MSDOS__) |
---|
417 | if (rl_filename_completion_desired && temp == 0 && isalpha (pathname[0]) && pathname[1] == ':') |
---|
418 | temp = pathname + 1; |
---|
419 | #endif |
---|
420 | return (temp ? ++temp : pathname); |
---|
421 | } |
---|
422 | |
---|
423 | /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we |
---|
424 | are using it, check for and output a single character for `special' |
---|
425 | filenames. Return the number of characters we output. */ |
---|
426 | |
---|
427 | #define PUTX(c) \ |
---|
428 | do { \ |
---|
429 | if (CTRL_CHAR (c)) \ |
---|
430 | { \ |
---|
431 | putc ('^', rl_outstream); \ |
---|
432 | putc (UNCTRL (c), rl_outstream); \ |
---|
433 | printed_len += 2; \ |
---|
434 | } \ |
---|
435 | else if (c == RUBOUT) \ |
---|
436 | { \ |
---|
437 | putc ('^', rl_outstream); \ |
---|
438 | putc ('?', rl_outstream); \ |
---|
439 | printed_len += 2; \ |
---|
440 | } \ |
---|
441 | else \ |
---|
442 | { \ |
---|
443 | putc (c, rl_outstream); \ |
---|
444 | printed_len++; \ |
---|
445 | } \ |
---|
446 | } while (0) |
---|
447 | |
---|
448 | static int |
---|
449 | print_filename (to_print, full_pathname) |
---|
450 | char *to_print, *full_pathname; |
---|
451 | { |
---|
452 | int printed_len = 0; |
---|
453 | #if !defined (VISIBLE_STATS) |
---|
454 | char *s; |
---|
455 | |
---|
456 | for (s = to_print; *s; s++) |
---|
457 | { |
---|
458 | PUTX (*s); |
---|
459 | } |
---|
460 | #else |
---|
461 | char *s, c, *new_full_pathname; |
---|
462 | int extension_char, slen, tlen; |
---|
463 | |
---|
464 | for (s = to_print; *s; s++) |
---|
465 | { |
---|
466 | PUTX (*s); |
---|
467 | } |
---|
468 | |
---|
469 | if (rl_filename_completion_desired && rl_visible_stats) |
---|
470 | { |
---|
471 | /* If to_print != full_pathname, to_print is the basename of the |
---|
472 | path passed. In this case, we try to expand the directory |
---|
473 | name before checking for the stat character. */ |
---|
474 | if (to_print != full_pathname) |
---|
475 | { |
---|
476 | /* Terminate the directory name. */ |
---|
477 | c = to_print[-1]; |
---|
478 | to_print[-1] = '\0'; |
---|
479 | |
---|
480 | /* If setting the last slash in full_pathname to a NUL results in |
---|
481 | full_pathname being the empty string, we are trying to complete |
---|
482 | files in the root directory. If we pass a null string to the |
---|
483 | bash directory completion hook, for example, it will expand it |
---|
484 | to the current directory. We just want the `/'. */ |
---|
485 | s = tilde_expand (full_pathname && *full_pathname ? full_pathname : "/"); |
---|
486 | if (rl_directory_completion_hook) |
---|
487 | (*rl_directory_completion_hook) (&s); |
---|
488 | |
---|
489 | slen = strlen (s); |
---|
490 | tlen = strlen (to_print); |
---|
491 | new_full_pathname = xmalloc (slen + tlen + 2); |
---|
492 | strcpy (new_full_pathname, s); |
---|
493 | new_full_pathname[slen] = '/'; |
---|
494 | strcpy (new_full_pathname + slen + 1, to_print); |
---|
495 | |
---|
496 | extension_char = stat_char (new_full_pathname); |
---|
497 | |
---|
498 | free (new_full_pathname); |
---|
499 | to_print[-1] = c; |
---|
500 | } |
---|
501 | else |
---|
502 | { |
---|
503 | s = tilde_expand (full_pathname); |
---|
504 | extension_char = stat_char (s); |
---|
505 | } |
---|
506 | |
---|
507 | free (s); |
---|
508 | if (extension_char) |
---|
509 | { |
---|
510 | putc (extension_char, rl_outstream); |
---|
511 | printed_len++; |
---|
512 | } |
---|
513 | } |
---|
514 | #endif /* VISIBLE_STATS */ |
---|
515 | return printed_len; |
---|
516 | } |
---|
517 | |
---|
518 | static char * |
---|
519 | rl_quote_filename (s, rtype, qcp) |
---|
520 | char *s; |
---|
521 | int rtype; |
---|
522 | char *qcp; |
---|
523 | { |
---|
524 | char *r; |
---|
525 | |
---|
526 | r = xmalloc (strlen (s) + 2); |
---|
527 | *r = *rl_completer_quote_characters; |
---|
528 | strcpy (r + 1, s); |
---|
529 | if (qcp) |
---|
530 | *qcp = *rl_completer_quote_characters; |
---|
531 | return r; |
---|
532 | } |
---|
533 | |
---|
534 | /* Find the bounds of the current word for completion purposes, and leave |
---|
535 | rl_point set to the end of the word. This function skips quoted |
---|
536 | substrings (characters between matched pairs of characters in |
---|
537 | rl_completer_quote_characters. First we try to find an unclosed |
---|
538 | quoted substring on which to do matching. If one is not found, we use |
---|
539 | the word break characters to find the boundaries of the current word. |
---|
540 | We call an application-specific function to decide whether or not a |
---|
541 | particular word break character is quoted; if that function returns a |
---|
542 | non-zero result, the character does not break a word. This function |
---|
543 | returns the opening quote character if we found an unclosed quoted |
---|
544 | substring, '\0' otherwise. FP, if non-null, is set to a value saying |
---|
545 | which (shell-like) quote characters we found (single quote, double |
---|
546 | quote, or backslash) anywhere in the string. DP, if non-null, is set to |
---|
547 | the value of the delimiter character that caused a word break. */ |
---|
548 | |
---|
549 | static char |
---|
550 | find_completion_word (fp, dp) |
---|
551 | int *fp, *dp; |
---|
552 | { |
---|
553 | int scan, end, found_quote, delimiter, pass_next, isbrk; |
---|
554 | char quote_char; |
---|
555 | |
---|
556 | end = rl_point; |
---|
557 | found_quote = delimiter = 0; |
---|
558 | quote_char = '\0'; |
---|
559 | |
---|
560 | if (rl_completer_quote_characters) |
---|
561 | { |
---|
562 | /* We have a list of characters which can be used in pairs to |
---|
563 | quote substrings for the completer. Try to find the start |
---|
564 | of an unclosed quoted substring. */ |
---|
565 | /* FOUND_QUOTE is set so we know what kind of quotes we found. */ |
---|
566 | for (scan = pass_next = 0; scan < end; scan++) |
---|
567 | { |
---|
568 | if (pass_next) |
---|
569 | { |
---|
570 | pass_next = 0; |
---|
571 | continue; |
---|
572 | } |
---|
573 | |
---|
574 | if (rl_line_buffer[scan] == '\\') |
---|
575 | { |
---|
576 | pass_next = 1; |
---|
577 | found_quote |= RL_QF_BACKSLASH; |
---|
578 | continue; |
---|
579 | } |
---|
580 | |
---|
581 | if (quote_char != '\0') |
---|
582 | { |
---|
583 | /* Ignore everything until the matching close quote char. */ |
---|
584 | if (rl_line_buffer[scan] == quote_char) |
---|
585 | { |
---|
586 | /* Found matching close. Abandon this substring. */ |
---|
587 | quote_char = '\0'; |
---|
588 | rl_point = end; |
---|
589 | } |
---|
590 | } |
---|
591 | else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan])) |
---|
592 | { |
---|
593 | /* Found start of a quoted substring. */ |
---|
594 | quote_char = rl_line_buffer[scan]; |
---|
595 | rl_point = scan + 1; |
---|
596 | /* Shell-like quoting conventions. */ |
---|
597 | if (quote_char == '\'') |
---|
598 | found_quote |= RL_QF_SINGLE_QUOTE; |
---|
599 | else if (quote_char == '"') |
---|
600 | found_quote |= RL_QF_DOUBLE_QUOTE; |
---|
601 | } |
---|
602 | } |
---|
603 | } |
---|
604 | |
---|
605 | if (rl_point == end && quote_char == '\0') |
---|
606 | { |
---|
607 | /* We didn't find an unclosed quoted substring upon which to do |
---|
608 | completion, so use the word break characters to find the |
---|
609 | substring on which to complete. */ |
---|
610 | while (--rl_point) |
---|
611 | { |
---|
612 | scan = rl_line_buffer[rl_point]; |
---|
613 | |
---|
614 | if (strchr (rl_completer_word_break_characters, scan) == 0) |
---|
615 | continue; |
---|
616 | |
---|
617 | /* Call the application-specific function to tell us whether |
---|
618 | this word break character is quoted and should be skipped. */ |
---|
619 | if (rl_char_is_quoted_p && found_quote && |
---|
620 | (*rl_char_is_quoted_p) (rl_line_buffer, rl_point)) |
---|
621 | continue; |
---|
622 | |
---|
623 | /* Convoluted code, but it avoids an n^2 algorithm with calls |
---|
624 | to char_is_quoted. */ |
---|
625 | break; |
---|
626 | } |
---|
627 | } |
---|
628 | |
---|
629 | /* If we are at an unquoted word break, then advance past it. */ |
---|
630 | scan = rl_line_buffer[rl_point]; |
---|
631 | |
---|
632 | /* If there is an application-specific function to say whether or not |
---|
633 | a character is quoted and we found a quote character, let that |
---|
634 | function decide whether or not a character is a word break, even |
---|
635 | if it is found in rl_completer_word_break_characters. Don't bother |
---|
636 | if we're at the end of the line, though. */ |
---|
637 | if (scan) |
---|
638 | { |
---|
639 | if (rl_char_is_quoted_p) |
---|
640 | isbrk = (found_quote == 0 || |
---|
641 | (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) && |
---|
642 | strchr (rl_completer_word_break_characters, scan) != 0; |
---|
643 | else |
---|
644 | isbrk = strchr (rl_completer_word_break_characters, scan) != 0; |
---|
645 | |
---|
646 | if (isbrk) |
---|
647 | { |
---|
648 | /* If the character that caused the word break was a quoting |
---|
649 | character, then remember it as the delimiter. */ |
---|
650 | if (rl_basic_quote_characters && |
---|
651 | strchr (rl_basic_quote_characters, scan) && |
---|
652 | (end - rl_point) > 1) |
---|
653 | delimiter = scan; |
---|
654 | |
---|
655 | /* If the character isn't needed to determine something special |
---|
656 | about what kind of completion to perform, then advance past it. */ |
---|
657 | if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0) |
---|
658 | rl_point++; |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | if (fp) |
---|
663 | *fp = found_quote; |
---|
664 | if (dp) |
---|
665 | *dp = delimiter; |
---|
666 | |
---|
667 | return (quote_char); |
---|
668 | } |
---|
669 | |
---|
670 | static char ** |
---|
671 | gen_completion_matches (text, start, end, our_func, found_quote, quote_char) |
---|
672 | char *text; |
---|
673 | int start, end; |
---|
674 | Function *our_func; |
---|
675 | int found_quote, quote_char; |
---|
676 | { |
---|
677 | char **matches, *temp; |
---|
678 | |
---|
679 | /* If the user wants to TRY to complete, but then wants to give |
---|
680 | up and use the default completion function, they set the |
---|
681 | variable rl_attempted_completion_function. */ |
---|
682 | if (rl_attempted_completion_function) |
---|
683 | { |
---|
684 | matches = (*rl_attempted_completion_function) (text, start, end); |
---|
685 | |
---|
686 | if (matches || rl_attempted_completion_over) |
---|
687 | { |
---|
688 | rl_attempted_completion_over = 0; |
---|
689 | return (matches); |
---|
690 | } |
---|
691 | } |
---|
692 | |
---|
693 | /* Beware -- we're stripping the quotes here. Do this only if we know |
---|
694 | we are doing filename completion and the application has defined a |
---|
695 | filename dequoting function. */ |
---|
696 | temp = (char *)NULL; |
---|
697 | |
---|
698 | if (found_quote && our_func == (Function *)filename_completion_function && |
---|
699 | rl_filename_dequoting_function) |
---|
700 | { |
---|
701 | /* delete single and double quotes */ |
---|
702 | temp = (*rl_filename_dequoting_function) (text, quote_char); |
---|
703 | text = temp; /* not freeing text is not a memory leak */ |
---|
704 | } |
---|
705 | |
---|
706 | matches = completion_matches (text, (CPFunction *)our_func); |
---|
707 | FREE (temp); |
---|
708 | return matches; |
---|
709 | } |
---|
710 | |
---|
711 | /* Filter out duplicates in MATCHES. This frees up the strings in |
---|
712 | MATCHES. */ |
---|
713 | static char ** |
---|
714 | remove_duplicate_matches (matches) |
---|
715 | char **matches; |
---|
716 | { |
---|
717 | char *lowest_common; |
---|
718 | int i, j, newlen; |
---|
719 | char dead_slot; |
---|
720 | char **temp_array; |
---|
721 | |
---|
722 | /* Sort the items. */ |
---|
723 | for (i = 0; matches[i]; i++) |
---|
724 | ; |
---|
725 | |
---|
726 | /* Sort the array without matches[0], since we need it to |
---|
727 | stay in place no matter what. */ |
---|
728 | if (i) |
---|
729 | qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); |
---|
730 | |
---|
731 | /* Remember the lowest common denominator for it may be unique. */ |
---|
732 | lowest_common = savestring (matches[0]); |
---|
733 | |
---|
734 | for (i = newlen = 0; matches[i + 1]; i++) |
---|
735 | { |
---|
736 | if (strcmp (matches[i], matches[i + 1]) == 0) |
---|
737 | { |
---|
738 | free (matches[i]); |
---|
739 | matches[i] = (char *)&dead_slot; |
---|
740 | } |
---|
741 | else |
---|
742 | newlen++; |
---|
743 | } |
---|
744 | |
---|
745 | /* We have marked all the dead slots with (char *)&dead_slot. |
---|
746 | Copy all the non-dead entries into a new array. */ |
---|
747 | temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *)); |
---|
748 | for (i = j = 1; matches[i]; i++) |
---|
749 | { |
---|
750 | if (matches[i] != (char *)&dead_slot) |
---|
751 | temp_array[j++] = matches[i]; |
---|
752 | } |
---|
753 | temp_array[j] = (char *)NULL; |
---|
754 | |
---|
755 | if (matches[0] != (char *)&dead_slot) |
---|
756 | free (matches[0]); |
---|
757 | |
---|
758 | /* Place the lowest common denominator back in [0]. */ |
---|
759 | temp_array[0] = lowest_common; |
---|
760 | |
---|
761 | /* If there is one string left, and it is identical to the |
---|
762 | lowest common denominator, then the LCD is the string to |
---|
763 | insert. */ |
---|
764 | if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0) |
---|
765 | { |
---|
766 | free (temp_array[1]); |
---|
767 | temp_array[1] = (char *)NULL; |
---|
768 | } |
---|
769 | return (temp_array); |
---|
770 | } |
---|
771 | |
---|
772 | /* Find the common prefix of the list of matches, and put it into |
---|
773 | matches[0]. */ |
---|
774 | static int |
---|
775 | compute_lcd_of_matches (match_list, matches, text) |
---|
776 | char **match_list; |
---|
777 | int matches; |
---|
778 | char *text; |
---|
779 | { |
---|
780 | register int i, c1, c2, si; |
---|
781 | int low; /* Count of max-matched characters. */ |
---|
782 | |
---|
783 | /* If only one match, just use that. Otherwise, compare each |
---|
784 | member of the list with the next, finding out where they |
---|
785 | stop matching. */ |
---|
786 | if (matches == 1) |
---|
787 | { |
---|
788 | match_list[0] = match_list[1]; |
---|
789 | match_list[1] = (char *)NULL; |
---|
790 | return 1; |
---|
791 | } |
---|
792 | |
---|
793 | for (i = 1, low = 100000; i < matches; i++) |
---|
794 | { |
---|
795 | if (_rl_completion_case_fold) |
---|
796 | { |
---|
797 | for (si = 0; |
---|
798 | (c1 = _rl_to_lower(match_list[i][si])) && |
---|
799 | (c2 = _rl_to_lower(match_list[i + 1][si])); |
---|
800 | si++) |
---|
801 | if (c1 != c2) |
---|
802 | break; |
---|
803 | } |
---|
804 | else |
---|
805 | { |
---|
806 | for (si = 0; |
---|
807 | (c1 = match_list[i][si]) && |
---|
808 | (c2 = match_list[i + 1][si]); |
---|
809 | si++) |
---|
810 | if (c1 != c2) |
---|
811 | break; |
---|
812 | } |
---|
813 | |
---|
814 | if (low > si) |
---|
815 | low = si; |
---|
816 | } |
---|
817 | |
---|
818 | /* If there were multiple matches, but none matched up to even the |
---|
819 | first character, and the user typed something, use that as the |
---|
820 | value of matches[0]. */ |
---|
821 | if (low == 0 && text && *text) |
---|
822 | { |
---|
823 | match_list[0] = xmalloc (strlen (text) + 1); |
---|
824 | strcpy (match_list[0], text); |
---|
825 | } |
---|
826 | else |
---|
827 | { |
---|
828 | match_list[0] = xmalloc (low + 1); |
---|
829 | strncpy (match_list[0], match_list[1], low); |
---|
830 | match_list[0][low] = '\0'; |
---|
831 | } |
---|
832 | |
---|
833 | return matches; |
---|
834 | } |
---|
835 | |
---|
836 | static int |
---|
837 | postprocess_matches (matchesp, matching_filenames) |
---|
838 | char ***matchesp; |
---|
839 | int matching_filenames; |
---|
840 | { |
---|
841 | char *t, **matches, **temp_matches; |
---|
842 | int nmatch, i; |
---|
843 | |
---|
844 | matches = *matchesp; |
---|
845 | |
---|
846 | /* It seems to me that in all the cases we handle we would like |
---|
847 | to ignore duplicate possiblilities. Scan for the text to |
---|
848 | insert being identical to the other completions. */ |
---|
849 | if (rl_ignore_completion_duplicates) |
---|
850 | { |
---|
851 | temp_matches = remove_duplicate_matches (matches); |
---|
852 | free (matches); |
---|
853 | matches = temp_matches; |
---|
854 | } |
---|
855 | |
---|
856 | /* If we are matching filenames, then here is our chance to |
---|
857 | do clever processing by re-examining the list. Call the |
---|
858 | ignore function with the array as a parameter. It can |
---|
859 | munge the array, deleting matches as it desires. */ |
---|
860 | if (rl_ignore_some_completions_function && matching_filenames) |
---|
861 | { |
---|
862 | for (nmatch = 1; matches[nmatch]; nmatch++) |
---|
863 | ; |
---|
864 | (void)(*rl_ignore_some_completions_function) (matches); |
---|
865 | if (matches == 0 || matches[0] == 0) |
---|
866 | { |
---|
867 | FREE (matches); |
---|
868 | *matchesp = (char **)0; |
---|
869 | return 0; |
---|
870 | } |
---|
871 | else |
---|
872 | { |
---|
873 | /* If we removed some matches, recompute the common prefix. */ |
---|
874 | for (i = 1; matches[i]; i++) |
---|
875 | ; |
---|
876 | if (i > 1 && i < nmatch) |
---|
877 | { |
---|
878 | t = matches[0]; |
---|
879 | compute_lcd_of_matches (matches, i - 1, t); |
---|
880 | FREE (t); |
---|
881 | } |
---|
882 | } |
---|
883 | } |
---|
884 | |
---|
885 | *matchesp = matches; |
---|
886 | return (1); |
---|
887 | } |
---|
888 | |
---|
889 | /* A convenience function for displaying a list of strings in |
---|
890 | columnar format on readline's output stream. MATCHES is the list |
---|
891 | of strings, in argv format, LEN is the number of strings in MATCHES, |
---|
892 | and MAX is the length of the longest string in MATCHES. */ |
---|
893 | void |
---|
894 | rl_display_match_list (matches, len, max) |
---|
895 | char **matches; |
---|
896 | int len, max; |
---|
897 | { |
---|
898 | int count, limit, printed_len; |
---|
899 | int i, j, k, l; |
---|
900 | char *temp; |
---|
901 | |
---|
902 | /* How many items of MAX length can we fit in the screen window? */ |
---|
903 | max += 2; |
---|
904 | limit = screenwidth / max; |
---|
905 | if (limit != 1 && (limit * max == screenwidth)) |
---|
906 | limit--; |
---|
907 | |
---|
908 | /* Avoid a possible floating exception. If max > screenwidth, |
---|
909 | limit will be 0 and a divide-by-zero fault will result. */ |
---|
910 | if (limit == 0) |
---|
911 | limit = 1; |
---|
912 | |
---|
913 | /* How many iterations of the printing loop? */ |
---|
914 | count = (len + (limit - 1)) / limit; |
---|
915 | |
---|
916 | /* Watch out for special case. If LEN is less than LIMIT, then |
---|
917 | just do the inner printing loop. |
---|
918 | 0 < len <= limit implies count = 1. */ |
---|
919 | |
---|
920 | /* Sort the items if they are not already sorted. */ |
---|
921 | if (rl_ignore_completion_duplicates == 0) |
---|
922 | qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); |
---|
923 | |
---|
924 | crlf (); |
---|
925 | |
---|
926 | if (_rl_print_completions_horizontally == 0) |
---|
927 | { |
---|
928 | /* Print the sorted items, up-and-down alphabetically, like ls. */ |
---|
929 | for (i = 1; i <= count; i++) |
---|
930 | { |
---|
931 | for (j = 0, l = i; j < limit; j++) |
---|
932 | { |
---|
933 | if (l > len || matches[l] == 0) |
---|
934 | break; |
---|
935 | else |
---|
936 | { |
---|
937 | temp = printable_part (matches[l]); |
---|
938 | printed_len = print_filename (temp, matches[l]); |
---|
939 | |
---|
940 | if (j + 1 < limit) |
---|
941 | for (k = 0; k < max - printed_len; k++) |
---|
942 | putc (' ', rl_outstream); |
---|
943 | } |
---|
944 | l += count; |
---|
945 | } |
---|
946 | crlf (); |
---|
947 | } |
---|
948 | } |
---|
949 | else |
---|
950 | { |
---|
951 | /* Print the sorted items, across alphabetically, like ls -x. */ |
---|
952 | for (i = 1; matches[i]; i++) |
---|
953 | { |
---|
954 | temp = printable_part (matches[i]); |
---|
955 | printed_len = print_filename (temp, matches[i]); |
---|
956 | /* Have we reached the end of this line? */ |
---|
957 | if (matches[i+1]) |
---|
958 | { |
---|
959 | if (i && (limit > 1) && (i % limit) == 0) |
---|
960 | crlf (); |
---|
961 | else |
---|
962 | for (k = 0; k < max - printed_len; k++) |
---|
963 | putc (' ', rl_outstream); |
---|
964 | } |
---|
965 | } |
---|
966 | crlf (); |
---|
967 | } |
---|
968 | } |
---|
969 | |
---|
970 | /* Display MATCHES, a list of matching filenames in argv format. This |
---|
971 | handles the simple case -- a single match -- first. If there is more |
---|
972 | than one match, we compute the number of strings in the list and the |
---|
973 | length of the longest string, which will be needed by the display |
---|
974 | function. If the application wants to handle displaying the list of |
---|
975 | matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the |
---|
976 | address of a function, and we just call it. If we're handling the |
---|
977 | display ourselves, we just call rl_display_match_list. We also check |
---|
978 | that the list of matches doesn't exceed the user-settable threshold, |
---|
979 | and ask the user if he wants to see the list if there are more matches |
---|
980 | than RL_COMPLETION_QUERY_ITEMS. */ |
---|
981 | static void |
---|
982 | display_matches (matches) |
---|
983 | char **matches; |
---|
984 | { |
---|
985 | int len, max, i; |
---|
986 | char *temp; |
---|
987 | |
---|
988 | /* Move to the last visible line of a possibly-multiple-line command. */ |
---|
989 | _rl_move_vert (_rl_vis_botlin); |
---|
990 | |
---|
991 | /* Handle simple case first. What if there is only one answer? */ |
---|
992 | if (matches[1] == 0) |
---|
993 | { |
---|
994 | temp = printable_part (matches[0]); |
---|
995 | crlf (); |
---|
996 | print_filename (temp, matches[0]); |
---|
997 | crlf (); |
---|
998 | |
---|
999 | rl_forced_update_display (); |
---|
1000 | rl_display_fixed = 1; |
---|
1001 | |
---|
1002 | return; |
---|
1003 | } |
---|
1004 | |
---|
1005 | /* There is more than one answer. Find out how many there are, |
---|
1006 | and find the maximum printed length of a single entry. */ |
---|
1007 | for (max = 0, i = 1; matches[i]; i++) |
---|
1008 | { |
---|
1009 | temp = printable_part (matches[i]); |
---|
1010 | len = strlen (temp); |
---|
1011 | |
---|
1012 | if (len > max) |
---|
1013 | max = len; |
---|
1014 | } |
---|
1015 | |
---|
1016 | len = i - 1; |
---|
1017 | |
---|
1018 | /* If the caller has defined a display hook, then call that now. */ |
---|
1019 | if (rl_completion_display_matches_hook) |
---|
1020 | { |
---|
1021 | (*rl_completion_display_matches_hook) (matches, len, max); |
---|
1022 | return; |
---|
1023 | } |
---|
1024 | |
---|
1025 | /* If there are many items, then ask the user if she really wants to |
---|
1026 | see them all. */ |
---|
1027 | if (len >= rl_completion_query_items) |
---|
1028 | { |
---|
1029 | crlf (); |
---|
1030 | fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len); |
---|
1031 | fflush (rl_outstream); |
---|
1032 | if (get_y_or_n () == 0) |
---|
1033 | { |
---|
1034 | crlf (); |
---|
1035 | |
---|
1036 | rl_forced_update_display (); |
---|
1037 | rl_display_fixed = 1; |
---|
1038 | |
---|
1039 | return; |
---|
1040 | } |
---|
1041 | } |
---|
1042 | |
---|
1043 | rl_display_match_list (matches, len, max); |
---|
1044 | |
---|
1045 | rl_forced_update_display (); |
---|
1046 | rl_display_fixed = 1; |
---|
1047 | } |
---|
1048 | |
---|
1049 | static char * |
---|
1050 | make_quoted_replacement (match, mtype, qc) |
---|
1051 | char *match; |
---|
1052 | int mtype; |
---|
1053 | char *qc; /* Pointer to quoting character, if any */ |
---|
1054 | { |
---|
1055 | int should_quote, do_replace; |
---|
1056 | char *replacement; |
---|
1057 | |
---|
1058 | /* If we are doing completion on quoted substrings, and any matches |
---|
1059 | contain any of the completer_word_break_characters, then auto- |
---|
1060 | matically prepend the substring with a quote character (just pick |
---|
1061 | the first one from the list of such) if it does not already begin |
---|
1062 | with a quote string. FIXME: Need to remove any such automatically |
---|
1063 | inserted quote character when it no longer is necessary, such as |
---|
1064 | if we change the string we are completing on and the new set of |
---|
1065 | matches don't require a quoted substring. */ |
---|
1066 | replacement = match; |
---|
1067 | |
---|
1068 | should_quote = match && rl_completer_quote_characters && |
---|
1069 | rl_filename_completion_desired && |
---|
1070 | rl_filename_quoting_desired; |
---|
1071 | |
---|
1072 | if (should_quote) |
---|
1073 | should_quote = should_quote && (!qc || !*qc || |
---|
1074 | (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc))); |
---|
1075 | |
---|
1076 | if (should_quote) |
---|
1077 | { |
---|
1078 | /* If there is a single match, see if we need to quote it. |
---|
1079 | This also checks whether the common prefix of several |
---|
1080 | matches needs to be quoted. */ |
---|
1081 | should_quote = rl_filename_quote_characters |
---|
1082 | ? (rl_strpbrk (match, rl_filename_quote_characters) != 0) |
---|
1083 | : 0; |
---|
1084 | |
---|
1085 | do_replace = should_quote ? mtype : NO_MATCH; |
---|
1086 | /* Quote the replacement, since we found an embedded |
---|
1087 | word break character in a potential match. */ |
---|
1088 | if (do_replace != NO_MATCH && rl_filename_quoting_function) |
---|
1089 | replacement = (*rl_filename_quoting_function) (match, do_replace, qc); |
---|
1090 | } |
---|
1091 | return (replacement); |
---|
1092 | } |
---|
1093 | |
---|
1094 | static void |
---|
1095 | insert_match (match, start, mtype, qc) |
---|
1096 | char *match; |
---|
1097 | int start, mtype; |
---|
1098 | char *qc; |
---|
1099 | { |
---|
1100 | char *replacement; |
---|
1101 | char oqc; |
---|
1102 | |
---|
1103 | oqc = qc ? *qc : '\0'; |
---|
1104 | replacement = make_quoted_replacement (match, mtype, qc); |
---|
1105 | |
---|
1106 | /* Now insert the match. */ |
---|
1107 | if (replacement) |
---|
1108 | { |
---|
1109 | /* Don't double an opening quote character. */ |
---|
1110 | if (qc && *qc && start && rl_line_buffer[start - 1] == *qc && |
---|
1111 | replacement[0] == *qc) |
---|
1112 | start--; |
---|
1113 | /* If make_quoted_replacement changed the quoting character, remove |
---|
1114 | the opening quote and insert the (fully-quoted) replacement. */ |
---|
1115 | else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc && |
---|
1116 | replacement[0] != oqc) |
---|
1117 | start--; |
---|
1118 | _rl_replace_text (replacement, start, rl_point - 1); |
---|
1119 | if (replacement != match) |
---|
1120 | free (replacement); |
---|
1121 | } |
---|
1122 | } |
---|
1123 | |
---|
1124 | /* Append any necessary closing quote and a separator character to the |
---|
1125 | just-inserted match. If the user has specified that directories |
---|
1126 | should be marked by a trailing `/', append one of those instead. The |
---|
1127 | default trailing character is a space. Returns the number of characters |
---|
1128 | appended. */ |
---|
1129 | static int |
---|
1130 | append_to_match (text, delimiter, quote_char) |
---|
1131 | char *text; |
---|
1132 | int delimiter, quote_char; |
---|
1133 | { |
---|
1134 | char temp_string[4], *filename; |
---|
1135 | int temp_string_index; |
---|
1136 | struct stat finfo; |
---|
1137 | |
---|
1138 | temp_string_index = 0; |
---|
1139 | if (quote_char && rl_point && rl_line_buffer[rl_point - 1] != quote_char) |
---|
1140 | temp_string[temp_string_index++] = quote_char; |
---|
1141 | |
---|
1142 | if (delimiter) |
---|
1143 | temp_string[temp_string_index++] = delimiter; |
---|
1144 | else if (rl_completion_append_character) |
---|
1145 | temp_string[temp_string_index++] = rl_completion_append_character; |
---|
1146 | |
---|
1147 | temp_string[temp_string_index++] = '\0'; |
---|
1148 | |
---|
1149 | if (rl_filename_completion_desired) |
---|
1150 | { |
---|
1151 | filename = tilde_expand (text); |
---|
1152 | if (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode)) |
---|
1153 | { |
---|
1154 | if (_rl_complete_mark_directories && rl_line_buffer[rl_point] != '/') |
---|
1155 | rl_insert_text ("/"); |
---|
1156 | } |
---|
1157 | else |
---|
1158 | { |
---|
1159 | if (rl_point == rl_end) |
---|
1160 | rl_insert_text (temp_string); |
---|
1161 | } |
---|
1162 | free (filename); |
---|
1163 | } |
---|
1164 | else |
---|
1165 | { |
---|
1166 | if (rl_point == rl_end) |
---|
1167 | rl_insert_text (temp_string); |
---|
1168 | } |
---|
1169 | |
---|
1170 | return (temp_string_index); |
---|
1171 | } |
---|
1172 | |
---|
1173 | static void |
---|
1174 | insert_all_matches (matches, point, qc) |
---|
1175 | char **matches; |
---|
1176 | int point; |
---|
1177 | char *qc; |
---|
1178 | { |
---|
1179 | int i; |
---|
1180 | char *rp; |
---|
1181 | |
---|
1182 | rl_begin_undo_group (); |
---|
1183 | /* remove any opening quote character; make_quoted_replacement will add |
---|
1184 | it back. */ |
---|
1185 | if (qc && *qc && point && rl_line_buffer[point - 1] == *qc) |
---|
1186 | point--; |
---|
1187 | rl_delete_text (point, rl_point); |
---|
1188 | rl_point = point; |
---|
1189 | |
---|
1190 | if (matches[1]) |
---|
1191 | { |
---|
1192 | for (i = 1; matches[i]; i++) |
---|
1193 | { |
---|
1194 | rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc); |
---|
1195 | rl_insert_text (rp); |
---|
1196 | rl_insert_text (" "); |
---|
1197 | if (rp != matches[i]) |
---|
1198 | free (rp); |
---|
1199 | } |
---|
1200 | } |
---|
1201 | else |
---|
1202 | { |
---|
1203 | rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc); |
---|
1204 | rl_insert_text (rp); |
---|
1205 | rl_insert_text (" "); |
---|
1206 | if (rp != matches[0]) |
---|
1207 | free (rp); |
---|
1208 | } |
---|
1209 | rl_end_undo_group (); |
---|
1210 | } |
---|
1211 | |
---|
1212 | static void |
---|
1213 | free_match_list (matches) |
---|
1214 | char **matches; |
---|
1215 | { |
---|
1216 | register int i; |
---|
1217 | |
---|
1218 | for (i = 0; matches[i]; i++) |
---|
1219 | free (matches[i]); |
---|
1220 | free (matches); |
---|
1221 | } |
---|
1222 | |
---|
1223 | /* Complete the word at or before point. |
---|
1224 | WHAT_TO_DO says what to do with the completion. |
---|
1225 | `?' means list the possible completions. |
---|
1226 | TAB means do standard completion. |
---|
1227 | `*' means insert all of the possible completions. |
---|
1228 | `!' means to do standard completion, and list all possible completions if |
---|
1229 | there is more than one. */ |
---|
1230 | int |
---|
1231 | rl_complete_internal (what_to_do) |
---|
1232 | int what_to_do; |
---|
1233 | { |
---|
1234 | char **matches; |
---|
1235 | Function *our_func; |
---|
1236 | int start, end, delimiter, found_quote, i; |
---|
1237 | char *text, *saved_line_buffer; |
---|
1238 | char quote_char; |
---|
1239 | |
---|
1240 | /* Only the completion entry function can change these. */ |
---|
1241 | rl_filename_completion_desired = 0; |
---|
1242 | rl_filename_quoting_desired = 1; |
---|
1243 | rl_completion_type = what_to_do; |
---|
1244 | |
---|
1245 | saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL; |
---|
1246 | our_func = rl_completion_entry_function |
---|
1247 | ? rl_completion_entry_function |
---|
1248 | : (Function *)filename_completion_function; |
---|
1249 | |
---|
1250 | /* We now look backwards for the start of a filename/variable word. */ |
---|
1251 | end = rl_point; |
---|
1252 | found_quote = delimiter = 0; |
---|
1253 | quote_char = '\0'; |
---|
1254 | |
---|
1255 | if (rl_point) |
---|
1256 | /* This (possibly) changes rl_point. If it returns a non-zero char, |
---|
1257 | we know we have an open quote. */ |
---|
1258 | quote_char = find_completion_word (&found_quote, &delimiter); |
---|
1259 | |
---|
1260 | start = rl_point; |
---|
1261 | rl_point = end; |
---|
1262 | |
---|
1263 | text = rl_copy_text (start, end); |
---|
1264 | matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char); |
---|
1265 | free (text); |
---|
1266 | |
---|
1267 | if (matches == 0) |
---|
1268 | { |
---|
1269 | ding (); |
---|
1270 | FREE (saved_line_buffer); |
---|
1271 | return (0); |
---|
1272 | } |
---|
1273 | |
---|
1274 | #if 0 |
---|
1275 | /* If we are matching filenames, our_func will have been set to |
---|
1276 | filename_completion_function */ |
---|
1277 | i = our_func == (Function *)filename_completion_function; |
---|
1278 | #else |
---|
1279 | /* If we are matching filenames, the attempted completion function will |
---|
1280 | have set rl_filename_completion_desired to a non-zero value. The basic |
---|
1281 | filename_completion_function does this. */ |
---|
1282 | i = rl_filename_completion_desired; |
---|
1283 | #endif |
---|
1284 | |
---|
1285 | if (postprocess_matches (&matches, i) == 0) |
---|
1286 | { |
---|
1287 | ding (); |
---|
1288 | FREE (saved_line_buffer); |
---|
1289 | completion_changed_buffer = 0; |
---|
1290 | return (0); |
---|
1291 | } |
---|
1292 | |
---|
1293 | switch (what_to_do) |
---|
1294 | { |
---|
1295 | case TAB: |
---|
1296 | case '!': |
---|
1297 | /* Insert the first match with proper quoting. */ |
---|
1298 | if (*matches[0]) |
---|
1299 | insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char); |
---|
1300 | |
---|
1301 | /* If there are more matches, ring the bell to indicate. |
---|
1302 | If we are in vi mode, Posix.2 says to not ring the bell. |
---|
1303 | If the `show-all-if-ambiguous' variable is set, display |
---|
1304 | all the matches immediately. Otherwise, if this was the |
---|
1305 | only match, and we are hacking files, check the file to |
---|
1306 | see if it was a directory. If so, and the `mark-directories' |
---|
1307 | variable is set, add a '/' to the name. If not, and we |
---|
1308 | are at the end of the line, then add a space. */ |
---|
1309 | if (matches[1]) |
---|
1310 | { |
---|
1311 | if (what_to_do == '!') |
---|
1312 | { |
---|
1313 | display_matches (matches); |
---|
1314 | break; |
---|
1315 | } |
---|
1316 | else if (rl_editing_mode != vi_mode) |
---|
1317 | ding (); /* There are other matches remaining. */ |
---|
1318 | } |
---|
1319 | else |
---|
1320 | append_to_match (matches[0], delimiter, quote_char); |
---|
1321 | |
---|
1322 | break; |
---|
1323 | |
---|
1324 | case '*': |
---|
1325 | insert_all_matches (matches, start, "e_char); |
---|
1326 | break; |
---|
1327 | |
---|
1328 | case '?': |
---|
1329 | display_matches (matches); |
---|
1330 | break; |
---|
1331 | |
---|
1332 | default: |
---|
1333 | fprintf (stderr, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do); |
---|
1334 | ding (); |
---|
1335 | FREE (saved_line_buffer); |
---|
1336 | return 1; |
---|
1337 | } |
---|
1338 | |
---|
1339 | free_match_list (matches); |
---|
1340 | |
---|
1341 | /* Check to see if the line has changed through all of this manipulation. */ |
---|
1342 | if (saved_line_buffer) |
---|
1343 | { |
---|
1344 | completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0; |
---|
1345 | free (saved_line_buffer); |
---|
1346 | } |
---|
1347 | |
---|
1348 | return 0; |
---|
1349 | } |
---|
1350 | |
---|
1351 | /***************************************************************/ |
---|
1352 | /* */ |
---|
1353 | /* Application-callable completion match generator functions */ |
---|
1354 | /* */ |
---|
1355 | /***************************************************************/ |
---|
1356 | |
---|
1357 | /* Return an array of (char *) which is a list of completions for TEXT. |
---|
1358 | If there are no completions, return a NULL pointer. |
---|
1359 | The first entry in the returned array is the substitution for TEXT. |
---|
1360 | The remaining entries are the possible completions. |
---|
1361 | The array is terminated with a NULL pointer. |
---|
1362 | |
---|
1363 | ENTRY_FUNCTION is a function of two args, and returns a (char *). |
---|
1364 | The first argument is TEXT. |
---|
1365 | The second is a state argument; it should be zero on the first call, and |
---|
1366 | non-zero on subsequent calls. It returns a NULL pointer to the caller |
---|
1367 | when there are no more matches. |
---|
1368 | */ |
---|
1369 | char ** |
---|
1370 | completion_matches (text, entry_function) |
---|
1371 | char *text; |
---|
1372 | CPFunction *entry_function; |
---|
1373 | { |
---|
1374 | /* Number of slots in match_list. */ |
---|
1375 | int match_list_size; |
---|
1376 | |
---|
1377 | /* The list of matches. */ |
---|
1378 | char **match_list; |
---|
1379 | |
---|
1380 | /* Number of matches actually found. */ |
---|
1381 | int matches; |
---|
1382 | |
---|
1383 | /* Temporary string binder. */ |
---|
1384 | char *string; |
---|
1385 | |
---|
1386 | matches = 0; |
---|
1387 | match_list_size = 10; |
---|
1388 | match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *)); |
---|
1389 | match_list[1] = (char *)NULL; |
---|
1390 | |
---|
1391 | while (string = (*entry_function) (text, matches)) |
---|
1392 | { |
---|
1393 | if (matches + 1 == match_list_size) |
---|
1394 | match_list = (char **)xrealloc |
---|
1395 | (match_list, ((match_list_size += 10) + 1) * sizeof (char *)); |
---|
1396 | |
---|
1397 | match_list[++matches] = string; |
---|
1398 | match_list[matches + 1] = (char *)NULL; |
---|
1399 | } |
---|
1400 | |
---|
1401 | /* If there were any matches, then look through them finding out the |
---|
1402 | lowest common denominator. That then becomes match_list[0]. */ |
---|
1403 | if (matches) |
---|
1404 | compute_lcd_of_matches (match_list, matches, text); |
---|
1405 | else /* There were no matches. */ |
---|
1406 | { |
---|
1407 | free (match_list); |
---|
1408 | match_list = (char **)NULL; |
---|
1409 | } |
---|
1410 | return (match_list); |
---|
1411 | } |
---|
1412 | |
---|
1413 | /* A completion function for usernames. |
---|
1414 | TEXT contains a partial username preceded by a random |
---|
1415 | character (usually `~'). */ |
---|
1416 | char * |
---|
1417 | username_completion_function (text, state) |
---|
1418 | char *text; |
---|
1419 | int state; |
---|
1420 | { |
---|
1421 | #if defined (__WIN32__) || defined (__OPENNT) |
---|
1422 | return (char *)NULL; |
---|
1423 | #else /* !__WIN32__ && !__OPENNT) */ |
---|
1424 | static char *username = (char *)NULL; |
---|
1425 | static struct passwd *entry; |
---|
1426 | static int namelen, first_char, first_char_loc; |
---|
1427 | char *value; |
---|
1428 | |
---|
1429 | if (state == 0) |
---|
1430 | { |
---|
1431 | FREE (username); |
---|
1432 | |
---|
1433 | first_char = *text; |
---|
1434 | first_char_loc = first_char == '~'; |
---|
1435 | |
---|
1436 | username = savestring (&text[first_char_loc]); |
---|
1437 | namelen = strlen (username); |
---|
1438 | setpwent (); |
---|
1439 | } |
---|
1440 | |
---|
1441 | while (entry = getpwent ()) |
---|
1442 | { |
---|
1443 | /* Null usernames should result in all users as possible completions. */ |
---|
1444 | if (namelen == 0 || (STREQN (username, entry->pw_name, namelen))) |
---|
1445 | break; |
---|
1446 | } |
---|
1447 | |
---|
1448 | if (entry == 0) |
---|
1449 | { |
---|
1450 | endpwent (); |
---|
1451 | return ((char *)NULL); |
---|
1452 | } |
---|
1453 | else |
---|
1454 | { |
---|
1455 | value = xmalloc (2 + strlen (entry->pw_name)); |
---|
1456 | |
---|
1457 | *value = *text; |
---|
1458 | |
---|
1459 | strcpy (value + first_char_loc, entry->pw_name); |
---|
1460 | |
---|
1461 | if (first_char == '~') |
---|
1462 | rl_filename_completion_desired = 1; |
---|
1463 | |
---|
1464 | return (value); |
---|
1465 | } |
---|
1466 | #endif /* !__WIN32__ && !__OPENNT */ |
---|
1467 | } |
---|
1468 | |
---|
1469 | /* Okay, now we write the entry_function for filename completion. In the |
---|
1470 | general case. Note that completion in the shell is a little different |
---|
1471 | because of all the pathnames that must be followed when looking up the |
---|
1472 | completion for a command. */ |
---|
1473 | char * |
---|
1474 | filename_completion_function (text, state) |
---|
1475 | char *text; |
---|
1476 | int state; |
---|
1477 | { |
---|
1478 | static DIR *directory = (DIR *)NULL; |
---|
1479 | static char *filename = (char *)NULL; |
---|
1480 | static char *dirname = (char *)NULL; |
---|
1481 | static char *users_dirname = (char *)NULL; |
---|
1482 | static int filename_len; |
---|
1483 | char *temp; |
---|
1484 | int dirlen; |
---|
1485 | struct dirent *entry; |
---|
1486 | |
---|
1487 | /* If we don't have any state, then do some initialization. */ |
---|
1488 | if (state == 0) |
---|
1489 | { |
---|
1490 | /* If we were interrupted before closing the directory or reading |
---|
1491 | all of its contents, close it. */ |
---|
1492 | if (directory) |
---|
1493 | { |
---|
1494 | closedir (directory); |
---|
1495 | directory = (DIR *)NULL; |
---|
1496 | } |
---|
1497 | FREE (dirname); |
---|
1498 | FREE (filename); |
---|
1499 | FREE (users_dirname); |
---|
1500 | |
---|
1501 | filename = savestring (text); |
---|
1502 | if (*text == 0) |
---|
1503 | text = "."; |
---|
1504 | dirname = savestring (text); |
---|
1505 | |
---|
1506 | temp = strrchr (dirname, '/'); |
---|
1507 | |
---|
1508 | #if defined (__MSDOS__) |
---|
1509 | /* special hack for //X/... */ |
---|
1510 | if (dirname[0] == '/' && dirname[1] == '/' && isalpha (dirname[2]) && dirname[3] == '/') |
---|
1511 | temp = strrchr (dirname + 3, '/'); |
---|
1512 | #endif |
---|
1513 | |
---|
1514 | if (temp) |
---|
1515 | { |
---|
1516 | strcpy (filename, ++temp); |
---|
1517 | *temp = '\0'; |
---|
1518 | } |
---|
1519 | #if defined (__MSDOS__) |
---|
1520 | /* searches from current directory on the drive */ |
---|
1521 | else if (isalpha (dirname[0]) && dirname[1] == ':') |
---|
1522 | { |
---|
1523 | strcpy (filename, dirname + 2); |
---|
1524 | dirname[2] = '\0'; |
---|
1525 | } |
---|
1526 | #endif |
---|
1527 | else |
---|
1528 | { |
---|
1529 | dirname[0] = '.'; |
---|
1530 | dirname[1] = '\0'; |
---|
1531 | } |
---|
1532 | |
---|
1533 | /* We aren't done yet. We also support the "~user" syntax. */ |
---|
1534 | |
---|
1535 | /* Save the version of the directory that the user typed. */ |
---|
1536 | users_dirname = savestring (dirname); |
---|
1537 | |
---|
1538 | if (*dirname == '~') |
---|
1539 | { |
---|
1540 | temp = tilde_expand (dirname); |
---|
1541 | free (dirname); |
---|
1542 | dirname = temp; |
---|
1543 | } |
---|
1544 | |
---|
1545 | if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname)) |
---|
1546 | { |
---|
1547 | free (users_dirname); |
---|
1548 | users_dirname = savestring (dirname); |
---|
1549 | } |
---|
1550 | |
---|
1551 | directory = opendir (dirname); |
---|
1552 | filename_len = strlen (filename); |
---|
1553 | |
---|
1554 | rl_filename_completion_desired = 1; |
---|
1555 | } |
---|
1556 | |
---|
1557 | /* At this point we should entertain the possibility of hacking wildcarded |
---|
1558 | filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name |
---|
1559 | contains globbing characters, then build an array of directories, and |
---|
1560 | then map over that list while completing. */ |
---|
1561 | /* *** UNIMPLEMENTED *** */ |
---|
1562 | |
---|
1563 | /* Now that we have some state, we can read the directory. */ |
---|
1564 | |
---|
1565 | entry = (struct dirent *)NULL; |
---|
1566 | while (directory && (entry = readdir (directory))) |
---|
1567 | { |
---|
1568 | /* Special case for no filename. |
---|
1569 | All entries except "." and ".." match. */ |
---|
1570 | if (filename_len == 0) |
---|
1571 | { |
---|
1572 | if (entry->d_name[0] != '.' || |
---|
1573 | (entry->d_name[1] && |
---|
1574 | (entry->d_name[1] != '.' || entry->d_name[2]))) |
---|
1575 | break; |
---|
1576 | } |
---|
1577 | else |
---|
1578 | { |
---|
1579 | /* Otherwise, if these match up to the length of filename, then |
---|
1580 | it is a match. */ |
---|
1581 | if (_rl_completion_case_fold) |
---|
1582 | { |
---|
1583 | if ((_rl_to_lower (entry->d_name[0]) == _rl_to_lower (filename[0])) && |
---|
1584 | (((int)D_NAMLEN (entry)) >= filename_len) && |
---|
1585 | (_rl_strnicmp (filename, entry->d_name, filename_len) == 0)) |
---|
1586 | break; |
---|
1587 | } |
---|
1588 | else |
---|
1589 | { |
---|
1590 | if ((entry->d_name[0] == filename[0]) && |
---|
1591 | (((int)D_NAMLEN (entry)) >= filename_len) && |
---|
1592 | (strncmp (filename, entry->d_name, filename_len) == 0)) |
---|
1593 | break; |
---|
1594 | } |
---|
1595 | } |
---|
1596 | } |
---|
1597 | |
---|
1598 | if (entry == 0) |
---|
1599 | { |
---|
1600 | if (directory) |
---|
1601 | { |
---|
1602 | closedir (directory); |
---|
1603 | directory = (DIR *)NULL; |
---|
1604 | } |
---|
1605 | if (dirname) |
---|
1606 | { |
---|
1607 | free (dirname); |
---|
1608 | dirname = (char *)NULL; |
---|
1609 | } |
---|
1610 | if (filename) |
---|
1611 | { |
---|
1612 | free (filename); |
---|
1613 | filename = (char *)NULL; |
---|
1614 | } |
---|
1615 | if (users_dirname) |
---|
1616 | { |
---|
1617 | free (users_dirname); |
---|
1618 | users_dirname = (char *)NULL; |
---|
1619 | } |
---|
1620 | |
---|
1621 | return (char *)NULL; |
---|
1622 | } |
---|
1623 | else |
---|
1624 | { |
---|
1625 | /* dirname && (strcmp (dirname, ".") != 0) */ |
---|
1626 | if (dirname && (dirname[0] != '.' || dirname[1])) |
---|
1627 | { |
---|
1628 | if (rl_complete_with_tilde_expansion && *users_dirname == '~') |
---|
1629 | { |
---|
1630 | dirlen = strlen (dirname); |
---|
1631 | temp = xmalloc (2 + dirlen + D_NAMLEN (entry)); |
---|
1632 | strcpy (temp, dirname); |
---|
1633 | /* Canonicalization cuts off any final slash present. We |
---|
1634 | may need to add it back. */ |
---|
1635 | if (dirname[dirlen - 1] != '/') |
---|
1636 | { |
---|
1637 | temp[dirlen++] = '/'; |
---|
1638 | temp[dirlen] = '\0'; |
---|
1639 | } |
---|
1640 | } |
---|
1641 | else |
---|
1642 | { |
---|
1643 | dirlen = strlen (users_dirname); |
---|
1644 | temp = xmalloc (1 + dirlen + D_NAMLEN (entry)); |
---|
1645 | strcpy (temp, users_dirname); |
---|
1646 | } |
---|
1647 | |
---|
1648 | strcpy (temp + dirlen, entry->d_name); |
---|
1649 | } |
---|
1650 | else |
---|
1651 | temp = savestring (entry->d_name); |
---|
1652 | |
---|
1653 | return (temp); |
---|
1654 | } |
---|
1655 | } |
---|
1656 | |
---|
1657 | /* An initial implementation of a menu completion function a la tcsh. The |
---|
1658 | first time (if the last readline command was not rl_menu_complete), we |
---|
1659 | generate the list of matches. This code is very similar to the code in |
---|
1660 | rl_complete_internal -- there should be a way to combine the two. Then, |
---|
1661 | for each item in the list of matches, we insert the match in an undoable |
---|
1662 | fashion, with the appropriate character appended (this happens on the |
---|
1663 | second and subsequent consecutive calls to rl_menu_complete). When we |
---|
1664 | hit the end of the match list, we restore the original unmatched text, |
---|
1665 | ring the bell, and reset the counter to zero. */ |
---|
1666 | int |
---|
1667 | rl_menu_complete (count, ignore) |
---|
1668 | int count, ignore; |
---|
1669 | { |
---|
1670 | Function *our_func; |
---|
1671 | int matching_filenames, found_quote; |
---|
1672 | |
---|
1673 | static char *orig_text; |
---|
1674 | static char **matches = (char **)0; |
---|
1675 | static int match_list_index = 0; |
---|
1676 | static int match_list_size = 0; |
---|
1677 | static int orig_start, orig_end; |
---|
1678 | static char quote_char; |
---|
1679 | static int delimiter; |
---|
1680 | |
---|
1681 | /* The first time through, we generate the list of matches and set things |
---|
1682 | up to insert them. */ |
---|
1683 | if (rl_last_func != rl_menu_complete) |
---|
1684 | { |
---|
1685 | /* Clean up from previous call, if any. */ |
---|
1686 | FREE (orig_text); |
---|
1687 | if (matches) |
---|
1688 | free_match_list (matches); |
---|
1689 | |
---|
1690 | match_list_index = match_list_size = 0; |
---|
1691 | matches = (char **)NULL; |
---|
1692 | |
---|
1693 | /* Only the completion entry function can change these. */ |
---|
1694 | rl_filename_completion_desired = 0; |
---|
1695 | rl_filename_quoting_desired = 1; |
---|
1696 | rl_completion_type = '%'; |
---|
1697 | |
---|
1698 | our_func = rl_completion_entry_function |
---|
1699 | ? rl_completion_entry_function |
---|
1700 | : (Function *)filename_completion_function; |
---|
1701 | |
---|
1702 | /* We now look backwards for the start of a filename/variable word. */ |
---|
1703 | orig_end = rl_point; |
---|
1704 | found_quote = delimiter = 0; |
---|
1705 | quote_char = '\0'; |
---|
1706 | |
---|
1707 | if (rl_point) |
---|
1708 | /* This (possibly) changes rl_point. If it returns a non-zero char, |
---|
1709 | we know we have an open quote. */ |
---|
1710 | quote_char = find_completion_word (&found_quote, &delimiter); |
---|
1711 | |
---|
1712 | orig_start = rl_point; |
---|
1713 | rl_point = orig_end; |
---|
1714 | |
---|
1715 | orig_text = rl_copy_text (orig_start, orig_end); |
---|
1716 | matches = gen_completion_matches (orig_text, orig_start, orig_end, |
---|
1717 | our_func, found_quote, quote_char); |
---|
1718 | |
---|
1719 | #if 0 |
---|
1720 | /* If we are matching filenames, our_func will have been set to |
---|
1721 | filename_completion_function */ |
---|
1722 | matching_filenames = our_func == (Function *)filename_completion_function; |
---|
1723 | #else |
---|
1724 | /* If we are matching filenames, the attempted completion function will |
---|
1725 | have set rl_filename_completion_desired to a non-zero value. The basic |
---|
1726 | filename_completion_function does this. */ |
---|
1727 | matching_filenames = rl_filename_completion_desired; |
---|
1728 | #endif |
---|
1729 | if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0) |
---|
1730 | { |
---|
1731 | ding (); |
---|
1732 | FREE (matches); |
---|
1733 | matches = (char **)0; |
---|
1734 | FREE (orig_text); |
---|
1735 | orig_text = (char *)0; |
---|
1736 | completion_changed_buffer = 0; |
---|
1737 | return (0); |
---|
1738 | } |
---|
1739 | |
---|
1740 | for (match_list_size = 0; matches[match_list_size]; match_list_size++) |
---|
1741 | ; |
---|
1742 | /* matches[0] is lcd if match_list_size > 1, but the circular buffer |
---|
1743 | code below should take care of it. */ |
---|
1744 | } |
---|
1745 | |
---|
1746 | /* Now we have the list of matches. Replace the text between |
---|
1747 | rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with |
---|
1748 | matches[match_list_index], and add any necessary closing char. */ |
---|
1749 | |
---|
1750 | if (matches == 0 || match_list_size == 0) |
---|
1751 | { |
---|
1752 | ding (); |
---|
1753 | FREE (matches); |
---|
1754 | matches = (char **)0; |
---|
1755 | completion_changed_buffer = 0; |
---|
1756 | return (0); |
---|
1757 | } |
---|
1758 | |
---|
1759 | match_list_index = (match_list_index + count) % match_list_size; |
---|
1760 | if (match_list_index < 0) |
---|
1761 | match_list_index += match_list_size; |
---|
1762 | |
---|
1763 | if (match_list_index == 0 && match_list_size > 1) |
---|
1764 | { |
---|
1765 | ding (); |
---|
1766 | insert_match (orig_text, orig_start, MULT_MATCH, "e_char); |
---|
1767 | } |
---|
1768 | else |
---|
1769 | { |
---|
1770 | insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char); |
---|
1771 | append_to_match (matches[match_list_index], delimiter, quote_char); |
---|
1772 | } |
---|
1773 | |
---|
1774 | completion_changed_buffer = 1; |
---|
1775 | return (0); |
---|
1776 | } |
---|