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