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