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"
27 #include "exceptions.h"
29 #include "cli/cli-decode.h"
31 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
32 calling a hook instead so we eliminate the CLI dependency. */
35 /* Needed for rl_completer_word_break_characters() and for
36 rl_filename_completion_function. */
37 #include "readline/readline.h"
39 /* readline defines this. */
42 #include "completer.h"
44 /* Prototypes for local functions. */
46 char *line_completion_function (const char *text, int matches,
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,
54 but 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
57 current_language->la_word_break_characters() and
58 gdb_completer_command_word_break_characters. I'm not sure when
59 we need this behavior (perhaps for funky characters in C++
62 /* Variables which are necessary for fancy command line editing. */
64 /* When completing on command names, we remove '-' from the list of
65 word break characters, since we use it in command names. If the
66 readline library sees one in any of the current completion strings,
67 it thinks that the string needs to be quoted and automatically
68 supplies a leading quote. */
69 static char *gdb_completer_command_word_break_characters =
70 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
72 /* When completing on file names, we remove from the list of word
73 break characters any characters that are commonly used in file
74 names, such as '-', '+', '~', etc. Otherwise, readline displays
75 incorrect completion candidates. */
76 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
77 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
78 programs support @foo style response files. */
79 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
81 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
84 /* Characters that can be used to quote completion strings. Note that
85 we can't include '"' because the gdb C parser treats such quoted
86 sequences as strings. */
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,
103 rl_line_buffer, rl_point);
106 /* This can be used for functions which don't want to complete on
107 symbols but don't want to complete on anything else either. */
109 noop_completer (struct cmd_list_element *ignore,
110 char *text, char *prefix)
115 /* Complete on filenames. */
117 filename_completer (struct cmd_list_element *ignore,
118 char *text, char *word)
123 int return_val_alloced;
126 /* Small for testing. */
127 return_val_alloced = 1;
128 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
135 p = rl_filename_completion_function (text, subsequent_name);
136 if (return_val_used >= return_val_alloced)
138 return_val_alloced *= 2;
140 (char **) xrealloc (return_val,
141 return_val_alloced * sizeof (char *));
145 return_val[return_val_used++] = p;
148 /* We need to set subsequent_name to a non-zero value before the
149 continue line below, because otherwise, if the first file
150 seen by GDB is a backup file whose name ends in a `~', we
151 will loop indefinitely. */
153 /* Like emacs, don't complete on old versions. Especially
154 useful in the "source" command. */
155 if (p[strlen (p) - 1] == '~')
162 /* Return exactly p. */
163 return_val[return_val_used++] = p;
164 else if (word > text)
166 /* Return some portion of p. */
167 q = xmalloc (strlen (p) + 5);
168 strcpy (q, p + (word - text));
169 return_val[return_val_used++] = q;
174 /* Return some of TEXT plus p. */
175 q = xmalloc (strlen (p) + (text - word) + 5);
176 strncpy (q, word, text - word);
177 q[text - word] = '\0';
179 return_val[return_val_used++] = q;
184 /* There is no way to do this just long enough to affect quote
185 inserting without also affecting the next completion. This
186 should be fixed in readline. FIXME. */
187 /* Ensure that readline does the right thing
188 with respect to inserting quotes. */
189 rl_completer_word_break_characters = "";
194 /* Complete on locations, which might be of two possible forms:
200 This is intended to be used in commands that set breakpoints
204 location_completer (struct cmd_list_element *ignore,
205 char *text, char *word)
207 int n_syms = 0, n_files = 0;
208 char ** fn_list = NULL;
212 int quoted = *text == '\'' || *text == '"';
213 int quote_char = '\0';
215 char *file_to_match = NULL;
216 char *symbol_start = text;
217 char *orig_text = text;
220 /* Do we have an unquoted colon, as in "break foo.c::bar"? */
221 for (p = text; *p != '\0'; ++p)
223 if (*p == '\\' && p[1] == '\'')
225 else if (*p == '\'' || *p == '"')
229 while (*p != '\0' && *p != quote_found)
231 if (*p == '\\' && p[1] == quote_found)
236 if (*p == quote_found)
239 break; /* Hit the end of text. */
241 #if HAVE_DOS_BASED_FILE_SYSTEM
242 /* If we have a DOS-style absolute file name at the beginning of
243 TEXT, and the colon after the drive letter is the only colon
244 we found, pretend the colon is not there. */
245 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
248 else if (*p == ':' && !colon)
251 symbol_start = p + 1;
253 else if (strchr (current_language->la_word_break_characters(), *p))
254 symbol_start = p + 1;
259 text_len = strlen (text);
261 /* Where is the file name? */
266 file_to_match = (char *) xmalloc (colon - text + 1);
267 strncpy (file_to_match, text, colon - text + 1);
268 /* Remove trailing colons and quotes from the file name. */
269 for (s = file_to_match + (colon - text);
272 if (*s == ':' || *s == quote_char)
275 /* If the text includes a colon, they want completion only on a
276 symbol name after the colon. Otherwise, we need to complete on
277 symbols as well as on files. */
280 list = make_file_symbol_completion_list (symbol_start, word,
282 xfree (file_to_match);
286 list = make_symbol_completion_list (symbol_start, word);
287 /* If text includes characters which cannot appear in a file
288 name, they cannot be asking for completion on files. */
290 gdb_completer_file_name_break_characters) == text_len)
291 fn_list = make_source_files_completion_list (text, text);
294 /* How many completions do we have in both lists? */
296 for ( ; fn_list[n_files]; n_files++)
299 for ( ; list[n_syms]; n_syms++)
302 /* Make list[] large enough to hold both lists, then catenate
303 fn_list[] onto the end of list[]. */
304 if (n_syms && n_files)
306 list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
307 memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
312 /* If we only have file names as possible completion, we should
313 bring them in sync with what rl_complete expects. The
314 problem is that if the user types "break /foo/b TAB", and the
315 possible completions are "/foo/bar" and "/foo/baz"
316 rl_complete expects us to return "bar" and "baz", without the
317 leading directories, as possible completions, because `word'
318 starts at the "b". But we ignore the value of `word' when we
319 call make_source_files_completion_list above (because that
320 would not DTRT when the completion results in both symbols
321 and file names), so make_source_files_completion_list returns
322 the full "/foo/bar" and "/foo/baz" strings. This produces
323 wrong results when, e.g., there's only one possible
324 completion, because rl_complete will prepend "/foo/" to each
325 candidate completion. The loop below removes that leading
327 for (n_files = 0; fn_list[n_files]; n_files++)
329 memmove (fn_list[n_files], fn_list[n_files] + (word - text),
330 strlen (fn_list[n_files]) + 1 - (word - text));
332 /* Return just the file-name list as the result. */
337 /* No completions at all. As the final resort, try completing
338 on the entire text as a symbol. */
339 list = make_symbol_completion_list (orig_text, word);
348 /* Helper for expression_completer which recursively counts the number
349 of named fields and methods in a structure or union type. */
351 count_struct_fields (struct type *type)
355 CHECK_TYPEDEF (type);
356 for (i = 0; i < TYPE_NFIELDS (type); ++i)
358 if (i < TYPE_N_BASECLASSES (type))
359 result += count_struct_fields (TYPE_BASECLASS (type, i));
360 else if (TYPE_FIELD_NAME (type, i))
362 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
364 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
366 /* Recurse into anonymous unions. */
367 result += count_struct_fields (TYPE_FIELD_TYPE (type, i));
372 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
374 if (TYPE_FN_FIELDLIST_NAME (type, i))
381 /* Helper for expression_completer which recursively adds field and
382 method names from TYPE, a struct or union type, to the array
383 OUTPUT. This function assumes that OUTPUT is correctly-sized. */
385 add_struct_fields (struct type *type, int *nextp, char **output,
386 char *fieldname, int namelen)
389 int computed_type_name = 0;
390 char *type_name = NULL;
392 CHECK_TYPEDEF (type);
393 for (i = 0; i < TYPE_NFIELDS (type); ++i)
395 if (i < TYPE_N_BASECLASSES (type))
396 add_struct_fields (TYPE_BASECLASS (type, i), nextp,
397 output, fieldname, namelen);
398 else if (TYPE_FIELD_NAME (type, i))
400 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
402 if (! strncmp (TYPE_FIELD_NAME (type, i),
405 output[*nextp] = xstrdup (TYPE_FIELD_NAME (type, i));
409 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
411 /* Recurse into anonymous unions. */
412 add_struct_fields (TYPE_FIELD_TYPE (type, i), nextp,
413 output, fieldname, namelen);
418 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
420 char *name = TYPE_FN_FIELDLIST_NAME (type, i);
422 if (name && ! strncmp (name, fieldname, namelen))
424 if (!computed_type_name)
426 type_name = type_name_no_tag (type);
427 computed_type_name = 1;
429 /* Omit constructors from the completion list. */
430 if (!type_name || strcmp (type_name, name))
432 output[*nextp] = xstrdup (name);
439 /* Complete on expressions. Often this means completing on symbol
440 names, but some language parsers also have support for completing
443 expression_completer (struct cmd_list_element *ignore,
444 char *text, char *word)
446 struct type *type = NULL;
448 volatile struct gdb_exception except;
450 /* Perform a tentative parse of the expression, to see whether a
451 field completion is required. */
453 TRY_CATCH (except, RETURN_MASK_ERROR)
455 type = parse_field_expression (text, &fieldname);
457 if (except.reason < 0)
459 if (fieldname && type)
463 CHECK_TYPEDEF (type);
464 if (TYPE_CODE (type) != TYPE_CODE_PTR
465 && TYPE_CODE (type) != TYPE_CODE_REF)
467 type = TYPE_TARGET_TYPE (type);
470 if (TYPE_CODE (type) == TYPE_CODE_UNION
471 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
473 int alloc = count_struct_fields (type);
474 int flen = strlen (fieldname);
476 char **result = (char **) xmalloc ((alloc + 1) * sizeof (char *));
478 add_struct_fields (type, &out, result, fieldname, flen);
486 /* Commands which complete on locations want to see the entire
489 p > text && p[-1] != ' ' && p[-1] != '\t';
493 /* Not ideal but it is what we used to do before... */
494 return location_completer (ignore, p, word);
497 /* Here are some useful test cases for completion. FIXME: These
498 should be put in the test suite. They should be tested with both
501 "show output-" "radix"
502 "show output" "-radix"
503 "p" ambiguous (commands starting with p--path, print, printf, etc.)
504 "p " ambiguous (all symbols)
505 "info t foo" no completions
506 "info t " no completions
507 "info t" ambiguous ("info target", "info terminal", etc.)
508 "info ajksdlfk" no completions
509 "info ajksdlfk " no completions
511 "info " ambiguous (all info commands)
512 "p \"a" no completions (string constant)
513 "p 'a" ambiguous (all symbols starting with a)
514 "p b-a" ambiguous (all symbols starting with a)
515 "p b-" ambiguous (all symbols)
516 "file Make" "file" (word break hard to screw up here)
517 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
526 complete_line_internal_reason;
529 /* Internal function used to handle completions.
532 TEXT is the caller's idea of the "word" we are looking at.
534 LINE_BUFFER is available to be looked at; it contains the entire
535 text of the line. POINT is the offset in that line of the cursor.
536 You should pretend that the line ends at POINT.
538 REASON is of type complete_line_internal_reason.
540 If REASON is handle_brkchars:
541 Preliminary phase, called by gdb_completion_word_break_characters
542 function, is used to determine the correct set of chars that are
543 word delimiters depending on the current command in line_buffer.
544 No completion list should be generated; the return value should be
545 NULL. This is checked by an assertion in that function.
547 If REASON is handle_completions:
548 Main phase, called by complete_line function, is used to get the list
549 of posible completions.
551 If REASON is handle_help:
552 Special case when completing a 'help' command. In this case,
553 once sub-command completions are exhausted, we simply return NULL.
557 complete_line_internal (const char *text,
558 char *line_buffer, int point,
559 complete_line_internal_reason reason)
562 char *tmp_command, *p;
563 /* Pointer within tmp_command which corresponds to text. */
565 struct cmd_list_element *c, *result_list;
567 /* Choose the default set of word break characters to break
568 completions. If we later find out that we are doing completions
569 on command strings (as opposed to strings supplied by the
570 individual command completer functions, which can be any string)
571 then we will switch to the special word break set for command
572 strings, which leaves out the '-' character used in some
574 rl_completer_word_break_characters =
575 current_language->la_word_break_characters();
577 /* Decide whether to complete on a list of gdb commands or on
579 tmp_command = (char *) alloca (point + 1);
582 strncpy (tmp_command, line_buffer, point);
583 tmp_command[point] = '\0';
584 /* Since text always contains some number of characters leading up
585 to point, we can find the equivalent position in tmp_command
586 by subtracting that many characters from the end of tmp_command. */
587 word = tmp_command + point - strlen (text);
591 /* An empty line we want to consider ambiguous; that is, it
592 could be any command. */
593 c = (struct cmd_list_element *) -1;
598 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
601 /* Move p up to the next interesting thing. */
602 while (*p == ' ' || *p == '\t')
609 /* It is an unrecognized command. So there are no
610 possible completions. */
613 else if (c == (struct cmd_list_element *) -1)
617 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
618 doesn't advance over that thing itself. Do so now. */
620 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
622 if (q != tmp_command + point)
624 /* There is something beyond the ambiguous
625 command, so there are no possible completions. For
626 example, "info t " or "info t foo" does not complete
627 to anything, because "info t" can be "info target" or
633 /* We're trying to complete on the command which was ambiguous.
634 This we can deal with. */
637 if (reason != handle_brkchars)
638 list = complete_on_cmdlist (*result_list->prefixlist, p,
643 if (reason != handle_brkchars)
644 list = complete_on_cmdlist (cmdlist, p, word);
646 /* Ensure that readline does the right thing with respect to
648 rl_completer_word_break_characters =
649 gdb_completer_command_word_break_characters;
654 /* We've recognized a full command. */
656 if (p == tmp_command + point)
658 /* There is no non-whitespace in the line beyond the
661 if (p[-1] == ' ' || p[-1] == '\t')
663 /* The command is followed by whitespace; we need to
664 complete on whatever comes after command. */
667 /* It is a prefix command; what comes after it is
668 a subcommand (e.g. "info "). */
669 if (reason != handle_brkchars)
670 list = complete_on_cmdlist (*c->prefixlist, p, word);
672 /* Ensure that readline does the right thing
673 with respect to inserting quotes. */
674 rl_completer_word_break_characters =
675 gdb_completer_command_word_break_characters;
677 else if (reason == handle_help)
681 if (reason != handle_brkchars)
682 list = complete_on_enum (c->enums, p, word);
683 rl_completer_word_break_characters =
684 gdb_completer_command_word_break_characters;
688 /* It is a normal command; what comes after it is
689 completed by the command's completer function. */
690 if (c->completer == filename_completer)
692 /* Many commands which want to complete on
693 file names accept several file names, as
694 in "run foo bar >>baz". So we don't want
695 to complete the entire text after the
696 command, just the last word. To this
697 end, we need to find the beginning of the
698 file name by starting at `word' and going
702 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
705 rl_completer_word_break_characters =
706 gdb_completer_file_name_break_characters;
708 else if (c->completer == location_completer)
710 /* Commands which complete on locations want to
711 see the entire argument. */
714 && p[-1] != ' ' && p[-1] != '\t';
718 if (reason != handle_brkchars && c->completer != NULL)
719 list = (*c->completer) (c, p, word);
724 /* The command is not followed by whitespace; we need to
725 complete on the command itself, e.g. "p" which is a
726 command itself but also can complete to "print", "ptype"
730 /* Find the command we are completing on. */
732 while (q > tmp_command)
734 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
740 if (reason != handle_brkchars)
741 list = complete_on_cmdlist (result_list, q, word);
743 /* Ensure that readline does the right thing
744 with respect to inserting quotes. */
745 rl_completer_word_break_characters =
746 gdb_completer_command_word_break_characters;
749 else if (reason == handle_help)
753 /* There is non-whitespace beyond the command. */
755 if (c->prefixlist && !c->allow_unknown)
757 /* It is an unrecognized subcommand of a prefix command,
758 e.g. "info adsfkdj". */
763 if (reason != handle_brkchars)
764 list = complete_on_enum (c->enums, p, word);
768 /* It is a normal command. */
769 if (c->completer == filename_completer)
771 /* See the commentary above about the specifics
772 of file-name completion. */
775 && strchr (gdb_completer_file_name_break_characters,
779 rl_completer_word_break_characters =
780 gdb_completer_file_name_break_characters;
782 else if (c->completer == location_completer)
786 && p[-1] != ' ' && p[-1] != '\t';
790 if (reason != handle_brkchars && c->completer != NULL)
791 list = (*c->completer) (c, p, word);
798 /* Generate completions all at once. Returns a NULL-terminated array
799 of strings. Both the array and each element are allocated with
800 xmalloc. It can also return NULL if there are no completions.
802 TEXT is the caller's idea of the "word" we are looking at.
804 LINE_BUFFER is available to be looked at; it contains the entire
807 POINT is the offset in that line of the cursor. You
808 should pretend that the line ends at POINT. */
811 complete_line (const char *text, char *line_buffer, int point)
813 return complete_line_internal (text, line_buffer,
814 point, handle_completions);
817 /* Complete on command names. Used by "help". */
819 command_completer (struct cmd_list_element *ignore,
820 char *text, char *word)
822 return complete_line_internal (word, text,
823 strlen (text), handle_help);
826 /* Get the list of chars that are considered as word breaks
827 for the current command. */
830 gdb_completion_word_break_characters (void)
834 list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
836 gdb_assert (list == NULL);
837 return rl_completer_word_break_characters;
840 /* Generate completions one by one for the completer. Each time we
841 are called return another potential completion to the caller.
842 line_completion just completes on commands or passes the buck to
843 the command's completer function, the stuff specific to symbol
844 completion is in make_symbol_completion_list.
846 TEXT is the caller's idea of the "word" we are looking at.
848 MATCHES is the number of matches that have currently been collected
849 from calling this completion function. When zero, then we need to
850 initialize, otherwise the initialization has already taken place
851 and we can just return the next potential completion string.
853 LINE_BUFFER is available to be looked at; it contains the entire
854 text of the line. POINT is the offset in that line of the cursor.
855 You should pretend that the line ends at POINT.
857 Returns NULL if there are no more completions, else a pointer to a
858 string which is a possible completion, it is the caller's
859 responsibility to free the string. */
862 line_completion_function (const char *text, int matches,
863 char *line_buffer, int point)
865 static char **list = (char **) NULL; /* Cache of completions. */
866 static int index; /* Next cached completion. */
871 /* The caller is beginning to accumulate a new set of
872 completions, so we need to find all of them now, and cache
873 them for returning one at a time on future calls. */
877 /* Free the storage used by LIST, but not by the strings
878 inside. This is because rl_complete_internal () frees
879 the strings. As complete_line may abort by calling
880 `error' clear LIST now. */
885 list = complete_line (text, line_buffer, point);
888 /* If we found a list of potential completions during initialization
889 then dole them out one at a time. The vector of completions is
890 NULL terminated, so after returning the last one, return NULL
891 (and continue to do so) each time we are called after that, until
892 a new list is available. */
896 output = list[index];
904 /* Can't do this because readline hasn't yet checked the word breaks
905 for figuring out whether to insert a quote. */
907 /* Make sure the word break characters are set back to normal for
908 the next time that readline tries to complete something. */
909 rl_completer_word_break_characters =
910 current_language->la_word_break_characters();
916 /* Skip over the possibly quoted word STR (as defined by the quote
917 characters QUOTECHARS and the the word break characters
918 BREAKCHARS). Returns pointer to the location after the "word". If
919 either QUOTECHARS or BREAKCHARS is NULL, use the same values used
923 skip_quoted_chars (char *str, char *quotechars, char *breakchars)
925 char quote_char = '\0';
928 if (quotechars == NULL)
929 quotechars = gdb_completer_quote_characters;
931 if (breakchars == NULL)
932 breakchars = current_language->la_word_break_characters();
934 for (scan = str; *scan != '\0'; scan++)
936 if (quote_char != '\0')
938 /* Ignore everything until the matching close quote char. */
939 if (*scan == quote_char)
941 /* Found matching close quote. */
946 else if (strchr (quotechars, *scan))
948 /* Found start of a quoted string. */
951 else if (strchr (breakchars, *scan))
960 /* Skip over the possibly quoted word STR (as defined by the quote
961 characters and word break characters used by the completer).
962 Returns pointer to the location after the "word". */
965 skip_quoted (char *str)
967 return skip_quoted_chars (str, NULL, NULL);