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