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