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