* gdbarch.sh (make_corefile_notes): New architecture callback.
[deliverable/binutils-gdb.git] / gdb / completer.c
1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000-2001, 2007-2012 Free Software Foundation, Inc.
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
8 the Free Software Foundation; either version 3 of the License, or
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
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include "symtab.h"
21 #include "gdbtypes.h"
22 #include "expression.h"
23 #include "filenames.h" /* For DOSish file names. */
24 #include "language.h"
25 #include "gdb_assert.h"
26 #include "exceptions.h"
27
28 #include "cli/cli-decode.h"
29
30 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
31 calling a hook instead so we eliminate the CLI dependency. */
32 #include "gdbcmd.h"
33
34 /* Needed for rl_completer_word_break_characters() and for
35 rl_filename_completion_function. */
36 #include "readline/readline.h"
37
38 /* readline defines this. */
39 #undef savestring
40
41 #include "completer.h"
42
43 /* Prototypes for local functions. */
44 static
45 char *line_completion_function (const char *text, int matches,
46 char *line_buffer,
47 int point);
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,
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.
54 (2) If one of the matches contains a word break character, readline
55 will quote it. That's why we switch between
56 current_language->la_word_break_characters() and
57 gdb_completer_command_word_break_characters. I'm not sure when
58 we need this behavior (perhaps for funky characters in C++
59 symbols?). */
60
61 /* Variables which are necessary for fancy command line editing. */
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,
66 it thinks that the string needs to be quoted and automatically
67 supplies a leading quote. */
68 static 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. */
75 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
76 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
77 programs support @foo style response files. */
78 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
79 #else
80 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
81 #endif
82
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. */
86 static char *gdb_completer_quote_characters = "'";
87 \f
88 /* Accessor for some completer data that may interest other files. */
89
90 char *
91 get_gdb_completer_quote_characters (void)
92 {
93 return gdb_completer_quote_characters;
94 }
95
96 /* Line completion interface function for readline. */
97
98 char *
99 readline_line_completion_function (const char *text, int matches)
100 {
101 return line_completion_function (text, matches,
102 rl_line_buffer, rl_point);
103 }
104
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. */
107 char **
108 noop_completer (struct cmd_list_element *ignore,
109 char *text, char *prefix)
110 {
111 return NULL;
112 }
113
114 /* Complete on filenames. */
115 char **
116 filename_completer (struct cmd_list_element *ignore,
117 char *text, char *word)
118 {
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 {
132 char *p, *q;
133
134 p = rl_filename_completion_function (text, subsequent_name);
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
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. */
151 subsequent_name = 1;
152 /* Like emacs, don't complete on old versions. Especially
153 useful in the "source" command. */
154 if (p[strlen (p) - 1] == '~')
155 {
156 xfree (p);
157 continue;
158 }
159
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 }
181 }
182 #if 0
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. */
186 /* Ensure that readline does the right thing
187 with respect to inserting quotes. */
188 rl_completer_word_break_characters = "";
189 #endif
190 return return_val;
191 }
192
193 /* Complete on locations, which might be of two possible forms:
194
195 file:line
196 or
197 symbol+offset
198
199 This is intended to be used in commands that set breakpoints
200 etc. */
201
202 char **
203 location_completer (struct cmd_list_element *ignore,
204 char *text, char *word)
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
238 break; /* Hit the end of text. */
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 }
252 else if (strchr (current_language->la_word_break_characters(), *p))
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. */
288 if (strcspn (text,
289 gdb_completer_file_name_break_characters) == text_len)
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);
339 xfree (fn_list);
340 }
341 else
342 xfree (fn_list);
343
344 return list;
345 }
346
347 /* Helper for expression_completer which recursively counts the number
348 of named fields and methods in a structure or union type. */
349 static int
350 count_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))
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 }
369 }
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
377 return result;
378 }
379
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. */
383 static void
384 add_struct_fields (struct type *type, int *nextp, char **output,
385 char *fieldname, int namelen)
386 {
387 int i;
388 int computed_type_name = 0;
389 char *type_name = NULL;
390
391 CHECK_TYPEDEF (type);
392 for (i = 0; i < TYPE_NFIELDS (type); ++i)
393 {
394 if (i < TYPE_N_BASECLASSES (type))
395 add_struct_fields (TYPE_BASECLASS (type, i), nextp,
396 output, fieldname, namelen);
397 else if (TYPE_FIELD_NAME (type, i))
398 {
399 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
400 {
401 if (! strncmp (TYPE_FIELD_NAME (type, i),
402 fieldname, namelen))
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. */
411 add_struct_fields (TYPE_FIELD_TYPE (type, i), nextp,
412 output, fieldname, namelen);
413 }
414 }
415 }
416
417 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
418 {
419 char *name = TYPE_FN_FIELDLIST_NAME (type, i);
420
421 if (name && ! strncmp (name, fieldname, namelen))
422 {
423 if (!computed_type_name)
424 {
425 type_name = type_name_no_tag (type);
426 computed_type_name = 1;
427 }
428 /* Omit constructors from the completion list. */
429 if (!type_name || strcmp (type_name, name))
430 {
431 output[*nextp] = xstrdup (name);
432 ++*nextp;
433 }
434 }
435 }
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. */
441 char **
442 expression_completer (struct cmd_list_element *ignore,
443 char *text, char *word)
444 {
445 struct type *type = NULL;
446 char *fieldname, *p;
447 volatile struct gdb_exception except;
448
449 /* Perform a tentative parse of the expression, to see whether a
450 field completion is required. */
451 fieldname = NULL;
452 TRY_CATCH (except, RETURN_MASK_ERROR)
453 {
454 type = parse_field_expression (text, &fieldname);
455 }
456 if (except.reason < 0)
457 return NULL;
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;
479 xfree (fieldname);
480 return result;
481 }
482 }
483 xfree (fieldname);
484
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
492 /* Not ideal but it is what we used to do before... */
493 return location_completer (ignore, p, word);
494 }
495
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.
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
519 typedef enum
520 {
521 handle_brkchars,
522 handle_completions,
523 handle_help
524 }
525 complete_line_internal_reason;
526
527
528 /* Internal function used to handle completions.
529
530
531 TEXT is the caller's idea of the "word" we are looking at.
532
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.
536
537 REASON is of type complete_line_internal_reason.
538
539 If REASON is handle_brkchars:
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.
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,
552 once sub-command completions are exhausted, we simply return NULL.
553 */
554
555 static char **
556 complete_line_internal (const char *text,
557 char *line_buffer, int point,
558 complete_line_internal_reason reason)
559 {
560 char **list = NULL;
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
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. */
573 rl_completer_word_break_characters =
574 current_language->la_word_break_characters();
575
576 /* Decide whether to complete on a list of gdb commands or on
577 symbols. */
578 tmp_command = (char *) alloca (point + 1);
579 p = tmp_command;
580
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);
587
588 if (point == 0)
589 {
590 /* An empty line we want to consider ambiguous; that is, it
591 could be any command. */
592 c = CMD_LIST_AMBIGUOUS;
593 result_list = 0;
594 }
595 else
596 {
597 c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
598 }
599
600 /* Move p up to the next interesting thing. */
601 while (*p == ' ' || *p == '\t')
602 {
603 p++;
604 }
605
606 if (!c)
607 {
608 /* It is an unrecognized command. So there are no
609 possible completions. */
610 list = NULL;
611 }
612 else if (c == CMD_LIST_AMBIGUOUS)
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)
622 {
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". */
628 list = NULL;
629 }
630 else
631 {
632 /* We're trying to complete on the command which was ambiguous.
633 This we can deal with. */
634 if (result_list)
635 {
636 if (reason != handle_brkchars)
637 list = complete_on_cmdlist (*result_list->prefixlist, p,
638 word);
639 }
640 else
641 {
642 if (reason != handle_brkchars)
643 list = complete_on_cmdlist (cmdlist, p, word);
644 }
645 /* Ensure that readline does the right thing with respect to
646 inserting quotes. */
647 rl_completer_word_break_characters =
648 gdb_completer_command_word_break_characters;
649 }
650 }
651 else
652 {
653 /* We've recognized a full command. */
654
655 if (p == tmp_command + point)
656 {
657 /* There is no non-whitespace in the line beyond the
658 command. */
659
660 if (p[-1] == ' ' || p[-1] == '\t')
661 {
662 /* The command is followed by whitespace; we need to
663 complete on whatever comes after command. */
664 if (c->prefixlist)
665 {
666 /* It is a prefix command; what comes after it is
667 a subcommand (e.g. "info "). */
668 if (reason != handle_brkchars)
669 list = complete_on_cmdlist (*c->prefixlist, p, word);
670
671 /* Ensure that readline does the right thing
672 with respect to inserting quotes. */
673 rl_completer_word_break_characters =
674 gdb_completer_command_word_break_characters;
675 }
676 else if (reason == handle_help)
677 list = NULL;
678 else if (c->enums)
679 {
680 if (reason != handle_brkchars)
681 list = complete_on_enum (c->enums, p, word);
682 rl_completer_word_break_characters =
683 gdb_completer_command_word_break_characters;
684 }
685 else
686 {
687 /* It is a normal command; what comes after it is
688 completed by the command's completer function. */
689 if (c->completer == filename_completer)
690 {
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. */
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 }
707 else if (c->completer == location_completer)
708 {
709 /* Commands which complete on locations want to
710 see the entire argument. */
711 for (p = word;
712 p > tmp_command
713 && p[-1] != ' ' && p[-1] != '\t';
714 p--)
715 ;
716 }
717 if (reason != handle_brkchars && c->completer != NULL)
718 list = (*c->completer) (c, p, word);
719 }
720 }
721 else
722 {
723 /* The command is not followed by whitespace; we need to
724 complete on the command itself, e.g. "p" which is a
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
739 if (reason != handle_brkchars)
740 list = complete_on_cmdlist (result_list, q, word);
741
742 /* Ensure that readline does the right thing
743 with respect to inserting quotes. */
744 rl_completer_word_break_characters =
745 gdb_completer_command_word_break_characters;
746 }
747 }
748 else if (reason == handle_help)
749 list = NULL;
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 {
762 if (reason != handle_brkchars)
763 list = complete_on_enum (c->enums, p, word);
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
774 && strchr (gdb_completer_file_name_break_characters,
775 p[-1]) == NULL;
776 p--)
777 ;
778 rl_completer_word_break_characters =
779 gdb_completer_file_name_break_characters;
780 }
781 else if (c->completer == location_completer)
782 {
783 for (p = word;
784 p > tmp_command
785 && p[-1] != ' ' && p[-1] != '\t';
786 p--)
787 ;
788 }
789 if (reason != handle_brkchars && c->completer != NULL)
790 list = (*c->completer) (c, p, word);
791 }
792 }
793 }
794
795 return list;
796 }
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.
800
801 TEXT is the caller's idea of the "word" we are looking at.
802
803 LINE_BUFFER is available to be looked at; it contains the entire
804 text of the line.
805
806 POINT is the offset in that line of the cursor. You
807 should pretend that the line ends at POINT. */
808
809 char **
810 complete_line (const char *text, char *line_buffer, int point)
811 {
812 return complete_line_internal (text, line_buffer,
813 point, handle_completions);
814 }
815
816 /* Complete on command names. Used by "help". */
817 char **
818 command_completer (struct cmd_list_element *ignore,
819 char *text, char *word)
820 {
821 return complete_line_internal (word, text,
822 strlen (text), handle_help);
823 }
824
825 /* Get the list of chars that are considered as word breaks
826 for the current command. */
827
828 char *
829 gdb_completion_word_break_characters (void)
830 {
831 char **list;
832
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;
837 }
838
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.
844
845 TEXT is the caller's idea of the "word" we are looking at.
846
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.
851
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.
855
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. */
859
860 static char *
861 line_completion_function (const char *text, int matches,
862 char *line_buffer, int point)
863 {
864 static char **list = (char **) NULL; /* Cache of completions. */
865 static int index; /* Next cached completion. */
866 char *output = NULL;
867
868 if (matches == 0)
869 {
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. */
873
874 if (list)
875 {
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. */
880 xfree (list);
881 list = NULL;
882 }
883 index = 0;
884 list = complete_line (text, line_buffer, point);
885 }
886
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. */
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)
906 /* Make sure the word break characters are set back to normal for
907 the next time that readline tries to complete something. */
908 rl_completer_word_break_characters =
909 current_language->la_word_break_characters();
910 #endif
911
912 return (output);
913 }
914
915 /* Skip over the possibly quoted word STR (as defined by the quote
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. */
920
921 char *
922 skip_quoted_chars (char *str, char *quotechars, char *breakchars)
923 {
924 char quote_char = '\0';
925 char *scan;
926
927 if (quotechars == NULL)
928 quotechars = gdb_completer_quote_characters;
929
930 if (breakchars == NULL)
931 breakchars = current_language->la_word_break_characters();
932
933 for (scan = str; *scan != '\0'; scan++)
934 {
935 if (quote_char != '\0')
936 {
937 /* Ignore everything until the matching close quote char. */
938 if (*scan == quote_char)
939 {
940 /* Found matching close quote. */
941 scan++;
942 break;
943 }
944 }
945 else if (strchr (quotechars, *scan))
946 {
947 /* Found start of a quoted string. */
948 quote_char = *scan;
949 }
950 else if (strchr (breakchars, *scan))
951 {
952 break;
953 }
954 }
955
956 return (scan);
957 }
958
959 /* Skip over the possibly quoted word STR (as defined by the quote
960 characters and word break characters used by the completer).
961 Returns pointer to the location after the "word". */
962
963 char *
964 skip_quoted (char *str)
965 {
966 return skip_quoted_chars (str, NULL, NULL);
967 }
This page took 0.073608 seconds and 4 git commands to generate.