Deleted Added
full compact
1/* $FreeBSD: head/contrib/libreadline/complete.c 58314 2000-03-19 22:00:57Z 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
10 as published by the Free Software Foundation; either version 1, or
11 as published by the Free Software Foundation; either version 2, or
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. */
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>

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

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"
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
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
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 *));
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 *));
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 *));
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 *));
112
118extern char *xmalloc (), *xrealloc ();
119
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
135int _rl_completion_case_fold;
136#endif
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;

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

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

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

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 : "/");
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] = '/';

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

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 {
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;
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 }
661 }
662
663 if (fp)
664 *fp = found_quote;
665 if (dp)
666 *dp = delimiter;
667
668 return (quote_char);

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

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);
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 {

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

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);
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 {

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

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)
1423 return (char *)NULL;
1412#else /* !__GO32__ */
1424#else /* !__WIN32__ && !__OPENNT) */
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);

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

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 */
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)

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

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

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

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);
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 = '%';

--- 80 unchanged lines hidden ---