1/* Line completion stuff for GDB, the GNU debugger. 2 Copyright (C) 2000, 2001, 2007, 2008, 2009, 2010, 2011 3 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20#include "defs.h" 21#include "symtab.h" 22#include "gdbtypes.h" 23#include "expression.h" 24#include "filenames.h" /* For DOSish file names. */ 25#include "language.h" 26#include "gdb_assert.h" 27#include "exceptions.h" 28 29#include "cli/cli-decode.h" 30 31/* FIXME: This is needed because of lookup_cmd_1 (). We should be 32 calling a hook instead so we eliminate the CLI dependency. */ 33#include "gdbcmd.h" 34 35/* Needed for rl_completer_word_break_characters() and for 36 rl_filename_completion_function. */ 37#include "readline/readline.h" 38 39/* readline defines this. */ 40#undef savestring 41 42#include "completer.h" 43 44/* Prototypes for local functions. */ 45static 46char *line_completion_function (const char *text, int matches, 47 char *line_buffer, 48 int point); 49 50/* readline uses the word breaks for two things: 51 (1) In figuring out where to point the TEXT parameter to the 52 rl_completion_entry_function. Since we don't use TEXT for much, 53 it doesn't matter a lot what the word breaks are for this purpose, 54 but it does affect how much stuff M-? lists. 55 (2) If one of the matches contains a word break character, readline 56 will quote it. That's why we switch between 57 current_language->la_word_break_characters() and 58 gdb_completer_command_word_break_characters. I'm not sure when 59 we need this behavior (perhaps for funky characters in C++ 60 symbols?). */ 61 62/* Variables which are necessary for fancy command line editing. */ 63 64/* When completing on command names, we remove '-' from the list of 65 word break characters, since we use it in command names. If the 66 readline library sees one in any of the current completion strings, 67 it thinks that the string needs to be quoted and automatically 68 supplies a leading quote. */ 69static char *gdb_completer_command_word_break_characters = 70" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; 71 72/* When completing on file names, we remove from the list of word 73 break characters any characters that are commonly used in file 74 names, such as '-', '+', '~', etc. Otherwise, readline displays 75 incorrect completion candidates. */ 76#ifdef HAVE_DOS_BASED_FILE_SYSTEM 77/* MS-DOS and MS-Windows use colon as part of the drive spec, and most 78 programs support @foo style response files. */ 79static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@"; 80#else 81static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><"; 82#endif 83 84/* Characters that can be used to quote completion strings. Note that 85 we can't include '"' because the gdb C parser treats such quoted 86 sequences as strings. */ 87static char *gdb_completer_quote_characters = "'"; 88 89/* Accessor for some completer data that may interest other files. */ 90 91char * 92get_gdb_completer_quote_characters (void) 93{ 94 return gdb_completer_quote_characters; 95} 96 97/* Line completion interface function for readline. */ 98 99char * 100readline_line_completion_function (const char *text, int matches) 101{ 102 return line_completion_function (text, matches, 103 rl_line_buffer, rl_point); 104} 105 106/* This can be used for functions which don't want to complete on 107 symbols but don't want to complete on anything else either. */ 108char ** 109noop_completer (struct cmd_list_element *ignore, 110 char *text, char *prefix) 111{ 112 return NULL; 113} 114 115/* Complete on filenames. */ 116char ** 117filename_completer (struct cmd_list_element *ignore, 118 char *text, char *word) 119{ 120 int subsequent_name; 121 char **return_val; 122 int return_val_used; 123 int return_val_alloced; 124 125 return_val_used = 0; 126 /* Small for testing. */ 127 return_val_alloced = 1; 128 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *)); 129 130 subsequent_name = 0; 131 while (1) 132 { 133 char *p, *q; 134 135 p = rl_filename_completion_function (text, subsequent_name); 136 if (return_val_used >= return_val_alloced) 137 { 138 return_val_alloced *= 2; 139 return_val = 140 (char **) xrealloc (return_val, 141 return_val_alloced * sizeof (char *)); 142 } 143 if (p == NULL) 144 { 145 return_val[return_val_used++] = p; 146 break; 147 } 148 /* We need to set subsequent_name to a non-zero value before the 149 continue line below, because otherwise, if the first file 150 seen by GDB is a backup file whose name ends in a `~', we 151 will loop indefinitely. */ 152 subsequent_name = 1; 153 /* Like emacs, don't complete on old versions. Especially 154 useful in the "source" command. */ 155 if (p[strlen (p) - 1] == '~') 156 { 157 xfree (p); 158 continue; 159 } 160 161 if (word == text) 162 /* Return exactly p. */ 163 return_val[return_val_used++] = p; 164 else if (word > text) 165 { 166 /* Return some portion of p. */ 167 q = xmalloc (strlen (p) + 5); 168 strcpy (q, p + (word - text)); 169 return_val[return_val_used++] = q; 170 xfree (p); 171 } 172 else 173 { 174 /* Return some of TEXT plus p. */ 175 q = xmalloc (strlen (p) + (text - word) + 5); 176 strncpy (q, word, text - word); 177 q[text - word] = '\0'; 178 strcat (q, p); 179 return_val[return_val_used++] = q; 180 xfree (p); 181 } 182 } 183#if 0 184 /* There is no way to do this just long enough to affect quote 185 inserting without also affecting the next completion. This 186 should be fixed in readline. FIXME. */ 187 /* Ensure that readline does the right thing 188 with respect to inserting quotes. */ 189 rl_completer_word_break_characters = ""; 190#endif 191 return return_val; 192} 193 194/* Complete on locations, which might be of two possible forms: 195 196 file:line 197 or 198 symbol+offset 199 200 This is intended to be used in commands that set breakpoints 201 etc. */ 202 203char ** 204location_completer (struct cmd_list_element *ignore, 205 char *text, char *word) 206{ 207 int n_syms = 0, n_files = 0; 208 char ** fn_list = NULL; 209 char ** list = NULL; 210 char *p; 211 int quote_found = 0; 212 int quoted = *text == '\'' || *text == '"'; 213 int quote_char = '\0'; 214 char *colon = NULL; 215 char *file_to_match = NULL; 216 char *symbol_start = text; 217 char *orig_text = text; 218 size_t text_len; 219 220 /* Do we have an unquoted colon, as in "break foo.c::bar"? */ 221 for (p = text; *p != '\0'; ++p) 222 { 223 if (*p == '\\' && p[1] == '\'') 224 p++; 225 else if (*p == '\'' || *p == '"') 226 { 227 quote_found = *p; 228 quote_char = *p++; 229 while (*p != '\0' && *p != quote_found) 230 { 231 if (*p == '\\' && p[1] == quote_found) 232 p++; 233 p++; 234 } 235 236 if (*p == quote_found) 237 quote_found = 0; 238 else 239 break; /* Hit the end of text. */ 240 } 241#if HAVE_DOS_BASED_FILE_SYSTEM 242 /* If we have a DOS-style absolute file name at the beginning of 243 TEXT, and the colon after the drive letter is the only colon 244 we found, pretend the colon is not there. */ 245 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted) 246 ; 247#endif 248 else if (*p == ':' && !colon) 249 { 250 colon = p; 251 symbol_start = p + 1; 252 } 253 else if (strchr (current_language->la_word_break_characters(), *p)) 254 symbol_start = p + 1; 255 } 256 257 if (quoted) 258 text++; 259 text_len = strlen (text); 260 261 /* Where is the file name? */ 262 if (colon) 263 { 264 char *s; 265 266 file_to_match = (char *) xmalloc (colon - text + 1); 267 strncpy (file_to_match, text, colon - text + 1); 268 /* Remove trailing colons and quotes from the file name. */ 269 for (s = file_to_match + (colon - text); 270 s > file_to_match; 271 s--) 272 if (*s == ':' || *s == quote_char) 273 *s = '\0'; 274 } 275 /* If the text includes a colon, they want completion only on a 276 symbol name after the colon. Otherwise, we need to complete on 277 symbols as well as on files. */ 278 if (colon) 279 { 280 list = make_file_symbol_completion_list (symbol_start, word, 281 file_to_match); 282 xfree (file_to_match); 283 } 284 else 285 { 286 list = make_symbol_completion_list (symbol_start, word); 287 /* If text includes characters which cannot appear in a file 288 name, they cannot be asking for completion on files. */ 289 if (strcspn (text, 290 gdb_completer_file_name_break_characters) == text_len) 291 fn_list = make_source_files_completion_list (text, text); 292 } 293 294 /* How many completions do we have in both lists? */ 295 if (fn_list) 296 for ( ; fn_list[n_files]; n_files++) 297 ; 298 if (list) 299 for ( ; list[n_syms]; n_syms++) 300 ; 301 302 /* Make list[] large enough to hold both lists, then catenate 303 fn_list[] onto the end of list[]. */ 304 if (n_syms && n_files) 305 { 306 list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *)); 307 memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *)); 308 xfree (fn_list); 309 } 310 else if (n_files) 311 { 312 /* If we only have file names as possible completion, we should 313 bring them in sync with what rl_complete expects. The 314 problem is that if the user types "break /foo/b TAB", and the 315 possible completions are "/foo/bar" and "/foo/baz" 316 rl_complete expects us to return "bar" and "baz", without the 317 leading directories, as possible completions, because `word' 318 starts at the "b". But we ignore the value of `word' when we 319 call make_source_files_completion_list above (because that 320 would not DTRT when the completion results in both symbols 321 and file names), so make_source_files_completion_list returns 322 the full "/foo/bar" and "/foo/baz" strings. This produces 323 wrong results when, e.g., there's only one possible 324 completion, because rl_complete will prepend "/foo/" to each 325 candidate completion. The loop below removes that leading 326 part. */ 327 for (n_files = 0; fn_list[n_files]; n_files++) 328 { 329 memmove (fn_list[n_files], fn_list[n_files] + (word - text), 330 strlen (fn_list[n_files]) + 1 - (word - text)); 331 } 332 /* Return just the file-name list as the result. */ 333 list = fn_list; 334 } 335 else if (!n_syms) 336 { 337 /* No completions at all. As the final resort, try completing 338 on the entire text as a symbol. */ 339 list = make_symbol_completion_list (orig_text, word); 340 xfree (fn_list); 341 } 342 else 343 xfree (fn_list); 344 345 return list; 346} 347 348/* Helper for expression_completer which recursively counts the number 349 of named fields and methods in a structure or union type. */ 350static int 351count_struct_fields (struct type *type) 352{ 353 int i, result = 0; 354 355 CHECK_TYPEDEF (type); 356 for (i = 0; i < TYPE_NFIELDS (type); ++i) 357 { 358 if (i < TYPE_N_BASECLASSES (type)) 359 result += count_struct_fields (TYPE_BASECLASS (type, i)); 360 else if (TYPE_FIELD_NAME (type, i)) 361 { 362 if (TYPE_FIELD_NAME (type, i)[0] != '\0') 363 ++result; 364 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION) 365 { 366 /* Recurse into anonymous unions. */ 367 result += count_struct_fields (TYPE_FIELD_TYPE (type, i)); 368 } 369 } 370 } 371 372 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) 373 { 374 if (TYPE_FN_FIELDLIST_NAME (type, i)) 375 ++result; 376 } 377 378 return result; 379} 380 381/* Helper for expression_completer which recursively adds field and 382 method names from TYPE, a struct or union type, to the array 383 OUTPUT. This function assumes that OUTPUT is correctly-sized. */ 384static void 385add_struct_fields (struct type *type, int *nextp, char **output, 386 char *fieldname, int namelen) 387{ 388 int i; 389 int computed_type_name = 0; 390 char *type_name = NULL; 391 392 CHECK_TYPEDEF (type); 393 for (i = 0; i < TYPE_NFIELDS (type); ++i) 394 { 395 if (i < TYPE_N_BASECLASSES (type)) 396 add_struct_fields (TYPE_BASECLASS (type, i), nextp, 397 output, fieldname, namelen); 398 else if (TYPE_FIELD_NAME (type, i)) 399 { 400 if (TYPE_FIELD_NAME (type, i)[0] != '\0') 401 { 402 if (! strncmp (TYPE_FIELD_NAME (type, i), 403 fieldname, namelen)) 404 { 405 output[*nextp] = xstrdup (TYPE_FIELD_NAME (type, i)); 406 ++*nextp; 407 } 408 } 409 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION) 410 { 411 /* Recurse into anonymous unions. */ 412 add_struct_fields (TYPE_FIELD_TYPE (type, i), nextp, 413 output, fieldname, namelen); 414 } 415 } 416 } 417 418 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) 419 { 420 char *name = TYPE_FN_FIELDLIST_NAME (type, i); 421 422 if (name && ! strncmp (name, fieldname, namelen)) 423 { 424 if (!computed_type_name) 425 { 426 type_name = type_name_no_tag (type); 427 computed_type_name = 1; 428 } 429 /* Omit constructors from the completion list. */ 430 if (!type_name || strcmp (type_name, name)) 431 { 432 output[*nextp] = xstrdup (name); 433 ++*nextp; 434 } 435 } 436 } 437} 438 439/* Complete on expressions. Often this means completing on symbol 440 names, but some language parsers also have support for completing 441 field names. */ 442char ** 443expression_completer (struct cmd_list_element *ignore, 444 char *text, char *word) 445{ 446 struct type *type = NULL; 447 char *fieldname, *p; 448 volatile struct gdb_exception except; 449 450 /* Perform a tentative parse of the expression, to see whether a 451 field completion is required. */ 452 fieldname = NULL; 453 TRY_CATCH (except, RETURN_MASK_ERROR) 454 { 455 type = parse_field_expression (text, &fieldname); 456 } 457 if (except.reason < 0) 458 return NULL; 459 if (fieldname && type) 460 { 461 for (;;) 462 { 463 CHECK_TYPEDEF (type); 464 if (TYPE_CODE (type) != TYPE_CODE_PTR 465 && TYPE_CODE (type) != TYPE_CODE_REF) 466 break; 467 type = TYPE_TARGET_TYPE (type); 468 } 469 470 if (TYPE_CODE (type) == TYPE_CODE_UNION 471 || TYPE_CODE (type) == TYPE_CODE_STRUCT) 472 { 473 int alloc = count_struct_fields (type); 474 int flen = strlen (fieldname); 475 int out = 0; 476 char **result = (char **) xmalloc ((alloc + 1) * sizeof (char *)); 477 478 add_struct_fields (type, &out, result, fieldname, flen); 479 result[out] = NULL; 480 xfree (fieldname); 481 return result; 482 } 483 } 484 xfree (fieldname); 485 486 /* Commands which complete on locations want to see the entire 487 argument. */ 488 for (p = word; 489 p > text && p[-1] != ' ' && p[-1] != '\t'; 490 p--) 491 ; 492 493 /* Not ideal but it is what we used to do before... */ 494 return location_completer (ignore, p, word); 495} 496 497/* Here are some useful test cases for completion. FIXME: These 498 should be put in the test suite. They should be tested with both 499 M-? and TAB. 500 501 "show output-" "radix" 502 "show output" "-radix" 503 "p" ambiguous (commands starting with p--path, print, printf, etc.) 504 "p " ambiguous (all symbols) 505 "info t foo" no completions 506 "info t " no completions 507 "info t" ambiguous ("info target", "info terminal", etc.) 508 "info ajksdlfk" no completions 509 "info ajksdlfk " no completions 510 "info" " " 511 "info " ambiguous (all info commands) 512 "p \"a" no completions (string constant) 513 "p 'a" ambiguous (all symbols starting with a) 514 "p b-a" ambiguous (all symbols starting with a) 515 "p b-" ambiguous (all symbols) 516 "file Make" "file" (word break hard to screw up here) 517 "file ../gdb.stabs/we" "ird" (needs to not break word at slash) 518 */ 519 520typedef enum 521{ 522 handle_brkchars, 523 handle_completions, 524 handle_help 525} 526complete_line_internal_reason; 527 528 529/* Internal function used to handle completions. 530 531 532 TEXT is the caller's idea of the "word" we are looking at. 533 534 LINE_BUFFER is available to be looked at; it contains the entire 535 text of the line. POINT is the offset in that line of the cursor. 536 You should pretend that the line ends at POINT. 537 538 REASON is of type complete_line_internal_reason. 539 540 If REASON is handle_brkchars: 541 Preliminary phase, called by gdb_completion_word_break_characters 542 function, is used to determine the correct set of chars that are 543 word delimiters depending on the current command in line_buffer. 544 No completion list should be generated; the return value should be 545 NULL. This is checked by an assertion in that function. 546 547 If REASON is handle_completions: 548 Main phase, called by complete_line function, is used to get the list 549 of posible completions. 550 551 If REASON is handle_help: 552 Special case when completing a 'help' command. In this case, 553 once sub-command completions are exhausted, we simply return NULL. 554 */ 555 556static char ** 557complete_line_internal (const char *text, 558 char *line_buffer, int point, 559 complete_line_internal_reason reason) 560{ 561 char **list = NULL; 562 char *tmp_command, *p; 563 /* Pointer within tmp_command which corresponds to text. */ 564 char *word; 565 struct cmd_list_element *c, *result_list; 566 567 /* Choose the default set of word break characters to break 568 completions. If we later find out that we are doing completions 569 on command strings (as opposed to strings supplied by the 570 individual command completer functions, which can be any string) 571 then we will switch to the special word break set for command 572 strings, which leaves out the '-' character used in some 573 commands. */ 574 rl_completer_word_break_characters = 575 current_language->la_word_break_characters(); 576 577 /* Decide whether to complete on a list of gdb commands or on 578 symbols. */ 579 tmp_command = (char *) alloca (point + 1); 580 p = tmp_command; 581 582 strncpy (tmp_command, line_buffer, point); 583 tmp_command[point] = '\0'; 584 /* Since text always contains some number of characters leading up 585 to point, we can find the equivalent position in tmp_command 586 by subtracting that many characters from the end of tmp_command. */ 587 word = tmp_command + point - strlen (text); 588 589 if (point == 0) 590 { 591 /* An empty line we want to consider ambiguous; that is, it 592 could be any command. */ 593 c = CMD_LIST_AMBIGUOUS; 594 result_list = 0; 595 } 596 else 597 { 598 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1); 599 } 600 601 /* Move p up to the next interesting thing. */ 602 while (*p == ' ' || *p == '\t') 603 { 604 p++; 605 } 606 607 if (!c) 608 { 609 /* It is an unrecognized command. So there are no 610 possible completions. */ 611 list = NULL; 612 } 613 else if (c == CMD_LIST_AMBIGUOUS) 614 { 615 char *q; 616 617 /* lookup_cmd_1 advances p up to the first ambiguous thing, but 618 doesn't advance over that thing itself. Do so now. */ 619 q = p; 620 while (*q && (isalnum (*q) || *q == '-' || *q == '_')) 621 ++q; 622 if (q != tmp_command + point) 623 { 624 /* There is something beyond the ambiguous 625 command, so there are no possible completions. For 626 example, "info t " or "info t foo" does not complete 627 to anything, because "info t" can be "info target" or 628 "info terminal". */ 629 list = NULL; 630 } 631 else 632 { 633 /* We're trying to complete on the command which was ambiguous. 634 This we can deal with. */ 635 if (result_list) 636 { 637 if (reason != handle_brkchars) 638 list = complete_on_cmdlist (*result_list->prefixlist, p, 639 word); 640 } 641 else 642 { 643 if (reason != handle_brkchars) 644 list = complete_on_cmdlist (cmdlist, p, word); 645 } 646 /* Ensure that readline does the right thing with respect to 647 inserting quotes. */ 648 rl_completer_word_break_characters = 649 gdb_completer_command_word_break_characters; 650 } 651 } 652 else 653 { 654 /* We've recognized a full command. */ 655 656 if (p == tmp_command + point) 657 { 658 /* There is no non-whitespace in the line beyond the 659 command. */ 660 661 if (p[-1] == ' ' || p[-1] == '\t') 662 { 663 /* The command is followed by whitespace; we need to 664 complete on whatever comes after command. */ 665 if (c->prefixlist) 666 { 667 /* It is a prefix command; what comes after it is 668 a subcommand (e.g. "info "). */ 669 if (reason != handle_brkchars) 670 list = complete_on_cmdlist (*c->prefixlist, p, word); 671 672 /* Ensure that readline does the right thing 673 with respect to inserting quotes. */ 674 rl_completer_word_break_characters = 675 gdb_completer_command_word_break_characters; 676 } 677 else if (reason == handle_help) 678 list = NULL; 679 else if (c->enums) 680 { 681 if (reason != handle_brkchars) 682 list = complete_on_enum (c->enums, p, word); 683 rl_completer_word_break_characters = 684 gdb_completer_command_word_break_characters; 685 } 686 else 687 { 688 /* It is a normal command; what comes after it is 689 completed by the command's completer function. */ 690 if (c->completer == filename_completer) 691 { 692 /* Many commands which want to complete on 693 file names accept several file names, as 694 in "run foo bar >>baz". So we don't want 695 to complete the entire text after the 696 command, just the last word. To this 697 end, we need to find the beginning of the 698 file name by starting at `word' and going 699 backwards. */ 700 for (p = word; 701 p > tmp_command 702 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL; 703 p--) 704 ; 705 rl_completer_word_break_characters = 706 gdb_completer_file_name_break_characters; 707 } 708 else if (c->completer == location_completer) 709 { 710 /* Commands which complete on locations want to 711 see the entire argument. */ 712 for (p = word; 713 p > tmp_command 714 && p[-1] != ' ' && p[-1] != '\t'; 715 p--) 716 ; 717 } 718 if (reason != handle_brkchars && c->completer != NULL) 719 list = (*c->completer) (c, p, word); 720 } 721 } 722 else 723 { 724 /* The command is not followed by whitespace; we need to 725 complete on the command itself, e.g. "p" which is a 726 command itself but also can complete to "print", "ptype" 727 etc. */ 728 char *q; 729 730 /* Find the command we are completing on. */ 731 q = p; 732 while (q > tmp_command) 733 { 734 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') 735 --q; 736 else 737 break; 738 } 739 740 if (reason != handle_brkchars) 741 list = complete_on_cmdlist (result_list, q, word); 742 743 /* Ensure that readline does the right thing 744 with respect to inserting quotes. */ 745 rl_completer_word_break_characters = 746 gdb_completer_command_word_break_characters; 747 } 748 } 749 else if (reason == handle_help) 750 list = NULL; 751 else 752 { 753 /* There is non-whitespace beyond the command. */ 754 755 if (c->prefixlist && !c->allow_unknown) 756 { 757 /* It is an unrecognized subcommand of a prefix command, 758 e.g. "info adsfkdj". */ 759 list = NULL; 760 } 761 else if (c->enums) 762 { 763 if (reason != handle_brkchars) 764 list = complete_on_enum (c->enums, p, word); 765 } 766 else 767 { 768 /* It is a normal command. */ 769 if (c->completer == filename_completer) 770 { 771 /* See the commentary above about the specifics 772 of file-name completion. */ 773 for (p = word; 774 p > tmp_command 775 && strchr (gdb_completer_file_name_break_characters, 776 p[-1]) == NULL; 777 p--) 778 ; 779 rl_completer_word_break_characters = 780 gdb_completer_file_name_break_characters; 781 } 782 else if (c->completer == location_completer) 783 { 784 for (p = word; 785 p > tmp_command 786 && p[-1] != ' ' && p[-1] != '\t'; 787 p--) 788 ; 789 } 790 if (reason != handle_brkchars && c->completer != NULL) 791 list = (*c->completer) (c, p, word); 792 } 793 } 794 } 795 796 return list; 797} 798/* Generate completions all at once. Returns a NULL-terminated array 799 of strings. Both the array and each element are allocated with 800 xmalloc. It can also return NULL if there are no completions. 801 802 TEXT is the caller's idea of the "word" we are looking at. 803 804 LINE_BUFFER is available to be looked at; it contains the entire 805 text of the line. 806 807 POINT is the offset in that line of the cursor. You 808 should pretend that the line ends at POINT. */ 809 810char ** 811complete_line (const char *text, char *line_buffer, int point) 812{ 813 return complete_line_internal (text, line_buffer, 814 point, handle_completions); 815} 816 817/* Complete on command names. Used by "help". */ 818char ** 819command_completer (struct cmd_list_element *ignore, 820 char *text, char *word) 821{ 822 return complete_line_internal (word, text, 823 strlen (text), handle_help); 824} 825 826/* Get the list of chars that are considered as word breaks 827 for the current command. */ 828 829char * 830gdb_completion_word_break_characters (void) 831{ 832 char **list; 833 834 list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point, 835 handle_brkchars); 836 gdb_assert (list == NULL); 837 return rl_completer_word_break_characters; 838} 839 840/* Generate completions one by one for the completer. Each time we 841 are called return another potential completion to the caller. 842 line_completion just completes on commands or passes the buck to 843 the command's completer function, the stuff specific to symbol 844 completion is in make_symbol_completion_list. 845 846 TEXT is the caller's idea of the "word" we are looking at. 847 848 MATCHES is the number of matches that have currently been collected 849 from calling this completion function. When zero, then we need to 850 initialize, otherwise the initialization has already taken place 851 and we can just return the next potential completion string. 852 853 LINE_BUFFER is available to be looked at; it contains the entire 854 text of the line. POINT is the offset in that line of the cursor. 855 You should pretend that the line ends at POINT. 856 857 Returns NULL if there are no more completions, else a pointer to a 858 string which is a possible completion, it is the caller's 859 responsibility to free the string. */ 860 861static char * 862line_completion_function (const char *text, int matches, 863 char *line_buffer, int point) 864{ 865 static char **list = (char **) NULL; /* Cache of completions. */ 866 static int index; /* Next cached completion. */ 867 char *output = NULL; 868 869 if (matches == 0) 870 { 871 /* The caller is beginning to accumulate a new set of 872 completions, so we need to find all of them now, and cache 873 them for returning one at a time on future calls. */ 874 875 if (list) 876 { 877 /* Free the storage used by LIST, but not by the strings 878 inside. This is because rl_complete_internal () frees 879 the strings. As complete_line may abort by calling 880 `error' clear LIST now. */ 881 xfree (list); 882 list = NULL; 883 } 884 index = 0; 885 list = complete_line (text, line_buffer, point); 886 } 887 888 /* If we found a list of potential completions during initialization 889 then dole them out one at a time. The vector of completions is 890 NULL terminated, so after returning the last one, return NULL 891 (and continue to do so) each time we are called after that, until 892 a new list is available. */ 893 894 if (list) 895 { 896 output = list[index]; 897 if (output) 898 { 899 index++; 900 } 901 } 902 903#if 0 904 /* Can't do this because readline hasn't yet checked the word breaks 905 for figuring out whether to insert a quote. */ 906 if (output == NULL) 907 /* Make sure the word break characters are set back to normal for 908 the next time that readline tries to complete something. */ 909 rl_completer_word_break_characters = 910 current_language->la_word_break_characters(); 911#endif 912 913 return (output); 914} 915 916/* Skip over the possibly quoted word STR (as defined by the quote 917 characters QUOTECHARS and the word break characters BREAKCHARS). 918 Returns pointer to the location after the "word". If either 919 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the 920 completer. */ 921 922char * 923skip_quoted_chars (char *str, char *quotechars, char *breakchars) 924{ 925 char quote_char = '\0'; 926 char *scan; 927 928 if (quotechars == NULL) 929 quotechars = gdb_completer_quote_characters; 930 931 if (breakchars == NULL) 932 breakchars = current_language->la_word_break_characters(); 933 934 for (scan = str; *scan != '\0'; scan++) 935 { 936 if (quote_char != '\0') 937 { 938 /* Ignore everything until the matching close quote char. */ 939 if (*scan == quote_char) 940 { 941 /* Found matching close quote. */ 942 scan++; 943 break; 944 } 945 } 946 else if (strchr (quotechars, *scan)) 947 { 948 /* Found start of a quoted string. */ 949 quote_char = *scan; 950 } 951 else if (strchr (breakchars, *scan)) 952 { 953 break; 954 } 955 } 956 957 return (scan); 958} 959 960/* Skip over the possibly quoted word STR (as defined by the quote 961 characters and word break characters used by the completer). 962 Returns pointer to the location after the "word". */ 963 964char * 965skip_quoted (char *str) 966{ 967 return skip_quoted_chars (str, NULL, NULL); 968} 969