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 MS |
109 | noop_completer (struct cmd_list_element *ignore, |
110 | char *text, char *prefix) | |
d75b5104 EZ |
111 | { |
112 | return NULL; | |
113 | } | |
114 | ||
c5f0f3d0 | 115 | /* Complete on filenames. */ |
49c4e619 | 116 | VEC (char_ptr) * |
aff410f1 MS |
117 | filename_completer (struct cmd_list_element *ignore, |
118 | char *text, 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 MS |
186 | location_completer (struct cmd_list_element *ignore, |
187 | char *text, 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; | |
c94fdfd0 EZ |
192 | char *p; |
193 | int quote_found = 0; | |
194 | int quoted = *text == '\'' || *text == '"'; | |
195 | int quote_char = '\0'; | |
196 | char *colon = NULL; | |
197 | char *file_to_match = NULL; | |
198 | char *symbol_start = text; | |
199 | char *orig_text = text; | |
200 | size_t text_len; | |
201 | ||
202 | /* Do we have an unquoted colon, as in "break foo.c::bar"? */ | |
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 | { | |
288 | for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, p); ++ix) | |
289 | VEC_safe_push (char_ptr, list, p); | |
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 | { | |
299 | /* If we only have file names as possible completion, we should | |
300 | bring them in sync with what rl_complete expects. The | |
301 | problem is that if the user types "break /foo/b TAB", and the | |
302 | possible completions are "/foo/bar" and "/foo/baz" | |
303 | rl_complete expects us to return "bar" and "baz", without the | |
304 | leading directories, as possible completions, because `word' | |
305 | starts at the "b". But we ignore the value of `word' when we | |
306 | call make_source_files_completion_list above (because that | |
307 | would not DTRT when the completion results in both symbols | |
308 | and file names), so make_source_files_completion_list returns | |
309 | the full "/foo/bar" and "/foo/baz" strings. This produces | |
310 | wrong results when, e.g., there's only one possible | |
311 | completion, because rl_complete will prepend "/foo/" to each | |
312 | candidate completion. The loop below removes that leading | |
313 | part. */ | |
49c4e619 | 314 | for (ix = 0; VEC_iterate (char_ptr, list, ix, p); ++ix) |
c94fdfd0 | 315 | { |
49c4e619 TT |
316 | memmove (p, p + (word - text), |
317 | strlen (p) + 1 - (word - text)); | |
c94fdfd0 | 318 | } |
c94fdfd0 EZ |
319 | } |
320 | else if (!n_syms) | |
321 | { | |
322 | /* No completions at all. As the final resort, try completing | |
323 | on the entire text as a symbol. */ | |
324 | list = make_symbol_completion_list (orig_text, word); | |
325 | } | |
326 | ||
327 | return list; | |
328 | } | |
329 | ||
1c71341a TT |
330 | /* Helper for expression_completer which recursively adds field and |
331 | method names from TYPE, a struct or union type, to the array | |
49c4e619 | 332 | OUTPUT. */ |
65d12d83 | 333 | static void |
49c4e619 | 334 | add_struct_fields (struct type *type, VEC (char_ptr) **output, |
65d12d83 TT |
335 | char *fieldname, int namelen) |
336 | { | |
337 | int i; | |
b32d97f3 | 338 | int computed_type_name = 0; |
0d5cff50 | 339 | const char *type_name = NULL; |
65d12d83 TT |
340 | |
341 | CHECK_TYPEDEF (type); | |
342 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
343 | { | |
344 | if (i < TYPE_N_BASECLASSES (type)) | |
49c4e619 | 345 | add_struct_fields (TYPE_BASECLASS (type, i), |
aff410f1 | 346 | output, fieldname, namelen); |
9ae8282d | 347 | else if (TYPE_FIELD_NAME (type, i)) |
65d12d83 | 348 | { |
9ae8282d TT |
349 | if (TYPE_FIELD_NAME (type, i)[0] != '\0') |
350 | { | |
aff410f1 MS |
351 | if (! strncmp (TYPE_FIELD_NAME (type, i), |
352 | fieldname, namelen)) | |
49c4e619 TT |
353 | VEC_safe_push (char_ptr, *output, |
354 | xstrdup (TYPE_FIELD_NAME (type, i))); | |
9ae8282d TT |
355 | } |
356 | else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION) | |
357 | { | |
358 | /* Recurse into anonymous unions. */ | |
49c4e619 | 359 | add_struct_fields (TYPE_FIELD_TYPE (type, i), |
aff410f1 | 360 | output, fieldname, namelen); |
9ae8282d | 361 | } |
65d12d83 TT |
362 | } |
363 | } | |
1c71341a TT |
364 | |
365 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) | |
366 | { | |
0d5cff50 | 367 | const char *name = TYPE_FN_FIELDLIST_NAME (type, i); |
c5504eaf | 368 | |
1c71341a TT |
369 | if (name && ! strncmp (name, fieldname, namelen)) |
370 | { | |
b32d97f3 TT |
371 | if (!computed_type_name) |
372 | { | |
373 | type_name = type_name_no_tag (type); | |
374 | computed_type_name = 1; | |
375 | } | |
1c71341a | 376 | /* Omit constructors from the completion list. */ |
907af001 | 377 | if (!type_name || strcmp (type_name, name)) |
49c4e619 | 378 | VEC_safe_push (char_ptr, *output, xstrdup (name)); |
1c71341a TT |
379 | } |
380 | } | |
65d12d83 TT |
381 | } |
382 | ||
383 | /* Complete on expressions. Often this means completing on symbol | |
384 | names, but some language parsers also have support for completing | |
385 | field names. */ | |
49c4e619 | 386 | VEC (char_ptr) * |
aff410f1 MS |
387 | expression_completer (struct cmd_list_element *ignore, |
388 | char *text, char *word) | |
65d12d83 | 389 | { |
c92817ce | 390 | struct type *type = NULL; |
37cd5d19 | 391 | char *fieldname, *p; |
c92817ce | 392 | volatile struct gdb_exception except; |
2f68a895 | 393 | enum type_code code = TYPE_CODE_UNDEF; |
65d12d83 TT |
394 | |
395 | /* Perform a tentative parse of the expression, to see whether a | |
396 | field completion is required. */ | |
397 | fieldname = NULL; | |
c92817ce TT |
398 | TRY_CATCH (except, RETURN_MASK_ERROR) |
399 | { | |
2f68a895 | 400 | type = parse_expression_for_completion (text, &fieldname, &code); |
c92817ce TT |
401 | } |
402 | if (except.reason < 0) | |
403 | return NULL; | |
65d12d83 TT |
404 | if (fieldname && type) |
405 | { | |
406 | for (;;) | |
407 | { | |
408 | CHECK_TYPEDEF (type); | |
409 | if (TYPE_CODE (type) != TYPE_CODE_PTR | |
410 | && TYPE_CODE (type) != TYPE_CODE_REF) | |
411 | break; | |
412 | type = TYPE_TARGET_TYPE (type); | |
413 | } | |
414 | ||
415 | if (TYPE_CODE (type) == TYPE_CODE_UNION | |
416 | || TYPE_CODE (type) == TYPE_CODE_STRUCT) | |
417 | { | |
65d12d83 | 418 | int flen = strlen (fieldname); |
49c4e619 | 419 | VEC (char_ptr) *result = NULL; |
65d12d83 | 420 | |
49c4e619 | 421 | add_struct_fields (type, &result, fieldname, flen); |
a0b7aece | 422 | xfree (fieldname); |
65d12d83 TT |
423 | return result; |
424 | } | |
425 | } | |
2f68a895 TT |
426 | else if (fieldname && code != TYPE_CODE_UNDEF) |
427 | { | |
428 | VEC (char_ptr) *result; | |
429 | struct cleanup *cleanup = make_cleanup (xfree, fieldname); | |
430 | ||
431 | result = make_symbol_completion_type (fieldname, fieldname, code); | |
432 | do_cleanups (cleanup); | |
433 | return result; | |
434 | } | |
a0b7aece | 435 | xfree (fieldname); |
65d12d83 | 436 | |
37cd5d19 TT |
437 | /* Commands which complete on locations want to see the entire |
438 | argument. */ | |
439 | for (p = word; | |
440 | p > text && p[-1] != ' ' && p[-1] != '\t'; | |
441 | p--) | |
442 | ; | |
443 | ||
aff410f1 | 444 | /* Not ideal but it is what we used to do before... */ |
d8906c6f | 445 | return location_completer (ignore, p, word); |
65d12d83 TT |
446 | } |
447 | ||
aff410f1 MS |
448 | /* Here are some useful test cases for completion. FIXME: These |
449 | should be put in the test suite. They should be tested with both | |
450 | M-? and TAB. | |
c5f0f3d0 FN |
451 | |
452 | "show output-" "radix" | |
453 | "show output" "-radix" | |
454 | "p" ambiguous (commands starting with p--path, print, printf, etc.) | |
455 | "p " ambiguous (all symbols) | |
456 | "info t foo" no completions | |
457 | "info t " no completions | |
458 | "info t" ambiguous ("info target", "info terminal", etc.) | |
459 | "info ajksdlfk" no completions | |
460 | "info ajksdlfk " no completions | |
461 | "info" " " | |
462 | "info " ambiguous (all info commands) | |
463 | "p \"a" no completions (string constant) | |
464 | "p 'a" ambiguous (all symbols starting with a) | |
465 | "p b-a" ambiguous (all symbols starting with a) | |
466 | "p b-" ambiguous (all symbols) | |
467 | "file Make" "file" (word break hard to screw up here) | |
468 | "file ../gdb.stabs/we" "ird" (needs to not break word at slash) | |
469 | */ | |
470 | ||
67c296a2 PM |
471 | typedef enum |
472 | { | |
473 | handle_brkchars, | |
474 | handle_completions, | |
475 | handle_help | |
476 | } | |
477 | complete_line_internal_reason; | |
478 | ||
479 | ||
480 | /* Internal function used to handle completions. | |
481 | ||
c5f0f3d0 FN |
482 | |
483 | TEXT is the caller's idea of the "word" we are looking at. | |
484 | ||
aff410f1 MS |
485 | LINE_BUFFER is available to be looked at; it contains the entire |
486 | text of the line. POINT is the offset in that line of the cursor. | |
487 | You should pretend that the line ends at POINT. | |
67c296a2 PM |
488 | |
489 | REASON is of type complete_line_internal_reason. | |
490 | ||
491 | If REASON is handle_brkchars: | |
aff410f1 MS |
492 | Preliminary phase, called by gdb_completion_word_break_characters |
493 | function, is used to determine the correct set of chars that are | |
494 | word delimiters depending on the current command in line_buffer. | |
495 | No completion list should be generated; the return value should be | |
496 | NULL. This is checked by an assertion in that function. | |
67c296a2 PM |
497 | |
498 | If REASON is handle_completions: | |
499 | Main phase, called by complete_line function, is used to get the list | |
500 | of posible completions. | |
501 | ||
502 | If REASON is handle_help: | |
503 | Special case when completing a 'help' command. In this case, | |
14032a66 | 504 | once sub-command completions are exhausted, we simply return NULL. |
67c296a2 | 505 | */ |
14032a66 | 506 | |
49c4e619 | 507 | static VEC (char_ptr) * |
aff410f1 MS |
508 | complete_line_internal (const char *text, |
509 | char *line_buffer, int point, | |
67c296a2 | 510 | complete_line_internal_reason reason) |
c5f0f3d0 | 511 | { |
49c4e619 | 512 | VEC (char_ptr) *list = NULL; |
c5f0f3d0 | 513 | char *tmp_command, *p; |
ace21957 | 514 | int ignore_help_classes; |
c5f0f3d0 FN |
515 | /* Pointer within tmp_command which corresponds to text. */ |
516 | char *word; | |
517 | struct cmd_list_element *c, *result_list; | |
518 | ||
aff410f1 MS |
519 | /* Choose the default set of word break characters to break |
520 | completions. If we later find out that we are doing completions | |
521 | on command strings (as opposed to strings supplied by the | |
522 | individual command completer functions, which can be any string) | |
523 | then we will switch to the special word break set for command | |
524 | strings, which leaves out the '-' character used in some | |
525 | commands. */ | |
83d31a92 | 526 | rl_completer_word_break_characters = |
51065942 | 527 | current_language->la_word_break_characters(); |
c5f0f3d0 | 528 | |
aff410f1 MS |
529 | /* Decide whether to complete on a list of gdb commands or on |
530 | symbols. */ | |
83d31a92 TT |
531 | tmp_command = (char *) alloca (point + 1); |
532 | p = tmp_command; | |
c5f0f3d0 | 533 | |
ace21957 MF |
534 | /* The help command should complete help aliases. */ |
535 | ignore_help_classes = reason != handle_help; | |
536 | ||
83d31a92 TT |
537 | strncpy (tmp_command, line_buffer, point); |
538 | tmp_command[point] = '\0'; | |
539 | /* Since text always contains some number of characters leading up | |
540 | to point, we can find the equivalent position in tmp_command | |
541 | by subtracting that many characters from the end of tmp_command. */ | |
542 | word = tmp_command + point - strlen (text); | |
c5f0f3d0 | 543 | |
83d31a92 TT |
544 | if (point == 0) |
545 | { | |
546 | /* An empty line we want to consider ambiguous; that is, it | |
547 | could be any command. */ | |
1427fe5e | 548 | c = CMD_LIST_AMBIGUOUS; |
83d31a92 TT |
549 | result_list = 0; |
550 | } | |
551 | else | |
552 | { | |
ace21957 | 553 | c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes); |
83d31a92 | 554 | } |
c5f0f3d0 | 555 | |
83d31a92 TT |
556 | /* Move p up to the next interesting thing. */ |
557 | while (*p == ' ' || *p == '\t') | |
558 | { | |
559 | p++; | |
560 | } | |
c5f0f3d0 | 561 | |
83d31a92 TT |
562 | if (!c) |
563 | { | |
564 | /* It is an unrecognized command. So there are no | |
565 | possible completions. */ | |
566 | list = NULL; | |
567 | } | |
1427fe5e | 568 | else if (c == CMD_LIST_AMBIGUOUS) |
83d31a92 TT |
569 | { |
570 | char *q; | |
571 | ||
572 | /* lookup_cmd_1 advances p up to the first ambiguous thing, but | |
573 | doesn't advance over that thing itself. Do so now. */ | |
574 | q = p; | |
575 | while (*q && (isalnum (*q) || *q == '-' || *q == '_')) | |
576 | ++q; | |
577 | if (q != tmp_command + point) | |
c5f0f3d0 | 578 | { |
83d31a92 TT |
579 | /* There is something beyond the ambiguous |
580 | command, so there are no possible completions. For | |
581 | example, "info t " or "info t foo" does not complete | |
582 | to anything, because "info t" can be "info target" or | |
583 | "info terminal". */ | |
c5f0f3d0 FN |
584 | list = NULL; |
585 | } | |
83d31a92 | 586 | else |
c5f0f3d0 | 587 | { |
83d31a92 TT |
588 | /* We're trying to complete on the command which was ambiguous. |
589 | This we can deal with. */ | |
590 | if (result_list) | |
c5f0f3d0 | 591 | { |
67c296a2 PM |
592 | if (reason != handle_brkchars) |
593 | list = complete_on_cmdlist (*result_list->prefixlist, p, | |
ace21957 | 594 | word, ignore_help_classes); |
c5f0f3d0 FN |
595 | } |
596 | else | |
597 | { | |
67c296a2 | 598 | if (reason != handle_brkchars) |
ace21957 MF |
599 | list = complete_on_cmdlist (cmdlist, p, word, |
600 | ignore_help_classes); | |
c5f0f3d0 | 601 | } |
489f0516 | 602 | /* Ensure that readline does the right thing with respect to |
83d31a92 TT |
603 | inserting quotes. */ |
604 | rl_completer_word_break_characters = | |
605 | gdb_completer_command_word_break_characters; | |
c5f0f3d0 | 606 | } |
83d31a92 TT |
607 | } |
608 | else | |
609 | { | |
610 | /* We've recognized a full command. */ | |
611 | ||
612 | if (p == tmp_command + point) | |
c5f0f3d0 | 613 | { |
aff410f1 MS |
614 | /* There is no non-whitespace in the line beyond the |
615 | command. */ | |
c5f0f3d0 | 616 | |
83d31a92 | 617 | if (p[-1] == ' ' || p[-1] == '\t') |
c5f0f3d0 | 618 | { |
aff410f1 MS |
619 | /* The command is followed by whitespace; we need to |
620 | complete on whatever comes after command. */ | |
83d31a92 | 621 | if (c->prefixlist) |
c5f0f3d0 | 622 | { |
83d31a92 TT |
623 | /* It is a prefix command; what comes after it is |
624 | a subcommand (e.g. "info "). */ | |
67c296a2 | 625 | if (reason != handle_brkchars) |
ace21957 MF |
626 | list = complete_on_cmdlist (*c->prefixlist, p, word, |
627 | ignore_help_classes); | |
c5f0f3d0 | 628 | |
489f0516 | 629 | /* Ensure that readline does the right thing |
9c3f90bd | 630 | with respect to inserting quotes. */ |
c5f0f3d0 FN |
631 | rl_completer_word_break_characters = |
632 | gdb_completer_command_word_break_characters; | |
633 | } | |
67c296a2 | 634 | else if (reason == handle_help) |
14032a66 | 635 | list = NULL; |
c5f0f3d0 FN |
636 | else if (c->enums) |
637 | { | |
67c296a2 PM |
638 | if (reason != handle_brkchars) |
639 | list = complete_on_enum (c->enums, p, word); | |
83d31a92 TT |
640 | rl_completer_word_break_characters = |
641 | gdb_completer_command_word_break_characters; | |
c5f0f3d0 FN |
642 | } |
643 | else | |
644 | { | |
83d31a92 TT |
645 | /* It is a normal command; what comes after it is |
646 | completed by the command's completer function. */ | |
c5f0f3d0 | 647 | if (c->completer == filename_completer) |
7830cf6f | 648 | { |
83d31a92 TT |
649 | /* Many commands which want to complete on |
650 | file names accept several file names, as | |
651 | in "run foo bar >>baz". So we don't want | |
652 | to complete the entire text after the | |
653 | command, just the last word. To this | |
654 | end, we need to find the beginning of the | |
655 | file name by starting at `word' and going | |
656 | backwards. */ | |
7830cf6f EZ |
657 | for (p = word; |
658 | p > tmp_command | |
659 | && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL; | |
660 | p--) | |
661 | ; | |
662 | rl_completer_word_break_characters = | |
663 | gdb_completer_file_name_break_characters; | |
664 | } | |
37cd5d19 | 665 | else if (c->completer == location_completer) |
c94fdfd0 | 666 | { |
83d31a92 TT |
667 | /* Commands which complete on locations want to |
668 | see the entire argument. */ | |
c94fdfd0 EZ |
669 | for (p = word; |
670 | p > tmp_command | |
671 | && p[-1] != ' ' && p[-1] != '\t'; | |
672 | p--) | |
673 | ; | |
674 | } | |
2d9c5cff | 675 | if (reason != handle_brkchars && c->completer != NULL) |
67c296a2 | 676 | list = (*c->completer) (c, p, word); |
c5f0f3d0 FN |
677 | } |
678 | } | |
83d31a92 TT |
679 | else |
680 | { | |
681 | /* The command is not followed by whitespace; we need to | |
aff410f1 | 682 | complete on the command itself, e.g. "p" which is a |
83d31a92 TT |
683 | command itself but also can complete to "print", "ptype" |
684 | etc. */ | |
685 | char *q; | |
686 | ||
687 | /* Find the command we are completing on. */ | |
688 | q = p; | |
689 | while (q > tmp_command) | |
690 | { | |
691 | if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') | |
692 | --q; | |
693 | else | |
694 | break; | |
695 | } | |
696 | ||
67c296a2 | 697 | if (reason != handle_brkchars) |
ace21957 MF |
698 | list = complete_on_cmdlist (result_list, q, word, |
699 | ignore_help_classes); | |
83d31a92 | 700 | |
489f0516 | 701 | /* Ensure that readline does the right thing |
9c3f90bd | 702 | with respect to inserting quotes. */ |
83d31a92 TT |
703 | rl_completer_word_break_characters = |
704 | gdb_completer_command_word_break_characters; | |
705 | } | |
706 | } | |
67c296a2 | 707 | else if (reason == handle_help) |
14032a66 | 708 | list = NULL; |
83d31a92 TT |
709 | else |
710 | { | |
711 | /* There is non-whitespace beyond the command. */ | |
712 | ||
713 | if (c->prefixlist && !c->allow_unknown) | |
714 | { | |
715 | /* It is an unrecognized subcommand of a prefix command, | |
716 | e.g. "info adsfkdj". */ | |
717 | list = NULL; | |
718 | } | |
719 | else if (c->enums) | |
720 | { | |
67c296a2 PM |
721 | if (reason != handle_brkchars) |
722 | list = complete_on_enum (c->enums, p, word); | |
83d31a92 TT |
723 | } |
724 | else | |
725 | { | |
726 | /* It is a normal command. */ | |
727 | if (c->completer == filename_completer) | |
728 | { | |
729 | /* See the commentary above about the specifics | |
730 | of file-name completion. */ | |
731 | for (p = word; | |
732 | p > tmp_command | |
aff410f1 MS |
733 | && strchr (gdb_completer_file_name_break_characters, |
734 | p[-1]) == NULL; | |
83d31a92 TT |
735 | p--) |
736 | ; | |
737 | rl_completer_word_break_characters = | |
738 | gdb_completer_file_name_break_characters; | |
739 | } | |
37cd5d19 | 740 | else if (c->completer == location_completer) |
83d31a92 TT |
741 | { |
742 | for (p = word; | |
743 | p > tmp_command | |
744 | && p[-1] != ' ' && p[-1] != '\t'; | |
745 | p--) | |
746 | ; | |
747 | } | |
2d9c5cff | 748 | if (reason != handle_brkchars && c->completer != NULL) |
67c296a2 | 749 | list = (*c->completer) (c, p, word); |
83d31a92 TT |
750 | } |
751 | } | |
752 | } | |
753 | ||
754 | return list; | |
755 | } | |
49c4e619 TT |
756 | /* Generate completions all at once. Returns a vector of strings. |
757 | Each element is allocated with xmalloc. It can also return NULL if | |
758 | there are no completions. | |
83d31a92 | 759 | |
67c296a2 PM |
760 | TEXT is the caller's idea of the "word" we are looking at. |
761 | ||
aff410f1 MS |
762 | LINE_BUFFER is available to be looked at; it contains the entire |
763 | text of the line. | |
67c296a2 PM |
764 | |
765 | POINT is the offset in that line of the cursor. You | |
766 | should pretend that the line ends at POINT. */ | |
14032a66 | 767 | |
49c4e619 | 768 | VEC (char_ptr) * |
14032a66 TT |
769 | complete_line (const char *text, char *line_buffer, int point) |
770 | { | |
aff410f1 MS |
771 | return complete_line_internal (text, line_buffer, |
772 | point, handle_completions); | |
14032a66 TT |
773 | } |
774 | ||
775 | /* Complete on command names. Used by "help". */ | |
49c4e619 | 776 | VEC (char_ptr) * |
aff410f1 MS |
777 | command_completer (struct cmd_list_element *ignore, |
778 | char *text, char *word) | |
14032a66 | 779 | { |
aff410f1 MS |
780 | return complete_line_internal (word, text, |
781 | strlen (text), handle_help); | |
67c296a2 PM |
782 | } |
783 | ||
de0bea00 MF |
784 | /* Complete on signals. */ |
785 | ||
786 | VEC (char_ptr) * | |
787 | signal_completer (struct cmd_list_element *ignore, | |
788 | char *text, char *word) | |
789 | { | |
790 | int i; | |
791 | VEC (char_ptr) *return_val = NULL; | |
792 | size_t len = strlen (word); | |
793 | enum gdb_signal signum; | |
794 | const char *signame; | |
795 | ||
796 | for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum) | |
797 | { | |
798 | /* Can't handle this, so skip it. */ | |
799 | if (signum == GDB_SIGNAL_0) | |
800 | continue; | |
801 | ||
802 | signame = gdb_signal_to_name (signum); | |
803 | ||
804 | /* Ignore the unknown signal case. */ | |
805 | if (!signame || strcmp (signame, "?") == 0) | |
806 | continue; | |
807 | ||
808 | if (strncasecmp (signame, word, len) == 0) | |
809 | VEC_safe_push (char_ptr, return_val, xstrdup (signame)); | |
810 | } | |
811 | ||
812 | return return_val; | |
813 | } | |
814 | ||
67c296a2 PM |
815 | /* Get the list of chars that are considered as word breaks |
816 | for the current command. */ | |
817 | ||
818 | char * | |
819 | gdb_completion_word_break_characters (void) | |
820 | { | |
49c4e619 | 821 | VEC (char_ptr) *list; |
c5504eaf | 822 | |
67c296a2 PM |
823 | list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point, |
824 | handle_brkchars); | |
825 | gdb_assert (list == NULL); | |
826 | return rl_completer_word_break_characters; | |
14032a66 TT |
827 | } |
828 | ||
aff410f1 MS |
829 | /* Generate completions one by one for the completer. Each time we |
830 | are called return another potential completion to the caller. | |
831 | line_completion just completes on commands or passes the buck to | |
832 | the command's completer function, the stuff specific to symbol | |
833 | completion is in make_symbol_completion_list. | |
83d31a92 TT |
834 | |
835 | TEXT is the caller's idea of the "word" we are looking at. | |
836 | ||
aff410f1 MS |
837 | MATCHES is the number of matches that have currently been collected |
838 | from calling this completion function. When zero, then we need to | |
839 | initialize, otherwise the initialization has already taken place | |
840 | and we can just return the next potential completion string. | |
83d31a92 | 841 | |
aff410f1 MS |
842 | LINE_BUFFER is available to be looked at; it contains the entire |
843 | text of the line. POINT is the offset in that line of the cursor. | |
844 | You should pretend that the line ends at POINT. | |
83d31a92 | 845 | |
aff410f1 MS |
846 | Returns NULL if there are no more completions, else a pointer to a |
847 | string which is a possible completion, it is the caller's | |
848 | responsibility to free the string. */ | |
83d31a92 | 849 | |
38017ce8 | 850 | static char * |
9c3f90bd MS |
851 | line_completion_function (const char *text, int matches, |
852 | char *line_buffer, int point) | |
83d31a92 | 853 | { |
49c4e619 | 854 | static VEC (char_ptr) *list = NULL; /* Cache of completions. */ |
9c3f90bd | 855 | static int index; /* Next cached completion. */ |
83d31a92 TT |
856 | char *output = NULL; |
857 | ||
858 | if (matches == 0) | |
859 | { | |
aff410f1 MS |
860 | /* The caller is beginning to accumulate a new set of |
861 | completions, so we need to find all of them now, and cache | |
862 | them for returning one at a time on future calls. */ | |
83d31a92 TT |
863 | |
864 | if (list) | |
865 | { | |
aff410f1 MS |
866 | /* Free the storage used by LIST, but not by the strings |
867 | inside. This is because rl_complete_internal () frees | |
868 | the strings. As complete_line may abort by calling | |
869 | `error' clear LIST now. */ | |
49c4e619 | 870 | VEC_free (char_ptr, list); |
c5f0f3d0 | 871 | } |
83d31a92 TT |
872 | index = 0; |
873 | list = complete_line (text, line_buffer, point); | |
c5f0f3d0 FN |
874 | } |
875 | ||
aff410f1 | 876 | /* If we found a list of potential completions during initialization |
49c4e619 TT |
877 | then dole them out one at a time. After returning the last one, |
878 | return NULL (and continue to do so) each time we are called after | |
879 | that, until a new list is available. */ | |
c5f0f3d0 FN |
880 | |
881 | if (list) | |
882 | { | |
49c4e619 | 883 | if (index < VEC_length (char_ptr, list)) |
c5f0f3d0 | 884 | { |
49c4e619 | 885 | output = VEC_index (char_ptr, list, index); |
c5f0f3d0 FN |
886 | index++; |
887 | } | |
888 | } | |
889 | ||
890 | #if 0 | |
891 | /* Can't do this because readline hasn't yet checked the word breaks | |
892 | for figuring out whether to insert a quote. */ | |
893 | if (output == NULL) | |
aff410f1 MS |
894 | /* Make sure the word break characters are set back to normal for |
895 | the next time that readline tries to complete something. */ | |
c5f0f3d0 | 896 | rl_completer_word_break_characters = |
51065942 | 897 | current_language->la_word_break_characters(); |
c5f0f3d0 FN |
898 | #endif |
899 | ||
900 | return (output); | |
901 | } | |
4e87b832 KD |
902 | |
903 | /* Skip over the possibly quoted word STR (as defined by the quote | |
b021a221 MS |
904 | characters QUOTECHARS and the word break characters BREAKCHARS). |
905 | Returns pointer to the location after the "word". If either | |
906 | QUOTECHARS or BREAKCHARS is NULL, use the same values used by the | |
907 | completer. */ | |
c5f0f3d0 FN |
908 | |
909 | char * | |
4e87b832 | 910 | skip_quoted_chars (char *str, char *quotechars, char *breakchars) |
c5f0f3d0 FN |
911 | { |
912 | char quote_char = '\0'; | |
913 | char *scan; | |
914 | ||
4e87b832 KD |
915 | if (quotechars == NULL) |
916 | quotechars = gdb_completer_quote_characters; | |
917 | ||
918 | if (breakchars == NULL) | |
51065942 | 919 | breakchars = current_language->la_word_break_characters(); |
4e87b832 | 920 | |
c5f0f3d0 FN |
921 | for (scan = str; *scan != '\0'; scan++) |
922 | { | |
923 | if (quote_char != '\0') | |
924 | { | |
9c3f90bd | 925 | /* Ignore everything until the matching close quote char. */ |
c5f0f3d0 FN |
926 | if (*scan == quote_char) |
927 | { | |
9c3f90bd | 928 | /* Found matching close quote. */ |
c5f0f3d0 FN |
929 | scan++; |
930 | break; | |
931 | } | |
932 | } | |
4e87b832 | 933 | else if (strchr (quotechars, *scan)) |
c5f0f3d0 | 934 | { |
aff410f1 | 935 | /* Found start of a quoted string. */ |
c5f0f3d0 FN |
936 | quote_char = *scan; |
937 | } | |
4e87b832 | 938 | else if (strchr (breakchars, *scan)) |
c5f0f3d0 FN |
939 | { |
940 | break; | |
941 | } | |
942 | } | |
4e87b832 | 943 | |
c5f0f3d0 FN |
944 | return (scan); |
945 | } | |
946 | ||
4e87b832 KD |
947 | /* Skip over the possibly quoted word STR (as defined by the quote |
948 | characters and word break characters used by the completer). | |
9c3f90bd | 949 | Returns pointer to the location after the "word". */ |
4e87b832 KD |
950 | |
951 | char * | |
952 | skip_quoted (char *str) | |
953 | { | |
954 | return skip_quoted_chars (str, NULL, NULL); | |
955 | } |