1/* $FreeBSD: head/contrib/libreadline/complete.c 136758 2004-10-21 20:02:02Z peter $ */
2
3/* complete.c -- filename completion for readline. */
4
5/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
6
7 This file is part of the GNU Readline Library, a library for
8 reading lines of text with interactive input and history editing.
9
10 The GNU Readline Library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2, or
13 (at your option) any later version.
--- 31 unchanged lines hidden (view full) ---
45
46#include <stdio.h>
47
48#include <errno.h>
49#if !defined (errno)
50extern int errno;
51#endif /* !errno */
52
53#include <pwd.h>
54
55#include "posixdir.h"
56#include "posixstat.h"
57
58/* System-specific feature definitions and include files. */
59#include "rldefs.h"
60#include "rlmbutil.h"
61
--- 14 unchanged lines hidden (view full) ---
76# define LSTAT stat
77#endif
78
79/* Unix version of a hidden file. Could be different on other systems. */
80#define HIDDEN_FILE(fname) ((fname)[0] == '.')
81
82/* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
83 defined. */
84#if !defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE)
85extern struct passwd *getpwent PARAMS((void));
86#endif /* !HAVE_GETPW_DECLS || _POSIX_SOURCE */
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. */
--- 108 unchanged lines hidden (view full) ---
203
204/* Set to a character indicating the type of completion being performed
205 by rl_complete_internal, available for use by application completion
206 functions. */
207int rl_completion_type = 0;
208
209/* Up to this many items will be displayed in response to a
210 possible-completions call. After that, we ask the user if
211 she is sure she wants to see them all. */
212int rl_completion_query_items = 100;
213
214int _rl_page_completions = 1;
215
216/* The basic list of characters that signal a break between words for the
217 completer routine. The contents of this variable is what breaks words
218 in the shell, i.e. " \t\n\"\\'`@$><=" */
219const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
--- 398 unchanged lines hidden (view full) ---
618 const char *to_print;
619{
620 int printed_len;
621 const char *s;
622#if defined (HANDLE_MULTIBYTE)
623 mbstate_t ps;
624 const char *end;
625 size_t tlen;
626
627 end = to_print + strlen (to_print) + 1;
628 memset (&ps, 0, sizeof (mbstate_t));
629#endif
630
631 printed_len = 0;
632 s = to_print;
633 while (*s)
--- 16 unchanged lines hidden (view full) ---
650 s++;
651#if defined (HANDLE_MULTIBYTE)
652 memset (&ps, 0, sizeof (mbstate_t));
653#endif
654 }
655 else
656 {
657#if defined (HANDLE_MULTIBYTE)
658 tlen = mbrlen (s, end - s, &ps);
659 if (MB_INVALIDCH (tlen))
660 {
661 tlen = 1;
662 memset (&ps, 0, sizeof (mbstate_t));
663 }
664 else if (MB_NULLWCH (tlen))
665 break;
666 fwrite (s, 1, tlen, rl_outstream);
667 s += tlen;
668#else
669 putc (*s, rl_outstream);
670 s++;
671#endif
672 printed_len++;
673 }
674 }
675
676 return printed_len;
677}
678
679/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
680 are using it, check for and output a single character for `special'
681 filenames. Return the number of characters we output. */
682
683static int
684print_filename (to_print, full_pathname)
685 char *to_print, *full_pathname;
686{
687 int printed_len, extension_char, slen, tlen;
688 char *s, c, *new_full_pathname;
689
690 extension_char = 0;
691 printed_len = fnprint (to_print);
692
693#if defined (VISIBLE_STATS)
694 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
695#else
696 if (rl_filename_completion_desired && _rl_complete_mark_directories)
--- 8 unchanged lines hidden (view full) ---
705 c = to_print[-1];
706 to_print[-1] = '\0';
707
708 /* If setting the last slash in full_pathname to a NUL results in
709 full_pathname being the empty string, we are trying to complete
710 files in the root directory. If we pass a null string to the
711 bash directory completion hook, for example, it will expand it
712 to the current directory. We just want the `/'. */
713 s = tilde_expand (full_pathname && *full_pathname ? full_pathname : "/");
714 if (rl_directory_completion_hook)
715 (*rl_directory_completion_hook) (&s);
716
717 slen = strlen (s);
718 tlen = strlen (to_print);
719 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
720 strcpy (new_full_pathname, s);
721 new_full_pathname[slen] = '/';
722 strcpy (new_full_pathname + slen + 1, to_print);
723
724#if defined (VISIBLE_STATS)
725 if (rl_visible_stats)
726 extension_char = stat_char (new_full_pathname);
727 else
728#endif
--- 75 unchanged lines hidden (view full) ---
804 brkchars = rl_completer_word_break_characters;
805
806 if (rl_completer_quote_characters)
807 {
808 /* We have a list of characters which can be used in pairs to
809 quote substrings for the completer. Try to find the start
810 of an unclosed quoted substring. */
811 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
812#if defined (HANDLE_MULTIBYTE)
813 for (scan = pass_next = 0; scan < end;
814 scan = ((MB_CUR_MAX == 1 || rl_byte_oriented)
815 ? (scan + 1)
816 : _rl_find_next_mbchar (rl_line_buffer, scan, 1, MB_FIND_ANY)))
817#else
818 for (scan = pass_next = 0; scan < end; scan++)
819#endif
820 {
821 if (pass_next)
822 {
823 pass_next = 0;
824 continue;
825 }
826
827 /* Shell-like semantics for single quotes -- don't allow backslash
--- 33 unchanged lines hidden (view full) ---
861 }
862 }
863
864 if (rl_point == end && quote_char == '\0')
865 {
866 /* We didn't find an unclosed quoted substring upon which to do
867 completion, so use the word break characters to find the
868 substring on which to complete. */
869#if defined (HANDLE_MULTIBYTE)
870 while (rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_ANY))
871#else
872 while (--rl_point)
873#endif
874 {
875 scan = rl_line_buffer[rl_point];
876
877 if (strchr (brkchars, scan) == 0)
878 continue;
879
880 /* Call the application-specific function to tell us whether
881 this word break character is quoted and should be skipped. */
--- 266 unchanged lines hidden (view full) ---
1148 check against the list of matches
1149 FI */
1150 dtext = (char *)NULL;
1151 if (rl_filename_completion_desired &&
1152 rl_filename_dequoting_function &&
1153 rl_completion_found_quote &&
1154 rl_filename_quoting_desired)
1155 {
1156 dtext = (*rl_filename_dequoting_function) (text, rl_completion_quote_character);
1157 text = dtext;
1158 }
1159
1160 /* sort the list to get consistent answers. */
1161 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1162
1163 si = strlen (text);
1164 if (si <= low)
--- 229 unchanged lines hidden (view full) ---
1394 if (rl_completion_display_matches_hook)
1395 {
1396 (*rl_completion_display_matches_hook) (matches, len, max);
1397 return;
1398 }
1399
1400 /* If there are many items, then ask the user if she really wants to
1401 see them all. */
1402 if (len >= rl_completion_query_items)
1403 {
1404 rl_crlf ();
1405 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1406 fflush (rl_outstream);
1407 if (get_y_or_n (0) == 0)
1408 {
1409 rl_crlf ();
1410
--- 120 unchanged lines hidden (view full) ---
1531 if (rl_filename_completion_desired)
1532 {
1533 filename = tilde_expand (text);
1534 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1535 ? LSTAT (filename, &finfo)
1536 : stat (filename, &finfo);
1537 if (s == 0 && S_ISDIR (finfo.st_mode))
1538 {
1539 if (_rl_complete_mark_directories)
1540 {
1541 /* This is clumsy. Avoid putting in a double slash if point
1542 is at the end of the line and the previous character is a
1543 slash. */
1544 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1545 ;
1546 else if (rl_line_buffer[rl_point] != '/')
1547 rl_insert_text ("/");
--- 297 unchanged lines hidden (view full) ---
1845 first_char = *text;
1846 first_char_loc = first_char == '~';
1847
1848 username = savestring (&text[first_char_loc]);
1849 namelen = strlen (username);
1850 setpwent ();
1851 }
1852
1853 while (entry = getpwent ())
1854 {
1855 /* Null usernames should result in all users as possible completions. */
1856 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
1857 break;
1858 }
1859
1860 if (entry == 0)
1861 {
1862 endpwent ();
1863 return ((char *)NULL);
1864 }
1865 else
1866 {
1867 value = (char *)xmalloc (2 + strlen (entry->pw_name));
1868
1869 *value = *text;
1870
--- 295 unchanged lines hidden (view full) ---
2166 {
2167 rl_ding ();
2168 FREE (matches);
2169 matches = (char **)0;
2170 completion_changed_buffer = 0;
2171 return (0);
2172 }
2173
2174 match_list_index = (match_list_index + count) % match_list_size;
2175 if (match_list_index < 0)
2176 match_list_index += match_list_size;
2177
2178 if (match_list_index == 0 && match_list_size > 1)
2179 {
2180 rl_ding ();
2181 insert_match (orig_text, orig_start, MULT_MATCH, "e_char);
2182 }
2183 else
2184 {
2185 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
2186 append_to_match (matches[match_list_index], delimiter, quote_char,
2187 strcmp (orig_text, matches[match_list_index]));
2188 }
2189
2190 completion_changed_buffer = 1;
2191 return (0);
2192}
2
3/* complete.c -- filename completion for readline. */
4
5/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
6
7 This file is part of the GNU Readline Library, a library for
8 reading lines of text with interactive input and history editing.
9
10 The GNU Readline Library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2, or
13 (at your option) any later version.
--- 31 unchanged lines hidden (view full) ---
45
46#include <stdio.h>
47
48#include <errno.h>
49#if !defined (errno)
50extern int errno;
51#endif /* !errno */
52
53#include <pwd.h>
54
55#include "posixdir.h"
56#include "posixstat.h"
57
58/* System-specific feature definitions and include files. */
59#include "rldefs.h"
60#include "rlmbutil.h"
61
--- 14 unchanged lines hidden (view full) ---
76# define LSTAT stat
77#endif
78
79/* Unix version of a hidden file. Could be different on other systems. */
80#define HIDDEN_FILE(fname) ((fname)[0] == '.')
81
82/* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is
83 defined. */
84#if !defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE)
85extern struct passwd *getpwent PARAMS((void));
86#endif /* !HAVE_GETPW_DECLS || _POSIX_SOURCE */
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. */
--- 108 unchanged lines hidden (view full) ---
203
204/* Set to a character indicating the type of completion being performed
205 by rl_complete_internal, available for use by application completion
206 functions. */
207int rl_completion_type = 0;
208
209/* Up to this many items will be displayed in response to a
210 possible-completions call. After that, we ask the user if
211 she is sure she wants to see them all. */
212int rl_completion_query_items = 100;
213
214int _rl_page_completions = 1;
215
216/* The basic list of characters that signal a break between words for the
217 completer routine. The contents of this variable is what breaks words
218 in the shell, i.e. " \t\n\"\\'`@$><=" */
219const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */
--- 398 unchanged lines hidden (view full) ---
618 const char *to_print;
619{
620 int printed_len;
621 const char *s;
622#if defined (HANDLE_MULTIBYTE)
623 mbstate_t ps;
624 const char *end;
625 size_t tlen;
626
627 end = to_print + strlen (to_print) + 1;
628 memset (&ps, 0, sizeof (mbstate_t));
629#endif
630
631 printed_len = 0;
632 s = to_print;
633 while (*s)
--- 16 unchanged lines hidden (view full) ---
650 s++;
651#if defined (HANDLE_MULTIBYTE)
652 memset (&ps, 0, sizeof (mbstate_t));
653#endif
654 }
655 else
656 {
657#if defined (HANDLE_MULTIBYTE)
658 tlen = mbrlen (s, end - s, &ps);
659 if (MB_INVALIDCH (tlen))
660 {
661 tlen = 1;
662 memset (&ps, 0, sizeof (mbstate_t));
663 }
664 else if (MB_NULLWCH (tlen))
665 break;
666 fwrite (s, 1, tlen, rl_outstream);
667 s += tlen;
668#else
669 putc (*s, rl_outstream);
670 s++;
671#endif
672 printed_len++;
673 }
674 }
675
676 return printed_len;
677}
678
679/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
680 are using it, check for and output a single character for `special'
681 filenames. Return the number of characters we output. */
682
683static int
684print_filename (to_print, full_pathname)
685 char *to_print, *full_pathname;
686{
687 int printed_len, extension_char, slen, tlen;
688 char *s, c, *new_full_pathname;
689
690 extension_char = 0;
691 printed_len = fnprint (to_print);
692
693#if defined (VISIBLE_STATS)
694 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
695#else
696 if (rl_filename_completion_desired && _rl_complete_mark_directories)
--- 8 unchanged lines hidden (view full) ---
705 c = to_print[-1];
706 to_print[-1] = '\0';
707
708 /* If setting the last slash in full_pathname to a NUL results in
709 full_pathname being the empty string, we are trying to complete
710 files in the root directory. If we pass a null string to the
711 bash directory completion hook, for example, it will expand it
712 to the current directory. We just want the `/'. */
713 s = tilde_expand (full_pathname && *full_pathname ? full_pathname : "/");
714 if (rl_directory_completion_hook)
715 (*rl_directory_completion_hook) (&s);
716
717 slen = strlen (s);
718 tlen = strlen (to_print);
719 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
720 strcpy (new_full_pathname, s);
721 new_full_pathname[slen] = '/';
722 strcpy (new_full_pathname + slen + 1, to_print);
723
724#if defined (VISIBLE_STATS)
725 if (rl_visible_stats)
726 extension_char = stat_char (new_full_pathname);
727 else
728#endif
--- 75 unchanged lines hidden (view full) ---
804 brkchars = rl_completer_word_break_characters;
805
806 if (rl_completer_quote_characters)
807 {
808 /* We have a list of characters which can be used in pairs to
809 quote substrings for the completer. Try to find the start
810 of an unclosed quoted substring. */
811 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
812#if defined (HANDLE_MULTIBYTE)
813 for (scan = pass_next = 0; scan < end;
814 scan = ((MB_CUR_MAX == 1 || rl_byte_oriented)
815 ? (scan + 1)
816 : _rl_find_next_mbchar (rl_line_buffer, scan, 1, MB_FIND_ANY)))
817#else
818 for (scan = pass_next = 0; scan < end; scan++)
819#endif
820 {
821 if (pass_next)
822 {
823 pass_next = 0;
824 continue;
825 }
826
827 /* Shell-like semantics for single quotes -- don't allow backslash
--- 33 unchanged lines hidden (view full) ---
861 }
862 }
863
864 if (rl_point == end && quote_char == '\0')
865 {
866 /* We didn't find an unclosed quoted substring upon which to do
867 completion, so use the word break characters to find the
868 substring on which to complete. */
869#if defined (HANDLE_MULTIBYTE)
870 while (rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_ANY))
871#else
872 while (--rl_point)
873#endif
874 {
875 scan = rl_line_buffer[rl_point];
876
877 if (strchr (brkchars, scan) == 0)
878 continue;
879
880 /* Call the application-specific function to tell us whether
881 this word break character is quoted and should be skipped. */
--- 266 unchanged lines hidden (view full) ---
1148 check against the list of matches
1149 FI */
1150 dtext = (char *)NULL;
1151 if (rl_filename_completion_desired &&
1152 rl_filename_dequoting_function &&
1153 rl_completion_found_quote &&
1154 rl_filename_quoting_desired)
1155 {
1156 dtext = (*rl_filename_dequoting_function) (text, rl_completion_quote_character);
1157 text = dtext;
1158 }
1159
1160 /* sort the list to get consistent answers. */
1161 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
1162
1163 si = strlen (text);
1164 if (si <= low)
--- 229 unchanged lines hidden (view full) ---
1394 if (rl_completion_display_matches_hook)
1395 {
1396 (*rl_completion_display_matches_hook) (matches, len, max);
1397 return;
1398 }
1399
1400 /* If there are many items, then ask the user if she really wants to
1401 see them all. */
1402 if (len >= rl_completion_query_items)
1403 {
1404 rl_crlf ();
1405 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len);
1406 fflush (rl_outstream);
1407 if (get_y_or_n (0) == 0)
1408 {
1409 rl_crlf ();
1410
--- 120 unchanged lines hidden (view full) ---
1531 if (rl_filename_completion_desired)
1532 {
1533 filename = tilde_expand (text);
1534 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0)
1535 ? LSTAT (filename, &finfo)
1536 : stat (filename, &finfo);
1537 if (s == 0 && S_ISDIR (finfo.st_mode))
1538 {
1539 if (_rl_complete_mark_directories)
1540 {
1541 /* This is clumsy. Avoid putting in a double slash if point
1542 is at the end of the line and the previous character is a
1543 slash. */
1544 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/')
1545 ;
1546 else if (rl_line_buffer[rl_point] != '/')
1547 rl_insert_text ("/");
--- 297 unchanged lines hidden (view full) ---
1845 first_char = *text;
1846 first_char_loc = first_char == '~';
1847
1848 username = savestring (&text[first_char_loc]);
1849 namelen = strlen (username);
1850 setpwent ();
1851 }
1852
1853 while (entry = getpwent ())
1854 {
1855 /* Null usernames should result in all users as possible completions. */
1856 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen)))
1857 break;
1858 }
1859
1860 if (entry == 0)
1861 {
1862 endpwent ();
1863 return ((char *)NULL);
1864 }
1865 else
1866 {
1867 value = (char *)xmalloc (2 + strlen (entry->pw_name));
1868
1869 *value = *text;
1870
--- 295 unchanged lines hidden (view full) ---
2166 {
2167 rl_ding ();
2168 FREE (matches);
2169 matches = (char **)0;
2170 completion_changed_buffer = 0;
2171 return (0);
2172 }
2173
2174 match_list_index = (match_list_index + count) % match_list_size;
2175 if (match_list_index < 0)
2176 match_list_index += match_list_size;
2177
2178 if (match_list_index == 0 && match_list_size > 1)
2179 {
2180 rl_ding ();
2181 insert_match (orig_text, orig_start, MULT_MATCH, "e_char);
2182 }
2183 else
2184 {
2185 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char);
2186 append_to_match (matches[match_list_index], delimiter, quote_char,
2187 strcmp (orig_text, matches[match_list_index]));
2188 }
2189
2190 completion_changed_buffer = 1;
2191 return (0);
2192}