2007-08-17 Michael Snyder <msnyder@access-company.com>
[binutils-gdb.git] / gdb / completer.c
1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000, 2001, 2007 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "filenames.h" /* For DOSish file names. */
26 #include "language.h"
27
28 #include "cli/cli-decode.h"
29
30 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
31 calling a hook instead so we eliminate the CLI dependency. */
32 #include "gdbcmd.h"
33
34 /* Needed for rl_completer_word_break_characters() and for
35 rl_filename_completion_function. */
36 #include "readline/readline.h"
37
38 /* readline defines this. */
39 #undef savestring
40
41 #include "completer.h"
42
43 /* Prototypes for local functions. */
44 static
45 char *line_completion_function (const char *text, int matches,
46 char *line_buffer,
47 int point);
48
49 /* readline uses the word breaks for two things:
50 (1) In figuring out where to point the TEXT parameter to the
51 rl_completion_entry_function. Since we don't use TEXT for much,
52 it doesn't matter a lot what the word breaks are for this purpose, but
53 it does affect how much stuff M-? lists.
54 (2) If one of the matches contains a word break character, readline
55 will quote it. That's why we switch between
56 current_language->la_word_break_characters() and
57 gdb_completer_command_word_break_characters. I'm not sure when
58 we need this behavior (perhaps for funky characters in C++ symbols?). */
59
60 /* Variables which are necessary for fancy command line editing. */
61
62 /* When completing on command names, we remove '-' from the list of
63 word break characters, since we use it in command names. If the
64 readline library sees one in any of the current completion strings,
65 it thinks that the string needs to be quoted and automatically supplies
66 a leading quote. */
67 static char *gdb_completer_command_word_break_characters =
68 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
69
70 /* When completing on file names, we remove from the list of word
71 break characters any characters that are commonly used in file
72 names, such as '-', '+', '~', etc. Otherwise, readline displays
73 incorrect completion candidates. */
74 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
75 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
76 programs support @foo style response files. */
77 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
78 #else
79 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
80 #endif
81
82 /* These are used when completing on locations, which can mix file
83 names and symbol names separated by a colon. */
84 static char *gdb_completer_loc_break_characters = " \t\n*|\"';:?><,";
85
86 /* Characters that can be used to quote completion strings. Note that we
87 can't include '"' because the gdb C parser treats such quoted sequences
88 as strings. */
89 static char *gdb_completer_quote_characters = "'";
90 \f
91 /* Accessor for some completer data that may interest other files. */
92
93 char *
94 get_gdb_completer_quote_characters (void)
95 {
96 return gdb_completer_quote_characters;
97 }
98
99 /* Line completion interface function for readline. */
100
101 char *
102 readline_line_completion_function (const char *text, int matches)
103 {
104 return line_completion_function (text, matches, rl_line_buffer, rl_point);
105 }
106
107 /* This can be used for functions which don't want to complete on symbols
108 but don't want to complete on anything else either. */
109 char **
110 noop_completer (char *text, char *prefix)
111 {
112 return NULL;
113 }
114
115 /* Complete on filenames. */
116 char **
117 filename_completer (char *text, char *word)
118 {
119 int subsequent_name;
120 char **return_val;
121 int return_val_used;
122 int return_val_alloced;
123
124 return_val_used = 0;
125 /* Small for testing. */
126 return_val_alloced = 1;
127 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
128
129 subsequent_name = 0;
130 while (1)
131 {
132 char *p, *q;
133 p = rl_filename_completion_function (text, subsequent_name);
134 if (return_val_used >= return_val_alloced)
135 {
136 return_val_alloced *= 2;
137 return_val =
138 (char **) xrealloc (return_val,
139 return_val_alloced * sizeof (char *));
140 }
141 if (p == NULL)
142 {
143 return_val[return_val_used++] = p;
144 break;
145 }
146 /* We need to set subsequent_name to a non-zero value before the
147 continue line below, because otherwise, if the first file seen
148 by GDB is a backup file whose name ends in a `~', we will loop
149 indefinitely. */
150 subsequent_name = 1;
151 /* Like emacs, don't complete on old versions. Especially useful
152 in the "source" command. */
153 if (p[strlen (p) - 1] == '~')
154 {
155 xfree (p);
156 continue;
157 }
158
159 if (word == text)
160 /* Return exactly p. */
161 return_val[return_val_used++] = p;
162 else if (word > text)
163 {
164 /* Return some portion of p. */
165 q = xmalloc (strlen (p) + 5);
166 strcpy (q, p + (word - text));
167 return_val[return_val_used++] = q;
168 xfree (p);
169 }
170 else
171 {
172 /* Return some of TEXT plus p. */
173 q = xmalloc (strlen (p) + (text - word) + 5);
174 strncpy (q, word, text - word);
175 q[text - word] = '\0';
176 strcat (q, p);
177 return_val[return_val_used++] = q;
178 xfree (p);
179 }
180 }
181 #if 0
182 /* There is no way to do this just long enough to affect quote inserting
183 without also affecting the next completion. This should be fixed in
184 readline. FIXME. */
185 /* Ensure that readline does the right thing
186 with respect to inserting quotes. */
187 rl_completer_word_break_characters = "";
188 #endif
189 return return_val;
190 }
191
192 /* Complete on locations, which might be of two possible forms:
193
194 file:line
195 or
196 symbol+offset
197
198 This is intended to be used in commands that set breakpoints etc. */
199 char **
200 location_completer (char *text, char *word)
201 {
202 int n_syms = 0, n_files = 0;
203 char ** fn_list = NULL;
204 char ** list = NULL;
205 char *p;
206 int quote_found = 0;
207 int quoted = *text == '\'' || *text == '"';
208 int quote_char = '\0';
209 char *colon = NULL;
210 char *file_to_match = NULL;
211 char *symbol_start = text;
212 char *orig_text = text;
213 size_t text_len;
214
215 /* Do we have an unquoted colon, as in "break foo.c::bar"? */
216 for (p = text; *p != '\0'; ++p)
217 {
218 if (*p == '\\' && p[1] == '\'')
219 p++;
220 else if (*p == '\'' || *p == '"')
221 {
222 quote_found = *p;
223 quote_char = *p++;
224 while (*p != '\0' && *p != quote_found)
225 {
226 if (*p == '\\' && p[1] == quote_found)
227 p++;
228 p++;
229 }
230
231 if (*p == quote_found)
232 quote_found = 0;
233 else
234 break; /* Hit the end of text. */
235 }
236 #if HAVE_DOS_BASED_FILE_SYSTEM
237 /* If we have a DOS-style absolute file name at the beginning of
238 TEXT, and the colon after the drive letter is the only colon
239 we found, pretend the colon is not there. */
240 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
241 ;
242 #endif
243 else if (*p == ':' && !colon)
244 {
245 colon = p;
246 symbol_start = p + 1;
247 }
248 else if (strchr (current_language->la_word_break_characters(), *p))
249 symbol_start = p + 1;
250 }
251
252 if (quoted)
253 text++;
254 text_len = strlen (text);
255
256 /* Where is the file name? */
257 if (colon)
258 {
259 char *s;
260
261 file_to_match = (char *) xmalloc (colon - text + 1);
262 strncpy (file_to_match, text, colon - text + 1);
263 /* Remove trailing colons and quotes from the file name. */
264 for (s = file_to_match + (colon - text);
265 s > file_to_match;
266 s--)
267 if (*s == ':' || *s == quote_char)
268 *s = '\0';
269 }
270 /* If the text includes a colon, they want completion only on a
271 symbol name after the colon. Otherwise, we need to complete on
272 symbols as well as on files. */
273 if (colon)
274 {
275 list = make_file_symbol_completion_list (symbol_start, word,
276 file_to_match);
277 xfree (file_to_match);
278 }
279 else
280 {
281 list = make_symbol_completion_list (symbol_start, word);
282 /* If text includes characters which cannot appear in a file
283 name, they cannot be asking for completion on files. */
284 if (strcspn (text,
285 gdb_completer_file_name_break_characters) == text_len)
286 fn_list = make_source_files_completion_list (text, text);
287 }
288
289 /* How many completions do we have in both lists? */
290 if (fn_list)
291 for ( ; fn_list[n_files]; n_files++)
292 ;
293 if (list)
294 for ( ; list[n_syms]; n_syms++)
295 ;
296
297 /* Make list[] large enough to hold both lists, then catenate
298 fn_list[] onto the end of list[]. */
299 if (n_syms && n_files)
300 {
301 list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
302 memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
303 xfree (fn_list);
304 }
305 else if (n_files)
306 {
307 /* If we only have file names as possible completion, we should
308 bring them in sync with what rl_complete expects. The
309 problem is that if the user types "break /foo/b TAB", and the
310 possible completions are "/foo/bar" and "/foo/baz"
311 rl_complete expects us to return "bar" and "baz", without the
312 leading directories, as possible completions, because `word'
313 starts at the "b". But we ignore the value of `word' when we
314 call make_source_files_completion_list above (because that
315 would not DTRT when the completion results in both symbols
316 and file names), so make_source_files_completion_list returns
317 the full "/foo/bar" and "/foo/baz" strings. This produces
318 wrong results when, e.g., there's only one possible
319 completion, because rl_complete will prepend "/foo/" to each
320 candidate completion. The loop below removes that leading
321 part. */
322 for (n_files = 0; fn_list[n_files]; n_files++)
323 {
324 memmove (fn_list[n_files], fn_list[n_files] + (word - text),
325 strlen (fn_list[n_files]) + 1 - (word - text));
326 }
327 /* Return just the file-name list as the result. */
328 list = fn_list;
329 }
330 else if (!n_syms)
331 {
332 /* No completions at all. As the final resort, try completing
333 on the entire text as a symbol. */
334 list = make_symbol_completion_list (orig_text, word);
335 xfree (fn_list);
336 }
337 else
338 xfree (fn_list);
339
340 return list;
341 }
342
343 /* Complete on command names. Used by "help". */
344 char **
345 command_completer (char *text, char *word)
346 {
347 return complete_on_cmdlist (cmdlist, text, word);
348 }
349
350
351 /* Here are some useful test cases for completion. FIXME: These should
352 be put in the test suite. They should be tested with both M-? and TAB.
353
354 "show output-" "radix"
355 "show output" "-radix"
356 "p" ambiguous (commands starting with p--path, print, printf, etc.)
357 "p " ambiguous (all symbols)
358 "info t foo" no completions
359 "info t " no completions
360 "info t" ambiguous ("info target", "info terminal", etc.)
361 "info ajksdlfk" no completions
362 "info ajksdlfk " no completions
363 "info" " "
364 "info " ambiguous (all info commands)
365 "p \"a" no completions (string constant)
366 "p 'a" ambiguous (all symbols starting with a)
367 "p b-a" ambiguous (all symbols starting with a)
368 "p b-" ambiguous (all symbols)
369 "file Make" "file" (word break hard to screw up here)
370 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
371 */
372
373 /* Generate completions all at once. Returns a NULL-terminated array
374 of strings. Both the array and each element are allocated with
375 xmalloc. It can also return NULL if there are no completions.
376
377 TEXT is the caller's idea of the "word" we are looking at.
378
379 LINE_BUFFER is available to be looked at; it contains the entire text
380 of the line. POINT is the offset in that line of the cursor. You
381 should pretend that the line ends at POINT. */
382
383 char **
384 complete_line (const char *text, char *line_buffer, int point)
385 {
386 char **list = NULL;
387 char *tmp_command, *p;
388 /* Pointer within tmp_command which corresponds to text. */
389 char *word;
390 struct cmd_list_element *c, *result_list;
391
392 /* Choose the default set of word break characters to break completions.
393 If we later find out that we are doing completions on command strings
394 (as opposed to strings supplied by the individual command completer
395 functions, which can be any string) then we will switch to the
396 special word break set for command strings, which leaves out the
397 '-' character used in some commands. */
398
399 rl_completer_word_break_characters =
400 current_language->la_word_break_characters();
401
402 /* Decide whether to complete on a list of gdb commands or on symbols. */
403 tmp_command = (char *) alloca (point + 1);
404 p = tmp_command;
405
406 strncpy (tmp_command, line_buffer, point);
407 tmp_command[point] = '\0';
408 /* Since text always contains some number of characters leading up
409 to point, we can find the equivalent position in tmp_command
410 by subtracting that many characters from the end of tmp_command. */
411 word = tmp_command + point - strlen (text);
412
413 if (point == 0)
414 {
415 /* An empty line we want to consider ambiguous; that is, it
416 could be any command. */
417 c = (struct cmd_list_element *) -1;
418 result_list = 0;
419 }
420 else
421 {
422 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
423 }
424
425 /* Move p up to the next interesting thing. */
426 while (*p == ' ' || *p == '\t')
427 {
428 p++;
429 }
430
431 if (!c)
432 {
433 /* It is an unrecognized command. So there are no
434 possible completions. */
435 list = NULL;
436 }
437 else if (c == (struct cmd_list_element *) -1)
438 {
439 char *q;
440
441 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
442 doesn't advance over that thing itself. Do so now. */
443 q = p;
444 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
445 ++q;
446 if (q != tmp_command + point)
447 {
448 /* There is something beyond the ambiguous
449 command, so there are no possible completions. For
450 example, "info t " or "info t foo" does not complete
451 to anything, because "info t" can be "info target" or
452 "info terminal". */
453 list = NULL;
454 }
455 else
456 {
457 /* We're trying to complete on the command which was ambiguous.
458 This we can deal with. */
459 if (result_list)
460 {
461 list = complete_on_cmdlist (*result_list->prefixlist, p,
462 word);
463 }
464 else
465 {
466 list = complete_on_cmdlist (cmdlist, p, word);
467 }
468 /* Ensure that readline does the right thing with respect to
469 inserting quotes. */
470 rl_completer_word_break_characters =
471 gdb_completer_command_word_break_characters;
472 }
473 }
474 else
475 {
476 /* We've recognized a full command. */
477
478 if (p == tmp_command + point)
479 {
480 /* There is no non-whitespace in the line beyond the command. */
481
482 if (p[-1] == ' ' || p[-1] == '\t')
483 {
484 /* The command is followed by whitespace; we need to complete
485 on whatever comes after command. */
486 if (c->prefixlist)
487 {
488 /* It is a prefix command; what comes after it is
489 a subcommand (e.g. "info "). */
490 list = complete_on_cmdlist (*c->prefixlist, p, word);
491
492 /* Ensure that readline does the right thing
493 with respect to inserting quotes. */
494 rl_completer_word_break_characters =
495 gdb_completer_command_word_break_characters;
496 }
497 else if (c->enums)
498 {
499 list = complete_on_enum (c->enums, p, word);
500 rl_completer_word_break_characters =
501 gdb_completer_command_word_break_characters;
502 }
503 else
504 {
505 /* It is a normal command; what comes after it is
506 completed by the command's completer function. */
507 if (c->completer == filename_completer)
508 {
509 /* Many commands which want to complete on
510 file names accept several file names, as
511 in "run foo bar >>baz". So we don't want
512 to complete the entire text after the
513 command, just the last word. To this
514 end, we need to find the beginning of the
515 file name by starting at `word' and going
516 backwards. */
517 for (p = word;
518 p > tmp_command
519 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
520 p--)
521 ;
522 rl_completer_word_break_characters =
523 gdb_completer_file_name_break_characters;
524 }
525 else if (c->completer == location_completer)
526 {
527 /* Commands which complete on locations want to
528 see the entire argument. */
529 for (p = word;
530 p > tmp_command
531 && p[-1] != ' ' && p[-1] != '\t';
532 p--)
533 ;
534 }
535 list = (*c->completer) (p, word);
536 }
537 }
538 else
539 {
540 /* The command is not followed by whitespace; we need to
541 complete on the command itself. e.g. "p" which is a
542 command itself but also can complete to "print", "ptype"
543 etc. */
544 char *q;
545
546 /* Find the command we are completing on. */
547 q = p;
548 while (q > tmp_command)
549 {
550 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
551 --q;
552 else
553 break;
554 }
555
556 list = complete_on_cmdlist (result_list, q, word);
557
558 /* Ensure that readline does the right thing
559 with respect to inserting quotes. */
560 rl_completer_word_break_characters =
561 gdb_completer_command_word_break_characters;
562 }
563 }
564 else
565 {
566 /* There is non-whitespace beyond the command. */
567
568 if (c->prefixlist && !c->allow_unknown)
569 {
570 /* It is an unrecognized subcommand of a prefix command,
571 e.g. "info adsfkdj". */
572 list = NULL;
573 }
574 else if (c->enums)
575 {
576 list = complete_on_enum (c->enums, p, word);
577 }
578 else
579 {
580 /* It is a normal command. */
581 if (c->completer == filename_completer)
582 {
583 /* See the commentary above about the specifics
584 of file-name completion. */
585 for (p = word;
586 p > tmp_command
587 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
588 p--)
589 ;
590 rl_completer_word_break_characters =
591 gdb_completer_file_name_break_characters;
592 }
593 else if (c->completer == location_completer)
594 {
595 for (p = word;
596 p > tmp_command
597 && p[-1] != ' ' && p[-1] != '\t';
598 p--)
599 ;
600 }
601 list = (*c->completer) (p, word);
602 }
603 }
604 }
605
606 return list;
607 }
608
609 /* Generate completions one by one for the completer. Each time we are
610 called return another potential completion to the caller.
611 line_completion just completes on commands or passes the buck to the
612 command's completer function, the stuff specific to symbol completion
613 is in make_symbol_completion_list.
614
615 TEXT is the caller's idea of the "word" we are looking at.
616
617 MATCHES is the number of matches that have currently been collected from
618 calling this completion function. When zero, then we need to initialize,
619 otherwise the initialization has already taken place and we can just
620 return the next potential completion string.
621
622 LINE_BUFFER is available to be looked at; it contains the entire text
623 of the line. POINT is the offset in that line of the cursor. You
624 should pretend that the line ends at POINT.
625
626 Returns NULL if there are no more completions, else a pointer to a string
627 which is a possible completion, it is the caller's responsibility to
628 free the string. */
629
630 static char *
631 line_completion_function (const char *text, int matches,
632 char *line_buffer, int point)
633 {
634 static char **list = (char **) NULL; /* Cache of completions. */
635 static int index; /* Next cached completion. */
636 char *output = NULL;
637
638 if (matches == 0)
639 {
640 /* The caller is beginning to accumulate a new set of completions, so
641 we need to find all of them now, and cache them for returning one at
642 a time on future calls. */
643
644 if (list)
645 {
646 /* Free the storage used by LIST, but not by the strings inside.
647 This is because rl_complete_internal () frees the strings. */
648 xfree (list);
649 }
650 index = 0;
651 list = complete_line (text, line_buffer, point);
652 }
653
654 /* If we found a list of potential completions during initialization then
655 dole them out one at a time. The vector of completions is NULL
656 terminated, so after returning the last one, return NULL (and continue
657 to do so) each time we are called after that, until a new list is
658 available. */
659
660 if (list)
661 {
662 output = list[index];
663 if (output)
664 {
665 index++;
666 }
667 }
668
669 #if 0
670 /* Can't do this because readline hasn't yet checked the word breaks
671 for figuring out whether to insert a quote. */
672 if (output == NULL)
673 /* Make sure the word break characters are set back to normal for the
674 next time that readline tries to complete something. */
675 rl_completer_word_break_characters =
676 current_language->la_word_break_characters();
677 #endif
678
679 return (output);
680 }
681
682 /* Skip over the possibly quoted word STR (as defined by the quote
683 characters QUOTECHARS and the the word break characters
684 BREAKCHARS). Returns pointer to the location after the "word". If
685 either QUOTECHARS or BREAKCHARS is NULL, use the same values used
686 by the completer. */
687
688 char *
689 skip_quoted_chars (char *str, char *quotechars, char *breakchars)
690 {
691 char quote_char = '\0';
692 char *scan;
693
694 if (quotechars == NULL)
695 quotechars = gdb_completer_quote_characters;
696
697 if (breakchars == NULL)
698 breakchars = current_language->la_word_break_characters();
699
700 for (scan = str; *scan != '\0'; scan++)
701 {
702 if (quote_char != '\0')
703 {
704 /* Ignore everything until the matching close quote char. */
705 if (*scan == quote_char)
706 {
707 /* Found matching close quote. */
708 scan++;
709 break;
710 }
711 }
712 else if (strchr (quotechars, *scan))
713 {
714 /* Found start of a quoted string. */
715 quote_char = *scan;
716 }
717 else if (strchr (breakchars, *scan))
718 {
719 break;
720 }
721 }
722
723 return (scan);
724 }
725
726 /* Skip over the possibly quoted word STR (as defined by the quote
727 characters and word break characters used by the completer).
728 Returns pointer to the location after the "word". */
729
730 char *
731 skip_quoted (char *str)
732 {
733 return skip_quoted_chars (str, NULL, NULL);
734 }