GDB (API)
/home/stan/gdb/src/gdb/completer.c
Go to the documentation of this file.
00001 /* Line completion stuff for GDB, the GNU debugger.
00002    Copyright (C) 2000-2013 Free Software Foundation, Inc.
00003 
00004    This file is part of GDB.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00018 
00019 #include "defs.h"
00020 #include "symtab.h"
00021 #include "gdbtypes.h"
00022 #include "expression.h"
00023 #include "filenames.h"          /* For DOSish file names.  */
00024 #include "language.h"
00025 #include "gdb_assert.h"
00026 #include "exceptions.h"
00027 #include "gdb_signals.h"
00028 
00029 #include "cli/cli-decode.h"
00030 
00031 /* FIXME: This is needed because of lookup_cmd_1 ().  We should be
00032    calling a hook instead so we eliminate the CLI dependency.  */
00033 #include "gdbcmd.h"
00034 
00035 /* Needed for rl_completer_word_break_characters() and for
00036    rl_filename_completion_function.  */
00037 #include "readline/readline.h"
00038 
00039 /* readline defines this.  */
00040 #undef savestring
00041 
00042 #include "completer.h"
00043 
00044 /* Prototypes for local functions.  */
00045 static
00046 char *line_completion_function (const char *text, int matches, 
00047                                 char *line_buffer,
00048                                 int point);
00049 
00050 /* readline uses the word breaks for two things:
00051    (1) In figuring out where to point the TEXT parameter to the
00052    rl_completion_entry_function.  Since we don't use TEXT for much,
00053    it doesn't matter a lot what the word breaks are for this purpose,
00054    but it does affect how much stuff M-? lists.
00055    (2) If one of the matches contains a word break character, readline
00056    will quote it.  That's why we switch between
00057    current_language->la_word_break_characters() and
00058    gdb_completer_command_word_break_characters.  I'm not sure when
00059    we need this behavior (perhaps for funky characters in C++ 
00060    symbols?).  */
00061 
00062 /* Variables which are necessary for fancy command line editing.  */
00063 
00064 /* When completing on command names, we remove '-' from the list of
00065    word break characters, since we use it in command names.  If the
00066    readline library sees one in any of the current completion strings,
00067    it thinks that the string needs to be quoted and automatically
00068    supplies a leading quote.  */
00069 static char *gdb_completer_command_word_break_characters =
00070 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
00071 
00072 /* When completing on file names, we remove from the list of word
00073    break characters any characters that are commonly used in file
00074    names, such as '-', '+', '~', etc.  Otherwise, readline displays
00075    incorrect completion candidates.  */
00076 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
00077 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
00078    programs support @foo style response files.  */
00079 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
00080 #else
00081 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
00082 #endif
00083 
00084 /* Characters that can be used to quote completion strings.  Note that
00085    we can't include '"' because the gdb C parser treats such quoted
00086    sequences as strings.  */
00087 static char *gdb_completer_quote_characters = "'";
00088 
00089 /* Accessor for some completer data that may interest other files.  */
00090 
00091 char *
00092 get_gdb_completer_quote_characters (void)
00093 {
00094   return gdb_completer_quote_characters;
00095 }
00096 
00097 /* Line completion interface function for readline.  */
00098 
00099 char *
00100 readline_line_completion_function (const char *text, int matches)
00101 {
00102   return line_completion_function (text, matches, 
00103                                    rl_line_buffer, rl_point);
00104 }
00105 
00106 /* This can be used for functions which don't want to complete on
00107    symbols but don't want to complete on anything else either.  */
00108 VEC (char_ptr) *
00109 noop_completer (struct cmd_list_element *ignore, 
00110                 const char *text, const char *prefix)
00111 {
00112   return NULL;
00113 }
00114 
00115 /* Complete on filenames.  */
00116 VEC (char_ptr) *
00117 filename_completer (struct cmd_list_element *ignore, 
00118                     const char *text, const char *word)
00119 {
00120   int subsequent_name;
00121   VEC (char_ptr) *return_val = NULL;
00122 
00123   subsequent_name = 0;
00124   while (1)
00125     {
00126       char *p, *q;
00127 
00128       p = rl_filename_completion_function (text, subsequent_name);
00129       if (p == NULL)
00130         break;
00131       /* We need to set subsequent_name to a non-zero value before the
00132          continue line below, because otherwise, if the first file
00133          seen by GDB is a backup file whose name ends in a `~', we
00134          will loop indefinitely.  */
00135       subsequent_name = 1;
00136       /* Like emacs, don't complete on old versions.  Especially
00137          useful in the "source" command.  */
00138       if (p[strlen (p) - 1] == '~')
00139         {
00140           xfree (p);
00141           continue;
00142         }
00143 
00144       if (word == text)
00145         /* Return exactly p.  */
00146         q = p;
00147       else if (word > text)
00148         {
00149           /* Return some portion of p.  */
00150           q = xmalloc (strlen (p) + 5);
00151           strcpy (q, p + (word - text));
00152           xfree (p);
00153         }
00154       else
00155         {
00156           /* Return some of TEXT plus p.  */
00157           q = xmalloc (strlen (p) + (text - word) + 5);
00158           strncpy (q, word, text - word);
00159           q[text - word] = '\0';
00160           strcat (q, p);
00161           xfree (p);
00162         }
00163       VEC_safe_push (char_ptr, return_val, q);
00164     }
00165 #if 0
00166   /* There is no way to do this just long enough to affect quote
00167      inserting without also affecting the next completion.  This
00168      should be fixed in readline.  FIXME.  */
00169   /* Ensure that readline does the right thing
00170      with respect to inserting quotes.  */
00171   rl_completer_word_break_characters = "";
00172 #endif
00173   return return_val;
00174 }
00175 
00176 /* Complete on locations, which might be of two possible forms:
00177 
00178        file:line
00179    or
00180        symbol+offset
00181 
00182    This is intended to be used in commands that set breakpoints
00183    etc.  */
00184 
00185 VEC (char_ptr) *
00186 location_completer (struct cmd_list_element *ignore, 
00187                     const char *text, const char *word)
00188 {
00189   int n_syms, n_files, ix;
00190   VEC (char_ptr) *fn_list = NULL;
00191   VEC (char_ptr) *list = NULL;
00192   const char *p;
00193   int quote_found = 0;
00194   int quoted = *text == '\'' || *text == '"';
00195   int quote_char = '\0';
00196   const char *colon = NULL;
00197   char *file_to_match = NULL;
00198   const char *symbol_start = text;
00199   const char *orig_text = text;
00200   size_t text_len;
00201 
00202   /* Do we have an unquoted colon, as in "break foo.c:bar"?  */
00203   for (p = text; *p != '\0'; ++p)
00204     {
00205       if (*p == '\\' && p[1] == '\'')
00206         p++;
00207       else if (*p == '\'' || *p == '"')
00208         {
00209           quote_found = *p;
00210           quote_char = *p++;
00211           while (*p != '\0' && *p != quote_found)
00212             {
00213               if (*p == '\\' && p[1] == quote_found)
00214                 p++;
00215               p++;
00216             }
00217 
00218           if (*p == quote_found)
00219             quote_found = 0;
00220           else
00221             break;              /* Hit the end of text.  */
00222         }
00223 #if HAVE_DOS_BASED_FILE_SYSTEM
00224       /* If we have a DOS-style absolute file name at the beginning of
00225          TEXT, and the colon after the drive letter is the only colon
00226          we found, pretend the colon is not there.  */
00227       else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
00228         ;
00229 #endif
00230       else if (*p == ':' && !colon)
00231         {
00232           colon = p;
00233           symbol_start = p + 1;
00234         }
00235       else if (strchr (current_language->la_word_break_characters(), *p))
00236         symbol_start = p + 1;
00237     }
00238 
00239   if (quoted)
00240     text++;
00241   text_len = strlen (text);
00242 
00243   /* Where is the file name?  */
00244   if (colon)
00245     {
00246       char *s;
00247 
00248       file_to_match = (char *) xmalloc (colon - text + 1);
00249       strncpy (file_to_match, text, colon - text + 1);
00250       /* Remove trailing colons and quotes from the file name.  */
00251       for (s = file_to_match + (colon - text);
00252            s > file_to_match;
00253            s--)
00254         if (*s == ':' || *s == quote_char)
00255           *s = '\0';
00256     }
00257   /* If the text includes a colon, they want completion only on a
00258      symbol name after the colon.  Otherwise, we need to complete on
00259      symbols as well as on files.  */
00260   if (colon)
00261     {
00262       list = make_file_symbol_completion_list (symbol_start, word,
00263                                                file_to_match);
00264       xfree (file_to_match);
00265     }
00266   else
00267     {
00268       list = make_symbol_completion_list (symbol_start, word);
00269       /* If text includes characters which cannot appear in a file
00270          name, they cannot be asking for completion on files.  */
00271       if (strcspn (text, 
00272                    gdb_completer_file_name_break_characters) == text_len)
00273         fn_list = make_source_files_completion_list (text, text);
00274     }
00275 
00276   n_syms = VEC_length (char_ptr, list);
00277   n_files = VEC_length (char_ptr, fn_list);
00278 
00279   /* Catenate fn_list[] onto the end of list[].  */
00280   if (!n_syms)
00281     {
00282       VEC_free (char_ptr, list); /* Paranoia.  */
00283       list = fn_list;
00284       fn_list = NULL;
00285     }
00286   else
00287     {
00288       char *fn;
00289 
00290       for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, fn); ++ix)
00291         VEC_safe_push (char_ptr, list, fn);
00292       VEC_free (char_ptr, fn_list);
00293     }
00294 
00295   if (n_syms && n_files)
00296     {
00297       /* Nothing.  */
00298     }
00299   else if (n_files)
00300     {
00301       char *fn;
00302 
00303       /* If we only have file names as possible completion, we should
00304          bring them in sync with what rl_complete expects.  The
00305          problem is that if the user types "break /foo/b TAB", and the
00306          possible completions are "/foo/bar" and "/foo/baz"
00307          rl_complete expects us to return "bar" and "baz", without the
00308          leading directories, as possible completions, because `word'
00309          starts at the "b".  But we ignore the value of `word' when we
00310          call make_source_files_completion_list above (because that
00311          would not DTRT when the completion results in both symbols
00312          and file names), so make_source_files_completion_list returns
00313          the full "/foo/bar" and "/foo/baz" strings.  This produces
00314          wrong results when, e.g., there's only one possible
00315          completion, because rl_complete will prepend "/foo/" to each
00316          candidate completion.  The loop below removes that leading
00317          part.  */
00318       for (ix = 0; VEC_iterate (char_ptr, list, ix, fn); ++ix)
00319         {
00320           memmove (fn, fn + (word - text),
00321                    strlen (fn) + 1 - (word - text));
00322         }
00323     }
00324   else if (!n_syms)
00325     {
00326       /* No completions at all.  As the final resort, try completing
00327          on the entire text as a symbol.  */
00328       list = make_symbol_completion_list (orig_text, word);
00329     }
00330 
00331   return list;
00332 }
00333 
00334 /* Helper for expression_completer which recursively adds field and
00335    method names from TYPE, a struct or union type, to the array
00336    OUTPUT.  */
00337 static void
00338 add_struct_fields (struct type *type, VEC (char_ptr) **output,
00339                    char *fieldname, int namelen)
00340 {
00341   int i;
00342   int computed_type_name = 0;
00343   const char *type_name = NULL;
00344 
00345   CHECK_TYPEDEF (type);
00346   for (i = 0; i < TYPE_NFIELDS (type); ++i)
00347     {
00348       if (i < TYPE_N_BASECLASSES (type))
00349         add_struct_fields (TYPE_BASECLASS (type, i),
00350                            output, fieldname, namelen);
00351       else if (TYPE_FIELD_NAME (type, i))
00352         {
00353           if (TYPE_FIELD_NAME (type, i)[0] != '\0')
00354             {
00355               if (! strncmp (TYPE_FIELD_NAME (type, i), 
00356                              fieldname, namelen))
00357                 VEC_safe_push (char_ptr, *output,
00358                                xstrdup (TYPE_FIELD_NAME (type, i)));
00359             }
00360           else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
00361             {
00362               /* Recurse into anonymous unions.  */
00363               add_struct_fields (TYPE_FIELD_TYPE (type, i),
00364                                  output, fieldname, namelen);
00365             }
00366         }
00367     }
00368 
00369   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
00370     {
00371       const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
00372 
00373       if (name && ! strncmp (name, fieldname, namelen))
00374         {
00375           if (!computed_type_name)
00376             {
00377               type_name = type_name_no_tag (type);
00378               computed_type_name = 1;
00379             }
00380           /* Omit constructors from the completion list.  */
00381           if (!type_name || strcmp (type_name, name))
00382             VEC_safe_push (char_ptr, *output, xstrdup (name));
00383         }
00384     }
00385 }
00386 
00387 /* Complete on expressions.  Often this means completing on symbol
00388    names, but some language parsers also have support for completing
00389    field names.  */
00390 VEC (char_ptr) *
00391 expression_completer (struct cmd_list_element *ignore, 
00392                       const char *text, const char *word)
00393 {
00394   struct type *type = NULL;
00395   char *fieldname;
00396   const char *p;
00397   volatile struct gdb_exception except;
00398   enum type_code code = TYPE_CODE_UNDEF;
00399 
00400   /* Perform a tentative parse of the expression, to see whether a
00401      field completion is required.  */
00402   fieldname = NULL;
00403   TRY_CATCH (except, RETURN_MASK_ERROR)
00404     {
00405       type = parse_expression_for_completion (text, &fieldname, &code);
00406     }
00407   if (except.reason < 0)
00408     return NULL;
00409   if (fieldname && type)
00410     {
00411       for (;;)
00412         {
00413           CHECK_TYPEDEF (type);
00414           if (TYPE_CODE (type) != TYPE_CODE_PTR
00415               && TYPE_CODE (type) != TYPE_CODE_REF)
00416             break;
00417           type = TYPE_TARGET_TYPE (type);
00418         }
00419 
00420       if (TYPE_CODE (type) == TYPE_CODE_UNION
00421           || TYPE_CODE (type) == TYPE_CODE_STRUCT)
00422         {
00423           int flen = strlen (fieldname);
00424           VEC (char_ptr) *result = NULL;
00425 
00426           add_struct_fields (type, &result, fieldname, flen);
00427           xfree (fieldname);
00428           return result;
00429         }
00430     }
00431   else if (fieldname && code != TYPE_CODE_UNDEF)
00432     {
00433       VEC (char_ptr) *result;
00434       struct cleanup *cleanup = make_cleanup (xfree, fieldname);
00435 
00436       result = make_symbol_completion_type (fieldname, fieldname, code);
00437       do_cleanups (cleanup);
00438       return result;
00439     }
00440   xfree (fieldname);
00441 
00442   /* Commands which complete on locations want to see the entire
00443      argument.  */
00444   for (p = word;
00445        p > text && p[-1] != ' ' && p[-1] != '\t';
00446        p--)
00447     ;
00448 
00449   /* Not ideal but it is what we used to do before...  */
00450   return location_completer (ignore, p, word);
00451 }
00452 
00453 /* Here are some useful test cases for completion.  FIXME: These
00454    should be put in the test suite.  They should be tested with both
00455    M-? and TAB.
00456 
00457    "show output-" "radix"
00458    "show output" "-radix"
00459    "p" ambiguous (commands starting with p--path, print, printf, etc.)
00460    "p "  ambiguous (all symbols)
00461    "info t foo" no completions
00462    "info t " no completions
00463    "info t" ambiguous ("info target", "info terminal", etc.)
00464    "info ajksdlfk" no completions
00465    "info ajksdlfk " no completions
00466    "info" " "
00467    "info " ambiguous (all info commands)
00468    "p \"a" no completions (string constant)
00469    "p 'a" ambiguous (all symbols starting with a)
00470    "p b-a" ambiguous (all symbols starting with a)
00471    "p b-" ambiguous (all symbols)
00472    "file Make" "file" (word break hard to screw up here)
00473    "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
00474  */
00475 
00476 typedef enum
00477 {
00478   handle_brkchars,
00479   handle_completions,
00480   handle_help
00481 }
00482 complete_line_internal_reason;
00483 
00484 
00485 /* Internal function used to handle completions.
00486 
00487 
00488    TEXT is the caller's idea of the "word" we are looking at.
00489 
00490    LINE_BUFFER is available to be looked at; it contains the entire
00491    text of the line.  POINT is the offset in that line of the cursor.
00492    You should pretend that the line ends at POINT.
00493 
00494    REASON is of type complete_line_internal_reason.
00495 
00496    If REASON is handle_brkchars:
00497    Preliminary phase, called by gdb_completion_word_break_characters
00498    function, is used to determine the correct set of chars that are
00499    word delimiters depending on the current command in line_buffer.
00500    No completion list should be generated; the return value should be
00501    NULL.  This is checked by an assertion in that function.
00502 
00503    If REASON is handle_completions:
00504    Main phase, called by complete_line function, is used to get the list
00505    of posible completions.
00506 
00507    If REASON is handle_help:
00508    Special case when completing a 'help' command.  In this case,
00509    once sub-command completions are exhausted, we simply return NULL.
00510  */
00511 
00512 static VEC (char_ptr) *
00513 complete_line_internal (const char *text, 
00514                         const char *line_buffer, int point,
00515                         complete_line_internal_reason reason)
00516 {
00517   VEC (char_ptr) *list = NULL;
00518   char *tmp_command;
00519   const char *p;
00520   int ignore_help_classes;
00521   /* Pointer within tmp_command which corresponds to text.  */
00522   char *word;
00523   struct cmd_list_element *c, *result_list;
00524 
00525   /* Choose the default set of word break characters to break
00526      completions.  If we later find out that we are doing completions
00527      on command strings (as opposed to strings supplied by the
00528      individual command completer functions, which can be any string)
00529      then we will switch to the special word break set for command
00530      strings, which leaves out the '-' character used in some
00531      commands.  */
00532   rl_completer_word_break_characters =
00533     current_language->la_word_break_characters();
00534 
00535   /* Decide whether to complete on a list of gdb commands or on
00536      symbols.  */
00537   tmp_command = (char *) alloca (point + 1);
00538   p = tmp_command;
00539 
00540   /* The help command should complete help aliases.  */
00541   ignore_help_classes = reason != handle_help;
00542 
00543   strncpy (tmp_command, line_buffer, point);
00544   tmp_command[point] = '\0';
00545   /* Since text always contains some number of characters leading up
00546      to point, we can find the equivalent position in tmp_command
00547      by subtracting that many characters from the end of tmp_command.  */
00548   word = tmp_command + point - strlen (text);
00549 
00550   if (point == 0)
00551     {
00552       /* An empty line we want to consider ambiguous; that is, it
00553          could be any command.  */
00554       c = CMD_LIST_AMBIGUOUS;
00555       result_list = 0;
00556     }
00557   else
00558     {
00559       c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes);
00560     }
00561 
00562   /* Move p up to the next interesting thing.  */
00563   while (*p == ' ' || *p == '\t')
00564     {
00565       p++;
00566     }
00567 
00568   if (!c)
00569     {
00570       /* It is an unrecognized command.  So there are no
00571          possible completions.  */
00572       list = NULL;
00573     }
00574   else if (c == CMD_LIST_AMBIGUOUS)
00575     {
00576       const char *q;
00577 
00578       /* lookup_cmd_1 advances p up to the first ambiguous thing, but
00579          doesn't advance over that thing itself.  Do so now.  */
00580       q = p;
00581       while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
00582         ++q;
00583       if (q != tmp_command + point)
00584         {
00585           /* There is something beyond the ambiguous
00586              command, so there are no possible completions.  For
00587              example, "info t " or "info t foo" does not complete
00588              to anything, because "info t" can be "info target" or
00589              "info terminal".  */
00590           list = NULL;
00591         }
00592       else
00593         {
00594           /* We're trying to complete on the command which was ambiguous.
00595              This we can deal with.  */
00596           if (result_list)
00597             {
00598               if (reason != handle_brkchars)
00599                 list = complete_on_cmdlist (*result_list->prefixlist, p,
00600                                             word, ignore_help_classes);
00601             }
00602           else
00603             {
00604               if (reason != handle_brkchars)
00605                 list = complete_on_cmdlist (cmdlist, p, word,
00606                                             ignore_help_classes);
00607             }
00608           /* Ensure that readline does the right thing with respect to
00609              inserting quotes.  */
00610           rl_completer_word_break_characters =
00611             gdb_completer_command_word_break_characters;
00612         }
00613     }
00614   else
00615     {
00616       /* We've recognized a full command.  */
00617 
00618       if (p == tmp_command + point)
00619         {
00620           /* There is no non-whitespace in the line beyond the
00621              command.  */
00622 
00623           if (p[-1] == ' ' || p[-1] == '\t')
00624             {
00625               /* The command is followed by whitespace; we need to
00626                  complete on whatever comes after command.  */
00627               if (c->prefixlist)
00628                 {
00629                   /* It is a prefix command; what comes after it is
00630                      a subcommand (e.g. "info ").  */
00631                   if (reason != handle_brkchars)
00632                     list = complete_on_cmdlist (*c->prefixlist, p, word,
00633                                                 ignore_help_classes);
00634 
00635                   /* Ensure that readline does the right thing
00636                      with respect to inserting quotes.  */
00637                   rl_completer_word_break_characters =
00638                     gdb_completer_command_word_break_characters;
00639                 }
00640               else if (reason == handle_help)
00641                 list = NULL;
00642               else if (c->enums)
00643                 {
00644                   if (reason != handle_brkchars)
00645                     list = complete_on_enum (c->enums, p, word);
00646                   rl_completer_word_break_characters =
00647                     gdb_completer_command_word_break_characters;
00648                 }
00649               else
00650                 {
00651                   /* It is a normal command; what comes after it is
00652                      completed by the command's completer function.  */
00653                   if (c->completer == filename_completer)
00654                     {
00655                       /* Many commands which want to complete on
00656                          file names accept several file names, as
00657                          in "run foo bar >>baz".  So we don't want
00658                          to complete the entire text after the
00659                          command, just the last word.  To this
00660                          end, we need to find the beginning of the
00661                          file name by starting at `word' and going
00662                          backwards.  */
00663                       for (p = word;
00664                            p > tmp_command
00665                              && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
00666                            p--)
00667                         ;
00668                       rl_completer_word_break_characters =
00669                         gdb_completer_file_name_break_characters;
00670                     }
00671                   else if (c->completer == location_completer)
00672                     {
00673                       /* Commands which complete on locations want to
00674                          see the entire argument.  */
00675                       for (p = word;
00676                            p > tmp_command
00677                              && p[-1] != ' ' && p[-1] != '\t';
00678                            p--)
00679                         ;
00680                     }
00681                   if (reason != handle_brkchars && c->completer != NULL)
00682                     list = (*c->completer) (c, p, word);
00683                 }
00684             }
00685           else
00686             {
00687               /* The command is not followed by whitespace; we need to
00688                  complete on the command itself, e.g. "p" which is a
00689                  command itself but also can complete to "print", "ptype"
00690                  etc.  */
00691               const char *q;
00692 
00693               /* Find the command we are completing on.  */
00694               q = p;
00695               while (q > tmp_command)
00696                 {
00697                   if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
00698                     --q;
00699                   else
00700                     break;
00701                 }
00702 
00703               if (reason != handle_brkchars)
00704                 list = complete_on_cmdlist (result_list, q, word,
00705                                             ignore_help_classes);
00706 
00707               /* Ensure that readline does the right thing
00708                  with respect to inserting quotes.  */
00709               rl_completer_word_break_characters =
00710                 gdb_completer_command_word_break_characters;
00711             }
00712         }
00713       else if (reason == handle_help)
00714         list = NULL;
00715       else
00716         {
00717           /* There is non-whitespace beyond the command.  */
00718 
00719           if (c->prefixlist && !c->allow_unknown)
00720             {
00721               /* It is an unrecognized subcommand of a prefix command,
00722                  e.g. "info adsfkdj".  */
00723               list = NULL;
00724             }
00725           else if (c->enums)
00726             {
00727               if (reason != handle_brkchars)
00728                 list = complete_on_enum (c->enums, p, word);
00729             }
00730           else
00731             {
00732               /* It is a normal command.  */
00733               if (c->completer == filename_completer)
00734                 {
00735                   /* See the commentary above about the specifics
00736                      of file-name completion.  */
00737                   for (p = word;
00738                        p > tmp_command
00739                          && strchr (gdb_completer_file_name_break_characters, 
00740                                     p[-1]) == NULL;
00741                        p--)
00742                     ;
00743                   rl_completer_word_break_characters =
00744                     gdb_completer_file_name_break_characters;
00745                 }
00746               else if (c->completer == location_completer)
00747                 {
00748                   for (p = word;
00749                        p > tmp_command
00750                          && p[-1] != ' ' && p[-1] != '\t';
00751                        p--)
00752                     ;
00753                 }
00754               if (reason != handle_brkchars && c->completer != NULL)
00755                 list = (*c->completer) (c, p, word);
00756             }
00757         }
00758     }
00759 
00760   return list;
00761 }
00762 /* Generate completions all at once.  Returns a vector of strings.
00763    Each element is allocated with xmalloc.  It can also return NULL if
00764    there are no completions.
00765 
00766    TEXT is the caller's idea of the "word" we are looking at.
00767 
00768    LINE_BUFFER is available to be looked at; it contains the entire
00769    text of the line.
00770 
00771    POINT is the offset in that line of the cursor.  You
00772    should pretend that the line ends at POINT.  */
00773 
00774 VEC (char_ptr) *
00775 complete_line (const char *text, char *line_buffer, int point)
00776 {
00777   return complete_line_internal (text, line_buffer, 
00778                                  point, handle_completions);
00779 }
00780 
00781 /* Complete on command names.  Used by "help".  */
00782 VEC (char_ptr) *
00783 command_completer (struct cmd_list_element *ignore, 
00784                    const char *text, const char *word)
00785 {
00786   return complete_line_internal (word, text, 
00787                                  strlen (text), handle_help);
00788 }
00789 
00790 /* Complete on signals.  */
00791 
00792 VEC (char_ptr) *
00793 signal_completer (struct cmd_list_element *ignore,
00794                   const char *text, const char *word)
00795 {
00796   VEC (char_ptr) *return_val = NULL;
00797   size_t len = strlen (word);
00798   enum gdb_signal signum;
00799   const char *signame;
00800 
00801   for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
00802     {
00803       /* Can't handle this, so skip it.  */
00804       if (signum == GDB_SIGNAL_0)
00805         continue;
00806 
00807       signame = gdb_signal_to_name (signum);
00808 
00809       /* Ignore the unknown signal case.  */
00810       if (!signame || strcmp (signame, "?") == 0)
00811         continue;
00812 
00813       if (strncasecmp (signame, word, len) == 0)
00814         VEC_safe_push (char_ptr, return_val, xstrdup (signame));
00815     }
00816 
00817   return return_val;
00818 }
00819 
00820 /* Get the list of chars that are considered as word breaks
00821    for the current command.  */
00822 
00823 char *
00824 gdb_completion_word_break_characters (void)
00825 {
00826   VEC (char_ptr) *list;
00827 
00828   list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
00829                                  handle_brkchars);
00830   gdb_assert (list == NULL);
00831   return rl_completer_word_break_characters;
00832 }
00833 
00834 /* Generate completions one by one for the completer.  Each time we
00835    are called return another potential completion to the caller.
00836    line_completion just completes on commands or passes the buck to
00837    the command's completer function, the stuff specific to symbol
00838    completion is in make_symbol_completion_list.
00839 
00840    TEXT is the caller's idea of the "word" we are looking at.
00841 
00842    MATCHES is the number of matches that have currently been collected
00843    from calling this completion function.  When zero, then we need to
00844    initialize, otherwise the initialization has already taken place
00845    and we can just return the next potential completion string.
00846 
00847    LINE_BUFFER is available to be looked at; it contains the entire
00848    text of the line.  POINT is the offset in that line of the cursor.
00849    You should pretend that the line ends at POINT.
00850 
00851    Returns NULL if there are no more completions, else a pointer to a
00852    string which is a possible completion, it is the caller's
00853    responsibility to free the string.  */
00854 
00855 static char *
00856 line_completion_function (const char *text, int matches, 
00857                           char *line_buffer, int point)
00858 {
00859   static VEC (char_ptr) *list = NULL;   /* Cache of completions.  */
00860   static int index;                     /* Next cached completion.  */
00861   char *output = NULL;
00862 
00863   if (matches == 0)
00864     {
00865       /* The caller is beginning to accumulate a new set of
00866          completions, so we need to find all of them now, and cache
00867          them for returning one at a time on future calls.  */
00868 
00869       if (list)
00870         {
00871           /* Free the storage used by LIST, but not by the strings
00872              inside.  This is because rl_complete_internal () frees
00873              the strings.  As complete_line may abort by calling
00874              `error' clear LIST now.  */
00875           VEC_free (char_ptr, list);
00876         }
00877       index = 0;
00878       list = complete_line (text, line_buffer, point);
00879     }
00880 
00881   /* If we found a list of potential completions during initialization
00882      then dole them out one at a time.  After returning the last one,
00883      return NULL (and continue to do so) each time we are called after
00884      that, until a new list is available.  */
00885 
00886   if (list)
00887     {
00888       if (index < VEC_length (char_ptr, list))
00889         {
00890           output = VEC_index (char_ptr, list, index);
00891           index++;
00892         }
00893     }
00894 
00895 #if 0
00896   /* Can't do this because readline hasn't yet checked the word breaks
00897      for figuring out whether to insert a quote.  */
00898   if (output == NULL)
00899     /* Make sure the word break characters are set back to normal for
00900        the next time that readline tries to complete something.  */
00901     rl_completer_word_break_characters =
00902       current_language->la_word_break_characters();
00903 #endif
00904 
00905   return (output);
00906 }
00907 
00908 /* Skip over the possibly quoted word STR (as defined by the quote
00909    characters QUOTECHARS and the word break characters BREAKCHARS).
00910    Returns pointer to the location after the "word".  If either
00911    QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
00912    completer.  */
00913 
00914 const char *
00915 skip_quoted_chars (const char *str, const char *quotechars,
00916                    const char *breakchars)
00917 {
00918   char quote_char = '\0';
00919   const char *scan;
00920 
00921   if (quotechars == NULL)
00922     quotechars = gdb_completer_quote_characters;
00923 
00924   if (breakchars == NULL)
00925     breakchars = current_language->la_word_break_characters();
00926 
00927   for (scan = str; *scan != '\0'; scan++)
00928     {
00929       if (quote_char != '\0')
00930         {
00931           /* Ignore everything until the matching close quote char.  */
00932           if (*scan == quote_char)
00933             {
00934               /* Found matching close quote.  */
00935               scan++;
00936               break;
00937             }
00938         }
00939       else if (strchr (quotechars, *scan))
00940         {
00941           /* Found start of a quoted string.  */
00942           quote_char = *scan;
00943         }
00944       else if (strchr (breakchars, *scan))
00945         {
00946           break;
00947         }
00948     }
00949 
00950   return (scan);
00951 }
00952 
00953 /* Skip over the possibly quoted word STR (as defined by the quote
00954    characters and word break characters used by the completer).
00955    Returns pointer to the location after the "word".  */
00956 
00957 const char *
00958 skip_quoted (const char *str)
00959 {
00960   return skip_quoted_chars (str, NULL, NULL);
00961 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines