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