Deleted Added
sdiff udiff text old ( 119614 ) new ( 136758 )
full compact
1/* $FreeBSD: head/contrib/libreadline/complete.c 119614 2003-08-31 18:29:38Z ache $ */
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
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.

--- 11 unchanged lines hidden (view full) ---

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
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>

--- 54 unchanged lines hidden (view full) ---

95
96#if defined (VISIBLE_STATS)
97# if !defined (X_OK)
98# define X_OK 1
99# endif
100static int stat_char PARAMS((char *));
101#endif
102
103static char *rl_quote_filename PARAMS((char *, int, char *));
104
105static void set_completion_defaults PARAMS((int));
106static int get_y_or_n PARAMS((int));
107static int _rl_internal_pager PARAMS((int));
108static char *printable_part PARAMS((char *));
109static int print_filename PARAMS((char *, char *));
110
111static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int));
112
113static char **remove_duplicate_matches PARAMS((char **));
114static void insert_match PARAMS((char *, int, int, char *));
115static int append_to_match PARAMS((char *, int, int, int));
116static void insert_all_matches PARAMS((char **, int, char *));

--- 9 unchanged lines hidden (view full) ---

126/* */
127/* **************************************************************** */
128
129/* Variables known only to the readline library. */
130
131/* If non-zero, non-unique completions always show the list of matches. */
132int _rl_complete_show_all = 0;
133
134/* If non-zero, completed directory names have a slash appended. */
135int _rl_complete_mark_directories = 1;
136
137/* If non-zero, the symlinked directory completion behavior introduced in
138 readline-4.2a is disabled, and symlinks that point to directories have
139 a slash appended (subject to the value of _rl_complete_mark_directories).
140 This is user-settable via the mark-symlinked-directories variable. */
141int _rl_complete_mark_symlink_dirs = 0;

--- 68 unchanged lines hidden (view full) ---

210const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
211
212/* List of basic quoting characters. */
213const char *rl_basic_quote_characters = "\"'";
214
215/* The list of characters that signal a break between words for
216 rl_complete_internal. The default list is the contents of
217 rl_basic_word_break_characters. */
218const char *rl_completer_word_break_characters = (const char *)NULL;
219
220/* List of characters which can be used to quote a substring of the line.
221 Completion occurs on the entire substring, and within the substring
222 rl_completer_word_break_characters are treated as any other character,
223 unless they also appear within this list. */
224const char *rl_completer_quote_characters = (const char *)NULL;
225
226/* List of characters that should be quoted in filenames by the completer. */
227const char *rl_filename_quote_characters = (const char *)NULL;

--- 50 unchanged lines hidden (view full) ---

278 possible closing quote. This is set to 0 by rl_complete_internal and
279 may be changed by an application-specific completion function. */
280int rl_completion_suppress_append = 0;
281
282/* Character appended to completed words when at the end of the line. The
283 default is a space. */
284int rl_completion_append_character = ' ';
285
286/* If non-zero, a slash will be appended to completed filenames that are
287 symbolic links to directory names, subject to the value of the
288 mark-directories variable (which is user-settable). This exists so
289 that application completion functions can override the user's preference
290 (set via the mark-symlinked-directories variable) if appropriate.
291 It's set to the value of _rl_complete_mark_symlink_dirs in
292 rl_complete_internal before any application-specific completion
293 function is called, so without that function doing anything, the user's

--- 22 unchanged lines hidden (view full) ---

316 int ignore, invoking_key;
317{
318 if (rl_inhibit_completion)
319 return (_rl_insert_char (ignore, invoking_key));
320 else if (rl_last_func == rl_complete && !completion_changed_buffer)
321 return (rl_complete_internal ('?'));
322 else if (_rl_complete_show_all)
323 return (rl_complete_internal ('!'));
324 else
325 return (rl_complete_internal (TAB));
326}
327
328/* List the possible completions. See description of rl_complete (). */
329int
330rl_possible_completions (ignore, invoking_key)
331 int ignore, invoking_key;

--- 16 unchanged lines hidden (view full) ---

348int
349rl_completion_mode (cfunc)
350 rl_command_func_t *cfunc;
351{
352 if (rl_last_func == cfunc && !completion_changed_buffer)
353 return '?';
354 else if (_rl_complete_show_all)
355 return '!';
356 else
357 return TAB;
358}
359
360/************************************/
361/* */
362/* Completion utility functions */
363/* */

--- 4 unchanged lines hidden (view full) ---

368static void
369set_completion_defaults (what_to_do)
370 int what_to_do;
371{
372 /* Only the completion entry function can change these. */
373 rl_filename_completion_desired = 0;
374 rl_filename_quoting_desired = 1;
375 rl_completion_type = what_to_do;
376 rl_completion_suppress_append = 0;
377
378 /* The completion entry function may optionally change this. */
379 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
380}
381
382/* The user must press "y" or "n". Non-zero return means "y" pressed. */
383static int
384get_y_or_n (for_pager)

--- 34 unchanged lines hidden (view full) ---

419 if (i == 0)
420 return -1;
421 else if (i == 2)
422 return (lines - 1);
423 else
424 return 0;
425}
426
427#if defined (VISIBLE_STATS)
428/* Return the character which best describes FILENAME.
429 `@' for symbolic links
430 `/' for directories
431 `*' for executables
432 `=' for sockets
433 `|' for FIFOs
434 `%' for character special devices

--- 81 unchanged lines hidden (view full) ---

516 if (*x == '/')
517 break;
518 return ((*x == '/') ? x + 1 : pathname);
519 }
520 else
521 return ++temp;
522}
523
524/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
525 are using it, check for and output a single character for `special'
526 filenames. Return the number of characters we output. */
527
528#define PUTX(c) \
529 do { \
530 if (CTRL_CHAR (c)) \
531 { \
532 putc ('^', rl_outstream); \
533 putc (UNCTRL (c), rl_outstream); \
534 printed_len += 2; \
535 } \
536 else if (c == RUBOUT) \
537 { \
538 putc ('^', rl_outstream); \
539 putc ('?', rl_outstream); \
540 printed_len += 2; \
541 } \
542 else \
543 { \
544 putc (c, rl_outstream); \
545 printed_len++; \
546 } \
547 } while (0)
548
549static int
550print_filename (to_print, full_pathname)
551 char *to_print, *full_pathname;
552{
553 int printed_len = 0;
554#if !defined (VISIBLE_STATS)
555 char *s;
556
557 for (s = to_print; *s; s++)
558 {
559 PUTX (*s);
560 }
561#else
562 char *s, c, *new_full_pathname;
563 int extension_char, slen, tlen;
564
565 for (s = to_print; *s; s++)
566 {
567 PUTX (*s);
568 }
569
570 if (rl_filename_completion_desired && rl_visible_stats)
571 {
572 /* If to_print != full_pathname, to_print is the basename of the
573 path passed. In this case, we try to expand the directory
574 name before checking for the stat character. */
575 if (to_print != full_pathname)
576 {
577 /* Terminate the directory name. */
578 c = to_print[-1];

--- 10 unchanged lines hidden (view full) ---

589
590 slen = strlen (s);
591 tlen = strlen (to_print);
592 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
593 strcpy (new_full_pathname, s);
594 new_full_pathname[slen] = '/';
595 strcpy (new_full_pathname + slen + 1, to_print);
596
597 extension_char = stat_char (new_full_pathname);
598
599 free (new_full_pathname);
600 to_print[-1] = c;
601 }
602 else
603 {
604 s = tilde_expand (full_pathname);
605 extension_char = stat_char (s);
606 }
607
608 free (s);
609 if (extension_char)
610 {
611 putc (extension_char, rl_outstream);
612 printed_len++;
613 }
614 }
615#endif /* VISIBLE_STATS */
616 return printed_len;
617}
618
619static char *
620rl_quote_filename (s, rtype, qcp)
621 char *s;
622 int rtype;
623 char *qcp;

--- 23 unchanged lines hidden (view full) ---

647 quote, or backslash) anywhere in the string. DP, if non-null, is set to
648 the value of the delimiter character that caused a word break. */
649
650char
651_rl_find_completion_word (fp, dp)
652 int *fp, *dp;
653{
654 int scan, end, found_quote, delimiter, pass_next, isbrk;
655 char quote_char;
656
657 end = rl_point;
658 found_quote = delimiter = 0;
659 quote_char = '\0';
660
661 if (rl_completer_quote_characters)
662 {
663 /* We have a list of characters which can be used in pairs to
664 quote substrings for the completer. Try to find the start
665 of an unclosed quoted substring. */
666 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
667 for (scan = pass_next = 0; scan < end; scan++)
668 {
669 if (pass_next)
670 {
671 pass_next = 0;
672 continue;
673 }
674
675 /* Shell-like semantics for single quotes -- don't allow backslash

--- 41 unchanged lines hidden (view full) ---

717#if defined (HANDLE_MULTIBYTE)
718 while (rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_ANY))
719#else
720 while (--rl_point)
721#endif
722 {
723 scan = rl_line_buffer[rl_point];
724
725 if (strchr (rl_completer_word_break_characters, scan) == 0)
726 continue;
727
728 /* Call the application-specific function to tell us whether
729 this word break character is quoted and should be skipped. */
730 if (rl_char_is_quoted_p && found_quote &&
731 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point))
732 continue;
733

--- 11 unchanged lines hidden (view full) ---

745 function decide whether or not a character is a word break, even
746 if it is found in rl_completer_word_break_characters. Don't bother
747 if we're at the end of the line, though. */
748 if (scan)
749 {
750 if (rl_char_is_quoted_p)
751 isbrk = (found_quote == 0 ||
752 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
753 strchr (rl_completer_word_break_characters, scan) != 0;
754 else
755 isbrk = strchr (rl_completer_word_break_characters, scan) != 0;
756
757 if (isbrk)
758 {
759 /* If the character that caused the word break was a quoting
760 character, then remember it as the delimiter. */
761 if (rl_basic_quote_characters &&
762 strchr (rl_basic_quote_characters, scan) &&
763 (end - rl_point) > 1)

--- 18 unchanged lines hidden (view full) ---

782gen_completion_matches (text, start, end, our_func, found_quote, quote_char)
783 char *text;
784 int start, end;
785 rl_compentry_func_t *our_func;
786 int found_quote, quote_char;
787{
788 char **matches, *temp;
789
790 /* If the user wants to TRY to complete, but then wants to give
791 up and use the default completion function, they set the
792 variable rl_attempted_completion_function. */
793 if (rl_attempted_completion_function)
794 {
795 matches = (*rl_attempted_completion_function) (text, start, end);
796
797 if (matches || rl_attempted_completion_over)

--- 87 unchanged lines hidden (view full) ---

885static int
886compute_lcd_of_matches (match_list, matches, text)
887 char **match_list;
888 int matches;
889 const char *text;
890{
891 register int i, c1, c2, si;
892 int low; /* Count of max-matched characters. */
893#if defined (HANDLE_MULTIBYTE)
894 int v;
895 mbstate_t ps1, ps2;
896 wchar_t wc1, wc2;
897#endif
898
899 /* If only one match, just use that. Otherwise, compare each
900 member of the list with the next, finding out where they

--- 75 unchanged lines hidden (view full) ---

976 match_list[0] = (char *)xmalloc (low + 1);
977
978 /* XXX - this might need changes in the presence of multibyte chars */
979
980 /* If we are ignoring case, try to preserve the case of the string
981 the user typed in the face of multiple matches differing in case. */
982 if (_rl_completion_case_fold)
983 {
984 /* sort the list to get consistent answers. */
985 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
986
987 si = strlen (text);
988 if (si <= low)
989 {
990 for (i = 1; i <= matches; i++)
991 if (strncmp (match_list[i], text, si) == 0)
992 {
993 strncpy (match_list[0], match_list[i], low);
994 break;
995 }
996 /* no casematch, use first entry */
997 if (i > matches)
998 strncpy (match_list[0], match_list[1], low);
999 }
1000 else
1001 /* otherwise, just use the text the user typed. */
1002 strncpy (match_list[0], text, low);
1003 }
1004 else
1005 strncpy (match_list[0], match_list[1], low);
1006
1007 match_list[0][low] = '\0';
1008 }
1009
1010 return matches;

--- 188 unchanged lines hidden (view full) ---

1199 return;
1200 }
1201
1202 /* There is more than one answer. Find out how many there are,
1203 and find the maximum printed length of a single entry. */
1204 for (max = 0, i = 1; matches[i]; i++)
1205 {
1206 temp = printable_part (matches[i]);
1207 len = strlen (temp);
1208
1209 if (len > max)
1210 max = len;
1211 }
1212
1213 len = i - 1;
1214
1215 /* If the caller has defined a display hook, then call that now. */

--- 118 unchanged lines hidden (view full) ---

1334 char *text;
1335 int delimiter, quote_char, nontrivial_match;
1336{
1337 char temp_string[4], *filename;
1338 int temp_string_index, s;
1339 struct stat finfo;
1340
1341 temp_string_index = 0;
1342 if (quote_char && rl_point && rl_line_buffer[rl_point - 1] != quote_char)
1343 temp_string[temp_string_index++] = quote_char;
1344
1345 if (delimiter)
1346 temp_string[temp_string_index++] = delimiter;
1347 else if (rl_completion_suppress_append == 0 && rl_completion_append_character)
1348 temp_string[temp_string_index++] = rl_completion_append_character;
1349
1350 temp_string[temp_string_index++] = '\0';

--- 94 unchanged lines hidden (view full) ---

1445}
1446
1447/* Complete the word at or before point.
1448 WHAT_TO_DO says what to do with the completion.
1449 `?' means list the possible completions.
1450 TAB means do standard completion.
1451 `*' means insert all of the possible completions.
1452 `!' means to do standard completion, and list all possible completions if
1453 there is more than one. */
1454int
1455rl_complete_internal (what_to_do)
1456 int what_to_do;
1457{
1458 char **matches;
1459 rl_compentry_func_t *our_func;
1460 int start, end, delimiter, found_quote, i, nontrivial_lcd;
1461 char *text, *saved_line_buffer;
1462 char quote_char;
1463
1464 RL_SETSTATE(RL_STATE_COMPLETING);
1465
1466 set_completion_defaults (what_to_do);
1467
1468 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL;
1469 our_func = rl_completion_entry_function
1470 ? rl_completion_entry_function
1471 : rl_filename_completion_function;
1472
1473 /* We now look backwards for the start of a filename/variable word. */
1474 end = rl_point;
1475 found_quote = delimiter = 0;
1476 quote_char = '\0';
1477
1478 if (rl_point)
1479 /* This (possibly) changes rl_point. If it returns a non-zero char,
1480 we know we have an open quote. */

--- 31 unchanged lines hidden (view full) ---

1512 RL_UNSETSTATE(RL_STATE_COMPLETING);
1513 return (0);
1514 }
1515
1516 switch (what_to_do)
1517 {
1518 case TAB:
1519 case '!':
1520 /* Insert the first match with proper quoting. */
1521 if (*matches[0])
1522 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, &quote_char);
1523
1524 /* If there are more matches, ring the bell to indicate.
1525 If we are in vi mode, Posix.2 says to not ring the bell.
1526 If the `show-all-if-ambiguous' variable is set, display
1527 all the matches immediately. Otherwise, if this was the
1528 only match, and we are hacking files, check the file to
1529 see if it was a directory. If so, and the `mark-directories'
1530 variable is set, add a '/' to the name. If not, and we
1531 are at the end of the line, then add a space. */
1532 if (matches[1])
1533 {
1534 if (what_to_do == '!')
1535 {
1536 display_matches (matches);
1537 break;
1538 }
1539 else if (rl_editing_mode != vi_mode)
1540 rl_ding (); /* There are other matches remaining. */
1541 }
1542 else
1543 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd);
1544
1545 break;
1546

--- 459 unchanged lines hidden ---