Make "break foo" find "A::foo", A::B::foo", etc. [C++ and wild matching]
[deliverable/binutils-gdb.git] / gdb / completer.c
CommitLineData
c5f0f3d0 1/* Line completion stuff for GDB, the GNU debugger.
61baf725 2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
c5f0f3d0
FN
3
4 This file is part of GDB.
5
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c5f0f3d0
FN
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c5f0f3d0
FN
18
19#include "defs.h"
20#include "symtab.h"
21#include "gdbtypes.h"
22#include "expression.h"
9c3f90bd 23#include "filenames.h" /* For DOSish file names. */
51065942 24#include "language.h"
de0bea00 25#include "gdb_signals.h"
71c24708
AA
26#include "target.h"
27#include "reggroups.h"
28#include "user-regs.h"
51f0e40d 29#include "arch-utils.h"
87f0e720 30#include "location.h"
eb3ff9a5 31#include <algorithm>
c6756f62 32#include "linespec.h"
18a642a1
AC
33#include "cli/cli-decode.h"
34
03717487
MS
35/* FIXME: This is needed because of lookup_cmd_1 (). We should be
36 calling a hook instead so we eliminate the CLI dependency. */
c5f0f3d0
FN
37#include "gdbcmd.h"
38
c94fdfd0 39/* Needed for rl_completer_word_break_characters() and for
38017ce8 40 rl_filename_completion_function. */
dbda9972 41#include "readline/readline.h"
c5f0f3d0
FN
42
43/* readline defines this. */
44#undef savestring
45
46#include "completer.h"
47
eb3ff9a5
PA
48/* Misc state that needs to be tracked across several different
49 readline completer entry point calls, all related to a single
50 completion invocation. */
51
52struct gdb_completer_state
53{
54 /* The current completion's completion tracker. This is a global
55 because a tracker can be shared between the handle_brkchars and
56 handle_completion phases, which involves different readline
57 callbacks. */
58 completion_tracker *tracker = NULL;
59
60 /* Whether the current completion was aborted. */
61 bool aborted = false;
62};
63
64/* The current completion state. */
65static gdb_completer_state current_completion;
66
c6756f62
PA
67/* An enumeration of the various things a user might attempt to
68 complete for a location. If you change this, remember to update
69 the explicit_options array below too. */
87f0e720
KS
70
71enum explicit_location_match_type
72{
73 /* The filename of a source file. */
74 MATCH_SOURCE,
75
76 /* The name of a function or method. */
77 MATCH_FUNCTION,
78
a20714ff
PA
79 /* The fully-qualified name of a function or method. */
80 MATCH_QUALIFIED,
81
c6756f62
PA
82 /* A line number. */
83 MATCH_LINE,
84
87f0e720
KS
85 /* The name of a label. */
86 MATCH_LABEL
87};
88
9c3f90bd 89/* Prototypes for local functions. */
c5f0f3d0
FN
90
91/* readline uses the word breaks for two things:
92 (1) In figuring out where to point the TEXT parameter to the
93 rl_completion_entry_function. Since we don't use TEXT for much,
aff410f1
MS
94 it doesn't matter a lot what the word breaks are for this purpose,
95 but it does affect how much stuff M-? lists.
c5f0f3d0
FN
96 (2) If one of the matches contains a word break character, readline
97 will quote it. That's why we switch between
51065942 98 current_language->la_word_break_characters() and
c5f0f3d0 99 gdb_completer_command_word_break_characters. I'm not sure when
aff410f1
MS
100 we need this behavior (perhaps for funky characters in C++
101 symbols?). */
c5f0f3d0
FN
102
103/* Variables which are necessary for fancy command line editing. */
c5f0f3d0
FN
104
105/* When completing on command names, we remove '-' from the list of
106 word break characters, since we use it in command names. If the
107 readline library sees one in any of the current completion strings,
aff410f1
MS
108 it thinks that the string needs to be quoted and automatically
109 supplies a leading quote. */
67cb5b2d 110static const char gdb_completer_command_word_break_characters[] =
c5f0f3d0
FN
111" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
112
113/* When completing on file names, we remove from the list of word
114 break characters any characters that are commonly used in file
115 names, such as '-', '+', '~', etc. Otherwise, readline displays
116 incorrect completion candidates. */
7830cf6f
EZ
117/* MS-DOS and MS-Windows use colon as part of the drive spec, and most
118 programs support @foo style response files. */
67cb5b2d
PA
119static const char gdb_completer_file_name_break_characters[] =
120#ifdef HAVE_DOS_BASED_FILE_SYSTEM
121 " \t\n*|\"';?><@";
7830cf6f 122#else
67cb5b2d 123 " \t\n*|\"';:?><";
7830cf6f 124#endif
c5f0f3d0 125
aff410f1
MS
126/* Characters that can be used to quote completion strings. Note that
127 we can't include '"' because the gdb C parser treats such quoted
128 sequences as strings. */
67cb5b2d 129static const char gdb_completer_quote_characters[] = "'";
c5f0f3d0 130\f
9c3f90bd 131/* Accessor for some completer data that may interest other files. */
c5f0f3d0 132
67cb5b2d 133const char *
c5f0f3d0
FN
134get_gdb_completer_quote_characters (void)
135{
136 return gdb_completer_quote_characters;
137}
138
aff410f1
MS
139/* This can be used for functions which don't want to complete on
140 symbols but don't want to complete on anything else either. */
eb3ff9a5
PA
141
142void
aff410f1 143noop_completer (struct cmd_list_element *ignore,
eb3ff9a5 144 completion_tracker &tracker,
6f937416 145 const char *text, const char *prefix)
d75b5104 146{
d75b5104
EZ
147}
148
c5f0f3d0 149/* Complete on filenames. */
6e1dbf8c 150
eb3ff9a5
PA
151void
152filename_completer (struct cmd_list_element *ignore,
153 completion_tracker &tracker,
6f937416 154 const char *text, const char *word)
c5f0f3d0 155{
c5f0f3d0 156 int subsequent_name;
49c4e619 157 VEC (char_ptr) *return_val = NULL;
c5f0f3d0
FN
158
159 subsequent_name = 0;
160 while (1)
161 {
1e8189fb 162 char *p, *q;
c5504eaf 163
38017ce8 164 p = rl_filename_completion_function (text, subsequent_name);
c5f0f3d0 165 if (p == NULL)
49c4e619 166 break;
c5f0f3d0 167 /* We need to set subsequent_name to a non-zero value before the
aff410f1
MS
168 continue line below, because otherwise, if the first file
169 seen by GDB is a backup file whose name ends in a `~', we
170 will loop indefinitely. */
c5f0f3d0 171 subsequent_name = 1;
aff410f1
MS
172 /* Like emacs, don't complete on old versions. Especially
173 useful in the "source" command. */
c5f0f3d0 174 if (p[strlen (p) - 1] == '~')
1e8189fb
MS
175 {
176 xfree (p);
177 continue;
178 }
c5f0f3d0 179
1e8189fb
MS
180 if (word == text)
181 /* Return exactly p. */
49c4e619 182 q = p;
1e8189fb
MS
183 else if (word > text)
184 {
185 /* Return some portion of p. */
224c3ddb 186 q = (char *) xmalloc (strlen (p) + 5);
1e8189fb 187 strcpy (q, p + (word - text));
1e8189fb
MS
188 xfree (p);
189 }
190 else
191 {
192 /* Return some of TEXT plus p. */
224c3ddb 193 q = (char *) xmalloc (strlen (p) + (text - word) + 5);
1e8189fb
MS
194 strncpy (q, word, text - word);
195 q[text - word] = '\0';
196 strcat (q, p);
1e8189fb
MS
197 xfree (p);
198 }
eb3ff9a5 199 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (q));
c5f0f3d0
FN
200 }
201#if 0
aff410f1
MS
202 /* There is no way to do this just long enough to affect quote
203 inserting without also affecting the next completion. This
204 should be fixed in readline. FIXME. */
489f0516 205 /* Ensure that readline does the right thing
c5f0f3d0
FN
206 with respect to inserting quotes. */
207 rl_completer_word_break_characters = "";
208#endif
c5f0f3d0
FN
209}
210
6e1dbf8c
PA
211/* The corresponding completer_handle_brkchars
212 implementation. */
213
214static void
215filename_completer_handle_brkchars (struct cmd_list_element *ignore,
eb3ff9a5 216 completion_tracker &tracker,
6e1dbf8c
PA
217 const char *text, const char *word)
218{
219 set_rl_completer_word_break_characters
220 (gdb_completer_file_name_break_characters);
221}
222
6a2c1b87
PA
223/* Possible values for the found_quote flags word used by the completion
224 functions. It says what kind of (shell-like) quoting we found anywhere
225 in the line. */
226#define RL_QF_SINGLE_QUOTE 0x01
227#define RL_QF_DOUBLE_QUOTE 0x02
228#define RL_QF_BACKSLASH 0x04
229#define RL_QF_OTHER_QUOTE 0x08
230
231/* Find the bounds of the current word for completion purposes, and
232 return a pointer to the end of the word. This mimics (and is a
233 modified version of) readline's _rl_find_completion_word internal
234 function.
235
236 This function skips quoted substrings (characters between matched
237 pairs of characters in rl_completer_quote_characters). We try to
238 find an unclosed quoted substring on which to do matching. If one
239 is not found, we use the word break characters to find the
240 boundaries of the current word. QC, if non-null, is set to the
241 opening quote character if we found an unclosed quoted substring,
242 '\0' otherwise. DP, if non-null, is set to the value of the
243 delimiter character that caused a word break. */
244
245struct gdb_rl_completion_word_info
246{
247 const char *word_break_characters;
248 const char *quote_characters;
249 const char *basic_quote_characters;
250};
251
252static const char *
253gdb_rl_find_completion_word (struct gdb_rl_completion_word_info *info,
254 int *qc, int *dp,
255 const char *line_buffer)
256{
257 int scan, end, found_quote, delimiter, pass_next, isbrk;
258 char quote_char;
259 const char *brkchars;
260 int point = strlen (line_buffer);
261
262 /* The algorithm below does '--point'. Avoid buffer underflow with
263 the empty string. */
264 if (point == 0)
265 {
266 if (qc != NULL)
267 *qc = '\0';
268 if (dp != NULL)
269 *dp = '\0';
270 return line_buffer;
271 }
272
273 end = point;
274 found_quote = delimiter = 0;
275 quote_char = '\0';
276
277 brkchars = info->word_break_characters;
278
279 if (info->quote_characters != NULL)
280 {
281 /* We have a list of characters which can be used in pairs to
282 quote substrings for the completer. Try to find the start of
283 an unclosed quoted substring. */
284 /* FOUND_QUOTE is set so we know what kind of quotes we
285 found. */
286 for (scan = pass_next = 0;
287 scan < end;
288 scan++)
289 {
290 if (pass_next)
291 {
292 pass_next = 0;
293 continue;
294 }
295
296 /* Shell-like semantics for single quotes -- don't allow
297 backslash to quote anything in single quotes, especially
298 not the closing quote. If you don't like this, take out
299 the check on the value of quote_char. */
300 if (quote_char != '\'' && line_buffer[scan] == '\\')
301 {
302 pass_next = 1;
303 found_quote |= RL_QF_BACKSLASH;
304 continue;
305 }
306
307 if (quote_char != '\0')
308 {
309 /* Ignore everything until the matching close quote
310 char. */
311 if (line_buffer[scan] == quote_char)
312 {
313 /* Found matching close. Abandon this
314 substring. */
315 quote_char = '\0';
316 point = end;
317 }
318 }
319 else if (strchr (info->quote_characters, line_buffer[scan]))
320 {
321 /* Found start of a quoted substring. */
322 quote_char = line_buffer[scan];
323 point = scan + 1;
324 /* Shell-like quoting conventions. */
325 if (quote_char == '\'')
326 found_quote |= RL_QF_SINGLE_QUOTE;
327 else if (quote_char == '"')
328 found_quote |= RL_QF_DOUBLE_QUOTE;
329 else
330 found_quote |= RL_QF_OTHER_QUOTE;
331 }
332 }
333 }
334
335 if (point == end && quote_char == '\0')
336 {
337 /* We didn't find an unclosed quoted substring upon which to do
338 completion, so use the word break characters to find the
339 substring on which to complete. */
340 while (--point)
341 {
342 scan = line_buffer[point];
343
344 if (strchr (brkchars, scan) != 0)
345 break;
346 }
347 }
348
349 /* If we are at an unquoted word break, then advance past it. */
350 scan = line_buffer[point];
351
352 if (scan)
353 {
354 isbrk = strchr (brkchars, scan) != 0;
355
356 if (isbrk)
357 {
358 /* If the character that caused the word break was a quoting
359 character, then remember it as the delimiter. */
360 if (info->basic_quote_characters
361 && strchr (info->basic_quote_characters, scan)
362 && (end - point) > 1)
363 delimiter = scan;
364
365 point++;
366 }
367 }
368
369 if (qc != NULL)
370 *qc = quote_char;
371 if (dp != NULL)
372 *dp = delimiter;
373
374 return line_buffer + point;
375}
376
c6756f62
PA
377/* See completer.h. */
378
379const char *
380advance_to_expression_complete_word_point (completion_tracker &tracker,
381 const char *text)
382{
383 gdb_rl_completion_word_info info;
384
385 info.word_break_characters
386 = current_language->la_word_break_characters ();
387 info.quote_characters = gdb_completer_quote_characters;
388 info.basic_quote_characters = rl_basic_quote_characters;
389
390 const char *start
391 = gdb_rl_find_completion_word (&info, NULL, NULL, text);
392
393 tracker.advance_custom_word_point_by (start - text);
394
395 return start;
396}
397
398/* See completer.h. */
399
400bool
401completion_tracker::completes_to_completion_word (const char *word)
402{
403 if (m_lowest_common_denominator_unique)
404 {
405 const char *lcd = m_lowest_common_denominator;
406
407 if (strncmp_iw (word, lcd, strlen (lcd)) == 0)
408 {
409 /* Maybe skip the function and complete on keywords. */
410 size_t wordlen = strlen (word);
411 if (word[wordlen - 1] == ' ')
412 return true;
413 }
414 }
415
416 return false;
417}
418
87f0e720 419/* Complete on linespecs, which might be of two possible forms:
c94fdfd0
EZ
420
421 file:line
422 or
423 symbol+offset
424
aff410f1
MS
425 This is intended to be used in commands that set breakpoints
426 etc. */
427
eb3ff9a5
PA
428static void
429complete_files_symbols (completion_tracker &tracker,
430 const char *text, const char *word)
c94fdfd0 431{
eb3ff9a5
PA
432 int ix;
433 completion_list fn_list;
6f937416 434 const char *p;
c94fdfd0
EZ
435 int quote_found = 0;
436 int quoted = *text == '\'' || *text == '"';
437 int quote_char = '\0';
6f937416 438 const char *colon = NULL;
c94fdfd0 439 char *file_to_match = NULL;
6f937416
PA
440 const char *symbol_start = text;
441 const char *orig_text = text;
c94fdfd0 442
59be2b6a 443 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
c94fdfd0
EZ
444 for (p = text; *p != '\0'; ++p)
445 {
446 if (*p == '\\' && p[1] == '\'')
447 p++;
448 else if (*p == '\'' || *p == '"')
449 {
450 quote_found = *p;
451 quote_char = *p++;
452 while (*p != '\0' && *p != quote_found)
453 {
454 if (*p == '\\' && p[1] == quote_found)
455 p++;
456 p++;
457 }
458
459 if (*p == quote_found)
460 quote_found = 0;
461 else
9c3f90bd 462 break; /* Hit the end of text. */
c94fdfd0
EZ
463 }
464#if HAVE_DOS_BASED_FILE_SYSTEM
465 /* If we have a DOS-style absolute file name at the beginning of
466 TEXT, and the colon after the drive letter is the only colon
467 we found, pretend the colon is not there. */
468 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
469 ;
470#endif
471 else if (*p == ':' && !colon)
472 {
473 colon = p;
474 symbol_start = p + 1;
475 }
51065942 476 else if (strchr (current_language->la_word_break_characters(), *p))
c94fdfd0
EZ
477 symbol_start = p + 1;
478 }
479
480 if (quoted)
481 text++;
c94fdfd0
EZ
482
483 /* Where is the file name? */
484 if (colon)
485 {
486 char *s;
487
488 file_to_match = (char *) xmalloc (colon - text + 1);
bbfa2517
YQ
489 strncpy (file_to_match, text, colon - text);
490 file_to_match[colon - text] = '\0';
c94fdfd0
EZ
491 /* Remove trailing colons and quotes from the file name. */
492 for (s = file_to_match + (colon - text);
493 s > file_to_match;
494 s--)
495 if (*s == ':' || *s == quote_char)
496 *s = '\0';
497 }
498 /* If the text includes a colon, they want completion only on a
499 symbol name after the colon. Otherwise, we need to complete on
500 symbols as well as on files. */
501 if (colon)
502 {
c6756f62
PA
503 collect_file_symbol_completion_matches (tracker,
504 complete_symbol_mode::EXPRESSION,
b5ec771e 505 symbol_name_match_type::EXPRESSION,
c6756f62 506 symbol_start, word,
eb3ff9a5 507 file_to_match);
c94fdfd0
EZ
508 xfree (file_to_match);
509 }
510 else
511 {
eb3ff9a5
PA
512 size_t text_len = strlen (text);
513
c6756f62
PA
514 collect_symbol_completion_matches (tracker,
515 complete_symbol_mode::EXPRESSION,
b5ec771e 516 symbol_name_match_type::EXPRESSION,
c6756f62 517 symbol_start, word);
c94fdfd0
EZ
518 /* If text includes characters which cannot appear in a file
519 name, they cannot be asking for completion on files. */
eb3ff9a5 520 if (strcspn (text,
1f20ed91 521 gdb_completer_file_name_break_characters) == text_len)
c94fdfd0
EZ
522 fn_list = make_source_files_completion_list (text, text);
523 }
524
eb3ff9a5 525 if (!fn_list.empty () && !tracker.have_completions ())
c94fdfd0 526 {
6f937416
PA
527 char *fn;
528
c94fdfd0
EZ
529 /* If we only have file names as possible completion, we should
530 bring them in sync with what rl_complete expects. The
531 problem is that if the user types "break /foo/b TAB", and the
532 possible completions are "/foo/bar" and "/foo/baz"
533 rl_complete expects us to return "bar" and "baz", without the
534 leading directories, as possible completions, because `word'
535 starts at the "b". But we ignore the value of `word' when we
536 call make_source_files_completion_list above (because that
537 would not DTRT when the completion results in both symbols
538 and file names), so make_source_files_completion_list returns
539 the full "/foo/bar" and "/foo/baz" strings. This produces
540 wrong results when, e.g., there's only one possible
541 completion, because rl_complete will prepend "/foo/" to each
542 candidate completion. The loop below removes that leading
543 part. */
eb3ff9a5 544 for (const auto &fn_up: fn_list)
c94fdfd0 545 {
eb3ff9a5
PA
546 char *fn = fn_up.get ();
547 memmove (fn, fn + (word - text), strlen (fn) + 1 - (word - text));
c94fdfd0 548 }
c94fdfd0 549 }
eb3ff9a5
PA
550
551 tracker.add_completions (std::move (fn_list));
552
553 if (!tracker.have_completions ())
c94fdfd0
EZ
554 {
555 /* No completions at all. As the final resort, try completing
556 on the entire text as a symbol. */
eb3ff9a5 557 collect_symbol_completion_matches (tracker,
c6756f62 558 complete_symbol_mode::EXPRESSION,
b5ec771e 559 symbol_name_match_type::EXPRESSION,
eb3ff9a5 560 orig_text, word);
c94fdfd0 561 }
eb3ff9a5
PA
562}
563
c45ec17c
PA
564/* See completer.h. */
565
566completion_list
567complete_source_filenames (const char *text)
568{
569 size_t text_len = strlen (text);
570
571 /* If text includes characters which cannot appear in a file name,
572 the user cannot be asking for completion on files. */
573 if (strcspn (text,
574 gdb_completer_file_name_break_characters)
575 == text_len)
576 return make_source_files_completion_list (text, text);
577
578 return {};
579}
580
581/* Complete address and linespec locations. */
582
583static void
584complete_address_and_linespec_locations (completion_tracker &tracker,
a20714ff
PA
585 const char *text,
586 symbol_name_match_type match_type)
c45ec17c
PA
587{
588 if (*text == '*')
589 {
590 tracker.advance_custom_word_point_by (1);
591 text++;
592 const char *word
593 = advance_to_expression_complete_word_point (tracker, text);
594 complete_expression (tracker, text, word);
595 }
596 else
597 {
a20714ff 598 linespec_complete (tracker, text, match_type);
c45ec17c
PA
599 }
600}
601
c6756f62
PA
602/* The explicit location options. Note that indexes into this array
603 must match the explicit_location_match_type enumerators. */
c45ec17c 604
c6756f62
PA
605static const char *const explicit_options[] =
606 {
607 "-source",
608 "-function",
a20714ff 609 "-qualified",
c6756f62
PA
610 "-line",
611 "-label",
612 NULL
613 };
614
615/* The probe modifier options. These can appear before a location in
616 breakpoint commands. */
617static const char *const probe_options[] =
618 {
619 "-probe",
620 "-probe-stap",
621 "-probe-dtrace",
622 NULL
623 };
624
eb3ff9a5 625/* Returns STRING if not NULL, the empty string otherwise. */
c94fdfd0 626
eb3ff9a5
PA
627static const char *
628string_or_empty (const char *string)
629{
630 return string != NULL ? string : "";
c94fdfd0
EZ
631}
632
87f0e720
KS
633/* A helper function to collect explicit location matches for the given
634 LOCATION, which is attempting to match on WORD. */
635
eb3ff9a5
PA
636static void
637collect_explicit_location_matches (completion_tracker &tracker,
638 struct event_location *location,
87f0e720 639 enum explicit_location_match_type what,
c6756f62
PA
640 const char *word,
641 const struct language_defn *language)
87f0e720 642{
67994074
KS
643 const struct explicit_location *explicit_loc
644 = get_explicit_location (location);
87f0e720 645
a20714ff
PA
646 /* True if the option expects an argument. */
647 bool needs_arg = true;
648
c6756f62
PA
649 /* Note, in the various MATCH_* below, we complete on
650 explicit_loc->foo instead of WORD, because only the former will
651 have already skipped past any quote char. */
87f0e720
KS
652 switch (what)
653 {
654 case MATCH_SOURCE:
655 {
eb3ff9a5
PA
656 const char *source = string_or_empty (explicit_loc->source_filename);
657 completion_list matches
c6756f62 658 = make_source_files_completion_list (source, source);
eb3ff9a5 659 tracker.add_completions (std::move (matches));
87f0e720
KS
660 }
661 break;
662
663 case MATCH_FUNCTION:
664 {
eb3ff9a5 665 const char *function = string_or_empty (explicit_loc->function_name);
c6756f62 666 linespec_complete_function (tracker, function,
a20714ff 667 explicit_loc->func_name_match_type,
c6756f62 668 explicit_loc->source_filename);
87f0e720
KS
669 }
670 break;
671
a20714ff
PA
672 case MATCH_QUALIFIED:
673 needs_arg = false;
674 break;
c6756f62
PA
675 case MATCH_LINE:
676 /* Nothing to offer. */
677 break;
678
87f0e720 679 case MATCH_LABEL:
a2459270
PA
680 {
681 const char *label = string_or_empty (explicit_loc->label_name);
682 linespec_complete_label (tracker, language,
683 explicit_loc->source_filename,
684 explicit_loc->function_name,
a20714ff 685 explicit_loc->func_name_match_type,
a2459270
PA
686 label);
687 }
87f0e720
KS
688 break;
689
690 default:
691 gdb_assert_not_reached ("unhandled explicit_location_match_type");
692 }
c6756f62 693
a20714ff 694 if (!needs_arg || tracker.completes_to_completion_word (word))
c6756f62
PA
695 {
696 tracker.discard_completions ();
697 tracker.advance_custom_word_point_by (strlen (word));
698 complete_on_enum (tracker, explicit_options, "", "");
699 complete_on_enum (tracker, linespec_keywords, "", "");
700 }
701 else if (!tracker.have_completions ())
702 {
703 /* Maybe we have an unterminated linespec keyword at the tail of
704 the string. Try completing on that. */
705 size_t wordlen = strlen (word);
706 const char *keyword = word + wordlen;
707
708 if (wordlen > 0 && keyword[-1] != ' ')
709 {
710 while (keyword > word && *keyword != ' ')
711 keyword--;
712 /* Don't complete on keywords if we'd be completing on the
713 whole explicit linespec option. E.g., "b -function
714 thr<tab>" should not complete to the "thread"
715 keyword. */
716 if (keyword != word)
717 {
f1735a53 718 keyword = skip_spaces (keyword);
c6756f62
PA
719
720 tracker.advance_custom_word_point_by (keyword - word);
721 complete_on_enum (tracker, linespec_keywords, keyword, keyword);
722 }
723 }
724 else if (wordlen > 0 && keyword[-1] == ' ')
725 {
726 /* Assume that we're maybe past the explicit location
727 argument, and we didn't manage to find any match because
728 the user wants to create a pending breakpoint. Offer the
729 keyword and explicit location options as possible
730 completions. */
731 tracker.advance_custom_word_point_by (keyword - word);
732 complete_on_enum (tracker, linespec_keywords, keyword, keyword);
733 complete_on_enum (tracker, explicit_options, keyword, keyword);
734 }
735 }
87f0e720
KS
736}
737
c6756f62
PA
738/* If the next word in *TEXT_P is any of the keywords in KEYWORDS,
739 then advance both TEXT_P and the word point in the tracker past the
740 keyword and return the (0-based) index in the KEYWORDS array that
741 matched. Otherwise, return -1. */
87f0e720 742
c6756f62
PA
743static int
744skip_keyword (completion_tracker &tracker,
745 const char * const *keywords, const char **text_p)
87f0e720 746{
c6756f62 747 const char *text = *text_p;
f1735a53 748 const char *after = skip_to_space (text);
c6756f62 749 size_t len = after - text;
87f0e720 750
c6756f62
PA
751 if (text[len] != ' ')
752 return -1;
753
754 int found = -1;
755 for (int i = 0; keywords[i] != NULL; i++)
756 {
757 if (strncmp (keywords[i], text, len) == 0)
758 {
759 if (found == -1)
760 found = i;
761 else
762 return -1;
763 }
764 }
765
766 if (found != -1)
767 {
768 tracker.advance_custom_word_point_by (len + 1);
769 text += len + 1;
770 *text_p = text;
771 return found;
772 }
773
774 return -1;
87f0e720
KS
775}
776
777/* A completer function for explicit locations. This function
c6756f62
PA
778 completes both options ("-source", "-line", etc) and values. If
779 completing a quoted string, then QUOTED_ARG_START and
780 QUOTED_ARG_END point to the quote characters. LANGUAGE is the
781 current language. */
87f0e720 782
eb3ff9a5
PA
783static void
784complete_explicit_location (completion_tracker &tracker,
785 struct event_location *location,
c6756f62
PA
786 const char *text,
787 const language_defn *language,
788 const char *quoted_arg_start,
789 const char *quoted_arg_end)
87f0e720 790{
c6756f62
PA
791 if (*text != '-')
792 return;
87f0e720 793
c6756f62 794 int keyword = skip_keyword (tracker, explicit_options, &text);
87f0e720 795
c6756f62
PA
796 if (keyword == -1)
797 complete_on_enum (tracker, explicit_options, text, text);
798 else
87f0e720 799 {
c6756f62
PA
800 /* Completing on value. */
801 enum explicit_location_match_type what
802 = (explicit_location_match_type) keyword;
803
804 if (quoted_arg_start != NULL && quoted_arg_end != NULL)
87f0e720 805 {
c6756f62
PA
806 if (quoted_arg_end[1] == '\0')
807 {
808 /* If completing a quoted string with the cursor right
809 at the terminating quote char, complete the
810 completion word without interpretation, so that
811 readline advances the cursor one whitespace past the
812 quote, even if there's no match. This makes these
813 cases behave the same:
814
815 before: "b -function function()"
816 after: "b -function function() "
817
818 before: "b -function 'function()'"
819 after: "b -function 'function()' "
820
821 and trusts the user in this case:
822
823 before: "b -function 'not_loaded_function_yet()'"
824 after: "b -function 'not_loaded_function_yet()' "
825 */
826 gdb::unique_xmalloc_ptr<char> text_copy
827 (xstrdup (text));
828 tracker.add_completion (std::move (text_copy));
829 }
830 else if (quoted_arg_end[1] == ' ')
831 {
832 /* We're maybe past the explicit location argument.
833 Skip the argument without interpretion, assuming the
834 user may want to create pending breakpoint. Offer
835 the keyword and explicit location options as possible
836 completions. */
837 tracker.advance_custom_word_point_by (strlen (text));
838 complete_on_enum (tracker, linespec_keywords, "", "");
839 complete_on_enum (tracker, explicit_options, "", "");
840 }
841 return;
842 }
843
844 /* Now gather matches */
845 collect_explicit_location_matches (tracker, location, what, text,
846 language);
847 }
848}
87f0e720 849
c6756f62 850/* A completer for locations. */
87f0e720 851
c6756f62
PA
852void
853location_completer (struct cmd_list_element *ignore,
854 completion_tracker &tracker,
c45ec17c 855 const char *text, const char * /* word */)
c6756f62
PA
856{
857 int found_probe_option = -1;
858
859 /* If we have a probe modifier, skip it. This can only appear as
860 first argument. Until we have a specific completer for probes,
861 falling back to the linespec completer for the remainder of the
862 line is better than nothing. */
863 if (text[0] == '-' && text[1] == 'p')
864 found_probe_option = skip_keyword (tracker, probe_options, &text);
865
866 const char *option_text = text;
867 int saved_word_point = tracker.custom_word_point ();
868
869 const char *copy = text;
870
871 explicit_completion_info completion_info;
872 event_location_up location
873 = string_to_explicit_location (&copy, current_language,
874 &completion_info);
875 if (completion_info.quoted_arg_start != NULL
876 && completion_info.quoted_arg_end == NULL)
877 {
878 /* Found an unbalanced quote. */
879 tracker.set_quote_char (*completion_info.quoted_arg_start);
880 tracker.advance_custom_word_point_by (1);
87f0e720 881 }
c6756f62 882
a20714ff 883 if (completion_info.saw_explicit_location_option)
87f0e720 884 {
c6756f62 885 if (*copy != '\0')
87f0e720 886 {
c6756f62
PA
887 tracker.advance_custom_word_point_by (copy - text);
888 text = copy;
889
890 /* We found a terminator at the tail end of the string,
891 which means we're past the explicit location options. We
892 may have a keyword to complete on. If we have a whole
893 keyword, then complete whatever comes after as an
894 expression. This is mainly for the "if" keyword. If the
895 "thread" and "task" keywords gain their own completers,
896 they should be used here. */
897 int keyword = skip_keyword (tracker, linespec_keywords, &text);
898
899 if (keyword == -1)
900 {
901 complete_on_enum (tracker, linespec_keywords, text, text);
902 }
903 else
904 {
905 const char *word
906 = advance_to_expression_complete_word_point (tracker, text);
907 complete_expression (tracker, text, word);
908 }
87f0e720 909 }
c6756f62 910 else
87f0e720 911 {
c6756f62
PA
912 tracker.advance_custom_word_point_by (completion_info.last_option
913 - text);
914 text = completion_info.last_option;
915
916 complete_explicit_location (tracker, location.get (), text,
917 current_language,
918 completion_info.quoted_arg_start,
919 completion_info.quoted_arg_end);
920
87f0e720 921 }
c6756f62 922 }
a20714ff
PA
923 /* This is an address or linespec location. */
924 else if (location != NULL)
925 {
926 /* Handle non-explicit location options. */
927
928 int keyword = skip_keyword (tracker, explicit_options, &text);
929 if (keyword == -1)
930 complete_on_enum (tracker, explicit_options, text, text);
931 else
932 {
933 tracker.advance_custom_word_point_by (copy - text);
934 text = copy;
935
936 symbol_name_match_type match_type
937 = get_explicit_location (location.get ())->func_name_match_type;
938 complete_address_and_linespec_locations (tracker, text, match_type);
939 }
940 }
c6756f62
PA
941 else
942 {
a20714ff
PA
943 /* No options. */
944 complete_address_and_linespec_locations (tracker, text,
945 symbol_name_match_type::WILD);
c6756f62 946 }
87f0e720 947
c6756f62 948 /* Add matches for option names, if either:
87f0e720 949
c6756f62
PA
950 - Some completer above found some matches, but the word point did
951 not advance (e.g., "b <tab>" finds all functions, or "b -<tab>"
952 matches all objc selectors), or;
953
954 - Some completer above advanced the word point, but found no
955 matches.
956 */
957 if ((text[0] == '-' || text[0] == '\0')
958 && (!tracker.have_completions ()
959 || tracker.custom_word_point () == saved_word_point))
960 {
961 tracker.set_custom_word_point (saved_word_point);
962 text = option_text;
963
964 if (found_probe_option == -1)
965 complete_on_enum (tracker, probe_options, text, text);
966 complete_on_enum (tracker, explicit_options, text, text);
87f0e720 967 }
87f0e720
KS
968}
969
c6756f62
PA
970/* The corresponding completer_handle_brkchars
971 implementation. */
87f0e720 972
c6756f62
PA
973static void
974location_completer_handle_brkchars (struct cmd_list_element *ignore,
975 completion_tracker &tracker,
976 const char *text,
977 const char *word_ignored)
87f0e720 978{
c6756f62 979 tracker.set_use_custom_word_point (true);
87f0e720 980
c6756f62 981 location_completer (ignore, tracker, text, NULL);
87f0e720
KS
982}
983
1c71341a 984/* Helper for expression_completer which recursively adds field and
eb3ff9a5
PA
985 method names from TYPE, a struct or union type, to the OUTPUT
986 list. */
987
65d12d83 988static void
eb3ff9a5 989add_struct_fields (struct type *type, completion_list &output,
65d12d83
TT
990 char *fieldname, int namelen)
991{
992 int i;
b32d97f3 993 int computed_type_name = 0;
0d5cff50 994 const char *type_name = NULL;
65d12d83 995
f168693b 996 type = check_typedef (type);
65d12d83
TT
997 for (i = 0; i < TYPE_NFIELDS (type); ++i)
998 {
999 if (i < TYPE_N_BASECLASSES (type))
49c4e619 1000 add_struct_fields (TYPE_BASECLASS (type, i),
aff410f1 1001 output, fieldname, namelen);
9ae8282d 1002 else if (TYPE_FIELD_NAME (type, i))
65d12d83 1003 {
9ae8282d
TT
1004 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
1005 {
aff410f1
MS
1006 if (! strncmp (TYPE_FIELD_NAME (type, i),
1007 fieldname, namelen))
eb3ff9a5 1008 output.emplace_back (xstrdup (TYPE_FIELD_NAME (type, i)));
9ae8282d
TT
1009 }
1010 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
1011 {
1012 /* Recurse into anonymous unions. */
49c4e619 1013 add_struct_fields (TYPE_FIELD_TYPE (type, i),
aff410f1 1014 output, fieldname, namelen);
9ae8282d 1015 }
65d12d83
TT
1016 }
1017 }
1c71341a
TT
1018
1019 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1020 {
0d5cff50 1021 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
c5504eaf 1022
1c71341a
TT
1023 if (name && ! strncmp (name, fieldname, namelen))
1024 {
b32d97f3
TT
1025 if (!computed_type_name)
1026 {
1027 type_name = type_name_no_tag (type);
1028 computed_type_name = 1;
1029 }
1c71341a 1030 /* Omit constructors from the completion list. */
907af001 1031 if (!type_name || strcmp (type_name, name))
eb3ff9a5 1032 output.emplace_back (xstrdup (name));
1c71341a
TT
1033 }
1034 }
65d12d83
TT
1035}
1036
c45ec17c 1037/* See completer.h. */
eb3ff9a5 1038
c45ec17c 1039void
eb3ff9a5
PA
1040complete_expression (completion_tracker &tracker,
1041 const char *text, const char *word)
65d12d83 1042{
c92817ce 1043 struct type *type = NULL;
6f937416 1044 char *fieldname;
2f68a895 1045 enum type_code code = TYPE_CODE_UNDEF;
65d12d83
TT
1046
1047 /* Perform a tentative parse of the expression, to see whether a
1048 field completion is required. */
1049 fieldname = NULL;
492d29ea 1050 TRY
c92817ce 1051 {
2f68a895 1052 type = parse_expression_for_completion (text, &fieldname, &code);
c92817ce 1053 }
492d29ea
PA
1054 CATCH (except, RETURN_MASK_ERROR)
1055 {
eb3ff9a5 1056 return;
492d29ea
PA
1057 }
1058 END_CATCH
1059
65d12d83
TT
1060 if (fieldname && type)
1061 {
1062 for (;;)
1063 {
f168693b 1064 type = check_typedef (type);
aa006118 1065 if (TYPE_CODE (type) != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type))
65d12d83
TT
1066 break;
1067 type = TYPE_TARGET_TYPE (type);
1068 }
1069
1070 if (TYPE_CODE (type) == TYPE_CODE_UNION
1071 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
1072 {
65d12d83 1073 int flen = strlen (fieldname);
eb3ff9a5 1074 completion_list result;
65d12d83 1075
eb3ff9a5 1076 add_struct_fields (type, result, fieldname, flen);
a0b7aece 1077 xfree (fieldname);
eb3ff9a5
PA
1078 tracker.add_completions (std::move (result));
1079 return;
65d12d83
TT
1080 }
1081 }
2f68a895
TT
1082 else if (fieldname && code != TYPE_CODE_UNDEF)
1083 {
1084 VEC (char_ptr) *result;
1085 struct cleanup *cleanup = make_cleanup (xfree, fieldname);
1086
eb3ff9a5
PA
1087 collect_symbol_completion_matches_type (tracker, fieldname, fieldname,
1088 code);
2f68a895 1089 do_cleanups (cleanup);
eb3ff9a5 1090 return;
2f68a895 1091 }
a0b7aece 1092 xfree (fieldname);
65d12d83 1093
eb3ff9a5
PA
1094 complete_files_symbols (tracker, text, word);
1095}
1096
1097/* Complete on expressions. Often this means completing on symbol
1098 names, but some language parsers also have support for completing
1099 field names. */
1100
1101void
1102expression_completer (struct cmd_list_element *ignore,
1103 completion_tracker &tracker,
1104 const char *text, const char *word)
1105{
1106 complete_expression (tracker, text, word);
65d12d83
TT
1107}
1108
7d793aa9
SDJ
1109/* See definition in completer.h. */
1110
67cb5b2d
PA
1111void
1112set_rl_completer_word_break_characters (const char *break_chars)
1113{
1114 rl_completer_word_break_characters = (char *) break_chars;
1115}
1116
1117/* See definition in completer.h. */
1118
7d793aa9
SDJ
1119void
1120set_gdb_completion_word_break_characters (completer_ftype *fn)
1121{
67cb5b2d
PA
1122 const char *break_chars;
1123
7d793aa9
SDJ
1124 /* So far we are only interested in differentiating filename
1125 completers from everything else. */
1126 if (fn == filename_completer)
67cb5b2d 1127 break_chars = gdb_completer_file_name_break_characters;
7d793aa9 1128 else
67cb5b2d
PA
1129 break_chars = gdb_completer_command_word_break_characters;
1130
1131 set_rl_completer_word_break_characters (break_chars);
7d793aa9
SDJ
1132}
1133
78b13106
PA
1134/* Complete on symbols. */
1135
eb3ff9a5 1136void
78b13106 1137symbol_completer (struct cmd_list_element *ignore,
eb3ff9a5 1138 completion_tracker &tracker,
78b13106
PA
1139 const char *text, const char *word)
1140{
c6756f62 1141 collect_symbol_completion_matches (tracker, complete_symbol_mode::EXPRESSION,
b5ec771e 1142 symbol_name_match_type::EXPRESSION,
c6756f62 1143 text, word);
78b13106
PA
1144}
1145
aff410f1
MS
1146/* Here are some useful test cases for completion. FIXME: These
1147 should be put in the test suite. They should be tested with both
1148 M-? and TAB.
c5f0f3d0
FN
1149
1150 "show output-" "radix"
1151 "show output" "-radix"
1152 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1153 "p " ambiguous (all symbols)
1154 "info t foo" no completions
1155 "info t " no completions
1156 "info t" ambiguous ("info target", "info terminal", etc.)
1157 "info ajksdlfk" no completions
1158 "info ajksdlfk " no completions
1159 "info" " "
1160 "info " ambiguous (all info commands)
1161 "p \"a" no completions (string constant)
1162 "p 'a" ambiguous (all symbols starting with a)
1163 "p b-a" ambiguous (all symbols starting with a)
1164 "p b-" ambiguous (all symbols)
1165 "file Make" "file" (word break hard to screw up here)
1166 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1167 */
1168
eb3ff9a5 1169enum complete_line_internal_reason
67c296a2 1170{
eb3ff9a5 1171 /* Preliminary phase, called by gdb_completion_word_break_characters
c6756f62
PA
1172 function, is used to either:
1173
1174 #1 - Determine the set of chars that are word delimiters
1175 depending on the current command in line_buffer.
1176
1177 #2 - Manually advance RL_POINT to the "word break" point instead
1178 of letting readline do it (based on too-simple character
1179 matching).
1180
1181 Simpler completers that just pass a brkchars array to readline
1182 (#1 above) must defer generating the completions to the main
1183 phase (below). No completion list should be generated in this
1184 phase.
1185
1186 OTOH, completers that manually advance the word point(#2 above)
1187 must set "use_custom_word_point" in the tracker and generate
1188 their completion in this phase. Note that this is the convenient
1189 thing to do since they'll be parsing the input line anyway. */
67c296a2 1190 handle_brkchars,
eb3ff9a5
PA
1191
1192 /* Main phase, called by complete_line function, is used to get the
1193 list of possible completions. */
67c296a2 1194 handle_completions,
eb3ff9a5
PA
1195
1196 /* Special case when completing a 'help' command. In this case,
1197 once sub-command completions are exhausted, we simply return
1198 NULL. */
1199 handle_help,
1200};
67c296a2 1201
6e1dbf8c
PA
1202/* Helper for complete_line_internal to simplify it. */
1203
eb3ff9a5
PA
1204static void
1205complete_line_internal_normal_command (completion_tracker &tracker,
1206 const char *command, const char *word,
6e1dbf8c
PA
1207 const char *cmd_args,
1208 complete_line_internal_reason reason,
1209 struct cmd_list_element *c)
1210{
1211 const char *p = cmd_args;
1212
1213 if (c->completer == filename_completer)
1214 {
1215 /* Many commands which want to complete on file names accept
1216 several file names, as in "run foo bar >>baz". So we don't
1217 want to complete the entire text after the command, just the
1218 last word. To this end, we need to find the beginning of the
1219 file name by starting at `word' and going backwards. */
1220 for (p = word;
1221 p > command
1222 && strchr (gdb_completer_file_name_break_characters,
1223 p[-1]) == NULL;
1224 p--)
1225 ;
1226 }
1227
1228 if (reason == handle_brkchars)
1229 {
1230 completer_handle_brkchars_ftype *brkchars_fn;
1231
1232 if (c->completer_handle_brkchars != NULL)
1233 brkchars_fn = c->completer_handle_brkchars;
1234 else
1235 {
1236 brkchars_fn
1237 = (completer_handle_brkchars_func_for_completer
1238 (c->completer));
1239 }
1240
eb3ff9a5 1241 brkchars_fn (c, tracker, p, word);
6e1dbf8c
PA
1242 }
1243
1244 if (reason != handle_brkchars && c->completer != NULL)
eb3ff9a5 1245 (*c->completer) (c, tracker, p, word);
6e1dbf8c 1246}
67c296a2
PM
1247
1248/* Internal function used to handle completions.
1249
c5f0f3d0
FN
1250
1251 TEXT is the caller's idea of the "word" we are looking at.
1252
aff410f1
MS
1253 LINE_BUFFER is available to be looked at; it contains the entire
1254 text of the line. POINT is the offset in that line of the cursor.
1255 You should pretend that the line ends at POINT.
67c296a2 1256
eb3ff9a5 1257 See complete_line_internal_reason for description of REASON. */
14032a66 1258
eb3ff9a5
PA
1259static void
1260complete_line_internal_1 (completion_tracker &tracker,
1261 const char *text,
1262 const char *line_buffer, int point,
1263 complete_line_internal_reason reason)
c5f0f3d0 1264{
6f937416
PA
1265 char *tmp_command;
1266 const char *p;
ace21957 1267 int ignore_help_classes;
c5f0f3d0 1268 /* Pointer within tmp_command which corresponds to text. */
eb3ff9a5 1269 const char *word;
c5f0f3d0
FN
1270 struct cmd_list_element *c, *result_list;
1271
aff410f1
MS
1272 /* Choose the default set of word break characters to break
1273 completions. If we later find out that we are doing completions
1274 on command strings (as opposed to strings supplied by the
1275 individual command completer functions, which can be any string)
1276 then we will switch to the special word break set for command
1277 strings, which leaves out the '-' character used in some
1278 commands. */
67cb5b2d
PA
1279 set_rl_completer_word_break_characters
1280 (current_language->la_word_break_characters());
c5f0f3d0 1281
aff410f1
MS
1282 /* Decide whether to complete on a list of gdb commands or on
1283 symbols. */
83d31a92
TT
1284 tmp_command = (char *) alloca (point + 1);
1285 p = tmp_command;
c5f0f3d0 1286
ace21957
MF
1287 /* The help command should complete help aliases. */
1288 ignore_help_classes = reason != handle_help;
1289
83d31a92
TT
1290 strncpy (tmp_command, line_buffer, point);
1291 tmp_command[point] = '\0';
eb3ff9a5
PA
1292 if (reason == handle_brkchars)
1293 {
1294 gdb_assert (text == NULL);
1295 word = NULL;
1296 }
1297 else
1298 {
1299 /* Since text always contains some number of characters leading up
1300 to point, we can find the equivalent position in tmp_command
1301 by subtracting that many characters from the end of tmp_command. */
1302 word = tmp_command + point - strlen (text);
1303 }
c5f0f3d0 1304
a81aaca0
PA
1305 /* Move P up to the start of the command. */
1306 p = skip_spaces (p);
1307
1308 if (*p == '\0')
83d31a92 1309 {
a81aaca0
PA
1310 /* An empty line is ambiguous; that is, it could be any
1311 command. */
1427fe5e 1312 c = CMD_LIST_AMBIGUOUS;
83d31a92
TT
1313 result_list = 0;
1314 }
1315 else
1316 {
ace21957 1317 c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes);
83d31a92 1318 }
c5f0f3d0 1319
83d31a92
TT
1320 /* Move p up to the next interesting thing. */
1321 while (*p == ' ' || *p == '\t')
1322 {
1323 p++;
1324 }
c5f0f3d0 1325
c6756f62
PA
1326 tracker.advance_custom_word_point_by (p - tmp_command);
1327
83d31a92
TT
1328 if (!c)
1329 {
1330 /* It is an unrecognized command. So there are no
1331 possible completions. */
83d31a92 1332 }
1427fe5e 1333 else if (c == CMD_LIST_AMBIGUOUS)
83d31a92 1334 {
6f937416 1335 const char *q;
83d31a92
TT
1336
1337 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1338 doesn't advance over that thing itself. Do so now. */
1339 q = p;
1340 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
1341 ++q;
1342 if (q != tmp_command + point)
c5f0f3d0 1343 {
83d31a92
TT
1344 /* There is something beyond the ambiguous
1345 command, so there are no possible completions. For
1346 example, "info t " or "info t foo" does not complete
1347 to anything, because "info t" can be "info target" or
1348 "info terminal". */
c5f0f3d0 1349 }
83d31a92 1350 else
c5f0f3d0 1351 {
83d31a92
TT
1352 /* We're trying to complete on the command which was ambiguous.
1353 This we can deal with. */
1354 if (result_list)
c5f0f3d0 1355 {
67c296a2 1356 if (reason != handle_brkchars)
eb3ff9a5
PA
1357 complete_on_cmdlist (*result_list->prefixlist, tracker, p,
1358 word, ignore_help_classes);
c5f0f3d0
FN
1359 }
1360 else
1361 {
67c296a2 1362 if (reason != handle_brkchars)
eb3ff9a5
PA
1363 complete_on_cmdlist (cmdlist, tracker, p, word,
1364 ignore_help_classes);
c5f0f3d0 1365 }
489f0516 1366 /* Ensure that readline does the right thing with respect to
83d31a92 1367 inserting quotes. */
67cb5b2d
PA
1368 set_rl_completer_word_break_characters
1369 (gdb_completer_command_word_break_characters);
c5f0f3d0 1370 }
83d31a92
TT
1371 }
1372 else
1373 {
1374 /* We've recognized a full command. */
1375
1376 if (p == tmp_command + point)
c5f0f3d0 1377 {
aff410f1
MS
1378 /* There is no non-whitespace in the line beyond the
1379 command. */
c5f0f3d0 1380
83d31a92 1381 if (p[-1] == ' ' || p[-1] == '\t')
c5f0f3d0 1382 {
aff410f1
MS
1383 /* The command is followed by whitespace; we need to
1384 complete on whatever comes after command. */
83d31a92 1385 if (c->prefixlist)
c5f0f3d0 1386 {
83d31a92
TT
1387 /* It is a prefix command; what comes after it is
1388 a subcommand (e.g. "info "). */
67c296a2 1389 if (reason != handle_brkchars)
eb3ff9a5
PA
1390 complete_on_cmdlist (*c->prefixlist, tracker, p, word,
1391 ignore_help_classes);
c5f0f3d0 1392
489f0516 1393 /* Ensure that readline does the right thing
9c3f90bd 1394 with respect to inserting quotes. */
67cb5b2d
PA
1395 set_rl_completer_word_break_characters
1396 (gdb_completer_command_word_break_characters);
c5f0f3d0 1397 }
67c296a2 1398 else if (reason == handle_help)
eb3ff9a5 1399 ;
c5f0f3d0
FN
1400 else if (c->enums)
1401 {
67c296a2 1402 if (reason != handle_brkchars)
eb3ff9a5 1403 complete_on_enum (tracker, c->enums, p, word);
67cb5b2d
PA
1404 set_rl_completer_word_break_characters
1405 (gdb_completer_command_word_break_characters);
c5f0f3d0
FN
1406 }
1407 else
1408 {
83d31a92
TT
1409 /* It is a normal command; what comes after it is
1410 completed by the command's completer function. */
eb3ff9a5
PA
1411 complete_line_internal_normal_command (tracker,
1412 tmp_command, word, p,
1413 reason, c);
c5f0f3d0
FN
1414 }
1415 }
83d31a92
TT
1416 else
1417 {
1418 /* The command is not followed by whitespace; we need to
aff410f1 1419 complete on the command itself, e.g. "p" which is a
83d31a92
TT
1420 command itself but also can complete to "print", "ptype"
1421 etc. */
6f937416 1422 const char *q;
83d31a92
TT
1423
1424 /* Find the command we are completing on. */
1425 q = p;
1426 while (q > tmp_command)
1427 {
1428 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
1429 --q;
1430 else
1431 break;
1432 }
1433
67c296a2 1434 if (reason != handle_brkchars)
eb3ff9a5
PA
1435 complete_on_cmdlist (result_list, tracker, q, word,
1436 ignore_help_classes);
83d31a92 1437
489f0516 1438 /* Ensure that readline does the right thing
9c3f90bd 1439 with respect to inserting quotes. */
67cb5b2d
PA
1440 set_rl_completer_word_break_characters
1441 (gdb_completer_command_word_break_characters);
83d31a92
TT
1442 }
1443 }
67c296a2 1444 else if (reason == handle_help)
eb3ff9a5 1445 ;
83d31a92
TT
1446 else
1447 {
1448 /* There is non-whitespace beyond the command. */
1449
1450 if (c->prefixlist && !c->allow_unknown)
1451 {
1452 /* It is an unrecognized subcommand of a prefix command,
1453 e.g. "info adsfkdj". */
83d31a92
TT
1454 }
1455 else if (c->enums)
1456 {
67c296a2 1457 if (reason != handle_brkchars)
eb3ff9a5 1458 complete_on_enum (tracker, c->enums, p, word);
83d31a92
TT
1459 }
1460 else
1461 {
1462 /* It is a normal command. */
eb3ff9a5
PA
1463 complete_line_internal_normal_command (tracker,
1464 tmp_command, word, p,
1465 reason, c);
83d31a92
TT
1466 }
1467 }
1468 }
83d31a92 1469}
ef0b411a 1470
eb3ff9a5
PA
1471/* Wrapper around complete_line_internal_1 to handle
1472 MAX_COMPLETIONS_REACHED_ERROR. */
ef0b411a 1473
eb3ff9a5
PA
1474static void
1475complete_line_internal (completion_tracker &tracker,
1476 const char *text,
1477 const char *line_buffer, int point,
1478 complete_line_internal_reason reason)
1479{
1480 TRY
1481 {
1482 complete_line_internal_1 (tracker, text, line_buffer, point, reason);
1483 }
1484 CATCH (except, RETURN_MASK_ERROR)
1485 {
1486 if (except.error != MAX_COMPLETIONS_REACHED_ERROR)
1487 throw_exception (except);
1488 }
a87c1427 1489 END_CATCH
eb3ff9a5 1490}
ef0b411a
GB
1491
1492/* See completer.h. */
1493
eb3ff9a5 1494int max_completions = 200;
ef0b411a 1495
eb3ff9a5
PA
1496/* Initial size of the table. It automagically grows from here. */
1497#define INITIAL_COMPLETION_HTAB_SIZE 200
ef0b411a 1498
eb3ff9a5 1499/* See completer.h. */
ef0b411a 1500
eb3ff9a5 1501completion_tracker::completion_tracker ()
ef0b411a 1502{
eb3ff9a5
PA
1503 m_entries_hash = htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE,
1504 htab_hash_string, (htab_eq) streq,
1505 NULL, xcalloc, xfree);
ef0b411a
GB
1506}
1507
1508/* See completer.h. */
1509
c6756f62
PA
1510void
1511completion_tracker::discard_completions ()
1512{
1513 xfree (m_lowest_common_denominator);
1514 m_lowest_common_denominator = NULL;
1515
1516 m_lowest_common_denominator_unique = false;
1517
1518 m_entries_vec.clear ();
1519
1520 htab_delete (m_entries_hash);
1521 m_entries_hash = htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE,
1522 htab_hash_string, (htab_eq) streq,
1523 NULL, xcalloc, xfree);
1524}
1525
1526/* See completer.h. */
1527
eb3ff9a5 1528completion_tracker::~completion_tracker ()
ef0b411a 1529{
eb3ff9a5
PA
1530 xfree (m_lowest_common_denominator);
1531 htab_delete (m_entries_hash);
ef0b411a
GB
1532}
1533
1534/* See completer.h. */
1535
eb3ff9a5 1536bool
a207cff2
PA
1537completion_tracker::maybe_add_completion
1538 (gdb::unique_xmalloc_ptr<char> name,
1539 completion_match_for_lcd *match_for_lcd)
ef0b411a
GB
1540{
1541 void **slot;
1542
ef0b411a 1543 if (max_completions == 0)
eb3ff9a5 1544 return false;
ef0b411a 1545
eb3ff9a5
PA
1546 if (htab_elements (m_entries_hash) >= max_completions)
1547 return false;
ef0b411a 1548
eb3ff9a5
PA
1549 slot = htab_find_slot (m_entries_hash, name.get (), INSERT);
1550 if (*slot == HTAB_EMPTY_ENTRY)
1551 {
a207cff2
PA
1552 const char *match_for_lcd_str = NULL;
1553
1554 if (match_for_lcd != NULL)
1555 match_for_lcd_str = match_for_lcd->finish ();
1556
1557 if (match_for_lcd_str == NULL)
1558 match_for_lcd_str = name.get ();
ef0b411a 1559
eb3ff9a5 1560 recompute_lowest_common_denominator (match_for_lcd_str);
ef0b411a 1561
eb3ff9a5
PA
1562 *slot = name.get ();
1563 m_entries_vec.push_back (std::move (name));
1564 }
ef0b411a 1565
eb3ff9a5
PA
1566 return true;
1567}
1568
1569/* See completer.h. */
ef0b411a 1570
eb3ff9a5 1571void
a207cff2
PA
1572completion_tracker::add_completion (gdb::unique_xmalloc_ptr<char> name,
1573 completion_match_for_lcd *match_for_lcd)
eb3ff9a5 1574{
a207cff2 1575 if (!maybe_add_completion (std::move (name), match_for_lcd))
eb3ff9a5 1576 throw_error (MAX_COMPLETIONS_REACHED_ERROR, _("Max completions reached."));
ef0b411a
GB
1577}
1578
eb3ff9a5
PA
1579/* See completer.h. */
1580
ef0b411a 1581void
eb3ff9a5 1582completion_tracker::add_completions (completion_list &&list)
ef0b411a 1583{
eb3ff9a5
PA
1584 for (auto &candidate : list)
1585 add_completion (std::move (candidate));
ef0b411a
GB
1586}
1587
eb3ff9a5
PA
1588/* Generate completions all at once. Does nothing if max_completions
1589 is 0. If max_completions is non-negative, this will collect at
1590 most max_completions strings.
83d31a92 1591
67c296a2
PM
1592 TEXT is the caller's idea of the "word" we are looking at.
1593
aff410f1
MS
1594 LINE_BUFFER is available to be looked at; it contains the entire
1595 text of the line.
67c296a2
PM
1596
1597 POINT is the offset in that line of the cursor. You
1598 should pretend that the line ends at POINT. */
14032a66 1599
eb3ff9a5
PA
1600void
1601complete_line (completion_tracker &tracker,
1602 const char *text, const char *line_buffer, int point)
14032a66 1603{
ef0b411a 1604 if (max_completions == 0)
eb3ff9a5
PA
1605 return;
1606 complete_line_internal (tracker, text, line_buffer, point,
1607 handle_completions);
14032a66
TT
1608}
1609
1610/* Complete on command names. Used by "help". */
6e1dbf8c 1611
eb3ff9a5 1612void
aff410f1 1613command_completer (struct cmd_list_element *ignore,
eb3ff9a5 1614 completion_tracker &tracker,
6f937416 1615 const char *text, const char *word)
14032a66 1616{
eb3ff9a5
PA
1617 complete_line_internal (tracker, word, text,
1618 strlen (text), handle_help);
67c296a2
PM
1619}
1620
6e1dbf8c
PA
1621/* The corresponding completer_handle_brkchars implementation. */
1622
1623static void
1624command_completer_handle_brkchars (struct cmd_list_element *ignore,
eb3ff9a5 1625 completion_tracker &tracker,
6e1dbf8c
PA
1626 const char *text, const char *word)
1627{
1628 set_rl_completer_word_break_characters
1629 (gdb_completer_command_word_break_characters);
1630}
1631
de0bea00
MF
1632/* Complete on signals. */
1633
eb3ff9a5 1634void
de0bea00 1635signal_completer (struct cmd_list_element *ignore,
eb3ff9a5 1636 completion_tracker &tracker,
6f937416 1637 const char *text, const char *word)
de0bea00 1638{
de0bea00 1639 size_t len = strlen (word);
570dc176 1640 int signum;
de0bea00
MF
1641 const char *signame;
1642
1643 for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
1644 {
1645 /* Can't handle this, so skip it. */
1646 if (signum == GDB_SIGNAL_0)
1647 continue;
1648
570dc176 1649 signame = gdb_signal_to_name ((enum gdb_signal) signum);
de0bea00
MF
1650
1651 /* Ignore the unknown signal case. */
1652 if (!signame || strcmp (signame, "?") == 0)
1653 continue;
1654
1655 if (strncasecmp (signame, word, len) == 0)
eb3ff9a5
PA
1656 {
1657 gdb::unique_xmalloc_ptr<char> copy (xstrdup (signame));
1658 tracker.add_completion (std::move (copy));
1659 }
de0bea00 1660 }
de0bea00
MF
1661}
1662
51f0e40d
AB
1663/* Bit-flags for selecting what the register and/or register-group
1664 completer should complete on. */
71c24708 1665
8d297bbf 1666enum reg_completer_target
51f0e40d
AB
1667 {
1668 complete_register_names = 0x1,
1669 complete_reggroup_names = 0x2
1670 };
8d297bbf 1671DEF_ENUM_FLAGS_TYPE (enum reg_completer_target, reg_completer_targets);
51f0e40d
AB
1672
1673/* Complete register names and/or reggroup names based on the value passed
1674 in TARGETS. At least one bit in TARGETS must be set. */
1675
eb3ff9a5
PA
1676static void
1677reg_or_group_completer_1 (completion_tracker &tracker,
51f0e40d 1678 const char *text, const char *word,
8d297bbf 1679 reg_completer_targets targets)
71c24708 1680{
71c24708
AA
1681 size_t len = strlen (word);
1682 struct gdbarch *gdbarch;
71c24708 1683 const char *name;
71c24708 1684
51f0e40d
AB
1685 gdb_assert ((targets & (complete_register_names
1686 | complete_reggroup_names)) != 0);
1687 gdbarch = get_current_arch ();
71c24708 1688
51f0e40d 1689 if ((targets & complete_register_names) != 0)
71c24708 1690 {
51f0e40d
AB
1691 int i;
1692
1693 for (i = 0;
1694 (name = user_reg_map_regnum_to_name (gdbarch, i)) != NULL;
1695 i++)
1696 {
1697 if (*name != '\0' && strncmp (word, name, len) == 0)
eb3ff9a5
PA
1698 {
1699 gdb::unique_xmalloc_ptr<char> copy (xstrdup (name));
1700 tracker.add_completion (std::move (copy));
1701 }
51f0e40d 1702 }
71c24708
AA
1703 }
1704
51f0e40d 1705 if ((targets & complete_reggroup_names) != 0)
71c24708 1706 {
51f0e40d
AB
1707 struct reggroup *group;
1708
1709 for (group = reggroup_next (gdbarch, NULL);
1710 group != NULL;
1711 group = reggroup_next (gdbarch, group))
1712 {
1713 name = reggroup_name (group);
1714 if (strncmp (word, name, len) == 0)
eb3ff9a5
PA
1715 {
1716 gdb::unique_xmalloc_ptr<char> copy (xstrdup (name));
1717 tracker.add_completion (std::move (copy));
1718 }
51f0e40d 1719 }
71c24708 1720 }
71c24708
AA
1721}
1722
51f0e40d
AB
1723/* Perform completion on register and reggroup names. */
1724
eb3ff9a5 1725void
51f0e40d 1726reg_or_group_completer (struct cmd_list_element *ignore,
eb3ff9a5 1727 completion_tracker &tracker,
51f0e40d
AB
1728 const char *text, const char *word)
1729{
eb3ff9a5
PA
1730 reg_or_group_completer_1 (tracker, text, word,
1731 (complete_register_names
1732 | complete_reggroup_names));
51f0e40d
AB
1733}
1734
1735/* Perform completion on reggroup names. */
1736
eb3ff9a5 1737void
51f0e40d 1738reggroup_completer (struct cmd_list_element *ignore,
eb3ff9a5 1739 completion_tracker &tracker,
51f0e40d
AB
1740 const char *text, const char *word)
1741{
eb3ff9a5
PA
1742 reg_or_group_completer_1 (tracker, text, word,
1743 complete_reggroup_names);
51f0e40d 1744}
71c24708 1745
6e1dbf8c
PA
1746/* The default completer_handle_brkchars implementation. */
1747
1748static void
1749default_completer_handle_brkchars (struct cmd_list_element *ignore,
eb3ff9a5 1750 completion_tracker &tracker,
6e1dbf8c
PA
1751 const char *text, const char *word)
1752{
1753 set_rl_completer_word_break_characters
1754 (current_language->la_word_break_characters ());
1755}
1756
1757/* See definition in completer.h. */
1758
1759completer_handle_brkchars_ftype *
1760completer_handle_brkchars_func_for_completer (completer_ftype *fn)
1761{
1762 if (fn == filename_completer)
1763 return filename_completer_handle_brkchars;
1764
c6756f62
PA
1765 if (fn == location_completer)
1766 return location_completer_handle_brkchars;
1767
6e1dbf8c
PA
1768 if (fn == command_completer)
1769 return command_completer_handle_brkchars;
1770
1771 return default_completer_handle_brkchars;
1772}
1773
c6756f62
PA
1774/* Used as brkchars when we want to tell readline we have a custom
1775 word point. We do that by making our rl_completion_word_break_hook
1776 set RL_POINT to the desired word point, and return the character at
1777 the word break point as the break char. This is two bytes in order
1778 to fit one break character plus the terminating null. */
1779static char gdb_custom_word_point_brkchars[2];
1780
1781/* Since rl_basic_quote_characters is not completer-specific, we save
1782 its original value here, in order to be able to restore it in
1783 gdb_rl_attempted_completion_function. */
1784static const char *gdb_org_rl_basic_quote_characters = rl_basic_quote_characters;
1785
67c296a2
PM
1786/* Get the list of chars that are considered as word breaks
1787 for the current command. */
1788
eb3ff9a5
PA
1789static char *
1790gdb_completion_word_break_characters_throw ()
67c296a2 1791{
eb3ff9a5
PA
1792 /* New completion starting. Get rid of the previous tracker and
1793 start afresh. */
1794 delete current_completion.tracker;
1795 current_completion.tracker = new completion_tracker ();
1796
1797 completion_tracker &tracker = *current_completion.tracker;
1798
1799 complete_line_internal (tracker, NULL, rl_line_buffer,
1800 rl_point, handle_brkchars);
c5504eaf 1801
c6756f62
PA
1802 if (tracker.use_custom_word_point ())
1803 {
1804 gdb_assert (tracker.custom_word_point () > 0);
1805 rl_point = tracker.custom_word_point () - 1;
1806 gdb_custom_word_point_brkchars[0] = rl_line_buffer[rl_point];
1807 rl_completer_word_break_characters = gdb_custom_word_point_brkchars;
1808 rl_completer_quote_characters = NULL;
1809
1810 /* Clear this too, so that if we're completing a quoted string,
1811 readline doesn't consider the quote character a delimiter.
1812 If we didn't do this, readline would auto-complete {b
1813 'fun<tab>} to {'b 'function()'}, i.e., add the terminating
1814 \', but, it wouldn't append the separator space either, which
1815 is not desirable. So instead we take care of appending the
1816 quote character to the LCD ourselves, in
1817 gdb_rl_attempted_completion_function. Since this global is
1818 not just completer-specific, we'll restore it back to the
1819 default in gdb_rl_attempted_completion_function. */
1820 rl_basic_quote_characters = NULL;
1821 }
1822
67c296a2 1823 return rl_completer_word_break_characters;
14032a66
TT
1824}
1825
eb3ff9a5
PA
1826char *
1827gdb_completion_word_break_characters ()
1828{
1829 /* New completion starting. */
1830 current_completion.aborted = false;
83d31a92 1831
eb3ff9a5
PA
1832 TRY
1833 {
1834 return gdb_completion_word_break_characters_throw ();
1835 }
1836 CATCH (ex, RETURN_MASK_ALL)
1837 {
1838 /* Set this to that gdb_rl_attempted_completion_function knows
1839 to abort early. */
1840 current_completion.aborted = true;
1841 }
1842 END_CATCH
83d31a92 1843
eb3ff9a5
PA
1844 return NULL;
1845}
83d31a92 1846
eb3ff9a5 1847/* See completer.h. */
83d31a92 1848
6a2c1b87
PA
1849const char *
1850completion_find_completion_word (completion_tracker &tracker, const char *text,
1851 int *quote_char)
1852{
1853 size_t point = strlen (text);
1854
1855 complete_line_internal (tracker, NULL, text, point, handle_brkchars);
1856
c6756f62
PA
1857 if (tracker.use_custom_word_point ())
1858 {
1859 gdb_assert (tracker.custom_word_point () > 0);
1860 *quote_char = tracker.quote_char ();
1861 return text + tracker.custom_word_point ();
1862 }
1863
6a2c1b87
PA
1864 gdb_rl_completion_word_info info;
1865
1866 info.word_break_characters = rl_completer_word_break_characters;
1867 info.quote_characters = gdb_completer_quote_characters;
1868 info.basic_quote_characters = rl_basic_quote_characters;
1869
1870 return gdb_rl_find_completion_word (&info, quote_char, NULL, text);
1871}
1872
1873/* See completer.h. */
1874
eb3ff9a5
PA
1875void
1876completion_tracker::recompute_lowest_common_denominator (const char *new_match)
83d31a92 1877{
eb3ff9a5
PA
1878 if (m_lowest_common_denominator == NULL)
1879 {
1880 /* We don't have a lowest common denominator yet, so simply take
1881 the whole NEW_MATCH as being it. */
1882 m_lowest_common_denominator = xstrdup (new_match);
1883 m_lowest_common_denominator_unique = true;
1884 }
1885 else
83d31a92 1886 {
eb3ff9a5
PA
1887 /* Find the common denominator between the currently-known
1888 lowest common denominator and NEW_MATCH. That becomes the
1889 new lowest common denominator. */
1890 size_t i;
83d31a92 1891
eb3ff9a5
PA
1892 for (i = 0;
1893 (new_match[i] != '\0'
1894 && new_match[i] == m_lowest_common_denominator[i]);
1895 i++)
1896 ;
1897 if (m_lowest_common_denominator[i] != new_match[i])
83d31a92 1898 {
eb3ff9a5
PA
1899 m_lowest_common_denominator[i] = '\0';
1900 m_lowest_common_denominator_unique = false;
c5f0f3d0
FN
1901 }
1902 }
eb3ff9a5
PA
1903}
1904
c6756f62
PA
1905/* See completer.h. */
1906
1907void
1908completion_tracker::advance_custom_word_point_by (size_t len)
1909{
1910 m_custom_word_point += len;
1911}
1912
eb3ff9a5
PA
1913/* Build a new C string that is a copy of LCD with the whitespace of
1914 ORIG/ORIG_LEN preserved.
1915
1916 Say the user is completing a symbol name, with spaces, like:
1917
1918 "foo ( i"
1919
1920 and the resulting completion match is:
1921
1922 "foo(int)"
1923
1924 we want to end up with an input line like:
1925
1926 "foo ( int)"
1927 ^^^^^^^ => text from LCD [1], whitespace from ORIG preserved.
1928 ^^ => new text from LCD
1929
1930 [1] - We must take characters from the LCD instead of the original
1931 text, since some completions want to change upper/lowercase. E.g.:
c5f0f3d0 1932
eb3ff9a5 1933 "handle sig<>"
c5f0f3d0 1934
eb3ff9a5
PA
1935 completes to:
1936
1937 "handle SIG[QUIT|etc.]"
1938*/
1939
1940static char *
1941expand_preserving_ws (const char *orig, size_t orig_len,
1942 const char *lcd)
1943{
1944 const char *p_orig = orig;
1945 const char *orig_end = orig + orig_len;
1946 const char *p_lcd = lcd;
1947 std::string res;
1948
1949 while (p_orig < orig_end)
c5f0f3d0 1950 {
eb3ff9a5
PA
1951 if (*p_orig == ' ')
1952 {
1953 while (p_orig < orig_end && *p_orig == ' ')
1954 res += *p_orig++;
f1735a53 1955 p_lcd = skip_spaces (p_lcd);
eb3ff9a5
PA
1956 }
1957 else
c5f0f3d0 1958 {
eb3ff9a5
PA
1959 /* Take characters from the LCD instead of the original
1960 text, since some completions change upper/lowercase.
1961 E.g.:
1962 "handle sig<>"
1963 completes to:
1964 "handle SIG[QUIT|etc.]"
1965 */
1966 res += *p_lcd;
1967 p_orig++;
1968 p_lcd++;
c5f0f3d0
FN
1969 }
1970 }
1971
eb3ff9a5
PA
1972 while (*p_lcd != '\0')
1973 res += *p_lcd++;
1974
1975 return xstrdup (res.c_str ());
1976}
1977
1978/* See completer.h. */
1979
1980completion_result
1981completion_tracker::build_completion_result (const char *text,
1982 int start, int end)
1983{
1984 completion_list &list = m_entries_vec; /* The completions. */
1985
1986 if (list.empty ())
1987 return {};
1988
1989 /* +1 for the LCD, and +1 for NULL termination. */
1990 char **match_list = XNEWVEC (char *, 1 + list.size () + 1);
1991
1992 /* Build replacement word, based on the LCD. */
1993
1994 match_list[0]
1995 = expand_preserving_ws (text, end - start,
1996 m_lowest_common_denominator);
1997
1998 if (m_lowest_common_denominator_unique)
1999 {
c6756f62
PA
2000 /* We don't rely on readline appending the quote char as
2001 delimiter as then readline wouldn't append the ' ' after the
2002 completion. */
2003 char buf[2] = { quote_char () };
2004
2005 match_list[0] = reconcat (match_list[0], match_list[0],
2006 buf, (char *) NULL);
eb3ff9a5
PA
2007 match_list[1] = NULL;
2008
c45ec17c
PA
2009 /* If the tracker wants to, or we already have a space at the
2010 end of the match, tell readline to skip appending
2011 another. */
eb3ff9a5 2012 bool completion_suppress_append
c45ec17c
PA
2013 = (suppress_append_ws ()
2014 || match_list[0][strlen (match_list[0]) - 1] == ' ');
eb3ff9a5
PA
2015
2016 return completion_result (match_list, 1, completion_suppress_append);
2017 }
2018 else
2019 {
2020 int ix;
2021
2022 for (ix = 0; ix < list.size (); ++ix)
2023 match_list[ix + 1] = list[ix].release ();
2024 match_list[ix + 1] = NULL;
2025
2026 return completion_result (match_list, list.size (), false);
2027 }
2028}
2029
2030/* See completer.h */
2031
2032completion_result::completion_result ()
2033 : match_list (NULL), number_matches (0),
2034 completion_suppress_append (false)
2035{}
2036
2037/* See completer.h */
2038
2039completion_result::completion_result (char **match_list_,
2040 size_t number_matches_,
2041 bool completion_suppress_append_)
2042 : match_list (match_list_),
2043 number_matches (number_matches_),
2044 completion_suppress_append (completion_suppress_append_)
2045{}
2046
2047/* See completer.h */
2048
2049completion_result::~completion_result ()
2050{
2051 reset_match_list ();
2052}
2053
2054/* See completer.h */
2055
2056completion_result::completion_result (completion_result &&rhs)
2057{
2058 if (this == &rhs)
2059 return;
2060
2061 reset_match_list ();
2062 match_list = rhs.match_list;
2063 rhs.match_list = NULL;
2064 number_matches = rhs.number_matches;
2065 rhs.number_matches = 0;
2066}
2067
2068/* See completer.h */
2069
2070char **
2071completion_result::release_match_list ()
2072{
2073 char **ret = match_list;
2074 match_list = NULL;
2075 return ret;
2076}
2077
eb3ff9a5
PA
2078/* See completer.h */
2079
2080void
2081completion_result::sort_match_list ()
2082{
2083 if (number_matches > 1)
2084 {
2085 /* Element 0 is special (it's the common prefix), leave it
2086 be. */
2087 std::sort (&match_list[1],
2088 &match_list[number_matches + 1],
2089 compare_cstrings);
2090 }
2091}
2092
2093/* See completer.h */
2094
2095void
2096completion_result::reset_match_list ()
2097{
2098 if (match_list != NULL)
2099 {
2100 for (char **p = match_list; *p != NULL; p++)
2101 xfree (*p);
2102 xfree (match_list);
2103 match_list = NULL;
2104 }
2105}
2106
2107/* Helper for gdb_rl_attempted_completion_function, which does most of
2108 the work. This is called by readline to build the match list array
2109 and to determine the lowest common denominator. The real matches
2110 list starts at match[1], while match[0] is the slot holding
2111 readline's idea of the lowest common denominator of all matches,
2112 which is what readline replaces the completion "word" with.
2113
2114 TEXT is the caller's idea of the "word" we are looking at, as
2115 computed in the handle_brkchars phase.
2116
2117 START is the offset from RL_LINE_BUFFER where TEXT starts. END is
2118 the offset from RL_LINE_BUFFER where TEXT ends (i.e., where
2119 rl_point is).
2120
2121 You should thus pretend that the line ends at END (relative to
2122 RL_LINE_BUFFER).
2123
2124 RL_LINE_BUFFER contains the entire text of the line. RL_POINT is
2125 the offset in that line of the cursor. You should pretend that the
2126 line ends at POINT.
2127
2128 Returns NULL if there are no completions. */
2129
2130static char **
2131gdb_rl_attempted_completion_function_throw (const char *text, int start, int end)
2132{
c6756f62
PA
2133 /* Completers that provide a custom word point in the
2134 handle_brkchars phase also compute their completions then.
2135 Completers that leave the completion word handling to readline
2136 must be called twice. If rl_point (i.e., END) is at column 0,
2137 then readline skips the handle_brkchars phase, and so we create a
2138 tracker now in that case too. */
2139 if (end == 0 || !current_completion.tracker->use_custom_word_point ())
2140 {
2141 delete current_completion.tracker;
2142 current_completion.tracker = new completion_tracker ();
eb3ff9a5 2143
c6756f62
PA
2144 complete_line (*current_completion.tracker, text,
2145 rl_line_buffer, rl_point);
2146 }
c5f0f3d0 2147
eb3ff9a5
PA
2148 completion_tracker &tracker = *current_completion.tracker;
2149
2150 completion_result result
2151 = tracker.build_completion_result (text, start, end);
2152
2153 rl_completion_suppress_append = result.completion_suppress_append;
2154 return result.release_match_list ();
2155}
2156
2157/* Function installed as "rl_attempted_completion_function" readline
2158 hook. Wrapper around gdb_rl_attempted_completion_function_throw
2159 that catches C++ exceptions, which can't cross readline. */
2160
2161char **
2162gdb_rl_attempted_completion_function (const char *text, int start, int end)
2163{
c6756f62
PA
2164 /* Restore globals that might have been tweaked in
2165 gdb_completion_word_break_characters. */
2166 rl_basic_quote_characters = gdb_org_rl_basic_quote_characters;
2167
eb3ff9a5
PA
2168 /* If we end up returning NULL, either on error, or simple because
2169 there are no matches, inhibit readline's default filename
2170 completer. */
2171 rl_attempted_completion_over = 1;
2172
2173 /* If the handle_brkchars phase was aborted, don't try
2174 completing. */
2175 if (current_completion.aborted)
2176 return NULL;
2177
2178 TRY
2179 {
2180 return gdb_rl_attempted_completion_function_throw (text, start, end);
2181 }
2182 CATCH (ex, RETURN_MASK_ALL)
2183 {
2184 }
2185 END_CATCH
2186
2187 return NULL;
c5f0f3d0 2188}
4e87b832
KD
2189
2190/* Skip over the possibly quoted word STR (as defined by the quote
b021a221
MS
2191 characters QUOTECHARS and the word break characters BREAKCHARS).
2192 Returns pointer to the location after the "word". If either
2193 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
2194 completer. */
c5f0f3d0 2195
d7561cbb
KS
2196const char *
2197skip_quoted_chars (const char *str, const char *quotechars,
2198 const char *breakchars)
c5f0f3d0
FN
2199{
2200 char quote_char = '\0';
d7561cbb 2201 const char *scan;
c5f0f3d0 2202
4e87b832
KD
2203 if (quotechars == NULL)
2204 quotechars = gdb_completer_quote_characters;
2205
2206 if (breakchars == NULL)
51065942 2207 breakchars = current_language->la_word_break_characters();
4e87b832 2208
c5f0f3d0
FN
2209 for (scan = str; *scan != '\0'; scan++)
2210 {
2211 if (quote_char != '\0')
2212 {
9c3f90bd 2213 /* Ignore everything until the matching close quote char. */
c5f0f3d0
FN
2214 if (*scan == quote_char)
2215 {
9c3f90bd 2216 /* Found matching close quote. */
c5f0f3d0
FN
2217 scan++;
2218 break;
2219 }
2220 }
4e87b832 2221 else if (strchr (quotechars, *scan))
c5f0f3d0 2222 {
aff410f1 2223 /* Found start of a quoted string. */
c5f0f3d0
FN
2224 quote_char = *scan;
2225 }
4e87b832 2226 else if (strchr (breakchars, *scan))
c5f0f3d0
FN
2227 {
2228 break;
2229 }
2230 }
4e87b832 2231
c5f0f3d0
FN
2232 return (scan);
2233}
2234
4e87b832
KD
2235/* Skip over the possibly quoted word STR (as defined by the quote
2236 characters and word break characters used by the completer).
9c3f90bd 2237 Returns pointer to the location after the "word". */
4e87b832 2238
d7561cbb
KS
2239const char *
2240skip_quoted (const char *str)
4e87b832
KD
2241{
2242 return skip_quoted_chars (str, NULL, NULL);
2243}
ef0b411a
GB
2244
2245/* Return a message indicating that the maximum number of completions
2246 has been reached and that there may be more. */
2247
2248const char *
2249get_max_completions_reached_message (void)
2250{
2251 return _("*** List may be truncated, max-completions reached. ***");
2252}
82083d6d
DE
2253\f
2254/* GDB replacement for rl_display_match_list.
2255 Readline doesn't provide a clean interface for TUI(curses).
2256 A hack previously used was to send readline's rl_outstream through a pipe
2257 and read it from the event loop. Bleah. IWBN if readline abstracted
2258 away all the necessary bits, and this is what this code does. It
2259 replicates the parts of readline we need and then adds an abstraction
2260 layer, currently implemented as struct match_list_displayer, so that both
2261 CLI and TUI can use it. We copy all this readline code to minimize
2262 GDB-specific mods to readline. Once this code performs as desired then
2263 we can submit it to the readline maintainers.
2264
2265 N.B. A lot of the code is the way it is in order to minimize differences
2266 from readline's copy. */
2267
2268/* Not supported here. */
2269#undef VISIBLE_STATS
2270
2271#if defined (HANDLE_MULTIBYTE)
2272#define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
2273#define MB_NULLWCH(x) ((x) == 0)
2274#endif
2275
2276#define ELLIPSIS_LEN 3
2277
2278/* gdb version of readline/complete.c:get_y_or_n.
2279 'y' -> returns 1, and 'n' -> returns 0.
2280 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
2281 If FOR_PAGER is non-zero, then also supported are:
2282 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
2283
2284static int
2285gdb_get_y_or_n (int for_pager, const struct match_list_displayer *displayer)
2286{
2287 int c;
2288
2289 for (;;)
2290 {
2291 RL_SETSTATE (RL_STATE_MOREINPUT);
2292 c = displayer->read_key (displayer);
2293 RL_UNSETSTATE (RL_STATE_MOREINPUT);
2294
2295 if (c == 'y' || c == 'Y' || c == ' ')
2296 return 1;
2297 if (c == 'n' || c == 'N' || c == RUBOUT)
2298 return 0;
2299 if (c == ABORT_CHAR || c < 0)
2300 {
2301 /* Readline doesn't erase_entire_line here, but without it the
2302 --More-- prompt isn't erased and neither is the text entered
2303 thus far redisplayed. */
2304 displayer->erase_entire_line (displayer);
2305 /* Note: The arguments to rl_abort are ignored. */
2306 rl_abort (0, 0);
2307 }
2308 if (for_pager && (c == NEWLINE || c == RETURN))
2309 return 2;
2310 if (for_pager && (c == 'q' || c == 'Q'))
2311 return 0;
2312 displayer->beep (displayer);
2313 }
2314}
2315
2316/* Pager function for tab-completion.
2317 This is based on readline/complete.c:_rl_internal_pager.
2318 LINES is the number of lines of output displayed thus far.
2319 Returns:
2320 -1 -> user pressed 'n' or equivalent,
2321 0 -> user pressed 'y' or equivalent,
2322 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
2323
2324static int
2325gdb_display_match_list_pager (int lines,
2326 const struct match_list_displayer *displayer)
2327{
2328 int i;
2329
2330 displayer->puts (displayer, "--More--");
2331 displayer->flush (displayer);
2332 i = gdb_get_y_or_n (1, displayer);
2333 displayer->erase_entire_line (displayer);
2334 if (i == 0)
2335 return -1;
2336 else if (i == 2)
2337 return (lines - 1);
2338 else
2339 return 0;
2340}
2341
2342/* Return non-zero if FILENAME is a directory.
2343 Based on readline/complete.c:path_isdir. */
2344
2345static int
2346gdb_path_isdir (const char *filename)
2347{
2348 struct stat finfo;
2349
2350 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
2351}
2352
2353/* Return the portion of PATHNAME that should be output when listing
2354 possible completions. If we are hacking filename completion, we
2355 are only interested in the basename, the portion following the
2356 final slash. Otherwise, we return what we were passed. Since
2357 printing empty strings is not very informative, if we're doing
2358 filename completion, and the basename is the empty string, we look
2359 for the previous slash and return the portion following that. If
2360 there's no previous slash, we just return what we were passed.
2361
2362 Based on readline/complete.c:printable_part. */
2363
2364static char *
2365gdb_printable_part (char *pathname)
2366{
2367 char *temp, *x;
2368
2369 if (rl_filename_completion_desired == 0) /* don't need to do anything */
2370 return (pathname);
2371
2372 temp = strrchr (pathname, '/');
5836a818 2373#if defined (__MSDOS__)
82083d6d
DE
2374 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
2375 temp = pathname + 1;
2376#endif
2377
2378 if (temp == 0 || *temp == '\0')
2379 return (pathname);
2380 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
2381 Look for a previous slash and, if one is found, return the portion
2382 following that slash. If there's no previous slash, just return the
2383 pathname we were passed. */
2384 else if (temp[1] == '\0')
2385 {
2386 for (x = temp - 1; x > pathname; x--)
2387 if (*x == '/')
2388 break;
2389 return ((*x == '/') ? x + 1 : pathname);
2390 }
2391 else
2392 return ++temp;
2393}
2394
2395/* Compute width of STRING when displayed on screen by print_filename.
2396 Based on readline/complete.c:fnwidth. */
2397
2398static int
2399gdb_fnwidth (const char *string)
2400{
2401 int width, pos;
2402#if defined (HANDLE_MULTIBYTE)
2403 mbstate_t ps;
2404 int left, w;
2405 size_t clen;
2406 wchar_t wc;
2407
2408 left = strlen (string) + 1;
2409 memset (&ps, 0, sizeof (mbstate_t));
2410#endif
2411
2412 width = pos = 0;
2413 while (string[pos])
2414 {
2415 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
2416 {
2417 width += 2;
2418 pos++;
2419 }
2420 else
2421 {
2422#if defined (HANDLE_MULTIBYTE)
2423 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
2424 if (MB_INVALIDCH (clen))
2425 {
2426 width++;
2427 pos++;
2428 memset (&ps, 0, sizeof (mbstate_t));
2429 }
2430 else if (MB_NULLWCH (clen))
2431 break;
2432 else
2433 {
2434 pos += clen;
2435 w = wcwidth (wc);
2436 width += (w >= 0) ? w : 1;
2437 }
2438#else
2439 width++;
2440 pos++;
2441#endif
2442 }
2443 }
2444
2445 return width;
2446}
2447
2448/* Print TO_PRINT, one matching completion.
2449 PREFIX_BYTES is number of common prefix bytes.
2450 Based on readline/complete.c:fnprint. */
2451
2452static int
2453gdb_fnprint (const char *to_print, int prefix_bytes,
2454 const struct match_list_displayer *displayer)
2455{
2456 int printed_len, w;
2457 const char *s;
2458#if defined (HANDLE_MULTIBYTE)
2459 mbstate_t ps;
2460 const char *end;
2461 size_t tlen;
2462 int width;
2463 wchar_t wc;
2464
2465 end = to_print + strlen (to_print) + 1;
2466 memset (&ps, 0, sizeof (mbstate_t));
2467#endif
2468
2469 printed_len = 0;
2470
2471 /* Don't print only the ellipsis if the common prefix is one of the
2472 possible completions */
2473 if (to_print[prefix_bytes] == '\0')
2474 prefix_bytes = 0;
2475
2476 if (prefix_bytes)
2477 {
2478 char ellipsis;
2479
2480 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
2481 for (w = 0; w < ELLIPSIS_LEN; w++)
2482 displayer->putch (displayer, ellipsis);
2483 printed_len = ELLIPSIS_LEN;
2484 }
2485
2486 s = to_print + prefix_bytes;
2487 while (*s)
2488 {
2489 if (CTRL_CHAR (*s))
2490 {
2491 displayer->putch (displayer, '^');
2492 displayer->putch (displayer, UNCTRL (*s));
2493 printed_len += 2;
2494 s++;
2495#if defined (HANDLE_MULTIBYTE)
2496 memset (&ps, 0, sizeof (mbstate_t));
2497#endif
2498 }
2499 else if (*s == RUBOUT)
2500 {
2501 displayer->putch (displayer, '^');
2502 displayer->putch (displayer, '?');
2503 printed_len += 2;
2504 s++;
2505#if defined (HANDLE_MULTIBYTE)
2506 memset (&ps, 0, sizeof (mbstate_t));
2507#endif
2508 }
2509 else
2510 {
2511#if defined (HANDLE_MULTIBYTE)
2512 tlen = mbrtowc (&wc, s, end - s, &ps);
2513 if (MB_INVALIDCH (tlen))
2514 {
2515 tlen = 1;
2516 width = 1;
2517 memset (&ps, 0, sizeof (mbstate_t));
2518 }
2519 else if (MB_NULLWCH (tlen))
2520 break;
2521 else
2522 {
2523 w = wcwidth (wc);
2524 width = (w >= 0) ? w : 1;
2525 }
2526 for (w = 0; w < tlen; ++w)
2527 displayer->putch (displayer, s[w]);
2528 s += tlen;
2529 printed_len += width;
2530#else
2531 displayer->putch (displayer, *s);
2532 s++;
2533 printed_len++;
2534#endif
2535 }
2536 }
2537
2538 return printed_len;
2539}
2540
2541/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
2542 are using it, check for and output a single character for `special'
2543 filenames. Return the number of characters we output.
2544 Based on readline/complete.c:print_filename. */
2545
2546static int
2547gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes,
2548 const struct match_list_displayer *displayer)
2549{
2550 int printed_len, extension_char, slen, tlen;
a121b7c1
PA
2551 char *s, c, *new_full_pathname;
2552 const char *dn;
82083d6d
DE
2553 extern int _rl_complete_mark_directories;
2554
2555 extension_char = 0;
2556 printed_len = gdb_fnprint (to_print, prefix_bytes, displayer);
2557
2558#if defined (VISIBLE_STATS)
2559 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
2560#else
2561 if (rl_filename_completion_desired && _rl_complete_mark_directories)
2562#endif
2563 {
2564 /* If to_print != full_pathname, to_print is the basename of the
2565 path passed. In this case, we try to expand the directory
2566 name before checking for the stat character. */
2567 if (to_print != full_pathname)
2568 {
2569 /* Terminate the directory name. */
2570 c = to_print[-1];
2571 to_print[-1] = '\0';
2572
2573 /* If setting the last slash in full_pathname to a NUL results in
2574 full_pathname being the empty string, we are trying to complete
2575 files in the root directory. If we pass a null string to the
2576 bash directory completion hook, for example, it will expand it
2577 to the current directory. We just want the `/'. */
2578 if (full_pathname == 0 || *full_pathname == 0)
2579 dn = "/";
2580 else if (full_pathname[0] != '/')
2581 dn = full_pathname;
2582 else if (full_pathname[1] == 0)
2583 dn = "//"; /* restore trailing slash to `//' */
2584 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
2585 dn = "/"; /* don't turn /// into // */
2586 else
2587 dn = full_pathname;
2588 s = tilde_expand (dn);
2589 if (rl_directory_completion_hook)
2590 (*rl_directory_completion_hook) (&s);
2591
2592 slen = strlen (s);
2593 tlen = strlen (to_print);
2594 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
2595 strcpy (new_full_pathname, s);
2596 if (s[slen - 1] == '/')
2597 slen--;
2598 else
2599 new_full_pathname[slen] = '/';
2600 new_full_pathname[slen] = '/';
2601 strcpy (new_full_pathname + slen + 1, to_print);
2602
2603#if defined (VISIBLE_STATS)
2604 if (rl_visible_stats)
2605 extension_char = stat_char (new_full_pathname);
2606 else
2607#endif
2608 if (gdb_path_isdir (new_full_pathname))
2609 extension_char = '/';
2610
2611 xfree (new_full_pathname);
2612 to_print[-1] = c;
2613 }
2614 else
2615 {
2616 s = tilde_expand (full_pathname);
2617#if defined (VISIBLE_STATS)
2618 if (rl_visible_stats)
2619 extension_char = stat_char (s);
2620 else
2621#endif
2622 if (gdb_path_isdir (s))
2623 extension_char = '/';
2624 }
2625
2626 xfree (s);
2627 if (extension_char)
2628 {
2629 displayer->putch (displayer, extension_char);
2630 printed_len++;
2631 }
2632 }
2633
2634 return printed_len;
2635}
2636
2637/* GDB version of readline/complete.c:complete_get_screenwidth. */
2638
2639static int
2640gdb_complete_get_screenwidth (const struct match_list_displayer *displayer)
2641{
2642 /* Readline has other stuff here which it's not clear we need. */
2643 return displayer->width;
2644}
2645
56000a98
PA
2646extern int _rl_completion_prefix_display_length;
2647extern int _rl_print_completions_horizontally;
2648
2649EXTERN_C int _rl_qsort_string_compare (const void *, const void *);
2650typedef int QSFUNC (const void *, const void *);
2651
82083d6d 2652/* GDB version of readline/complete.c:rl_display_match_list.
ef0b411a
GB
2653 See gdb_display_match_list for a description of MATCHES, LEN, MAX.
2654 Returns non-zero if all matches are displayed. */
82083d6d 2655
ef0b411a 2656static int
82083d6d
DE
2657gdb_display_match_list_1 (char **matches, int len, int max,
2658 const struct match_list_displayer *displayer)
2659{
2660 int count, limit, printed_len, lines, cols;
2661 int i, j, k, l, common_length, sind;
2662 char *temp, *t;
2663 int page_completions = displayer->height != INT_MAX && pagination_enabled;
82083d6d
DE
2664
2665 /* Find the length of the prefix common to all items: length as displayed
2666 characters (common_length) and as a byte index into the matches (sind) */
2667 common_length = sind = 0;
2668 if (_rl_completion_prefix_display_length > 0)
2669 {
2670 t = gdb_printable_part (matches[0]);
2671 temp = strrchr (t, '/');
2672 common_length = temp ? gdb_fnwidth (temp) : gdb_fnwidth (t);
2673 sind = temp ? strlen (temp) : strlen (t);
2674
2675 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
2676 max -= common_length - ELLIPSIS_LEN;
2677 else
2678 common_length = sind = 0;
2679 }
2680
2681 /* How many items of MAX length can we fit in the screen window? */
2682 cols = gdb_complete_get_screenwidth (displayer);
2683 max += 2;
2684 limit = cols / max;
2685 if (limit != 1 && (limit * max == cols))
2686 limit--;
2687
2688 /* If cols == 0, limit will end up -1 */
2689 if (cols < displayer->width && limit < 0)
2690 limit = 1;
2691
2692 /* Avoid a possible floating exception. If max > cols,
2693 limit will be 0 and a divide-by-zero fault will result. */
2694 if (limit == 0)
2695 limit = 1;
2696
2697 /* How many iterations of the printing loop? */
2698 count = (len + (limit - 1)) / limit;
2699
2700 /* Watch out for special case. If LEN is less than LIMIT, then
2701 just do the inner printing loop.
2702 0 < len <= limit implies count = 1. */
2703
2704 /* Sort the items if they are not already sorted. */
2705 if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
2706 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
2707
2708 displayer->crlf (displayer);
2709
2710 lines = 0;
2711 if (_rl_print_completions_horizontally == 0)
2712 {
2713 /* Print the sorted items, up-and-down alphabetically, like ls. */
2714 for (i = 1; i <= count; i++)
2715 {
2716 for (j = 0, l = i; j < limit; j++)
2717 {
2718 if (l > len || matches[l] == 0)
2719 break;
2720 else
2721 {
2722 temp = gdb_printable_part (matches[l]);
2723 printed_len = gdb_print_filename (temp, matches[l], sind,
2724 displayer);
2725
2726 if (j + 1 < limit)
2727 for (k = 0; k < max - printed_len; k++)
2728 displayer->putch (displayer, ' ');
2729 }
2730 l += count;
2731 }
2732 displayer->crlf (displayer);
2733 lines++;
2734 if (page_completions && lines >= (displayer->height - 1) && i < count)
2735 {
2736 lines = gdb_display_match_list_pager (lines, displayer);
2737 if (lines < 0)
ef0b411a 2738 return 0;
82083d6d
DE
2739 }
2740 }
2741 }
2742 else
2743 {
2744 /* Print the sorted items, across alphabetically, like ls -x. */
2745 for (i = 1; matches[i]; i++)
2746 {
2747 temp = gdb_printable_part (matches[i]);
2748 printed_len = gdb_print_filename (temp, matches[i], sind, displayer);
2749 /* Have we reached the end of this line? */
2750 if (matches[i+1])
2751 {
2752 if (i && (limit > 1) && (i % limit) == 0)
2753 {
2754 displayer->crlf (displayer);
2755 lines++;
2756 if (page_completions && lines >= displayer->height - 1)
2757 {
2758 lines = gdb_display_match_list_pager (lines, displayer);
2759 if (lines < 0)
ef0b411a 2760 return 0;
82083d6d
DE
2761 }
2762 }
2763 else
2764 for (k = 0; k < max - printed_len; k++)
2765 displayer->putch (displayer, ' ');
2766 }
2767 }
2768 displayer->crlf (displayer);
2769 }
ef0b411a
GB
2770
2771 return 1;
82083d6d
DE
2772}
2773
2774/* Utility for displaying completion list matches, used by both CLI and TUI.
2775
2776 MATCHES is the list of strings, in argv format, LEN is the number of
05cdcf3d
DE
2777 strings in MATCHES, and MAX is the length of the longest string in
2778 MATCHES. */
82083d6d
DE
2779
2780void
2781gdb_display_match_list (char **matches, int len, int max,
2782 const struct match_list_displayer *displayer)
2783{
ef0b411a
GB
2784 /* Readline will never call this if complete_line returned NULL. */
2785 gdb_assert (max_completions != 0);
2786
2787 /* complete_line will never return more than this. */
2788 if (max_completions > 0)
2789 gdb_assert (len <= max_completions);
2790
82083d6d
DE
2791 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
2792 {
2793 char msg[100];
2794
2795 /* We can't use *query here because they wait for <RET> which is
2796 wrong here. This follows the readline version as closely as possible
2797 for compatibility's sake. See readline/complete.c. */
2798
2799 displayer->crlf (displayer);
2800
2801 xsnprintf (msg, sizeof (msg),
2802 "Display all %d possibilities? (y or n)", len);
2803 displayer->puts (displayer, msg);
2804 displayer->flush (displayer);
2805
2806 if (gdb_get_y_or_n (0, displayer) == 0)
2807 {
2808 displayer->crlf (displayer);
2809 return;
2810 }
2811 }
2812
ef0b411a
GB
2813 if (gdb_display_match_list_1 (matches, len, max, displayer))
2814 {
2815 /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
2816 if (len == max_completions)
2817 {
2818 /* The maximum number of completions has been reached. Warn the user
2819 that there may be more. */
2820 const char *message = get_max_completions_reached_message ();
2821
2822 displayer->puts (displayer, message);
2823 displayer->crlf (displayer);
2824 }
2825 }
2826}
ef0b411a
GB
2827
2828void
2829_initialize_completer (void)
2830{
2831 add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class,
2832 &max_completions, _("\
2833Set maximum number of completion candidates."), _("\
2834Show maximum number of completion candidates."), _("\
2835Use this to limit the number of candidates considered\n\
2836during completion. Specifying \"unlimited\" or -1\n\
2837disables limiting. Note that setting either no limit or\n\
2838a very large limit can make completion slow."),
2839 NULL, NULL, &setlist, &showlist);
82083d6d 2840}
This page took 1.192988 seconds and 4 git commands to generate.