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