ppc-aix osabi sniffer: Turn test of bfd flavour into assertion
[deliverable/binutils-gdb.git] / gdb / completer.c
CommitLineData
c5f0f3d0 1/* Line completion stuff for GDB, the GNU debugger.
0b302171 2 Copyright (C) 2000-2001, 2007-2012 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"
67c296a2 25#include "gdb_assert.h"
c92817ce 26#include "exceptions.h"
de0bea00 27#include "gdb_signals.h"
c5f0f3d0 28
18a642a1
AC
29#include "cli/cli-decode.h"
30
03717487
MS
31/* FIXME: This is needed because of lookup_cmd_1 (). We should be
32 calling a hook instead so we eliminate the CLI dependency. */
c5f0f3d0
FN
33#include "gdbcmd.h"
34
c94fdfd0 35/* Needed for rl_completer_word_break_characters() and for
38017ce8 36 rl_filename_completion_function. */
dbda9972 37#include "readline/readline.h"
c5f0f3d0
FN
38
39/* readline defines this. */
40#undef savestring
41
42#include "completer.h"
43
9c3f90bd 44/* Prototypes for local functions. */
38017ce8 45static
03717487
MS
46char *line_completion_function (const char *text, int matches,
47 char *line_buffer,
d75b5104 48 int point);
c5f0f3d0
FN
49
50/* readline uses the word breaks for two things:
51 (1) In figuring out where to point the TEXT parameter to the
52 rl_completion_entry_function. Since we don't use TEXT for much,
aff410f1
MS
53 it doesn't matter a lot what the word breaks are for this purpose,
54 but it does affect how much stuff M-? lists.
c5f0f3d0
FN
55 (2) If one of the matches contains a word break character, readline
56 will quote it. That's why we switch between
51065942 57 current_language->la_word_break_characters() and
c5f0f3d0 58 gdb_completer_command_word_break_characters. I'm not sure when
aff410f1
MS
59 we need this behavior (perhaps for funky characters in C++
60 symbols?). */
c5f0f3d0
FN
61
62/* Variables which are necessary for fancy command line editing. */
c5f0f3d0
FN
63
64/* When completing on command names, we remove '-' from the list of
65 word break characters, since we use it in command names. If the
66 readline library sees one in any of the current completion strings,
aff410f1
MS
67 it thinks that the string needs to be quoted and automatically
68 supplies a leading quote. */
c5f0f3d0
FN
69static char *gdb_completer_command_word_break_characters =
70" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
71
72/* When completing on file names, we remove from the list of word
73 break characters any characters that are commonly used in file
74 names, such as '-', '+', '~', etc. Otherwise, readline displays
75 incorrect completion candidates. */
c3690141 76#ifdef HAVE_DOS_BASED_FILE_SYSTEM
7830cf6f
EZ
77/* MS-DOS and MS-Windows use colon as part of the drive spec, and most
78 programs support @foo style response files. */
79static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
80#else
81static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
82#endif
c5f0f3d0 83
aff410f1
MS
84/* Characters that can be used to quote completion strings. Note that
85 we can't include '"' because the gdb C parser treats such quoted
86 sequences as strings. */
c5f0f3d0
FN
87static char *gdb_completer_quote_characters = "'";
88\f
9c3f90bd 89/* Accessor for some completer data that may interest other files. */
c5f0f3d0 90
c5f0f3d0
FN
91char *
92get_gdb_completer_quote_characters (void)
93{
94 return gdb_completer_quote_characters;
95}
96
d75b5104
EZ
97/* Line completion interface function for readline. */
98
99char *
38017ce8 100readline_line_completion_function (const char *text, int matches)
d75b5104 101{
aff410f1
MS
102 return line_completion_function (text, matches,
103 rl_line_buffer, rl_point);
d75b5104
EZ
104}
105
aff410f1
MS
106/* This can be used for functions which don't want to complete on
107 symbols but don't want to complete on anything else either. */
49c4e619 108VEC (char_ptr) *
aff410f1
MS
109noop_completer (struct cmd_list_element *ignore,
110 char *text, char *prefix)
d75b5104
EZ
111{
112 return NULL;
113}
114
c5f0f3d0 115/* Complete on filenames. */
49c4e619 116VEC (char_ptr) *
aff410f1
MS
117filename_completer (struct cmd_list_element *ignore,
118 char *text, char *word)
c5f0f3d0 119{
c5f0f3d0 120 int subsequent_name;
49c4e619 121 VEC (char_ptr) *return_val = NULL;
c5f0f3d0
FN
122
123 subsequent_name = 0;
124 while (1)
125 {
1e8189fb 126 char *p, *q;
c5504eaf 127
38017ce8 128 p = rl_filename_completion_function (text, subsequent_name);
c5f0f3d0 129 if (p == NULL)
49c4e619 130 break;
c5f0f3d0 131 /* We need to set subsequent_name to a non-zero value before the
aff410f1
MS
132 continue line below, because otherwise, if the first file
133 seen by GDB is a backup file whose name ends in a `~', we
134 will loop indefinitely. */
c5f0f3d0 135 subsequent_name = 1;
aff410f1
MS
136 /* Like emacs, don't complete on old versions. Especially
137 useful in the "source" command. */
c5f0f3d0 138 if (p[strlen (p) - 1] == '~')
1e8189fb
MS
139 {
140 xfree (p);
141 continue;
142 }
c5f0f3d0 143
1e8189fb
MS
144 if (word == text)
145 /* Return exactly p. */
49c4e619 146 q = p;
1e8189fb
MS
147 else if (word > text)
148 {
149 /* Return some portion of p. */
150 q = xmalloc (strlen (p) + 5);
151 strcpy (q, p + (word - text));
1e8189fb
MS
152 xfree (p);
153 }
154 else
155 {
156 /* Return some of TEXT plus p. */
157 q = xmalloc (strlen (p) + (text - word) + 5);
158 strncpy (q, word, text - word);
159 q[text - word] = '\0';
160 strcat (q, p);
1e8189fb
MS
161 xfree (p);
162 }
49c4e619 163 VEC_safe_push (char_ptr, return_val, q);
c5f0f3d0
FN
164 }
165#if 0
aff410f1
MS
166 /* There is no way to do this just long enough to affect quote
167 inserting without also affecting the next completion. This
168 should be fixed in readline. FIXME. */
489f0516 169 /* Ensure that readline does the right thing
c5f0f3d0
FN
170 with respect to inserting quotes. */
171 rl_completer_word_break_characters = "";
172#endif
173 return return_val;
174}
175
c94fdfd0
EZ
176/* Complete on locations, which might be of two possible forms:
177
178 file:line
179 or
180 symbol+offset
181
aff410f1
MS
182 This is intended to be used in commands that set breakpoints
183 etc. */
184
49c4e619 185VEC (char_ptr) *
aff410f1
MS
186location_completer (struct cmd_list_element *ignore,
187 char *text, char *word)
c94fdfd0 188{
49c4e619
TT
189 int n_syms, n_files, ix;
190 VEC (char_ptr) *fn_list = NULL;
191 VEC (char_ptr) *list = NULL;
c94fdfd0
EZ
192 char *p;
193 int quote_found = 0;
194 int quoted = *text == '\'' || *text == '"';
195 int quote_char = '\0';
196 char *colon = NULL;
197 char *file_to_match = NULL;
198 char *symbol_start = text;
199 char *orig_text = text;
200 size_t text_len;
201
202 /* Do we have an unquoted colon, as in "break foo.c::bar"? */
203 for (p = text; *p != '\0'; ++p)
204 {
205 if (*p == '\\' && p[1] == '\'')
206 p++;
207 else if (*p == '\'' || *p == '"')
208 {
209 quote_found = *p;
210 quote_char = *p++;
211 while (*p != '\0' && *p != quote_found)
212 {
213 if (*p == '\\' && p[1] == quote_found)
214 p++;
215 p++;
216 }
217
218 if (*p == quote_found)
219 quote_found = 0;
220 else
9c3f90bd 221 break; /* Hit the end of text. */
c94fdfd0
EZ
222 }
223#if HAVE_DOS_BASED_FILE_SYSTEM
224 /* If we have a DOS-style absolute file name at the beginning of
225 TEXT, and the colon after the drive letter is the only colon
226 we found, pretend the colon is not there. */
227 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
228 ;
229#endif
230 else if (*p == ':' && !colon)
231 {
232 colon = p;
233 symbol_start = p + 1;
234 }
51065942 235 else if (strchr (current_language->la_word_break_characters(), *p))
c94fdfd0
EZ
236 symbol_start = p + 1;
237 }
238
239 if (quoted)
240 text++;
241 text_len = strlen (text);
242
243 /* Where is the file name? */
244 if (colon)
245 {
246 char *s;
247
248 file_to_match = (char *) xmalloc (colon - text + 1);
249 strncpy (file_to_match, text, colon - text + 1);
250 /* Remove trailing colons and quotes from the file name. */
251 for (s = file_to_match + (colon - text);
252 s > file_to_match;
253 s--)
254 if (*s == ':' || *s == quote_char)
255 *s = '\0';
256 }
257 /* If the text includes a colon, they want completion only on a
258 symbol name after the colon. Otherwise, we need to complete on
259 symbols as well as on files. */
260 if (colon)
261 {
262 list = make_file_symbol_completion_list (symbol_start, word,
263 file_to_match);
264 xfree (file_to_match);
265 }
266 else
267 {
268 list = make_symbol_completion_list (symbol_start, word);
269 /* If text includes characters which cannot appear in a file
270 name, they cannot be asking for completion on files. */
1f20ed91
MS
271 if (strcspn (text,
272 gdb_completer_file_name_break_characters) == text_len)
c94fdfd0
EZ
273 fn_list = make_source_files_completion_list (text, text);
274 }
275
49c4e619
TT
276 n_syms = VEC_length (char_ptr, list);
277 n_files = VEC_length (char_ptr, fn_list);
278
279 /* Catenate fn_list[] onto the end of list[]. */
280 if (!n_syms)
281 {
282 VEC_free (char_ptr, list); /* Paranoia. */
283 list = fn_list;
284 fn_list = NULL;
285 }
286 else
287 {
288 for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, p); ++ix)
289 VEC_safe_push (char_ptr, list, p);
290 VEC_free (char_ptr, fn_list);
291 }
c94fdfd0 292
c94fdfd0
EZ
293 if (n_syms && n_files)
294 {
49c4e619 295 /* Nothing. */
c94fdfd0
EZ
296 }
297 else if (n_files)
298 {
299 /* If we only have file names as possible completion, we should
300 bring them in sync with what rl_complete expects. The
301 problem is that if the user types "break /foo/b TAB", and the
302 possible completions are "/foo/bar" and "/foo/baz"
303 rl_complete expects us to return "bar" and "baz", without the
304 leading directories, as possible completions, because `word'
305 starts at the "b". But we ignore the value of `word' when we
306 call make_source_files_completion_list above (because that
307 would not DTRT when the completion results in both symbols
308 and file names), so make_source_files_completion_list returns
309 the full "/foo/bar" and "/foo/baz" strings. This produces
310 wrong results when, e.g., there's only one possible
311 completion, because rl_complete will prepend "/foo/" to each
312 candidate completion. The loop below removes that leading
313 part. */
49c4e619 314 for (ix = 0; VEC_iterate (char_ptr, list, ix, p); ++ix)
c94fdfd0 315 {
49c4e619
TT
316 memmove (p, p + (word - text),
317 strlen (p) + 1 - (word - text));
c94fdfd0 318 }
c94fdfd0
EZ
319 }
320 else if (!n_syms)
321 {
322 /* No completions at all. As the final resort, try completing
323 on the entire text as a symbol. */
324 list = make_symbol_completion_list (orig_text, word);
325 }
326
327 return list;
328}
329
65d12d83 330/* Helper for expression_completer which recursively counts the number
1c71341a 331 of named fields and methods in a structure or union type. */
65d12d83
TT
332static int
333count_struct_fields (struct type *type)
334{
335 int i, result = 0;
336
337 CHECK_TYPEDEF (type);
338 for (i = 0; i < TYPE_NFIELDS (type); ++i)
339 {
340 if (i < TYPE_N_BASECLASSES (type))
341 result += count_struct_fields (TYPE_BASECLASS (type, i));
342 else if (TYPE_FIELD_NAME (type, i))
9ae8282d
TT
343 {
344 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
345 ++result;
346 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
347 {
348 /* Recurse into anonymous unions. */
349 result += count_struct_fields (TYPE_FIELD_TYPE (type, i));
350 }
351 }
65d12d83 352 }
1c71341a
TT
353
354 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
355 {
356 if (TYPE_FN_FIELDLIST_NAME (type, i))
357 ++result;
358 }
359
65d12d83
TT
360 return result;
361}
362
1c71341a
TT
363/* Helper for expression_completer which recursively adds field and
364 method names from TYPE, a struct or union type, to the array
49c4e619 365 OUTPUT. */
65d12d83 366static void
49c4e619 367add_struct_fields (struct type *type, VEC (char_ptr) **output,
65d12d83
TT
368 char *fieldname, int namelen)
369{
370 int i;
b32d97f3 371 int computed_type_name = 0;
0d5cff50 372 const char *type_name = NULL;
65d12d83
TT
373
374 CHECK_TYPEDEF (type);
375 for (i = 0; i < TYPE_NFIELDS (type); ++i)
376 {
377 if (i < TYPE_N_BASECLASSES (type))
49c4e619 378 add_struct_fields (TYPE_BASECLASS (type, i),
aff410f1 379 output, fieldname, namelen);
9ae8282d 380 else if (TYPE_FIELD_NAME (type, i))
65d12d83 381 {
9ae8282d
TT
382 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
383 {
aff410f1
MS
384 if (! strncmp (TYPE_FIELD_NAME (type, i),
385 fieldname, namelen))
49c4e619
TT
386 VEC_safe_push (char_ptr, *output,
387 xstrdup (TYPE_FIELD_NAME (type, i)));
9ae8282d
TT
388 }
389 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
390 {
391 /* Recurse into anonymous unions. */
49c4e619 392 add_struct_fields (TYPE_FIELD_TYPE (type, i),
aff410f1 393 output, fieldname, namelen);
9ae8282d 394 }
65d12d83
TT
395 }
396 }
1c71341a
TT
397
398 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
399 {
0d5cff50 400 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
c5504eaf 401
1c71341a
TT
402 if (name && ! strncmp (name, fieldname, namelen))
403 {
b32d97f3
TT
404 if (!computed_type_name)
405 {
406 type_name = type_name_no_tag (type);
407 computed_type_name = 1;
408 }
1c71341a 409 /* Omit constructors from the completion list. */
907af001 410 if (!type_name || strcmp (type_name, name))
49c4e619 411 VEC_safe_push (char_ptr, *output, xstrdup (name));
1c71341a
TT
412 }
413 }
65d12d83
TT
414}
415
416/* Complete on expressions. Often this means completing on symbol
417 names, but some language parsers also have support for completing
418 field names. */
49c4e619 419VEC (char_ptr) *
aff410f1
MS
420expression_completer (struct cmd_list_element *ignore,
421 char *text, char *word)
65d12d83 422{
c92817ce 423 struct type *type = NULL;
37cd5d19 424 char *fieldname, *p;
c92817ce 425 volatile struct gdb_exception except;
65d12d83
TT
426
427 /* Perform a tentative parse of the expression, to see whether a
428 field completion is required. */
429 fieldname = NULL;
c92817ce
TT
430 TRY_CATCH (except, RETURN_MASK_ERROR)
431 {
432 type = parse_field_expression (text, &fieldname);
433 }
434 if (except.reason < 0)
435 return NULL;
65d12d83
TT
436 if (fieldname && type)
437 {
438 for (;;)
439 {
440 CHECK_TYPEDEF (type);
441 if (TYPE_CODE (type) != TYPE_CODE_PTR
442 && TYPE_CODE (type) != TYPE_CODE_REF)
443 break;
444 type = TYPE_TARGET_TYPE (type);
445 }
446
447 if (TYPE_CODE (type) == TYPE_CODE_UNION
448 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
449 {
450 int alloc = count_struct_fields (type);
451 int flen = strlen (fieldname);
49c4e619 452 VEC (char_ptr) *result = NULL;
65d12d83 453
49c4e619 454 add_struct_fields (type, &result, fieldname, flen);
a0b7aece 455 xfree (fieldname);
65d12d83
TT
456 return result;
457 }
458 }
a0b7aece 459 xfree (fieldname);
65d12d83 460
37cd5d19
TT
461 /* Commands which complete on locations want to see the entire
462 argument. */
463 for (p = word;
464 p > text && p[-1] != ' ' && p[-1] != '\t';
465 p--)
466 ;
467
aff410f1 468 /* Not ideal but it is what we used to do before... */
d8906c6f 469 return location_completer (ignore, p, word);
65d12d83
TT
470}
471
aff410f1
MS
472/* Here are some useful test cases for completion. FIXME: These
473 should be put in the test suite. They should be tested with both
474 M-? and TAB.
c5f0f3d0
FN
475
476 "show output-" "radix"
477 "show output" "-radix"
478 "p" ambiguous (commands starting with p--path, print, printf, etc.)
479 "p " ambiguous (all symbols)
480 "info t foo" no completions
481 "info t " no completions
482 "info t" ambiguous ("info target", "info terminal", etc.)
483 "info ajksdlfk" no completions
484 "info ajksdlfk " no completions
485 "info" " "
486 "info " ambiguous (all info commands)
487 "p \"a" no completions (string constant)
488 "p 'a" ambiguous (all symbols starting with a)
489 "p b-a" ambiguous (all symbols starting with a)
490 "p b-" ambiguous (all symbols)
491 "file Make" "file" (word break hard to screw up here)
492 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
493 */
494
67c296a2
PM
495typedef enum
496{
497 handle_brkchars,
498 handle_completions,
499 handle_help
500}
501complete_line_internal_reason;
502
503
504/* Internal function used to handle completions.
505
c5f0f3d0
FN
506
507 TEXT is the caller's idea of the "word" we are looking at.
508
aff410f1
MS
509 LINE_BUFFER is available to be looked at; it contains the entire
510 text of the line. POINT is the offset in that line of the cursor.
511 You should pretend that the line ends at POINT.
67c296a2
PM
512
513 REASON is of type complete_line_internal_reason.
514
515 If REASON is handle_brkchars:
aff410f1
MS
516 Preliminary phase, called by gdb_completion_word_break_characters
517 function, is used to determine the correct set of chars that are
518 word delimiters depending on the current command in line_buffer.
519 No completion list should be generated; the return value should be
520 NULL. This is checked by an assertion in that function.
67c296a2
PM
521
522 If REASON is handle_completions:
523 Main phase, called by complete_line function, is used to get the list
524 of posible completions.
525
526 If REASON is handle_help:
527 Special case when completing a 'help' command. In this case,
14032a66 528 once sub-command completions are exhausted, we simply return NULL.
67c296a2 529 */
14032a66 530
49c4e619 531static VEC (char_ptr) *
aff410f1
MS
532complete_line_internal (const char *text,
533 char *line_buffer, int point,
67c296a2 534 complete_line_internal_reason reason)
c5f0f3d0 535{
49c4e619 536 VEC (char_ptr) *list = NULL;
c5f0f3d0
FN
537 char *tmp_command, *p;
538 /* Pointer within tmp_command which corresponds to text. */
539 char *word;
540 struct cmd_list_element *c, *result_list;
541
aff410f1
MS
542 /* Choose the default set of word break characters to break
543 completions. If we later find out that we are doing completions
544 on command strings (as opposed to strings supplied by the
545 individual command completer functions, which can be any string)
546 then we will switch to the special word break set for command
547 strings, which leaves out the '-' character used in some
548 commands. */
83d31a92 549 rl_completer_word_break_characters =
51065942 550 current_language->la_word_break_characters();
c5f0f3d0 551
aff410f1
MS
552 /* Decide whether to complete on a list of gdb commands or on
553 symbols. */
83d31a92
TT
554 tmp_command = (char *) alloca (point + 1);
555 p = tmp_command;
c5f0f3d0 556
83d31a92
TT
557 strncpy (tmp_command, line_buffer, point);
558 tmp_command[point] = '\0';
559 /* Since text always contains some number of characters leading up
560 to point, we can find the equivalent position in tmp_command
561 by subtracting that many characters from the end of tmp_command. */
562 word = tmp_command + point - strlen (text);
c5f0f3d0 563
83d31a92
TT
564 if (point == 0)
565 {
566 /* An empty line we want to consider ambiguous; that is, it
567 could be any command. */
1427fe5e 568 c = CMD_LIST_AMBIGUOUS;
83d31a92
TT
569 result_list = 0;
570 }
571 else
572 {
573 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
574 }
c5f0f3d0 575
83d31a92
TT
576 /* Move p up to the next interesting thing. */
577 while (*p == ' ' || *p == '\t')
578 {
579 p++;
580 }
c5f0f3d0 581
83d31a92
TT
582 if (!c)
583 {
584 /* It is an unrecognized command. So there are no
585 possible completions. */
586 list = NULL;
587 }
1427fe5e 588 else if (c == CMD_LIST_AMBIGUOUS)
83d31a92
TT
589 {
590 char *q;
591
592 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
593 doesn't advance over that thing itself. Do so now. */
594 q = p;
595 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
596 ++q;
597 if (q != tmp_command + point)
c5f0f3d0 598 {
83d31a92
TT
599 /* There is something beyond the ambiguous
600 command, so there are no possible completions. For
601 example, "info t " or "info t foo" does not complete
602 to anything, because "info t" can be "info target" or
603 "info terminal". */
c5f0f3d0
FN
604 list = NULL;
605 }
83d31a92 606 else
c5f0f3d0 607 {
83d31a92
TT
608 /* We're trying to complete on the command which was ambiguous.
609 This we can deal with. */
610 if (result_list)
c5f0f3d0 611 {
67c296a2
PM
612 if (reason != handle_brkchars)
613 list = complete_on_cmdlist (*result_list->prefixlist, p,
614 word);
c5f0f3d0
FN
615 }
616 else
617 {
67c296a2
PM
618 if (reason != handle_brkchars)
619 list = complete_on_cmdlist (cmdlist, p, word);
c5f0f3d0 620 }
489f0516 621 /* Ensure that readline does the right thing with respect to
83d31a92
TT
622 inserting quotes. */
623 rl_completer_word_break_characters =
624 gdb_completer_command_word_break_characters;
c5f0f3d0 625 }
83d31a92
TT
626 }
627 else
628 {
629 /* We've recognized a full command. */
630
631 if (p == tmp_command + point)
c5f0f3d0 632 {
aff410f1
MS
633 /* There is no non-whitespace in the line beyond the
634 command. */
c5f0f3d0 635
83d31a92 636 if (p[-1] == ' ' || p[-1] == '\t')
c5f0f3d0 637 {
aff410f1
MS
638 /* The command is followed by whitespace; we need to
639 complete on whatever comes after command. */
83d31a92 640 if (c->prefixlist)
c5f0f3d0 641 {
83d31a92
TT
642 /* It is a prefix command; what comes after it is
643 a subcommand (e.g. "info "). */
67c296a2
PM
644 if (reason != handle_brkchars)
645 list = complete_on_cmdlist (*c->prefixlist, p, word);
c5f0f3d0 646
489f0516 647 /* Ensure that readline does the right thing
9c3f90bd 648 with respect to inserting quotes. */
c5f0f3d0
FN
649 rl_completer_word_break_characters =
650 gdb_completer_command_word_break_characters;
651 }
67c296a2 652 else if (reason == handle_help)
14032a66 653 list = NULL;
c5f0f3d0
FN
654 else if (c->enums)
655 {
67c296a2
PM
656 if (reason != handle_brkchars)
657 list = complete_on_enum (c->enums, p, word);
83d31a92
TT
658 rl_completer_word_break_characters =
659 gdb_completer_command_word_break_characters;
c5f0f3d0
FN
660 }
661 else
662 {
83d31a92
TT
663 /* It is a normal command; what comes after it is
664 completed by the command's completer function. */
c5f0f3d0 665 if (c->completer == filename_completer)
7830cf6f 666 {
83d31a92
TT
667 /* Many commands which want to complete on
668 file names accept several file names, as
669 in "run foo bar >>baz". So we don't want
670 to complete the entire text after the
671 command, just the last word. To this
672 end, we need to find the beginning of the
673 file name by starting at `word' and going
674 backwards. */
7830cf6f
EZ
675 for (p = word;
676 p > tmp_command
677 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
678 p--)
679 ;
680 rl_completer_word_break_characters =
681 gdb_completer_file_name_break_characters;
682 }
37cd5d19 683 else if (c->completer == location_completer)
c94fdfd0 684 {
83d31a92
TT
685 /* Commands which complete on locations want to
686 see the entire argument. */
c94fdfd0
EZ
687 for (p = word;
688 p > tmp_command
689 && p[-1] != ' ' && p[-1] != '\t';
690 p--)
691 ;
692 }
2d9c5cff 693 if (reason != handle_brkchars && c->completer != NULL)
67c296a2 694 list = (*c->completer) (c, p, word);
c5f0f3d0
FN
695 }
696 }
83d31a92
TT
697 else
698 {
699 /* The command is not followed by whitespace; we need to
aff410f1 700 complete on the command itself, e.g. "p" which is a
83d31a92
TT
701 command itself but also can complete to "print", "ptype"
702 etc. */
703 char *q;
704
705 /* Find the command we are completing on. */
706 q = p;
707 while (q > tmp_command)
708 {
709 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
710 --q;
711 else
712 break;
713 }
714
67c296a2
PM
715 if (reason != handle_brkchars)
716 list = complete_on_cmdlist (result_list, q, word);
83d31a92 717
489f0516 718 /* Ensure that readline does the right thing
9c3f90bd 719 with respect to inserting quotes. */
83d31a92
TT
720 rl_completer_word_break_characters =
721 gdb_completer_command_word_break_characters;
722 }
723 }
67c296a2 724 else if (reason == handle_help)
14032a66 725 list = NULL;
83d31a92
TT
726 else
727 {
728 /* There is non-whitespace beyond the command. */
729
730 if (c->prefixlist && !c->allow_unknown)
731 {
732 /* It is an unrecognized subcommand of a prefix command,
733 e.g. "info adsfkdj". */
734 list = NULL;
735 }
736 else if (c->enums)
737 {
67c296a2
PM
738 if (reason != handle_brkchars)
739 list = complete_on_enum (c->enums, p, word);
83d31a92
TT
740 }
741 else
742 {
743 /* It is a normal command. */
744 if (c->completer == filename_completer)
745 {
746 /* See the commentary above about the specifics
747 of file-name completion. */
748 for (p = word;
749 p > tmp_command
aff410f1
MS
750 && strchr (gdb_completer_file_name_break_characters,
751 p[-1]) == NULL;
83d31a92
TT
752 p--)
753 ;
754 rl_completer_word_break_characters =
755 gdb_completer_file_name_break_characters;
756 }
37cd5d19 757 else if (c->completer == location_completer)
83d31a92
TT
758 {
759 for (p = word;
760 p > tmp_command
761 && p[-1] != ' ' && p[-1] != '\t';
762 p--)
763 ;
764 }
2d9c5cff 765 if (reason != handle_brkchars && c->completer != NULL)
67c296a2 766 list = (*c->completer) (c, p, word);
83d31a92
TT
767 }
768 }
769 }
770
771 return list;
772}
49c4e619
TT
773/* Generate completions all at once. Returns a vector of strings.
774 Each element is allocated with xmalloc. It can also return NULL if
775 there are no completions.
83d31a92 776
67c296a2
PM
777 TEXT is the caller's idea of the "word" we are looking at.
778
aff410f1
MS
779 LINE_BUFFER is available to be looked at; it contains the entire
780 text of the line.
67c296a2
PM
781
782 POINT is the offset in that line of the cursor. You
783 should pretend that the line ends at POINT. */
14032a66 784
49c4e619 785VEC (char_ptr) *
14032a66
TT
786complete_line (const char *text, char *line_buffer, int point)
787{
aff410f1
MS
788 return complete_line_internal (text, line_buffer,
789 point, handle_completions);
14032a66
TT
790}
791
792/* Complete on command names. Used by "help". */
49c4e619 793VEC (char_ptr) *
aff410f1
MS
794command_completer (struct cmd_list_element *ignore,
795 char *text, char *word)
14032a66 796{
aff410f1
MS
797 return complete_line_internal (word, text,
798 strlen (text), handle_help);
67c296a2
PM
799}
800
de0bea00
MF
801/* Complete on signals. */
802
803VEC (char_ptr) *
804signal_completer (struct cmd_list_element *ignore,
805 char *text, char *word)
806{
807 int i;
808 VEC (char_ptr) *return_val = NULL;
809 size_t len = strlen (word);
810 enum gdb_signal signum;
811 const char *signame;
812
813 for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
814 {
815 /* Can't handle this, so skip it. */
816 if (signum == GDB_SIGNAL_0)
817 continue;
818
819 signame = gdb_signal_to_name (signum);
820
821 /* Ignore the unknown signal case. */
822 if (!signame || strcmp (signame, "?") == 0)
823 continue;
824
825 if (strncasecmp (signame, word, len) == 0)
826 VEC_safe_push (char_ptr, return_val, xstrdup (signame));
827 }
828
829 return return_val;
830}
831
67c296a2
PM
832/* Get the list of chars that are considered as word breaks
833 for the current command. */
834
835char *
836gdb_completion_word_break_characters (void)
837{
49c4e619 838 VEC (char_ptr) *list;
c5504eaf 839
67c296a2
PM
840 list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
841 handle_brkchars);
842 gdb_assert (list == NULL);
843 return rl_completer_word_break_characters;
14032a66
TT
844}
845
aff410f1
MS
846/* Generate completions one by one for the completer. Each time we
847 are called return another potential completion to the caller.
848 line_completion just completes on commands or passes the buck to
849 the command's completer function, the stuff specific to symbol
850 completion is in make_symbol_completion_list.
83d31a92
TT
851
852 TEXT is the caller's idea of the "word" we are looking at.
853
aff410f1
MS
854 MATCHES is the number of matches that have currently been collected
855 from calling this completion function. When zero, then we need to
856 initialize, otherwise the initialization has already taken place
857 and we can just return the next potential completion string.
83d31a92 858
aff410f1
MS
859 LINE_BUFFER is available to be looked at; it contains the entire
860 text of the line. POINT is the offset in that line of the cursor.
861 You should pretend that the line ends at POINT.
83d31a92 862
aff410f1
MS
863 Returns NULL if there are no more completions, else a pointer to a
864 string which is a possible completion, it is the caller's
865 responsibility to free the string. */
83d31a92 866
38017ce8 867static char *
9c3f90bd
MS
868line_completion_function (const char *text, int matches,
869 char *line_buffer, int point)
83d31a92 870{
49c4e619 871 static VEC (char_ptr) *list = NULL; /* Cache of completions. */
9c3f90bd 872 static int index; /* Next cached completion. */
83d31a92
TT
873 char *output = NULL;
874
875 if (matches == 0)
876 {
aff410f1
MS
877 /* The caller is beginning to accumulate a new set of
878 completions, so we need to find all of them now, and cache
879 them for returning one at a time on future calls. */
83d31a92
TT
880
881 if (list)
882 {
aff410f1
MS
883 /* Free the storage used by LIST, but not by the strings
884 inside. This is because rl_complete_internal () frees
885 the strings. As complete_line may abort by calling
886 `error' clear LIST now. */
49c4e619 887 VEC_free (char_ptr, list);
c5f0f3d0 888 }
83d31a92
TT
889 index = 0;
890 list = complete_line (text, line_buffer, point);
c5f0f3d0
FN
891 }
892
aff410f1 893 /* If we found a list of potential completions during initialization
49c4e619
TT
894 then dole them out one at a time. After returning the last one,
895 return NULL (and continue to do so) each time we are called after
896 that, until a new list is available. */
c5f0f3d0
FN
897
898 if (list)
899 {
49c4e619 900 if (index < VEC_length (char_ptr, list))
c5f0f3d0 901 {
49c4e619 902 output = VEC_index (char_ptr, list, index);
c5f0f3d0
FN
903 index++;
904 }
905 }
906
907#if 0
908 /* Can't do this because readline hasn't yet checked the word breaks
909 for figuring out whether to insert a quote. */
910 if (output == NULL)
aff410f1
MS
911 /* Make sure the word break characters are set back to normal for
912 the next time that readline tries to complete something. */
c5f0f3d0 913 rl_completer_word_break_characters =
51065942 914 current_language->la_word_break_characters();
c5f0f3d0
FN
915#endif
916
917 return (output);
918}
4e87b832
KD
919
920/* Skip over the possibly quoted word STR (as defined by the quote
b021a221
MS
921 characters QUOTECHARS and the word break characters BREAKCHARS).
922 Returns pointer to the location after the "word". If either
923 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
924 completer. */
c5f0f3d0
FN
925
926char *
4e87b832 927skip_quoted_chars (char *str, char *quotechars, char *breakchars)
c5f0f3d0
FN
928{
929 char quote_char = '\0';
930 char *scan;
931
4e87b832
KD
932 if (quotechars == NULL)
933 quotechars = gdb_completer_quote_characters;
934
935 if (breakchars == NULL)
51065942 936 breakchars = current_language->la_word_break_characters();
4e87b832 937
c5f0f3d0
FN
938 for (scan = str; *scan != '\0'; scan++)
939 {
940 if (quote_char != '\0')
941 {
9c3f90bd 942 /* Ignore everything until the matching close quote char. */
c5f0f3d0
FN
943 if (*scan == quote_char)
944 {
9c3f90bd 945 /* Found matching close quote. */
c5f0f3d0
FN
946 scan++;
947 break;
948 }
949 }
4e87b832 950 else if (strchr (quotechars, *scan))
c5f0f3d0 951 {
aff410f1 952 /* Found start of a quoted string. */
c5f0f3d0
FN
953 quote_char = *scan;
954 }
4e87b832 955 else if (strchr (breakchars, *scan))
c5f0f3d0
FN
956 {
957 break;
958 }
959 }
4e87b832 960
c5f0f3d0
FN
961 return (scan);
962}
963
4e87b832
KD
964/* Skip over the possibly quoted word STR (as defined by the quote
965 characters and word break characters used by the completer).
9c3f90bd 966 Returns pointer to the location after the "word". */
4e87b832
KD
967
968char *
969skip_quoted (char *str)
970{
971 return skip_quoted_chars (str, NULL, NULL);
972}
This page took 0.783424 seconds and 4 git commands to generate.