1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000-2019 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 "common/gdb_signals.h"
27 #include "reggroups.h"
28 #include "user-regs.h"
29 #include "arch-utils.h"
33 #include "cli/cli-decode.h"
35 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
36 calling a hook instead so we eliminate the CLI dependency. */
39 /* Needed for rl_completer_word_break_characters() and for
40 rl_filename_completion_function. */
41 #include "readline/readline.h"
43 /* readline defines this. */
46 #include "completer.h"
48 /* Misc state that needs to be tracked across several different
49 readline completer entry point calls, all related to a single
50 completion invocation. */
52 struct gdb_completer_state
54 /* The current completion's completion tracker. This is a global
55 because a tracker can be shared between the handle_brkchars and
56 handle_completion phases, which involves different readline
58 completion_tracker
*tracker
= NULL
;
60 /* Whether the current completion was aborted. */
64 /* The current completion state. */
65 static gdb_completer_state current_completion
;
67 /* An enumeration of the various things a user might attempt to
68 complete for a location. If you change this, remember to update
69 the explicit_options array below too. */
71 enum explicit_location_match_type
73 /* The filename of a source file. */
76 /* The name of a function or method. */
79 /* The fully-qualified name of a function or method. */
85 /* The name of a label. */
89 /* Prototypes for local functions. */
91 /* readline uses the word breaks for two things:
92 (1) In figuring out where to point the TEXT parameter to the
93 rl_completion_entry_function. Since we don't use TEXT for much,
94 it doesn't matter a lot what the word breaks are for this purpose,
95 but it does affect how much stuff M-? lists.
96 (2) If one of the matches contains a word break character, readline
97 will quote it. That's why we switch between
98 current_language->la_word_break_characters() and
99 gdb_completer_command_word_break_characters. I'm not sure when
100 we need this behavior (perhaps for funky characters in C++
103 /* Variables which are necessary for fancy command line editing. */
105 /* When completing on command names, we remove '-' from the list of
106 word break characters, since we use it in command names. If the
107 readline library sees one in any of the current completion strings,
108 it thinks that the string needs to be quoted and automatically
109 supplies a leading quote. */
110 static const char gdb_completer_command_word_break_characters
[] =
111 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
113 /* When completing on file names, we remove from the list of word
114 break characters any characters that are commonly used in file
115 names, such as '-', '+', '~', etc. Otherwise, readline displays
116 incorrect completion candidates. */
117 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
118 programs support @foo style response files. */
119 static const char gdb_completer_file_name_break_characters
[] =
120 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
126 /* Characters that can be used to quote completion strings. Note that
127 we can't include '"' because the gdb C parser treats such quoted
128 sequences as strings. */
129 static const char gdb_completer_quote_characters
[] = "'";
131 /* Accessor for some completer data that may interest other files. */
134 get_gdb_completer_quote_characters (void)
136 return gdb_completer_quote_characters
;
139 /* This can be used for functions which don't want to complete on
140 symbols but don't want to complete on anything else either. */
143 noop_completer (struct cmd_list_element
*ignore
,
144 completion_tracker
&tracker
,
145 const char *text
, const char *prefix
)
149 /* Complete on filenames. */
152 filename_completer (struct cmd_list_element
*ignore
,
153 completion_tracker
&tracker
,
154 const char *text
, const char *word
)
161 gdb::unique_xmalloc_ptr
<char> p_rl
162 (rl_filename_completion_function (text
, subsequent_name
));
165 /* We need to set subsequent_name to a non-zero value before the
166 continue line below, because otherwise, if the first file
167 seen by GDB is a backup file whose name ends in a `~', we
168 will loop indefinitely. */
170 /* Like emacs, don't complete on old versions. Especially
171 useful in the "source" command. */
172 const char *p
= p_rl
.get ();
173 if (p
[strlen (p
) - 1] == '~')
176 tracker
.add_completion
177 (make_completion_match_str (std::move (p_rl
), text
, word
));
180 /* There is no way to do this just long enough to affect quote
181 inserting without also affecting the next completion. This
182 should be fixed in readline. FIXME. */
183 /* Ensure that readline does the right thing
184 with respect to inserting quotes. */
185 rl_completer_word_break_characters
= "";
189 /* The corresponding completer_handle_brkchars
193 filename_completer_handle_brkchars (struct cmd_list_element
*ignore
,
194 completion_tracker
&tracker
,
195 const char *text
, const char *word
)
197 set_rl_completer_word_break_characters
198 (gdb_completer_file_name_break_characters
);
201 /* Possible values for the found_quote flags word used by the completion
202 functions. It says what kind of (shell-like) quoting we found anywhere
204 #define RL_QF_SINGLE_QUOTE 0x01
205 #define RL_QF_DOUBLE_QUOTE 0x02
206 #define RL_QF_BACKSLASH 0x04
207 #define RL_QF_OTHER_QUOTE 0x08
209 /* Find the bounds of the current word for completion purposes, and
210 return a pointer to the end of the word. This mimics (and is a
211 modified version of) readline's _rl_find_completion_word internal
214 This function skips quoted substrings (characters between matched
215 pairs of characters in rl_completer_quote_characters). We try to
216 find an unclosed quoted substring on which to do matching. If one
217 is not found, we use the word break characters to find the
218 boundaries of the current word. QC, if non-null, is set to the
219 opening quote character if we found an unclosed quoted substring,
220 '\0' otherwise. DP, if non-null, is set to the value of the
221 delimiter character that caused a word break. */
223 struct gdb_rl_completion_word_info
225 const char *word_break_characters
;
226 const char *quote_characters
;
227 const char *basic_quote_characters
;
231 gdb_rl_find_completion_word (struct gdb_rl_completion_word_info
*info
,
233 const char *line_buffer
)
235 int scan
, end
, found_quote
, delimiter
, pass_next
, isbrk
;
237 const char *brkchars
;
238 int point
= strlen (line_buffer
);
240 /* The algorithm below does '--point'. Avoid buffer underflow with
252 found_quote
= delimiter
= 0;
255 brkchars
= info
->word_break_characters
;
257 if (info
->quote_characters
!= NULL
)
259 /* We have a list of characters which can be used in pairs to
260 quote substrings for the completer. Try to find the start of
261 an unclosed quoted substring. */
262 /* FOUND_QUOTE is set so we know what kind of quotes we
264 for (scan
= pass_next
= 0;
274 /* Shell-like semantics for single quotes -- don't allow
275 backslash to quote anything in single quotes, especially
276 not the closing quote. If you don't like this, take out
277 the check on the value of quote_char. */
278 if (quote_char
!= '\'' && line_buffer
[scan
] == '\\')
281 found_quote
|= RL_QF_BACKSLASH
;
285 if (quote_char
!= '\0')
287 /* Ignore everything until the matching close quote
289 if (line_buffer
[scan
] == quote_char
)
291 /* Found matching close. Abandon this
297 else if (strchr (info
->quote_characters
, line_buffer
[scan
]))
299 /* Found start of a quoted substring. */
300 quote_char
= line_buffer
[scan
];
302 /* Shell-like quoting conventions. */
303 if (quote_char
== '\'')
304 found_quote
|= RL_QF_SINGLE_QUOTE
;
305 else if (quote_char
== '"')
306 found_quote
|= RL_QF_DOUBLE_QUOTE
;
308 found_quote
|= RL_QF_OTHER_QUOTE
;
313 if (point
== end
&& quote_char
== '\0')
315 /* We didn't find an unclosed quoted substring upon which to do
316 completion, so use the word break characters to find the
317 substring on which to complete. */
320 scan
= line_buffer
[point
];
322 if (strchr (brkchars
, scan
) != 0)
327 /* If we are at an unquoted word break, then advance past it. */
328 scan
= line_buffer
[point
];
332 isbrk
= strchr (brkchars
, scan
) != 0;
336 /* If the character that caused the word break was a quoting
337 character, then remember it as the delimiter. */
338 if (info
->basic_quote_characters
339 && strchr (info
->basic_quote_characters
, scan
)
340 && (end
- point
) > 1)
352 return line_buffer
+ point
;
355 /* See completer.h. */
358 advance_to_expression_complete_word_point (completion_tracker
&tracker
,
361 gdb_rl_completion_word_info info
;
363 info
.word_break_characters
364 = current_language
->la_word_break_characters ();
365 info
.quote_characters
= gdb_completer_quote_characters
;
366 info
.basic_quote_characters
= rl_basic_quote_characters
;
369 = gdb_rl_find_completion_word (&info
, NULL
, NULL
, text
);
371 tracker
.advance_custom_word_point_by (start
- text
);
376 /* See completer.h. */
379 completion_tracker::completes_to_completion_word (const char *word
)
381 if (m_lowest_common_denominator_unique
)
383 const char *lcd
= m_lowest_common_denominator
;
385 if (strncmp_iw (word
, lcd
, strlen (lcd
)) == 0)
387 /* Maybe skip the function and complete on keywords. */
388 size_t wordlen
= strlen (word
);
389 if (word
[wordlen
- 1] == ' ')
397 /* Complete on linespecs, which might be of two possible forms:
403 This is intended to be used in commands that set breakpoints
407 complete_files_symbols (completion_tracker
&tracker
,
408 const char *text
, const char *word
)
410 completion_list fn_list
;
413 int quoted
= *text
== '\'' || *text
== '"';
414 int quote_char
= '\0';
415 const char *colon
= NULL
;
416 char *file_to_match
= NULL
;
417 const char *symbol_start
= text
;
418 const char *orig_text
= text
;
420 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
421 for (p
= text
; *p
!= '\0'; ++p
)
423 if (*p
== '\\' && p
[1] == '\'')
425 else if (*p
== '\'' || *p
== '"')
429 while (*p
!= '\0' && *p
!= quote_found
)
431 if (*p
== '\\' && p
[1] == quote_found
)
436 if (*p
== quote_found
)
439 break; /* Hit the end of text. */
441 #if HAVE_DOS_BASED_FILE_SYSTEM
442 /* If we have a DOS-style absolute file name at the beginning of
443 TEXT, and the colon after the drive letter is the only colon
444 we found, pretend the colon is not there. */
445 else if (p
< text
+ 3 && *p
== ':' && p
== text
+ 1 + quoted
)
448 else if (*p
== ':' && !colon
)
451 symbol_start
= p
+ 1;
453 else if (strchr (current_language
->la_word_break_characters(), *p
))
454 symbol_start
= p
+ 1;
460 /* Where is the file name? */
465 file_to_match
= (char *) xmalloc (colon
- text
+ 1);
466 strncpy (file_to_match
, text
, colon
- text
);
467 file_to_match
[colon
- text
] = '\0';
468 /* Remove trailing colons and quotes from the file name. */
469 for (s
= file_to_match
+ (colon
- text
);
472 if (*s
== ':' || *s
== quote_char
)
475 /* If the text includes a colon, they want completion only on a
476 symbol name after the colon. Otherwise, we need to complete on
477 symbols as well as on files. */
480 collect_file_symbol_completion_matches (tracker
,
481 complete_symbol_mode::EXPRESSION
,
482 symbol_name_match_type::EXPRESSION
,
485 xfree (file_to_match
);
489 size_t text_len
= strlen (text
);
491 collect_symbol_completion_matches (tracker
,
492 complete_symbol_mode::EXPRESSION
,
493 symbol_name_match_type::EXPRESSION
,
495 /* If text includes characters which cannot appear in a file
496 name, they cannot be asking for completion on files. */
498 gdb_completer_file_name_break_characters
) == text_len
)
499 fn_list
= make_source_files_completion_list (text
, text
);
502 if (!fn_list
.empty () && !tracker
.have_completions ())
504 /* If we only have file names as possible completion, we should
505 bring them in sync with what rl_complete expects. The
506 problem is that if the user types "break /foo/b TAB", and the
507 possible completions are "/foo/bar" and "/foo/baz"
508 rl_complete expects us to return "bar" and "baz", without the
509 leading directories, as possible completions, because `word'
510 starts at the "b". But we ignore the value of `word' when we
511 call make_source_files_completion_list above (because that
512 would not DTRT when the completion results in both symbols
513 and file names), so make_source_files_completion_list returns
514 the full "/foo/bar" and "/foo/baz" strings. This produces
515 wrong results when, e.g., there's only one possible
516 completion, because rl_complete will prepend "/foo/" to each
517 candidate completion. The loop below removes that leading
519 for (const auto &fn_up
: fn_list
)
521 char *fn
= fn_up
.get ();
522 memmove (fn
, fn
+ (word
- text
), strlen (fn
) + 1 - (word
- text
));
526 tracker
.add_completions (std::move (fn_list
));
528 if (!tracker
.have_completions ())
530 /* No completions at all. As the final resort, try completing
531 on the entire text as a symbol. */
532 collect_symbol_completion_matches (tracker
,
533 complete_symbol_mode::EXPRESSION
,
534 symbol_name_match_type::EXPRESSION
,
539 /* See completer.h. */
542 complete_source_filenames (const char *text
)
544 size_t text_len
= strlen (text
);
546 /* If text includes characters which cannot appear in a file name,
547 the user cannot be asking for completion on files. */
549 gdb_completer_file_name_break_characters
)
551 return make_source_files_completion_list (text
, text
);
556 /* Complete address and linespec locations. */
559 complete_address_and_linespec_locations (completion_tracker
&tracker
,
561 symbol_name_match_type match_type
)
565 tracker
.advance_custom_word_point_by (1);
568 = advance_to_expression_complete_word_point (tracker
, text
);
569 complete_expression (tracker
, text
, word
);
573 linespec_complete (tracker
, text
, match_type
);
577 /* The explicit location options. Note that indexes into this array
578 must match the explicit_location_match_type enumerators. */
580 static const char *const explicit_options
[] =
590 /* The probe modifier options. These can appear before a location in
591 breakpoint commands. */
592 static const char *const probe_options
[] =
600 /* Returns STRING if not NULL, the empty string otherwise. */
603 string_or_empty (const char *string
)
605 return string
!= NULL
? string
: "";
608 /* A helper function to collect explicit location matches for the given
609 LOCATION, which is attempting to match on WORD. */
612 collect_explicit_location_matches (completion_tracker
&tracker
,
613 struct event_location
*location
,
614 enum explicit_location_match_type what
,
616 const struct language_defn
*language
)
618 const struct explicit_location
*explicit_loc
619 = get_explicit_location (location
);
621 /* True if the option expects an argument. */
622 bool needs_arg
= true;
624 /* Note, in the various MATCH_* below, we complete on
625 explicit_loc->foo instead of WORD, because only the former will
626 have already skipped past any quote char. */
631 const char *source
= string_or_empty (explicit_loc
->source_filename
);
632 completion_list matches
633 = make_source_files_completion_list (source
, source
);
634 tracker
.add_completions (std::move (matches
));
640 const char *function
= string_or_empty (explicit_loc
->function_name
);
641 linespec_complete_function (tracker
, function
,
642 explicit_loc
->func_name_match_type
,
643 explicit_loc
->source_filename
);
647 case MATCH_QUALIFIED
:
651 /* Nothing to offer. */
656 const char *label
= string_or_empty (explicit_loc
->label_name
);
657 linespec_complete_label (tracker
, language
,
658 explicit_loc
->source_filename
,
659 explicit_loc
->function_name
,
660 explicit_loc
->func_name_match_type
,
666 gdb_assert_not_reached ("unhandled explicit_location_match_type");
669 if (!needs_arg
|| tracker
.completes_to_completion_word (word
))
671 tracker
.discard_completions ();
672 tracker
.advance_custom_word_point_by (strlen (word
));
673 complete_on_enum (tracker
, explicit_options
, "", "");
674 complete_on_enum (tracker
, linespec_keywords
, "", "");
676 else if (!tracker
.have_completions ())
678 /* Maybe we have an unterminated linespec keyword at the tail of
679 the string. Try completing on that. */
680 size_t wordlen
= strlen (word
);
681 const char *keyword
= word
+ wordlen
;
683 if (wordlen
> 0 && keyword
[-1] != ' ')
685 while (keyword
> word
&& *keyword
!= ' ')
687 /* Don't complete on keywords if we'd be completing on the
688 whole explicit linespec option. E.g., "b -function
689 thr<tab>" should not complete to the "thread"
693 keyword
= skip_spaces (keyword
);
695 tracker
.advance_custom_word_point_by (keyword
- word
);
696 complete_on_enum (tracker
, linespec_keywords
, keyword
, keyword
);
699 else if (wordlen
> 0 && keyword
[-1] == ' ')
701 /* Assume that we're maybe past the explicit location
702 argument, and we didn't manage to find any match because
703 the user wants to create a pending breakpoint. Offer the
704 keyword and explicit location options as possible
706 tracker
.advance_custom_word_point_by (keyword
- word
);
707 complete_on_enum (tracker
, linespec_keywords
, keyword
, keyword
);
708 complete_on_enum (tracker
, explicit_options
, keyword
, keyword
);
713 /* If the next word in *TEXT_P is any of the keywords in KEYWORDS,
714 then advance both TEXT_P and the word point in the tracker past the
715 keyword and return the (0-based) index in the KEYWORDS array that
716 matched. Otherwise, return -1. */
719 skip_keyword (completion_tracker
&tracker
,
720 const char * const *keywords
, const char **text_p
)
722 const char *text
= *text_p
;
723 const char *after
= skip_to_space (text
);
724 size_t len
= after
- text
;
726 if (text
[len
] != ' ')
730 for (int i
= 0; keywords
[i
] != NULL
; i
++)
732 if (strncmp (keywords
[i
], text
, len
) == 0)
743 tracker
.advance_custom_word_point_by (len
+ 1);
752 /* A completer function for explicit locations. This function
753 completes both options ("-source", "-line", etc) and values. If
754 completing a quoted string, then QUOTED_ARG_START and
755 QUOTED_ARG_END point to the quote characters. LANGUAGE is the
759 complete_explicit_location (completion_tracker
&tracker
,
760 struct event_location
*location
,
762 const language_defn
*language
,
763 const char *quoted_arg_start
,
764 const char *quoted_arg_end
)
769 int keyword
= skip_keyword (tracker
, explicit_options
, &text
);
772 complete_on_enum (tracker
, explicit_options
, text
, text
);
775 /* Completing on value. */
776 enum explicit_location_match_type what
777 = (explicit_location_match_type
) keyword
;
779 if (quoted_arg_start
!= NULL
&& quoted_arg_end
!= NULL
)
781 if (quoted_arg_end
[1] == '\0')
783 /* If completing a quoted string with the cursor right
784 at the terminating quote char, complete the
785 completion word without interpretation, so that
786 readline advances the cursor one whitespace past the
787 quote, even if there's no match. This makes these
788 cases behave the same:
790 before: "b -function function()"
791 after: "b -function function() "
793 before: "b -function 'function()'"
794 after: "b -function 'function()' "
796 and trusts the user in this case:
798 before: "b -function 'not_loaded_function_yet()'"
799 after: "b -function 'not_loaded_function_yet()' "
801 gdb::unique_xmalloc_ptr
<char> text_copy
803 tracker
.add_completion (std::move (text_copy
));
805 else if (quoted_arg_end
[1] == ' ')
807 /* We're maybe past the explicit location argument.
808 Skip the argument without interpretion, assuming the
809 user may want to create pending breakpoint. Offer
810 the keyword and explicit location options as possible
812 tracker
.advance_custom_word_point_by (strlen (text
));
813 complete_on_enum (tracker
, linespec_keywords
, "", "");
814 complete_on_enum (tracker
, explicit_options
, "", "");
819 /* Now gather matches */
820 collect_explicit_location_matches (tracker
, location
, what
, text
,
825 /* A completer for locations. */
828 location_completer (struct cmd_list_element
*ignore
,
829 completion_tracker
&tracker
,
830 const char *text
, const char * /* word */)
832 int found_probe_option
= -1;
834 /* If we have a probe modifier, skip it. This can only appear as
835 first argument. Until we have a specific completer for probes,
836 falling back to the linespec completer for the remainder of the
837 line is better than nothing. */
838 if (text
[0] == '-' && text
[1] == 'p')
839 found_probe_option
= skip_keyword (tracker
, probe_options
, &text
);
841 const char *option_text
= text
;
842 int saved_word_point
= tracker
.custom_word_point ();
844 const char *copy
= text
;
846 explicit_completion_info completion_info
;
847 event_location_up location
848 = string_to_explicit_location (©
, current_language
,
850 if (completion_info
.quoted_arg_start
!= NULL
851 && completion_info
.quoted_arg_end
== NULL
)
853 /* Found an unbalanced quote. */
854 tracker
.set_quote_char (*completion_info
.quoted_arg_start
);
855 tracker
.advance_custom_word_point_by (1);
858 if (completion_info
.saw_explicit_location_option
)
862 tracker
.advance_custom_word_point_by (copy
- text
);
865 /* We found a terminator at the tail end of the string,
866 which means we're past the explicit location options. We
867 may have a keyword to complete on. If we have a whole
868 keyword, then complete whatever comes after as an
869 expression. This is mainly for the "if" keyword. If the
870 "thread" and "task" keywords gain their own completers,
871 they should be used here. */
872 int keyword
= skip_keyword (tracker
, linespec_keywords
, &text
);
876 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
881 = advance_to_expression_complete_word_point (tracker
, text
);
882 complete_expression (tracker
, text
, word
);
887 tracker
.advance_custom_word_point_by (completion_info
.last_option
889 text
= completion_info
.last_option
;
891 complete_explicit_location (tracker
, location
.get (), text
,
893 completion_info
.quoted_arg_start
,
894 completion_info
.quoted_arg_end
);
898 /* This is an address or linespec location. */
899 else if (location
!= NULL
)
901 /* Handle non-explicit location options. */
903 int keyword
= skip_keyword (tracker
, explicit_options
, &text
);
905 complete_on_enum (tracker
, explicit_options
, text
, text
);
908 tracker
.advance_custom_word_point_by (copy
- text
);
911 symbol_name_match_type match_type
912 = get_explicit_location (location
.get ())->func_name_match_type
;
913 complete_address_and_linespec_locations (tracker
, text
, match_type
);
919 complete_address_and_linespec_locations (tracker
, text
,
920 symbol_name_match_type::WILD
);
923 /* Add matches for option names, if either:
925 - Some completer above found some matches, but the word point did
926 not advance (e.g., "b <tab>" finds all functions, or "b -<tab>"
927 matches all objc selectors), or;
929 - Some completer above advanced the word point, but found no
932 if ((text
[0] == '-' || text
[0] == '\0')
933 && (!tracker
.have_completions ()
934 || tracker
.custom_word_point () == saved_word_point
))
936 tracker
.set_custom_word_point (saved_word_point
);
939 if (found_probe_option
== -1)
940 complete_on_enum (tracker
, probe_options
, text
, text
);
941 complete_on_enum (tracker
, explicit_options
, text
, text
);
945 /* The corresponding completer_handle_brkchars
949 location_completer_handle_brkchars (struct cmd_list_element
*ignore
,
950 completion_tracker
&tracker
,
952 const char *word_ignored
)
954 tracker
.set_use_custom_word_point (true);
956 location_completer (ignore
, tracker
, text
, NULL
);
959 /* Helper for expression_completer which recursively adds field and
960 method names from TYPE, a struct or union type, to the OUTPUT
964 add_struct_fields (struct type
*type
, completion_list
&output
,
965 const char *fieldname
, int namelen
)
968 int computed_type_name
= 0;
969 const char *type_name
= NULL
;
971 type
= check_typedef (type
);
972 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
974 if (i
< TYPE_N_BASECLASSES (type
))
975 add_struct_fields (TYPE_BASECLASS (type
, i
),
976 output
, fieldname
, namelen
);
977 else if (TYPE_FIELD_NAME (type
, i
))
979 if (TYPE_FIELD_NAME (type
, i
)[0] != '\0')
981 if (! strncmp (TYPE_FIELD_NAME (type
, i
),
983 output
.emplace_back (xstrdup (TYPE_FIELD_NAME (type
, i
)));
985 else if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_UNION
)
987 /* Recurse into anonymous unions. */
988 add_struct_fields (TYPE_FIELD_TYPE (type
, i
),
989 output
, fieldname
, namelen
);
994 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
996 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
998 if (name
&& ! strncmp (name
, fieldname
, namelen
))
1000 if (!computed_type_name
)
1002 type_name
= TYPE_NAME (type
);
1003 computed_type_name
= 1;
1005 /* Omit constructors from the completion list. */
1006 if (!type_name
|| strcmp (type_name
, name
))
1007 output
.emplace_back (xstrdup (name
));
1012 /* See completer.h. */
1015 complete_expression (completion_tracker
&tracker
,
1016 const char *text
, const char *word
)
1018 struct type
*type
= NULL
;
1019 gdb::unique_xmalloc_ptr
<char> fieldname
;
1020 enum type_code code
= TYPE_CODE_UNDEF
;
1022 /* Perform a tentative parse of the expression, to see whether a
1023 field completion is required. */
1026 type
= parse_expression_for_completion (text
, &fieldname
, &code
);
1028 CATCH (except
, RETURN_MASK_ERROR
)
1034 if (fieldname
!= nullptr && type
)
1038 type
= check_typedef (type
);
1039 if (TYPE_CODE (type
) != TYPE_CODE_PTR
&& !TYPE_IS_REFERENCE (type
))
1041 type
= TYPE_TARGET_TYPE (type
);
1044 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1045 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1047 completion_list result
;
1049 add_struct_fields (type
, result
, fieldname
.get (),
1050 strlen (fieldname
.get ()));
1051 tracker
.add_completions (std::move (result
));
1055 else if (fieldname
!= nullptr && code
!= TYPE_CODE_UNDEF
)
1057 collect_symbol_completion_matches_type (tracker
, fieldname
.get (),
1058 fieldname
.get (), code
);
1062 complete_files_symbols (tracker
, text
, word
);
1065 /* Complete on expressions. Often this means completing on symbol
1066 names, but some language parsers also have support for completing
1070 expression_completer (struct cmd_list_element
*ignore
,
1071 completion_tracker
&tracker
,
1072 const char *text
, const char *word
)
1074 complete_expression (tracker
, text
, word
);
1077 /* See definition in completer.h. */
1080 set_rl_completer_word_break_characters (const char *break_chars
)
1082 rl_completer_word_break_characters
= (char *) break_chars
;
1085 /* See definition in completer.h. */
1088 set_gdb_completion_word_break_characters (completer_ftype
*fn
)
1090 const char *break_chars
;
1092 /* So far we are only interested in differentiating filename
1093 completers from everything else. */
1094 if (fn
== filename_completer
)
1095 break_chars
= gdb_completer_file_name_break_characters
;
1097 break_chars
= gdb_completer_command_word_break_characters
;
1099 set_rl_completer_word_break_characters (break_chars
);
1102 /* Complete on symbols. */
1105 symbol_completer (struct cmd_list_element
*ignore
,
1106 completion_tracker
&tracker
,
1107 const char *text
, const char *word
)
1109 collect_symbol_completion_matches (tracker
, complete_symbol_mode::EXPRESSION
,
1110 symbol_name_match_type::EXPRESSION
,
1114 /* Here are some useful test cases for completion. FIXME: These
1115 should be put in the test suite. They should be tested with both
1118 "show output-" "radix"
1119 "show output" "-radix"
1120 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1121 "p " ambiguous (all symbols)
1122 "info t foo" no completions
1123 "info t " no completions
1124 "info t" ambiguous ("info target", "info terminal", etc.)
1125 "info ajksdlfk" no completions
1126 "info ajksdlfk " no completions
1128 "info " ambiguous (all info commands)
1129 "p \"a" no completions (string constant)
1130 "p 'a" ambiguous (all symbols starting with a)
1131 "p b-a" ambiguous (all symbols starting with a)
1132 "p b-" ambiguous (all symbols)
1133 "file Make" "file" (word break hard to screw up here)
1134 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1137 enum complete_line_internal_reason
1139 /* Preliminary phase, called by gdb_completion_word_break_characters
1140 function, is used to either:
1142 #1 - Determine the set of chars that are word delimiters
1143 depending on the current command in line_buffer.
1145 #2 - Manually advance RL_POINT to the "word break" point instead
1146 of letting readline do it (based on too-simple character
1149 Simpler completers that just pass a brkchars array to readline
1150 (#1 above) must defer generating the completions to the main
1151 phase (below). No completion list should be generated in this
1154 OTOH, completers that manually advance the word point(#2 above)
1155 must set "use_custom_word_point" in the tracker and generate
1156 their completion in this phase. Note that this is the convenient
1157 thing to do since they'll be parsing the input line anyway. */
1160 /* Main phase, called by complete_line function, is used to get the
1161 list of possible completions. */
1164 /* Special case when completing a 'help' command. In this case,
1165 once sub-command completions are exhausted, we simply return
1170 /* Helper for complete_line_internal to simplify it. */
1173 complete_line_internal_normal_command (completion_tracker
&tracker
,
1174 const char *command
, const char *word
,
1175 const char *cmd_args
,
1176 complete_line_internal_reason reason
,
1177 struct cmd_list_element
*c
)
1179 const char *p
= cmd_args
;
1181 if (c
->completer
== filename_completer
)
1183 /* Many commands which want to complete on file names accept
1184 several file names, as in "run foo bar >>baz". So we don't
1185 want to complete the entire text after the command, just the
1186 last word. To this end, we need to find the beginning of the
1187 file name by starting at `word' and going backwards. */
1190 && strchr (gdb_completer_file_name_break_characters
,
1196 if (reason
== handle_brkchars
)
1198 completer_handle_brkchars_ftype
*brkchars_fn
;
1200 if (c
->completer_handle_brkchars
!= NULL
)
1201 brkchars_fn
= c
->completer_handle_brkchars
;
1205 = (completer_handle_brkchars_func_for_completer
1209 brkchars_fn (c
, tracker
, p
, word
);
1212 if (reason
!= handle_brkchars
&& c
->completer
!= NULL
)
1213 (*c
->completer
) (c
, tracker
, p
, word
);
1216 /* Internal function used to handle completions.
1219 TEXT is the caller's idea of the "word" we are looking at.
1221 LINE_BUFFER is available to be looked at; it contains the entire
1222 text of the line. POINT is the offset in that line of the cursor.
1223 You should pretend that the line ends at POINT.
1225 See complete_line_internal_reason for description of REASON. */
1228 complete_line_internal_1 (completion_tracker
&tracker
,
1230 const char *line_buffer
, int point
,
1231 complete_line_internal_reason reason
)
1235 int ignore_help_classes
;
1236 /* Pointer within tmp_command which corresponds to text. */
1238 struct cmd_list_element
*c
, *result_list
;
1240 /* Choose the default set of word break characters to break
1241 completions. If we later find out that we are doing completions
1242 on command strings (as opposed to strings supplied by the
1243 individual command completer functions, which can be any string)
1244 then we will switch to the special word break set for command
1245 strings, which leaves out the '-' character used in some
1247 set_rl_completer_word_break_characters
1248 (current_language
->la_word_break_characters());
1250 /* Decide whether to complete on a list of gdb commands or on
1252 tmp_command
= (char *) alloca (point
+ 1);
1255 /* The help command should complete help aliases. */
1256 ignore_help_classes
= reason
!= handle_help
;
1258 strncpy (tmp_command
, line_buffer
, point
);
1259 tmp_command
[point
] = '\0';
1260 if (reason
== handle_brkchars
)
1262 gdb_assert (text
== NULL
);
1267 /* Since text always contains some number of characters leading up
1268 to point, we can find the equivalent position in tmp_command
1269 by subtracting that many characters from the end of tmp_command. */
1270 word
= tmp_command
+ point
- strlen (text
);
1273 /* Move P up to the start of the command. */
1274 p
= skip_spaces (p
);
1278 /* An empty line is ambiguous; that is, it could be any
1280 c
= CMD_LIST_AMBIGUOUS
;
1285 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, ignore_help_classes
);
1288 /* Move p up to the next interesting thing. */
1289 while (*p
== ' ' || *p
== '\t')
1294 tracker
.advance_custom_word_point_by (p
- tmp_command
);
1298 /* It is an unrecognized command. So there are no
1299 possible completions. */
1301 else if (c
== CMD_LIST_AMBIGUOUS
)
1305 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1306 doesn't advance over that thing itself. Do so now. */
1308 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1310 if (q
!= tmp_command
+ point
)
1312 /* There is something beyond the ambiguous
1313 command, so there are no possible completions. For
1314 example, "info t " or "info t foo" does not complete
1315 to anything, because "info t" can be "info target" or
1320 /* We're trying to complete on the command which was ambiguous.
1321 This we can deal with. */
1324 if (reason
!= handle_brkchars
)
1325 complete_on_cmdlist (*result_list
->prefixlist
, tracker
, p
,
1326 word
, ignore_help_classes
);
1330 if (reason
!= handle_brkchars
)
1331 complete_on_cmdlist (cmdlist
, tracker
, p
, word
,
1332 ignore_help_classes
);
1334 /* Ensure that readline does the right thing with respect to
1335 inserting quotes. */
1336 set_rl_completer_word_break_characters
1337 (gdb_completer_command_word_break_characters
);
1342 /* We've recognized a full command. */
1344 if (p
== tmp_command
+ point
)
1346 /* There is no non-whitespace in the line beyond the
1349 if (p
[-1] == ' ' || p
[-1] == '\t')
1351 /* The command is followed by whitespace; we need to
1352 complete on whatever comes after command. */
1355 /* It is a prefix command; what comes after it is
1356 a subcommand (e.g. "info "). */
1357 if (reason
!= handle_brkchars
)
1358 complete_on_cmdlist (*c
->prefixlist
, tracker
, p
, word
,
1359 ignore_help_classes
);
1361 /* Ensure that readline does the right thing
1362 with respect to inserting quotes. */
1363 set_rl_completer_word_break_characters
1364 (gdb_completer_command_word_break_characters
);
1366 else if (reason
== handle_help
)
1370 if (reason
!= handle_brkchars
)
1371 complete_on_enum (tracker
, c
->enums
, p
, word
);
1372 set_rl_completer_word_break_characters
1373 (gdb_completer_command_word_break_characters
);
1377 /* It is a normal command; what comes after it is
1378 completed by the command's completer function. */
1379 complete_line_internal_normal_command (tracker
,
1380 tmp_command
, word
, p
,
1386 /* The command is not followed by whitespace; we need to
1387 complete on the command itself, e.g. "p" which is a
1388 command itself but also can complete to "print", "ptype"
1392 /* Find the command we are completing on. */
1394 while (q
> tmp_command
)
1396 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1402 if (reason
!= handle_brkchars
)
1403 complete_on_cmdlist (result_list
, tracker
, q
, word
,
1404 ignore_help_classes
);
1406 /* Ensure that readline does the right thing
1407 with respect to inserting quotes. */
1408 set_rl_completer_word_break_characters
1409 (gdb_completer_command_word_break_characters
);
1412 else if (reason
== handle_help
)
1416 /* There is non-whitespace beyond the command. */
1418 if (c
->prefixlist
&& !c
->allow_unknown
)
1420 /* It is an unrecognized subcommand of a prefix command,
1421 e.g. "info adsfkdj". */
1425 if (reason
!= handle_brkchars
)
1426 complete_on_enum (tracker
, c
->enums
, p
, word
);
1430 /* It is a normal command. */
1431 complete_line_internal_normal_command (tracker
,
1432 tmp_command
, word
, p
,
1439 /* Wrapper around complete_line_internal_1 to handle
1440 MAX_COMPLETIONS_REACHED_ERROR. */
1443 complete_line_internal (completion_tracker
&tracker
,
1445 const char *line_buffer
, int point
,
1446 complete_line_internal_reason reason
)
1450 complete_line_internal_1 (tracker
, text
, line_buffer
, point
, reason
);
1452 CATCH (except
, RETURN_MASK_ERROR
)
1454 if (except
.error
!= MAX_COMPLETIONS_REACHED_ERROR
)
1455 throw_exception (except
);
1460 /* See completer.h. */
1462 int max_completions
= 200;
1464 /* Initial size of the table. It automagically grows from here. */
1465 #define INITIAL_COMPLETION_HTAB_SIZE 200
1467 /* See completer.h. */
1469 completion_tracker::completion_tracker ()
1471 m_entries_hash
= htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE
,
1472 htab_hash_string
, streq_hash
,
1473 NULL
, xcalloc
, xfree
);
1476 /* See completer.h. */
1479 completion_tracker::discard_completions ()
1481 xfree (m_lowest_common_denominator
);
1482 m_lowest_common_denominator
= NULL
;
1484 m_lowest_common_denominator_unique
= false;
1486 m_entries_vec
.clear ();
1488 htab_delete (m_entries_hash
);
1489 m_entries_hash
= htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE
,
1490 htab_hash_string
, streq_hash
,
1491 NULL
, xcalloc
, xfree
);
1494 /* See completer.h. */
1496 completion_tracker::~completion_tracker ()
1498 xfree (m_lowest_common_denominator
);
1499 htab_delete (m_entries_hash
);
1502 /* See completer.h. */
1505 completion_tracker::maybe_add_completion
1506 (gdb::unique_xmalloc_ptr
<char> name
,
1507 completion_match_for_lcd
*match_for_lcd
,
1508 const char *text
, const char *word
)
1512 if (max_completions
== 0)
1515 if (htab_elements (m_entries_hash
) >= max_completions
)
1518 slot
= htab_find_slot (m_entries_hash
, name
.get (), INSERT
);
1519 if (*slot
== HTAB_EMPTY_ENTRY
)
1521 const char *match_for_lcd_str
= NULL
;
1523 if (match_for_lcd
!= NULL
)
1524 match_for_lcd_str
= match_for_lcd
->finish ();
1526 if (match_for_lcd_str
== NULL
)
1527 match_for_lcd_str
= name
.get ();
1529 gdb::unique_xmalloc_ptr
<char> lcd
1530 = make_completion_match_str (match_for_lcd_str
, text
, word
);
1532 recompute_lowest_common_denominator (std::move (lcd
));
1534 *slot
= name
.get ();
1535 m_entries_vec
.push_back (std::move (name
));
1541 /* See completer.h. */
1544 completion_tracker::add_completion (gdb::unique_xmalloc_ptr
<char> name
,
1545 completion_match_for_lcd
*match_for_lcd
,
1546 const char *text
, const char *word
)
1548 if (!maybe_add_completion (std::move (name
), match_for_lcd
, text
, word
))
1549 throw_error (MAX_COMPLETIONS_REACHED_ERROR
, _("Max completions reached."));
1552 /* See completer.h. */
1555 completion_tracker::add_completions (completion_list
&&list
)
1557 for (auto &candidate
: list
)
1558 add_completion (std::move (candidate
));
1561 /* Helper for the make_completion_match_str overloads. Returns NULL
1562 as an indication that we want MATCH_NAME exactly. It is up to the
1563 caller to xstrdup that string if desired. */
1566 make_completion_match_str_1 (const char *match_name
,
1567 const char *text
, const char *word
)
1573 /* Return NULL as an indication that we want MATCH_NAME
1577 else if (word
> text
)
1579 /* Return some portion of MATCH_NAME. */
1580 newobj
= xstrdup (match_name
+ (word
- text
));
1584 /* Return some of WORD plus MATCH_NAME. */
1585 size_t len
= strlen (match_name
);
1586 newobj
= (char *) xmalloc (text
- word
+ len
+ 1);
1587 memcpy (newobj
, word
, text
- word
);
1588 memcpy (newobj
+ (text
- word
), match_name
, len
+ 1);
1594 /* See completer.h. */
1596 gdb::unique_xmalloc_ptr
<char>
1597 make_completion_match_str (const char *match_name
,
1598 const char *text
, const char *word
)
1600 char *newobj
= make_completion_match_str_1 (match_name
, text
, word
);
1602 newobj
= xstrdup (match_name
);
1603 return gdb::unique_xmalloc_ptr
<char> (newobj
);
1606 /* See completer.h. */
1608 gdb::unique_xmalloc_ptr
<char>
1609 make_completion_match_str (gdb::unique_xmalloc_ptr
<char> &&match_name
,
1610 const char *text
, const char *word
)
1612 char *newobj
= make_completion_match_str_1 (match_name
.get (), text
, word
);
1614 return std::move (match_name
);
1615 return gdb::unique_xmalloc_ptr
<char> (newobj
);
1618 /* Generate completions all at once. Does nothing if max_completions
1619 is 0. If max_completions is non-negative, this will collect at
1620 most max_completions strings.
1622 TEXT is the caller's idea of the "word" we are looking at.
1624 LINE_BUFFER is available to be looked at; it contains the entire
1627 POINT is the offset in that line of the cursor. You
1628 should pretend that the line ends at POINT. */
1631 complete_line (completion_tracker
&tracker
,
1632 const char *text
, const char *line_buffer
, int point
)
1634 if (max_completions
== 0)
1636 complete_line_internal (tracker
, text
, line_buffer
, point
,
1637 handle_completions
);
1640 /* Complete on command names. Used by "help". */
1643 command_completer (struct cmd_list_element
*ignore
,
1644 completion_tracker
&tracker
,
1645 const char *text
, const char *word
)
1647 complete_line_internal (tracker
, word
, text
,
1648 strlen (text
), handle_help
);
1651 /* The corresponding completer_handle_brkchars implementation. */
1654 command_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1655 completion_tracker
&tracker
,
1656 const char *text
, const char *word
)
1658 set_rl_completer_word_break_characters
1659 (gdb_completer_command_word_break_characters
);
1662 /* Complete on signals. */
1665 signal_completer (struct cmd_list_element
*ignore
,
1666 completion_tracker
&tracker
,
1667 const char *text
, const char *word
)
1669 size_t len
= strlen (word
);
1671 const char *signame
;
1673 for (signum
= GDB_SIGNAL_FIRST
; signum
!= GDB_SIGNAL_LAST
; ++signum
)
1675 /* Can't handle this, so skip it. */
1676 if (signum
== GDB_SIGNAL_0
)
1679 signame
= gdb_signal_to_name ((enum gdb_signal
) signum
);
1681 /* Ignore the unknown signal case. */
1682 if (!signame
|| strcmp (signame
, "?") == 0)
1685 if (strncasecmp (signame
, word
, len
) == 0)
1687 gdb::unique_xmalloc_ptr
<char> copy (xstrdup (signame
));
1688 tracker
.add_completion (std::move (copy
));
1693 /* Bit-flags for selecting what the register and/or register-group
1694 completer should complete on. */
1696 enum reg_completer_target
1698 complete_register_names
= 0x1,
1699 complete_reggroup_names
= 0x2
1701 DEF_ENUM_FLAGS_TYPE (enum reg_completer_target
, reg_completer_targets
);
1703 /* Complete register names and/or reggroup names based on the value passed
1704 in TARGETS. At least one bit in TARGETS must be set. */
1707 reg_or_group_completer_1 (completion_tracker
&tracker
,
1708 const char *text
, const char *word
,
1709 reg_completer_targets targets
)
1711 size_t len
= strlen (word
);
1712 struct gdbarch
*gdbarch
;
1715 gdb_assert ((targets
& (complete_register_names
1716 | complete_reggroup_names
)) != 0);
1717 gdbarch
= get_current_arch ();
1719 if ((targets
& complete_register_names
) != 0)
1724 (name
= user_reg_map_regnum_to_name (gdbarch
, i
)) != NULL
;
1727 if (*name
!= '\0' && strncmp (word
, name
, len
) == 0)
1729 gdb::unique_xmalloc_ptr
<char> copy (xstrdup (name
));
1730 tracker
.add_completion (std::move (copy
));
1735 if ((targets
& complete_reggroup_names
) != 0)
1737 struct reggroup
*group
;
1739 for (group
= reggroup_next (gdbarch
, NULL
);
1741 group
= reggroup_next (gdbarch
, group
))
1743 name
= reggroup_name (group
);
1744 if (strncmp (word
, name
, len
) == 0)
1746 gdb::unique_xmalloc_ptr
<char> copy (xstrdup (name
));
1747 tracker
.add_completion (std::move (copy
));
1753 /* Perform completion on register and reggroup names. */
1756 reg_or_group_completer (struct cmd_list_element
*ignore
,
1757 completion_tracker
&tracker
,
1758 const char *text
, const char *word
)
1760 reg_or_group_completer_1 (tracker
, text
, word
,
1761 (complete_register_names
1762 | complete_reggroup_names
));
1765 /* Perform completion on reggroup names. */
1768 reggroup_completer (struct cmd_list_element
*ignore
,
1769 completion_tracker
&tracker
,
1770 const char *text
, const char *word
)
1772 reg_or_group_completer_1 (tracker
, text
, word
,
1773 complete_reggroup_names
);
1776 /* The default completer_handle_brkchars implementation. */
1779 default_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1780 completion_tracker
&tracker
,
1781 const char *text
, const char *word
)
1783 set_rl_completer_word_break_characters
1784 (current_language
->la_word_break_characters ());
1787 /* See definition in completer.h. */
1789 completer_handle_brkchars_ftype
*
1790 completer_handle_brkchars_func_for_completer (completer_ftype
*fn
)
1792 if (fn
== filename_completer
)
1793 return filename_completer_handle_brkchars
;
1795 if (fn
== location_completer
)
1796 return location_completer_handle_brkchars
;
1798 if (fn
== command_completer
)
1799 return command_completer_handle_brkchars
;
1801 return default_completer_handle_brkchars
;
1804 /* Used as brkchars when we want to tell readline we have a custom
1805 word point. We do that by making our rl_completion_word_break_hook
1806 set RL_POINT to the desired word point, and return the character at
1807 the word break point as the break char. This is two bytes in order
1808 to fit one break character plus the terminating null. */
1809 static char gdb_custom_word_point_brkchars
[2];
1811 /* Since rl_basic_quote_characters is not completer-specific, we save
1812 its original value here, in order to be able to restore it in
1813 gdb_rl_attempted_completion_function. */
1814 static const char *gdb_org_rl_basic_quote_characters
= rl_basic_quote_characters
;
1816 /* Get the list of chars that are considered as word breaks
1817 for the current command. */
1820 gdb_completion_word_break_characters_throw ()
1822 /* New completion starting. Get rid of the previous tracker and
1824 delete current_completion
.tracker
;
1825 current_completion
.tracker
= new completion_tracker ();
1827 completion_tracker
&tracker
= *current_completion
.tracker
;
1829 complete_line_internal (tracker
, NULL
, rl_line_buffer
,
1830 rl_point
, handle_brkchars
);
1832 if (tracker
.use_custom_word_point ())
1834 gdb_assert (tracker
.custom_word_point () > 0);
1835 rl_point
= tracker
.custom_word_point () - 1;
1836 gdb_custom_word_point_brkchars
[0] = rl_line_buffer
[rl_point
];
1837 rl_completer_word_break_characters
= gdb_custom_word_point_brkchars
;
1838 rl_completer_quote_characters
= NULL
;
1840 /* Clear this too, so that if we're completing a quoted string,
1841 readline doesn't consider the quote character a delimiter.
1842 If we didn't do this, readline would auto-complete {b
1843 'fun<tab>} to {'b 'function()'}, i.e., add the terminating
1844 \', but, it wouldn't append the separator space either, which
1845 is not desirable. So instead we take care of appending the
1846 quote character to the LCD ourselves, in
1847 gdb_rl_attempted_completion_function. Since this global is
1848 not just completer-specific, we'll restore it back to the
1849 default in gdb_rl_attempted_completion_function. */
1850 rl_basic_quote_characters
= NULL
;
1853 return rl_completer_word_break_characters
;
1857 gdb_completion_word_break_characters ()
1859 /* New completion starting. */
1860 current_completion
.aborted
= false;
1864 return gdb_completion_word_break_characters_throw ();
1866 CATCH (ex
, RETURN_MASK_ALL
)
1868 /* Set this to that gdb_rl_attempted_completion_function knows
1870 current_completion
.aborted
= true;
1877 /* See completer.h. */
1880 completion_find_completion_word (completion_tracker
&tracker
, const char *text
,
1883 size_t point
= strlen (text
);
1885 complete_line_internal (tracker
, NULL
, text
, point
, handle_brkchars
);
1887 if (tracker
.use_custom_word_point ())
1889 gdb_assert (tracker
.custom_word_point () > 0);
1890 *quote_char
= tracker
.quote_char ();
1891 return text
+ tracker
.custom_word_point ();
1894 gdb_rl_completion_word_info info
;
1896 info
.word_break_characters
= rl_completer_word_break_characters
;
1897 info
.quote_characters
= gdb_completer_quote_characters
;
1898 info
.basic_quote_characters
= rl_basic_quote_characters
;
1900 return gdb_rl_find_completion_word (&info
, quote_char
, NULL
, text
);
1903 /* See completer.h. */
1906 completion_tracker::recompute_lowest_common_denominator
1907 (gdb::unique_xmalloc_ptr
<char> &&new_match_up
)
1909 if (m_lowest_common_denominator
== NULL
)
1911 /* We don't have a lowest common denominator yet, so simply take
1912 the whole NEW_MATCH_UP as being it. */
1913 m_lowest_common_denominator
= new_match_up
.release ();
1914 m_lowest_common_denominator_unique
= true;
1918 /* Find the common denominator between the currently-known
1919 lowest common denominator and NEW_MATCH_UP. That becomes the
1920 new lowest common denominator. */
1922 const char *new_match
= new_match_up
.get ();
1925 (new_match
[i
] != '\0'
1926 && new_match
[i
] == m_lowest_common_denominator
[i
]);
1929 if (m_lowest_common_denominator
[i
] != new_match
[i
])
1931 m_lowest_common_denominator
[i
] = '\0';
1932 m_lowest_common_denominator_unique
= false;
1937 /* See completer.h. */
1940 completion_tracker::advance_custom_word_point_by (size_t len
)
1942 m_custom_word_point
+= len
;
1945 /* Build a new C string that is a copy of LCD with the whitespace of
1946 ORIG/ORIG_LEN preserved.
1948 Say the user is completing a symbol name, with spaces, like:
1952 and the resulting completion match is:
1956 we want to end up with an input line like:
1959 ^^^^^^^ => text from LCD [1], whitespace from ORIG preserved.
1960 ^^ => new text from LCD
1962 [1] - We must take characters from the LCD instead of the original
1963 text, since some completions want to change upper/lowercase. E.g.:
1969 "handle SIG[QUIT|etc.]"
1973 expand_preserving_ws (const char *orig
, size_t orig_len
,
1976 const char *p_orig
= orig
;
1977 const char *orig_end
= orig
+ orig_len
;
1978 const char *p_lcd
= lcd
;
1981 while (p_orig
< orig_end
)
1985 while (p_orig
< orig_end
&& *p_orig
== ' ')
1987 p_lcd
= skip_spaces (p_lcd
);
1991 /* Take characters from the LCD instead of the original
1992 text, since some completions change upper/lowercase.
1996 "handle SIG[QUIT|etc.]"
2004 while (*p_lcd
!= '\0')
2007 return xstrdup (res
.c_str ());
2010 /* See completer.h. */
2013 completion_tracker::build_completion_result (const char *text
,
2016 completion_list
&list
= m_entries_vec
; /* The completions. */
2021 /* +1 for the LCD, and +1 for NULL termination. */
2022 char **match_list
= XNEWVEC (char *, 1 + list
.size () + 1);
2024 /* Build replacement word, based on the LCD. */
2027 = expand_preserving_ws (text
, end
- start
,
2028 m_lowest_common_denominator
);
2030 if (m_lowest_common_denominator_unique
)
2032 /* We don't rely on readline appending the quote char as
2033 delimiter as then readline wouldn't append the ' ' after the
2035 char buf
[2] = { (char) quote_char () };
2037 match_list
[0] = reconcat (match_list
[0], match_list
[0],
2038 buf
, (char *) NULL
);
2039 match_list
[1] = NULL
;
2041 /* If the tracker wants to, or we already have a space at the
2042 end of the match, tell readline to skip appending
2044 bool completion_suppress_append
2045 = (suppress_append_ws ()
2046 || match_list
[0][strlen (match_list
[0]) - 1] == ' ');
2048 return completion_result (match_list
, 1, completion_suppress_append
);
2054 for (ix
= 0; ix
< list
.size (); ++ix
)
2055 match_list
[ix
+ 1] = list
[ix
].release ();
2056 match_list
[ix
+ 1] = NULL
;
2058 return completion_result (match_list
, list
.size (), false);
2062 /* See completer.h */
2064 completion_result::completion_result ()
2065 : match_list (NULL
), number_matches (0),
2066 completion_suppress_append (false)
2069 /* See completer.h */
2071 completion_result::completion_result (char **match_list_
,
2072 size_t number_matches_
,
2073 bool completion_suppress_append_
)
2074 : match_list (match_list_
),
2075 number_matches (number_matches_
),
2076 completion_suppress_append (completion_suppress_append_
)
2079 /* See completer.h */
2081 completion_result::~completion_result ()
2083 reset_match_list ();
2086 /* See completer.h */
2088 completion_result::completion_result (completion_result
&&rhs
)
2093 reset_match_list ();
2094 match_list
= rhs
.match_list
;
2095 rhs
.match_list
= NULL
;
2096 number_matches
= rhs
.number_matches
;
2097 rhs
.number_matches
= 0;
2100 /* See completer.h */
2103 completion_result::release_match_list ()
2105 char **ret
= match_list
;
2110 /* See completer.h */
2113 completion_result::sort_match_list ()
2115 if (number_matches
> 1)
2117 /* Element 0 is special (it's the common prefix), leave it
2119 std::sort (&match_list
[1],
2120 &match_list
[number_matches
+ 1],
2125 /* See completer.h */
2128 completion_result::reset_match_list ()
2130 if (match_list
!= NULL
)
2132 for (char **p
= match_list
; *p
!= NULL
; p
++)
2139 /* Helper for gdb_rl_attempted_completion_function, which does most of
2140 the work. This is called by readline to build the match list array
2141 and to determine the lowest common denominator. The real matches
2142 list starts at match[1], while match[0] is the slot holding
2143 readline's idea of the lowest common denominator of all matches,
2144 which is what readline replaces the completion "word" with.
2146 TEXT is the caller's idea of the "word" we are looking at, as
2147 computed in the handle_brkchars phase.
2149 START is the offset from RL_LINE_BUFFER where TEXT starts. END is
2150 the offset from RL_LINE_BUFFER where TEXT ends (i.e., where
2153 You should thus pretend that the line ends at END (relative to
2156 RL_LINE_BUFFER contains the entire text of the line. RL_POINT is
2157 the offset in that line of the cursor. You should pretend that the
2160 Returns NULL if there are no completions. */
2163 gdb_rl_attempted_completion_function_throw (const char *text
, int start
, int end
)
2165 /* Completers that provide a custom word point in the
2166 handle_brkchars phase also compute their completions then.
2167 Completers that leave the completion word handling to readline
2168 must be called twice. If rl_point (i.e., END) is at column 0,
2169 then readline skips the handle_brkchars phase, and so we create a
2170 tracker now in that case too. */
2171 if (end
== 0 || !current_completion
.tracker
->use_custom_word_point ())
2173 delete current_completion
.tracker
;
2174 current_completion
.tracker
= new completion_tracker ();
2176 complete_line (*current_completion
.tracker
, text
,
2177 rl_line_buffer
, rl_point
);
2180 completion_tracker
&tracker
= *current_completion
.tracker
;
2182 completion_result result
2183 = tracker
.build_completion_result (text
, start
, end
);
2185 rl_completion_suppress_append
= result
.completion_suppress_append
;
2186 return result
.release_match_list ();
2189 /* Function installed as "rl_attempted_completion_function" readline
2190 hook. Wrapper around gdb_rl_attempted_completion_function_throw
2191 that catches C++ exceptions, which can't cross readline. */
2194 gdb_rl_attempted_completion_function (const char *text
, int start
, int end
)
2196 /* Restore globals that might have been tweaked in
2197 gdb_completion_word_break_characters. */
2198 rl_basic_quote_characters
= gdb_org_rl_basic_quote_characters
;
2200 /* If we end up returning NULL, either on error, or simple because
2201 there are no matches, inhibit readline's default filename
2203 rl_attempted_completion_over
= 1;
2205 /* If the handle_brkchars phase was aborted, don't try
2207 if (current_completion
.aborted
)
2212 return gdb_rl_attempted_completion_function_throw (text
, start
, end
);
2214 CATCH (ex
, RETURN_MASK_ALL
)
2222 /* Skip over the possibly quoted word STR (as defined by the quote
2223 characters QUOTECHARS and the word break characters BREAKCHARS).
2224 Returns pointer to the location after the "word". If either
2225 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
2229 skip_quoted_chars (const char *str
, const char *quotechars
,
2230 const char *breakchars
)
2232 char quote_char
= '\0';
2235 if (quotechars
== NULL
)
2236 quotechars
= gdb_completer_quote_characters
;
2238 if (breakchars
== NULL
)
2239 breakchars
= current_language
->la_word_break_characters();
2241 for (scan
= str
; *scan
!= '\0'; scan
++)
2243 if (quote_char
!= '\0')
2245 /* Ignore everything until the matching close quote char. */
2246 if (*scan
== quote_char
)
2248 /* Found matching close quote. */
2253 else if (strchr (quotechars
, *scan
))
2255 /* Found start of a quoted string. */
2258 else if (strchr (breakchars
, *scan
))
2267 /* Skip over the possibly quoted word STR (as defined by the quote
2268 characters and word break characters used by the completer).
2269 Returns pointer to the location after the "word". */
2272 skip_quoted (const char *str
)
2274 return skip_quoted_chars (str
, NULL
, NULL
);
2277 /* Return a message indicating that the maximum number of completions
2278 has been reached and that there may be more. */
2281 get_max_completions_reached_message (void)
2283 return _("*** List may be truncated, max-completions reached. ***");
2286 /* GDB replacement for rl_display_match_list.
2287 Readline doesn't provide a clean interface for TUI(curses).
2288 A hack previously used was to send readline's rl_outstream through a pipe
2289 and read it from the event loop. Bleah. IWBN if readline abstracted
2290 away all the necessary bits, and this is what this code does. It
2291 replicates the parts of readline we need and then adds an abstraction
2292 layer, currently implemented as struct match_list_displayer, so that both
2293 CLI and TUI can use it. We copy all this readline code to minimize
2294 GDB-specific mods to readline. Once this code performs as desired then
2295 we can submit it to the readline maintainers.
2297 N.B. A lot of the code is the way it is in order to minimize differences
2298 from readline's copy. */
2300 /* Not supported here. */
2301 #undef VISIBLE_STATS
2303 #if defined (HANDLE_MULTIBYTE)
2304 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
2305 #define MB_NULLWCH(x) ((x) == 0)
2308 #define ELLIPSIS_LEN 3
2310 /* gdb version of readline/complete.c:get_y_or_n.
2311 'y' -> returns 1, and 'n' -> returns 0.
2312 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
2313 If FOR_PAGER is non-zero, then also supported are:
2314 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
2317 gdb_get_y_or_n (int for_pager
, const struct match_list_displayer
*displayer
)
2323 RL_SETSTATE (RL_STATE_MOREINPUT
);
2324 c
= displayer
->read_key (displayer
);
2325 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
2327 if (c
== 'y' || c
== 'Y' || c
== ' ')
2329 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
2331 if (c
== ABORT_CHAR
|| c
< 0)
2333 /* Readline doesn't erase_entire_line here, but without it the
2334 --More-- prompt isn't erased and neither is the text entered
2335 thus far redisplayed. */
2336 displayer
->erase_entire_line (displayer
);
2337 /* Note: The arguments to rl_abort are ignored. */
2340 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
2342 if (for_pager
&& (c
== 'q' || c
== 'Q'))
2344 displayer
->beep (displayer
);
2348 /* Pager function for tab-completion.
2349 This is based on readline/complete.c:_rl_internal_pager.
2350 LINES is the number of lines of output displayed thus far.
2352 -1 -> user pressed 'n' or equivalent,
2353 0 -> user pressed 'y' or equivalent,
2354 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
2357 gdb_display_match_list_pager (int lines
,
2358 const struct match_list_displayer
*displayer
)
2362 displayer
->puts (displayer
, "--More--");
2363 displayer
->flush (displayer
);
2364 i
= gdb_get_y_or_n (1, displayer
);
2365 displayer
->erase_entire_line (displayer
);
2374 /* Return non-zero if FILENAME is a directory.
2375 Based on readline/complete.c:path_isdir. */
2378 gdb_path_isdir (const char *filename
)
2382 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
2385 /* Return the portion of PATHNAME that should be output when listing
2386 possible completions. If we are hacking filename completion, we
2387 are only interested in the basename, the portion following the
2388 final slash. Otherwise, we return what we were passed. Since
2389 printing empty strings is not very informative, if we're doing
2390 filename completion, and the basename is the empty string, we look
2391 for the previous slash and return the portion following that. If
2392 there's no previous slash, we just return what we were passed.
2394 Based on readline/complete.c:printable_part. */
2397 gdb_printable_part (char *pathname
)
2401 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
2404 temp
= strrchr (pathname
, '/');
2405 #if defined (__MSDOS__)
2406 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
2407 temp
= pathname
+ 1;
2410 if (temp
== 0 || *temp
== '\0')
2412 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
2413 Look for a previous slash and, if one is found, return the portion
2414 following that slash. If there's no previous slash, just return the
2415 pathname we were passed. */
2416 else if (temp
[1] == '\0')
2418 for (x
= temp
- 1; x
> pathname
; x
--)
2421 return ((*x
== '/') ? x
+ 1 : pathname
);
2427 /* Compute width of STRING when displayed on screen by print_filename.
2428 Based on readline/complete.c:fnwidth. */
2431 gdb_fnwidth (const char *string
)
2434 #if defined (HANDLE_MULTIBYTE)
2440 left
= strlen (string
) + 1;
2441 memset (&ps
, 0, sizeof (mbstate_t));
2447 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
2454 #if defined (HANDLE_MULTIBYTE)
2455 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
2456 if (MB_INVALIDCH (clen
))
2460 memset (&ps
, 0, sizeof (mbstate_t));
2462 else if (MB_NULLWCH (clen
))
2468 width
+= (w
>= 0) ? w
: 1;
2480 /* Print TO_PRINT, one matching completion.
2481 PREFIX_BYTES is number of common prefix bytes.
2482 Based on readline/complete.c:fnprint. */
2485 gdb_fnprint (const char *to_print
, int prefix_bytes
,
2486 const struct match_list_displayer
*displayer
)
2490 #if defined (HANDLE_MULTIBYTE)
2497 end
= to_print
+ strlen (to_print
) + 1;
2498 memset (&ps
, 0, sizeof (mbstate_t));
2503 /* Don't print only the ellipsis if the common prefix is one of the
2504 possible completions */
2505 if (to_print
[prefix_bytes
] == '\0')
2512 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
2513 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
2514 displayer
->putch (displayer
, ellipsis
);
2515 printed_len
= ELLIPSIS_LEN
;
2518 s
= to_print
+ prefix_bytes
;
2523 displayer
->putch (displayer
, '^');
2524 displayer
->putch (displayer
, UNCTRL (*s
));
2527 #if defined (HANDLE_MULTIBYTE)
2528 memset (&ps
, 0, sizeof (mbstate_t));
2531 else if (*s
== RUBOUT
)
2533 displayer
->putch (displayer
, '^');
2534 displayer
->putch (displayer
, '?');
2537 #if defined (HANDLE_MULTIBYTE)
2538 memset (&ps
, 0, sizeof (mbstate_t));
2543 #if defined (HANDLE_MULTIBYTE)
2544 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
2545 if (MB_INVALIDCH (tlen
))
2549 memset (&ps
, 0, sizeof (mbstate_t));
2551 else if (MB_NULLWCH (tlen
))
2556 width
= (w
>= 0) ? w
: 1;
2558 for (w
= 0; w
< tlen
; ++w
)
2559 displayer
->putch (displayer
, s
[w
]);
2561 printed_len
+= width
;
2563 displayer
->putch (displayer
, *s
);
2573 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
2574 are using it, check for and output a single character for `special'
2575 filenames. Return the number of characters we output.
2576 Based on readline/complete.c:print_filename. */
2579 gdb_print_filename (char *to_print
, char *full_pathname
, int prefix_bytes
,
2580 const struct match_list_displayer
*displayer
)
2582 int printed_len
, extension_char
, slen
, tlen
;
2583 char *s
, c
, *new_full_pathname
;
2585 extern int _rl_complete_mark_directories
;
2588 printed_len
= gdb_fnprint (to_print
, prefix_bytes
, displayer
);
2590 #if defined (VISIBLE_STATS)
2591 if (rl_filename_completion_desired
&& (rl_visible_stats
|| _rl_complete_mark_directories
))
2593 if (rl_filename_completion_desired
&& _rl_complete_mark_directories
)
2596 /* If to_print != full_pathname, to_print is the basename of the
2597 path passed. In this case, we try to expand the directory
2598 name before checking for the stat character. */
2599 if (to_print
!= full_pathname
)
2601 /* Terminate the directory name. */
2603 to_print
[-1] = '\0';
2605 /* If setting the last slash in full_pathname to a NUL results in
2606 full_pathname being the empty string, we are trying to complete
2607 files in the root directory. If we pass a null string to the
2608 bash directory completion hook, for example, it will expand it
2609 to the current directory. We just want the `/'. */
2610 if (full_pathname
== 0 || *full_pathname
== 0)
2612 else if (full_pathname
[0] != '/')
2614 else if (full_pathname
[1] == 0)
2615 dn
= "//"; /* restore trailing slash to `//' */
2616 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
2617 dn
= "/"; /* don't turn /// into // */
2620 s
= tilde_expand (dn
);
2621 if (rl_directory_completion_hook
)
2622 (*rl_directory_completion_hook
) (&s
);
2625 tlen
= strlen (to_print
);
2626 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
2627 strcpy (new_full_pathname
, s
);
2628 if (s
[slen
- 1] == '/')
2631 new_full_pathname
[slen
] = '/';
2632 new_full_pathname
[slen
] = '/';
2633 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
2635 #if defined (VISIBLE_STATS)
2636 if (rl_visible_stats
)
2637 extension_char
= stat_char (new_full_pathname
);
2640 if (gdb_path_isdir (new_full_pathname
))
2641 extension_char
= '/';
2643 xfree (new_full_pathname
);
2648 s
= tilde_expand (full_pathname
);
2649 #if defined (VISIBLE_STATS)
2650 if (rl_visible_stats
)
2651 extension_char
= stat_char (s
);
2654 if (gdb_path_isdir (s
))
2655 extension_char
= '/';
2661 displayer
->putch (displayer
, extension_char
);
2669 /* GDB version of readline/complete.c:complete_get_screenwidth. */
2672 gdb_complete_get_screenwidth (const struct match_list_displayer
*displayer
)
2674 /* Readline has other stuff here which it's not clear we need. */
2675 return displayer
->width
;
2678 extern int _rl_completion_prefix_display_length
;
2679 extern int _rl_print_completions_horizontally
;
2681 EXTERN_C
int _rl_qsort_string_compare (const void *, const void *);
2682 typedef int QSFUNC (const void *, const void *);
2684 /* GDB version of readline/complete.c:rl_display_match_list.
2685 See gdb_display_match_list for a description of MATCHES, LEN, MAX.
2686 Returns non-zero if all matches are displayed. */
2689 gdb_display_match_list_1 (char **matches
, int len
, int max
,
2690 const struct match_list_displayer
*displayer
)
2692 int count
, limit
, printed_len
, lines
, cols
;
2693 int i
, j
, k
, l
, common_length
, sind
;
2695 int page_completions
= displayer
->height
!= INT_MAX
&& pagination_enabled
;
2697 /* Find the length of the prefix common to all items: length as displayed
2698 characters (common_length) and as a byte index into the matches (sind) */
2699 common_length
= sind
= 0;
2700 if (_rl_completion_prefix_display_length
> 0)
2702 t
= gdb_printable_part (matches
[0]);
2703 temp
= strrchr (t
, '/');
2704 common_length
= temp
? gdb_fnwidth (temp
) : gdb_fnwidth (t
);
2705 sind
= temp
? strlen (temp
) : strlen (t
);
2707 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
2708 max
-= common_length
- ELLIPSIS_LEN
;
2710 common_length
= sind
= 0;
2713 /* How many items of MAX length can we fit in the screen window? */
2714 cols
= gdb_complete_get_screenwidth (displayer
);
2717 if (limit
!= 1 && (limit
* max
== cols
))
2720 /* If cols == 0, limit will end up -1 */
2721 if (cols
< displayer
->width
&& limit
< 0)
2724 /* Avoid a possible floating exception. If max > cols,
2725 limit will be 0 and a divide-by-zero fault will result. */
2729 /* How many iterations of the printing loop? */
2730 count
= (len
+ (limit
- 1)) / limit
;
2732 /* Watch out for special case. If LEN is less than LIMIT, then
2733 just do the inner printing loop.
2734 0 < len <= limit implies count = 1. */
2736 /* Sort the items if they are not already sorted. */
2737 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
2738 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
2740 displayer
->crlf (displayer
);
2743 if (_rl_print_completions_horizontally
== 0)
2745 /* Print the sorted items, up-and-down alphabetically, like ls. */
2746 for (i
= 1; i
<= count
; i
++)
2748 for (j
= 0, l
= i
; j
< limit
; j
++)
2750 if (l
> len
|| matches
[l
] == 0)
2754 temp
= gdb_printable_part (matches
[l
]);
2755 printed_len
= gdb_print_filename (temp
, matches
[l
], sind
,
2759 for (k
= 0; k
< max
- printed_len
; k
++)
2760 displayer
->putch (displayer
, ' ');
2764 displayer
->crlf (displayer
);
2766 if (page_completions
&& lines
>= (displayer
->height
- 1) && i
< count
)
2768 lines
= gdb_display_match_list_pager (lines
, displayer
);
2776 /* Print the sorted items, across alphabetically, like ls -x. */
2777 for (i
= 1; matches
[i
]; i
++)
2779 temp
= gdb_printable_part (matches
[i
]);
2780 printed_len
= gdb_print_filename (temp
, matches
[i
], sind
, displayer
);
2781 /* Have we reached the end of this line? */
2784 if (i
&& (limit
> 1) && (i
% limit
) == 0)
2786 displayer
->crlf (displayer
);
2788 if (page_completions
&& lines
>= displayer
->height
- 1)
2790 lines
= gdb_display_match_list_pager (lines
, displayer
);
2796 for (k
= 0; k
< max
- printed_len
; k
++)
2797 displayer
->putch (displayer
, ' ');
2800 displayer
->crlf (displayer
);
2806 /* Utility for displaying completion list matches, used by both CLI and TUI.
2808 MATCHES is the list of strings, in argv format, LEN is the number of
2809 strings in MATCHES, and MAX is the length of the longest string in
2813 gdb_display_match_list (char **matches
, int len
, int max
,
2814 const struct match_list_displayer
*displayer
)
2816 /* Readline will never call this if complete_line returned NULL. */
2817 gdb_assert (max_completions
!= 0);
2819 /* complete_line will never return more than this. */
2820 if (max_completions
> 0)
2821 gdb_assert (len
<= max_completions
);
2823 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
2827 /* We can't use *query here because they wait for <RET> which is
2828 wrong here. This follows the readline version as closely as possible
2829 for compatibility's sake. See readline/complete.c. */
2831 displayer
->crlf (displayer
);
2833 xsnprintf (msg
, sizeof (msg
),
2834 "Display all %d possibilities? (y or n)", len
);
2835 displayer
->puts (displayer
, msg
);
2836 displayer
->flush (displayer
);
2838 if (gdb_get_y_or_n (0, displayer
) == 0)
2840 displayer
->crlf (displayer
);
2845 if (gdb_display_match_list_1 (matches
, len
, max
, displayer
))
2847 /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
2848 if (len
== max_completions
)
2850 /* The maximum number of completions has been reached. Warn the user
2851 that there may be more. */
2852 const char *message
= get_max_completions_reached_message ();
2854 displayer
->puts (displayer
, message
);
2855 displayer
->crlf (displayer
);
2861 _initialize_completer (void)
2863 add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class
,
2864 &max_completions
, _("\
2865 Set maximum number of completion candidates."), _("\
2866 Show maximum number of completion candidates."), _("\
2867 Use this to limit the number of candidates considered\n\
2868 during completion. Specifying \"unlimited\" or -1\n\
2869 disables limiting. Note that setting either no limit or\n\
2870 a very large limit can make completion slow."),
2871 NULL
, NULL
, &setlist
, &showlist
);