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