1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000, 2001, 2007, 2008, 2009 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "expression.h"
23 #include "filenames.h" /* For DOSish file names. */
26 #include "cli/cli-decode.h"
28 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
29 calling a hook instead so we eliminate the CLI dependency. */
32 /* Needed for rl_completer_word_break_characters() and for
33 rl_filename_completion_function. */
34 #include "readline/readline.h"
36 /* readline defines this. */
39 #include "completer.h"
41 /* Prototypes for local functions. */
43 char *line_completion_function (const char *text
, int matches
,
47 /* readline uses the word breaks for two things:
48 (1) In figuring out where to point the TEXT parameter to the
49 rl_completion_entry_function. Since we don't use TEXT for much,
50 it doesn't matter a lot what the word breaks are for this purpose, but
51 it does affect how much stuff M-? lists.
52 (2) If one of the matches contains a word break character, readline
53 will quote it. That's why we switch between
54 current_language->la_word_break_characters() and
55 gdb_completer_command_word_break_characters. I'm not sure when
56 we need this behavior (perhaps for funky characters in C++ symbols?). */
58 /* Variables which are necessary for fancy command line editing. */
60 /* When completing on command names, we remove '-' from the list of
61 word break characters, since we use it in command names. If the
62 readline library sees one in any of the current completion strings,
63 it thinks that the string needs to be quoted and automatically supplies
65 static char *gdb_completer_command_word_break_characters
=
66 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
68 /* When completing on file names, we remove from the list of word
69 break characters any characters that are commonly used in file
70 names, such as '-', '+', '~', etc. Otherwise, readline displays
71 incorrect completion candidates. */
72 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
73 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
74 programs support @foo style response files. */
75 static char *gdb_completer_file_name_break_characters
= " \t\n*|\"';?><@";
77 static char *gdb_completer_file_name_break_characters
= " \t\n*|\"';:?><";
80 /* These are used when completing on locations, which can mix file
81 names and symbol names separated by a colon. */
82 static char *gdb_completer_loc_break_characters
= " \t\n*|\"';:?><,";
84 /* Characters that can be used to quote completion strings. Note that we
85 can't include '"' because the gdb C parser treats such quoted sequences
87 static char *gdb_completer_quote_characters
= "'";
89 /* Accessor for some completer data that may interest other files. */
92 get_gdb_completer_quote_characters (void)
94 return gdb_completer_quote_characters
;
97 /* Line completion interface function for readline. */
100 readline_line_completion_function (const char *text
, int matches
)
102 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
105 /* This can be used for functions which don't want to complete on symbols
106 but don't want to complete on anything else either. */
108 noop_completer (char *text
, char *prefix
)
113 /* Complete on filenames. */
115 filename_completer (char *text
, char *word
)
120 int return_val_alloced
;
123 /* Small for testing. */
124 return_val_alloced
= 1;
125 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
131 p
= rl_filename_completion_function (text
, subsequent_name
);
132 if (return_val_used
>= return_val_alloced
)
134 return_val_alloced
*= 2;
136 (char **) xrealloc (return_val
,
137 return_val_alloced
* sizeof (char *));
141 return_val
[return_val_used
++] = p
;
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
149 /* Like emacs, don't complete on old versions. Especially useful
150 in the "source" command. */
151 if (p
[strlen (p
) - 1] == '~')
158 /* Return exactly p. */
159 return_val
[return_val_used
++] = p
;
160 else if (word
> text
)
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
;
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';
175 return_val
[return_val_used
++] = q
;
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
183 /* Ensure that readline does the right thing
184 with respect to inserting quotes. */
185 rl_completer_word_break_characters
= "";
190 /* Complete on locations, which might be of two possible forms:
196 This is intended to be used in commands that set breakpoints etc. */
198 location_completer (char *text
, char *word
)
200 int n_syms
= 0, n_files
= 0;
201 char ** fn_list
= NULL
;
205 int quoted
= *text
== '\'' || *text
== '"';
206 int quote_char
= '\0';
208 char *file_to_match
= NULL
;
209 char *symbol_start
= text
;
210 char *orig_text
= text
;
213 /* Do we have an unquoted colon, as in "break foo.c::bar"? */
214 for (p
= text
; *p
!= '\0'; ++p
)
216 if (*p
== '\\' && p
[1] == '\'')
218 else if (*p
== '\'' || *p
== '"')
222 while (*p
!= '\0' && *p
!= quote_found
)
224 if (*p
== '\\' && p
[1] == quote_found
)
229 if (*p
== quote_found
)
232 break; /* Hit the end of text. */
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
)
241 else if (*p
== ':' && !colon
)
244 symbol_start
= p
+ 1;
246 else if (strchr (current_language
->la_word_break_characters(), *p
))
247 symbol_start
= p
+ 1;
252 text_len
= strlen (text
);
254 /* Where is the file name? */
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
);
265 if (*s
== ':' || *s
== quote_char
)
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. */
273 list
= make_file_symbol_completion_list (symbol_start
, word
,
275 xfree (file_to_match
);
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. */
283 gdb_completer_file_name_break_characters
) == text_len
)
284 fn_list
= make_source_files_completion_list (text
, text
);
287 /* How many completions do we have in both lists? */
289 for ( ; fn_list
[n_files
]; n_files
++)
292 for ( ; list
[n_syms
]; n_syms
++)
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
)
299 list
= xrealloc (list
, (n_syms
+ n_files
+ 1) * sizeof (char *));
300 memcpy (list
+ n_syms
, fn_list
, (n_files
+ 1) * sizeof (char *));
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
320 for (n_files
= 0; fn_list
[n_files
]; n_files
++)
322 memmove (fn_list
[n_files
], fn_list
[n_files
] + (word
- text
),
323 strlen (fn_list
[n_files
]) + 1 - (word
- text
));
325 /* Return just the file-name list as the result. */
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
);
341 /* Helper for expression_completer which recursively counts the number
342 of named fields and methods in a structure or union type. */
344 count_struct_fields (struct type
*type
)
348 CHECK_TYPEDEF (type
);
349 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
351 if (i
< TYPE_N_BASECLASSES (type
))
352 result
+= count_struct_fields (TYPE_BASECLASS (type
, i
));
353 else if (TYPE_FIELD_NAME (type
, i
))
357 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
359 if (TYPE_FN_FIELDLIST_NAME (type
, i
))
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. */
370 add_struct_fields (struct type
*type
, int *nextp
, char **output
,
371 char *fieldname
, int namelen
)
374 char *type_name
= NULL
;
376 CHECK_TYPEDEF (type
);
377 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
379 if (i
< TYPE_N_BASECLASSES (type
))
380 add_struct_fields (TYPE_BASECLASS (type
, i
), nextp
, output
,
382 else if (TYPE_FIELD_NAME (type
, i
)
383 && ! strncmp (TYPE_FIELD_NAME (type
, i
), fieldname
, namelen
))
385 output
[*nextp
] = xstrdup (TYPE_FIELD_NAME (type
, i
));
390 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
392 char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
393 if (name
&& ! strncmp (name
, fieldname
, namelen
))
396 type_name
= type_name_no_tag (type
);
397 /* Omit constructors from the completion list. */
398 if (strcmp (type_name
, name
))
400 output
[*nextp
] = xstrdup (name
);
407 /* Complete on expressions. Often this means completing on symbol
408 names, but some language parsers also have support for completing
411 expression_completer (char *text
, char *word
)
416 /* Perform a tentative parse of the expression, to see whether a
417 field completion is required. */
419 type
= parse_field_expression (text
, &fieldname
);
420 if (fieldname
&& type
)
424 CHECK_TYPEDEF (type
);
425 if (TYPE_CODE (type
) != TYPE_CODE_PTR
426 && TYPE_CODE (type
) != TYPE_CODE_REF
)
428 type
= TYPE_TARGET_TYPE (type
);
431 if (TYPE_CODE (type
) == TYPE_CODE_UNION
432 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
434 int alloc
= count_struct_fields (type
);
435 int flen
= strlen (fieldname
);
437 char **result
= (char **) xmalloc ((alloc
+ 1) * sizeof (char *));
439 add_struct_fields (type
, &out
, result
, fieldname
, flen
);
447 /* Commands which complete on locations want to see the entire
450 p
> text
&& p
[-1] != ' ' && p
[-1] != '\t';
454 /* Not ideal but it is what we used to do before... */
455 return location_completer (p
, word
);
458 /* Here are some useful test cases for completion. FIXME: These should
459 be put in the test suite. They should be tested with both M-? and TAB.
461 "show output-" "radix"
462 "show output" "-radix"
463 "p" ambiguous (commands starting with p--path, print, printf, etc.)
464 "p " ambiguous (all symbols)
465 "info t foo" no completions
466 "info t " no completions
467 "info t" ambiguous ("info target", "info terminal", etc.)
468 "info ajksdlfk" no completions
469 "info ajksdlfk " no completions
471 "info " ambiguous (all info commands)
472 "p \"a" no completions (string constant)
473 "p 'a" ambiguous (all symbols starting with a)
474 "p b-a" ambiguous (all symbols starting with a)
475 "p b-" ambiguous (all symbols)
476 "file Make" "file" (word break hard to screw up here)
477 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
480 /* Generate completions all at once. Returns a NULL-terminated array
481 of strings. Both the array and each element are allocated with
482 xmalloc. It can also return NULL if there are no completions.
484 TEXT is the caller's idea of the "word" we are looking at.
486 LINE_BUFFER is available to be looked at; it contains the entire text
487 of the line. POINT is the offset in that line of the cursor. You
488 should pretend that the line ends at POINT.
490 FOR_HELP is true when completing a 'help' command. In this case,
491 once sub-command completions are exhausted, we simply return NULL.
492 When FOR_HELP is false, we will call a sub-command's completion
496 complete_line_internal (const char *text
, char *line_buffer
, int point
,
500 char *tmp_command
, *p
;
501 /* Pointer within tmp_command which corresponds to text. */
503 struct cmd_list_element
*c
, *result_list
;
505 /* Choose the default set of word break characters to break completions.
506 If we later find out that we are doing completions on command strings
507 (as opposed to strings supplied by the individual command completer
508 functions, which can be any string) then we will switch to the
509 special word break set for command strings, which leaves out the
510 '-' character used in some commands. */
512 rl_completer_word_break_characters
=
513 current_language
->la_word_break_characters();
515 /* Decide whether to complete on a list of gdb commands or on symbols. */
516 tmp_command
= (char *) alloca (point
+ 1);
519 strncpy (tmp_command
, line_buffer
, point
);
520 tmp_command
[point
] = '\0';
521 /* Since text always contains some number of characters leading up
522 to point, we can find the equivalent position in tmp_command
523 by subtracting that many characters from the end of tmp_command. */
524 word
= tmp_command
+ point
- strlen (text
);
528 /* An empty line we want to consider ambiguous; that is, it
529 could be any command. */
530 c
= (struct cmd_list_element
*) -1;
535 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
538 /* Move p up to the next interesting thing. */
539 while (*p
== ' ' || *p
== '\t')
546 /* It is an unrecognized command. So there are no
547 possible completions. */
550 else if (c
== (struct cmd_list_element
*) -1)
554 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
555 doesn't advance over that thing itself. Do so now. */
557 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
559 if (q
!= tmp_command
+ point
)
561 /* There is something beyond the ambiguous
562 command, so there are no possible completions. For
563 example, "info t " or "info t foo" does not complete
564 to anything, because "info t" can be "info target" or
570 /* We're trying to complete on the command which was ambiguous.
571 This we can deal with. */
574 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
579 list
= complete_on_cmdlist (cmdlist
, p
, word
);
581 /* Ensure that readline does the right thing with respect to
583 rl_completer_word_break_characters
=
584 gdb_completer_command_word_break_characters
;
589 /* We've recognized a full command. */
591 if (p
== tmp_command
+ point
)
593 /* There is no non-whitespace in the line beyond the command. */
595 if (p
[-1] == ' ' || p
[-1] == '\t')
597 /* The command is followed by whitespace; we need to complete
598 on whatever comes after command. */
601 /* It is a prefix command; what comes after it is
602 a subcommand (e.g. "info "). */
603 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
605 /* Ensure that readline does the right thing
606 with respect to inserting quotes. */
607 rl_completer_word_break_characters
=
608 gdb_completer_command_word_break_characters
;
614 list
= complete_on_enum (c
->enums
, p
, word
);
615 rl_completer_word_break_characters
=
616 gdb_completer_command_word_break_characters
;
620 /* It is a normal command; what comes after it is
621 completed by the command's completer function. */
622 if (c
->completer
== filename_completer
)
624 /* Many commands which want to complete on
625 file names accept several file names, as
626 in "run foo bar >>baz". So we don't want
627 to complete the entire text after the
628 command, just the last word. To this
629 end, we need to find the beginning of the
630 file name by starting at `word' and going
634 && strchr (gdb_completer_file_name_break_characters
, p
[-1]) == NULL
;
637 rl_completer_word_break_characters
=
638 gdb_completer_file_name_break_characters
;
640 else if (c
->completer
== location_completer
)
642 /* Commands which complete on locations want to
643 see the entire argument. */
646 && p
[-1] != ' ' && p
[-1] != '\t';
650 list
= (*c
->completer
) (p
, word
);
655 /* The command is not followed by whitespace; we need to
656 complete on the command itself. e.g. "p" which is a
657 command itself but also can complete to "print", "ptype"
661 /* Find the command we are completing on. */
663 while (q
> tmp_command
)
665 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
671 list
= complete_on_cmdlist (result_list
, q
, word
);
673 /* Ensure that readline does the right thing
674 with respect to inserting quotes. */
675 rl_completer_word_break_characters
=
676 gdb_completer_command_word_break_characters
;
683 /* There is non-whitespace beyond the command. */
685 if (c
->prefixlist
&& !c
->allow_unknown
)
687 /* It is an unrecognized subcommand of a prefix command,
688 e.g. "info adsfkdj". */
693 list
= complete_on_enum (c
->enums
, p
, word
);
697 /* It is a normal command. */
698 if (c
->completer
== filename_completer
)
700 /* See the commentary above about the specifics
701 of file-name completion. */
704 && strchr (gdb_completer_file_name_break_characters
, p
[-1]) == NULL
;
707 rl_completer_word_break_characters
=
708 gdb_completer_file_name_break_characters
;
710 else if (c
->completer
== location_completer
)
714 && p
[-1] != ' ' && p
[-1] != '\t';
718 list
= (*c
->completer
) (p
, word
);
726 /* Like complete_line_internal, but always passes 0 for FOR_HELP. */
729 complete_line (const char *text
, char *line_buffer
, int point
)
731 return complete_line_internal (text
, line_buffer
, point
, 0);
734 /* Complete on command names. Used by "help". */
736 command_completer (char *text
, char *word
)
738 return complete_line_internal (word
, text
, strlen (text
), 1);
741 /* Generate completions one by one for the completer. Each time we are
742 called return another potential completion to the caller.
743 line_completion just completes on commands or passes the buck to the
744 command's completer function, the stuff specific to symbol completion
745 is in make_symbol_completion_list.
747 TEXT is the caller's idea of the "word" we are looking at.
749 MATCHES is the number of matches that have currently been collected from
750 calling this completion function. When zero, then we need to initialize,
751 otherwise the initialization has already taken place and we can just
752 return the next potential completion string.
754 LINE_BUFFER is available to be looked at; it contains the entire text
755 of the line. POINT is the offset in that line of the cursor. You
756 should pretend that the line ends at POINT.
758 Returns NULL if there are no more completions, else a pointer to a string
759 which is a possible completion, it is the caller's responsibility to
763 line_completion_function (const char *text
, int matches
,
764 char *line_buffer
, int point
)
766 static char **list
= (char **) NULL
; /* Cache of completions. */
767 static int index
; /* Next cached completion. */
772 /* The caller is beginning to accumulate a new set of completions, so
773 we need to find all of them now, and cache them for returning one at
774 a time on future calls. */
778 /* Free the storage used by LIST, but not by the strings inside.
779 This is because rl_complete_internal () frees the strings.
780 As complete_line may abort by calling `error' clear LIST now. */
785 list
= complete_line (text
, line_buffer
, point
);
788 /* If we found a list of potential completions during initialization then
789 dole them out one at a time. The vector of completions is NULL
790 terminated, so after returning the last one, return NULL (and continue
791 to do so) each time we are called after that, until a new list is
796 output
= list
[index
];
804 /* Can't do this because readline hasn't yet checked the word breaks
805 for figuring out whether to insert a quote. */
807 /* Make sure the word break characters are set back to normal for the
808 next time that readline tries to complete something. */
809 rl_completer_word_break_characters
=
810 current_language
->la_word_break_characters();
816 /* Skip over the possibly quoted word STR (as defined by the quote
817 characters QUOTECHARS and the the word break characters
818 BREAKCHARS). Returns pointer to the location after the "word". If
819 either QUOTECHARS or BREAKCHARS is NULL, use the same values used
823 skip_quoted_chars (char *str
, char *quotechars
, char *breakchars
)
825 char quote_char
= '\0';
828 if (quotechars
== NULL
)
829 quotechars
= gdb_completer_quote_characters
;
831 if (breakchars
== NULL
)
832 breakchars
= current_language
->la_word_break_characters();
834 for (scan
= str
; *scan
!= '\0'; scan
++)
836 if (quote_char
!= '\0')
838 /* Ignore everything until the matching close quote char. */
839 if (*scan
== quote_char
)
841 /* Found matching close quote. */
846 else if (strchr (quotechars
, *scan
))
848 /* Found start of a quoted string. */
851 else if (strchr (breakchars
, *scan
))
860 /* Skip over the possibly quoted word STR (as defined by the quote
861 characters and word break characters used by the completer).
862 Returns pointer to the location after the "word". */
865 skip_quoted (char *str
)
867 return skip_quoted_chars (str
, NULL
, NULL
);