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