1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright 2000, 2001 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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "expression.h"
25 #include "filenames.h" /* for DOSish file names */
27 /* FIXME: This is needed because of lookup_cmd_1().
28 We should be calling a hook instead so we eliminate the CLI dependency. */
31 /* Needed for rl_completer_word_break_characters() and for
32 filename_completion_function. */
33 #include <readline/readline.h>
35 /* readline defines this. */
38 #include "completer.h"
40 /* Prototypes for local functions */
41 char *line_completion_function (char *text, int matches, char *line_buffer,
44 /* readline uses the word breaks for two things:
45 (1) In figuring out where to point the TEXT parameter to the
46 rl_completion_entry_function. Since we don't use TEXT for much,
47 it doesn't matter a lot what the word breaks are for this purpose, but
48 it does affect how much stuff M-? lists.
49 (2) If one of the matches contains a word break character, readline
50 will quote it. That's why we switch between
51 gdb_completer_word_break_characters and
52 gdb_completer_command_word_break_characters. I'm not sure when
53 we need this behavior (perhaps for funky characters in C++ symbols?). */
55 /* Variables which are necessary for fancy command line editing. */
56 static char *gdb_completer_word_break_characters =
57 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
59 /* When completing on command names, we remove '-' from the list of
60 word break characters, since we use it in command names. If the
61 readline library sees one in any of the current completion strings,
62 it thinks that the string needs to be quoted and automatically supplies
64 static char *gdb_completer_command_word_break_characters =
65 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
67 /* When completing on file names, we remove from the list of word
68 break characters any characters that are commonly used in file
69 names, such as '-', '+', '~', etc. Otherwise, readline displays
70 incorrect completion candidates. */
71 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
72 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
73 programs support @foo style response files. */
74 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
76 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
79 /* These are used when completing on locations, which can mix file
80 names and symbol names separated by a colon. */
81 static char *gdb_completer_loc_break_characters = " \t\n*|\"';:?><,";
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
86 static char *gdb_completer_quote_characters = "'";
88 /* Accessor for some completer data that may interest other files. */
91 get_gdb_completer_word_break_characters (void)
93 return gdb_completer_word_break_characters;
97 get_gdb_completer_quote_characters (void)
99 return gdb_completer_quote_characters;
102 /* Line completion interface function for readline. */
105 readline_line_completion_function (char *text, int matches)
107 return line_completion_function (text, matches, rl_line_buffer, rl_point);
110 /* This can be used for functions which don't want to complete on symbols
111 but don't want to complete on anything else either. */
113 noop_completer (char *text, char *prefix)
118 /* Complete on filenames. */
120 filename_completer (char *text, char *word)
125 int return_val_alloced;
128 /* Small for testing. */
129 return_val_alloced = 1;
130 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
136 p = filename_completion_function (text, subsequent_name);
137 if (return_val_used >= return_val_alloced)
139 return_val_alloced *= 2;
141 (char **) xrealloc (return_val,
142 return_val_alloced * sizeof (char *));
146 return_val[return_val_used++] = p;
149 /* We need to set subsequent_name to a non-zero value before the
150 continue line below, because otherwise, if the first file seen
151 by GDB is a backup file whose name ends in a `~', we will loop
154 /* Like emacs, don't complete on old versions. Especially useful
155 in the "source" command. */
156 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;
185 /* There is no way to do this just long enough to affect quote inserting
186 without also affecting the next completion. This should be fixed in
188 /* Insure that readline does the right thing
189 with respect to inserting quotes. */
190 rl_completer_word_break_characters = "";
195 /* Complete on locations, which might be of two possible forms:
201 This is intended to be used in commands that set breakpoints etc. */
203 location_completer (char *text, char *word)
205 int n_syms = 0, n_files = 0;
206 char ** fn_list = NULL;
210 int quoted = *text == '\'' || *text == '"';
211 int quote_char = '\0';
213 char *file_to_match = NULL;
214 char *symbol_start = text;
215 char *orig_text = text;
218 /* Do we have an unquoted colon, as in "break foo.c::bar"? */
219 for (p = text; *p != '\0'; ++p)
221 if (*p == '\\' && p[1] == '\'')
223 else if (*p == '\'' || *p == '"')
227 while (*p != '\0' && *p != quote_found)
229 if (*p == '\\' && p[1] == quote_found)
234 if (*p == quote_found)
237 break; /* hit the end of text */
239 #if HAVE_DOS_BASED_FILE_SYSTEM
240 /* If we have a DOS-style absolute file name at the beginning of
241 TEXT, and the colon after the drive letter is the only colon
242 we found, pretend the colon is not there. */
243 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
246 else if (*p == ':' && !colon)
249 symbol_start = p + 1;
251 else if (strchr (gdb_completer_word_break_characters, *p))
252 symbol_start = p + 1;
257 text_len = strlen (text);
259 /* Where is the file name? */
264 file_to_match = (char *) xmalloc (colon - text + 1);
265 strncpy (file_to_match, text, colon - text + 1);
266 /* Remove trailing colons and quotes from the file name. */
267 for (s = file_to_match + (colon - text);
270 if (*s == ':' || *s == quote_char)
273 /* If the text includes a colon, they want completion only on a
274 symbol name after the colon. Otherwise, we need to complete on
275 symbols as well as on files. */
278 list = make_file_symbol_completion_list (symbol_start, word,
280 xfree (file_to_match);
284 list = make_symbol_completion_list (symbol_start, word);
285 /* If text includes characters which cannot appear in a file
286 name, they cannot be asking for completion on files. */
287 if (strcspn (text, gdb_completer_file_name_break_characters) == text_len)
288 fn_list = make_source_files_completion_list (text, text);
291 /* How many completions do we have in both lists? */
293 for ( ; fn_list[n_files]; n_files++)
296 for ( ; list[n_syms]; n_syms++)
299 /* Make list[] large enough to hold both lists, then catenate
300 fn_list[] onto the end of list[]. */
301 if (n_syms && n_files)
303 list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
304 memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
309 /* If we only have file names as possible completion, we should
310 bring them in sync with what rl_complete expects. The
311 problem is that if the user types "break /foo/b TAB", and the
312 possible completions are "/foo/bar" and "/foo/baz"
313 rl_complete expects us to return "bar" and "baz", without the
314 leading directories, as possible completions, because `word'
315 starts at the "b". But we ignore the value of `word' when we
316 call make_source_files_completion_list above (because that
317 would not DTRT when the completion results in both symbols
318 and file names), so make_source_files_completion_list returns
319 the full "/foo/bar" and "/foo/baz" strings. This produces
320 wrong results when, e.g., there's only one possible
321 completion, because rl_complete will prepend "/foo/" to each
322 candidate completion. The loop below removes that leading
324 for (n_files = 0; fn_list[n_files]; n_files++)
326 memmove (fn_list[n_files], fn_list[n_files] + (word - text),
327 strlen (fn_list[n_files]) + 1 - (word - text));
329 /* Return just the file-name list as the result. */
334 /* No completions at all. As the final resort, try completing
335 on the entire text as a symbol. */
336 list = make_symbol_completion_list (orig_text, word);
342 /* Complete on command names. Used by "help". */
344 command_completer (char *text, char *word)
346 return complete_on_cmdlist (cmdlist, text, word);
350 /* Here are some useful test cases for completion. FIXME: These should
351 be put in the test suite. They should be tested with both M-? and TAB.
353 "show output-" "radix"
354 "show output" "-radix"
355 "p" ambiguous (commands starting with p--path, print, printf, etc.)
356 "p " ambiguous (all symbols)
357 "info t foo" no completions
358 "info t " no completions
359 "info t" ambiguous ("info target", "info terminal", etc.)
360 "info ajksdlfk" no completions
361 "info ajksdlfk " no completions
363 "info " ambiguous (all info commands)
364 "p \"a" no completions (string constant)
365 "p 'a" ambiguous (all symbols starting with a)
366 "p b-a" ambiguous (all symbols starting with a)
367 "p b-" ambiguous (all symbols)
368 "file Make" "file" (word break hard to screw up here)
369 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
372 /* Generate completions all at once. Returns a NULL-terminated array
373 of strings. Both the array and each element are allocated with
374 xmalloc. It can also return NULL if there are no completions.
376 TEXT is the caller's idea of the "word" we are looking at.
378 LINE_BUFFER is available to be looked at; it contains the entire text
379 of the line. POINT is the offset in that line of the cursor. You
380 should pretend that the line ends at POINT. */
383 complete_line (char *text, char *line_buffer, int point)
386 char *tmp_command, *p;
387 /* Pointer within tmp_command which corresponds to text. */
389 struct cmd_list_element *c, *result_list;
391 /* Choose the default set of word break characters to break completions.
392 If we later find out that we are doing completions on command strings
393 (as opposed to strings supplied by the individual command completer
394 functions, which can be any string) then we will switch to the
395 special word break set for command strings, which leaves out the
396 '-' character used in some commands. */
398 rl_completer_word_break_characters =
399 gdb_completer_word_break_characters;
401 /* Decide whether to complete on a list of gdb commands or on symbols. */
402 tmp_command = (char *) alloca (point + 1);
405 strncpy (tmp_command, line_buffer, point);
406 tmp_command[point] = '\0';
407 /* Since text always contains some number of characters leading up
408 to point, we can find the equivalent position in tmp_command
409 by subtracting that many characters from the end of tmp_command. */
410 word = tmp_command + point - strlen (text);
414 /* An empty line we want to consider ambiguous; that is, it
415 could be any command. */
416 c = (struct cmd_list_element *) -1;
421 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
424 /* Move p up to the next interesting thing. */
425 while (*p == ' ' || *p == '\t')
432 /* It is an unrecognized command. So there are no
433 possible completions. */
436 else if (c == (struct cmd_list_element *) -1)
440 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
441 doesn't advance over that thing itself. Do so now. */
443 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
445 if (q != tmp_command + point)
447 /* There is something beyond the ambiguous
448 command, so there are no possible completions. For
449 example, "info t " or "info t foo" does not complete
450 to anything, because "info t" can be "info target" or
456 /* We're trying to complete on the command which was ambiguous.
457 This we can deal with. */
460 list = complete_on_cmdlist (*result_list->prefixlist, p,
465 list = complete_on_cmdlist (cmdlist, p, word);
467 /* Insure that readline does the right thing with respect to
469 rl_completer_word_break_characters =
470 gdb_completer_command_word_break_characters;
475 /* We've recognized a full command. */
477 if (p == tmp_command + point)
479 /* There is no non-whitespace in the line beyond the command. */
481 if (p[-1] == ' ' || p[-1] == '\t')
483 /* The command is followed by whitespace; we need to complete
484 on whatever comes after command. */
487 /* It is a prefix command; what comes after it is
488 a subcommand (e.g. "info "). */
489 list = complete_on_cmdlist (*c->prefixlist, p, word);
491 /* Insure that readline does the right thing
492 with respect to inserting quotes. */
493 rl_completer_word_break_characters =
494 gdb_completer_command_word_break_characters;
498 list = complete_on_enum (c->enums, p, word);
499 rl_completer_word_break_characters =
500 gdb_completer_command_word_break_characters;
504 /* It is a normal command; what comes after it is
505 completed by the command's completer function. */
506 if (c->completer == filename_completer)
508 /* Many commands which want to complete on
509 file names accept several file names, as
510 in "run foo bar >>baz". So we don't want
511 to complete the entire text after the
512 command, just the last word. To this
513 end, we need to find the beginning of the
514 file name by starting at `word' and going
518 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
521 rl_completer_word_break_characters =
522 gdb_completer_file_name_break_characters;
524 else if (c->completer == location_completer)
526 /* Commands which complete on locations want to
527 see the entire argument. */
530 && p[-1] != ' ' && p[-1] != '\t';
534 list = (*c->completer) (p, word);
539 /* The command is not followed by whitespace; we need to
540 complete on the command itself. e.g. "p" which is a
541 command itself but also can complete to "print", "ptype"
545 /* Find the command we are completing on. */
547 while (q > tmp_command)
549 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
555 list = complete_on_cmdlist (result_list, q, word);
557 /* Insure that readline does the right thing
558 with respect to inserting quotes. */
559 rl_completer_word_break_characters =
560 gdb_completer_command_word_break_characters;
565 /* There is non-whitespace beyond the command. */
567 if (c->prefixlist && !c->allow_unknown)
569 /* It is an unrecognized subcommand of a prefix command,
570 e.g. "info adsfkdj". */
575 list = complete_on_enum (c->enums, p, word);
579 /* It is a normal command. */
580 if (c->completer == filename_completer)
582 /* See the commentary above about the specifics
583 of file-name completion. */
586 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
589 rl_completer_word_break_characters =
590 gdb_completer_file_name_break_characters;
592 else if (c->completer == location_completer)
596 && p[-1] != ' ' && p[-1] != '\t';
600 list = (*c->completer) (p, word);
608 /* Generate completions one by one for the completer. Each time we are
609 called return another potential completion to the caller.
610 line_completion just completes on commands or passes the buck to the
611 command's completer function, the stuff specific to symbol completion
612 is in make_symbol_completion_list.
614 TEXT is the caller's idea of the "word" we are looking at.
616 MATCHES is the number of matches that have currently been collected from
617 calling this completion function. When zero, then we need to initialize,
618 otherwise the initialization has already taken place and we can just
619 return the next potential completion string.
621 LINE_BUFFER is available to be looked at; it contains the entire text
622 of the line. POINT is the offset in that line of the cursor. You
623 should pretend that the line ends at POINT.
625 Returns NULL if there are no more completions, else a pointer to a string
626 which is a possible completion, it is the caller's responsibility to
630 line_completion_function (char *text, int matches, char *line_buffer, int point)
632 static char **list = (char **) NULL; /* Cache of completions */
633 static int index; /* Next cached completion */
638 /* The caller is beginning to accumulate a new set of completions, so
639 we need to find all of them now, and cache them for returning one at
640 a time on future calls. */
644 /* Free the storage used by LIST, but not by the strings inside.
645 This is because rl_complete_internal () frees the strings. */
649 list = complete_line (text, line_buffer, point);
652 /* If we found a list of potential completions during initialization then
653 dole them out one at a time. The vector of completions is NULL
654 terminated, so after returning the last one, return NULL (and continue
655 to do so) each time we are called after that, until a new list is
660 output = list[index];
668 /* Can't do this because readline hasn't yet checked the word breaks
669 for figuring out whether to insert a quote. */
671 /* Make sure the word break characters are set back to normal for the
672 next time that readline tries to complete something. */
673 rl_completer_word_break_characters =
674 gdb_completer_word_break_characters;
679 /* Skip over a possibly quoted word (as defined by the quote characters
680 and word break characters the completer uses). Returns pointer to the
681 location after the "word". */
684 skip_quoted (char *str)
686 char quote_char = '\0';
689 for (scan = str; *scan != '\0'; scan++)
691 if (quote_char != '\0')
693 /* Ignore everything until the matching close quote char */
694 if (*scan == quote_char)
696 /* Found matching close quote. */
701 else if (strchr (gdb_completer_quote_characters, *scan))
703 /* Found start of a quoted string. */
706 else if (strchr (gdb_completer_word_break_characters, *scan))