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"
26 /* FIXME: This is needed because of lookup_cmd_1().
27 We should be calling a hook instead so we eliminate the CLI dependency. */
30 /* Needed for rl_completer_word_break_characters() */
31 #include <readline/readline.h>
33 /* readline defines this. */
36 #include "completer.h"
38 /* Prototypes for local functions */
40 /* readline uses the word breaks for two things:
41 (1) In figuring out where to point the TEXT parameter to the
42 rl_completion_entry_function. Since we don't use TEXT for much,
43 it doesn't matter a lot what the word breaks are for this purpose, but
44 it does affect how much stuff M-? lists.
45 (2) If one of the matches contains a word break character, readline
46 will quote it. That's why we switch between
47 gdb_completer_word_break_characters and
48 gdb_completer_command_word_break_characters. I'm not sure when
49 we need this behavior (perhaps for funky characters in C++ symbols?). */
51 /* Variables which are necessary for fancy command line editing. */
52 static char *gdb_completer_word_break_characters =
53 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
55 /* When completing on command names, we remove '-' from the list of
56 word break characters, since we use it in command names. If the
57 readline library sees one in any of the current completion strings,
58 it thinks that the string needs to be quoted and automatically supplies
60 static char *gdb_completer_command_word_break_characters =
61 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
63 /* When completing on file names, we remove from the list of word
64 break characters any characters that are commonly used in file
65 names, such as '-', '+', '~', etc. Otherwise, readline displays
66 incorrect completion candidates. */
67 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
68 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
69 programs support @foo style response files. */
70 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
72 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
75 /* Characters that can be used to quote completion strings. Note that we
76 can't include '"' because the gdb C parser treats such quoted sequences
78 static char *gdb_completer_quote_characters = "'";
80 /* Accessor for some completer data that may interest other files. */
83 get_gdb_completer_word_break_characters (void)
85 return gdb_completer_word_break_characters;
89 get_gdb_completer_quote_characters (void)
91 return gdb_completer_quote_characters;
94 /* Complete on filenames. */
96 filename_completer (char *text, char *word)
99 extern char *filename_completion_function (char *, int);
103 int return_val_alloced;
106 /* Small for testing. */
107 return_val_alloced = 1;
108 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
114 p = filename_completion_function (text, subsequent_name);
115 if (return_val_used >= return_val_alloced)
117 return_val_alloced *= 2;
119 (char **) xrealloc (return_val,
120 return_val_alloced * sizeof (char *));
124 return_val[return_val_used++] = p;
127 /* We need to set subsequent_name to a non-zero value before the
128 continue line below, because otherwise, if the first file seen
129 by GDB is a backup file whose name ends in a `~', we will loop
132 /* Like emacs, don't complete on old versions. Especially useful
133 in the "source" command. */
134 if (p[strlen (p) - 1] == '~')
140 /* Return exactly p. */
141 return_val[return_val_used++] = p;
142 else if (word > text)
144 /* Return some portion of p. */
145 q = xmalloc (strlen (p) + 5);
146 strcpy (q, p + (word - text));
147 return_val[return_val_used++] = q;
152 /* Return some of TEXT plus p. */
153 q = xmalloc (strlen (p) + (text - word) + 5);
154 strncpy (q, word, text - word);
155 q[text - word] = '\0';
157 return_val[return_val_used++] = q;
163 /* There is no way to do this just long enough to affect quote inserting
164 without also affecting the next completion. This should be fixed in
166 /* Insure that readline does the right thing
167 with respect to inserting quotes. */
168 rl_completer_word_break_characters = "";
173 /* Here are some useful test cases for completion. FIXME: These should
174 be put in the test suite. They should be tested with both M-? and TAB.
176 "show output-" "radix"
177 "show output" "-radix"
178 "p" ambiguous (commands starting with p--path, print, printf, etc.)
179 "p " ambiguous (all symbols)
180 "info t foo" no completions
181 "info t " no completions
182 "info t" ambiguous ("info target", "info terminal", etc.)
183 "info ajksdlfk" no completions
184 "info ajksdlfk " no completions
186 "info " ambiguous (all info commands)
187 "p \"a" no completions (string constant)
188 "p 'a" ambiguous (all symbols starting with a)
189 "p b-a" ambiguous (all symbols starting with a)
190 "p b-" ambiguous (all symbols)
191 "file Make" "file" (word break hard to screw up here)
192 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
195 /* Generate completions one by one for the completer. Each time we are
196 called return another potential completion to the caller.
197 line_completion just completes on commands or passes the buck to the
198 command's completer function, the stuff specific to symbol completion
199 is in make_symbol_completion_list.
201 TEXT is the caller's idea of the "word" we are looking at.
203 MATCHES is the number of matches that have currently been collected from
204 calling this completion function. When zero, then we need to initialize,
205 otherwise the initialization has already taken place and we can just
206 return the next potential completion string.
208 LINE_BUFFER is available to be looked at; it contains the entire text
209 of the line. POINT is the offset in that line of the cursor. You
210 should pretend that the line ends at POINT.
212 Returns NULL if there are no more completions, else a pointer to a string
213 which is a possible completion, it is the caller's responsibility to
217 line_completion_function (char *text, int matches, char *line_buffer, int point)
219 static char **list = (char **) NULL; /* Cache of completions */
220 static int index; /* Next cached completion */
222 char *tmp_command, *p;
223 /* Pointer within tmp_command which corresponds to text. */
225 struct cmd_list_element *c, *result_list;
229 /* The caller is beginning to accumulate a new set of completions, so
230 we need to find all of them now, and cache them for returning one at
231 a time on future calls. */
235 /* Free the storage used by LIST, but not by the strings inside.
236 This is because rl_complete_internal () frees the strings. */
242 /* Choose the default set of word break characters to break completions.
243 If we later find out that we are doing completions on command strings
244 (as opposed to strings supplied by the individual command completer
245 functions, which can be any string) then we will switch to the
246 special word break set for command strings, which leaves out the
247 '-' character used in some commands. */
249 rl_completer_word_break_characters =
250 gdb_completer_word_break_characters;
252 /* Decide whether to complete on a list of gdb commands or on symbols. */
253 tmp_command = (char *) alloca (point + 1);
256 strncpy (tmp_command, line_buffer, point);
257 tmp_command[point] = '\0';
258 /* Since text always contains some number of characters leading up
259 to point, we can find the equivalent position in tmp_command
260 by subtracting that many characters from the end of tmp_command. */
261 word = tmp_command + point - strlen (text);
265 /* An empty line we want to consider ambiguous; that is, it
266 could be any command. */
267 c = (struct cmd_list_element *) -1;
272 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
275 /* Move p up to the next interesting thing. */
276 while (*p == ' ' || *p == '\t')
283 /* It is an unrecognized command. So there are no
284 possible completions. */
287 else if (c == (struct cmd_list_element *) -1)
291 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
292 doesn't advance over that thing itself. Do so now. */
294 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
296 if (q != tmp_command + point)
298 /* There is something beyond the ambiguous
299 command, so there are no possible completions. For
300 example, "info t " or "info t foo" does not complete
301 to anything, because "info t" can be "info target" or
307 /* We're trying to complete on the command which was ambiguous.
308 This we can deal with. */
311 list = complete_on_cmdlist (*result_list->prefixlist, p,
316 list = complete_on_cmdlist (cmdlist, p, word);
318 /* Insure that readline does the right thing with respect to
320 rl_completer_word_break_characters =
321 gdb_completer_command_word_break_characters;
326 /* We've recognized a full command. */
328 if (p == tmp_command + point)
330 /* There is no non-whitespace in the line beyond the command. */
332 if (p[-1] == ' ' || p[-1] == '\t')
334 /* The command is followed by whitespace; we need to complete
335 on whatever comes after command. */
338 /* It is a prefix command; what comes after it is
339 a subcommand (e.g. "info "). */
340 list = complete_on_cmdlist (*c->prefixlist, p, word);
342 /* Insure that readline does the right thing
343 with respect to inserting quotes. */
344 rl_completer_word_break_characters =
345 gdb_completer_command_word_break_characters;
349 list = complete_on_enum (c->enums, p, word);
350 rl_completer_word_break_characters =
351 gdb_completer_command_word_break_characters;
355 /* It is a normal command; what comes after it is
356 completed by the command's completer function. */
357 if (c->completer == filename_completer)
359 /* Many commands which want to complete on
360 file names accept several file names, as
361 in "run foo bar >>baz". So we don't want
362 to complete the entire text after the
363 command, just the last word. To this
364 end, we need to find the beginning of the
365 file name starting at `word' and going
369 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
372 rl_completer_word_break_characters =
373 gdb_completer_file_name_break_characters;
375 list = (*c->completer) (p, word);
380 /* The command is not followed by whitespace; we need to
381 complete on the command itself. e.g. "p" which is a
382 command itself but also can complete to "print", "ptype"
386 /* Find the command we are completing on. */
388 while (q > tmp_command)
390 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
396 list = complete_on_cmdlist (result_list, q, word);
398 /* Insure that readline does the right thing
399 with respect to inserting quotes. */
400 rl_completer_word_break_characters =
401 gdb_completer_command_word_break_characters;
406 /* There is non-whitespace beyond the command. */
408 if (c->prefixlist && !c->allow_unknown)
410 /* It is an unrecognized subcommand of a prefix command,
411 e.g. "info adsfkdj". */
416 list = complete_on_enum (c->enums, p, word);
420 /* It is a normal command. */
421 if (c->completer == filename_completer)
423 /* See the commentary above about the specifics
424 of file-name completion. */
427 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
430 rl_completer_word_break_characters =
431 gdb_completer_file_name_break_characters;
433 list = (*c->completer) (p, word);
439 /* If we found a list of potential completions during initialization then
440 dole them out one at a time. The vector of completions is NULL
441 terminated, so after returning the last one, return NULL (and continue
442 to do so) each time we are called after that, until a new list is
447 output = list[index];
455 /* Can't do this because readline hasn't yet checked the word breaks
456 for figuring out whether to insert a quote. */
458 /* Make sure the word break characters are set back to normal for the
459 next time that readline tries to complete something. */
460 rl_completer_word_break_characters =
461 gdb_completer_word_break_characters;
466 /* Skip over a possibly quoted word (as defined by the quote characters
467 and word break characters the completer uses). Returns pointer to the
468 location after the "word". */
471 skip_quoted (char *str)
473 char quote_char = '\0';
476 for (scan = str; *scan != '\0'; scan++)
478 if (quote_char != '\0')
480 /* Ignore everything until the matching close quote char */
481 if (*scan == quote_char)
483 /* Found matching close quote. */
488 else if (strchr (gdb_completer_quote_characters, *scan))
490 /* Found start of a quoted string. */
493 else if (strchr (gdb_completer_word_break_characters, *scan))