* s390-mkopc.c (main): Exit with error 1 if sscanf fails
[deliverable/binutils-gdb.git] / gdb / completer.c
CommitLineData
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"
c92817ce 27#include "exceptions.h"
c5f0f3d0 28
18a642a1
AC
29#include "cli/cli-decode.h"
30
03717487
MS
31/* FIXME: This is needed because of lookup_cmd_1 (). We should be
32 calling a hook instead so we eliminate the CLI dependency. */
c5f0f3d0
FN
33#include "gdbcmd.h"
34
c94fdfd0 35/* Needed for rl_completer_word_break_characters() and for
38017ce8 36 rl_filename_completion_function. */
dbda9972 37#include "readline/readline.h"
c5f0f3d0
FN
38
39/* readline defines this. */
40#undef savestring
41
42#include "completer.h"
43
9c3f90bd 44/* Prototypes for local functions. */
38017ce8 45static
03717487
MS
46char *line_completion_function (const char *text, int matches,
47 char *line_buffer,
d75b5104 48 int point);
c5f0f3d0
FN
49
50/* readline uses the word breaks for two things:
51 (1) In figuring out where to point the TEXT parameter to the
52 rl_completion_entry_function. Since we don't use TEXT for much,
53 it doesn't matter a lot what the word breaks are for this purpose, but
54 it does affect how much stuff M-? lists.
55 (2) If one of the matches contains a word break character, readline
56 will quote it. That's why we switch between
51065942 57 current_language->la_word_break_characters() and
c5f0f3d0
FN
58 gdb_completer_command_word_break_characters. I'm not sure when
59 we need this behavior (perhaps for funky characters in C++ symbols?). */
60
61/* Variables which are necessary for fancy command line editing. */
c5f0f3d0
FN
62
63/* When completing on command names, we remove '-' from the list of
64 word break characters, since we use it in command names. If the
65 readline library sees one in any of the current completion strings,
66 it thinks that the string needs to be quoted and automatically supplies
9c3f90bd 67 a leading quote. */
c5f0f3d0
FN
68static char *gdb_completer_command_word_break_characters =
69" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
70
71/* When completing on file names, we remove from the list of word
72 break characters any characters that are commonly used in file
73 names, such as '-', '+', '~', etc. Otherwise, readline displays
74 incorrect completion candidates. */
c3690141 75#ifdef HAVE_DOS_BASED_FILE_SYSTEM
7830cf6f
EZ
76/* MS-DOS and MS-Windows use colon as part of the drive spec, and most
77 programs support @foo style response files. */
78static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
79#else
80static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
81#endif
c5f0f3d0
FN
82
83/* Characters that can be used to quote completion strings. Note that we
84 can't include '"' because the gdb C parser treats such quoted sequences
9c3f90bd 85 as strings. */
c5f0f3d0
FN
86static char *gdb_completer_quote_characters = "'";
87\f
9c3f90bd 88/* Accessor for some completer data that may interest other files. */
c5f0f3d0 89
c5f0f3d0
FN
90char *
91get_gdb_completer_quote_characters (void)
92{
93 return gdb_completer_quote_characters;
94}
95
d75b5104
EZ
96/* Line completion interface function for readline. */
97
98char *
38017ce8 99readline_line_completion_function (const char *text, int matches)
d75b5104
EZ
100{
101 return line_completion_function (text, matches, rl_line_buffer, rl_point);
102}
103
104/* This can be used for functions which don't want to complete on symbols
105 but don't want to complete on anything else either. */
106char **
d8906c6f 107noop_completer (struct cmd_list_element *ignore, char *text, char *prefix)
d75b5104
EZ
108{
109 return NULL;
110}
111
c5f0f3d0
FN
112/* Complete on filenames. */
113char **
d8906c6f 114filename_completer (struct cmd_list_element *ignore, char *text, char *word)
c5f0f3d0 115{
c5f0f3d0
FN
116 int subsequent_name;
117 char **return_val;
118 int return_val_used;
119 int return_val_alloced;
120
121 return_val_used = 0;
122 /* Small for testing. */
123 return_val_alloced = 1;
124 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
125
126 subsequent_name = 0;
127 while (1)
128 {
1e8189fb 129 char *p, *q;
c5504eaf 130
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. */
197char **
d8906c6f 198location_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
343static int
344count_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
369static void
370add_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);
c5504eaf 394
1c71341a
TT
395 if (name && ! strncmp (name, fieldname, namelen))
396 {
b32d97f3
TT
397 if (!computed_type_name)
398 {
399 type_name = type_name_no_tag (type);
400 computed_type_name = 1;
401 }
1c71341a 402 /* Omit constructors from the completion list. */
907af001 403 if (!type_name || strcmp (type_name, name))
1c71341a
TT
404 {
405 output[*nextp] = xstrdup (name);
406 ++*nextp;
407 }
408 }
409 }
65d12d83
TT
410}
411
412/* Complete on expressions. Often this means completing on symbol
413 names, but some language parsers also have support for completing
414 field names. */
415char **
d8906c6f 416expression_completer (struct cmd_list_element *ignore, char *text, char *word)
65d12d83 417{
c92817ce 418 struct type *type = NULL;
37cd5d19 419 char *fieldname, *p;
c92817ce 420 volatile struct gdb_exception except;
65d12d83
TT
421
422 /* Perform a tentative parse of the expression, to see whether a
423 field completion is required. */
424 fieldname = NULL;
c92817ce
TT
425 TRY_CATCH (except, RETURN_MASK_ERROR)
426 {
427 type = parse_field_expression (text, &fieldname);
428 }
429 if (except.reason < 0)
430 return NULL;
65d12d83
TT
431 if (fieldname && type)
432 {
433 for (;;)
434 {
435 CHECK_TYPEDEF (type);
436 if (TYPE_CODE (type) != TYPE_CODE_PTR
437 && TYPE_CODE (type) != TYPE_CODE_REF)
438 break;
439 type = TYPE_TARGET_TYPE (type);
440 }
441
442 if (TYPE_CODE (type) == TYPE_CODE_UNION
443 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
444 {
445 int alloc = count_struct_fields (type);
446 int flen = strlen (fieldname);
447 int out = 0;
448 char **result = (char **) xmalloc ((alloc + 1) * sizeof (char *));
449
450 add_struct_fields (type, &out, result, fieldname, flen);
451 result[out] = NULL;
a0b7aece 452 xfree (fieldname);
65d12d83
TT
453 return result;
454 }
455 }
a0b7aece 456 xfree (fieldname);
65d12d83 457
37cd5d19
TT
458 /* Commands which complete on locations want to see the entire
459 argument. */
460 for (p = word;
461 p > text && p[-1] != ' ' && p[-1] != '\t';
462 p--)
463 ;
464
65d12d83 465 /* Not ideal but it is what we used to do before... */
d8906c6f 466 return location_completer (ignore, p, word);
65d12d83
TT
467}
468
c5f0f3d0
FN
469/* Here are some useful test cases for completion. FIXME: These should
470 be put in the test suite. They should be tested with both M-? and TAB.
471
472 "show output-" "radix"
473 "show output" "-radix"
474 "p" ambiguous (commands starting with p--path, print, printf, etc.)
475 "p " ambiguous (all symbols)
476 "info t foo" no completions
477 "info t " no completions
478 "info t" ambiguous ("info target", "info terminal", etc.)
479 "info ajksdlfk" no completions
480 "info ajksdlfk " no completions
481 "info" " "
482 "info " ambiguous (all info commands)
483 "p \"a" no completions (string constant)
484 "p 'a" ambiguous (all symbols starting with a)
485 "p b-a" ambiguous (all symbols starting with a)
486 "p b-" ambiguous (all symbols)
487 "file Make" "file" (word break hard to screw up here)
488 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
489 */
490
67c296a2
PM
491typedef enum
492{
493 handle_brkchars,
494 handle_completions,
495 handle_help
496}
497complete_line_internal_reason;
498
499
500/* Internal function used to handle completions.
501
c5f0f3d0
FN
502
503 TEXT is the caller's idea of the "word" we are looking at.
504
c5f0f3d0
FN
505 LINE_BUFFER is available to be looked at; it contains the entire text
506 of the line. POINT is the offset in that line of the cursor. You
14032a66 507 should pretend that the line ends at POINT.
67c296a2
PM
508
509 REASON is of type complete_line_internal_reason.
510
511 If REASON is handle_brkchars:
512 Preliminary phase, called by gdb_completion_word_break_characters function,
513 is used to determine the correct set of chars that are word delimiters
514 depending on the current command in line_buffer.
515 No completion list should be generated; the return value should be NULL.
516 This is checked by an assertion in that function.
517
518 If REASON is handle_completions:
519 Main phase, called by complete_line function, is used to get the list
520 of posible completions.
521
522 If REASON is handle_help:
523 Special case when completing a 'help' command. In this case,
14032a66 524 once sub-command completions are exhausted, we simply return NULL.
67c296a2 525 */
14032a66
TT
526
527static char **
528complete_line_internal (const char *text, char *line_buffer, int point,
67c296a2 529 complete_line_internal_reason reason)
c5f0f3d0 530{
83d31a92 531 char **list = NULL;
c5f0f3d0
FN
532 char *tmp_command, *p;
533 /* Pointer within tmp_command which corresponds to text. */
534 char *word;
535 struct cmd_list_element *c, *result_list;
536
83d31a92
TT
537 /* Choose the default set of word break characters to break completions.
538 If we later find out that we are doing completions on command strings
539 (as opposed to strings supplied by the individual command completer
540 functions, which can be any string) then we will switch to the
541 special word break set for command strings, which leaves out the
542 '-' character used in some commands. */
83d31a92 543 rl_completer_word_break_characters =
51065942 544 current_language->la_word_break_characters();
c5f0f3d0 545
9c3f90bd 546 /* Decide whether to complete on a list of gdb commands or on symbols. */
83d31a92
TT
547 tmp_command = (char *) alloca (point + 1);
548 p = tmp_command;
c5f0f3d0 549
83d31a92
TT
550 strncpy (tmp_command, line_buffer, point);
551 tmp_command[point] = '\0';
552 /* Since text always contains some number of characters leading up
553 to point, we can find the equivalent position in tmp_command
554 by subtracting that many characters from the end of tmp_command. */
555 word = tmp_command + point - strlen (text);
c5f0f3d0 556
83d31a92
TT
557 if (point == 0)
558 {
559 /* An empty line we want to consider ambiguous; that is, it
560 could be any command. */
561 c = (struct cmd_list_element *) -1;
562 result_list = 0;
563 }
564 else
565 {
566 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
567 }
c5f0f3d0 568
83d31a92
TT
569 /* Move p up to the next interesting thing. */
570 while (*p == ' ' || *p == '\t')
571 {
572 p++;
573 }
c5f0f3d0 574
83d31a92
TT
575 if (!c)
576 {
577 /* It is an unrecognized command. So there are no
578 possible completions. */
579 list = NULL;
580 }
581 else if (c == (struct cmd_list_element *) -1)
582 {
583 char *q;
584
585 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
586 doesn't advance over that thing itself. Do so now. */
587 q = p;
588 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
589 ++q;
590 if (q != tmp_command + point)
c5f0f3d0 591 {
83d31a92
TT
592 /* There is something beyond the ambiguous
593 command, so there are no possible completions. For
594 example, "info t " or "info t foo" does not complete
595 to anything, because "info t" can be "info target" or
596 "info terminal". */
c5f0f3d0
FN
597 list = NULL;
598 }
83d31a92 599 else
c5f0f3d0 600 {
83d31a92
TT
601 /* We're trying to complete on the command which was ambiguous.
602 This we can deal with. */
603 if (result_list)
c5f0f3d0 604 {
67c296a2
PM
605 if (reason != handle_brkchars)
606 list = complete_on_cmdlist (*result_list->prefixlist, p,
607 word);
c5f0f3d0
FN
608 }
609 else
610 {
67c296a2
PM
611 if (reason != handle_brkchars)
612 list = complete_on_cmdlist (cmdlist, p, word);
c5f0f3d0 613 }
489f0516 614 /* Ensure that readline does the right thing with respect to
83d31a92
TT
615 inserting quotes. */
616 rl_completer_word_break_characters =
617 gdb_completer_command_word_break_characters;
c5f0f3d0 618 }
83d31a92
TT
619 }
620 else
621 {
622 /* We've recognized a full command. */
623
624 if (p == tmp_command + point)
c5f0f3d0 625 {
83d31a92 626 /* There is no non-whitespace in the line beyond the command. */
c5f0f3d0 627
83d31a92 628 if (p[-1] == ' ' || p[-1] == '\t')
c5f0f3d0 629 {
83d31a92
TT
630 /* The command is followed by whitespace; we need to complete
631 on whatever comes after command. */
632 if (c->prefixlist)
c5f0f3d0 633 {
83d31a92
TT
634 /* It is a prefix command; what comes after it is
635 a subcommand (e.g. "info "). */
67c296a2
PM
636 if (reason != handle_brkchars)
637 list = complete_on_cmdlist (*c->prefixlist, p, word);
c5f0f3d0 638
489f0516 639 /* Ensure that readline does the right thing
9c3f90bd 640 with respect to inserting quotes. */
c5f0f3d0
FN
641 rl_completer_word_break_characters =
642 gdb_completer_command_word_break_characters;
643 }
67c296a2 644 else if (reason == handle_help)
14032a66 645 list = NULL;
c5f0f3d0
FN
646 else if (c->enums)
647 {
67c296a2
PM
648 if (reason != handle_brkchars)
649 list = complete_on_enum (c->enums, p, word);
83d31a92
TT
650 rl_completer_word_break_characters =
651 gdb_completer_command_word_break_characters;
c5f0f3d0
FN
652 }
653 else
654 {
83d31a92
TT
655 /* It is a normal command; what comes after it is
656 completed by the command's completer function. */
c5f0f3d0 657 if (c->completer == filename_completer)
7830cf6f 658 {
83d31a92
TT
659 /* Many commands which want to complete on
660 file names accept several file names, as
661 in "run foo bar >>baz". So we don't want
662 to complete the entire text after the
663 command, just the last word. To this
664 end, we need to find the beginning of the
665 file name by starting at `word' and going
666 backwards. */
7830cf6f
EZ
667 for (p = word;
668 p > tmp_command
669 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
670 p--)
671 ;
672 rl_completer_word_break_characters =
673 gdb_completer_file_name_break_characters;
674 }
37cd5d19 675 else if (c->completer == location_completer)
c94fdfd0 676 {
83d31a92
TT
677 /* Commands which complete on locations want to
678 see the entire argument. */
c94fdfd0
EZ
679 for (p = word;
680 p > tmp_command
681 && p[-1] != ' ' && p[-1] != '\t';
682 p--)
683 ;
684 }
2d9c5cff 685 if (reason != handle_brkchars && c->completer != NULL)
67c296a2 686 list = (*c->completer) (c, p, word);
c5f0f3d0
FN
687 }
688 }
83d31a92
TT
689 else
690 {
691 /* The command is not followed by whitespace; we need to
692 complete on the command itself. e.g. "p" which is a
693 command itself but also can complete to "print", "ptype"
694 etc. */
695 char *q;
696
697 /* Find the command we are completing on. */
698 q = p;
699 while (q > tmp_command)
700 {
701 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
702 --q;
703 else
704 break;
705 }
706
67c296a2
PM
707 if (reason != handle_brkchars)
708 list = complete_on_cmdlist (result_list, q, word);
83d31a92 709
489f0516 710 /* Ensure that readline does the right thing
9c3f90bd 711 with respect to inserting quotes. */
83d31a92
TT
712 rl_completer_word_break_characters =
713 gdb_completer_command_word_break_characters;
714 }
715 }
67c296a2 716 else if (reason == handle_help)
14032a66 717 list = NULL;
83d31a92
TT
718 else
719 {
720 /* There is non-whitespace beyond the command. */
721
722 if (c->prefixlist && !c->allow_unknown)
723 {
724 /* It is an unrecognized subcommand of a prefix command,
725 e.g. "info adsfkdj". */
726 list = NULL;
727 }
728 else if (c->enums)
729 {
67c296a2
PM
730 if (reason != handle_brkchars)
731 list = complete_on_enum (c->enums, p, word);
83d31a92
TT
732 }
733 else
734 {
735 /* It is a normal command. */
736 if (c->completer == filename_completer)
737 {
738 /* See the commentary above about the specifics
739 of file-name completion. */
740 for (p = word;
741 p > tmp_command
742 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
743 p--)
744 ;
745 rl_completer_word_break_characters =
746 gdb_completer_file_name_break_characters;
747 }
37cd5d19 748 else if (c->completer == location_completer)
83d31a92
TT
749 {
750 for (p = word;
751 p > tmp_command
752 && p[-1] != ' ' && p[-1] != '\t';
753 p--)
754 ;
755 }
2d9c5cff 756 if (reason != handle_brkchars && c->completer != NULL)
67c296a2 757 list = (*c->completer) (c, p, word);
83d31a92
TT
758 }
759 }
760 }
761
762 return list;
763}
67c296a2
PM
764/* Generate completions all at once. Returns a NULL-terminated array
765 of strings. Both the array and each element are allocated with
766 xmalloc. It can also return NULL if there are no completions.
83d31a92 767
67c296a2
PM
768 TEXT is the caller's idea of the "word" we are looking at.
769
770 LINE_BUFFER is available to be looked at; it contains the entire text
771 of the line.
772
773 POINT is the offset in that line of the cursor. You
774 should pretend that the line ends at POINT. */
14032a66
TT
775
776char **
777complete_line (const char *text, char *line_buffer, int point)
778{
67c296a2 779 return complete_line_internal (text, line_buffer, point, handle_completions);
14032a66
TT
780}
781
782/* Complete on command names. Used by "help". */
783char **
d8906c6f 784command_completer (struct cmd_list_element *ignore, char *text, char *word)
14032a66 785{
67c296a2
PM
786 return complete_line_internal (word, text, strlen (text), handle_help);
787}
788
789/* Get the list of chars that are considered as word breaks
790 for the current command. */
791
792char *
793gdb_completion_word_break_characters (void)
794{
c5504eaf
MS
795 char **list;
796
67c296a2
PM
797 list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
798 handle_brkchars);
799 gdb_assert (list == NULL);
800 return rl_completer_word_break_characters;
14032a66
TT
801}
802
83d31a92
TT
803/* Generate completions one by one for the completer. Each time we are
804 called return another potential completion to the caller.
805 line_completion just completes on commands or passes the buck to the
806 command's completer function, the stuff specific to symbol completion
807 is in make_symbol_completion_list.
808
809 TEXT is the caller's idea of the "word" we are looking at.
810
811 MATCHES is the number of matches that have currently been collected from
812 calling this completion function. When zero, then we need to initialize,
813 otherwise the initialization has already taken place and we can just
814 return the next potential completion string.
815
816 LINE_BUFFER is available to be looked at; it contains the entire text
817 of the line. POINT is the offset in that line of the cursor. You
818 should pretend that the line ends at POINT.
819
820 Returns NULL if there are no more completions, else a pointer to a string
821 which is a possible completion, it is the caller's responsibility to
822 free the string. */
823
38017ce8 824static char *
9c3f90bd
MS
825line_completion_function (const char *text, int matches,
826 char *line_buffer, int point)
83d31a92 827{
9c3f90bd
MS
828 static char **list = (char **) NULL; /* Cache of completions. */
829 static int index; /* Next cached completion. */
83d31a92
TT
830 char *output = NULL;
831
832 if (matches == 0)
833 {
834 /* The caller is beginning to accumulate a new set of completions, so
835 we need to find all of them now, and cache them for returning one at
9c3f90bd 836 a time on future calls. */
83d31a92
TT
837
838 if (list)
839 {
840 /* Free the storage used by LIST, but not by the strings inside.
6f4de6c9
JK
841 This is because rl_complete_internal () frees the strings.
842 As complete_line may abort by calling `error' clear LIST now. */
83d31a92 843 xfree (list);
6f4de6c9 844 list = NULL;
c5f0f3d0 845 }
83d31a92
TT
846 index = 0;
847 list = complete_line (text, line_buffer, point);
c5f0f3d0
FN
848 }
849
850 /* If we found a list of potential completions during initialization then
851 dole them out one at a time. The vector of completions is NULL
852 terminated, so after returning the last one, return NULL (and continue
853 to do so) each time we are called after that, until a new list is
9c3f90bd 854 available. */
c5f0f3d0
FN
855
856 if (list)
857 {
858 output = list[index];
859 if (output)
860 {
861 index++;
862 }
863 }
864
865#if 0
866 /* Can't do this because readline hasn't yet checked the word breaks
867 for figuring out whether to insert a quote. */
868 if (output == NULL)
869 /* Make sure the word break characters are set back to normal for the
870 next time that readline tries to complete something. */
871 rl_completer_word_break_characters =
51065942 872 current_language->la_word_break_characters();
c5f0f3d0
FN
873#endif
874
875 return (output);
876}
4e87b832
KD
877
878/* Skip over the possibly quoted word STR (as defined by the quote
879 characters QUOTECHARS and the the word break characters
880 BREAKCHARS). Returns pointer to the location after the "word". If
881 either QUOTECHARS or BREAKCHARS is NULL, use the same values used
882 by the completer. */
c5f0f3d0
FN
883
884char *
4e87b832 885skip_quoted_chars (char *str, char *quotechars, char *breakchars)
c5f0f3d0
FN
886{
887 char quote_char = '\0';
888 char *scan;
889
4e87b832
KD
890 if (quotechars == NULL)
891 quotechars = gdb_completer_quote_characters;
892
893 if (breakchars == NULL)
51065942 894 breakchars = current_language->la_word_break_characters();
4e87b832 895
c5f0f3d0
FN
896 for (scan = str; *scan != '\0'; scan++)
897 {
898 if (quote_char != '\0')
899 {
9c3f90bd 900 /* Ignore everything until the matching close quote char. */
c5f0f3d0
FN
901 if (*scan == quote_char)
902 {
9c3f90bd 903 /* Found matching close quote. */
c5f0f3d0
FN
904 scan++;
905 break;
906 }
907 }
4e87b832 908 else if (strchr (quotechars, *scan))
c5f0f3d0
FN
909 {
910 /* Found start of a quoted string. */
911 quote_char = *scan;
912 }
4e87b832 913 else if (strchr (breakchars, *scan))
c5f0f3d0
FN
914 {
915 break;
916 }
917 }
4e87b832 918
c5f0f3d0
FN
919 return (scan);
920}
921
4e87b832
KD
922/* Skip over the possibly quoted word STR (as defined by the quote
923 characters and word break characters used by the completer).
9c3f90bd 924 Returns pointer to the location after the "word". */
4e87b832
KD
925
926char *
927skip_quoted (char *str)
928{
929 return skip_quoted_chars (str, NULL, NULL);
930}
This page took 0.686299 seconds and 4 git commands to generate.