1 /* complete.c -- filename completion for readline. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
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.
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.
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
24 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
31 #if !defined (NO_SYS_FILE)
32 # include <sys/file.h>
33 #endif /* !NO_SYS_FILE */
35 #if defined (HAVE_UNISTD_H)
37 #endif /* HAVE_UNISTD_H */
39 #if defined (HAVE_STDLIB_H)
42 # include "ansi_stdlib.h"
43 #endif /* HAVE_STDLIB_H */
46 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
52 #if defined (USG) && !defined (HAVE_GETPW_DECLS)
53 extern struct passwd *getpwent ();
54 #endif /* USG && !HAVE_GETPW_DECLS */
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);
61 extern struct passwd *getpwent ();
62 # endif /* !__STDC__ */
63 #endif /* isc386 && _POSIX_SOURCE */
65 #include "posixstat.h"
67 /* System-specific feature definitions and include files. */
70 /* Some standard library routines. */
73 /* Possible values for do_replace in rl_complete_internal. */
75 #define SINGLE_MATCH 1
78 #if !defined (strchr) && !defined (__STDC__)
79 extern char *strchr (), *strrchr ();
80 #endif /* !strchr && !__STDC__ */
82 extern char *tilde_expand ();
83 extern char *rl_copy_text ();
85 extern Function *rl_last_func;
86 extern int rl_editing_mode;
87 extern int screenwidth;
89 /* Forward declarations for functions defined and used in this file. */
90 char *filename_completion_function ();
91 char **completion_matches ();
93 static int compare_strings ();
94 static char *rl_strpbrk ();
96 #if defined (STATIC_MALLOC)
97 static char *xmalloc (), *xrealloc ();
99 extern char *xmalloc (), *xrealloc ();
100 #endif /* STATIC_MALLOC */
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;
107 /* Non-zero means readline completion functions perform tilde expansion. */
108 int rl_complete_with_tilde_expansion = 0;
110 /* If non-zero, non-unique completions always show the list of matches. */
111 int _rl_complete_show_all = 0;
113 #if defined (VISIBLE_STATS)
118 static int stat_char ();
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 */
126 /* **************************************************************** */
128 /* Completion matching, from readline's point of view. */
130 /* **************************************************************** */
132 /* Pointer to the generator function for completion_matches ().
133 NULL means to use filename_entry_function (), the default filename
135 Function *rl_completion_entry_function = (Function *)NULL;
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
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;
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;
150 /* Local variable states what happened during the last completion attempt. */
151 static int completion_changed_buffer = 0;
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. */
157 rl_complete (ignore, invoking_key)
158 int ignore, invoking_key;
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 ('!'));
165 return (rl_complete_internal (TAB));
168 /* List the possible completions. See description of rl_complete (). */
169 rl_possible_completions (ignore, invoking_key)
170 int ignore, invoking_key;
172 return (rl_complete_internal ('?'));
175 rl_insert_completions (ignore, invoking_key)
176 int ignore, invoking_key;
178 return (rl_complete_internal ('*'));
181 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
189 if (c == 'y' || c == 'Y' || c == ' ')
191 if (c == 'n' || c == 'N' || c == RUBOUT)
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;
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\"\\'`@$><=;|&{(";
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;
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;
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;
225 /* If non-zero, then disallow duplicates in the matches. */
226 int rl_ignore_completion_duplicates = 1;
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;
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;
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;
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. */
257 _delete_quotes (text)
264 ret = xmalloc (l + 1);
265 for (quoted = 0, p = text, r = ret; p && *p; p++)
267 /* Allow backslash-quoted characters to pass through unscathed. */
271 if (quoted && *p == quoted)
277 if (quoted == 0 && (*p == '\'' || *p == '"'))
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. */
294 printable_part (pathname)
297 char *temp = (char *)NULL;
299 if (rl_filename_completion_desired)
300 temp = strrchr (pathname, '/');
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. */
314 putc ('^', rl_outstream); \
315 putc (UNCTRL (c), rl_outstream); \
317 else if (c == RUBOUT) \
319 putc ('^', rl_outstream); \
320 putc ('?', rl_outstream); \
323 putc (c, rl_outstream)
326 print_filename (to_print, full_pathname)
327 char *to_print, *full_pathname;
329 #if !defined (VISIBLE_STATS)
332 for (s = to_print; *s; s++)
338 char *s, c, *new_full_pathname;
339 int extension_char = 0, slen, tlen;
341 for (s = to_print; *s; s++)
346 if (rl_filename_completion_desired && rl_visible_stats)
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)
353 /* Terminate the directory name. */
357 s = tilde_expand (full_pathname);
358 if (rl_directory_completion_hook)
359 (*rl_directory_completion_hook) (&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);
368 extension_char = stat_char (new_full_pathname);
370 free (new_full_pathname);
375 s = tilde_expand (full_pathname);
376 extension_char = stat_char (s);
381 putc (extension_char, rl_outstream);
382 return (extension_char != 0);
386 #endif /* VISIBLE_STATS */
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)
401 int start, scan, end, delimiter = 0, pass_next;
402 char *text, *saved_line_buffer;
404 char quote_char = '\0';
408 saved_line_buffer = savestring (rl_line_buffer);
410 saved_line_buffer = (char *)NULL;
412 if (rl_completion_entry_function)
413 our_func = rl_completion_entry_function;
415 our_func = (Function *)filename_completion_function;
417 /* Only the completion entry function can change these. */
418 rl_filename_completion_desired = 0;
419 rl_filename_quoting_desired = 1;
421 /* We now look backwards for the start of a filename/variable word. */
426 if (rl_completer_quote_characters)
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++)
440 if (rl_line_buffer[scan] == '\\')
447 if (quote_char != '\0')
449 /* Ignore everything until the matching close quote char. */
450 if (rl_line_buffer[scan] == quote_char)
452 /* Found matching close. Abandon this substring. */
457 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
459 /* Found start of a quoted substring. */
460 quote_char = rl_line_buffer[scan];
462 /* Shell-like quoting conventions. */
463 if (quote_char == '\'')
465 else if (quote_char == '"')
471 if (rl_point == end && quote_char == '\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. */
479 scan = rl_line_buffer[rl_point];
481 if (strchr (rl_completer_word_break_characters, scan) == 0)
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))
491 /* Convoluted code, but it avoids an n^2 algorithm with calls
492 to char_is_quoted. */
497 /* If we are at an unquoted word break, then advance past it. */
498 scan = rl_line_buffer[rl_point];
500 if ((found_quote == 0 || char_is_quoted (rl_line_buffer, rl_point) == 0) &&
501 strchr (rl_completer_word_break_characters, scan))
503 if (strchr (rl_completer_word_break_characters, scan))
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)
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)
518 /* At this point, we know we have an open quote if quote_char != '\0'. */
521 text = rl_copy_text (start, end);
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)
528 matches = (*rl_attempted_completion_function) (text, start, end);
530 if (matches || rl_attempted_completion_over)
532 rl_attempted_completion_over = 0;
533 our_func = (Function *)NULL;
534 goto after_usual_completion;
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)
543 /* delete single and double quotes */
544 replacement = _delete_quotes (text);
547 replacement = (char *)0;
551 matches = completion_matches (text, our_func);
553 after_usual_completion:
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)
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++)
578 /* Try sorting the array without matches[0], since we need it to
579 stay in place no matter what. */
581 qsort (matches+1, i-1, sizeof (char *), compare_strings);
583 /* Remember the lowest common denominator for it may be unique. */
584 lowest_common = savestring (matches[0]);
586 for (i = 0; matches[i + 1]; i++)
588 if (strcmp (matches[i], matches[i + 1]) == 0)
591 matches[i] = (char *)&dead_slot;
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++)
602 if (matches[i] != (char *)&dead_slot)
603 temp_array[j++] = matches[i];
605 temp_array[j] = (char *)NULL;
607 if (matches[0] != (char *)&dead_slot)
611 matches = temp_array;
613 /* Place the lowest common denominator back in [0]. */
614 matches[0] = lowest_common;
616 /* If there is one string left, and it is identical to the
617 lowest common denominator, then the LCD is the string to
619 if (j == 2 && strcmp (matches[0], matches[1]) == 0)
622 matches[1] = (char *)NULL;
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)
637 (void)(*rl_ignore_some_completions_function)(matches);
638 if (matches == 0 || matches[0] == 0)
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];
657 should_quote = matches[0] && rl_completer_quote_characters &&
658 rl_filename_completion_desired &&
659 rl_filename_quoting_desired;
663 should_quote = should_quote && (!quote_char || quote_char == '"');
665 should_quote = should_quote && !quote_char;
672 do_replace = NO_MATCH;
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;
680 should_quote = should_quote || rl_strpbrk (matches[0], "#$`?*[!") != 0;
684 do_replace = matches[1] ? MULT_MATCH : SINGLE_MATCH;
686 if (do_replace != NO_MATCH)
689 /* Quote the replacement, since we found an
690 embedded word break character in a potential
694 extern char *double_quote (); /* in builtins/common.c */
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. */
704 if (mtext[0] == '~' && do_replace == SINGLE_MATCH)
705 mtext = tilde_expand (matches[0]);
706 rtext = double_quote (mtext);
707 if (mtext != matches[0])
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 == '"')
718 strcpy (replacement, rtext + 1);
723 strcpy (replacement, rtext);
724 if (do_replace == MULT_MATCH)
725 replacement[rlen - 1] = '\0';
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]);
741 rl_begin_undo_group ();
742 rl_delete_text (start, rl_point);
744 rl_insert_text (replacement);
745 rl_end_undo_group ();
746 if (replacement != matches[0])
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. */
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. */
765 int temp_string_index = 0;
768 temp_string[temp_string_index++] = quote_char;
770 temp_string[temp_string_index++] = delimiter ? delimiter : ' ';
771 temp_string[temp_string_index++] = '\0';
773 if (rl_filename_completion_desired)
776 char *filename = tilde_expand (matches[0]);
778 if ((stat (filename, &finfo) == 0) && S_ISDIR (finfo.st_mode))
780 if (rl_line_buffer[rl_point] != '/')
781 rl_insert_text ("/");
785 if (rl_point == rl_end)
786 rl_insert_text (temp_string);
792 if (rl_point == rl_end)
793 rl_insert_text (temp_string);
802 rl_begin_undo_group ();
803 rl_delete_text (start, rl_point);
809 rl_insert_text (matches[i++]);
810 rl_insert_text (" ");
815 rl_insert_text (matches[0]);
816 rl_insert_text (" ");
818 rl_end_undo_group ();
824 int len, count, limit, max;
827 /* Handle simple case first. What if there is only one answer? */
832 temp = printable_part (matches[0]);
834 print_filename (temp, matches[0]);
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
843 for (max = 0, i = 1; matches[i]; i++)
848 temp = printable_part (matches[i]);
849 name_length = strlen (temp);
851 if (name_length > max)
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)
862 fprintf (rl_outstream,
863 "There are %d possibilities. Do you really", len);
865 fprintf (rl_outstream, "wish to see them all? (y or n)");
866 fflush (rl_outstream);
874 /* How many items of MAX length can we fit in the screen window? */
876 limit = screenwidth / max;
877 if (limit != 1 && (limit * max == screenwidth))
880 /* Avoid a possible floating exception. If max > screenwidth,
881 limit will be 0 and a divide-by-zero fault will result. */
885 /* How many iterations of the printing loop? */
886 count = (len + (limit - 1)) / limit;
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. */
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);
896 /* Print the sorted items, up-and-down alphabetically, like
900 for (i = 1; i <= count; i++)
902 for (j = 0, l = i; j < limit; j++)
904 if (l > len || !matches[l])
911 temp = printable_part (matches[l]);
912 printed_length = strlen (temp);
913 printed_length += print_filename (temp, matches[l]);
917 for (k = 0; k < max - printed_length; k++)
918 putc (' ', rl_outstream);
932 fprintf (stderr, "\r\nreadline: bad value for what_to_do in rl_complete\n");
936 for (i = 0; matches[i]; i++)
941 /* Check to see if the line has changed through all of this manipulation. */
942 if (saved_line_buffer)
944 if (strcmp (rl_line_buffer, saved_line_buffer) != 0)
945 completion_changed_buffer = 1;
947 completion_changed_buffer = 0;
949 free (saved_line_buffer);
954 #if defined (VISIBLE_STATS)
955 /* Return the character which best describes FILENAME.
956 `@' for symbolic links
967 #if defined (S_ISLNK)
968 r = lstat (filename, &finfo);
970 r = stat (filename, &finfo);
977 if (S_ISDIR (finfo.st_mode))
979 #if defined (S_ISLNK)
980 else if (S_ISLNK (finfo.st_mode))
983 #if defined (S_ISSOCK)
984 else if (S_ISSOCK (finfo.st_mode))
986 #endif /* S_ISSOCK */
987 else if (S_ISREG (finfo.st_mode))
989 if (access (filename, X_OK) == 0)
994 #endif /* VISIBLE_STATS */
996 /* Stupid comparison routine for qsort () ing strings. */
998 compare_strings (s1, s2)
1003 result = **s1 - **s2;
1005 result = strcmp (*s1, *s2);
1010 /* A completion function for usernames.
1011 TEXT contains a partial username preceded by a random
1012 character (usually `~'). */
1014 username_completion_function (text, state)
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;
1032 if (first_char == '~')
1037 username = savestring (&text[first_char_loc]);
1038 namelen = strlen (username);
1042 while (entry = getpwent ())
1044 /* Null usernames should result in all users as possible completions. */
1047 else if ((username[0] == entry->pw_name[0]) &&
1048 (strncmp (username, entry->pw_name, namelen) == 0))
1055 return ((char *)NULL);
1059 char *value = xmalloc (2 + strlen (entry->pw_name));
1063 strcpy (value + first_char_loc, entry->pw_name);
1065 if (first_char == '~')
1066 rl_filename_completion_desired = 1;
1070 #endif /* !__GO32__ */
1073 /* **************************************************************** */
1077 /* **************************************************************** */
1079 /* Non-zero means that case is not significant in completion. */
1080 int completion_case_fold = 0;
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.
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.
1095 completion_matches (text, entry_function)
1097 CPFunction *entry_function;
1099 /* Number of slots in match_list. */
1100 int match_list_size;
1102 /* The list of matches. */
1104 (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
1106 /* Number of matches actually found. */
1109 /* Temporary string binder. */
1112 match_list[1] = (char *)NULL;
1114 while (string = (*entry_function) (text, matches))
1116 if (matches + 1 == match_list_size)
1117 match_list = (char **)xrealloc
1118 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
1120 match_list[++matches] = string;
1121 match_list[matches + 1] = (char *)NULL;
1124 /* If there were any matches, then look through them finding out the
1125 lowest common denominator. That then becomes match_list[0]. */
1129 int low = 100000; /* Count of max-matched characters. */
1131 /* If only one match, just use that. */
1134 match_list[0] = match_list[1];
1135 match_list[1] = (char *)NULL;
1139 /* Otherwise, compare each member of the list with
1140 the next, finding out where they stop matching. */
1144 register int c1, c2, si;
1146 if (completion_case_fold)
1149 (c1 = to_lower(match_list[i][si])) &&
1150 (c2 = to_lower(match_list[i + 1][si]));
1152 if (c1 != c2) break;
1157 (c1 = match_list[i][si]) &&
1158 (c2 = match_list[i + 1][si]);
1160 if (c1 != c2) break;
1163 if (low > si) low = si;
1166 match_list[0] = xmalloc (low + 1);
1167 strncpy (match_list[0], match_list[1], low);
1168 match_list[0][low] = '\0';
1171 else /* There were no matches. */
1174 match_list = (char **)NULL;
1176 return (match_list);
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. */
1184 filename_completion_function (text, state)
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;
1194 struct dirent *entry = (struct dirent *)NULL;
1196 /* If we don't have any state, then do some initialization. */
1201 if (dirname) free (dirname);
1202 if (filename) free (filename);
1203 if (users_dirname) free (users_dirname);
1205 filename = savestring (text);
1206 if (!*text) text = ".";
1207 dirname = savestring (text);
1209 temp = strrchr (dirname, '/');
1213 strcpy (filename, ++temp);
1217 strcpy (dirname, ".");
1219 /* We aren't done yet. We also support the "~user" syntax. */
1221 /* Save the version of the directory that the user typed. */
1222 users_dirname = savestring (dirname);
1225 int replace_dirname;
1227 temp_dirname = tilde_expand (dirname);
1229 dirname = temp_dirname;
1231 replace_dirname = 0;
1232 if (rl_directory_completion_hook)
1233 replace_dirname = (*rl_directory_completion_hook) (&dirname);
1234 if (replace_dirname)
1236 free (users_dirname);
1237 users_dirname = savestring (dirname);
1240 directory = opendir (dirname);
1241 filename_len = strlen (filename);
1243 rl_filename_completion_desired = 1;
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 *** */
1252 /* Now that we have some state, we can read the directory. */
1254 while (directory && (entry = readdir (directory)))
1256 /* Special case for no filename.
1257 All entries except "." and ".." match. */
1260 if ((strcmp (entry->d_name, ".") != 0) &&
1261 (strcmp (entry->d_name, "..") != 0))
1266 /* Otherwise, if these match up to the length of filename, then
1268 if ((entry->d_name[0] == filename[0]) &&
1269 (((int)D_NAMLEN (entry)) >= filename_len) &&
1270 (strncmp (filename, entry->d_name, filename_len) == 0))
1279 closedir (directory);
1280 directory = (DIR *)NULL;
1285 dirname = (char *)NULL;
1290 filename = (char *)NULL;
1294 free (users_dirname);
1295 users_dirname = (char *)NULL;
1298 return (char *)NULL;
1304 /* dirname && (strcmp (dirname, ".") != 0) */
1305 if (dirname && (dirname[0] != '.' || dirname[1]))
1307 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
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
1314 if (dirname[dirlen - 1] != '/')
1317 temp[dirlen + 1] = '\0';
1322 temp = xmalloc (1 + strlen (users_dirname) + D_NAMLEN (entry));
1323 strcpy (temp, users_dirname);
1326 strcat (temp, entry->d_name);
1329 temp = (savestring (entry->d_name));
1335 /* A function for simple tilde expansion. */
1337 rl_tilde_expand (ignore, key)
1340 register int start, end;
1346 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
1348 homedir = tilde_expand ("~");
1351 else if (rl_line_buffer[start] != '~')
1353 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--);
1362 while (!whitespace (rl_line_buffer[end]) && end < rl_end);
1364 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
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
1370 if (rl_line_buffer[start] == '~')
1375 len = end - start + 1;
1376 temp = xmalloc (len + 1);
1377 strncpy (temp, rl_line_buffer + start, len);
1379 homedir = tilde_expand (temp);
1383 rl_begin_undo_group ();
1384 rl_delete_text (start, end + 1);
1386 rl_insert_text (homedir);
1387 rl_end_undo_group ();
1393 /* Find the first occurrence in STRING1 of any character from STRING2.
1394 Return a pointer to the character in STRING1. */
1396 rl_strpbrk (string1, string2)
1397 char *string1, *string2;
1399 register char *scan;
1401 for (; *string1; string1++)
1403 for (scan = string2; *scan; scan++)
1405 if (*string1 == *scan)
1411 return ((char *)NULL);
1414 #if defined (STATIC_MALLOC)
1416 /* **************************************************************** */
1418 /* xmalloc and xrealloc () */
1420 /* **************************************************************** */
1422 static void memory_error_and_abort ();
1428 char *temp = (char *)malloc (bytes);
1431 memory_error_and_abort ();
1436 xrealloc (pointer, bytes)
1443 temp = (char *)malloc (bytes);
1445 temp = (char *)realloc (pointer, bytes);
1448 memory_error_and_abort ();
1454 memory_error_and_abort ()
1456 fprintf (stderr, "readline: Out of virtual memory!\n");
1459 #endif /* STATIC_MALLOC */