1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000, 2001, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "expression.h"
24 #include "filenames.h" /* For DOSish file names. */
26 #include "gdb_assert.h"
28 #include "cli/cli-decode.h"
30 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
31 calling a hook instead so we eliminate the CLI dependency. */
34 /* Needed for rl_completer_word_break_characters() and for
35 rl_filename_completion_function. */
36 #include "readline/readline.h"
38 /* readline defines this. */
41 #include "completer.h"
43 /* Prototypes for local functions. */
45 char *line_completion_function (const char *text, int matches,
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
56 current_language->la_word_break_characters() and
57 gdb_completer_command_word_break_characters. I'm not sure when
58 we need this behavior (perhaps for funky characters in C++ symbols?). */
60 /* Variables which are necessary for fancy command line editing. */
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
67 static char *gdb_completer_command_word_break_characters =
68 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
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. */
74 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
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*|\"';?><@";
79 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
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
85 static char *gdb_completer_quote_characters = "'";
87 /* Accessor for some completer data that may interest other files. */
90 get_gdb_completer_quote_characters (void)
92 return gdb_completer_quote_characters;
95 /* Line completion interface function for readline. */
98 readline_line_completion_function (const char *text, int matches)
100 return line_completion_function (text, matches, rl_line_buffer, rl_point);
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. */
106 noop_completer (struct cmd_list_element *ignore, char *text, char *prefix)
111 /* Complete on filenames. */
113 filename_completer (struct cmd_list_element *ignore, char *text, char *word)
118 int return_val_alloced;
121 /* Small for testing. */
122 return_val_alloced = 1;
123 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
129 p = rl_filename_completion_function (text, subsequent_name);
130 if (return_val_used >= return_val_alloced)
132 return_val_alloced *= 2;
134 (char **) xrealloc (return_val,
135 return_val_alloced * sizeof (char *));
139 return_val[return_val_used++] = p;
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
147 /* Like emacs, don't complete on old versions. Especially useful
148 in the "source" command. */
149 if (p[strlen (p) - 1] == '~')
156 /* Return exactly p. */
157 return_val[return_val_used++] = p;
158 else if (word > text)
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;
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';
173 return_val[return_val_used++] = q;
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
181 /* Ensure that readline does the right thing
182 with respect to inserting quotes. */
183 rl_completer_word_break_characters = "";
188 /* Complete on locations, which might be of two possible forms:
194 This is intended to be used in commands that set breakpoints etc. */
196 location_completer (struct cmd_list_element *ignore, char *text, char *word)
198 int n_syms = 0, n_files = 0;
199 char ** fn_list = NULL;
203 int quoted = *text == '\'' || *text == '"';
204 int quote_char = '\0';
206 char *file_to_match = NULL;
207 char *symbol_start = text;
208 char *orig_text = text;
211 /* Do we have an unquoted colon, as in "break foo.c::bar"? */
212 for (p = text; *p != '\0'; ++p)
214 if (*p == '\\' && p[1] == '\'')
216 else if (*p == '\'' || *p == '"')
220 while (*p != '\0' && *p != quote_found)
222 if (*p == '\\' && p[1] == quote_found)
227 if (*p == quote_found)
230 break; /* Hit the end of text. */
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)
239 else if (*p == ':' && !colon)
242 symbol_start = p + 1;
244 else if (strchr (current_language->la_word_break_characters(), *p))
245 symbol_start = p + 1;
250 text_len = strlen (text);
252 /* Where is the file name? */
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);
263 if (*s == ':' || *s == quote_char)
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. */
271 list = make_file_symbol_completion_list (symbol_start, word,
273 xfree (file_to_match);
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. */
281 gdb_completer_file_name_break_characters) == text_len)
282 fn_list = make_source_files_completion_list (text, text);
285 /* How many completions do we have in both lists? */
287 for ( ; fn_list[n_files]; n_files++)
290 for ( ; list[n_syms]; n_syms++)
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)
297 list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
298 memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
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
318 for (n_files = 0; fn_list[n_files]; n_files++)
320 memmove (fn_list[n_files], fn_list[n_files] + (word - text),
321 strlen (fn_list[n_files]) + 1 - (word - text));
323 /* Return just the file-name list as the result. */
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);
339 /* Helper for expression_completer which recursively counts the number
340 of named fields and methods in a structure or union type. */
342 count_struct_fields (struct type *type)
346 CHECK_TYPEDEF (type);
347 for (i = 0; i < TYPE_NFIELDS (type); ++i)
349 if (i < TYPE_N_BASECLASSES (type))
350 result += count_struct_fields (TYPE_BASECLASS (type, i));
351 else if (TYPE_FIELD_NAME (type, i))
355 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
357 if (TYPE_FN_FIELDLIST_NAME (type, i))
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. */
368 add_struct_fields (struct type *type, int *nextp, char **output,
369 char *fieldname, int namelen)
372 int computed_type_name = 0;
373 char *type_name = NULL;
375 CHECK_TYPEDEF (type);
376 for (i = 0; i < TYPE_NFIELDS (type); ++i)
378 if (i < TYPE_N_BASECLASSES (type))
379 add_struct_fields (TYPE_BASECLASS (type, i), nextp, output,
381 else if (TYPE_FIELD_NAME (type, i)
382 && ! strncmp (TYPE_FIELD_NAME (type, i), fieldname, namelen))
384 output[*nextp] = xstrdup (TYPE_FIELD_NAME (type, i));
389 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
391 char *name = TYPE_FN_FIELDLIST_NAME (type, i);
392 if (name && ! strncmp (name, fieldname, namelen))
394 if (!computed_type_name)
396 type_name = type_name_no_tag (type);
397 computed_type_name = 1;
399 /* Omit constructors from the completion list. */
400 if (!type_name || strcmp (type_name, name))
402 output[*nextp] = xstrdup (name);
409 /* Complete on expressions. Often this means completing on symbol
410 names, but some language parsers also have support for completing
413 expression_completer (struct cmd_list_element *ignore, char *text, char *word)
418 /* Perform a tentative parse of the expression, to see whether a
419 field completion is required. */
421 type = parse_field_expression (text, &fieldname);
422 if (fieldname && type)
426 CHECK_TYPEDEF (type);
427 if (TYPE_CODE (type) != TYPE_CODE_PTR
428 && TYPE_CODE (type) != TYPE_CODE_REF)
430 type = TYPE_TARGET_TYPE (type);
433 if (TYPE_CODE (type) == TYPE_CODE_UNION
434 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
436 int alloc = count_struct_fields (type);
437 int flen = strlen (fieldname);
439 char **result = (char **) xmalloc ((alloc + 1) * sizeof (char *));
441 add_struct_fields (type, &out, result, fieldname, flen);
449 /* Commands which complete on locations want to see the entire
452 p > text && p[-1] != ' ' && p[-1] != '\t';
456 /* Not ideal but it is what we used to do before... */
457 return location_completer (ignore, p, word);
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.
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
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)
488 complete_line_internal_reason;
491 /* Internal function used to handle completions.
494 TEXT is the caller's idea of the "word" we are looking at.
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
498 should pretend that the line ends at POINT.
500 REASON is of type complete_line_internal_reason.
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.
509 If REASON is handle_completions:
510 Main phase, called by complete_line function, is used to get the list
511 of posible completions.
513 If REASON is handle_help:
514 Special case when completing a 'help' command. In this case,
515 once sub-command completions are exhausted, we simply return NULL.
519 complete_line_internal (const char *text, char *line_buffer, int point,
520 complete_line_internal_reason reason)
523 char *tmp_command, *p;
524 /* Pointer within tmp_command which corresponds to text. */
526 struct cmd_list_element *c, *result_list;
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. */
534 rl_completer_word_break_characters =
535 current_language->la_word_break_characters();
537 /* Decide whether to complete on a list of gdb commands or on symbols. */
538 tmp_command = (char *) alloca (point + 1);
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);
550 /* An empty line we want to consider ambiguous; that is, it
551 could be any command. */
552 c = (struct cmd_list_element *) -1;
557 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
560 /* Move p up to the next interesting thing. */
561 while (*p == ' ' || *p == '\t')
568 /* It is an unrecognized command. So there are no
569 possible completions. */
572 else if (c == (struct cmd_list_element *) -1)
576 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
577 doesn't advance over that thing itself. Do so now. */
579 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
581 if (q != tmp_command + point)
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
592 /* We're trying to complete on the command which was ambiguous.
593 This we can deal with. */
596 if (reason != handle_brkchars)
597 list = complete_on_cmdlist (*result_list->prefixlist, p,
602 if (reason != handle_brkchars)
603 list = complete_on_cmdlist (cmdlist, p, word);
605 /* Ensure that readline does the right thing with respect to
607 rl_completer_word_break_characters =
608 gdb_completer_command_word_break_characters;
613 /* We've recognized a full command. */
615 if (p == tmp_command + point)
617 /* There is no non-whitespace in the line beyond the command. */
619 if (p[-1] == ' ' || p[-1] == '\t')
621 /* The command is followed by whitespace; we need to complete
622 on whatever comes after command. */
625 /* It is a prefix command; what comes after it is
626 a subcommand (e.g. "info "). */
627 if (reason != handle_brkchars)
628 list = complete_on_cmdlist (*c->prefixlist, p, word);
630 /* Ensure that readline does the right thing
631 with respect to inserting quotes. */
632 rl_completer_word_break_characters =
633 gdb_completer_command_word_break_characters;
635 else if (reason == handle_help)
639 if (reason != handle_brkchars)
640 list = complete_on_enum (c->enums, p, word);
641 rl_completer_word_break_characters =
642 gdb_completer_command_word_break_characters;
646 /* It is a normal command; what comes after it is
647 completed by the command's completer function. */
648 if (c->completer == filename_completer)
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
660 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
663 rl_completer_word_break_characters =
664 gdb_completer_file_name_break_characters;
666 else if (c->completer == location_completer)
668 /* Commands which complete on locations want to
669 see the entire argument. */
672 && p[-1] != ' ' && p[-1] != '\t';
676 if (reason != handle_brkchars && c->completer != NULL)
677 list = (*c->completer) (c, p, word);
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"
688 /* Find the command we are completing on. */
690 while (q > tmp_command)
692 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
698 if (reason != handle_brkchars)
699 list = complete_on_cmdlist (result_list, q, word);
701 /* Ensure that readline does the right thing
702 with respect to inserting quotes. */
703 rl_completer_word_break_characters =
704 gdb_completer_command_word_break_characters;
707 else if (reason == handle_help)
711 /* There is non-whitespace beyond the command. */
713 if (c->prefixlist && !c->allow_unknown)
715 /* It is an unrecognized subcommand of a prefix command,
716 e.g. "info adsfkdj". */
721 if (reason != handle_brkchars)
722 list = complete_on_enum (c->enums, p, word);
726 /* It is a normal command. */
727 if (c->completer == filename_completer)
729 /* See the commentary above about the specifics
730 of file-name completion. */
733 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
736 rl_completer_word_break_characters =
737 gdb_completer_file_name_break_characters;
739 else if (c->completer == location_completer)
743 && p[-1] != ' ' && p[-1] != '\t';
747 if (reason != handle_brkchars && c->completer != NULL)
748 list = (*c->completer) (c, p, word);
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.
759 TEXT is the caller's idea of the "word" we are looking at.
761 LINE_BUFFER is available to be looked at; it contains the entire text
764 POINT is the offset in that line of the cursor. You
765 should pretend that the line ends at POINT. */
768 complete_line (const char *text, char *line_buffer, int point)
770 return complete_line_internal (text, line_buffer, point, handle_completions);
773 /* Complete on command names. Used by "help". */
775 command_completer (struct cmd_list_element *ignore, char *text, char *word)
777 return complete_line_internal (word, text, strlen (text), handle_help);
780 /* Get the list of chars that are considered as word breaks
781 for the current command. */
784 gdb_completion_word_break_characters (void)
787 list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
789 gdb_assert (list == NULL);
790 return rl_completer_word_break_characters;
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.
799 TEXT is the caller's idea of the "word" we are looking at.
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.
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.
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
815 line_completion_function (const char *text, int matches,
816 char *line_buffer, int point)
818 static char **list = (char **) NULL; /* Cache of completions. */
819 static int index; /* Next cached completion. */
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
826 a time on future calls. */
830 /* Free the storage used by LIST, but not by the strings inside.
831 This is because rl_complete_internal () frees the strings.
832 As complete_line may abort by calling `error' clear LIST now. */
837 list = complete_line (text, line_buffer, point);
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
848 output = list[index];
856 /* Can't do this because readline hasn't yet checked the word breaks
857 for figuring out whether to insert a quote. */
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 =
862 current_language->la_word_break_characters();
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
875 skip_quoted_chars (char *str, char *quotechars, char *breakchars)
877 char quote_char = '\0';
880 if (quotechars == NULL)
881 quotechars = gdb_completer_quote_characters;
883 if (breakchars == NULL)
884 breakchars = current_language->la_word_break_characters();
886 for (scan = str; *scan != '\0'; scan++)
888 if (quote_char != '\0')
890 /* Ignore everything until the matching close quote char. */
891 if (*scan == quote_char)
893 /* Found matching close quote. */
898 else if (strchr (quotechars, *scan))
900 /* Found start of a quoted string. */
903 else if (strchr (breakchars, *scan))
912 /* Skip over the possibly quoted word STR (as defined by the quote
913 characters and word break characters used by the completer).
914 Returns pointer to the location after the "word". */
917 skip_quoted (char *str)
919 return skip_quoted_chars (str, NULL, NULL);