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 /* Here are some useful test cases for completion. FIXME: These should
343 be put in the test suite. They should be tested with both M-? and TAB.
345 "show output-" "radix"
346 "show output" "-radix"
347 "p" ambiguous (commands starting with p--path, print, printf, etc.)
348 "p " ambiguous (all symbols)
349 "info t foo" no completions
350 "info t " no completions
351 "info t" ambiguous ("info target", "info terminal", etc.)
352 "info ajksdlfk" no completions
353 "info ajksdlfk " no completions
355 "info " ambiguous (all info commands)
356 "p \"a" no completions (string constant)
357 "p 'a" ambiguous (all symbols starting with a)
358 "p b-a" ambiguous (all symbols starting with a)
359 "p b-" ambiguous (all symbols)
360 "file Make" "file" (word break hard to screw up here)
361 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
364 /* Generate completions one by one for the completer. Each time we are
365 called return another potential completion to the caller.
366 line_completion just completes on commands or passes the buck to the
367 command's completer function, the stuff specific to symbol completion
368 is in make_symbol_completion_list.
370 TEXT is the caller's idea of the "word" we are looking at.
372 MATCHES is the number of matches that have currently been collected from
373 calling this completion function. When zero, then we need to initialize,
374 otherwise the initialization has already taken place and we can just
375 return the next potential completion string.
377 LINE_BUFFER is available to be looked at; it contains the entire text
378 of the line. POINT is the offset in that line of the cursor. You
379 should pretend that the line ends at POINT.
381 Returns NULL if there are no more completions, else a pointer to a string
382 which is a possible completion, it is the caller's responsibility to
386 line_completion_function (char *text, int matches, char *line_buffer, int point)
388 static char **list = (char **) NULL; /* Cache of completions */
389 static int index; /* Next cached completion */
391 char *tmp_command, *p;
392 /* Pointer within tmp_command which corresponds to text. */
394 struct cmd_list_element *c, *result_list;
398 /* The caller is beginning to accumulate a new set of completions, so
399 we need to find all of them now, and cache them for returning one at
400 a time on future calls. */
404 /* Free the storage used by LIST, but not by the strings inside.
405 This is because rl_complete_internal () frees the strings. */
411 /* Choose the default set of word break characters to break completions.
412 If we later find out that we are doing completions on command strings
413 (as opposed to strings supplied by the individual command completer
414 functions, which can be any string) then we will switch to the
415 special word break set for command strings, which leaves out the
416 '-' character used in some commands. */
418 rl_completer_word_break_characters =
419 gdb_completer_word_break_characters;
421 /* Decide whether to complete on a list of gdb commands or on symbols. */
422 tmp_command = (char *) alloca (point + 1);
425 strncpy (tmp_command, line_buffer, point);
426 tmp_command[point] = '\0';
427 /* Since text always contains some number of characters leading up
428 to point, we can find the equivalent position in tmp_command
429 by subtracting that many characters from the end of tmp_command. */
430 word = tmp_command + point - strlen (text);
434 /* An empty line we want to consider ambiguous; that is, it
435 could be any command. */
436 c = (struct cmd_list_element *) -1;
441 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
444 /* Move p up to the next interesting thing. */
445 while (*p == ' ' || *p == '\t')
452 /* It is an unrecognized command. So there are no
453 possible completions. */
456 else if (c == (struct cmd_list_element *) -1)
460 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
461 doesn't advance over that thing itself. Do so now. */
463 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
465 if (q != tmp_command + point)
467 /* There is something beyond the ambiguous
468 command, so there are no possible completions. For
469 example, "info t " or "info t foo" does not complete
470 to anything, because "info t" can be "info target" or
476 /* We're trying to complete on the command which was ambiguous.
477 This we can deal with. */
480 list = complete_on_cmdlist (*result_list->prefixlist, p,
485 list = complete_on_cmdlist (cmdlist, p, word);
487 /* Insure that readline does the right thing with respect to
489 rl_completer_word_break_characters =
490 gdb_completer_command_word_break_characters;
495 /* We've recognized a full command. */
497 if (p == tmp_command + point)
499 /* There is no non-whitespace in the line beyond the command. */
501 if (p[-1] == ' ' || p[-1] == '\t')
503 /* The command is followed by whitespace; we need to complete
504 on whatever comes after command. */
507 /* It is a prefix command; what comes after it is
508 a subcommand (e.g. "info "). */
509 list = complete_on_cmdlist (*c->prefixlist, p, word);
511 /* Insure that readline does the right thing
512 with respect to inserting quotes. */
513 rl_completer_word_break_characters =
514 gdb_completer_command_word_break_characters;
518 list = complete_on_enum (c->enums, p, word);
519 rl_completer_word_break_characters =
520 gdb_completer_command_word_break_characters;
524 /* It is a normal command; what comes after it is
525 completed by the command's completer function. */
526 if (c->completer == filename_completer)
528 /* Many commands which want to complete on
529 file names accept several file names, as
530 in "run foo bar >>baz". So we don't want
531 to complete the entire text after the
532 command, just the last word. To this
533 end, we need to find the beginning of the
534 file name by starting at `word' and going
538 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
541 rl_completer_word_break_characters =
542 gdb_completer_file_name_break_characters;
544 else if (c->completer == location_completer)
546 /* Commands which complete on locations want to
547 see the entire argument. */
550 && p[-1] != ' ' && p[-1] != '\t';
554 list = (*c->completer) (p, word);
559 /* The command is not followed by whitespace; we need to
560 complete on the command itself. e.g. "p" which is a
561 command itself but also can complete to "print", "ptype"
565 /* Find the command we are completing on. */
567 while (q > tmp_command)
569 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
575 list = complete_on_cmdlist (result_list, q, word);
577 /* Insure that readline does the right thing
578 with respect to inserting quotes. */
579 rl_completer_word_break_characters =
580 gdb_completer_command_word_break_characters;
585 /* There is non-whitespace beyond the command. */
587 if (c->prefixlist && !c->allow_unknown)
589 /* It is an unrecognized subcommand of a prefix command,
590 e.g. "info adsfkdj". */
595 list = complete_on_enum (c->enums, p, word);
599 /* It is a normal command. */
600 if (c->completer == filename_completer)
602 /* See the commentary above about the specifics
603 of file-name completion. */
606 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
609 rl_completer_word_break_characters =
610 gdb_completer_file_name_break_characters;
612 else if (c->completer == location_completer)
616 && p[-1] != ' ' && p[-1] != '\t';
620 list = (*c->completer) (p, word);
626 /* If we found a list of potential completions during initialization then
627 dole them out one at a time. The vector of completions is NULL
628 terminated, so after returning the last one, return NULL (and continue
629 to do so) each time we are called after that, until a new list is
634 output = list[index];
642 /* Can't do this because readline hasn't yet checked the word breaks
643 for figuring out whether to insert a quote. */
645 /* Make sure the word break characters are set back to normal for the
646 next time that readline tries to complete something. */
647 rl_completer_word_break_characters =
648 gdb_completer_word_break_characters;
653 /* Skip over a possibly quoted word (as defined by the quote characters
654 and word break characters the completer uses). Returns pointer to the
655 location after the "word". */
658 skip_quoted (char *str)
660 char quote_char = '\0';
663 for (scan = str; *scan != '\0'; scan++)
665 if (quote_char != '\0')
667 /* Ignore everything until the matching close quote char */
668 if (*scan == quote_char)
670 /* Found matching close quote. */
675 else if (strchr (gdb_completer_quote_characters, *scan))
677 /* Found start of a quoted string. */
680 else if (strchr (gdb_completer_word_break_characters, *scan))