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