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