1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "expression.h"
23 #include "filenames.h" /* For DOSish file names. */
25 #include "gdb_signals.h"
27 #include "reggroups.h"
28 #include "user-regs.h"
30 #include "cli/cli-decode.h"
32 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
33 calling a hook instead so we eliminate the CLI dependency. */
36 /* Needed for rl_completer_word_break_characters() and for
37 rl_filename_completion_function. */
38 #include "readline/readline.h"
40 /* readline defines this. */
43 #include "completer.h"
45 /* Prototypes for local functions. */
47 char *line_completion_function (const char *text
, int matches
,
51 /* readline uses the word breaks for two things:
52 (1) In figuring out where to point the TEXT parameter to the
53 rl_completion_entry_function. Since we don't use TEXT for much,
54 it doesn't matter a lot what the word breaks are for this purpose,
55 but it does affect how much stuff M-? lists.
56 (2) If one of the matches contains a word break character, readline
57 will quote it. That's why we switch between
58 current_language->la_word_break_characters() and
59 gdb_completer_command_word_break_characters. I'm not sure when
60 we need this behavior (perhaps for funky characters in C++
63 /* Variables which are necessary for fancy command line editing. */
65 /* When completing on command names, we remove '-' from the list of
66 word break characters, since we use it in command names. If the
67 readline library sees one in any of the current completion strings,
68 it thinks that the string needs to be quoted and automatically
69 supplies a leading quote. */
70 static char *gdb_completer_command_word_break_characters
=
71 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
73 /* When completing on file names, we remove from the list of word
74 break characters any characters that are commonly used in file
75 names, such as '-', '+', '~', etc. Otherwise, readline displays
76 incorrect completion candidates. */
77 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
78 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
79 programs support @foo style response files. */
80 static char *gdb_completer_file_name_break_characters
= " \t\n*|\"';?><@";
82 static char *gdb_completer_file_name_break_characters
= " \t\n*|\"';:?><";
85 /* Characters that can be used to quote completion strings. Note that
86 we can't include '"' because the gdb C parser treats such quoted
87 sequences as strings. */
88 static char *gdb_completer_quote_characters
= "'";
90 /* Accessor for some completer data that may interest other files. */
93 get_gdb_completer_quote_characters (void)
95 return gdb_completer_quote_characters
;
98 /* Line completion interface function for readline. */
101 readline_line_completion_function (const char *text
, int matches
)
103 return line_completion_function (text
, matches
,
104 rl_line_buffer
, rl_point
);
107 /* This can be used for functions which don't want to complete on
108 symbols but don't want to complete on anything else either. */
110 noop_completer (struct cmd_list_element
*ignore
,
111 const char *text
, const char *prefix
)
116 /* Complete on filenames. */
118 filename_completer (struct cmd_list_element
*ignore
,
119 const char *text
, const char *word
)
122 VEC (char_ptr
) *return_val
= NULL
;
129 p
= rl_filename_completion_function (text
, subsequent_name
);
132 /* We need to set subsequent_name to a non-zero value before the
133 continue line below, because otherwise, if the first file
134 seen by GDB is a backup file whose name ends in a `~', we
135 will loop indefinitely. */
137 /* Like emacs, don't complete on old versions. Especially
138 useful in the "source" command. */
139 if (p
[strlen (p
) - 1] == '~')
146 /* Return exactly p. */
148 else if (word
> text
)
150 /* Return some portion of p. */
151 q
= xmalloc (strlen (p
) + 5);
152 strcpy (q
, p
+ (word
- text
));
157 /* Return some of TEXT plus p. */
158 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
159 strncpy (q
, word
, text
- word
);
160 q
[text
- word
] = '\0';
164 VEC_safe_push (char_ptr
, return_val
, q
);
167 /* There is no way to do this just long enough to affect quote
168 inserting without also affecting the next completion. This
169 should be fixed in readline. FIXME. */
170 /* Ensure that readline does the right thing
171 with respect to inserting quotes. */
172 rl_completer_word_break_characters
= "";
177 /* Complete on locations, which might be of two possible forms:
183 This is intended to be used in commands that set breakpoints
187 location_completer (struct cmd_list_element
*ignore
,
188 const char *text
, const char *word
)
190 int n_syms
, n_files
, ix
;
191 VEC (char_ptr
) *fn_list
= NULL
;
192 VEC (char_ptr
) *list
= NULL
;
195 int quoted
= *text
== '\'' || *text
== '"';
196 int quote_char
= '\0';
197 const char *colon
= NULL
;
198 char *file_to_match
= NULL
;
199 const char *symbol_start
= text
;
200 const char *orig_text
= text
;
203 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
204 for (p
= text
; *p
!= '\0'; ++p
)
206 if (*p
== '\\' && p
[1] == '\'')
208 else if (*p
== '\'' || *p
== '"')
212 while (*p
!= '\0' && *p
!= quote_found
)
214 if (*p
== '\\' && p
[1] == quote_found
)
219 if (*p
== quote_found
)
222 break; /* Hit the end of text. */
224 #if HAVE_DOS_BASED_FILE_SYSTEM
225 /* If we have a DOS-style absolute file name at the beginning of
226 TEXT, and the colon after the drive letter is the only colon
227 we found, pretend the colon is not there. */
228 else if (p
< text
+ 3 && *p
== ':' && p
== text
+ 1 + quoted
)
231 else if (*p
== ':' && !colon
)
234 symbol_start
= p
+ 1;
236 else if (strchr (current_language
->la_word_break_characters(), *p
))
237 symbol_start
= p
+ 1;
242 text_len
= strlen (text
);
244 /* Where is the file name? */
249 file_to_match
= (char *) xmalloc (colon
- text
+ 1);
250 strncpy (file_to_match
, text
, colon
- text
+ 1);
251 /* Remove trailing colons and quotes from the file name. */
252 for (s
= file_to_match
+ (colon
- text
);
255 if (*s
== ':' || *s
== quote_char
)
258 /* If the text includes a colon, they want completion only on a
259 symbol name after the colon. Otherwise, we need to complete on
260 symbols as well as on files. */
263 list
= make_file_symbol_completion_list (symbol_start
, word
,
265 xfree (file_to_match
);
269 list
= make_symbol_completion_list (symbol_start
, word
);
270 /* If text includes characters which cannot appear in a file
271 name, they cannot be asking for completion on files. */
273 gdb_completer_file_name_break_characters
) == text_len
)
274 fn_list
= make_source_files_completion_list (text
, text
);
277 n_syms
= VEC_length (char_ptr
, list
);
278 n_files
= VEC_length (char_ptr
, fn_list
);
280 /* Catenate fn_list[] onto the end of list[]. */
283 VEC_free (char_ptr
, list
); /* Paranoia. */
291 for (ix
= 0; VEC_iterate (char_ptr
, fn_list
, ix
, fn
); ++ix
)
292 VEC_safe_push (char_ptr
, list
, fn
);
293 VEC_free (char_ptr
, fn_list
);
296 if (n_syms
&& n_files
)
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
319 for (ix
= 0; VEC_iterate (char_ptr
, list
, ix
, fn
); ++ix
)
321 memmove (fn
, fn
+ (word
- text
),
322 strlen (fn
) + 1 - (word
- text
));
327 /* No completions at all. As the final resort, try completing
328 on the entire text as a symbol. */
329 list
= make_symbol_completion_list (orig_text
, word
);
335 /* Helper for expression_completer which recursively adds field and
336 method names from TYPE, a struct or union type, to the array
339 add_struct_fields (struct type
*type
, VEC (char_ptr
) **output
,
340 char *fieldname
, int namelen
)
343 int computed_type_name
= 0;
344 const char *type_name
= NULL
;
346 CHECK_TYPEDEF (type
);
347 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
349 if (i
< TYPE_N_BASECLASSES (type
))
350 add_struct_fields (TYPE_BASECLASS (type
, i
),
351 output
, fieldname
, namelen
);
352 else if (TYPE_FIELD_NAME (type
, i
))
354 if (TYPE_FIELD_NAME (type
, i
)[0] != '\0')
356 if (! strncmp (TYPE_FIELD_NAME (type
, i
),
358 VEC_safe_push (char_ptr
, *output
,
359 xstrdup (TYPE_FIELD_NAME (type
, i
)));
361 else if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_UNION
)
363 /* Recurse into anonymous unions. */
364 add_struct_fields (TYPE_FIELD_TYPE (type
, i
),
365 output
, fieldname
, namelen
);
370 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
372 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
374 if (name
&& ! strncmp (name
, fieldname
, namelen
))
376 if (!computed_type_name
)
378 type_name
= type_name_no_tag (type
);
379 computed_type_name
= 1;
381 /* Omit constructors from the completion list. */
382 if (!type_name
|| strcmp (type_name
, name
))
383 VEC_safe_push (char_ptr
, *output
, xstrdup (name
));
388 /* Complete on expressions. Often this means completing on symbol
389 names, but some language parsers also have support for completing
392 expression_completer (struct cmd_list_element
*ignore
,
393 const char *text
, const char *word
)
395 struct type
*type
= NULL
;
398 volatile struct gdb_exception except
;
399 enum type_code code
= TYPE_CODE_UNDEF
;
401 /* Perform a tentative parse of the expression, to see whether a
402 field completion is required. */
404 TRY_CATCH (except
, RETURN_MASK_ERROR
)
406 type
= parse_expression_for_completion (text
, &fieldname
, &code
);
408 if (except
.reason
< 0)
410 if (fieldname
&& type
)
414 CHECK_TYPEDEF (type
);
415 if (TYPE_CODE (type
) != TYPE_CODE_PTR
416 && TYPE_CODE (type
) != TYPE_CODE_REF
)
418 type
= TYPE_TARGET_TYPE (type
);
421 if (TYPE_CODE (type
) == TYPE_CODE_UNION
422 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
424 int flen
= strlen (fieldname
);
425 VEC (char_ptr
) *result
= NULL
;
427 add_struct_fields (type
, &result
, fieldname
, flen
);
432 else if (fieldname
&& code
!= TYPE_CODE_UNDEF
)
434 VEC (char_ptr
) *result
;
435 struct cleanup
*cleanup
= make_cleanup (xfree
, fieldname
);
437 result
= make_symbol_completion_type (fieldname
, fieldname
, code
);
438 do_cleanups (cleanup
);
443 /* Commands which complete on locations want to see the entire
446 p
> text
&& p
[-1] != ' ' && p
[-1] != '\t';
450 /* Not ideal but it is what we used to do before... */
451 return location_completer (ignore
, p
, word
);
454 /* See definition in completer.h. */
457 set_gdb_completion_word_break_characters (completer_ftype
*fn
)
459 /* So far we are only interested in differentiating filename
460 completers from everything else. */
461 if (fn
== filename_completer
)
462 rl_completer_word_break_characters
463 = gdb_completer_file_name_break_characters
;
465 rl_completer_word_break_characters
466 = gdb_completer_command_word_break_characters
;
469 /* Here are some useful test cases for completion. FIXME: These
470 should be put in the test suite. They should be tested with both
473 "show output-" "radix"
474 "show output" "-radix"
475 "p" ambiguous (commands starting with p--path, print, printf, etc.)
476 "p " ambiguous (all symbols)
477 "info t foo" no completions
478 "info t " no completions
479 "info t" ambiguous ("info target", "info terminal", etc.)
480 "info ajksdlfk" no completions
481 "info ajksdlfk " no completions
483 "info " ambiguous (all info commands)
484 "p \"a" no completions (string constant)
485 "p 'a" ambiguous (all symbols starting with a)
486 "p b-a" ambiguous (all symbols starting with a)
487 "p b-" ambiguous (all symbols)
488 "file Make" "file" (word break hard to screw up here)
489 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
498 complete_line_internal_reason
;
501 /* Internal function used to handle completions.
504 TEXT is the caller's idea of the "word" we are looking at.
506 LINE_BUFFER is available to be looked at; it contains the entire
507 text of the line. POINT is the offset in that line of the cursor.
508 You should pretend that the line ends at POINT.
510 REASON is of type complete_line_internal_reason.
512 If REASON is handle_brkchars:
513 Preliminary phase, called by gdb_completion_word_break_characters
514 function, is used to determine the correct set of chars that are
515 word delimiters depending on the current command in line_buffer.
516 No completion list should be generated; the return value should be
517 NULL. This is checked by an assertion in that function.
519 If REASON is handle_completions:
520 Main phase, called by complete_line function, is used to get the list
521 of posible completions.
523 If REASON is handle_help:
524 Special case when completing a 'help' command. In this case,
525 once sub-command completions are exhausted, we simply return NULL.
528 static VEC (char_ptr
) *
529 complete_line_internal (const char *text
,
530 const char *line_buffer
, int point
,
531 complete_line_internal_reason reason
)
533 VEC (char_ptr
) *list
= NULL
;
536 int ignore_help_classes
;
537 /* Pointer within tmp_command which corresponds to text. */
539 struct cmd_list_element
*c
, *result_list
;
541 /* Choose the default set of word break characters to break
542 completions. If we later find out that we are doing completions
543 on command strings (as opposed to strings supplied by the
544 individual command completer functions, which can be any string)
545 then we will switch to the special word break set for command
546 strings, which leaves out the '-' character used in some
548 rl_completer_word_break_characters
=
549 current_language
->la_word_break_characters();
551 /* Decide whether to complete on a list of gdb commands or on
553 tmp_command
= (char *) alloca (point
+ 1);
556 /* The help command should complete help aliases. */
557 ignore_help_classes
= reason
!= handle_help
;
559 strncpy (tmp_command
, line_buffer
, point
);
560 tmp_command
[point
] = '\0';
561 /* Since text always contains some number of characters leading up
562 to point, we can find the equivalent position in tmp_command
563 by subtracting that many characters from the end of tmp_command. */
564 word
= tmp_command
+ point
- strlen (text
);
568 /* An empty line we want to consider ambiguous; that is, it
569 could be any command. */
570 c
= CMD_LIST_AMBIGUOUS
;
575 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, ignore_help_classes
);
578 /* Move p up to the next interesting thing. */
579 while (*p
== ' ' || *p
== '\t')
586 /* It is an unrecognized command. So there are no
587 possible completions. */
590 else if (c
== CMD_LIST_AMBIGUOUS
)
594 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
595 doesn't advance over that thing itself. Do so now. */
597 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
599 if (q
!= tmp_command
+ point
)
601 /* There is something beyond the ambiguous
602 command, so there are no possible completions. For
603 example, "info t " or "info t foo" does not complete
604 to anything, because "info t" can be "info target" or
610 /* We're trying to complete on the command which was ambiguous.
611 This we can deal with. */
614 if (reason
!= handle_brkchars
)
615 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
616 word
, ignore_help_classes
);
620 if (reason
!= handle_brkchars
)
621 list
= complete_on_cmdlist (cmdlist
, p
, word
,
622 ignore_help_classes
);
624 /* Ensure that readline does the right thing with respect to
626 rl_completer_word_break_characters
=
627 gdb_completer_command_word_break_characters
;
632 /* We've recognized a full command. */
634 if (p
== tmp_command
+ point
)
636 /* There is no non-whitespace in the line beyond the
639 if (p
[-1] == ' ' || p
[-1] == '\t')
641 /* The command is followed by whitespace; we need to
642 complete on whatever comes after command. */
645 /* It is a prefix command; what comes after it is
646 a subcommand (e.g. "info "). */
647 if (reason
!= handle_brkchars
)
648 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
,
649 ignore_help_classes
);
651 /* Ensure that readline does the right thing
652 with respect to inserting quotes. */
653 rl_completer_word_break_characters
=
654 gdb_completer_command_word_break_characters
;
656 else if (reason
== handle_help
)
660 if (reason
!= handle_brkchars
)
661 list
= complete_on_enum (c
->enums
, p
, word
);
662 rl_completer_word_break_characters
=
663 gdb_completer_command_word_break_characters
;
667 /* It is a normal command; what comes after it is
668 completed by the command's completer function. */
669 if (c
->completer
== filename_completer
)
671 /* Many commands which want to complete on
672 file names accept several file names, as
673 in "run foo bar >>baz". So we don't want
674 to complete the entire text after the
675 command, just the last word. To this
676 end, we need to find the beginning of the
677 file name by starting at `word' and going
681 && strchr (gdb_completer_file_name_break_characters
, p
[-1]) == NULL
;
684 rl_completer_word_break_characters
=
685 gdb_completer_file_name_break_characters
;
687 else if (c
->completer
== location_completer
)
689 /* Commands which complete on locations want to
690 see the entire argument. */
693 && p
[-1] != ' ' && p
[-1] != '\t';
697 if (reason
== handle_brkchars
698 && c
->completer_handle_brkchars
!= NULL
)
699 (*c
->completer_handle_brkchars
) (c
, p
, word
);
700 if (reason
!= handle_brkchars
&& c
->completer
!= NULL
)
701 list
= (*c
->completer
) (c
, p
, word
);
706 /* The command is not followed by whitespace; we need to
707 complete on the command itself, e.g. "p" which is a
708 command itself but also can complete to "print", "ptype"
712 /* Find the command we are completing on. */
714 while (q
> tmp_command
)
716 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
722 if (reason
!= handle_brkchars
)
723 list
= complete_on_cmdlist (result_list
, q
, word
,
724 ignore_help_classes
);
726 /* Ensure that readline does the right thing
727 with respect to inserting quotes. */
728 rl_completer_word_break_characters
=
729 gdb_completer_command_word_break_characters
;
732 else if (reason
== handle_help
)
736 /* There is non-whitespace beyond the command. */
738 if (c
->prefixlist
&& !c
->allow_unknown
)
740 /* It is an unrecognized subcommand of a prefix command,
741 e.g. "info adsfkdj". */
746 if (reason
!= handle_brkchars
)
747 list
= complete_on_enum (c
->enums
, p
, word
);
751 /* It is a normal command. */
752 if (c
->completer
== filename_completer
)
754 /* See the commentary above about the specifics
755 of file-name completion. */
758 && strchr (gdb_completer_file_name_break_characters
,
762 rl_completer_word_break_characters
=
763 gdb_completer_file_name_break_characters
;
765 else if (c
->completer
== location_completer
)
769 && p
[-1] != ' ' && p
[-1] != '\t';
773 if (reason
== handle_brkchars
774 && c
->completer_handle_brkchars
!= NULL
)
775 (*c
->completer_handle_brkchars
) (c
, p
, word
);
776 if (reason
!= handle_brkchars
&& c
->completer
!= NULL
)
777 list
= (*c
->completer
) (c
, p
, word
);
785 /* See completer.h. */
787 int max_completions
= 200;
789 /* See completer.h. */
792 new_completion_tracker (void)
794 if (max_completions
<= 0)
797 return htab_create_alloc (max_completions
,
798 htab_hash_string
, (htab_eq
) streq
,
799 NULL
, xcalloc
, xfree
);
802 /* Cleanup routine to free a completion tracker and reset the pointer
806 free_completion_tracker (void *p
)
808 completion_tracker_t
*tracker_ptr
= p
;
810 htab_delete (*tracker_ptr
);
814 /* See completer.h. */
817 make_cleanup_free_completion_tracker (completion_tracker_t
*tracker_ptr
)
819 if (*tracker_ptr
== NULL
)
820 return make_cleanup (null_cleanup
, NULL
);
822 return make_cleanup (free_completion_tracker
, tracker_ptr
);
825 /* See completer.h. */
827 enum maybe_add_completion_enum
828 maybe_add_completion (completion_tracker_t tracker
, char *name
)
832 if (max_completions
< 0)
833 return MAYBE_ADD_COMPLETION_OK
;
834 if (max_completions
== 0)
835 return MAYBE_ADD_COMPLETION_MAX_REACHED
;
837 gdb_assert (tracker
!= NULL
);
839 if (htab_elements (tracker
) >= max_completions
)
840 return MAYBE_ADD_COMPLETION_MAX_REACHED
;
842 slot
= htab_find_slot (tracker
, name
, INSERT
);
844 if (*slot
!= HTAB_EMPTY_ENTRY
)
845 return MAYBE_ADD_COMPLETION_DUPLICATE
;
849 return (htab_elements (tracker
) < max_completions
850 ? MAYBE_ADD_COMPLETION_OK
851 : MAYBE_ADD_COMPLETION_OK_MAX_REACHED
);
855 throw_max_completions_reached_error (void)
857 throw_error (MAX_COMPLETIONS_REACHED_ERROR
, _("Max completions reached."));
860 /* Generate completions all at once. Returns a vector of unique strings
861 allocated with xmalloc. Returns NULL if there are no completions
862 or if max_completions is 0. If max_completions is non-negative, this will
863 return at most max_completions strings.
865 TEXT is the caller's idea of the "word" we are looking at.
867 LINE_BUFFER is available to be looked at; it contains the entire
870 POINT is the offset in that line of the cursor. You
871 should pretend that the line ends at POINT. */
874 complete_line (const char *text
, const char *line_buffer
, int point
)
876 VEC (char_ptr
) *list
;
877 VEC (char_ptr
) *result
= NULL
;
878 struct cleanup
*cleanups
;
879 completion_tracker_t tracker
;
883 if (max_completions
== 0)
885 list
= complete_line_internal (text
, line_buffer
, point
,
887 if (max_completions
< 0)
890 tracker
= new_completion_tracker ();
891 cleanups
= make_cleanup_free_completion_tracker (&tracker
);
892 make_cleanup_free_char_ptr_vec (list
);
894 /* Do a final test for too many completions. Individual completers may
895 do some of this, but are not required to. Duplicates are also removed
896 here. Otherwise the user is left scratching his/her head: readline and
897 complete_command will remove duplicates, and if removal of duplicates
898 there brings the total under max_completions the user may think gdb quit
899 searching too early. */
901 for (ix
= 0, max_reached
= 0;
902 !max_reached
&& VEC_iterate (char_ptr
, list
, ix
, candidate
);
905 enum maybe_add_completion_enum add_status
;
907 add_status
= maybe_add_completion (tracker
, candidate
);
911 case MAYBE_ADD_COMPLETION_OK
:
912 VEC_safe_push (char_ptr
, result
, xstrdup (candidate
));
914 case MAYBE_ADD_COMPLETION_OK_MAX_REACHED
:
915 VEC_safe_push (char_ptr
, result
, xstrdup (candidate
));
918 case MAYBE_ADD_COMPLETION_MAX_REACHED
:
919 gdb_assert_not_reached ("more than max completions reached");
920 case MAYBE_ADD_COMPLETION_DUPLICATE
:
925 do_cleanups (cleanups
);
930 /* Complete on command names. Used by "help". */
932 command_completer (struct cmd_list_element
*ignore
,
933 const char *text
, const char *word
)
935 return complete_line_internal (word
, text
,
936 strlen (text
), handle_help
);
939 /* Complete on signals. */
942 signal_completer (struct cmd_list_element
*ignore
,
943 const char *text
, const char *word
)
945 VEC (char_ptr
) *return_val
= NULL
;
946 size_t len
= strlen (word
);
947 enum gdb_signal signum
;
950 for (signum
= GDB_SIGNAL_FIRST
; signum
!= GDB_SIGNAL_LAST
; ++signum
)
952 /* Can't handle this, so skip it. */
953 if (signum
== GDB_SIGNAL_0
)
956 signame
= gdb_signal_to_name (signum
);
958 /* Ignore the unknown signal case. */
959 if (!signame
|| strcmp (signame
, "?") == 0)
962 if (strncasecmp (signame
, word
, len
) == 0)
963 VEC_safe_push (char_ptr
, return_val
, xstrdup (signame
));
969 /* Complete on a register or reggroup. */
972 reg_or_group_completer (struct cmd_list_element
*ignore
,
973 const char *text
, const char *word
)
975 VEC (char_ptr
) *result
= NULL
;
976 size_t len
= strlen (word
);
977 struct gdbarch
*gdbarch
;
978 struct reggroup
*group
;
982 if (!target_has_registers
)
985 gdbarch
= get_frame_arch (get_selected_frame (NULL
));
988 (name
= user_reg_map_regnum_to_name (gdbarch
, i
)) != NULL
;
991 if (*name
!= '\0' && strncmp (word
, name
, len
) == 0)
992 VEC_safe_push (char_ptr
, result
, xstrdup (name
));
995 for (group
= reggroup_next (gdbarch
, NULL
);
997 group
= reggroup_next (gdbarch
, group
))
999 name
= reggroup_name (group
);
1000 if (strncmp (word
, name
, len
) == 0)
1001 VEC_safe_push (char_ptr
, result
, xstrdup (name
));
1008 /* Get the list of chars that are considered as word breaks
1009 for the current command. */
1012 gdb_completion_word_break_characters (void)
1014 VEC (char_ptr
) *list
;
1016 list
= complete_line_internal (rl_line_buffer
, rl_line_buffer
, rl_point
,
1018 gdb_assert (list
== NULL
);
1019 return rl_completer_word_break_characters
;
1022 /* Generate completions one by one for the completer. Each time we
1023 are called return another potential completion to the caller.
1024 line_completion just completes on commands or passes the buck to
1025 the command's completer function, the stuff specific to symbol
1026 completion is in make_symbol_completion_list.
1028 TEXT is the caller's idea of the "word" we are looking at.
1030 MATCHES is the number of matches that have currently been collected
1031 from calling this completion function. When zero, then we need to
1032 initialize, otherwise the initialization has already taken place
1033 and we can just return the next potential completion string.
1035 LINE_BUFFER is available to be looked at; it contains the entire
1036 text of the line. POINT is the offset in that line of the cursor.
1037 You should pretend that the line ends at POINT.
1039 Returns NULL if there are no more completions, else a pointer to a
1040 string which is a possible completion, it is the caller's
1041 responsibility to free the string. */
1044 line_completion_function (const char *text
, int matches
,
1045 char *line_buffer
, int point
)
1047 static VEC (char_ptr
) *list
= NULL
; /* Cache of completions. */
1048 static int index
; /* Next cached completion. */
1049 char *output
= NULL
;
1053 /* The caller is beginning to accumulate a new set of
1054 completions, so we need to find all of them now, and cache
1055 them for returning one at a time on future calls. */
1059 /* Free the storage used by LIST, but not by the strings
1060 inside. This is because rl_complete_internal () frees
1061 the strings. As complete_line may abort by calling
1062 `error' clear LIST now. */
1063 VEC_free (char_ptr
, list
);
1066 list
= complete_line (text
, line_buffer
, point
);
1069 /* If we found a list of potential completions during initialization
1070 then dole them out one at a time. After returning the last one,
1071 return NULL (and continue to do so) each time we are called after
1072 that, until a new list is available. */
1076 if (index
< VEC_length (char_ptr
, list
))
1078 output
= VEC_index (char_ptr
, list
, index
);
1084 /* Can't do this because readline hasn't yet checked the word breaks
1085 for figuring out whether to insert a quote. */
1087 /* Make sure the word break characters are set back to normal for
1088 the next time that readline tries to complete something. */
1089 rl_completer_word_break_characters
=
1090 current_language
->la_word_break_characters();
1096 /* Skip over the possibly quoted word STR (as defined by the quote
1097 characters QUOTECHARS and the word break characters BREAKCHARS).
1098 Returns pointer to the location after the "word". If either
1099 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
1103 skip_quoted_chars (const char *str
, const char *quotechars
,
1104 const char *breakchars
)
1106 char quote_char
= '\0';
1109 if (quotechars
== NULL
)
1110 quotechars
= gdb_completer_quote_characters
;
1112 if (breakchars
== NULL
)
1113 breakchars
= current_language
->la_word_break_characters();
1115 for (scan
= str
; *scan
!= '\0'; scan
++)
1117 if (quote_char
!= '\0')
1119 /* Ignore everything until the matching close quote char. */
1120 if (*scan
== quote_char
)
1122 /* Found matching close quote. */
1127 else if (strchr (quotechars
, *scan
))
1129 /* Found start of a quoted string. */
1132 else if (strchr (breakchars
, *scan
))
1141 /* Skip over the possibly quoted word STR (as defined by the quote
1142 characters and word break characters used by the completer).
1143 Returns pointer to the location after the "word". */
1146 skip_quoted (const char *str
)
1148 return skip_quoted_chars (str
, NULL
, NULL
);
1151 /* Return a message indicating that the maximum number of completions
1152 has been reached and that there may be more. */
1155 get_max_completions_reached_message (void)
1157 return _("*** List may be truncated, max-completions reached. ***");
1160 /* GDB replacement for rl_display_match_list.
1161 Readline doesn't provide a clean interface for TUI(curses).
1162 A hack previously used was to send readline's rl_outstream through a pipe
1163 and read it from the event loop. Bleah. IWBN if readline abstracted
1164 away all the necessary bits, and this is what this code does. It
1165 replicates the parts of readline we need and then adds an abstraction
1166 layer, currently implemented as struct match_list_displayer, so that both
1167 CLI and TUI can use it. We copy all this readline code to minimize
1168 GDB-specific mods to readline. Once this code performs as desired then
1169 we can submit it to the readline maintainers.
1171 N.B. A lot of the code is the way it is in order to minimize differences
1172 from readline's copy. */
1174 /* Not supported here. */
1175 #undef VISIBLE_STATS
1177 #if defined (HANDLE_MULTIBYTE)
1178 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
1179 #define MB_NULLWCH(x) ((x) == 0)
1182 #define ELLIPSIS_LEN 3
1184 /* gdb version of readline/complete.c:get_y_or_n.
1185 'y' -> returns 1, and 'n' -> returns 0.
1186 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
1187 If FOR_PAGER is non-zero, then also supported are:
1188 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
1191 gdb_get_y_or_n (int for_pager
, const struct match_list_displayer
*displayer
)
1197 RL_SETSTATE (RL_STATE_MOREINPUT
);
1198 c
= displayer
->read_key (displayer
);
1199 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
1201 if (c
== 'y' || c
== 'Y' || c
== ' ')
1203 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
1205 if (c
== ABORT_CHAR
|| c
< 0)
1207 /* Readline doesn't erase_entire_line here, but without it the
1208 --More-- prompt isn't erased and neither is the text entered
1209 thus far redisplayed. */
1210 displayer
->erase_entire_line (displayer
);
1211 /* Note: The arguments to rl_abort are ignored. */
1214 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
1216 if (for_pager
&& (c
== 'q' || c
== 'Q'))
1218 displayer
->beep (displayer
);
1222 /* Pager function for tab-completion.
1223 This is based on readline/complete.c:_rl_internal_pager.
1224 LINES is the number of lines of output displayed thus far.
1226 -1 -> user pressed 'n' or equivalent,
1227 0 -> user pressed 'y' or equivalent,
1228 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
1231 gdb_display_match_list_pager (int lines
,
1232 const struct match_list_displayer
*displayer
)
1236 displayer
->puts (displayer
, "--More--");
1237 displayer
->flush (displayer
);
1238 i
= gdb_get_y_or_n (1, displayer
);
1239 displayer
->erase_entire_line (displayer
);
1248 /* Return non-zero if FILENAME is a directory.
1249 Based on readline/complete.c:path_isdir. */
1252 gdb_path_isdir (const char *filename
)
1256 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
1259 /* Return the portion of PATHNAME that should be output when listing
1260 possible completions. If we are hacking filename completion, we
1261 are only interested in the basename, the portion following the
1262 final slash. Otherwise, we return what we were passed. Since
1263 printing empty strings is not very informative, if we're doing
1264 filename completion, and the basename is the empty string, we look
1265 for the previous slash and return the portion following that. If
1266 there's no previous slash, we just return what we were passed.
1268 Based on readline/complete.c:printable_part. */
1271 gdb_printable_part (char *pathname
)
1275 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
1278 temp
= strrchr (pathname
, '/');
1279 #if defined (__MSDOS__)
1280 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
1281 temp
= pathname
+ 1;
1284 if (temp
== 0 || *temp
== '\0')
1286 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
1287 Look for a previous slash and, if one is found, return the portion
1288 following that slash. If there's no previous slash, just return the
1289 pathname we were passed. */
1290 else if (temp
[1] == '\0')
1292 for (x
= temp
- 1; x
> pathname
; x
--)
1295 return ((*x
== '/') ? x
+ 1 : pathname
);
1301 /* Compute width of STRING when displayed on screen by print_filename.
1302 Based on readline/complete.c:fnwidth. */
1305 gdb_fnwidth (const char *string
)
1308 #if defined (HANDLE_MULTIBYTE)
1314 left
= strlen (string
) + 1;
1315 memset (&ps
, 0, sizeof (mbstate_t));
1321 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
1328 #if defined (HANDLE_MULTIBYTE)
1329 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
1330 if (MB_INVALIDCH (clen
))
1334 memset (&ps
, 0, sizeof (mbstate_t));
1336 else if (MB_NULLWCH (clen
))
1342 width
+= (w
>= 0) ? w
: 1;
1354 /* Print TO_PRINT, one matching completion.
1355 PREFIX_BYTES is number of common prefix bytes.
1356 Based on readline/complete.c:fnprint. */
1359 gdb_fnprint (const char *to_print
, int prefix_bytes
,
1360 const struct match_list_displayer
*displayer
)
1364 #if defined (HANDLE_MULTIBYTE)
1371 end
= to_print
+ strlen (to_print
) + 1;
1372 memset (&ps
, 0, sizeof (mbstate_t));
1377 /* Don't print only the ellipsis if the common prefix is one of the
1378 possible completions */
1379 if (to_print
[prefix_bytes
] == '\0')
1386 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
1387 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
1388 displayer
->putch (displayer
, ellipsis
);
1389 printed_len
= ELLIPSIS_LEN
;
1392 s
= to_print
+ prefix_bytes
;
1397 displayer
->putch (displayer
, '^');
1398 displayer
->putch (displayer
, UNCTRL (*s
));
1401 #if defined (HANDLE_MULTIBYTE)
1402 memset (&ps
, 0, sizeof (mbstate_t));
1405 else if (*s
== RUBOUT
)
1407 displayer
->putch (displayer
, '^');
1408 displayer
->putch (displayer
, '?');
1411 #if defined (HANDLE_MULTIBYTE)
1412 memset (&ps
, 0, sizeof (mbstate_t));
1417 #if defined (HANDLE_MULTIBYTE)
1418 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
1419 if (MB_INVALIDCH (tlen
))
1423 memset (&ps
, 0, sizeof (mbstate_t));
1425 else if (MB_NULLWCH (tlen
))
1430 width
= (w
>= 0) ? w
: 1;
1432 for (w
= 0; w
< tlen
; ++w
)
1433 displayer
->putch (displayer
, s
[w
]);
1435 printed_len
+= width
;
1437 displayer
->putch (displayer
, *s
);
1447 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
1448 are using it, check for and output a single character for `special'
1449 filenames. Return the number of characters we output.
1450 Based on readline/complete.c:print_filename. */
1453 gdb_print_filename (char *to_print
, char *full_pathname
, int prefix_bytes
,
1454 const struct match_list_displayer
*displayer
)
1456 int printed_len
, extension_char
, slen
, tlen
;
1457 char *s
, c
, *new_full_pathname
, *dn
;
1458 extern int _rl_complete_mark_directories
;
1461 printed_len
= gdb_fnprint (to_print
, prefix_bytes
, displayer
);
1463 #if defined (VISIBLE_STATS)
1464 if (rl_filename_completion_desired
&& (rl_visible_stats
|| _rl_complete_mark_directories
))
1466 if (rl_filename_completion_desired
&& _rl_complete_mark_directories
)
1469 /* If to_print != full_pathname, to_print is the basename of the
1470 path passed. In this case, we try to expand the directory
1471 name before checking for the stat character. */
1472 if (to_print
!= full_pathname
)
1474 /* Terminate the directory name. */
1476 to_print
[-1] = '\0';
1478 /* If setting the last slash in full_pathname to a NUL results in
1479 full_pathname being the empty string, we are trying to complete
1480 files in the root directory. If we pass a null string to the
1481 bash directory completion hook, for example, it will expand it
1482 to the current directory. We just want the `/'. */
1483 if (full_pathname
== 0 || *full_pathname
== 0)
1485 else if (full_pathname
[0] != '/')
1487 else if (full_pathname
[1] == 0)
1488 dn
= "//"; /* restore trailing slash to `//' */
1489 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
1490 dn
= "/"; /* don't turn /// into // */
1493 s
= tilde_expand (dn
);
1494 if (rl_directory_completion_hook
)
1495 (*rl_directory_completion_hook
) (&s
);
1498 tlen
= strlen (to_print
);
1499 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
1500 strcpy (new_full_pathname
, s
);
1501 if (s
[slen
- 1] == '/')
1504 new_full_pathname
[slen
] = '/';
1505 new_full_pathname
[slen
] = '/';
1506 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
1508 #if defined (VISIBLE_STATS)
1509 if (rl_visible_stats
)
1510 extension_char
= stat_char (new_full_pathname
);
1513 if (gdb_path_isdir (new_full_pathname
))
1514 extension_char
= '/';
1516 xfree (new_full_pathname
);
1521 s
= tilde_expand (full_pathname
);
1522 #if defined (VISIBLE_STATS)
1523 if (rl_visible_stats
)
1524 extension_char
= stat_char (s
);
1527 if (gdb_path_isdir (s
))
1528 extension_char
= '/';
1534 displayer
->putch (displayer
, extension_char
);
1542 /* GDB version of readline/complete.c:complete_get_screenwidth. */
1545 gdb_complete_get_screenwidth (const struct match_list_displayer
*displayer
)
1547 /* Readline has other stuff here which it's not clear we need. */
1548 return displayer
->width
;
1551 extern int _rl_completion_prefix_display_length
;
1552 extern int _rl_print_completions_horizontally
;
1554 EXTERN_C
int _rl_qsort_string_compare (const void *, const void *);
1555 typedef int QSFUNC (const void *, const void *);
1557 /* GDB version of readline/complete.c:rl_display_match_list.
1558 See gdb_display_match_list for a description of MATCHES, LEN, MAX.
1559 Returns non-zero if all matches are displayed. */
1562 gdb_display_match_list_1 (char **matches
, int len
, int max
,
1563 const struct match_list_displayer
*displayer
)
1565 int count
, limit
, printed_len
, lines
, cols
;
1566 int i
, j
, k
, l
, common_length
, sind
;
1568 int page_completions
= displayer
->height
!= INT_MAX
&& pagination_enabled
;
1570 /* Find the length of the prefix common to all items: length as displayed
1571 characters (common_length) and as a byte index into the matches (sind) */
1572 common_length
= sind
= 0;
1573 if (_rl_completion_prefix_display_length
> 0)
1575 t
= gdb_printable_part (matches
[0]);
1576 temp
= strrchr (t
, '/');
1577 common_length
= temp
? gdb_fnwidth (temp
) : gdb_fnwidth (t
);
1578 sind
= temp
? strlen (temp
) : strlen (t
);
1580 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
1581 max
-= common_length
- ELLIPSIS_LEN
;
1583 common_length
= sind
= 0;
1586 /* How many items of MAX length can we fit in the screen window? */
1587 cols
= gdb_complete_get_screenwidth (displayer
);
1590 if (limit
!= 1 && (limit
* max
== cols
))
1593 /* If cols == 0, limit will end up -1 */
1594 if (cols
< displayer
->width
&& limit
< 0)
1597 /* Avoid a possible floating exception. If max > cols,
1598 limit will be 0 and a divide-by-zero fault will result. */
1602 /* How many iterations of the printing loop? */
1603 count
= (len
+ (limit
- 1)) / limit
;
1605 /* Watch out for special case. If LEN is less than LIMIT, then
1606 just do the inner printing loop.
1607 0 < len <= limit implies count = 1. */
1609 /* Sort the items if they are not already sorted. */
1610 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
1611 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1613 displayer
->crlf (displayer
);
1616 if (_rl_print_completions_horizontally
== 0)
1618 /* Print the sorted items, up-and-down alphabetically, like ls. */
1619 for (i
= 1; i
<= count
; i
++)
1621 for (j
= 0, l
= i
; j
< limit
; j
++)
1623 if (l
> len
|| matches
[l
] == 0)
1627 temp
= gdb_printable_part (matches
[l
]);
1628 printed_len
= gdb_print_filename (temp
, matches
[l
], sind
,
1632 for (k
= 0; k
< max
- printed_len
; k
++)
1633 displayer
->putch (displayer
, ' ');
1637 displayer
->crlf (displayer
);
1639 if (page_completions
&& lines
>= (displayer
->height
- 1) && i
< count
)
1641 lines
= gdb_display_match_list_pager (lines
, displayer
);
1649 /* Print the sorted items, across alphabetically, like ls -x. */
1650 for (i
= 1; matches
[i
]; i
++)
1652 temp
= gdb_printable_part (matches
[i
]);
1653 printed_len
= gdb_print_filename (temp
, matches
[i
], sind
, displayer
);
1654 /* Have we reached the end of this line? */
1657 if (i
&& (limit
> 1) && (i
% limit
) == 0)
1659 displayer
->crlf (displayer
);
1661 if (page_completions
&& lines
>= displayer
->height
- 1)
1663 lines
= gdb_display_match_list_pager (lines
, displayer
);
1669 for (k
= 0; k
< max
- printed_len
; k
++)
1670 displayer
->putch (displayer
, ' ');
1673 displayer
->crlf (displayer
);
1679 /* Utility for displaying completion list matches, used by both CLI and TUI.
1681 MATCHES is the list of strings, in argv format, LEN is the number of
1682 strings in MATCHES, and MAX is the length of the longest string in
1686 gdb_display_match_list (char **matches
, int len
, int max
,
1687 const struct match_list_displayer
*displayer
)
1689 /* Readline will never call this if complete_line returned NULL. */
1690 gdb_assert (max_completions
!= 0);
1692 /* complete_line will never return more than this. */
1693 if (max_completions
> 0)
1694 gdb_assert (len
<= max_completions
);
1696 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
1700 /* We can't use *query here because they wait for <RET> which is
1701 wrong here. This follows the readline version as closely as possible
1702 for compatibility's sake. See readline/complete.c. */
1704 displayer
->crlf (displayer
);
1706 xsnprintf (msg
, sizeof (msg
),
1707 "Display all %d possibilities? (y or n)", len
);
1708 displayer
->puts (displayer
, msg
);
1709 displayer
->flush (displayer
);
1711 if (gdb_get_y_or_n (0, displayer
) == 0)
1713 displayer
->crlf (displayer
);
1718 if (gdb_display_match_list_1 (matches
, len
, max
, displayer
))
1720 /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
1721 if (len
== max_completions
)
1723 /* The maximum number of completions has been reached. Warn the user
1724 that there may be more. */
1725 const char *message
= get_max_completions_reached_message ();
1727 displayer
->puts (displayer
, message
);
1728 displayer
->crlf (displayer
);
1733 extern initialize_file_ftype _initialize_completer
; /* -Wmissing-prototypes */
1736 _initialize_completer (void)
1738 add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class
,
1739 &max_completions
, _("\
1740 Set maximum number of completion candidates."), _("\
1741 Show maximum number of completion candidates."), _("\
1742 Use this to limit the number of candidates considered\n\
1743 during completion. Specifying \"unlimited\" or -1\n\
1744 disables limiting. Note that setting either no limit or\n\
1745 a very large limit can make completion slow."),
1746 NULL
, NULL
, &setlist
, &showlist
);