GDB (API)
/home/stan/gdb/src/gdb/cli/cli-decode.c
Go to the documentation of this file.
00001 /* Handle lists of commands, their decoding and documentation, for GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00017 
00018 #include "defs.h"
00019 #include "symtab.h"
00020 #include <ctype.h>
00021 #include "gdb_regex.h"
00022 #include "gdb_string.h"
00023 #include "completer.h"
00024 #include "ui-out.h"
00025 #include "cli/cli-cmds.h"
00026 #include "cli/cli-decode.h"
00027 #include "gdb_assert.h"
00028 
00029 /* Prototypes for local functions.  */
00030 
00031 static void undef_cmd_error (const char *, const char *);
00032 
00033 static struct cmd_list_element *delete_cmd (const char *name,
00034                                             struct cmd_list_element **list,
00035                                             struct cmd_list_element **prehook,
00036                                             struct cmd_list_element **prehookee,
00037                                             struct cmd_list_element **posthook,
00038                                             struct cmd_list_element **posthookee);
00039 
00040 static struct cmd_list_element *find_cmd (const char *command,
00041                                           int len,
00042                                           struct cmd_list_element *clist,
00043                                           int ignore_help_classes,
00044                                           int *nfound);
00045 
00046 static void help_all (struct ui_file *stream);
00047 
00048 /* Look up a command whose 'prefixlist' is KEY.  Return the command if found,
00049    otherwise return NULL.  */
00050 
00051 static struct cmd_list_element *
00052 lookup_cmd_for_prefixlist (struct cmd_list_element **key,
00053                            struct cmd_list_element *list)
00054 {
00055   struct cmd_list_element *p = NULL;
00056 
00057   for (p = list; p != NULL; p = p->next)
00058     {
00059       struct cmd_list_element *q;
00060 
00061       if (p->prefixlist == NULL)
00062         continue;
00063       else if (p->prefixlist == key)
00064         return p;
00065 
00066       q = lookup_cmd_for_prefixlist (key, *(p->prefixlist));
00067       if (q != NULL)
00068         return q;
00069     }
00070 
00071   return NULL;
00072 }
00073 
00074 static void
00075 set_cmd_prefix (struct cmd_list_element *c, struct cmd_list_element **list)
00076 {
00077   struct cmd_list_element *p;
00078 
00079   /* Check to see if *LIST contains any element other than C.  */
00080   for (p = *list; p != NULL; p = p->next)
00081     if (p != c)
00082       break;
00083 
00084   if (p == NULL)
00085     {
00086       /* *SET_LIST only contains SET.  */
00087       p = lookup_cmd_for_prefixlist (list, setlist);
00088 
00089       c->prefix = p ? (p->cmd_pointer ? p->cmd_pointer : p) : p;
00090     }
00091   else
00092     c->prefix = p->prefix;
00093 }
00094 
00095 static void
00096 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
00097                         struct ui_file *stream);
00098 
00099 
00100 /* Set the callback function for the specified command.  For each both
00101    the commands callback and func() are set.  The latter set to a
00102    bounce function (unless cfunc / sfunc is NULL that is).  */
00103 
00104 static void
00105 do_cfunc (struct cmd_list_element *c, char *args, int from_tty)
00106 {
00107   c->function.cfunc (args, from_tty); /* Ok.  */
00108 }
00109 
00110 void
00111 set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
00112 {
00113   if (cfunc == NULL)
00114     cmd->func = NULL;
00115   else
00116     cmd->func = do_cfunc;
00117   cmd->function.cfunc = cfunc; /* Ok.  */
00118 }
00119 
00120 static void
00121 do_sfunc (struct cmd_list_element *c, char *args, int from_tty)
00122 {
00123   c->function.sfunc (args, from_tty, c); /* Ok.  */
00124 }
00125 
00126 void
00127 set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc)
00128 {
00129   if (sfunc == NULL)
00130     cmd->func = NULL;
00131   else
00132     cmd->func = do_sfunc;
00133   cmd->function.sfunc = sfunc; /* Ok.  */
00134 }
00135 
00136 int
00137 cmd_cfunc_eq (struct cmd_list_element *cmd,
00138               void (*cfunc) (char *args, int from_tty))
00139 {
00140   return cmd->func == do_cfunc && cmd->function.cfunc == cfunc;
00141 }
00142 
00143 void
00144 set_cmd_context (struct cmd_list_element *cmd, void *context)
00145 {
00146   cmd->context = context;
00147 }
00148 
00149 void *
00150 get_cmd_context (struct cmd_list_element *cmd)
00151 {
00152   return cmd->context;
00153 }
00154 
00155 enum cmd_types
00156 cmd_type (struct cmd_list_element *cmd)
00157 {
00158   return cmd->type;
00159 }
00160 
00161 void
00162 set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
00163 {
00164   cmd->completer = completer; /* Ok.  */
00165 }
00166 
00167 /* Add element named NAME.
00168    Space for NAME and DOC must be allocated by the caller.
00169    CLASS is the top level category into which commands are broken down
00170    for "help" purposes.
00171    FUN should be the function to execute the command;
00172    it will get a character string as argument, with leading
00173    and trailing blanks already eliminated.
00174 
00175    DOC is a documentation string for the command.
00176    Its first line should be a complete sentence.
00177    It should start with ? for a command that is an abbreviation
00178    or with * for a command that most users don't need to know about.
00179 
00180    Add this command to command list *LIST.
00181 
00182    Returns a pointer to the added command (not necessarily the head 
00183    of *LIST).  */
00184 
00185 struct cmd_list_element *
00186 add_cmd (const char *name, enum command_class class, void (*fun) (char *, int),
00187          char *doc, struct cmd_list_element **list)
00188 {
00189   struct cmd_list_element *c
00190     = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
00191   struct cmd_list_element *p, *iter;
00192 
00193   /* Turn each alias of the old command into an alias of the new
00194      command.  */
00195   c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
00196                            &c->hook_post, &c->hookee_post);
00197   for (iter = c->aliases; iter; iter = iter->alias_chain)
00198     iter->cmd_pointer = c;
00199   if (c->hook_pre)
00200     c->hook_pre->hookee_pre = c;
00201   if (c->hookee_pre)
00202     c->hookee_pre->hook_pre = c;
00203   if (c->hook_post)
00204     c->hook_post->hookee_post = c;
00205   if (c->hookee_post)
00206     c->hookee_post->hook_post = c;
00207 
00208   if (*list == NULL || strcmp ((*list)->name, name) >= 0)
00209     {
00210       c->next = *list;
00211       *list = c;
00212     }
00213   else
00214     {
00215       p = *list;
00216       while (p->next && strcmp (p->next->name, name) <= 0)
00217         {
00218           p = p->next;
00219         }
00220       c->next = p->next;
00221       p->next = c;
00222     }
00223 
00224   c->name = name;
00225   c->class = class;
00226   set_cmd_cfunc (c, fun);
00227   set_cmd_context (c, NULL);
00228   c->doc = doc;
00229   c->flags = 0;
00230   c->replacement = NULL;
00231   c->pre_show_hook = NULL;
00232   c->hook_in = 0;
00233   c->prefixlist = NULL;
00234   c->prefixname = NULL;
00235   c->allow_unknown = 0;
00236   c->prefix = NULL;
00237   c->abbrev_flag = 0;
00238   set_cmd_completer (c, make_symbol_completion_list_fn);
00239   c->destroyer = NULL;
00240   c->type = not_set_cmd;
00241   c->var = NULL;
00242   c->var_type = var_boolean;
00243   c->enums = NULL;
00244   c->user_commands = NULL;
00245   c->cmd_pointer = NULL;
00246   c->alias_chain = NULL;
00247 
00248   return c;
00249 }
00250 
00251 /* Deprecates a command CMD.
00252    REPLACEMENT is the name of the command which should be used in
00253    place of this command, or NULL if no such command exists.
00254 
00255    This function does not check to see if command REPLACEMENT exists
00256    since gdb may not have gotten around to adding REPLACEMENT when
00257    this function is called.
00258 
00259    Returns a pointer to the deprecated command.  */
00260 
00261 struct cmd_list_element *
00262 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
00263 {
00264   cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
00265 
00266   if (replacement != NULL)
00267     cmd->replacement = replacement;
00268   else
00269     cmd->replacement = NULL;
00270 
00271   return cmd;
00272 }
00273 
00274 struct cmd_list_element *
00275 add_alias_cmd (const char *name, const char *oldname, enum command_class class,
00276                int abbrev_flag, struct cmd_list_element **list)
00277 {
00278   const char *tmp;
00279   struct cmd_list_element *old;
00280   struct cmd_list_element *c;
00281 
00282   tmp = oldname;
00283   old = lookup_cmd (&tmp, *list, "", 1, 1);
00284 
00285   if (old == 0)
00286     {
00287       struct cmd_list_element *prehook, *prehookee, *posthook, *posthookee;
00288       struct cmd_list_element *aliases = delete_cmd (name, list,
00289                                                      &prehook, &prehookee,
00290                                                      &posthook, &posthookee);
00291 
00292       /* If this happens, it means a programmer error somewhere.  */
00293       gdb_assert (!aliases && !prehook && !prehookee
00294                   && !posthook && ! posthookee);
00295       return 0;
00296     }
00297 
00298   c = add_cmd (name, class, NULL, old->doc, list);
00299 
00300   /* If OLD->DOC can be freed, we should make another copy.  */
00301   if ((old->flags & DOC_ALLOCATED) != 0)
00302     {
00303       c->doc = xstrdup (old->doc);
00304       c->flags |= DOC_ALLOCATED;
00305     }
00306   /* NOTE: Both FUNC and all the FUNCTIONs need to be copied.  */
00307   c->func = old->func;
00308   c->function = old->function;
00309   c->prefixlist = old->prefixlist;
00310   c->prefixname = old->prefixname;
00311   c->allow_unknown = old->allow_unknown;
00312   c->abbrev_flag = abbrev_flag;
00313   c->cmd_pointer = old;
00314   c->alias_chain = old->aliases;
00315   old->aliases = c;
00316 
00317   set_cmd_prefix (c, list);
00318   return c;
00319 }
00320 
00321 /* Like add_cmd but adds an element for a command prefix: a name that
00322    should be followed by a subcommand to be looked up in another
00323    command list.  PREFIXLIST should be the address of the variable
00324    containing that list.  */
00325 
00326 struct cmd_list_element *
00327 add_prefix_cmd (const char *name, enum command_class class,
00328                 void (*fun) (char *, int),
00329                 char *doc, struct cmd_list_element **prefixlist,
00330                 char *prefixname, int allow_unknown,
00331                 struct cmd_list_element **list)
00332 {
00333   struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
00334   struct cmd_list_element *p;
00335 
00336   c->prefixlist = prefixlist;
00337   c->prefixname = prefixname;
00338   c->allow_unknown = allow_unknown;
00339 
00340   if (list == &cmdlist)
00341     c->prefix = NULL;
00342   else
00343     set_cmd_prefix (c, list);
00344 
00345   /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST.  */
00346   for (p = *prefixlist; p != NULL; p = p->next)
00347     p->prefix = c;
00348 
00349   return c;
00350 }
00351 
00352 /* Like add_prefix_cmd but sets the abbrev_flag on the new command.  */
00353 
00354 struct cmd_list_element *
00355 add_abbrev_prefix_cmd (const char *name, enum command_class class,
00356                        void (*fun) (char *, int), char *doc,
00357                        struct cmd_list_element **prefixlist, char *prefixname,
00358                        int allow_unknown, struct cmd_list_element **list)
00359 {
00360   struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
00361 
00362   c->prefixlist = prefixlist;
00363   c->prefixname = prefixname;
00364   c->allow_unknown = allow_unknown;
00365   c->abbrev_flag = 1;
00366   return c;
00367 }
00368 
00369 /* This is an empty "cfunc".  */
00370 void
00371 not_just_help_class_command (char *args, int from_tty)
00372 {
00373 }
00374 
00375 /* This is an empty "sfunc".  */
00376 static void empty_sfunc (char *, int, struct cmd_list_element *);
00377 
00378 static void
00379 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
00380 {
00381 }
00382 
00383 /* Add element named NAME to command list LIST (the list for set/show
00384    or some sublist thereof).
00385    TYPE is set_cmd or show_cmd.
00386    CLASS is as in add_cmd.
00387    VAR_TYPE is the kind of thing we are setting.
00388    VAR is address of the variable being controlled by this command.
00389    DOC is the documentation string.  */
00390 
00391 static struct cmd_list_element *
00392 add_set_or_show_cmd (const char *name,
00393                      enum cmd_types type,
00394                      enum command_class class,
00395                      var_types var_type,
00396                      void *var,
00397                      char *doc,
00398                      struct cmd_list_element **list)
00399 {
00400   struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);
00401 
00402   gdb_assert (type == set_cmd || type == show_cmd);
00403   c->type = type;
00404   c->var_type = var_type;
00405   c->var = var;
00406   /* This needs to be something besides NULL so that this isn't
00407      treated as a help class.  */
00408   set_cmd_sfunc (c, empty_sfunc);
00409   return c;
00410 }
00411 
00412 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
00413    CLASS is as in add_cmd.  VAR_TYPE is the kind of thing we are
00414    setting.  VAR is address of the variable being controlled by this
00415    command.  SET_FUNC and SHOW_FUNC are the callback functions (if
00416    non-NULL).  SET_DOC, SHOW_DOC and HELP_DOC are the documentation
00417    strings.  PRINT the format string to print the value.  SET_RESULT
00418    and SHOW_RESULT, if not NULL, are set to the resulting command
00419    structures.  */
00420 
00421 static void
00422 add_setshow_cmd_full (const char *name,
00423                       enum command_class class,
00424                       var_types var_type, void *var,
00425                       const char *set_doc, const char *show_doc,
00426                       const char *help_doc,
00427                       cmd_sfunc_ftype *set_func,
00428                       show_value_ftype *show_func,
00429                       struct cmd_list_element **set_list,
00430                       struct cmd_list_element **show_list,
00431                       struct cmd_list_element **set_result,
00432                       struct cmd_list_element **show_result)
00433 {
00434   struct cmd_list_element *set;
00435   struct cmd_list_element *show;
00436   char *full_set_doc;
00437   char *full_show_doc;
00438 
00439   if (help_doc != NULL)
00440     {
00441       full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
00442       full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
00443     }
00444   else
00445     {
00446       full_set_doc = xstrdup (set_doc);
00447       full_show_doc = xstrdup (show_doc);
00448     }
00449   set = add_set_or_show_cmd (name, set_cmd, class, var_type, var,
00450                              full_set_doc, set_list);
00451   set->flags |= DOC_ALLOCATED;
00452 
00453   if (set_func != NULL)
00454     set_cmd_sfunc (set, set_func);
00455 
00456   set_cmd_prefix (set, set_list);
00457 
00458   show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
00459                               full_show_doc, show_list);
00460   show->flags |= DOC_ALLOCATED;
00461   show->show_value_func = show_func;
00462 
00463   if (set_result != NULL)
00464     *set_result = set;
00465   if (show_result != NULL)
00466     *show_result = show;
00467 }
00468 
00469 /* Add element named NAME to command list LIST (the list for set or
00470    some sublist thereof).  CLASS is as in add_cmd.  ENUMLIST is a list
00471    of strings which may follow NAME.  VAR is address of the variable
00472    which will contain the matching string (from ENUMLIST).  */
00473 
00474 void
00475 add_setshow_enum_cmd (const char *name,
00476                       enum command_class class,
00477                       const char *const *enumlist,
00478                       const char **var,
00479                       const char *set_doc,
00480                       const char *show_doc,
00481                       const char *help_doc,
00482                       cmd_sfunc_ftype *set_func,
00483                       show_value_ftype *show_func,
00484                       struct cmd_list_element **set_list,
00485                       struct cmd_list_element **show_list)
00486 {
00487   struct cmd_list_element *c;
00488 
00489   add_setshow_cmd_full (name, class, var_enum, var,
00490                         set_doc, show_doc, help_doc,
00491                         set_func, show_func,
00492                         set_list, show_list,
00493                         &c, NULL);
00494   c->enums = enumlist;
00495 }
00496 
00497 const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
00498 
00499 /* Add an auto-boolean command named NAME to both the set and show
00500    command list lists.  CLASS is as in add_cmd.  VAR is address of the
00501    variable which will contain the value.  DOC is the documentation
00502    string.  FUNC is the corresponding callback.  */
00503 void
00504 add_setshow_auto_boolean_cmd (const char *name,
00505                               enum command_class class,
00506                               enum auto_boolean *var,
00507                               const char *set_doc, const char *show_doc,
00508                               const char *help_doc,
00509                               cmd_sfunc_ftype *set_func,
00510                               show_value_ftype *show_func,
00511                               struct cmd_list_element **set_list,
00512                               struct cmd_list_element **show_list)
00513 {
00514   struct cmd_list_element *c;
00515 
00516   add_setshow_cmd_full (name, class, var_auto_boolean, var,
00517                         set_doc, show_doc, help_doc,
00518                         set_func, show_func,
00519                         set_list, show_list,
00520                         &c, NULL);
00521   c->enums = auto_boolean_enums;
00522 }
00523 
00524 /* Add element named NAME to both the set and show command LISTs (the
00525    list for set/show or some sublist thereof).  CLASS is as in
00526    add_cmd.  VAR is address of the variable which will contain the
00527    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
00528 void
00529 add_setshow_boolean_cmd (const char *name, enum command_class class, int *var,
00530                          const char *set_doc, const char *show_doc,
00531                          const char *help_doc,
00532                          cmd_sfunc_ftype *set_func,
00533                          show_value_ftype *show_func,
00534                          struct cmd_list_element **set_list,
00535                          struct cmd_list_element **show_list)
00536 {
00537   static const char *boolean_enums[] = { "on", "off", NULL };
00538   struct cmd_list_element *c;
00539 
00540   add_setshow_cmd_full (name, class, var_boolean, var,
00541                         set_doc, show_doc, help_doc,
00542                         set_func, show_func,
00543                         set_list, show_list,
00544                         &c, NULL);
00545   c->enums = boolean_enums;
00546 }
00547 
00548 /* Add element named NAME to both the set and show command LISTs (the
00549    list for set/show or some sublist thereof).  */
00550 void
00551 add_setshow_filename_cmd (const char *name, enum command_class class,
00552                           char **var,
00553                           const char *set_doc, const char *show_doc,
00554                           const char *help_doc,
00555                           cmd_sfunc_ftype *set_func,
00556                           show_value_ftype *show_func,
00557                           struct cmd_list_element **set_list,
00558                           struct cmd_list_element **show_list)
00559 {
00560   struct cmd_list_element *set_result;
00561 
00562   add_setshow_cmd_full (name, class, var_filename, var,
00563                         set_doc, show_doc, help_doc,
00564                         set_func, show_func,
00565                         set_list, show_list,
00566                         &set_result, NULL);
00567   set_cmd_completer (set_result, filename_completer);
00568 }
00569 
00570 /* Add element named NAME to both the set and show command LISTs (the
00571    list for set/show or some sublist thereof).  */
00572 void
00573 add_setshow_string_cmd (const char *name, enum command_class class,
00574                         char **var,
00575                         const char *set_doc, const char *show_doc,
00576                         const char *help_doc,
00577                         cmd_sfunc_ftype *set_func,
00578                         show_value_ftype *show_func,
00579                         struct cmd_list_element **set_list,
00580                         struct cmd_list_element **show_list)
00581 {
00582   add_setshow_cmd_full (name, class, var_string, var,
00583                         set_doc, show_doc, help_doc,
00584                         set_func, show_func,
00585                         set_list, show_list,
00586                         NULL, NULL);
00587 }
00588 
00589 /* Add element named NAME to both the set and show command LISTs (the
00590    list for set/show or some sublist thereof).  */
00591 struct cmd_list_element *
00592 add_setshow_string_noescape_cmd (const char *name, enum command_class class,
00593                                  char **var,
00594                                  const char *set_doc, const char *show_doc,
00595                                  const char *help_doc,
00596                                  cmd_sfunc_ftype *set_func,
00597                                  show_value_ftype *show_func,
00598                                  struct cmd_list_element **set_list,
00599                                  struct cmd_list_element **show_list)
00600 {
00601   struct cmd_list_element *set_cmd;
00602 
00603   add_setshow_cmd_full (name, class, var_string_noescape, var,
00604                         set_doc, show_doc, help_doc,
00605                         set_func, show_func,
00606                         set_list, show_list,
00607                         &set_cmd, NULL);
00608   return set_cmd;
00609 }
00610 
00611 /* Add element named NAME to both the set and show command LISTs (the
00612    list for set/show or some sublist thereof).  */
00613 void
00614 add_setshow_optional_filename_cmd (const char *name, enum command_class class,
00615                                    char **var,
00616                                    const char *set_doc, const char *show_doc,
00617                                    const char *help_doc,
00618                                    cmd_sfunc_ftype *set_func,
00619                                    show_value_ftype *show_func,
00620                                    struct cmd_list_element **set_list,
00621                                    struct cmd_list_element **show_list)
00622 {
00623   struct cmd_list_element *set_result;
00624  
00625   add_setshow_cmd_full (name, class, var_optional_filename, var,
00626                         set_doc, show_doc, help_doc,
00627                         set_func, show_func,
00628                         set_list, show_list,
00629                         &set_result, NULL);
00630                 
00631   set_cmd_completer (set_result, filename_completer);
00632 
00633 }
00634 
00635 /* Completes on literal "unlimited".  Used by integer commands that
00636    support a special "unlimited" value.  */
00637 
00638 static VEC (char_ptr) *
00639 integer_unlimited_completer (struct cmd_list_element *ignore,
00640                              const char *text, const char *word)
00641 {
00642   static const char * const keywords[] =
00643     {
00644       "unlimited",
00645       NULL,
00646     };
00647 
00648   return complete_on_enum (keywords, text, word);
00649 }
00650 
00651 /* Add element named NAME to both the set and show command LISTs (the
00652    list for set/show or some sublist thereof).  CLASS is as in
00653    add_cmd.  VAR is address of the variable which will contain the
00654    value.  SET_DOC and SHOW_DOC are the documentation strings.  This
00655    function is only used in Python API.  Please don't use it elsewhere.  */
00656 void
00657 add_setshow_integer_cmd (const char *name, enum command_class class,
00658                          int *var,
00659                          const char *set_doc, const char *show_doc,
00660                          const char *help_doc,
00661                          cmd_sfunc_ftype *set_func,
00662                          show_value_ftype *show_func,
00663                          struct cmd_list_element **set_list,
00664                          struct cmd_list_element **show_list)
00665 {
00666   struct cmd_list_element *set;
00667 
00668   add_setshow_cmd_full (name, class, var_integer, var,
00669                         set_doc, show_doc, help_doc,
00670                         set_func, show_func,
00671                         set_list, show_list,
00672                         &set, NULL);
00673 
00674   set_cmd_completer (set, integer_unlimited_completer);
00675 }
00676 
00677 /* Add element named NAME to both the set and show command LISTs (the
00678    list for set/show or some sublist thereof).  CLASS is as in
00679    add_cmd.  VAR is address of the variable which will contain the
00680    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
00681 void
00682 add_setshow_uinteger_cmd (const char *name, enum command_class class,
00683                           unsigned int *var,
00684                           const char *set_doc, const char *show_doc,
00685                           const char *help_doc,
00686                           cmd_sfunc_ftype *set_func,
00687                           show_value_ftype *show_func,
00688                           struct cmd_list_element **set_list,
00689                           struct cmd_list_element **show_list)
00690 {
00691   struct cmd_list_element *set;
00692 
00693   add_setshow_cmd_full (name, class, var_uinteger, var,
00694                         set_doc, show_doc, help_doc,
00695                         set_func, show_func,
00696                         set_list, show_list,
00697                         &set, NULL);
00698 
00699   set_cmd_completer (set, integer_unlimited_completer);
00700 }
00701 
00702 /* Add element named NAME to both the set and show command LISTs (the
00703    list for set/show or some sublist thereof).  CLASS is as in
00704    add_cmd.  VAR is address of the variable which will contain the
00705    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
00706 void
00707 add_setshow_zinteger_cmd (const char *name, enum command_class class,
00708                           int *var,
00709                           const char *set_doc, const char *show_doc,
00710                           const char *help_doc,
00711                           cmd_sfunc_ftype *set_func,
00712                           show_value_ftype *show_func,
00713                           struct cmd_list_element **set_list,
00714                           struct cmd_list_element **show_list)
00715 {
00716   add_setshow_cmd_full (name, class, var_zinteger, var,
00717                         set_doc, show_doc, help_doc,
00718                         set_func, show_func,
00719                         set_list, show_list,
00720                         NULL, NULL);
00721 }
00722 
00723 void
00724 add_setshow_zuinteger_unlimited_cmd (const char *name,
00725                                      enum command_class class,
00726                                      int *var,
00727                                      const char *set_doc,
00728                                      const char *show_doc,
00729                                      const char *help_doc,
00730                                      cmd_sfunc_ftype *set_func,
00731                                      show_value_ftype *show_func,
00732                                      struct cmd_list_element **set_list,
00733                                      struct cmd_list_element **show_list)
00734 {
00735   struct cmd_list_element *set;
00736 
00737   add_setshow_cmd_full (name, class, var_zuinteger_unlimited, var,
00738                         set_doc, show_doc, help_doc,
00739                         set_func, show_func,
00740                         set_list, show_list,
00741                         &set, NULL);
00742 
00743   set_cmd_completer (set, integer_unlimited_completer);
00744 }
00745 
00746 /* Add element named NAME to both the set and show command LISTs (the
00747    list for set/show or some sublist thereof).  CLASS is as in
00748    add_cmd.  VAR is address of the variable which will contain the
00749    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
00750 void
00751 add_setshow_zuinteger_cmd (const char *name, enum command_class class,
00752                            unsigned int *var,
00753                            const char *set_doc, const char *show_doc,
00754                            const char *help_doc,
00755                            cmd_sfunc_ftype *set_func,
00756                            show_value_ftype *show_func,
00757                            struct cmd_list_element **set_list,
00758                            struct cmd_list_element **show_list)
00759 {
00760   add_setshow_cmd_full (name, class, var_zuinteger, var,
00761                         set_doc, show_doc, help_doc,
00762                         set_func, show_func,
00763                         set_list, show_list,
00764                         NULL, NULL);
00765 }
00766 
00767 /* Remove the command named NAME from the command list.  Return the
00768    list commands which were aliased to the deleted command.  If the
00769    command had no aliases, return NULL.  The various *HOOKs are set to
00770    the pre- and post-hook commands for the deleted command.  If the
00771    command does not have a hook, the corresponding out parameter is
00772    set to NULL.  */
00773 
00774 static struct cmd_list_element *
00775 delete_cmd (const char *name, struct cmd_list_element **list,
00776             struct cmd_list_element **prehook,
00777             struct cmd_list_element **prehookee,
00778             struct cmd_list_element **posthook,
00779             struct cmd_list_element **posthookee)
00780 {
00781   struct cmd_list_element *iter;
00782   struct cmd_list_element **previous_chain_ptr;
00783   struct cmd_list_element *aliases = NULL;
00784 
00785   *prehook = NULL;
00786   *prehookee = NULL;
00787   *posthook = NULL;
00788   *posthookee = NULL;
00789   previous_chain_ptr = list;
00790 
00791   for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
00792     {
00793       if (strcmp (iter->name, name) == 0)
00794         {
00795           if (iter->destroyer)
00796             iter->destroyer (iter, iter->context);
00797           if (iter->hookee_pre)
00798             iter->hookee_pre->hook_pre = 0;
00799           *prehook = iter->hook_pre;
00800           *prehookee = iter->hookee_pre;
00801           if (iter->hookee_post)
00802             iter->hookee_post->hook_post = 0;
00803           if (iter->doc && (iter->flags & DOC_ALLOCATED) != 0)
00804             xfree (iter->doc);
00805           *posthook = iter->hook_post;
00806           *posthookee = iter->hookee_post;
00807 
00808           /* Update the link.  */
00809           *previous_chain_ptr = iter->next;
00810 
00811           aliases = iter->aliases;
00812 
00813           /* If this command was an alias, remove it from the list of
00814              aliases.  */
00815           if (iter->cmd_pointer)
00816             {
00817               struct cmd_list_element **prevp = &iter->cmd_pointer->aliases;
00818               struct cmd_list_element *a = *prevp;
00819 
00820               while (a != iter)
00821                 {
00822                   prevp = &a->alias_chain;
00823                   a = *prevp;
00824                 }
00825               *prevp = iter->alias_chain;
00826             }
00827 
00828           xfree (iter);
00829 
00830           /* We won't see another command with the same name.  */
00831           break;
00832         }
00833       else
00834         previous_chain_ptr = &iter->next;
00835     }
00836 
00837   return aliases;
00838 }
00839 
00840 /* Shorthands to the commands above.  */
00841 
00842 /* Add an element to the list of info subcommands.  */
00843 
00844 struct cmd_list_element *
00845 add_info (const char *name, void (*fun) (char *, int), char *doc)
00846 {
00847   return add_cmd (name, no_class, fun, doc, &infolist);
00848 }
00849 
00850 /* Add an alias to the list of info subcommands.  */
00851 
00852 struct cmd_list_element *
00853 add_info_alias (const char *name, char *oldname, int abbrev_flag)
00854 {
00855   return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
00856 }
00857 
00858 /* Add an element to the list of commands.  */
00859 
00860 struct cmd_list_element *
00861 add_com (const char *name, enum command_class class, void (*fun) (char *, int),
00862          char *doc)
00863 {
00864   return add_cmd (name, class, fun, doc, &cmdlist);
00865 }
00866 
00867 /* Add an alias or abbreviation command to the list of commands.  */
00868 
00869 struct cmd_list_element *
00870 add_com_alias (const char *name, const char *oldname, enum command_class class,
00871                int abbrev_flag)
00872 {
00873   return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
00874 }
00875 
00876 /* Recursively walk the commandlist structures, and print out the
00877    documentation of commands that match our regex in either their
00878    name, or their documentation.
00879 */
00880 void 
00881 apropos_cmd (struct ui_file *stream, 
00882              struct cmd_list_element *commandlist,
00883              struct re_pattern_buffer *regex, char *prefix)
00884 {
00885   struct cmd_list_element *c;
00886   int returnvalue;
00887 
00888   /* Walk through the commands.  */
00889   for (c=commandlist;c;c=c->next)
00890     {
00891       returnvalue = -1; /* Needed to avoid double printing.  */
00892       if (c->name != NULL)
00893         {
00894           /* Try to match against the name.  */
00895           returnvalue = re_search (regex, c->name, strlen(c->name),
00896                                    0, strlen (c->name), NULL);
00897           if (returnvalue >= 0)
00898             {
00899               print_help_for_command (c, prefix, 
00900                                       0 /* don't recurse */, stream);
00901             }
00902         }
00903       if (c->doc != NULL && returnvalue < 0)
00904         {
00905           /* Try to match against documentation.  */
00906           if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
00907             {
00908               print_help_for_command (c, prefix, 
00909                                       0 /* don't recurse */, stream);
00910             }
00911         }
00912       /* Check if this command has subcommands and is not an
00913          abbreviation.  We skip listing subcommands of abbreviations
00914          in order to avoid duplicates in the output.  */
00915       if (c->prefixlist != NULL && !c->abbrev_flag)
00916         {
00917           /* Recursively call ourselves on the subcommand list,
00918              passing the right prefix in.  */
00919           apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
00920         }
00921     }
00922 }
00923 
00924 /* This command really has to deal with two things:
00925    1) I want documentation on *this string* (usually called by
00926       "help commandname").
00927 
00928    2) I want documentation on *this list* (usually called by giving a
00929       command that requires subcommands.  Also called by saying just
00930       "help".)
00931 
00932    I am going to split this into two seperate comamnds, help_cmd and
00933    help_list.  */
00934 
00935 void
00936 help_cmd (char *arg, struct ui_file *stream)
00937 {
00938   struct cmd_list_element *c;
00939   const char *command = arg;
00940 
00941   if (!command)
00942     {
00943       help_list (cmdlist, "", all_classes, stream);
00944       return;
00945     }
00946 
00947   if (strcmp (command, "all") == 0)
00948     {
00949       help_all (stream);
00950       return;
00951     }
00952 
00953   c = lookup_cmd (&command, cmdlist, "", 0, 0);
00954 
00955   if (c == 0)
00956     return;
00957 
00958   /* There are three cases here.
00959      If c->prefixlist is nonzero, we have a prefix command.
00960      Print its documentation, then list its subcommands.
00961 
00962      If c->func is non NULL, we really have a command.  Print its
00963      documentation and return.
00964 
00965      If c->func is NULL, we have a class name.  Print its
00966      documentation (as if it were a command) and then set class to the
00967      number of this class so that the commands in the class will be
00968      listed.  */
00969 
00970   fputs_filtered (c->doc, stream);
00971   fputs_filtered ("\n", stream);
00972 
00973   if (c->prefixlist == 0 && c->func != NULL)
00974     return;
00975   fprintf_filtered (stream, "\n");
00976 
00977   /* If this is a prefix command, print it's subcommands.  */
00978   if (c->prefixlist)
00979     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
00980 
00981   /* If this is a class name, print all of the commands in the class.  */
00982   if (c->func == NULL)
00983     help_list (cmdlist, "", c->class, stream);
00984 
00985   if (c->hook_pre || c->hook_post)
00986     fprintf_filtered (stream,
00987                       "\nThis command has a hook (or hooks) defined:\n");
00988 
00989   if (c->hook_pre)
00990     fprintf_filtered (stream,
00991                       "\tThis command is run after  : %s (pre hook)\n",
00992                     c->hook_pre->name);
00993   if (c->hook_post)
00994     fprintf_filtered (stream,
00995                       "\tThis command is run before : %s (post hook)\n",
00996                     c->hook_post->name);
00997 }
00998 
00999 /*
01000  * Get a specific kind of help on a command list.
01001  *
01002  * LIST is the list.
01003  * CMDTYPE is the prefix to use in the title string.
01004  * CLASS is the class with which to list the nodes of this list (see
01005  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
01006  * everything, ALL_CLASSES for just classes, and non-negative for only things
01007  * in a specific class.
01008  * and STREAM is the output stream on which to print things.
01009  * If you call this routine with a class >= 0, it recurses.
01010  */
01011 void
01012 help_list (struct cmd_list_element *list, char *cmdtype,
01013            enum command_class class, struct ui_file *stream)
01014 {
01015   int len;
01016   char *cmdtype1, *cmdtype2;
01017 
01018   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
01019    */
01020   len = strlen (cmdtype);
01021   cmdtype1 = (char *) alloca (len + 1);
01022   cmdtype1[0] = 0;
01023   cmdtype2 = (char *) alloca (len + 4);
01024   cmdtype2[0] = 0;
01025   if (len)
01026     {
01027       cmdtype1[0] = ' ';
01028       strncpy (cmdtype1 + 1, cmdtype, len - 1);
01029       cmdtype1[len] = 0;
01030       strncpy (cmdtype2, cmdtype, len - 1);
01031       strcpy (cmdtype2 + len - 1, " sub");
01032     }
01033 
01034   if (class == all_classes)
01035     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
01036   else
01037     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
01038 
01039   help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
01040 
01041   if (class == all_classes)
01042     {
01043       fprintf_filtered (stream, "\n\
01044 Type \"help%s\" followed by a class name for a list of commands in ",
01045                         cmdtype1);
01046       wrap_here ("");
01047       fprintf_filtered (stream, "that class.");
01048 
01049       fprintf_filtered (stream, "\n\
01050 Type \"help all\" for the list of all commands.");
01051     }
01052 
01053   fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
01054                     cmdtype1, cmdtype2);
01055   wrap_here ("");
01056   fputs_filtered ("for ", stream);
01057   wrap_here ("");
01058   fputs_filtered ("full ", stream);
01059   wrap_here ("");
01060   fputs_filtered ("documentation.\n", stream);
01061   fputs_filtered ("Type \"apropos word\" to search "
01062                   "for commands related to \"word\".\n", stream);
01063   fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
01064                   stream);
01065 }
01066 
01067 static void
01068 help_all (struct ui_file *stream)
01069 {
01070   struct cmd_list_element *c;
01071   int seen_unclassified = 0;
01072 
01073   for (c = cmdlist; c; c = c->next)
01074     {
01075       if (c->abbrev_flag)
01076         continue;
01077       /* If this is a class name, print all of the commands in the
01078          class.  */
01079 
01080       if (c->func == NULL)
01081         {
01082           fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
01083           help_cmd_list (cmdlist, c->class, "", 1, stream);
01084         }
01085     }
01086 
01087   /* While it's expected that all commands are in some class,
01088      as a safety measure, we'll print commands outside of any
01089      class at the end.  */
01090 
01091   for (c = cmdlist; c; c = c->next)
01092     {
01093       if (c->abbrev_flag)
01094         continue;
01095 
01096       if (c->class == no_class)
01097         {
01098           if (!seen_unclassified)
01099             {
01100               fprintf_filtered (stream, "\nUnclassified commands\n\n");
01101               seen_unclassified = 1;
01102             }
01103           print_help_for_command (c, "", 1, stream);
01104         }
01105     }
01106 
01107 }
01108 
01109 /* Print only the first line of STR on STREAM.  */
01110 void
01111 print_doc_line (struct ui_file *stream, char *str)
01112 {
01113   static char *line_buffer = 0;
01114   static int line_size;
01115   char *p;
01116 
01117   if (!line_buffer)
01118     {
01119       line_size = 80;
01120       line_buffer = (char *) xmalloc (line_size);
01121     }
01122 
01123   /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
01124      like '.gdbinit'.  */
01125   p = str;
01126   while (*p && *p != '\n'
01127          && ((*p != '.' && *p != ',') || (p[1] && !isspace (p[1]))))
01128     p++;
01129   if (p - str > line_size - 1)
01130     {
01131       line_size = p - str + 1;
01132       xfree (line_buffer);
01133       line_buffer = (char *) xmalloc (line_size);
01134     }
01135   strncpy (line_buffer, str, p - str);
01136   line_buffer[p - str] = '\0';
01137   if (islower (line_buffer[0]))
01138     line_buffer[0] = toupper (line_buffer[0]);
01139   fputs_filtered (line_buffer, stream);
01140 }
01141 
01142 /* Print one-line help for command C.
01143    If RECURSE is non-zero, also print one-line descriptions
01144    of all prefixed subcommands.  */
01145 static void
01146 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
01147                         struct ui_file *stream)
01148 {
01149   fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
01150   print_doc_line (stream, c->doc);
01151   fputs_filtered ("\n", stream);
01152   
01153   if (recurse
01154       && c->prefixlist != 0
01155       && c->abbrev_flag == 0)
01156     /* Subcommands of a prefix command typically have 'all_commands'
01157        as class.  If we pass CLASS to recursive invocation,
01158        most often we won't see anything.  */
01159     help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream);
01160 }
01161 
01162 /*
01163  * Implement a help command on command list LIST.
01164  * RECURSE should be non-zero if this should be done recursively on
01165  * all sublists of LIST.
01166  * PREFIX is the prefix to print before each command name.
01167  * STREAM is the stream upon which the output should be written.
01168  * CLASS should be:
01169  *      A non-negative class number to list only commands in that
01170  * class.
01171  *      ALL_COMMANDS to list all commands in list.
01172  *      ALL_CLASSES  to list all classes in list.
01173  *
01174  *   Note that RECURSE will be active on *all* sublists, not just the
01175  * ones selected by the criteria above (ie. the selection mechanism
01176  * is at the low level, not the high-level).
01177  */
01178 void
01179 help_cmd_list (struct cmd_list_element *list, enum command_class class,
01180                char *prefix, int recurse, struct ui_file *stream)
01181 {
01182   struct cmd_list_element *c;
01183 
01184   for (c = list; c; c = c->next)
01185     {      
01186       if (c->abbrev_flag == 0
01187           && (class == all_commands
01188               || (class == all_classes && c->func == NULL)
01189               || (class == c->class && c->func != NULL)))
01190         {
01191           print_help_for_command (c, prefix, recurse, stream);
01192         }
01193       else if (c->abbrev_flag == 0 && recurse
01194                && class == class_user && c->prefixlist != NULL)
01195         /* User-defined commands may be subcommands.  */
01196         help_cmd_list (*c->prefixlist, class, c->prefixname, 
01197                        recurse, stream);
01198     }
01199 }
01200 
01201 
01202 /* Search the input clist for 'command'.  Return the command if
01203    found (or NULL if not), and return the number of commands
01204    found in nfound.  */
01205 
01206 static struct cmd_list_element *
01207 find_cmd (const char *command, int len, struct cmd_list_element *clist,
01208           int ignore_help_classes, int *nfound)
01209 {
01210   struct cmd_list_element *found, *c;
01211 
01212   found = (struct cmd_list_element *) NULL;
01213   *nfound = 0;
01214   for (c = clist; c; c = c->next)
01215     if (!strncmp (command, c->name, len)
01216         && (!ignore_help_classes || c->func))
01217       {
01218         found = c;
01219         (*nfound)++;
01220         if (c->name[len] == '\0')
01221           {
01222             *nfound = 1;
01223             break;
01224           }
01225       }
01226   return found;
01227 }
01228 
01229 static int
01230 find_command_name_length (const char *text)
01231 {
01232   const char *p = text;
01233 
01234   /* Treating underscores as part of command words is important
01235      so that "set args_foo()" doesn't get interpreted as
01236      "set args _foo()".  */
01237   /* Some characters are only used for TUI specific commands.
01238      However, they are always allowed for the sake of consistency.
01239 
01240      The XDB compatibility characters are only allowed when using the
01241      right mode because they clash with other GDB commands -
01242      specifically '/' is used as a suffix for print, examine and
01243      display.
01244 
01245      Note that this is larger than the character set allowed when
01246      creating user-defined commands.  */
01247 
01248   /* Recognize '!' as a single character command so that, e.g., "!ls"
01249      works as expected.  */
01250   if (*p == '!')
01251     return 1;
01252 
01253   while (isalnum (*p) || *p == '-' || *p == '_'
01254          /* Characters used by TUI specific commands.  */
01255          || *p == '+' || *p == '<' || *p == '>' || *p == '$'
01256          /* Characters used for XDB compatibility.  */
01257          || (xdb_commands && (*p == '/' || *p == '?')))
01258     p++;
01259 
01260   return p - text;
01261 }
01262 
01263 /* Return TRUE if NAME is a valid user-defined command name.
01264    This is a stricter subset of all gdb commands,
01265    see find_command_name_length.  */
01266 
01267 int
01268 valid_user_defined_cmd_name_p (const char *name)
01269 {
01270   const char *p;
01271 
01272   if (*name == '\0')
01273     return FALSE;
01274 
01275   /* Alas "42" is a legitimate user-defined command.
01276      In the interests of not breaking anything we preserve that.  */
01277 
01278   for (p = name; *p != '\0'; ++p)
01279     {
01280       if (isalnum (*p)
01281           || *p == '-'
01282           || *p == '_')
01283         ; /* Ok.  */
01284       else
01285         return FALSE;
01286     }
01287 
01288   return TRUE;
01289 }
01290 
01291 /* This routine takes a line of TEXT and a CLIST in which to start the
01292    lookup.  When it returns it will have incremented the text pointer past
01293    the section of text it matched, set *RESULT_LIST to point to the list in
01294    which the last word was matched, and will return a pointer to the cmd
01295    list element which the text matches.  It will return NULL if no match at
01296    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
01297    matches are possible; in this case *RESULT_LIST will be set to point to
01298    the list in which there are ambiguous choices (and *TEXT will be set to
01299    the ambiguous text string).
01300 
01301    If the located command was an abbreviation, this routine returns the base
01302    command of the abbreviation.
01303 
01304    It does no error reporting whatsoever; control will always return
01305    to the superior routine.
01306 
01307    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
01308    at the prefix_command (ie. the best match) *or* (special case) will be NULL
01309    if no prefix command was ever found.  For example, in the case of "info a",
01310    "info" matches without ambiguity, but "a" could be "args" or "address", so
01311    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
01312    RESULT_LIST should not be interpeted as a pointer to the beginning of a
01313    list; it simply points to a specific command.  In the case of an ambiguous
01314    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
01315    "info t" can be "info types" or "info target"; upon return *TEXT has been
01316    advanced past "info ").
01317 
01318    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
01319    affect the operation).
01320 
01321    This routine does *not* modify the text pointed to by TEXT.
01322 
01323    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
01324    are actually help classes rather than commands (i.e. the function field of
01325    the struct cmd_list_element is NULL).  */
01326 
01327 struct cmd_list_element *
01328 lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
01329               struct cmd_list_element **result_list, int ignore_help_classes)
01330 {
01331   char *command;
01332   int len, tmp, nfound;
01333   struct cmd_list_element *found, *c;
01334   const char *line = *text;
01335 
01336   while (**text == ' ' || **text == '\t')
01337     (*text)++;
01338 
01339   /* Identify the name of the command.  */
01340   len = find_command_name_length (*text);
01341 
01342   /* If nothing but whitespace, return 0.  */
01343   if (len == 0)
01344     return 0;
01345 
01346   /* *text and p now bracket the first command word to lookup (and
01347      it's length is len).  We copy this into a local temporary.  */
01348 
01349 
01350   command = (char *) alloca (len + 1);
01351   memcpy (command, *text, len);
01352   command[len] = '\0';
01353 
01354   /* Look it up.  */
01355   found = 0;
01356   nfound = 0;
01357   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
01358 
01359   /* We didn't find the command in the entered case, so lower case it
01360      and search again.  */
01361   if (!found || nfound == 0)
01362     {
01363       for (tmp = 0; tmp < len; tmp++)
01364         {
01365           char x = command[tmp];
01366 
01367           command[tmp] = isupper (x) ? tolower (x) : x;
01368         }
01369       found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
01370     }
01371 
01372   /* If nothing matches, we have a simple failure.  */
01373   if (nfound == 0)
01374     return 0;
01375 
01376   if (nfound > 1)
01377     {
01378       if (result_list != NULL)
01379         /* Will be modified in calling routine
01380            if we know what the prefix command is.  */
01381         *result_list = 0;
01382       return CMD_LIST_AMBIGUOUS;        /* Ambiguous.  */
01383     }
01384 
01385   /* We've matched something on this list.  Move text pointer forward.  */
01386 
01387   *text += len;
01388 
01389   if (found->cmd_pointer)
01390     {
01391       /* We drop the alias (abbreviation) in favor of the command it
01392        is pointing to.  If the alias is deprecated, though, we need to
01393        warn the user about it before we drop it.  Note that while we
01394        are warning about the alias, we may also warn about the command
01395        itself and we will adjust the appropriate DEPRECATED_WARN_USER
01396        flags.  */
01397       
01398       if (found->flags & DEPRECATED_WARN_USER)
01399         deprecated_cmd_warning (line);
01400       found = found->cmd_pointer;
01401     }
01402   /* If we found a prefix command, keep looking.  */
01403 
01404   if (found->prefixlist)
01405     {
01406       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
01407                         ignore_help_classes);
01408       if (!c)
01409         {
01410           /* Didn't find anything; this is as far as we got.  */
01411           if (result_list != NULL)
01412             *result_list = clist;
01413           return found;
01414         }
01415       else if (c == CMD_LIST_AMBIGUOUS)
01416         {
01417           /* We've gotten this far properly, but the next step is
01418              ambiguous.  We need to set the result list to the best
01419              we've found (if an inferior hasn't already set it).  */
01420           if (result_list != NULL)
01421             if (!*result_list)
01422               /* This used to say *result_list = *found->prefixlist.
01423                  If that was correct, need to modify the documentation
01424                  at the top of this function to clarify what is
01425                  supposed to be going on.  */
01426               *result_list = found;
01427           return c;
01428         }
01429       else
01430         {
01431           /* We matched!  */
01432           return c;
01433         }
01434     }
01435   else
01436     {
01437       if (result_list != NULL)
01438         *result_list = clist;
01439       return found;
01440     }
01441 }
01442 
01443 /* All this hair to move the space to the front of cmdtype */
01444 
01445 static void
01446 undef_cmd_error (const char *cmdtype, const char *q)
01447 {
01448   error (_("Undefined %scommand: \"%s\".  Try \"help%s%.*s\"."),
01449          cmdtype,
01450          q,
01451          *cmdtype ? " " : "",
01452          (int) strlen (cmdtype) - 1,
01453          cmdtype);
01454 }
01455 
01456 /* Look up the contents of *LINE as a command in the command list LIST.
01457    LIST is a chain of struct cmd_list_element's.
01458    If it is found, return the struct cmd_list_element for that command
01459    and update *LINE to point after the command name, at the first argument.
01460    If not found, call error if ALLOW_UNKNOWN is zero
01461    otherwise (or if error returns) return zero.
01462    Call error if specified command is ambiguous,
01463    unless ALLOW_UNKNOWN is negative.
01464    CMDTYPE precedes the word "command" in the error message.
01465 
01466    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
01467    elements which are actually help classes rather than commands (i.e.
01468    the function field of the struct cmd_list_element is 0).  */
01469 
01470 struct cmd_list_element *
01471 lookup_cmd (const char **line, struct cmd_list_element *list, char *cmdtype,
01472             int allow_unknown, int ignore_help_classes)
01473 {
01474   struct cmd_list_element *last_list = 0;
01475   struct cmd_list_element *c;
01476 
01477   /* Note: Do not remove trailing whitespace here because this
01478      would be wrong for complete_command.  Jim Kingdon  */
01479 
01480   if (!*line)
01481     error (_("Lack of needed %scommand"), cmdtype);
01482 
01483   c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
01484 
01485   if (!c)
01486     {
01487       if (!allow_unknown)
01488         {
01489           char *q;
01490           int len = find_command_name_length (*line);
01491 
01492           q = (char *) alloca (len + 1);
01493           strncpy (q, *line, len);
01494           q[len] = '\0';
01495           undef_cmd_error (cmdtype, q);
01496         }
01497       else
01498         return 0;
01499     }
01500   else if (c == CMD_LIST_AMBIGUOUS)
01501     {
01502       /* Ambigous.  Local values should be off prefixlist or called
01503          values.  */
01504       int local_allow_unknown = (last_list ? last_list->allow_unknown :
01505                                  allow_unknown);
01506       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
01507       struct cmd_list_element *local_list =
01508         (last_list ? *(last_list->prefixlist) : list);
01509 
01510       if (local_allow_unknown < 0)
01511         {
01512           if (last_list)
01513             return last_list;   /* Found something.  */
01514           else
01515             return 0;           /* Found nothing.  */
01516         }
01517       else
01518         {
01519           /* Report as error.  */
01520           int amb_len;
01521           char ambbuf[100];
01522 
01523           for (amb_len = 0;
01524                ((*line)[amb_len] && (*line)[amb_len] != ' '
01525                 && (*line)[amb_len] != '\t');
01526                amb_len++)
01527             ;
01528 
01529           ambbuf[0] = 0;
01530           for (c = local_list; c; c = c->next)
01531             if (!strncmp (*line, c->name, amb_len))
01532               {
01533                 if (strlen (ambbuf) + strlen (c->name) + 6
01534                     < (int) sizeof ambbuf)
01535                   {
01536                     if (strlen (ambbuf))
01537                       strcat (ambbuf, ", ");
01538                     strcat (ambbuf, c->name);
01539                   }
01540                 else
01541                   {
01542                     strcat (ambbuf, "..");
01543                     break;
01544                   }
01545               }
01546           error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype,
01547                  *line, ambbuf);
01548           return 0;             /* lint */
01549         }
01550     }
01551   else
01552     {
01553       if (c->type == set_cmd && **line != '\0' && !isspace (**line))
01554         error (_("Argument must be preceded by space."));
01555 
01556       /* We've got something.  It may still not be what the caller
01557          wants (if this command *needs* a subcommand).  */
01558       while (**line == ' ' || **line == '\t')
01559         (*line)++;
01560 
01561       if (c->prefixlist && **line && !c->allow_unknown)
01562         undef_cmd_error (c->prefixname, *line);
01563 
01564       /* Seems to be what he wants.  Return it.  */
01565       return c;
01566     }
01567   return 0;
01568 }
01569 
01570 /* We are here presumably because an alias or command in TEXT is
01571    deprecated and a warning message should be generated.  This
01572    function decodes TEXT and potentially generates a warning message
01573    as outlined below.
01574    
01575    Example for 'set endian big' which has a fictitious alias 'seb'.
01576    
01577    If alias wasn't used in TEXT, and the command is deprecated:
01578    "warning: 'set endian big' is deprecated." 
01579    
01580    If alias was used, and only the alias is deprecated:
01581    "warning: 'seb' an alias for the command 'set endian big' is deprecated."
01582    
01583    If alias was used and command is deprecated (regardless of whether
01584    the alias itself is deprecated:
01585    
01586    "warning: 'set endian big' (seb) is deprecated."
01587 
01588    After the message has been sent, clear the appropriate flags in the
01589    command and/or the alias so the user is no longer bothered.
01590    
01591 */
01592 void
01593 deprecated_cmd_warning (const char *text)
01594 {
01595   struct cmd_list_element *alias = NULL;
01596   struct cmd_list_element *prefix_cmd = NULL;
01597   struct cmd_list_element *cmd = NULL;
01598 
01599   if (!lookup_cmd_composition (text, &alias, &prefix_cmd, &cmd))
01600     /* Return if text doesn't evaluate to a command.  */
01601     return;
01602 
01603   if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
01604       || (cmd->flags & DEPRECATED_WARN_USER) ) ) 
01605     /* Return if nothing is deprecated.  */
01606     return;
01607   
01608   printf_filtered ("Warning:");
01609   
01610   if (alias && !(cmd->flags & CMD_DEPRECATED))
01611     printf_filtered (" '%s', an alias for the", alias->name);
01612     
01613   printf_filtered (" command '");
01614   
01615   if (prefix_cmd)
01616     printf_filtered ("%s", prefix_cmd->prefixname);
01617   
01618   printf_filtered ("%s", cmd->name);
01619 
01620   if (alias && (cmd->flags & CMD_DEPRECATED))
01621     printf_filtered ("' (%s) is deprecated.\n", alias->name);
01622   else
01623     printf_filtered ("' is deprecated.\n"); 
01624   
01625 
01626   /* If it is only the alias that is deprecated, we want to indicate
01627      the new alias, otherwise we'll indicate the new command.  */
01628 
01629   if (alias && !(cmd->flags & CMD_DEPRECATED))
01630     {
01631       if (alias->replacement)
01632         printf_filtered ("Use '%s'.\n\n", alias->replacement);
01633       else
01634         printf_filtered ("No alternative known.\n\n");
01635      }  
01636   else
01637     {
01638       if (cmd->replacement)
01639         printf_filtered ("Use '%s'.\n\n", cmd->replacement);
01640       else
01641         printf_filtered ("No alternative known.\n\n");
01642     }
01643 
01644   /* We've warned you, now we'll keep quiet.  */
01645   if (alias)
01646     alias->flags &= ~DEPRECATED_WARN_USER;
01647   
01648   cmd->flags &= ~DEPRECATED_WARN_USER;
01649 }
01650 
01651 
01652 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
01653    Return 1 on success, 0 on failure.
01654    
01655    If LINE refers to an alias, *alias will point to that alias.
01656    
01657    If LINE is a postfix command (i.e. one that is preceded by a prefix
01658    command) set *prefix_cmd.
01659    
01660    Set *cmd to point to the command LINE indicates.
01661    
01662    If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not 
01663    exist, they are NULL when we return.
01664    
01665 */
01666 int
01667 lookup_cmd_composition (const char *text,
01668                       struct cmd_list_element **alias,
01669                       struct cmd_list_element **prefix_cmd, 
01670                       struct cmd_list_element **cmd)
01671 {
01672   char *command;
01673   int len, tmp, nfound;
01674   struct cmd_list_element *cur_list;
01675   struct cmd_list_element *prev_cmd;
01676 
01677   *alias = NULL;
01678   *prefix_cmd = NULL;
01679   *cmd = NULL;
01680   
01681   cur_list = cmdlist;
01682   
01683   while (1)
01684     { 
01685       /* Go through as many command lists as we need to,
01686          to find the command TEXT refers to.  */
01687       
01688       prev_cmd = *cmd;
01689       
01690       while (*text == ' ' || *text == '\t')
01691         (text)++;
01692       
01693       /* Identify the name of the command.  */
01694       len = find_command_name_length (text);
01695       
01696       /* If nothing but whitespace, return.  */
01697       if (len == 0)
01698         return 0;
01699       
01700       /* Text is the start of the first command word to lookup (and
01701          it's length is len).  We copy this into a local temporary.  */
01702       
01703       command = (char *) alloca (len + 1);
01704       memcpy (command, text, len);
01705       command[len] = '\0';
01706       
01707       /* Look it up.  */
01708       *cmd = 0;
01709       nfound = 0;
01710       *cmd = find_cmd (command, len, cur_list, 1, &nfound);
01711       
01712       /* We didn't find the command in the entered case, so lower case
01713          it and search again.
01714       */
01715       if (!*cmd || nfound == 0)
01716         {
01717           for (tmp = 0; tmp < len; tmp++)
01718             {
01719               char x = command[tmp];
01720 
01721               command[tmp] = isupper (x) ? tolower (x) : x;
01722             }
01723           *cmd = find_cmd (command, len, cur_list, 1, &nfound);
01724         }
01725       
01726       if (*cmd == CMD_LIST_AMBIGUOUS)
01727         {
01728           return 0;              /* ambiguous */
01729         }
01730       
01731       if (*cmd == NULL)
01732         return 0;                /* nothing found */
01733       else
01734         {
01735           if ((*cmd)->cmd_pointer)
01736             {
01737               /* cmd was actually an alias, we note that an alias was
01738                  used (by assigning *alais) and we set *cmd.  */
01739               *alias = *cmd;
01740               *cmd = (*cmd)->cmd_pointer;
01741             }
01742           *prefix_cmd = prev_cmd;
01743         }
01744       if ((*cmd)->prefixlist)
01745         cur_list = *(*cmd)->prefixlist;
01746       else
01747         return 1;
01748       
01749       text += len;
01750     }
01751 }
01752 
01753 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
01754 
01755 /* Return a vector of char pointers which point to the different
01756    possible completions in LIST of TEXT.
01757 
01758    WORD points in the same buffer as TEXT, and completions should be
01759    returned relative to this position.  For example, suppose TEXT is
01760    "foo" and we want to complete to "foobar".  If WORD is "oo", return
01761    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
01762 
01763 VEC (char_ptr) *
01764 complete_on_cmdlist (struct cmd_list_element *list,
01765                      const char *text, const char *word,
01766                      int ignore_help_classes)
01767 {
01768   struct cmd_list_element *ptr;
01769   VEC (char_ptr) *matchlist = NULL;
01770   int textlen = strlen (text);
01771   int pass;
01772   int saw_deprecated_match = 0;
01773 
01774   /* We do one or two passes.  In the first pass, we skip deprecated
01775      commands.  If we see no matching commands in the first pass, and
01776      if we did happen to see a matching deprecated command, we do
01777      another loop to collect those.  */
01778   for (pass = 0; matchlist == 0 && pass < 2; ++pass)
01779     {
01780       for (ptr = list; ptr; ptr = ptr->next)
01781         if (!strncmp (ptr->name, text, textlen)
01782             && !ptr->abbrev_flag
01783             && (!ignore_help_classes || ptr->func
01784                 || ptr->prefixlist))
01785           {
01786             char *match;
01787 
01788             if (pass == 0)
01789               {
01790                 if ((ptr->flags & CMD_DEPRECATED) != 0)
01791                   {
01792                     saw_deprecated_match = 1;
01793                     continue;
01794                   }
01795               }
01796 
01797             match = (char *) xmalloc (strlen (word) + strlen (ptr->name) + 1);
01798             if (word == text)
01799               strcpy (match, ptr->name);
01800             else if (word > text)
01801               {
01802                 /* Return some portion of ptr->name.  */
01803                 strcpy (match, ptr->name + (word - text));
01804               }
01805             else
01806               {
01807                 /* Return some of text plus ptr->name.  */
01808                 strncpy (match, word, text - word);
01809                 match[text - word] = '\0';
01810                 strcat (match, ptr->name);
01811               }
01812             VEC_safe_push (char_ptr, matchlist, match);
01813           }
01814       /* If we saw no matching deprecated commands in the first pass,
01815          just bail out.  */
01816       if (!saw_deprecated_match)
01817         break;
01818     }
01819 
01820   return matchlist;
01821 }
01822 
01823 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
01824 
01825 /* Return a vector of char pointers which point to the different
01826    possible completions in CMD of TEXT.
01827 
01828    WORD points in the same buffer as TEXT, and completions should be
01829    returned relative to this position.  For example, suppose TEXT is "foo"
01830    and we want to complete to "foobar".  If WORD is "oo", return
01831    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
01832 
01833 VEC (char_ptr) *
01834 complete_on_enum (const char *const *enumlist,
01835                   const char *text, const char *word)
01836 {
01837   VEC (char_ptr) *matchlist = NULL;
01838   int textlen = strlen (text);
01839   int i;
01840   const char *name;
01841 
01842   for (i = 0; (name = enumlist[i]) != NULL; i++)
01843     if (strncmp (name, text, textlen) == 0)
01844       {
01845         char *match;
01846 
01847         match = (char *) xmalloc (strlen (word) + strlen (name) + 1);
01848         if (word == text)
01849           strcpy (match, name);
01850         else if (word > text)
01851           {
01852             /* Return some portion of name.  */
01853             strcpy (match, name + (word - text));
01854           }
01855         else
01856           {
01857             /* Return some of text plus name.  */
01858             strncpy (match, word, text - word);
01859             match[text - word] = '\0';
01860             strcat (match, name);
01861           }
01862         VEC_safe_push (char_ptr, matchlist, match);
01863       }
01864 
01865   return matchlist;
01866 }
01867 
01868 
01869 /* Check function pointer.  */
01870 int
01871 cmd_func_p (struct cmd_list_element *cmd)
01872 {
01873   return (cmd->func != NULL);
01874 }
01875 
01876 
01877 /* Call the command function.  */
01878 void
01879 cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
01880 {
01881   if (cmd_func_p (cmd))
01882     (*cmd->func) (cmd, args, from_tty);
01883   else
01884     error (_("Invalid command"));
01885 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines