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