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