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