1 /* complete.c -- filename completion for readline. */
2
3 /* Copyright (C) 1987-2005 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
7
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
12
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 # include "config_readline.h"
26 #endif
27
28 #include <sys/types.h>
29 #include <fcntl.h>
30 /* FreeBSD 5.3 will not declare u_int in sys/types.h, file.h needs it */
31 #if defined (HAVE_SYS_FILE_H) && !defined(__FreeBSD__)
32 # include <sys/file.h>
33 #endif
34
35 #if defined (HAVE_UNISTD_H)
36 # include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38
39 #if defined (HAVE_STDLIB_H)
40 # include <stdlib.h>
41 #else
42 # include "ansi_stdlib.h"
43 #endif /* HAVE_STDLIB_H */
44
45 #include <stdio.h>
46
47 #include <errno.h>
48 #if !defined (errno)
49 extern int errno;
50 #endif /* !errno */
51
52 #if defined (HAVE_PWD_H)
53 #include <pwd.h>
54 #endif
55
56 #include "posixdir.h"
57 #include "posixstat.h"
58
59 /* System-specific feature definitions and include files. */
60 #include "rldefs.h"
61 #include "rlmbutil.h"
62
63 /* Some standard library routines. */
64 #include "readline.h"
65 #include "xmalloc.h"
66 #include "rlprivate.h"
67
68 #ifdef __STDC__
69 typedef int QSFUNC (const void *, const void *);
70 #else
71 typedef int QSFUNC ();
72 #endif
73
74 #ifdef HAVE_LSTAT
75 # define LSTAT lstat
76 #else
77 # define LSTAT stat
78 #endif
79
80 /* Unix version of a hidden file. Could be different on other systems. */
81 #define HIDDEN_FILE(fname) ((fname)[0] == '.')
82
83 /* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
84 defined. */
85 #if defined (HAVE_GETPWENT) && (!defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE))
86 extern struct passwd *getpwent PARAMS((void));
87 #endif /* HAVE_GETPWENT && (!HAVE_GETPW_DECLS || _POSIX_SOURCE) */
88
89 /* If non-zero, then this is the address of a function to call when
90 completing a word would normally display the list of possible matches.
91 This function is called instead of actually doing the display.
92 It takes three arguments: (char **matches, int num_matches, int max_length)
93 where MATCHES is the array of strings that matched, NUM_MATCHES is the
94 number of strings in that array, and MAX_LENGTH is the length of the
95 longest string in that array. */
96 rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)NULL;
97
98 #if defined (VISIBLE_STATS)
99 # if !defined (X_OK)
100 # define X_OK 1
101 # endif
102 static int stat_char PARAMS((char *));
103 #endif
104
105 static int path_isdir PARAMS((const char *));
106
107 static char *rl_quote_filename PARAMS((char *, int, char *));
108
109 static void set_completion_defaults PARAMS((int));
110 static int get_y_or_n PARAMS((int));
111 static int _rl_internal_pager PARAMS((int));
112 static char *printable_part PARAMS((char *));
113 static int fnwidth PARAMS((const char *));
114 static int fnprint PARAMS((const char *));
115 static int print_filename PARAMS((char *, char *));
116
117 static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int));
118
119 static char **remove_duplicate_matches PARAMS((char **));
120 static void insert_match PARAMS((char *, int, int, char *));
121 static int append_to_match PARAMS((char *, int, int, int));
122 static void insert_all_matches PARAMS((char **, int, char *));
123 static void display_matches PARAMS((char **));
124 static int compute_lcd_of_matches PARAMS((char **, int, const char *));
125 static int postprocess_matches PARAMS((char ***, int));
126
127 static char *make_quoted_replacement PARAMS((char *, int, char *));
128
129 /* **************************************************************** */
130 /* */
131 /* Completion matching, from readline's point of view. */
132 /* */
133 /* **************************************************************** */
134
135 /* Variables known only to the readline library. */
136
137 /* If non-zero, non-unique completions always show the list of matches. */
138 int _rl_complete_show_all = 0;
139
140 /* If non-zero, non-unique completions show the list of matches, unless it
141 is not possible to do partial completion and modify the line. */
142 int _rl_complete_show_unmodified = 0;
143
144 /* If non-zero, completed directory names have a slash appended. */
145 int _rl_complete_mark_directories = 1;
146
147 /* If non-zero, the symlinked directory completion behavior introduced in
148 readline-4.2a is disabled, and symlinks that point to directories have
149 a slash appended (subject to the value of _rl_complete_mark_directories).
150 This is user-settable via the mark-symlinked-directories variable. */
151 int _rl_complete_mark_symlink_dirs = 0;
152
153 /* If non-zero, completions are printed horizontally in alphabetical order,
154 like `ls -x'. */
155 int _rl_print_completions_horizontally;
156
157 /* Non-zero means that case is not significant in filename completion. */
158 #if defined (__MSDOS__) && !defined (__DJGPP__)
159 int _rl_completion_case_fold = 1;
160 #else
161 int _rl_completion_case_fold;
162 #endif
163
164 /* If non-zero, don't match hidden files (filenames beginning with a `.' on
165 Unix) when doing filename completion. */
166 int _rl_match_hidden_files = 1;
167
168 /* Global variables available to applications using readline. */
169
170 #if defined (VISIBLE_STATS)
171 /* Non-zero means add an additional character to each filename displayed
172 during listing completion iff rl_filename_completion_desired which helps
173 to indicate the type of file being listed. */
174 int rl_visible_stats = 0;
175 #endif /* VISIBLE_STATS */
176
177 /* If non-zero, then this is the address of a function to call when
178 completing on a directory name. The function is called with
179 the address of a string (the current directory name) as an arg. */
180 rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
181
182 rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
183
184 /* Non-zero means readline completion functions perform tilde expansion. */
185 int rl_complete_with_tilde_expansion = 0;
186
187 /* Pointer to the generator function for completion_matches ().
188 NULL means to use rl_filename_completion_function (), the default filename
189 completer. */
190 rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL;
191
192 /* Pointer to alternative function to create matches.
193 Function is called with TEXT, START, and END.
194 START and END are indices in RL_LINE_BUFFER saying what the boundaries
195 of TEXT are.
196 If this function exists and returns NULL then call the value of
197 rl_completion_entry_function to try to match, otherwise use the
198 array of strings returned. */
199 rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL;
200
201 /* Non-zero means to suppress normal filename completion after the
202 user-specified completion function has been called. */
203 int rl_attempted_completion_over = 0;
204
205 /* Set to a character indicating the type of completion being performed
206 by rl_complete_internal, available for use by application completion
207 functions. */
208 int rl_completion_type = 0;
209
210 /* Up to this many items will be displayed in response to a
211 possible-completions call. After that, we ask the user if
212 she is sure she wants to see them all. A negative value means
213 don't ask. */
214 int rl_completion_query_items = 100;
215
216 int _rl_page_completions = 1;
217
218 /* The basic list of characters that signal a break between words for the
219 completer routine. The contents of this variable is what breaks words
220 in the shell, i.e. " \t\n\"\\'`@$><=" */
221 const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
222
223 /* List of basic quoting characters. */
224 const char *rl_basic_quote_characters = "\"'";
225
226 /* The list of characters that signal a break between words for
227 rl_complete_internal. The default list is the contents of
228 rl_basic_word_break_characters. */
229 /*const*/ char *rl_completer_word_break_characters = (/*const*/ char *)NULL;
230
231 /* Hook function to allow an application to set the completion word
232 break characters before readline breaks up the line. Allows
233 position-dependent word break characters. */
234 rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL;
235
236 /* List of characters which can be used to quote a substring of the line.
237 Completion occurs on the entire substring, and within the substring
238 rl_completer_word_break_characters are treated as any other character,
239 unless they also appear within this list. */
240 const char *rl_completer_quote_characters = (const char *)NULL;
241
242 /* List of characters that should be quoted in filenames by the completer. */
243 const char *rl_filename_quote_characters = (const char *)NULL;
244
245 /* List of characters that are word break characters, but should be left
246 in TEXT when it is passed to the completion function. The shell uses
247 this to help determine what kind of completing to do. */
248 const char *rl_special_prefixes = (const char *)NULL;
249
250 /* If non-zero, then disallow duplicates in the matches. */
251 int rl_ignore_completion_duplicates = 1;
252
253 /* Non-zero means that the results of the matches are to be treated
254 as filenames. This is ALWAYS zero on entry, and can only be changed
255 within a completion entry finder function. */
256 int rl_filename_completion_desired = 0;
257
258 /* Non-zero means that the results of the matches are to be quoted using
259 double quotes (or an application-specific quoting mechanism) if the
260 filename contains any characters in rl_filename_quote_chars. This is
261 ALWAYS non-zero on entry, and can only be changed within a completion
262 entry finder function. */
263 int rl_filename_quoting_desired = 1;
264
265 /* This function, if defined, is called by the completer when real
266 filename completion is done, after all the matching names have been
267 generated. It is passed a (char**) known as matches in the code below.
268 It consists of a NULL-terminated array of pointers to potential
269 matching strings. The 1st element (matches[0]) is the maximal
270 substring that is common to all matches. This function can re-arrange
271 the list of matches as required, but all elements of the array must be
272 free()'d if they are deleted. The main intent of this function is
273 to implement FIGNORE a la SunOS csh. */
274 rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL;
275
276 /* Set to a function to quote a filename in an application-specific fashion.
277 Called with the text to quote, the type of match found (single or multiple)
278 and a pointer to the quoting character to be used, which the function can
279 reset if desired. */
280 rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename;
281
282 /* Function to call to remove quoting characters from a filename. Called
283 before completion is attempted, so the embedded quotes do not interfere
284 with matching names in the file system. Readline doesn't do anything
285 with this; it's set only by applications. */
286 rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL;
287
288 /* Function to call to decide whether or not a word break character is
289 quoted. If a character is quoted, it does not break words for the
290 completer. */
291 rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL;
292
293 /* If non-zero, the completion functions don't append anything except a
294 possible closing quote. This is set to 0 by rl_complete_internal and
295 may be changed by an application-specific completion function. */
296 int rl_completion_suppress_append = 0;
297
298 /* Character appended to completed words when at the end of the line. The
299 default is a space. */
300 int rl_completion_append_character = ' ';
301
302 /* If non-zero, the completion functions don't append any closing quote.
303 This is set to 0 by rl_complete_internal and may be changed by an
304 application-specific completion function. */
305 int rl_completion_suppress_quote = 0;
306
307 /* Set to any quote character readline thinks it finds before any application
308 completion function is called. */
309 int rl_completion_quote_character;
310
311 /* Set to a non-zero value if readline found quoting anywhere in the word to
312 be completed; set before any application completion function is called. */
313 int rl_completion_found_quote;
314
315 /* If non-zero, a slash will be appended to completed filenames that are
316 symbolic links to directory names, subject to the value of the
317 mark-directories variable (which is user-settable). This exists so
318 that application completion functions can override the user's preference
319 (set via the mark-symlinked-directories variable) if appropriate.
320 It's set to the value of _rl_complete_mark_symlink_dirs in
321 rl_complete_internal before any application-specific completion
322 function is called, so without that function doing anything, the user's
323 preferences are honored. */
324 int rl_completion_mark_symlink_dirs;
325
326 /* If non-zero, inhibit completion (temporarily). */
327 int rl_inhibit_completion;
328
329 /* Variables local to this file. */
330
331 /* Local variable states what happened during the last completion attempt. */
332 static int completion_changed_buffer;
333
334 /*************************************/
335 /* */
336 /* Bindable completion functions */
337 /* */
338 /*************************************/
339
340 /* Complete the word at or before point. You have supplied the function
341 that does the initial simple matching selection algorithm (see
342 rl_completion_matches ()). The default is to do filename completion. */
343 int
rl_complete(ignore,invoking_key)344 rl_complete (ignore, invoking_key)
345 int ignore, invoking_key;
346 {
347 if (rl_inhibit_completion)
348 return (_rl_insert_char (ignore, invoking_key));
349 else if (rl_last_func == rl_complete && !completion_changed_buffer)
350 return (rl_complete_internal ('?'));
351 else if (_rl_complete_show_all)
352 return (rl_complete_internal ('!'));
353 else if (_rl_complete_show_unmodified)
354 return (rl_complete_internal ('@'));
355 else
356 return (rl_complete_internal (TAB));
357 }
358
359 /* List the possible completions. See description of rl_complete (). */
360 int
rl_possible_completions(ignore,invoking_key)361 rl_possible_completions (ignore, invoking_key)
362 int ignore __attribute__((unused)), invoking_key __attribute__((unused));
363 {
364 return (rl_complete_internal ('?'));
365 }
366
367 int
rl_insert_completions(ignore,invoking_key)368 rl_insert_completions (ignore, invoking_key)
369 int ignore __attribute__((unused)), invoking_key __attribute__((unused));
370 {
371 return (rl_complete_internal ('*'));
372 }
373
374 /* Return the correct value to pass to rl_complete_internal performing
375 the same tests as rl_complete. This allows consecutive calls to an
376 application's completion function to list possible completions and for
377 an application-specific completion function to honor the
378 show-all-if-ambiguous readline variable. */
379 int
rl_completion_mode(cfunc)380 rl_completion_mode (cfunc)
381 rl_command_func_t *cfunc;
382 {
383 if (rl_last_func == cfunc && !completion_changed_buffer)
384 return '?';
385 else if (_rl_complete_show_all)
386 return '!';
387 else if (_rl_complete_show_unmodified)
388 return '@';
389 else
390 return TAB;
391 }
392
393 /************************************/
394 /* */
395 /* Completion utility functions */
396 /* */
397 /************************************/
398
399 /* Set default values for readline word completion. These are the variables
400 that application completion functions can change or inspect. */
401 static void
set_completion_defaults(what_to_do)402 set_completion_defaults (what_to_do)
403 int what_to_do;
404 {
405 /* Only the completion entry function can change these. */
406 rl_filename_completion_desired = 0;
407 rl_filename_quoting_desired = 1;
408 rl_completion_type = what_to_do;
409 rl_completion_suppress_append = rl_completion_suppress_quote = 0;
410
411 /* The completion entry function may optionally change this. */
412 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
413 }
414
415 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
416 static int
get_y_or_n(for_pager)417 get_y_or_n (for_pager)
418 int for_pager;
419 {
420 int c;
421
422 for (;;)
423 {
424 RL_SETSTATE(RL_STATE_MOREINPUT);
425 c = rl_read_key ();
426 RL_UNSETSTATE(RL_STATE_MOREINPUT);
427
428 if (c == 'y' || c == 'Y' || c == ' ')
429 return (1);
430 if (c == 'n' || c == 'N' || c == RUBOUT)
431 return (0);
432 if (c == ABORT_CHAR)
433 _rl_abort_internal ();
434 if (for_pager && (c == NEWLINE || c == RETURN))
435 return (2);
436 if (for_pager && (c == 'q' || c == 'Q'))
437 return (0);
438 rl_ding ();
439 }
440 }
441
442 static int
_rl_internal_pager(lines)443 _rl_internal_pager (lines)
444 int lines;
445 {
446 int i;
447
448 fprintf (rl_outstream, "--More--");
449 fflush (rl_outstream);
450 i = get_y_or_n (1);
451 _rl_erase_entire_line ();
452 if (i == 0)
453 return -1;
454 else if (i == 2)
455 return (lines - 1);
456 else
457 return 0;
458 }
459
460 static int
path_isdir(filename)461 path_isdir (filename)
462 const char *filename;
463 {
464 struct stat finfo;
465
466 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
467 }
468
469 #if defined (VISIBLE_STATS)
470 /* Return the character which best describes FILENAME.
471 `@' for symbolic links
472 `/' for directories
473 `*' for executables
474 `=' for sockets
475 `|' for FIFOs
476 `%' for character special devices
477 `#' for block special devices */
478 static int
stat_char(filename)479 stat_char (filename)
480 char *filename;
481 {
482 struct stat finfo;
483 int character, r;
484
485 #if defined (HAVE_LSTAT) && defined (S_ISLNK)
486 r = lstat (filename, &finfo);
487 #else
488 r = stat (filename, &finfo);
489 #endif
490
491 if (r == -1)
492 return (0);
493
494 character = 0;
495 if (S_ISDIR (finfo.st_mode))
496 character = '/';
497 #if defined (S_ISCHR)
498 else if (S_ISCHR (finfo.st_mode))
499 character = '%';
500 #endif /* S_ISCHR */
501 #if defined (S_ISBLK)
502 else if (S_ISBLK (finfo.st_mode))
503 character = '#';
504 #endif /* S_ISBLK */
505 #if defined (S_ISLNK)
506 else if (S_ISLNK (finfo.st_mode))
507 character = '@';
508 #endif /* S_ISLNK */
509 #if defined (S_ISSOCK)
510 else if (S_ISSOCK (finfo.st_mode))
511 character = '=';
512 #endif /* S_ISSOCK */
513 #if defined (S_ISFIFO)
514 else if (S_ISFIFO (finfo.st_mode))
515 character = '|';
516 #endif
517 else if (S_ISREG (finfo.st_mode))
518 {
519 if (access (filename, X_OK) == 0)
520 character = '*';
521 }
522 return (character);
523 }
524 #endif /* VISIBLE_STATS */
525
526 /* Return the portion of PATHNAME that should be output when listing
527 possible completions. If we are hacking filename completion, we
528 are only interested in the basename, the portion following the
529 final slash. Otherwise, we return what we were passed. Since
530 printing empty strings is not very informative, if we're doing
531 filename completion, and the basename is the empty string, we look
532 for the previous slash and return the portion following that. If
533 there's no previous slash, we just return what we were passed. */
534 static char *
printable_part(pathname)535 printable_part (pathname)
536 char *pathname;
537 {
538 char *temp, *x;
539
540 if (rl_filename_completion_desired == 0) /* don't need to do anything */
541 return (pathname);
542
543 temp = strrchr (pathname, '/');
544 #if defined (__MSDOS__)
545 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
546 temp = pathname + 1;
547 #endif
548
549 if (temp == 0 || *temp == '\0')
550 return (pathname);
551 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
552 Look for a previous slash and, if one is found, return the portion
553 following that slash. If there's no previous slash, just return the
554 pathname we were passed. */
555 else if (temp[1] == '\0')
556 {
557 for (x = temp - 1; x > pathname; x--)
558 if (*x == '/')
559 break;
560 return ((*x == '/') ? x + 1 : pathname);
561 }
562 else
563 return ++temp;
564 }
565
566 /* Compute width of STRING when displayed on screen by print_filename */
567 static int
fnwidth(string)568 fnwidth (string)
569 const char *string;
570 {
571 int width, pos;
572 #if defined (HANDLE_MULTIBYTE)
573 mbstate_t ps;
574 int left, w;
575 size_t clen;
576 wchar_t wc;
577
578 left = strlen (string) + 1;
579 memset (&ps, 0, sizeof (mbstate_t));
580 #endif
581
582 width = pos = 0;
583 while (string[pos])
584 {
585 if (CTRL_CHAR (*string) || *string == RUBOUT)
586 {
587 width += 2;
588 pos++;
589 }
590 else
591 {
592 #if defined (HANDLE_MULTIBYTE)
593 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
594 if (MB_INVALIDCH (clen))
595 {
596 width++;
597 pos++;
598 memset (&ps, 0, sizeof (mbstate_t));
599 }
600 else if (MB_NULLWCH (clen))
601 break;
602 else
603 {
604 pos += clen;
605 w = wcwidth (wc);
606 width += (w >= 0) ? w : 1;
607 }
608 #else
609 width++;
610 pos++;
611 #endif
612 }
613 }
614
615 return width;
616 }
617
618 static int
fnprint(to_print)619 fnprint (to_print)
620 const char *to_print;
621 {
622 int printed_len;
623 const char *s;
624 #if defined (HANDLE_MULTIBYTE)
625 mbstate_t ps;
626 const char *end;
627 size_t tlen;
628 int width, w;
629 wchar_t wc;
630
631 end = to_print + strlen (to_print) + 1;
632 memset (&ps, 0, sizeof (mbstate_t));
633 #endif
634
635 printed_len = 0;
636 s = to_print;
637 while (*s)
638 {
639 if (CTRL_CHAR (*s))
640 {
641 putc ('^', rl_outstream);
642 putc (UNCTRL (*s), rl_outstream);
643 printed_len += 2;
644 s++;
645 #if defined (HANDLE_MULTIBYTE)
646 memset (&ps, 0, sizeof (mbstate_t));
647 #endif
648 }
649 else if (*s == RUBOUT)
650 {
651 putc ('^', rl_outstream);
652 putc ('?', rl_outstream);
653 printed_len += 2;
654 s++;
655 #if defined (HANDLE_MULTIBYTE)
656 memset (&ps, 0, sizeof (mbstate_t));
657 #endif
658 }
659 else
660 {
661 #if defined (HANDLE_MULTIBYTE)
662 tlen = mbrtowc (&wc, s, end - s, &ps);
663 if (MB_INVALIDCH (tlen))
664 {
665 tlen = 1;
666 width = 1;
667 memset (&ps, 0, sizeof (mbstate_t));
668 }
669 else if (MB_NULLWCH (tlen))
670 break;
671 else
672 {
673 w = wcwidth (wc);
674 width = (w >= 0) ? w : 1;
675 }
676 fwrite (s, 1, tlen, rl_outstream);
677 s += tlen;
678 printed_len += width;
679 #else
680 putc (*s, rl_outstream);
681 s++;
682 printed_len++;
683 #endif
684 }
685 }
686
687 return printed_len;
688 }
689
690 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
691 are using it, check for and output a single character for `special'
692 filenames. Return the number of characters we output. */
693
694 static int
print_filename(to_print,full_pathname)695 print_filename (to_print, full_pathname)
696 char *to_print, *full_pathname;
697 {
698 int printed_len, extension_char, slen, tlen;
699 char *s, c, *new_full_pathname;
700 const char *dn;
701
702 extension_char = 0;
703 printed_len = fnprint (to_print);
704
705 #if defined (VISIBLE_STATS)
706 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
707 #else
708 if (rl_filename_completion_desired && _rl_complete_mark_directories)
709 #endif
710 {
711 /* If to_print != full_pathname, to_print is the basename of the
712 path passed. In this case, we try to expand the directory
713 name before checking for the stat character. */
714 if (to_print != full_pathname)
715 {
716 /* Terminate the directory name. */
717 c = to_print[-1];
718 to_print[-1] = '\0';
719
720 /* If setting the last slash in full_pathname to a NUL results in
721 full_pathname being the empty string, we are trying to complete
722 files in the root directory. If we pass a null string to the
723 bash directory completion hook, for example, it will expand it
724 to the current directory. We just want the `/'. */
725 if (full_pathname == 0 || *full_pathname == 0)
726 dn = "/";
727 else if (full_pathname[0] != '/')
728 dn = full_pathname;
729 else if (full_pathname[1] == 0)
730 dn = "//"; /* restore trailing slash to `//' */
731 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
732 dn = "/"; /* don't turn /// into // */
733 else
734 dn = full_pathname;
735 s = tilde_expand (dn);
736 if (rl_directory_completion_hook)
737 (*rl_directory_completion_hook) (&s);
738
739 slen = strlen (s);
740 tlen = strlen (to_print);
741 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
742 strcpy (new_full_pathname, s);
743 if (s[slen - 1] == '/')
744 slen--;
745 else
746 new_full_pathname[slen] = '/';
747 new_full_pathname[slen] = '/';
748 strcpy (new_full_pathname + slen + 1, to_print);
749
750 #if defined (VISIBLE_STATS)
751 if (rl_visible_stats)
752 extension_char = stat_char (new_full_pathname);
753 else
754 #endif
755 if (path_isdir (new_full_pathname))
756 extension_char = '/';
757
758 free (new_full_pathname);
759 to_print[-1] = c;
760 }
761 else
762 {
763 s = tilde_expand (full_pathname);
764 #if defined (VISIBLE_STATS)
765 if (rl_visible_stats)
766 extension_char = stat_char (s);
767 else
768 #endif
769 if (path_isdir (s))
770 extension_char = '/';
771 }
772
773 free (s);
774 if (extension_char)
775 {
776 putc (extension_char, rl_outstream);
777 printed_len++;
778 }
779 }
780
781 return printed_len;
782 }
783
784 static char *
rl_quote_filename(s,rtype,qcp)785 rl_quote_filename (s, rtype, qcp)
786 char *s;
787 int rtype __attribute__((unused));
788 char *qcp;
789 {
790 char *r;
791
792 r = (char *)xmalloc (strlen (s) + 2);
793 *r = *rl_completer_quote_characters;
794 strcpy (r + 1, s);
795 if (qcp)
796 *qcp = *rl_completer_quote_characters;
797 return r;
798 }
799
800 /* Find the bounds of the current word for completion purposes, and leave
801 rl_point set to the end of the word. This function skips quoted
802 substrings (characters between matched pairs of characters in
803 rl_completer_quote_characters). First we try to find an unclosed
804 quoted substring on which to do matching. If one is not found, we use
805 the word break characters to find the boundaries of the current word.
806 We call an application-specific function to decide whether or not a
807 particular word break character is quoted; if that function returns a
808 non-zero result, the character does not break a word. This function
809 returns the opening quote character if we found an unclosed quoted
810 substring, '\0' otherwise. FP, if non-null, is set to a value saying
811 which (shell-like) quote characters we found (single quote, double
812 quote, or backslash) anywhere in the string. DP, if non-null, is set to
813 the value of the delimiter character that caused a word break. */
814
815 char
_rl_find_completion_word(fp,dp)816 _rl_find_completion_word (fp, dp)
817 int *fp, *dp;
818 {
819 int scan, end, found_quote, delimiter, pass_next, isbrk;
820 char quote_char, *brkchars;
821
822 end = rl_point;
823 found_quote = delimiter = 0;
824 quote_char = '\0';
825
826 brkchars = 0;
827 if (rl_completion_word_break_hook)
828 brkchars = (*rl_completion_word_break_hook) ();
829 if (brkchars == 0)
830 brkchars = rl_completer_word_break_characters;
831
832 if (rl_completer_quote_characters)
833 {
834 /* We have a list of characters which can be used in pairs to
835 quote substrings for the completer. Try to find the start
836 of an unclosed quoted substring. */
837 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
838 for (scan = pass_next = 0; scan < end; scan = MB_NEXTCHAR (rl_line_buffer, scan, 1, MB_FIND_ANY))
839 {
840 if (pass_next)
841 {
842 pass_next = 0;
843 continue;
844 }
845
846 /* Shell-like semantics for single quotes -- don't allow backslash
847 to quote anything in single quotes, especially not the closing
848 quote. If you don't like this, take out the check on the value
849 of quote_char. */
850 if (quote_char != '\'' && rl_line_buffer[scan] == '\\')
851 {
852 pass_next = 1;
853 found_quote |= RL_QF_BACKSLASH;
854 continue;
855 }
856
857 if (quote_char != '\0')
858 {
859 /* Ignore everything until the matching close quote char. */
860 if (rl_line_buffer[scan] == quote_char)
861 {
862 /* Found matching close. Abandon this substring. */
863 quote_char = '\0';
864 rl_point = end;
865 }
866 }
867 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
868 {
869 /* Found start of a quoted substring. */
870 quote_char = rl_line_buffer[scan];
871 rl_point = scan + 1;
872 /* Shell-like quoting conventions. */
873 if (quote_char == '\'')
874 found_quote |= RL_QF_SINGLE_QUOTE;
875 else if (quote_char == '"')
876 found_quote |= RL_QF_DOUBLE_QUOTE;
877 else
878 found_quote |= RL_QF_OTHER_QUOTE;
879 }
880 }
881 }
882
883 if (rl_point == end && quote_char == '\0')
884 {
885 /* We didn't find an unclosed quoted substring upon which to do
886 completion, so use the word break characters to find the
887 substring on which to complete. */
888 while ((rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_ANY)))
889 {
890 scan = rl_line_buffer[rl_point];
891
892 if (strchr (brkchars, scan) == 0)
893 continue;
894
895 /* Call the application-specific function to tell us whether
896 this word break character is quoted and should be skipped. */
897 if (rl_char_is_quoted_p && found_quote &&
898 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
899 continue;
900
901 /* Convoluted code, but it avoids an n^2 algorithm with calls
902 to char_is_quoted. */
903 break;
904 }
905 }
906
907 /* If we are at an unquoted word break, then advance past it. */
908 scan = rl_line_buffer[rl_point];
909
910 /* If there is an application-specific function to say whether or not
911 a character is quoted and we found a quote character, let that
912 function decide whether or not a character is a word break, even
913 if it is found in rl_completer_word_break_characters. Don't bother
914 if we're at the end of the line, though. */
915 if (scan)
916 {
917 if (rl_char_is_quoted_p)
918 isbrk = (found_quote == 0 ||
919 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
920 strchr (brkchars, scan) != 0;
921 else
922 isbrk = strchr (brkchars, scan) != 0;
923
924 if (isbrk)
925 {
926 /* If the character that caused the word break was a quoting
927 character, then remember it as the delimiter. */
928 if (rl_basic_quote_characters &&
929 strchr (rl_basic_quote_characters, scan) &&
930 (end - rl_point) > 1)
931 delimiter = scan;
932
933 /* If the character isn't needed to determine something special
934 about what kind of completion to perform, then advance past it. */
935 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0)
936 rl_point++;
937 }
938 }
939
940 if (fp)
941 *fp = found_quote;
942 if (dp)
943 *dp = delimiter;
944
945 return (quote_char);
946 }
947
948 static char **
gen_completion_matches(text,start,end,our_func,found_quote,quote_char)949 gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
950 char *text;
951 int start, end;
952 rl_compentry_func_t *our_func;
953 int found_quote, quote_char;
954 {
955 char **matches;
956
957 rl_completion_found_quote = found_quote;
958 rl_completion_quote_character = quote_char;
959
960 /* If the user wants to TRY to complete, but then wants to give
961 up and use the default completion function, they set the
962 variable rl_attempted_completion_function. */
963 if (rl_attempted_completion_function)
964 {
965 matches = (*rl_attempted_completion_function) (text, start, end);
966
967 if (matches || rl_attempted_completion_over)
968 {
969 rl_attempted_completion_over = 0;
970 return (matches);
971 }
972 }
973
974 /* XXX -- filename dequoting moved into rl_filename_completion_function */
975
976 matches = rl_completion_matches (text, our_func);
977 return matches;
978 }
979
980 /* Filter out duplicates in MATCHES. This frees up the strings in
981 MATCHES. */
982 static char **
remove_duplicate_matches(matches)983 remove_duplicate_matches (matches)
984 char **matches;
985 {
986 char *lowest_common;
987 int i, j, newlen;
988 char dead_slot;
989 char **temp_array;
990
991 /* Sort the items. */
992 for (i = 0; matches[i]; i++)
993 ;
994
995 /* Sort the array without matches[0], since we need it to
996 stay in place no matter what. */
997 if (i)
998 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
999
1000 /* Remember the lowest common denominator for it may be unique. */
1001 lowest_common = savestring (matches[0]);
1002
1003 for (i = newlen = 0; matches[i + 1]; i++)
1004 {
1005 if (strcmp (matches[i], matches[i + 1]) == 0)
1006 {
1007 free (matches[i]);
1008 matches[i] = (char *)&dead_slot;
1009 }
1010 else
1011 newlen++;
1012 }
1013
1014 /* We have marked all the dead slots with (char *)&dead_slot.
1015 Copy all the non-dead entries into a new array. */
1016 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
1017 for (i = j = 1; matches[i]; i++)
1018 {
1019 if (matches[i] != (char *)&dead_slot)
1020 temp_array[j++] = matches[i];
1021 }
1022 temp_array[j] = (char *)NULL;
1023
1024 if (matches[0] != (char *)&dead_slot)
1025 free (matches[0]);
1026
1027 /* Place the lowest common denominator back in [0]. */
1028 temp_array[0] = lowest_common;
1029
1030 /* If there is one string left, and it is identical to the
1031 lowest common denominator, then the LCD is the string to
1032 insert. */
1033 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
1034 {
1035 free (temp_array[1]);
1036 temp_array[1] = (char *)NULL;
1037 }
1038 return (temp_array);
1039 }
1040
1041 /* Find the common prefix of the list of matches, and put it into
1042 matches[0]. */
1043 static int
compute_lcd_of_matches(match_list,matches,text)1044 compute_lcd_of_matches (match_list, matches, text)
1045 char **match_list;
1046 int matches;
1047 const char *text;
1048 {
1049 register int i, c1, c2, si;
1050 int low; /* Count of max-matched characters. */
1051 char *dtext; /* dequoted TEXT, if needed */
1052 #if defined (HANDLE_MULTIBYTE)
1053 int v;
1054 mbstate_t ps1, ps2;
1055 wchar_t wc1, wc2;
1056 #endif
1057
1058 /* If only one match, just use that. Otherwise, compare each
1059 member of the list with the next, finding out where they
1060 stop matching. */
1061 if (matches == 1)
1062 {
1063 match_list[0] = match_list[1];
1064 match_list[1] = (char *)NULL;
1065 return 1;
1066 }
1067
1068 for (i = 1, low = 100000; i < matches; i++)
1069 {
1070 #if defined (HANDLE_MULTIBYTE)
1071 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1072 {
1073 memset (&ps1, 0, sizeof (mbstate_t));
1074 memset (&ps2, 0, sizeof (mbstate_t));
1075 }
1076 #endif
1077 if (_rl_completion_case_fold)
1078 {
1079 for (si = 0;
1080 (c1 = _rl_to_lower(match_list[i][si])) &&
1081 (c2 = _rl_to_lower(match_list[i + 1][si]));
1082 si++)
1083 #if defined (HANDLE_MULTIBYTE)
1084 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1085 {
1086 v = mbrtowc (&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1);
1087 mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2);
1088 wc1 = towlower (wc1);
1089 wc2 = towlower (wc2);
1090 if (wc1 != wc2)
1091 break;
1092 else if (v > 1)
1093 si += v - 1;
1094 }
1095 else
1096 #endif
1097 if (c1 != c2)
1098 break;
1099 }
1100 else
1101 {
1102 for (si = 0;
1103 (c1 = match_list[i][si]) &&
1104 (c2 = match_list[i + 1][si]);
1105 si++)
1106 #if defined (HANDLE_MULTIBYTE)
1107 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1108 {
1109 mbstate_t ps_back;
1110 ps_back = ps1;
1111 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2))
1112 break;
1113 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1)
1114 si += v - 1;
1115 }
1116 else
1117 #endif
1118 if (c1 != c2)
1119 break;
1120 }
1121
1122 if (low > si)
1123 low = si;
1124 }
1125
1126 /* If there were multiple matches, but none matched up to even the
1127 first character, and the user typed something, use that as the
1128 value of matches[0]. */
1129 if (low == 0 && text && *text)
1130 {
1131 match_list[0] = (char *)xmalloc (strlen (text) + 1);
1132 strcpy (match_list[0], text);
1133 }
1134 else
1135 {
1136 match_list[0] = (char *)xmalloc (low + 1);
1137
1138 /* XXX - this might need changes in the presence of multibyte chars */
1139
1140 /* If we are ignoring case, try to preserve the case of the string
1141 the user typed in the face of multiple matches differing in case. */
1142 if (_rl_completion_case_fold)
1143 {
1144 /* We're making an assumption here:
1145 IF we're completing filenames AND
1146 the application has defined a filename dequoting function AND
1147 we found a quote character AND
1148 the application has requested filename quoting
1149 THEN
1150 we assume that TEXT was dequoted before checking against
1151 the file system and needs to be dequoted here before we
1152 check against the list of matches
1153 FI */
1154 dtext = (char *)NULL;
1155 if (rl_filename_completion_desired &&
1156 rl_filename_dequoting_function &&
1157 rl_completion_found_quote &&
1158 rl_filename_quoting_desired)
1159 {
1160 dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
1161 text = dtext;
1162 }
1163
1164 /* sort the list to get consistent answers. */
1165 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1166
1167 si = strlen (text);
1168 if (si <= low)
1169 {
1170 for (i = 1; i <= matches; i++)
1171 if (strncmp (match_list[i], text, si) == 0)
1172 {
1173 strncpy (match_list[0], match_list[i], low);
1174 break;
1175 }
1176 /* no casematch, use first entry */
1177 if (i > matches)
1178 strncpy (match_list[0], match_list[1], low);
1179 }
1180 else
1181 /* otherwise, just use the text the user typed. */
1182 strncpy (match_list[0], text, low);
1183
1184 FREE (dtext);
1185 }
1186 else
1187 strncpy (match_list[0], match_list[1], low);
1188
1189 match_list[0][low] = '\0';
1190 }
1191
1192 return matches;
1193 }
1194
1195 static int
postprocess_matches(matchesp,matching_filenames)1196 postprocess_matches (matchesp, matching_filenames)
1197 char ***matchesp;
1198 int matching_filenames;
1199 {
1200 char *t, **matches, **temp_matches;
1201 int nmatch, i;
1202
1203 matches = *matchesp;
1204
1205 if (matches == 0)
1206 return 0;
1207
1208 /* It seems to me that in all the cases we handle we would like
1209 to ignore duplicate possiblilities. Scan for the text to
1210 insert being identical to the other completions. */
1211 if (rl_ignore_completion_duplicates)
1212 {
1213 temp_matches = remove_duplicate_matches (matches);
1214 free (matches);
1215 matches = temp_matches;
1216 }
1217
1218 /* If we are matching filenames, then here is our chance to
1219 do clever processing by re-examining the list. Call the
1220 ignore function with the array as a parameter. It can
1221 munge the array, deleting matches as it desires. */
1222 if (rl_ignore_some_completions_function && matching_filenames)
1223 {
1224 for (nmatch = 1; matches[nmatch]; nmatch++)
1225 ;
1226 (void)(*rl_ignore_some_completions_function) (matches);
1227 if (matches == 0 || matches[0] == 0)
1228 {
1229 FREE (matches);
1230 *matchesp = (char **)0;
1231 return 0;
1232 }
1233 else
1234 {
1235 /* If we removed some matches, recompute the common prefix. */
1236 for (i = 1; matches[i]; i++)
1237 ;
1238 if (i > 1 && i < nmatch)
1239 {
1240 t = matches[0];
1241 compute_lcd_of_matches (matches, i - 1, t);
1242 FREE (t);
1243 }
1244 }
1245 }
1246
1247 *matchesp = matches;
1248 return (1);
1249 }
1250
1251 /* A convenience function for displaying a list of strings in
1252 columnar format on readline's output stream. MATCHES is the list
1253 of strings, in argv format, LEN is the number of strings in MATCHES,
1254 and MAX is the length of the longest string in MATCHES. */
1255 void
rl_display_match_list(matches,len,max)1256 rl_display_match_list (matches, len, max)
1257 char **matches;
1258 int len, max;
1259 {
1260 int count, limit, printed_len, lines;
1261 int i, j, k, l;
1262 char *temp;
1263
1264 /* How many items of MAX length can we fit in the screen window? */
1265 max += 2;
1266 limit = _rl_screenwidth / max;
1267 if (limit != 1 && (limit * max == _rl_screenwidth))
1268 limit--;
1269
1270 /* Avoid a possible floating exception. If max > _rl_screenwidth,
1271 limit will be 0 and a divide-by-zero fault will result. */
1272 if (limit == 0)
1273 limit = 1;
1274
1275 /* How many iterations of the printing loop? */
1276 count = (len + (limit - 1)) / limit;
1277
1278 /* Watch out for special case. If LEN is less than LIMIT, then
1279 just do the inner printing loop.
1280 0 < len <= limit implies count = 1. */
1281
1282 /* Sort the items if they are not already sorted. */
1283 if (rl_ignore_completion_duplicates == 0)
1284 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1285
1286 rl_crlf ();
1287
1288 lines = 0;
1289 if (_rl_print_completions_horizontally == 0)
1290 {
1291 /* Print the sorted items, up-and-down alphabetically, like ls. */
1292 for (i = 1; i <= count; i++)
1293 {
1294 for (j = 0, l = i; j < limit; j++)
1295 {
1296 if (l > len || matches[l] == 0)
1297 break;
1298 else
1299 {
1300 temp = printable_part (matches[l]);
1301 printed_len = print_filename (temp, matches[l]);
1302
1303 if (j + 1 < limit)
1304 for (k = 0; k < max - printed_len; k++)
1305 putc (' ', rl_outstream);
1306 }
1307 l += count;
1308 }
1309 rl_crlf ();
1310 lines++;
1311 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count)
1312 {
1313 lines = _rl_internal_pager (lines);
1314 if (lines < 0)
1315 return;
1316 }
1317 }
1318 }
1319 else
1320 {
1321 /* Print the sorted items, across alphabetically, like ls -x. */
1322 for (i = 1; matches[i]; i++)
1323 {
1324 temp = printable_part (matches[i]);
1325 printed_len = print_filename (temp, matches[i]);
1326 /* Have we reached the end of this line? */
1327 if (matches[i+1])
1328 {
1329 if (i && (limit > 1) && (i % limit) == 0)
1330 {
1331 rl_crlf ();
1332 lines++;
1333 if (_rl_page_completions && lines >= _rl_screenheight - 1)
1334 {
1335 lines = _rl_internal_pager (lines);
1336 if (lines < 0)
1337 return;
1338 }
1339 }
1340 else
1341 for (k = 0; k < max - printed_len; k++)
1342 putc (' ', rl_outstream);
1343 }
1344 }
1345 rl_crlf ();
1346 }
1347 }
1348
1349 /* Display MATCHES, a list of matching filenames in argv format. This
1350 handles the simple case -- a single match -- first. If there is more
1351 than one match, we compute the number of strings in the list and the
1352 length of the longest string, which will be needed by the display
1353 function. If the application wants to handle displaying the list of
1354 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the
1355 address of a function, and we just call it. If we're handling the
1356 display ourselves, we just call rl_display_match_list. We also check
1357 that the list of matches doesn't exceed the user-settable threshold,
1358 and ask the user if he wants to see the list if there are more matches
1359 than RL_COMPLETION_QUERY_ITEMS. */
1360 static void
display_matches(matches)1361 display_matches (matches)
1362 char **matches;
1363 {
1364 int len, max, i;
1365 char *temp;
1366
1367 /* Move to the last visible line of a possibly-multiple-line command. */
1368 _rl_move_vert (_rl_vis_botlin);
1369
1370 /* Handle simple case first. What if there is only one answer? */
1371 if (matches[1] == 0)
1372 {
1373 temp = printable_part (matches[0]);
1374 rl_crlf ();
1375 print_filename (temp, matches[0]);
1376 rl_crlf ();
1377
1378 rl_forced_update_display ();
1379 rl_display_fixed = 1;
1380
1381 return;
1382 }
1383
1384 /* There is more than one answer. Find out how many there are,
1385 and find the maximum printed length of a single entry. */
1386 for (max = 0, i = 1; matches[i]; i++)
1387 {
1388 temp = printable_part (matches[i]);
1389 len = fnwidth (temp);
1390
1391 if (len > max)
1392 max = len;
1393 }
1394
1395 len = i - 1;
1396
1397 /* If the caller has defined a display hook, then call that now. */
1398 if (rl_completion_display_matches_hook)
1399 {
1400 (*rl_completion_display_matches_hook) (matches, len, max);
1401 return;
1402 }
1403
1404 /* If there are many items, then ask the user if she really wants to
1405 see them all. */
1406 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
1407 {
1408 rl_crlf ();
1409 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1410 fflush (rl_outstream);
1411 if (get_y_or_n (0) == 0)
1412 {
1413 rl_crlf ();
1414
1415 rl_forced_update_display ();
1416 rl_display_fixed = 1;
1417
1418 return;
1419 }
1420 }
1421
1422 rl_display_match_list (matches, len, max);
1423
1424 rl_forced_update_display ();
1425 rl_display_fixed = 1;
1426 }
1427
1428 static char *
make_quoted_replacement(match,mtype,qc)1429 make_quoted_replacement (match, mtype, qc)
1430 char *match;
1431 int mtype;
1432 char *qc; /* Pointer to quoting character, if any */
1433 {
1434 int should_quote, do_replace;
1435 char *replacement;
1436
1437 /* If we are doing completion on quoted substrings, and any matches
1438 contain any of the completer_word_break_characters, then auto-
1439 matically prepend the substring with a quote character (just pick
1440 the first one from the list of such) if it does not already begin
1441 with a quote string. FIXME: Need to remove any such automatically
1442 inserted quote character when it no longer is necessary, such as
1443 if we change the string we are completing on and the new set of
1444 matches don't require a quoted substring. */
1445 replacement = match;
1446
1447 should_quote = match && rl_completer_quote_characters &&
1448 rl_filename_completion_desired &&
1449 rl_filename_quoting_desired;
1450
1451 if (should_quote)
1452 should_quote = should_quote && (!qc || !*qc ||
1453 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc)));
1454
1455 if (should_quote)
1456 {
1457 /* If there is a single match, see if we need to quote it.
1458 This also checks whether the common prefix of several
1459 matches needs to be quoted. */
1460 should_quote = rl_filename_quote_characters
1461 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0)
1462 : 0;
1463
1464 do_replace = should_quote ? mtype : NO_MATCH;
1465 /* Quote the replacement, since we found an embedded
1466 word break character in a potential match. */
1467 if (do_replace != NO_MATCH && rl_filename_quoting_function)
1468 replacement = (*rl_filename_quoting_function) (match, do_replace, qc);
1469 }
1470 return (replacement);
1471 }
1472
1473 static void
insert_match(match,start,mtype,qc)1474 insert_match (match, start, mtype, qc)
1475 char *match;
1476 int start, mtype;
1477 char *qc;
1478 {
1479 char *replacement;
1480 char oqc;
1481
1482 oqc = qc ? *qc : '\0';
1483 replacement = make_quoted_replacement (match, mtype, qc);
1484
1485 /* Now insert the match. */
1486 if (replacement)
1487 {
1488 /* Don't double an opening quote character. */
1489 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc &&
1490 replacement[0] == *qc)
1491 start--;
1492 /* If make_quoted_replacement changed the quoting character, remove
1493 the opening quote and insert the (fully-quoted) replacement. */
1494 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc &&
1495 replacement[0] != oqc)
1496 start--;
1497 _rl_replace_text (replacement, start, rl_point - 1);
1498 if (replacement != match)
1499 free (replacement);
1500 }
1501 }
1502
1503 /* Append any necessary closing quote and a separator character to the
1504 just-inserted match. If the user has specified that directories
1505 should be marked by a trailing `/', append one of those instead. The
1506 default trailing character is a space. Returns the number of characters
1507 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS
1508 has them) and don't add a suffix for a symlink to a directory. A
1509 nontrivial match is one that actually adds to the word being completed.
1510 The variable rl_completion_mark_symlink_dirs controls this behavior
1511 (it's initially set to the what the user has chosen, indicated by the
1512 value of _rl_complete_mark_symlink_dirs, but may be modified by an
1513 application's completion function). */
1514 static int
append_to_match(text,delimiter,quote_char,nontrivial_match)1515 append_to_match (text, delimiter, quote_char, nontrivial_match)
1516 char *text;
1517 int delimiter, quote_char, nontrivial_match;
1518 {
1519 char temp_string[4], *filename;
1520 int temp_string_index, s;
1521 struct stat finfo;
1522
1523 temp_string_index = 0;
1524 if (quote_char && rl_point && rl_completion_suppress_quote == 0 &&
1525 rl_line_buffer[rl_point - 1] != quote_char)
1526 temp_string[temp_string_index++] = quote_char;
1527
1528 if (delimiter)
1529 temp_string[temp_string_index++] = delimiter;
1530 else if (rl_completion_suppress_append == 0 && rl_completion_append_character)
1531 temp_string[temp_string_index++] = rl_completion_append_character;
1532
1533 temp_string[temp_string_index++] = '\0';
1534
1535 if (rl_filename_completion_desired)
1536 {
1537 filename = tilde_expand (text);
1538 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1539 ? LSTAT (filename, &finfo)
1540 : stat (filename, &finfo);
1541 if (s == 0 && S_ISDIR (finfo.st_mode))
1542 {
1543 if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */)
1544 {
1545 /* This is clumsy. Avoid putting in a double slash if point
1546 is at the end of the line and the previous character is a
1547 slash. */
1548 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1549 ;
1550 else if (rl_line_buffer[rl_point] != '/')
1551 rl_insert_text ("/");
1552 }
1553 }
1554 #ifdef S_ISLNK
1555 /* Don't add anything if the filename is a symlink and resolves to a
1556 directory. */
1557 else if (s == 0 && S_ISLNK (finfo.st_mode) &&
1558 stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode))
1559 ;
1560 #endif
1561 else
1562 {
1563 if (rl_point == rl_end && temp_string_index)
1564 rl_insert_text (temp_string);
1565 }
1566 free (filename);
1567 }
1568 else
1569 {
1570 if (rl_point == rl_end && temp_string_index)
1571 rl_insert_text (temp_string);
1572 }
1573
1574 return (temp_string_index);
1575 }
1576
1577 static void
insert_all_matches(matches,point,qc)1578 insert_all_matches (matches, point, qc)
1579 char **matches;
1580 int point;
1581 char *qc;
1582 {
1583 int i;
1584 char *rp;
1585
1586 rl_begin_undo_group ();
1587 /* remove any opening quote character; make_quoted_replacement will add
1588 it back. */
1589 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc)
1590 point--;
1591 rl_delete_text (point, rl_point);
1592 rl_point = point;
1593
1594 if (matches[1])
1595 {
1596 for (i = 1; matches[i]; i++)
1597 {
1598 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc);
1599 rl_insert_text (rp);
1600 rl_insert_text (" ");
1601 if (rp != matches[i])
1602 free (rp);
1603 }
1604 }
1605 else
1606 {
1607 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc);
1608 rl_insert_text (rp);
1609 rl_insert_text (" ");
1610 if (rp != matches[0])
1611 free (rp);
1612 }
1613 rl_end_undo_group ();
1614 }
1615
1616 void
_rl_free_match_list(matches)1617 _rl_free_match_list (matches)
1618 char **matches;
1619 {
1620 register int i;
1621
1622 if (matches == 0)
1623 return;
1624
1625 for (i = 0; matches[i]; i++)
1626 free (matches[i]);
1627 free (matches);
1628 }
1629
1630 /* Complete the word at or before point.
1631 WHAT_TO_DO says what to do with the completion.
1632 `?' means list the possible completions.
1633 TAB means do standard completion.
1634 `*' means insert all of the possible completions.
1635 `!' means to do standard completion, and list all possible completions if
1636 there is more than one.
1637 `@' means to do standard completion, and list all possible completions if
1638 there is more than one and partial completion is not possible. */
1639 int
rl_complete_internal(what_to_do)1640 rl_complete_internal (what_to_do)
1641 int what_to_do;
1642 {
1643 char **matches;
1644 rl_compentry_func_t *our_func;
1645 int start, end, delimiter, found_quote, i, nontrivial_lcd;
1646 char *text, *saved_line_buffer;
1647 char quote_char;
1648
1649 RL_SETSTATE(RL_STATE_COMPLETING);
1650
1651 set_completion_defaults (what_to_do);
1652
1653 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
1654 our_func = rl_completion_entry_function
1655 ? rl_completion_entry_function
1656 : rl_filename_completion_function;
1657 /* We now look backwards for the start of a filename/variable word. */
1658 end = rl_point;
1659 found_quote = delimiter = 0;
1660 quote_char = '\0';
1661
1662 if (rl_point)
1663 /* This (possibly) changes rl_point. If it returns a non-zero char,
1664 we know we have an open quote. */
1665 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
1666
1667 start = rl_point;
1668 rl_point = end;
1669
1670 text = rl_copy_text (start, end);
1671 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
1672 /* nontrivial_lcd is set if the common prefix adds something to the word
1673 being completed. */
1674 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0;
1675 free (text);
1676
1677 if (matches == 0)
1678 {
1679 rl_ding ();
1680 FREE (saved_line_buffer);
1681 completion_changed_buffer = 0;
1682 RL_UNSETSTATE(RL_STATE_COMPLETING);
1683 return (0);
1684 }
1685
1686 /* If we are matching filenames, the attempted completion function will
1687 have set rl_filename_completion_desired to a non-zero value. The basic
1688 rl_filename_completion_function does this. */
1689 i = rl_filename_completion_desired;
1690
1691 if (postprocess_matches (&matches, i) == 0)
1692 {
1693 rl_ding ();
1694 FREE (saved_line_buffer);
1695 completion_changed_buffer = 0;
1696 RL_UNSETSTATE(RL_STATE_COMPLETING);
1697 return (0);
1698 }
1699
1700 switch (what_to_do)
1701 {
1702 case TAB:
1703 case '!':
1704 case '@':
1705 /* Insert the first match with proper quoting. */
1706 if (*matches[0])
1707 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char);
1708
1709 /* If there are more matches, ring the bell to indicate.
1710 If we are in vi mode, Posix.2 says to not ring the bell.
1711 If the `show-all-if-ambiguous' variable is set, display
1712 all the matches immediately. Otherwise, if this was the
1713 only match, and we are hacking files, check the file to
1714 see if it was a directory. If so, and the `mark-directories'
1715 variable is set, add a '/' to the name. If not, and we
1716 are at the end of the line, then add a space. */
1717 if (matches[1])
1718 {
1719 if (what_to_do == '!')
1720 {
1721 display_matches (matches);
1722 break;
1723 }
1724 else if (what_to_do == '@')
1725 {
1726 if (nontrivial_lcd == 0)
1727 display_matches (matches);
1728 break;
1729 }
1730 else if (rl_editing_mode != vi_mode)
1731 rl_ding (); /* There are other matches remaining. */
1732 }
1733 else
1734 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
1735
1736 break;
1737
1738 case '*':
1739 insert_all_matches (matches, start, "e_char);
1740 break;
1741
1742 case '?':
1743 display_matches (matches);
1744 break;
1745
1746 default:
1747 fprintf (stderr, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do);
1748 rl_ding ();
1749 FREE (saved_line_buffer);
1750 RL_UNSETSTATE(RL_STATE_COMPLETING);
1751 return 1;
1752 }
1753
1754 _rl_free_match_list (matches);
1755
1756 /* Check to see if the line has changed through all of this manipulation. */
1757 if (saved_line_buffer)
1758 {
1759 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0;
1760 free (saved_line_buffer);
1761 }
1762
1763 RL_UNSETSTATE(RL_STATE_COMPLETING);
1764 return 0;
1765 }
1766
1767 /***************************************************************/
1768 /* */
1769 /* Application-callable completion match generator functions */
1770 /* */
1771 /***************************************************************/
1772
1773 /* Return an array of (char *) which is a list of completions for TEXT.
1774 If there are no completions, return a NULL pointer.
1775 The first entry in the returned array is the substitution for TEXT.
1776 The remaining entries are the possible completions.
1777 The array is terminated with a NULL pointer.
1778
1779 ENTRY_FUNCTION is a function of two args, and returns a (char *).
1780 The first argument is TEXT.
1781 The second is a state argument; it should be zero on the first call, and
1782 non-zero on subsequent calls. It returns a NULL pointer to the caller
1783 when there are no more matches.
1784 */
1785 char **
rl_completion_matches(text,entry_function)1786 rl_completion_matches (text, entry_function)
1787 const char *text;
1788 rl_compentry_func_t *entry_function;
1789 {
1790 /* Number of slots in match_list. */
1791 int match_list_size;
1792
1793 /* The list of matches. */
1794 char **match_list;
1795
1796 /* Number of matches actually found. */
1797 int matches;
1798
1799 /* Temporary string binder. */
1800 char *string;
1801
1802 matches = 0;
1803 match_list_size = 10;
1804 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *));
1805 match_list[1] = (char *)NULL;
1806
1807 while ((string = (*entry_function) (text, matches)))
1808 {
1809 if (matches + 1 == match_list_size)
1810 match_list = (char **)xrealloc
1811 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
1812
1813 match_list[++matches] = string;
1814 match_list[matches + 1] = (char *)NULL;
1815 }
1816
1817 /* If there were any matches, then look through them finding out the
1818 lowest common denominator. That then becomes match_list[0]. */
1819 if (matches)
1820 compute_lcd_of_matches (match_list, matches, text);
1821 else /* There were no matches. */
1822 {
1823 free (match_list);
1824 match_list = (char **)NULL;
1825 }
1826 return (match_list);
1827 }
1828
1829 /* A completion function for usernames.
1830 TEXT contains a partial username preceded by a random
1831 character (usually `~'). */
1832 char *
rl_username_completion_function(text,state)1833 rl_username_completion_function (text, state)
1834 const char *text;
1835 int state;
1836 {
1837 #if defined (__WIN32__) || defined (__OPENNT)
1838 return (char *)NULL;
1839 #else /* !__WIN32__ && !__OPENNT) */
1840 static char *username = (char *)NULL;
1841 static struct passwd *entry;
1842 static int first_char, first_char_loc;
1843 char *value;
1844 #if defined (HAVE_GETPWENT)
1845 static int namelen;
1846 #endif
1847
1848 if (state == 0)
1849 {
1850 FREE (username);
1851
1852 first_char = *text;
1853 first_char_loc = first_char == '~';
1854
1855 username = savestring (&text[first_char_loc]);
1856 #if defined (HAVE_GETPWENT)
1857 namelen = strlen (username);
1858 #endif
1859 setpwent ();
1860 }
1861
1862 #if defined (HAVE_GETPWENT)
1863 while (entry = getpwent ())
1864 {
1865 /* Null usernames should result in all users as possible completions. */
1866 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
1867 break;
1868 }
1869 #endif
1870
1871 if (entry == 0)
1872 {
1873 #if defined (HAVE_GETPWENT)
1874 endpwent ();
1875 #endif
1876 return ((char *)NULL);
1877 }
1878 else
1879 {
1880 value = (char *)xmalloc (2 + strlen (entry->pw_name));
1881
1882 *value = *text;
1883
1884 strcpy (value + first_char_loc, entry->pw_name);
1885
1886 if (first_char == '~')
1887 rl_filename_completion_desired = 1;
1888
1889 return (value);
1890 }
1891 #endif /* !__WIN32__ && !__OPENNT */
1892 }
1893
1894 /* Okay, now we write the entry_function for filename completion. In the
1895 general case. Note that completion in the shell is a little different
1896 because of all the pathnames that must be followed when looking up the
1897 completion for a command. */
1898 char *
rl_filename_completion_function(text,state)1899 rl_filename_completion_function (text, state)
1900 const char *text;
1901 int state;
1902 {
1903 static DIR *directory = (DIR *)NULL;
1904 static char *filename = (char *)NULL;
1905 static char *dirname = (char *)NULL;
1906 static char *users_dirname = (char *)NULL;
1907 static int filename_len;
1908 char *temp;
1909 int dirlen;
1910 struct dirent *entry;
1911
1912 /* If we don't have any state, then do some initialization. */
1913 if (state == 0)
1914 {
1915 /* If we were interrupted before closing the directory or reading
1916 all of its contents, close it. */
1917 if (directory)
1918 {
1919 closedir (directory);
1920 directory = (DIR *)NULL;
1921 }
1922 FREE (dirname);
1923 FREE (filename);
1924 FREE (users_dirname);
1925
1926 filename = savestring (text);
1927 if (*text == 0)
1928 text = ".";
1929 dirname = savestring (text);
1930
1931 temp = strrchr (dirname, '/');
1932
1933 #if defined (__MSDOS__)
1934 /* special hack for //X/... */
1935 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/')
1936 temp = strrchr (dirname + 3, '/');
1937 #endif
1938
1939 if (temp)
1940 {
1941 strcpy (filename, ++temp);
1942 *temp = '\0';
1943 }
1944 #if defined (__MSDOS__)
1945 /* searches from current directory on the drive */
1946 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':')
1947 {
1948 strcpy (filename, dirname + 2);
1949 dirname[2] = '\0';
1950 }
1951 #endif
1952 else
1953 {
1954 dirname[0] = '.';
1955 dirname[1] = '\0';
1956 }
1957
1958 /* We aren't done yet. We also support the "~user" syntax. */
1959
1960 /* Save the version of the directory that the user typed. */
1961 users_dirname = savestring (dirname);
1962
1963 if (*dirname == '~')
1964 {
1965 temp = tilde_expand (dirname);
1966 free (dirname);
1967 dirname = temp;
1968 }
1969
1970 if (rl_directory_rewrite_hook)
1971 (*rl_directory_rewrite_hook) (&dirname);
1972
1973 /* The directory completion hook should perform any necessary
1974 dequoting. */
1975 if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname))
1976 {
1977 free (users_dirname);
1978 users_dirname = savestring (dirname);
1979 }
1980 else if (rl_completion_found_quote && rl_filename_dequoting_function)
1981 {
1982 /* delete single and double quotes */
1983 temp = (*rl_filename_dequoting_function) (users_dirname, rl_completion_quote_character);
1984 free (users_dirname);
1985 users_dirname = temp;
1986 }
1987 directory = opendir (dirname);
1988
1989 /* Now dequote a non-null filename. */
1990 if (filename && *filename && rl_completion_found_quote && rl_filename_dequoting_function)
1991 {
1992 /* delete single and double quotes */
1993 temp = (*rl_filename_dequoting_function) (filename, rl_completion_quote_character);
1994 free (filename);
1995 filename = temp;
1996 }
1997 filename_len = strlen (filename);
1998
1999 rl_filename_completion_desired = 1;
2000 }
2001
2002 /* At this point we should entertain the possibility of hacking wildcarded
2003 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
2004 contains globbing characters, then build an array of directories, and
2005 then map over that list while completing. */
2006 /* *** UNIMPLEMENTED *** */
2007
2008 /* Now that we have some state, we can read the directory. */
2009
2010 entry = (struct dirent *)NULL;
2011 while (directory && (entry = readdir (directory)))
2012 {
2013 /* Special case for no filename. If the user has disabled the
2014 `match-hidden-files' variable, skip filenames beginning with `.'.
2015 All other entries except "." and ".." match. */
2016 if (filename_len == 0)
2017 {
2018 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (entry->d_name))
2019 continue;
2020
2021 if (entry->d_name[0] != '.' ||
2022 (entry->d_name[1] &&
2023 (entry->d_name[1] != '.' || entry->d_name[2])))
2024 break;
2025 }
2026 else
2027 {
2028 /* Otherwise, if these match up to the length of filename, then
2029 it is a match. */
2030 if (_rl_completion_case_fold)
2031 {
2032 if ((_rl_to_lower (entry->d_name[0]) == _rl_to_lower (filename[0])) &&
2033 (((int)D_NAMLEN (entry)) >= filename_len) &&
2034 (_rl_strnicmp (filename, entry->d_name, filename_len) == 0))
2035 break;
2036 }
2037 else
2038 {
2039 if ((entry->d_name[0] == filename[0]) &&
2040 (((int)D_NAMLEN (entry)) >= filename_len) &&
2041 (strncmp (filename, entry->d_name, filename_len) == 0))
2042 break;
2043 }
2044 }
2045 }
2046
2047 if (entry == 0)
2048 {
2049 if (directory)
2050 {
2051 closedir (directory);
2052 directory = (DIR *)NULL;
2053 }
2054 if (dirname)
2055 {
2056 free (dirname);
2057 dirname = (char *)NULL;
2058 }
2059 if (filename)
2060 {
2061 free (filename);
2062 filename = (char *)NULL;
2063 }
2064 if (users_dirname)
2065 {
2066 free (users_dirname);
2067 users_dirname = (char *)NULL;
2068 }
2069
2070 return (char *)NULL;
2071 }
2072 else
2073 {
2074 /* dirname && (strcmp (dirname, ".") != 0) */
2075 if (dirname && (dirname[0] != '.' || dirname[1]))
2076 {
2077 if (rl_complete_with_tilde_expansion && *users_dirname == '~')
2078 {
2079 dirlen = strlen (dirname);
2080 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
2081 strcpy (temp, dirname);
2082 /* Canonicalization cuts off any final slash present. We
2083 may need to add it back. */
2084 if (dirname[dirlen - 1] != '/')
2085 {
2086 temp[dirlen++] = '/';
2087 temp[dirlen] = '\0';
2088 }
2089 }
2090 else
2091 {
2092 dirlen = strlen (users_dirname);
2093 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry));
2094 strcpy (temp, users_dirname);
2095 /* Make sure that temp has a trailing slash here. */
2096 if (users_dirname[dirlen - 1] != '/')
2097 temp[dirlen++] = '/';
2098 }
2099
2100 strcpy (temp + dirlen, entry->d_name);
2101 }
2102 else
2103 temp = savestring (entry->d_name);
2104
2105 return (temp);
2106 }
2107 }
2108
2109 /* An initial implementation of a menu completion function a la tcsh. The
2110 first time (if the last readline command was not rl_menu_complete), we
2111 generate the list of matches. This code is very similar to the code in
2112 rl_complete_internal -- there should be a way to combine the two. Then,
2113 for each item in the list of matches, we insert the match in an undoable
2114 fashion, with the appropriate character appended (this happens on the
2115 second and subsequent consecutive calls to rl_menu_complete). When we
2116 hit the end of the match list, we restore the original unmatched text,
2117 ring the bell, and reset the counter to zero. */
2118 int
rl_menu_complete(count,ignore)2119 rl_menu_complete (count, ignore)
2120 int count, ignore __attribute__((unused));
2121 {
2122 rl_compentry_func_t *our_func;
2123 int matching_filenames, found_quote;
2124
2125 static char *orig_text;
2126 static char **matches = (char **)0;
2127 static int match_list_index = 0;
2128 static int match_list_size = 0;
2129 static int orig_start, orig_end;
2130 static char quote_char;
2131 static int delimiter;
2132
2133 /* The first time through, we generate the list of matches and set things
2134 up to insert them. */
2135 if (rl_last_func != rl_menu_complete)
2136 {
2137 /* Clean up from previous call, if any. */
2138 FREE (orig_text);
2139 if (matches)
2140 _rl_free_match_list (matches);
2141
2142 match_list_index = match_list_size = 0;
2143 matches = (char **)NULL;
2144
2145 /* Only the completion entry function can change these. */
2146 set_completion_defaults ('%');
2147
2148 our_func = rl_completion_entry_function
2149 ? rl_completion_entry_function
2150 : rl_filename_completion_function;
2151
2152 /* We now look backwards for the start of a filename/variable word. */
2153 orig_end = rl_point;
2154 found_quote = delimiter = 0;
2155 quote_char = '\0';
2156
2157 if (rl_point)
2158 /* This (possibly) changes rl_point. If it returns a non-zero char,
2159 we know we have an open quote. */
2160 quote_char = _rl_find_completion_word (&found_quote, &delimiter);
2161
2162 orig_start = rl_point;
2163 rl_point = orig_end;
2164
2165 orig_text = rl_copy_text (orig_start, orig_end);
2166 matches = gen_completion_matches (orig_text, orig_start, orig_end,
2167 our_func, found_quote, quote_char);
2168
2169 /* If we are matching filenames, the attempted completion function will
2170 have set rl_filename_completion_desired to a non-zero value. The basic
2171 rl_filename_completion_function does this. */
2172 matching_filenames = rl_filename_completion_desired;
2173
2174 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0)
2175 {
2176 rl_ding ();
2177 FREE (matches);
2178 matches = (char **)0;
2179 FREE (orig_text);
2180 orig_text = (char *)0;
2181 completion_changed_buffer = 0;
2182 return (0);
2183 }
2184
2185 for (match_list_size = 0; matches[match_list_size]; match_list_size++)
2186 ;
2187 /* matches[0] is lcd if match_list_size > 1, but the circular buffer
2188 code below should take care of it. */
2189 }
2190
2191 /* Now we have the list of matches. Replace the text between
2192 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with
2193 matches[match_list_index], and add any necessary closing char. */
2194
2195 if (matches == 0 || match_list_size == 0)
2196 {
2197 rl_ding ();
2198 FREE (matches);
2199 matches = (char **)0;
2200 completion_changed_buffer = 0;
2201 return (0);
2202 }
2203
2204 match_list_index += count;
2205 if (match_list_index < 0)
2206 match_list_index += match_list_size;
2207 else
2208 match_list_index %= match_list_size;
2209
2210 if (match_list_index == 0 && match_list_size > 1)
2211 {
2212 rl_ding ();
2213 insert_match (orig_text, orig_start, MULT_MATCH, "e_char);
2214 }
2215 else
2216 {
2217 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
2218 append_to_match (matches[match_list_index], delimiter, quote_char,
2219 strcmp (orig_text, matches[match_list_index]));
2220 }
2221
2222 completion_changed_buffer = 1;
2223 return (0);
2224 }
2225