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 /* A static function that can be passed to the htab hash system to be
86 used as a callback that deletes an item from the hash. */
87 static void deleter (void *arg
)
89 completion_hash_entry
*entry
= (completion_hash_entry
*) arg
;
95 /* The symbol name stored in this hash entry. */
96 gdb::unique_xmalloc_ptr
<char> m_name
;
98 /* The lowest common denominator string computed for this hash entry. */
99 gdb::unique_xmalloc_ptr
<char> m_lcd
;
102 /* Misc state that needs to be tracked across several different
103 readline completer entry point calls, all related to a single
104 completion invocation. */
106 struct gdb_completer_state
108 /* The current completion's completion tracker. This is a global
109 because a tracker can be shared between the handle_brkchars and
110 handle_completion phases, which involves different readline
112 completion_tracker
*tracker
= NULL
;
114 /* Whether the current completion was aborted. */
115 bool aborted
= false;
118 /* The current completion state. */
119 static gdb_completer_state current_completion
;
121 /* An enumeration of the various things a user might attempt to
122 complete for a location. If you change this, remember to update
123 the explicit_options array below too. */
125 enum explicit_location_match_type
127 /* The filename of a source file. */
130 /* The name of a function or method. */
133 /* The fully-qualified name of a function or method. */
139 /* The name of a label. */
143 /* Prototypes for local functions. */
145 /* readline uses the word breaks for two things:
146 (1) In figuring out where to point the TEXT parameter to the
147 rl_completion_entry_function. Since we don't use TEXT for much,
148 it doesn't matter a lot what the word breaks are for this purpose,
149 but it does affect how much stuff M-? lists.
150 (2) If one of the matches contains a word break character, readline
151 will quote it. That's why we switch between
152 current_language->la_word_break_characters() and
153 gdb_completer_command_word_break_characters. I'm not sure when
154 we need this behavior (perhaps for funky characters in C++
157 /* Variables which are necessary for fancy command line editing. */
159 /* When completing on command names, we remove '-' and '.' from the list of
160 word break characters, since we use it in command names. If the
161 readline library sees one in any of the current completion strings,
162 it thinks that the string needs to be quoted and automatically
163 supplies a leading quote. */
164 static const char gdb_completer_command_word_break_characters
[] =
165 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/><,";
167 /* When completing on file names, we remove from the list of word
168 break characters any characters that are commonly used in file
169 names, such as '-', '+', '~', etc. Otherwise, readline displays
170 incorrect completion candidates. */
171 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
172 programs support @foo style response files. */
173 static const char gdb_completer_file_name_break_characters
[] =
174 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
180 /* Characters that can be used to quote completion strings. Note that
181 we can't include '"' because the gdb C parser treats such quoted
182 sequences as strings. */
183 static const char gdb_completer_quote_characters
[] = "'";
185 /* Accessor for some completer data that may interest other files. */
188 get_gdb_completer_quote_characters (void)
190 return gdb_completer_quote_characters
;
193 /* This can be used for functions which don't want to complete on
194 symbols but don't want to complete on anything else either. */
197 noop_completer (struct cmd_list_element
*ignore
,
198 completion_tracker
&tracker
,
199 const char *text
, const char *prefix
)
203 /* Complete on filenames. */
206 filename_completer (struct cmd_list_element
*ignore
,
207 completion_tracker
&tracker
,
208 const char *text
, const char *word
)
215 gdb::unique_xmalloc_ptr
<char> p_rl
216 (rl_filename_completion_function (text
, subsequent_name
));
219 /* We need to set subsequent_name to a non-zero value before the
220 continue line below, because otherwise, if the first file
221 seen by GDB is a backup file whose name ends in a `~', we
222 will loop indefinitely. */
224 /* Like emacs, don't complete on old versions. Especially
225 useful in the "source" command. */
226 const char *p
= p_rl
.get ();
227 if (p
[strlen (p
) - 1] == '~')
230 tracker
.add_completion
231 (make_completion_match_str (std::move (p_rl
), text
, word
));
234 /* There is no way to do this just long enough to affect quote
235 inserting without also affecting the next completion. This
236 should be fixed in readline. FIXME. */
237 /* Ensure that readline does the right thing
238 with respect to inserting quotes. */
239 rl_completer_word_break_characters
= "";
243 /* The corresponding completer_handle_brkchars
247 filename_completer_handle_brkchars (struct cmd_list_element
*ignore
,
248 completion_tracker
&tracker
,
249 const char *text
, const char *word
)
251 set_rl_completer_word_break_characters
252 (gdb_completer_file_name_break_characters
);
255 /* Possible values for the found_quote flags word used by the completion
256 functions. It says what kind of (shell-like) quoting we found anywhere
258 #define RL_QF_SINGLE_QUOTE 0x01
259 #define RL_QF_DOUBLE_QUOTE 0x02
260 #define RL_QF_BACKSLASH 0x04
261 #define RL_QF_OTHER_QUOTE 0x08
263 /* Find the bounds of the current word for completion purposes, and
264 return a pointer to the end of the word. This mimics (and is a
265 modified version of) readline's _rl_find_completion_word internal
268 This function skips quoted substrings (characters between matched
269 pairs of characters in rl_completer_quote_characters). We try to
270 find an unclosed quoted substring on which to do matching. If one
271 is not found, we use the word break characters to find the
272 boundaries of the current word. QC, if non-null, is set to the
273 opening quote character if we found an unclosed quoted substring,
274 '\0' otherwise. DP, if non-null, is set to the value of the
275 delimiter character that caused a word break. */
277 struct gdb_rl_completion_word_info
279 const char *word_break_characters
;
280 const char *quote_characters
;
281 const char *basic_quote_characters
;
285 gdb_rl_find_completion_word (struct gdb_rl_completion_word_info
*info
,
287 const char *line_buffer
)
289 int scan
, end
, found_quote
, delimiter
, pass_next
, isbrk
;
291 const char *brkchars
;
292 int point
= strlen (line_buffer
);
294 /* The algorithm below does '--point'. Avoid buffer underflow with
306 found_quote
= delimiter
= 0;
309 brkchars
= info
->word_break_characters
;
311 if (info
->quote_characters
!= NULL
)
313 /* We have a list of characters which can be used in pairs to
314 quote substrings for the completer. Try to find the start of
315 an unclosed quoted substring. */
316 /* FOUND_QUOTE is set so we know what kind of quotes we
318 for (scan
= pass_next
= 0;
328 /* Shell-like semantics for single quotes -- don't allow
329 backslash to quote anything in single quotes, especially
330 not the closing quote. If you don't like this, take out
331 the check on the value of quote_char. */
332 if (quote_char
!= '\'' && line_buffer
[scan
] == '\\')
335 found_quote
|= RL_QF_BACKSLASH
;
339 if (quote_char
!= '\0')
341 /* Ignore everything until the matching close quote
343 if (line_buffer
[scan
] == quote_char
)
345 /* Found matching close. Abandon this
351 else if (strchr (info
->quote_characters
, line_buffer
[scan
]))
353 /* Found start of a quoted substring. */
354 quote_char
= line_buffer
[scan
];
356 /* Shell-like quoting conventions. */
357 if (quote_char
== '\'')
358 found_quote
|= RL_QF_SINGLE_QUOTE
;
359 else if (quote_char
== '"')
360 found_quote
|= RL_QF_DOUBLE_QUOTE
;
362 found_quote
|= RL_QF_OTHER_QUOTE
;
367 if (point
== end
&& quote_char
== '\0')
369 /* We didn't find an unclosed quoted substring upon which to do
370 completion, so use the word break characters to find the
371 substring on which to complete. */
374 scan
= line_buffer
[point
];
376 if (strchr (brkchars
, scan
) != 0)
381 /* If we are at an unquoted word break, then advance past it. */
382 scan
= line_buffer
[point
];
386 isbrk
= strchr (brkchars
, scan
) != 0;
390 /* If the character that caused the word break was a quoting
391 character, then remember it as the delimiter. */
392 if (info
->basic_quote_characters
393 && strchr (info
->basic_quote_characters
, scan
)
394 && (end
- point
) > 1)
406 return line_buffer
+ point
;
409 /* Find the completion word point for TEXT, emulating the algorithm
410 readline uses to find the word point, using WORD_BREAK_CHARACTERS
411 as word break characters. */
414 advance_to_completion_word (completion_tracker
&tracker
,
415 const char *word_break_characters
,
418 gdb_rl_completion_word_info info
;
420 info
.word_break_characters
= word_break_characters
;
421 info
.quote_characters
= gdb_completer_quote_characters
;
422 info
.basic_quote_characters
= rl_basic_quote_characters
;
426 = gdb_rl_find_completion_word (&info
, NULL
, &delimiter
, text
);
428 tracker
.advance_custom_word_point_by (start
- text
);
432 tracker
.set_quote_char (delimiter
);
433 tracker
.set_suppress_append_ws (true);
439 /* See completer.h. */
442 advance_to_expression_complete_word_point (completion_tracker
&tracker
,
445 const char *brk_chars
= current_language
->la_word_break_characters ();
446 return advance_to_completion_word (tracker
, brk_chars
, text
);
449 /* See completer.h. */
452 advance_to_filename_complete_word_point (completion_tracker
&tracker
,
455 const char *brk_chars
= gdb_completer_file_name_break_characters
;
456 return advance_to_completion_word (tracker
, brk_chars
, text
);
459 /* See completer.h. */
462 completion_tracker::completes_to_completion_word (const char *word
)
464 recompute_lowest_common_denominator ();
465 if (m_lowest_common_denominator_unique
)
467 const char *lcd
= m_lowest_common_denominator
;
469 if (strncmp_iw (word
, lcd
, strlen (lcd
)) == 0)
471 /* Maybe skip the function and complete on keywords. */
472 size_t wordlen
= strlen (word
);
473 if (word
[wordlen
- 1] == ' ')
481 /* See completer.h. */
484 complete_nested_command_line (completion_tracker
&tracker
, const char *text
)
486 /* Must be called from a custom-word-point completer. */
487 gdb_assert (tracker
.use_custom_word_point ());
489 /* Disable the custom word point temporarily, because we want to
490 probe whether the command we're completing itself uses a custom
492 tracker
.set_use_custom_word_point (false);
493 size_t save_custom_word_point
= tracker
.custom_word_point ();
495 int quote_char
= '\0';
496 const char *word
= completion_find_completion_word (tracker
, text
,
499 if (tracker
.use_custom_word_point ())
501 /* The command we're completing uses a custom word point, so the
502 tracker already contains the matches. We're done. */
506 /* Restore the custom word point settings. */
507 tracker
.set_custom_word_point (save_custom_word_point
);
508 tracker
.set_use_custom_word_point (true);
510 /* Run the handle_completions completer phase. */
511 complete_line (tracker
, word
, text
, strlen (text
));
514 /* Complete on linespecs, which might be of two possible forms:
520 This is intended to be used in commands that set breakpoints
524 complete_files_symbols (completion_tracker
&tracker
,
525 const char *text
, const char *word
)
527 completion_list fn_list
;
530 int quoted
= *text
== '\'' || *text
== '"';
531 int quote_char
= '\0';
532 const char *colon
= NULL
;
533 char *file_to_match
= NULL
;
534 const char *symbol_start
= text
;
535 const char *orig_text
= text
;
537 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
538 for (p
= text
; *p
!= '\0'; ++p
)
540 if (*p
== '\\' && p
[1] == '\'')
542 else if (*p
== '\'' || *p
== '"')
546 while (*p
!= '\0' && *p
!= quote_found
)
548 if (*p
== '\\' && p
[1] == quote_found
)
553 if (*p
== quote_found
)
556 break; /* Hit the end of text. */
558 #if HAVE_DOS_BASED_FILE_SYSTEM
559 /* If we have a DOS-style absolute file name at the beginning of
560 TEXT, and the colon after the drive letter is the only colon
561 we found, pretend the colon is not there. */
562 else if (p
< text
+ 3 && *p
== ':' && p
== text
+ 1 + quoted
)
565 else if (*p
== ':' && !colon
)
568 symbol_start
= p
+ 1;
570 else if (strchr (current_language
->la_word_break_characters(), *p
))
571 symbol_start
= p
+ 1;
577 /* Where is the file name? */
582 file_to_match
= (char *) xmalloc (colon
- text
+ 1);
583 strncpy (file_to_match
, text
, colon
- text
);
584 file_to_match
[colon
- text
] = '\0';
585 /* Remove trailing colons and quotes from the file name. */
586 for (s
= file_to_match
+ (colon
- text
);
589 if (*s
== ':' || *s
== quote_char
)
592 /* If the text includes a colon, they want completion only on a
593 symbol name after the colon. Otherwise, we need to complete on
594 symbols as well as on files. */
597 collect_file_symbol_completion_matches (tracker
,
598 complete_symbol_mode::EXPRESSION
,
599 symbol_name_match_type::EXPRESSION
,
602 xfree (file_to_match
);
606 size_t text_len
= strlen (text
);
608 collect_symbol_completion_matches (tracker
,
609 complete_symbol_mode::EXPRESSION
,
610 symbol_name_match_type::EXPRESSION
,
612 /* If text includes characters which cannot appear in a file
613 name, they cannot be asking for completion on files. */
615 gdb_completer_file_name_break_characters
) == text_len
)
616 fn_list
= make_source_files_completion_list (text
, text
);
619 if (!fn_list
.empty () && !tracker
.have_completions ())
621 /* If we only have file names as possible completion, we should
622 bring them in sync with what rl_complete expects. The
623 problem is that if the user types "break /foo/b TAB", and the
624 possible completions are "/foo/bar" and "/foo/baz"
625 rl_complete expects us to return "bar" and "baz", without the
626 leading directories, as possible completions, because `word'
627 starts at the "b". But we ignore the value of `word' when we
628 call make_source_files_completion_list above (because that
629 would not DTRT when the completion results in both symbols
630 and file names), so make_source_files_completion_list returns
631 the full "/foo/bar" and "/foo/baz" strings. This produces
632 wrong results when, e.g., there's only one possible
633 completion, because rl_complete will prepend "/foo/" to each
634 candidate completion. The loop below removes that leading
636 for (const auto &fn_up
: fn_list
)
638 char *fn
= fn_up
.get ();
639 memmove (fn
, fn
+ (word
- text
), strlen (fn
) + 1 - (word
- text
));
643 tracker
.add_completions (std::move (fn_list
));
645 if (!tracker
.have_completions ())
647 /* No completions at all. As the final resort, try completing
648 on the entire text as a symbol. */
649 collect_symbol_completion_matches (tracker
,
650 complete_symbol_mode::EXPRESSION
,
651 symbol_name_match_type::EXPRESSION
,
656 /* See completer.h. */
659 complete_source_filenames (const char *text
)
661 size_t text_len
= strlen (text
);
663 /* If text includes characters which cannot appear in a file name,
664 the user cannot be asking for completion on files. */
666 gdb_completer_file_name_break_characters
)
668 return make_source_files_completion_list (text
, text
);
673 /* Complete address and linespec locations. */
676 complete_address_and_linespec_locations (completion_tracker
&tracker
,
678 symbol_name_match_type match_type
)
682 tracker
.advance_custom_word_point_by (1);
685 = advance_to_expression_complete_word_point (tracker
, text
);
686 complete_expression (tracker
, text
, word
);
690 linespec_complete (tracker
, text
, match_type
);
694 /* The explicit location options. Note that indexes into this array
695 must match the explicit_location_match_type enumerators. */
697 static const char *const explicit_options
[] =
707 /* The probe modifier options. These can appear before a location in
708 breakpoint commands. */
709 static const char *const probe_options
[] =
717 /* Returns STRING if not NULL, the empty string otherwise. */
720 string_or_empty (const char *string
)
722 return string
!= NULL
? string
: "";
725 /* A helper function to collect explicit location matches for the given
726 LOCATION, which is attempting to match on WORD. */
729 collect_explicit_location_matches (completion_tracker
&tracker
,
730 struct event_location
*location
,
731 enum explicit_location_match_type what
,
733 const struct language_defn
*language
)
735 const struct explicit_location
*explicit_loc
736 = get_explicit_location (location
);
738 /* True if the option expects an argument. */
739 bool needs_arg
= true;
741 /* Note, in the various MATCH_* below, we complete on
742 explicit_loc->foo instead of WORD, because only the former will
743 have already skipped past any quote char. */
748 const char *source
= string_or_empty (explicit_loc
->source_filename
);
749 completion_list matches
750 = make_source_files_completion_list (source
, source
);
751 tracker
.add_completions (std::move (matches
));
757 const char *function
= string_or_empty (explicit_loc
->function_name
);
758 linespec_complete_function (tracker
, function
,
759 explicit_loc
->func_name_match_type
,
760 explicit_loc
->source_filename
);
764 case MATCH_QUALIFIED
:
768 /* Nothing to offer. */
773 const char *label
= string_or_empty (explicit_loc
->label_name
);
774 linespec_complete_label (tracker
, language
,
775 explicit_loc
->source_filename
,
776 explicit_loc
->function_name
,
777 explicit_loc
->func_name_match_type
,
783 gdb_assert_not_reached ("unhandled explicit_location_match_type");
786 if (!needs_arg
|| tracker
.completes_to_completion_word (word
))
788 tracker
.discard_completions ();
789 tracker
.advance_custom_word_point_by (strlen (word
));
790 complete_on_enum (tracker
, explicit_options
, "", "");
791 complete_on_enum (tracker
, linespec_keywords
, "", "");
793 else if (!tracker
.have_completions ())
795 /* Maybe we have an unterminated linespec keyword at the tail of
796 the string. Try completing on that. */
797 size_t wordlen
= strlen (word
);
798 const char *keyword
= word
+ wordlen
;
800 if (wordlen
> 0 && keyword
[-1] != ' ')
802 while (keyword
> word
&& *keyword
!= ' ')
804 /* Don't complete on keywords if we'd be completing on the
805 whole explicit linespec option. E.g., "b -function
806 thr<tab>" should not complete to the "thread"
810 keyword
= skip_spaces (keyword
);
812 tracker
.advance_custom_word_point_by (keyword
- word
);
813 complete_on_enum (tracker
, linespec_keywords
, keyword
, keyword
);
816 else if (wordlen
> 0 && keyword
[-1] == ' ')
818 /* Assume that we're maybe past the explicit location
819 argument, and we didn't manage to find any match because
820 the user wants to create a pending breakpoint. Offer the
821 keyword and explicit location options as possible
823 tracker
.advance_custom_word_point_by (keyword
- word
);
824 complete_on_enum (tracker
, linespec_keywords
, keyword
, keyword
);
825 complete_on_enum (tracker
, explicit_options
, keyword
, keyword
);
830 /* If the next word in *TEXT_P is any of the keywords in KEYWORDS,
831 then advance both TEXT_P and the word point in the tracker past the
832 keyword and return the (0-based) index in the KEYWORDS array that
833 matched. Otherwise, return -1. */
836 skip_keyword (completion_tracker
&tracker
,
837 const char * const *keywords
, const char **text_p
)
839 const char *text
= *text_p
;
840 const char *after
= skip_to_space (text
);
841 size_t len
= after
- text
;
843 if (text
[len
] != ' ')
847 for (int i
= 0; keywords
[i
] != NULL
; i
++)
849 if (strncmp (keywords
[i
], text
, len
) == 0)
860 tracker
.advance_custom_word_point_by (len
+ 1);
869 /* A completer function for explicit locations. This function
870 completes both options ("-source", "-line", etc) and values. If
871 completing a quoted string, then QUOTED_ARG_START and
872 QUOTED_ARG_END point to the quote characters. LANGUAGE is the
876 complete_explicit_location (completion_tracker
&tracker
,
877 struct event_location
*location
,
879 const language_defn
*language
,
880 const char *quoted_arg_start
,
881 const char *quoted_arg_end
)
886 int keyword
= skip_keyword (tracker
, explicit_options
, &text
);
889 complete_on_enum (tracker
, explicit_options
, text
, text
);
892 /* Completing on value. */
893 enum explicit_location_match_type what
894 = (explicit_location_match_type
) keyword
;
896 if (quoted_arg_start
!= NULL
&& quoted_arg_end
!= NULL
)
898 if (quoted_arg_end
[1] == '\0')
900 /* If completing a quoted string with the cursor right
901 at the terminating quote char, complete the
902 completion word without interpretation, so that
903 readline advances the cursor one whitespace past the
904 quote, even if there's no match. This makes these
905 cases behave the same:
907 before: "b -function function()"
908 after: "b -function function() "
910 before: "b -function 'function()'"
911 after: "b -function 'function()' "
913 and trusts the user in this case:
915 before: "b -function 'not_loaded_function_yet()'"
916 after: "b -function 'not_loaded_function_yet()' "
918 tracker
.add_completion (make_unique_xstrdup (text
));
920 else if (quoted_arg_end
[1] == ' ')
922 /* We're maybe past the explicit location argument.
923 Skip the argument without interpretation, assuming the
924 user may want to create pending breakpoint. Offer
925 the keyword and explicit location options as possible
927 tracker
.advance_custom_word_point_by (strlen (text
));
928 complete_on_enum (tracker
, linespec_keywords
, "", "");
929 complete_on_enum (tracker
, explicit_options
, "", "");
934 /* Now gather matches */
935 collect_explicit_location_matches (tracker
, location
, what
, text
,
940 /* A completer for locations. */
943 location_completer (struct cmd_list_element
*ignore
,
944 completion_tracker
&tracker
,
945 const char *text
, const char * /* word */)
947 int found_probe_option
= -1;
949 /* If we have a probe modifier, skip it. This can only appear as
950 first argument. Until we have a specific completer for probes,
951 falling back to the linespec completer for the remainder of the
952 line is better than nothing. */
953 if (text
[0] == '-' && text
[1] == 'p')
954 found_probe_option
= skip_keyword (tracker
, probe_options
, &text
);
956 const char *option_text
= text
;
957 int saved_word_point
= tracker
.custom_word_point ();
959 const char *copy
= text
;
961 explicit_completion_info completion_info
;
962 event_location_up location
963 = string_to_explicit_location (©
, current_language
,
965 if (completion_info
.quoted_arg_start
!= NULL
966 && completion_info
.quoted_arg_end
== NULL
)
968 /* Found an unbalanced quote. */
969 tracker
.set_quote_char (*completion_info
.quoted_arg_start
);
970 tracker
.advance_custom_word_point_by (1);
973 if (completion_info
.saw_explicit_location_option
)
977 tracker
.advance_custom_word_point_by (copy
- text
);
980 /* We found a terminator at the tail end of the string,
981 which means we're past the explicit location options. We
982 may have a keyword to complete on. If we have a whole
983 keyword, then complete whatever comes after as an
984 expression. This is mainly for the "if" keyword. If the
985 "thread" and "task" keywords gain their own completers,
986 they should be used here. */
987 int keyword
= skip_keyword (tracker
, linespec_keywords
, &text
);
991 complete_on_enum (tracker
, linespec_keywords
, text
, text
);
996 = advance_to_expression_complete_word_point (tracker
, text
);
997 complete_expression (tracker
, text
, word
);
1002 tracker
.advance_custom_word_point_by (completion_info
.last_option
1004 text
= completion_info
.last_option
;
1006 complete_explicit_location (tracker
, location
.get (), text
,
1008 completion_info
.quoted_arg_start
,
1009 completion_info
.quoted_arg_end
);
1013 /* This is an address or linespec location. */
1014 else if (location
!= NULL
)
1016 /* Handle non-explicit location options. */
1018 int keyword
= skip_keyword (tracker
, explicit_options
, &text
);
1020 complete_on_enum (tracker
, explicit_options
, text
, text
);
1023 tracker
.advance_custom_word_point_by (copy
- text
);
1026 symbol_name_match_type match_type
1027 = get_explicit_location (location
.get ())->func_name_match_type
;
1028 complete_address_and_linespec_locations (tracker
, text
, match_type
);
1034 complete_address_and_linespec_locations (tracker
, text
,
1035 symbol_name_match_type::WILD
);
1038 /* Add matches for option names, if either:
1040 - Some completer above found some matches, but the word point did
1041 not advance (e.g., "b <tab>" finds all functions, or "b -<tab>"
1042 matches all objc selectors), or;
1044 - Some completer above advanced the word point, but found no
1047 if ((text
[0] == '-' || text
[0] == '\0')
1048 && (!tracker
.have_completions ()
1049 || tracker
.custom_word_point () == saved_word_point
))
1051 tracker
.set_custom_word_point (saved_word_point
);
1054 if (found_probe_option
== -1)
1055 complete_on_enum (tracker
, probe_options
, text
, text
);
1056 complete_on_enum (tracker
, explicit_options
, text
, text
);
1060 /* The corresponding completer_handle_brkchars
1064 location_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1065 completion_tracker
&tracker
,
1067 const char *word_ignored
)
1069 tracker
.set_use_custom_word_point (true);
1071 location_completer (ignore
, tracker
, text
, NULL
);
1074 /* Helper for expression_completer which recursively adds field and
1075 method names from TYPE, a struct or union type, to the OUTPUT
1079 add_struct_fields (struct type
*type
, completion_list
&output
,
1080 const char *fieldname
, int namelen
)
1083 int computed_type_name
= 0;
1084 const char *type_name
= NULL
;
1086 type
= check_typedef (type
);
1087 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1089 if (i
< TYPE_N_BASECLASSES (type
))
1090 add_struct_fields (TYPE_BASECLASS (type
, i
),
1091 output
, fieldname
, namelen
);
1092 else if (TYPE_FIELD_NAME (type
, i
))
1094 if (TYPE_FIELD_NAME (type
, i
)[0] != '\0')
1096 if (! strncmp (TYPE_FIELD_NAME (type
, i
),
1097 fieldname
, namelen
))
1098 output
.emplace_back (xstrdup (TYPE_FIELD_NAME (type
, i
)));
1100 else if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_UNION
)
1102 /* Recurse into anonymous unions. */
1103 add_struct_fields (TYPE_FIELD_TYPE (type
, i
),
1104 output
, fieldname
, namelen
);
1109 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1111 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1113 if (name
&& ! strncmp (name
, fieldname
, namelen
))
1115 if (!computed_type_name
)
1117 type_name
= TYPE_NAME (type
);
1118 computed_type_name
= 1;
1120 /* Omit constructors from the completion list. */
1121 if (!type_name
|| strcmp (type_name
, name
))
1122 output
.emplace_back (xstrdup (name
));
1127 /* See completer.h. */
1130 complete_expression (completion_tracker
&tracker
,
1131 const char *text
, const char *word
)
1133 struct type
*type
= NULL
;
1134 gdb::unique_xmalloc_ptr
<char> fieldname
;
1135 enum type_code code
= TYPE_CODE_UNDEF
;
1137 /* Perform a tentative parse of the expression, to see whether a
1138 field completion is required. */
1141 type
= parse_expression_for_completion (text
, &fieldname
, &code
);
1143 catch (const gdb_exception_error
&except
)
1148 if (fieldname
!= nullptr && type
)
1152 type
= check_typedef (type
);
1153 if (TYPE_CODE (type
) != TYPE_CODE_PTR
&& !TYPE_IS_REFERENCE (type
))
1155 type
= TYPE_TARGET_TYPE (type
);
1158 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1159 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1161 completion_list result
;
1163 add_struct_fields (type
, result
, fieldname
.get (),
1164 strlen (fieldname
.get ()));
1165 tracker
.add_completions (std::move (result
));
1169 else if (fieldname
!= nullptr && code
!= TYPE_CODE_UNDEF
)
1171 collect_symbol_completion_matches_type (tracker
, fieldname
.get (),
1172 fieldname
.get (), code
);
1176 complete_files_symbols (tracker
, text
, word
);
1179 /* Complete on expressions. Often this means completing on symbol
1180 names, but some language parsers also have support for completing
1184 expression_completer (struct cmd_list_element
*ignore
,
1185 completion_tracker
&tracker
,
1186 const char *text
, const char *word
)
1188 complete_expression (tracker
, text
, word
);
1191 /* See definition in completer.h. */
1194 set_rl_completer_word_break_characters (const char *break_chars
)
1196 rl_completer_word_break_characters
= (char *) break_chars
;
1199 /* Complete on symbols. */
1202 symbol_completer (struct cmd_list_element
*ignore
,
1203 completion_tracker
&tracker
,
1204 const char *text
, const char *word
)
1206 collect_symbol_completion_matches (tracker
, complete_symbol_mode::EXPRESSION
,
1207 symbol_name_match_type::EXPRESSION
,
1211 /* Here are some useful test cases for completion. FIXME: These
1212 should be put in the test suite. They should be tested with both
1215 "show output-" "radix"
1216 "show output" "-radix"
1217 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1218 "p " ambiguous (all symbols)
1219 "info t foo" no completions
1220 "info t " no completions
1221 "info t" ambiguous ("info target", "info terminal", etc.)
1222 "info ajksdlfk" no completions
1223 "info ajksdlfk " no completions
1225 "info " ambiguous (all info commands)
1226 "p \"a" no completions (string constant)
1227 "p 'a" ambiguous (all symbols starting with a)
1228 "p b-a" ambiguous (all symbols starting with a)
1229 "p b-" ambiguous (all symbols)
1230 "file Make" "file" (word break hard to screw up here)
1231 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1234 enum complete_line_internal_reason
1236 /* Preliminary phase, called by gdb_completion_word_break_characters
1237 function, is used to either:
1239 #1 - Determine the set of chars that are word delimiters
1240 depending on the current command in line_buffer.
1242 #2 - Manually advance RL_POINT to the "word break" point instead
1243 of letting readline do it (based on too-simple character
1246 Simpler completers that just pass a brkchars array to readline
1247 (#1 above) must defer generating the completions to the main
1248 phase (below). No completion list should be generated in this
1251 OTOH, completers that manually advance the word point(#2 above)
1252 must set "use_custom_word_point" in the tracker and generate
1253 their completion in this phase. Note that this is the convenient
1254 thing to do since they'll be parsing the input line anyway. */
1257 /* Main phase, called by complete_line function, is used to get the
1258 list of possible completions. */
1261 /* Special case when completing a 'help' command. In this case,
1262 once sub-command completions are exhausted, we simply return
1267 /* Helper for complete_line_internal to simplify it. */
1270 complete_line_internal_normal_command (completion_tracker
&tracker
,
1271 const char *command
, const char *word
,
1272 const char *cmd_args
,
1273 complete_line_internal_reason reason
,
1274 struct cmd_list_element
*c
)
1276 const char *p
= cmd_args
;
1278 if (c
->completer
== filename_completer
)
1280 /* Many commands which want to complete on file names accept
1281 several file names, as in "run foo bar >>baz". So we don't
1282 want to complete the entire text after the command, just the
1283 last word. To this end, we need to find the beginning of the
1284 file name by starting at `word' and going backwards. */
1287 && strchr (gdb_completer_file_name_break_characters
,
1293 if (reason
== handle_brkchars
)
1295 completer_handle_brkchars_ftype
*brkchars_fn
;
1297 if (c
->completer_handle_brkchars
!= NULL
)
1298 brkchars_fn
= c
->completer_handle_brkchars
;
1302 = (completer_handle_brkchars_func_for_completer
1306 brkchars_fn (c
, tracker
, p
, word
);
1309 if (reason
!= handle_brkchars
&& c
->completer
!= NULL
)
1310 (*c
->completer
) (c
, tracker
, p
, word
);
1313 /* Internal function used to handle completions.
1316 TEXT is the caller's idea of the "word" we are looking at.
1318 LINE_BUFFER is available to be looked at; it contains the entire
1319 text of the line. POINT is the offset in that line of the cursor.
1320 You should pretend that the line ends at POINT.
1322 See complete_line_internal_reason for description of REASON. */
1325 complete_line_internal_1 (completion_tracker
&tracker
,
1327 const char *line_buffer
, int point
,
1328 complete_line_internal_reason reason
)
1332 int ignore_help_classes
;
1333 /* Pointer within tmp_command which corresponds to text. */
1335 struct cmd_list_element
*c
, *result_list
;
1337 /* Choose the default set of word break characters to break
1338 completions. If we later find out that we are doing completions
1339 on command strings (as opposed to strings supplied by the
1340 individual command completer functions, which can be any string)
1341 then we will switch to the special word break set for command
1342 strings, which leaves out the '-' and '.' character used in some
1344 set_rl_completer_word_break_characters
1345 (current_language
->la_word_break_characters());
1347 /* Decide whether to complete on a list of gdb commands or on
1349 tmp_command
= (char *) alloca (point
+ 1);
1352 /* The help command should complete help aliases. */
1353 ignore_help_classes
= reason
!= handle_help
;
1355 strncpy (tmp_command
, line_buffer
, point
);
1356 tmp_command
[point
] = '\0';
1357 if (reason
== handle_brkchars
)
1359 gdb_assert (text
== NULL
);
1364 /* Since text always contains some number of characters leading up
1365 to point, we can find the equivalent position in tmp_command
1366 by subtracting that many characters from the end of tmp_command. */
1367 word
= tmp_command
+ point
- strlen (text
);
1370 /* Move P up to the start of the command. */
1371 p
= skip_spaces (p
);
1375 /* An empty line is ambiguous; that is, it could be any
1377 c
= CMD_LIST_AMBIGUOUS
;
1382 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, ignore_help_classes
);
1385 /* Move p up to the next interesting thing. */
1386 while (*p
== ' ' || *p
== '\t')
1391 tracker
.advance_custom_word_point_by (p
- tmp_command
);
1395 /* It is an unrecognized command. So there are no
1396 possible completions. */
1398 else if (c
== CMD_LIST_AMBIGUOUS
)
1402 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1403 doesn't advance over that thing itself. Do so now. */
1405 while (valid_cmd_char_p (*q
))
1407 if (q
!= tmp_command
+ point
)
1409 /* There is something beyond the ambiguous
1410 command, so there are no possible completions. For
1411 example, "info t " or "info t foo" does not complete
1412 to anything, because "info t" can be "info target" or
1417 /* We're trying to complete on the command which was ambiguous.
1418 This we can deal with. */
1421 if (reason
!= handle_brkchars
)
1422 complete_on_cmdlist (*result_list
->prefixlist
, tracker
, p
,
1423 word
, ignore_help_classes
);
1427 if (reason
!= handle_brkchars
)
1428 complete_on_cmdlist (cmdlist
, tracker
, p
, word
,
1429 ignore_help_classes
);
1431 /* Ensure that readline does the right thing with respect to
1432 inserting quotes. */
1433 set_rl_completer_word_break_characters
1434 (gdb_completer_command_word_break_characters
);
1439 /* We've recognized a full command. */
1441 if (p
== tmp_command
+ point
)
1443 /* There is no non-whitespace in the line beyond the
1446 if (p
[-1] == ' ' || p
[-1] == '\t')
1448 /* The command is followed by whitespace; we need to
1449 complete on whatever comes after command. */
1452 /* It is a prefix command; what comes after it is
1453 a subcommand (e.g. "info "). */
1454 if (reason
!= handle_brkchars
)
1455 complete_on_cmdlist (*c
->prefixlist
, tracker
, p
, word
,
1456 ignore_help_classes
);
1458 /* Ensure that readline does the right thing
1459 with respect to inserting quotes. */
1460 set_rl_completer_word_break_characters
1461 (gdb_completer_command_word_break_characters
);
1463 else if (reason
== handle_help
)
1467 if (reason
!= handle_brkchars
)
1468 complete_on_enum (tracker
, c
->enums
, p
, word
);
1469 set_rl_completer_word_break_characters
1470 (gdb_completer_command_word_break_characters
);
1474 /* It is a normal command; what comes after it is
1475 completed by the command's completer function. */
1476 complete_line_internal_normal_command (tracker
,
1477 tmp_command
, word
, p
,
1483 /* The command is not followed by whitespace; we need to
1484 complete on the command itself, e.g. "p" which is a
1485 command itself but also can complete to "print", "ptype"
1489 /* Find the command we are completing on. */
1491 while (q
> tmp_command
)
1493 if (valid_cmd_char_p (q
[-1]))
1499 /* Move the custom word point back too. */
1500 tracker
.advance_custom_word_point_by (q
- p
);
1502 if (reason
!= handle_brkchars
)
1503 complete_on_cmdlist (result_list
, tracker
, q
, word
,
1504 ignore_help_classes
);
1506 /* Ensure that readline does the right thing
1507 with respect to inserting quotes. */
1508 set_rl_completer_word_break_characters
1509 (gdb_completer_command_word_break_characters
);
1512 else if (reason
== handle_help
)
1516 /* There is non-whitespace beyond the command. */
1518 if (c
->prefixlist
&& !c
->allow_unknown
)
1520 /* It is an unrecognized subcommand of a prefix command,
1521 e.g. "info adsfkdj". */
1525 if (reason
!= handle_brkchars
)
1526 complete_on_enum (tracker
, c
->enums
, p
, word
);
1530 /* It is a normal command. */
1531 complete_line_internal_normal_command (tracker
,
1532 tmp_command
, word
, p
,
1539 /* Wrapper around complete_line_internal_1 to handle
1540 MAX_COMPLETIONS_REACHED_ERROR. */
1543 complete_line_internal (completion_tracker
&tracker
,
1545 const char *line_buffer
, int point
,
1546 complete_line_internal_reason reason
)
1550 complete_line_internal_1 (tracker
, text
, line_buffer
, point
, reason
);
1552 catch (const gdb_exception_error
&except
)
1554 if (except
.error
!= MAX_COMPLETIONS_REACHED_ERROR
)
1559 /* See completer.h. */
1561 int max_completions
= 200;
1563 /* Initial size of the table. It automagically grows from here. */
1564 #define INITIAL_COMPLETION_HTAB_SIZE 200
1566 /* See completer.h. */
1568 completion_tracker::completion_tracker ()
1570 discard_completions ();
1573 /* See completer.h. */
1576 completion_tracker::discard_completions ()
1578 xfree (m_lowest_common_denominator
);
1579 m_lowest_common_denominator
= NULL
;
1581 m_lowest_common_denominator_unique
= false;
1582 m_lowest_common_denominator_valid
= false;
1584 /* A null check here allows this function to be used from the
1586 if (m_entries_hash
!= NULL
)
1587 htab_delete (m_entries_hash
);
1589 /* A callback used by the hash table to compare new entries with existing
1590 entries. We can't use the standard streq_hash function here as the
1591 key to our hash is just a single string, while the values we store in
1592 the hash are a struct containing multiple strings. */
1593 static auto entry_eq_func
1594 = [] (const void *first
, const void *second
) -> int
1596 /* The FIRST argument is the entry already in the hash table, and
1597 the SECOND argument is the new item being inserted. */
1598 const completion_hash_entry
*entry
1599 = (const completion_hash_entry
*) first
;
1600 const char *name_str
= (const char *) second
;
1602 return entry
->is_name_eq (name_str
);
1605 m_entries_hash
= htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE
,
1606 htab_hash_string
, entry_eq_func
,
1607 completion_hash_entry::deleter
,
1611 /* See completer.h. */
1613 completion_tracker::~completion_tracker ()
1615 xfree (m_lowest_common_denominator
);
1616 htab_delete (m_entries_hash
);
1619 /* See completer.h. */
1622 completion_tracker::maybe_add_completion
1623 (gdb::unique_xmalloc_ptr
<char> name
,
1624 completion_match_for_lcd
*match_for_lcd
,
1625 const char *text
, const char *word
)
1629 if (max_completions
== 0)
1632 if (htab_elements (m_entries_hash
) >= max_completions
)
1635 hashval_t hash
= htab_hash_string (name
.get ());
1636 slot
= htab_find_slot_with_hash (m_entries_hash
, name
.get (), hash
, INSERT
);
1637 if (*slot
== HTAB_EMPTY_ENTRY
)
1639 const char *match_for_lcd_str
= NULL
;
1641 if (match_for_lcd
!= NULL
)
1642 match_for_lcd_str
= match_for_lcd
->finish ();
1644 if (match_for_lcd_str
== NULL
)
1645 match_for_lcd_str
= name
.get ();
1647 gdb::unique_xmalloc_ptr
<char> lcd
1648 = make_completion_match_str (match_for_lcd_str
, text
, word
);
1650 size_t lcd_len
= strlen (lcd
.get ());
1651 *slot
= new completion_hash_entry (std::move (name
), std::move (lcd
));
1653 m_lowest_common_denominator_valid
= false;
1654 m_lowest_common_denominator_max_length
1655 = std::max (m_lowest_common_denominator_max_length
, lcd_len
);
1661 /* See completer.h. */
1664 completion_tracker::add_completion (gdb::unique_xmalloc_ptr
<char> name
,
1665 completion_match_for_lcd
*match_for_lcd
,
1666 const char *text
, const char *word
)
1668 if (!maybe_add_completion (std::move (name
), match_for_lcd
, text
, word
))
1669 throw_error (MAX_COMPLETIONS_REACHED_ERROR
, _("Max completions reached."));
1672 /* See completer.h. */
1675 completion_tracker::add_completions (completion_list
&&list
)
1677 for (auto &candidate
: list
)
1678 add_completion (std::move (candidate
));
1681 /* Helper for the make_completion_match_str overloads. Returns NULL
1682 as an indication that we want MATCH_NAME exactly. It is up to the
1683 caller to xstrdup that string if desired. */
1686 make_completion_match_str_1 (const char *match_name
,
1687 const char *text
, const char *word
)
1693 /* Return NULL as an indication that we want MATCH_NAME
1697 else if (word
> text
)
1699 /* Return some portion of MATCH_NAME. */
1700 newobj
= xstrdup (match_name
+ (word
- text
));
1704 /* Return some of WORD plus MATCH_NAME. */
1705 size_t len
= strlen (match_name
);
1706 newobj
= (char *) xmalloc (text
- word
+ len
+ 1);
1707 memcpy (newobj
, word
, text
- word
);
1708 memcpy (newobj
+ (text
- word
), match_name
, len
+ 1);
1714 /* See completer.h. */
1716 gdb::unique_xmalloc_ptr
<char>
1717 make_completion_match_str (const char *match_name
,
1718 const char *text
, const char *word
)
1720 char *newobj
= make_completion_match_str_1 (match_name
, text
, word
);
1722 newobj
= xstrdup (match_name
);
1723 return gdb::unique_xmalloc_ptr
<char> (newobj
);
1726 /* See completer.h. */
1728 gdb::unique_xmalloc_ptr
<char>
1729 make_completion_match_str (gdb::unique_xmalloc_ptr
<char> &&match_name
,
1730 const char *text
, const char *word
)
1732 char *newobj
= make_completion_match_str_1 (match_name
.get (), text
, word
);
1734 return std::move (match_name
);
1735 return gdb::unique_xmalloc_ptr
<char> (newobj
);
1738 /* See complete.h. */
1741 complete (const char *line
, char const **word
, int *quote_char
)
1743 completion_tracker tracker_handle_brkchars
;
1744 completion_tracker tracker_handle_completions
;
1745 completion_tracker
*tracker
;
1747 /* The WORD should be set to the end of word to complete. We initialize
1748 to the completion point which is assumed to be at the end of LINE.
1749 This leaves WORD to be initialized to a sensible value in cases
1750 completion_find_completion_word() fails i.e., throws an exception.
1752 *word
= line
+ strlen (line
);
1756 *word
= completion_find_completion_word (tracker_handle_brkchars
,
1759 /* Completers that provide a custom word point in the
1760 handle_brkchars phase also compute their completions then.
1761 Completers that leave the completion word handling to readline
1762 must be called twice. */
1763 if (tracker_handle_brkchars
.use_custom_word_point ())
1764 tracker
= &tracker_handle_brkchars
;
1767 complete_line (tracker_handle_completions
, *word
, line
, strlen (line
));
1768 tracker
= &tracker_handle_completions
;
1771 catch (const gdb_exception
&ex
)
1776 return tracker
->build_completion_result (*word
, *word
- line
, strlen (line
));
1780 /* Generate completions all at once. Does nothing if max_completions
1781 is 0. If max_completions is non-negative, this will collect at
1782 most max_completions strings.
1784 TEXT is the caller's idea of the "word" we are looking at.
1786 LINE_BUFFER is available to be looked at; it contains the entire
1789 POINT is the offset in that line of the cursor. You
1790 should pretend that the line ends at POINT. */
1793 complete_line (completion_tracker
&tracker
,
1794 const char *text
, const char *line_buffer
, int point
)
1796 if (max_completions
== 0)
1798 complete_line_internal (tracker
, text
, line_buffer
, point
,
1799 handle_completions
);
1802 /* Complete on command names. Used by "help". */
1805 command_completer (struct cmd_list_element
*ignore
,
1806 completion_tracker
&tracker
,
1807 const char *text
, const char *word
)
1809 complete_line_internal (tracker
, word
, text
,
1810 strlen (text
), handle_help
);
1813 /* The corresponding completer_handle_brkchars implementation. */
1816 command_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1817 completion_tracker
&tracker
,
1818 const char *text
, const char *word
)
1820 set_rl_completer_word_break_characters
1821 (gdb_completer_command_word_break_characters
);
1824 /* Complete on signals. */
1827 signal_completer (struct cmd_list_element
*ignore
,
1828 completion_tracker
&tracker
,
1829 const char *text
, const char *word
)
1831 size_t len
= strlen (word
);
1833 const char *signame
;
1835 for (signum
= GDB_SIGNAL_FIRST
; signum
!= GDB_SIGNAL_LAST
; ++signum
)
1837 /* Can't handle this, so skip it. */
1838 if (signum
== GDB_SIGNAL_0
)
1841 signame
= gdb_signal_to_name ((enum gdb_signal
) signum
);
1843 /* Ignore the unknown signal case. */
1844 if (!signame
|| strcmp (signame
, "?") == 0)
1847 if (strncasecmp (signame
, word
, len
) == 0)
1848 tracker
.add_completion (make_unique_xstrdup (signame
));
1852 /* Bit-flags for selecting what the register and/or register-group
1853 completer should complete on. */
1855 enum reg_completer_target
1857 complete_register_names
= 0x1,
1858 complete_reggroup_names
= 0x2
1860 DEF_ENUM_FLAGS_TYPE (enum reg_completer_target
, reg_completer_targets
);
1862 /* Complete register names and/or reggroup names based on the value passed
1863 in TARGETS. At least one bit in TARGETS must be set. */
1866 reg_or_group_completer_1 (completion_tracker
&tracker
,
1867 const char *text
, const char *word
,
1868 reg_completer_targets targets
)
1870 size_t len
= strlen (word
);
1871 struct gdbarch
*gdbarch
;
1874 gdb_assert ((targets
& (complete_register_names
1875 | complete_reggroup_names
)) != 0);
1876 gdbarch
= get_current_arch ();
1878 if ((targets
& complete_register_names
) != 0)
1883 (name
= user_reg_map_regnum_to_name (gdbarch
, i
)) != NULL
;
1886 if (*name
!= '\0' && strncmp (word
, name
, len
) == 0)
1887 tracker
.add_completion (make_unique_xstrdup (name
));
1891 if ((targets
& complete_reggroup_names
) != 0)
1893 struct reggroup
*group
;
1895 for (group
= reggroup_next (gdbarch
, NULL
);
1897 group
= reggroup_next (gdbarch
, group
))
1899 name
= reggroup_name (group
);
1900 if (strncmp (word
, name
, len
) == 0)
1901 tracker
.add_completion (make_unique_xstrdup (name
));
1906 /* Perform completion on register and reggroup names. */
1909 reg_or_group_completer (struct cmd_list_element
*ignore
,
1910 completion_tracker
&tracker
,
1911 const char *text
, const char *word
)
1913 reg_or_group_completer_1 (tracker
, text
, word
,
1914 (complete_register_names
1915 | complete_reggroup_names
));
1918 /* Perform completion on reggroup names. */
1921 reggroup_completer (struct cmd_list_element
*ignore
,
1922 completion_tracker
&tracker
,
1923 const char *text
, const char *word
)
1925 reg_or_group_completer_1 (tracker
, text
, word
,
1926 complete_reggroup_names
);
1929 /* The default completer_handle_brkchars implementation. */
1932 default_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1933 completion_tracker
&tracker
,
1934 const char *text
, const char *word
)
1936 set_rl_completer_word_break_characters
1937 (current_language
->la_word_break_characters ());
1940 /* See definition in completer.h. */
1942 completer_handle_brkchars_ftype
*
1943 completer_handle_brkchars_func_for_completer (completer_ftype
*fn
)
1945 if (fn
== filename_completer
)
1946 return filename_completer_handle_brkchars
;
1948 if (fn
== location_completer
)
1949 return location_completer_handle_brkchars
;
1951 if (fn
== command_completer
)
1952 return command_completer_handle_brkchars
;
1954 return default_completer_handle_brkchars
;
1957 /* Used as brkchars when we want to tell readline we have a custom
1958 word point. We do that by making our rl_completion_word_break_hook
1959 set RL_POINT to the desired word point, and return the character at
1960 the word break point as the break char. This is two bytes in order
1961 to fit one break character plus the terminating null. */
1962 static char gdb_custom_word_point_brkchars
[2];
1964 /* Since rl_basic_quote_characters is not completer-specific, we save
1965 its original value here, in order to be able to restore it in
1966 gdb_rl_attempted_completion_function. */
1967 static const char *gdb_org_rl_basic_quote_characters
= rl_basic_quote_characters
;
1969 /* Get the list of chars that are considered as word breaks
1970 for the current command. */
1973 gdb_completion_word_break_characters_throw ()
1975 /* New completion starting. Get rid of the previous tracker and
1977 delete current_completion
.tracker
;
1978 current_completion
.tracker
= new completion_tracker ();
1980 completion_tracker
&tracker
= *current_completion
.tracker
;
1982 complete_line_internal (tracker
, NULL
, rl_line_buffer
,
1983 rl_point
, handle_brkchars
);
1985 if (tracker
.use_custom_word_point ())
1987 gdb_assert (tracker
.custom_word_point () > 0);
1988 rl_point
= tracker
.custom_word_point () - 1;
1990 gdb_assert (rl_point
>= 0 && rl_point
< strlen (rl_line_buffer
));
1992 gdb_custom_word_point_brkchars
[0] = rl_line_buffer
[rl_point
];
1993 rl_completer_word_break_characters
= gdb_custom_word_point_brkchars
;
1994 rl_completer_quote_characters
= NULL
;
1996 /* Clear this too, so that if we're completing a quoted string,
1997 readline doesn't consider the quote character a delimiter.
1998 If we didn't do this, readline would auto-complete {b
1999 'fun<tab>} to {'b 'function()'}, i.e., add the terminating
2000 \', but, it wouldn't append the separator space either, which
2001 is not desirable. So instead we take care of appending the
2002 quote character to the LCD ourselves, in
2003 gdb_rl_attempted_completion_function. Since this global is
2004 not just completer-specific, we'll restore it back to the
2005 default in gdb_rl_attempted_completion_function. */
2006 rl_basic_quote_characters
= NULL
;
2009 return rl_completer_word_break_characters
;
2013 gdb_completion_word_break_characters ()
2015 /* New completion starting. */
2016 current_completion
.aborted
= false;
2020 return gdb_completion_word_break_characters_throw ();
2022 catch (const gdb_exception
&ex
)
2024 /* Set this to that gdb_rl_attempted_completion_function knows
2026 current_completion
.aborted
= true;
2032 /* See completer.h. */
2035 completion_find_completion_word (completion_tracker
&tracker
, const char *text
,
2038 size_t point
= strlen (text
);
2040 complete_line_internal (tracker
, NULL
, text
, point
, handle_brkchars
);
2042 if (tracker
.use_custom_word_point ())
2044 gdb_assert (tracker
.custom_word_point () > 0);
2045 *quote_char
= tracker
.quote_char ();
2046 return text
+ tracker
.custom_word_point ();
2049 gdb_rl_completion_word_info info
;
2051 info
.word_break_characters
= rl_completer_word_break_characters
;
2052 info
.quote_characters
= gdb_completer_quote_characters
;
2053 info
.basic_quote_characters
= rl_basic_quote_characters
;
2055 return gdb_rl_find_completion_word (&info
, quote_char
, NULL
, text
);
2058 /* See completer.h. */
2061 completion_tracker::recompute_lcd_visitor (completion_hash_entry
*entry
)
2063 if (!m_lowest_common_denominator_valid
)
2065 /* This is the first lowest common denominator that we are
2066 considering, just copy it in. */
2067 strcpy (m_lowest_common_denominator
, entry
->get_lcd ());
2068 m_lowest_common_denominator_unique
= true;
2069 m_lowest_common_denominator_valid
= true;
2073 /* Find the common denominator between the currently-known lowest
2074 common denominator and NEW_MATCH_UP. That becomes the new lowest
2075 common denominator. */
2077 const char *new_match
= entry
->get_lcd ();
2080 (new_match
[i
] != '\0'
2081 && new_match
[i
] == m_lowest_common_denominator
[i
]);
2084 if (m_lowest_common_denominator
[i
] != new_match
[i
])
2086 m_lowest_common_denominator
[i
] = '\0';
2087 m_lowest_common_denominator_unique
= false;
2092 /* See completer.h. */
2095 completion_tracker::recompute_lowest_common_denominator ()
2097 /* We've already done this. */
2098 if (m_lowest_common_denominator_valid
)
2101 /* Resize the storage to ensure we have enough space, the plus one gives
2102 us space for the trailing null terminator we will include. */
2103 m_lowest_common_denominator
2104 = (char *) xrealloc (m_lowest_common_denominator
,
2105 m_lowest_common_denominator_max_length
+ 1);
2107 /* Callback used to visit each entry in the m_entries_hash. */
2109 = [] (void **slot
, void *info
) -> int
2111 completion_tracker
*obj
= (completion_tracker
*) info
;
2112 completion_hash_entry
*entry
= (completion_hash_entry
*) *slot
;
2113 obj
->recompute_lcd_visitor (entry
);
2117 htab_traverse (m_entries_hash
, visitor_func
, this);
2118 m_lowest_common_denominator_valid
= true;
2121 /* See completer.h. */
2124 completion_tracker::advance_custom_word_point_by (int len
)
2126 m_custom_word_point
+= len
;
2129 /* Build a new C string that is a copy of LCD with the whitespace of
2130 ORIG/ORIG_LEN preserved.
2132 Say the user is completing a symbol name, with spaces, like:
2136 and the resulting completion match is:
2140 we want to end up with an input line like:
2143 ^^^^^^^ => text from LCD [1], whitespace from ORIG preserved.
2144 ^^ => new text from LCD
2146 [1] - We must take characters from the LCD instead of the original
2147 text, since some completions want to change upper/lowercase. E.g.:
2153 "handle SIG[QUIT|etc.]"
2157 expand_preserving_ws (const char *orig
, size_t orig_len
,
2160 const char *p_orig
= orig
;
2161 const char *orig_end
= orig
+ orig_len
;
2162 const char *p_lcd
= lcd
;
2165 while (p_orig
< orig_end
)
2169 while (p_orig
< orig_end
&& *p_orig
== ' ')
2171 p_lcd
= skip_spaces (p_lcd
);
2175 /* Take characters from the LCD instead of the original
2176 text, since some completions change upper/lowercase.
2180 "handle SIG[QUIT|etc.]"
2188 while (*p_lcd
!= '\0')
2191 return xstrdup (res
.c_str ());
2194 /* See completer.h. */
2197 completion_tracker::build_completion_result (const char *text
,
2200 size_t element_count
= htab_elements (m_entries_hash
);
2202 if (element_count
== 0)
2205 /* +1 for the LCD, and +1 for NULL termination. */
2206 char **match_list
= XNEWVEC (char *, 1 + element_count
+ 1);
2208 /* Build replacement word, based on the LCD. */
2210 recompute_lowest_common_denominator ();
2212 = expand_preserving_ws (text
, end
- start
,
2213 m_lowest_common_denominator
);
2215 if (m_lowest_common_denominator_unique
)
2217 /* We don't rely on readline appending the quote char as
2218 delimiter as then readline wouldn't append the ' ' after the
2220 char buf
[2] = { (char) quote_char () };
2222 match_list
[0] = reconcat (match_list
[0], match_list
[0],
2223 buf
, (char *) NULL
);
2224 match_list
[1] = NULL
;
2226 /* If the tracker wants to, or we already have a space at the
2227 end of the match, tell readline to skip appending
2229 bool completion_suppress_append
2230 = (suppress_append_ws ()
2231 || match_list
[0][strlen (match_list
[0]) - 1] == ' ');
2233 return completion_result (match_list
, 1, completion_suppress_append
);
2237 /* State object used while building the completion list. */
2240 list_builder (char **ml
)
2245 /* The list we are filling. */
2248 /* The next index in the list to write to. */
2251 list_builder
builder (match_list
);
2253 /* Visit each entry in m_entries_hash and add it to the completion
2254 list, updating the builder state object. */
2256 = [] (void **slot
, void *info
) -> int
2258 completion_hash_entry
*entry
= (completion_hash_entry
*) *slot
;
2259 list_builder
*state
= (list_builder
*) info
;
2261 state
->match_list
[state
->index
] = entry
->release_name ();
2266 /* Build the completion list and add a null at the end. */
2267 htab_traverse_noresize (m_entries_hash
, func
, &builder
);
2268 match_list
[builder
.index
] = NULL
;
2270 return completion_result (match_list
, builder
.index
- 1, false);
2274 /* See completer.h */
2276 completion_result::completion_result ()
2277 : match_list (NULL
), number_matches (0),
2278 completion_suppress_append (false)
2281 /* See completer.h */
2283 completion_result::completion_result (char **match_list_
,
2284 size_t number_matches_
,
2285 bool completion_suppress_append_
)
2286 : match_list (match_list_
),
2287 number_matches (number_matches_
),
2288 completion_suppress_append (completion_suppress_append_
)
2291 /* See completer.h */
2293 completion_result::~completion_result ()
2295 reset_match_list ();
2298 /* See completer.h */
2300 completion_result::completion_result (completion_result
&&rhs
)
2305 reset_match_list ();
2306 match_list
= rhs
.match_list
;
2307 rhs
.match_list
= NULL
;
2308 number_matches
= rhs
.number_matches
;
2309 rhs
.number_matches
= 0;
2312 /* See completer.h */
2315 completion_result::release_match_list ()
2317 char **ret
= match_list
;
2322 /* See completer.h */
2325 completion_result::sort_match_list ()
2327 if (number_matches
> 1)
2329 /* Element 0 is special (it's the common prefix), leave it
2331 std::sort (&match_list
[1],
2332 &match_list
[number_matches
+ 1],
2337 /* See completer.h */
2340 completion_result::reset_match_list ()
2342 if (match_list
!= NULL
)
2344 for (char **p
= match_list
; *p
!= NULL
; p
++)
2351 /* Helper for gdb_rl_attempted_completion_function, which does most of
2352 the work. This is called by readline to build the match list array
2353 and to determine the lowest common denominator. The real matches
2354 list starts at match[1], while match[0] is the slot holding
2355 readline's idea of the lowest common denominator of all matches,
2356 which is what readline replaces the completion "word" with.
2358 TEXT is the caller's idea of the "word" we are looking at, as
2359 computed in the handle_brkchars phase.
2361 START is the offset from RL_LINE_BUFFER where TEXT starts. END is
2362 the offset from RL_LINE_BUFFER where TEXT ends (i.e., where
2365 You should thus pretend that the line ends at END (relative to
2368 RL_LINE_BUFFER contains the entire text of the line. RL_POINT is
2369 the offset in that line of the cursor. You should pretend that the
2372 Returns NULL if there are no completions. */
2375 gdb_rl_attempted_completion_function_throw (const char *text
, int start
, int end
)
2377 /* Completers that provide a custom word point in the
2378 handle_brkchars phase also compute their completions then.
2379 Completers that leave the completion word handling to readline
2380 must be called twice. If rl_point (i.e., END) is at column 0,
2381 then readline skips the handle_brkchars phase, and so we create a
2382 tracker now in that case too. */
2383 if (end
== 0 || !current_completion
.tracker
->use_custom_word_point ())
2385 delete current_completion
.tracker
;
2386 current_completion
.tracker
= new completion_tracker ();
2388 complete_line (*current_completion
.tracker
, text
,
2389 rl_line_buffer
, rl_point
);
2392 completion_tracker
&tracker
= *current_completion
.tracker
;
2394 completion_result result
2395 = tracker
.build_completion_result (text
, start
, end
);
2397 rl_completion_suppress_append
= result
.completion_suppress_append
;
2398 return result
.release_match_list ();
2401 /* Function installed as "rl_attempted_completion_function" readline
2402 hook. Wrapper around gdb_rl_attempted_completion_function_throw
2403 that catches C++ exceptions, which can't cross readline. */
2406 gdb_rl_attempted_completion_function (const char *text
, int start
, int end
)
2408 /* Restore globals that might have been tweaked in
2409 gdb_completion_word_break_characters. */
2410 rl_basic_quote_characters
= gdb_org_rl_basic_quote_characters
;
2412 /* If we end up returning NULL, either on error, or simple because
2413 there are no matches, inhibit readline's default filename
2415 rl_attempted_completion_over
= 1;
2417 /* If the handle_brkchars phase was aborted, don't try
2419 if (current_completion
.aborted
)
2424 return gdb_rl_attempted_completion_function_throw (text
, start
, end
);
2426 catch (const gdb_exception
&ex
)
2433 /* Skip over the possibly quoted word STR (as defined by the quote
2434 characters QUOTECHARS and the word break characters BREAKCHARS).
2435 Returns pointer to the location after the "word". If either
2436 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
2440 skip_quoted_chars (const char *str
, const char *quotechars
,
2441 const char *breakchars
)
2443 char quote_char
= '\0';
2446 if (quotechars
== NULL
)
2447 quotechars
= gdb_completer_quote_characters
;
2449 if (breakchars
== NULL
)
2450 breakchars
= current_language
->la_word_break_characters();
2452 for (scan
= str
; *scan
!= '\0'; scan
++)
2454 if (quote_char
!= '\0')
2456 /* Ignore everything until the matching close quote char. */
2457 if (*scan
== quote_char
)
2459 /* Found matching close quote. */
2464 else if (strchr (quotechars
, *scan
))
2466 /* Found start of a quoted string. */
2469 else if (strchr (breakchars
, *scan
))
2478 /* Skip over the possibly quoted word STR (as defined by the quote
2479 characters and word break characters used by the completer).
2480 Returns pointer to the location after the "word". */
2483 skip_quoted (const char *str
)
2485 return skip_quoted_chars (str
, NULL
, NULL
);
2488 /* Return a message indicating that the maximum number of completions
2489 has been reached and that there may be more. */
2492 get_max_completions_reached_message (void)
2494 return _("*** List may be truncated, max-completions reached. ***");
2497 /* GDB replacement for rl_display_match_list.
2498 Readline doesn't provide a clean interface for TUI(curses).
2499 A hack previously used was to send readline's rl_outstream through a pipe
2500 and read it from the event loop. Bleah. IWBN if readline abstracted
2501 away all the necessary bits, and this is what this code does. It
2502 replicates the parts of readline we need and then adds an abstraction
2503 layer, currently implemented as struct match_list_displayer, so that both
2504 CLI and TUI can use it. We copy all this readline code to minimize
2505 GDB-specific mods to readline. Once this code performs as desired then
2506 we can submit it to the readline maintainers.
2508 N.B. A lot of the code is the way it is in order to minimize differences
2509 from readline's copy. */
2511 /* Not supported here. */
2512 #undef VISIBLE_STATS
2514 #if defined (HANDLE_MULTIBYTE)
2515 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
2516 #define MB_NULLWCH(x) ((x) == 0)
2519 #define ELLIPSIS_LEN 3
2521 /* gdb version of readline/complete.c:get_y_or_n.
2522 'y' -> returns 1, and 'n' -> returns 0.
2523 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
2524 If FOR_PAGER is non-zero, then also supported are:
2525 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
2528 gdb_get_y_or_n (int for_pager
, const struct match_list_displayer
*displayer
)
2534 RL_SETSTATE (RL_STATE_MOREINPUT
);
2535 c
= displayer
->read_key (displayer
);
2536 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
2538 if (c
== 'y' || c
== 'Y' || c
== ' ')
2540 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
2542 if (c
== ABORT_CHAR
|| c
< 0)
2544 /* Readline doesn't erase_entire_line here, but without it the
2545 --More-- prompt isn't erased and neither is the text entered
2546 thus far redisplayed. */
2547 displayer
->erase_entire_line (displayer
);
2548 /* Note: The arguments to rl_abort are ignored. */
2551 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
2553 if (for_pager
&& (c
== 'q' || c
== 'Q'))
2555 displayer
->beep (displayer
);
2559 /* Pager function for tab-completion.
2560 This is based on readline/complete.c:_rl_internal_pager.
2561 LINES is the number of lines of output displayed thus far.
2563 -1 -> user pressed 'n' or equivalent,
2564 0 -> user pressed 'y' or equivalent,
2565 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
2568 gdb_display_match_list_pager (int lines
,
2569 const struct match_list_displayer
*displayer
)
2573 displayer
->puts (displayer
, "--More--");
2574 displayer
->flush (displayer
);
2575 i
= gdb_get_y_or_n (1, displayer
);
2576 displayer
->erase_entire_line (displayer
);
2585 /* Return non-zero if FILENAME is a directory.
2586 Based on readline/complete.c:path_isdir. */
2589 gdb_path_isdir (const char *filename
)
2593 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
2596 /* Return the portion of PATHNAME that should be output when listing
2597 possible completions. If we are hacking filename completion, we
2598 are only interested in the basename, the portion following the
2599 final slash. Otherwise, we return what we were passed. Since
2600 printing empty strings is not very informative, if we're doing
2601 filename completion, and the basename is the empty string, we look
2602 for the previous slash and return the portion following that. If
2603 there's no previous slash, we just return what we were passed.
2605 Based on readline/complete.c:printable_part. */
2608 gdb_printable_part (char *pathname
)
2612 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
2615 temp
= strrchr (pathname
, '/');
2616 #if defined (__MSDOS__)
2617 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
2618 temp
= pathname
+ 1;
2621 if (temp
== 0 || *temp
== '\0')
2623 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
2624 Look for a previous slash and, if one is found, return the portion
2625 following that slash. If there's no previous slash, just return the
2626 pathname we were passed. */
2627 else if (temp
[1] == '\0')
2629 for (x
= temp
- 1; x
> pathname
; x
--)
2632 return ((*x
== '/') ? x
+ 1 : pathname
);
2638 /* Compute width of STRING when displayed on screen by print_filename.
2639 Based on readline/complete.c:fnwidth. */
2642 gdb_fnwidth (const char *string
)
2645 #if defined (HANDLE_MULTIBYTE)
2651 left
= strlen (string
) + 1;
2652 memset (&ps
, 0, sizeof (mbstate_t));
2658 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
2665 #if defined (HANDLE_MULTIBYTE)
2666 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
2667 if (MB_INVALIDCH (clen
))
2671 memset (&ps
, 0, sizeof (mbstate_t));
2673 else if (MB_NULLWCH (clen
))
2679 width
+= (w
>= 0) ? w
: 1;
2691 /* Print TO_PRINT, one matching completion.
2692 PREFIX_BYTES is number of common prefix bytes.
2693 Based on readline/complete.c:fnprint. */
2696 gdb_fnprint (const char *to_print
, int prefix_bytes
,
2697 const struct match_list_displayer
*displayer
)
2701 #if defined (HANDLE_MULTIBYTE)
2708 end
= to_print
+ strlen (to_print
) + 1;
2709 memset (&ps
, 0, sizeof (mbstate_t));
2714 /* Don't print only the ellipsis if the common prefix is one of the
2715 possible completions */
2716 if (to_print
[prefix_bytes
] == '\0')
2723 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
2724 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
2725 displayer
->putch (displayer
, ellipsis
);
2726 printed_len
= ELLIPSIS_LEN
;
2729 s
= to_print
+ prefix_bytes
;
2734 displayer
->putch (displayer
, '^');
2735 displayer
->putch (displayer
, UNCTRL (*s
));
2738 #if defined (HANDLE_MULTIBYTE)
2739 memset (&ps
, 0, sizeof (mbstate_t));
2742 else if (*s
== RUBOUT
)
2744 displayer
->putch (displayer
, '^');
2745 displayer
->putch (displayer
, '?');
2748 #if defined (HANDLE_MULTIBYTE)
2749 memset (&ps
, 0, sizeof (mbstate_t));
2754 #if defined (HANDLE_MULTIBYTE)
2755 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
2756 if (MB_INVALIDCH (tlen
))
2760 memset (&ps
, 0, sizeof (mbstate_t));
2762 else if (MB_NULLWCH (tlen
))
2767 width
= (w
>= 0) ? w
: 1;
2769 for (w
= 0; w
< tlen
; ++w
)
2770 displayer
->putch (displayer
, s
[w
]);
2772 printed_len
+= width
;
2774 displayer
->putch (displayer
, *s
);
2784 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
2785 are using it, check for and output a single character for `special'
2786 filenames. Return the number of characters we output.
2787 Based on readline/complete.c:print_filename. */
2790 gdb_print_filename (char *to_print
, char *full_pathname
, int prefix_bytes
,
2791 const struct match_list_displayer
*displayer
)
2793 int printed_len
, extension_char
, slen
, tlen
;
2794 char *s
, c
, *new_full_pathname
;
2796 extern int _rl_complete_mark_directories
;
2799 printed_len
= gdb_fnprint (to_print
, prefix_bytes
, displayer
);
2801 #if defined (VISIBLE_STATS)
2802 if (rl_filename_completion_desired
&& (rl_visible_stats
|| _rl_complete_mark_directories
))
2804 if (rl_filename_completion_desired
&& _rl_complete_mark_directories
)
2807 /* If to_print != full_pathname, to_print is the basename of the
2808 path passed. In this case, we try to expand the directory
2809 name before checking for the stat character. */
2810 if (to_print
!= full_pathname
)
2812 /* Terminate the directory name. */
2814 to_print
[-1] = '\0';
2816 /* If setting the last slash in full_pathname to a NUL results in
2817 full_pathname being the empty string, we are trying to complete
2818 files in the root directory. If we pass a null string to the
2819 bash directory completion hook, for example, it will expand it
2820 to the current directory. We just want the `/'. */
2821 if (full_pathname
== 0 || *full_pathname
== 0)
2823 else if (full_pathname
[0] != '/')
2825 else if (full_pathname
[1] == 0)
2826 dn
= "//"; /* restore trailing slash to `//' */
2827 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
2828 dn
= "/"; /* don't turn /// into // */
2831 s
= tilde_expand (dn
);
2832 if (rl_directory_completion_hook
)
2833 (*rl_directory_completion_hook
) (&s
);
2836 tlen
= strlen (to_print
);
2837 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
2838 strcpy (new_full_pathname
, s
);
2839 if (s
[slen
- 1] == '/')
2842 new_full_pathname
[slen
] = '/';
2843 new_full_pathname
[slen
] = '/';
2844 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
2846 #if defined (VISIBLE_STATS)
2847 if (rl_visible_stats
)
2848 extension_char
= stat_char (new_full_pathname
);
2851 if (gdb_path_isdir (new_full_pathname
))
2852 extension_char
= '/';
2854 xfree (new_full_pathname
);
2859 s
= tilde_expand (full_pathname
);
2860 #if defined (VISIBLE_STATS)
2861 if (rl_visible_stats
)
2862 extension_char
= stat_char (s
);
2865 if (gdb_path_isdir (s
))
2866 extension_char
= '/';
2872 displayer
->putch (displayer
, extension_char
);
2880 /* GDB version of readline/complete.c:complete_get_screenwidth. */
2883 gdb_complete_get_screenwidth (const struct match_list_displayer
*displayer
)
2885 /* Readline has other stuff here which it's not clear we need. */
2886 return displayer
->width
;
2889 extern int _rl_completion_prefix_display_length
;
2890 extern int _rl_print_completions_horizontally
;
2892 EXTERN_C
int _rl_qsort_string_compare (const void *, const void *);
2893 typedef int QSFUNC (const void *, const void *);
2895 /* GDB version of readline/complete.c:rl_display_match_list.
2896 See gdb_display_match_list for a description of MATCHES, LEN, MAX.
2897 Returns non-zero if all matches are displayed. */
2900 gdb_display_match_list_1 (char **matches
, int len
, int max
,
2901 const struct match_list_displayer
*displayer
)
2903 int count
, limit
, printed_len
, lines
, cols
;
2904 int i
, j
, k
, l
, common_length
, sind
;
2906 int page_completions
= displayer
->height
!= INT_MAX
&& pagination_enabled
;
2908 /* Find the length of the prefix common to all items: length as displayed
2909 characters (common_length) and as a byte index into the matches (sind) */
2910 common_length
= sind
= 0;
2911 if (_rl_completion_prefix_display_length
> 0)
2913 t
= gdb_printable_part (matches
[0]);
2914 temp
= strrchr (t
, '/');
2915 common_length
= temp
? gdb_fnwidth (temp
) : gdb_fnwidth (t
);
2916 sind
= temp
? strlen (temp
) : strlen (t
);
2918 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
2919 max
-= common_length
- ELLIPSIS_LEN
;
2921 common_length
= sind
= 0;
2924 /* How many items of MAX length can we fit in the screen window? */
2925 cols
= gdb_complete_get_screenwidth (displayer
);
2928 if (limit
!= 1 && (limit
* max
== cols
))
2931 /* If cols == 0, limit will end up -1 */
2932 if (cols
< displayer
->width
&& limit
< 0)
2935 /* Avoid a possible floating exception. If max > cols,
2936 limit will be 0 and a divide-by-zero fault will result. */
2940 /* How many iterations of the printing loop? */
2941 count
= (len
+ (limit
- 1)) / limit
;
2943 /* Watch out for special case. If LEN is less than LIMIT, then
2944 just do the inner printing loop.
2945 0 < len <= limit implies count = 1. */
2947 /* Sort the items if they are not already sorted. */
2948 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
2949 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
2951 displayer
->crlf (displayer
);
2954 if (_rl_print_completions_horizontally
== 0)
2956 /* Print the sorted items, up-and-down alphabetically, like ls. */
2957 for (i
= 1; i
<= count
; i
++)
2959 for (j
= 0, l
= i
; j
< limit
; j
++)
2961 if (l
> len
|| matches
[l
] == 0)
2965 temp
= gdb_printable_part (matches
[l
]);
2966 printed_len
= gdb_print_filename (temp
, matches
[l
], sind
,
2970 for (k
= 0; k
< max
- printed_len
; k
++)
2971 displayer
->putch (displayer
, ' ');
2975 displayer
->crlf (displayer
);
2977 if (page_completions
&& lines
>= (displayer
->height
- 1) && i
< count
)
2979 lines
= gdb_display_match_list_pager (lines
, displayer
);
2987 /* Print the sorted items, across alphabetically, like ls -x. */
2988 for (i
= 1; matches
[i
]; i
++)
2990 temp
= gdb_printable_part (matches
[i
]);
2991 printed_len
= gdb_print_filename (temp
, matches
[i
], sind
, displayer
);
2992 /* Have we reached the end of this line? */
2995 if (i
&& (limit
> 1) && (i
% limit
) == 0)
2997 displayer
->crlf (displayer
);
2999 if (page_completions
&& lines
>= displayer
->height
- 1)
3001 lines
= gdb_display_match_list_pager (lines
, displayer
);
3007 for (k
= 0; k
< max
- printed_len
; k
++)
3008 displayer
->putch (displayer
, ' ');
3011 displayer
->crlf (displayer
);
3017 /* Utility for displaying completion list matches, used by both CLI and TUI.
3019 MATCHES is the list of strings, in argv format, LEN is the number of
3020 strings in MATCHES, and MAX is the length of the longest string in
3024 gdb_display_match_list (char **matches
, int len
, int max
,
3025 const struct match_list_displayer
*displayer
)
3027 /* Readline will never call this if complete_line returned NULL. */
3028 gdb_assert (max_completions
!= 0);
3030 /* complete_line will never return more than this. */
3031 if (max_completions
> 0)
3032 gdb_assert (len
<= max_completions
);
3034 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
3038 /* We can't use *query here because they wait for <RET> which is
3039 wrong here. This follows the readline version as closely as possible
3040 for compatibility's sake. See readline/complete.c. */
3042 displayer
->crlf (displayer
);
3044 xsnprintf (msg
, sizeof (msg
),
3045 "Display all %d possibilities? (y or n)", len
);
3046 displayer
->puts (displayer
, msg
);
3047 displayer
->flush (displayer
);
3049 if (gdb_get_y_or_n (0, displayer
) == 0)
3051 displayer
->crlf (displayer
);
3056 if (gdb_display_match_list_1 (matches
, len
, max
, displayer
))
3058 /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
3059 if (len
== max_completions
)
3061 /* The maximum number of completions has been reached. Warn the user
3062 that there may be more. */
3063 const char *message
= get_max_completions_reached_message ();
3065 displayer
->puts (displayer
, message
);
3066 displayer
->crlf (displayer
);
3071 void _initialize_completer ();
3073 _initialize_completer ()
3075 add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class
,
3076 &max_completions
, _("\
3077 Set maximum number of completion candidates."), _("\
3078 Show maximum number of completion candidates."), _("\
3079 Use this to limit the number of candidates considered\n\
3080 during completion. Specifying \"unlimited\" or -1\n\
3081 disables limiting. Note that setting either no limit or\n\
3082 a very large limit can make completion slow."),
3083 NULL
, NULL
, &setlist
, &showlist
);