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