Commit | Line | Data |
---|---|---|
c5f0f3d0 | 1 | /* Line completion stuff for GDB, the GNU debugger. |
ecd75fc8 | 2 | Copyright (C) 2000-2014 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" |
c5f0f3d0 | 26 | |
18a642a1 AC |
27 | #include "cli/cli-decode.h" |
28 | ||
03717487 MS |
29 | /* FIXME: This is needed because of lookup_cmd_1 (). We should be |
30 | calling a hook instead so we eliminate the CLI dependency. */ | |
c5f0f3d0 FN |
31 | #include "gdbcmd.h" |
32 | ||
c94fdfd0 | 33 | /* Needed for rl_completer_word_break_characters() and for |
38017ce8 | 34 | rl_filename_completion_function. */ |
dbda9972 | 35 | #include "readline/readline.h" |
c5f0f3d0 FN |
36 | |
37 | /* readline defines this. */ | |
38 | #undef savestring | |
39 | ||
40 | #include "completer.h" | |
41 | ||
9c3f90bd | 42 | /* Prototypes for local functions. */ |
38017ce8 | 43 | static |
03717487 MS |
44 | char *line_completion_function (const char *text, int matches, |
45 | char *line_buffer, | |
d75b5104 | 46 | int point); |
c5f0f3d0 FN |
47 | |
48 | /* readline uses the word breaks for two things: | |
49 | (1) In figuring out where to point the TEXT parameter to the | |
50 | rl_completion_entry_function. Since we don't use TEXT for much, | |
aff410f1 MS |
51 | it doesn't matter a lot what the word breaks are for this purpose, |
52 | but it does affect how much stuff M-? lists. | |
c5f0f3d0 FN |
53 | (2) If one of the matches contains a word break character, readline |
54 | will quote it. That's why we switch between | |
51065942 | 55 | current_language->la_word_break_characters() and |
c5f0f3d0 | 56 | gdb_completer_command_word_break_characters. I'm not sure when |
aff410f1 MS |
57 | we need this behavior (perhaps for funky characters in C++ |
58 | symbols?). */ | |
c5f0f3d0 FN |
59 | |
60 | /* Variables which are necessary for fancy command line editing. */ | |
c5f0f3d0 FN |
61 | |
62 | /* When completing on command names, we remove '-' from the list of | |
63 | word break characters, since we use it in command names. If the | |
64 | readline library sees one in any of the current completion strings, | |
aff410f1 MS |
65 | it thinks that the string needs to be quoted and automatically |
66 | supplies a leading quote. */ | |
c5f0f3d0 FN |
67 | static char *gdb_completer_command_word_break_characters = |
68 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; | |
69 | ||
70 | /* When completing on file names, we remove from the list of word | |
71 | break characters any characters that are commonly used in file | |
72 | names, such as '-', '+', '~', etc. Otherwise, readline displays | |
73 | incorrect completion candidates. */ | |
c3690141 | 74 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
7830cf6f EZ |
75 | /* MS-DOS and MS-Windows use colon as part of the drive spec, and most |
76 | programs support @foo style response files. */ | |
77 | static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@"; | |
78 | #else | |
79 | static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><"; | |
80 | #endif | |
c5f0f3d0 | 81 | |
aff410f1 MS |
82 | /* Characters that can be used to quote completion strings. Note that |
83 | we can't include '"' because the gdb C parser treats such quoted | |
84 | sequences as strings. */ | |
c5f0f3d0 FN |
85 | static char *gdb_completer_quote_characters = "'"; |
86 | \f | |
9c3f90bd | 87 | /* Accessor for some completer data that may interest other files. */ |
c5f0f3d0 | 88 | |
c5f0f3d0 FN |
89 | char * |
90 | get_gdb_completer_quote_characters (void) | |
91 | { | |
92 | return gdb_completer_quote_characters; | |
93 | } | |
94 | ||
d75b5104 EZ |
95 | /* Line completion interface function for readline. */ |
96 | ||
97 | char * | |
38017ce8 | 98 | readline_line_completion_function (const char *text, int matches) |
d75b5104 | 99 | { |
aff410f1 MS |
100 | return line_completion_function (text, matches, |
101 | rl_line_buffer, rl_point); | |
d75b5104 EZ |
102 | } |
103 | ||
aff410f1 MS |
104 | /* This can be used for functions which don't want to complete on |
105 | symbols but don't want to complete on anything else either. */ | |
49c4e619 | 106 | VEC (char_ptr) * |
aff410f1 | 107 | noop_completer (struct cmd_list_element *ignore, |
6f937416 | 108 | const char *text, const char *prefix) |
d75b5104 EZ |
109 | { |
110 | return NULL; | |
111 | } | |
112 | ||
c5f0f3d0 | 113 | /* Complete on filenames. */ |
49c4e619 | 114 | VEC (char_ptr) * |
aff410f1 | 115 | filename_completer (struct cmd_list_element *ignore, |
6f937416 | 116 | const char *text, const char *word) |
c5f0f3d0 | 117 | { |
c5f0f3d0 | 118 | int subsequent_name; |
49c4e619 | 119 | VEC (char_ptr) *return_val = NULL; |
c5f0f3d0 FN |
120 | |
121 | subsequent_name = 0; | |
122 | while (1) | |
123 | { | |
1e8189fb | 124 | char *p, *q; |
c5504eaf | 125 | |
38017ce8 | 126 | p = rl_filename_completion_function (text, subsequent_name); |
c5f0f3d0 | 127 | if (p == NULL) |
49c4e619 | 128 | break; |
c5f0f3d0 | 129 | /* We need to set subsequent_name to a non-zero value before the |
aff410f1 MS |
130 | continue line below, because otherwise, if the first file |
131 | seen by GDB is a backup file whose name ends in a `~', we | |
132 | will loop indefinitely. */ | |
c5f0f3d0 | 133 | subsequent_name = 1; |
aff410f1 MS |
134 | /* Like emacs, don't complete on old versions. Especially |
135 | useful in the "source" command. */ | |
c5f0f3d0 | 136 | if (p[strlen (p) - 1] == '~') |
1e8189fb MS |
137 | { |
138 | xfree (p); | |
139 | continue; | |
140 | } | |
c5f0f3d0 | 141 | |
1e8189fb MS |
142 | if (word == text) |
143 | /* Return exactly p. */ | |
49c4e619 | 144 | q = p; |
1e8189fb MS |
145 | else if (word > text) |
146 | { | |
147 | /* Return some portion of p. */ | |
148 | q = xmalloc (strlen (p) + 5); | |
149 | strcpy (q, p + (word - text)); | |
1e8189fb MS |
150 | xfree (p); |
151 | } | |
152 | else | |
153 | { | |
154 | /* Return some of TEXT plus p. */ | |
155 | q = xmalloc (strlen (p) + (text - word) + 5); | |
156 | strncpy (q, word, text - word); | |
157 | q[text - word] = '\0'; | |
158 | strcat (q, p); | |
1e8189fb MS |
159 | xfree (p); |
160 | } | |
49c4e619 | 161 | VEC_safe_push (char_ptr, return_val, q); |
c5f0f3d0 FN |
162 | } |
163 | #if 0 | |
aff410f1 MS |
164 | /* There is no way to do this just long enough to affect quote |
165 | inserting without also affecting the next completion. This | |
166 | should be fixed in readline. FIXME. */ | |
489f0516 | 167 | /* Ensure that readline does the right thing |
c5f0f3d0 FN |
168 | with respect to inserting quotes. */ |
169 | rl_completer_word_break_characters = ""; | |
170 | #endif | |
171 | return return_val; | |
172 | } | |
173 | ||
c94fdfd0 EZ |
174 | /* Complete on locations, which might be of two possible forms: |
175 | ||
176 | file:line | |
177 | or | |
178 | symbol+offset | |
179 | ||
aff410f1 MS |
180 | This is intended to be used in commands that set breakpoints |
181 | etc. */ | |
182 | ||
49c4e619 | 183 | VEC (char_ptr) * |
aff410f1 | 184 | location_completer (struct cmd_list_element *ignore, |
6f937416 | 185 | const char *text, const char *word) |
c94fdfd0 | 186 | { |
49c4e619 TT |
187 | int n_syms, n_files, ix; |
188 | VEC (char_ptr) *fn_list = NULL; | |
189 | VEC (char_ptr) *list = NULL; | |
6f937416 | 190 | const char *p; |
c94fdfd0 EZ |
191 | int quote_found = 0; |
192 | int quoted = *text == '\'' || *text == '"'; | |
193 | int quote_char = '\0'; | |
6f937416 | 194 | const char *colon = NULL; |
c94fdfd0 | 195 | char *file_to_match = NULL; |
6f937416 PA |
196 | const char *symbol_start = text; |
197 | const char *orig_text = text; | |
c94fdfd0 EZ |
198 | size_t text_len; |
199 | ||
59be2b6a | 200 | /* Do we have an unquoted colon, as in "break foo.c:bar"? */ |
c94fdfd0 EZ |
201 | for (p = text; *p != '\0'; ++p) |
202 | { | |
203 | if (*p == '\\' && p[1] == '\'') | |
204 | p++; | |
205 | else if (*p == '\'' || *p == '"') | |
206 | { | |
207 | quote_found = *p; | |
208 | quote_char = *p++; | |
209 | while (*p != '\0' && *p != quote_found) | |
210 | { | |
211 | if (*p == '\\' && p[1] == quote_found) | |
212 | p++; | |
213 | p++; | |
214 | } | |
215 | ||
216 | if (*p == quote_found) | |
217 | quote_found = 0; | |
218 | else | |
9c3f90bd | 219 | break; /* Hit the end of text. */ |
c94fdfd0 EZ |
220 | } |
221 | #if HAVE_DOS_BASED_FILE_SYSTEM | |
222 | /* If we have a DOS-style absolute file name at the beginning of | |
223 | TEXT, and the colon after the drive letter is the only colon | |
224 | we found, pretend the colon is not there. */ | |
225 | else if (p < text + 3 && *p == ':' && p == text + 1 + quoted) | |
226 | ; | |
227 | #endif | |
228 | else if (*p == ':' && !colon) | |
229 | { | |
230 | colon = p; | |
231 | symbol_start = p + 1; | |
232 | } | |
51065942 | 233 | else if (strchr (current_language->la_word_break_characters(), *p)) |
c94fdfd0 EZ |
234 | symbol_start = p + 1; |
235 | } | |
236 | ||
237 | if (quoted) | |
238 | text++; | |
239 | text_len = strlen (text); | |
240 | ||
241 | /* Where is the file name? */ | |
242 | if (colon) | |
243 | { | |
244 | char *s; | |
245 | ||
246 | file_to_match = (char *) xmalloc (colon - text + 1); | |
247 | strncpy (file_to_match, text, colon - text + 1); | |
248 | /* Remove trailing colons and quotes from the file name. */ | |
249 | for (s = file_to_match + (colon - text); | |
250 | s > file_to_match; | |
251 | s--) | |
252 | if (*s == ':' || *s == quote_char) | |
253 | *s = '\0'; | |
254 | } | |
255 | /* If the text includes a colon, they want completion only on a | |
256 | symbol name after the colon. Otherwise, we need to complete on | |
257 | symbols as well as on files. */ | |
258 | if (colon) | |
259 | { | |
260 | list = make_file_symbol_completion_list (symbol_start, word, | |
261 | file_to_match); | |
262 | xfree (file_to_match); | |
263 | } | |
264 | else | |
265 | { | |
266 | list = make_symbol_completion_list (symbol_start, word); | |
267 | /* If text includes characters which cannot appear in a file | |
268 | name, they cannot be asking for completion on files. */ | |
1f20ed91 MS |
269 | if (strcspn (text, |
270 | gdb_completer_file_name_break_characters) == text_len) | |
c94fdfd0 EZ |
271 | fn_list = make_source_files_completion_list (text, text); |
272 | } | |
273 | ||
49c4e619 TT |
274 | n_syms = VEC_length (char_ptr, list); |
275 | n_files = VEC_length (char_ptr, fn_list); | |
276 | ||
277 | /* Catenate fn_list[] onto the end of list[]. */ | |
278 | if (!n_syms) | |
279 | { | |
280 | VEC_free (char_ptr, list); /* Paranoia. */ | |
281 | list = fn_list; | |
282 | fn_list = NULL; | |
283 | } | |
284 | else | |
285 | { | |
6f937416 PA |
286 | char *fn; |
287 | ||
288 | for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, fn); ++ix) | |
289 | VEC_safe_push (char_ptr, list, fn); | |
49c4e619 TT |
290 | VEC_free (char_ptr, fn_list); |
291 | } | |
c94fdfd0 | 292 | |
c94fdfd0 EZ |
293 | if (n_syms && n_files) |
294 | { | |
49c4e619 | 295 | /* Nothing. */ |
c94fdfd0 EZ |
296 | } |
297 | else if (n_files) | |
298 | { | |
6f937416 PA |
299 | char *fn; |
300 | ||
c94fdfd0 EZ |
301 | /* If we only have file names as possible completion, we should |
302 | bring them in sync with what rl_complete expects. The | |
303 | problem is that if the user types "break /foo/b TAB", and the | |
304 | possible completions are "/foo/bar" and "/foo/baz" | |
305 | rl_complete expects us to return "bar" and "baz", without the | |
306 | leading directories, as possible completions, because `word' | |
307 | starts at the "b". But we ignore the value of `word' when we | |
308 | call make_source_files_completion_list above (because that | |
309 | would not DTRT when the completion results in both symbols | |
310 | and file names), so make_source_files_completion_list returns | |
311 | the full "/foo/bar" and "/foo/baz" strings. This produces | |
312 | wrong results when, e.g., there's only one possible | |
313 | completion, because rl_complete will prepend "/foo/" to each | |
314 | candidate completion. The loop below removes that leading | |
315 | part. */ | |
6f937416 | 316 | for (ix = 0; VEC_iterate (char_ptr, list, ix, fn); ++ix) |
c94fdfd0 | 317 | { |
6f937416 PA |
318 | memmove (fn, fn + (word - text), |
319 | strlen (fn) + 1 - (word - text)); | |
c94fdfd0 | 320 | } |
c94fdfd0 EZ |
321 | } |
322 | else if (!n_syms) | |
323 | { | |
324 | /* No completions at all. As the final resort, try completing | |
325 | on the entire text as a symbol. */ | |
326 | list = make_symbol_completion_list (orig_text, word); | |
327 | } | |
328 | ||
329 | return list; | |
330 | } | |
331 | ||
1c71341a TT |
332 | /* Helper for expression_completer which recursively adds field and |
333 | method names from TYPE, a struct or union type, to the array | |
49c4e619 | 334 | OUTPUT. */ |
65d12d83 | 335 | static void |
49c4e619 | 336 | add_struct_fields (struct type *type, VEC (char_ptr) **output, |
65d12d83 TT |
337 | char *fieldname, int namelen) |
338 | { | |
339 | int i; | |
b32d97f3 | 340 | int computed_type_name = 0; |
0d5cff50 | 341 | const char *type_name = NULL; |
65d12d83 TT |
342 | |
343 | CHECK_TYPEDEF (type); | |
344 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
345 | { | |
346 | if (i < TYPE_N_BASECLASSES (type)) | |
49c4e619 | 347 | add_struct_fields (TYPE_BASECLASS (type, i), |
aff410f1 | 348 | output, fieldname, namelen); |
9ae8282d | 349 | else if (TYPE_FIELD_NAME (type, i)) |
65d12d83 | 350 | { |
9ae8282d TT |
351 | if (TYPE_FIELD_NAME (type, i)[0] != '\0') |
352 | { | |
aff410f1 MS |
353 | if (! strncmp (TYPE_FIELD_NAME (type, i), |
354 | fieldname, namelen)) | |
49c4e619 TT |
355 | VEC_safe_push (char_ptr, *output, |
356 | xstrdup (TYPE_FIELD_NAME (type, i))); | |
9ae8282d TT |
357 | } |
358 | else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION) | |
359 | { | |
360 | /* Recurse into anonymous unions. */ | |
49c4e619 | 361 | add_struct_fields (TYPE_FIELD_TYPE (type, i), |
aff410f1 | 362 | output, fieldname, namelen); |
9ae8282d | 363 | } |
65d12d83 TT |
364 | } |
365 | } | |
1c71341a TT |
366 | |
367 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) | |
368 | { | |
0d5cff50 | 369 | const char *name = TYPE_FN_FIELDLIST_NAME (type, i); |
c5504eaf | 370 | |
1c71341a TT |
371 | if (name && ! strncmp (name, fieldname, namelen)) |
372 | { | |
b32d97f3 TT |
373 | if (!computed_type_name) |
374 | { | |
375 | type_name = type_name_no_tag (type); | |
376 | computed_type_name = 1; | |
377 | } | |
1c71341a | 378 | /* Omit constructors from the completion list. */ |
907af001 | 379 | if (!type_name || strcmp (type_name, name)) |
49c4e619 | 380 | VEC_safe_push (char_ptr, *output, xstrdup (name)); |
1c71341a TT |
381 | } |
382 | } | |
65d12d83 TT |
383 | } |
384 | ||
385 | /* Complete on expressions. Often this means completing on symbol | |
386 | names, but some language parsers also have support for completing | |
387 | field names. */ | |
49c4e619 | 388 | VEC (char_ptr) * |
aff410f1 | 389 | expression_completer (struct cmd_list_element *ignore, |
6f937416 | 390 | const char *text, const char *word) |
65d12d83 | 391 | { |
c92817ce | 392 | struct type *type = NULL; |
6f937416 PA |
393 | char *fieldname; |
394 | const char *p; | |
c92817ce | 395 | volatile struct gdb_exception except; |
2f68a895 | 396 | enum type_code code = TYPE_CODE_UNDEF; |
65d12d83 TT |
397 | |
398 | /* Perform a tentative parse of the expression, to see whether a | |
399 | field completion is required. */ | |
400 | fieldname = NULL; | |
c92817ce TT |
401 | TRY_CATCH (except, RETURN_MASK_ERROR) |
402 | { | |
2f68a895 | 403 | type = parse_expression_for_completion (text, &fieldname, &code); |
c92817ce TT |
404 | } |
405 | if (except.reason < 0) | |
406 | return NULL; | |
65d12d83 TT |
407 | if (fieldname && type) |
408 | { | |
409 | for (;;) | |
410 | { | |
411 | CHECK_TYPEDEF (type); | |
412 | if (TYPE_CODE (type) != TYPE_CODE_PTR | |
413 | && TYPE_CODE (type) != TYPE_CODE_REF) | |
414 | break; | |
415 | type = TYPE_TARGET_TYPE (type); | |
416 | } | |
417 | ||
418 | if (TYPE_CODE (type) == TYPE_CODE_UNION | |
419 | || TYPE_CODE (type) == TYPE_CODE_STRUCT) | |
420 | { | |
65d12d83 | 421 | int flen = strlen (fieldname); |
49c4e619 | 422 | VEC (char_ptr) *result = NULL; |
65d12d83 | 423 | |
49c4e619 | 424 | add_struct_fields (type, &result, fieldname, flen); |
a0b7aece | 425 | xfree (fieldname); |
65d12d83 TT |
426 | return result; |
427 | } | |
428 | } | |
2f68a895 TT |
429 | else if (fieldname && code != TYPE_CODE_UNDEF) |
430 | { | |
431 | VEC (char_ptr) *result; | |
432 | struct cleanup *cleanup = make_cleanup (xfree, fieldname); | |
433 | ||
434 | result = make_symbol_completion_type (fieldname, fieldname, code); | |
435 | do_cleanups (cleanup); | |
436 | return result; | |
437 | } | |
a0b7aece | 438 | xfree (fieldname); |
65d12d83 | 439 | |
37cd5d19 TT |
440 | /* Commands which complete on locations want to see the entire |
441 | argument. */ | |
442 | for (p = word; | |
443 | p > text && p[-1] != ' ' && p[-1] != '\t'; | |
444 | p--) | |
445 | ; | |
446 | ||
aff410f1 | 447 | /* Not ideal but it is what we used to do before... */ |
d8906c6f | 448 | return location_completer (ignore, p, word); |
65d12d83 TT |
449 | } |
450 | ||
7d793aa9 SDJ |
451 | /* See definition in completer.h. */ |
452 | ||
453 | void | |
454 | set_gdb_completion_word_break_characters (completer_ftype *fn) | |
455 | { | |
456 | /* So far we are only interested in differentiating filename | |
457 | completers from everything else. */ | |
458 | if (fn == filename_completer) | |
459 | rl_completer_word_break_characters | |
460 | = gdb_completer_file_name_break_characters; | |
461 | else | |
462 | rl_completer_word_break_characters | |
463 | = gdb_completer_command_word_break_characters; | |
464 | } | |
465 | ||
aff410f1 MS |
466 | /* Here are some useful test cases for completion. FIXME: These |
467 | should be put in the test suite. They should be tested with both | |
468 | M-? and TAB. | |
c5f0f3d0 FN |
469 | |
470 | "show output-" "radix" | |
471 | "show output" "-radix" | |
472 | "p" ambiguous (commands starting with p--path, print, printf, etc.) | |
473 | "p " ambiguous (all symbols) | |
474 | "info t foo" no completions | |
475 | "info t " no completions | |
476 | "info t" ambiguous ("info target", "info terminal", etc.) | |
477 | "info ajksdlfk" no completions | |
478 | "info ajksdlfk " no completions | |
479 | "info" " " | |
480 | "info " ambiguous (all info commands) | |
481 | "p \"a" no completions (string constant) | |
482 | "p 'a" ambiguous (all symbols starting with a) | |
483 | "p b-a" ambiguous (all symbols starting with a) | |
484 | "p b-" ambiguous (all symbols) | |
485 | "file Make" "file" (word break hard to screw up here) | |
486 | "file ../gdb.stabs/we" "ird" (needs to not break word at slash) | |
487 | */ | |
488 | ||
67c296a2 PM |
489 | typedef enum |
490 | { | |
491 | handle_brkchars, | |
492 | handle_completions, | |
493 | handle_help | |
494 | } | |
495 | complete_line_internal_reason; | |
496 | ||
497 | ||
498 | /* Internal function used to handle completions. | |
499 | ||
c5f0f3d0 FN |
500 | |
501 | TEXT is the caller's idea of the "word" we are looking at. | |
502 | ||
aff410f1 MS |
503 | LINE_BUFFER is available to be looked at; it contains the entire |
504 | text of the line. POINT is the offset in that line of the cursor. | |
505 | You should pretend that the line ends at POINT. | |
67c296a2 PM |
506 | |
507 | REASON is of type complete_line_internal_reason. | |
508 | ||
509 | If REASON is handle_brkchars: | |
aff410f1 MS |
510 | Preliminary phase, called by gdb_completion_word_break_characters |
511 | function, is used to determine the correct set of chars that are | |
512 | word delimiters depending on the current command in line_buffer. | |
513 | No completion list should be generated; the return value should be | |
514 | NULL. This is checked by an assertion in that function. | |
67c296a2 PM |
515 | |
516 | If REASON is handle_completions: | |
517 | Main phase, called by complete_line function, is used to get the list | |
518 | of posible completions. | |
519 | ||
520 | If REASON is handle_help: | |
521 | Special case when completing a 'help' command. In this case, | |
14032a66 | 522 | once sub-command completions are exhausted, we simply return NULL. |
67c296a2 | 523 | */ |
14032a66 | 524 | |
49c4e619 | 525 | static VEC (char_ptr) * |
aff410f1 | 526 | complete_line_internal (const char *text, |
6f937416 | 527 | const char *line_buffer, int point, |
67c296a2 | 528 | complete_line_internal_reason reason) |
c5f0f3d0 | 529 | { |
49c4e619 | 530 | VEC (char_ptr) *list = NULL; |
6f937416 PA |
531 | char *tmp_command; |
532 | const char *p; | |
ace21957 | 533 | int ignore_help_classes; |
c5f0f3d0 FN |
534 | /* Pointer within tmp_command which corresponds to text. */ |
535 | char *word; | |
536 | struct cmd_list_element *c, *result_list; | |
537 | ||
aff410f1 MS |
538 | /* Choose the default set of word break characters to break |
539 | completions. If we later find out that we are doing completions | |
540 | on command strings (as opposed to strings supplied by the | |
541 | individual command completer functions, which can be any string) | |
542 | then we will switch to the special word break set for command | |
543 | strings, which leaves out the '-' character used in some | |
544 | commands. */ | |
83d31a92 | 545 | rl_completer_word_break_characters = |
51065942 | 546 | current_language->la_word_break_characters(); |
c5f0f3d0 | 547 | |
aff410f1 MS |
548 | /* Decide whether to complete on a list of gdb commands or on |
549 | symbols. */ | |
83d31a92 TT |
550 | tmp_command = (char *) alloca (point + 1); |
551 | p = tmp_command; | |
c5f0f3d0 | 552 | |
ace21957 MF |
553 | /* The help command should complete help aliases. */ |
554 | ignore_help_classes = reason != handle_help; | |
555 | ||
83d31a92 TT |
556 | strncpy (tmp_command, line_buffer, point); |
557 | tmp_command[point] = '\0'; | |
558 | /* Since text always contains some number of characters leading up | |
559 | to point, we can find the equivalent position in tmp_command | |
560 | by subtracting that many characters from the end of tmp_command. */ | |
561 | word = tmp_command + point - strlen (text); | |
c5f0f3d0 | 562 | |
83d31a92 TT |
563 | if (point == 0) |
564 | { | |
565 | /* An empty line we want to consider ambiguous; that is, it | |
566 | could be any command. */ | |
1427fe5e | 567 | c = CMD_LIST_AMBIGUOUS; |
83d31a92 TT |
568 | result_list = 0; |
569 | } | |
570 | else | |
571 | { | |
ace21957 | 572 | c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes); |
83d31a92 | 573 | } |
c5f0f3d0 | 574 | |
83d31a92 TT |
575 | /* Move p up to the next interesting thing. */ |
576 | while (*p == ' ' || *p == '\t') | |
577 | { | |
578 | p++; | |
579 | } | |
c5f0f3d0 | 580 | |
83d31a92 TT |
581 | if (!c) |
582 | { | |
583 | /* It is an unrecognized command. So there are no | |
584 | possible completions. */ | |
585 | list = NULL; | |
586 | } | |
1427fe5e | 587 | else if (c == CMD_LIST_AMBIGUOUS) |
83d31a92 | 588 | { |
6f937416 | 589 | const char *q; |
83d31a92 TT |
590 | |
591 | /* lookup_cmd_1 advances p up to the first ambiguous thing, but | |
592 | doesn't advance over that thing itself. Do so now. */ | |
593 | q = p; | |
594 | while (*q && (isalnum (*q) || *q == '-' || *q == '_')) | |
595 | ++q; | |
596 | if (q != tmp_command + point) | |
c5f0f3d0 | 597 | { |
83d31a92 TT |
598 | /* There is something beyond the ambiguous |
599 | command, so there are no possible completions. For | |
600 | example, "info t " or "info t foo" does not complete | |
601 | to anything, because "info t" can be "info target" or | |
602 | "info terminal". */ | |
c5f0f3d0 FN |
603 | list = NULL; |
604 | } | |
83d31a92 | 605 | else |
c5f0f3d0 | 606 | { |
83d31a92 TT |
607 | /* We're trying to complete on the command which was ambiguous. |
608 | This we can deal with. */ | |
609 | if (result_list) | |
c5f0f3d0 | 610 | { |
67c296a2 PM |
611 | if (reason != handle_brkchars) |
612 | list = complete_on_cmdlist (*result_list->prefixlist, p, | |
ace21957 | 613 | word, ignore_help_classes); |
c5f0f3d0 FN |
614 | } |
615 | else | |
616 | { | |
67c296a2 | 617 | if (reason != handle_brkchars) |
ace21957 MF |
618 | list = complete_on_cmdlist (cmdlist, p, word, |
619 | ignore_help_classes); | |
c5f0f3d0 | 620 | } |
489f0516 | 621 | /* Ensure that readline does the right thing with respect to |
83d31a92 TT |
622 | inserting quotes. */ |
623 | rl_completer_word_break_characters = | |
624 | gdb_completer_command_word_break_characters; | |
c5f0f3d0 | 625 | } |
83d31a92 TT |
626 | } |
627 | else | |
628 | { | |
629 | /* We've recognized a full command. */ | |
630 | ||
631 | if (p == tmp_command + point) | |
c5f0f3d0 | 632 | { |
aff410f1 MS |
633 | /* There is no non-whitespace in the line beyond the |
634 | command. */ | |
c5f0f3d0 | 635 | |
83d31a92 | 636 | if (p[-1] == ' ' || p[-1] == '\t') |
c5f0f3d0 | 637 | { |
aff410f1 MS |
638 | /* The command is followed by whitespace; we need to |
639 | complete on whatever comes after command. */ | |
83d31a92 | 640 | if (c->prefixlist) |
c5f0f3d0 | 641 | { |
83d31a92 TT |
642 | /* It is a prefix command; what comes after it is |
643 | a subcommand (e.g. "info "). */ | |
67c296a2 | 644 | if (reason != handle_brkchars) |
ace21957 MF |
645 | list = complete_on_cmdlist (*c->prefixlist, p, word, |
646 | ignore_help_classes); | |
c5f0f3d0 | 647 | |
489f0516 | 648 | /* Ensure that readline does the right thing |
9c3f90bd | 649 | with respect to inserting quotes. */ |
c5f0f3d0 FN |
650 | rl_completer_word_break_characters = |
651 | gdb_completer_command_word_break_characters; | |
652 | } | |
67c296a2 | 653 | else if (reason == handle_help) |
14032a66 | 654 | list = NULL; |
c5f0f3d0 FN |
655 | else if (c->enums) |
656 | { | |
67c296a2 PM |
657 | if (reason != handle_brkchars) |
658 | list = complete_on_enum (c->enums, p, word); | |
83d31a92 TT |
659 | rl_completer_word_break_characters = |
660 | gdb_completer_command_word_break_characters; | |
c5f0f3d0 FN |
661 | } |
662 | else | |
663 | { | |
83d31a92 TT |
664 | /* It is a normal command; what comes after it is |
665 | completed by the command's completer function. */ | |
c5f0f3d0 | 666 | if (c->completer == filename_completer) |
7830cf6f | 667 | { |
83d31a92 TT |
668 | /* Many commands which want to complete on |
669 | file names accept several file names, as | |
670 | in "run foo bar >>baz". So we don't want | |
671 | to complete the entire text after the | |
672 | command, just the last word. To this | |
673 | end, we need to find the beginning of the | |
674 | file name by starting at `word' and going | |
675 | backwards. */ | |
7830cf6f EZ |
676 | for (p = word; |
677 | p > tmp_command | |
678 | && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL; | |
679 | p--) | |
680 | ; | |
681 | rl_completer_word_break_characters = | |
682 | gdb_completer_file_name_break_characters; | |
683 | } | |
37cd5d19 | 684 | else if (c->completer == location_completer) |
c94fdfd0 | 685 | { |
83d31a92 TT |
686 | /* Commands which complete on locations want to |
687 | see the entire argument. */ | |
c94fdfd0 EZ |
688 | for (p = word; |
689 | p > tmp_command | |
690 | && p[-1] != ' ' && p[-1] != '\t'; | |
691 | p--) | |
692 | ; | |
693 | } | |
7d793aa9 SDJ |
694 | if (reason == handle_brkchars |
695 | && c->completer_handle_brkchars != NULL) | |
696 | (*c->completer_handle_brkchars) (c, p, word); | |
2d9c5cff | 697 | if (reason != handle_brkchars && c->completer != NULL) |
67c296a2 | 698 | list = (*c->completer) (c, p, word); |
c5f0f3d0 FN |
699 | } |
700 | } | |
83d31a92 TT |
701 | else |
702 | { | |
703 | /* The command is not followed by whitespace; we need to | |
aff410f1 | 704 | complete on the command itself, e.g. "p" which is a |
83d31a92 TT |
705 | command itself but also can complete to "print", "ptype" |
706 | etc. */ | |
6f937416 | 707 | const char *q; |
83d31a92 TT |
708 | |
709 | /* Find the command we are completing on. */ | |
710 | q = p; | |
711 | while (q > tmp_command) | |
712 | { | |
713 | if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') | |
714 | --q; | |
715 | else | |
716 | break; | |
717 | } | |
718 | ||
67c296a2 | 719 | if (reason != handle_brkchars) |
ace21957 MF |
720 | list = complete_on_cmdlist (result_list, q, word, |
721 | ignore_help_classes); | |
83d31a92 | 722 | |
489f0516 | 723 | /* Ensure that readline does the right thing |
9c3f90bd | 724 | with respect to inserting quotes. */ |
83d31a92 TT |
725 | rl_completer_word_break_characters = |
726 | gdb_completer_command_word_break_characters; | |
727 | } | |
728 | } | |
67c296a2 | 729 | else if (reason == handle_help) |
14032a66 | 730 | list = NULL; |
83d31a92 TT |
731 | else |
732 | { | |
733 | /* There is non-whitespace beyond the command. */ | |
734 | ||
735 | if (c->prefixlist && !c->allow_unknown) | |
736 | { | |
737 | /* It is an unrecognized subcommand of a prefix command, | |
738 | e.g. "info adsfkdj". */ | |
739 | list = NULL; | |
740 | } | |
741 | else if (c->enums) | |
742 | { | |
67c296a2 PM |
743 | if (reason != handle_brkchars) |
744 | list = complete_on_enum (c->enums, p, word); | |
83d31a92 TT |
745 | } |
746 | else | |
747 | { | |
748 | /* It is a normal command. */ | |
749 | if (c->completer == filename_completer) | |
750 | { | |
751 | /* See the commentary above about the specifics | |
752 | of file-name completion. */ | |
753 | for (p = word; | |
754 | p > tmp_command | |
aff410f1 MS |
755 | && strchr (gdb_completer_file_name_break_characters, |
756 | p[-1]) == NULL; | |
83d31a92 TT |
757 | p--) |
758 | ; | |
759 | rl_completer_word_break_characters = | |
760 | gdb_completer_file_name_break_characters; | |
761 | } | |
37cd5d19 | 762 | else if (c->completer == location_completer) |
83d31a92 TT |
763 | { |
764 | for (p = word; | |
765 | p > tmp_command | |
766 | && p[-1] != ' ' && p[-1] != '\t'; | |
767 | p--) | |
768 | ; | |
769 | } | |
7d793aa9 SDJ |
770 | if (reason == handle_brkchars |
771 | && c->completer_handle_brkchars != NULL) | |
772 | (*c->completer_handle_brkchars) (c, p, word); | |
2d9c5cff | 773 | if (reason != handle_brkchars && c->completer != NULL) |
67c296a2 | 774 | list = (*c->completer) (c, p, word); |
83d31a92 TT |
775 | } |
776 | } | |
777 | } | |
778 | ||
779 | return list; | |
780 | } | |
49c4e619 TT |
781 | /* Generate completions all at once. Returns a vector of strings. |
782 | Each element is allocated with xmalloc. It can also return NULL if | |
783 | there are no completions. | |
83d31a92 | 784 | |
67c296a2 PM |
785 | TEXT is the caller's idea of the "word" we are looking at. |
786 | ||
aff410f1 MS |
787 | LINE_BUFFER is available to be looked at; it contains the entire |
788 | text of the line. | |
67c296a2 PM |
789 | |
790 | POINT is the offset in that line of the cursor. You | |
791 | should pretend that the line ends at POINT. */ | |
14032a66 | 792 | |
49c4e619 | 793 | VEC (char_ptr) * |
1834676b | 794 | complete_line (const char *text, const char *line_buffer, int point) |
14032a66 | 795 | { |
aff410f1 MS |
796 | return complete_line_internal (text, line_buffer, |
797 | point, handle_completions); | |
14032a66 TT |
798 | } |
799 | ||
800 | /* Complete on command names. Used by "help". */ | |
49c4e619 | 801 | VEC (char_ptr) * |
aff410f1 | 802 | command_completer (struct cmd_list_element *ignore, |
6f937416 | 803 | const char *text, const char *word) |
14032a66 | 804 | { |
aff410f1 MS |
805 | return complete_line_internal (word, text, |
806 | strlen (text), handle_help); | |
67c296a2 PM |
807 | } |
808 | ||
de0bea00 MF |
809 | /* Complete on signals. */ |
810 | ||
811 | VEC (char_ptr) * | |
812 | signal_completer (struct cmd_list_element *ignore, | |
6f937416 | 813 | const char *text, const char *word) |
de0bea00 | 814 | { |
de0bea00 MF |
815 | VEC (char_ptr) *return_val = NULL; |
816 | size_t len = strlen (word); | |
817 | enum gdb_signal signum; | |
818 | const char *signame; | |
819 | ||
820 | for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum) | |
821 | { | |
822 | /* Can't handle this, so skip it. */ | |
823 | if (signum == GDB_SIGNAL_0) | |
824 | continue; | |
825 | ||
826 | signame = gdb_signal_to_name (signum); | |
827 | ||
828 | /* Ignore the unknown signal case. */ | |
829 | if (!signame || strcmp (signame, "?") == 0) | |
830 | continue; | |
831 | ||
832 | if (strncasecmp (signame, word, len) == 0) | |
833 | VEC_safe_push (char_ptr, return_val, xstrdup (signame)); | |
834 | } | |
835 | ||
836 | return return_val; | |
837 | } | |
838 | ||
67c296a2 PM |
839 | /* Get the list of chars that are considered as word breaks |
840 | for the current command. */ | |
841 | ||
842 | char * | |
843 | gdb_completion_word_break_characters (void) | |
844 | { | |
49c4e619 | 845 | VEC (char_ptr) *list; |
c5504eaf | 846 | |
67c296a2 PM |
847 | list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point, |
848 | handle_brkchars); | |
849 | gdb_assert (list == NULL); | |
850 | return rl_completer_word_break_characters; | |
14032a66 TT |
851 | } |
852 | ||
aff410f1 MS |
853 | /* Generate completions one by one for the completer. Each time we |
854 | are called return another potential completion to the caller. | |
855 | line_completion just completes on commands or passes the buck to | |
856 | the command's completer function, the stuff specific to symbol | |
857 | completion is in make_symbol_completion_list. | |
83d31a92 TT |
858 | |
859 | TEXT is the caller's idea of the "word" we are looking at. | |
860 | ||
aff410f1 MS |
861 | MATCHES is the number of matches that have currently been collected |
862 | from calling this completion function. When zero, then we need to | |
863 | initialize, otherwise the initialization has already taken place | |
864 | and we can just return the next potential completion string. | |
83d31a92 | 865 | |
aff410f1 MS |
866 | LINE_BUFFER is available to be looked at; it contains the entire |
867 | text of the line. POINT is the offset in that line of the cursor. | |
868 | You should pretend that the line ends at POINT. | |
83d31a92 | 869 | |
aff410f1 MS |
870 | Returns NULL if there are no more completions, else a pointer to a |
871 | string which is a possible completion, it is the caller's | |
872 | responsibility to free the string. */ | |
83d31a92 | 873 | |
38017ce8 | 874 | static char * |
9c3f90bd MS |
875 | line_completion_function (const char *text, int matches, |
876 | char *line_buffer, int point) | |
83d31a92 | 877 | { |
49c4e619 | 878 | static VEC (char_ptr) *list = NULL; /* Cache of completions. */ |
9c3f90bd | 879 | static int index; /* Next cached completion. */ |
83d31a92 TT |
880 | char *output = NULL; |
881 | ||
882 | if (matches == 0) | |
883 | { | |
aff410f1 MS |
884 | /* The caller is beginning to accumulate a new set of |
885 | completions, so we need to find all of them now, and cache | |
886 | them for returning one at a time on future calls. */ | |
83d31a92 TT |
887 | |
888 | if (list) | |
889 | { | |
aff410f1 MS |
890 | /* Free the storage used by LIST, but not by the strings |
891 | inside. This is because rl_complete_internal () frees | |
892 | the strings. As complete_line may abort by calling | |
893 | `error' clear LIST now. */ | |
49c4e619 | 894 | VEC_free (char_ptr, list); |
c5f0f3d0 | 895 | } |
83d31a92 TT |
896 | index = 0; |
897 | list = complete_line (text, line_buffer, point); | |
c5f0f3d0 FN |
898 | } |
899 | ||
aff410f1 | 900 | /* If we found a list of potential completions during initialization |
49c4e619 TT |
901 | then dole them out one at a time. After returning the last one, |
902 | return NULL (and continue to do so) each time we are called after | |
903 | that, until a new list is available. */ | |
c5f0f3d0 FN |
904 | |
905 | if (list) | |
906 | { | |
49c4e619 | 907 | if (index < VEC_length (char_ptr, list)) |
c5f0f3d0 | 908 | { |
49c4e619 | 909 | output = VEC_index (char_ptr, list, index); |
c5f0f3d0 FN |
910 | index++; |
911 | } | |
912 | } | |
913 | ||
914 | #if 0 | |
915 | /* Can't do this because readline hasn't yet checked the word breaks | |
916 | for figuring out whether to insert a quote. */ | |
917 | if (output == NULL) | |
aff410f1 MS |
918 | /* Make sure the word break characters are set back to normal for |
919 | the next time that readline tries to complete something. */ | |
c5f0f3d0 | 920 | rl_completer_word_break_characters = |
51065942 | 921 | current_language->la_word_break_characters(); |
c5f0f3d0 FN |
922 | #endif |
923 | ||
924 | return (output); | |
925 | } | |
4e87b832 KD |
926 | |
927 | /* Skip over the possibly quoted word STR (as defined by the quote | |
b021a221 MS |
928 | characters QUOTECHARS and the word break characters BREAKCHARS). |
929 | Returns pointer to the location after the "word". If either | |
930 | QUOTECHARS or BREAKCHARS is NULL, use the same values used by the | |
931 | completer. */ | |
c5f0f3d0 | 932 | |
d7561cbb KS |
933 | const char * |
934 | skip_quoted_chars (const char *str, const char *quotechars, | |
935 | const char *breakchars) | |
c5f0f3d0 FN |
936 | { |
937 | char quote_char = '\0'; | |
d7561cbb | 938 | const char *scan; |
c5f0f3d0 | 939 | |
4e87b832 KD |
940 | if (quotechars == NULL) |
941 | quotechars = gdb_completer_quote_characters; | |
942 | ||
943 | if (breakchars == NULL) | |
51065942 | 944 | breakchars = current_language->la_word_break_characters(); |
4e87b832 | 945 | |
c5f0f3d0 FN |
946 | for (scan = str; *scan != '\0'; scan++) |
947 | { | |
948 | if (quote_char != '\0') | |
949 | { | |
9c3f90bd | 950 | /* Ignore everything until the matching close quote char. */ |
c5f0f3d0 FN |
951 | if (*scan == quote_char) |
952 | { | |
9c3f90bd | 953 | /* Found matching close quote. */ |
c5f0f3d0 FN |
954 | scan++; |
955 | break; | |
956 | } | |
957 | } | |
4e87b832 | 958 | else if (strchr (quotechars, *scan)) |
c5f0f3d0 | 959 | { |
aff410f1 | 960 | /* Found start of a quoted string. */ |
c5f0f3d0 FN |
961 | quote_char = *scan; |
962 | } | |
4e87b832 | 963 | else if (strchr (breakchars, *scan)) |
c5f0f3d0 FN |
964 | { |
965 | break; | |
966 | } | |
967 | } | |
4e87b832 | 968 | |
c5f0f3d0 FN |
969 | return (scan); |
970 | } | |
971 | ||
4e87b832 KD |
972 | /* Skip over the possibly quoted word STR (as defined by the quote |
973 | characters and word break characters used by the completer). | |
9c3f90bd | 974 | Returns pointer to the location after the "word". */ |
4e87b832 | 975 | |
d7561cbb KS |
976 | const char * |
977 | skip_quoted (const char *str) | |
4e87b832 KD |
978 | { |
979 | return skip_quoted_chars (str, NULL, NULL); | |
980 | } |