1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "expression.h"
23 #include "filenames.h" /* For DOSish file names. */
25 #include "gdb_signals.h"
27 #include "reggroups.h"
28 #include "user-regs.h"
29 #include "arch-utils.h"
32 #include "cli/cli-decode.h"
34 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
35 calling a hook instead so we eliminate the CLI dependency. */
38 /* Needed for rl_completer_word_break_characters() and for
39 rl_filename_completion_function. */
40 #include "readline/readline.h"
42 /* readline defines this. */
45 #include "completer.h"
47 /* An enumeration of the various things a user might
48 attempt to complete for a location. */
50 enum explicit_location_match_type
52 /* The filename of a source file. */
55 /* The name of a function or method. */
58 /* The name of a label. */
62 /* Prototypes for local functions. */
64 char *line_completion_function (const char *text
, int matches
,
68 /* readline uses the word breaks for two things:
69 (1) In figuring out where to point the TEXT parameter to the
70 rl_completion_entry_function. Since we don't use TEXT for much,
71 it doesn't matter a lot what the word breaks are for this purpose,
72 but it does affect how much stuff M-? lists.
73 (2) If one of the matches contains a word break character, readline
74 will quote it. That's why we switch between
75 current_language->la_word_break_characters() and
76 gdb_completer_command_word_break_characters. I'm not sure when
77 we need this behavior (perhaps for funky characters in C++
80 /* Variables which are necessary for fancy command line editing. */
82 /* When completing on command names, we remove '-' from the list of
83 word break characters, since we use it in command names. If the
84 readline library sees one in any of the current completion strings,
85 it thinks that the string needs to be quoted and automatically
86 supplies a leading quote. */
87 static const char gdb_completer_command_word_break_characters
[] =
88 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
90 /* When completing on file names, we remove from the list of word
91 break characters any characters that are commonly used in file
92 names, such as '-', '+', '~', etc. Otherwise, readline displays
93 incorrect completion candidates. */
94 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
95 programs support @foo style response files. */
96 static const char gdb_completer_file_name_break_characters
[] =
97 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
103 /* Characters that can be used to quote completion strings. Note that
104 we can't include '"' because the gdb C parser treats such quoted
105 sequences as strings. */
106 static const char gdb_completer_quote_characters
[] = "'";
108 /* Accessor for some completer data that may interest other files. */
111 get_gdb_completer_quote_characters (void)
113 return gdb_completer_quote_characters
;
116 /* Line completion interface function for readline. */
119 readline_line_completion_function (const char *text
, int matches
)
121 return line_completion_function (text
, matches
,
122 rl_line_buffer
, rl_point
);
125 /* This can be used for functions which don't want to complete on
126 symbols but don't want to complete on anything else either. */
128 noop_completer (struct cmd_list_element
*ignore
,
129 const char *text
, const char *prefix
)
134 /* Complete on filenames. */
137 filename_completer (struct cmd_list_element
*ignore
,
138 const char *text
, const char *word
)
141 VEC (char_ptr
) *return_val
= NULL
;
148 p
= rl_filename_completion_function (text
, subsequent_name
);
151 /* We need to set subsequent_name to a non-zero value before the
152 continue line below, because otherwise, if the first file
153 seen by GDB is a backup file whose name ends in a `~', we
154 will loop indefinitely. */
156 /* Like emacs, don't complete on old versions. Especially
157 useful in the "source" command. */
158 if (p
[strlen (p
) - 1] == '~')
165 /* Return exactly p. */
167 else if (word
> text
)
169 /* Return some portion of p. */
170 q
= (char *) xmalloc (strlen (p
) + 5);
171 strcpy (q
, p
+ (word
- text
));
176 /* Return some of TEXT plus p. */
177 q
= (char *) xmalloc (strlen (p
) + (text
- word
) + 5);
178 strncpy (q
, word
, text
- word
);
179 q
[text
- word
] = '\0';
183 VEC_safe_push (char_ptr
, return_val
, q
);
186 /* There is no way to do this just long enough to affect quote
187 inserting without also affecting the next completion. This
188 should be fixed in readline. FIXME. */
189 /* Ensure that readline does the right thing
190 with respect to inserting quotes. */
191 rl_completer_word_break_characters
= "";
196 /* The corresponding completer_handle_brkchars
200 filename_completer_handle_brkchars (struct cmd_list_element
*ignore
,
201 const char *text
, const char *word
)
203 set_rl_completer_word_break_characters
204 (gdb_completer_file_name_break_characters
);
207 /* Complete on linespecs, which might be of two possible forms:
213 This is intended to be used in commands that set breakpoints
216 static VEC (char_ptr
) *
217 linespec_location_completer (struct cmd_list_element
*ignore
,
218 const char *text
, const char *word
)
220 int n_syms
, n_files
, ix
;
221 VEC (char_ptr
) *fn_list
= NULL
;
222 VEC (char_ptr
) *list
= NULL
;
225 int quoted
= *text
== '\'' || *text
== '"';
226 int quote_char
= '\0';
227 const char *colon
= NULL
;
228 char *file_to_match
= NULL
;
229 const char *symbol_start
= text
;
230 const char *orig_text
= text
;
233 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
234 for (p
= text
; *p
!= '\0'; ++p
)
236 if (*p
== '\\' && p
[1] == '\'')
238 else if (*p
== '\'' || *p
== '"')
242 while (*p
!= '\0' && *p
!= quote_found
)
244 if (*p
== '\\' && p
[1] == quote_found
)
249 if (*p
== quote_found
)
252 break; /* Hit the end of text. */
254 #if HAVE_DOS_BASED_FILE_SYSTEM
255 /* If we have a DOS-style absolute file name at the beginning of
256 TEXT, and the colon after the drive letter is the only colon
257 we found, pretend the colon is not there. */
258 else if (p
< text
+ 3 && *p
== ':' && p
== text
+ 1 + quoted
)
261 else if (*p
== ':' && !colon
)
264 symbol_start
= p
+ 1;
266 else if (strchr (current_language
->la_word_break_characters(), *p
))
267 symbol_start
= p
+ 1;
272 text_len
= strlen (text
);
274 /* Where is the file name? */
279 file_to_match
= (char *) xmalloc (colon
- text
+ 1);
280 strncpy (file_to_match
, text
, colon
- text
);
281 file_to_match
[colon
- text
] = '\0';
282 /* Remove trailing colons and quotes from the file name. */
283 for (s
= file_to_match
+ (colon
- text
);
286 if (*s
== ':' || *s
== quote_char
)
289 /* If the text includes a colon, they want completion only on a
290 symbol name after the colon. Otherwise, we need to complete on
291 symbols as well as on files. */
294 list
= make_file_symbol_completion_list (symbol_start
, word
,
296 xfree (file_to_match
);
300 list
= make_symbol_completion_list (symbol_start
, word
);
301 /* If text includes characters which cannot appear in a file
302 name, they cannot be asking for completion on files. */
304 gdb_completer_file_name_break_characters
) == text_len
)
305 fn_list
= make_source_files_completion_list (text
, text
);
308 n_syms
= VEC_length (char_ptr
, list
);
309 n_files
= VEC_length (char_ptr
, fn_list
);
311 /* Catenate fn_list[] onto the end of list[]. */
314 VEC_free (char_ptr
, list
); /* Paranoia. */
322 for (ix
= 0; VEC_iterate (char_ptr
, fn_list
, ix
, fn
); ++ix
)
323 VEC_safe_push (char_ptr
, list
, fn
);
324 VEC_free (char_ptr
, fn_list
);
327 if (n_syms
&& n_files
)
335 /* If we only have file names as possible completion, we should
336 bring them in sync with what rl_complete expects. The
337 problem is that if the user types "break /foo/b TAB", and the
338 possible completions are "/foo/bar" and "/foo/baz"
339 rl_complete expects us to return "bar" and "baz", without the
340 leading directories, as possible completions, because `word'
341 starts at the "b". But we ignore the value of `word' when we
342 call make_source_files_completion_list above (because that
343 would not DTRT when the completion results in both symbols
344 and file names), so make_source_files_completion_list returns
345 the full "/foo/bar" and "/foo/baz" strings. This produces
346 wrong results when, e.g., there's only one possible
347 completion, because rl_complete will prepend "/foo/" to each
348 candidate completion. The loop below removes that leading
350 for (ix
= 0; VEC_iterate (char_ptr
, list
, ix
, fn
); ++ix
)
352 memmove (fn
, fn
+ (word
- text
),
353 strlen (fn
) + 1 - (word
- text
));
358 /* No completions at all. As the final resort, try completing
359 on the entire text as a symbol. */
360 list
= make_symbol_completion_list (orig_text
, word
);
366 /* A helper function to collect explicit location matches for the given
367 LOCATION, which is attempting to match on WORD. */
369 static VEC (char_ptr
) *
370 collect_explicit_location_matches (struct event_location
*location
,
371 enum explicit_location_match_type what
,
374 VEC (char_ptr
) *matches
= NULL
;
375 const struct explicit_location
*explicit_loc
376 = get_explicit_location (location
);
382 const char *text
= (explicit_loc
->source_filename
== NULL
383 ? "" : explicit_loc
->source_filename
);
385 matches
= make_source_files_completion_list (text
, word
);
391 const char *text
= (explicit_loc
->function_name
== NULL
392 ? "" : explicit_loc
->function_name
);
394 if (explicit_loc
->source_filename
!= NULL
)
396 const char *filename
= explicit_loc
->source_filename
;
398 matches
= make_file_symbol_completion_list (text
, word
, filename
);
401 matches
= make_symbol_completion_list (text
, word
);
410 gdb_assert_not_reached ("unhandled explicit_location_match_type");
416 /* A convenience macro to (safely) back up P to the previous word. */
419 backup_text_ptr (const char *p
, const char *text
)
421 while (p
> text
&& isspace (*p
))
423 for (; p
> text
&& !isspace (p
[-1]); --p
)
429 /* A completer function for explicit locations. This function
430 completes both options ("-source", "-line", etc) and values. */
432 static VEC (char_ptr
) *
433 explicit_location_completer (struct cmd_list_element
*ignore
,
434 struct event_location
*location
,
435 const char *text
, const char *word
)
438 VEC (char_ptr
) *matches
= NULL
;
440 /* Find the beginning of the word. This is necessary because
441 we need to know if we are completing an option name or value. We
442 don't get the leading '-' from the completer. */
443 p
= backup_text_ptr (word
, text
);
447 /* Completing on option name. */
448 static const char *const keywords
[] =
457 /* Skip over the '-'. */
460 return complete_on_enum (keywords
, p
, p
);
464 /* Completing on value (or unknown). Get the previous word to see what
465 the user is completing on. */
467 const char *new_word
, *end
;
468 enum explicit_location_match_type what
;
469 struct explicit_location
*explicit_loc
470 = get_explicit_location (location
);
472 /* Backup P to the previous word, which should be the option
473 the user is attempting to complete. */
476 p
= backup_text_ptr (p
, text
);
479 if (strncmp (p
, "-source", len
) == 0)
482 new_word
= explicit_loc
->source_filename
+ offset
;
484 else if (strncmp (p
, "-function", len
) == 0)
486 what
= MATCH_FUNCTION
;
487 new_word
= explicit_loc
->function_name
+ offset
;
489 else if (strncmp (p
, "-label", len
) == 0)
492 new_word
= explicit_loc
->label_name
+ offset
;
496 /* The user isn't completing on any valid option name,
497 e.g., "break -source foo.c [tab]". */
501 /* If the user hasn't entered a search expression, e.g.,
502 "break -function <TAB><TAB>", new_word will be NULL, but
503 search routines require non-NULL search words. */
504 if (new_word
== NULL
)
507 /* Now gather matches */
508 matches
= collect_explicit_location_matches (location
, what
, new_word
);
514 /* A completer for locations. */
517 location_completer (struct cmd_list_element
*ignore
,
518 const char *text
, const char *word
)
520 VEC (char_ptr
) *matches
= NULL
;
521 const char *copy
= text
;
523 event_location_up location
= string_to_explicit_location (©
,
526 if (location
!= NULL
)
527 matches
= explicit_location_completer (ignore
, location
.get (),
531 /* This is an address or linespec location.
532 Right now both of these are handled by the (old) linespec
534 matches
= linespec_location_completer (ignore
, text
, word
);
540 /* Helper for expression_completer which recursively adds field and
541 method names from TYPE, a struct or union type, to the array
544 add_struct_fields (struct type
*type
, VEC (char_ptr
) **output
,
545 char *fieldname
, int namelen
)
548 int computed_type_name
= 0;
549 const char *type_name
= NULL
;
551 type
= check_typedef (type
);
552 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
554 if (i
< TYPE_N_BASECLASSES (type
))
555 add_struct_fields (TYPE_BASECLASS (type
, i
),
556 output
, fieldname
, namelen
);
557 else if (TYPE_FIELD_NAME (type
, i
))
559 if (TYPE_FIELD_NAME (type
, i
)[0] != '\0')
561 if (! strncmp (TYPE_FIELD_NAME (type
, i
),
563 VEC_safe_push (char_ptr
, *output
,
564 xstrdup (TYPE_FIELD_NAME (type
, i
)));
566 else if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_UNION
)
568 /* Recurse into anonymous unions. */
569 add_struct_fields (TYPE_FIELD_TYPE (type
, i
),
570 output
, fieldname
, namelen
);
575 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
577 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
579 if (name
&& ! strncmp (name
, fieldname
, namelen
))
581 if (!computed_type_name
)
583 type_name
= type_name_no_tag (type
);
584 computed_type_name
= 1;
586 /* Omit constructors from the completion list. */
587 if (!type_name
|| strcmp (type_name
, name
))
588 VEC_safe_push (char_ptr
, *output
, xstrdup (name
));
593 /* Complete on expressions. Often this means completing on symbol
594 names, but some language parsers also have support for completing
597 expression_completer (struct cmd_list_element
*ignore
,
598 const char *text
, const char *word
)
600 struct type
*type
= NULL
;
602 enum type_code code
= TYPE_CODE_UNDEF
;
604 /* Perform a tentative parse of the expression, to see whether a
605 field completion is required. */
609 type
= parse_expression_for_completion (text
, &fieldname
, &code
);
611 CATCH (except
, RETURN_MASK_ERROR
)
617 if (fieldname
&& type
)
621 type
= check_typedef (type
);
622 if (TYPE_CODE (type
) != TYPE_CODE_PTR
&& !TYPE_IS_REFERENCE (type
))
624 type
= TYPE_TARGET_TYPE (type
);
627 if (TYPE_CODE (type
) == TYPE_CODE_UNION
628 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
630 int flen
= strlen (fieldname
);
631 VEC (char_ptr
) *result
= NULL
;
633 add_struct_fields (type
, &result
, fieldname
, flen
);
638 else if (fieldname
&& code
!= TYPE_CODE_UNDEF
)
640 VEC (char_ptr
) *result
;
641 struct cleanup
*cleanup
= make_cleanup (xfree
, fieldname
);
643 result
= make_symbol_completion_type (fieldname
, fieldname
, code
);
644 do_cleanups (cleanup
);
649 /* Not ideal but it is what we used to do before... */
650 return linespec_location_completer (ignore
, text
, word
);
653 /* See definition in completer.h. */
656 set_rl_completer_word_break_characters (const char *break_chars
)
658 rl_completer_word_break_characters
= (char *) break_chars
;
661 /* See definition in completer.h. */
664 set_gdb_completion_word_break_characters (completer_ftype
*fn
)
666 const char *break_chars
;
668 /* So far we are only interested in differentiating filename
669 completers from everything else. */
670 if (fn
== filename_completer
)
671 break_chars
= gdb_completer_file_name_break_characters
;
673 break_chars
= gdb_completer_command_word_break_characters
;
675 set_rl_completer_word_break_characters (break_chars
);
678 /* Complete on symbols. */
681 symbol_completer (struct cmd_list_element
*ignore
,
682 const char *text
, const char *word
)
684 return make_symbol_completion_list (text
, word
);
687 /* Here are some useful test cases for completion. FIXME: These
688 should be put in the test suite. They should be tested with both
691 "show output-" "radix"
692 "show output" "-radix"
693 "p" ambiguous (commands starting with p--path, print, printf, etc.)
694 "p " ambiguous (all symbols)
695 "info t foo" no completions
696 "info t " no completions
697 "info t" ambiguous ("info target", "info terminal", etc.)
698 "info ajksdlfk" no completions
699 "info ajksdlfk " no completions
701 "info " ambiguous (all info commands)
702 "p \"a" no completions (string constant)
703 "p 'a" ambiguous (all symbols starting with a)
704 "p b-a" ambiguous (all symbols starting with a)
705 "p b-" ambiguous (all symbols)
706 "file Make" "file" (word break hard to screw up here)
707 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
716 complete_line_internal_reason
;
718 /* Helper for complete_line_internal to simplify it. */
720 static VEC (char_ptr
) *
721 complete_line_internal_normal_command (const char *command
, const char *word
,
722 const char *cmd_args
,
723 complete_line_internal_reason reason
,
724 struct cmd_list_element
*c
)
726 const char *p
= cmd_args
;
728 if (c
->completer
== filename_completer
)
730 /* Many commands which want to complete on file names accept
731 several file names, as in "run foo bar >>baz". So we don't
732 want to complete the entire text after the command, just the
733 last word. To this end, we need to find the beginning of the
734 file name by starting at `word' and going backwards. */
737 && strchr (gdb_completer_file_name_break_characters
,
743 if (reason
== handle_brkchars
)
745 completer_handle_brkchars_ftype
*brkchars_fn
;
747 if (c
->completer_handle_brkchars
!= NULL
)
748 brkchars_fn
= c
->completer_handle_brkchars
;
752 = (completer_handle_brkchars_func_for_completer
756 brkchars_fn (c
, p
, word
);
759 if (reason
!= handle_brkchars
&& c
->completer
!= NULL
)
760 return (*c
->completer
) (c
, p
, word
);
764 /* Internal function used to handle completions.
767 TEXT is the caller's idea of the "word" we are looking at.
769 LINE_BUFFER is available to be looked at; it contains the entire
770 text of the line. POINT is the offset in that line of the cursor.
771 You should pretend that the line ends at POINT.
773 REASON is of type complete_line_internal_reason.
775 If REASON is handle_brkchars:
776 Preliminary phase, called by gdb_completion_word_break_characters
777 function, is used to determine the correct set of chars that are
778 word delimiters depending on the current command in line_buffer.
779 No completion list should be generated; the return value should be
780 NULL. This is checked by an assertion in that function.
782 If REASON is handle_completions:
783 Main phase, called by complete_line function, is used to get the list
784 of posible completions.
786 If REASON is handle_help:
787 Special case when completing a 'help' command. In this case,
788 once sub-command completions are exhausted, we simply return NULL.
791 static VEC (char_ptr
) *
792 complete_line_internal (const char *text
,
793 const char *line_buffer
, int point
,
794 complete_line_internal_reason reason
)
796 VEC (char_ptr
) *list
= NULL
;
799 int ignore_help_classes
;
800 /* Pointer within tmp_command which corresponds to text. */
802 struct cmd_list_element
*c
, *result_list
;
804 /* Choose the default set of word break characters to break
805 completions. If we later find out that we are doing completions
806 on command strings (as opposed to strings supplied by the
807 individual command completer functions, which can be any string)
808 then we will switch to the special word break set for command
809 strings, which leaves out the '-' character used in some
811 set_rl_completer_word_break_characters
812 (current_language
->la_word_break_characters());
814 /* Decide whether to complete on a list of gdb commands or on
816 tmp_command
= (char *) alloca (point
+ 1);
819 /* The help command should complete help aliases. */
820 ignore_help_classes
= reason
!= handle_help
;
822 strncpy (tmp_command
, line_buffer
, point
);
823 tmp_command
[point
] = '\0';
824 /* Since text always contains some number of characters leading up
825 to point, we can find the equivalent position in tmp_command
826 by subtracting that many characters from the end of tmp_command. */
827 word
= tmp_command
+ point
- strlen (text
);
831 /* An empty line we want to consider ambiguous; that is, it
832 could be any command. */
833 c
= CMD_LIST_AMBIGUOUS
;
838 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, ignore_help_classes
);
841 /* Move p up to the next interesting thing. */
842 while (*p
== ' ' || *p
== '\t')
849 /* It is an unrecognized command. So there are no
850 possible completions. */
853 else if (c
== CMD_LIST_AMBIGUOUS
)
857 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
858 doesn't advance over that thing itself. Do so now. */
860 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
862 if (q
!= tmp_command
+ point
)
864 /* There is something beyond the ambiguous
865 command, so there are no possible completions. For
866 example, "info t " or "info t foo" does not complete
867 to anything, because "info t" can be "info target" or
873 /* We're trying to complete on the command which was ambiguous.
874 This we can deal with. */
877 if (reason
!= handle_brkchars
)
878 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
879 word
, ignore_help_classes
);
883 if (reason
!= handle_brkchars
)
884 list
= complete_on_cmdlist (cmdlist
, p
, word
,
885 ignore_help_classes
);
887 /* Ensure that readline does the right thing with respect to
889 set_rl_completer_word_break_characters
890 (gdb_completer_command_word_break_characters
);
895 /* We've recognized a full command. */
897 if (p
== tmp_command
+ point
)
899 /* There is no non-whitespace in the line beyond the
902 if (p
[-1] == ' ' || p
[-1] == '\t')
904 /* The command is followed by whitespace; we need to
905 complete on whatever comes after command. */
908 /* It is a prefix command; what comes after it is
909 a subcommand (e.g. "info "). */
910 if (reason
!= handle_brkchars
)
911 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
,
912 ignore_help_classes
);
914 /* Ensure that readline does the right thing
915 with respect to inserting quotes. */
916 set_rl_completer_word_break_characters
917 (gdb_completer_command_word_break_characters
);
919 else if (reason
== handle_help
)
923 if (reason
!= handle_brkchars
)
924 list
= complete_on_enum (c
->enums
, p
, word
);
925 set_rl_completer_word_break_characters
926 (gdb_completer_command_word_break_characters
);
930 /* It is a normal command; what comes after it is
931 completed by the command's completer function. */
932 list
= complete_line_internal_normal_command (tmp_command
,
939 /* The command is not followed by whitespace; we need to
940 complete on the command itself, e.g. "p" which is a
941 command itself but also can complete to "print", "ptype"
945 /* Find the command we are completing on. */
947 while (q
> tmp_command
)
949 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
955 if (reason
!= handle_brkchars
)
956 list
= complete_on_cmdlist (result_list
, q
, word
,
957 ignore_help_classes
);
959 /* Ensure that readline does the right thing
960 with respect to inserting quotes. */
961 set_rl_completer_word_break_characters
962 (gdb_completer_command_word_break_characters
);
965 else if (reason
== handle_help
)
969 /* There is non-whitespace beyond the command. */
971 if (c
->prefixlist
&& !c
->allow_unknown
)
973 /* It is an unrecognized subcommand of a prefix command,
974 e.g. "info adsfkdj". */
979 if (reason
!= handle_brkchars
)
980 list
= complete_on_enum (c
->enums
, p
, word
);
984 /* It is a normal command. */
985 list
= complete_line_internal_normal_command (tmp_command
,
995 /* See completer.h. */
997 int max_completions
= 200;
999 /* See completer.h. */
1001 completion_tracker_t
1002 new_completion_tracker (void)
1004 if (max_completions
<= 0)
1007 return htab_create_alloc (max_completions
,
1008 htab_hash_string
, (htab_eq
) streq
,
1009 NULL
, xcalloc
, xfree
);
1012 /* Cleanup routine to free a completion tracker and reset the pointer
1016 free_completion_tracker (void *p
)
1018 completion_tracker_t
*tracker_ptr
= (completion_tracker_t
*) p
;
1020 htab_delete (*tracker_ptr
);
1021 *tracker_ptr
= NULL
;
1024 /* See completer.h. */
1027 make_cleanup_free_completion_tracker (completion_tracker_t
*tracker_ptr
)
1029 if (*tracker_ptr
== NULL
)
1030 return make_cleanup (null_cleanup
, NULL
);
1032 return make_cleanup (free_completion_tracker
, tracker_ptr
);
1035 /* See completer.h. */
1037 enum maybe_add_completion_enum
1038 maybe_add_completion (completion_tracker_t tracker
, char *name
)
1042 if (max_completions
< 0)
1043 return MAYBE_ADD_COMPLETION_OK
;
1044 if (max_completions
== 0)
1045 return MAYBE_ADD_COMPLETION_MAX_REACHED
;
1047 gdb_assert (tracker
!= NULL
);
1049 if (htab_elements (tracker
) >= max_completions
)
1050 return MAYBE_ADD_COMPLETION_MAX_REACHED
;
1052 slot
= htab_find_slot (tracker
, name
, INSERT
);
1054 if (*slot
!= HTAB_EMPTY_ENTRY
)
1055 return MAYBE_ADD_COMPLETION_DUPLICATE
;
1059 return (htab_elements (tracker
) < max_completions
1060 ? MAYBE_ADD_COMPLETION_OK
1061 : MAYBE_ADD_COMPLETION_OK_MAX_REACHED
);
1065 throw_max_completions_reached_error (void)
1067 throw_error (MAX_COMPLETIONS_REACHED_ERROR
, _("Max completions reached."));
1070 /* Generate completions all at once. Returns a vector of unique strings
1071 allocated with xmalloc. Returns NULL if there are no completions
1072 or if max_completions is 0. If max_completions is non-negative, this will
1073 return at most max_completions strings.
1075 TEXT is the caller's idea of the "word" we are looking at.
1077 LINE_BUFFER is available to be looked at; it contains the entire
1080 POINT is the offset in that line of the cursor. You
1081 should pretend that the line ends at POINT. */
1084 complete_line (const char *text
, const char *line_buffer
, int point
)
1086 VEC (char_ptr
) *list
;
1087 VEC (char_ptr
) *result
= NULL
;
1088 struct cleanup
*cleanups
;
1089 completion_tracker_t tracker
;
1091 int ix
, max_reached
;
1093 if (max_completions
== 0)
1095 list
= complete_line_internal (text
, line_buffer
, point
,
1096 handle_completions
);
1097 if (max_completions
< 0)
1100 tracker
= new_completion_tracker ();
1101 cleanups
= make_cleanup_free_completion_tracker (&tracker
);
1102 make_cleanup_free_char_ptr_vec (list
);
1104 /* Do a final test for too many completions. Individual completers may
1105 do some of this, but are not required to. Duplicates are also removed
1106 here. Otherwise the user is left scratching his/her head: readline and
1107 complete_command will remove duplicates, and if removal of duplicates
1108 there brings the total under max_completions the user may think gdb quit
1109 searching too early. */
1111 for (ix
= 0, max_reached
= 0;
1112 !max_reached
&& VEC_iterate (char_ptr
, list
, ix
, candidate
);
1115 enum maybe_add_completion_enum add_status
;
1117 add_status
= maybe_add_completion (tracker
, candidate
);
1121 case MAYBE_ADD_COMPLETION_OK
:
1122 VEC_safe_push (char_ptr
, result
, xstrdup (candidate
));
1124 case MAYBE_ADD_COMPLETION_OK_MAX_REACHED
:
1125 VEC_safe_push (char_ptr
, result
, xstrdup (candidate
));
1128 case MAYBE_ADD_COMPLETION_MAX_REACHED
:
1129 gdb_assert_not_reached ("more than max completions reached");
1130 case MAYBE_ADD_COMPLETION_DUPLICATE
:
1135 do_cleanups (cleanups
);
1140 /* Complete on command names. Used by "help". */
1143 command_completer (struct cmd_list_element
*ignore
,
1144 const char *text
, const char *word
)
1146 return complete_line_internal (word
, text
,
1147 strlen (text
), handle_help
);
1150 /* The corresponding completer_handle_brkchars implementation. */
1153 command_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1154 const char *text
, const char *word
)
1156 set_rl_completer_word_break_characters
1157 (gdb_completer_command_word_break_characters
);
1160 /* Complete on signals. */
1163 signal_completer (struct cmd_list_element
*ignore
,
1164 const char *text
, const char *word
)
1166 VEC (char_ptr
) *return_val
= NULL
;
1167 size_t len
= strlen (word
);
1169 const char *signame
;
1171 for (signum
= GDB_SIGNAL_FIRST
; signum
!= GDB_SIGNAL_LAST
; ++signum
)
1173 /* Can't handle this, so skip it. */
1174 if (signum
== GDB_SIGNAL_0
)
1177 signame
= gdb_signal_to_name ((enum gdb_signal
) signum
);
1179 /* Ignore the unknown signal case. */
1180 if (!signame
|| strcmp (signame
, "?") == 0)
1183 if (strncasecmp (signame
, word
, len
) == 0)
1184 VEC_safe_push (char_ptr
, return_val
, xstrdup (signame
));
1190 /* Bit-flags for selecting what the register and/or register-group
1191 completer should complete on. */
1193 enum reg_completer_target
1195 complete_register_names
= 0x1,
1196 complete_reggroup_names
= 0x2
1198 DEF_ENUM_FLAGS_TYPE (enum reg_completer_target
, reg_completer_targets
);
1200 /* Complete register names and/or reggroup names based on the value passed
1201 in TARGETS. At least one bit in TARGETS must be set. */
1203 static VEC (char_ptr
) *
1204 reg_or_group_completer_1 (struct cmd_list_element
*ignore
,
1205 const char *text
, const char *word
,
1206 reg_completer_targets targets
)
1208 VEC (char_ptr
) *result
= NULL
;
1209 size_t len
= strlen (word
);
1210 struct gdbarch
*gdbarch
;
1213 gdb_assert ((targets
& (complete_register_names
1214 | complete_reggroup_names
)) != 0);
1215 gdbarch
= get_current_arch ();
1217 if ((targets
& complete_register_names
) != 0)
1222 (name
= user_reg_map_regnum_to_name (gdbarch
, i
)) != NULL
;
1225 if (*name
!= '\0' && strncmp (word
, name
, len
) == 0)
1226 VEC_safe_push (char_ptr
, result
, xstrdup (name
));
1230 if ((targets
& complete_reggroup_names
) != 0)
1232 struct reggroup
*group
;
1234 for (group
= reggroup_next (gdbarch
, NULL
);
1236 group
= reggroup_next (gdbarch
, group
))
1238 name
= reggroup_name (group
);
1239 if (strncmp (word
, name
, len
) == 0)
1240 VEC_safe_push (char_ptr
, result
, xstrdup (name
));
1247 /* Perform completion on register and reggroup names. */
1250 reg_or_group_completer (struct cmd_list_element
*ignore
,
1251 const char *text
, const char *word
)
1253 return reg_or_group_completer_1 (ignore
, text
, word
,
1254 (complete_register_names
1255 | complete_reggroup_names
));
1258 /* Perform completion on reggroup names. */
1261 reggroup_completer (struct cmd_list_element
*ignore
,
1262 const char *text
, const char *word
)
1264 return reg_or_group_completer_1 (ignore
, text
, word
,
1265 complete_reggroup_names
);
1268 /* The default completer_handle_brkchars implementation. */
1271 default_completer_handle_brkchars (struct cmd_list_element
*ignore
,
1272 const char *text
, const char *word
)
1274 set_rl_completer_word_break_characters
1275 (current_language
->la_word_break_characters ());
1278 /* See definition in completer.h. */
1280 completer_handle_brkchars_ftype
*
1281 completer_handle_brkchars_func_for_completer (completer_ftype
*fn
)
1283 if (fn
== filename_completer
)
1284 return filename_completer_handle_brkchars
;
1286 if (fn
== command_completer
)
1287 return command_completer_handle_brkchars
;
1289 return default_completer_handle_brkchars
;
1292 /* Get the list of chars that are considered as word breaks
1293 for the current command. */
1296 gdb_completion_word_break_characters (void)
1298 VEC (char_ptr
) *list
;
1300 list
= complete_line_internal (rl_line_buffer
, rl_line_buffer
, rl_point
,
1302 gdb_assert (list
== NULL
);
1303 return rl_completer_word_break_characters
;
1306 /* Generate completions one by one for the completer. Each time we
1307 are called return another potential completion to the caller.
1308 line_completion just completes on commands or passes the buck to
1309 the command's completer function, the stuff specific to symbol
1310 completion is in make_symbol_completion_list.
1312 TEXT is the caller's idea of the "word" we are looking at.
1314 MATCHES is the number of matches that have currently been collected
1315 from calling this completion function. When zero, then we need to
1316 initialize, otherwise the initialization has already taken place
1317 and we can just return the next potential completion string.
1319 LINE_BUFFER is available to be looked at; it contains the entire
1320 text of the line. POINT is the offset in that line of the cursor.
1321 You should pretend that the line ends at POINT.
1323 Returns NULL if there are no more completions, else a pointer to a
1324 string which is a possible completion, it is the caller's
1325 responsibility to free the string. */
1328 line_completion_function (const char *text
, int matches
,
1329 char *line_buffer
, int point
)
1331 static VEC (char_ptr
) *list
= NULL
; /* Cache of completions. */
1332 static int index
; /* Next cached completion. */
1333 char *output
= NULL
;
1337 /* The caller is beginning to accumulate a new set of
1338 completions, so we need to find all of them now, and cache
1339 them for returning one at a time on future calls. */
1343 /* Free the storage used by LIST, but not by the strings
1344 inside. This is because rl_complete_internal () frees
1345 the strings. As complete_line may abort by calling
1346 `error' clear LIST now. */
1347 VEC_free (char_ptr
, list
);
1350 list
= complete_line (text
, line_buffer
, point
);
1353 /* If we found a list of potential completions during initialization
1354 then dole them out one at a time. After returning the last one,
1355 return NULL (and continue to do so) each time we are called after
1356 that, until a new list is available. */
1360 if (index
< VEC_length (char_ptr
, list
))
1362 output
= VEC_index (char_ptr
, list
, index
);
1368 /* Can't do this because readline hasn't yet checked the word breaks
1369 for figuring out whether to insert a quote. */
1371 /* Make sure the word break characters are set back to normal for
1372 the next time that readline tries to complete something. */
1373 rl_completer_word_break_characters
=
1374 current_language
->la_word_break_characters();
1380 /* Skip over the possibly quoted word STR (as defined by the quote
1381 characters QUOTECHARS and the word break characters BREAKCHARS).
1382 Returns pointer to the location after the "word". If either
1383 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
1387 skip_quoted_chars (const char *str
, const char *quotechars
,
1388 const char *breakchars
)
1390 char quote_char
= '\0';
1393 if (quotechars
== NULL
)
1394 quotechars
= gdb_completer_quote_characters
;
1396 if (breakchars
== NULL
)
1397 breakchars
= current_language
->la_word_break_characters();
1399 for (scan
= str
; *scan
!= '\0'; scan
++)
1401 if (quote_char
!= '\0')
1403 /* Ignore everything until the matching close quote char. */
1404 if (*scan
== quote_char
)
1406 /* Found matching close quote. */
1411 else if (strchr (quotechars
, *scan
))
1413 /* Found start of a quoted string. */
1416 else if (strchr (breakchars
, *scan
))
1425 /* Skip over the possibly quoted word STR (as defined by the quote
1426 characters and word break characters used by the completer).
1427 Returns pointer to the location after the "word". */
1430 skip_quoted (const char *str
)
1432 return skip_quoted_chars (str
, NULL
, NULL
);
1435 /* Return a message indicating that the maximum number of completions
1436 has been reached and that there may be more. */
1439 get_max_completions_reached_message (void)
1441 return _("*** List may be truncated, max-completions reached. ***");
1444 /* GDB replacement for rl_display_match_list.
1445 Readline doesn't provide a clean interface for TUI(curses).
1446 A hack previously used was to send readline's rl_outstream through a pipe
1447 and read it from the event loop. Bleah. IWBN if readline abstracted
1448 away all the necessary bits, and this is what this code does. It
1449 replicates the parts of readline we need and then adds an abstraction
1450 layer, currently implemented as struct match_list_displayer, so that both
1451 CLI and TUI can use it. We copy all this readline code to minimize
1452 GDB-specific mods to readline. Once this code performs as desired then
1453 we can submit it to the readline maintainers.
1455 N.B. A lot of the code is the way it is in order to minimize differences
1456 from readline's copy. */
1458 /* Not supported here. */
1459 #undef VISIBLE_STATS
1461 #if defined (HANDLE_MULTIBYTE)
1462 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
1463 #define MB_NULLWCH(x) ((x) == 0)
1466 #define ELLIPSIS_LEN 3
1468 /* gdb version of readline/complete.c:get_y_or_n.
1469 'y' -> returns 1, and 'n' -> returns 0.
1470 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
1471 If FOR_PAGER is non-zero, then also supported are:
1472 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
1475 gdb_get_y_or_n (int for_pager
, const struct match_list_displayer
*displayer
)
1481 RL_SETSTATE (RL_STATE_MOREINPUT
);
1482 c
= displayer
->read_key (displayer
);
1483 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
1485 if (c
== 'y' || c
== 'Y' || c
== ' ')
1487 if (c
== 'n' || c
== 'N' || c
== RUBOUT
)
1489 if (c
== ABORT_CHAR
|| c
< 0)
1491 /* Readline doesn't erase_entire_line here, but without it the
1492 --More-- prompt isn't erased and neither is the text entered
1493 thus far redisplayed. */
1494 displayer
->erase_entire_line (displayer
);
1495 /* Note: The arguments to rl_abort are ignored. */
1498 if (for_pager
&& (c
== NEWLINE
|| c
== RETURN
))
1500 if (for_pager
&& (c
== 'q' || c
== 'Q'))
1502 displayer
->beep (displayer
);
1506 /* Pager function for tab-completion.
1507 This is based on readline/complete.c:_rl_internal_pager.
1508 LINES is the number of lines of output displayed thus far.
1510 -1 -> user pressed 'n' or equivalent,
1511 0 -> user pressed 'y' or equivalent,
1512 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
1515 gdb_display_match_list_pager (int lines
,
1516 const struct match_list_displayer
*displayer
)
1520 displayer
->puts (displayer
, "--More--");
1521 displayer
->flush (displayer
);
1522 i
= gdb_get_y_or_n (1, displayer
);
1523 displayer
->erase_entire_line (displayer
);
1532 /* Return non-zero if FILENAME is a directory.
1533 Based on readline/complete.c:path_isdir. */
1536 gdb_path_isdir (const char *filename
)
1540 return (stat (filename
, &finfo
) == 0 && S_ISDIR (finfo
.st_mode
));
1543 /* Return the portion of PATHNAME that should be output when listing
1544 possible completions. If we are hacking filename completion, we
1545 are only interested in the basename, the portion following the
1546 final slash. Otherwise, we return what we were passed. Since
1547 printing empty strings is not very informative, if we're doing
1548 filename completion, and the basename is the empty string, we look
1549 for the previous slash and return the portion following that. If
1550 there's no previous slash, we just return what we were passed.
1552 Based on readline/complete.c:printable_part. */
1555 gdb_printable_part (char *pathname
)
1559 if (rl_filename_completion_desired
== 0) /* don't need to do anything */
1562 temp
= strrchr (pathname
, '/');
1563 #if defined (__MSDOS__)
1564 if (temp
== 0 && ISALPHA ((unsigned char)pathname
[0]) && pathname
[1] == ':')
1565 temp
= pathname
+ 1;
1568 if (temp
== 0 || *temp
== '\0')
1570 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
1571 Look for a previous slash and, if one is found, return the portion
1572 following that slash. If there's no previous slash, just return the
1573 pathname we were passed. */
1574 else if (temp
[1] == '\0')
1576 for (x
= temp
- 1; x
> pathname
; x
--)
1579 return ((*x
== '/') ? x
+ 1 : pathname
);
1585 /* Compute width of STRING when displayed on screen by print_filename.
1586 Based on readline/complete.c:fnwidth. */
1589 gdb_fnwidth (const char *string
)
1592 #if defined (HANDLE_MULTIBYTE)
1598 left
= strlen (string
) + 1;
1599 memset (&ps
, 0, sizeof (mbstate_t));
1605 if (CTRL_CHAR (string
[pos
]) || string
[pos
] == RUBOUT
)
1612 #if defined (HANDLE_MULTIBYTE)
1613 clen
= mbrtowc (&wc
, string
+ pos
, left
- pos
, &ps
);
1614 if (MB_INVALIDCH (clen
))
1618 memset (&ps
, 0, sizeof (mbstate_t));
1620 else if (MB_NULLWCH (clen
))
1626 width
+= (w
>= 0) ? w
: 1;
1638 /* Print TO_PRINT, one matching completion.
1639 PREFIX_BYTES is number of common prefix bytes.
1640 Based on readline/complete.c:fnprint. */
1643 gdb_fnprint (const char *to_print
, int prefix_bytes
,
1644 const struct match_list_displayer
*displayer
)
1648 #if defined (HANDLE_MULTIBYTE)
1655 end
= to_print
+ strlen (to_print
) + 1;
1656 memset (&ps
, 0, sizeof (mbstate_t));
1661 /* Don't print only the ellipsis if the common prefix is one of the
1662 possible completions */
1663 if (to_print
[prefix_bytes
] == '\0')
1670 ellipsis
= (to_print
[prefix_bytes
] == '.') ? '_' : '.';
1671 for (w
= 0; w
< ELLIPSIS_LEN
; w
++)
1672 displayer
->putch (displayer
, ellipsis
);
1673 printed_len
= ELLIPSIS_LEN
;
1676 s
= to_print
+ prefix_bytes
;
1681 displayer
->putch (displayer
, '^');
1682 displayer
->putch (displayer
, UNCTRL (*s
));
1685 #if defined (HANDLE_MULTIBYTE)
1686 memset (&ps
, 0, sizeof (mbstate_t));
1689 else if (*s
== RUBOUT
)
1691 displayer
->putch (displayer
, '^');
1692 displayer
->putch (displayer
, '?');
1695 #if defined (HANDLE_MULTIBYTE)
1696 memset (&ps
, 0, sizeof (mbstate_t));
1701 #if defined (HANDLE_MULTIBYTE)
1702 tlen
= mbrtowc (&wc
, s
, end
- s
, &ps
);
1703 if (MB_INVALIDCH (tlen
))
1707 memset (&ps
, 0, sizeof (mbstate_t));
1709 else if (MB_NULLWCH (tlen
))
1714 width
= (w
>= 0) ? w
: 1;
1716 for (w
= 0; w
< tlen
; ++w
)
1717 displayer
->putch (displayer
, s
[w
]);
1719 printed_len
+= width
;
1721 displayer
->putch (displayer
, *s
);
1731 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
1732 are using it, check for and output a single character for `special'
1733 filenames. Return the number of characters we output.
1734 Based on readline/complete.c:print_filename. */
1737 gdb_print_filename (char *to_print
, char *full_pathname
, int prefix_bytes
,
1738 const struct match_list_displayer
*displayer
)
1740 int printed_len
, extension_char
, slen
, tlen
;
1741 char *s
, c
, *new_full_pathname
;
1743 extern int _rl_complete_mark_directories
;
1746 printed_len
= gdb_fnprint (to_print
, prefix_bytes
, displayer
);
1748 #if defined (VISIBLE_STATS)
1749 if (rl_filename_completion_desired
&& (rl_visible_stats
|| _rl_complete_mark_directories
))
1751 if (rl_filename_completion_desired
&& _rl_complete_mark_directories
)
1754 /* If to_print != full_pathname, to_print is the basename of the
1755 path passed. In this case, we try to expand the directory
1756 name before checking for the stat character. */
1757 if (to_print
!= full_pathname
)
1759 /* Terminate the directory name. */
1761 to_print
[-1] = '\0';
1763 /* If setting the last slash in full_pathname to a NUL results in
1764 full_pathname being the empty string, we are trying to complete
1765 files in the root directory. If we pass a null string to the
1766 bash directory completion hook, for example, it will expand it
1767 to the current directory. We just want the `/'. */
1768 if (full_pathname
== 0 || *full_pathname
== 0)
1770 else if (full_pathname
[0] != '/')
1772 else if (full_pathname
[1] == 0)
1773 dn
= "//"; /* restore trailing slash to `//' */
1774 else if (full_pathname
[1] == '/' && full_pathname
[2] == 0)
1775 dn
= "/"; /* don't turn /// into // */
1778 s
= tilde_expand (dn
);
1779 if (rl_directory_completion_hook
)
1780 (*rl_directory_completion_hook
) (&s
);
1783 tlen
= strlen (to_print
);
1784 new_full_pathname
= (char *)xmalloc (slen
+ tlen
+ 2);
1785 strcpy (new_full_pathname
, s
);
1786 if (s
[slen
- 1] == '/')
1789 new_full_pathname
[slen
] = '/';
1790 new_full_pathname
[slen
] = '/';
1791 strcpy (new_full_pathname
+ slen
+ 1, to_print
);
1793 #if defined (VISIBLE_STATS)
1794 if (rl_visible_stats
)
1795 extension_char
= stat_char (new_full_pathname
);
1798 if (gdb_path_isdir (new_full_pathname
))
1799 extension_char
= '/';
1801 xfree (new_full_pathname
);
1806 s
= tilde_expand (full_pathname
);
1807 #if defined (VISIBLE_STATS)
1808 if (rl_visible_stats
)
1809 extension_char
= stat_char (s
);
1812 if (gdb_path_isdir (s
))
1813 extension_char
= '/';
1819 displayer
->putch (displayer
, extension_char
);
1827 /* GDB version of readline/complete.c:complete_get_screenwidth. */
1830 gdb_complete_get_screenwidth (const struct match_list_displayer
*displayer
)
1832 /* Readline has other stuff here which it's not clear we need. */
1833 return displayer
->width
;
1836 extern int _rl_completion_prefix_display_length
;
1837 extern int _rl_print_completions_horizontally
;
1839 EXTERN_C
int _rl_qsort_string_compare (const void *, const void *);
1840 typedef int QSFUNC (const void *, const void *);
1842 /* GDB version of readline/complete.c:rl_display_match_list.
1843 See gdb_display_match_list for a description of MATCHES, LEN, MAX.
1844 Returns non-zero if all matches are displayed. */
1847 gdb_display_match_list_1 (char **matches
, int len
, int max
,
1848 const struct match_list_displayer
*displayer
)
1850 int count
, limit
, printed_len
, lines
, cols
;
1851 int i
, j
, k
, l
, common_length
, sind
;
1853 int page_completions
= displayer
->height
!= INT_MAX
&& pagination_enabled
;
1855 /* Find the length of the prefix common to all items: length as displayed
1856 characters (common_length) and as a byte index into the matches (sind) */
1857 common_length
= sind
= 0;
1858 if (_rl_completion_prefix_display_length
> 0)
1860 t
= gdb_printable_part (matches
[0]);
1861 temp
= strrchr (t
, '/');
1862 common_length
= temp
? gdb_fnwidth (temp
) : gdb_fnwidth (t
);
1863 sind
= temp
? strlen (temp
) : strlen (t
);
1865 if (common_length
> _rl_completion_prefix_display_length
&& common_length
> ELLIPSIS_LEN
)
1866 max
-= common_length
- ELLIPSIS_LEN
;
1868 common_length
= sind
= 0;
1871 /* How many items of MAX length can we fit in the screen window? */
1872 cols
= gdb_complete_get_screenwidth (displayer
);
1875 if (limit
!= 1 && (limit
* max
== cols
))
1878 /* If cols == 0, limit will end up -1 */
1879 if (cols
< displayer
->width
&& limit
< 0)
1882 /* Avoid a possible floating exception. If max > cols,
1883 limit will be 0 and a divide-by-zero fault will result. */
1887 /* How many iterations of the printing loop? */
1888 count
= (len
+ (limit
- 1)) / limit
;
1890 /* Watch out for special case. If LEN is less than LIMIT, then
1891 just do the inner printing loop.
1892 0 < len <= limit implies count = 1. */
1894 /* Sort the items if they are not already sorted. */
1895 if (rl_ignore_completion_duplicates
== 0 && rl_sort_completion_matches
)
1896 qsort (matches
+ 1, len
, sizeof (char *), (QSFUNC
*)_rl_qsort_string_compare
);
1898 displayer
->crlf (displayer
);
1901 if (_rl_print_completions_horizontally
== 0)
1903 /* Print the sorted items, up-and-down alphabetically, like ls. */
1904 for (i
= 1; i
<= count
; i
++)
1906 for (j
= 0, l
= i
; j
< limit
; j
++)
1908 if (l
> len
|| matches
[l
] == 0)
1912 temp
= gdb_printable_part (matches
[l
]);
1913 printed_len
= gdb_print_filename (temp
, matches
[l
], sind
,
1917 for (k
= 0; k
< max
- printed_len
; k
++)
1918 displayer
->putch (displayer
, ' ');
1922 displayer
->crlf (displayer
);
1924 if (page_completions
&& lines
>= (displayer
->height
- 1) && i
< count
)
1926 lines
= gdb_display_match_list_pager (lines
, displayer
);
1934 /* Print the sorted items, across alphabetically, like ls -x. */
1935 for (i
= 1; matches
[i
]; i
++)
1937 temp
= gdb_printable_part (matches
[i
]);
1938 printed_len
= gdb_print_filename (temp
, matches
[i
], sind
, displayer
);
1939 /* Have we reached the end of this line? */
1942 if (i
&& (limit
> 1) && (i
% limit
) == 0)
1944 displayer
->crlf (displayer
);
1946 if (page_completions
&& lines
>= displayer
->height
- 1)
1948 lines
= gdb_display_match_list_pager (lines
, displayer
);
1954 for (k
= 0; k
< max
- printed_len
; k
++)
1955 displayer
->putch (displayer
, ' ');
1958 displayer
->crlf (displayer
);
1964 /* Utility for displaying completion list matches, used by both CLI and TUI.
1966 MATCHES is the list of strings, in argv format, LEN is the number of
1967 strings in MATCHES, and MAX is the length of the longest string in
1971 gdb_display_match_list (char **matches
, int len
, int max
,
1972 const struct match_list_displayer
*displayer
)
1974 /* Readline will never call this if complete_line returned NULL. */
1975 gdb_assert (max_completions
!= 0);
1977 /* complete_line will never return more than this. */
1978 if (max_completions
> 0)
1979 gdb_assert (len
<= max_completions
);
1981 if (rl_completion_query_items
> 0 && len
>= rl_completion_query_items
)
1985 /* We can't use *query here because they wait for <RET> which is
1986 wrong here. This follows the readline version as closely as possible
1987 for compatibility's sake. See readline/complete.c. */
1989 displayer
->crlf (displayer
);
1991 xsnprintf (msg
, sizeof (msg
),
1992 "Display all %d possibilities? (y or n)", len
);
1993 displayer
->puts (displayer
, msg
);
1994 displayer
->flush (displayer
);
1996 if (gdb_get_y_or_n (0, displayer
) == 0)
1998 displayer
->crlf (displayer
);
2003 if (gdb_display_match_list_1 (matches
, len
, max
, displayer
))
2005 /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
2006 if (len
== max_completions
)
2008 /* The maximum number of completions has been reached. Warn the user
2009 that there may be more. */
2010 const char *message
= get_max_completions_reached_message ();
2012 displayer
->puts (displayer
, message
);
2013 displayer
->crlf (displayer
);
2018 extern initialize_file_ftype _initialize_completer
; /* -Wmissing-prototypes */
2021 _initialize_completer (void)
2023 add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class
,
2024 &max_completions
, _("\
2025 Set maximum number of completion candidates."), _("\
2026 Show maximum number of completion candidates."), _("\
2027 Use this to limit the number of candidates considered\n\
2028 during completion. Specifying \"unlimited\" or -1\n\
2029 disables limiting. Note that setting either no limit or\n\
2030 a very large limit can make completion slow."),
2031 NULL
, NULL
, &setlist
, &showlist
);