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