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