1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright 2000 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 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?/><";
69 /* Characters that can be used to quote completion strings. Note that we
70 can't include '"' because the gdb C parser treats such quoted sequences
72 static char *gdb_completer_quote_characters = "'";
74 /* Accessor for some completer data that may interest other files. */
77 get_gdb_completer_word_break_characters (void)
79 return gdb_completer_word_break_characters;
83 get_gdb_completer_quote_characters (void)
85 return gdb_completer_quote_characters;
88 /* Complete on filenames. */
90 filename_completer (char *text, char *word)
93 extern char *filename_completion_function (char *, int);
97 int return_val_alloced;
100 /* Small for testing. */
101 return_val_alloced = 1;
102 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
108 p = filename_completion_function (text, subsequent_name);
109 if (return_val_used >= return_val_alloced)
111 return_val_alloced *= 2;
113 (char **) xrealloc (return_val,
114 return_val_alloced * sizeof (char *));
118 return_val[return_val_used++] = p;
121 /* We need to set subsequent_name to a non-zero value before the
122 continue line below, because otherwise, if the first file seen
123 by GDB is a backup file whose name ends in a `~', we will loop
126 /* Like emacs, don't complete on old versions. Especially useful
127 in the "source" command. */
128 if (p[strlen (p) - 1] == '~')
134 /* Return exactly p. */
135 return_val[return_val_used++] = p;
136 else if (word > text)
138 /* Return some portion of p. */
139 q = xmalloc (strlen (p) + 5);
140 strcpy (q, p + (word - text));
141 return_val[return_val_used++] = q;
146 /* Return some of TEXT plus p. */
147 q = xmalloc (strlen (p) + (text - word) + 5);
148 strncpy (q, word, text - word);
149 q[text - word] = '\0';
151 return_val[return_val_used++] = q;
157 /* There is no way to do this just long enough to affect quote inserting
158 without also affecting the next completion. This should be fixed in
160 /* Insure that readline does the right thing
161 with respect to inserting quotes. */
162 rl_completer_word_break_characters = "";
167 /* Here are some useful test cases for completion. FIXME: These should
168 be put in the test suite. They should be tested with both M-? and TAB.
170 "show output-" "radix"
171 "show output" "-radix"
172 "p" ambiguous (commands starting with p--path, print, printf, etc.)
173 "p " ambiguous (all symbols)
174 "info t foo" no completions
175 "info t " no completions
176 "info t" ambiguous ("info target", "info terminal", etc.)
177 "info ajksdlfk" no completions
178 "info ajksdlfk " no completions
180 "info " ambiguous (all info commands)
181 "p \"a" no completions (string constant)
182 "p 'a" ambiguous (all symbols starting with a)
183 "p b-a" ambiguous (all symbols starting with a)
184 "p b-" ambiguous (all symbols)
185 "file Make" "file" (word break hard to screw up here)
186 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
189 /* Generate completions one by one for the completer. Each time we are
190 called return another potential completion to the caller.
191 line_completion just completes on commands or passes the buck to the
192 command's completer function, the stuff specific to symbol completion
193 is in make_symbol_completion_list.
195 TEXT is the caller's idea of the "word" we are looking at.
197 MATCHES is the number of matches that have currently been collected from
198 calling this completion function. When zero, then we need to initialize,
199 otherwise the initialization has already taken place and we can just
200 return the next potential completion string.
202 LINE_BUFFER is available to be looked at; it contains the entire text
203 of the line. POINT is the offset in that line of the cursor. You
204 should pretend that the line ends at POINT.
206 Returns NULL if there are no more completions, else a pointer to a string
207 which is a possible completion, it is the caller's responsibility to
211 line_completion_function (char *text, int matches, char *line_buffer, int point)
213 static char **list = (char **) NULL; /* Cache of completions */
214 static int index; /* Next cached completion */
216 char *tmp_command, *p;
217 /* Pointer within tmp_command which corresponds to text. */
219 struct cmd_list_element *c, *result_list;
223 /* The caller is beginning to accumulate a new set of completions, so
224 we need to find all of them now, and cache them for returning one at
225 a time on future calls. */
229 /* Free the storage used by LIST, but not by the strings inside.
230 This is because rl_complete_internal () frees the strings. */
236 /* Choose the default set of word break characters to break completions.
237 If we later find out that we are doing completions on command strings
238 (as opposed to strings supplied by the individual command completer
239 functions, which can be any string) then we will switch to the
240 special word break set for command strings, which leaves out the
241 '-' character used in some commands. */
243 rl_completer_word_break_characters =
244 gdb_completer_word_break_characters;
246 /* Decide whether to complete on a list of gdb commands or on symbols. */
247 tmp_command = (char *) alloca (point + 1);
250 strncpy (tmp_command, line_buffer, point);
251 tmp_command[point] = '\0';
252 /* Since text always contains some number of characters leading up
253 to point, we can find the equivalent position in tmp_command
254 by subtracting that many characters from the end of tmp_command. */
255 word = tmp_command + point - strlen (text);
259 /* An empty line we want to consider ambiguous; that is, it
260 could be any command. */
261 c = (struct cmd_list_element *) -1;
266 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
269 /* Move p up to the next interesting thing. */
270 while (*p == ' ' || *p == '\t')
277 /* It is an unrecognized command. So there are no
278 possible completions. */
281 else if (c == (struct cmd_list_element *) -1)
285 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
286 doesn't advance over that thing itself. Do so now. */
288 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
290 if (q != tmp_command + point)
292 /* There is something beyond the ambiguous
293 command, so there are no possible completions. For
294 example, "info t " or "info t foo" does not complete
295 to anything, because "info t" can be "info target" or
301 /* We're trying to complete on the command which was ambiguous.
302 This we can deal with. */
305 list = complete_on_cmdlist (*result_list->prefixlist, p,
310 list = complete_on_cmdlist (cmdlist, p, word);
312 /* Insure that readline does the right thing with respect to
314 rl_completer_word_break_characters =
315 gdb_completer_command_word_break_characters;
320 /* We've recognized a full command. */
322 if (p == tmp_command + point)
324 /* There is no non-whitespace in the line beyond the command. */
326 if (p[-1] == ' ' || p[-1] == '\t')
328 /* The command is followed by whitespace; we need to complete
329 on whatever comes after command. */
332 /* It is a prefix command; what comes after it is
333 a subcommand (e.g. "info "). */
334 list = complete_on_cmdlist (*c->prefixlist, p, word);
336 /* Insure that readline does the right thing
337 with respect to inserting quotes. */
338 rl_completer_word_break_characters =
339 gdb_completer_command_word_break_characters;
343 list = complete_on_enum (c->enums, p, word);
344 rl_completer_word_break_characters =
345 gdb_completer_command_word_break_characters;
349 /* It is a normal command; what comes after it is
350 completed by the command's completer function. */
351 list = (*c->completer) (p, word);
352 if (c->completer == filename_completer)
353 rl_completer_word_break_characters =
354 gdb_completer_file_name_break_characters;
359 /* The command is not followed by whitespace; we need to
360 complete on the command itself. e.g. "p" which is a
361 command itself but also can complete to "print", "ptype"
365 /* Find the command we are completing on. */
367 while (q > tmp_command)
369 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
375 list = complete_on_cmdlist (result_list, q, word);
377 /* Insure that readline does the right thing
378 with respect to inserting quotes. */
379 rl_completer_word_break_characters =
380 gdb_completer_command_word_break_characters;
385 /* There is non-whitespace beyond the command. */
387 if (c->prefixlist && !c->allow_unknown)
389 /* It is an unrecognized subcommand of a prefix command,
390 e.g. "info adsfkdj". */
395 list = complete_on_enum (c->enums, p, word);
399 /* It is a normal command. */
400 list = (*c->completer) (p, word);
401 if (c->completer == filename_completer)
402 rl_completer_word_break_characters =
403 gdb_completer_file_name_break_characters;
409 /* If we found a list of potential completions during initialization then
410 dole them out one at a time. The vector of completions is NULL
411 terminated, so after returning the last one, return NULL (and continue
412 to do so) each time we are called after that, until a new list is
417 output = list[index];
425 /* Can't do this because readline hasn't yet checked the word breaks
426 for figuring out whether to insert a quote. */
428 /* Make sure the word break characters are set back to normal for the
429 next time that readline tries to complete something. */
430 rl_completer_word_break_characters =
431 gdb_completer_word_break_characters;
436 /* Skip over a possibly quoted word (as defined by the quote characters
437 and word break characters the completer uses). Returns pointer to the
438 location after the "word". */
441 skip_quoted (char *str)
443 char quote_char = '\0';
446 for (scan = str; *scan != '\0'; scan++)
448 if (quote_char != '\0')
450 /* Ignore everything until the matching close quote char */
451 if (*scan == quote_char)
453 /* Found matching close quote. */
458 else if (strchr (gdb_completer_quote_characters, *scan))
460 /* Found start of a quoted string. */
463 else if (strchr (gdb_completer_word_break_characters, *scan))