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