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