Bug Summary

File:src/gnu/lib/libreadline/complete.c
Warning:line 840, column 11
Null pointer passed as 1st argument to string comparison function

Annotated Source Code

Press '?' to see keyboard shortcuts

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