Unify CLI/TUI interface to readline tab completion.
[deliverable/binutils-gdb.git] / gdb / completer.c
CommitLineData
c5f0f3d0 1/* Line completion stuff for GDB, the GNU debugger.
32d0add0 2 Copyright (C) 2000-2015 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"
20#include "symtab.h"
21#include "gdbtypes.h"
22#include "expression.h"
9c3f90bd 23#include "filenames.h" /* For DOSish file names. */
51065942 24#include "language.h"
de0bea00 25#include "gdb_signals.h"
71c24708
AA
26#include "target.h"
27#include "reggroups.h"
28#include "user-regs.h"
c5f0f3d0 29
18a642a1
AC
30#include "cli/cli-decode.h"
31
03717487
MS
32/* FIXME: This is needed because of lookup_cmd_1 (). We should be
33 calling a hook instead so we eliminate the CLI dependency. */
c5f0f3d0
FN
34#include "gdbcmd.h"
35
c94fdfd0 36/* Needed for rl_completer_word_break_characters() and for
38017ce8 37 rl_filename_completion_function. */
dbda9972 38#include "readline/readline.h"
c5f0f3d0
FN
39
40/* readline defines this. */
41#undef savestring
42
43#include "completer.h"
44
9c3f90bd 45/* Prototypes for local functions. */
38017ce8 46static
03717487
MS
47char *line_completion_function (const char *text, int matches,
48 char *line_buffer,
d75b5104 49 int point);
c5f0f3d0
FN
50
51/* readline uses the word breaks for two things:
52 (1) In figuring out where to point the TEXT parameter to the
53 rl_completion_entry_function. Since we don't use TEXT for much,
aff410f1
MS
54 it doesn't matter a lot what the word breaks are for this purpose,
55 but it does affect how much stuff M-? lists.
c5f0f3d0
FN
56 (2) If one of the matches contains a word break character, readline
57 will quote it. That's why we switch between
51065942 58 current_language->la_word_break_characters() and
c5f0f3d0 59 gdb_completer_command_word_break_characters. I'm not sure when
aff410f1
MS
60 we need this behavior (perhaps for funky characters in C++
61 symbols?). */
c5f0f3d0
FN
62
63/* Variables which are necessary for fancy command line editing. */
c5f0f3d0
FN
64
65/* When completing on command names, we remove '-' from the list of
66 word break characters, since we use it in command names. If the
67 readline library sees one in any of the current completion strings,
aff410f1
MS
68 it thinks that the string needs to be quoted and automatically
69 supplies a leading quote. */
c5f0f3d0
FN
70static char *gdb_completer_command_word_break_characters =
71" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
72
73/* When completing on file names, we remove from the list of word
74 break characters any characters that are commonly used in file
75 names, such as '-', '+', '~', etc. Otherwise, readline displays
76 incorrect completion candidates. */
c3690141 77#ifdef HAVE_DOS_BASED_FILE_SYSTEM
7830cf6f
EZ
78/* MS-DOS and MS-Windows use colon as part of the drive spec, and most
79 programs support @foo style response files. */
80static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
81#else
82static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
83#endif
c5f0f3d0 84
aff410f1
MS
85/* Characters that can be used to quote completion strings. Note that
86 we can't include '"' because the gdb C parser treats such quoted
87 sequences as strings. */
c5f0f3d0
FN
88static char *gdb_completer_quote_characters = "'";
89\f
9c3f90bd 90/* Accessor for some completer data that may interest other files. */
c5f0f3d0 91
c5f0f3d0
FN
92char *
93get_gdb_completer_quote_characters (void)
94{
95 return gdb_completer_quote_characters;
96}
97
d75b5104
EZ
98/* Line completion interface function for readline. */
99
100char *
38017ce8 101readline_line_completion_function (const char *text, int matches)
d75b5104 102{
aff410f1
MS
103 return line_completion_function (text, matches,
104 rl_line_buffer, rl_point);
d75b5104
EZ
105}
106
aff410f1
MS
107/* This can be used for functions which don't want to complete on
108 symbols but don't want to complete on anything else either. */
49c4e619 109VEC (char_ptr) *
aff410f1 110noop_completer (struct cmd_list_element *ignore,
6f937416 111 const char *text, const char *prefix)
d75b5104
EZ
112{
113 return NULL;
114}
115
c5f0f3d0 116/* Complete on filenames. */
49c4e619 117VEC (char_ptr) *
aff410f1 118filename_completer (struct cmd_list_element *ignore,
6f937416 119 const char *text, const char *word)
c5f0f3d0 120{
c5f0f3d0 121 int subsequent_name;
49c4e619 122 VEC (char_ptr) *return_val = NULL;
c5f0f3d0
FN
123
124 subsequent_name = 0;
125 while (1)
126 {
1e8189fb 127 char *p, *q;
c5504eaf 128
38017ce8 129 p = rl_filename_completion_function (text, subsequent_name);
c5f0f3d0 130 if (p == NULL)
49c4e619 131 break;
c5f0f3d0 132 /* We need to set subsequent_name to a non-zero value before the
aff410f1
MS
133 continue line below, because otherwise, if the first file
134 seen by GDB is a backup file whose name ends in a `~', we
135 will loop indefinitely. */
c5f0f3d0 136 subsequent_name = 1;
aff410f1
MS
137 /* Like emacs, don't complete on old versions. Especially
138 useful in the "source" command. */
c5f0f3d0 139 if (p[strlen (p) - 1] == '~')
1e8189fb
MS
140 {
141 xfree (p);
142 continue;
143 }
c5f0f3d0 144
1e8189fb
MS
145 if (word == text)
146 /* Return exactly p. */
49c4e619 147 q = p;
1e8189fb
MS
148 else if (word > text)
149 {
150 /* Return some portion of p. */
151 q = xmalloc (strlen (p) + 5);
152 strcpy (q, p + (word - text));
1e8189fb
MS
153 xfree (p);
154 }
155 else
156 {
157 /* Return some of TEXT plus p. */
158 q = xmalloc (strlen (p) + (text - word) + 5);
159 strncpy (q, word, text - word);
160 q[text - word] = '\0';
161 strcat (q, p);
1e8189fb
MS
162 xfree (p);
163 }
49c4e619 164 VEC_safe_push (char_ptr, return_val, q);
c5f0f3d0
FN
165 }
166#if 0
aff410f1
MS
167 /* There is no way to do this just long enough to affect quote
168 inserting without also affecting the next completion. This
169 should be fixed in readline. FIXME. */
489f0516 170 /* Ensure that readline does the right thing
c5f0f3d0
FN
171 with respect to inserting quotes. */
172 rl_completer_word_break_characters = "";
173#endif
174 return return_val;
175}
176
c94fdfd0
EZ
177/* Complete on locations, which might be of two possible forms:
178
179 file:line
180 or
181 symbol+offset
182
aff410f1
MS
183 This is intended to be used in commands that set breakpoints
184 etc. */
185
49c4e619 186VEC (char_ptr) *
aff410f1 187location_completer (struct cmd_list_element *ignore,
6f937416 188 const char *text, const char *word)
c94fdfd0 189{
49c4e619
TT
190 int n_syms, n_files, ix;
191 VEC (char_ptr) *fn_list = NULL;
192 VEC (char_ptr) *list = NULL;
6f937416 193 const char *p;
c94fdfd0
EZ
194 int quote_found = 0;
195 int quoted = *text == '\'' || *text == '"';
196 int quote_char = '\0';
6f937416 197 const char *colon = NULL;
c94fdfd0 198 char *file_to_match = NULL;
6f937416
PA
199 const char *symbol_start = text;
200 const char *orig_text = text;
c94fdfd0
EZ
201 size_t text_len;
202
59be2b6a 203 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
c94fdfd0
EZ
204 for (p = text; *p != '\0'; ++p)
205 {
206 if (*p == '\\' && p[1] == '\'')
207 p++;
208 else if (*p == '\'' || *p == '"')
209 {
210 quote_found = *p;
211 quote_char = *p++;
212 while (*p != '\0' && *p != quote_found)
213 {
214 if (*p == '\\' && p[1] == quote_found)
215 p++;
216 p++;
217 }
218
219 if (*p == quote_found)
220 quote_found = 0;
221 else
9c3f90bd 222 break; /* Hit the end of text. */
c94fdfd0
EZ
223 }
224#if HAVE_DOS_BASED_FILE_SYSTEM
225 /* If we have a DOS-style absolute file name at the beginning of
226 TEXT, and the colon after the drive letter is the only colon
227 we found, pretend the colon is not there. */
228 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
229 ;
230#endif
231 else if (*p == ':' && !colon)
232 {
233 colon = p;
234 symbol_start = p + 1;
235 }
51065942 236 else if (strchr (current_language->la_word_break_characters(), *p))
c94fdfd0
EZ
237 symbol_start = p + 1;
238 }
239
240 if (quoted)
241 text++;
242 text_len = strlen (text);
243
244 /* Where is the file name? */
245 if (colon)
246 {
247 char *s;
248
249 file_to_match = (char *) xmalloc (colon - text + 1);
250 strncpy (file_to_match, text, colon - text + 1);
251 /* Remove trailing colons and quotes from the file name. */
252 for (s = file_to_match + (colon - text);
253 s > file_to_match;
254 s--)
255 if (*s == ':' || *s == quote_char)
256 *s = '\0';
257 }
258 /* If the text includes a colon, they want completion only on a
259 symbol name after the colon. Otherwise, we need to complete on
260 symbols as well as on files. */
261 if (colon)
262 {
263 list = make_file_symbol_completion_list (symbol_start, word,
264 file_to_match);
265 xfree (file_to_match);
266 }
267 else
268 {
269 list = make_symbol_completion_list (symbol_start, word);
270 /* If text includes characters which cannot appear in a file
271 name, they cannot be asking for completion on files. */
1f20ed91
MS
272 if (strcspn (text,
273 gdb_completer_file_name_break_characters) == text_len)
c94fdfd0
EZ
274 fn_list = make_source_files_completion_list (text, text);
275 }
276
49c4e619
TT
277 n_syms = VEC_length (char_ptr, list);
278 n_files = VEC_length (char_ptr, fn_list);
279
280 /* Catenate fn_list[] onto the end of list[]. */
281 if (!n_syms)
282 {
283 VEC_free (char_ptr, list); /* Paranoia. */
284 list = fn_list;
285 fn_list = NULL;
286 }
287 else
288 {
6f937416
PA
289 char *fn;
290
291 for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, fn); ++ix)
292 VEC_safe_push (char_ptr, list, fn);
49c4e619
TT
293 VEC_free (char_ptr, fn_list);
294 }
c94fdfd0 295
c94fdfd0
EZ
296 if (n_syms && n_files)
297 {
49c4e619 298 /* Nothing. */
c94fdfd0
EZ
299 }
300 else if (n_files)
301 {
6f937416
PA
302 char *fn;
303
c94fdfd0
EZ
304 /* If we only have file names as possible completion, we should
305 bring them in sync with what rl_complete expects. The
306 problem is that if the user types "break /foo/b TAB", and the
307 possible completions are "/foo/bar" and "/foo/baz"
308 rl_complete expects us to return "bar" and "baz", without the
309 leading directories, as possible completions, because `word'
310 starts at the "b". But we ignore the value of `word' when we
311 call make_source_files_completion_list above (because that
312 would not DTRT when the completion results in both symbols
313 and file names), so make_source_files_completion_list returns
314 the full "/foo/bar" and "/foo/baz" strings. This produces
315 wrong results when, e.g., there's only one possible
316 completion, because rl_complete will prepend "/foo/" to each
317 candidate completion. The loop below removes that leading
318 part. */
6f937416 319 for (ix = 0; VEC_iterate (char_ptr, list, ix, fn); ++ix)
c94fdfd0 320 {
6f937416
PA
321 memmove (fn, fn + (word - text),
322 strlen (fn) + 1 - (word - text));
c94fdfd0 323 }
c94fdfd0
EZ
324 }
325 else if (!n_syms)
326 {
327 /* No completions at all. As the final resort, try completing
328 on the entire text as a symbol. */
329 list = make_symbol_completion_list (orig_text, word);
330 }
331
332 return list;
333}
334
1c71341a
TT
335/* Helper for expression_completer which recursively adds field and
336 method names from TYPE, a struct or union type, to the array
49c4e619 337 OUTPUT. */
65d12d83 338static void
49c4e619 339add_struct_fields (struct type *type, VEC (char_ptr) **output,
65d12d83
TT
340 char *fieldname, int namelen)
341{
342 int i;
b32d97f3 343 int computed_type_name = 0;
0d5cff50 344 const char *type_name = NULL;
65d12d83
TT
345
346 CHECK_TYPEDEF (type);
347 for (i = 0; i < TYPE_NFIELDS (type); ++i)
348 {
349 if (i < TYPE_N_BASECLASSES (type))
49c4e619 350 add_struct_fields (TYPE_BASECLASS (type, i),
aff410f1 351 output, fieldname, namelen);
9ae8282d 352 else if (TYPE_FIELD_NAME (type, i))
65d12d83 353 {
9ae8282d
TT
354 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
355 {
aff410f1
MS
356 if (! strncmp (TYPE_FIELD_NAME (type, i),
357 fieldname, namelen))
49c4e619
TT
358 VEC_safe_push (char_ptr, *output,
359 xstrdup (TYPE_FIELD_NAME (type, i)));
9ae8282d
TT
360 }
361 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
362 {
363 /* Recurse into anonymous unions. */
49c4e619 364 add_struct_fields (TYPE_FIELD_TYPE (type, i),
aff410f1 365 output, fieldname, namelen);
9ae8282d 366 }
65d12d83
TT
367 }
368 }
1c71341a
TT
369
370 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
371 {
0d5cff50 372 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
c5504eaf 373
1c71341a
TT
374 if (name && ! strncmp (name, fieldname, namelen))
375 {
b32d97f3
TT
376 if (!computed_type_name)
377 {
378 type_name = type_name_no_tag (type);
379 computed_type_name = 1;
380 }
1c71341a 381 /* Omit constructors from the completion list. */
907af001 382 if (!type_name || strcmp (type_name, name))
49c4e619 383 VEC_safe_push (char_ptr, *output, xstrdup (name));
1c71341a
TT
384 }
385 }
65d12d83
TT
386}
387
388/* Complete on expressions. Often this means completing on symbol
389 names, but some language parsers also have support for completing
390 field names. */
49c4e619 391VEC (char_ptr) *
aff410f1 392expression_completer (struct cmd_list_element *ignore,
6f937416 393 const char *text, const char *word)
65d12d83 394{
c92817ce 395 struct type *type = NULL;
6f937416
PA
396 char *fieldname;
397 const char *p;
c92817ce 398 volatile struct gdb_exception except;
2f68a895 399 enum type_code code = TYPE_CODE_UNDEF;
65d12d83
TT
400
401 /* Perform a tentative parse of the expression, to see whether a
402 field completion is required. */
403 fieldname = NULL;
c92817ce
TT
404 TRY_CATCH (except, RETURN_MASK_ERROR)
405 {
2f68a895 406 type = parse_expression_for_completion (text, &fieldname, &code);
c92817ce
TT
407 }
408 if (except.reason < 0)
409 return NULL;
65d12d83
TT
410 if (fieldname && type)
411 {
412 for (;;)
413 {
414 CHECK_TYPEDEF (type);
415 if (TYPE_CODE (type) != TYPE_CODE_PTR
416 && TYPE_CODE (type) != TYPE_CODE_REF)
417 break;
418 type = TYPE_TARGET_TYPE (type);
419 }
420
421 if (TYPE_CODE (type) == TYPE_CODE_UNION
422 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
423 {
65d12d83 424 int flen = strlen (fieldname);
49c4e619 425 VEC (char_ptr) *result = NULL;
65d12d83 426
49c4e619 427 add_struct_fields (type, &result, fieldname, flen);
a0b7aece 428 xfree (fieldname);
65d12d83
TT
429 return result;
430 }
431 }
2f68a895
TT
432 else if (fieldname && code != TYPE_CODE_UNDEF)
433 {
434 VEC (char_ptr) *result;
435 struct cleanup *cleanup = make_cleanup (xfree, fieldname);
436
437 result = make_symbol_completion_type (fieldname, fieldname, code);
438 do_cleanups (cleanup);
439 return result;
440 }
a0b7aece 441 xfree (fieldname);
65d12d83 442
37cd5d19
TT
443 /* Commands which complete on locations want to see the entire
444 argument. */
445 for (p = word;
446 p > text && p[-1] != ' ' && p[-1] != '\t';
447 p--)
448 ;
449
aff410f1 450 /* Not ideal but it is what we used to do before... */
d8906c6f 451 return location_completer (ignore, p, word);
65d12d83
TT
452}
453
7d793aa9
SDJ
454/* See definition in completer.h. */
455
456void
457set_gdb_completion_word_break_characters (completer_ftype *fn)
458{
459 /* So far we are only interested in differentiating filename
460 completers from everything else. */
461 if (fn == filename_completer)
462 rl_completer_word_break_characters
463 = gdb_completer_file_name_break_characters;
464 else
465 rl_completer_word_break_characters
466 = gdb_completer_command_word_break_characters;
467}
468
aff410f1
MS
469/* Here are some useful test cases for completion. FIXME: These
470 should be put in the test suite. They should be tested with both
471 M-? and TAB.
c5f0f3d0
FN
472
473 "show output-" "radix"
474 "show output" "-radix"
475 "p" ambiguous (commands starting with p--path, print, printf, etc.)
476 "p " ambiguous (all symbols)
477 "info t foo" no completions
478 "info t " no completions
479 "info t" ambiguous ("info target", "info terminal", etc.)
480 "info ajksdlfk" no completions
481 "info ajksdlfk " no completions
482 "info" " "
483 "info " ambiguous (all info commands)
484 "p \"a" no completions (string constant)
485 "p 'a" ambiguous (all symbols starting with a)
486 "p b-a" ambiguous (all symbols starting with a)
487 "p b-" ambiguous (all symbols)
488 "file Make" "file" (word break hard to screw up here)
489 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
490 */
491
67c296a2
PM
492typedef enum
493{
494 handle_brkchars,
495 handle_completions,
496 handle_help
497}
498complete_line_internal_reason;
499
500
501/* Internal function used to handle completions.
502
c5f0f3d0
FN
503
504 TEXT is the caller's idea of the "word" we are looking at.
505
aff410f1
MS
506 LINE_BUFFER is available to be looked at; it contains the entire
507 text of the line. POINT is the offset in that line of the cursor.
508 You should pretend that the line ends at POINT.
67c296a2
PM
509
510 REASON is of type complete_line_internal_reason.
511
512 If REASON is handle_brkchars:
aff410f1
MS
513 Preliminary phase, called by gdb_completion_word_break_characters
514 function, is used to determine the correct set of chars that are
515 word delimiters depending on the current command in line_buffer.
516 No completion list should be generated; the return value should be
517 NULL. This is checked by an assertion in that function.
67c296a2
PM
518
519 If REASON is handle_completions:
520 Main phase, called by complete_line function, is used to get the list
521 of posible completions.
522
523 If REASON is handle_help:
524 Special case when completing a 'help' command. In this case,
14032a66 525 once sub-command completions are exhausted, we simply return NULL.
67c296a2 526 */
14032a66 527
49c4e619 528static VEC (char_ptr) *
aff410f1 529complete_line_internal (const char *text,
6f937416 530 const char *line_buffer, int point,
67c296a2 531 complete_line_internal_reason reason)
c5f0f3d0 532{
49c4e619 533 VEC (char_ptr) *list = NULL;
6f937416
PA
534 char *tmp_command;
535 const char *p;
ace21957 536 int ignore_help_classes;
c5f0f3d0
FN
537 /* Pointer within tmp_command which corresponds to text. */
538 char *word;
539 struct cmd_list_element *c, *result_list;
540
aff410f1
MS
541 /* Choose the default set of word break characters to break
542 completions. If we later find out that we are doing completions
543 on command strings (as opposed to strings supplied by the
544 individual command completer functions, which can be any string)
545 then we will switch to the special word break set for command
546 strings, which leaves out the '-' character used in some
547 commands. */
83d31a92 548 rl_completer_word_break_characters =
51065942 549 current_language->la_word_break_characters();
c5f0f3d0 550
aff410f1
MS
551 /* Decide whether to complete on a list of gdb commands or on
552 symbols. */
83d31a92
TT
553 tmp_command = (char *) alloca (point + 1);
554 p = tmp_command;
c5f0f3d0 555
ace21957
MF
556 /* The help command should complete help aliases. */
557 ignore_help_classes = reason != handle_help;
558
83d31a92
TT
559 strncpy (tmp_command, line_buffer, point);
560 tmp_command[point] = '\0';
561 /* Since text always contains some number of characters leading up
562 to point, we can find the equivalent position in tmp_command
563 by subtracting that many characters from the end of tmp_command. */
564 word = tmp_command + point - strlen (text);
c5f0f3d0 565
83d31a92
TT
566 if (point == 0)
567 {
568 /* An empty line we want to consider ambiguous; that is, it
569 could be any command. */
1427fe5e 570 c = CMD_LIST_AMBIGUOUS;
83d31a92
TT
571 result_list = 0;
572 }
573 else
574 {
ace21957 575 c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes);
83d31a92 576 }
c5f0f3d0 577
83d31a92
TT
578 /* Move p up to the next interesting thing. */
579 while (*p == ' ' || *p == '\t')
580 {
581 p++;
582 }
c5f0f3d0 583
83d31a92
TT
584 if (!c)
585 {
586 /* It is an unrecognized command. So there are no
587 possible completions. */
588 list = NULL;
589 }
1427fe5e 590 else if (c == CMD_LIST_AMBIGUOUS)
83d31a92 591 {
6f937416 592 const char *q;
83d31a92
TT
593
594 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
595 doesn't advance over that thing itself. Do so now. */
596 q = p;
597 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
598 ++q;
599 if (q != tmp_command + point)
c5f0f3d0 600 {
83d31a92
TT
601 /* There is something beyond the ambiguous
602 command, so there are no possible completions. For
603 example, "info t " or "info t foo" does not complete
604 to anything, because "info t" can be "info target" or
605 "info terminal". */
c5f0f3d0
FN
606 list = NULL;
607 }
83d31a92 608 else
c5f0f3d0 609 {
83d31a92
TT
610 /* We're trying to complete on the command which was ambiguous.
611 This we can deal with. */
612 if (result_list)
c5f0f3d0 613 {
67c296a2
PM
614 if (reason != handle_brkchars)
615 list = complete_on_cmdlist (*result_list->prefixlist, p,
ace21957 616 word, ignore_help_classes);
c5f0f3d0
FN
617 }
618 else
619 {
67c296a2 620 if (reason != handle_brkchars)
ace21957
MF
621 list = complete_on_cmdlist (cmdlist, p, word,
622 ignore_help_classes);
c5f0f3d0 623 }
489f0516 624 /* Ensure that readline does the right thing with respect to
83d31a92
TT
625 inserting quotes. */
626 rl_completer_word_break_characters =
627 gdb_completer_command_word_break_characters;
c5f0f3d0 628 }
83d31a92
TT
629 }
630 else
631 {
632 /* We've recognized a full command. */
633
634 if (p == tmp_command + point)
c5f0f3d0 635 {
aff410f1
MS
636 /* There is no non-whitespace in the line beyond the
637 command. */
c5f0f3d0 638
83d31a92 639 if (p[-1] == ' ' || p[-1] == '\t')
c5f0f3d0 640 {
aff410f1
MS
641 /* The command is followed by whitespace; we need to
642 complete on whatever comes after command. */
83d31a92 643 if (c->prefixlist)
c5f0f3d0 644 {
83d31a92
TT
645 /* It is a prefix command; what comes after it is
646 a subcommand (e.g. "info "). */
67c296a2 647 if (reason != handle_brkchars)
ace21957
MF
648 list = complete_on_cmdlist (*c->prefixlist, p, word,
649 ignore_help_classes);
c5f0f3d0 650
489f0516 651 /* Ensure that readline does the right thing
9c3f90bd 652 with respect to inserting quotes. */
c5f0f3d0
FN
653 rl_completer_word_break_characters =
654 gdb_completer_command_word_break_characters;
655 }
67c296a2 656 else if (reason == handle_help)
14032a66 657 list = NULL;
c5f0f3d0
FN
658 else if (c->enums)
659 {
67c296a2
PM
660 if (reason != handle_brkchars)
661 list = complete_on_enum (c->enums, p, word);
83d31a92
TT
662 rl_completer_word_break_characters =
663 gdb_completer_command_word_break_characters;
c5f0f3d0
FN
664 }
665 else
666 {
83d31a92
TT
667 /* It is a normal command; what comes after it is
668 completed by the command's completer function. */
c5f0f3d0 669 if (c->completer == filename_completer)
7830cf6f 670 {
83d31a92
TT
671 /* Many commands which want to complete on
672 file names accept several file names, as
673 in "run foo bar >>baz". So we don't want
674 to complete the entire text after the
675 command, just the last word. To this
676 end, we need to find the beginning of the
677 file name by starting at `word' and going
678 backwards. */
7830cf6f
EZ
679 for (p = word;
680 p > tmp_command
681 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
682 p--)
683 ;
684 rl_completer_word_break_characters =
685 gdb_completer_file_name_break_characters;
686 }
37cd5d19 687 else if (c->completer == location_completer)
c94fdfd0 688 {
83d31a92
TT
689 /* Commands which complete on locations want to
690 see the entire argument. */
c94fdfd0
EZ
691 for (p = word;
692 p > tmp_command
693 && p[-1] != ' ' && p[-1] != '\t';
694 p--)
695 ;
696 }
7d793aa9
SDJ
697 if (reason == handle_brkchars
698 && c->completer_handle_brkchars != NULL)
699 (*c->completer_handle_brkchars) (c, p, word);
2d9c5cff 700 if (reason != handle_brkchars && c->completer != NULL)
67c296a2 701 list = (*c->completer) (c, p, word);
c5f0f3d0
FN
702 }
703 }
83d31a92
TT
704 else
705 {
706 /* The command is not followed by whitespace; we need to
aff410f1 707 complete on the command itself, e.g. "p" which is a
83d31a92
TT
708 command itself but also can complete to "print", "ptype"
709 etc. */
6f937416 710 const char *q;
83d31a92
TT
711
712 /* Find the command we are completing on. */
713 q = p;
714 while (q > tmp_command)
715 {
716 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
717 --q;
718 else
719 break;
720 }
721
67c296a2 722 if (reason != handle_brkchars)
ace21957
MF
723 list = complete_on_cmdlist (result_list, q, word,
724 ignore_help_classes);
83d31a92 725
489f0516 726 /* Ensure that readline does the right thing
9c3f90bd 727 with respect to inserting quotes. */
83d31a92
TT
728 rl_completer_word_break_characters =
729 gdb_completer_command_word_break_characters;
730 }
731 }
67c296a2 732 else if (reason == handle_help)
14032a66 733 list = NULL;
83d31a92
TT
734 else
735 {
736 /* There is non-whitespace beyond the command. */
737
738 if (c->prefixlist && !c->allow_unknown)
739 {
740 /* It is an unrecognized subcommand of a prefix command,
741 e.g. "info adsfkdj". */
742 list = NULL;
743 }
744 else if (c->enums)
745 {
67c296a2
PM
746 if (reason != handle_brkchars)
747 list = complete_on_enum (c->enums, p, word);
83d31a92
TT
748 }
749 else
750 {
751 /* It is a normal command. */
752 if (c->completer == filename_completer)
753 {
754 /* See the commentary above about the specifics
755 of file-name completion. */
756 for (p = word;
757 p > tmp_command
aff410f1
MS
758 && strchr (gdb_completer_file_name_break_characters,
759 p[-1]) == NULL;
83d31a92
TT
760 p--)
761 ;
762 rl_completer_word_break_characters =
763 gdb_completer_file_name_break_characters;
764 }
37cd5d19 765 else if (c->completer == location_completer)
83d31a92
TT
766 {
767 for (p = word;
768 p > tmp_command
769 && p[-1] != ' ' && p[-1] != '\t';
770 p--)
771 ;
772 }
7d793aa9
SDJ
773 if (reason == handle_brkchars
774 && c->completer_handle_brkchars != NULL)
775 (*c->completer_handle_brkchars) (c, p, word);
2d9c5cff 776 if (reason != handle_brkchars && c->completer != NULL)
67c296a2 777 list = (*c->completer) (c, p, word);
83d31a92
TT
778 }
779 }
780 }
781
782 return list;
783}
49c4e619
TT
784/* Generate completions all at once. Returns a vector of strings.
785 Each element is allocated with xmalloc. It can also return NULL if
786 there are no completions.
83d31a92 787
67c296a2
PM
788 TEXT is the caller's idea of the "word" we are looking at.
789
aff410f1
MS
790 LINE_BUFFER is available to be looked at; it contains the entire
791 text of the line.
67c296a2
PM
792
793 POINT is the offset in that line of the cursor. You
794 should pretend that the line ends at POINT. */
14032a66 795
49c4e619 796VEC (char_ptr) *
1834676b 797complete_line (const char *text, const char *line_buffer, int point)
14032a66 798{
aff410f1
MS
799 return complete_line_internal (text, line_buffer,
800 point, handle_completions);
14032a66
TT
801}
802
803/* Complete on command names. Used by "help". */
49c4e619 804VEC (char_ptr) *
aff410f1 805command_completer (struct cmd_list_element *ignore,
6f937416 806 const char *text, const char *word)
14032a66 807{
aff410f1
MS
808 return complete_line_internal (word, text,
809 strlen (text), handle_help);
67c296a2
PM
810}
811
de0bea00
MF
812/* Complete on signals. */
813
814VEC (char_ptr) *
815signal_completer (struct cmd_list_element *ignore,
6f937416 816 const char *text, const char *word)
de0bea00 817{
de0bea00
MF
818 VEC (char_ptr) *return_val = NULL;
819 size_t len = strlen (word);
820 enum gdb_signal signum;
821 const char *signame;
822
823 for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
824 {
825 /* Can't handle this, so skip it. */
826 if (signum == GDB_SIGNAL_0)
827 continue;
828
829 signame = gdb_signal_to_name (signum);
830
831 /* Ignore the unknown signal case. */
832 if (!signame || strcmp (signame, "?") == 0)
833 continue;
834
835 if (strncasecmp (signame, word, len) == 0)
836 VEC_safe_push (char_ptr, return_val, xstrdup (signame));
837 }
838
839 return return_val;
840}
841
71c24708
AA
842/* Complete on a register or reggroup. */
843
844VEC (char_ptr) *
845reg_or_group_completer (struct cmd_list_element *ignore,
846 const char *text, const char *word)
847{
848 VEC (char_ptr) *result = NULL;
849 size_t len = strlen (word);
850 struct gdbarch *gdbarch;
851 struct reggroup *group;
852 const char *name;
853 int i;
854
855 if (!target_has_registers)
856 return result;
857
858 gdbarch = get_frame_arch (get_selected_frame (NULL));
859
860 for (i = 0;
861 (name = user_reg_map_regnum_to_name (gdbarch, i)) != NULL;
862 i++)
863 {
864 if (*name != '\0' && strncmp (word, name, len) == 0)
865 VEC_safe_push (char_ptr, result, xstrdup (name));
866 }
867
868 for (group = reggroup_next (gdbarch, NULL);
869 group != NULL;
870 group = reggroup_next (gdbarch, group))
871 {
872 name = reggroup_name (group);
873 if (strncmp (word, name, len) == 0)
874 VEC_safe_push (char_ptr, result, xstrdup (name));
875 }
876
877 return result;
878}
879
880
67c296a2
PM
881/* Get the list of chars that are considered as word breaks
882 for the current command. */
883
884char *
885gdb_completion_word_break_characters (void)
886{
49c4e619 887 VEC (char_ptr) *list;
c5504eaf 888
67c296a2
PM
889 list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
890 handle_brkchars);
891 gdb_assert (list == NULL);
892 return rl_completer_word_break_characters;
14032a66
TT
893}
894
aff410f1
MS
895/* Generate completions one by one for the completer. Each time we
896 are called return another potential completion to the caller.
897 line_completion just completes on commands or passes the buck to
898 the command's completer function, the stuff specific to symbol
899 completion is in make_symbol_completion_list.
83d31a92
TT
900
901 TEXT is the caller's idea of the "word" we are looking at.
902
aff410f1
MS
903 MATCHES is the number of matches that have currently been collected
904 from calling this completion function. When zero, then we need to
905 initialize, otherwise the initialization has already taken place
906 and we can just return the next potential completion string.
83d31a92 907
aff410f1
MS
908 LINE_BUFFER is available to be looked at; it contains the entire
909 text of the line. POINT is the offset in that line of the cursor.
910 You should pretend that the line ends at POINT.
83d31a92 911
aff410f1
MS
912 Returns NULL if there are no more completions, else a pointer to a
913 string which is a possible completion, it is the caller's
914 responsibility to free the string. */
83d31a92 915
38017ce8 916static char *
9c3f90bd
MS
917line_completion_function (const char *text, int matches,
918 char *line_buffer, int point)
83d31a92 919{
49c4e619 920 static VEC (char_ptr) *list = NULL; /* Cache of completions. */
9c3f90bd 921 static int index; /* Next cached completion. */
83d31a92
TT
922 char *output = NULL;
923
924 if (matches == 0)
925 {
aff410f1
MS
926 /* The caller is beginning to accumulate a new set of
927 completions, so we need to find all of them now, and cache
928 them for returning one at a time on future calls. */
83d31a92
TT
929
930 if (list)
931 {
aff410f1
MS
932 /* Free the storage used by LIST, but not by the strings
933 inside. This is because rl_complete_internal () frees
934 the strings. As complete_line may abort by calling
935 `error' clear LIST now. */
49c4e619 936 VEC_free (char_ptr, list);
c5f0f3d0 937 }
83d31a92
TT
938 index = 0;
939 list = complete_line (text, line_buffer, point);
c5f0f3d0
FN
940 }
941
aff410f1 942 /* If we found a list of potential completions during initialization
49c4e619
TT
943 then dole them out one at a time. After returning the last one,
944 return NULL (and continue to do so) each time we are called after
945 that, until a new list is available. */
c5f0f3d0
FN
946
947 if (list)
948 {
49c4e619 949 if (index < VEC_length (char_ptr, list))
c5f0f3d0 950 {
49c4e619 951 output = VEC_index (char_ptr, list, index);
c5f0f3d0
FN
952 index++;
953 }
954 }
955
956#if 0
957 /* Can't do this because readline hasn't yet checked the word breaks
958 for figuring out whether to insert a quote. */
959 if (output == NULL)
aff410f1
MS
960 /* Make sure the word break characters are set back to normal for
961 the next time that readline tries to complete something. */
c5f0f3d0 962 rl_completer_word_break_characters =
51065942 963 current_language->la_word_break_characters();
c5f0f3d0
FN
964#endif
965
966 return (output);
967}
4e87b832
KD
968
969/* Skip over the possibly quoted word STR (as defined by the quote
b021a221
MS
970 characters QUOTECHARS and the word break characters BREAKCHARS).
971 Returns pointer to the location after the "word". If either
972 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
973 completer. */
c5f0f3d0 974
d7561cbb
KS
975const char *
976skip_quoted_chars (const char *str, const char *quotechars,
977 const char *breakchars)
c5f0f3d0
FN
978{
979 char quote_char = '\0';
d7561cbb 980 const char *scan;
c5f0f3d0 981
4e87b832
KD
982 if (quotechars == NULL)
983 quotechars = gdb_completer_quote_characters;
984
985 if (breakchars == NULL)
51065942 986 breakchars = current_language->la_word_break_characters();
4e87b832 987
c5f0f3d0
FN
988 for (scan = str; *scan != '\0'; scan++)
989 {
990 if (quote_char != '\0')
991 {
9c3f90bd 992 /* Ignore everything until the matching close quote char. */
c5f0f3d0
FN
993 if (*scan == quote_char)
994 {
9c3f90bd 995 /* Found matching close quote. */
c5f0f3d0
FN
996 scan++;
997 break;
998 }
999 }
4e87b832 1000 else if (strchr (quotechars, *scan))
c5f0f3d0 1001 {
aff410f1 1002 /* Found start of a quoted string. */
c5f0f3d0
FN
1003 quote_char = *scan;
1004 }
4e87b832 1005 else if (strchr (breakchars, *scan))
c5f0f3d0
FN
1006 {
1007 break;
1008 }
1009 }
4e87b832 1010
c5f0f3d0
FN
1011 return (scan);
1012}
1013
4e87b832
KD
1014/* Skip over the possibly quoted word STR (as defined by the quote
1015 characters and word break characters used by the completer).
9c3f90bd 1016 Returns pointer to the location after the "word". */
4e87b832 1017
d7561cbb
KS
1018const char *
1019skip_quoted (const char *str)
4e87b832
KD
1020{
1021 return skip_quoted_chars (str, NULL, NULL);
1022}
82083d6d
DE
1023\f
1024/* GDB replacement for rl_display_match_list.
1025 Readline doesn't provide a clean interface for TUI(curses).
1026 A hack previously used was to send readline's rl_outstream through a pipe
1027 and read it from the event loop. Bleah. IWBN if readline abstracted
1028 away all the necessary bits, and this is what this code does. It
1029 replicates the parts of readline we need and then adds an abstraction
1030 layer, currently implemented as struct match_list_displayer, so that both
1031 CLI and TUI can use it. We copy all this readline code to minimize
1032 GDB-specific mods to readline. Once this code performs as desired then
1033 we can submit it to the readline maintainers.
1034
1035 N.B. A lot of the code is the way it is in order to minimize differences
1036 from readline's copy. */
1037
1038/* Not supported here. */
1039#undef VISIBLE_STATS
1040
1041#if defined (HANDLE_MULTIBYTE)
1042#define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
1043#define MB_NULLWCH(x) ((x) == 0)
1044#endif
1045
1046#define ELLIPSIS_LEN 3
1047
1048/* gdb version of readline/complete.c:get_y_or_n.
1049 'y' -> returns 1, and 'n' -> returns 0.
1050 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
1051 If FOR_PAGER is non-zero, then also supported are:
1052 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
1053
1054static int
1055gdb_get_y_or_n (int for_pager, const struct match_list_displayer *displayer)
1056{
1057 int c;
1058
1059 for (;;)
1060 {
1061 RL_SETSTATE (RL_STATE_MOREINPUT);
1062 c = displayer->read_key (displayer);
1063 RL_UNSETSTATE (RL_STATE_MOREINPUT);
1064
1065 if (c == 'y' || c == 'Y' || c == ' ')
1066 return 1;
1067 if (c == 'n' || c == 'N' || c == RUBOUT)
1068 return 0;
1069 if (c == ABORT_CHAR || c < 0)
1070 {
1071 /* Readline doesn't erase_entire_line here, but without it the
1072 --More-- prompt isn't erased and neither is the text entered
1073 thus far redisplayed. */
1074 displayer->erase_entire_line (displayer);
1075 /* Note: The arguments to rl_abort are ignored. */
1076 rl_abort (0, 0);
1077 }
1078 if (for_pager && (c == NEWLINE || c == RETURN))
1079 return 2;
1080 if (for_pager && (c == 'q' || c == 'Q'))
1081 return 0;
1082 displayer->beep (displayer);
1083 }
1084}
1085
1086/* Pager function for tab-completion.
1087 This is based on readline/complete.c:_rl_internal_pager.
1088 LINES is the number of lines of output displayed thus far.
1089 Returns:
1090 -1 -> user pressed 'n' or equivalent,
1091 0 -> user pressed 'y' or equivalent,
1092 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
1093
1094static int
1095gdb_display_match_list_pager (int lines,
1096 const struct match_list_displayer *displayer)
1097{
1098 int i;
1099
1100 displayer->puts (displayer, "--More--");
1101 displayer->flush (displayer);
1102 i = gdb_get_y_or_n (1, displayer);
1103 displayer->erase_entire_line (displayer);
1104 if (i == 0)
1105 return -1;
1106 else if (i == 2)
1107 return (lines - 1);
1108 else
1109 return 0;
1110}
1111
1112/* Return non-zero if FILENAME is a directory.
1113 Based on readline/complete.c:path_isdir. */
1114
1115static int
1116gdb_path_isdir (const char *filename)
1117{
1118 struct stat finfo;
1119
1120 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
1121}
1122
1123/* Return the portion of PATHNAME that should be output when listing
1124 possible completions. If we are hacking filename completion, we
1125 are only interested in the basename, the portion following the
1126 final slash. Otherwise, we return what we were passed. Since
1127 printing empty strings is not very informative, if we're doing
1128 filename completion, and the basename is the empty string, we look
1129 for the previous slash and return the portion following that. If
1130 there's no previous slash, we just return what we were passed.
1131
1132 Based on readline/complete.c:printable_part. */
1133
1134static char *
1135gdb_printable_part (char *pathname)
1136{
1137 char *temp, *x;
1138
1139 if (rl_filename_completion_desired == 0) /* don't need to do anything */
1140 return (pathname);
1141
1142 temp = strrchr (pathname, '/');
1143#if defined (__MSDOS__)
1144 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
1145 temp = pathname + 1;
1146#endif
1147
1148 if (temp == 0 || *temp == '\0')
1149 return (pathname);
1150 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
1151 Look for a previous slash and, if one is found, return the portion
1152 following that slash. If there's no previous slash, just return the
1153 pathname we were passed. */
1154 else if (temp[1] == '\0')
1155 {
1156 for (x = temp - 1; x > pathname; x--)
1157 if (*x == '/')
1158 break;
1159 return ((*x == '/') ? x + 1 : pathname);
1160 }
1161 else
1162 return ++temp;
1163}
1164
1165/* Compute width of STRING when displayed on screen by print_filename.
1166 Based on readline/complete.c:fnwidth. */
1167
1168static int
1169gdb_fnwidth (const char *string)
1170{
1171 int width, pos;
1172#if defined (HANDLE_MULTIBYTE)
1173 mbstate_t ps;
1174 int left, w;
1175 size_t clen;
1176 wchar_t wc;
1177
1178 left = strlen (string) + 1;
1179 memset (&ps, 0, sizeof (mbstate_t));
1180#endif
1181
1182 width = pos = 0;
1183 while (string[pos])
1184 {
1185 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
1186 {
1187 width += 2;
1188 pos++;
1189 }
1190 else
1191 {
1192#if defined (HANDLE_MULTIBYTE)
1193 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
1194 if (MB_INVALIDCH (clen))
1195 {
1196 width++;
1197 pos++;
1198 memset (&ps, 0, sizeof (mbstate_t));
1199 }
1200 else if (MB_NULLWCH (clen))
1201 break;
1202 else
1203 {
1204 pos += clen;
1205 w = wcwidth (wc);
1206 width += (w >= 0) ? w : 1;
1207 }
1208#else
1209 width++;
1210 pos++;
1211#endif
1212 }
1213 }
1214
1215 return width;
1216}
1217
1218/* Print TO_PRINT, one matching completion.
1219 PREFIX_BYTES is number of common prefix bytes.
1220 Based on readline/complete.c:fnprint. */
1221
1222static int
1223gdb_fnprint (const char *to_print, int prefix_bytes,
1224 const struct match_list_displayer *displayer)
1225{
1226 int printed_len, w;
1227 const char *s;
1228#if defined (HANDLE_MULTIBYTE)
1229 mbstate_t ps;
1230 const char *end;
1231 size_t tlen;
1232 int width;
1233 wchar_t wc;
1234
1235 end = to_print + strlen (to_print) + 1;
1236 memset (&ps, 0, sizeof (mbstate_t));
1237#endif
1238
1239 printed_len = 0;
1240
1241 /* Don't print only the ellipsis if the common prefix is one of the
1242 possible completions */
1243 if (to_print[prefix_bytes] == '\0')
1244 prefix_bytes = 0;
1245
1246 if (prefix_bytes)
1247 {
1248 char ellipsis;
1249
1250 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
1251 for (w = 0; w < ELLIPSIS_LEN; w++)
1252 displayer->putch (displayer, ellipsis);
1253 printed_len = ELLIPSIS_LEN;
1254 }
1255
1256 s = to_print + prefix_bytes;
1257 while (*s)
1258 {
1259 if (CTRL_CHAR (*s))
1260 {
1261 displayer->putch (displayer, '^');
1262 displayer->putch (displayer, UNCTRL (*s));
1263 printed_len += 2;
1264 s++;
1265#if defined (HANDLE_MULTIBYTE)
1266 memset (&ps, 0, sizeof (mbstate_t));
1267#endif
1268 }
1269 else if (*s == RUBOUT)
1270 {
1271 displayer->putch (displayer, '^');
1272 displayer->putch (displayer, '?');
1273 printed_len += 2;
1274 s++;
1275#if defined (HANDLE_MULTIBYTE)
1276 memset (&ps, 0, sizeof (mbstate_t));
1277#endif
1278 }
1279 else
1280 {
1281#if defined (HANDLE_MULTIBYTE)
1282 tlen = mbrtowc (&wc, s, end - s, &ps);
1283 if (MB_INVALIDCH (tlen))
1284 {
1285 tlen = 1;
1286 width = 1;
1287 memset (&ps, 0, sizeof (mbstate_t));
1288 }
1289 else if (MB_NULLWCH (tlen))
1290 break;
1291 else
1292 {
1293 w = wcwidth (wc);
1294 width = (w >= 0) ? w : 1;
1295 }
1296 for (w = 0; w < tlen; ++w)
1297 displayer->putch (displayer, s[w]);
1298 s += tlen;
1299 printed_len += width;
1300#else
1301 displayer->putch (displayer, *s);
1302 s++;
1303 printed_len++;
1304#endif
1305 }
1306 }
1307
1308 return printed_len;
1309}
1310
1311/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
1312 are using it, check for and output a single character for `special'
1313 filenames. Return the number of characters we output.
1314 Based on readline/complete.c:print_filename. */
1315
1316static int
1317gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes,
1318 const struct match_list_displayer *displayer)
1319{
1320 int printed_len, extension_char, slen, tlen;
1321 char *s, c, *new_full_pathname, *dn;
1322 extern int _rl_complete_mark_directories;
1323
1324 extension_char = 0;
1325 printed_len = gdb_fnprint (to_print, prefix_bytes, displayer);
1326
1327#if defined (VISIBLE_STATS)
1328 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
1329#else
1330 if (rl_filename_completion_desired && _rl_complete_mark_directories)
1331#endif
1332 {
1333 /* If to_print != full_pathname, to_print is the basename of the
1334 path passed. In this case, we try to expand the directory
1335 name before checking for the stat character. */
1336 if (to_print != full_pathname)
1337 {
1338 /* Terminate the directory name. */
1339 c = to_print[-1];
1340 to_print[-1] = '\0';
1341
1342 /* If setting the last slash in full_pathname to a NUL results in
1343 full_pathname being the empty string, we are trying to complete
1344 files in the root directory. If we pass a null string to the
1345 bash directory completion hook, for example, it will expand it
1346 to the current directory. We just want the `/'. */
1347 if (full_pathname == 0 || *full_pathname == 0)
1348 dn = "/";
1349 else if (full_pathname[0] != '/')
1350 dn = full_pathname;
1351 else if (full_pathname[1] == 0)
1352 dn = "//"; /* restore trailing slash to `//' */
1353 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
1354 dn = "/"; /* don't turn /// into // */
1355 else
1356 dn = full_pathname;
1357 s = tilde_expand (dn);
1358 if (rl_directory_completion_hook)
1359 (*rl_directory_completion_hook) (&s);
1360
1361 slen = strlen (s);
1362 tlen = strlen (to_print);
1363 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
1364 strcpy (new_full_pathname, s);
1365 if (s[slen - 1] == '/')
1366 slen--;
1367 else
1368 new_full_pathname[slen] = '/';
1369 new_full_pathname[slen] = '/';
1370 strcpy (new_full_pathname + slen + 1, to_print);
1371
1372#if defined (VISIBLE_STATS)
1373 if (rl_visible_stats)
1374 extension_char = stat_char (new_full_pathname);
1375 else
1376#endif
1377 if (gdb_path_isdir (new_full_pathname))
1378 extension_char = '/';
1379
1380 xfree (new_full_pathname);
1381 to_print[-1] = c;
1382 }
1383 else
1384 {
1385 s = tilde_expand (full_pathname);
1386#if defined (VISIBLE_STATS)
1387 if (rl_visible_stats)
1388 extension_char = stat_char (s);
1389 else
1390#endif
1391 if (gdb_path_isdir (s))
1392 extension_char = '/';
1393 }
1394
1395 xfree (s);
1396 if (extension_char)
1397 {
1398 displayer->putch (displayer, extension_char);
1399 printed_len++;
1400 }
1401 }
1402
1403 return printed_len;
1404}
1405
1406/* GDB version of readline/complete.c:complete_get_screenwidth. */
1407
1408static int
1409gdb_complete_get_screenwidth (const struct match_list_displayer *displayer)
1410{
1411 /* Readline has other stuff here which it's not clear we need. */
1412 return displayer->width;
1413}
1414
1415/* GDB version of readline/complete.c:rl_display_match_list.
1416 See gdb_display_match_list for a description of MATCHES, LEN, MAX. */
1417
1418static void
1419gdb_display_match_list_1 (char **matches, int len, int max,
1420 const struct match_list_displayer *displayer)
1421{
1422 int count, limit, printed_len, lines, cols;
1423 int i, j, k, l, common_length, sind;
1424 char *temp, *t;
1425 int page_completions = displayer->height != INT_MAX && pagination_enabled;
1426 extern int _rl_completion_prefix_display_length;
1427 extern int _rl_qsort_string_compare (const void *, const void *);
1428 extern int _rl_print_completions_horizontally;
1429 typedef int QSFUNC (const void *, const void *);
1430
1431 /* Find the length of the prefix common to all items: length as displayed
1432 characters (common_length) and as a byte index into the matches (sind) */
1433 common_length = sind = 0;
1434 if (_rl_completion_prefix_display_length > 0)
1435 {
1436 t = gdb_printable_part (matches[0]);
1437 temp = strrchr (t, '/');
1438 common_length = temp ? gdb_fnwidth (temp) : gdb_fnwidth (t);
1439 sind = temp ? strlen (temp) : strlen (t);
1440
1441 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
1442 max -= common_length - ELLIPSIS_LEN;
1443 else
1444 common_length = sind = 0;
1445 }
1446
1447 /* How many items of MAX length can we fit in the screen window? */
1448 cols = gdb_complete_get_screenwidth (displayer);
1449 max += 2;
1450 limit = cols / max;
1451 if (limit != 1 && (limit * max == cols))
1452 limit--;
1453
1454 /* If cols == 0, limit will end up -1 */
1455 if (cols < displayer->width && limit < 0)
1456 limit = 1;
1457
1458 /* Avoid a possible floating exception. If max > cols,
1459 limit will be 0 and a divide-by-zero fault will result. */
1460 if (limit == 0)
1461 limit = 1;
1462
1463 /* How many iterations of the printing loop? */
1464 count = (len + (limit - 1)) / limit;
1465
1466 /* Watch out for special case. If LEN is less than LIMIT, then
1467 just do the inner printing loop.
1468 0 < len <= limit implies count = 1. */
1469
1470 /* Sort the items if they are not already sorted. */
1471 if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
1472 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1473
1474 displayer->crlf (displayer);
1475
1476 lines = 0;
1477 if (_rl_print_completions_horizontally == 0)
1478 {
1479 /* Print the sorted items, up-and-down alphabetically, like ls. */
1480 for (i = 1; i <= count; i++)
1481 {
1482 for (j = 0, l = i; j < limit; j++)
1483 {
1484 if (l > len || matches[l] == 0)
1485 break;
1486 else
1487 {
1488 temp = gdb_printable_part (matches[l]);
1489 printed_len = gdb_print_filename (temp, matches[l], sind,
1490 displayer);
1491
1492 if (j + 1 < limit)
1493 for (k = 0; k < max - printed_len; k++)
1494 displayer->putch (displayer, ' ');
1495 }
1496 l += count;
1497 }
1498 displayer->crlf (displayer);
1499 lines++;
1500 if (page_completions && lines >= (displayer->height - 1) && i < count)
1501 {
1502 lines = gdb_display_match_list_pager (lines, displayer);
1503 if (lines < 0)
1504 return;
1505 }
1506 }
1507 }
1508 else
1509 {
1510 /* Print the sorted items, across alphabetically, like ls -x. */
1511 for (i = 1; matches[i]; i++)
1512 {
1513 temp = gdb_printable_part (matches[i]);
1514 printed_len = gdb_print_filename (temp, matches[i], sind, displayer);
1515 /* Have we reached the end of this line? */
1516 if (matches[i+1])
1517 {
1518 if (i && (limit > 1) && (i % limit) == 0)
1519 {
1520 displayer->crlf (displayer);
1521 lines++;
1522 if (page_completions && lines >= displayer->height - 1)
1523 {
1524 lines = gdb_display_match_list_pager (lines, displayer);
1525 if (lines < 0)
1526 return;
1527 }
1528 }
1529 else
1530 for (k = 0; k < max - printed_len; k++)
1531 displayer->putch (displayer, ' ');
1532 }
1533 }
1534 displayer->crlf (displayer);
1535 }
1536}
1537
1538/* Utility for displaying completion list matches, used by both CLI and TUI.
1539
1540 MATCHES is the list of strings, in argv format, LEN is the number of
1541 strings in MATCHES, and MAX is the length of the longest string in MATCHES.
1542
1543 This function handles the LIST_MAYBE_TRUNCATED marker that we add to the
1544 completion list.
1545
1546 Note: While LIST_MAYBE_TRUNCATED contributes to MAX, it's not long enough
1547 that we worry about it. */
1548
1549void
1550gdb_display_match_list (char **matches, int len, int max,
1551 const struct match_list_displayer *displayer)
1552{
1553 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
1554 {
1555 char msg[100];
1556
1557 /* We can't use *query here because they wait for <RET> which is
1558 wrong here. This follows the readline version as closely as possible
1559 for compatibility's sake. See readline/complete.c. */
1560
1561 displayer->crlf (displayer);
1562
1563 xsnprintf (msg, sizeof (msg),
1564 "Display all %d possibilities? (y or n)", len);
1565 displayer->puts (displayer, msg);
1566 displayer->flush (displayer);
1567
1568 if (gdb_get_y_or_n (0, displayer) == 0)
1569 {
1570 displayer->crlf (displayer);
1571 return;
1572 }
1573 }
1574
1575 gdb_display_match_list_1 (matches, len, max, displayer);
1576}
This page took 0.940788 seconds and 4 git commands to generate.