Merge insn decode tables, and generally tidy.
[deliverable/binutils-gdb.git] / gdb / completer.c
CommitLineData
c5f0f3d0 1/* Line completion stuff for GDB, the GNU debugger.
b6ba6518 2 Copyright 2000, 2001 Free Software Foundation, Inc.
c5f0f3d0
FN
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21#include "defs.h"
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "expression.h"
25
26/* FIXME: This is needed because of lookup_cmd_1().
27 We should be calling a hook instead so we eliminate the CLI dependency. */
28#include "gdbcmd.h"
29
30/* Needed for rl_completer_word_break_characters() */
31#include <readline/readline.h>
32
33/* readline defines this. */
34#undef savestring
35
36#include "completer.h"
37
38/* Prototypes for local functions */
39
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?). */
50
51/* Variables which are necessary for fancy command line editing. */
52static char *gdb_completer_word_break_characters =
53" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
54
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
59 a leading quote. */
60static char *gdb_completer_command_word_break_characters =
61" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
62
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. */
c3690141 67#ifdef HAVE_DOS_BASED_FILE_SYSTEM
7830cf6f
EZ
68/* MS-DOS and MS-Windows use colon as part of the drive spec, and most
69 programs support @foo style response files. */
70static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
71#else
72static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
73#endif
c5f0f3d0
FN
74
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
77 as strings. */
78static char *gdb_completer_quote_characters = "'";
79\f
80/* Accessor for some completer data that may interest other files. */
81
82char *
83get_gdb_completer_word_break_characters (void)
84{
85 return gdb_completer_word_break_characters;
86}
87
88char *
89get_gdb_completer_quote_characters (void)
90{
91 return gdb_completer_quote_characters;
92}
93
94/* Complete on filenames. */
95char **
96filename_completer (char *text, char *word)
97{
98 /* From readline. */
99extern char *filename_completion_function (char *, int);
100 int subsequent_name;
101 char **return_val;
102 int return_val_used;
103 int return_val_alloced;
104
105 return_val_used = 0;
106 /* Small for testing. */
107 return_val_alloced = 1;
108 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
109
110 subsequent_name = 0;
111 while (1)
112 {
113 char *p;
114 p = filename_completion_function (text, subsequent_name);
115 if (return_val_used >= return_val_alloced)
116 {
117 return_val_alloced *= 2;
118 return_val =
119 (char **) xrealloc (return_val,
120 return_val_alloced * sizeof (char *));
121 }
122 if (p == NULL)
123 {
124 return_val[return_val_used++] = p;
125 break;
126 }
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
130 indefinitely. */
131 subsequent_name = 1;
132 /* Like emacs, don't complete on old versions. Especially useful
133 in the "source" command. */
134 if (p[strlen (p) - 1] == '~')
135 continue;
136
137 {
138 char *q;
139 if (word == text)
140 /* Return exactly p. */
141 return_val[return_val_used++] = p;
142 else if (word > text)
143 {
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;
b8c9b27d 148 xfree (p);
c5f0f3d0
FN
149 }
150 else
151 {
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';
156 strcat (q, p);
157 return_val[return_val_used++] = q;
b8c9b27d 158 xfree (p);
c5f0f3d0
FN
159 }
160 }
161 }
162#if 0
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
165 readline. FIXME. */
166 /* Insure that readline does the right thing
167 with respect to inserting quotes. */
168 rl_completer_word_break_characters = "";
169#endif
170 return return_val;
171}
172
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.
175
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
185 "info" " "
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)
193 */
194
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.
200
201 TEXT is the caller's idea of the "word" we are looking at.
202
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.
207
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.
211
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
214 free the string. */
215
216char *
217line_completion_function (char *text, int matches, char *line_buffer, int point)
218{
219 static char **list = (char **) NULL; /* Cache of completions */
220 static int index; /* Next cached completion */
221 char *output = NULL;
222 char *tmp_command, *p;
223 /* Pointer within tmp_command which corresponds to text. */
224 char *word;
225 struct cmd_list_element *c, *result_list;
226
227 if (matches == 0)
228 {
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. */
232
233 if (list)
234 {
235 /* Free the storage used by LIST, but not by the strings inside.
236 This is because rl_complete_internal () frees the strings. */
b8c9b27d 237 xfree (list);
c5f0f3d0
FN
238 }
239 list = 0;
240 index = 0;
241
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. */
248
249 rl_completer_word_break_characters =
250 gdb_completer_word_break_characters;
251
252 /* Decide whether to complete on a list of gdb commands or on symbols. */
253 tmp_command = (char *) alloca (point + 1);
254 p = tmp_command;
255
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);
262
263 if (point == 0)
264 {
265 /* An empty line we want to consider ambiguous; that is, it
266 could be any command. */
267 c = (struct cmd_list_element *) -1;
268 result_list = 0;
269 }
270 else
271 {
272 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
273 }
274
275 /* Move p up to the next interesting thing. */
276 while (*p == ' ' || *p == '\t')
277 {
278 p++;
279 }
280
281 if (!c)
282 {
283 /* It is an unrecognized command. So there are no
284 possible completions. */
285 list = NULL;
286 }
287 else if (c == (struct cmd_list_element *) -1)
288 {
289 char *q;
290
291 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
292 doesn't advance over that thing itself. Do so now. */
293 q = p;
294 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
295 ++q;
296 if (q != tmp_command + point)
297 {
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
302 "info terminal". */
303 list = NULL;
304 }
305 else
306 {
307 /* We're trying to complete on the command which was ambiguous.
308 This we can deal with. */
309 if (result_list)
310 {
311 list = complete_on_cmdlist (*result_list->prefixlist, p,
312 word);
313 }
314 else
315 {
316 list = complete_on_cmdlist (cmdlist, p, word);
317 }
318 /* Insure that readline does the right thing with respect to
319 inserting quotes. */
320 rl_completer_word_break_characters =
321 gdb_completer_command_word_break_characters;
322 }
323 }
324 else
325 {
326 /* We've recognized a full command. */
327
328 if (p == tmp_command + point)
329 {
330 /* There is no non-whitespace in the line beyond the command. */
331
332 if (p[-1] == ' ' || p[-1] == '\t')
333 {
334 /* The command is followed by whitespace; we need to complete
335 on whatever comes after command. */
336 if (c->prefixlist)
337 {
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);
341
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;
346 }
347 else if (c->enums)
348 {
349 list = complete_on_enum (c->enums, p, word);
350 rl_completer_word_break_characters =
351 gdb_completer_command_word_break_characters;
352 }
353 else
354 {
355 /* It is a normal command; what comes after it is
356 completed by the command's completer function. */
c5f0f3d0 357 if (c->completer == filename_completer)
7830cf6f
EZ
358 {
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
366 backwards. */
367 for (p = word;
368 p > tmp_command
369 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
370 p--)
371 ;
372 rl_completer_word_break_characters =
373 gdb_completer_file_name_break_characters;
374 }
375 list = (*c->completer) (p, word);
c5f0f3d0
FN
376 }
377 }
378 else
379 {
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"
383 etc. */
384 char *q;
385
386 /* Find the command we are completing on. */
387 q = p;
388 while (q > tmp_command)
389 {
390 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
391 --q;
392 else
393 break;
394 }
395
396 list = complete_on_cmdlist (result_list, q, word);
397
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;
402 }
403 }
404 else
405 {
406 /* There is non-whitespace beyond the command. */
407
408 if (c->prefixlist && !c->allow_unknown)
409 {
410 /* It is an unrecognized subcommand of a prefix command,
411 e.g. "info adsfkdj". */
412 list = NULL;
413 }
414 else if (c->enums)
415 {
416 list = complete_on_enum (c->enums, p, word);
417 }
418 else
419 {
420 /* It is a normal command. */
c5f0f3d0 421 if (c->completer == filename_completer)
7830cf6f
EZ
422 {
423 /* See the commentary above about the specifics
424 of file-name completion. */
425 for (p = word;
426 p > tmp_command
427 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
428 p--)
429 ;
430 rl_completer_word_break_characters =
431 gdb_completer_file_name_break_characters;
432 }
433 list = (*c->completer) (p, word);
c5f0f3d0
FN
434 }
435 }
436 }
437 }
438
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
443 available. */
444
445 if (list)
446 {
447 output = list[index];
448 if (output)
449 {
450 index++;
451 }
452 }
453
454#if 0
455 /* Can't do this because readline hasn't yet checked the word breaks
456 for figuring out whether to insert a quote. */
457 if (output == NULL)
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;
462#endif
463
464 return (output);
465}
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". */
469
470char *
471skip_quoted (char *str)
472{
473 char quote_char = '\0';
474 char *scan;
475
476 for (scan = str; *scan != '\0'; scan++)
477 {
478 if (quote_char != '\0')
479 {
480 /* Ignore everything until the matching close quote char */
481 if (*scan == quote_char)
482 {
483 /* Found matching close quote. */
484 scan++;
485 break;
486 }
487 }
488 else if (strchr (gdb_completer_quote_characters, *scan))
489 {
490 /* Found start of a quoted string. */
491 quote_char = *scan;
492 }
493 else if (strchr (gdb_completer_word_break_characters, *scan))
494 {
495 break;
496 }
497 }
498 return (scan);
499}
500
This page took 0.069694 seconds and 4 git commands to generate.