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