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