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