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