1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000-2020 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 "gdbsupport/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 /* See completer.h. */
50 class completion_tracker::completion_hash_entry
54 completion_hash_entry (gdb::unique_xmalloc_ptr
<char> name
,
55 gdb::unique_xmalloc_ptr
<char> lcd
)
56 : m_name (std::move (name
)),
57 m_lcd (std::move (lcd
))
62 /* Returns a pointer to the lowest common denominator string. This
63 string will only be valid while this hash entry is still valid as the
64 string continues to be owned by this hash entry and will be released
65 when this entry is deleted. */
66 char *get_lcd () const
71 /* Get, and release the name field from this hash entry. This can only
72 be called once, after which the name field is no longer valid. This
73 should be used to pass ownership of the name to someone else. */
76 return m_name
.release ();
79 /* Return true of the name in this hash entry is STR. */
80 bool is_name_eq (const char *str
) const
82 return strcmp (m_name
.get (), str
) == 0;
85 /* Return the hash value based on the name of the entry. */
86 hashval_t
hash_name () const
88 return htab_hash_string (m_name
.get ());
91 /* A static function that can be passed to the htab hash system to be
92 used as a callback that deletes an item from the hash. */
93 static void deleter (void *arg
)
95 completion_hash_entry
*entry
= (completion_hash_entry
*) arg
;
101 /* The symbol name stored in this hash entry. */
102 gdb::unique_xmalloc_ptr
<char> m_name
;
104 /* The lowest common denominator string computed for this hash entry. */
105 gdb::unique_xmalloc_ptr
<char> m_lcd
;
108 /* Misc state that needs to be tracked across several different
109 readline completer entry point calls, all related to a single
110 completion invocation. */
112 struct gdb_completer_state
114 /* The current completion's completion tracker. This is a global
115 because a tracker can be shared between the handle_brkchars and
116 handle_completion phases, which involves different readline
118 completion_tracker
*tracker
= NULL
;
120 /* Whether the current completion was aborted. */
121 bool aborted
= false;
124 /* The current completion state. */
125 static gdb_completer_state current_completion
;
127 /* An enumeration of the various things a user might attempt to
128 complete for a location. If you change this, remember to update
129 the explicit_options array below too. */
131 enum explicit_location_match_type
133 /* The filename of a source file. */
136 /* The name of a function or method. */
139 /* The fully-qualified name of a function or method. */
145 /* The name of a label. */
149 /* Prototypes for local functions. */
151 /* readline uses the word breaks for two things:
152 (1) In figuring out where to point the TEXT parameter to the
153 rl_completion_entry_function. Since we don't use TEXT for much,
154 it doesn't matter a lot what the word breaks are for this purpose,
155 but it does affect how much stuff M-? lists.
156 (2) If one of the matches contains a word break character, readline
157 will quote it. That's why we switch between
158 current_language->word_break_characters () and
159 gdb_completer_command_word_break_characters. I'm not sure when
160 we need this behavior (perhaps for funky characters in C++
163 /* Variables which are necessary for fancy command line editing. */
165 /* When completing on command names, we remove '-' and '.' from the list of
166 word break characters, since we use it in command names. If the
167 readline library sees one in any of the current completion strings,
168 it thinks that the string needs to be quoted and automatically
169 supplies a leading quote. */
170 static const char gdb_completer_command_word_break_characters
[] =
171 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/><,";
173 /* When completing on file names, we remove from the list of word
174 break characters any characters that are commonly used in file
175 names, such as '-', '+', '~', etc. Otherwise, readline displays
176 incorrect completion candidates. */
177 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
178 programs support @foo style response files. */
179 static const char gdb_completer_file_name_break_characters
[] =
180 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
186 /* Characters that can be used to quote completion strings. Note that
187 we can't include '"' because the gdb C parser treats such quoted
188 sequences as strings. */
189 static const char gdb_completer_quote_characters
[] = "'";
191 /* Accessor for some completer data that may interest other files. */
194 get_gdb_completer_quote_characters (void)
196 return gdb_completer_quote_characters
;
199 /* This can be used for functions which don't want to complete on
200 symbols but don't want to complete on anything else either. */
203 noop_completer (struct cmd_list_element
*ignore
,
204 completion_tracker
&tracker
,
205 const char *text
, const char *prefix
)
209 /* Complete on filenames. */
212 filename_completer (struct cmd_list_element
*ignore
,
213 completion_tracker
&tracker
,
214 const char *text
, const char *word
)
221 gdb::unique_xmalloc_ptr
<char> p_rl
222 (rl_filename_completion_function (text
, subsequent_name
));
225 /* We need to set subsequent_name to a non-zero value before the
226 continue line below, because otherwise, if the first file
227 seen by GDB is a backup file whose name ends in a `~', we
228 will loop indefinitely. */
230 /* Like emacs, don't complete on old versions. Especially
231 useful in the "source" command. */
232 const char *p
= p_rl
.get ();
233 if (p
[strlen (p
) - 1] == '~')
236 tracker
.add_completion
237 (make_completion_match_str (std::move (p_rl
), text
, word
));
240 /* There is no way to do this just long enough to affect quote
241 inserting without also affecting the next completion. This
242 should be fixed in readline. FIXME. */
243 /* Ensure that readline does the right thing
244 with respect to inserting quotes. */
245 rl_completer_word_break_characters
= "";
249 /* The corresponding completer_handle_brkchars
253 filename_completer_handle_brkchars (struct cmd_list_element
*ignore
,
254 completion_tracker
&tracker
,
255 const char *text
, const char *word
)
257 set_rl_completer_word_break_characters
258 (gdb_completer_file_name_break_characters
);
261 /* Possible values for the found_quote flags word used by the completion
262 functions. It says what kind of (shell-like) quoting we found anywhere
264 #define RL_QF_SINGLE_QUOTE 0x01
265 #define RL_QF_DOUBLE_QUOTE 0x02
266 #define RL_QF_BACKSLASH 0x04
267 #define RL_QF_OTHER_QUOTE 0x08
269 /* Find the bounds of the current word for completion purposes, and
270 return a pointer to the end of the word. This mimics (and is a
271 modified version of) readline's _rl_find_completion_word internal
274 This function skips quoted substrings (characters between matched
275 pairs of characters in rl_completer_quote_characters). We try to
276 find an unclosed quoted substring on which to do matching. If one
277 is not found, we use the word break characters to find the
278 boundaries of the current word. QC, if non-null, is set to the
279 opening quote character if we found an unclosed quoted substring,
280 '\0' otherwise. DP, if non-null, is set to the value of the
281 delimiter character that caused a word break. */
283 struct gdb_rl_completion_word_info
285 const char *word_break_characters
;
286 const char *quote_characters
;
287 const char *basic_quote_characters
;
291 gdb_rl_find_completion_word (struct gdb_rl_completion_word_info
*info
,
293 const char *line_buffer
)
295 int scan
, end
, found_quote
, delimiter
, pass_next
, isbrk
;
297 const char *brkchars
;
298 int point
= strlen (line_buffer
);
300 /* The algorithm below does '--point'. Avoid buffer underflow with
312 found_quote
= delimiter
= 0;
315 brkchars
= info
->word_break_characters
;
317 if (info
->quote_characters
!= NULL
)
319 /* We have a list of characters which can be used in pairs to
320 quote substrings for the completer. Try to find the start of
321 an unclosed quoted substring. */
322 /* FOUND_QUOTE is set so we know what kind of quotes we
324 for (scan
= pass_next
= 0;
334 /* Shell-like semantics for single quotes -- don't allow
335 backslash to quote anything in single quotes, especially
336 not the closing quote. If you don't like this, take out
337 the check on the value of quote_char. */
338 if (quote_char
!= '\'' && line_buffer
[scan
] == '\\')
341 found_quote
|= RL_QF_BACKSLASH
;
345 if (quote_char
!= '\0')
347 /* Ignore everything until the matching close quote
349 if (line_buffer
[scan
] == quote_char
)
351 /* Found matching close. Abandon this
357 else if (strchr (info
->quote_characters
, line_buffer
[scan
]))
359 /* Found start of a quoted substring. */
360 quote_char
= line_buffer
[scan
];
362 /* Shell-like quoting conventions. */
363 if (quote_char
== '\'')
364 found_quote
|= RL_QF_SINGLE_QUOTE
;
365 else if (quote_char
== '"')
366 found_quote
|= RL_QF_DOUBLE_QUOTE
;
368 found_quote
|= RL_QF_OTHER_QUOTE
;
373 if (point
== end
&& quote_char
== '\0')
375 /* We didn't find an unclosed quoted substring upon which to do
376 completion, so use the word break characters to find the
377 substring on which to complete. */
380 scan
= line_buffer
[point
];
382 if (strchr (brkchars
, scan
) != 0)
387 /* If we are at an unquoted word break, then advance past it. */
388 scan
= line_buffer
[point
];
392 isbrk
= strchr (brkchars
, scan
) != 0;
396 /* If the character that caused the word break was a quoting
397 character, then remember it as the delimiter. */
398 if (info
->basic_quote_characters
399 && strchr (info
->basic_quote_characters
, scan
)
400 && (end
- point
) > 1)
412 return line_buffer
+ point
;
415 /* Find the completion word point for TEXT, emulating the algorithm
416 readline uses to find the word point, using WORD_BREAK_CHARACTERS
417 as word break characters. */
420 advance_to_completion_word (completion_tracker
&tracker
,
421 const char *word_break_characters
,
424 gdb_rl_completion_word_info info
;
426 info
.word_break_characters
= word_break_characters
;
427 info
.quote_characters
= gdb_completer_quote_characters
;
428 info
.basic_quote_characters
= rl_basic_quote_characters
;
432 = gdb_rl_find_completion_word (&info
, NULL
, &delimiter
, text
);
434 tracker
.advance_custom_word_point_by (start
- text
);
438 tracker
.set_quote_char (delimiter
);
439 tracker
.set_suppress_append_ws (true);
445 /* See completer.h. */
448 advance_to_expression_complete_word_point (completion_tracker
&tracker
,
451 const char *brk_chars
= current_language
->word_break_characters ();
452 return advance_to_completion_word (tracker
, brk_chars
, text
);
455 /* See completer.h. */
458 advance_to_filename_complete_word_point (completion_tracker
&tracker
,
461 const char *brk_chars
= gdb_completer_file_name_break_characters
;
462 return advance_to_completion_word (tracker
, brk_chars
, text
);
465 /* See completer.h. */
468 completion_tracker::completes_to_completion_word (const char *word
)
470 recompute_lowest_common_denominator ();
471 if (m_lowest_common_denominator_unique
)
473 const char *lcd
= m_lowest_common_denominator
;
475 if (strncmp_iw (word
, lcd
, strlen (lcd
)) == 0)
477 /* Maybe skip the function and complete on keywords. */
478 size_t wordlen
= strlen (word
);
479 if (word
[wordlen
- 1] == ' ')
487 /* See completer.h. */
490 complete_nested_command_line (completion_tracker
&tracker
, const char *text
)
492 /* Must be called from a custom-word-point completer. */
493 gdb_assert (tracker
.use_custom_word_point ());
495 /* Disable the custom word point temporarily, because we want to
496 probe whether the command we're completing itself uses a custom
498 tracker
.set_use_custom_word_point (false);
499 size_t save_custom_word_point
= tracker
.custom_word_point ();
501 int quote_char
= '\0';
502 const char *word
= completion_find_completion_word (tracker
, text
,
505 if (tracker
.use_custom_word_point ())
507 /* The command we're completing uses a custom word point, so the
508 tracker already contains the matches. We're done. */
512 /* Restore the custom word point settings. */
513 tracker
.set_custom_word_point (save_custom_word_point
);
514 tracker
.set_use_custom_word_point (true);
516 /* Run the handle_completions completer phase. */
517 complete_line (tracker
, word
, text
, strlen (text
));
520 /* Complete on linespecs, which might be of two possible forms:
526 This is intended to be used in commands that set breakpoints
530 complete_files_symbols (completion_tracker
&tracker
,
531 const char *text
, const char *word
)
533 completion_list fn_list
;
536 int quoted
= *text
== '\'' || *text
== '"';
537 int quote_char
= '\0';
538 const char *colon
= NULL
;
539 char *file_to_match
= NULL
;
540 const char *symbol_start
= text
;
541 const char *orig_text
= text
;
543 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
544 for (p
= text
; *p
!= '\0'; ++p
)
546 if (*p
== '\\' && p
[1] == '\'')
548 else if (*p
== '\'' || *p
== '"')
552 while (*p
!= '\0' && *p
!= quote_found
)
554 if (*p
== '\\' && p
[1] == quote_found
)
559 if (*p
== quote_found
)
562 break; /* Hit the end of text. */
564 #if HAVE_DOS_BASED_FILE_SYSTEM
565 /* If we have a DOS-style absolute file name at the beginning of
566 TEXT, and the colon after the drive letter is the only colon
567 we found, pretend the colon is not there. */
568 else if (p
< text
+ 3 && *p
== ':' && p
== text
+ 1 + quoted
)
571 else if (*p
== ':' && !colon
)
574 symbol_start
= p
+ 1;
576 else if (strchr (current_language
->word_break_characters (), *p
))
577 symbol_start
= p
+ 1;
583 /* Where is the file name? */
588 file_to_match
= (char *) xmalloc (colon
- text
+ 1);
589 strncpy (file_to_match
, text
, colon
- text
);
590 file_to_match
[colon
- text
] = '\0';
591 /* Remove trailing colons and quotes from the file name. */
592 for (s
= file_to_match
+ (colon
- text
);
595 if (*s
== ':' || *s
== quote_char
)
598 /* If the text includes a colon, they want completion only on a
599 symbol name after the colon. Otherwise, we need to complete on
600 symbols as well as on files. */
603 collect_file_symbol_completion_matches (tracker
,
604 complete_symbol_mode::EXPRESSION
,
605 symbol_name_match_type::EXPRESSION
,
608 xfree (file_to_match
);
612 size_t text_len
= strlen (text
);
614 collect_symbol_completion_matches (tracker
,
615 complete_symbol_mode::EXPRESSION
,
616 symbol_name_match_type::EXPRESSION
,
618 /* If text includes characters which cannot appear in a file
619 name, they cannot be asking for completion on files. */
621 gdb_completer_file_name_break_characters
) == text_len
)
622 fn_list
= make_source_files_completion_list (text
, text
);
625 if (!fn_list
.empty () && !tracker
.have_completions ())
627 /* If we only have file names as possible completion, we should
628 bring them in sync with what rl_complete expects. The
629 problem is that if the user types "break /foo/b TAB", and the
630 possible completions are "/foo/bar" and "/foo/baz"
631 rl_complete expects us to return "bar" and "baz", without the
632 leading directories, as possible completions, because `word'
633 starts at the "b". But we ignore the value of `word' when we
634 call make_source_files_completion_list above (because that
635 would not DTRT when the completion results in both symbols
636 and file names), so make_source_files_completion_list returns
637 the full "/foo/bar" and "/foo/baz" strings. This produces
638 wrong results when, e.g., there's only one possible
639 completion, because rl_complete will prepend "/foo/" to each
640 candidate completion. The loop below removes that leading
642 for (const auto &fn_up
: fn_list
)
644 char *fn
= fn_up
.get ();
645 memmove (fn
, fn
+ (word
- text
), strlen (fn
) + 1 - (word
- text
));
649 tracker
.add_completions (std::move (fn_list
));
651 if (!tracker
.have_completions ())
653 /* No completions at all. As the final resort, try completing
654 on the entire text as a symbol. */
655 collect_symbol_completion_matches (tracker
,
656 complete_symbol_mode::EXPRESSION
,
657 symbol_name_match_type::EXPRESSION
,
662 /* See completer.h. */
665 complete_source_filenames (const char *text
)
667 size_t text_len
= strlen (text
);
669 /* If text includes characters which cannot appear in a file name,
670 the user cannot be asking for completion on files. */
672 gdb_completer_file_name_break_characters
)
674 return make_source_files_completion_list (text
, text
);
679 /* Complete address and linespec locations. */
682 complete_address_and_linespec_locations (completion_tracker
&tracker
,
684 symbol_name_match_type match_type
)
688 tracker
.advance_custom_word_point_by (1);
691 = advance_to_expression_complete_word_point (tracker
, text
);
692 complete_expression (tracker
, text
, word
);
696 linespec_complete (tracker
, text
, match_type
);
700 /* The explicit location options. Note that indexes into this array
701 must match the explicit_location_match_type enumerators. */
703 static const char *const explicit_options
[] =
713 /* The probe modifier options. These can appear before a location in
714 breakpoint commands. */
715 static const char *const probe_options
[] =
723 /* Returns STRING if not NULL, the empty string otherwise. */
726 string_or_empty (const char *string
)
728 return string
!= NULL
? string
: "";
731 /* A helper function to collect explicit location matches for the given
732 LOCATION, which is attempting to match on WORD. */
735 collect_explicit_location_matches (completion_tracker
&tracker
,
736 struct event_location
*location
,
737 enum explicit_location_match_type what
,
739 const struct language_defn
*language
)
741 const struct explicit_location
*explicit_loc
742 = get_explicit_location (location
);
744 /* True if the option expects an argument. */
745 bool needs_arg
= true;
747 /* Note, in the various MATCH_* below, we complete on
748 explicit_loc->foo instead of WORD, because only the former will
749 have already skipped past any quote char. */
754 const char *source
= string_or_empty (explicit_loc
->source_filename
);
755 completion_list matches
756 = make_source_files_completion_list (source
, source
);
757 tracker
.add_completions (std::move (matches
));
763 const char *function
= string_or_empty (explicit_loc
->function_name
);
764 linespec_complete_function (tracker
, function
,
765 explicit_loc
->func_name_match_type
,
766 explicit_loc
->source_filename
);
770 case MATCH_QUALIFIED
:
774 /* Nothing to offer. */
779 const char *label
= string_or_empty (explicit_loc
->label_name
);
780 linespec_complete_label (tracker
, language
,
781 explicit_loc
->source_filename
,
782 explicit_loc
->function_name
,
783 explicit_loc
->func_name_match_type
,
789 gdb_assert_not_reached ("unhandled explicit_location_match_type");
792 if (!needs_arg
|| tracker
.completes_to_completion_word (word
))
794 tracker
.discard_completions ();
795 tracker
.advance_custom_word_point_by (strlen (word
));
796 complete_on_enum (tracker
, explicit_options
, "", "");
797 complete_on_enum (tracker
, linespec_keywords
, "", "");
799 else if (!tracker
.have_completions ())
801 /* Maybe we have an unterminated linespec keyword at the tail of
802 the string. Try completing on that. */
803 size_t wordlen
= strlen (word
);
804 const char *keyword
= word
+ wordlen
;
806 if (wordlen
> 0 && keyword
[-1] != ' ')
808 while (keyword
> word
&& *keyword
!= ' ')
810 /* Don't complete on keywords if we'd be completing on the
811 whole explicit linespec option. E.g., "b -function
812 thr<tab>" should not complete to the "thread"
816 keyword
= skip_spaces (keyword
);
818 tracker
.advance_custom_word_point_by (keyword
- word
);
819 complete_on_enum (tracker
, linespec_keywords
, keyword
, keyword
);
822 else if (wordlen
> 0 && keyword
[-1] == ' ')
824 /* Assume that we're maybe past the explicit location
825 argument, and we didn't manage to find any match because
826 the user wants to create a pending breakpoint. Offer the
827 keyword and explicit location options as possible
829 tracker
.advance_custom_word_point_by (keyword
- word
);
830 complete_on_enum (tracker
, linespec_keywords
, keyword
, keyword
);
831 complete_on_enum (tracker
, explicit_options
, keyword
, keyword
);
836 /* If the next word in *TEXT_P is any of the keywords in KEYWORDS,
837 then advance both TEXT_P and the word point in the tracker past the
838 keyword and return the (0-based) index in the KEYWORDS array that
839 matched. Otherwise, return -1. */
842 skip_keyword (completion_tracker
&tracker
,
843 const char * const *keywords
, const char **text_p
)
845 const char *text
= *text_p
;
846 const char *after
= skip_to_space (text
);
847 size_t len
= after
- text
;
849 if (text
[len
] != ' ')
853 for (int i
= 0; keywords
[i
] != NULL
; i
++)
855 if (strncmp (keywords
[i
], text
, len
) == 0)
866 tracker
.advance_custom_word_point_by (len
+ 1);
875 /* A completer function for explicit locations. This function
876 completes both options ("-source", "-line", etc) and values. If
877 completing a quoted string, then QUOTED_ARG_START and
878 QUOTED_ARG_END point to the quote characters. LANGUAGE is the
882 complete_explicit_location (completion_tracker
&tracker
,
883 struct event_location
*location
,
885 const language_defn
*language
,
886 const char *quoted_arg_start
,
887 const char *quoted_arg_end
)
892 int keyword
= skip_keyword (tracker
, explicit_options
, &text
);
895 complete_on_enum (tracker
, explicit_options
, text
, text
);
898 /* Completing on value. */
899 enum explicit_location_match_type what
900 = (explicit_location_match_type
) keyword
;
902 if (quoted_arg_start
!= NULL
&& quoted_arg_end
!= NULL
)
904 if (quoted_arg_end
[1] == '\0')
906 /* If completing a quoted string with the cursor right
907 at the terminating quote char, complete the
908 completion word without interpretation, so that
909 readline advances the cursor one whitespace past the
910 quote, even if there's no match. This makes these
911 cases behave the same:
913 before: "b -function function()"
914 after: "b -function function() "
916 before: "b -function 'function()'"
917 after: "b -function 'function()' "
919 and trusts the user in this case:
921 before: "b -function 'not_loaded_function_yet()'"
922 after: "b -function 'not_loaded_function_yet()' "
924 tracker
.add_completion (make_unique_xstrdup (text
));
926 else if (quoted_arg_end
[1] == ' ')
928 /* We're maybe past the explicit location argument.
929 Skip the argument without interpretation, assuming the
930 user may want to create pending breakpoint. Offer
931 the keyword and explicit location options as possible
933 tracker
.advance_custom_word_point_by (strlen (text
));
934 complete_on_enum (tracker
, linespec_keywords
, "", "");
935 complete_on_enum (tracker
, explicit_options
, "", "");
940 /* Now gather matches */
941 collect_explicit_location_matches (tracker
, location
, what
, text
,
946 /* A completer for locations. */
949 location_completer (struct cmd_list_element
*ignore
,
950 completion_tracker
&tracker
,
951 const char *text
, const char * /* word */)
953 int found_probe_option
= -1;
955 /* If we have a probe modifier, skip it. This can only appear as
956 first argument. Until we have a specific completer for probes,
957 falling back to the linespec completer for the remainder of the
958 line is better than nothing. */
959 if (text
[0] == '-' && text
[1] == 'p')
960 found_probe_option
= skip_keyword (tracker
, probe_options
, &text
);
962 const char *option_text
= text
;
963 int saved_word_point
= tracker
.custom_word_point ();
965 const char *copy
= text
;
967 explicit_completion_info completion_info
;
968 event_location_up location
969 = string_to_explicit_location (©
, current_language
,
971 if (completion_info
.quoted_arg_start
!= NULL
972 && completion_info
.quoted_arg_end
== NULL
)
974 /* Found an unbalanced quote. */
975 tracker
.set_quote_char (*completion_info
.quoted_arg_start
);
976 tracker
.advance_custom_word_point_by (1);
979 if (completion_info
.saw_explicit_location_option
)
983 tracker
.advance_custom_word_point_by (copy
- text
);
986 /* We found a terminator at the tail end of the string,
987 which means we're past the explicit location options. We
988 may have a keyword to complete on. If we have a whole
989 keyword, then complete whatever comes after as an
990 expression. This is mainly for the "if" keyword. If the
991 "thread" and "task" keywords gain their own completers,
992 they should be used here. */
993 int keyword
= skip_keyword (tracker
, linespec_keywords
, &text
);
997 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
1002 = advance_to_expression_complete_word_point (tracker
, text
);
1003 complete_expression (tracker
, text
, word
);
1008 tracker
.advance_custom_word_point_by (completion_info
.last_option
1010 text
= completion_info
.last_option
;
1012 complete_explicit_location (tracker
, location
.get (), text
,
1014 completion_info
.quoted_arg_start
,
1015 completion_info
.quoted_arg_end
);
1019 /* This is an address or linespec location. */
1020 else if (location
!= NULL
)
1022 /* Handle non-explicit location options. */
1024 int keyword
= skip_keyword (tracker
, explicit_options
, &text
);
1026 complete_on_enum (tracker
, explicit_options
, text
, text
);
1029 tracker
.advance_custom_word_point_by (copy
- text
);
1032 symbol_name_match_type match_type
1033 = get_explicit_location (location
.get ())->func_name_match_type
;
1034 complete_address_and_linespec_locations (tracker
, text
, match_type
);
1040 complete_address_and_linespec_locations (tracker
, text
,
1041 symbol_name_match_type::WILD
);
1044 /* Add matches for option names, if either:
1046 - Some completer above found some matches, but the word point did
1047 not advance (e.g., "b <tab>" finds all functions, or "b -<tab>"
1048 matches all objc selectors), or;
1050 - Some completer above advanced the word point, but found no
1053 if ((text
[0] == '-' || text
[0] == '\0')
1054 && (!tracker
.have_completions ()
1055 || tracker
.custom_word_point () == saved_word_point
))
1057 tracker
.set_custom_word_point (saved_word_point
);
1060 if (found_probe_option
== -1)
1061 complete_on_enum (tracker
, probe_options
, text
, text
);
1062 complete_on_enum (tracker
, explicit_options
, text
, text
);
1066 /* The corresponding completer_handle_brkchars
1070 location_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1071 completion_tracker
&tracker
,
1073 const char *word_ignored
)
1075 tracker
.set_use_custom_word_point (true);
1077 location_completer (ignore
, tracker
, text
, NULL
);
1080 /* Helper for expression_completer which recursively adds field and
1081 method names from TYPE, a struct or union type, to the OUTPUT
1085 add_struct_fields (struct type
*type
, completion_list
&output
,
1086 const char *fieldname
, int namelen
)
1089 int computed_type_name
= 0;
1090 const char *type_name
= NULL
;
1092 type
= check_typedef (type
);
1093 for (i
= 0; i
< type
->num_fields (); ++i
)
1095 if (i
< TYPE_N_BASECLASSES (type
))
1096 add_struct_fields (TYPE_BASECLASS (type
, i
),
1097 output
, fieldname
, namelen
);
1098 else if (TYPE_FIELD_NAME (type
, i
))
1100 if (TYPE_FIELD_NAME (type
, i
)[0] != '\0')
1102 if (! strncmp (TYPE_FIELD_NAME (type
, i
),
1103 fieldname
, namelen
))
1104 output
.emplace_back (xstrdup (TYPE_FIELD_NAME (type
, i
)));
1106 else if (type
->field (i
).type ()->code () == TYPE_CODE_UNION
)
1108 /* Recurse into anonymous unions. */
1109 add_struct_fields (type
->field (i
).type (),
1110 output
, fieldname
, namelen
);
1115 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1117 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1119 if (name
&& ! strncmp (name
, fieldname
, namelen
))
1121 if (!computed_type_name
)
1123 type_name
= type
->name ();
1124 computed_type_name
= 1;
1126 /* Omit constructors from the completion list. */
1127 if (!type_name
|| strcmp (type_name
, name
))
1128 output
.emplace_back (xstrdup (name
));
1133 /* See completer.h. */
1136 complete_expression (completion_tracker
&tracker
,
1137 const char *text
, const char *word
)
1139 struct type
*type
= NULL
;
1140 gdb::unique_xmalloc_ptr
<char> fieldname
;
1141 enum type_code code
= TYPE_CODE_UNDEF
;
1143 /* Perform a tentative parse of the expression, to see whether a
1144 field completion is required. */
1147 type
= parse_expression_for_completion (text
, &fieldname
, &code
);
1149 catch (const gdb_exception_error
&except
)
1154 if (fieldname
!= nullptr && type
)
1158 type
= check_typedef (type
);
1159 if (type
->code () != TYPE_CODE_PTR
&& !TYPE_IS_REFERENCE (type
))
1161 type
= TYPE_TARGET_TYPE (type
);
1164 if (type
->code () == TYPE_CODE_UNION
1165 || type
->code () == TYPE_CODE_STRUCT
)
1167 completion_list result
;
1169 add_struct_fields (type
, result
, fieldname
.get (),
1170 strlen (fieldname
.get ()));
1171 tracker
.add_completions (std::move (result
));
1175 else if (fieldname
!= nullptr && code
!= TYPE_CODE_UNDEF
)
1177 collect_symbol_completion_matches_type (tracker
, fieldname
.get (),
1178 fieldname
.get (), code
);
1182 complete_files_symbols (tracker
, text
, word
);
1185 /* Complete on expressions. Often this means completing on symbol
1186 names, but some language parsers also have support for completing
1190 expression_completer (struct cmd_list_element
*ignore
,
1191 completion_tracker
&tracker
,
1192 const char *text
, const char *word
)
1194 complete_expression (tracker
, text
, word
);
1197 /* See definition in completer.h. */
1200 set_rl_completer_word_break_characters (const char *break_chars
)
1202 rl_completer_word_break_characters
= (char *) break_chars
;
1205 /* Complete on symbols. */
1208 symbol_completer (struct cmd_list_element
*ignore
,
1209 completion_tracker
&tracker
,
1210 const char *text
, const char *word
)
1212 collect_symbol_completion_matches (tracker
, complete_symbol_mode::EXPRESSION
,
1213 symbol_name_match_type::EXPRESSION
,
1217 /* Here are some useful test cases for completion. FIXME: These
1218 should be put in the test suite. They should be tested with both
1221 "show output-" "radix"
1222 "show output" "-radix"
1223 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1224 "p " ambiguous (all symbols)
1225 "info t foo" no completions
1226 "info t " no completions
1227 "info t" ambiguous ("info target", "info terminal", etc.)
1228 "info ajksdlfk" no completions
1229 "info ajksdlfk " no completions
1231 "info " ambiguous (all info commands)
1232 "p \"a" no completions (string constant)
1233 "p 'a" ambiguous (all symbols starting with a)
1234 "p b-a" ambiguous (all symbols starting with a)
1235 "p b-" ambiguous (all symbols)
1236 "file Make" "file" (word break hard to screw up here)
1237 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1240 enum complete_line_internal_reason
1242 /* Preliminary phase, called by gdb_completion_word_break_characters
1243 function, is used to either:
1245 #1 - Determine the set of chars that are word delimiters
1246 depending on the current command in line_buffer.
1248 #2 - Manually advance RL_POINT to the "word break" point instead
1249 of letting readline do it (based on too-simple character
1252 Simpler completers that just pass a brkchars array to readline
1253 (#1 above) must defer generating the completions to the main
1254 phase (below). No completion list should be generated in this
1257 OTOH, completers that manually advance the word point(#2 above)
1258 must set "use_custom_word_point" in the tracker and generate
1259 their completion in this phase. Note that this is the convenient
1260 thing to do since they'll be parsing the input line anyway. */
1263 /* Main phase, called by complete_line function, is used to get the
1264 list of possible completions. */
1267 /* Special case when completing a 'help' command. In this case,
1268 once sub-command completions are exhausted, we simply return
1273 /* Helper for complete_line_internal to simplify it. */
1276 complete_line_internal_normal_command (completion_tracker
&tracker
,
1277 const char *command
, const char *word
,
1278 const char *cmd_args
,
1279 complete_line_internal_reason reason
,
1280 struct cmd_list_element
*c
)
1282 const char *p
= cmd_args
;
1284 if (c
->completer
== filename_completer
)
1286 /* Many commands which want to complete on file names accept
1287 several file names, as in "run foo bar >>baz". So we don't
1288 want to complete the entire text after the command, just the
1289 last word. To this end, we need to find the beginning of the
1290 file name by starting at `word' and going backwards. */
1293 && strchr (gdb_completer_file_name_break_characters
,
1299 if (reason
== handle_brkchars
)
1301 completer_handle_brkchars_ftype
*brkchars_fn
;
1303 if (c
->completer_handle_brkchars
!= NULL
)
1304 brkchars_fn
= c
->completer_handle_brkchars
;
1308 = (completer_handle_brkchars_func_for_completer
1312 brkchars_fn (c
, tracker
, p
, word
);
1315 if (reason
!= handle_brkchars
&& c
->completer
!= NULL
)
1316 (*c
->completer
) (c
, tracker
, p
, word
);
1319 /* Internal function used to handle completions.
1322 TEXT is the caller's idea of the "word" we are looking at.
1324 LINE_BUFFER is available to be looked at; it contains the entire
1325 text of the line. POINT is the offset in that line of the cursor.
1326 You should pretend that the line ends at POINT.
1328 See complete_line_internal_reason for description of REASON. */
1331 complete_line_internal_1 (completion_tracker
&tracker
,
1333 const char *line_buffer
, int point
,
1334 complete_line_internal_reason reason
)
1338 int ignore_help_classes
;
1339 /* Pointer within tmp_command which corresponds to text. */
1341 struct cmd_list_element
*c
, *result_list
;
1343 /* Choose the default set of word break characters to break
1344 completions. If we later find out that we are doing completions
1345 on command strings (as opposed to strings supplied by the
1346 individual command completer functions, which can be any string)
1347 then we will switch to the special word break set for command
1348 strings, which leaves out the '-' and '.' character used in some
1350 set_rl_completer_word_break_characters
1351 (current_language
->word_break_characters ());
1353 /* Decide whether to complete on a list of gdb commands or on
1355 tmp_command
= (char *) alloca (point
+ 1);
1358 /* The help command should complete help aliases. */
1359 ignore_help_classes
= reason
!= handle_help
;
1361 strncpy (tmp_command
, line_buffer
, point
);
1362 tmp_command
[point
] = '\0';
1363 if (reason
== handle_brkchars
)
1365 gdb_assert (text
== NULL
);
1370 /* Since text always contains some number of characters leading up
1371 to point, we can find the equivalent position in tmp_command
1372 by subtracting that many characters from the end of tmp_command. */
1373 word
= tmp_command
+ point
- strlen (text
);
1376 /* Move P up to the start of the command. */
1377 p
= skip_spaces (p
);
1381 /* An empty line is ambiguous; that is, it could be any
1383 c
= CMD_LIST_AMBIGUOUS
;
1388 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, NULL
, ignore_help_classes
);
1391 /* Move p up to the next interesting thing. */
1392 while (*p
== ' ' || *p
== '\t')
1397 tracker
.advance_custom_word_point_by (p
- tmp_command
);
1401 /* It is an unrecognized command. So there are no
1402 possible completions. */
1404 else if (c
== CMD_LIST_AMBIGUOUS
)
1408 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1409 doesn't advance over that thing itself. Do so now. */
1411 while (valid_cmd_char_p (*q
))
1413 if (q
!= tmp_command
+ point
)
1415 /* There is something beyond the ambiguous
1416 command, so there are no possible completions. For
1417 example, "info t " or "info t foo" does not complete
1418 to anything, because "info t" can be "info target" or
1423 /* We're trying to complete on the command which was ambiguous.
1424 This we can deal with. */
1427 if (reason
!= handle_brkchars
)
1428 complete_on_cmdlist (*result_list
->prefixlist
, tracker
, p
,
1429 word
, ignore_help_classes
);
1433 if (reason
!= handle_brkchars
)
1434 complete_on_cmdlist (cmdlist
, tracker
, p
, word
,
1435 ignore_help_classes
);
1437 /* Ensure that readline does the right thing with respect to
1438 inserting quotes. */
1439 set_rl_completer_word_break_characters
1440 (gdb_completer_command_word_break_characters
);
1445 /* We've recognized a full command. */
1447 if (p
== tmp_command
+ point
)
1449 /* There is no non-whitespace in the line beyond the
1452 if (p
[-1] == ' ' || p
[-1] == '\t')
1454 /* The command is followed by whitespace; we need to
1455 complete on whatever comes after command. */
1458 /* It is a prefix command; what comes after it is
1459 a subcommand (e.g. "info "). */
1460 if (reason
!= handle_brkchars
)
1461 complete_on_cmdlist (*c
->prefixlist
, tracker
, p
, word
,
1462 ignore_help_classes
);
1464 /* Ensure that readline does the right thing
1465 with respect to inserting quotes. */
1466 set_rl_completer_word_break_characters
1467 (gdb_completer_command_word_break_characters
);
1469 else if (reason
== handle_help
)
1473 if (reason
!= handle_brkchars
)
1474 complete_on_enum (tracker
, c
->enums
, p
, word
);
1475 set_rl_completer_word_break_characters
1476 (gdb_completer_command_word_break_characters
);
1480 /* It is a normal command; what comes after it is
1481 completed by the command's completer function. */
1482 complete_line_internal_normal_command (tracker
,
1483 tmp_command
, word
, p
,
1489 /* The command is not followed by whitespace; we need to
1490 complete on the command itself, e.g. "p" which is a
1491 command itself but also can complete to "print", "ptype"
1495 /* Find the command we are completing on. */
1497 while (q
> tmp_command
)
1499 if (valid_cmd_char_p (q
[-1]))
1505 /* Move the custom word point back too. */
1506 tracker
.advance_custom_word_point_by (q
- p
);
1508 if (reason
!= handle_brkchars
)
1509 complete_on_cmdlist (result_list
, tracker
, q
, word
,
1510 ignore_help_classes
);
1512 /* Ensure that readline does the right thing
1513 with respect to inserting quotes. */
1514 set_rl_completer_word_break_characters
1515 (gdb_completer_command_word_break_characters
);
1518 else if (reason
== handle_help
)
1522 /* There is non-whitespace beyond the command. */
1524 if (c
->prefixlist
&& !c
->allow_unknown
)
1526 /* It is an unrecognized subcommand of a prefix command,
1527 e.g. "info adsfkdj". */
1531 if (reason
!= handle_brkchars
)
1532 complete_on_enum (tracker
, c
->enums
, p
, word
);
1536 /* It is a normal command. */
1537 complete_line_internal_normal_command (tracker
,
1538 tmp_command
, word
, p
,
1545 /* Wrapper around complete_line_internal_1 to handle
1546 MAX_COMPLETIONS_REACHED_ERROR. */
1549 complete_line_internal (completion_tracker
&tracker
,
1551 const char *line_buffer
, int point
,
1552 complete_line_internal_reason reason
)
1556 complete_line_internal_1 (tracker
, text
, line_buffer
, point
, reason
);
1558 catch (const gdb_exception_error
&except
)
1560 if (except
.error
!= MAX_COMPLETIONS_REACHED_ERROR
)
1565 /* See completer.h. */
1567 int max_completions
= 200;
1569 /* Initial size of the table. It automagically grows from here. */
1570 #define INITIAL_COMPLETION_HTAB_SIZE 200
1572 /* See completer.h. */
1574 completion_tracker::completion_tracker ()
1576 discard_completions ();
1579 /* See completer.h. */
1582 completion_tracker::discard_completions ()
1584 xfree (m_lowest_common_denominator
);
1585 m_lowest_common_denominator
= NULL
;
1587 m_lowest_common_denominator_unique
= false;
1588 m_lowest_common_denominator_valid
= false;
1590 m_entries_hash
.reset (nullptr);
1592 /* A callback used by the hash table to compare new entries with existing
1593 entries. We can't use the standard streq_hash function here as the
1594 key to our hash is just a single string, while the values we store in
1595 the hash are a struct containing multiple strings. */
1596 static auto entry_eq_func
1597 = [] (const void *first
, const void *second
) -> int
1599 /* The FIRST argument is the entry already in the hash table, and
1600 the SECOND argument is the new item being inserted. */
1601 const completion_hash_entry
*entry
1602 = (const completion_hash_entry
*) first
;
1603 const char *name_str
= (const char *) second
;
1605 return entry
->is_name_eq (name_str
);
1608 /* Callback used by the hash table to compute the hash value for an
1609 existing entry. This is needed when expanding the hash table. */
1610 static auto entry_hash_func
1611 = [] (const void *arg
) -> hashval_t
1613 const completion_hash_entry
*entry
1614 = (const completion_hash_entry
*) arg
;
1615 return entry
->hash_name ();
1618 m_entries_hash
.reset (htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE
,
1619 entry_hash_func
, entry_eq_func
,
1620 completion_hash_entry::deleter
,
1624 /* See completer.h. */
1626 completion_tracker::~completion_tracker ()
1628 xfree (m_lowest_common_denominator
);
1631 /* See completer.h. */
1634 completion_tracker::maybe_add_completion
1635 (gdb::unique_xmalloc_ptr
<char> name
,
1636 completion_match_for_lcd
*match_for_lcd
,
1637 const char *text
, const char *word
)
1641 if (max_completions
== 0)
1644 if (htab_elements (m_entries_hash
.get ()) >= max_completions
)
1647 hashval_t hash
= htab_hash_string (name
.get ());
1648 slot
= htab_find_slot_with_hash (m_entries_hash
.get (), name
.get (),
1650 if (*slot
== HTAB_EMPTY_ENTRY
)
1652 const char *match_for_lcd_str
= NULL
;
1654 if (match_for_lcd
!= NULL
)
1655 match_for_lcd_str
= match_for_lcd
->finish ();
1657 if (match_for_lcd_str
== NULL
)
1658 match_for_lcd_str
= name
.get ();
1660 gdb::unique_xmalloc_ptr
<char> lcd
1661 = make_completion_match_str (match_for_lcd_str
, text
, word
);
1663 size_t lcd_len
= strlen (lcd
.get ());
1664 *slot
= new completion_hash_entry (std::move (name
), std::move (lcd
));
1666 m_lowest_common_denominator_valid
= false;
1667 m_lowest_common_denominator_max_length
1668 = std::max (m_lowest_common_denominator_max_length
, lcd_len
);
1674 /* See completer.h. */
1677 completion_tracker::add_completion (gdb::unique_xmalloc_ptr
<char> name
,
1678 completion_match_for_lcd
*match_for_lcd
,
1679 const char *text
, const char *word
)
1681 if (!maybe_add_completion (std::move (name
), match_for_lcd
, text
, word
))
1682 throw_error (MAX_COMPLETIONS_REACHED_ERROR
, _("Max completions reached."));
1685 /* See completer.h. */
1688 completion_tracker::add_completions (completion_list
&&list
)
1690 for (auto &candidate
: list
)
1691 add_completion (std::move (candidate
));
1694 /* See completer.h. */
1697 completion_tracker::remove_completion (const char *name
)
1699 hashval_t hash
= htab_hash_string (name
);
1700 if (htab_find_slot_with_hash (m_entries_hash
.get (), name
, hash
, NO_INSERT
)
1703 htab_remove_elt_with_hash (m_entries_hash
.get (), name
, hash
);
1704 m_lowest_common_denominator_valid
= false;
1708 /* Helper for the make_completion_match_str overloads. Returns NULL
1709 as an indication that we want MATCH_NAME exactly. It is up to the
1710 caller to xstrdup that string if desired. */
1713 make_completion_match_str_1 (const char *match_name
,
1714 const char *text
, const char *word
)
1720 /* Return NULL as an indication that we want MATCH_NAME
1724 else if (word
> text
)
1726 /* Return some portion of MATCH_NAME. */
1727 newobj
= xstrdup (match_name
+ (word
- text
));
1731 /* Return some of WORD plus MATCH_NAME. */
1732 size_t len
= strlen (match_name
);
1733 newobj
= (char *) xmalloc (text
- word
+ len
+ 1);
1734 memcpy (newobj
, word
, text
- word
);
1735 memcpy (newobj
+ (text
- word
), match_name
, len
+ 1);
1741 /* See completer.h. */
1743 gdb::unique_xmalloc_ptr
<char>
1744 make_completion_match_str (const char *match_name
,
1745 const char *text
, const char *word
)
1747 char *newobj
= make_completion_match_str_1 (match_name
, text
, word
);
1749 newobj
= xstrdup (match_name
);
1750 return gdb::unique_xmalloc_ptr
<char> (newobj
);
1753 /* See completer.h. */
1755 gdb::unique_xmalloc_ptr
<char>
1756 make_completion_match_str (gdb::unique_xmalloc_ptr
<char> &&match_name
,
1757 const char *text
, const char *word
)
1759 char *newobj
= make_completion_match_str_1 (match_name
.get (), text
, word
);
1761 return std::move (match_name
);
1762 return gdb::unique_xmalloc_ptr
<char> (newobj
);
1765 /* See complete.h. */
1768 complete (const char *line
, char const **word
, int *quote_char
)
1770 completion_tracker tracker_handle_brkchars
;
1771 completion_tracker tracker_handle_completions
;
1772 completion_tracker
*tracker
;
1774 /* The WORD should be set to the end of word to complete. We initialize
1775 to the completion point which is assumed to be at the end of LINE.
1776 This leaves WORD to be initialized to a sensible value in cases
1777 completion_find_completion_word() fails i.e., throws an exception.
1779 *word
= line
+ strlen (line
);
1783 *word
= completion_find_completion_word (tracker_handle_brkchars
,
1786 /* Completers that provide a custom word point in the
1787 handle_brkchars phase also compute their completions then.
1788 Completers that leave the completion word handling to readline
1789 must be called twice. */
1790 if (tracker_handle_brkchars
.use_custom_word_point ())
1791 tracker
= &tracker_handle_brkchars
;
1794 complete_line (tracker_handle_completions
, *word
, line
, strlen (line
));
1795 tracker
= &tracker_handle_completions
;
1798 catch (const gdb_exception
&ex
)
1803 return tracker
->build_completion_result (*word
, *word
- line
, strlen (line
));
1807 /* Generate completions all at once. Does nothing if max_completions
1808 is 0. If max_completions is non-negative, this will collect at
1809 most max_completions strings.
1811 TEXT is the caller's idea of the "word" we are looking at.
1813 LINE_BUFFER is available to be looked at; it contains the entire
1816 POINT is the offset in that line of the cursor. You
1817 should pretend that the line ends at POINT. */
1820 complete_line (completion_tracker
&tracker
,
1821 const char *text
, const char *line_buffer
, int point
)
1823 if (max_completions
== 0)
1825 complete_line_internal (tracker
, text
, line_buffer
, point
,
1826 handle_completions
);
1829 /* Complete on command names. Used by "help". */
1832 command_completer (struct cmd_list_element
*ignore
,
1833 completion_tracker
&tracker
,
1834 const char *text
, const char *word
)
1836 complete_line_internal (tracker
, word
, text
,
1837 strlen (text
), handle_help
);
1840 /* The corresponding completer_handle_brkchars implementation. */
1843 command_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1844 completion_tracker
&tracker
,
1845 const char *text
, const char *word
)
1847 set_rl_completer_word_break_characters
1848 (gdb_completer_command_word_break_characters
);
1851 /* Complete on signals. */
1854 signal_completer (struct cmd_list_element
*ignore
,
1855 completion_tracker
&tracker
,
1856 const char *text
, const char *word
)
1858 size_t len
= strlen (word
);
1860 const char *signame
;
1862 for (signum
= GDB_SIGNAL_FIRST
; signum
!= GDB_SIGNAL_LAST
; ++signum
)
1864 /* Can't handle this, so skip it. */
1865 if (signum
== GDB_SIGNAL_0
)
1868 signame
= gdb_signal_to_name ((enum gdb_signal
) signum
);
1870 /* Ignore the unknown signal case. */
1871 if (!signame
|| strcmp (signame
, "?") == 0)
1874 if (strncasecmp (signame
, word
, len
) == 0)
1875 tracker
.add_completion (make_unique_xstrdup (signame
));
1879 /* Bit-flags for selecting what the register and/or register-group
1880 completer should complete on. */
1882 enum reg_completer_target
1884 complete_register_names
= 0x1,
1885 complete_reggroup_names
= 0x2
1887 DEF_ENUM_FLAGS_TYPE (enum reg_completer_target
, reg_completer_targets
);
1889 /* Complete register names and/or reggroup names based on the value passed
1890 in TARGETS. At least one bit in TARGETS must be set. */
1893 reg_or_group_completer_1 (completion_tracker
&tracker
,
1894 const char *text
, const char *word
,
1895 reg_completer_targets targets
)
1897 size_t len
= strlen (word
);
1898 struct gdbarch
*gdbarch
;
1901 gdb_assert ((targets
& (complete_register_names
1902 | complete_reggroup_names
)) != 0);
1903 gdbarch
= get_current_arch ();
1905 if ((targets
& complete_register_names
) != 0)
1910 (name
= user_reg_map_regnum_to_name (gdbarch
, i
)) != NULL
;
1913 if (*name
!= '\0' && strncmp (word
, name
, len
) == 0)
1914 tracker
.add_completion (make_unique_xstrdup (name
));
1918 if ((targets
& complete_reggroup_names
) != 0)
1920 struct reggroup
*group
;
1922 for (group
= reggroup_next (gdbarch
, NULL
);
1924 group
= reggroup_next (gdbarch
, group
))
1926 name
= reggroup_name (group
);
1927 if (strncmp (word
, name
, len
) == 0)
1928 tracker
.add_completion (make_unique_xstrdup (name
));
1933 /* Perform completion on register and reggroup names. */
1936 reg_or_group_completer (struct cmd_list_element
*ignore
,
1937 completion_tracker
&tracker
,
1938 const char *text
, const char *word
)
1940 reg_or_group_completer_1 (tracker
, text
, word
,
1941 (complete_register_names
1942 | complete_reggroup_names
));
1945 /* Perform completion on reggroup names. */
1948 reggroup_completer (struct cmd_list_element
*ignore
,
1949 completion_tracker
&tracker
,
1950 const char *text
, const char *word
)
1952 reg_or_group_completer_1 (tracker
, text
, word
,
1953 complete_reggroup_names
);
1956 /* The default completer_handle_brkchars implementation. */
1959 default_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1960 completion_tracker
&tracker
,
1961 const char *text
, const char *word
)
1963 set_rl_completer_word_break_characters
1964 (current_language
->word_break_characters ());
1967 /* See definition in completer.h. */
1969 completer_handle_brkchars_ftype
*
1970 completer_handle_brkchars_func_for_completer (completer_ftype
*fn
)
1972 if (fn
== filename_completer
)
1973 return filename_completer_handle_brkchars
;
1975 if (fn
== location_completer
)
1976 return location_completer_handle_brkchars
;
1978 if (fn
== command_completer
)
1979 return command_completer_handle_brkchars
;
1981 return default_completer_handle_brkchars
;
1984 /* Used as brkchars when we want to tell readline we have a custom
1985 word point. We do that by making our rl_completion_word_break_hook
1986 set RL_POINT to the desired word point, and return the character at
1987 the word break point as the break char. This is two bytes in order
1988 to fit one break character plus the terminating null. */
1989 static char gdb_custom_word_point_brkchars
[2];
1991 /* Since rl_basic_quote_characters is not completer-specific, we save
1992 its original value here, in order to be able to restore it in
1993 gdb_rl_attempted_completion_function. */
1994 static const char *gdb_org_rl_basic_quote_characters
= rl_basic_quote_characters
;
1996 /* Get the list of chars that are considered as word breaks
1997 for the current command. */
2000 gdb_completion_word_break_characters_throw ()
2002 /* New completion starting. Get rid of the previous tracker and
2004 delete current_completion
.tracker
;
2005 current_completion
.tracker
= new completion_tracker ();
2007 completion_tracker
&tracker
= *current_completion
.tracker
;
2009 complete_line_internal (tracker
, NULL
, rl_line_buffer
,
2010 rl_point
, handle_brkchars
);
2012 if (tracker
.use_custom_word_point ())
2014 gdb_assert (tracker
.custom_word_point () > 0);
2015 rl_point
= tracker
.custom_word_point () - 1;
2017 gdb_assert (rl_point
>= 0 && rl_point
< strlen (rl_line_buffer
));
2019 gdb_custom_word_point_brkchars
[0] = rl_line_buffer
[rl_point
];
2020 rl_completer_word_break_characters
= gdb_custom_word_point_brkchars
;
2021 rl_completer_quote_characters
= NULL
;
2023 /* Clear this too, so that if we're completing a quoted string,
2024 readline doesn't consider the quote character a delimiter.
2025 If we didn't do this, readline would auto-complete {b
2026 'fun<tab>} to {'b 'function()'}, i.e., add the terminating
2027 \', but, it wouldn't append the separator space either, which
2028 is not desirable. So instead we take care of appending the
2029 quote character to the LCD ourselves, in
2030 gdb_rl_attempted_completion_function. Since this global is
2031 not just completer-specific, we'll restore it back to the
2032 default in gdb_rl_attempted_completion_function. */
2033 rl_basic_quote_characters
= NULL
;
2036 return rl_completer_word_break_characters
;
2040 gdb_completion_word_break_characters ()
2042 /* New completion starting. */
2043 current_completion
.aborted
= false;
2047 return gdb_completion_word_break_characters_throw ();
2049 catch (const gdb_exception
&ex
)
2051 /* Set this to that gdb_rl_attempted_completion_function knows
2053 current_completion
.aborted
= true;
2059 /* See completer.h. */
2062 completion_find_completion_word (completion_tracker
&tracker
, const char *text
,
2065 size_t point
= strlen (text
);
2067 complete_line_internal (tracker
, NULL
, text
, point
, handle_brkchars
);
2069 if (tracker
.use_custom_word_point ())
2071 gdb_assert (tracker
.custom_word_point () > 0);
2072 *quote_char
= tracker
.quote_char ();
2073 return text
+ tracker
.custom_word_point ();
2076 gdb_rl_completion_word_info info
;
2078 info
.word_break_characters
= rl_completer_word_break_characters
;
2079 info
.quote_characters
= gdb_completer_quote_characters
;
2080 info
.basic_quote_characters
= rl_basic_quote_characters
;
2082 return gdb_rl_find_completion_word (&info
, quote_char
, NULL
, text
);
2085 /* See completer.h. */
2088 completion_tracker::recompute_lcd_visitor (completion_hash_entry
*entry
)
2090 if (!m_lowest_common_denominator_valid
)
2092 /* This is the first lowest common denominator that we are
2093 considering, just copy it in. */
2094 strcpy (m_lowest_common_denominator
, entry
->get_lcd ());
2095 m_lowest_common_denominator_unique
= true;
2096 m_lowest_common_denominator_valid
= true;
2100 /* Find the common denominator between the currently-known lowest
2101 common denominator and NEW_MATCH_UP. That becomes the new lowest
2102 common denominator. */
2104 const char *new_match
= entry
->get_lcd ();
2107 (new_match
[i
] != '\0'
2108 && new_match
[i
] == m_lowest_common_denominator
[i
]);
2111 if (m_lowest_common_denominator
[i
] != new_match
[i
])
2113 m_lowest_common_denominator
[i
] = '\0';
2114 m_lowest_common_denominator_unique
= false;
2119 /* See completer.h. */
2122 completion_tracker::recompute_lowest_common_denominator ()
2124 /* We've already done this. */
2125 if (m_lowest_common_denominator_valid
)
2128 /* Resize the storage to ensure we have enough space, the plus one gives
2129 us space for the trailing null terminator we will include. */
2130 m_lowest_common_denominator
2131 = (char *) xrealloc (m_lowest_common_denominator
,
2132 m_lowest_common_denominator_max_length
+ 1);
2134 /* Callback used to visit each entry in the m_entries_hash. */
2136 = [] (void **slot
, void *info
) -> int
2138 completion_tracker
*obj
= (completion_tracker
*) info
;
2139 completion_hash_entry
*entry
= (completion_hash_entry
*) *slot
;
2140 obj
->recompute_lcd_visitor (entry
);
2144 htab_traverse (m_entries_hash
.get (), visitor_func
, this);
2145 m_lowest_common_denominator_valid
= true;
2148 /* See completer.h. */
2151 completion_tracker::advance_custom_word_point_by (int len
)
2153 m_custom_word_point
+= len
;
2156 /* Build a new C string that is a copy of LCD with the whitespace of
2157 ORIG/ORIG_LEN preserved.
2159 Say the user is completing a symbol name, with spaces, like:
2163 and the resulting completion match is:
2167 we want to end up with an input line like:
2170 ^^^^^^^ => text from LCD [1], whitespace from ORIG preserved.
2171 ^^ => new text from LCD
2173 [1] - We must take characters from the LCD instead of the original
2174 text, since some completions want to change upper/lowercase. E.g.:
2180 "handle SIG[QUIT|etc.]"
2184 expand_preserving_ws (const char *orig
, size_t orig_len
,
2187 const char *p_orig
= orig
;
2188 const char *orig_end
= orig
+ orig_len
;
2189 const char *p_lcd
= lcd
;
2192 while (p_orig
< orig_end
)
2196 while (p_orig
< orig_end
&& *p_orig
== ' ')
2198 p_lcd
= skip_spaces (p_lcd
);
2202 /* Take characters from the LCD instead of the original
2203 text, since some completions change upper/lowercase.
2207 "handle SIG[QUIT|etc.]"
2215 while (*p_lcd
!= '\0')
2218 return xstrdup (res
.c_str ());
2221 /* See completer.h. */
2224 completion_tracker::build_completion_result (const char *text
,
2227 size_t element_count
= htab_elements (m_entries_hash
.get ());
2229 if (element_count
== 0)
2232 /* +1 for the LCD, and +1 for NULL termination. */
2233 char **match_list
= XNEWVEC (char *, 1 + element_count
+ 1);
2235 /* Build replacement word, based on the LCD. */
2237 recompute_lowest_common_denominator ();
2239 = expand_preserving_ws (text
, end
- start
,
2240 m_lowest_common_denominator
);
2242 if (m_lowest_common_denominator_unique
)
2244 /* We don't rely on readline appending the quote char as
2245 delimiter as then readline wouldn't append the ' ' after the
2247 char buf
[2] = { (char) quote_char () };
2249 match_list
[0] = reconcat (match_list
[0], match_list
[0],
2250 buf
, (char *) NULL
);
2251 match_list
[1] = NULL
;
2253 /* If the tracker wants to, or we already have a space at the
2254 end of the match, tell readline to skip appending
2256 bool completion_suppress_append
2257 = (suppress_append_ws ()
2258 || match_list
[0][strlen (match_list
[0]) - 1] == ' ');
2260 return completion_result (match_list
, 1, completion_suppress_append
);
2264 /* State object used while building the completion list. */
2267 list_builder (char **ml
)
2272 /* The list we are filling. */
2275 /* The next index in the list to write to. */
2278 list_builder
builder (match_list
);
2280 /* Visit each entry in m_entries_hash and add it to the completion
2281 list, updating the builder state object. */
2283 = [] (void **slot
, void *info
) -> int
2285 completion_hash_entry
*entry
= (completion_hash_entry
*) *slot
;
2286 list_builder
*state
= (list_builder
*) info
;
2288 state
->match_list
[state
->index
] = entry
->release_name ();
2293 /* Build the completion list and add a null at the end. */
2294 htab_traverse_noresize (m_entries_hash
.get (), func
, &builder
);
2295 match_list
[builder
.index
] = NULL
;
2297 return completion_result (match_list
, builder
.index
- 1, false);
2301 /* See completer.h */
2303 completion_result::completion_result ()
2304 : match_list (NULL
), number_matches (0),
2305 completion_suppress_append (false)
2308 /* See completer.h */
2310 completion_result::completion_result (char **match_list_
,
2311 size_t number_matches_
,
2312 bool completion_suppress_append_
)
2313 : match_list (match_list_
),
2314 number_matches (number_matches_
),
2315 completion_suppress_append (completion_suppress_append_
)
2318 /* See completer.h */
2320 completion_result::~completion_result ()
2322 reset_match_list ();
2325 /* See completer.h */
2327 completion_result::completion_result (completion_result
&&rhs
) noexcept
2328 : match_list (rhs
.match_list
),
2329 number_matches (rhs
.number_matches
)
2331 rhs
.match_list
= NULL
;
2332 rhs
.number_matches
= 0;
2335 /* See completer.h */
2338 completion_result::release_match_list ()
2340 char **ret
= match_list
;
2345 /* See completer.h */
2348 completion_result::sort_match_list ()
2350 if (number_matches
> 1)
2352 /* Element 0 is special (it's the common prefix), leave it
2354 std::sort (&match_list
[1],
2355 &match_list
[number_matches
+ 1],
2360 /* See completer.h */
2363 completion_result::reset_match_list ()
2365 if (match_list
!= NULL
)
2367 for (char **p
= match_list
; *p
!= NULL
; p
++)
2374 /* Helper for gdb_rl_attempted_completion_function, which does most of
2375 the work. This is called by readline to build the match list array
2376 and to determine the lowest common denominator. The real matches
2377 list starts at match[1], while match[0] is the slot holding
2378 readline's idea of the lowest common denominator of all matches,
2379 which is what readline replaces the completion "word" with.
2381 TEXT is the caller's idea of the "word" we are looking at, as
2382 computed in the handle_brkchars phase.
2384 START is the offset from RL_LINE_BUFFER where TEXT starts. END is
2385 the offset from RL_LINE_BUFFER where TEXT ends (i.e., where
2388 You should thus pretend that the line ends at END (relative to
2391 RL_LINE_BUFFER contains the entire text of the line. RL_POINT is
2392 the offset in that line of the cursor. You should pretend that the
2395 Returns NULL if there are no completions. */
2398 gdb_rl_attempted_completion_function_throw (const char *text
, int start
, int end
)
2400 /* Completers that provide a custom word point in the
2401 handle_brkchars phase also compute their completions then.
2402 Completers that leave the completion word handling to readline
2403 must be called twice. If rl_point (i.e., END) is at column 0,
2404 then readline skips the handle_brkchars phase, and so we create a
2405 tracker now in that case too. */
2406 if (end
== 0 || !current_completion
.tracker
->use_custom_word_point ())
2408 delete current_completion
.tracker
;
2409 current_completion
.tracker
= new completion_tracker ();
2411 complete_line (*current_completion
.tracker
, text
,
2412 rl_line_buffer
, rl_point
);
2415 completion_tracker
&tracker
= *current_completion
.tracker
;
2417 completion_result result
2418 = tracker
.build_completion_result (text
, start
, end
);
2420 rl_completion_suppress_append
= result
.completion_suppress_append
;
2421 return result
.release_match_list ();
2424 /* Function installed as "rl_attempted_completion_function" readline
2425 hook. Wrapper around gdb_rl_attempted_completion_function_throw
2426 that catches C++ exceptions, which can't cross readline. */
2429 gdb_rl_attempted_completion_function (const char *text
, int start
, int end
)
2431 /* Restore globals that might have been tweaked in
2432 gdb_completion_word_break_characters. */
2433 rl_basic_quote_characters
= gdb_org_rl_basic_quote_characters
;
2435 /* If we end up returning NULL, either on error, or simple because
2436 there are no matches, inhibit readline's default filename
2438 rl_attempted_completion_over
= 1;
2440 /* If the handle_brkchars phase was aborted, don't try
2442 if (current_completion
.aborted
)
2447 return gdb_rl_attempted_completion_function_throw (text
, start
, end
);
2449 catch (const gdb_exception
&ex
)
2456 /* Skip over the possibly quoted word STR (as defined by the quote
2457 characters QUOTECHARS and the word break characters BREAKCHARS).
2458 Returns pointer to the location after the "word". If either
2459 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
2463 skip_quoted_chars (const char *str
, const char *quotechars
,
2464 const char *breakchars
)
2466 char quote_char
= '\0';
2469 if (quotechars
== NULL
)
2470 quotechars
= gdb_completer_quote_characters
;
2472 if (breakchars
== NULL
)
2473 breakchars
= current_language
->word_break_characters ();
2475 for (scan
= str
; *scan
!= '\0'; scan
++)
2477 if (quote_char
!= '\0')
2479 /* Ignore everything until the matching close quote char. */
2480 if (*scan
== quote_char
)
2482 /* Found matching close quote. */
2487 else if (strchr (quotechars
, *scan
))
2489 /* Found start of a quoted string. */
2492 else if (strchr (breakchars
, *scan
))
2501 /* Skip over the possibly quoted word STR (as defined by the quote
2502 characters and word break characters used by the completer).
2503 Returns pointer to the location after the "word". */
2506 skip_quoted (const char *str
)
2508 return skip_quoted_chars (str
, NULL
, NULL
);
2511 /* Return a message indicating that the maximum number of completions
2512 has been reached and that there may be more. */
2515 get_max_completions_reached_message (void)
2517 return _("*** List may be truncated, max-completions reached. ***");
2520 /* GDB replacement for rl_display_match_list.
2521 Readline doesn't provide a clean interface for TUI(curses).
2522 A hack previously used was to send readline's rl_outstream through a pipe
2523 and read it from the event loop. Bleah. IWBN if readline abstracted
2524 away all the necessary bits, and this is what this code does. It
2525 replicates the parts of readline we need and then adds an abstraction
2526 layer, currently implemented as struct match_list_displayer, so that both
2527 CLI and TUI can use it. We copy all this readline code to minimize
2528 GDB-specific mods to readline. Once this code performs as desired then
2529 we can submit it to the readline maintainers.
2531 N.B. A lot of the code is the way it is in order to minimize differences
2532 from readline's copy. */
2534 /* Not supported here. */
2535 #undef VISIBLE_STATS
2537 #if defined (HANDLE_MULTIBYTE)
2538 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
2539 #define MB_NULLWCH(x) ((x) == 0)
2542 #define ELLIPSIS_LEN 3
2544 /* gdb version of readline/complete.c:get_y_or_n.
2545 'y' -> returns 1, and 'n' -> returns 0.
2546 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
2547 If FOR_PAGER is non-zero, then also supported are:
2548 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
2551 gdb_get_y_or_n (int for_pager
, const struct match_list_displayer
*displayer
)
2557 RL_SETSTATE (RL_STATE_MOREINPUT
);
2558 c
= displayer
->read_key (displayer
);
2559 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
2561 if (c
== 'y' || c
== 'Y' || c
== ' ')
2563 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
2565 if (c
== ABORT_CHAR
|| c
< 0)
2567 /* Readline doesn't erase_entire_line here, but without it the
2568 --More-- prompt isn't erased and neither is the text entered
2569 thus far redisplayed. */
2570 displayer
->erase_entire_line (displayer
);
2571 /* Note: The arguments to rl_abort are ignored. */
2574 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
2576 if (for_pager
&& (c
== 'q' || c
== 'Q'))
2578 displayer
->beep (displayer
);
2582 /* Pager function for tab-completion.
2583 This is based on readline/complete.c:_rl_internal_pager.
2584 LINES is the number of lines of output displayed thus far.
2586 -1 -> user pressed 'n' or equivalent,
2587 0 -> user pressed 'y' or equivalent,
2588 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
2591 gdb_display_match_list_pager (int lines
,
2592 const struct match_list_displayer
*displayer
)
2596 displayer
->puts (displayer
, "--More--");
2597 displayer
->flush (displayer
);
2598 i
= gdb_get_y_or_n (1, displayer
);
2599 displayer
->erase_entire_line (displayer
);
2608 /* Return non-zero if FILENAME is a directory.
2609 Based on readline/complete.c:path_isdir. */
2612 gdb_path_isdir (const char *filename
)
2616 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
2619 /* Return the portion of PATHNAME that should be output when listing
2620 possible completions. If we are hacking filename completion, we
2621 are only interested in the basename, the portion following the
2622 final slash. Otherwise, we return what we were passed. Since
2623 printing empty strings is not very informative, if we're doing
2624 filename completion, and the basename is the empty string, we look
2625 for the previous slash and return the portion following that. If
2626 there's no previous slash, we just return what we were passed.
2628 Based on readline/complete.c:printable_part. */
2631 gdb_printable_part (char *pathname
)
2635 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
2638 temp
= strrchr (pathname
, '/');
2639 #if defined (__MSDOS__)
2640 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
2641 temp
= pathname
+ 1;
2644 if (temp
== 0 || *temp
== '\0')
2646 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
2647 Look for a previous slash and, if one is found, return the portion
2648 following that slash. If there's no previous slash, just return the
2649 pathname we were passed. */
2650 else if (temp
[1] == '\0')
2652 for (x
= temp
- 1; x
> pathname
; x
--)
2655 return ((*x
== '/') ? x
+ 1 : pathname
);
2661 /* Compute width of STRING when displayed on screen by print_filename.
2662 Based on readline/complete.c:fnwidth. */
2665 gdb_fnwidth (const char *string
)
2668 #if defined (HANDLE_MULTIBYTE)
2674 left
= strlen (string
) + 1;
2675 memset (&ps
, 0, sizeof (mbstate_t));
2681 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
2688 #if defined (HANDLE_MULTIBYTE)
2689 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
2690 if (MB_INVALIDCH (clen
))
2694 memset (&ps
, 0, sizeof (mbstate_t));
2696 else if (MB_NULLWCH (clen
))
2702 width
+= (w
>= 0) ? w
: 1;
2714 /* Print TO_PRINT, one matching completion.
2715 PREFIX_BYTES is number of common prefix bytes.
2716 Based on readline/complete.c:fnprint. */
2719 gdb_fnprint (const char *to_print
, int prefix_bytes
,
2720 const struct match_list_displayer
*displayer
)
2724 #if defined (HANDLE_MULTIBYTE)
2731 end
= to_print
+ strlen (to_print
) + 1;
2732 memset (&ps
, 0, sizeof (mbstate_t));
2737 /* Don't print only the ellipsis if the common prefix is one of the
2738 possible completions */
2739 if (to_print
[prefix_bytes
] == '\0')
2746 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
2747 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
2748 displayer
->putch (displayer
, ellipsis
);
2749 printed_len
= ELLIPSIS_LEN
;
2752 s
= to_print
+ prefix_bytes
;
2757 displayer
->putch (displayer
, '^');
2758 displayer
->putch (displayer
, UNCTRL (*s
));
2761 #if defined (HANDLE_MULTIBYTE)
2762 memset (&ps
, 0, sizeof (mbstate_t));
2765 else if (*s
== RUBOUT
)
2767 displayer
->putch (displayer
, '^');
2768 displayer
->putch (displayer
, '?');
2771 #if defined (HANDLE_MULTIBYTE)
2772 memset (&ps
, 0, sizeof (mbstate_t));
2777 #if defined (HANDLE_MULTIBYTE)
2778 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
2779 if (MB_INVALIDCH (tlen
))
2783 memset (&ps
, 0, sizeof (mbstate_t));
2785 else if (MB_NULLWCH (tlen
))
2790 width
= (w
>= 0) ? w
: 1;
2792 for (w
= 0; w
< tlen
; ++w
)
2793 displayer
->putch (displayer
, s
[w
]);
2795 printed_len
+= width
;
2797 displayer
->putch (displayer
, *s
);
2807 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
2808 are using it, check for and output a single character for `special'
2809 filenames. Return the number of characters we output.
2810 Based on readline/complete.c:print_filename. */
2813 gdb_print_filename (char *to_print
, char *full_pathname
, int prefix_bytes
,
2814 const struct match_list_displayer
*displayer
)
2816 int printed_len
, extension_char
, slen
, tlen
;
2817 char *s
, c
, *new_full_pathname
;
2819 extern int _rl_complete_mark_directories
;
2822 printed_len
= gdb_fnprint (to_print
, prefix_bytes
, displayer
);
2824 #if defined (VISIBLE_STATS)
2825 if (rl_filename_completion_desired
&& (rl_visible_stats
|| _rl_complete_mark_directories
))
2827 if (rl_filename_completion_desired
&& _rl_complete_mark_directories
)
2830 /* If to_print != full_pathname, to_print is the basename of the
2831 path passed. In this case, we try to expand the directory
2832 name before checking for the stat character. */
2833 if (to_print
!= full_pathname
)
2835 /* Terminate the directory name. */
2837 to_print
[-1] = '\0';
2839 /* If setting the last slash in full_pathname to a NUL results in
2840 full_pathname being the empty string, we are trying to complete
2841 files in the root directory. If we pass a null string to the
2842 bash directory completion hook, for example, it will expand it
2843 to the current directory. We just want the `/'. */
2844 if (full_pathname
== 0 || *full_pathname
== 0)
2846 else if (full_pathname
[0] != '/')
2848 else if (full_pathname
[1] == 0)
2849 dn
= "//"; /* restore trailing slash to `//' */
2850 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
2851 dn
= "/"; /* don't turn /// into // */
2854 s
= tilde_expand (dn
);
2855 if (rl_directory_completion_hook
)
2856 (*rl_directory_completion_hook
) (&s
);
2859 tlen
= strlen (to_print
);
2860 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
2861 strcpy (new_full_pathname
, s
);
2862 if (s
[slen
- 1] == '/')
2865 new_full_pathname
[slen
] = '/';
2866 new_full_pathname
[slen
] = '/';
2867 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
2869 #if defined (VISIBLE_STATS)
2870 if (rl_visible_stats
)
2871 extension_char
= stat_char (new_full_pathname
);
2874 if (gdb_path_isdir (new_full_pathname
))
2875 extension_char
= '/';
2877 xfree (new_full_pathname
);
2882 s
= tilde_expand (full_pathname
);
2883 #if defined (VISIBLE_STATS)
2884 if (rl_visible_stats
)
2885 extension_char
= stat_char (s
);
2888 if (gdb_path_isdir (s
))
2889 extension_char
= '/';
2895 displayer
->putch (displayer
, extension_char
);
2903 /* GDB version of readline/complete.c:complete_get_screenwidth. */
2906 gdb_complete_get_screenwidth (const struct match_list_displayer
*displayer
)
2908 /* Readline has other stuff here which it's not clear we need. */
2909 return displayer
->width
;
2912 extern int _rl_completion_prefix_display_length
;
2913 extern int _rl_print_completions_horizontally
;
2915 EXTERN_C
int _rl_qsort_string_compare (const void *, const void *);
2916 typedef int QSFUNC (const void *, const void *);
2918 /* GDB version of readline/complete.c:rl_display_match_list.
2919 See gdb_display_match_list for a description of MATCHES, LEN, MAX.
2920 Returns non-zero if all matches are displayed. */
2923 gdb_display_match_list_1 (char **matches
, int len
, int max
,
2924 const struct match_list_displayer
*displayer
)
2926 int count
, limit
, printed_len
, lines
, cols
;
2927 int i
, j
, k
, l
, common_length
, sind
;
2929 int page_completions
= displayer
->height
!= INT_MAX
&& pagination_enabled
;
2931 /* Find the length of the prefix common to all items: length as displayed
2932 characters (common_length) and as a byte index into the matches (sind) */
2933 common_length
= sind
= 0;
2934 if (_rl_completion_prefix_display_length
> 0)
2936 t
= gdb_printable_part (matches
[0]);
2937 temp
= strrchr (t
, '/');
2938 common_length
= temp
? gdb_fnwidth (temp
) : gdb_fnwidth (t
);
2939 sind
= temp
? strlen (temp
) : strlen (t
);
2941 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
2942 max
-= common_length
- ELLIPSIS_LEN
;
2944 common_length
= sind
= 0;
2947 /* How many items of MAX length can we fit in the screen window? */
2948 cols
= gdb_complete_get_screenwidth (displayer
);
2951 if (limit
!= 1 && (limit
* max
== cols
))
2954 /* If cols == 0, limit will end up -1 */
2955 if (cols
< displayer
->width
&& limit
< 0)
2958 /* Avoid a possible floating exception. If max > cols,
2959 limit will be 0 and a divide-by-zero fault will result. */
2963 /* How many iterations of the printing loop? */
2964 count
= (len
+ (limit
- 1)) / limit
;
2966 /* Watch out for special case. If LEN is less than LIMIT, then
2967 just do the inner printing loop.
2968 0 < len <= limit implies count = 1. */
2970 /* Sort the items if they are not already sorted. */
2971 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
2972 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
2974 displayer
->crlf (displayer
);
2977 if (_rl_print_completions_horizontally
== 0)
2979 /* Print the sorted items, up-and-down alphabetically, like ls. */
2980 for (i
= 1; i
<= count
; i
++)
2982 for (j
= 0, l
= i
; j
< limit
; j
++)
2984 if (l
> len
|| matches
[l
] == 0)
2988 temp
= gdb_printable_part (matches
[l
]);
2989 printed_len
= gdb_print_filename (temp
, matches
[l
], sind
,
2993 for (k
= 0; k
< max
- printed_len
; k
++)
2994 displayer
->putch (displayer
, ' ');
2998 displayer
->crlf (displayer
);
3000 if (page_completions
&& lines
>= (displayer
->height
- 1) && i
< count
)
3002 lines
= gdb_display_match_list_pager (lines
, displayer
);
3010 /* Print the sorted items, across alphabetically, like ls -x. */
3011 for (i
= 1; matches
[i
]; i
++)
3013 temp
= gdb_printable_part (matches
[i
]);
3014 printed_len
= gdb_print_filename (temp
, matches
[i
], sind
, displayer
);
3015 /* Have we reached the end of this line? */
3018 if (i
&& (limit
> 1) && (i
% limit
) == 0)
3020 displayer
->crlf (displayer
);
3022 if (page_completions
&& lines
>= displayer
->height
- 1)
3024 lines
= gdb_display_match_list_pager (lines
, displayer
);
3030 for (k
= 0; k
< max
- printed_len
; k
++)
3031 displayer
->putch (displayer
, ' ');
3034 displayer
->crlf (displayer
);
3040 /* Utility for displaying completion list matches, used by both CLI and TUI.
3042 MATCHES is the list of strings, in argv format, LEN is the number of
3043 strings in MATCHES, and MAX is the length of the longest string in
3047 gdb_display_match_list (char **matches
, int len
, int max
,
3048 const struct match_list_displayer
*displayer
)
3050 /* Readline will never call this if complete_line returned NULL. */
3051 gdb_assert (max_completions
!= 0);
3053 /* complete_line will never return more than this. */
3054 if (max_completions
> 0)
3055 gdb_assert (len
<= max_completions
);
3057 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
3061 /* We can't use *query here because they wait for <RET> which is
3062 wrong here. This follows the readline version as closely as possible
3063 for compatibility's sake. See readline/complete.c. */
3065 displayer
->crlf (displayer
);
3067 xsnprintf (msg
, sizeof (msg
),
3068 "Display all %d possibilities? (y or n)", len
);
3069 displayer
->puts (displayer
, msg
);
3070 displayer
->flush (displayer
);
3072 if (gdb_get_y_or_n (0, displayer
) == 0)
3074 displayer
->crlf (displayer
);
3079 if (gdb_display_match_list_1 (matches
, len
, max
, displayer
))
3081 /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
3082 if (len
== max_completions
)
3084 /* The maximum number of completions has been reached. Warn the user
3085 that there may be more. */
3086 const char *message
= get_max_completions_reached_message ();
3088 displayer
->puts (displayer
, message
);
3089 displayer
->crlf (displayer
);
3094 void _initialize_completer ();
3096 _initialize_completer ()
3098 add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class
,
3099 &max_completions
, _("\
3100 Set maximum number of completion candidates."), _("\
3101 Show maximum number of completion candidates."), _("\
3102 Use this to limit the number of candidates considered\n\
3103 during completion. Specifying \"unlimited\" or -1\n\
3104 disables limiting. Note that setting either no limit or\n\
3105 a very large limit can make completion slow."),
3106 NULL
, NULL
, &setlist
, &showlist
);