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