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