| 1 | /* Line completion stuff for GDB, the GNU debugger. |
| 2 | Copyright (C) 2000, 2001, 2007, 2008, 2009 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 3 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, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include "defs.h" |
| 20 | #include "symtab.h" |
| 21 | #include "gdbtypes.h" |
| 22 | #include "expression.h" |
| 23 | #include "filenames.h" /* For DOSish file names. */ |
| 24 | #include "language.h" |
| 25 | #include "gdb_assert.h" |
| 26 | |
| 27 | #include "cli/cli-decode.h" |
| 28 | |
| 29 | /* FIXME: This is needed because of lookup_cmd_1 (). We should be |
| 30 | calling a hook instead so we eliminate the CLI dependency. */ |
| 31 | #include "gdbcmd.h" |
| 32 | |
| 33 | /* Needed for rl_completer_word_break_characters() and for |
| 34 | rl_filename_completion_function. */ |
| 35 | #include "readline/readline.h" |
| 36 | |
| 37 | /* readline defines this. */ |
| 38 | #undef savestring |
| 39 | |
| 40 | #include "completer.h" |
| 41 | |
| 42 | /* Prototypes for local functions. */ |
| 43 | static |
| 44 | char *line_completion_function (const char *text, int matches, |
| 45 | 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 | \f |
| 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 (struct cmd_list_element *ignore, char *text, char *prefix) |
| 110 | { |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | /* Complete on filenames. */ |
| 115 | char ** |
| 116 | filename_completer (struct cmd_list_element *ignore, 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, *q; |
| 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 | { |
| 154 | xfree (p); |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | if (word == text) |
| 159 | /* Return exactly p. */ |
| 160 | return_val[return_val_used++] = p; |
| 161 | else if (word > text) |
| 162 | { |
| 163 | /* Return some portion of p. */ |
| 164 | q = xmalloc (strlen (p) + 5); |
| 165 | strcpy (q, p + (word - text)); |
| 166 | return_val[return_val_used++] = q; |
| 167 | xfree (p); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | /* Return some of TEXT plus p. */ |
| 172 | q = xmalloc (strlen (p) + (text - word) + 5); |
| 173 | strncpy (q, word, text - word); |
| 174 | q[text - word] = '\0'; |
| 175 | strcat (q, p); |
| 176 | return_val[return_val_used++] = q; |
| 177 | xfree (p); |
| 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 | /* Ensure 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 (struct cmd_list_element *ignore, 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, |
| 284 | gdb_completer_file_name_break_characters) == text_len) |
| 285 | fn_list = make_source_files_completion_list (text, text); |
| 286 | } |
| 287 | |
| 288 | /* How many completions do we have in both lists? */ |
| 289 | if (fn_list) |
| 290 | for ( ; fn_list[n_files]; n_files++) |
| 291 | ; |
| 292 | if (list) |
| 293 | for ( ; list[n_syms]; n_syms++) |
| 294 | ; |
| 295 | |
| 296 | /* Make list[] large enough to hold both lists, then catenate |
| 297 | fn_list[] onto the end of list[]. */ |
| 298 | if (n_syms && n_files) |
| 299 | { |
| 300 | list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *)); |
| 301 | memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *)); |
| 302 | xfree (fn_list); |
| 303 | } |
| 304 | else if (n_files) |
| 305 | { |
| 306 | /* If we only have file names as possible completion, we should |
| 307 | bring them in sync with what rl_complete expects. The |
| 308 | problem is that if the user types "break /foo/b TAB", and the |
| 309 | possible completions are "/foo/bar" and "/foo/baz" |
| 310 | rl_complete expects us to return "bar" and "baz", without the |
| 311 | leading directories, as possible completions, because `word' |
| 312 | starts at the "b". But we ignore the value of `word' when we |
| 313 | call make_source_files_completion_list above (because that |
| 314 | would not DTRT when the completion results in both symbols |
| 315 | and file names), so make_source_files_completion_list returns |
| 316 | the full "/foo/bar" and "/foo/baz" strings. This produces |
| 317 | wrong results when, e.g., there's only one possible |
| 318 | completion, because rl_complete will prepend "/foo/" to each |
| 319 | candidate completion. The loop below removes that leading |
| 320 | part. */ |
| 321 | for (n_files = 0; fn_list[n_files]; n_files++) |
| 322 | { |
| 323 | memmove (fn_list[n_files], fn_list[n_files] + (word - text), |
| 324 | strlen (fn_list[n_files]) + 1 - (word - text)); |
| 325 | } |
| 326 | /* Return just the file-name list as the result. */ |
| 327 | list = fn_list; |
| 328 | } |
| 329 | else if (!n_syms) |
| 330 | { |
| 331 | /* No completions at all. As the final resort, try completing |
| 332 | on the entire text as a symbol. */ |
| 333 | list = make_symbol_completion_list (orig_text, word); |
| 334 | xfree (fn_list); |
| 335 | } |
| 336 | else |
| 337 | xfree (fn_list); |
| 338 | |
| 339 | return list; |
| 340 | } |
| 341 | |
| 342 | /* Helper for expression_completer which recursively counts the number |
| 343 | of named fields and methods in a structure or union type. */ |
| 344 | static int |
| 345 | count_struct_fields (struct type *type) |
| 346 | { |
| 347 | int i, result = 0; |
| 348 | |
| 349 | CHECK_TYPEDEF (type); |
| 350 | for (i = 0; i < TYPE_NFIELDS (type); ++i) |
| 351 | { |
| 352 | if (i < TYPE_N_BASECLASSES (type)) |
| 353 | result += count_struct_fields (TYPE_BASECLASS (type, i)); |
| 354 | else if (TYPE_FIELD_NAME (type, i)) |
| 355 | ++result; |
| 356 | } |
| 357 | |
| 358 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) |
| 359 | { |
| 360 | if (TYPE_FN_FIELDLIST_NAME (type, i)) |
| 361 | ++result; |
| 362 | } |
| 363 | |
| 364 | return result; |
| 365 | } |
| 366 | |
| 367 | /* Helper for expression_completer which recursively adds field and |
| 368 | method names from TYPE, a struct or union type, to the array |
| 369 | OUTPUT. This function assumes that OUTPUT is correctly-sized. */ |
| 370 | static void |
| 371 | add_struct_fields (struct type *type, int *nextp, char **output, |
| 372 | char *fieldname, int namelen) |
| 373 | { |
| 374 | int i; |
| 375 | int computed_type_name = 0; |
| 376 | char *type_name = NULL; |
| 377 | |
| 378 | CHECK_TYPEDEF (type); |
| 379 | for (i = 0; i < TYPE_NFIELDS (type); ++i) |
| 380 | { |
| 381 | if (i < TYPE_N_BASECLASSES (type)) |
| 382 | add_struct_fields (TYPE_BASECLASS (type, i), nextp, output, |
| 383 | fieldname, namelen); |
| 384 | else if (TYPE_FIELD_NAME (type, i) |
| 385 | && ! strncmp (TYPE_FIELD_NAME (type, i), fieldname, namelen)) |
| 386 | { |
| 387 | output[*nextp] = xstrdup (TYPE_FIELD_NAME (type, i)); |
| 388 | ++*nextp; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) |
| 393 | { |
| 394 | char *name = TYPE_FN_FIELDLIST_NAME (type, i); |
| 395 | if (name && ! strncmp (name, fieldname, namelen)) |
| 396 | { |
| 397 | if (!computed_type_name) |
| 398 | { |
| 399 | type_name = type_name_no_tag (type); |
| 400 | computed_type_name = 1; |
| 401 | } |
| 402 | /* Omit constructors from the completion list. */ |
| 403 | if (type_name && strcmp (type_name, name)) |
| 404 | { |
| 405 | output[*nextp] = xstrdup (name); |
| 406 | ++*nextp; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* Complete on expressions. Often this means completing on symbol |
| 413 | names, but some language parsers also have support for completing |
| 414 | field names. */ |
| 415 | char ** |
| 416 | expression_completer (struct cmd_list_element *ignore, char *text, char *word) |
| 417 | { |
| 418 | struct type *type; |
| 419 | char *fieldname, *p; |
| 420 | |
| 421 | /* Perform a tentative parse of the expression, to see whether a |
| 422 | field completion is required. */ |
| 423 | fieldname = NULL; |
| 424 | type = parse_field_expression (text, &fieldname); |
| 425 | if (fieldname && type) |
| 426 | { |
| 427 | for (;;) |
| 428 | { |
| 429 | CHECK_TYPEDEF (type); |
| 430 | if (TYPE_CODE (type) != TYPE_CODE_PTR |
| 431 | && TYPE_CODE (type) != TYPE_CODE_REF) |
| 432 | break; |
| 433 | type = TYPE_TARGET_TYPE (type); |
| 434 | } |
| 435 | |
| 436 | if (TYPE_CODE (type) == TYPE_CODE_UNION |
| 437 | || TYPE_CODE (type) == TYPE_CODE_STRUCT) |
| 438 | { |
| 439 | int alloc = count_struct_fields (type); |
| 440 | int flen = strlen (fieldname); |
| 441 | int out = 0; |
| 442 | char **result = (char **) xmalloc ((alloc + 1) * sizeof (char *)); |
| 443 | |
| 444 | add_struct_fields (type, &out, result, fieldname, flen); |
| 445 | result[out] = NULL; |
| 446 | xfree (fieldname); |
| 447 | return result; |
| 448 | } |
| 449 | } |
| 450 | xfree (fieldname); |
| 451 | |
| 452 | /* Commands which complete on locations want to see the entire |
| 453 | argument. */ |
| 454 | for (p = word; |
| 455 | p > text && p[-1] != ' ' && p[-1] != '\t'; |
| 456 | p--) |
| 457 | ; |
| 458 | |
| 459 | /* Not ideal but it is what we used to do before... */ |
| 460 | return location_completer (ignore, p, word); |
| 461 | } |
| 462 | |
| 463 | /* Here are some useful test cases for completion. FIXME: These should |
| 464 | be put in the test suite. They should be tested with both M-? and TAB. |
| 465 | |
| 466 | "show output-" "radix" |
| 467 | "show output" "-radix" |
| 468 | "p" ambiguous (commands starting with p--path, print, printf, etc.) |
| 469 | "p " ambiguous (all symbols) |
| 470 | "info t foo" no completions |
| 471 | "info t " no completions |
| 472 | "info t" ambiguous ("info target", "info terminal", etc.) |
| 473 | "info ajksdlfk" no completions |
| 474 | "info ajksdlfk " no completions |
| 475 | "info" " " |
| 476 | "info " ambiguous (all info commands) |
| 477 | "p \"a" no completions (string constant) |
| 478 | "p 'a" ambiguous (all symbols starting with a) |
| 479 | "p b-a" ambiguous (all symbols starting with a) |
| 480 | "p b-" ambiguous (all symbols) |
| 481 | "file Make" "file" (word break hard to screw up here) |
| 482 | "file ../gdb.stabs/we" "ird" (needs to not break word at slash) |
| 483 | */ |
| 484 | |
| 485 | typedef enum |
| 486 | { |
| 487 | handle_brkchars, |
| 488 | handle_completions, |
| 489 | handle_help |
| 490 | } |
| 491 | complete_line_internal_reason; |
| 492 | |
| 493 | |
| 494 | /* Internal function used to handle completions. |
| 495 | |
| 496 | |
| 497 | TEXT is the caller's idea of the "word" we are looking at. |
| 498 | |
| 499 | LINE_BUFFER is available to be looked at; it contains the entire text |
| 500 | of the line. POINT is the offset in that line of the cursor. You |
| 501 | should pretend that the line ends at POINT. |
| 502 | |
| 503 | REASON is of type complete_line_internal_reason. |
| 504 | |
| 505 | If REASON is handle_brkchars: |
| 506 | Preliminary phase, called by gdb_completion_word_break_characters function, |
| 507 | is used to determine the correct set of chars that are word delimiters |
| 508 | depending on the current command in line_buffer. |
| 509 | No completion list should be generated; the return value should be NULL. |
| 510 | This is checked by an assertion in that function. |
| 511 | |
| 512 | If REASON is handle_completions: |
| 513 | Main phase, called by complete_line function, is used to get the list |
| 514 | of posible completions. |
| 515 | |
| 516 | If REASON is handle_help: |
| 517 | Special case when completing a 'help' command. In this case, |
| 518 | once sub-command completions are exhausted, we simply return NULL. |
| 519 | */ |
| 520 | |
| 521 | static char ** |
| 522 | complete_line_internal (const char *text, char *line_buffer, int point, |
| 523 | complete_line_internal_reason reason) |
| 524 | { |
| 525 | char **list = NULL; |
| 526 | char *tmp_command, *p; |
| 527 | /* Pointer within tmp_command which corresponds to text. */ |
| 528 | char *word; |
| 529 | struct cmd_list_element *c, *result_list; |
| 530 | |
| 531 | /* Choose the default set of word break characters to break completions. |
| 532 | If we later find out that we are doing completions on command strings |
| 533 | (as opposed to strings supplied by the individual command completer |
| 534 | functions, which can be any string) then we will switch to the |
| 535 | special word break set for command strings, which leaves out the |
| 536 | '-' character used in some commands. */ |
| 537 | rl_completer_word_break_characters = |
| 538 | current_language->la_word_break_characters(); |
| 539 | |
| 540 | /* Decide whether to complete on a list of gdb commands or on symbols. */ |
| 541 | tmp_command = (char *) alloca (point + 1); |
| 542 | p = tmp_command; |
| 543 | |
| 544 | strncpy (tmp_command, line_buffer, point); |
| 545 | tmp_command[point] = '\0'; |
| 546 | /* Since text always contains some number of characters leading up |
| 547 | to point, we can find the equivalent position in tmp_command |
| 548 | by subtracting that many characters from the end of tmp_command. */ |
| 549 | word = tmp_command + point - strlen (text); |
| 550 | |
| 551 | if (point == 0) |
| 552 | { |
| 553 | /* An empty line we want to consider ambiguous; that is, it |
| 554 | could be any command. */ |
| 555 | c = (struct cmd_list_element *) -1; |
| 556 | result_list = 0; |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | c = lookup_cmd_1 (&p, cmdlist, &result_list, 1); |
| 561 | } |
| 562 | |
| 563 | /* Move p up to the next interesting thing. */ |
| 564 | while (*p == ' ' || *p == '\t') |
| 565 | { |
| 566 | p++; |
| 567 | } |
| 568 | |
| 569 | if (!c) |
| 570 | { |
| 571 | /* It is an unrecognized command. So there are no |
| 572 | possible completions. */ |
| 573 | list = NULL; |
| 574 | } |
| 575 | else if (c == (struct cmd_list_element *) -1) |
| 576 | { |
| 577 | char *q; |
| 578 | |
| 579 | /* lookup_cmd_1 advances p up to the first ambiguous thing, but |
| 580 | doesn't advance over that thing itself. Do so now. */ |
| 581 | q = p; |
| 582 | while (*q && (isalnum (*q) || *q == '-' || *q == '_')) |
| 583 | ++q; |
| 584 | if (q != tmp_command + point) |
| 585 | { |
| 586 | /* There is something beyond the ambiguous |
| 587 | command, so there are no possible completions. For |
| 588 | example, "info t " or "info t foo" does not complete |
| 589 | to anything, because "info t" can be "info target" or |
| 590 | "info terminal". */ |
| 591 | list = NULL; |
| 592 | } |
| 593 | else |
| 594 | { |
| 595 | /* We're trying to complete on the command which was ambiguous. |
| 596 | This we can deal with. */ |
| 597 | if (result_list) |
| 598 | { |
| 599 | if (reason != handle_brkchars) |
| 600 | list = complete_on_cmdlist (*result_list->prefixlist, p, |
| 601 | word); |
| 602 | } |
| 603 | else |
| 604 | { |
| 605 | if (reason != handle_brkchars) |
| 606 | list = complete_on_cmdlist (cmdlist, p, word); |
| 607 | } |
| 608 | /* Ensure that readline does the right thing with respect to |
| 609 | inserting quotes. */ |
| 610 | rl_completer_word_break_characters = |
| 611 | gdb_completer_command_word_break_characters; |
| 612 | } |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | /* We've recognized a full command. */ |
| 617 | |
| 618 | if (p == tmp_command + point) |
| 619 | { |
| 620 | /* There is no non-whitespace in the line beyond the command. */ |
| 621 | |
| 622 | if (p[-1] == ' ' || p[-1] == '\t') |
| 623 | { |
| 624 | /* The command is followed by whitespace; we need to complete |
| 625 | on whatever comes after command. */ |
| 626 | if (c->prefixlist) |
| 627 | { |
| 628 | /* It is a prefix command; what comes after it is |
| 629 | a subcommand (e.g. "info "). */ |
| 630 | if (reason != handle_brkchars) |
| 631 | list = complete_on_cmdlist (*c->prefixlist, p, word); |
| 632 | |
| 633 | /* Ensure that readline does the right thing |
| 634 | with respect to inserting quotes. */ |
| 635 | rl_completer_word_break_characters = |
| 636 | gdb_completer_command_word_break_characters; |
| 637 | } |
| 638 | else if (reason == handle_help) |
| 639 | list = NULL; |
| 640 | else if (c->enums) |
| 641 | { |
| 642 | if (reason != handle_brkchars) |
| 643 | list = complete_on_enum (c->enums, p, word); |
| 644 | rl_completer_word_break_characters = |
| 645 | gdb_completer_command_word_break_characters; |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | /* It is a normal command; what comes after it is |
| 650 | completed by the command's completer function. */ |
| 651 | if (c->completer == filename_completer) |
| 652 | { |
| 653 | /* Many commands which want to complete on |
| 654 | file names accept several file names, as |
| 655 | in "run foo bar >>baz". So we don't want |
| 656 | to complete the entire text after the |
| 657 | command, just the last word. To this |
| 658 | end, we need to find the beginning of the |
| 659 | file name by starting at `word' and going |
| 660 | backwards. */ |
| 661 | for (p = word; |
| 662 | p > tmp_command |
| 663 | && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL; |
| 664 | p--) |
| 665 | ; |
| 666 | rl_completer_word_break_characters = |
| 667 | gdb_completer_file_name_break_characters; |
| 668 | } |
| 669 | else if (c->completer == location_completer) |
| 670 | { |
| 671 | /* Commands which complete on locations want to |
| 672 | see the entire argument. */ |
| 673 | for (p = word; |
| 674 | p > tmp_command |
| 675 | && p[-1] != ' ' && p[-1] != '\t'; |
| 676 | p--) |
| 677 | ; |
| 678 | } |
| 679 | if (reason != handle_brkchars) |
| 680 | list = (*c->completer) (c, p, word); |
| 681 | } |
| 682 | } |
| 683 | else |
| 684 | { |
| 685 | /* The command is not followed by whitespace; we need to |
| 686 | complete on the command itself. e.g. "p" which is a |
| 687 | command itself but also can complete to "print", "ptype" |
| 688 | etc. */ |
| 689 | char *q; |
| 690 | |
| 691 | /* Find the command we are completing on. */ |
| 692 | q = p; |
| 693 | while (q > tmp_command) |
| 694 | { |
| 695 | if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') |
| 696 | --q; |
| 697 | else |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | if (reason != handle_brkchars) |
| 702 | list = complete_on_cmdlist (result_list, q, word); |
| 703 | |
| 704 | /* Ensure that readline does the right thing |
| 705 | with respect to inserting quotes. */ |
| 706 | rl_completer_word_break_characters = |
| 707 | gdb_completer_command_word_break_characters; |
| 708 | } |
| 709 | } |
| 710 | else if (reason == handle_help) |
| 711 | list = NULL; |
| 712 | else |
| 713 | { |
| 714 | /* There is non-whitespace beyond the command. */ |
| 715 | |
| 716 | if (c->prefixlist && !c->allow_unknown) |
| 717 | { |
| 718 | /* It is an unrecognized subcommand of a prefix command, |
| 719 | e.g. "info adsfkdj". */ |
| 720 | list = NULL; |
| 721 | } |
| 722 | else if (c->enums) |
| 723 | { |
| 724 | if (reason != handle_brkchars) |
| 725 | list = complete_on_enum (c->enums, p, word); |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | /* It is a normal command. */ |
| 730 | if (c->completer == filename_completer) |
| 731 | { |
| 732 | /* See the commentary above about the specifics |
| 733 | of file-name completion. */ |
| 734 | for (p = word; |
| 735 | p > tmp_command |
| 736 | && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL; |
| 737 | p--) |
| 738 | ; |
| 739 | rl_completer_word_break_characters = |
| 740 | gdb_completer_file_name_break_characters; |
| 741 | } |
| 742 | else if (c->completer == location_completer) |
| 743 | { |
| 744 | for (p = word; |
| 745 | p > tmp_command |
| 746 | && p[-1] != ' ' && p[-1] != '\t'; |
| 747 | p--) |
| 748 | ; |
| 749 | } |
| 750 | if (reason != handle_brkchars) |
| 751 | list = (*c->completer) (c, p, word); |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | return list; |
| 757 | } |
| 758 | /* Generate completions all at once. Returns a NULL-terminated array |
| 759 | of strings. Both the array and each element are allocated with |
| 760 | xmalloc. It can also return NULL if there are no completions. |
| 761 | |
| 762 | TEXT is the caller's idea of the "word" we are looking at. |
| 763 | |
| 764 | LINE_BUFFER is available to be looked at; it contains the entire text |
| 765 | of the line. |
| 766 | |
| 767 | POINT is the offset in that line of the cursor. You |
| 768 | should pretend that the line ends at POINT. */ |
| 769 | |
| 770 | char ** |
| 771 | complete_line (const char *text, char *line_buffer, int point) |
| 772 | { |
| 773 | return complete_line_internal (text, line_buffer, point, handle_completions); |
| 774 | } |
| 775 | |
| 776 | /* Complete on command names. Used by "help". */ |
| 777 | char ** |
| 778 | command_completer (struct cmd_list_element *ignore, char *text, char *word) |
| 779 | { |
| 780 | return complete_line_internal (word, text, strlen (text), handle_help); |
| 781 | } |
| 782 | |
| 783 | /* Get the list of chars that are considered as word breaks |
| 784 | for the current command. */ |
| 785 | |
| 786 | char * |
| 787 | gdb_completion_word_break_characters (void) |
| 788 | { |
| 789 | char ** list; |
| 790 | list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point, |
| 791 | handle_brkchars); |
| 792 | gdb_assert (list == NULL); |
| 793 | return rl_completer_word_break_characters; |
| 794 | } |
| 795 | |
| 796 | /* Generate completions one by one for the completer. Each time we are |
| 797 | called return another potential completion to the caller. |
| 798 | line_completion just completes on commands or passes the buck to the |
| 799 | command's completer function, the stuff specific to symbol completion |
| 800 | is in make_symbol_completion_list. |
| 801 | |
| 802 | TEXT is the caller's idea of the "word" we are looking at. |
| 803 | |
| 804 | MATCHES is the number of matches that have currently been collected from |
| 805 | calling this completion function. When zero, then we need to initialize, |
| 806 | otherwise the initialization has already taken place and we can just |
| 807 | return the next potential completion string. |
| 808 | |
| 809 | LINE_BUFFER is available to be looked at; it contains the entire text |
| 810 | of the line. POINT is the offset in that line of the cursor. You |
| 811 | should pretend that the line ends at POINT. |
| 812 | |
| 813 | Returns NULL if there are no more completions, else a pointer to a string |
| 814 | which is a possible completion, it is the caller's responsibility to |
| 815 | free the string. */ |
| 816 | |
| 817 | static char * |
| 818 | line_completion_function (const char *text, int matches, |
| 819 | char *line_buffer, int point) |
| 820 | { |
| 821 | static char **list = (char **) NULL; /* Cache of completions. */ |
| 822 | static int index; /* Next cached completion. */ |
| 823 | char *output = NULL; |
| 824 | |
| 825 | if (matches == 0) |
| 826 | { |
| 827 | /* The caller is beginning to accumulate a new set of completions, so |
| 828 | we need to find all of them now, and cache them for returning one at |
| 829 | a time on future calls. */ |
| 830 | |
| 831 | if (list) |
| 832 | { |
| 833 | /* Free the storage used by LIST, but not by the strings inside. |
| 834 | This is because rl_complete_internal () frees the strings. |
| 835 | As complete_line may abort by calling `error' clear LIST now. */ |
| 836 | xfree (list); |
| 837 | list = NULL; |
| 838 | } |
| 839 | index = 0; |
| 840 | list = complete_line (text, line_buffer, point); |
| 841 | } |
| 842 | |
| 843 | /* If we found a list of potential completions during initialization then |
| 844 | dole them out one at a time. The vector of completions is NULL |
| 845 | terminated, so after returning the last one, return NULL (and continue |
| 846 | to do so) each time we are called after that, until a new list is |
| 847 | available. */ |
| 848 | |
| 849 | if (list) |
| 850 | { |
| 851 | output = list[index]; |
| 852 | if (output) |
| 853 | { |
| 854 | index++; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | #if 0 |
| 859 | /* Can't do this because readline hasn't yet checked the word breaks |
| 860 | for figuring out whether to insert a quote. */ |
| 861 | if (output == NULL) |
| 862 | /* Make sure the word break characters are set back to normal for the |
| 863 | next time that readline tries to complete something. */ |
| 864 | rl_completer_word_break_characters = |
| 865 | current_language->la_word_break_characters(); |
| 866 | #endif |
| 867 | |
| 868 | return (output); |
| 869 | } |
| 870 | |
| 871 | /* Skip over the possibly quoted word STR (as defined by the quote |
| 872 | characters QUOTECHARS and the the word break characters |
| 873 | BREAKCHARS). Returns pointer to the location after the "word". If |
| 874 | either QUOTECHARS or BREAKCHARS is NULL, use the same values used |
| 875 | by the completer. */ |
| 876 | |
| 877 | char * |
| 878 | skip_quoted_chars (char *str, char *quotechars, char *breakchars) |
| 879 | { |
| 880 | char quote_char = '\0'; |
| 881 | char *scan; |
| 882 | |
| 883 | if (quotechars == NULL) |
| 884 | quotechars = gdb_completer_quote_characters; |
| 885 | |
| 886 | if (breakchars == NULL) |
| 887 | breakchars = current_language->la_word_break_characters(); |
| 888 | |
| 889 | for (scan = str; *scan != '\0'; scan++) |
| 890 | { |
| 891 | if (quote_char != '\0') |
| 892 | { |
| 893 | /* Ignore everything until the matching close quote char. */ |
| 894 | if (*scan == quote_char) |
| 895 | { |
| 896 | /* Found matching close quote. */ |
| 897 | scan++; |
| 898 | break; |
| 899 | } |
| 900 | } |
| 901 | else if (strchr (quotechars, *scan)) |
| 902 | { |
| 903 | /* Found start of a quoted string. */ |
| 904 | quote_char = *scan; |
| 905 | } |
| 906 | else if (strchr (breakchars, *scan)) |
| 907 | { |
| 908 | break; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | return (scan); |
| 913 | } |
| 914 | |
| 915 | /* Skip over the possibly quoted word STR (as defined by the quote |
| 916 | characters and word break characters used by the completer). |
| 917 | Returns pointer to the location after the "word". */ |
| 918 | |
| 919 | char * |
| 920 | skip_quoted (char *str) |
| 921 | { |
| 922 | return skip_quoted_chars (str, NULL, NULL); |
| 923 | } |