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