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. --- 11 unchanged lines hidden (view full) --- 25 26#if defined (HAVE_CONFIG_H) 27# include <config.h> 28#endif 29 30#include <sys/types.h> 31#include <fcntl.h> 32#if defined (HAVE_SYS_FILE_H) |
33# include <sys/file.h> |
34#endif 35 36#if defined (HAVE_UNISTD_H) 37# include <unistd.h> 38#endif /* HAVE_UNISTD_H */ 39 40#if defined (HAVE_STDLIB_H) 41# include <stdlib.h> --- 54 unchanged lines hidden (view full) --- 96 97#if defined (VISIBLE_STATS) 98# if !defined (X_OK) 99# define X_OK 1 100# endif 101static int stat_char PARAMS((char *)); 102#endif 103 |
104static int path_isdir PARAMS((const char *)); 105 |
106static char *rl_quote_filename PARAMS((char *, int, char *)); 107 108static void set_completion_defaults PARAMS((int)); 109static int get_y_or_n PARAMS((int)); 110static int _rl_internal_pager PARAMS((int)); 111static char *printable_part PARAMS((char *)); |
112static int fnwidth PARAMS((const char *)); 113static int fnprint PARAMS((const char *)); |
114static int print_filename PARAMS((char *, char *)); 115 116static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int)); 117 118static char **remove_duplicate_matches PARAMS((char **)); 119static void insert_match PARAMS((char *, int, int, char *)); 120static int append_to_match PARAMS((char *, int, int, int)); 121static void insert_all_matches PARAMS((char **, int, char *)); --- 9 unchanged lines hidden (view full) --- 131/* */ 132/* **************************************************************** */ 133 134/* Variables known only to the readline library. */ 135 136/* If non-zero, non-unique completions always show the list of matches. */ 137int _rl_complete_show_all = 0; 138 |
139/* If non-zero, non-unique completions show the list of matches, unless it 140 is not possible to do partial completion and modify the line. */ 141int _rl_complete_show_unmodified = 0; 142 |
143/* If non-zero, completed directory names have a slash appended. */ 144int _rl_complete_mark_directories = 1; 145 146/* If non-zero, the symlinked directory completion behavior introduced in 147 readline-4.2a is disabled, and symlinks that point to directories have 148 a slash appended (subject to the value of _rl_complete_mark_directories). 149 This is user-settable via the mark-symlinked-directories variable. */ 150int _rl_complete_mark_symlink_dirs = 0; --- 68 unchanged lines hidden (view full) --- 219const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */ 220 221/* List of basic quoting characters. */ 222const char *rl_basic_quote_characters = "\"'"; 223 224/* The list of characters that signal a break between words for 225 rl_complete_internal. The default list is the contents of 226 rl_basic_word_break_characters. */ |
227/*const*/ char *rl_completer_word_break_characters = (/*const*/ char *)NULL; |
228 |
229/* Hook function to allow an application to set the completion word 230 break characters before readline breaks up the line. Allows 231 position-dependent word break characters. */ 232rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL; 233 |
234/* List of characters which can be used to quote a substring of the line. 235 Completion occurs on the entire substring, and within the substring 236 rl_completer_word_break_characters are treated as any other character, 237 unless they also appear within this list. */ 238const char *rl_completer_quote_characters = (const char *)NULL; 239 240/* List of characters that should be quoted in filenames by the completer. */ 241const char *rl_filename_quote_characters = (const char *)NULL; --- 50 unchanged lines hidden (view full) --- 292 possible closing quote. This is set to 0 by rl_complete_internal and 293 may be changed by an application-specific completion function. */ 294int rl_completion_suppress_append = 0; 295 296/* Character appended to completed words when at the end of the line. The 297 default is a space. */ 298int rl_completion_append_character = ' '; 299 |
300/* If non-zero, the completion functions don't append any closing quote. 301 This is set to 0 by rl_complete_internal and may be changed by an 302 application-specific completion function. */ 303int rl_completion_suppress_quote = 0; 304 305/* Set to any quote character readline thinks it finds before any application 306 completion function is called. */ 307int rl_completion_quote_character; 308 309/* Set to a non-zero value if readline found quoting anywhere in the word to 310 be completed; set before any application completion function is called. */ 311int rl_completion_found_quote; 312 |
313/* If non-zero, a slash will be appended to completed filenames that are 314 symbolic links to directory names, subject to the value of the 315 mark-directories variable (which is user-settable). This exists so 316 that application completion functions can override the user's preference 317 (set via the mark-symlinked-directories variable) if appropriate. 318 It's set to the value of _rl_complete_mark_symlink_dirs in 319 rl_complete_internal before any application-specific completion 320 function is called, so without that function doing anything, the user's --- 22 unchanged lines hidden (view full) --- 343 int ignore, invoking_key; 344{ 345 if (rl_inhibit_completion) 346 return (_rl_insert_char (ignore, invoking_key)); 347 else if (rl_last_func == rl_complete && !completion_changed_buffer) 348 return (rl_complete_internal ('?')); 349 else if (_rl_complete_show_all) 350 return (rl_complete_internal ('!')); |
351 else if (_rl_complete_show_unmodified) 352 return (rl_complete_internal ('@')); |
353 else 354 return (rl_complete_internal (TAB)); 355} 356 357/* List the possible completions. See description of rl_complete (). */ 358int 359rl_possible_completions (ignore, invoking_key) 360 int ignore, invoking_key; --- 16 unchanged lines hidden (view full) --- 377int 378rl_completion_mode (cfunc) 379 rl_command_func_t *cfunc; 380{ 381 if (rl_last_func == cfunc && !completion_changed_buffer) 382 return '?'; 383 else if (_rl_complete_show_all) 384 return '!'; |
385 else if (_rl_complete_show_unmodified) 386 return '@'; |
387 else 388 return TAB; 389} 390 391/************************************/ 392/* */ 393/* Completion utility functions */ 394/* */ --- 4 unchanged lines hidden (view full) --- 399static void 400set_completion_defaults (what_to_do) 401 int what_to_do; 402{ 403 /* Only the completion entry function can change these. */ 404 rl_filename_completion_desired = 0; 405 rl_filename_quoting_desired = 1; 406 rl_completion_type = what_to_do; |
407 rl_completion_suppress_append = rl_completion_suppress_quote = 0; |
408 409 /* The completion entry function may optionally change this. */ 410 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs; 411} 412 413/* The user must press "y" or "n". Non-zero return means "y" pressed. */ 414static int 415get_y_or_n (for_pager) --- 34 unchanged lines hidden (view full) --- 450 if (i == 0) 451 return -1; 452 else if (i == 2) 453 return (lines - 1); 454 else 455 return 0; 456} 457 |
458static int 459path_isdir (filename) 460 const char *filename; 461{ 462 struct stat finfo; 463 464 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode)); 465} 466 |
467#if defined (VISIBLE_STATS) 468/* Return the character which best describes FILENAME. 469 `@' for symbolic links 470 `/' for directories 471 `*' for executables 472 `=' for sockets 473 `|' for FIFOs 474 `%' for character special devices --- 81 unchanged lines hidden (view full) --- 556 if (*x == '/') 557 break; 558 return ((*x == '/') ? x + 1 : pathname); 559 } 560 else 561 return ++temp; 562} 563 |
564/* Compute width of STRING when displayed on screen by print_filename */ 565static int 566fnwidth (string) 567 const char *string; 568{ 569 int width, pos; 570#if defined (HANDLE_MULTIBYTE) 571 mbstate_t ps; 572 int left, w; 573 size_t clen; 574 wchar_t wc; 575 576 left = strlen (string) + 1; 577 memset (&ps, 0, sizeof (mbstate_t)); 578#endif 579 580 width = pos = 0; 581 while (string[pos]) 582 { 583 if (CTRL_CHAR (*string) || *string == RUBOUT) 584 { 585 width += 2; 586 pos++; 587 } 588 else 589 { 590#if defined (HANDLE_MULTIBYTE) 591 clen = mbrtowc (&wc, string + pos, left - pos, &ps); 592 if (MB_INVALIDCH (clen)) 593 { 594 width++; 595 pos++; 596 memset (&ps, 0, sizeof (mbstate_t)); 597 } 598 else if (MB_NULLWCH (clen)) 599 break; 600 else 601 { 602 pos += clen; 603 w = wcwidth (wc); 604 width += (w >= 0) ? w : 1; 605 } 606#else 607 width++; 608 pos++; 609#endif 610 } 611 } 612 613 return width; 614} 615 616static int 617fnprint (to_print) 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) 634 { 635 if (CTRL_CHAR (*s)) 636 { 637 putc ('^', rl_outstream); 638 putc (UNCTRL (*s), rl_outstream); 639 printed_len += 2; 640 s++; 641#if defined (HANDLE_MULTIBYTE) 642 memset (&ps, 0, sizeof (mbstate_t)); 643#endif 644 } 645 else if (*s == RUBOUT) 646 { 647 putc ('^', rl_outstream); 648 putc ('?', rl_outstream); 649 printed_len += 2; 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) 697#endif |
698 { 699 /* If to_print != full_pathname, to_print is the basename of the 700 path passed. In this case, we try to expand the directory 701 name before checking for the stat character. */ 702 if (to_print != full_pathname) 703 { 704 /* Terminate the directory name. */ 705 c = to_print[-1]; --- 10 unchanged lines hidden (view full) --- 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 729 if (path_isdir (new_full_pathname)) 730 extension_char = '/'; |
731 732 free (new_full_pathname); 733 to_print[-1] = c; 734 } 735 else 736 { 737 s = tilde_expand (full_pathname); |
738#if defined (VISIBLE_STATS) 739 if (rl_visible_stats) 740 extension_char = stat_char (s); 741 else 742#endif 743 if (path_isdir (s)) 744 extension_char = '/'; |
745 } 746 747 free (s); 748 if (extension_char) 749 { 750 putc (extension_char, rl_outstream); 751 printed_len++; 752 } 753 } |
754 |
755 return printed_len; 756} 757 758static char * 759rl_quote_filename (s, rtype, qcp) 760 char *s; 761 int rtype; 762 char *qcp; --- 23 unchanged lines hidden (view full) --- 786 quote, or backslash) anywhere in the string. DP, if non-null, is set to 787 the value of the delimiter character that caused a word break. */ 788 789char 790_rl_find_completion_word (fp, dp) 791 int *fp, *dp; 792{ 793 int scan, end, found_quote, delimiter, pass_next, isbrk; |
794 char quote_char, *brkchars; |
795 796 end = rl_point; 797 found_quote = delimiter = 0; 798 quote_char = '\0'; 799 |
800 brkchars = 0; 801 if (rl_completion_word_break_hook) 802 brkchars = (*rl_completion_word_break_hook) (); 803 if (brkchars == 0) 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 --- 41 unchanged lines hidden (view full) --- 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. */ 882 if (rl_char_is_quoted_p && found_quote && 883 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point)) 884 continue; 885 --- 11 unchanged lines hidden (view full) --- 897 function decide whether or not a character is a word break, even 898 if it is found in rl_completer_word_break_characters. Don't bother 899 if we're at the end of the line, though. */ 900 if (scan) 901 { 902 if (rl_char_is_quoted_p) 903 isbrk = (found_quote == 0 || 904 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) && |
905 strchr (brkchars, scan) != 0; |
906 else |
907 isbrk = strchr (brkchars, scan) != 0; |
908 909 if (isbrk) 910 { 911 /* If the character that caused the word break was a quoting 912 character, then remember it as the delimiter. */ 913 if (rl_basic_quote_characters && 914 strchr (rl_basic_quote_characters, scan) && 915 (end - rl_point) > 1) --- 18 unchanged lines hidden (view full) --- 934gen_completion_matches (text, start, end, our_func, found_quote, quote_char) 935 char *text; 936 int start, end; 937 rl_compentry_func_t *our_func; 938 int found_quote, quote_char; 939{ 940 char **matches, *temp; 941 |
942 rl_completion_found_quote = found_quote; 943 rl_completion_quote_character = quote_char; 944 |
945 /* If the user wants to TRY to complete, but then wants to give 946 up and use the default completion function, they set the 947 variable rl_attempted_completion_function. */ 948 if (rl_attempted_completion_function) 949 { 950 matches = (*rl_attempted_completion_function) (text, start, end); 951 952 if (matches || rl_attempted_completion_over) --- 87 unchanged lines hidden (view full) --- 1040static int 1041compute_lcd_of_matches (match_list, matches, text) 1042 char **match_list; 1043 int matches; 1044 const char *text; 1045{ 1046 register int i, c1, c2, si; 1047 int low; /* Count of max-matched characters. */ |
1048 char *dtext; /* dequoted TEXT, if needed */ |
1049#if defined (HANDLE_MULTIBYTE) 1050 int v; 1051 mbstate_t ps1, ps2; 1052 wchar_t wc1, wc2; 1053#endif 1054 1055 /* If only one match, just use that. Otherwise, compare each 1056 member of the list with the next, finding out where they --- 75 unchanged lines hidden (view full) --- 1132 match_list[0] = (char *)xmalloc (low + 1); 1133 1134 /* XXX - this might need changes in the presence of multibyte chars */ 1135 1136 /* If we are ignoring case, try to preserve the case of the string 1137 the user typed in the face of multiple matches differing in case. */ 1138 if (_rl_completion_case_fold) 1139 { |
1140 /* We're making an assumption here: 1141 IF we're completing filenames AND 1142 the application has defined a filename dequoting function AND 1143 we found a quote character AND 1144 the application has requested filename quoting 1145 THEN 1146 we assume that TEXT was dequoted before checking against 1147 the file system and needs to be dequoted here before we 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) 1165 { 1166 for (i = 1; i <= matches; i++) 1167 if (strncmp (match_list[i], text, si) == 0) 1168 { 1169 strncpy (match_list[0], match_list[i], low); 1170 break; 1171 } 1172 /* no casematch, use first entry */ 1173 if (i > matches) 1174 strncpy (match_list[0], match_list[1], low); 1175 } 1176 else 1177 /* otherwise, just use the text the user typed. */ 1178 strncpy (match_list[0], text, low); |
1179 1180 FREE (dtext); |
1181 } 1182 else 1183 strncpy (match_list[0], match_list[1], low); 1184 1185 match_list[0][low] = '\0'; 1186 } 1187 1188 return matches; --- 188 unchanged lines hidden (view full) --- 1377 return; 1378 } 1379 1380 /* There is more than one answer. Find out how many there are, 1381 and find the maximum printed length of a single entry. */ 1382 for (max = 0, i = 1; matches[i]; i++) 1383 { 1384 temp = printable_part (matches[i]); |
1385 len = fnwidth (temp); |
1386 1387 if (len > max) 1388 max = len; 1389 } 1390 1391 len = i - 1; 1392 1393 /* If the caller has defined a display hook, then call that now. */ --- 118 unchanged lines hidden (view full) --- 1512 char *text; 1513 int delimiter, quote_char, nontrivial_match; 1514{ 1515 char temp_string[4], *filename; 1516 int temp_string_index, s; 1517 struct stat finfo; 1518 1519 temp_string_index = 0; |
1520 if (quote_char && rl_point && rl_completion_suppress_quote == 0 && 1521 rl_line_buffer[rl_point - 1] != quote_char) |
1522 temp_string[temp_string_index++] = quote_char; 1523 1524 if (delimiter) 1525 temp_string[temp_string_index++] = delimiter; 1526 else if (rl_completion_suppress_append == 0 && rl_completion_append_character) 1527 temp_string[temp_string_index++] = rl_completion_append_character; 1528 1529 temp_string[temp_string_index++] = '\0'; --- 94 unchanged lines hidden (view full) --- 1624} 1625 1626/* Complete the word at or before point. 1627 WHAT_TO_DO says what to do with the completion. 1628 `?' means list the possible completions. 1629 TAB means do standard completion. 1630 `*' means insert all of the possible completions. 1631 `!' means to do standard completion, and list all possible completions if |
1632 there is more than one. 1633 `@' means to do standard completion, and list all possible completions if 1634 there is more than one and partial completion is not possible. */ |
1635int 1636rl_complete_internal (what_to_do) 1637 int what_to_do; 1638{ 1639 char **matches; 1640 rl_compentry_func_t *our_func; 1641 int start, end, delimiter, found_quote, i, nontrivial_lcd; 1642 char *text, *saved_line_buffer; 1643 char quote_char; 1644 1645 RL_SETSTATE(RL_STATE_COMPLETING); 1646 1647 set_completion_defaults (what_to_do); 1648 1649 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL; 1650 our_func = rl_completion_entry_function 1651 ? rl_completion_entry_function 1652 : rl_filename_completion_function; |
1653 /* We now look backwards for the start of a filename/variable word. */ 1654 end = rl_point; 1655 found_quote = delimiter = 0; 1656 quote_char = '\0'; 1657 1658 if (rl_point) 1659 /* This (possibly) changes rl_point. If it returns a non-zero char, 1660 we know we have an open quote. */ --- 31 unchanged lines hidden (view full) --- 1692 RL_UNSETSTATE(RL_STATE_COMPLETING); 1693 return (0); 1694 } 1695 1696 switch (what_to_do) 1697 { 1698 case TAB: 1699 case '!': |
1700 case '@': |
1701 /* Insert the first match with proper quoting. */ 1702 if (*matches[0]) 1703 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char); 1704 1705 /* If there are more matches, ring the bell to indicate. 1706 If we are in vi mode, Posix.2 says to not ring the bell. 1707 If the `show-all-if-ambiguous' variable is set, display 1708 all the matches immediately. Otherwise, if this was the 1709 only match, and we are hacking files, check the file to 1710 see if it was a directory. If so, and the `mark-directories' 1711 variable is set, add a '/' to the name. If not, and we 1712 are at the end of the line, then add a space. */ 1713 if (matches[1]) 1714 { 1715 if (what_to_do == '!') 1716 { 1717 display_matches (matches); 1718 break; 1719 } |
1720 else if (what_to_do == '@') 1721 { 1722 if (nontrivial_lcd == 0) 1723 display_matches (matches); 1724 break; 1725 } |
1726 else if (rl_editing_mode != vi_mode) 1727 rl_ding (); /* There are other matches remaining. */ 1728 } 1729 else 1730 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd); 1731 1732 break; 1733 --- 459 unchanged lines hidden --- |