1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000-2024 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"
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"
34 #include "gdbsupport/gdb_tilde_expand.h"
36 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
37 calling a hook instead so we eliminate the CLI dependency. */
40 /* Needed for rl_completer_word_break_characters and for
41 rl_filename_completion_function. */
42 #include "readline/readline.h"
44 /* readline defines this. */
47 #include "completer.h"
49 /* See completer.h. */
51 class completion_tracker::completion_hash_entry
55 completion_hash_entry (gdb::unique_xmalloc_ptr
<char> name
,
56 gdb::unique_xmalloc_ptr
<char> lcd
)
57 : m_name (std::move (name
)),
58 m_lcd (std::move (lcd
))
63 /* Returns a pointer to the lowest common denominator string. This
64 string will only be valid while this hash entry is still valid as the
65 string continues to be owned by this hash entry and will be released
66 when this entry is deleted. */
67 char *get_lcd () const
72 /* Get, and release the name field from this hash entry. This can only
73 be called once, after which the name field is no longer valid. This
74 should be used to pass ownership of the name to someone else. */
77 return m_name
.release ();
80 /* Return true of the name in this hash entry is STR. */
81 bool is_name_eq (const char *str
) const
83 return strcmp (m_name
.get (), str
) == 0;
86 /* Return the hash value based on the name of the entry. */
87 hashval_t
hash_name () const
89 return htab_hash_string (m_name
.get ());
94 /* The symbol name stored in this hash entry. */
95 gdb::unique_xmalloc_ptr
<char> m_name
;
97 /* The lowest common denominator string computed for this hash entry. */
98 gdb::unique_xmalloc_ptr
<char> m_lcd
;
101 /* Misc state that needs to be tracked across several different
102 readline completer entry point calls, all related to a single
103 completion invocation. */
105 struct gdb_completer_state
107 /* The current completion's completion tracker. This is a global
108 because a tracker can be shared between the handle_brkchars and
109 handle_completion phases, which involves different readline
111 completion_tracker
*tracker
= NULL
;
113 /* Whether the current completion was aborted. */
114 bool aborted
= false;
117 /* The current completion state. */
118 static gdb_completer_state current_completion
;
120 /* An enumeration of the various things a user might attempt to
121 complete for a location. If you change this, remember to update
122 the explicit_options array below too. */
124 enum explicit_location_match_type
126 /* The filename of a source file. */
129 /* The name of a function or method. */
132 /* The fully-qualified name of a function or method. */
138 /* The name of a label. */
142 /* Prototypes for local functions. */
144 /* readline uses the word breaks for two things:
145 (1) In figuring out where to point the TEXT parameter to the
146 rl_completion_entry_function. Since we don't use TEXT for much,
147 it doesn't matter a lot what the word breaks are for this purpose,
148 but it does affect how much stuff M-? lists.
149 (2) If one of the matches contains a word break character, readline
150 will quote it. That's why we switch between
151 current_language->word_break_characters () and
152 gdb_completer_command_word_break_characters. I'm not sure when
153 we need this behavior (perhaps for funky characters in C++
156 /* Variables which are necessary for fancy command line editing. */
158 /* When completing on command names, we remove '-' and '.' from the list of
159 word break characters, since we use it in command names. If the
160 readline library sees one in any of the current completion strings,
161 it thinks that the string needs to be quoted and automatically
162 supplies a leading quote. */
163 static const char gdb_completer_command_word_break_characters
[] =
164 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/><,";
166 /* When completing on file names, we remove from the list of word
167 break characters any characters that are commonly used in file
168 names, such as '-', '+', '~', etc. Otherwise, readline displays
169 incorrect completion candidates. */
170 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
171 programs support @foo style response files. */
172 static const char gdb_completer_file_name_break_characters
[] =
173 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
179 /* Characters that can be used to quote completion strings. Note that
180 we can't include '"' because the gdb C parser treats such quoted
181 sequences as strings. */
182 static const char gdb_completer_quote_characters
[] = "'";
184 /* Accessor for some completer data that may interest other files. */
187 get_gdb_completer_quote_characters (void)
189 return gdb_completer_quote_characters
;
192 /* This can be used for functions which don't want to complete on
193 symbols but don't want to complete on anything else either. */
196 noop_completer (struct cmd_list_element
*ignore
,
197 completion_tracker
&tracker
,
198 const char *text
, const char *prefix
)
202 /* Complete on filenames. */
205 filename_completer (struct cmd_list_element
*ignore
,
206 completion_tracker
&tracker
,
207 const char *text
, const char *word
)
214 gdb::unique_xmalloc_ptr
<char> p_rl
215 (rl_filename_completion_function (text
, subsequent_name
));
218 /* We need to set subsequent_name to a non-zero value before the
219 continue line below, because otherwise, if the first file
220 seen by GDB is a backup file whose name ends in a `~', we
221 will loop indefinitely. */
223 /* Like emacs, don't complete on old versions. Especially
224 useful in the "source" command. */
225 const char *p
= p_rl
.get ();
226 if (p
[strlen (p
) - 1] == '~')
229 /* Readline appends a trailing '/' if the completion is a
230 directory. If this completion request originated from outside
231 readline (e.g. GDB's 'complete' command), then we append the
232 trailing '/' ourselves now. */
233 if (!tracker
.from_readline ())
235 std::string expanded
= gdb_tilde_expand (p_rl
.get ());
237 const bool isdir
= (stat (expanded
.c_str (), &finfo
) == 0
238 && S_ISDIR (finfo
.st_mode
));
240 p_rl
.reset (concat (p_rl
.get (), "/", nullptr));
243 tracker
.add_completion
244 (make_completion_match_str (std::move (p_rl
), text
, word
));
247 /* There is no way to do this just long enough to affect quote
248 inserting without also affecting the next completion. This
249 should be fixed in readline. FIXME. */
250 /* Ensure that readline does the right thing
251 with respect to inserting quotes. */
252 rl_completer_word_break_characters
= "";
256 /* The corresponding completer_handle_brkchars
260 filename_completer_handle_brkchars (struct cmd_list_element
*ignore
,
261 completion_tracker
&tracker
,
262 const char *text
, const char *word
)
264 set_rl_completer_word_break_characters
265 (gdb_completer_file_name_break_characters
);
268 /* Find the bounds of the current word for completion purposes, and
269 return a pointer to the end of the word. This mimics (and is a
270 modified version of) readline's _rl_find_completion_word internal
273 This function skips quoted substrings (characters between matched
274 pairs of characters in rl_completer_quote_characters). We try to
275 find an unclosed quoted substring on which to do matching. If one
276 is not found, we use the word break characters to find the
277 boundaries of the current word. QC, if non-null, is set to the
278 opening quote character if we found an unclosed quoted substring,
279 '\0' otherwise. DP, if non-null, is set to the value of the
280 delimiter character that caused a word break. */
282 struct gdb_rl_completion_word_info
284 const char *word_break_characters
;
285 const char *quote_characters
;
286 const char *basic_quote_characters
;
290 gdb_rl_find_completion_word (struct gdb_rl_completion_word_info
*info
,
292 const char *line_buffer
)
294 int scan
, end
, delimiter
, pass_next
, isbrk
;
296 const char *brkchars
;
297 int point
= strlen (line_buffer
);
299 /* The algorithm below does '--point'. Avoid buffer underflow with
314 brkchars
= info
->word_break_characters
;
316 if (info
->quote_characters
!= NULL
)
318 /* We have a list of characters which can be used in pairs to
319 quote substrings for the completer. Try to find the start of
320 an unclosed quoted substring. */
321 for (scan
= pass_next
= 0;
331 /* Shell-like semantics for single quotes -- don't allow
332 backslash to quote anything in single quotes, especially
333 not the closing quote. If you don't like this, take out
334 the check on the value of quote_char. */
335 if (quote_char
!= '\'' && line_buffer
[scan
] == '\\')
341 if (quote_char
!= '\0')
343 /* Ignore everything until the matching close quote
345 if (line_buffer
[scan
] == quote_char
)
347 /* Found matching close. Abandon this
353 else if (strchr (info
->quote_characters
, line_buffer
[scan
]))
355 /* Found start of a quoted substring. */
356 quote_char
= line_buffer
[scan
];
362 if (point
== end
&& quote_char
== '\0')
364 /* We didn't find an unclosed quoted substring upon which to do
365 completion, so use the word break characters to find the
366 substring on which to complete. */
369 scan
= line_buffer
[point
];
371 if (strchr (brkchars
, scan
) != 0)
376 /* If we are at an unquoted word break, then advance past it. */
377 scan
= line_buffer
[point
];
381 isbrk
= strchr (brkchars
, scan
) != 0;
385 /* If the character that caused the word break was a quoting
386 character, then remember it as the delimiter. */
387 if (info
->basic_quote_characters
388 && strchr (info
->basic_quote_characters
, scan
)
389 && (end
- point
) > 1)
401 return line_buffer
+ point
;
404 /* Find the completion word point for TEXT, emulating the algorithm
405 readline uses to find the word point, using WORD_BREAK_CHARACTERS
406 as word break characters. */
409 advance_to_completion_word (completion_tracker
&tracker
,
410 const char *word_break_characters
,
413 gdb_rl_completion_word_info info
;
415 info
.word_break_characters
= word_break_characters
;
416 info
.quote_characters
= gdb_completer_quote_characters
;
417 info
.basic_quote_characters
= rl_basic_quote_characters
;
421 = gdb_rl_find_completion_word (&info
, NULL
, &delimiter
, text
);
423 tracker
.advance_custom_word_point_by (start
- text
);
427 tracker
.set_quote_char (delimiter
);
428 tracker
.set_suppress_append_ws (true);
434 /* See completer.h. */
437 advance_to_expression_complete_word_point (completion_tracker
&tracker
,
440 const char *brk_chars
= current_language
->word_break_characters ();
441 return advance_to_completion_word (tracker
, brk_chars
, text
);
444 /* See completer.h. */
447 advance_to_filename_complete_word_point (completion_tracker
&tracker
,
450 const char *brk_chars
= gdb_completer_file_name_break_characters
;
451 return advance_to_completion_word (tracker
, brk_chars
, text
);
454 /* See completer.h. */
457 completion_tracker::completes_to_completion_word (const char *word
)
459 recompute_lowest_common_denominator ();
460 if (m_lowest_common_denominator_unique
)
462 const char *lcd
= m_lowest_common_denominator
;
464 if (strncmp_iw (word
, lcd
, strlen (lcd
)) == 0)
466 /* Maybe skip the function and complete on keywords. */
467 size_t wordlen
= strlen (word
);
468 if (word
[wordlen
- 1] == ' ')
476 /* See completer.h. */
479 complete_nested_command_line (completion_tracker
&tracker
, const char *text
)
481 /* Must be called from a custom-word-point completer. */
482 gdb_assert (tracker
.use_custom_word_point ());
484 /* Disable the custom word point temporarily, because we want to
485 probe whether the command we're completing itself uses a custom
487 tracker
.set_use_custom_word_point (false);
488 size_t save_custom_word_point
= tracker
.custom_word_point ();
490 int quote_char
= '\0';
491 const char *word
= completion_find_completion_word (tracker
, text
,
494 if (tracker
.use_custom_word_point ())
496 /* The command we're completing uses a custom word point, so the
497 tracker already contains the matches. We're done. */
501 /* Restore the custom word point settings. */
502 tracker
.set_custom_word_point (save_custom_word_point
);
503 tracker
.set_use_custom_word_point (true);
505 /* Run the handle_completions completer phase. */
506 complete_line (tracker
, word
, text
, strlen (text
));
509 /* Complete on linespecs, which might be of two possible forms:
515 This is intended to be used in commands that set breakpoints
519 complete_files_symbols (completion_tracker
&tracker
,
520 const char *text
, const char *word
)
522 completion_list fn_list
;
525 int quoted
= *text
== '\'' || *text
== '"';
526 int quote_char
= '\0';
527 const char *colon
= NULL
;
528 char *file_to_match
= NULL
;
529 const char *symbol_start
= text
;
530 const char *orig_text
= text
;
532 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
533 for (p
= text
; *p
!= '\0'; ++p
)
535 if (*p
== '\\' && p
[1] == '\'')
537 else if (*p
== '\'' || *p
== '"')
541 while (*p
!= '\0' && *p
!= quote_found
)
543 if (*p
== '\\' && p
[1] == quote_found
)
548 if (*p
== quote_found
)
551 break; /* Hit the end of text. */
553 #if HAVE_DOS_BASED_FILE_SYSTEM
554 /* If we have a DOS-style absolute file name at the beginning of
555 TEXT, and the colon after the drive letter is the only colon
556 we found, pretend the colon is not there. */
557 else if (p
< text
+ 3 && *p
== ':' && p
== text
+ 1 + quoted
)
560 else if (*p
== ':' && !colon
)
563 symbol_start
= p
+ 1;
565 else if (strchr (current_language
->word_break_characters (), *p
))
566 symbol_start
= p
+ 1;
572 /* Where is the file name? */
577 file_to_match
= (char *) xmalloc (colon
- text
+ 1);
578 strncpy (file_to_match
, text
, colon
- text
);
579 file_to_match
[colon
- text
] = '\0';
580 /* Remove trailing colons and quotes from the file name. */
581 for (s
= file_to_match
+ (colon
- text
);
584 if (*s
== ':' || *s
== quote_char
)
587 /* If the text includes a colon, they want completion only on a
588 symbol name after the colon. Otherwise, we need to complete on
589 symbols as well as on files. */
592 collect_file_symbol_completion_matches (tracker
,
593 complete_symbol_mode::EXPRESSION
,
594 symbol_name_match_type::EXPRESSION
,
597 xfree (file_to_match
);
601 size_t text_len
= strlen (text
);
603 collect_symbol_completion_matches (tracker
,
604 complete_symbol_mode::EXPRESSION
,
605 symbol_name_match_type::EXPRESSION
,
607 /* If text includes characters which cannot appear in a file
608 name, they cannot be asking for completion on files. */
610 gdb_completer_file_name_break_characters
) == text_len
)
611 fn_list
= make_source_files_completion_list (text
, text
);
614 if (!fn_list
.empty () && !tracker
.have_completions ())
616 /* If we only have file names as possible completion, we should
617 bring them in sync with what rl_complete expects. The
618 problem is that if the user types "break /foo/b TAB", and the
619 possible completions are "/foo/bar" and "/foo/baz"
620 rl_complete expects us to return "bar" and "baz", without the
621 leading directories, as possible completions, because `word'
622 starts at the "b". But we ignore the value of `word' when we
623 call make_source_files_completion_list above (because that
624 would not DTRT when the completion results in both symbols
625 and file names), so make_source_files_completion_list returns
626 the full "/foo/bar" and "/foo/baz" strings. This produces
627 wrong results when, e.g., there's only one possible
628 completion, because rl_complete will prepend "/foo/" to each
629 candidate completion. The loop below removes that leading
631 for (const auto &fn_up
: fn_list
)
633 char *fn
= fn_up
.get ();
634 memmove (fn
, fn
+ (word
- text
), strlen (fn
) + 1 - (word
- text
));
638 tracker
.add_completions (std::move (fn_list
));
640 if (!tracker
.have_completions ())
642 /* No completions at all. As the final resort, try completing
643 on the entire text as a symbol. */
644 collect_symbol_completion_matches (tracker
,
645 complete_symbol_mode::EXPRESSION
,
646 symbol_name_match_type::EXPRESSION
,
651 /* See completer.h. */
654 complete_source_filenames (const char *text
)
656 size_t text_len
= strlen (text
);
658 /* If text includes characters which cannot appear in a file name,
659 the user cannot be asking for completion on files. */
661 gdb_completer_file_name_break_characters
)
663 return make_source_files_completion_list (text
, text
);
668 /* Complete address and linespec locations. */
671 complete_address_and_linespec_locations (completion_tracker
&tracker
,
673 symbol_name_match_type match_type
)
677 tracker
.advance_custom_word_point_by (1);
680 = advance_to_expression_complete_word_point (tracker
, text
);
681 complete_expression (tracker
, text
, word
);
685 linespec_complete (tracker
, text
, match_type
);
689 /* The explicit location options. Note that indexes into this array
690 must match the explicit_location_match_type enumerators. */
692 static const char *const explicit_options
[] =
702 /* The probe modifier options. These can appear before a location in
703 breakpoint commands. */
704 static const char *const probe_options
[] =
712 /* Returns STRING if not NULL, the empty string otherwise. */
715 string_or_empty (const char *string
)
717 return string
!= NULL
? string
: "";
720 /* A helper function to collect explicit location matches for the given
721 LOCATION, which is attempting to match on WORD. */
724 collect_explicit_location_matches (completion_tracker
&tracker
,
725 location_spec
*locspec
,
726 enum explicit_location_match_type what
,
728 const struct language_defn
*language
)
730 const explicit_location_spec
*explicit_loc
731 = as_explicit_location_spec (locspec
);
733 /* True if the option expects an argument. */
734 bool needs_arg
= true;
736 /* Note, in the various MATCH_* below, we complete on
737 explicit_loc->foo instead of WORD, because only the former will
738 have already skipped past any quote char. */
744 = string_or_empty (explicit_loc
->source_filename
.get ());
745 completion_list matches
746 = make_source_files_completion_list (source
, source
);
747 tracker
.add_completions (std::move (matches
));
754 = string_or_empty (explicit_loc
->function_name
.get ());
755 linespec_complete_function (tracker
, function
,
756 explicit_loc
->func_name_match_type
,
757 explicit_loc
->source_filename
.get ());
761 case MATCH_QUALIFIED
:
765 /* Nothing to offer. */
770 const char *label
= string_or_empty (explicit_loc
->label_name
.get ());
771 linespec_complete_label (tracker
, language
,
772 explicit_loc
->source_filename
.get (),
773 explicit_loc
->function_name
.get (),
774 explicit_loc
->func_name_match_type
,
780 gdb_assert_not_reached ("unhandled explicit_location_match_type");
783 if (!needs_arg
|| tracker
.completes_to_completion_word (word
))
785 tracker
.discard_completions ();
786 tracker
.advance_custom_word_point_by (strlen (word
));
787 complete_on_enum (tracker
, explicit_options
, "", "");
788 complete_on_enum (tracker
, linespec_keywords
, "", "");
790 else if (!tracker
.have_completions ())
792 /* Maybe we have an unterminated linespec keyword at the tail of
793 the string. Try completing on that. */
794 size_t wordlen
= strlen (word
);
795 const char *keyword
= word
+ wordlen
;
797 if (wordlen
> 0 && keyword
[-1] != ' ')
799 while (keyword
> word
&& *keyword
!= ' ')
801 /* Don't complete on keywords if we'd be completing on the
802 whole explicit linespec option. E.g., "b -function
803 thr<tab>" should not complete to the "thread"
807 keyword
= skip_spaces (keyword
);
809 tracker
.advance_custom_word_point_by (keyword
- word
);
810 complete_on_enum (tracker
, linespec_keywords
, keyword
, keyword
);
813 else if (wordlen
> 0 && keyword
[-1] == ' ')
815 /* Assume that we're maybe past the explicit location
816 argument, and we didn't manage to find any match because
817 the user wants to create a pending breakpoint. Offer the
818 keyword and explicit location options as possible
820 tracker
.advance_custom_word_point_by (keyword
- word
);
821 complete_on_enum (tracker
, linespec_keywords
, keyword
, keyword
);
822 complete_on_enum (tracker
, explicit_options
, keyword
, keyword
);
827 /* If the next word in *TEXT_P is any of the keywords in KEYWORDS,
828 then advance both TEXT_P and the word point in the tracker past the
829 keyword and return the (0-based) index in the KEYWORDS array that
830 matched. Otherwise, return -1. */
833 skip_keyword (completion_tracker
&tracker
,
834 const char * const *keywords
, const char **text_p
)
836 const char *text
= *text_p
;
837 const char *after
= skip_to_space (text
);
838 size_t len
= after
- text
;
840 if (text
[len
] != ' ')
844 for (int i
= 0; keywords
[i
] != NULL
; i
++)
846 if (strncmp (keywords
[i
], text
, len
) == 0)
857 tracker
.advance_custom_word_point_by (len
+ 1);
866 /* A completer function for explicit location specs. This function
867 completes both options ("-source", "-line", etc) and values. If
868 completing a quoted string, then QUOTED_ARG_START and
869 QUOTED_ARG_END point to the quote characters. LANGUAGE is the
873 complete_explicit_location_spec (completion_tracker
&tracker
,
874 location_spec
*locspec
,
876 const language_defn
*language
,
877 const char *quoted_arg_start
,
878 const char *quoted_arg_end
)
883 int keyword
= skip_keyword (tracker
, explicit_options
, &text
);
887 complete_on_enum (tracker
, explicit_options
, text
, text
);
888 /* There are keywords that start with "-". Include them, too. */
889 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
893 /* Completing on value. */
894 enum explicit_location_match_type what
895 = (explicit_location_match_type
) keyword
;
897 if (quoted_arg_start
!= NULL
&& quoted_arg_end
!= NULL
)
899 if (quoted_arg_end
[1] == '\0')
901 /* If completing a quoted string with the cursor right
902 at the terminating quote char, complete the
903 completion word without interpretation, so that
904 readline advances the cursor one whitespace past the
905 quote, even if there's no match. This makes these
906 cases behave the same:
908 before: "b -function function()"
909 after: "b -function function() "
911 before: "b -function 'function()'"
912 after: "b -function 'function()' "
914 and trusts the user in this case:
916 before: "b -function 'not_loaded_function_yet()'"
917 after: "b -function 'not_loaded_function_yet()' "
919 tracker
.add_completion (make_unique_xstrdup (text
));
921 else if (quoted_arg_end
[1] == ' ')
923 /* We're maybe past the explicit location argument.
924 Skip the argument without interpretation, assuming the
925 user may want to create pending breakpoint. Offer
926 the keyword and explicit location options as possible
928 tracker
.advance_custom_word_point_by (strlen (text
));
929 complete_on_enum (tracker
, linespec_keywords
, "", "");
930 complete_on_enum (tracker
, explicit_options
, "", "");
935 /* Now gather matches */
936 collect_explicit_location_matches (tracker
, locspec
, what
, text
,
941 /* A completer for locations. */
944 location_completer (struct cmd_list_element
*ignore
,
945 completion_tracker
&tracker
,
946 const char *text
, const char * /* word */)
948 int found_probe_option
= -1;
950 /* If we have a probe modifier, skip it. This can only appear as
951 first argument. Until we have a specific completer for probes,
952 falling back to the linespec completer for the remainder of the
953 line is better than nothing. */
954 if (text
[0] == '-' && text
[1] == 'p')
955 found_probe_option
= skip_keyword (tracker
, probe_options
, &text
);
957 const char *option_text
= text
;
958 int saved_word_point
= tracker
.custom_word_point ();
960 const char *copy
= text
;
962 explicit_completion_info completion_info
;
963 location_spec_up locspec
964 = string_to_explicit_location_spec (©
, current_language
,
966 if (completion_info
.quoted_arg_start
!= NULL
967 && completion_info
.quoted_arg_end
== NULL
)
969 /* Found an unbalanced quote. */
970 tracker
.set_quote_char (*completion_info
.quoted_arg_start
);
971 tracker
.advance_custom_word_point_by (1);
974 if (completion_info
.saw_explicit_location_spec_option
)
978 tracker
.advance_custom_word_point_by (copy
- text
);
981 /* We found a terminator at the tail end of the string,
982 which means we're past the explicit location options. We
983 may have a keyword to complete on. If we have a whole
984 keyword, then complete whatever comes after as an
985 expression. This is mainly for the "if" keyword. If the
986 "thread" and "task" keywords gain their own completers,
987 they should be used here. */
988 int keyword
= skip_keyword (tracker
, linespec_keywords
, &text
);
992 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
997 = advance_to_expression_complete_word_point (tracker
, text
);
998 complete_expression (tracker
, text
, word
);
1003 tracker
.advance_custom_word_point_by (completion_info
.last_option
1005 text
= completion_info
.last_option
;
1007 complete_explicit_location_spec (tracker
, locspec
.get (), text
,
1009 completion_info
.quoted_arg_start
,
1010 completion_info
.quoted_arg_end
);
1014 /* This is an address or linespec location. */
1015 else if (locspec
!= nullptr)
1017 /* Handle non-explicit location options. */
1019 int keyword
= skip_keyword (tracker
, explicit_options
, &text
);
1021 complete_on_enum (tracker
, explicit_options
, text
, text
);
1024 tracker
.advance_custom_word_point_by (copy
- text
);
1027 symbol_name_match_type match_type
1028 = as_explicit_location_spec (locspec
.get ())->func_name_match_type
;
1029 complete_address_and_linespec_locations (tracker
, text
, match_type
);
1035 complete_address_and_linespec_locations (tracker
, text
,
1036 symbol_name_match_type::WILD
);
1039 /* Add matches for option names, if either:
1041 - Some completer above found some matches, but the word point did
1042 not advance (e.g., "b <tab>" finds all functions, or "b -<tab>"
1043 matches all objc selectors), or;
1045 - Some completer above advanced the word point, but found no
1048 if ((text
[0] == '-' || text
[0] == '\0')
1049 && (!tracker
.have_completions ()
1050 || tracker
.custom_word_point () == saved_word_point
))
1052 tracker
.set_custom_word_point (saved_word_point
);
1055 if (found_probe_option
== -1)
1056 complete_on_enum (tracker
, probe_options
, text
, text
);
1057 complete_on_enum (tracker
, explicit_options
, text
, text
);
1061 /* The corresponding completer_handle_brkchars
1065 location_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1066 completion_tracker
&tracker
,
1068 const char *word_ignored
)
1070 tracker
.set_use_custom_word_point (true);
1072 location_completer (ignore
, tracker
, text
, NULL
);
1075 /* See completer.h. */
1078 complete_expression (completion_tracker
&tracker
,
1079 const char *text
, const char *word
)
1082 std::unique_ptr
<expr_completion_base
> expr_completer
;
1084 /* Perform a tentative parse of the expression, to see whether a
1085 field completion is required. */
1088 exp
= parse_expression_for_completion (text
, &expr_completer
);
1090 catch (const gdb_exception_error
&except
)
1095 /* Part of the parse_expression_for_completion contract. */
1096 gdb_assert ((exp
== nullptr) == (expr_completer
== nullptr));
1097 if (expr_completer
!= nullptr
1098 && expr_completer
->complete (exp
.get (), tracker
))
1101 complete_files_symbols (tracker
, text
, word
);
1104 /* Complete on expressions. Often this means completing on symbol
1105 names, but some language parsers also have support for completing
1109 expression_completer (struct cmd_list_element
*ignore
,
1110 completion_tracker
&tracker
,
1111 const char *text
, const char *word
)
1113 complete_expression (tracker
, text
, word
);
1116 /* See definition in completer.h. */
1119 set_rl_completer_word_break_characters (const char *break_chars
)
1121 rl_completer_word_break_characters
= (char *) break_chars
;
1124 /* Complete on symbols. */
1127 symbol_completer (struct cmd_list_element
*ignore
,
1128 completion_tracker
&tracker
,
1129 const char *text
, const char *word
)
1131 collect_symbol_completion_matches (tracker
, complete_symbol_mode::EXPRESSION
,
1132 symbol_name_match_type::EXPRESSION
,
1136 /* Here are some useful test cases for completion. FIXME: These
1137 should be put in the test suite. They should be tested with both
1140 "show output-" "radix"
1141 "show output" "-radix"
1142 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1143 "p " ambiguous (all symbols)
1144 "info t foo" no completions
1145 "info t " no completions
1146 "info t" ambiguous ("info target", "info terminal", etc.)
1147 "info ajksdlfk" no completions
1148 "info ajksdlfk " no completions
1150 "info " ambiguous (all info commands)
1151 "p \"a" no completions (string constant)
1152 "p 'a" ambiguous (all symbols starting with a)
1153 "p b-a" ambiguous (all symbols starting with a)
1154 "p b-" ambiguous (all symbols)
1155 "file Make" "file" (word break hard to screw up here)
1156 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1159 enum complete_line_internal_reason
1161 /* Preliminary phase, called by gdb_completion_word_break_characters
1162 function, is used to either:
1164 #1 - Determine the set of chars that are word delimiters
1165 depending on the current command in line_buffer.
1167 #2 - Manually advance RL_POINT to the "word break" point instead
1168 of letting readline do it (based on too-simple character
1171 Simpler completers that just pass a brkchars array to readline
1172 (#1 above) must defer generating the completions to the main
1173 phase (below). No completion list should be generated in this
1176 OTOH, completers that manually advance the word point(#2 above)
1177 must set "use_custom_word_point" in the tracker and generate
1178 their completion in this phase. Note that this is the convenient
1179 thing to do since they'll be parsing the input line anyway. */
1182 /* Main phase, called by complete_line function, is used to get the
1183 list of possible completions. */
1186 /* Special case when completing a 'help' command. In this case,
1187 once sub-command completions are exhausted, we simply return
1192 /* Helper for complete_line_internal to simplify it. */
1195 complete_line_internal_normal_command (completion_tracker
&tracker
,
1196 const char *command
, const char *word
,
1197 const char *cmd_args
,
1198 complete_line_internal_reason reason
,
1199 struct cmd_list_element
*c
)
1201 const char *p
= cmd_args
;
1203 if (c
->completer
== filename_completer
)
1205 /* Many commands which want to complete on file names accept
1206 several file names, as in "run foo bar >>baz". So we don't
1207 want to complete the entire text after the command, just the
1208 last word. To this end, we need to find the beginning of the
1209 file name by starting at `word' and going backwards. */
1212 && strchr (gdb_completer_file_name_break_characters
,
1218 if (reason
== handle_brkchars
)
1220 completer_handle_brkchars_ftype
*brkchars_fn
;
1222 if (c
->completer_handle_brkchars
!= NULL
)
1223 brkchars_fn
= c
->completer_handle_brkchars
;
1227 = (completer_handle_brkchars_func_for_completer
1231 brkchars_fn (c
, tracker
, p
, word
);
1234 if (reason
!= handle_brkchars
&& c
->completer
!= NULL
)
1235 (*c
->completer
) (c
, tracker
, p
, word
);
1238 /* Internal function used to handle completions.
1241 TEXT is the caller's idea of the "word" we are looking at.
1243 LINE_BUFFER is available to be looked at; it contains the entire
1244 text of the line. POINT is the offset in that line of the cursor.
1245 You should pretend that the line ends at POINT.
1247 See complete_line_internal_reason for description of REASON. */
1250 complete_line_internal_1 (completion_tracker
&tracker
,
1252 const char *line_buffer
, int point
,
1253 complete_line_internal_reason reason
)
1257 int ignore_help_classes
;
1258 /* Pointer within tmp_command which corresponds to text. */
1260 struct cmd_list_element
*c
, *result_list
;
1262 /* Choose the default set of word break characters to break
1263 completions. If we later find out that we are doing completions
1264 on command strings (as opposed to strings supplied by the
1265 individual command completer functions, which can be any string)
1266 then we will switch to the special word break set for command
1267 strings, which leaves out the '-' and '.' character used in some
1269 set_rl_completer_word_break_characters
1270 (current_language
->word_break_characters ());
1272 /* Decide whether to complete on a list of gdb commands or on
1274 tmp_command
= (char *) alloca (point
+ 1);
1277 /* The help command should complete help aliases. */
1278 ignore_help_classes
= reason
!= handle_help
;
1280 strncpy (tmp_command
, line_buffer
, point
);
1281 tmp_command
[point
] = '\0';
1282 if (reason
== handle_brkchars
)
1284 gdb_assert (text
== NULL
);
1289 /* Since text always contains some number of characters leading up
1290 to point, we can find the equivalent position in tmp_command
1291 by subtracting that many characters from the end of tmp_command. */
1292 word
= tmp_command
+ point
- strlen (text
);
1295 /* Move P up to the start of the command. */
1296 p
= skip_spaces (p
);
1300 /* An empty line is ambiguous; that is, it could be any
1302 c
= CMD_LIST_AMBIGUOUS
;
1306 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, NULL
, ignore_help_classes
,
1309 /* Move p up to the next interesting thing. */
1310 while (*p
== ' ' || *p
== '\t')
1315 tracker
.advance_custom_word_point_by (p
- tmp_command
);
1319 /* It is an unrecognized command. So there are no
1320 possible completions. */
1322 else if (c
== CMD_LIST_AMBIGUOUS
)
1326 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1327 doesn't advance over that thing itself. Do so now. */
1329 while (valid_cmd_char_p (*q
))
1331 if (q
!= tmp_command
+ point
)
1333 /* There is something beyond the ambiguous
1334 command, so there are no possible completions. For
1335 example, "info t " or "info t foo" does not complete
1336 to anything, because "info t" can be "info target" or
1341 /* We're trying to complete on the command which was ambiguous.
1342 This we can deal with. */
1345 if (reason
!= handle_brkchars
)
1346 complete_on_cmdlist (*result_list
->subcommands
, tracker
, p
,
1347 word
, ignore_help_classes
);
1351 if (reason
!= handle_brkchars
)
1352 complete_on_cmdlist (cmdlist
, tracker
, p
, word
,
1353 ignore_help_classes
);
1355 /* Ensure that readline does the right thing with respect to
1356 inserting quotes. */
1357 set_rl_completer_word_break_characters
1358 (gdb_completer_command_word_break_characters
);
1363 /* We've recognized a full command. */
1365 if (p
== tmp_command
+ point
)
1367 /* There is no non-whitespace in the line beyond the
1370 if (p
[-1] == ' ' || p
[-1] == '\t')
1372 /* The command is followed by whitespace; we need to
1373 complete on whatever comes after command. */
1374 if (c
->is_prefix ())
1376 /* It is a prefix command; what comes after it is
1377 a subcommand (e.g. "info "). */
1378 if (reason
!= handle_brkchars
)
1379 complete_on_cmdlist (*c
->subcommands
, tracker
, p
, word
,
1380 ignore_help_classes
);
1382 /* Ensure that readline does the right thing
1383 with respect to inserting quotes. */
1384 set_rl_completer_word_break_characters
1385 (gdb_completer_command_word_break_characters
);
1387 else if (reason
== handle_help
)
1391 if (reason
!= handle_brkchars
)
1392 complete_on_enum (tracker
, c
->enums
, p
, word
);
1393 set_rl_completer_word_break_characters
1394 (gdb_completer_command_word_break_characters
);
1398 /* It is a normal command; what comes after it is
1399 completed by the command's completer function. */
1400 complete_line_internal_normal_command (tracker
,
1401 tmp_command
, word
, p
,
1407 /* The command is not followed by whitespace; we need to
1408 complete on the command itself, e.g. "p" which is a
1409 command itself but also can complete to "print", "ptype"
1413 /* Find the command we are completing on. */
1415 while (q
> tmp_command
)
1417 if (valid_cmd_char_p (q
[-1]))
1423 /* Move the custom word point back too. */
1424 tracker
.advance_custom_word_point_by (q
- p
);
1426 if (reason
!= handle_brkchars
)
1427 complete_on_cmdlist (result_list
, tracker
, q
, word
,
1428 ignore_help_classes
);
1430 /* Ensure that readline does the right thing
1431 with respect to inserting quotes. */
1432 set_rl_completer_word_break_characters
1433 (gdb_completer_command_word_break_characters
);
1436 else if (reason
== handle_help
)
1440 /* There is non-whitespace beyond the command. */
1442 if (c
->is_prefix () && !c
->allow_unknown
)
1444 /* It is an unrecognized subcommand of a prefix command,
1445 e.g. "info adsfkdj". */
1449 if (reason
!= handle_brkchars
)
1450 complete_on_enum (tracker
, c
->enums
, p
, word
);
1454 /* It is a normal command. */
1455 complete_line_internal_normal_command (tracker
,
1456 tmp_command
, word
, p
,
1463 /* Wrapper around complete_line_internal_1 to handle
1464 MAX_COMPLETIONS_REACHED_ERROR. */
1467 complete_line_internal (completion_tracker
&tracker
,
1469 const char *line_buffer
, int point
,
1470 complete_line_internal_reason reason
)
1474 complete_line_internal_1 (tracker
, text
, line_buffer
, point
, reason
);
1476 catch (const gdb_exception_error
&except
)
1478 if (except
.error
!= MAX_COMPLETIONS_REACHED_ERROR
)
1483 /* See completer.h. */
1485 int max_completions
= 200;
1487 /* Initial size of the table. It automagically grows from here. */
1488 #define INITIAL_COMPLETION_HTAB_SIZE 200
1490 /* See completer.h. */
1492 completion_tracker::completion_tracker (bool from_readline
)
1493 : m_from_readline (from_readline
)
1495 discard_completions ();
1498 /* See completer.h. */
1501 completion_tracker::discard_completions ()
1503 xfree (m_lowest_common_denominator
);
1504 m_lowest_common_denominator
= NULL
;
1506 m_lowest_common_denominator_unique
= false;
1507 m_lowest_common_denominator_valid
= false;
1509 m_entries_hash
.reset (nullptr);
1511 /* A callback used by the hash table to compare new entries with existing
1512 entries. We can't use the standard htab_eq_string function here as the
1513 key to our hash is just a single string, while the values we store in
1514 the hash are a struct containing multiple strings. */
1515 static auto entry_eq_func
1516 = [] (const void *first
, const void *second
) -> int
1518 /* The FIRST argument is the entry already in the hash table, and
1519 the SECOND argument is the new item being inserted. */
1520 const completion_hash_entry
*entry
1521 = (const completion_hash_entry
*) first
;
1522 const char *name_str
= (const char *) second
;
1524 return entry
->is_name_eq (name_str
);
1527 /* Callback used by the hash table to compute the hash value for an
1528 existing entry. This is needed when expanding the hash table. */
1529 static auto entry_hash_func
1530 = [] (const void *arg
) -> hashval_t
1532 const completion_hash_entry
*entry
1533 = (const completion_hash_entry
*) arg
;
1534 return entry
->hash_name ();
1537 m_entries_hash
.reset
1538 (htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE
,
1539 entry_hash_func
, entry_eq_func
,
1540 htab_delete_entry
<completion_hash_entry
>,
1544 /* See completer.h. */
1546 completion_tracker::~completion_tracker ()
1548 xfree (m_lowest_common_denominator
);
1551 /* See completer.h. */
1554 completion_tracker::maybe_add_completion
1555 (gdb::unique_xmalloc_ptr
<char> name
,
1556 completion_match_for_lcd
*match_for_lcd
,
1557 const char *text
, const char *word
)
1561 if (max_completions
== 0)
1564 if (htab_elements (m_entries_hash
.get ()) >= max_completions
)
1567 hashval_t hash
= htab_hash_string (name
.get ());
1568 slot
= htab_find_slot_with_hash (m_entries_hash
.get (), name
.get (),
1570 if (*slot
== HTAB_EMPTY_ENTRY
)
1572 const char *match_for_lcd_str
= NULL
;
1574 if (match_for_lcd
!= NULL
)
1575 match_for_lcd_str
= match_for_lcd
->finish ();
1577 if (match_for_lcd_str
== NULL
)
1578 match_for_lcd_str
= name
.get ();
1580 gdb::unique_xmalloc_ptr
<char> lcd
1581 = make_completion_match_str (match_for_lcd_str
, text
, word
);
1583 size_t lcd_len
= strlen (lcd
.get ());
1584 *slot
= new completion_hash_entry (std::move (name
), std::move (lcd
));
1586 m_lowest_common_denominator_valid
= false;
1587 m_lowest_common_denominator_max_length
1588 = std::max (m_lowest_common_denominator_max_length
, lcd_len
);
1594 /* See completer.h. */
1597 completion_tracker::add_completion (gdb::unique_xmalloc_ptr
<char> name
,
1598 completion_match_for_lcd
*match_for_lcd
,
1599 const char *text
, const char *word
)
1601 if (!maybe_add_completion (std::move (name
), match_for_lcd
, text
, word
))
1602 throw_error (MAX_COMPLETIONS_REACHED_ERROR
, _("Max completions reached."));
1605 /* See completer.h. */
1608 completion_tracker::add_completions (completion_list
&&list
)
1610 for (auto &candidate
: list
)
1611 add_completion (std::move (candidate
));
1614 /* See completer.h. */
1617 completion_tracker::remove_completion (const char *name
)
1619 hashval_t hash
= htab_hash_string (name
);
1620 if (htab_find_slot_with_hash (m_entries_hash
.get (), name
, hash
, NO_INSERT
)
1623 htab_remove_elt_with_hash (m_entries_hash
.get (), name
, hash
);
1624 m_lowest_common_denominator_valid
= false;
1628 /* Helper for the make_completion_match_str overloads. Returns NULL
1629 as an indication that we want MATCH_NAME exactly. It is up to the
1630 caller to xstrdup that string if desired. */
1633 make_completion_match_str_1 (const char *match_name
,
1634 const char *text
, const char *word
)
1640 /* Return NULL as an indication that we want MATCH_NAME
1644 else if (word
> text
)
1646 /* Return some portion of MATCH_NAME. */
1647 newobj
= xstrdup (match_name
+ (word
- text
));
1651 /* Return some of WORD plus MATCH_NAME. */
1652 size_t len
= strlen (match_name
);
1653 newobj
= (char *) xmalloc (text
- word
+ len
+ 1);
1654 memcpy (newobj
, word
, text
- word
);
1655 memcpy (newobj
+ (text
- word
), match_name
, len
+ 1);
1661 /* See completer.h. */
1663 gdb::unique_xmalloc_ptr
<char>
1664 make_completion_match_str (const char *match_name
,
1665 const char *text
, const char *word
)
1667 char *newobj
= make_completion_match_str_1 (match_name
, text
, word
);
1669 newobj
= xstrdup (match_name
);
1670 return gdb::unique_xmalloc_ptr
<char> (newobj
);
1673 /* See completer.h. */
1675 gdb::unique_xmalloc_ptr
<char>
1676 make_completion_match_str (gdb::unique_xmalloc_ptr
<char> &&match_name
,
1677 const char *text
, const char *word
)
1679 char *newobj
= make_completion_match_str_1 (match_name
.get (), text
, word
);
1681 return std::move (match_name
);
1682 return gdb::unique_xmalloc_ptr
<char> (newobj
);
1685 /* See complete.h. */
1688 complete (const char *line
, char const **word
, int *quote_char
)
1690 completion_tracker
tracker_handle_brkchars (false);
1691 completion_tracker
tracker_handle_completions (false);
1692 completion_tracker
*tracker
;
1694 /* The WORD should be set to the end of word to complete. We initialize
1695 to the completion point which is assumed to be at the end of LINE.
1696 This leaves WORD to be initialized to a sensible value in cases
1697 completion_find_completion_word() fails i.e., throws an exception.
1699 *word
= line
+ strlen (line
);
1703 *word
= completion_find_completion_word (tracker_handle_brkchars
,
1706 /* Completers that provide a custom word point in the
1707 handle_brkchars phase also compute their completions then.
1708 Completers that leave the completion word handling to readline
1709 must be called twice. */
1710 if (tracker_handle_brkchars
.use_custom_word_point ())
1711 tracker
= &tracker_handle_brkchars
;
1714 complete_line (tracker_handle_completions
, *word
, line
, strlen (line
));
1715 tracker
= &tracker_handle_completions
;
1718 catch (const gdb_exception
&ex
)
1723 return tracker
->build_completion_result (*word
, *word
- line
, strlen (line
));
1727 /* Generate completions all at once. Does nothing if max_completions
1728 is 0. If max_completions is non-negative, this will collect at
1729 most max_completions strings.
1731 TEXT is the caller's idea of the "word" we are looking at.
1733 LINE_BUFFER is available to be looked at; it contains the entire
1736 POINT is the offset in that line of the cursor. You
1737 should pretend that the line ends at POINT. */
1740 complete_line (completion_tracker
&tracker
,
1741 const char *text
, const char *line_buffer
, int point
)
1743 if (max_completions
== 0)
1745 complete_line_internal (tracker
, text
, line_buffer
, point
,
1746 handle_completions
);
1749 /* Complete on command names. Used by "help". */
1752 command_completer (struct cmd_list_element
*ignore
,
1753 completion_tracker
&tracker
,
1754 const char *text
, const char *word
)
1756 complete_line_internal (tracker
, word
, text
,
1757 strlen (text
), handle_help
);
1760 /* The corresponding completer_handle_brkchars implementation. */
1763 command_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1764 completion_tracker
&tracker
,
1765 const char *text
, const char *word
)
1767 set_rl_completer_word_break_characters
1768 (gdb_completer_command_word_break_characters
);
1771 /* Complete on signals. */
1774 signal_completer (struct cmd_list_element
*ignore
,
1775 completion_tracker
&tracker
,
1776 const char *text
, const char *word
)
1778 size_t len
= strlen (word
);
1780 const char *signame
;
1782 for (signum
= GDB_SIGNAL_FIRST
; signum
!= GDB_SIGNAL_LAST
; ++signum
)
1784 /* Can't handle this, so skip it. */
1785 if (signum
== GDB_SIGNAL_0
)
1788 signame
= gdb_signal_to_name ((enum gdb_signal
) signum
);
1790 /* Ignore the unknown signal case. */
1791 if (!signame
|| strcmp (signame
, "?") == 0)
1794 if (strncasecmp (signame
, word
, len
) == 0)
1795 tracker
.add_completion (make_unique_xstrdup (signame
));
1799 /* Bit-flags for selecting what the register and/or register-group
1800 completer should complete on. */
1802 enum reg_completer_target
1804 complete_register_names
= 0x1,
1805 complete_reggroup_names
= 0x2
1807 DEF_ENUM_FLAGS_TYPE (enum reg_completer_target
, reg_completer_targets
);
1809 /* Complete register names and/or reggroup names based on the value passed
1810 in TARGETS. At least one bit in TARGETS must be set. */
1813 reg_or_group_completer_1 (completion_tracker
&tracker
,
1814 const char *text
, const char *word
,
1815 reg_completer_targets targets
)
1817 size_t len
= strlen (word
);
1818 struct gdbarch
*gdbarch
;
1821 gdb_assert ((targets
& (complete_register_names
1822 | complete_reggroup_names
)) != 0);
1823 gdbarch
= get_current_arch ();
1825 if ((targets
& complete_register_names
) != 0)
1830 (name
= user_reg_map_regnum_to_name (gdbarch
, i
)) != NULL
;
1833 if (*name
!= '\0' && strncmp (word
, name
, len
) == 0)
1834 tracker
.add_completion (make_unique_xstrdup (name
));
1838 if ((targets
& complete_reggroup_names
) != 0)
1840 for (const struct reggroup
*group
: gdbarch_reggroups (gdbarch
))
1842 name
= group
->name ();
1843 if (strncmp (word
, name
, len
) == 0)
1844 tracker
.add_completion (make_unique_xstrdup (name
));
1849 /* Perform completion on register and reggroup names. */
1852 reg_or_group_completer (struct cmd_list_element
*ignore
,
1853 completion_tracker
&tracker
,
1854 const char *text
, const char *word
)
1856 reg_or_group_completer_1 (tracker
, text
, word
,
1857 (complete_register_names
1858 | complete_reggroup_names
));
1861 /* Perform completion on reggroup names. */
1864 reggroup_completer (struct cmd_list_element
*ignore
,
1865 completion_tracker
&tracker
,
1866 const char *text
, const char *word
)
1868 reg_or_group_completer_1 (tracker
, text
, word
,
1869 complete_reggroup_names
);
1872 /* The default completer_handle_brkchars implementation. */
1875 default_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1876 completion_tracker
&tracker
,
1877 const char *text
, const char *word
)
1879 set_rl_completer_word_break_characters
1880 (current_language
->word_break_characters ());
1883 /* See definition in completer.h. */
1885 completer_handle_brkchars_ftype
*
1886 completer_handle_brkchars_func_for_completer (completer_ftype
*fn
)
1888 if (fn
== filename_completer
)
1889 return filename_completer_handle_brkchars
;
1891 if (fn
== location_completer
)
1892 return location_completer_handle_brkchars
;
1894 if (fn
== command_completer
)
1895 return command_completer_handle_brkchars
;
1897 return default_completer_handle_brkchars
;
1900 /* Used as brkchars when we want to tell readline we have a custom
1901 word point. We do that by making our rl_completion_word_break_hook
1902 set RL_POINT to the desired word point, and return the character at
1903 the word break point as the break char. This is two bytes in order
1904 to fit one break character plus the terminating null. */
1905 static char gdb_custom_word_point_brkchars
[2];
1907 /* Since rl_basic_quote_characters is not completer-specific, we save
1908 its original value here, in order to be able to restore it in
1909 gdb_rl_attempted_completion_function. */
1910 static const char *gdb_org_rl_basic_quote_characters
= rl_basic_quote_characters
;
1912 /* Get the list of chars that are considered as word breaks
1913 for the current command. */
1916 gdb_completion_word_break_characters_throw ()
1918 /* New completion starting. Get rid of the previous tracker and
1920 delete current_completion
.tracker
;
1921 current_completion
.tracker
= new completion_tracker (true);
1923 completion_tracker
&tracker
= *current_completion
.tracker
;
1925 complete_line_internal (tracker
, NULL
, rl_line_buffer
,
1926 rl_point
, handle_brkchars
);
1928 if (tracker
.use_custom_word_point ())
1930 gdb_assert (tracker
.custom_word_point () > 0);
1931 rl_point
= tracker
.custom_word_point () - 1;
1933 gdb_assert (rl_point
>= 0 && rl_point
< strlen (rl_line_buffer
));
1935 gdb_custom_word_point_brkchars
[0] = rl_line_buffer
[rl_point
];
1936 rl_completer_word_break_characters
= gdb_custom_word_point_brkchars
;
1937 rl_completer_quote_characters
= NULL
;
1939 /* Clear this too, so that if we're completing a quoted string,
1940 readline doesn't consider the quote character a delimiter.
1941 If we didn't do this, readline would auto-complete {b
1942 'fun<tab>} to {'b 'function()'}, i.e., add the terminating
1943 \', but, it wouldn't append the separator space either, which
1944 is not desirable. So instead we take care of appending the
1945 quote character to the LCD ourselves, in
1946 gdb_rl_attempted_completion_function. Since this global is
1947 not just completer-specific, we'll restore it back to the
1948 default in gdb_rl_attempted_completion_function. */
1949 rl_basic_quote_characters
= NULL
;
1952 return (char *) rl_completer_word_break_characters
;
1956 gdb_completion_word_break_characters ()
1958 /* New completion starting. */
1959 current_completion
.aborted
= false;
1963 return gdb_completion_word_break_characters_throw ();
1965 catch (const gdb_exception
&ex
)
1967 /* Set this to that gdb_rl_attempted_completion_function knows
1969 current_completion
.aborted
= true;
1975 /* See completer.h. */
1978 completion_find_completion_word (completion_tracker
&tracker
, const char *text
,
1981 size_t point
= strlen (text
);
1983 complete_line_internal (tracker
, NULL
, text
, point
, handle_brkchars
);
1985 if (tracker
.use_custom_word_point ())
1987 gdb_assert (tracker
.custom_word_point () > 0);
1988 *quote_char
= tracker
.quote_char ();
1989 return text
+ tracker
.custom_word_point ();
1992 gdb_rl_completion_word_info info
;
1994 info
.word_break_characters
= rl_completer_word_break_characters
;
1995 info
.quote_characters
= gdb_completer_quote_characters
;
1996 info
.basic_quote_characters
= rl_basic_quote_characters
;
1998 return gdb_rl_find_completion_word (&info
, quote_char
, NULL
, text
);
2001 /* See completer.h. */
2004 completion_tracker::recompute_lcd_visitor (completion_hash_entry
*entry
)
2006 if (!m_lowest_common_denominator_valid
)
2008 /* This is the first lowest common denominator that we are
2009 considering, just copy it in. */
2010 strcpy (m_lowest_common_denominator
, entry
->get_lcd ());
2011 m_lowest_common_denominator_unique
= true;
2012 m_lowest_common_denominator_valid
= true;
2016 /* Find the common denominator between the currently-known lowest
2017 common denominator and NEW_MATCH_UP. That becomes the new lowest
2018 common denominator. */
2020 const char *new_match
= entry
->get_lcd ();
2023 (new_match
[i
] != '\0'
2024 && new_match
[i
] == m_lowest_common_denominator
[i
]);
2027 if (m_lowest_common_denominator
[i
] != new_match
[i
])
2029 m_lowest_common_denominator
[i
] = '\0';
2030 m_lowest_common_denominator_unique
= false;
2035 /* See completer.h. */
2038 completion_tracker::recompute_lowest_common_denominator ()
2040 /* We've already done this. */
2041 if (m_lowest_common_denominator_valid
)
2044 /* Resize the storage to ensure we have enough space, the plus one gives
2045 us space for the trailing null terminator we will include. */
2046 m_lowest_common_denominator
2047 = (char *) xrealloc (m_lowest_common_denominator
,
2048 m_lowest_common_denominator_max_length
+ 1);
2050 /* Callback used to visit each entry in the m_entries_hash. */
2052 = [] (void **slot
, void *info
) -> int
2054 completion_tracker
*obj
= (completion_tracker
*) info
;
2055 completion_hash_entry
*entry
= (completion_hash_entry
*) *slot
;
2056 obj
->recompute_lcd_visitor (entry
);
2060 htab_traverse (m_entries_hash
.get (), visitor_func
, this);
2061 m_lowest_common_denominator_valid
= true;
2064 /* See completer.h. */
2067 completion_tracker::advance_custom_word_point_by (int len
)
2069 m_custom_word_point
+= len
;
2072 /* Build a new C string that is a copy of LCD with the whitespace of
2073 ORIG/ORIG_LEN preserved.
2075 Say the user is completing a symbol name, with spaces, like:
2079 and the resulting completion match is:
2083 we want to end up with an input line like:
2086 ^^^^^^^ => text from LCD [1], whitespace from ORIG preserved.
2087 ^^ => new text from LCD
2089 [1] - We must take characters from the LCD instead of the original
2090 text, since some completions want to change upper/lowercase. E.g.:
2096 "handle SIG[QUIT|etc.]"
2100 expand_preserving_ws (const char *orig
, size_t orig_len
,
2103 const char *p_orig
= orig
;
2104 const char *orig_end
= orig
+ orig_len
;
2105 const char *p_lcd
= lcd
;
2108 while (p_orig
< orig_end
)
2112 while (p_orig
< orig_end
&& *p_orig
== ' ')
2114 p_lcd
= skip_spaces (p_lcd
);
2118 /* Take characters from the LCD instead of the original
2119 text, since some completions change upper/lowercase.
2123 "handle SIG[QUIT|etc.]"
2131 while (*p_lcd
!= '\0')
2134 return xstrdup (res
.c_str ());
2137 /* See completer.h. */
2140 completion_tracker::build_completion_result (const char *text
,
2143 size_t element_count
= htab_elements (m_entries_hash
.get ());
2145 if (element_count
== 0)
2148 /* +1 for the LCD, and +1 for NULL termination. */
2149 char **match_list
= XNEWVEC (char *, 1 + element_count
+ 1);
2151 /* Build replacement word, based on the LCD. */
2153 recompute_lowest_common_denominator ();
2155 = expand_preserving_ws (text
, end
- start
,
2156 m_lowest_common_denominator
);
2158 if (m_lowest_common_denominator_unique
)
2160 /* We don't rely on readline appending the quote char as
2161 delimiter as then readline wouldn't append the ' ' after the
2163 char buf
[2] = { (char) quote_char () };
2165 match_list
[0] = reconcat (match_list
[0], match_list
[0],
2166 buf
, (char *) NULL
);
2167 match_list
[1] = NULL
;
2169 /* If the tracker wants to, or we already have a space at the
2170 end of the match, tell readline to skip appending
2172 char *match
= match_list
[0];
2173 bool completion_suppress_append
2174 = (suppress_append_ws ()
2175 || (match
[0] != '\0'
2176 && match
[strlen (match
) - 1] == ' '));
2178 return completion_result (match_list
, 1, completion_suppress_append
);
2182 /* State object used while building the completion list. */
2185 list_builder (char **ml
)
2190 /* The list we are filling. */
2193 /* The next index in the list to write to. */
2196 list_builder
builder (match_list
);
2198 /* Visit each entry in m_entries_hash and add it to the completion
2199 list, updating the builder state object. */
2201 = [] (void **slot
, void *info
) -> int
2203 completion_hash_entry
*entry
= (completion_hash_entry
*) *slot
;
2204 list_builder
*state
= (list_builder
*) info
;
2206 state
->match_list
[state
->index
] = entry
->release_name ();
2211 /* Build the completion list and add a null at the end. */
2212 htab_traverse_noresize (m_entries_hash
.get (), func
, &builder
);
2213 match_list
[builder
.index
] = NULL
;
2215 return completion_result (match_list
, builder
.index
- 1, false);
2219 /* See completer.h */
2221 completion_result::completion_result ()
2222 : match_list (NULL
), number_matches (0),
2223 completion_suppress_append (false)
2226 /* See completer.h */
2228 completion_result::completion_result (char **match_list_
,
2229 size_t number_matches_
,
2230 bool completion_suppress_append_
)
2231 : match_list (match_list_
),
2232 number_matches (number_matches_
),
2233 completion_suppress_append (completion_suppress_append_
)
2236 /* See completer.h */
2238 completion_result::~completion_result ()
2240 reset_match_list ();
2243 /* See completer.h */
2245 completion_result::completion_result (completion_result
&&rhs
) noexcept
2246 : match_list (rhs
.match_list
),
2247 number_matches (rhs
.number_matches
)
2249 rhs
.match_list
= NULL
;
2250 rhs
.number_matches
= 0;
2253 /* See completer.h */
2256 completion_result::release_match_list ()
2258 char **ret
= match_list
;
2263 /* See completer.h */
2266 completion_result::sort_match_list ()
2268 if (number_matches
> 1)
2270 /* Element 0 is special (it's the common prefix), leave it
2272 std::sort (&match_list
[1],
2273 &match_list
[number_matches
+ 1],
2278 /* See completer.h */
2281 completion_result::reset_match_list ()
2283 if (match_list
!= NULL
)
2285 for (char **p
= match_list
; *p
!= NULL
; p
++)
2292 /* Helper for gdb_rl_attempted_completion_function, which does most of
2293 the work. This is called by readline to build the match list array
2294 and to determine the lowest common denominator. The real matches
2295 list starts at match[1], while match[0] is the slot holding
2296 readline's idea of the lowest common denominator of all matches,
2297 which is what readline replaces the completion "word" with.
2299 TEXT is the caller's idea of the "word" we are looking at, as
2300 computed in the handle_brkchars phase.
2302 START is the offset from RL_LINE_BUFFER where TEXT starts. END is
2303 the offset from RL_LINE_BUFFER where TEXT ends (i.e., where
2306 You should thus pretend that the line ends at END (relative to
2309 RL_LINE_BUFFER contains the entire text of the line. RL_POINT is
2310 the offset in that line of the cursor. You should pretend that the
2313 Returns NULL if there are no completions. */
2316 gdb_rl_attempted_completion_function_throw (const char *text
, int start
, int end
)
2318 /* Completers that provide a custom word point in the
2319 handle_brkchars phase also compute their completions then.
2320 Completers that leave the completion word handling to readline
2321 must be called twice. If rl_point (i.e., END) is at column 0,
2322 then readline skips the handle_brkchars phase, and so we create a
2323 tracker now in that case too. */
2324 if (end
== 0 || !current_completion
.tracker
->use_custom_word_point ())
2326 delete current_completion
.tracker
;
2327 current_completion
.tracker
= new completion_tracker (true);
2329 complete_line (*current_completion
.tracker
, text
,
2330 rl_line_buffer
, rl_point
);
2333 completion_tracker
&tracker
= *current_completion
.tracker
;
2335 completion_result result
2336 = tracker
.build_completion_result (text
, start
, end
);
2338 rl_completion_suppress_append
= result
.completion_suppress_append
;
2339 return result
.release_match_list ();
2342 /* Function installed as "rl_attempted_completion_function" readline
2343 hook. Wrapper around gdb_rl_attempted_completion_function_throw
2344 that catches C++ exceptions, which can't cross readline. */
2347 gdb_rl_attempted_completion_function (const char *text
, int start
, int end
)
2349 /* Restore globals that might have been tweaked in
2350 gdb_completion_word_break_characters. */
2351 rl_basic_quote_characters
= gdb_org_rl_basic_quote_characters
;
2353 /* If we end up returning NULL, either on error, or simple because
2354 there are no matches, inhibit readline's default filename
2356 rl_attempted_completion_over
= 1;
2358 /* If the handle_brkchars phase was aborted, don't try
2360 if (current_completion
.aborted
)
2365 return gdb_rl_attempted_completion_function_throw (text
, start
, end
);
2367 catch (const gdb_exception
&ex
)
2374 /* Skip over the possibly quoted word STR (as defined by the quote
2375 characters QUOTECHARS and the word break characters BREAKCHARS).
2376 Returns pointer to the location after the "word". If either
2377 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
2381 skip_quoted_chars (const char *str
, const char *quotechars
,
2382 const char *breakchars
)
2384 char quote_char
= '\0';
2387 if (quotechars
== NULL
)
2388 quotechars
= gdb_completer_quote_characters
;
2390 if (breakchars
== NULL
)
2391 breakchars
= current_language
->word_break_characters ();
2393 for (scan
= str
; *scan
!= '\0'; scan
++)
2395 if (quote_char
!= '\0')
2397 /* Ignore everything until the matching close quote char. */
2398 if (*scan
== quote_char
)
2400 /* Found matching close quote. */
2405 else if (strchr (quotechars
, *scan
))
2407 /* Found start of a quoted string. */
2410 else if (strchr (breakchars
, *scan
))
2419 /* Skip over the possibly quoted word STR (as defined by the quote
2420 characters and word break characters used by the completer).
2421 Returns pointer to the location after the "word". */
2424 skip_quoted (const char *str
)
2426 return skip_quoted_chars (str
, NULL
, NULL
);
2429 /* Return a message indicating that the maximum number of completions
2430 has been reached and that there may be more. */
2433 get_max_completions_reached_message (void)
2435 return _("*** List may be truncated, max-completions reached. ***");
2438 /* GDB replacement for rl_display_match_list.
2439 Readline doesn't provide a clean interface for TUI(curses).
2440 A hack previously used was to send readline's rl_outstream through a pipe
2441 and read it from the event loop. Bleah. IWBN if readline abstracted
2442 away all the necessary bits, and this is what this code does. It
2443 replicates the parts of readline we need and then adds an abstraction
2444 layer, currently implemented as struct match_list_displayer, so that both
2445 CLI and TUI can use it. We copy all this readline code to minimize
2446 GDB-specific mods to readline. Once this code performs as desired then
2447 we can submit it to the readline maintainers.
2449 N.B. A lot of the code is the way it is in order to minimize differences
2450 from readline's copy. */
2452 /* Not supported here. */
2453 #undef VISIBLE_STATS
2455 #if defined (HANDLE_MULTIBYTE)
2456 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
2457 #define MB_NULLWCH(x) ((x) == 0)
2460 #define ELLIPSIS_LEN 3
2462 /* gdb version of readline/complete.c:get_y_or_n.
2463 'y' -> returns 1, and 'n' -> returns 0.
2464 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
2465 If FOR_PAGER is non-zero, then also supported are:
2466 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
2469 gdb_get_y_or_n (int for_pager
, const struct match_list_displayer
*displayer
)
2475 RL_SETSTATE (RL_STATE_MOREINPUT
);
2476 c
= displayer
->read_key (displayer
);
2477 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
2479 if (c
== 'y' || c
== 'Y' || c
== ' ')
2481 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
2483 if (c
== ABORT_CHAR
|| c
< 0)
2485 /* Readline doesn't erase_entire_line here, but without it the
2486 --More-- prompt isn't erased and neither is the text entered
2487 thus far redisplayed. */
2488 displayer
->erase_entire_line (displayer
);
2489 /* Note: The arguments to rl_abort are ignored. */
2492 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
2494 if (for_pager
&& (c
== 'q' || c
== 'Q'))
2496 displayer
->beep (displayer
);
2500 /* Pager function for tab-completion.
2501 This is based on readline/complete.c:_rl_internal_pager.
2502 LINES is the number of lines of output displayed thus far.
2504 -1 -> user pressed 'n' or equivalent,
2505 0 -> user pressed 'y' or equivalent,
2506 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
2509 gdb_display_match_list_pager (int lines
,
2510 const struct match_list_displayer
*displayer
)
2514 displayer
->puts (displayer
, "--More--");
2515 displayer
->flush (displayer
);
2516 i
= gdb_get_y_or_n (1, displayer
);
2517 displayer
->erase_entire_line (displayer
);
2526 /* Return non-zero if FILENAME is a directory.
2527 Based on readline/complete.c:path_isdir. */
2530 gdb_path_isdir (const char *filename
)
2534 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
2537 /* Return the portion of PATHNAME that should be output when listing
2538 possible completions. If we are hacking filename completion, we
2539 are only interested in the basename, the portion following the
2540 final slash. Otherwise, we return what we were passed. Since
2541 printing empty strings is not very informative, if we're doing
2542 filename completion, and the basename is the empty string, we look
2543 for the previous slash and return the portion following that. If
2544 there's no previous slash, we just return what we were passed.
2546 Based on readline/complete.c:printable_part. */
2549 gdb_printable_part (char *pathname
)
2553 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
2556 temp
= strrchr (pathname
, '/');
2557 #if defined (__MSDOS__)
2558 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
2559 temp
= pathname
+ 1;
2562 if (temp
== 0 || *temp
== '\0')
2564 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
2565 Look for a previous slash and, if one is found, return the portion
2566 following that slash. If there's no previous slash, just return the
2567 pathname we were passed. */
2568 else if (temp
[1] == '\0')
2570 for (x
= temp
- 1; x
> pathname
; x
--)
2573 return ((*x
== '/') ? x
+ 1 : pathname
);
2579 /* Compute width of STRING when displayed on screen by print_filename.
2580 Based on readline/complete.c:fnwidth. */
2583 gdb_fnwidth (const char *string
)
2586 #if defined (HANDLE_MULTIBYTE)
2592 left
= strlen (string
) + 1;
2593 memset (&ps
, 0, sizeof (mbstate_t));
2599 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
2606 #if defined (HANDLE_MULTIBYTE)
2607 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
2608 if (MB_INVALIDCH (clen
))
2612 memset (&ps
, 0, sizeof (mbstate_t));
2614 else if (MB_NULLWCH (clen
))
2620 width
+= (w
>= 0) ? w
: 1;
2632 /* Print TO_PRINT, one matching completion.
2633 PREFIX_BYTES is number of common prefix bytes.
2634 Based on readline/complete.c:fnprint. */
2637 gdb_fnprint (const char *to_print
, int prefix_bytes
,
2638 const struct match_list_displayer
*displayer
)
2642 #if defined (HANDLE_MULTIBYTE)
2649 end
= to_print
+ strlen (to_print
) + 1;
2650 memset (&ps
, 0, sizeof (mbstate_t));
2655 /* Don't print only the ellipsis if the common prefix is one of the
2656 possible completions */
2657 if (to_print
[prefix_bytes
] == '\0')
2664 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
2665 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
2666 displayer
->putch (displayer
, ellipsis
);
2667 printed_len
= ELLIPSIS_LEN
;
2670 s
= to_print
+ prefix_bytes
;
2675 displayer
->putch (displayer
, '^');
2676 displayer
->putch (displayer
, UNCTRL (*s
));
2679 #if defined (HANDLE_MULTIBYTE)
2680 memset (&ps
, 0, sizeof (mbstate_t));
2683 else if (*s
== RUBOUT
)
2685 displayer
->putch (displayer
, '^');
2686 displayer
->putch (displayer
, '?');
2689 #if defined (HANDLE_MULTIBYTE)
2690 memset (&ps
, 0, sizeof (mbstate_t));
2695 #if defined (HANDLE_MULTIBYTE)
2696 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
2697 if (MB_INVALIDCH (tlen
))
2701 memset (&ps
, 0, sizeof (mbstate_t));
2703 else if (MB_NULLWCH (tlen
))
2708 width
= (w
>= 0) ? w
: 1;
2710 for (w
= 0; w
< tlen
; ++w
)
2711 displayer
->putch (displayer
, s
[w
]);
2713 printed_len
+= width
;
2715 displayer
->putch (displayer
, *s
);
2725 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
2726 are using it, check for and output a single character for `special'
2727 filenames. Return the number of characters we output.
2728 Based on readline/complete.c:print_filename. */
2731 gdb_print_filename (char *to_print
, char *full_pathname
, int prefix_bytes
,
2732 const struct match_list_displayer
*displayer
)
2734 int printed_len
, extension_char
, slen
, tlen
;
2735 char *s
, c
, *new_full_pathname
;
2737 extern int _rl_complete_mark_directories
;
2740 printed_len
= gdb_fnprint (to_print
, prefix_bytes
, displayer
);
2742 #if defined (VISIBLE_STATS)
2743 if (rl_filename_completion_desired
&& (rl_visible_stats
|| _rl_complete_mark_directories
))
2745 if (rl_filename_completion_desired
&& _rl_complete_mark_directories
)
2748 /* If to_print != full_pathname, to_print is the basename of the
2749 path passed. In this case, we try to expand the directory
2750 name before checking for the stat character. */
2751 if (to_print
!= full_pathname
)
2753 /* Terminate the directory name. */
2755 to_print
[-1] = '\0';
2757 /* If setting the last slash in full_pathname to a NUL results in
2758 full_pathname being the empty string, we are trying to complete
2759 files in the root directory. If we pass a null string to the
2760 bash directory completion hook, for example, it will expand it
2761 to the current directory. We just want the `/'. */
2762 if (full_pathname
== 0 || *full_pathname
== 0)
2764 else if (full_pathname
[0] != '/')
2766 else if (full_pathname
[1] == 0)
2767 dn
= "//"; /* restore trailing slash to `//' */
2768 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
2769 dn
= "/"; /* don't turn /// into // */
2772 s
= tilde_expand (dn
);
2773 if (rl_directory_completion_hook
)
2774 (*rl_directory_completion_hook
) (&s
);
2777 tlen
= strlen (to_print
);
2778 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
2779 strcpy (new_full_pathname
, s
);
2780 if (s
[slen
- 1] == '/')
2783 new_full_pathname
[slen
] = '/';
2784 new_full_pathname
[slen
] = '/';
2785 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
2787 #if defined (VISIBLE_STATS)
2788 if (rl_visible_stats
)
2789 extension_char
= stat_char (new_full_pathname
);
2792 if (gdb_path_isdir (new_full_pathname
))
2793 extension_char
= '/';
2795 xfree (new_full_pathname
);
2800 s
= tilde_expand (full_pathname
);
2801 #if defined (VISIBLE_STATS)
2802 if (rl_visible_stats
)
2803 extension_char
= stat_char (s
);
2806 if (gdb_path_isdir (s
))
2807 extension_char
= '/';
2813 displayer
->putch (displayer
, extension_char
);
2821 /* GDB version of readline/complete.c:complete_get_screenwidth. */
2824 gdb_complete_get_screenwidth (const struct match_list_displayer
*displayer
)
2826 /* Readline has other stuff here which it's not clear we need. */
2827 return displayer
->width
;
2830 extern int _rl_completion_prefix_display_length
;
2831 extern int _rl_print_completions_horizontally
;
2833 extern "C" int _rl_qsort_string_compare (const void *, const void *);
2834 typedef int QSFUNC (const void *, const void *);
2836 /* GDB version of readline/complete.c:rl_display_match_list.
2837 See gdb_display_match_list for a description of MATCHES, LEN, MAX.
2838 Returns non-zero if all matches are displayed. */
2841 gdb_display_match_list_1 (char **matches
, int len
, int max
,
2842 const struct match_list_displayer
*displayer
)
2844 int count
, limit
, printed_len
, lines
, cols
;
2845 int i
, j
, k
, l
, common_length
, sind
;
2847 int page_completions
= displayer
->height
!= INT_MAX
&& pagination_enabled
;
2849 /* Find the length of the prefix common to all items: length as displayed
2850 characters (common_length) and as a byte index into the matches (sind) */
2851 common_length
= sind
= 0;
2852 if (_rl_completion_prefix_display_length
> 0)
2854 t
= gdb_printable_part (matches
[0]);
2855 temp
= strrchr (t
, '/');
2856 common_length
= temp
? gdb_fnwidth (temp
) : gdb_fnwidth (t
);
2857 sind
= temp
? strlen (temp
) : strlen (t
);
2859 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
2860 max
-= common_length
- ELLIPSIS_LEN
;
2862 common_length
= sind
= 0;
2865 /* How many items of MAX length can we fit in the screen window? */
2866 cols
= gdb_complete_get_screenwidth (displayer
);
2869 if (limit
!= 1 && (limit
* max
== cols
))
2872 /* If cols == 0, limit will end up -1 */
2873 if (cols
< displayer
->width
&& limit
< 0)
2876 /* Avoid a possible floating exception. If max > cols,
2877 limit will be 0 and a divide-by-zero fault will result. */
2881 /* How many iterations of the printing loop? */
2882 count
= (len
+ (limit
- 1)) / limit
;
2884 /* Watch out for special case. If LEN is less than LIMIT, then
2885 just do the inner printing loop.
2886 0 < len <= limit implies count = 1. */
2888 /* Sort the items if they are not already sorted. */
2889 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
2890 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
2892 displayer
->crlf (displayer
);
2895 if (_rl_print_completions_horizontally
== 0)
2897 /* Print the sorted items, up-and-down alphabetically, like ls. */
2898 for (i
= 1; i
<= count
; i
++)
2900 for (j
= 0, l
= i
; j
< limit
; j
++)
2902 if (l
> len
|| matches
[l
] == 0)
2906 temp
= gdb_printable_part (matches
[l
]);
2907 printed_len
= gdb_print_filename (temp
, matches
[l
], sind
,
2911 for (k
= 0; k
< max
- printed_len
; k
++)
2912 displayer
->putch (displayer
, ' ');
2916 displayer
->crlf (displayer
);
2918 if (page_completions
&& lines
>= (displayer
->height
- 1) && i
< count
)
2920 lines
= gdb_display_match_list_pager (lines
, displayer
);
2928 /* Print the sorted items, across alphabetically, like ls -x. */
2929 for (i
= 1; matches
[i
]; i
++)
2931 temp
= gdb_printable_part (matches
[i
]);
2932 printed_len
= gdb_print_filename (temp
, matches
[i
], sind
, displayer
);
2933 /* Have we reached the end of this line? */
2936 if (i
&& (limit
> 1) && (i
% limit
) == 0)
2938 displayer
->crlf (displayer
);
2940 if (page_completions
&& lines
>= displayer
->height
- 1)
2942 lines
= gdb_display_match_list_pager (lines
, displayer
);
2948 for (k
= 0; k
< max
- printed_len
; k
++)
2949 displayer
->putch (displayer
, ' ');
2952 displayer
->crlf (displayer
);
2958 /* Utility for displaying completion list matches, used by both CLI and TUI.
2960 MATCHES is the list of strings, in argv format, LEN is the number of
2961 strings in MATCHES, and MAX is the length of the longest string in
2965 gdb_display_match_list (char **matches
, int len
, int max
,
2966 const struct match_list_displayer
*displayer
)
2968 /* Readline will never call this if complete_line returned NULL. */
2969 gdb_assert (max_completions
!= 0);
2971 /* complete_line will never return more than this. */
2972 if (max_completions
> 0)
2973 gdb_assert (len
<= max_completions
);
2975 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
2979 /* We can't use *query here because they wait for <RET> which is
2980 wrong here. This follows the readline version as closely as possible
2981 for compatibility's sake. See readline/complete.c. */
2983 displayer
->crlf (displayer
);
2985 xsnprintf (msg
, sizeof (msg
),
2986 "Display all %d possibilities? (y or n)", len
);
2987 displayer
->puts (displayer
, msg
);
2988 displayer
->flush (displayer
);
2990 if (gdb_get_y_or_n (0, displayer
) == 0)
2992 displayer
->crlf (displayer
);
2997 if (gdb_display_match_list_1 (matches
, len
, max
, displayer
))
2999 /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
3000 if (len
== max_completions
)
3002 /* The maximum number of completions has been reached. Warn the user
3003 that there may be more. */
3004 const char *message
= get_max_completions_reached_message ();
3006 displayer
->puts (displayer
, message
);
3007 displayer
->crlf (displayer
);
3012 /* See completer.h. */
3015 skip_over_slash_fmt (completion_tracker
&tracker
, const char **args
)
3017 const char *text
= *args
;
3022 tracker
.set_use_custom_word_point (true);
3024 if (text
[1] == '\0')
3026 /* The user tried to complete after typing just the '/' character
3027 of the /FMT string. Step the completer past the '/', but we
3028 don't offer any completions. */
3034 /* The user has typed some characters after the '/', we assume
3035 this is a complete /FMT string, first skip over it. */
3036 text
= skip_to_space (text
);
3040 /* We're at the end of the input string. The user has typed
3041 '/FMT' and asked for a completion. Push an empty
3042 completion string, this will cause readline to insert a
3043 space so the user now has '/FMT '. */
3045 tracker
.add_completion (make_unique_xstrdup (text
));
3049 /* The user has already typed things after the /FMT, skip the
3050 whitespace and return false. Whoever called this function
3051 should then try to complete what comes next. */
3053 text
= skip_spaces (text
);
3057 tracker
.advance_custom_word_point_by (text
- *args
);
3065 void _initialize_completer ();
3067 _initialize_completer ()
3069 add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class
,
3070 &max_completions
, _("\
3071 Set maximum number of completion candidates."), _("\
3072 Show maximum number of completion candidates."), _("\
3073 Use this to limit the number of candidates considered\n\
3074 during completion. Specifying \"unlimited\" or -1\n\
3075 disables limiting. Note that setting either no limit or\n\
3076 a very large limit can make completion slow."),
3077 NULL
, NULL
, &setlist
, &showlist
);