1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000, 2001, 2007 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 2 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, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
24 #include "expression.h"
25 #include "filenames.h" /* For DOSish file names. */
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 /* These are used when completing on locations, which can mix file
83 names and symbol names separated by a colon. */
84 static char *gdb_completer_loc_break_characters = " \t\n*|\"';:?><,";
86 /* Characters that can be used to quote completion strings. Note that we
87 can't include '"' because the gdb C parser treats such quoted sequences
89 static char *gdb_completer_quote_characters = "'";
91 /* Accessor for some completer data that may interest other files. */
94 get_gdb_completer_quote_characters (void)
96 return gdb_completer_quote_characters;
99 /* Line completion interface function for readline. */
102 readline_line_completion_function (const char *text, int matches)
104 return line_completion_function (text, matches, rl_line_buffer, rl_point);
107 /* This can be used for functions which don't want to complete on symbols
108 but don't want to complete on anything else either. */
110 noop_completer (char *text, char *prefix)
115 /* Complete on filenames. */
117 filename_completer (char *text, char *word)
122 int return_val_alloced;
125 /* Small for testing. */
126 return_val_alloced = 1;
127 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
133 p = rl_filename_completion_function (text, subsequent_name);
134 if (return_val_used >= return_val_alloced)
136 return_val_alloced *= 2;
138 (char **) xrealloc (return_val,
139 return_val_alloced * sizeof (char *));
143 return_val[return_val_used++] = p;
146 /* We need to set subsequent_name to a non-zero value before the
147 continue line below, because otherwise, if the first file seen
148 by GDB is a backup file whose name ends in a `~', we will loop
151 /* Like emacs, don't complete on old versions. Especially useful
152 in the "source" command. */
153 if (p[strlen (p) - 1] == '~')
159 /* Return exactly p. */
160 return_val[return_val_used++] = p;
161 else if (word > text)
163 /* Return some portion of p. */
164 q = xmalloc (strlen (p) + 5);
165 strcpy (q, p + (word - text));
166 return_val[return_val_used++] = q;
171 /* Return some of TEXT plus p. */
172 q = xmalloc (strlen (p) + (text - word) + 5);
173 strncpy (q, word, text - word);
174 q[text - word] = '\0';
176 return_val[return_val_used++] = q;
182 /* There is no way to do this just long enough to affect quote inserting
183 without also affecting the next completion. This should be fixed in
185 /* Insure that readline does the right thing
186 with respect to inserting quotes. */
187 rl_completer_word_break_characters = "";
192 /* Complete on locations, which might be of two possible forms:
198 This is intended to be used in commands that set breakpoints etc. */
200 location_completer (char *text, char *word)
202 int n_syms = 0, n_files = 0;
203 char ** fn_list = NULL;
207 int quoted = *text == '\'' || *text == '"';
208 int quote_char = '\0';
210 char *file_to_match = NULL;
211 char *symbol_start = text;
212 char *orig_text = text;
215 /* Do we have an unquoted colon, as in "break foo.c::bar"? */
216 for (p = text; *p != '\0'; ++p)
218 if (*p == '\\' && p[1] == '\'')
220 else if (*p == '\'' || *p == '"')
224 while (*p != '\0' && *p != quote_found)
226 if (*p == '\\' && p[1] == quote_found)
231 if (*p == quote_found)
234 break; /* Hit the end of text. */
236 #if HAVE_DOS_BASED_FILE_SYSTEM
237 /* If we have a DOS-style absolute file name at the beginning of
238 TEXT, and the colon after the drive letter is the only colon
239 we found, pretend the colon is not there. */
240 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
243 else if (*p == ':' && !colon)
246 symbol_start = p + 1;
248 else if (strchr (current_language->la_word_break_characters(), *p))
249 symbol_start = p + 1;
254 text_len = strlen (text);
256 /* Where is the file name? */
261 file_to_match = (char *) xmalloc (colon - text + 1);
262 strncpy (file_to_match, text, colon - text + 1);
263 /* Remove trailing colons and quotes from the file name. */
264 for (s = file_to_match + (colon - text);
267 if (*s == ':' || *s == quote_char)
270 /* If the text includes a colon, they want completion only on a
271 symbol name after the colon. Otherwise, we need to complete on
272 symbols as well as on files. */
275 list = make_file_symbol_completion_list (symbol_start, word,
277 xfree (file_to_match);
281 list = make_symbol_completion_list (symbol_start, word);
282 /* If text includes characters which cannot appear in a file
283 name, they cannot be asking for completion on files. */
284 if (strcspn (text, gdb_completer_file_name_break_characters) == text_len)
285 fn_list = make_source_files_completion_list (text, text);
288 /* How many completions do we have in both lists? */
290 for ( ; fn_list[n_files]; n_files++)
293 for ( ; list[n_syms]; n_syms++)
296 /* Make list[] large enough to hold both lists, then catenate
297 fn_list[] onto the end of list[]. */
298 if (n_syms && n_files)
300 list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
301 memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
306 /* If we only have file names as possible completion, we should
307 bring them in sync with what rl_complete expects. The
308 problem is that if the user types "break /foo/b TAB", and the
309 possible completions are "/foo/bar" and "/foo/baz"
310 rl_complete expects us to return "bar" and "baz", without the
311 leading directories, as possible completions, because `word'
312 starts at the "b". But we ignore the value of `word' when we
313 call make_source_files_completion_list above (because that
314 would not DTRT when the completion results in both symbols
315 and file names), so make_source_files_completion_list returns
316 the full "/foo/bar" and "/foo/baz" strings. This produces
317 wrong results when, e.g., there's only one possible
318 completion, because rl_complete will prepend "/foo/" to each
319 candidate completion. The loop below removes that leading
321 for (n_files = 0; fn_list[n_files]; n_files++)
323 memmove (fn_list[n_files], fn_list[n_files] + (word - text),
324 strlen (fn_list[n_files]) + 1 - (word - text));
326 /* Return just the file-name list as the result. */
331 /* No completions at all. As the final resort, try completing
332 on the entire text as a symbol. */
333 list = make_symbol_completion_list (orig_text, word);
339 /* Complete on command names. Used by "help". */
341 command_completer (char *text, char *word)
343 return complete_on_cmdlist (cmdlist, text, word);
347 /* Here are some useful test cases for completion. FIXME: These should
348 be put in the test suite. They should be tested with both M-? and TAB.
350 "show output-" "radix"
351 "show output" "-radix"
352 "p" ambiguous (commands starting with p--path, print, printf, etc.)
353 "p " ambiguous (all symbols)
354 "info t foo" no completions
355 "info t " no completions
356 "info t" ambiguous ("info target", "info terminal", etc.)
357 "info ajksdlfk" no completions
358 "info ajksdlfk " no completions
360 "info " ambiguous (all info commands)
361 "p \"a" no completions (string constant)
362 "p 'a" ambiguous (all symbols starting with a)
363 "p b-a" ambiguous (all symbols starting with a)
364 "p b-" ambiguous (all symbols)
365 "file Make" "file" (word break hard to screw up here)
366 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
369 /* Generate completions all at once. Returns a NULL-terminated array
370 of strings. Both the array and each element are allocated with
371 xmalloc. It can also return NULL if there are no completions.
373 TEXT is the caller's idea of the "word" we are looking at.
375 LINE_BUFFER is available to be looked at; it contains the entire text
376 of the line. POINT is the offset in that line of the cursor. You
377 should pretend that the line ends at POINT. */
380 complete_line (const char *text, char *line_buffer, int point)
383 char *tmp_command, *p;
384 /* Pointer within tmp_command which corresponds to text. */
386 struct cmd_list_element *c, *result_list;
388 /* Choose the default set of word break characters to break completions.
389 If we later find out that we are doing completions on command strings
390 (as opposed to strings supplied by the individual command completer
391 functions, which can be any string) then we will switch to the
392 special word break set for command strings, which leaves out the
393 '-' character used in some commands. */
395 rl_completer_word_break_characters =
396 current_language->la_word_break_characters();
398 /* Decide whether to complete on a list of gdb commands or on symbols. */
399 tmp_command = (char *) alloca (point + 1);
402 strncpy (tmp_command, line_buffer, point);
403 tmp_command[point] = '\0';
404 /* Since text always contains some number of characters leading up
405 to point, we can find the equivalent position in tmp_command
406 by subtracting that many characters from the end of tmp_command. */
407 word = tmp_command + point - strlen (text);
411 /* An empty line we want to consider ambiguous; that is, it
412 could be any command. */
413 c = (struct cmd_list_element *) -1;
418 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
421 /* Move p up to the next interesting thing. */
422 while (*p == ' ' || *p == '\t')
429 /* It is an unrecognized command. So there are no
430 possible completions. */
433 else if (c == (struct cmd_list_element *) -1)
437 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
438 doesn't advance over that thing itself. Do so now. */
440 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
442 if (q != tmp_command + point)
444 /* There is something beyond the ambiguous
445 command, so there are no possible completions. For
446 example, "info t " or "info t foo" does not complete
447 to anything, because "info t" can be "info target" or
453 /* We're trying to complete on the command which was ambiguous.
454 This we can deal with. */
457 list = complete_on_cmdlist (*result_list->prefixlist, p,
462 list = complete_on_cmdlist (cmdlist, p, word);
464 /* Insure that readline does the right thing with respect to
466 rl_completer_word_break_characters =
467 gdb_completer_command_word_break_characters;
472 /* We've recognized a full command. */
474 if (p == tmp_command + point)
476 /* There is no non-whitespace in the line beyond the command. */
478 if (p[-1] == ' ' || p[-1] == '\t')
480 /* The command is followed by whitespace; we need to complete
481 on whatever comes after command. */
484 /* It is a prefix command; what comes after it is
485 a subcommand (e.g. "info "). */
486 list = complete_on_cmdlist (*c->prefixlist, p, word);
488 /* Insure that readline does the right thing
489 with respect to inserting quotes. */
490 rl_completer_word_break_characters =
491 gdb_completer_command_word_break_characters;
495 list = complete_on_enum (c->enums, p, word);
496 rl_completer_word_break_characters =
497 gdb_completer_command_word_break_characters;
501 /* It is a normal command; what comes after it is
502 completed by the command's completer function. */
503 if (c->completer == filename_completer)
505 /* Many commands which want to complete on
506 file names accept several file names, as
507 in "run foo bar >>baz". So we don't want
508 to complete the entire text after the
509 command, just the last word. To this
510 end, we need to find the beginning of the
511 file name by starting at `word' and going
515 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
518 rl_completer_word_break_characters =
519 gdb_completer_file_name_break_characters;
521 else if (c->completer == location_completer)
523 /* Commands which complete on locations want to
524 see the entire argument. */
527 && p[-1] != ' ' && p[-1] != '\t';
531 list = (*c->completer) (p, word);
536 /* The command is not followed by whitespace; we need to
537 complete on the command itself. e.g. "p" which is a
538 command itself but also can complete to "print", "ptype"
542 /* Find the command we are completing on. */
544 while (q > tmp_command)
546 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
552 list = complete_on_cmdlist (result_list, q, word);
554 /* Insure that readline does the right thing
555 with respect to inserting quotes. */
556 rl_completer_word_break_characters =
557 gdb_completer_command_word_break_characters;
562 /* There is non-whitespace beyond the command. */
564 if (c->prefixlist && !c->allow_unknown)
566 /* It is an unrecognized subcommand of a prefix command,
567 e.g. "info adsfkdj". */
572 list = complete_on_enum (c->enums, p, word);
576 /* It is a normal command. */
577 if (c->completer == filename_completer)
579 /* See the commentary above about the specifics
580 of file-name completion. */
583 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
586 rl_completer_word_break_characters =
587 gdb_completer_file_name_break_characters;
589 else if (c->completer == location_completer)
593 && p[-1] != ' ' && p[-1] != '\t';
597 list = (*c->completer) (p, word);
605 /* Generate completions one by one for the completer. Each time we are
606 called return another potential completion to the caller.
607 line_completion just completes on commands or passes the buck to the
608 command's completer function, the stuff specific to symbol completion
609 is in make_symbol_completion_list.
611 TEXT is the caller's idea of the "word" we are looking at.
613 MATCHES is the number of matches that have currently been collected from
614 calling this completion function. When zero, then we need to initialize,
615 otherwise the initialization has already taken place and we can just
616 return the next potential completion string.
618 LINE_BUFFER is available to be looked at; it contains the entire text
619 of the line. POINT is the offset in that line of the cursor. You
620 should pretend that the line ends at POINT.
622 Returns NULL if there are no more completions, else a pointer to a string
623 which is a possible completion, it is the caller's responsibility to
627 line_completion_function (const char *text, int matches,
628 char *line_buffer, int point)
630 static char **list = (char **) NULL; /* Cache of completions. */
631 static int index; /* Next cached completion. */
636 /* The caller is beginning to accumulate a new set of completions, so
637 we need to find all of them now, and cache them for returning one at
638 a time on future calls. */
642 /* Free the storage used by LIST, but not by the strings inside.
643 This is because rl_complete_internal () frees the strings. */
647 list = complete_line (text, line_buffer, point);
650 /* If we found a list of potential completions during initialization then
651 dole them out one at a time. The vector of completions is NULL
652 terminated, so after returning the last one, return NULL (and continue
653 to do so) each time we are called after that, until a new list is
658 output = list[index];
666 /* Can't do this because readline hasn't yet checked the word breaks
667 for figuring out whether to insert a quote. */
669 /* Make sure the word break characters are set back to normal for the
670 next time that readline tries to complete something. */
671 rl_completer_word_break_characters =
672 current_language->la_word_break_characters();
678 /* Skip over the possibly quoted word STR (as defined by the quote
679 characters QUOTECHARS and the the word break characters
680 BREAKCHARS). Returns pointer to the location after the "word". If
681 either QUOTECHARS or BREAKCHARS is NULL, use the same values used
685 skip_quoted_chars (char *str, char *quotechars, char *breakchars)
687 char quote_char = '\0';
690 if (quotechars == NULL)
691 quotechars = gdb_completer_quote_characters;
693 if (breakchars == NULL)
694 breakchars = current_language->la_word_break_characters();
696 for (scan = str; *scan != '\0'; scan++)
698 if (quote_char != '\0')
700 /* Ignore everything until the matching close quote char. */
701 if (*scan == quote_char)
703 /* Found matching close quote. */
708 else if (strchr (quotechars, *scan))
710 /* Found start of a quoted string. */
713 else if (strchr (breakchars, *scan))
722 /* Skip over the possibly quoted word STR (as defined by the quote
723 characters and word break characters used by the completer).
724 Returns pointer to the location after the "word". */
727 skip_quoted (char *str)
729 return skip_quoted_chars (str, NULL, NULL);