gdb/completer.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Line completion stuff for GDB, the GNU debugger.
  2.    Copyright (C) 2000-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "filenames.h"                /* For DOSish file names.  */
  19. #include "language.h"
  20. #include "gdb_signals.h"
  21. #include "target.h"
  22. #include "reggroups.h"
  23. #include "user-regs.h"

  24. #include "cli/cli-decode.h"

  25. /* FIXME: This is needed because of lookup_cmd_1 ().  We should be
  26.    calling a hook instead so we eliminate the CLI dependency.  */
  27. #include "gdbcmd.h"

  28. /* Needed for rl_completer_word_break_characters() and for
  29.    rl_filename_completion_function.  */
  30. #include "readline/readline.h"

  31. /* readline defines this.  */
  32. #undef savestring

  33. #include "completer.h"

  34. /* Prototypes for local functions.  */
  35. static
  36. char *line_completion_function (const char *text, int matches,
  37.                                 char *line_buffer,
  38.                                 int point);

  39. /* readline uses the word breaks for two things:
  40.    (1) In figuring out where to point the TEXT parameter to the
  41.    rl_completion_entry_function.  Since we don't use TEXT for much,
  42.    it doesn't matter a lot what the word breaks are for this purpose,
  43.    but it does affect how much stuff M-? lists.
  44.    (2) If one of the matches contains a word break character, readline
  45.    will quote it.  That's why we switch between
  46.    current_language->la_word_break_characters() and
  47.    gdb_completer_command_word_break_charactersI'm not sure when
  48.    we need this behavior (perhaps for funky characters in C++
  49.    symbols?).  */

  50. /* Variables which are necessary for fancy command line editing.  */

  51. /* When completing on command names, we remove '-' from the list of
  52.    word break characters, since we use it in command names.  If the
  53.    readline library sees one in any of the current completion strings,
  54.    it thinks that the string needs to be quoted and automatically
  55.    supplies a leading quote.  */
  56. static char *gdb_completer_command_word_break_characters =
  57. " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";

  58. /* When completing on file names, we remove from the list of word
  59.    break characters any characters that are commonly used in file
  60.    names, such as '-', '+', '~', etc.  Otherwise, readline displays
  61.    incorrect completion candidates.  */
  62. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  63. /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
  64.    programs support @foo style response files.  */
  65. static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
  66. #else
  67. static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
  68. #endif

  69. /* Characters that can be used to quote completion strings.  Note that
  70.    we can't include '"' because the gdb C parser treats such quoted
  71.    sequences as strings.  */
  72. static char *gdb_completer_quote_characters = "'";

  73. /* Accessor for some completer data that may interest other files.  */

  74. char *
  75. get_gdb_completer_quote_characters (void)
  76. {
  77.   return gdb_completer_quote_characters;
  78. }

  79. /* Line completion interface function for readline.  */

  80. char *
  81. readline_line_completion_function (const char *text, int matches)
  82. {
  83.   return line_completion_function (text, matches,
  84.                                    rl_line_buffer, rl_point);
  85. }

  86. /* This can be used for functions which don't want to complete on
  87.    symbols but don't want to complete on anything else either.  */
  88. VEC (char_ptr) *
  89. noop_completer (struct cmd_list_element *ignore,
  90.                 const char *text, const char *prefix)
  91. {
  92.   return NULL;
  93. }

  94. /* Complete on filenames.  */
  95. VEC (char_ptr) *
  96. filename_completer (struct cmd_list_element *ignore,
  97.                     const char *text, const char *word)
  98. {
  99.   int subsequent_name;
  100.   VEC (char_ptr) *return_val = NULL;

  101.   subsequent_name = 0;
  102.   while (1)
  103.     {
  104.       char *p, *q;

  105.       p = rl_filename_completion_function (text, subsequent_name);
  106.       if (p == NULL)
  107.         break;
  108.       /* We need to set subsequent_name to a non-zero value before the
  109.          continue line below, because otherwise, if the first file
  110.          seen by GDB is a backup file whose name ends in a `~', we
  111.          will loop indefinitely.  */
  112.       subsequent_name = 1;
  113.       /* Like emacs, don't complete on old versions.  Especially
  114.          useful in the "source" command.  */
  115.       if (p[strlen (p) - 1] == '~')
  116.         {
  117.           xfree (p);
  118.           continue;
  119.         }

  120.       if (word == text)
  121.         /* Return exactly p.  */
  122.         q = p;
  123.       else if (word > text)
  124.         {
  125.           /* Return some portion of p.  */
  126.           q = xmalloc (strlen (p) + 5);
  127.           strcpy (q, p + (word - text));
  128.           xfree (p);
  129.         }
  130.       else
  131.         {
  132.           /* Return some of TEXT plus p.  */
  133.           q = xmalloc (strlen (p) + (text - word) + 5);
  134.           strncpy (q, word, text - word);
  135.           q[text - word] = '\0';
  136.           strcat (q, p);
  137.           xfree (p);
  138.         }
  139.       VEC_safe_push (char_ptr, return_val, q);
  140.     }
  141. #if 0
  142.   /* There is no way to do this just long enough to affect quote
  143.      inserting without also affecting the next completion.  This
  144.      should be fixed in readline.  FIXME.  */
  145.   /* Ensure that readline does the right thing
  146.      with respect to inserting quotes.  */
  147.   rl_completer_word_break_characters = "";
  148. #endif
  149.   return return_val;
  150. }

  151. /* Complete on locations, which might be of two possible forms:

  152.        file:line
  153.    or
  154.        symbol+offset

  155.    This is intended to be used in commands that set breakpoints
  156.    etc.  */

  157. VEC (char_ptr) *
  158. location_completer (struct cmd_list_element *ignore,
  159.                     const char *text, const char *word)
  160. {
  161.   int n_syms, n_files, ix;
  162.   VEC (char_ptr) *fn_list = NULL;
  163.   VEC (char_ptr) *list = NULL;
  164.   const char *p;
  165.   int quote_found = 0;
  166.   int quoted = *text == '\'' || *text == '"';
  167.   int quote_char = '\0';
  168.   const char *colon = NULL;
  169.   char *file_to_match = NULL;
  170.   const char *symbol_start = text;
  171.   const char *orig_text = text;
  172.   size_t text_len;

  173.   /* Do we have an unquoted colon, as in "break foo.c:bar"?  */
  174.   for (p = text; *p != '\0'; ++p)
  175.     {
  176.       if (*p == '\\' && p[1] == '\'')
  177.         p++;
  178.       else if (*p == '\'' || *p == '"')
  179.         {
  180.           quote_found = *p;
  181.           quote_char = *p++;
  182.           while (*p != '\0' && *p != quote_found)
  183.             {
  184.               if (*p == '\\' && p[1] == quote_found)
  185.                 p++;
  186.               p++;
  187.             }

  188.           if (*p == quote_found)
  189.             quote_found = 0;
  190.           else
  191.             break;                /* Hit the end of text.  */
  192.         }
  193. #if HAVE_DOS_BASED_FILE_SYSTEM
  194.       /* If we have a DOS-style absolute file name at the beginning of
  195.          TEXT, and the colon after the drive letter is the only colon
  196.          we found, pretend the colon is not there.  */
  197.       else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
  198.         ;
  199. #endif
  200.       else if (*p == ':' && !colon)
  201.         {
  202.           colon = p;
  203.           symbol_start = p + 1;
  204.         }
  205.       else if (strchr (current_language->la_word_break_characters(), *p))
  206.         symbol_start = p + 1;
  207.     }

  208.   if (quoted)
  209.     text++;
  210.   text_len = strlen (text);

  211.   /* Where is the file name?  */
  212.   if (colon)
  213.     {
  214.       char *s;

  215.       file_to_match = (char *) xmalloc (colon - text + 1);
  216.       strncpy (file_to_match, text, colon - text + 1);
  217.       /* Remove trailing colons and quotes from the file name.  */
  218.       for (s = file_to_match + (colon - text);
  219.            s > file_to_match;
  220.            s--)
  221.         if (*s == ':' || *s == quote_char)
  222.           *s = '\0';
  223.     }
  224.   /* If the text includes a colon, they want completion only on a
  225.      symbol name after the colon.  Otherwise, we need to complete on
  226.      symbols as well as on files.  */
  227.   if (colon)
  228.     {
  229.       list = make_file_symbol_completion_list (symbol_start, word,
  230.                                                file_to_match);
  231.       xfree (file_to_match);
  232.     }
  233.   else
  234.     {
  235.       list = make_symbol_completion_list (symbol_start, word);
  236.       /* If text includes characters which cannot appear in a file
  237.          name, they cannot be asking for completion on files.  */
  238.       if (strcspn (text,
  239.                    gdb_completer_file_name_break_characters) == text_len)
  240.         fn_list = make_source_files_completion_list (text, text);
  241.     }

  242.   n_syms = VEC_length (char_ptr, list);
  243.   n_files = VEC_length (char_ptr, fn_list);

  244.   /* Catenate fn_list[] onto the end of list[].  */
  245.   if (!n_syms)
  246.     {
  247.       VEC_free (char_ptr, list); /* Paranoia.  */
  248.       list = fn_list;
  249.       fn_list = NULL;
  250.     }
  251.   else
  252.     {
  253.       char *fn;

  254.       for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, fn); ++ix)
  255.         VEC_safe_push (char_ptr, list, fn);
  256.       VEC_free (char_ptr, fn_list);
  257.     }

  258.   if (n_syms && n_files)
  259.     {
  260.       /* Nothing.  */
  261.     }
  262.   else if (n_files)
  263.     {
  264.       char *fn;

  265.       /* If we only have file names as possible completion, we should
  266.          bring them in sync with what rl_complete expects.  The
  267.          problem is that if the user types "break /foo/b TAB", and the
  268.          possible completions are "/foo/bar" and "/foo/baz"
  269.          rl_complete expects us to return "bar" and "baz", without the
  270.          leading directories, as possible completions, because `word'
  271.          starts at the "b".  But we ignore the value of `word' when we
  272.          call make_source_files_completion_list above (because that
  273.          would not DTRT when the completion results in both symbols
  274.          and file names), so make_source_files_completion_list returns
  275.          the full "/foo/bar" and "/foo/baz" strings.  This produces
  276.          wrong results when, e.g., there's only one possible
  277.          completion, because rl_complete will prepend "/foo/" to each
  278.          candidate completion.  The loop below removes that leading
  279.          part.  */
  280.       for (ix = 0; VEC_iterate (char_ptr, list, ix, fn); ++ix)
  281.         {
  282.           memmove (fn, fn + (word - text),
  283.                    strlen (fn) + 1 - (word - text));
  284.         }
  285.     }
  286.   else if (!n_syms)
  287.     {
  288.       /* No completions at all.  As the final resort, try completing
  289.          on the entire text as a symbol.  */
  290.       list = make_symbol_completion_list (orig_text, word);
  291.     }

  292.   return list;
  293. }

  294. /* Helper for expression_completer which recursively adds field and
  295.    method names from TYPE, a struct or union type, to the array
  296.    OUTPUT.  */
  297. static void
  298. add_struct_fields (struct type *type, VEC (char_ptr) **output,
  299.                    char *fieldname, int namelen)
  300. {
  301.   int i;
  302.   int computed_type_name = 0;
  303.   const char *type_name = NULL;

  304.   CHECK_TYPEDEF (type);
  305.   for (i = 0; i < TYPE_NFIELDS (type); ++i)
  306.     {
  307.       if (i < TYPE_N_BASECLASSES (type))
  308.         add_struct_fields (TYPE_BASECLASS (type, i),
  309.                            output, fieldname, namelen);
  310.       else if (TYPE_FIELD_NAME (type, i))
  311.         {
  312.           if (TYPE_FIELD_NAME (type, i)[0] != '\0')
  313.             {
  314.               if (! strncmp (TYPE_FIELD_NAME (type, i),
  315.                              fieldname, namelen))
  316.                 VEC_safe_push (char_ptr, *output,
  317.                                xstrdup (TYPE_FIELD_NAME (type, i)));
  318.             }
  319.           else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
  320.             {
  321.               /* Recurse into anonymous unions.  */
  322.               add_struct_fields (TYPE_FIELD_TYPE (type, i),
  323.                                  output, fieldname, namelen);
  324.             }
  325.         }
  326.     }

  327.   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
  328.     {
  329.       const char *name = TYPE_FN_FIELDLIST_NAME (type, i);

  330.       if (name && ! strncmp (name, fieldname, namelen))
  331.         {
  332.           if (!computed_type_name)
  333.             {
  334.               type_name = type_name_no_tag (type);
  335.               computed_type_name = 1;
  336.             }
  337.           /* Omit constructors from the completion list.  */
  338.           if (!type_name || strcmp (type_name, name))
  339.             VEC_safe_push (char_ptr, *output, xstrdup (name));
  340.         }
  341.     }
  342. }

  343. /* Complete on expressions.  Often this means completing on symbol
  344.    names, but some language parsers also have support for completing
  345.    field names.  */
  346. VEC (char_ptr) *
  347. expression_completer (struct cmd_list_element *ignore,
  348.                       const char *text, const char *word)
  349. {
  350.   struct type *type = NULL;
  351.   char *fieldname;
  352.   const char *p;
  353.   volatile struct gdb_exception except;
  354.   enum type_code code = TYPE_CODE_UNDEF;

  355.   /* Perform a tentative parse of the expression, to see whether a
  356.      field completion is required.  */
  357.   fieldname = NULL;
  358.   TRY_CATCH (except, RETURN_MASK_ERROR)
  359.     {
  360.       type = parse_expression_for_completion (text, &fieldname, &code);
  361.     }
  362.   if (except.reason < 0)
  363.     return NULL;
  364.   if (fieldname && type)
  365.     {
  366.       for (;;)
  367.         {
  368.           CHECK_TYPEDEF (type);
  369.           if (TYPE_CODE (type) != TYPE_CODE_PTR
  370.               && TYPE_CODE (type) != TYPE_CODE_REF)
  371.             break;
  372.           type = TYPE_TARGET_TYPE (type);
  373.         }

  374.       if (TYPE_CODE (type) == TYPE_CODE_UNION
  375.           || TYPE_CODE (type) == TYPE_CODE_STRUCT)
  376.         {
  377.           int flen = strlen (fieldname);
  378.           VEC (char_ptr) *result = NULL;

  379.           add_struct_fields (type, &result, fieldname, flen);
  380.           xfree (fieldname);
  381.           return result;
  382.         }
  383.     }
  384.   else if (fieldname && code != TYPE_CODE_UNDEF)
  385.     {
  386.       VEC (char_ptr) *result;
  387.       struct cleanup *cleanup = make_cleanup (xfree, fieldname);

  388.       result = make_symbol_completion_type (fieldname, fieldname, code);
  389.       do_cleanups (cleanup);
  390.       return result;
  391.     }
  392.   xfree (fieldname);

  393.   /* Commands which complete on locations want to see the entire
  394.      argument.  */
  395.   for (p = word;
  396.        p > text && p[-1] != ' ' && p[-1] != '\t';
  397.        p--)
  398.     ;

  399.   /* Not ideal but it is what we used to do before...  */
  400.   return location_completer (ignore, p, word);
  401. }

  402. /* See definition in completer.h.  */

  403. void
  404. set_gdb_completion_word_break_characters (completer_ftype *fn)
  405. {
  406.   /* So far we are only interested in differentiating filename
  407.      completers from everything else.  */
  408.   if (fn == filename_completer)
  409.     rl_completer_word_break_characters
  410.       = gdb_completer_file_name_break_characters;
  411.   else
  412.     rl_completer_word_break_characters
  413.       = gdb_completer_command_word_break_characters;
  414. }

  415. /* Here are some useful test cases for completion.  FIXME: These
  416.    should be put in the test suite.  They should be tested with both
  417.    M-? and TAB.

  418.    "show output-" "radix"
  419.    "show output" "-radix"
  420.    "p" ambiguous (commands starting with p--path, print, printf, etc.)
  421.    "p "  ambiguous (all symbols)
  422.    "info t foo" no completions
  423.    "info t " no completions
  424.    "info t" ambiguous ("info target", "info terminal", etc.)
  425.    "info ajksdlfk" no completions
  426.    "info ajksdlfk " no completions
  427.    "info" " "
  428.    "info " ambiguous (all info commands)
  429.    "p \"a" no completions (string constant)
  430.    "p 'a" ambiguous (all symbols starting with a)
  431.    "p b-a" ambiguous (all symbols starting with a)
  432.    "p b-" ambiguous (all symbols)
  433.    "file Make" "file" (word break hard to screw up here)
  434.    "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
  435. */

  436. typedef enum
  437. {
  438.   handle_brkchars,
  439.   handle_completions,
  440.   handle_help
  441. }
  442. complete_line_internal_reason;


  443. /* Internal function used to handle completions.


  444.    TEXT is the caller's idea of the "word" we are looking at.

  445.    LINE_BUFFER is available to be looked at; it contains the entire
  446.    text of the line.  POINT is the offset in that line of the cursor.
  447.    You should pretend that the line ends at POINT.

  448.    REASON is of type complete_line_internal_reason.

  449.    If REASON is handle_brkchars:
  450.    Preliminary phase, called by gdb_completion_word_break_characters
  451.    function, is used to determine the correct set of chars that are
  452.    word delimiters depending on the current command in line_buffer.
  453.    No completion list should be generated; the return value should be
  454.    NULL.  This is checked by an assertion in that function.

  455.    If REASON is handle_completions:
  456.    Main phase, called by complete_line function, is used to get the list
  457.    of posible completions.

  458.    If REASON is handle_help:
  459.    Special case when completing a 'help' command.  In this case,
  460.    once sub-command completions are exhausted, we simply return NULL.
  461. */

  462. static VEC (char_ptr) *
  463. complete_line_internal (const char *text,
  464.                         const char *line_buffer, int point,
  465.                         complete_line_internal_reason reason)
  466. {
  467.   VEC (char_ptr) *list = NULL;
  468.   char *tmp_command;
  469.   const char *p;
  470.   int ignore_help_classes;
  471.   /* Pointer within tmp_command which corresponds to text.  */
  472.   char *word;
  473.   struct cmd_list_element *c, *result_list;

  474.   /* Choose the default set of word break characters to break
  475.      completions.  If we later find out that we are doing completions
  476.      on command strings (as opposed to strings supplied by the
  477.      individual command completer functions, which can be any string)
  478.      then we will switch to the special word break set for command
  479.      strings, which leaves out the '-' character used in some
  480.      commands.  */
  481.   rl_completer_word_break_characters =
  482.     current_language->la_word_break_characters();

  483.   /* Decide whether to complete on a list of gdb commands or on
  484.      symbols.  */
  485.   tmp_command = (char *) alloca (point + 1);
  486.   p = tmp_command;

  487.   /* The help command should complete help aliases.  */
  488.   ignore_help_classes = reason != handle_help;

  489.   strncpy (tmp_command, line_buffer, point);
  490.   tmp_command[point] = '\0';
  491.   /* Since text always contains some number of characters leading up
  492.      to point, we can find the equivalent position in tmp_command
  493.      by subtracting that many characters from the end of tmp_command.  */
  494.   word = tmp_command + point - strlen (text);

  495.   if (point == 0)
  496.     {
  497.       /* An empty line we want to consider ambiguous; that is, it
  498.          could be any command.  */
  499.       c = CMD_LIST_AMBIGUOUS;
  500.       result_list = 0;
  501.     }
  502.   else
  503.     {
  504.       c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes);
  505.     }

  506.   /* Move p up to the next interesting thing.  */
  507.   while (*p == ' ' || *p == '\t')
  508.     {
  509.       p++;
  510.     }

  511.   if (!c)
  512.     {
  513.       /* It is an unrecognized command.  So there are no
  514.          possible completions.  */
  515.       list = NULL;
  516.     }
  517.   else if (c == CMD_LIST_AMBIGUOUS)
  518.     {
  519.       const char *q;

  520.       /* lookup_cmd_1 advances p up to the first ambiguous thing, but
  521.          doesn't advance over that thing itself.  Do so now.  */
  522.       q = p;
  523.       while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
  524.         ++q;
  525.       if (q != tmp_command + point)
  526.         {
  527.           /* There is something beyond the ambiguous
  528.              command, so there are no possible completions.  For
  529.              example, "info t " or "info t foo" does not complete
  530.              to anything, because "info t" can be "info target" or
  531.              "info terminal".  */
  532.           list = NULL;
  533.         }
  534.       else
  535.         {
  536.           /* We're trying to complete on the command which was ambiguous.
  537.              This we can deal with.  */
  538.           if (result_list)
  539.             {
  540.               if (reason != handle_brkchars)
  541.                 list = complete_on_cmdlist (*result_list->prefixlist, p,
  542.                                             word, ignore_help_classes);
  543.             }
  544.           else
  545.             {
  546.               if (reason != handle_brkchars)
  547.                 list = complete_on_cmdlist (cmdlist, p, word,
  548.                                             ignore_help_classes);
  549.             }
  550.           /* Ensure that readline does the right thing with respect to
  551.              inserting quotes.  */
  552.           rl_completer_word_break_characters =
  553.             gdb_completer_command_word_break_characters;
  554.         }
  555.     }
  556.   else
  557.     {
  558.       /* We've recognized a full command.  */

  559.       if (p == tmp_command + point)
  560.         {
  561.           /* There is no non-whitespace in the line beyond the
  562.              command.  */

  563.           if (p[-1] == ' ' || p[-1] == '\t')
  564.             {
  565.               /* The command is followed by whitespace; we need to
  566.                  complete on whatever comes after command.  */
  567.               if (c->prefixlist)
  568.                 {
  569.                   /* It is a prefix command; what comes after it is
  570.                      a subcommand (e.g. "info ").  */
  571.                   if (reason != handle_brkchars)
  572.                     list = complete_on_cmdlist (*c->prefixlist, p, word,
  573.                                                 ignore_help_classes);

  574.                   /* Ensure that readline does the right thing
  575.                      with respect to inserting quotes.  */
  576.                   rl_completer_word_break_characters =
  577.                     gdb_completer_command_word_break_characters;
  578.                 }
  579.               else if (reason == handle_help)
  580.                 list = NULL;
  581.               else if (c->enums)
  582.                 {
  583.                   if (reason != handle_brkchars)
  584.                     list = complete_on_enum (c->enums, p, word);
  585.                   rl_completer_word_break_characters =
  586.                     gdb_completer_command_word_break_characters;
  587.                 }
  588.               else
  589.                 {
  590.                   /* It is a normal command; what comes after it is
  591.                      completed by the command's completer function.  */
  592.                   if (c->completer == filename_completer)
  593.                     {
  594.                       /* Many commands which want to complete on
  595.                          file names accept several file names, as
  596.                          in "run foo bar >>baz".  So we don't want
  597.                          to complete the entire text after the
  598.                          command, just the last word.  To this
  599.                          end, we need to find the beginning of the
  600.                          file name by starting at `word' and going
  601.                          backwards.  */
  602.                       for (p = word;
  603.                            p > tmp_command
  604.                              && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
  605.                            p--)
  606.                         ;
  607.                       rl_completer_word_break_characters =
  608.                         gdb_completer_file_name_break_characters;
  609.                     }
  610.                   else if (c->completer == location_completer)
  611.                     {
  612.                       /* Commands which complete on locations want to
  613.                          see the entire argument.  */
  614.                       for (p = word;
  615.                            p > tmp_command
  616.                              && p[-1] != ' ' && p[-1] != '\t';
  617.                            p--)
  618.                         ;
  619.                     }
  620.                   if (reason == handle_brkchars
  621.                       && c->completer_handle_brkchars != NULL)
  622.                     (*c->completer_handle_brkchars) (c, p, word);
  623.                   if (reason != handle_brkchars && c->completer != NULL)
  624.                     list = (*c->completer) (c, p, word);
  625.                 }
  626.             }
  627.           else
  628.             {
  629.               /* The command is not followed by whitespace; we need to
  630.                  complete on the command itself, e.g. "p" which is a
  631.                  command itself but also can complete to "print", "ptype"
  632.                  etc.  */
  633.               const char *q;

  634.               /* Find the command we are completing on.  */
  635.               q = p;
  636.               while (q > tmp_command)
  637.                 {
  638.                   if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
  639.                     --q;
  640.                   else
  641.                     break;
  642.                 }

  643.               if (reason != handle_brkchars)
  644.                 list = complete_on_cmdlist (result_list, q, word,
  645.                                             ignore_help_classes);

  646.               /* Ensure that readline does the right thing
  647.                  with respect to inserting quotes.  */
  648.               rl_completer_word_break_characters =
  649.                 gdb_completer_command_word_break_characters;
  650.             }
  651.         }
  652.       else if (reason == handle_help)
  653.         list = NULL;
  654.       else
  655.         {
  656.           /* There is non-whitespace beyond the command.  */

  657.           if (c->prefixlist && !c->allow_unknown)
  658.             {
  659.               /* It is an unrecognized subcommand of a prefix command,
  660.                  e.g. "info adsfkdj".  */
  661.               list = NULL;
  662.             }
  663.           else if (c->enums)
  664.             {
  665.               if (reason != handle_brkchars)
  666.                 list = complete_on_enum (c->enums, p, word);
  667.             }
  668.           else
  669.             {
  670.               /* It is a normal command.  */
  671.               if (c->completer == filename_completer)
  672.                 {
  673.                   /* See the commentary above about the specifics
  674.                      of file-name completion.  */
  675.                   for (p = word;
  676.                        p > tmp_command
  677.                          && strchr (gdb_completer_file_name_break_characters,
  678.                                     p[-1]) == NULL;
  679.                        p--)
  680.                     ;
  681.                   rl_completer_word_break_characters =
  682.                     gdb_completer_file_name_break_characters;
  683.                 }
  684.               else if (c->completer == location_completer)
  685.                 {
  686.                   for (p = word;
  687.                        p > tmp_command
  688.                          && p[-1] != ' ' && p[-1] != '\t';
  689.                        p--)
  690.                     ;
  691.                 }
  692.               if (reason == handle_brkchars
  693.                   && c->completer_handle_brkchars != NULL)
  694.                 (*c->completer_handle_brkchars) (c, p, word);
  695.               if (reason != handle_brkchars && c->completer != NULL)
  696.                 list = (*c->completer) (c, p, word);
  697.             }
  698.         }
  699.     }

  700.   return list;
  701. }
  702. /* Generate completions all at once.  Returns a vector of strings.
  703.    Each element is allocated with xmalloc.  It can also return NULL if
  704.    there are no completions.

  705.    TEXT is the caller's idea of the "word" we are looking at.

  706.    LINE_BUFFER is available to be looked at; it contains the entire
  707.    text of the line.

  708.    POINT is the offset in that line of the cursor.  You
  709.    should pretend that the line ends at POINT.  */

  710. VEC (char_ptr) *
  711. complete_line (const char *text, const char *line_buffer, int point)
  712. {
  713.   return complete_line_internal (text, line_buffer,
  714.                                  point, handle_completions);
  715. }

  716. /* Complete on command names.  Used by "help".  */
  717. VEC (char_ptr) *
  718. command_completer (struct cmd_list_element *ignore,
  719.                    const char *text, const char *word)
  720. {
  721.   return complete_line_internal (word, text,
  722.                                  strlen (text), handle_help);
  723. }

  724. /* Complete on signals.  */

  725. VEC (char_ptr) *
  726. signal_completer (struct cmd_list_element *ignore,
  727.                   const char *text, const char *word)
  728. {
  729.   VEC (char_ptr) *return_val = NULL;
  730.   size_t len = strlen (word);
  731.   enum gdb_signal signum;
  732.   const char *signame;

  733.   for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
  734.     {
  735.       /* Can't handle this, so skip it.  */
  736.       if (signum == GDB_SIGNAL_0)
  737.         continue;

  738.       signame = gdb_signal_to_name (signum);

  739.       /* Ignore the unknown signal case.  */
  740.       if (!signame || strcmp (signame, "?") == 0)
  741.         continue;

  742.       if (strncasecmp (signame, word, len) == 0)
  743.         VEC_safe_push (char_ptr, return_val, xstrdup (signame));
  744.     }

  745.   return return_val;
  746. }

  747. /* Complete on a register or reggroup.  */

  748. VEC (char_ptr) *
  749. reg_or_group_completer (struct cmd_list_element *ignore,
  750.                         const char *text, const char *word)
  751. {
  752.   VEC (char_ptr) *result = NULL;
  753.   size_t len = strlen (word);
  754.   struct gdbarch *gdbarch;
  755.   struct reggroup *group;
  756.   const char *name;
  757.   int i;

  758.   if (!target_has_registers)
  759.     return result;

  760.   gdbarch = get_frame_arch (get_selected_frame (NULL));

  761.   for (i = 0;
  762.        (name = user_reg_map_regnum_to_name (gdbarch, i)) != NULL;
  763.        i++)
  764.     {
  765.       if (*name != '\0' && strncmp (word, name, len) == 0)
  766.         VEC_safe_push (char_ptr, result, xstrdup (name));
  767.     }

  768.   for (group = reggroup_next (gdbarch, NULL);
  769.        group != NULL;
  770.        group = reggroup_next (gdbarch, group))
  771.     {
  772.       name = reggroup_name (group);
  773.       if (strncmp (word, name, len) == 0)
  774.         VEC_safe_push (char_ptr, result, xstrdup (name));
  775.     }

  776.   return result;
  777. }


  778. /* Get the list of chars that are considered as word breaks
  779.    for the current command.  */

  780. char *
  781. gdb_completion_word_break_characters (void)
  782. {
  783.   VEC (char_ptr) *list;

  784.   list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
  785.                                  handle_brkchars);
  786.   gdb_assert (list == NULL);
  787.   return rl_completer_word_break_characters;
  788. }

  789. /* Generate completions one by one for the completer.  Each time we
  790.    are called return another potential completion to the caller.
  791.    line_completion just completes on commands or passes the buck to
  792.    the command's completer function, the stuff specific to symbol
  793.    completion is in make_symbol_completion_list.

  794.    TEXT is the caller's idea of the "word" we are looking at.

  795.    MATCHES is the number of matches that have currently been collected
  796.    from calling this completion function.  When zero, then we need to
  797.    initialize, otherwise the initialization has already taken place
  798.    and we can just return the next potential completion string.

  799.    LINE_BUFFER is available to be looked at; it contains the entire
  800.    text of the line.  POINT is the offset in that line of the cursor.
  801.    You should pretend that the line ends at POINT.

  802.    Returns NULL if there are no more completions, else a pointer to a
  803.    string which is a possible completion, it is the caller's
  804.    responsibility to free the string.  */

  805. static char *
  806. line_completion_function (const char *text, int matches,
  807.                           char *line_buffer, int point)
  808. {
  809.   static VEC (char_ptr) *list = NULL;        /* Cache of completions.  */
  810.   static int index;                        /* Next cached completion.  */
  811.   char *output = NULL;

  812.   if (matches == 0)
  813.     {
  814.       /* The caller is beginning to accumulate a new set of
  815.          completions, so we need to find all of them now, and cache
  816.          them for returning one at a time on future calls.  */

  817.       if (list)
  818.         {
  819.           /* Free the storage used by LIST, but not by the strings
  820.              inside.  This is because rl_complete_internal () frees
  821.              the strings.  As complete_line may abort by calling
  822.              `error' clear LIST now.  */
  823.           VEC_free (char_ptr, list);
  824.         }
  825.       index = 0;
  826.       list = complete_line (text, line_buffer, point);
  827.     }

  828.   /* If we found a list of potential completions during initialization
  829.      then dole them out one at a time.  After returning the last one,
  830.      return NULL (and continue to do so) each time we are called after
  831.      that, until a new list is available.  */

  832.   if (list)
  833.     {
  834.       if (index < VEC_length (char_ptr, list))
  835.         {
  836.           output = VEC_index (char_ptr, list, index);
  837.           index++;
  838.         }
  839.     }

  840. #if 0
  841.   /* Can't do this because readline hasn't yet checked the word breaks
  842.      for figuring out whether to insert a quote.  */
  843.   if (output == NULL)
  844.     /* Make sure the word break characters are set back to normal for
  845.        the next time that readline tries to complete something.  */
  846.     rl_completer_word_break_characters =
  847.       current_language->la_word_break_characters();
  848. #endif

  849.   return (output);
  850. }

  851. /* Skip over the possibly quoted word STR (as defined by the quote
  852.    characters QUOTECHARS and the word break characters BREAKCHARS).
  853.    Returns pointer to the location after the "word".  If either
  854.    QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
  855.    completer.  */

  856. const char *
  857. skip_quoted_chars (const char *str, const char *quotechars,
  858.                    const char *breakchars)
  859. {
  860.   char quote_char = '\0';
  861.   const char *scan;

  862.   if (quotechars == NULL)
  863.     quotechars = gdb_completer_quote_characters;

  864.   if (breakchars == NULL)
  865.     breakchars = current_language->la_word_break_characters();

  866.   for (scan = str; *scan != '\0'; scan++)
  867.     {
  868.       if (quote_char != '\0')
  869.         {
  870.           /* Ignore everything until the matching close quote char.  */
  871.           if (*scan == quote_char)
  872.             {
  873.               /* Found matching close quote.  */
  874.               scan++;
  875.               break;
  876.             }
  877.         }
  878.       else if (strchr (quotechars, *scan))
  879.         {
  880.           /* Found start of a quoted string.  */
  881.           quote_char = *scan;
  882.         }
  883.       else if (strchr (breakchars, *scan))
  884.         {
  885.           break;
  886.         }
  887.     }

  888.   return (scan);
  889. }

  890. /* Skip over the possibly quoted word STR (as defined by the quote
  891.    characters and word break characters used by the completer).
  892.    Returns pointer to the location after the "word".  */

  893. const char *
  894. skip_quoted (const char *str)
  895. {
  896.   return skip_quoted_chars (str, NULL, NULL);
  897. }