complete.c (136760) | complete.c (157188) |
---|---|
1/* $FreeBSD: head/contrib/libreadline/complete.c 136758 2004-10-21 20:02:02Z peter $ */ 2 | 1/* $FreeBSD: head/contrib/libreadline/complete.c 157188 2006-03-27 23:11:32Z ache $ */ |
3/* complete.c -- filename completion for readline. */ 4 | 2/* complete.c -- filename completion for readline. */ 3 |
5/* Copyright (C) 1987-2004 Free Software Foundation, Inc. | 4/* Copyright (C) 1987-2005 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 | 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. --- 31 unchanged lines hidden (view full) --- 44 45#include <stdio.h> 46 47#include <errno.h> 48#if !defined (errno) 49extern int errno; 50#endif /* !errno */ 51 |
52#if defined (HAVE_PWD_H) |
|
53#include <pwd.h> | 53#include <pwd.h> |
54#endif |
|
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. */ | 55 56#include "posixdir.h" 57#include "posixstat.h" 58 59/* System-specific feature definitions and include files. */ 60#include "rldefs.h" 61#include "rlmbutil.h" 62 --- 14 unchanged lines hidden (view full) --- 77# define LSTAT stat 78#endif 79 80/* Unix version of a hidden file. Could be different on other systems. */ 81#define HIDDEN_FILE(fname) ((fname)[0] == '.') 82 83/* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is 84 defined. */ |
84#if !defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE) | 85#if defined (HAVE_GETPWENT) && (!defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE)) |
85extern struct passwd *getpwent PARAMS((void)); | 86extern struct passwd *getpwent PARAMS((void)); |
86#endif /* !HAVE_GETPW_DECLS || _POSIX_SOURCE */ | 87#endif /* HAVE_GETPWENT && (!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 | 88 89/* If non-zero, then this is the address of a function to call when 90 completing a word would normally display the list of possible matches. 91 This function is called instead of actually doing the display. 92 It takes three arguments: (char **matches, int num_matches, int max_length) 93 where MATCHES is the array of strings that matched, NUM_MATCHES is the 94 number of strings in that array, and MAX_LENGTH is the length of the 95 longest string in that array. */ --- 108 unchanged lines hidden (view full) --- 204 205/* Set to a character indicating the type of completion being performed 206 by rl_complete_internal, available for use by application completion 207 functions. */ 208int rl_completion_type = 0; 209 210/* Up to this many items will be displayed in response to a 211 possible-completions call. After that, we ask the user if |
211 she is sure she wants to see them all. */ | 212 she is sure she wants to see them all. A negative value means 213 don't ask. */ |
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; | 214int rl_completion_query_items = 100; 215 216int _rl_page_completions = 1; 217 218/* The basic list of characters that signal a break between words for the 219 completer routine. The contents of this variable is what breaks words 220 in the shell, i.e. " \t\n\"\\'`@$><=" */ 221const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */ --- 398 unchanged lines hidden (view full) --- 620 const char *to_print; 621{ 622 int printed_len; 623 const char *s; 624#if defined (HANDLE_MULTIBYTE) 625 mbstate_t ps; 626 const char *end; 627 size_t tlen; |
628 int width, w; 629 wchar_t wc; |
|
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) | 630 631 end = to_print + strlen (to_print) + 1; 632 memset (&ps, 0, sizeof (mbstate_t)); 633#endif 634 635 printed_len = 0; 636 s = to_print; 637 while (*s) --- 16 unchanged lines hidden (view full) --- 654 s++; 655#if defined (HANDLE_MULTIBYTE) 656 memset (&ps, 0, sizeof (mbstate_t)); 657#endif 658 } 659 else 660 { 661#if defined (HANDLE_MULTIBYTE) |
658 tlen = mbrlen (s, end - s, &ps); | 662 tlen = mbrtowc (&wc, s, end - s, &ps); |
659 if (MB_INVALIDCH (tlen)) 660 { 661 tlen = 1; | 663 if (MB_INVALIDCH (tlen)) 664 { 665 tlen = 1; |
666 width = 1; |
|
662 memset (&ps, 0, sizeof (mbstate_t)); 663 } 664 else if (MB_NULLWCH (tlen)) 665 break; | 667 memset (&ps, 0, sizeof (mbstate_t)); 668 } 669 else if (MB_NULLWCH (tlen)) 670 break; |
671 else 672 { 673 w = wcwidth (wc); 674 width = (w >= 0) ? w : 1; 675 } |
|
666 fwrite (s, 1, tlen, rl_outstream); 667 s += tlen; | 676 fwrite (s, 1, tlen, rl_outstream); 677 s += tlen; |
678 printed_len += width; |
|
668#else 669 putc (*s, rl_outstream); 670 s++; | 679#else 680 putc (*s, rl_outstream); 681 s++; |
671#endif | |
672 printed_len++; | 682 printed_len++; |
683#endif |
|
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; | 684 } 685 } 686 687 return printed_len; 688} 689 690/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we 691 are using it, check for and output a single character for `special' 692 filenames. Return the number of characters we output. */ 693 694static int 695print_filename (to_print, full_pathname) 696 char *to_print, *full_pathname; 697{ 698 int printed_len, extension_char, slen, tlen; |
688 char *s, c, *new_full_pathname; | 699 char *s, c, *new_full_pathname, *dn; |
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 `/'. */ | 700 701 extension_char = 0; 702 printed_len = fnprint (to_print); 703 704#if defined (VISIBLE_STATS) 705 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories)) 706#else 707 if (rl_filename_completion_desired && _rl_complete_mark_directories) --- 8 unchanged lines hidden (view full) --- 716 c = to_print[-1]; 717 to_print[-1] = '\0'; 718 719 /* If setting the last slash in full_pathname to a NUL results in 720 full_pathname being the empty string, we are trying to complete 721 files in the root directory. If we pass a null string to the 722 bash directory completion hook, for example, it will expand it 723 to the current directory. We just want the `/'. */ |
713 s = tilde_expand (full_pathname && *full_pathname ? full_pathname : "/"); | 724 if (full_pathname == 0 || *full_pathname == 0) 725 dn = "/"; 726 else if (full_pathname[0] != '/') 727 dn = full_pathname; 728 else if (full_pathname[1] == 0) 729 dn = "//"; /* restore trailing slash to `//' */ 730 else if (full_pathname[1] == '/' && full_pathname[2] == 0) 731 dn = "/"; /* don't turn /// into // */ 732 else 733 dn = full_pathname; 734 s = tilde_expand (dn); |
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); | 735 if (rl_directory_completion_hook) 736 (*rl_directory_completion_hook) (&s); 737 738 slen = strlen (s); 739 tlen = strlen (to_print); 740 new_full_pathname = (char *)xmalloc (slen + tlen + 2); 741 strcpy (new_full_pathname, s); |
742 if (s[slen - 1] == '/') 743 slen--; 744 else 745 new_full_pathname[slen] = '/'; |
|
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. */ | 746 new_full_pathname[slen] = '/'; 747 strcpy (new_full_pathname + slen + 1, to_print); 748 749#if defined (VISIBLE_STATS) 750 if (rl_visible_stats) 751 extension_char = stat_char (new_full_pathname); 752 else 753#endif --- 75 unchanged lines hidden (view full) --- 829 brkchars = rl_completer_word_break_characters; 830 831 if (rl_completer_quote_characters) 832 { 833 /* We have a list of characters which can be used in pairs to 834 quote substrings for the completer. Try to find the start 835 of an unclosed quoted substring. */ 836 /* 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 | 837 for (scan = pass_next = 0; scan < end; scan = MB_NEXTCHAR (rl_line_buffer, scan, 1, MB_FIND_ANY)) |
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. */ | 838 { 839 if (pass_next) 840 { 841 pass_next = 0; 842 continue; 843 } 844 845 /* Shell-like semantics for single quotes -- don't allow backslash --- 33 unchanged lines hidden (view full) --- 879 } 880 } 881 882 if (rl_point == end && quote_char == '\0') 883 { 884 /* We didn't find an unclosed quoted substring upon which to do 885 completion, so use the word break characters to find the 886 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 | 887 while (rl_point = MB_PREVCHAR (rl_line_buffer, rl_point, MB_FIND_ANY)) |
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 { | 888 { 889 scan = rl_line_buffer[rl_point]; 890 891 if (strchr (brkchars, scan) == 0) 892 continue; 893 894 /* Call the application-specific function to tell us whether 895 this word break character is quoted and should be skipped. */ --- 266 unchanged lines hidden (view full) --- 1162 check against the list of matches 1163 FI */ 1164 dtext = (char *)NULL; 1165 if (rl_filename_completion_desired && 1166 rl_filename_dequoting_function && 1167 rl_completion_found_quote && 1168 rl_filename_quoting_desired) 1169 { |
1156 dtext = (*rl_filename_dequoting_function) (text, rl_completion_quote_character); | 1170 dtext = (*rl_filename_dequoting_function) ((char *)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. */ | 1171 text = dtext; 1172 } 1173 1174 /* sort the list to get consistent answers. */ 1175 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare); 1176 1177 si = strlen (text); 1178 if (si <= low) --- 229 unchanged lines hidden (view full) --- 1408 if (rl_completion_display_matches_hook) 1409 { 1410 (*rl_completion_display_matches_hook) (matches, len, max); 1411 return; 1412 } 1413 1414 /* If there are many items, then ask the user if she really wants to 1415 see them all. */ |
1402 if (len >= rl_completion_query_items) | 1416 if (rl_completion_query_items > 0 && 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 { | 1417 { 1418 rl_crlf (); 1419 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len); 1420 fflush (rl_outstream); 1421 if (get_y_or_n (0) == 0) 1422 { 1423 rl_crlf (); 1424 --- 120 unchanged lines hidden (view full) --- 1545 if (rl_filename_completion_desired) 1546 { 1547 filename = tilde_expand (text); 1548 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0) 1549 ? LSTAT (filename, &finfo) 1550 : stat (filename, &finfo); 1551 if (s == 0 && S_ISDIR (finfo.st_mode)) 1552 { |
1539 if (_rl_complete_mark_directories) | 1553 if (_rl_complete_mark_directories /* && rl_completion_suppress_append == 0 */) |
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 | 1554 { 1555 /* This is clumsy. Avoid putting in a double slash if point 1556 is at the end of the line and the previous character is a 1557 slash. */ 1558 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/') 1559 ; 1560 else if (rl_line_buffer[rl_point] != '/') 1561 rl_insert_text ("/"); --- 297 unchanged lines hidden (view full) --- 1859 first_char = *text; 1860 first_char_loc = first_char == '~'; 1861 1862 username = savestring (&text[first_char_loc]); 1863 namelen = strlen (username); 1864 setpwent (); 1865 } 1866 |
1867#if defined (HAVE_GETPWENT) |
|
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 } | 1868 while (entry = getpwent ()) 1869 { 1870 /* Null usernames should result in all users as possible completions. */ 1871 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen))) 1872 break; 1873 } |
1874#endif |
|
1859 1860 if (entry == 0) 1861 { | 1875 1876 if (entry == 0) 1877 { |
1878#if defined (HAVE_GETPWENT) |
|
1862 endpwent (); | 1879 endpwent (); |
1880#endif |
|
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 | 1881 return ((char *)NULL); 1882 } 1883 else 1884 { 1885 value = (char *)xmalloc (2 + strlen (entry->pw_name)); 1886 1887 *value = *text; 1888 --- 295 unchanged lines hidden (view full) --- 2184 { 2185 rl_ding (); 2186 FREE (matches); 2187 matches = (char **)0; 2188 completion_changed_buffer = 0; 2189 return (0); 2190 } 2191 |
2174 match_list_index = (match_list_index + count) % match_list_size; | 2192 match_list_index += count; |
2175 if (match_list_index < 0) 2176 match_list_index += match_list_size; | 2193 if (match_list_index < 0) 2194 match_list_index += match_list_size; |
2195 else 2196 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} | 2197 2198 if (match_list_index == 0 && match_list_size > 1) 2199 { 2200 rl_ding (); 2201 insert_match (orig_text, orig_start, MULT_MATCH, "e_char); 2202 } 2203 else 2204 { 2205 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char); 2206 append_to_match (matches[match_list_index], delimiter, quote_char, 2207 strcmp (orig_text, matches[match_list_index])); 2208 } 2209 2210 completion_changed_buffer = 1; 2211 return (0); 2212} |