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