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