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 1, 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 675 Mass Ave, Cambridge, MA 02139, USA. */
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
25# include <config.h>
26#endif
27
28#include <sys/types.h>
29#include <fcntl.h>
--- 35 unchanged lines hidden (view full) ---
65#include "posixdir.h"
66#include "posixstat.h"
67
68/* System-specific feature definitions and include files. */
69#include "rldefs.h"
70
71/* Some standard library routines. */
72#include "readline.h"
73
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 ();
79
80extern Function *rl_last_func;
81extern int rl_editing_mode;
82extern int screenwidth;
83
84extern void _rl_move_vert ();
85extern int _rl_vis_botlin;
86extern int rl_display_fixed;
87
88/* If non-zero, then this is the address of a function to call when
89 completing a word would normally display the list of possible matches.
90 This function is called instead of actually doing the display.
91 It takes three arguments: (char **matches, int num_matches, int max_length)
92 where MATCHES is the array of strings that matched, NUM_MATCHES is the
93 number of strings in that array, and MAX_LENGTH is the length of the
94 longest string in that array. */
95VFunction *rl_completion_display_matches_hook = (VFunction *)NULL;
96
97/* Forward declarations for functions defined and used in this file. */
98char *filename_completion_function ();
99char **completion_matches ();
100
101#if defined (VISIBLE_STATS)
102# if !defined (X_OK)
103# define X_OK 1
104# endif
105static int stat_char ();
106#endif
107
108static char *rl_quote_filename ();
109static char *rl_strpbrk ();
110
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 ();
117
118extern char *xmalloc (), *xrealloc ();
119
120/* **************************************************************** */
121/* */
122/* Completion matching, from readline's point of view. */
123/* */
124/* **************************************************************** */
125
126/* Variables known only to the readline library. */
127
128/* If non-zero, non-unique completions always show the list of matches. */
129int _rl_complete_show_all = 0;
130
131/* If non-zero, completed directory names have a slash appended. */
132int _rl_complete_mark_directories = 1;
133
134/* If non-zero, completions are printed horizontally in alphabetical order,
135 like `ls -x'. */
136int _rl_print_completions_horizontally;
137
138/* Non-zero means that case is not significant in filename completion. */
139int _rl_completion_case_fold;
140
141/* Global variables available to applications using readline. */
142
143#if defined (VISIBLE_STATS)
144/* Non-zero means add an additional character to each filename displayed
145 during listing completion iff rl_filename_completion_desired which helps
146 to indicate the type of file being listed. */
147int rl_visible_stats = 0;
--- 264 unchanged lines hidden (view full) ---
412 final slash. Otherwise, we return what we were passed. */
413static char *
414printable_part (pathname)
415 char *pathname;
416{
417 char *temp;
418
419 temp = rl_filename_completion_desired ? strrchr (pathname, '/') : (char *)NULL;
420 return (temp ? ++temp : pathname);
421}
422
423/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
424 are using it, check for and output a single character for `special'
425 filenames. Return the number of characters we output. */
426
427#define PUTX(c) \
--- 44 unchanged lines hidden (view full) ---
472 path passed. In this case, we try to expand the directory
473 name before checking for the stat character. */
474 if (to_print != full_pathname)
475 {
476 /* Terminate the directory name. */
477 c = to_print[-1];
478 to_print[-1] = '\0';
479
480 s = tilde_expand (full_pathname);
481 if (rl_directory_completion_hook)
482 (*rl_directory_completion_hook) (&s);
483
484 slen = strlen (s);
485 tlen = strlen (to_print);
486 new_full_pathname = xmalloc (slen + tlen + 2);
487 strcpy (new_full_pathname, s);
488 new_full_pathname[slen] = '/';
--- 133 unchanged lines hidden (view full) ---
622 }
623
624 /* If we are at an unquoted word break, then advance past it. */
625 scan = rl_line_buffer[rl_point];
626
627 /* If there is an application-specific function to say whether or not
628 a character is quoted and we found a quote character, let that
629 function decide whether or not a character is a word break, even
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)
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;
644
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++;
649 }
650
651 if (fp)
652 *fp = found_quote;
653 if (dp)
654 *dp = delimiter;
655
656 return (quote_char);
--- 53 unchanged lines hidden (view full) ---
710
711 /* Sort the items. */
712 for (i = 0; matches[i]; i++)
713 ;
714
715 /* Sort the array without matches[0], since we need it to
716 stay in place no matter what. */
717 if (i)
718 qsort (matches+1, i-1, sizeof (char *), _rl_qsort_string_compare);
719
720 /* Remember the lowest common denominator for it may be unique. */
721 lowest_common = savestring (matches[0]);
722
723 for (i = newlen = 0; matches[i + 1]; i++)
724 {
725 if (strcmp (matches[i], matches[i + 1]) == 0)
726 {
--- 176 unchanged lines hidden (view full) ---
903 count = (len + (limit - 1)) / limit;
904
905 /* Watch out for special case. If LEN is less than LIMIT, then
906 just do the inner printing loop.
907 0 < len <= limit implies count = 1. */
908
909 /* Sort the items if they are not already sorted. */
910 if (rl_ignore_completion_duplicates == 0)
911 qsort (matches + 1, len, sizeof (char *), _rl_qsort_string_compare);
912
913 crlf ();
914
915 if (_rl_print_completions_horizontally == 0)
916 {
917 /* Print the sorted items, up-and-down alphabetically, like ls. */
918 for (i = 1; i <= count; i++)
919 {
--- 482 unchanged lines hidden (view full) ---
1402/* A completion function for usernames.
1403 TEXT contains a partial username preceded by a random
1404 character (usually `~'). */
1405char *
1406username_completion_function (text, state)
1407 char *text;
1408 int state;
1409{
1410#if defined (__GO32__) || defined (__WIN32__) || defined (__OPENNT)
1411 return (char *)NULL;
1412#else /* !__GO32__ */
1413 static char *username = (char *)NULL;
1414 static struct passwd *entry;
1415 static int namelen, first_char, first_char_loc;
1416 char *value;
1417
1418 if (state == 0)
1419 {
1420 FREE (username);
--- 26 unchanged lines hidden (view full) ---
1447
1448 strcpy (value + first_char_loc, entry->pw_name);
1449
1450 if (first_char == '~')
1451 rl_filename_completion_desired = 1;
1452
1453 return (value);
1454 }
1455#endif /* !__GO32__ */
1456}
1457
1458/* Okay, now we write the entry_function for filename completion. In the
1459 general case. Note that completion in the shell is a little different
1460 because of all the pathnames that must be followed when looking up the
1461 completion for a command. */
1462char *
1463filename_completion_function (text, state)
--- 25 unchanged lines hidden (view full) ---
1489
1490 filename = savestring (text);
1491 if (*text == 0)
1492 text = ".";
1493 dirname = savestring (text);
1494
1495 temp = strrchr (dirname, '/');
1496
1497 if (temp)
1498 {
1499 strcpy (filename, ++temp);
1500 *temp = '\0';
1501 }
1502 else
1503 {
1504 dirname[0] = '.';
1505 dirname[1] = '\0';
1506 }
1507
1508 /* We aren't done yet. We also support the "~user" syntax. */
1509
--- 145 unchanged lines hidden (view full) ---
1655
1656 /* The first time through, we generate the list of matches and set things
1657 up to insert them. */
1658 if (rl_last_func != rl_menu_complete)
1659 {
1660 /* Clean up from previous call, if any. */
1661 FREE (orig_text);
1662 if (matches)
1663 {
1664 for (match_list_index = 0; matches[match_list_index]; match_list_index++)
1665 free (matches[match_list_index]);
1666 free (matches);
1667 }
1668
1669 match_list_index = match_list_size = 0;
1670 matches = (char **)NULL;
1671
1672 /* Only the completion entry function can change these. */
1673 rl_filename_completion_desired = 0;
1674 rl_filename_quoting_desired = 1;
1675 rl_completion_type = '%';
--- 80 unchanged lines hidden ---
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 1, 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 675 Mass Ave, Cambridge, MA 02139, USA. */
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
25# include <config.h>
26#endif
27
28#include <sys/types.h>
29#include <fcntl.h>
--- 35 unchanged lines hidden (view full) ---
65#include "posixdir.h"
66#include "posixstat.h"
67
68/* System-specific feature definitions and include files. */
69#include "rldefs.h"
70
71/* Some standard library routines. */
72#include "readline.h"
73
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 ();
79
80extern Function *rl_last_func;
81extern int rl_editing_mode;
82extern int screenwidth;
83
84extern void _rl_move_vert ();
85extern int _rl_vis_botlin;
86extern int rl_display_fixed;
87
88/* If non-zero, then this is the address of a function to call when
89 completing a word would normally display the list of possible matches.
90 This function is called instead of actually doing the display.
91 It takes three arguments: (char **matches, int num_matches, int max_length)
92 where MATCHES is the array of strings that matched, NUM_MATCHES is the
93 number of strings in that array, and MAX_LENGTH is the length of the
94 longest string in that array. */
95VFunction *rl_completion_display_matches_hook = (VFunction *)NULL;
96
97/* Forward declarations for functions defined and used in this file. */
98char *filename_completion_function ();
99char **completion_matches ();
100
101#if defined (VISIBLE_STATS)
102# if !defined (X_OK)
103# define X_OK 1
104# endif
105static int stat_char ();
106#endif
107
108static char *rl_quote_filename ();
109static char *rl_strpbrk ();
110
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 ();
117
118extern char *xmalloc (), *xrealloc ();
119
120/* **************************************************************** */
121/* */
122/* Completion matching, from readline's point of view. */
123/* */
124/* **************************************************************** */
125
126/* Variables known only to the readline library. */
127
128/* If non-zero, non-unique completions always show the list of matches. */
129int _rl_complete_show_all = 0;
130
131/* If non-zero, completed directory names have a slash appended. */
132int _rl_complete_mark_directories = 1;
133
134/* If non-zero, completions are printed horizontally in alphabetical order,
135 like `ls -x'. */
136int _rl_print_completions_horizontally;
137
138/* Non-zero means that case is not significant in filename completion. */
139int _rl_completion_case_fold;
140
141/* Global variables available to applications using readline. */
142
143#if defined (VISIBLE_STATS)
144/* Non-zero means add an additional character to each filename displayed
145 during listing completion iff rl_filename_completion_desired which helps
146 to indicate the type of file being listed. */
147int rl_visible_stats = 0;
--- 264 unchanged lines hidden (view full) ---
412 final slash. Otherwise, we return what we were passed. */
413static char *
414printable_part (pathname)
415 char *pathname;
416{
417 char *temp;
418
419 temp = rl_filename_completion_desired ? strrchr (pathname, '/') : (char *)NULL;
420 return (temp ? ++temp : pathname);
421}
422
423/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
424 are using it, check for and output a single character for `special'
425 filenames. Return the number of characters we output. */
426
427#define PUTX(c) \
--- 44 unchanged lines hidden (view full) ---
472 path passed. In this case, we try to expand the directory
473 name before checking for the stat character. */
474 if (to_print != full_pathname)
475 {
476 /* Terminate the directory name. */
477 c = to_print[-1];
478 to_print[-1] = '\0';
479
480 s = tilde_expand (full_pathname);
481 if (rl_directory_completion_hook)
482 (*rl_directory_completion_hook) (&s);
483
484 slen = strlen (s);
485 tlen = strlen (to_print);
486 new_full_pathname = xmalloc (slen + tlen + 2);
487 strcpy (new_full_pathname, s);
488 new_full_pathname[slen] = '/';
--- 133 unchanged lines hidden (view full) ---
622 }
623
624 /* If we are at an unquoted word break, then advance past it. */
625 scan = rl_line_buffer[rl_point];
626
627 /* If there is an application-specific function to say whether or not
628 a character is quoted and we found a quote character, let that
629 function decide whether or not a character is a word break, even
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)
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;
644
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++;
649 }
650
651 if (fp)
652 *fp = found_quote;
653 if (dp)
654 *dp = delimiter;
655
656 return (quote_char);
--- 53 unchanged lines hidden (view full) ---
710
711 /* Sort the items. */
712 for (i = 0; matches[i]; i++)
713 ;
714
715 /* Sort the array without matches[0], since we need it to
716 stay in place no matter what. */
717 if (i)
718 qsort (matches+1, i-1, sizeof (char *), _rl_qsort_string_compare);
719
720 /* Remember the lowest common denominator for it may be unique. */
721 lowest_common = savestring (matches[0]);
722
723 for (i = newlen = 0; matches[i + 1]; i++)
724 {
725 if (strcmp (matches[i], matches[i + 1]) == 0)
726 {
--- 176 unchanged lines hidden (view full) ---
903 count = (len + (limit - 1)) / limit;
904
905 /* Watch out for special case. If LEN is less than LIMIT, then
906 just do the inner printing loop.
907 0 < len <= limit implies count = 1. */
908
909 /* Sort the items if they are not already sorted. */
910 if (rl_ignore_completion_duplicates == 0)
911 qsort (matches + 1, len, sizeof (char *), _rl_qsort_string_compare);
912
913 crlf ();
914
915 if (_rl_print_completions_horizontally == 0)
916 {
917 /* Print the sorted items, up-and-down alphabetically, like ls. */
918 for (i = 1; i <= count; i++)
919 {
--- 482 unchanged lines hidden (view full) ---
1402/* A completion function for usernames.
1403 TEXT contains a partial username preceded by a random
1404 character (usually `~'). */
1405char *
1406username_completion_function (text, state)
1407 char *text;
1408 int state;
1409{
1410#if defined (__GO32__) || defined (__WIN32__) || defined (__OPENNT)
1411 return (char *)NULL;
1412#else /* !__GO32__ */
1413 static char *username = (char *)NULL;
1414 static struct passwd *entry;
1415 static int namelen, first_char, first_char_loc;
1416 char *value;
1417
1418 if (state == 0)
1419 {
1420 FREE (username);
--- 26 unchanged lines hidden (view full) ---
1447
1448 strcpy (value + first_char_loc, entry->pw_name);
1449
1450 if (first_char == '~')
1451 rl_filename_completion_desired = 1;
1452
1453 return (value);
1454 }
1455#endif /* !__GO32__ */
1456}
1457
1458/* Okay, now we write the entry_function for filename completion. In the
1459 general case. Note that completion in the shell is a little different
1460 because of all the pathnames that must be followed when looking up the
1461 completion for a command. */
1462char *
1463filename_completion_function (text, state)
--- 25 unchanged lines hidden (view full) ---
1489
1490 filename = savestring (text);
1491 if (*text == 0)
1492 text = ".";
1493 dirname = savestring (text);
1494
1495 temp = strrchr (dirname, '/');
1496
1497 if (temp)
1498 {
1499 strcpy (filename, ++temp);
1500 *temp = '\0';
1501 }
1502 else
1503 {
1504 dirname[0] = '.';
1505 dirname[1] = '\0';
1506 }
1507
1508 /* We aren't done yet. We also support the "~user" syntax. */
1509
--- 145 unchanged lines hidden (view full) ---
1655
1656 /* The first time through, we generate the list of matches and set things
1657 up to insert them. */
1658 if (rl_last_func != rl_menu_complete)
1659 {
1660 /* Clean up from previous call, if any. */
1661 FREE (orig_text);
1662 if (matches)
1663 {
1664 for (match_list_index = 0; matches[match_list_index]; match_list_index++)
1665 free (matches[match_list_index]);
1666 free (matches);
1667 }
1668
1669 match_list_index = match_list_size = 0;
1670 matches = (char **)NULL;
1671
1672 /* Only the completion entry function can change these. */
1673 rl_filename_completion_desired = 0;
1674 rl_filename_quoting_desired = 1;
1675 rl_completion_type = '%';
--- 80 unchanged lines hidden ---