| 1 | /* Line completion stuff for GDB, the GNU debugger. |
| 2 | Copyright (C) 2000-2017 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_signals.h" |
| 26 | #include "target.h" |
| 27 | #include "reggroups.h" |
| 28 | #include "user-regs.h" |
| 29 | #include "arch-utils.h" |
| 30 | #include "location.h" |
| 31 | |
| 32 | #include "cli/cli-decode.h" |
| 33 | |
| 34 | /* FIXME: This is needed because of lookup_cmd_1 (). We should be |
| 35 | calling a hook instead so we eliminate the CLI dependency. */ |
| 36 | #include "gdbcmd.h" |
| 37 | |
| 38 | /* Needed for rl_completer_word_break_characters() and for |
| 39 | rl_filename_completion_function. */ |
| 40 | #include "readline/readline.h" |
| 41 | |
| 42 | /* readline defines this. */ |
| 43 | #undef savestring |
| 44 | |
| 45 | #include "completer.h" |
| 46 | |
| 47 | /* An enumeration of the various things a user might |
| 48 | attempt to complete for a location. */ |
| 49 | |
| 50 | enum explicit_location_match_type |
| 51 | { |
| 52 | /* The filename of a source file. */ |
| 53 | MATCH_SOURCE, |
| 54 | |
| 55 | /* The name of a function or method. */ |
| 56 | MATCH_FUNCTION, |
| 57 | |
| 58 | /* The name of a label. */ |
| 59 | MATCH_LABEL |
| 60 | }; |
| 61 | |
| 62 | /* Prototypes for local functions. */ |
| 63 | static |
| 64 | char *line_completion_function (const char *text, int matches, |
| 65 | char *line_buffer, |
| 66 | int point); |
| 67 | |
| 68 | /* readline uses the word breaks for two things: |
| 69 | (1) In figuring out where to point the TEXT parameter to the |
| 70 | rl_completion_entry_function. Since we don't use TEXT for much, |
| 71 | it doesn't matter a lot what the word breaks are for this purpose, |
| 72 | but it does affect how much stuff M-? lists. |
| 73 | (2) If one of the matches contains a word break character, readline |
| 74 | will quote it. That's why we switch between |
| 75 | current_language->la_word_break_characters() and |
| 76 | gdb_completer_command_word_break_characters. I'm not sure when |
| 77 | we need this behavior (perhaps for funky characters in C++ |
| 78 | symbols?). */ |
| 79 | |
| 80 | /* Variables which are necessary for fancy command line editing. */ |
| 81 | |
| 82 | /* When completing on command names, we remove '-' from the list of |
| 83 | word break characters, since we use it in command names. If the |
| 84 | readline library sees one in any of the current completion strings, |
| 85 | it thinks that the string needs to be quoted and automatically |
| 86 | supplies a leading quote. */ |
| 87 | static const char gdb_completer_command_word_break_characters[] = |
| 88 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; |
| 89 | |
| 90 | /* When completing on file names, we remove from the list of word |
| 91 | break characters any characters that are commonly used in file |
| 92 | names, such as '-', '+', '~', etc. Otherwise, readline displays |
| 93 | incorrect completion candidates. */ |
| 94 | /* MS-DOS and MS-Windows use colon as part of the drive spec, and most |
| 95 | programs support @foo style response files. */ |
| 96 | static const char gdb_completer_file_name_break_characters[] = |
| 97 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
| 98 | " \t\n*|\"';?><@"; |
| 99 | #else |
| 100 | " \t\n*|\"';:?><"; |
| 101 | #endif |
| 102 | |
| 103 | /* Characters that can be used to quote completion strings. Note that |
| 104 | we can't include '"' because the gdb C parser treats such quoted |
| 105 | sequences as strings. */ |
| 106 | static const char gdb_completer_quote_characters[] = "'"; |
| 107 | \f |
| 108 | /* Accessor for some completer data that may interest other files. */ |
| 109 | |
| 110 | const char * |
| 111 | get_gdb_completer_quote_characters (void) |
| 112 | { |
| 113 | return gdb_completer_quote_characters; |
| 114 | } |
| 115 | |
| 116 | /* Line completion interface function for readline. */ |
| 117 | |
| 118 | char * |
| 119 | readline_line_completion_function (const char *text, int matches) |
| 120 | { |
| 121 | return line_completion_function (text, matches, |
| 122 | rl_line_buffer, rl_point); |
| 123 | } |
| 124 | |
| 125 | /* This can be used for functions which don't want to complete on |
| 126 | symbols but don't want to complete on anything else either. */ |
| 127 | VEC (char_ptr) * |
| 128 | noop_completer (struct cmd_list_element *ignore, |
| 129 | const char *text, const char *prefix) |
| 130 | { |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | /* Complete on filenames. */ |
| 135 | |
| 136 | VEC (char_ptr) * |
| 137 | filename_completer (struct cmd_list_element *ignore, |
| 138 | const char *text, const char *word) |
| 139 | { |
| 140 | int subsequent_name; |
| 141 | VEC (char_ptr) *return_val = NULL; |
| 142 | |
| 143 | subsequent_name = 0; |
| 144 | while (1) |
| 145 | { |
| 146 | char *p, *q; |
| 147 | |
| 148 | p = rl_filename_completion_function (text, subsequent_name); |
| 149 | if (p == NULL) |
| 150 | break; |
| 151 | /* We need to set subsequent_name to a non-zero value before the |
| 152 | continue line below, because otherwise, if the first file |
| 153 | seen by GDB is a backup file whose name ends in a `~', we |
| 154 | will loop indefinitely. */ |
| 155 | subsequent_name = 1; |
| 156 | /* Like emacs, don't complete on old versions. Especially |
| 157 | useful in the "source" command. */ |
| 158 | if (p[strlen (p) - 1] == '~') |
| 159 | { |
| 160 | xfree (p); |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | if (word == text) |
| 165 | /* Return exactly p. */ |
| 166 | q = p; |
| 167 | else if (word > text) |
| 168 | { |
| 169 | /* Return some portion of p. */ |
| 170 | q = (char *) xmalloc (strlen (p) + 5); |
| 171 | strcpy (q, p + (word - text)); |
| 172 | xfree (p); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | /* Return some of TEXT plus p. */ |
| 177 | q = (char *) xmalloc (strlen (p) + (text - word) + 5); |
| 178 | strncpy (q, word, text - word); |
| 179 | q[text - word] = '\0'; |
| 180 | strcat (q, p); |
| 181 | xfree (p); |
| 182 | } |
| 183 | VEC_safe_push (char_ptr, return_val, q); |
| 184 | } |
| 185 | #if 0 |
| 186 | /* There is no way to do this just long enough to affect quote |
| 187 | inserting without also affecting the next completion. This |
| 188 | should be fixed in readline. FIXME. */ |
| 189 | /* Ensure that readline does the right thing |
| 190 | with respect to inserting quotes. */ |
| 191 | rl_completer_word_break_characters = ""; |
| 192 | #endif |
| 193 | return return_val; |
| 194 | } |
| 195 | |
| 196 | /* The corresponding completer_handle_brkchars |
| 197 | implementation. */ |
| 198 | |
| 199 | static void |
| 200 | filename_completer_handle_brkchars (struct cmd_list_element *ignore, |
| 201 | const char *text, const char *word) |
| 202 | { |
| 203 | set_rl_completer_word_break_characters |
| 204 | (gdb_completer_file_name_break_characters); |
| 205 | } |
| 206 | |
| 207 | /* Complete on linespecs, which might be of two possible forms: |
| 208 | |
| 209 | file:line |
| 210 | or |
| 211 | symbol+offset |
| 212 | |
| 213 | This is intended to be used in commands that set breakpoints |
| 214 | etc. */ |
| 215 | |
| 216 | static VEC (char_ptr) * |
| 217 | linespec_location_completer (struct cmd_list_element *ignore, |
| 218 | const char *text, const char *word) |
| 219 | { |
| 220 | int n_syms, n_files, ix; |
| 221 | VEC (char_ptr) *fn_list = NULL; |
| 222 | VEC (char_ptr) *list = NULL; |
| 223 | const char *p; |
| 224 | int quote_found = 0; |
| 225 | int quoted = *text == '\'' || *text == '"'; |
| 226 | int quote_char = '\0'; |
| 227 | const char *colon = NULL; |
| 228 | char *file_to_match = NULL; |
| 229 | const char *symbol_start = text; |
| 230 | const char *orig_text = text; |
| 231 | size_t text_len; |
| 232 | |
| 233 | /* Do we have an unquoted colon, as in "break foo.c:bar"? */ |
| 234 | for (p = text; *p != '\0'; ++p) |
| 235 | { |
| 236 | if (*p == '\\' && p[1] == '\'') |
| 237 | p++; |
| 238 | else if (*p == '\'' || *p == '"') |
| 239 | { |
| 240 | quote_found = *p; |
| 241 | quote_char = *p++; |
| 242 | while (*p != '\0' && *p != quote_found) |
| 243 | { |
| 244 | if (*p == '\\' && p[1] == quote_found) |
| 245 | p++; |
| 246 | p++; |
| 247 | } |
| 248 | |
| 249 | if (*p == quote_found) |
| 250 | quote_found = 0; |
| 251 | else |
| 252 | break; /* Hit the end of text. */ |
| 253 | } |
| 254 | #if HAVE_DOS_BASED_FILE_SYSTEM |
| 255 | /* If we have a DOS-style absolute file name at the beginning of |
| 256 | TEXT, and the colon after the drive letter is the only colon |
| 257 | we found, pretend the colon is not there. */ |
| 258 | else if (p < text + 3 && *p == ':' && p == text + 1 + quoted) |
| 259 | ; |
| 260 | #endif |
| 261 | else if (*p == ':' && !colon) |
| 262 | { |
| 263 | colon = p; |
| 264 | symbol_start = p + 1; |
| 265 | } |
| 266 | else if (strchr (current_language->la_word_break_characters(), *p)) |
| 267 | symbol_start = p + 1; |
| 268 | } |
| 269 | |
| 270 | if (quoted) |
| 271 | text++; |
| 272 | text_len = strlen (text); |
| 273 | |
| 274 | /* Where is the file name? */ |
| 275 | if (colon) |
| 276 | { |
| 277 | char *s; |
| 278 | |
| 279 | file_to_match = (char *) xmalloc (colon - text + 1); |
| 280 | strncpy (file_to_match, text, colon - text); |
| 281 | file_to_match[colon - text] = '\0'; |
| 282 | /* Remove trailing colons and quotes from the file name. */ |
| 283 | for (s = file_to_match + (colon - text); |
| 284 | s > file_to_match; |
| 285 | s--) |
| 286 | if (*s == ':' || *s == quote_char) |
| 287 | *s = '\0'; |
| 288 | } |
| 289 | /* If the text includes a colon, they want completion only on a |
| 290 | symbol name after the colon. Otherwise, we need to complete on |
| 291 | symbols as well as on files. */ |
| 292 | if (colon) |
| 293 | { |
| 294 | list = make_file_symbol_completion_list (symbol_start, word, |
| 295 | file_to_match); |
| 296 | xfree (file_to_match); |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | list = make_symbol_completion_list (symbol_start, word); |
| 301 | /* If text includes characters which cannot appear in a file |
| 302 | name, they cannot be asking for completion on files. */ |
| 303 | if (strcspn (text, |
| 304 | gdb_completer_file_name_break_characters) == text_len) |
| 305 | fn_list = make_source_files_completion_list (text, text); |
| 306 | } |
| 307 | |
| 308 | n_syms = VEC_length (char_ptr, list); |
| 309 | n_files = VEC_length (char_ptr, fn_list); |
| 310 | |
| 311 | /* Catenate fn_list[] onto the end of list[]. */ |
| 312 | if (!n_syms) |
| 313 | { |
| 314 | VEC_free (char_ptr, list); /* Paranoia. */ |
| 315 | list = fn_list; |
| 316 | fn_list = NULL; |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | char *fn; |
| 321 | |
| 322 | for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, fn); ++ix) |
| 323 | VEC_safe_push (char_ptr, list, fn); |
| 324 | VEC_free (char_ptr, fn_list); |
| 325 | } |
| 326 | |
| 327 | if (n_syms && n_files) |
| 328 | { |
| 329 | /* Nothing. */ |
| 330 | } |
| 331 | else if (n_files) |
| 332 | { |
| 333 | char *fn; |
| 334 | |
| 335 | /* If we only have file names as possible completion, we should |
| 336 | bring them in sync with what rl_complete expects. The |
| 337 | problem is that if the user types "break /foo/b TAB", and the |
| 338 | possible completions are "/foo/bar" and "/foo/baz" |
| 339 | rl_complete expects us to return "bar" and "baz", without the |
| 340 | leading directories, as possible completions, because `word' |
| 341 | starts at the "b". But we ignore the value of `word' when we |
| 342 | call make_source_files_completion_list above (because that |
| 343 | would not DTRT when the completion results in both symbols |
| 344 | and file names), so make_source_files_completion_list returns |
| 345 | the full "/foo/bar" and "/foo/baz" strings. This produces |
| 346 | wrong results when, e.g., there's only one possible |
| 347 | completion, because rl_complete will prepend "/foo/" to each |
| 348 | candidate completion. The loop below removes that leading |
| 349 | part. */ |
| 350 | for (ix = 0; VEC_iterate (char_ptr, list, ix, fn); ++ix) |
| 351 | { |
| 352 | memmove (fn, fn + (word - text), |
| 353 | strlen (fn) + 1 - (word - text)); |
| 354 | } |
| 355 | } |
| 356 | else if (!n_syms) |
| 357 | { |
| 358 | /* No completions at all. As the final resort, try completing |
| 359 | on the entire text as a symbol. */ |
| 360 | list = make_symbol_completion_list (orig_text, word); |
| 361 | } |
| 362 | |
| 363 | return list; |
| 364 | } |
| 365 | |
| 366 | /* A helper function to collect explicit location matches for the given |
| 367 | LOCATION, which is attempting to match on WORD. */ |
| 368 | |
| 369 | static VEC (char_ptr) * |
| 370 | collect_explicit_location_matches (struct event_location *location, |
| 371 | enum explicit_location_match_type what, |
| 372 | const char *word) |
| 373 | { |
| 374 | VEC (char_ptr) *matches = NULL; |
| 375 | const struct explicit_location *explicit_loc |
| 376 | = get_explicit_location (location); |
| 377 | |
| 378 | switch (what) |
| 379 | { |
| 380 | case MATCH_SOURCE: |
| 381 | { |
| 382 | const char *text = (explicit_loc->source_filename == NULL |
| 383 | ? "" : explicit_loc->source_filename); |
| 384 | |
| 385 | matches = make_source_files_completion_list (text, word); |
| 386 | } |
| 387 | break; |
| 388 | |
| 389 | case MATCH_FUNCTION: |
| 390 | { |
| 391 | const char *text = (explicit_loc->function_name == NULL |
| 392 | ? "" : explicit_loc->function_name); |
| 393 | |
| 394 | if (explicit_loc->source_filename != NULL) |
| 395 | { |
| 396 | const char *filename = explicit_loc->source_filename; |
| 397 | |
| 398 | matches = make_file_symbol_completion_list (text, word, filename); |
| 399 | } |
| 400 | else |
| 401 | matches = make_symbol_completion_list (text, word); |
| 402 | } |
| 403 | break; |
| 404 | |
| 405 | case MATCH_LABEL: |
| 406 | /* Not supported. */ |
| 407 | break; |
| 408 | |
| 409 | default: |
| 410 | gdb_assert_not_reached ("unhandled explicit_location_match_type"); |
| 411 | } |
| 412 | |
| 413 | return matches; |
| 414 | } |
| 415 | |
| 416 | /* A convenience macro to (safely) back up P to the previous word. */ |
| 417 | |
| 418 | static const char * |
| 419 | backup_text_ptr (const char *p, const char *text) |
| 420 | { |
| 421 | while (p > text && isspace (*p)) |
| 422 | --p; |
| 423 | for (; p > text && !isspace (p[-1]); --p) |
| 424 | ; |
| 425 | |
| 426 | return p; |
| 427 | } |
| 428 | |
| 429 | /* A completer function for explicit locations. This function |
| 430 | completes both options ("-source", "-line", etc) and values. */ |
| 431 | |
| 432 | static VEC (char_ptr) * |
| 433 | explicit_location_completer (struct cmd_list_element *ignore, |
| 434 | struct event_location *location, |
| 435 | const char *text, const char *word) |
| 436 | { |
| 437 | const char *p; |
| 438 | VEC (char_ptr) *matches = NULL; |
| 439 | |
| 440 | /* Find the beginning of the word. This is necessary because |
| 441 | we need to know if we are completing an option name or value. We |
| 442 | don't get the leading '-' from the completer. */ |
| 443 | p = backup_text_ptr (word, text); |
| 444 | |
| 445 | if (*p == '-') |
| 446 | { |
| 447 | /* Completing on option name. */ |
| 448 | static const char *const keywords[] = |
| 449 | { |
| 450 | "source", |
| 451 | "function", |
| 452 | "line", |
| 453 | "label", |
| 454 | NULL |
| 455 | }; |
| 456 | |
| 457 | /* Skip over the '-'. */ |
| 458 | ++p; |
| 459 | |
| 460 | return complete_on_enum (keywords, p, p); |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | /* Completing on value (or unknown). Get the previous word to see what |
| 465 | the user is completing on. */ |
| 466 | size_t len, offset; |
| 467 | const char *new_word, *end; |
| 468 | enum explicit_location_match_type what; |
| 469 | struct explicit_location *explicit_loc |
| 470 | = get_explicit_location (location); |
| 471 | |
| 472 | /* Backup P to the previous word, which should be the option |
| 473 | the user is attempting to complete. */ |
| 474 | offset = word - p; |
| 475 | end = --p; |
| 476 | p = backup_text_ptr (p, text); |
| 477 | len = end - p; |
| 478 | |
| 479 | if (strncmp (p, "-source", len) == 0) |
| 480 | { |
| 481 | what = MATCH_SOURCE; |
| 482 | new_word = explicit_loc->source_filename + offset; |
| 483 | } |
| 484 | else if (strncmp (p, "-function", len) == 0) |
| 485 | { |
| 486 | what = MATCH_FUNCTION; |
| 487 | new_word = explicit_loc->function_name + offset; |
| 488 | } |
| 489 | else if (strncmp (p, "-label", len) == 0) |
| 490 | { |
| 491 | what = MATCH_LABEL; |
| 492 | new_word = explicit_loc->label_name + offset; |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | /* The user isn't completing on any valid option name, |
| 497 | e.g., "break -source foo.c [tab]". */ |
| 498 | return NULL; |
| 499 | } |
| 500 | |
| 501 | /* If the user hasn't entered a search expression, e.g., |
| 502 | "break -function <TAB><TAB>", new_word will be NULL, but |
| 503 | search routines require non-NULL search words. */ |
| 504 | if (new_word == NULL) |
| 505 | new_word = ""; |
| 506 | |
| 507 | /* Now gather matches */ |
| 508 | matches = collect_explicit_location_matches (location, what, new_word); |
| 509 | } |
| 510 | |
| 511 | return matches; |
| 512 | } |
| 513 | |
| 514 | /* A completer for locations. */ |
| 515 | |
| 516 | VEC (char_ptr) * |
| 517 | location_completer (struct cmd_list_element *ignore, |
| 518 | const char *text, const char *word) |
| 519 | { |
| 520 | VEC (char_ptr) *matches = NULL; |
| 521 | const char *copy = text; |
| 522 | |
| 523 | event_location_up location = string_to_explicit_location (©, |
| 524 | current_language, |
| 525 | 1); |
| 526 | if (location != NULL) |
| 527 | matches = explicit_location_completer (ignore, location.get (), |
| 528 | text, word); |
| 529 | else |
| 530 | { |
| 531 | /* This is an address or linespec location. |
| 532 | Right now both of these are handled by the (old) linespec |
| 533 | completer. */ |
| 534 | matches = linespec_location_completer (ignore, text, word); |
| 535 | } |
| 536 | |
| 537 | return matches; |
| 538 | } |
| 539 | |
| 540 | /* Helper for expression_completer which recursively adds field and |
| 541 | method names from TYPE, a struct or union type, to the array |
| 542 | OUTPUT. */ |
| 543 | static void |
| 544 | add_struct_fields (struct type *type, VEC (char_ptr) **output, |
| 545 | char *fieldname, int namelen) |
| 546 | { |
| 547 | int i; |
| 548 | int computed_type_name = 0; |
| 549 | const char *type_name = NULL; |
| 550 | |
| 551 | type = check_typedef (type); |
| 552 | for (i = 0; i < TYPE_NFIELDS (type); ++i) |
| 553 | { |
| 554 | if (i < TYPE_N_BASECLASSES (type)) |
| 555 | add_struct_fields (TYPE_BASECLASS (type, i), |
| 556 | output, fieldname, namelen); |
| 557 | else if (TYPE_FIELD_NAME (type, i)) |
| 558 | { |
| 559 | if (TYPE_FIELD_NAME (type, i)[0] != '\0') |
| 560 | { |
| 561 | if (! strncmp (TYPE_FIELD_NAME (type, i), |
| 562 | fieldname, namelen)) |
| 563 | VEC_safe_push (char_ptr, *output, |
| 564 | xstrdup (TYPE_FIELD_NAME (type, i))); |
| 565 | } |
| 566 | else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION) |
| 567 | { |
| 568 | /* Recurse into anonymous unions. */ |
| 569 | add_struct_fields (TYPE_FIELD_TYPE (type, i), |
| 570 | output, fieldname, namelen); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) |
| 576 | { |
| 577 | const char *name = TYPE_FN_FIELDLIST_NAME (type, i); |
| 578 | |
| 579 | if (name && ! strncmp (name, fieldname, namelen)) |
| 580 | { |
| 581 | if (!computed_type_name) |
| 582 | { |
| 583 | type_name = type_name_no_tag (type); |
| 584 | computed_type_name = 1; |
| 585 | } |
| 586 | /* Omit constructors from the completion list. */ |
| 587 | if (!type_name || strcmp (type_name, name)) |
| 588 | VEC_safe_push (char_ptr, *output, xstrdup (name)); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* Complete on expressions. Often this means completing on symbol |
| 594 | names, but some language parsers also have support for completing |
| 595 | field names. */ |
| 596 | VEC (char_ptr) * |
| 597 | expression_completer (struct cmd_list_element *ignore, |
| 598 | const char *text, const char *word) |
| 599 | { |
| 600 | struct type *type = NULL; |
| 601 | char *fieldname; |
| 602 | enum type_code code = TYPE_CODE_UNDEF; |
| 603 | |
| 604 | /* Perform a tentative parse of the expression, to see whether a |
| 605 | field completion is required. */ |
| 606 | fieldname = NULL; |
| 607 | TRY |
| 608 | { |
| 609 | type = parse_expression_for_completion (text, &fieldname, &code); |
| 610 | } |
| 611 | CATCH (except, RETURN_MASK_ERROR) |
| 612 | { |
| 613 | return NULL; |
| 614 | } |
| 615 | END_CATCH |
| 616 | |
| 617 | if (fieldname && type) |
| 618 | { |
| 619 | for (;;) |
| 620 | { |
| 621 | type = check_typedef (type); |
| 622 | if (TYPE_CODE (type) != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type)) |
| 623 | break; |
| 624 | type = TYPE_TARGET_TYPE (type); |
| 625 | } |
| 626 | |
| 627 | if (TYPE_CODE (type) == TYPE_CODE_UNION |
| 628 | || TYPE_CODE (type) == TYPE_CODE_STRUCT) |
| 629 | { |
| 630 | int flen = strlen (fieldname); |
| 631 | VEC (char_ptr) *result = NULL; |
| 632 | |
| 633 | add_struct_fields (type, &result, fieldname, flen); |
| 634 | xfree (fieldname); |
| 635 | return result; |
| 636 | } |
| 637 | } |
| 638 | else if (fieldname && code != TYPE_CODE_UNDEF) |
| 639 | { |
| 640 | VEC (char_ptr) *result; |
| 641 | struct cleanup *cleanup = make_cleanup (xfree, fieldname); |
| 642 | |
| 643 | result = make_symbol_completion_type (fieldname, fieldname, code); |
| 644 | do_cleanups (cleanup); |
| 645 | return result; |
| 646 | } |
| 647 | xfree (fieldname); |
| 648 | |
| 649 | /* Not ideal but it is what we used to do before... */ |
| 650 | return linespec_location_completer (ignore, text, word); |
| 651 | } |
| 652 | |
| 653 | /* See definition in completer.h. */ |
| 654 | |
| 655 | void |
| 656 | set_rl_completer_word_break_characters (const char *break_chars) |
| 657 | { |
| 658 | rl_completer_word_break_characters = (char *) break_chars; |
| 659 | } |
| 660 | |
| 661 | /* See definition in completer.h. */ |
| 662 | |
| 663 | void |
| 664 | set_gdb_completion_word_break_characters (completer_ftype *fn) |
| 665 | { |
| 666 | const char *break_chars; |
| 667 | |
| 668 | /* So far we are only interested in differentiating filename |
| 669 | completers from everything else. */ |
| 670 | if (fn == filename_completer) |
| 671 | break_chars = gdb_completer_file_name_break_characters; |
| 672 | else |
| 673 | break_chars = gdb_completer_command_word_break_characters; |
| 674 | |
| 675 | set_rl_completer_word_break_characters (break_chars); |
| 676 | } |
| 677 | |
| 678 | /* Complete on symbols. */ |
| 679 | |
| 680 | VEC (char_ptr) * |
| 681 | symbol_completer (struct cmd_list_element *ignore, |
| 682 | const char *text, const char *word) |
| 683 | { |
| 684 | return make_symbol_completion_list (text, word); |
| 685 | } |
| 686 | |
| 687 | /* Here are some useful test cases for completion. FIXME: These |
| 688 | should be put in the test suite. They should be tested with both |
| 689 | M-? and TAB. |
| 690 | |
| 691 | "show output-" "radix" |
| 692 | "show output" "-radix" |
| 693 | "p" ambiguous (commands starting with p--path, print, printf, etc.) |
| 694 | "p " ambiguous (all symbols) |
| 695 | "info t foo" no completions |
| 696 | "info t " no completions |
| 697 | "info t" ambiguous ("info target", "info terminal", etc.) |
| 698 | "info ajksdlfk" no completions |
| 699 | "info ajksdlfk " no completions |
| 700 | "info" " " |
| 701 | "info " ambiguous (all info commands) |
| 702 | "p \"a" no completions (string constant) |
| 703 | "p 'a" ambiguous (all symbols starting with a) |
| 704 | "p b-a" ambiguous (all symbols starting with a) |
| 705 | "p b-" ambiguous (all symbols) |
| 706 | "file Make" "file" (word break hard to screw up here) |
| 707 | "file ../gdb.stabs/we" "ird" (needs to not break word at slash) |
| 708 | */ |
| 709 | |
| 710 | typedef enum |
| 711 | { |
| 712 | handle_brkchars, |
| 713 | handle_completions, |
| 714 | handle_help |
| 715 | } |
| 716 | complete_line_internal_reason; |
| 717 | |
| 718 | /* Helper for complete_line_internal to simplify it. */ |
| 719 | |
| 720 | static VEC (char_ptr) * |
| 721 | complete_line_internal_normal_command (const char *command, const char *word, |
| 722 | const char *cmd_args, |
| 723 | complete_line_internal_reason reason, |
| 724 | struct cmd_list_element *c) |
| 725 | { |
| 726 | const char *p = cmd_args; |
| 727 | |
| 728 | if (c->completer == filename_completer) |
| 729 | { |
| 730 | /* Many commands which want to complete on file names accept |
| 731 | several file names, as in "run foo bar >>baz". So we don't |
| 732 | want to complete the entire text after the command, just the |
| 733 | last word. To this end, we need to find the beginning of the |
| 734 | file name by starting at `word' and going backwards. */ |
| 735 | for (p = word; |
| 736 | p > command |
| 737 | && strchr (gdb_completer_file_name_break_characters, |
| 738 | p[-1]) == NULL; |
| 739 | p--) |
| 740 | ; |
| 741 | } |
| 742 | |
| 743 | if (reason == handle_brkchars) |
| 744 | { |
| 745 | completer_handle_brkchars_ftype *brkchars_fn; |
| 746 | |
| 747 | if (c->completer_handle_brkchars != NULL) |
| 748 | brkchars_fn = c->completer_handle_brkchars; |
| 749 | else |
| 750 | { |
| 751 | brkchars_fn |
| 752 | = (completer_handle_brkchars_func_for_completer |
| 753 | (c->completer)); |
| 754 | } |
| 755 | |
| 756 | brkchars_fn (c, p, word); |
| 757 | } |
| 758 | |
| 759 | if (reason != handle_brkchars && c->completer != NULL) |
| 760 | return (*c->completer) (c, p, word); |
| 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | /* Internal function used to handle completions. |
| 765 | |
| 766 | |
| 767 | TEXT is the caller's idea of the "word" we are looking at. |
| 768 | |
| 769 | LINE_BUFFER is available to be looked at; it contains the entire |
| 770 | text of the line. POINT is the offset in that line of the cursor. |
| 771 | You should pretend that the line ends at POINT. |
| 772 | |
| 773 | REASON is of type complete_line_internal_reason. |
| 774 | |
| 775 | If REASON is handle_brkchars: |
| 776 | Preliminary phase, called by gdb_completion_word_break_characters |
| 777 | function, is used to determine the correct set of chars that are |
| 778 | word delimiters depending on the current command in line_buffer. |
| 779 | No completion list should be generated; the return value should be |
| 780 | NULL. This is checked by an assertion in that function. |
| 781 | |
| 782 | If REASON is handle_completions: |
| 783 | Main phase, called by complete_line function, is used to get the list |
| 784 | of posible completions. |
| 785 | |
| 786 | If REASON is handle_help: |
| 787 | Special case when completing a 'help' command. In this case, |
| 788 | once sub-command completions are exhausted, we simply return NULL. |
| 789 | */ |
| 790 | |
| 791 | static VEC (char_ptr) * |
| 792 | complete_line_internal (const char *text, |
| 793 | const char *line_buffer, int point, |
| 794 | complete_line_internal_reason reason) |
| 795 | { |
| 796 | VEC (char_ptr) *list = NULL; |
| 797 | char *tmp_command; |
| 798 | const char *p; |
| 799 | int ignore_help_classes; |
| 800 | /* Pointer within tmp_command which corresponds to text. */ |
| 801 | char *word; |
| 802 | struct cmd_list_element *c, *result_list; |
| 803 | |
| 804 | /* Choose the default set of word break characters to break |
| 805 | completions. If we later find out that we are doing completions |
| 806 | on command strings (as opposed to strings supplied by the |
| 807 | individual command completer functions, which can be any string) |
| 808 | then we will switch to the special word break set for command |
| 809 | strings, which leaves out the '-' character used in some |
| 810 | commands. */ |
| 811 | set_rl_completer_word_break_characters |
| 812 | (current_language->la_word_break_characters()); |
| 813 | |
| 814 | /* Decide whether to complete on a list of gdb commands or on |
| 815 | symbols. */ |
| 816 | tmp_command = (char *) alloca (point + 1); |
| 817 | p = tmp_command; |
| 818 | |
| 819 | /* The help command should complete help aliases. */ |
| 820 | ignore_help_classes = reason != handle_help; |
| 821 | |
| 822 | strncpy (tmp_command, line_buffer, point); |
| 823 | tmp_command[point] = '\0'; |
| 824 | /* Since text always contains some number of characters leading up |
| 825 | to point, we can find the equivalent position in tmp_command |
| 826 | by subtracting that many characters from the end of tmp_command. */ |
| 827 | word = tmp_command + point - strlen (text); |
| 828 | |
| 829 | if (point == 0) |
| 830 | { |
| 831 | /* An empty line we want to consider ambiguous; that is, it |
| 832 | could be any command. */ |
| 833 | c = CMD_LIST_AMBIGUOUS; |
| 834 | result_list = 0; |
| 835 | } |
| 836 | else |
| 837 | { |
| 838 | c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes); |
| 839 | } |
| 840 | |
| 841 | /* Move p up to the next interesting thing. */ |
| 842 | while (*p == ' ' || *p == '\t') |
| 843 | { |
| 844 | p++; |
| 845 | } |
| 846 | |
| 847 | if (!c) |
| 848 | { |
| 849 | /* It is an unrecognized command. So there are no |
| 850 | possible completions. */ |
| 851 | list = NULL; |
| 852 | } |
| 853 | else if (c == CMD_LIST_AMBIGUOUS) |
| 854 | { |
| 855 | const char *q; |
| 856 | |
| 857 | /* lookup_cmd_1 advances p up to the first ambiguous thing, but |
| 858 | doesn't advance over that thing itself. Do so now. */ |
| 859 | q = p; |
| 860 | while (*q && (isalnum (*q) || *q == '-' || *q == '_')) |
| 861 | ++q; |
| 862 | if (q != tmp_command + point) |
| 863 | { |
| 864 | /* There is something beyond the ambiguous |
| 865 | command, so there are no possible completions. For |
| 866 | example, "info t " or "info t foo" does not complete |
| 867 | to anything, because "info t" can be "info target" or |
| 868 | "info terminal". */ |
| 869 | list = NULL; |
| 870 | } |
| 871 | else |
| 872 | { |
| 873 | /* We're trying to complete on the command which was ambiguous. |
| 874 | This we can deal with. */ |
| 875 | if (result_list) |
| 876 | { |
| 877 | if (reason != handle_brkchars) |
| 878 | list = complete_on_cmdlist (*result_list->prefixlist, p, |
| 879 | word, ignore_help_classes); |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | if (reason != handle_brkchars) |
| 884 | list = complete_on_cmdlist (cmdlist, p, word, |
| 885 | ignore_help_classes); |
| 886 | } |
| 887 | /* Ensure that readline does the right thing with respect to |
| 888 | inserting quotes. */ |
| 889 | set_rl_completer_word_break_characters |
| 890 | (gdb_completer_command_word_break_characters); |
| 891 | } |
| 892 | } |
| 893 | else |
| 894 | { |
| 895 | /* We've recognized a full command. */ |
| 896 | |
| 897 | if (p == tmp_command + point) |
| 898 | { |
| 899 | /* There is no non-whitespace in the line beyond the |
| 900 | command. */ |
| 901 | |
| 902 | if (p[-1] == ' ' || p[-1] == '\t') |
| 903 | { |
| 904 | /* The command is followed by whitespace; we need to |
| 905 | complete on whatever comes after command. */ |
| 906 | if (c->prefixlist) |
| 907 | { |
| 908 | /* It is a prefix command; what comes after it is |
| 909 | a subcommand (e.g. "info "). */ |
| 910 | if (reason != handle_brkchars) |
| 911 | list = complete_on_cmdlist (*c->prefixlist, p, word, |
| 912 | ignore_help_classes); |
| 913 | |
| 914 | /* Ensure that readline does the right thing |
| 915 | with respect to inserting quotes. */ |
| 916 | set_rl_completer_word_break_characters |
| 917 | (gdb_completer_command_word_break_characters); |
| 918 | } |
| 919 | else if (reason == handle_help) |
| 920 | list = NULL; |
| 921 | else if (c->enums) |
| 922 | { |
| 923 | if (reason != handle_brkchars) |
| 924 | list = complete_on_enum (c->enums, p, word); |
| 925 | set_rl_completer_word_break_characters |
| 926 | (gdb_completer_command_word_break_characters); |
| 927 | } |
| 928 | else |
| 929 | { |
| 930 | /* It is a normal command; what comes after it is |
| 931 | completed by the command's completer function. */ |
| 932 | list = complete_line_internal_normal_command (tmp_command, |
| 933 | word, p, |
| 934 | reason, c); |
| 935 | } |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | /* The command is not followed by whitespace; we need to |
| 940 | complete on the command itself, e.g. "p" which is a |
| 941 | command itself but also can complete to "print", "ptype" |
| 942 | etc. */ |
| 943 | const char *q; |
| 944 | |
| 945 | /* Find the command we are completing on. */ |
| 946 | q = p; |
| 947 | while (q > tmp_command) |
| 948 | { |
| 949 | if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') |
| 950 | --q; |
| 951 | else |
| 952 | break; |
| 953 | } |
| 954 | |
| 955 | if (reason != handle_brkchars) |
| 956 | list = complete_on_cmdlist (result_list, q, word, |
| 957 | ignore_help_classes); |
| 958 | |
| 959 | /* Ensure that readline does the right thing |
| 960 | with respect to inserting quotes. */ |
| 961 | set_rl_completer_word_break_characters |
| 962 | (gdb_completer_command_word_break_characters); |
| 963 | } |
| 964 | } |
| 965 | else if (reason == handle_help) |
| 966 | list = NULL; |
| 967 | else |
| 968 | { |
| 969 | /* There is non-whitespace beyond the command. */ |
| 970 | |
| 971 | if (c->prefixlist && !c->allow_unknown) |
| 972 | { |
| 973 | /* It is an unrecognized subcommand of a prefix command, |
| 974 | e.g. "info adsfkdj". */ |
| 975 | list = NULL; |
| 976 | } |
| 977 | else if (c->enums) |
| 978 | { |
| 979 | if (reason != handle_brkchars) |
| 980 | list = complete_on_enum (c->enums, p, word); |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | /* It is a normal command. */ |
| 985 | list = complete_line_internal_normal_command (tmp_command, |
| 986 | word, p, |
| 987 | reason, c); |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | return list; |
| 993 | } |
| 994 | |
| 995 | /* See completer.h. */ |
| 996 | |
| 997 | int max_completions = 200; |
| 998 | |
| 999 | /* See completer.h. */ |
| 1000 | |
| 1001 | completion_tracker_t |
| 1002 | new_completion_tracker (void) |
| 1003 | { |
| 1004 | if (max_completions <= 0) |
| 1005 | return NULL; |
| 1006 | |
| 1007 | return htab_create_alloc (max_completions, |
| 1008 | htab_hash_string, (htab_eq) streq, |
| 1009 | NULL, xcalloc, xfree); |
| 1010 | } |
| 1011 | |
| 1012 | /* Cleanup routine to free a completion tracker and reset the pointer |
| 1013 | to NULL. */ |
| 1014 | |
| 1015 | static void |
| 1016 | free_completion_tracker (void *p) |
| 1017 | { |
| 1018 | completion_tracker_t *tracker_ptr = (completion_tracker_t *) p; |
| 1019 | |
| 1020 | htab_delete (*tracker_ptr); |
| 1021 | *tracker_ptr = NULL; |
| 1022 | } |
| 1023 | |
| 1024 | /* See completer.h. */ |
| 1025 | |
| 1026 | struct cleanup * |
| 1027 | make_cleanup_free_completion_tracker (completion_tracker_t *tracker_ptr) |
| 1028 | { |
| 1029 | if (*tracker_ptr == NULL) |
| 1030 | return make_cleanup (null_cleanup, NULL); |
| 1031 | |
| 1032 | return make_cleanup (free_completion_tracker, tracker_ptr); |
| 1033 | } |
| 1034 | |
| 1035 | /* See completer.h. */ |
| 1036 | |
| 1037 | enum maybe_add_completion_enum |
| 1038 | maybe_add_completion (completion_tracker_t tracker, char *name) |
| 1039 | { |
| 1040 | void **slot; |
| 1041 | |
| 1042 | if (max_completions < 0) |
| 1043 | return MAYBE_ADD_COMPLETION_OK; |
| 1044 | if (max_completions == 0) |
| 1045 | return MAYBE_ADD_COMPLETION_MAX_REACHED; |
| 1046 | |
| 1047 | gdb_assert (tracker != NULL); |
| 1048 | |
| 1049 | if (htab_elements (tracker) >= max_completions) |
| 1050 | return MAYBE_ADD_COMPLETION_MAX_REACHED; |
| 1051 | |
| 1052 | slot = htab_find_slot (tracker, name, INSERT); |
| 1053 | |
| 1054 | if (*slot != HTAB_EMPTY_ENTRY) |
| 1055 | return MAYBE_ADD_COMPLETION_DUPLICATE; |
| 1056 | |
| 1057 | *slot = name; |
| 1058 | |
| 1059 | return (htab_elements (tracker) < max_completions |
| 1060 | ? MAYBE_ADD_COMPLETION_OK |
| 1061 | : MAYBE_ADD_COMPLETION_OK_MAX_REACHED); |
| 1062 | } |
| 1063 | |
| 1064 | void |
| 1065 | throw_max_completions_reached_error (void) |
| 1066 | { |
| 1067 | throw_error (MAX_COMPLETIONS_REACHED_ERROR, _("Max completions reached.")); |
| 1068 | } |
| 1069 | |
| 1070 | /* Generate completions all at once. Returns a vector of unique strings |
| 1071 | allocated with xmalloc. Returns NULL if there are no completions |
| 1072 | or if max_completions is 0. If max_completions is non-negative, this will |
| 1073 | return at most max_completions strings. |
| 1074 | |
| 1075 | TEXT is the caller's idea of the "word" we are looking at. |
| 1076 | |
| 1077 | LINE_BUFFER is available to be looked at; it contains the entire |
| 1078 | text of the line. |
| 1079 | |
| 1080 | POINT is the offset in that line of the cursor. You |
| 1081 | should pretend that the line ends at POINT. */ |
| 1082 | |
| 1083 | VEC (char_ptr) * |
| 1084 | complete_line (const char *text, const char *line_buffer, int point) |
| 1085 | { |
| 1086 | VEC (char_ptr) *list; |
| 1087 | VEC (char_ptr) *result = NULL; |
| 1088 | struct cleanup *cleanups; |
| 1089 | completion_tracker_t tracker; |
| 1090 | char *candidate; |
| 1091 | int ix, max_reached; |
| 1092 | |
| 1093 | if (max_completions == 0) |
| 1094 | return NULL; |
| 1095 | list = complete_line_internal (text, line_buffer, point, |
| 1096 | handle_completions); |
| 1097 | if (max_completions < 0) |
| 1098 | return list; |
| 1099 | |
| 1100 | tracker = new_completion_tracker (); |
| 1101 | cleanups = make_cleanup_free_completion_tracker (&tracker); |
| 1102 | make_cleanup_free_char_ptr_vec (list); |
| 1103 | |
| 1104 | /* Do a final test for too many completions. Individual completers may |
| 1105 | do some of this, but are not required to. Duplicates are also removed |
| 1106 | here. Otherwise the user is left scratching his/her head: readline and |
| 1107 | complete_command will remove duplicates, and if removal of duplicates |
| 1108 | there brings the total under max_completions the user may think gdb quit |
| 1109 | searching too early. */ |
| 1110 | |
| 1111 | for (ix = 0, max_reached = 0; |
| 1112 | !max_reached && VEC_iterate (char_ptr, list, ix, candidate); |
| 1113 | ++ix) |
| 1114 | { |
| 1115 | enum maybe_add_completion_enum add_status; |
| 1116 | |
| 1117 | add_status = maybe_add_completion (tracker, candidate); |
| 1118 | |
| 1119 | switch (add_status) |
| 1120 | { |
| 1121 | case MAYBE_ADD_COMPLETION_OK: |
| 1122 | VEC_safe_push (char_ptr, result, xstrdup (candidate)); |
| 1123 | break; |
| 1124 | case MAYBE_ADD_COMPLETION_OK_MAX_REACHED: |
| 1125 | VEC_safe_push (char_ptr, result, xstrdup (candidate)); |
| 1126 | max_reached = 1; |
| 1127 | break; |
| 1128 | case MAYBE_ADD_COMPLETION_MAX_REACHED: |
| 1129 | gdb_assert_not_reached ("more than max completions reached"); |
| 1130 | case MAYBE_ADD_COMPLETION_DUPLICATE: |
| 1131 | break; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | do_cleanups (cleanups); |
| 1136 | |
| 1137 | return result; |
| 1138 | } |
| 1139 | |
| 1140 | /* Complete on command names. Used by "help". */ |
| 1141 | |
| 1142 | VEC (char_ptr) * |
| 1143 | command_completer (struct cmd_list_element *ignore, |
| 1144 | const char *text, const char *word) |
| 1145 | { |
| 1146 | return complete_line_internal (word, text, |
| 1147 | strlen (text), handle_help); |
| 1148 | } |
| 1149 | |
| 1150 | /* The corresponding completer_handle_brkchars implementation. */ |
| 1151 | |
| 1152 | static void |
| 1153 | command_completer_handle_brkchars (struct cmd_list_element *ignore, |
| 1154 | const char *text, const char *word) |
| 1155 | { |
| 1156 | set_rl_completer_word_break_characters |
| 1157 | (gdb_completer_command_word_break_characters); |
| 1158 | } |
| 1159 | |
| 1160 | /* Complete on signals. */ |
| 1161 | |
| 1162 | VEC (char_ptr) * |
| 1163 | signal_completer (struct cmd_list_element *ignore, |
| 1164 | const char *text, const char *word) |
| 1165 | { |
| 1166 | VEC (char_ptr) *return_val = NULL; |
| 1167 | size_t len = strlen (word); |
| 1168 | int signum; |
| 1169 | const char *signame; |
| 1170 | |
| 1171 | for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum) |
| 1172 | { |
| 1173 | /* Can't handle this, so skip it. */ |
| 1174 | if (signum == GDB_SIGNAL_0) |
| 1175 | continue; |
| 1176 | |
| 1177 | signame = gdb_signal_to_name ((enum gdb_signal) signum); |
| 1178 | |
| 1179 | /* Ignore the unknown signal case. */ |
| 1180 | if (!signame || strcmp (signame, "?") == 0) |
| 1181 | continue; |
| 1182 | |
| 1183 | if (strncasecmp (signame, word, len) == 0) |
| 1184 | VEC_safe_push (char_ptr, return_val, xstrdup (signame)); |
| 1185 | } |
| 1186 | |
| 1187 | return return_val; |
| 1188 | } |
| 1189 | |
| 1190 | /* Bit-flags for selecting what the register and/or register-group |
| 1191 | completer should complete on. */ |
| 1192 | |
| 1193 | enum reg_completer_target |
| 1194 | { |
| 1195 | complete_register_names = 0x1, |
| 1196 | complete_reggroup_names = 0x2 |
| 1197 | }; |
| 1198 | DEF_ENUM_FLAGS_TYPE (enum reg_completer_target, reg_completer_targets); |
| 1199 | |
| 1200 | /* Complete register names and/or reggroup names based on the value passed |
| 1201 | in TARGETS. At least one bit in TARGETS must be set. */ |
| 1202 | |
| 1203 | static VEC (char_ptr) * |
| 1204 | reg_or_group_completer_1 (struct cmd_list_element *ignore, |
| 1205 | const char *text, const char *word, |
| 1206 | reg_completer_targets targets) |
| 1207 | { |
| 1208 | VEC (char_ptr) *result = NULL; |
| 1209 | size_t len = strlen (word); |
| 1210 | struct gdbarch *gdbarch; |
| 1211 | const char *name; |
| 1212 | |
| 1213 | gdb_assert ((targets & (complete_register_names |
| 1214 | | complete_reggroup_names)) != 0); |
| 1215 | gdbarch = get_current_arch (); |
| 1216 | |
| 1217 | if ((targets & complete_register_names) != 0) |
| 1218 | { |
| 1219 | int i; |
| 1220 | |
| 1221 | for (i = 0; |
| 1222 | (name = user_reg_map_regnum_to_name (gdbarch, i)) != NULL; |
| 1223 | i++) |
| 1224 | { |
| 1225 | if (*name != '\0' && strncmp (word, name, len) == 0) |
| 1226 | VEC_safe_push (char_ptr, result, xstrdup (name)); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | if ((targets & complete_reggroup_names) != 0) |
| 1231 | { |
| 1232 | struct reggroup *group; |
| 1233 | |
| 1234 | for (group = reggroup_next (gdbarch, NULL); |
| 1235 | group != NULL; |
| 1236 | group = reggroup_next (gdbarch, group)) |
| 1237 | { |
| 1238 | name = reggroup_name (group); |
| 1239 | if (strncmp (word, name, len) == 0) |
| 1240 | VEC_safe_push (char_ptr, result, xstrdup (name)); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | return result; |
| 1245 | } |
| 1246 | |
| 1247 | /* Perform completion on register and reggroup names. */ |
| 1248 | |
| 1249 | VEC (char_ptr) * |
| 1250 | reg_or_group_completer (struct cmd_list_element *ignore, |
| 1251 | const char *text, const char *word) |
| 1252 | { |
| 1253 | return reg_or_group_completer_1 (ignore, text, word, |
| 1254 | (complete_register_names |
| 1255 | | complete_reggroup_names)); |
| 1256 | } |
| 1257 | |
| 1258 | /* Perform completion on reggroup names. */ |
| 1259 | |
| 1260 | VEC (char_ptr) * |
| 1261 | reggroup_completer (struct cmd_list_element *ignore, |
| 1262 | const char *text, const char *word) |
| 1263 | { |
| 1264 | return reg_or_group_completer_1 (ignore, text, word, |
| 1265 | complete_reggroup_names); |
| 1266 | } |
| 1267 | |
| 1268 | /* The default completer_handle_brkchars implementation. */ |
| 1269 | |
| 1270 | static void |
| 1271 | default_completer_handle_brkchars (struct cmd_list_element *ignore, |
| 1272 | const char *text, const char *word) |
| 1273 | { |
| 1274 | set_rl_completer_word_break_characters |
| 1275 | (current_language->la_word_break_characters ()); |
| 1276 | } |
| 1277 | |
| 1278 | /* See definition in completer.h. */ |
| 1279 | |
| 1280 | completer_handle_brkchars_ftype * |
| 1281 | completer_handle_brkchars_func_for_completer (completer_ftype *fn) |
| 1282 | { |
| 1283 | if (fn == filename_completer) |
| 1284 | return filename_completer_handle_brkchars; |
| 1285 | |
| 1286 | if (fn == command_completer) |
| 1287 | return command_completer_handle_brkchars; |
| 1288 | |
| 1289 | return default_completer_handle_brkchars; |
| 1290 | } |
| 1291 | |
| 1292 | /* Get the list of chars that are considered as word breaks |
| 1293 | for the current command. */ |
| 1294 | |
| 1295 | char * |
| 1296 | gdb_completion_word_break_characters (void) |
| 1297 | { |
| 1298 | VEC (char_ptr) *list; |
| 1299 | |
| 1300 | list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point, |
| 1301 | handle_brkchars); |
| 1302 | gdb_assert (list == NULL); |
| 1303 | return rl_completer_word_break_characters; |
| 1304 | } |
| 1305 | |
| 1306 | /* Generate completions one by one for the completer. Each time we |
| 1307 | are called return another potential completion to the caller. |
| 1308 | line_completion just completes on commands or passes the buck to |
| 1309 | the command's completer function, the stuff specific to symbol |
| 1310 | completion is in make_symbol_completion_list. |
| 1311 | |
| 1312 | TEXT is the caller's idea of the "word" we are looking at. |
| 1313 | |
| 1314 | MATCHES is the number of matches that have currently been collected |
| 1315 | from calling this completion function. When zero, then we need to |
| 1316 | initialize, otherwise the initialization has already taken place |
| 1317 | and we can just return the next potential completion string. |
| 1318 | |
| 1319 | LINE_BUFFER is available to be looked at; it contains the entire |
| 1320 | text of the line. POINT is the offset in that line of the cursor. |
| 1321 | You should pretend that the line ends at POINT. |
| 1322 | |
| 1323 | Returns NULL if there are no more completions, else a pointer to a |
| 1324 | string which is a possible completion, it is the caller's |
| 1325 | responsibility to free the string. */ |
| 1326 | |
| 1327 | static char * |
| 1328 | line_completion_function (const char *text, int matches, |
| 1329 | char *line_buffer, int point) |
| 1330 | { |
| 1331 | static VEC (char_ptr) *list = NULL; /* Cache of completions. */ |
| 1332 | static int index; /* Next cached completion. */ |
| 1333 | char *output = NULL; |
| 1334 | |
| 1335 | if (matches == 0) |
| 1336 | { |
| 1337 | /* The caller is beginning to accumulate a new set of |
| 1338 | completions, so we need to find all of them now, and cache |
| 1339 | them for returning one at a time on future calls. */ |
| 1340 | |
| 1341 | if (list) |
| 1342 | { |
| 1343 | /* Free the storage used by LIST, but not by the strings |
| 1344 | inside. This is because rl_complete_internal () frees |
| 1345 | the strings. As complete_line may abort by calling |
| 1346 | `error' clear LIST now. */ |
| 1347 | VEC_free (char_ptr, list); |
| 1348 | } |
| 1349 | index = 0; |
| 1350 | list = complete_line (text, line_buffer, point); |
| 1351 | } |
| 1352 | |
| 1353 | /* If we found a list of potential completions during initialization |
| 1354 | then dole them out one at a time. After returning the last one, |
| 1355 | return NULL (and continue to do so) each time we are called after |
| 1356 | that, until a new list is available. */ |
| 1357 | |
| 1358 | if (list) |
| 1359 | { |
| 1360 | if (index < VEC_length (char_ptr, list)) |
| 1361 | { |
| 1362 | output = VEC_index (char_ptr, list, index); |
| 1363 | index++; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | #if 0 |
| 1368 | /* Can't do this because readline hasn't yet checked the word breaks |
| 1369 | for figuring out whether to insert a quote. */ |
| 1370 | if (output == NULL) |
| 1371 | /* Make sure the word break characters are set back to normal for |
| 1372 | the next time that readline tries to complete something. */ |
| 1373 | rl_completer_word_break_characters = |
| 1374 | current_language->la_word_break_characters(); |
| 1375 | #endif |
| 1376 | |
| 1377 | return (output); |
| 1378 | } |
| 1379 | |
| 1380 | /* Skip over the possibly quoted word STR (as defined by the quote |
| 1381 | characters QUOTECHARS and the word break characters BREAKCHARS). |
| 1382 | Returns pointer to the location after the "word". If either |
| 1383 | QUOTECHARS or BREAKCHARS is NULL, use the same values used by the |
| 1384 | completer. */ |
| 1385 | |
| 1386 | const char * |
| 1387 | skip_quoted_chars (const char *str, const char *quotechars, |
| 1388 | const char *breakchars) |
| 1389 | { |
| 1390 | char quote_char = '\0'; |
| 1391 | const char *scan; |
| 1392 | |
| 1393 | if (quotechars == NULL) |
| 1394 | quotechars = gdb_completer_quote_characters; |
| 1395 | |
| 1396 | if (breakchars == NULL) |
| 1397 | breakchars = current_language->la_word_break_characters(); |
| 1398 | |
| 1399 | for (scan = str; *scan != '\0'; scan++) |
| 1400 | { |
| 1401 | if (quote_char != '\0') |
| 1402 | { |
| 1403 | /* Ignore everything until the matching close quote char. */ |
| 1404 | if (*scan == quote_char) |
| 1405 | { |
| 1406 | /* Found matching close quote. */ |
| 1407 | scan++; |
| 1408 | break; |
| 1409 | } |
| 1410 | } |
| 1411 | else if (strchr (quotechars, *scan)) |
| 1412 | { |
| 1413 | /* Found start of a quoted string. */ |
| 1414 | quote_char = *scan; |
| 1415 | } |
| 1416 | else if (strchr (breakchars, *scan)) |
| 1417 | { |
| 1418 | break; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | return (scan); |
| 1423 | } |
| 1424 | |
| 1425 | /* Skip over the possibly quoted word STR (as defined by the quote |
| 1426 | characters and word break characters used by the completer). |
| 1427 | Returns pointer to the location after the "word". */ |
| 1428 | |
| 1429 | const char * |
| 1430 | skip_quoted (const char *str) |
| 1431 | { |
| 1432 | return skip_quoted_chars (str, NULL, NULL); |
| 1433 | } |
| 1434 | |
| 1435 | /* Return a message indicating that the maximum number of completions |
| 1436 | has been reached and that there may be more. */ |
| 1437 | |
| 1438 | const char * |
| 1439 | get_max_completions_reached_message (void) |
| 1440 | { |
| 1441 | return _("*** List may be truncated, max-completions reached. ***"); |
| 1442 | } |
| 1443 | \f |
| 1444 | /* GDB replacement for rl_display_match_list. |
| 1445 | Readline doesn't provide a clean interface for TUI(curses). |
| 1446 | A hack previously used was to send readline's rl_outstream through a pipe |
| 1447 | and read it from the event loop. Bleah. IWBN if readline abstracted |
| 1448 | away all the necessary bits, and this is what this code does. It |
| 1449 | replicates the parts of readline we need and then adds an abstraction |
| 1450 | layer, currently implemented as struct match_list_displayer, so that both |
| 1451 | CLI and TUI can use it. We copy all this readline code to minimize |
| 1452 | GDB-specific mods to readline. Once this code performs as desired then |
| 1453 | we can submit it to the readline maintainers. |
| 1454 | |
| 1455 | N.B. A lot of the code is the way it is in order to minimize differences |
| 1456 | from readline's copy. */ |
| 1457 | |
| 1458 | /* Not supported here. */ |
| 1459 | #undef VISIBLE_STATS |
| 1460 | |
| 1461 | #if defined (HANDLE_MULTIBYTE) |
| 1462 | #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2) |
| 1463 | #define MB_NULLWCH(x) ((x) == 0) |
| 1464 | #endif |
| 1465 | |
| 1466 | #define ELLIPSIS_LEN 3 |
| 1467 | |
| 1468 | /* gdb version of readline/complete.c:get_y_or_n. |
| 1469 | 'y' -> returns 1, and 'n' -> returns 0. |
| 1470 | Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over. |
| 1471 | If FOR_PAGER is non-zero, then also supported are: |
| 1472 | NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */ |
| 1473 | |
| 1474 | static int |
| 1475 | gdb_get_y_or_n (int for_pager, const struct match_list_displayer *displayer) |
| 1476 | { |
| 1477 | int c; |
| 1478 | |
| 1479 | for (;;) |
| 1480 | { |
| 1481 | RL_SETSTATE (RL_STATE_MOREINPUT); |
| 1482 | c = displayer->read_key (displayer); |
| 1483 | RL_UNSETSTATE (RL_STATE_MOREINPUT); |
| 1484 | |
| 1485 | if (c == 'y' || c == 'Y' || c == ' ') |
| 1486 | return 1; |
| 1487 | if (c == 'n' || c == 'N' || c == RUBOUT) |
| 1488 | return 0; |
| 1489 | if (c == ABORT_CHAR || c < 0) |
| 1490 | { |
| 1491 | /* Readline doesn't erase_entire_line here, but without it the |
| 1492 | --More-- prompt isn't erased and neither is the text entered |
| 1493 | thus far redisplayed. */ |
| 1494 | displayer->erase_entire_line (displayer); |
| 1495 | /* Note: The arguments to rl_abort are ignored. */ |
| 1496 | rl_abort (0, 0); |
| 1497 | } |
| 1498 | if (for_pager && (c == NEWLINE || c == RETURN)) |
| 1499 | return 2; |
| 1500 | if (for_pager && (c == 'q' || c == 'Q')) |
| 1501 | return 0; |
| 1502 | displayer->beep (displayer); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | /* Pager function for tab-completion. |
| 1507 | This is based on readline/complete.c:_rl_internal_pager. |
| 1508 | LINES is the number of lines of output displayed thus far. |
| 1509 | Returns: |
| 1510 | -1 -> user pressed 'n' or equivalent, |
| 1511 | 0 -> user pressed 'y' or equivalent, |
| 1512 | N -> user pressed NEWLINE or equivalent and N is LINES - 1. */ |
| 1513 | |
| 1514 | static int |
| 1515 | gdb_display_match_list_pager (int lines, |
| 1516 | const struct match_list_displayer *displayer) |
| 1517 | { |
| 1518 | int i; |
| 1519 | |
| 1520 | displayer->puts (displayer, "--More--"); |
| 1521 | displayer->flush (displayer); |
| 1522 | i = gdb_get_y_or_n (1, displayer); |
| 1523 | displayer->erase_entire_line (displayer); |
| 1524 | if (i == 0) |
| 1525 | return -1; |
| 1526 | else if (i == 2) |
| 1527 | return (lines - 1); |
| 1528 | else |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | /* Return non-zero if FILENAME is a directory. |
| 1533 | Based on readline/complete.c:path_isdir. */ |
| 1534 | |
| 1535 | static int |
| 1536 | gdb_path_isdir (const char *filename) |
| 1537 | { |
| 1538 | struct stat finfo; |
| 1539 | |
| 1540 | return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode)); |
| 1541 | } |
| 1542 | |
| 1543 | /* Return the portion of PATHNAME that should be output when listing |
| 1544 | possible completions. If we are hacking filename completion, we |
| 1545 | are only interested in the basename, the portion following the |
| 1546 | final slash. Otherwise, we return what we were passed. Since |
| 1547 | printing empty strings is not very informative, if we're doing |
| 1548 | filename completion, and the basename is the empty string, we look |
| 1549 | for the previous slash and return the portion following that. If |
| 1550 | there's no previous slash, we just return what we were passed. |
| 1551 | |
| 1552 | Based on readline/complete.c:printable_part. */ |
| 1553 | |
| 1554 | static char * |
| 1555 | gdb_printable_part (char *pathname) |
| 1556 | { |
| 1557 | char *temp, *x; |
| 1558 | |
| 1559 | if (rl_filename_completion_desired == 0) /* don't need to do anything */ |
| 1560 | return (pathname); |
| 1561 | |
| 1562 | temp = strrchr (pathname, '/'); |
| 1563 | #if defined (__MSDOS__) |
| 1564 | if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':') |
| 1565 | temp = pathname + 1; |
| 1566 | #endif |
| 1567 | |
| 1568 | if (temp == 0 || *temp == '\0') |
| 1569 | return (pathname); |
| 1570 | /* If the basename is NULL, we might have a pathname like '/usr/src/'. |
| 1571 | Look for a previous slash and, if one is found, return the portion |
| 1572 | following that slash. If there's no previous slash, just return the |
| 1573 | pathname we were passed. */ |
| 1574 | else if (temp[1] == '\0') |
| 1575 | { |
| 1576 | for (x = temp - 1; x > pathname; x--) |
| 1577 | if (*x == '/') |
| 1578 | break; |
| 1579 | return ((*x == '/') ? x + 1 : pathname); |
| 1580 | } |
| 1581 | else |
| 1582 | return ++temp; |
| 1583 | } |
| 1584 | |
| 1585 | /* Compute width of STRING when displayed on screen by print_filename. |
| 1586 | Based on readline/complete.c:fnwidth. */ |
| 1587 | |
| 1588 | static int |
| 1589 | gdb_fnwidth (const char *string) |
| 1590 | { |
| 1591 | int width, pos; |
| 1592 | #if defined (HANDLE_MULTIBYTE) |
| 1593 | mbstate_t ps; |
| 1594 | int left, w; |
| 1595 | size_t clen; |
| 1596 | wchar_t wc; |
| 1597 | |
| 1598 | left = strlen (string) + 1; |
| 1599 | memset (&ps, 0, sizeof (mbstate_t)); |
| 1600 | #endif |
| 1601 | |
| 1602 | width = pos = 0; |
| 1603 | while (string[pos]) |
| 1604 | { |
| 1605 | if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT) |
| 1606 | { |
| 1607 | width += 2; |
| 1608 | pos++; |
| 1609 | } |
| 1610 | else |
| 1611 | { |
| 1612 | #if defined (HANDLE_MULTIBYTE) |
| 1613 | clen = mbrtowc (&wc, string + pos, left - pos, &ps); |
| 1614 | if (MB_INVALIDCH (clen)) |
| 1615 | { |
| 1616 | width++; |
| 1617 | pos++; |
| 1618 | memset (&ps, 0, sizeof (mbstate_t)); |
| 1619 | } |
| 1620 | else if (MB_NULLWCH (clen)) |
| 1621 | break; |
| 1622 | else |
| 1623 | { |
| 1624 | pos += clen; |
| 1625 | w = wcwidth (wc); |
| 1626 | width += (w >= 0) ? w : 1; |
| 1627 | } |
| 1628 | #else |
| 1629 | width++; |
| 1630 | pos++; |
| 1631 | #endif |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | return width; |
| 1636 | } |
| 1637 | |
| 1638 | /* Print TO_PRINT, one matching completion. |
| 1639 | PREFIX_BYTES is number of common prefix bytes. |
| 1640 | Based on readline/complete.c:fnprint. */ |
| 1641 | |
| 1642 | static int |
| 1643 | gdb_fnprint (const char *to_print, int prefix_bytes, |
| 1644 | const struct match_list_displayer *displayer) |
| 1645 | { |
| 1646 | int printed_len, w; |
| 1647 | const char *s; |
| 1648 | #if defined (HANDLE_MULTIBYTE) |
| 1649 | mbstate_t ps; |
| 1650 | const char *end; |
| 1651 | size_t tlen; |
| 1652 | int width; |
| 1653 | wchar_t wc; |
| 1654 | |
| 1655 | end = to_print + strlen (to_print) + 1; |
| 1656 | memset (&ps, 0, sizeof (mbstate_t)); |
| 1657 | #endif |
| 1658 | |
| 1659 | printed_len = 0; |
| 1660 | |
| 1661 | /* Don't print only the ellipsis if the common prefix is one of the |
| 1662 | possible completions */ |
| 1663 | if (to_print[prefix_bytes] == '\0') |
| 1664 | prefix_bytes = 0; |
| 1665 | |
| 1666 | if (prefix_bytes) |
| 1667 | { |
| 1668 | char ellipsis; |
| 1669 | |
| 1670 | ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.'; |
| 1671 | for (w = 0; w < ELLIPSIS_LEN; w++) |
| 1672 | displayer->putch (displayer, ellipsis); |
| 1673 | printed_len = ELLIPSIS_LEN; |
| 1674 | } |
| 1675 | |
| 1676 | s = to_print + prefix_bytes; |
| 1677 | while (*s) |
| 1678 | { |
| 1679 | if (CTRL_CHAR (*s)) |
| 1680 | { |
| 1681 | displayer->putch (displayer, '^'); |
| 1682 | displayer->putch (displayer, UNCTRL (*s)); |
| 1683 | printed_len += 2; |
| 1684 | s++; |
| 1685 | #if defined (HANDLE_MULTIBYTE) |
| 1686 | memset (&ps, 0, sizeof (mbstate_t)); |
| 1687 | #endif |
| 1688 | } |
| 1689 | else if (*s == RUBOUT) |
| 1690 | { |
| 1691 | displayer->putch (displayer, '^'); |
| 1692 | displayer->putch (displayer, '?'); |
| 1693 | printed_len += 2; |
| 1694 | s++; |
| 1695 | #if defined (HANDLE_MULTIBYTE) |
| 1696 | memset (&ps, 0, sizeof (mbstate_t)); |
| 1697 | #endif |
| 1698 | } |
| 1699 | else |
| 1700 | { |
| 1701 | #if defined (HANDLE_MULTIBYTE) |
| 1702 | tlen = mbrtowc (&wc, s, end - s, &ps); |
| 1703 | if (MB_INVALIDCH (tlen)) |
| 1704 | { |
| 1705 | tlen = 1; |
| 1706 | width = 1; |
| 1707 | memset (&ps, 0, sizeof (mbstate_t)); |
| 1708 | } |
| 1709 | else if (MB_NULLWCH (tlen)) |
| 1710 | break; |
| 1711 | else |
| 1712 | { |
| 1713 | w = wcwidth (wc); |
| 1714 | width = (w >= 0) ? w : 1; |
| 1715 | } |
| 1716 | for (w = 0; w < tlen; ++w) |
| 1717 | displayer->putch (displayer, s[w]); |
| 1718 | s += tlen; |
| 1719 | printed_len += width; |
| 1720 | #else |
| 1721 | displayer->putch (displayer, *s); |
| 1722 | s++; |
| 1723 | printed_len++; |
| 1724 | #endif |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | return printed_len; |
| 1729 | } |
| 1730 | |
| 1731 | /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we |
| 1732 | are using it, check for and output a single character for `special' |
| 1733 | filenames. Return the number of characters we output. |
| 1734 | Based on readline/complete.c:print_filename. */ |
| 1735 | |
| 1736 | static int |
| 1737 | gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes, |
| 1738 | const struct match_list_displayer *displayer) |
| 1739 | { |
| 1740 | int printed_len, extension_char, slen, tlen; |
| 1741 | char *s, c, *new_full_pathname; |
| 1742 | const char *dn; |
| 1743 | extern int _rl_complete_mark_directories; |
| 1744 | |
| 1745 | extension_char = 0; |
| 1746 | printed_len = gdb_fnprint (to_print, prefix_bytes, displayer); |
| 1747 | |
| 1748 | #if defined (VISIBLE_STATS) |
| 1749 | if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories)) |
| 1750 | #else |
| 1751 | if (rl_filename_completion_desired && _rl_complete_mark_directories) |
| 1752 | #endif |
| 1753 | { |
| 1754 | /* If to_print != full_pathname, to_print is the basename of the |
| 1755 | path passed. In this case, we try to expand the directory |
| 1756 | name before checking for the stat character. */ |
| 1757 | if (to_print != full_pathname) |
| 1758 | { |
| 1759 | /* Terminate the directory name. */ |
| 1760 | c = to_print[-1]; |
| 1761 | to_print[-1] = '\0'; |
| 1762 | |
| 1763 | /* If setting the last slash in full_pathname to a NUL results in |
| 1764 | full_pathname being the empty string, we are trying to complete |
| 1765 | files in the root directory. If we pass a null string to the |
| 1766 | bash directory completion hook, for example, it will expand it |
| 1767 | to the current directory. We just want the `/'. */ |
| 1768 | if (full_pathname == 0 || *full_pathname == 0) |
| 1769 | dn = "/"; |
| 1770 | else if (full_pathname[0] != '/') |
| 1771 | dn = full_pathname; |
| 1772 | else if (full_pathname[1] == 0) |
| 1773 | dn = "//"; /* restore trailing slash to `//' */ |
| 1774 | else if (full_pathname[1] == '/' && full_pathname[2] == 0) |
| 1775 | dn = "/"; /* don't turn /// into // */ |
| 1776 | else |
| 1777 | dn = full_pathname; |
| 1778 | s = tilde_expand (dn); |
| 1779 | if (rl_directory_completion_hook) |
| 1780 | (*rl_directory_completion_hook) (&s); |
| 1781 | |
| 1782 | slen = strlen (s); |
| 1783 | tlen = strlen (to_print); |
| 1784 | new_full_pathname = (char *)xmalloc (slen + tlen + 2); |
| 1785 | strcpy (new_full_pathname, s); |
| 1786 | if (s[slen - 1] == '/') |
| 1787 | slen--; |
| 1788 | else |
| 1789 | new_full_pathname[slen] = '/'; |
| 1790 | new_full_pathname[slen] = '/'; |
| 1791 | strcpy (new_full_pathname + slen + 1, to_print); |
| 1792 | |
| 1793 | #if defined (VISIBLE_STATS) |
| 1794 | if (rl_visible_stats) |
| 1795 | extension_char = stat_char (new_full_pathname); |
| 1796 | else |
| 1797 | #endif |
| 1798 | if (gdb_path_isdir (new_full_pathname)) |
| 1799 | extension_char = '/'; |
| 1800 | |
| 1801 | xfree (new_full_pathname); |
| 1802 | to_print[-1] = c; |
| 1803 | } |
| 1804 | else |
| 1805 | { |
| 1806 | s = tilde_expand (full_pathname); |
| 1807 | #if defined (VISIBLE_STATS) |
| 1808 | if (rl_visible_stats) |
| 1809 | extension_char = stat_char (s); |
| 1810 | else |
| 1811 | #endif |
| 1812 | if (gdb_path_isdir (s)) |
| 1813 | extension_char = '/'; |
| 1814 | } |
| 1815 | |
| 1816 | xfree (s); |
| 1817 | if (extension_char) |
| 1818 | { |
| 1819 | displayer->putch (displayer, extension_char); |
| 1820 | printed_len++; |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | return printed_len; |
| 1825 | } |
| 1826 | |
| 1827 | /* GDB version of readline/complete.c:complete_get_screenwidth. */ |
| 1828 | |
| 1829 | static int |
| 1830 | gdb_complete_get_screenwidth (const struct match_list_displayer *displayer) |
| 1831 | { |
| 1832 | /* Readline has other stuff here which it's not clear we need. */ |
| 1833 | return displayer->width; |
| 1834 | } |
| 1835 | |
| 1836 | extern int _rl_completion_prefix_display_length; |
| 1837 | extern int _rl_print_completions_horizontally; |
| 1838 | |
| 1839 | EXTERN_C int _rl_qsort_string_compare (const void *, const void *); |
| 1840 | typedef int QSFUNC (const void *, const void *); |
| 1841 | |
| 1842 | /* GDB version of readline/complete.c:rl_display_match_list. |
| 1843 | See gdb_display_match_list for a description of MATCHES, LEN, MAX. |
| 1844 | Returns non-zero if all matches are displayed. */ |
| 1845 | |
| 1846 | static int |
| 1847 | gdb_display_match_list_1 (char **matches, int len, int max, |
| 1848 | const struct match_list_displayer *displayer) |
| 1849 | { |
| 1850 | int count, limit, printed_len, lines, cols; |
| 1851 | int i, j, k, l, common_length, sind; |
| 1852 | char *temp, *t; |
| 1853 | int page_completions = displayer->height != INT_MAX && pagination_enabled; |
| 1854 | |
| 1855 | /* Find the length of the prefix common to all items: length as displayed |
| 1856 | characters (common_length) and as a byte index into the matches (sind) */ |
| 1857 | common_length = sind = 0; |
| 1858 | if (_rl_completion_prefix_display_length > 0) |
| 1859 | { |
| 1860 | t = gdb_printable_part (matches[0]); |
| 1861 | temp = strrchr (t, '/'); |
| 1862 | common_length = temp ? gdb_fnwidth (temp) : gdb_fnwidth (t); |
| 1863 | sind = temp ? strlen (temp) : strlen (t); |
| 1864 | |
| 1865 | if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN) |
| 1866 | max -= common_length - ELLIPSIS_LEN; |
| 1867 | else |
| 1868 | common_length = sind = 0; |
| 1869 | } |
| 1870 | |
| 1871 | /* How many items of MAX length can we fit in the screen window? */ |
| 1872 | cols = gdb_complete_get_screenwidth (displayer); |
| 1873 | max += 2; |
| 1874 | limit = cols / max; |
| 1875 | if (limit != 1 && (limit * max == cols)) |
| 1876 | limit--; |
| 1877 | |
| 1878 | /* If cols == 0, limit will end up -1 */ |
| 1879 | if (cols < displayer->width && limit < 0) |
| 1880 | limit = 1; |
| 1881 | |
| 1882 | /* Avoid a possible floating exception. If max > cols, |
| 1883 | limit will be 0 and a divide-by-zero fault will result. */ |
| 1884 | if (limit == 0) |
| 1885 | limit = 1; |
| 1886 | |
| 1887 | /* How many iterations of the printing loop? */ |
| 1888 | count = (len + (limit - 1)) / limit; |
| 1889 | |
| 1890 | /* Watch out for special case. If LEN is less than LIMIT, then |
| 1891 | just do the inner printing loop. |
| 1892 | 0 < len <= limit implies count = 1. */ |
| 1893 | |
| 1894 | /* Sort the items if they are not already sorted. */ |
| 1895 | if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches) |
| 1896 | qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); |
| 1897 | |
| 1898 | displayer->crlf (displayer); |
| 1899 | |
| 1900 | lines = 0; |
| 1901 | if (_rl_print_completions_horizontally == 0) |
| 1902 | { |
| 1903 | /* Print the sorted items, up-and-down alphabetically, like ls. */ |
| 1904 | for (i = 1; i <= count; i++) |
| 1905 | { |
| 1906 | for (j = 0, l = i; j < limit; j++) |
| 1907 | { |
| 1908 | if (l > len || matches[l] == 0) |
| 1909 | break; |
| 1910 | else |
| 1911 | { |
| 1912 | temp = gdb_printable_part (matches[l]); |
| 1913 | printed_len = gdb_print_filename (temp, matches[l], sind, |
| 1914 | displayer); |
| 1915 | |
| 1916 | if (j + 1 < limit) |
| 1917 | for (k = 0; k < max - printed_len; k++) |
| 1918 | displayer->putch (displayer, ' '); |
| 1919 | } |
| 1920 | l += count; |
| 1921 | } |
| 1922 | displayer->crlf (displayer); |
| 1923 | lines++; |
| 1924 | if (page_completions && lines >= (displayer->height - 1) && i < count) |
| 1925 | { |
| 1926 | lines = gdb_display_match_list_pager (lines, displayer); |
| 1927 | if (lines < 0) |
| 1928 | return 0; |
| 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | else |
| 1933 | { |
| 1934 | /* Print the sorted items, across alphabetically, like ls -x. */ |
| 1935 | for (i = 1; matches[i]; i++) |
| 1936 | { |
| 1937 | temp = gdb_printable_part (matches[i]); |
| 1938 | printed_len = gdb_print_filename (temp, matches[i], sind, displayer); |
| 1939 | /* Have we reached the end of this line? */ |
| 1940 | if (matches[i+1]) |
| 1941 | { |
| 1942 | if (i && (limit > 1) && (i % limit) == 0) |
| 1943 | { |
| 1944 | displayer->crlf (displayer); |
| 1945 | lines++; |
| 1946 | if (page_completions && lines >= displayer->height - 1) |
| 1947 | { |
| 1948 | lines = gdb_display_match_list_pager (lines, displayer); |
| 1949 | if (lines < 0) |
| 1950 | return 0; |
| 1951 | } |
| 1952 | } |
| 1953 | else |
| 1954 | for (k = 0; k < max - printed_len; k++) |
| 1955 | displayer->putch (displayer, ' '); |
| 1956 | } |
| 1957 | } |
| 1958 | displayer->crlf (displayer); |
| 1959 | } |
| 1960 | |
| 1961 | return 1; |
| 1962 | } |
| 1963 | |
| 1964 | /* Utility for displaying completion list matches, used by both CLI and TUI. |
| 1965 | |
| 1966 | MATCHES is the list of strings, in argv format, LEN is the number of |
| 1967 | strings in MATCHES, and MAX is the length of the longest string in |
| 1968 | MATCHES. */ |
| 1969 | |
| 1970 | void |
| 1971 | gdb_display_match_list (char **matches, int len, int max, |
| 1972 | const struct match_list_displayer *displayer) |
| 1973 | { |
| 1974 | /* Readline will never call this if complete_line returned NULL. */ |
| 1975 | gdb_assert (max_completions != 0); |
| 1976 | |
| 1977 | /* complete_line will never return more than this. */ |
| 1978 | if (max_completions > 0) |
| 1979 | gdb_assert (len <= max_completions); |
| 1980 | |
| 1981 | if (rl_completion_query_items > 0 && len >= rl_completion_query_items) |
| 1982 | { |
| 1983 | char msg[100]; |
| 1984 | |
| 1985 | /* We can't use *query here because they wait for <RET> which is |
| 1986 | wrong here. This follows the readline version as closely as possible |
| 1987 | for compatibility's sake. See readline/complete.c. */ |
| 1988 | |
| 1989 | displayer->crlf (displayer); |
| 1990 | |
| 1991 | xsnprintf (msg, sizeof (msg), |
| 1992 | "Display all %d possibilities? (y or n)", len); |
| 1993 | displayer->puts (displayer, msg); |
| 1994 | displayer->flush (displayer); |
| 1995 | |
| 1996 | if (gdb_get_y_or_n (0, displayer) == 0) |
| 1997 | { |
| 1998 | displayer->crlf (displayer); |
| 1999 | return; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | if (gdb_display_match_list_1 (matches, len, max, displayer)) |
| 2004 | { |
| 2005 | /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */ |
| 2006 | if (len == max_completions) |
| 2007 | { |
| 2008 | /* The maximum number of completions has been reached. Warn the user |
| 2009 | that there may be more. */ |
| 2010 | const char *message = get_max_completions_reached_message (); |
| 2011 | |
| 2012 | displayer->puts (displayer, message); |
| 2013 | displayer->crlf (displayer); |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | \f |
| 2018 | extern initialize_file_ftype _initialize_completer; /* -Wmissing-prototypes */ |
| 2019 | |
| 2020 | void |
| 2021 | _initialize_completer (void) |
| 2022 | { |
| 2023 | add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class, |
| 2024 | &max_completions, _("\ |
| 2025 | Set maximum number of completion candidates."), _("\ |
| 2026 | Show maximum number of completion candidates."), _("\ |
| 2027 | Use this to limit the number of candidates considered\n\ |
| 2028 | during completion. Specifying \"unlimited\" or -1\n\ |
| 2029 | disables limiting. Note that setting either no limit or\n\ |
| 2030 | a very large limit can make completion slow."), |
| 2031 | NULL, NULL, &setlist, &showlist); |
| 2032 | } |