1 /* Line completion stuff for GDB, the GNU debugger. 2 Copyright 2000, 2001 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place - Suite 330, 19 Boston, MA 02111-1307, USA. */ 20 21 #include "defs.h" 22 #include "symtab.h" 23 #include "gdbtypes.h" 24 #include "expression.h" 25 #include "filenames.h" /* for DOSish file names */ 26 #include "language.h" 27 28 #include "cli/cli-decode.h" 29 30 /* FIXME: This is needed because of lookup_cmd_1(). 31 We should be calling a hook instead so we eliminate the CLI dependency. */ 32 #include "gdbcmd.h" 33 34 /* Needed for rl_completer_word_break_characters() and for 35 rl_filename_completion_function. */ 36 #include "readline/readline.h" 37 38 /* readline defines this. */ 39 #undef savestring 40 41 #include "completer.h" 42 43 /* Prototypes for local functions */ 44 static 45 char *line_completion_function (const char *text, int matches, char *line_buffer, 46 int point); 47 48 /* readline uses the word breaks for two things: 49 (1) In figuring out where to point the TEXT parameter to the 50 rl_completion_entry_function. Since we don't use TEXT for much, 51 it doesn't matter a lot what the word breaks are for this purpose, but 52 it does affect how much stuff M-? lists. 53 (2) If one of the matches contains a word break character, readline 54 will quote it. That's why we switch between 55 current_language->la_word_break_characters() and 56 gdb_completer_command_word_break_characters. I'm not sure when 57 we need this behavior (perhaps for funky characters in C++ symbols?). */ 58 59 /* Variables which are necessary for fancy command line editing. */ 60 61 /* When completing on command names, we remove '-' from the list of 62 word break characters, since we use it in command names. If the 63 readline library sees one in any of the current completion strings, 64 it thinks that the string needs to be quoted and automatically supplies 65 a leading quote. */ 66 static char *gdb_completer_command_word_break_characters = 67 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; 68 69 /* When completing on file names, we remove from the list of word 70 break characters any characters that are commonly used in file 71 names, such as '-', '+', '~', etc. Otherwise, readline displays 72 incorrect completion candidates. */ 73 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 74 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most 75 programs support @foo style response files. */ 76 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@"; 77 #else 78 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><"; 79 #endif 80 81 /* These are used when completing on locations, which can mix file 82 names and symbol names separated by a colon. */ 83 static char *gdb_completer_loc_break_characters = " \t\n*|\"';:?><,"; 84 85 /* Characters that can be used to quote completion strings. Note that we 86 can't include '"' because the gdb C parser treats such quoted sequences 87 as strings. */ 88 static char *gdb_completer_quote_characters = "'"; 89 90 /* Accessor for some completer data that may interest other files. */ 91 92 char * 93 get_gdb_completer_quote_characters (void) 94 { 95 return gdb_completer_quote_characters; 96 } 97 98 /* Line completion interface function for readline. */ 99 100 char * 101 readline_line_completion_function (const char *text, int matches) 102 { 103 return line_completion_function (text, matches, rl_line_buffer, rl_point); 104 } 105 106 /* This can be used for functions which don't want to complete on symbols 107 but don't want to complete on anything else either. */ 108 char ** 109 noop_completer (char *text, char *prefix) 110 { 111 return NULL; 112 } 113 114 /* Complete on filenames. */ 115 char ** 116 filename_completer (char *text, char *word) 117 { 118 int subsequent_name; 119 char **return_val; 120 int return_val_used; 121 int return_val_alloced; 122 123 return_val_used = 0; 124 /* Small for testing. */ 125 return_val_alloced = 1; 126 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *)); 127 128 subsequent_name = 0; 129 while (1) 130 { 131 char *p; 132 p = rl_filename_completion_function (text, subsequent_name); 133 if (return_val_used >= return_val_alloced) 134 { 135 return_val_alloced *= 2; 136 return_val = 137 (char **) xrealloc (return_val, 138 return_val_alloced * sizeof (char *)); 139 } 140 if (p == NULL) 141 { 142 return_val[return_val_used++] = p; 143 break; 144 } 145 /* We need to set subsequent_name to a non-zero value before the 146 continue line below, because otherwise, if the first file seen 147 by GDB is a backup file whose name ends in a `~', we will loop 148 indefinitely. */ 149 subsequent_name = 1; 150 /* Like emacs, don't complete on old versions. Especially useful 151 in the "source" command. */ 152 if (p[strlen (p) - 1] == '~') 153 continue; 154 155 { 156 char *q; 157 if (word == text) 158 /* Return exactly p. */ 159 return_val[return_val_used++] = p; 160 else if (word > text) 161 { 162 /* Return some portion of p. */ 163 q = xmalloc (strlen (p) + 5); 164 strcpy (q, p + (word - text)); 165 return_val[return_val_used++] = q; 166 xfree (p); 167 } 168 else 169 { 170 /* Return some of TEXT plus p. */ 171 q = xmalloc (strlen (p) + (text - word) + 5); 172 strncpy (q, word, text - word); 173 q[text - word] = '\0'; 174 strcat (q, p); 175 return_val[return_val_used++] = q; 176 xfree (p); 177 } 178 } 179 } 180 #if 0 181 /* There is no way to do this just long enough to affect quote inserting 182 without also affecting the next completion. This should be fixed in 183 readline. FIXME. */ 184 /* Insure that readline does the right thing 185 with respect to inserting quotes. */ 186 rl_completer_word_break_characters = ""; 187 #endif 188 return return_val; 189 } 190 191 /* Complete on locations, which might be of two possible forms: 192 193 file:line 194 or 195 symbol+offset 196 197 This is intended to be used in commands that set breakpoints etc. */ 198 char ** 199 location_completer (char *text, char *word) 200 { 201 int n_syms = 0, n_files = 0; 202 char ** fn_list = NULL; 203 char ** list = NULL; 204 char *p; 205 int quote_found = 0; 206 int quoted = *text == '\'' || *text == '"'; 207 int quote_char = '\0'; 208 char *colon = NULL; 209 char *file_to_match = NULL; 210 char *symbol_start = text; 211 char *orig_text = text; 212 size_t text_len; 213 214 /* Do we have an unquoted colon, as in "break foo.c::bar"? */ 215 for (p = text; *p != '\0'; ++p) 216 { 217 if (*p == '\\' && p[1] == '\'') 218 p++; 219 else if (*p == '\'' || *p == '"') 220 { 221 quote_found = *p; 222 quote_char = *p++; 223 while (*p != '\0' && *p != quote_found) 224 { 225 if (*p == '\\' && p[1] == quote_found) 226 p++; 227 p++; 228 } 229 230 if (*p == quote_found) 231 quote_found = 0; 232 else 233 break; /* hit the end of text */ 234 } 235 #if HAVE_DOS_BASED_FILE_SYSTEM 236 /* If we have a DOS-style absolute file name at the beginning of 237 TEXT, and the colon after the drive letter is the only colon 238 we found, pretend the colon is not there. */ 239 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted) 240 ; 241 #endif 242 else if (*p == ':' && !colon) 243 { 244 colon = p; 245 symbol_start = p + 1; 246 } 247 else if (strchr (current_language->la_word_break_characters(), *p)) 248 symbol_start = p + 1; 249 } 250 251 if (quoted) 252 text++; 253 text_len = strlen (text); 254 255 /* Where is the file name? */ 256 if (colon) 257 { 258 char *s; 259 260 file_to_match = (char *) xmalloc (colon - text + 1); 261 strncpy (file_to_match, text, colon - text + 1); 262 /* Remove trailing colons and quotes from the file name. */ 263 for (s = file_to_match + (colon - text); 264 s > file_to_match; 265 s--) 266 if (*s == ':' || *s == quote_char) 267 *s = '\0'; 268 } 269 /* If the text includes a colon, they want completion only on a 270 symbol name after the colon. Otherwise, we need to complete on 271 symbols as well as on files. */ 272 if (colon) 273 { 274 list = make_file_symbol_completion_list (symbol_start, word, 275 file_to_match); 276 xfree (file_to_match); 277 } 278 else 279 { 280 list = make_symbol_completion_list (symbol_start, word); 281 /* If text includes characters which cannot appear in a file 282 name, they cannot be asking for completion on files. */ 283 if (strcspn (text, gdb_completer_file_name_break_characters) == text_len) 284 fn_list = make_source_files_completion_list (text, text); 285 } 286 287 /* How many completions do we have in both lists? */ 288 if (fn_list) 289 for ( ; fn_list[n_files]; n_files++) 290 ; 291 if (list) 292 for ( ; list[n_syms]; n_syms++) 293 ; 294 295 /* Make list[] large enough to hold both lists, then catenate 296 fn_list[] onto the end of list[]. */ 297 if (n_syms && n_files) 298 { 299 list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *)); 300 memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *)); 301 xfree (fn_list); 302 } 303 else if (n_files) 304 { 305 /* If we only have file names as possible completion, we should 306 bring them in sync with what rl_complete expects. The 307 problem is that if the user types "break /foo/b TAB", and the 308 possible completions are "/foo/bar" and "/foo/baz" 309 rl_complete expects us to return "bar" and "baz", without the 310 leading directories, as possible completions, because `word' 311 starts at the "b". But we ignore the value of `word' when we 312 call make_source_files_completion_list above (because that 313 would not DTRT when the completion results in both symbols 314 and file names), so make_source_files_completion_list returns 315 the full "/foo/bar" and "/foo/baz" strings. This produces 316 wrong results when, e.g., there's only one possible 317 completion, because rl_complete will prepend "/foo/" to each 318 candidate completion. The loop below removes that leading 319 part. */ 320 for (n_files = 0; fn_list[n_files]; n_files++) 321 { 322 memmove (fn_list[n_files], fn_list[n_files] + (word - text), 323 strlen (fn_list[n_files]) + 1 - (word - text)); 324 } 325 /* Return just the file-name list as the result. */ 326 list = fn_list; 327 } 328 else if (!n_syms) 329 { 330 /* No completions at all. As the final resort, try completing 331 on the entire text as a symbol. */ 332 list = make_symbol_completion_list (orig_text, word); 333 } 334 335 return list; 336 } 337 338 /* Complete on command names. Used by "help". */ 339 char ** 340 command_completer (char *text, char *word) 341 { 342 return complete_on_cmdlist (cmdlist, text, word); 343 } 344 345 346 /* Here are some useful test cases for completion. FIXME: These should 347 be put in the test suite. They should be tested with both M-? and TAB. 348 349 "show output-" "radix" 350 "show output" "-radix" 351 "p" ambiguous (commands starting with p--path, print, printf, etc.) 352 "p " ambiguous (all symbols) 353 "info t foo" no completions 354 "info t " no completions 355 "info t" ambiguous ("info target", "info terminal", etc.) 356 "info ajksdlfk" no completions 357 "info ajksdlfk " no completions 358 "info" " " 359 "info " ambiguous (all info commands) 360 "p \"a" no completions (string constant) 361 "p 'a" ambiguous (all symbols starting with a) 362 "p b-a" ambiguous (all symbols starting with a) 363 "p b-" ambiguous (all symbols) 364 "file Make" "file" (word break hard to screw up here) 365 "file ../gdb.stabs/we" "ird" (needs to not break word at slash) 366 */ 367 368 /* Generate completions all at once. Returns a NULL-terminated array 369 of strings. Both the array and each element are allocated with 370 xmalloc. It can also return NULL if there are no completions. 371 372 TEXT is the caller's idea of the "word" we are looking at. 373 374 LINE_BUFFER is available to be looked at; it contains the entire text 375 of the line. POINT is the offset in that line of the cursor. You 376 should pretend that the line ends at POINT. */ 377 378 char ** 379 complete_line (const char *text, char *line_buffer, int point) 380 { 381 char **list = NULL; 382 char *tmp_command, *p; 383 /* Pointer within tmp_command which corresponds to text. */ 384 char *word; 385 struct cmd_list_element *c, *result_list; 386 387 /* Choose the default set of word break characters to break completions. 388 If we later find out that we are doing completions on command strings 389 (as opposed to strings supplied by the individual command completer 390 functions, which can be any string) then we will switch to the 391 special word break set for command strings, which leaves out the 392 '-' character used in some commands. */ 393 394 rl_completer_word_break_characters = 395 current_language->la_word_break_characters(); 396 397 /* Decide whether to complete on a list of gdb commands or on symbols. */ 398 tmp_command = (char *) alloca (point + 1); 399 p = tmp_command; 400 401 strncpy (tmp_command, line_buffer, point); 402 tmp_command[point] = '\0'; 403 /* Since text always contains some number of characters leading up 404 to point, we can find the equivalent position in tmp_command 405 by subtracting that many characters from the end of tmp_command. */ 406 word = tmp_command + point - strlen (text); 407 408 if (point == 0) 409 { 410 /* An empty line we want to consider ambiguous; that is, it 411 could be any command. */ 412 c = (struct cmd_list_element *) -1; 413 result_list = 0; 414 } 415 else 416 { 417 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1); 418 } 419 420 /* Move p up to the next interesting thing. */ 421 while (*p == ' ' || *p == '\t') 422 { 423 p++; 424 } 425 426 if (!c) 427 { 428 /* It is an unrecognized command. So there are no 429 possible completions. */ 430 list = NULL; 431 } 432 else if (c == (struct cmd_list_element *) -1) 433 { 434 char *q; 435 436 /* lookup_cmd_1 advances p up to the first ambiguous thing, but 437 doesn't advance over that thing itself. Do so now. */ 438 q = p; 439 while (*q && (isalnum (*q) || *q == '-' || *q == '_')) 440 ++q; 441 if (q != tmp_command + point) 442 { 443 /* There is something beyond the ambiguous 444 command, so there are no possible completions. For 445 example, "info t " or "info t foo" does not complete 446 to anything, because "info t" can be "info target" or 447 "info terminal". */ 448 list = NULL; 449 } 450 else 451 { 452 /* We're trying to complete on the command which was ambiguous. 453 This we can deal with. */ 454 if (result_list) 455 { 456 list = complete_on_cmdlist (*result_list->prefixlist, p, 457 word); 458 } 459 else 460 { 461 list = complete_on_cmdlist (cmdlist, p, word); 462 } 463 /* Insure that readline does the right thing with respect to 464 inserting quotes. */ 465 rl_completer_word_break_characters = 466 gdb_completer_command_word_break_characters; 467 } 468 } 469 else 470 { 471 /* We've recognized a full command. */ 472 473 if (p == tmp_command + point) 474 { 475 /* There is no non-whitespace in the line beyond the command. */ 476 477 if (p[-1] == ' ' || p[-1] == '\t') 478 { 479 /* The command is followed by whitespace; we need to complete 480 on whatever comes after command. */ 481 if (c->prefixlist) 482 { 483 /* It is a prefix command; what comes after it is 484 a subcommand (e.g. "info "). */ 485 list = complete_on_cmdlist (*c->prefixlist, p, word); 486 487 /* Insure that readline does the right thing 488 with respect to inserting quotes. */ 489 rl_completer_word_break_characters = 490 gdb_completer_command_word_break_characters; 491 } 492 else if (c->enums) 493 { 494 list = complete_on_enum (c->enums, p, word); 495 rl_completer_word_break_characters = 496 gdb_completer_command_word_break_characters; 497 } 498 else 499 { 500 /* It is a normal command; what comes after it is 501 completed by the command's completer function. */ 502 if (c->completer == filename_completer) 503 { 504 /* Many commands which want to complete on 505 file names accept several file names, as 506 in "run foo bar >>baz". So we don't want 507 to complete the entire text after the 508 command, just the last word. To this 509 end, we need to find the beginning of the 510 file name by starting at `word' and going 511 backwards. */ 512 for (p = word; 513 p > tmp_command 514 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL; 515 p--) 516 ; 517 rl_completer_word_break_characters = 518 gdb_completer_file_name_break_characters; 519 } 520 else if (c->completer == location_completer) 521 { 522 /* Commands which complete on locations want to 523 see the entire argument. */ 524 for (p = word; 525 p > tmp_command 526 && p[-1] != ' ' && p[-1] != '\t'; 527 p--) 528 ; 529 } 530 list = (*c->completer) (p, word); 531 } 532 } 533 else 534 { 535 /* The command is not followed by whitespace; we need to 536 complete on the command itself. e.g. "p" which is a 537 command itself but also can complete to "print", "ptype" 538 etc. */ 539 char *q; 540 541 /* Find the command we are completing on. */ 542 q = p; 543 while (q > tmp_command) 544 { 545 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') 546 --q; 547 else 548 break; 549 } 550 551 list = complete_on_cmdlist (result_list, q, word); 552 553 /* Insure that readline does the right thing 554 with respect to inserting quotes. */ 555 rl_completer_word_break_characters = 556 gdb_completer_command_word_break_characters; 557 } 558 } 559 else 560 { 561 /* There is non-whitespace beyond the command. */ 562 563 if (c->prefixlist && !c->allow_unknown) 564 { 565 /* It is an unrecognized subcommand of a prefix command, 566 e.g. "info adsfkdj". */ 567 list = NULL; 568 } 569 else if (c->enums) 570 { 571 list = complete_on_enum (c->enums, p, word); 572 } 573 else 574 { 575 /* It is a normal command. */ 576 if (c->completer == filename_completer) 577 { 578 /* See the commentary above about the specifics 579 of file-name completion. */ 580 for (p = word; 581 p > tmp_command 582 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL; 583 p--) 584 ; 585 rl_completer_word_break_characters = 586 gdb_completer_file_name_break_characters; 587 } 588 else if (c->completer == location_completer) 589 { 590 for (p = word; 591 p > tmp_command 592 && p[-1] != ' ' && p[-1] != '\t'; 593 p--) 594 ; 595 } 596 list = (*c->completer) (p, word); 597 } 598 } 599 } 600 601 return list; 602 } 603 604 /* Generate completions one by one for the completer. Each time we are 605 called return another potential completion to the caller. 606 line_completion just completes on commands or passes the buck to the 607 command's completer function, the stuff specific to symbol completion 608 is in make_symbol_completion_list. 609 610 TEXT is the caller's idea of the "word" we are looking at. 611 612 MATCHES is the number of matches that have currently been collected from 613 calling this completion function. When zero, then we need to initialize, 614 otherwise the initialization has already taken place and we can just 615 return the next potential completion string. 616 617 LINE_BUFFER is available to be looked at; it contains the entire text 618 of the line. POINT is the offset in that line of the cursor. You 619 should pretend that the line ends at POINT. 620 621 Returns NULL if there are no more completions, else a pointer to a string 622 which is a possible completion, it is the caller's responsibility to 623 free the string. */ 624 625 static char * 626 line_completion_function (const char *text, int matches, char *line_buffer, int point) 627 { 628 static char **list = (char **) NULL; /* Cache of completions */ 629 static int index; /* Next cached completion */ 630 char *output = NULL; 631 632 if (matches == 0) 633 { 634 /* The caller is beginning to accumulate a new set of completions, so 635 we need to find all of them now, and cache them for returning one at 636 a time on future calls. */ 637 638 if (list) 639 { 640 /* Free the storage used by LIST, but not by the strings inside. 641 This is because rl_complete_internal () frees the strings. */ 642 xfree (list); 643 } 644 index = 0; 645 list = complete_line (text, line_buffer, point); 646 } 647 648 /* If we found a list of potential completions during initialization then 649 dole them out one at a time. The vector of completions is NULL 650 terminated, so after returning the last one, return NULL (and continue 651 to do so) each time we are called after that, until a new list is 652 available. */ 653 654 if (list) 655 { 656 output = list[index]; 657 if (output) 658 { 659 index++; 660 } 661 } 662 663 #if 0 664 /* Can't do this because readline hasn't yet checked the word breaks 665 for figuring out whether to insert a quote. */ 666 if (output == NULL) 667 /* Make sure the word break characters are set back to normal for the 668 next time that readline tries to complete something. */ 669 rl_completer_word_break_characters = 670 current_language->la_word_break_characters(); 671 #endif 672 673 return (output); 674 } 675 676 /* Skip over the possibly quoted word STR (as defined by the quote 677 characters QUOTECHARS and the the word break characters 678 BREAKCHARS). Returns pointer to the location after the "word". If 679 either QUOTECHARS or BREAKCHARS is NULL, use the same values used 680 by the completer. */ 681 682 char * 683 skip_quoted_chars (char *str, char *quotechars, char *breakchars) 684 { 685 char quote_char = '\0'; 686 char *scan; 687 688 if (quotechars == NULL) 689 quotechars = gdb_completer_quote_characters; 690 691 if (breakchars == NULL) 692 breakchars = current_language->la_word_break_characters(); 693 694 for (scan = str; *scan != '\0'; scan++) 695 { 696 if (quote_char != '\0') 697 { 698 /* Ignore everything until the matching close quote char */ 699 if (*scan == quote_char) 700 { 701 /* Found matching close quote. */ 702 scan++; 703 break; 704 } 705 } 706 else if (strchr (quotechars, *scan)) 707 { 708 /* Found start of a quoted string. */ 709 quote_char = *scan; 710 } 711 else if (strchr (breakchars, *scan)) 712 { 713 break; 714 } 715 } 716 717 return (scan); 718 } 719 720 /* Skip over the possibly quoted word STR (as defined by the quote 721 characters and word break characters used by the completer). 722 Returns pointer to the location after the "word". */ 723 724 char * 725 skip_quoted (char *str) 726 { 727 return skip_quoted_chars (str, NULL, NULL); 728 } 729