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