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