complete.c revision 21309
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 <stdio.h> 29#include <sys/types.h> 30#include <fcntl.h> 31#if defined (HAVE_SYS_FILE_H) 32#include <sys/file.h> 33#endif 34 35#if defined (HAVE_UNISTD_H) 36# include <unistd.h> 37#endif /* HAVE_UNISTD_H */ 38 39#if defined (HAVE_STDLIB_H) 40# include <stdlib.h> 41#else 42# include "ansi_stdlib.h" 43#endif /* HAVE_STDLIB_H */ 44 45#include <errno.h> 46#if !defined (errno) 47extern int errno; 48#endif /* !errno */ 49 50#include <pwd.h> 51#if !defined (HAVE_GETPW_DECLS) 52extern struct passwd *getpwent (); 53#endif /* USG && !HAVE_GETPW_DECLS */ 54 55/* ISC systems don't define getpwent() if _POSIX_SOURCE is defined. */ 56#if defined (isc386) && defined (_POSIX_SOURCE) 57# if defined (__STDC__) 58extern struct passwd *getpwent (void); 59# else 60extern struct passwd *getpwent (); 61# endif /* !__STDC__ */ 62#endif /* isc386 && _POSIX_SOURCE */ 63 64#include "posixdir.h" 65#include "posixstat.h" 66 67/* System-specific feature definitions and include files. */ 68#include "rldefs.h" 69 70/* Some standard library routines. */ 71#include "readline.h" 72 73extern char *tilde_expand (); 74extern char *rl_copy_text (); 75extern void _rl_abort_internal (); 76extern int _rl_qsort_string_compare (); 77 78extern Function *rl_last_func; 79extern int rl_editing_mode; 80extern int screenwidth; 81 82extern void _rl_move_vert (); 83extern int _rl_vis_botlin; 84extern int rl_display_fixed; 85 86/* Forward declarations for functions defined and used in this file. */ 87char *filename_completion_function (); 88char **completion_matches (); 89 90static char *rl_quote_filename (); 91static char *rl_strpbrk (); 92 93static char **remove_duplicate_matches (); 94static void insert_text (); 95static void insert_match (); 96static void append_to_match (); 97static void insert_all_matches (); 98static void display_matches (); 99static int compute_lcd_of_matches (); 100 101extern char *xmalloc (), *xrealloc (); 102 103/* If non-zero, then this is the address of a function to call when 104 completing on a directory name. The function is called with 105 the address of a string (the current directory name) as an arg. */ 106Function *rl_directory_completion_hook = (Function *)NULL; 107 108/* Non-zero means readline completion functions perform tilde expansion. */ 109int rl_complete_with_tilde_expansion = 0; 110 111/* If non-zero, non-unique completions always show the list of matches. */ 112int _rl_complete_show_all = 0; 113 114/* If non-zero, completed directory names have a slash appended. */ 115int _rl_complete_mark_directories = 1; 116 117#if defined (VISIBLE_STATS) 118# if !defined (X_OK) 119# define X_OK 1 120# endif 121 122static int stat_char (); 123 124/* Non-zero means add an additional character to each filename displayed 125 during listing completion iff rl_filename_completion_desired which helps 126 to indicate the type of file being listed. */ 127int rl_visible_stats = 0; 128#endif /* VISIBLE_STATS */ 129 130/* **************************************************************** */ 131/* */ 132/* Completion matching, from readline's point of view. */ 133/* */ 134/* **************************************************************** */ 135 136/* Local variable states what happened during the last completion attempt. */ 137static int completion_changed_buffer; 138 139/* Pointer to the generator function for completion_matches (). 140 NULL means to use filename_entry_function (), the default filename 141 completer. */ 142Function *rl_completion_entry_function = (Function *)NULL; 143 144/* Pointer to alternative function to create matches. 145 Function is called with TEXT, START, and END. 146 START and END are indices in RL_LINE_BUFFER saying what the boundaries 147 of TEXT are. 148 If this function exists and returns NULL then call the value of 149 rl_completion_entry_function to try to match, otherwise use the 150 array of strings returned. */ 151CPPFunction *rl_attempted_completion_function = (CPPFunction *)NULL; 152 153/* Non-zero means to suppress normal filename completion after the 154 user-specified completion function has been called. */ 155int rl_attempted_completion_over = 0; 156 157/* Set to a character indicating the type of completion being performed 158 by rl_complete_internal, available for use by application completion 159 functions. */ 160int rl_completion_type = 0; 161 162/* Up to this many items will be displayed in response to a 163 possible-completions call. After that, we ask the user if 164 she is sure she wants to see them all. */ 165int rl_completion_query_items = 100; 166 167/* The basic list of characters that signal a break between words for the 168 completer routine. The contents of this variable is what breaks words 169 in the shell, i.e. " \t\n\"\\'`@$><=" */ 170char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; 171 172/* List of basic quoting characters. */ 173char *rl_basic_quote_characters = "\"'"; 174 175/* The list of characters that signal a break between words for 176 rl_complete_internal. The default list is the contents of 177 rl_basic_word_break_characters. */ 178char *rl_completer_word_break_characters = (char *)NULL; 179 180/* List of characters which can be used to quote a substring of the line. 181 Completion occurs on the entire substring, and within the substring 182 rl_completer_word_break_characters are treated as any other character, 183 unless they also appear within this list. */ 184char *rl_completer_quote_characters = (char *)NULL; 185 186/* List of characters that should be quoted in filenames by the completer. */ 187char *rl_filename_quote_characters = (char *)NULL; 188 189/* List of characters that are word break characters, but should be left 190 in TEXT when it is passed to the completion function. The shell uses 191 this to help determine what kind of completing to do. */ 192char *rl_special_prefixes = (char *)NULL; 193 194/* If non-zero, then disallow duplicates in the matches. */ 195int rl_ignore_completion_duplicates = 1; 196 197/* Non-zero means that the results of the matches are to be treated 198 as filenames. This is ALWAYS zero on entry, and can only be changed 199 within a completion entry finder function. */ 200int rl_filename_completion_desired = 0; 201 202/* Non-zero means that the results of the matches are to be quoted using 203 double quotes (or an application-specific quoting mechanism) if the 204 filename contains any characters in rl_filename_quote_chars. This is 205 ALWAYS non-zero on entry, and can only be changed within a completion 206 entry finder function. */ 207int rl_filename_quoting_desired = 1; 208 209/* This function, if defined, is called by the completer when real 210 filename completion is done, after all the matching names have been 211 generated. It is passed a (char**) known as matches in the code below. 212 It consists of a NULL-terminated array of pointers to potential 213 matching strings. The 1st element (matches[0]) is the maximal 214 substring that is common to all matches. This function can re-arrange 215 the list of matches as required, but all elements of the array must be 216 free()'d if they are deleted. The main intent of this function is 217 to implement FIGNORE a la SunOS csh. */ 218Function *rl_ignore_some_completions_function = (Function *)NULL; 219 220/* Set to a function to quote a filename in an application-specific fashion. 221 Called with the text to quote, the type of match found (single or multiple) 222 and a pointer to the quoting character to be used, which the function can 223 reset if desired. */ 224CPFunction *rl_filename_quoting_function = rl_quote_filename; 225 226/* Function to call to remove quoting characters from a filename. Called 227 before completion is attempted, so the embedded quotes do not interfere 228 with matching names in the file system. Readline doesn't do anything 229 with this; it's set only by applications. */ 230CPFunction *rl_filename_dequoting_function = (CPFunction *)NULL; 231 232/* Function to call to decide whether or not a word break character is 233 quoted. If a character is quoted, it does not break words for the 234 completer. */ 235Function *rl_char_is_quoted_p = (Function *)NULL; 236 237/* Character appended to completed words when at the end of the line. The 238 default is a space. */ 239int rl_completion_append_character = ' '; 240 241/* If non-zero, inhibit completion (temporarily). */ 242int rl_inhibit_completion; 243 244/* Complete the word at or before point. You have supplied the function 245 that does the initial simple matching selection algorithm (see 246 completion_matches ()). The default is to do filename completion. */ 247int 248rl_complete (ignore, invoking_key) 249 int ignore, invoking_key; 250{ 251 if (rl_inhibit_completion) 252 return (rl_insert (ignore, invoking_key)); 253 else if (rl_last_func == rl_complete && !completion_changed_buffer) 254 return (rl_complete_internal ('?')); 255 else if (_rl_complete_show_all) 256 return (rl_complete_internal ('!')); 257 else 258 return (rl_complete_internal (TAB)); 259} 260 261/* List the possible completions. See description of rl_complete (). */ 262int 263rl_possible_completions (ignore, invoking_key) 264 int ignore, invoking_key; 265{ 266 return (rl_complete_internal ('?')); 267} 268 269int 270rl_insert_completions (ignore, invoking_key) 271 int ignore, invoking_key; 272{ 273 return (rl_complete_internal ('*')); 274} 275 276/* The user must press "y" or "n". Non-zero return means "y" pressed. */ 277static int 278get_y_or_n () 279{ 280 int c; 281 282 for (;;) 283 { 284 c = rl_read_key (); 285 if (c == 'y' || c == 'Y' || c == ' ') 286 return (1); 287 if (c == 'n' || c == 'N' || c == RUBOUT) 288 return (0); 289 if (c == ABORT_CHAR) 290 _rl_abort_internal (); 291 ding (); 292 } 293} 294 295/* Return the portion of PATHNAME that should be output when listing 296 possible completions. If we are hacking filename completion, we 297 are only interested in the basename, the portion following the 298 final slash. Otherwise, we return what we were passed. */ 299static char * 300printable_part (pathname) 301 char *pathname; 302{ 303 char *temp; 304 305 temp = rl_filename_completion_desired ? strrchr (pathname, '/') : (char *)NULL; 306 return (temp ? ++temp : pathname); 307} 308 309/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we 310 are using it, check for and output a single character for `special' 311 filenames. Return 1 if we printed an extension character, 0 if not. */ 312 313#define PUTX(c) \ 314 if (CTRL_CHAR (c)) \ 315 { \ 316 putc ('^', rl_outstream); \ 317 putc (UNCTRL (c), rl_outstream); \ 318 } \ 319 else if (c == RUBOUT) \ 320 { \ 321 putc ('^', rl_outstream); \ 322 putc ('?', rl_outstream); \ 323 } \ 324 else \ 325 putc (c, rl_outstream) 326 327static int 328print_filename (to_print, full_pathname) 329 char *to_print, *full_pathname; 330{ 331#if !defined (VISIBLE_STATS) 332 char *s; 333 334 for (s = to_print; *s; s++) 335 { 336 PUTX (*s); 337 } 338 return 0; 339#else 340 char *s, c, *new_full_pathname; 341 int extension_char, slen, tlen; 342 343 for (s = to_print; *s; s++) 344 { 345 PUTX (*s); 346 } 347 348 if (rl_filename_completion_desired && rl_visible_stats) 349 { 350 /* If to_print != full_pathname, to_print is the basename of the 351 path passed. In this case, we try to expand the directory 352 name before checking for the stat character. */ 353 if (to_print != full_pathname) 354 { 355 /* Terminate the directory name. */ 356 c = to_print[-1]; 357 to_print[-1] = '\0'; 358 359 s = tilde_expand (full_pathname); 360 if (rl_directory_completion_hook) 361 (*rl_directory_completion_hook) (&s); 362 363 slen = strlen (s); 364 tlen = strlen (to_print); 365 new_full_pathname = xmalloc (slen + tlen + 2); 366 strcpy (new_full_pathname, s); 367 new_full_pathname[slen] = '/'; 368 strcpy (new_full_pathname + slen + 1, to_print); 369 370 extension_char = stat_char (new_full_pathname); 371 372 free (new_full_pathname); 373 to_print[-1] = c; 374 } 375 else 376 { 377 s = tilde_expand (full_pathname); 378 extension_char = stat_char (s); 379 } 380 381 free (s); 382 if (extension_char) 383 putc (extension_char, rl_outstream); 384 return (extension_char != 0); 385 } 386 else 387 return 0; 388#endif /* VISIBLE_STATS */ 389} 390 391static char * 392rl_quote_filename (s, rtype, qcp) 393 char *s; 394 int rtype; 395 char *qcp; 396{ 397 char *r; 398 399 r = xmalloc (strlen (s) + 2); 400 *r = *rl_completer_quote_characters; 401 strcpy (r + 1, s); 402 if (qcp) 403 *qcp = *rl_completer_quote_characters; 404 return r; 405} 406 407/* Find the bounds of the current word for completion purposes, and leave 408 rl_point set to the end of the word. This function skips quoted 409 substrings (characters between matched pairs of characters in 410 rl_completer_quote_characters. First we try to find an unclosed 411 quoted substring on which to do matching. If one is not found, we use 412 the word break characters to find the boundaries of the current word. 413 We call an application-specific function to decide whether or not a 414 particular word break character is quoted; if that function returns a 415 non-zero result, the character does not break a word. This function 416 returns the opening quote character if we found an unclosed quoted 417 substring, '\0' otherwise. FP, if non-null, is set to a value saying 418 which (shell-like) quote characters we found (single quote, double 419 quote, or backslash) anywhere in the string. DP, if non-null, is set to 420 the value of the delimiter character that caused a word break. */ 421 422static char 423find_completion_word (fp, dp) 424 int *fp, *dp; 425{ 426 int scan, end, found_quote, delimiter, pass_next, isbrk; 427 char quote_char; 428 429 end = rl_point; 430 found_quote = delimiter = 0; 431 quote_char = '\0'; 432 433 if (rl_completer_quote_characters) 434 { 435 /* We have a list of characters which can be used in pairs to 436 quote substrings for the completer. Try to find the start 437 of an unclosed quoted substring. */ 438 /* FOUND_QUOTE is set so we know what kind of quotes we found. */ 439 for (scan = pass_next = 0; scan < end; scan++) 440 { 441 if (pass_next) 442 { 443 pass_next = 0; 444 continue; 445 } 446 447 if (rl_line_buffer[scan] == '\\') 448 { 449 pass_next = 1; 450 found_quote |= RL_QF_BACKSLASH; 451 continue; 452 } 453 454 if (quote_char != '\0') 455 { 456 /* Ignore everything until the matching close quote char. */ 457 if (rl_line_buffer[scan] == quote_char) 458 { 459 /* Found matching close. Abandon this substring. */ 460 quote_char = '\0'; 461 rl_point = end; 462 } 463 } 464 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan])) 465 { 466 /* Found start of a quoted substring. */ 467 quote_char = rl_line_buffer[scan]; 468 rl_point = scan + 1; 469 /* Shell-like quoting conventions. */ 470 if (quote_char == '\'') 471 found_quote |= RL_QF_SINGLE_QUOTE; 472 else if (quote_char == '"') 473 found_quote |= RL_QF_DOUBLE_QUOTE; 474 } 475 } 476 } 477 478 if (rl_point == end && quote_char == '\0') 479 { 480 /* We didn't find an unclosed quoted substring upon which to do 481 completion, so use the word break characters to find the 482 substring on which to complete. */ 483 while (--rl_point) 484 { 485 scan = rl_line_buffer[rl_point]; 486 487 if (strchr (rl_completer_word_break_characters, scan) == 0) 488 continue; 489 490 /* Call the application-specific function to tell us whether 491 this word break character is quoted and should be skipped. */ 492 if (rl_char_is_quoted_p && found_quote && 493 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point)) 494 continue; 495 496 /* Convoluted code, but it avoids an n^2 algorithm with calls 497 to char_is_quoted. */ 498 break; 499 } 500 } 501 502 /* If we are at an unquoted word break, then advance past it. */ 503 scan = rl_line_buffer[rl_point]; 504 505 /* If there is an application-specific function to say whether or not 506 a character is quoted and we found a quote character, let that 507 function decide whether or not a character is a word break, even 508 if it is found in rl_completer_word_break_characters. */ 509 if (rl_char_is_quoted_p) 510 isbrk = (found_quote == 0 || 511 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) && 512 strchr (rl_completer_word_break_characters, scan) != 0; 513 else 514 isbrk = strchr (rl_completer_word_break_characters, scan) != 0; 515 516 if (isbrk) 517 { 518 /* If the character that caused the word break was a quoting 519 character, then remember it as the delimiter. */ 520 if (rl_basic_quote_characters && strchr (rl_basic_quote_characters, scan) && (end - rl_point) > 1) 521 delimiter = scan; 522 523 /* If the character isn't needed to determine something special 524 about what kind of completion to perform, then advance past it. */ 525 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0) 526 rl_point++; 527 } 528 529 if (fp) 530 *fp = found_quote; 531 if (dp) 532 *dp = delimiter; 533 534 return (quote_char); 535} 536 537static char ** 538gen_completion_matches (text, start, end, our_func, found_quote, quote_char) 539 char *text; 540 int start, end; 541 Function *our_func; 542 int found_quote, quote_char; 543{ 544 char **matches, *temp; 545 546 /* If the user wants to TRY to complete, but then wants to give 547 up and use the default completion function, they set the 548 variable rl_attempted_completion_function. */ 549 if (rl_attempted_completion_function) 550 { 551 matches = (*rl_attempted_completion_function) (text, start, end); 552 553 if (matches || rl_attempted_completion_over) 554 { 555 rl_attempted_completion_over = 0; 556 return (matches); 557 } 558 } 559 560 /* Beware -- we're stripping the quotes here. Do this only if we know 561 we are doing filename completion and the application has defined a 562 filename dequoting function. */ 563 temp = (char *)NULL; 564 if (found_quote && our_func == (Function *)filename_completion_function && 565 rl_filename_dequoting_function) 566 { 567 /* delete single and double quotes */ 568 temp = (*rl_filename_dequoting_function) (text, quote_char); 569 text = temp; /* not freeing text is not a memory leak */ 570 } 571 572 matches = completion_matches (text, our_func); 573 FREE (temp); 574 return matches; 575} 576 577/* Filter out duplicates in MATCHES. This frees up the strings in 578 MATCHES. */ 579static char ** 580remove_duplicate_matches (matches) 581 char **matches; 582{ 583 char *lowest_common; 584 int i, j, newlen; 585 char dead_slot; 586 char **temp_array; 587 588 /* Sort the items. */ 589 for (i = 0; matches[i]; i++) 590 ; 591 592 /* Sort the array without matches[0], since we need it to 593 stay in place no matter what. */ 594 if (i) 595 qsort (matches+1, i-1, sizeof (char *), _rl_qsort_string_compare); 596 597 /* Remember the lowest common denominator for it may be unique. */ 598 lowest_common = savestring (matches[0]); 599 600 for (i = newlen = 0; matches[i + 1]; i++) 601 { 602 if (strcmp (matches[i], matches[i + 1]) == 0) 603 { 604 free (matches[i]); 605 matches[i] = (char *)&dead_slot; 606 } 607 else 608 newlen++; 609 } 610 611 /* We have marked all the dead slots with (char *)&dead_slot. 612 Copy all the non-dead entries into a new array. */ 613 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *)); 614 for (i = j = 1; matches[i]; i++) 615 { 616 if (matches[i] != (char *)&dead_slot) 617 temp_array[j++] = matches[i]; 618 } 619 temp_array[j] = (char *)NULL; 620 621 if (matches[0] != (char *)&dead_slot) 622 free (matches[0]); 623 624 /* Place the lowest common denominator back in [0]. */ 625 temp_array[0] = lowest_common; 626 627 /* If there is one string left, and it is identical to the 628 lowest common denominator, then the LCD is the string to 629 insert. */ 630 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0) 631 { 632 free (temp_array[1]); 633 temp_array[1] = (char *)NULL; 634 } 635 return (temp_array); 636} 637 638static void 639display_matches (matches) 640 char **matches; 641{ 642 int len, count, limit, max, printed_len; 643 int i, j, k, l; 644 char *temp; 645 646 /* Move to the last visible line of a possibly-multiple-line command. */ 647 _rl_move_vert (_rl_vis_botlin); 648 649 /* Handle simple case first. What if there is only one answer? */ 650 if (matches[1] == 0) 651 { 652 temp = printable_part (matches[0]); 653 crlf (); 654 print_filename (temp, matches[0]); 655 crlf (); 656#if 0 657 rl_on_new_line (); 658#else 659 rl_forced_update_display (); 660 rl_display_fixed = 1; 661#endif 662 return; 663 } 664 665 /* There is more than one answer. Find out how many there are, 666 and find the maximum printed length of a single entry. */ 667 for (max = 0, i = 1; matches[i]; i++) 668 { 669 temp = printable_part (matches[i]); 670 len = strlen (temp); 671 672 if (len > max) 673 max = len; 674 } 675 676 len = i - 1; 677 678 /* If there are many items, then ask the user if she really wants to 679 see them all. */ 680 if (len >= rl_completion_query_items) 681 { 682 crlf (); 683 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len); 684 fflush (rl_outstream); 685 if (get_y_or_n () == 0) 686 { 687 crlf (); 688#if 0 689 rl_on_new_line (); 690#else 691 rl_forced_update_display (); 692 rl_display_fixed = 1; 693#endif 694 return; 695 } 696 } 697 698 /* How many items of MAX length can we fit in the screen window? */ 699 max += 2; 700 limit = screenwidth / max; 701 if (limit != 1 && (limit * max == screenwidth)) 702 limit--; 703 704 /* Avoid a possible floating exception. If max > screenwidth, 705 limit will be 0 and a divide-by-zero fault will result. */ 706 if (limit == 0) 707 limit = 1; 708 709 /* How many iterations of the printing loop? */ 710 count = (len + (limit - 1)) / limit; 711 712 /* Watch out for special case. If LEN is less than LIMIT, then 713 just do the inner printing loop. 714 0 < len <= limit implies count = 1. */ 715 716 /* Sort the items if they are not already sorted. */ 717 if (rl_ignore_completion_duplicates == 0) 718 qsort (matches + 1, len, sizeof (char *), _rl_qsort_string_compare); 719 720 /* Print the sorted items, up-and-down alphabetically, like ls. */ 721 crlf (); 722 723 for (i = 1; i <= count; i++) 724 { 725 for (j = 0, l = i; j < limit; j++) 726 { 727 if (l > len || matches[l] == 0) 728 break; 729 else 730 { 731 temp = printable_part (matches[l]); 732 printed_len = strlen (temp) + print_filename (temp, matches[l]); 733 734 if (j + 1 < limit) 735 for (k = 0; k < max - printed_len; k++) 736 putc (' ', rl_outstream); 737 } 738 l += count; 739 } 740 crlf (); 741 } 742 743#if 0 744 rl_on_new_line (); 745#else 746 rl_forced_update_display (); 747 rl_display_fixed = 1; 748#endif 749} 750 751static void 752insert_text (text, start, end) 753 char *text; 754 int start, end; 755{ 756 rl_begin_undo_group (); 757 rl_delete_text (start, end + 1); 758 rl_point = start; 759 rl_insert_text (text); 760 rl_end_undo_group (); 761} 762 763static char * 764make_quoted_replacement (match, mtype, quote_char) 765 char *match; 766 int mtype, quote_char; 767{ 768 int should_quote, do_replace; 769 char *replacement, qc; 770 771 /* If we are doing completion on quoted substrings, and any matches 772 contain any of the completer_word_break_characters, then auto- 773 matically prepend the substring with a quote character (just pick 774 the first one from the list of such) if it does not already begin 775 with a quote string. FIXME: Need to remove any such automatically 776 inserted quote character when it no longer is necessary, such as 777 if we change the string we are completing on and the new set of 778 matches don't require a quoted substring. */ 779 replacement = match; 780 781 should_quote = match && rl_completer_quote_characters && 782 rl_filename_completion_desired && 783 rl_filename_quoting_desired; 784 785 if (should_quote) 786#if defined (SHELL) 787 should_quote = should_quote && (!quote_char || quote_char == '"' || quote_char == '\''); 788#else 789 should_quote = should_quote && !quote_char; 790#endif 791 792 if (should_quote) 793 { 794 /* If there is a single match, see if we need to quote it. 795 This also checks whether the common prefix of several 796 matches needs to be quoted. */ 797 should_quote = rl_strpbrk (match, rl_filename_quote_characters) != 0; 798 799 do_replace = should_quote ? mtype : NO_MATCH; 800 if (do_replace != NO_MATCH) 801 { 802 /* Quote the replacement, since we found an embedded 803 word break character in a potential match. */ 804 if (rl_filename_quoting_function) 805 { 806 qc = quote_char; /* must pass a (char *) to quoting function */ 807 replacement = (*rl_filename_quoting_function) 808 (match, do_replace, &qc); 809 quote_char = qc; 810 } 811 } 812 } 813 return (replacement); 814} 815 816static void 817insert_match (match, start, mtype, quote_char) 818 char *match; 819 int start, mtype, quote_char; 820{ 821 char *replacement; 822 823 replacement = make_quoted_replacement (match, mtype, quote_char); 824 825 /* Now insert the match. */ 826 if (replacement) 827 { 828 /* Don't double an opening quote character. */ 829 if (quote_char && start && rl_line_buffer[start - 1] == quote_char && 830 replacement[0] == quote_char) 831 start--; 832 insert_text (replacement, start, rl_point - 1); 833 if (replacement != match) 834 free (replacement); 835 } 836} 837 838/* Append any necessary closing quote and a separator character to the 839 just-inserted match. If the user has specified that directories 840 should be marked by a trailing `/', append one of those instead. The 841 default trailing character */ 842static void 843append_to_match (text, delimiter, quote_char) 844 char *text; 845 int delimiter, quote_char; 846{ 847 char temp_string[4], *filename; 848 int temp_string_index; 849 struct stat finfo; 850 851 temp_string_index = 0; 852 if (quote_char && rl_point && rl_line_buffer[rl_point - 1] != quote_char) 853 temp_string[temp_string_index++] = quote_char; 854 855 if (delimiter) 856 temp_string[temp_string_index++] = delimiter; 857 else if (rl_completion_append_character) 858 temp_string[temp_string_index++] = rl_completion_append_character; 859 860 temp_string[temp_string_index++] = '\0'; 861 862 if (rl_filename_completion_desired) 863 { 864 filename = tilde_expand (text); 865 if (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode)) 866 { 867 if (_rl_complete_mark_directories && rl_line_buffer[rl_point] != '/') 868 rl_insert_text ("/"); 869 } 870 else 871 { 872 if (rl_point == rl_end) 873 rl_insert_text (temp_string); 874 } 875 free (filename); 876 } 877 else 878 { 879 if (rl_point == rl_end) 880 rl_insert_text (temp_string); 881 } 882} 883 884static void 885insert_all_matches (matches, point, quote_char) 886 char **matches; 887 int point, quote_char; 888{ 889 int i; 890 char *rp; 891 892 rl_begin_undo_group (); 893 /* remove any opening quote character; make_quoted_replacement will add 894 it back. */ 895 if (quote_char && point && rl_line_buffer[point - 1] == quote_char) 896 point--; 897 rl_delete_text (point, rl_point); 898 rl_point = point; 899 900 if (matches[1]) 901 { 902 for (i = 1; matches[i]; i++) 903 { 904 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, quote_char); 905 rl_insert_text (rp); 906 rl_insert_text (" "); 907 if (rp != matches[i]) 908 free (rp); 909 } 910 } 911 else 912 { 913 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, quote_char); 914 rl_insert_text (rp); 915 rl_insert_text (" "); 916 if (rp != matches[0]) 917 free (rp); 918 } 919 rl_end_undo_group (); 920} 921 922/* Complete the word at or before point. 923 WHAT_TO_DO says what to do with the completion. 924 `?' means list the possible completions. 925 TAB means do standard completion. 926 `*' means insert all of the possible completions. 927 `!' means to do standard completion, and list all possible completions if 928 there is more than one. */ 929int 930rl_complete_internal (what_to_do) 931 int what_to_do; 932{ 933 char **matches, **temp_matches; 934 Function *our_func; 935 int start, end, delimiter, found_quote, nmatch, i; 936 char *text, *saved_line_buffer, *t; 937 char quote_char; 938 939 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL; 940 941 our_func = rl_completion_entry_function 942 ? rl_completion_entry_function 943 : (Function *)filename_completion_function; 944 945 /* Only the completion entry function can change these. */ 946 rl_filename_completion_desired = 0; 947 rl_filename_quoting_desired = 1; 948 949 rl_completion_type = what_to_do; 950 951 /* We now look backwards for the start of a filename/variable word. */ 952 end = rl_point; 953 954 found_quote = delimiter = 0; 955 quote_char = '\0'; 956 957 if (rl_point) 958 /* This (possibly) changes rl_point. If it returns a non-zero char, 959 we know we have an open quote. */ 960 quote_char = find_completion_word (&found_quote, &delimiter); 961 962 start = rl_point; 963 rl_point = end; 964 965 text = rl_copy_text (start, end); 966 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char); 967 free (text); 968 969 if (matches == 0) 970 { 971 ding (); 972 FREE (saved_line_buffer); 973 return 0; 974 } 975 976 /* It seems to me that in all the cases we handle we would like 977 to ignore duplicate possiblilities. Scan for the text to 978 insert being identical to the other completions. */ 979 if (rl_ignore_completion_duplicates) 980 { 981 temp_matches = remove_duplicate_matches (matches); 982 free (matches); 983 matches = temp_matches; 984 } 985 986 /* If we are matching filenames, then here is our chance to 987 do clever processing by re-examining the list. Call the 988 ignore function with the array as a parameter. It can 989 munge the array, deleting matches as it desires. */ 990 if (rl_ignore_some_completions_function && 991 our_func == (Function *)filename_completion_function) 992 { 993 for (nmatch = 1; matches[nmatch]; nmatch++) 994 ; 995 (void)(*rl_ignore_some_completions_function) (matches); 996 if (matches == 0 || matches[0] == 0) 997 { 998 FREE (matches); 999 ding (); 1000 FREE (saved_line_buffer); 1001 return 0; 1002 } 1003 else 1004 { 1005 /* If we removed some matches, recompute the common prefix. */ 1006 for (i = 1; matches[i]; i++) 1007 ; 1008 if (i > 1 && i < nmatch) 1009 { 1010 t = matches[0]; 1011 compute_lcd_of_matches (matches, i - 1, text); 1012 FREE (t); 1013 } 1014 } 1015 } 1016 1017 switch (what_to_do) 1018 { 1019 case TAB: 1020 case '!': 1021 /* Insert the first match with proper quoting. */ 1022 if (*matches[0]) 1023 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, quote_char); 1024 1025 /* If there are more matches, ring the bell to indicate. 1026 If we are in vi mode, Posix.2 says to not ring the bell. 1027 If the `show-all-if-ambiguous' variable is set, display 1028 all the matches immediately. Otherwise, if this was the 1029 only match, and we are hacking files, check the file to 1030 see if it was a directory. If so, and the `mark-directories' 1031 variable is set, add a '/' to the name. If not, and we 1032 are at the end of the line, then add a space. */ 1033 if (matches[1]) 1034 { 1035 if (what_to_do == '!') 1036 { 1037 display_matches (matches); 1038 break; 1039 } 1040 else if (rl_editing_mode != vi_mode) 1041 ding (); /* There are other matches remaining. */ 1042 } 1043 else 1044 append_to_match (matches[0], delimiter, quote_char); 1045 1046 break; 1047 1048 case '*': 1049 insert_all_matches (matches, start, quote_char); 1050 break; 1051 1052 case '?': 1053 display_matches (matches); 1054 break; 1055 1056 default: 1057 fprintf (stderr, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do); 1058 ding (); 1059 FREE (saved_line_buffer); 1060 return 1; 1061 } 1062 1063 for (i = 0; matches[i]; i++) 1064 free (matches[i]); 1065 free (matches); 1066 1067 /* Check to see if the line has changed through all of this manipulation. */ 1068 if (saved_line_buffer) 1069 { 1070 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0; 1071 free (saved_line_buffer); 1072 } 1073 1074 return 0; 1075} 1076 1077#if defined (VISIBLE_STATS) 1078/* Return the character which best describes FILENAME. 1079 `@' for symbolic links 1080 `/' for directories 1081 `*' for executables 1082 `=' for sockets 1083 `|' for FIFOs 1084 `%' for character special devices 1085 `#' for block special devices */ 1086static int 1087stat_char (filename) 1088 char *filename; 1089{ 1090 struct stat finfo; 1091 int character, r; 1092 1093#if defined (HAVE_LSTAT) && defined (S_ISLNK) 1094 r = lstat (filename, &finfo); 1095#else 1096 r = stat (filename, &finfo); 1097#endif 1098 1099 if (r == -1) 1100 return (0); 1101 1102 character = 0; 1103 if (S_ISDIR (finfo.st_mode)) 1104 character = '/'; 1105 else if (S_ISCHR (finfo.st_mode)) 1106 character = '%'; 1107 else if (S_ISBLK (finfo.st_mode)) 1108 character = '#'; 1109#if defined (S_ISLNK) 1110 else if (S_ISLNK (finfo.st_mode)) 1111 character = '@'; 1112#endif /* S_ISLNK */ 1113#if defined (S_ISSOCK) 1114 else if (S_ISSOCK (finfo.st_mode)) 1115 character = '='; 1116#endif /* S_ISSOCK */ 1117#if defined (S_ISFIFO) 1118 else if (S_ISFIFO (finfo.st_mode)) 1119 character = '|'; 1120#endif 1121 else if (S_ISREG (finfo.st_mode)) 1122 { 1123 if (access (filename, X_OK) == 0) 1124 character = '*'; 1125 } 1126 return (character); 1127} 1128#endif /* VISIBLE_STATS */ 1129 1130/* A completion function for usernames. 1131 TEXT contains a partial username preceded by a random 1132 character (usually `~'). */ 1133char * 1134username_completion_function (text, state) 1135 int state; 1136 char *text; 1137{ 1138#if defined (__GO32__) || defined (__WIN32__) 1139 return (char *)NULL; 1140#else /* !__GO32__ */ 1141 static char *username = (char *)NULL; 1142 static struct passwd *entry; 1143 static int namelen, first_char, first_char_loc; 1144 char *value; 1145 1146 if (state == 0) 1147 { 1148 FREE (username); 1149 1150 first_char = *text; 1151 first_char_loc = first_char == '~'; 1152 1153 username = savestring (&text[first_char_loc]); 1154 namelen = strlen (username); 1155 setpwent (); 1156 } 1157 1158 while (entry = getpwent ()) 1159 { 1160 /* Null usernames should result in all users as possible completions. */ 1161 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen))) 1162 break; 1163 } 1164 1165 if (entry == 0) 1166 { 1167 endpwent (); 1168 return ((char *)NULL); 1169 } 1170 else 1171 { 1172 value = xmalloc (2 + strlen (entry->pw_name)); 1173 1174 *value = *text; 1175 1176 strcpy (value + first_char_loc, entry->pw_name); 1177 1178 if (first_char == '~') 1179 rl_filename_completion_desired = 1; 1180 1181 return (value); 1182 } 1183#endif /* !__GO32__ */ 1184} 1185 1186/* **************************************************************** */ 1187/* */ 1188/* Completion */ 1189/* */ 1190/* **************************************************************** */ 1191 1192/* Non-zero means that case is not significant in completion. */ 1193int completion_case_fold = 0; 1194 1195/* Find the common prefix of the list of matches, and put it into 1196 matches[0]. */ 1197static int 1198compute_lcd_of_matches (match_list, matches, text) 1199 char **match_list; 1200 int matches; 1201 char *text; 1202{ 1203 register int i, c1, c2, si; 1204 int low; /* Count of max-matched characters. */ 1205 1206 /* If only one match, just use that. Otherwise, compare each 1207 member of the list with the next, finding out where they 1208 stop matching. */ 1209 if (matches == 1) 1210 { 1211 match_list[0] = match_list[1]; 1212 match_list[1] = (char *)NULL; 1213 return 1; 1214 } 1215 1216 for (i = 1, low = 100000; i < matches; i++) 1217 { 1218 if (completion_case_fold) 1219 { 1220 for (si = 0; 1221 (c1 = _rl_to_lower(match_list[i][si])) && 1222 (c2 = _rl_to_lower(match_list[i + 1][si])); 1223 si++) 1224 if (c1 != c2) 1225 break; 1226 } 1227 else 1228 { 1229 for (si = 0; 1230 (c1 = match_list[i][si]) && 1231 (c2 = match_list[i + 1][si]); 1232 si++) 1233 if (c1 != c2) 1234 break; 1235 } 1236 1237 if (low > si) 1238 low = si; 1239 } 1240 1241 /* If there were multiple matches, but none matched up to even the 1242 first character, and the user typed something, use that as the 1243 value of matches[0]. */ 1244 if (low == 0 && text && *text) 1245 { 1246 match_list[0] = xmalloc (strlen (text) + 1); 1247 strcpy (match_list[0], text); 1248 } 1249 else 1250 { 1251 match_list[0] = xmalloc (low + 1); 1252 strncpy (match_list[0], match_list[1], low); 1253 match_list[0][low] = '\0'; 1254 } 1255 1256 return matches; 1257} 1258 1259/* Return an array of (char *) which is a list of completions for TEXT. 1260 If there are no completions, return a NULL pointer. 1261 The first entry in the returned array is the substitution for TEXT. 1262 The remaining entries are the possible completions. 1263 The array is terminated with a NULL pointer. 1264 1265 ENTRY_FUNCTION is a function of two args, and returns a (char *). 1266 The first argument is TEXT. 1267 The second is a state argument; it should be zero on the first call, and 1268 non-zero on subsequent calls. It returns a NULL pointer to the caller 1269 when there are no more matches. 1270 */ 1271char ** 1272completion_matches (text, entry_function) 1273 char *text; 1274 CPFunction *entry_function; 1275{ 1276 /* Number of slots in match_list. */ 1277 int match_list_size; 1278 1279 /* The list of matches. */ 1280 char **match_list; 1281 1282 /* Number of matches actually found. */ 1283 int matches; 1284 1285 /* Temporary string binder. */ 1286 char *string; 1287 1288 matches = 0; 1289 match_list_size = 10; 1290 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *)); 1291 match_list[1] = (char *)NULL; 1292 1293 while (string = (*entry_function) (text, matches)) 1294 { 1295 if (matches + 1 == match_list_size) 1296 match_list = (char **)xrealloc 1297 (match_list, ((match_list_size += 10) + 1) * sizeof (char *)); 1298 1299 match_list[++matches] = string; 1300 match_list[matches + 1] = (char *)NULL; 1301 } 1302 1303 /* If there were any matches, then look through them finding out the 1304 lowest common denominator. That then becomes match_list[0]. */ 1305 if (matches) 1306 compute_lcd_of_matches (match_list, matches, text); 1307 else /* There were no matches. */ 1308 { 1309 free (match_list); 1310 match_list = (char **)NULL; 1311 } 1312 return (match_list); 1313} 1314 1315/* Okay, now we write the entry_function for filename completion. In the 1316 general case. Note that completion in the shell is a little different 1317 because of all the pathnames that must be followed when looking up the 1318 completion for a command. */ 1319char * 1320filename_completion_function (text, state) 1321 int state; 1322 char *text; 1323{ 1324 static DIR *directory; 1325 static char *filename = (char *)NULL; 1326 static char *dirname = (char *)NULL; 1327 static char *users_dirname = (char *)NULL; 1328 static int filename_len; 1329 char *temp; 1330 int dirlen; 1331 struct dirent *entry; 1332 1333 /* If we don't have any state, then do some initialization. */ 1334 if (state == 0) 1335 { 1336 FREE (dirname); 1337 FREE (filename); 1338 FREE (users_dirname); 1339 1340 filename = savestring (text); 1341 if (*text == 0) 1342 text = "."; 1343 dirname = savestring (text); 1344 1345 temp = strrchr (dirname, '/'); 1346 1347 if (temp) 1348 { 1349 strcpy (filename, ++temp); 1350 *temp = '\0'; 1351 } 1352 else 1353 { 1354 dirname[0] = '.'; 1355 dirname[1] = '\0'; 1356 } 1357 1358 /* We aren't done yet. We also support the "~user" syntax. */ 1359 1360 /* Save the version of the directory that the user typed. */ 1361 users_dirname = savestring (dirname); 1362 1363 if (*dirname == '~') 1364 { 1365 temp = tilde_expand (dirname); 1366 free (dirname); 1367 dirname = temp; 1368 } 1369 1370 if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname)) 1371 { 1372 free (users_dirname); 1373 users_dirname = savestring (dirname); 1374 } 1375 1376 directory = opendir (dirname); 1377 filename_len = strlen (filename); 1378 1379 rl_filename_completion_desired = 1; 1380 } 1381 1382 /* At this point we should entertain the possibility of hacking wildcarded 1383 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name 1384 contains globbing characters, then build an array of directories, and 1385 then map over that list while completing. */ 1386 /* *** UNIMPLEMENTED *** */ 1387 1388 /* Now that we have some state, we can read the directory. */ 1389 1390 entry = (struct dirent *)NULL; 1391 while (directory && (entry = readdir (directory))) 1392 { 1393 /* Special case for no filename. 1394 All entries except "." and ".." match. */ 1395 if (filename_len == 0) 1396 { 1397 if (entry->d_name[0] != '.' || 1398 (entry->d_name[1] && 1399 (entry->d_name[1] != '.' || entry->d_name[2]))) 1400 break; 1401 } 1402 else 1403 { 1404 /* Otherwise, if these match up to the length of filename, then 1405 it is a match. */ 1406 if ((entry->d_name[0] == filename[0]) && 1407 (((int)D_NAMLEN (entry)) >= filename_len) && 1408 (strncmp (filename, entry->d_name, filename_len) == 0)) 1409 break; 1410 } 1411 } 1412 1413 if (entry == 0) 1414 { 1415 if (directory) 1416 { 1417 closedir (directory); 1418 directory = (DIR *)NULL; 1419 } 1420 if (dirname) 1421 { 1422 free (dirname); 1423 dirname = (char *)NULL; 1424 } 1425 if (filename) 1426 { 1427 free (filename); 1428 filename = (char *)NULL; 1429 } 1430 if (users_dirname) 1431 { 1432 free (users_dirname); 1433 users_dirname = (char *)NULL; 1434 } 1435 1436 return (char *)NULL; 1437 } 1438 else 1439 { 1440 /* dirname && (strcmp (dirname, ".") != 0) */ 1441 if (dirname && (dirname[0] != '.' || dirname[1])) 1442 { 1443 if (rl_complete_with_tilde_expansion && *users_dirname == '~') 1444 { 1445 dirlen = strlen (dirname); 1446 temp = xmalloc (2 + dirlen + D_NAMLEN (entry)); 1447 strcpy (temp, dirname); 1448 /* Canonicalization cuts off any final slash present. We 1449 may need to add it back. */ 1450 if (dirname[dirlen - 1] != '/') 1451 { 1452 temp[dirlen++] = '/'; 1453 temp[dirlen] = '\0'; 1454 } 1455 } 1456 else 1457 { 1458 dirlen = strlen (users_dirname); 1459 temp = xmalloc (1 + dirlen + D_NAMLEN (entry)); 1460 strcpy (temp, users_dirname); 1461 } 1462 1463 strcpy (temp + dirlen, entry->d_name); /* strcat (temp, entry->d_name); */ 1464 } 1465 else 1466 temp = savestring (entry->d_name); 1467 1468 return (temp); 1469 } 1470} 1471 1472/* A function for simple tilde expansion. */ 1473int 1474rl_tilde_expand (ignore, key) 1475 int ignore, key; 1476{ 1477 register int start, end; 1478 char *homedir, *temp; 1479 int len; 1480 1481 end = rl_point; 1482 start = end - 1; 1483 1484 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~') 1485 { 1486 homedir = tilde_expand ("~"); 1487 insert_text (homedir, start, end); 1488 return (0); 1489 } 1490 else if (rl_line_buffer[start] != '~') 1491 { 1492 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--) 1493 ; 1494 start++; 1495 } 1496 1497 end = start; 1498 do 1499 end++; 1500 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end); 1501 1502 if (whitespace (rl_line_buffer[end]) || end >= rl_end) 1503 end--; 1504 1505 /* If the first character of the current word is a tilde, perform 1506 tilde expansion and insert the result. If not a tilde, do 1507 nothing. */ 1508 if (rl_line_buffer[start] == '~') 1509 { 1510 len = end - start + 1; 1511 temp = xmalloc (len + 1); 1512 strncpy (temp, rl_line_buffer + start, len); 1513 temp[len] = '\0'; 1514 homedir = tilde_expand (temp); 1515 free (temp); 1516 1517 insert_text (homedir, start, end); 1518 } 1519 1520 return (0); 1521} 1522 1523/* Find the first occurrence in STRING1 of any character from STRING2. 1524 Return a pointer to the character in STRING1. */ 1525static char * 1526rl_strpbrk (string1, string2) 1527 char *string1, *string2; 1528{ 1529 register char *scan; 1530 1531 for (; *string1; string1++) 1532 { 1533 for (scan = string2; *scan; scan++) 1534 { 1535 if (*string1 == *scan) 1536 { 1537 return (string1); 1538 } 1539 } 1540 } 1541 return ((char *)NULL); 1542} 1543