2010-11-17 Tristan Gingold <gingold@adacore.com>
[binutils-gdb.git] / gdb / completer.c
1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000, 2001, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "filenames.h" /* For DOSish file names. */
25 #include "language.h"
26 #include "gdb_assert.h"
27 #include "exceptions.h"
28
29 #include "cli/cli-decode.h"
30
31 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
32 calling a hook instead so we eliminate the CLI dependency. */
33 #include "gdbcmd.h"
34
35 /* Needed for rl_completer_word_break_characters() and for
36 rl_filename_completion_function. */
37 #include "readline/readline.h"
38
39 /* readline defines this. */
40 #undef savestring
41
42 #include "completer.h"
43
44 /* Prototypes for local functions. */
45 static
46 char *line_completion_function (const char *text, int matches,
47 char *line_buffer,
48 int point);
49
50 /* readline uses the word breaks for two things:
51 (1) In figuring out where to point the TEXT parameter to the
52 rl_completion_entry_function. Since we don't use TEXT for much,
53 it doesn't matter a lot what the word breaks are for this purpose, but
54 it does affect how much stuff M-? lists.
55 (2) If one of the matches contains a word break character, readline
56 will quote it. That's why we switch between
57 current_language->la_word_break_characters() and
58 gdb_completer_command_word_break_characters. I'm not sure when
59 we need this behavior (perhaps for funky characters in C++ symbols?). */
60
61 /* Variables which are necessary for fancy command line editing. */
62
63 /* When completing on command names, we remove '-' from the list of
64 word break characters, since we use it in command names. If the
65 readline library sees one in any of the current completion strings,
66 it thinks that the string needs to be quoted and automatically supplies
67 a leading quote. */
68 static char *gdb_completer_command_word_break_characters =
69 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
70
71 /* When completing on file names, we remove from the list of word
72 break characters any characters that are commonly used in file
73 names, such as '-', '+', '~', etc. Otherwise, readline displays
74 incorrect completion candidates. */
75 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
76 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
77 programs support @foo style response files. */
78 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
79 #else
80 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
81 #endif
82
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
85 as strings. */
86 static char *gdb_completer_quote_characters = "'";
87 \f
88 /* Accessor for some completer data that may interest other files. */
89
90 char *
91 get_gdb_completer_quote_characters (void)
92 {
93 return gdb_completer_quote_characters;
94 }
95
96 /* Line completion interface function for readline. */
97
98 char *
99 readline_line_completion_function (const char *text, int matches)
100 {
101 return line_completion_function (text, matches, rl_line_buffer, rl_point);
102 }
103
104 /* This can be used for functions which don't want to complete on symbols
105 but don't want to complete on anything else either. */
106 char **
107 noop_completer (struct cmd_list_element *ignore, char *text, char *prefix)
108 {
109 return NULL;
110 }
111
112 /* Complete on filenames. */
113 char **
114 filename_completer (struct cmd_list_element *ignore, char *text, char *word)
115 {
116 int subsequent_name;
117 char **return_val;
118 int return_val_used;
119 int return_val_alloced;
120
121 return_val_used = 0;
122 /* Small for testing. */
123 return_val_alloced = 1;
124 return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
125
126 subsequent_name = 0;
127 while (1)
128 {
129 char *p, *q;
130
131 p = rl_filename_completion_function (text, subsequent_name);
132 if (return_val_used >= return_val_alloced)
133 {
134 return_val_alloced *= 2;
135 return_val =
136 (char **) xrealloc (return_val,
137 return_val_alloced * sizeof (char *));
138 }
139 if (p == NULL)
140 {
141 return_val[return_val_used++] = p;
142 break;
143 }
144 /* We need to set subsequent_name to a non-zero value before the
145 continue line below, because otherwise, if the first file seen
146 by GDB is a backup file whose name ends in a `~', we will loop
147 indefinitely. */
148 subsequent_name = 1;
149 /* Like emacs, don't complete on old versions. Especially useful
150 in the "source" command. */
151 if (p[strlen (p) - 1] == '~')
152 {
153 xfree (p);
154 continue;
155 }
156
157 if (word == text)
158 /* Return exactly p. */
159 return_val[return_val_used++] = p;
160 else if (word > text)
161 {
162 /* Return some portion of p. */
163 q = xmalloc (strlen (p) + 5);
164 strcpy (q, p + (word - text));
165 return_val[return_val_used++] = q;
166 xfree (p);
167 }
168 else
169 {
170 /* Return some of TEXT plus p. */
171 q = xmalloc (strlen (p) + (text - word) + 5);
172 strncpy (q, word, text - word);
173 q[text - word] = '\0';
174 strcat (q, p);
175 return_val[return_val_used++] = q;
176 xfree (p);
177 }
178 }
179 #if 0
180 /* There is no way to do this just long enough to affect quote inserting
181 without also affecting the next completion. This should be fixed in
182 readline. FIXME. */
183 /* Ensure that readline does the right thing
184 with respect to inserting quotes. */
185 rl_completer_word_break_characters = "";
186 #endif
187 return return_val;
188 }
189
190 /* Complete on locations, which might be of two possible forms:
191
192 file:line
193 or
194 symbol+offset
195
196 This is intended to be used in commands that set breakpoints etc. */
197 char **
198 location_completer (struct cmd_list_element *ignore, char *text, char *word)
199 {
200 int n_syms = 0, n_files = 0;
201 char ** fn_list = NULL;
202 char ** list = NULL;
203 char *p;
204 int quote_found = 0;
205 int quoted = *text == '\'' || *text == '"';
206 int quote_char = '\0';
207 char *colon = NULL;
208 char *file_to_match = NULL;
209 char *symbol_start = text;
210 char *orig_text = text;
211 size_t text_len;
212
213 /* Do we have an unquoted colon, as in "break foo.c::bar"? */
214 for (p = text; *p != '\0'; ++p)
215 {
216 if (*p == '\\' && p[1] == '\'')
217 p++;
218 else if (*p == '\'' || *p == '"')
219 {
220 quote_found = *p;
221 quote_char = *p++;
222 while (*p != '\0' && *p != quote_found)
223 {
224 if (*p == '\\' && p[1] == quote_found)
225 p++;
226 p++;
227 }
228
229 if (*p == quote_found)
230 quote_found = 0;
231 else
232 break; /* Hit the end of text. */
233 }
234 #if HAVE_DOS_BASED_FILE_SYSTEM
235 /* If we have a DOS-style absolute file name at the beginning of
236 TEXT, and the colon after the drive letter is the only colon
237 we found, pretend the colon is not there. */
238 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
239 ;
240 #endif
241 else if (*p == ':' && !colon)
242 {
243 colon = p;
244 symbol_start = p + 1;
245 }
246 else if (strchr (current_language->la_word_break_characters(), *p))
247 symbol_start = p + 1;
248 }
249
250 if (quoted)
251 text++;
252 text_len = strlen (text);
253
254 /* Where is the file name? */
255 if (colon)
256 {
257 char *s;
258
259 file_to_match = (char *) xmalloc (colon - text + 1);
260 strncpy (file_to_match, text, colon - text + 1);
261 /* Remove trailing colons and quotes from the file name. */
262 for (s = file_to_match + (colon - text);
263 s > file_to_match;
264 s--)
265 if (*s == ':' || *s == quote_char)
266 *s = '\0';
267 }
268 /* If the text includes a colon, they want completion only on a
269 symbol name after the colon. Otherwise, we need to complete on
270 symbols as well as on files. */
271 if (colon)
272 {
273 list = make_file_symbol_completion_list (symbol_start, word,
274 file_to_match);
275 xfree (file_to_match);
276 }
277 else
278 {
279 list = make_symbol_completion_list (symbol_start, word);
280 /* If text includes characters which cannot appear in a file
281 name, they cannot be asking for completion on files. */
282 if (strcspn (text,
283 gdb_completer_file_name_break_characters) == text_len)
284 fn_list = make_source_files_completion_list (text, text);
285 }
286
287 /* How many completions do we have in both lists? */
288 if (fn_list)
289 for ( ; fn_list[n_files]; n_files++)
290 ;
291 if (list)
292 for ( ; list[n_syms]; n_syms++)
293 ;
294
295 /* Make list[] large enough to hold both lists, then catenate
296 fn_list[] onto the end of list[]. */
297 if (n_syms && n_files)
298 {
299 list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
300 memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
301 xfree (fn_list);
302 }
303 else if (n_files)
304 {
305 /* If we only have file names as possible completion, we should
306 bring them in sync with what rl_complete expects. The
307 problem is that if the user types "break /foo/b TAB", and the
308 possible completions are "/foo/bar" and "/foo/baz"
309 rl_complete expects us to return "bar" and "baz", without the
310 leading directories, as possible completions, because `word'
311 starts at the "b". But we ignore the value of `word' when we
312 call make_source_files_completion_list above (because that
313 would not DTRT when the completion results in both symbols
314 and file names), so make_source_files_completion_list returns
315 the full "/foo/bar" and "/foo/baz" strings. This produces
316 wrong results when, e.g., there's only one possible
317 completion, because rl_complete will prepend "/foo/" to each
318 candidate completion. The loop below removes that leading
319 part. */
320 for (n_files = 0; fn_list[n_files]; n_files++)
321 {
322 memmove (fn_list[n_files], fn_list[n_files] + (word - text),
323 strlen (fn_list[n_files]) + 1 - (word - text));
324 }
325 /* Return just the file-name list as the result. */
326 list = fn_list;
327 }
328 else if (!n_syms)
329 {
330 /* No completions at all. As the final resort, try completing
331 on the entire text as a symbol. */
332 list = make_symbol_completion_list (orig_text, word);
333 xfree (fn_list);
334 }
335 else
336 xfree (fn_list);
337
338 return list;
339 }
340
341 /* Helper for expression_completer which recursively counts the number
342 of named fields and methods in a structure or union type. */
343 static int
344 count_struct_fields (struct type *type)
345 {
346 int i, result = 0;
347
348 CHECK_TYPEDEF (type);
349 for (i = 0; i < TYPE_NFIELDS (type); ++i)
350 {
351 if (i < TYPE_N_BASECLASSES (type))
352 result += count_struct_fields (TYPE_BASECLASS (type, i));
353 else if (TYPE_FIELD_NAME (type, i))
354 {
355 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
356 ++result;
357 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
358 {
359 /* Recurse into anonymous unions. */
360 result += count_struct_fields (TYPE_FIELD_TYPE (type, i));
361 }
362 }
363 }
364
365 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
366 {
367 if (TYPE_FN_FIELDLIST_NAME (type, i))
368 ++result;
369 }
370
371 return result;
372 }
373
374 /* Helper for expression_completer which recursively adds field and
375 method names from TYPE, a struct or union type, to the array
376 OUTPUT. This function assumes that OUTPUT is correctly-sized. */
377 static void
378 add_struct_fields (struct type *type, int *nextp, char **output,
379 char *fieldname, int namelen)
380 {
381 int i;
382 int computed_type_name = 0;
383 char *type_name = NULL;
384
385 CHECK_TYPEDEF (type);
386 for (i = 0; i < TYPE_NFIELDS (type); ++i)
387 {
388 if (i < TYPE_N_BASECLASSES (type))
389 add_struct_fields (TYPE_BASECLASS (type, i), nextp, output,
390 fieldname, namelen);
391 else if (TYPE_FIELD_NAME (type, i))
392 {
393 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
394 {
395 if (! strncmp (TYPE_FIELD_NAME (type, i), fieldname, namelen))
396 {
397 output[*nextp] = xstrdup (TYPE_FIELD_NAME (type, i));
398 ++*nextp;
399 }
400 }
401 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
402 {
403 /* Recurse into anonymous unions. */
404 add_struct_fields (TYPE_FIELD_TYPE (type, i), nextp, output,
405 fieldname, namelen);
406 }
407 }
408 }
409
410 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
411 {
412 char *name = TYPE_FN_FIELDLIST_NAME (type, i);
413
414 if (name && ! strncmp (name, fieldname, namelen))
415 {
416 if (!computed_type_name)
417 {
418 type_name = type_name_no_tag (type);
419 computed_type_name = 1;
420 }
421 /* Omit constructors from the completion list. */
422 if (!type_name || strcmp (type_name, name))
423 {
424 output[*nextp] = xstrdup (name);
425 ++*nextp;
426 }
427 }
428 }
429 }
430
431 /* Complete on expressions. Often this means completing on symbol
432 names, but some language parsers also have support for completing
433 field names. */
434 char **
435 expression_completer (struct cmd_list_element *ignore, char *text, char *word)
436 {
437 struct type *type = NULL;
438 char *fieldname, *p;
439 volatile struct gdb_exception except;
440
441 /* Perform a tentative parse of the expression, to see whether a
442 field completion is required. */
443 fieldname = NULL;
444 TRY_CATCH (except, RETURN_MASK_ERROR)
445 {
446 type = parse_field_expression (text, &fieldname);
447 }
448 if (except.reason < 0)
449 return NULL;
450 if (fieldname && type)
451 {
452 for (;;)
453 {
454 CHECK_TYPEDEF (type);
455 if (TYPE_CODE (type) != TYPE_CODE_PTR
456 && TYPE_CODE (type) != TYPE_CODE_REF)
457 break;
458 type = TYPE_TARGET_TYPE (type);
459 }
460
461 if (TYPE_CODE (type) == TYPE_CODE_UNION
462 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
463 {
464 int alloc = count_struct_fields (type);
465 int flen = strlen (fieldname);
466 int out = 0;
467 char **result = (char **) xmalloc ((alloc + 1) * sizeof (char *));
468
469 add_struct_fields (type, &out, result, fieldname, flen);
470 result[out] = NULL;
471 xfree (fieldname);
472 return result;
473 }
474 }
475 xfree (fieldname);
476
477 /* Commands which complete on locations want to see the entire
478 argument. */
479 for (p = word;
480 p > text && p[-1] != ' ' && p[-1] != '\t';
481 p--)
482 ;
483
484 /* Not ideal but it is what we used to do before... */
485 return location_completer (ignore, p, word);
486 }
487
488 /* Here are some useful test cases for completion. FIXME: These should
489 be put in the test suite. They should be tested with both M-? and TAB.
490
491 "show output-" "radix"
492 "show output" "-radix"
493 "p" ambiguous (commands starting with p--path, print, printf, etc.)
494 "p " ambiguous (all symbols)
495 "info t foo" no completions
496 "info t " no completions
497 "info t" ambiguous ("info target", "info terminal", etc.)
498 "info ajksdlfk" no completions
499 "info ajksdlfk " no completions
500 "info" " "
501 "info " ambiguous (all info commands)
502 "p \"a" no completions (string constant)
503 "p 'a" ambiguous (all symbols starting with a)
504 "p b-a" ambiguous (all symbols starting with a)
505 "p b-" ambiguous (all symbols)
506 "file Make" "file" (word break hard to screw up here)
507 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
508 */
509
510 typedef enum
511 {
512 handle_brkchars,
513 handle_completions,
514 handle_help
515 }
516 complete_line_internal_reason;
517
518
519 /* Internal function used to handle completions.
520
521
522 TEXT is the caller's idea of the "word" we are looking at.
523
524 LINE_BUFFER is available to be looked at; it contains the entire text
525 of the line. POINT is the offset in that line of the cursor. You
526 should pretend that the line ends at POINT.
527
528 REASON is of type complete_line_internal_reason.
529
530 If REASON is handle_brkchars:
531 Preliminary phase, called by gdb_completion_word_break_characters function,
532 is used to determine the correct set of chars that are word delimiters
533 depending on the current command in line_buffer.
534 No completion list should be generated; the return value should be NULL.
535 This is checked by an assertion in that function.
536
537 If REASON is handle_completions:
538 Main phase, called by complete_line function, is used to get the list
539 of posible completions.
540
541 If REASON is handle_help:
542 Special case when completing a 'help' command. In this case,
543 once sub-command completions are exhausted, we simply return NULL.
544 */
545
546 static char **
547 complete_line_internal (const char *text, char *line_buffer, int point,
548 complete_line_internal_reason reason)
549 {
550 char **list = NULL;
551 char *tmp_command, *p;
552 /* Pointer within tmp_command which corresponds to text. */
553 char *word;
554 struct cmd_list_element *c, *result_list;
555
556 /* Choose the default set of word break characters to break completions.
557 If we later find out that we are doing completions on command strings
558 (as opposed to strings supplied by the individual command completer
559 functions, which can be any string) then we will switch to the
560 special word break set for command strings, which leaves out the
561 '-' character used in some commands. */
562 rl_completer_word_break_characters =
563 current_language->la_word_break_characters();
564
565 /* Decide whether to complete on a list of gdb commands or on symbols. */
566 tmp_command = (char *) alloca (point + 1);
567 p = tmp_command;
568
569 strncpy (tmp_command, line_buffer, point);
570 tmp_command[point] = '\0';
571 /* Since text always contains some number of characters leading up
572 to point, we can find the equivalent position in tmp_command
573 by subtracting that many characters from the end of tmp_command. */
574 word = tmp_command + point - strlen (text);
575
576 if (point == 0)
577 {
578 /* An empty line we want to consider ambiguous; that is, it
579 could be any command. */
580 c = (struct cmd_list_element *) -1;
581 result_list = 0;
582 }
583 else
584 {
585 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
586 }
587
588 /* Move p up to the next interesting thing. */
589 while (*p == ' ' || *p == '\t')
590 {
591 p++;
592 }
593
594 if (!c)
595 {
596 /* It is an unrecognized command. So there are no
597 possible completions. */
598 list = NULL;
599 }
600 else if (c == (struct cmd_list_element *) -1)
601 {
602 char *q;
603
604 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
605 doesn't advance over that thing itself. Do so now. */
606 q = p;
607 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
608 ++q;
609 if (q != tmp_command + point)
610 {
611 /* There is something beyond the ambiguous
612 command, so there are no possible completions. For
613 example, "info t " or "info t foo" does not complete
614 to anything, because "info t" can be "info target" or
615 "info terminal". */
616 list = NULL;
617 }
618 else
619 {
620 /* We're trying to complete on the command which was ambiguous.
621 This we can deal with. */
622 if (result_list)
623 {
624 if (reason != handle_brkchars)
625 list = complete_on_cmdlist (*result_list->prefixlist, p,
626 word);
627 }
628 else
629 {
630 if (reason != handle_brkchars)
631 list = complete_on_cmdlist (cmdlist, p, word);
632 }
633 /* Ensure that readline does the right thing with respect to
634 inserting quotes. */
635 rl_completer_word_break_characters =
636 gdb_completer_command_word_break_characters;
637 }
638 }
639 else
640 {
641 /* We've recognized a full command. */
642
643 if (p == tmp_command + point)
644 {
645 /* There is no non-whitespace in the line beyond the command. */
646
647 if (p[-1] == ' ' || p[-1] == '\t')
648 {
649 /* The command is followed by whitespace; we need to complete
650 on whatever comes after command. */
651 if (c->prefixlist)
652 {
653 /* It is a prefix command; what comes after it is
654 a subcommand (e.g. "info "). */
655 if (reason != handle_brkchars)
656 list = complete_on_cmdlist (*c->prefixlist, p, word);
657
658 /* Ensure that readline does the right thing
659 with respect to inserting quotes. */
660 rl_completer_word_break_characters =
661 gdb_completer_command_word_break_characters;
662 }
663 else if (reason == handle_help)
664 list = NULL;
665 else if (c->enums)
666 {
667 if (reason != handle_brkchars)
668 list = complete_on_enum (c->enums, p, word);
669 rl_completer_word_break_characters =
670 gdb_completer_command_word_break_characters;
671 }
672 else
673 {
674 /* It is a normal command; what comes after it is
675 completed by the command's completer function. */
676 if (c->completer == filename_completer)
677 {
678 /* Many commands which want to complete on
679 file names accept several file names, as
680 in "run foo bar >>baz". So we don't want
681 to complete the entire text after the
682 command, just the last word. To this
683 end, we need to find the beginning of the
684 file name by starting at `word' and going
685 backwards. */
686 for (p = word;
687 p > tmp_command
688 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
689 p--)
690 ;
691 rl_completer_word_break_characters =
692 gdb_completer_file_name_break_characters;
693 }
694 else if (c->completer == location_completer)
695 {
696 /* Commands which complete on locations want to
697 see the entire argument. */
698 for (p = word;
699 p > tmp_command
700 && p[-1] != ' ' && p[-1] != '\t';
701 p--)
702 ;
703 }
704 if (reason != handle_brkchars && c->completer != NULL)
705 list = (*c->completer) (c, p, word);
706 }
707 }
708 else
709 {
710 /* The command is not followed by whitespace; we need to
711 complete on the command itself. e.g. "p" which is a
712 command itself but also can complete to "print", "ptype"
713 etc. */
714 char *q;
715
716 /* Find the command we are completing on. */
717 q = p;
718 while (q > tmp_command)
719 {
720 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
721 --q;
722 else
723 break;
724 }
725
726 if (reason != handle_brkchars)
727 list = complete_on_cmdlist (result_list, q, word);
728
729 /* Ensure that readline does the right thing
730 with respect to inserting quotes. */
731 rl_completer_word_break_characters =
732 gdb_completer_command_word_break_characters;
733 }
734 }
735 else if (reason == handle_help)
736 list = NULL;
737 else
738 {
739 /* There is non-whitespace beyond the command. */
740
741 if (c->prefixlist && !c->allow_unknown)
742 {
743 /* It is an unrecognized subcommand of a prefix command,
744 e.g. "info adsfkdj". */
745 list = NULL;
746 }
747 else if (c->enums)
748 {
749 if (reason != handle_brkchars)
750 list = complete_on_enum (c->enums, p, word);
751 }
752 else
753 {
754 /* It is a normal command. */
755 if (c->completer == filename_completer)
756 {
757 /* See the commentary above about the specifics
758 of file-name completion. */
759 for (p = word;
760 p > tmp_command
761 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
762 p--)
763 ;
764 rl_completer_word_break_characters =
765 gdb_completer_file_name_break_characters;
766 }
767 else if (c->completer == location_completer)
768 {
769 for (p = word;
770 p > tmp_command
771 && p[-1] != ' ' && p[-1] != '\t';
772 p--)
773 ;
774 }
775 if (reason != handle_brkchars && c->completer != NULL)
776 list = (*c->completer) (c, p, word);
777 }
778 }
779 }
780
781 return list;
782 }
783 /* Generate completions all at once. Returns a NULL-terminated array
784 of strings. Both the array and each element are allocated with
785 xmalloc. It can also return NULL if there are no completions.
786
787 TEXT is the caller's idea of the "word" we are looking at.
788
789 LINE_BUFFER is available to be looked at; it contains the entire text
790 of the line.
791
792 POINT is the offset in that line of the cursor. You
793 should pretend that the line ends at POINT. */
794
795 char **
796 complete_line (const char *text, char *line_buffer, int point)
797 {
798 return complete_line_internal (text, line_buffer, point, handle_completions);
799 }
800
801 /* Complete on command names. Used by "help". */
802 char **
803 command_completer (struct cmd_list_element *ignore, char *text, char *word)
804 {
805 return complete_line_internal (word, text, strlen (text), handle_help);
806 }
807
808 /* Get the list of chars that are considered as word breaks
809 for the current command. */
810
811 char *
812 gdb_completion_word_break_characters (void)
813 {
814 char **list;
815
816 list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
817 handle_brkchars);
818 gdb_assert (list == NULL);
819 return rl_completer_word_break_characters;
820 }
821
822 /* Generate completions one by one for the completer. Each time we are
823 called return another potential completion to the caller.
824 line_completion just completes on commands or passes the buck to the
825 command's completer function, the stuff specific to symbol completion
826 is in make_symbol_completion_list.
827
828 TEXT is the caller's idea of the "word" we are looking at.
829
830 MATCHES is the number of matches that have currently been collected from
831 calling this completion function. When zero, then we need to initialize,
832 otherwise the initialization has already taken place and we can just
833 return the next potential completion string.
834
835 LINE_BUFFER is available to be looked at; it contains the entire text
836 of the line. POINT is the offset in that line of the cursor. You
837 should pretend that the line ends at POINT.
838
839 Returns NULL if there are no more completions, else a pointer to a string
840 which is a possible completion, it is the caller's responsibility to
841 free the string. */
842
843 static char *
844 line_completion_function (const char *text, int matches,
845 char *line_buffer, int point)
846 {
847 static char **list = (char **) NULL; /* Cache of completions. */
848 static int index; /* Next cached completion. */
849 char *output = NULL;
850
851 if (matches == 0)
852 {
853 /* The caller is beginning to accumulate a new set of completions, so
854 we need to find all of them now, and cache them for returning one at
855 a time on future calls. */
856
857 if (list)
858 {
859 /* Free the storage used by LIST, but not by the strings inside.
860 This is because rl_complete_internal () frees the strings.
861 As complete_line may abort by calling `error' clear LIST now. */
862 xfree (list);
863 list = NULL;
864 }
865 index = 0;
866 list = complete_line (text, line_buffer, point);
867 }
868
869 /* If we found a list of potential completions during initialization then
870 dole them out one at a time. The vector of completions is NULL
871 terminated, so after returning the last one, return NULL (and continue
872 to do so) each time we are called after that, until a new list is
873 available. */
874
875 if (list)
876 {
877 output = list[index];
878 if (output)
879 {
880 index++;
881 }
882 }
883
884 #if 0
885 /* Can't do this because readline hasn't yet checked the word breaks
886 for figuring out whether to insert a quote. */
887 if (output == NULL)
888 /* Make sure the word break characters are set back to normal for the
889 next time that readline tries to complete something. */
890 rl_completer_word_break_characters =
891 current_language->la_word_break_characters();
892 #endif
893
894 return (output);
895 }
896
897 /* Skip over the possibly quoted word STR (as defined by the quote
898 characters QUOTECHARS and the the word break characters
899 BREAKCHARS). Returns pointer to the location after the "word". If
900 either QUOTECHARS or BREAKCHARS is NULL, use the same values used
901 by the completer. */
902
903 char *
904 skip_quoted_chars (char *str, char *quotechars, char *breakchars)
905 {
906 char quote_char = '\0';
907 char *scan;
908
909 if (quotechars == NULL)
910 quotechars = gdb_completer_quote_characters;
911
912 if (breakchars == NULL)
913 breakchars = current_language->la_word_break_characters();
914
915 for (scan = str; *scan != '\0'; scan++)
916 {
917 if (quote_char != '\0')
918 {
919 /* Ignore everything until the matching close quote char. */
920 if (*scan == quote_char)
921 {
922 /* Found matching close quote. */
923 scan++;
924 break;
925 }
926 }
927 else if (strchr (quotechars, *scan))
928 {
929 /* Found start of a quoted string. */
930 quote_char = *scan;
931 }
932 else if (strchr (breakchars, *scan))
933 {
934 break;
935 }
936 }
937
938 return (scan);
939 }
940
941 /* Skip over the possibly quoted word STR (as defined by the quote
942 characters and word break characters used by the completer).
943 Returns pointer to the location after the "word". */
944
945 char *
946 skip_quoted (char *str)
947 {
948 return skip_quoted_chars (str, NULL, NULL);
949 }