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