Home | History | Annotate | Line # | Download | only in cli
cli-decode.c revision 1.1.1.1
      1 /* Handle lists of commands, their decoding and documentation, for GDB.
      2 
      3    Copyright (C) 1986-2014 Free Software Foundation, Inc.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #include "defs.h"
     19 #include "symtab.h"
     20 #include <ctype.h>
     21 #include "gdb_regex.h"
     22 #include <string.h>
     23 #include "completer.h"
     24 #include "ui-out.h"
     25 #include "cli/cli-cmds.h"
     26 #include "cli/cli-decode.h"
     27 #include "gdb_assert.h"
     28 
     29 /* Prototypes for local functions.  */
     30 
     31 static void undef_cmd_error (const char *, const char *);
     32 
     33 static struct cmd_list_element *delete_cmd (const char *name,
     34 					    struct cmd_list_element **list,
     35 					    struct cmd_list_element **prehook,
     36 					    struct cmd_list_element **prehookee,
     37 					    struct cmd_list_element **posthook,
     38 					    struct cmd_list_element **posthookee);
     39 
     40 static struct cmd_list_element *find_cmd (const char *command,
     41 					  int len,
     42 					  struct cmd_list_element *clist,
     43 					  int ignore_help_classes,
     44 					  int *nfound);
     45 
     46 static void help_all (struct ui_file *stream);
     47 
     48 /* Look up a command whose 'prefixlist' is KEY.  Return the command if found,
     49    otherwise return NULL.  */
     50 
     51 static struct cmd_list_element *
     52 lookup_cmd_for_prefixlist (struct cmd_list_element **key,
     53 			   struct cmd_list_element *list)
     54 {
     55   struct cmd_list_element *p = NULL;
     56 
     57   for (p = list; p != NULL; p = p->next)
     58     {
     59       struct cmd_list_element *q;
     60 
     61       if (p->prefixlist == NULL)
     62 	continue;
     63       else if (p->prefixlist == key)
     64 	return p;
     65 
     66       q = lookup_cmd_for_prefixlist (key, *(p->prefixlist));
     67       if (q != NULL)
     68 	return q;
     69     }
     70 
     71   return NULL;
     72 }
     73 
     74 static void
     75 set_cmd_prefix (struct cmd_list_element *c, struct cmd_list_element **list)
     76 {
     77   struct cmd_list_element *p;
     78 
     79   /* Check to see if *LIST contains any element other than C.  */
     80   for (p = *list; p != NULL; p = p->next)
     81     if (p != c)
     82       break;
     83 
     84   if (p == NULL)
     85     {
     86       /* *SET_LIST only contains SET.  */
     87       p = lookup_cmd_for_prefixlist (list, setlist);
     88 
     89       c->prefix = p ? (p->cmd_pointer ? p->cmd_pointer : p) : p;
     90     }
     91   else
     92     c->prefix = p->prefix;
     93 }
     94 
     95 static void
     96 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
     97 			struct ui_file *stream);
     98 
     99 
    100 /* Set the callback function for the specified command.  For each both
    102    the commands callback and func() are set.  The latter set to a
    103    bounce function (unless cfunc / sfunc is NULL that is).  */
    104 
    105 static void
    106 do_cfunc (struct cmd_list_element *c, char *args, int from_tty)
    107 {
    108   c->function.cfunc (args, from_tty); /* Ok.  */
    109 }
    110 
    111 void
    112 set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
    113 {
    114   if (cfunc == NULL)
    115     cmd->func = NULL;
    116   else
    117     cmd->func = do_cfunc;
    118   cmd->function.cfunc = cfunc; /* Ok.  */
    119 }
    120 
    121 static void
    122 do_sfunc (struct cmd_list_element *c, char *args, int from_tty)
    123 {
    124   c->function.sfunc (args, from_tty, c); /* Ok.  */
    125 }
    126 
    127 void
    128 set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc)
    129 {
    130   if (sfunc == NULL)
    131     cmd->func = NULL;
    132   else
    133     cmd->func = do_sfunc;
    134   cmd->function.sfunc = sfunc; /* Ok.  */
    135 }
    136 
    137 int
    138 cmd_cfunc_eq (struct cmd_list_element *cmd,
    139 	      void (*cfunc) (char *args, int from_tty))
    140 {
    141   return cmd->func == do_cfunc && cmd->function.cfunc == cfunc;
    142 }
    143 
    144 void
    145 set_cmd_context (struct cmd_list_element *cmd, void *context)
    146 {
    147   cmd->context = context;
    148 }
    149 
    150 void *
    151 get_cmd_context (struct cmd_list_element *cmd)
    152 {
    153   return cmd->context;
    154 }
    155 
    156 enum cmd_types
    157 cmd_type (struct cmd_list_element *cmd)
    158 {
    159   return cmd->type;
    160 }
    161 
    162 void
    163 set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
    164 {
    165   cmd->completer = completer; /* Ok.  */
    166 }
    167 
    168 /* Add element named NAME.
    169    Space for NAME and DOC must be allocated by the caller.
    170    CLASS is the top level category into which commands are broken down
    171    for "help" purposes.
    172    FUN should be the function to execute the command;
    173    it will get a character string as argument, with leading
    174    and trailing blanks already eliminated.
    175 
    176    DOC is a documentation string for the command.
    177    Its first line should be a complete sentence.
    178    It should start with ? for a command that is an abbreviation
    179    or with * for a command that most users don't need to know about.
    180 
    181    Add this command to command list *LIST.
    182 
    183    Returns a pointer to the added command (not necessarily the head
    184    of *LIST).  */
    185 
    186 struct cmd_list_element *
    187 add_cmd (const char *name, enum command_class class, void (*fun) (char *, int),
    188 	 char *doc, struct cmd_list_element **list)
    189 {
    190   struct cmd_list_element *c
    191     = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
    192   struct cmd_list_element *p, *iter;
    193 
    194   /* Turn each alias of the old command into an alias of the new
    195      command.  */
    196   c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
    197 			   &c->hook_post, &c->hookee_post);
    198   for (iter = c->aliases; iter; iter = iter->alias_chain)
    199     iter->cmd_pointer = c;
    200   if (c->hook_pre)
    201     c->hook_pre->hookee_pre = c;
    202   if (c->hookee_pre)
    203     c->hookee_pre->hook_pre = c;
    204   if (c->hook_post)
    205     c->hook_post->hookee_post = c;
    206   if (c->hookee_post)
    207     c->hookee_post->hook_post = c;
    208 
    209   if (*list == NULL || strcmp ((*list)->name, name) >= 0)
    210     {
    211       c->next = *list;
    212       *list = c;
    213     }
    214   else
    215     {
    216       p = *list;
    217       while (p->next && strcmp (p->next->name, name) <= 0)
    218 	{
    219 	  p = p->next;
    220 	}
    221       c->next = p->next;
    222       p->next = c;
    223     }
    224 
    225   c->name = name;
    226   c->class = class;
    227   set_cmd_cfunc (c, fun);
    228   set_cmd_context (c, NULL);
    229   c->doc = doc;
    230   c->flags = 0;
    231   c->replacement = NULL;
    232   c->pre_show_hook = NULL;
    233   c->hook_in = 0;
    234   c->prefixlist = NULL;
    235   c->prefixname = NULL;
    236   c->allow_unknown = 0;
    237   c->prefix = NULL;
    238   c->abbrev_flag = 0;
    239   set_cmd_completer (c, make_symbol_completion_list_fn);
    240   c->destroyer = NULL;
    241   c->type = not_set_cmd;
    242   c->var = NULL;
    243   c->var_type = var_boolean;
    244   c->enums = NULL;
    245   c->user_commands = NULL;
    246   c->cmd_pointer = NULL;
    247   c->alias_chain = NULL;
    248 
    249   return c;
    250 }
    251 
    252 /* Deprecates a command CMD.
    253    REPLACEMENT is the name of the command which should be used in
    254    place of this command, or NULL if no such command exists.
    255 
    256    This function does not check to see if command REPLACEMENT exists
    257    since gdb may not have gotten around to adding REPLACEMENT when
    258    this function is called.
    259 
    260    Returns a pointer to the deprecated command.  */
    261 
    262 struct cmd_list_element *
    263 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
    264 {
    265   cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
    266 
    267   if (replacement != NULL)
    268     cmd->replacement = replacement;
    269   else
    270     cmd->replacement = NULL;
    271 
    272   return cmd;
    273 }
    274 
    275 struct cmd_list_element *
    276 add_alias_cmd (const char *name, const char *oldname, enum command_class class,
    277 	       int abbrev_flag, struct cmd_list_element **list)
    278 {
    279   const char *tmp;
    280   struct cmd_list_element *old;
    281   struct cmd_list_element *c;
    282 
    283   tmp = oldname;
    284   old = lookup_cmd (&tmp, *list, "", 1, 1);
    285 
    286   if (old == 0)
    287     {
    288       struct cmd_list_element *prehook, *prehookee, *posthook, *posthookee;
    289       struct cmd_list_element *aliases = delete_cmd (name, list,
    290 						     &prehook, &prehookee,
    291 						     &posthook, &posthookee);
    292 
    293       /* If this happens, it means a programmer error somewhere.  */
    294       gdb_assert (!aliases && !prehook && !prehookee
    295 		  && !posthook && ! posthookee);
    296       return 0;
    297     }
    298 
    299   c = add_cmd (name, class, NULL, old->doc, list);
    300 
    301   /* If OLD->DOC can be freed, we should make another copy.  */
    302   if ((old->flags & DOC_ALLOCATED) != 0)
    303     {
    304       c->doc = xstrdup (old->doc);
    305       c->flags |= DOC_ALLOCATED;
    306     }
    307   /* NOTE: Both FUNC and all the FUNCTIONs need to be copied.  */
    308   c->func = old->func;
    309   c->function = old->function;
    310   c->prefixlist = old->prefixlist;
    311   c->prefixname = old->prefixname;
    312   c->allow_unknown = old->allow_unknown;
    313   c->abbrev_flag = abbrev_flag;
    314   c->cmd_pointer = old;
    315   c->alias_chain = old->aliases;
    316   old->aliases = c;
    317 
    318   set_cmd_prefix (c, list);
    319   return c;
    320 }
    321 
    322 /* Like add_cmd but adds an element for a command prefix: a name that
    323    should be followed by a subcommand to be looked up in another
    324    command list.  PREFIXLIST should be the address of the variable
    325    containing that list.  */
    326 
    327 struct cmd_list_element *
    328 add_prefix_cmd (const char *name, enum command_class class,
    329 		void (*fun) (char *, int),
    330 		char *doc, struct cmd_list_element **prefixlist,
    331 		char *prefixname, int allow_unknown,
    332 		struct cmd_list_element **list)
    333 {
    334   struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
    335   struct cmd_list_element *p;
    336 
    337   c->prefixlist = prefixlist;
    338   c->prefixname = prefixname;
    339   c->allow_unknown = allow_unknown;
    340 
    341   if (list == &cmdlist)
    342     c->prefix = NULL;
    343   else
    344     set_cmd_prefix (c, list);
    345 
    346   /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST.  */
    347   for (p = *prefixlist; p != NULL; p = p->next)
    348     p->prefix = c;
    349 
    350   return c;
    351 }
    352 
    353 /* Like add_prefix_cmd but sets the abbrev_flag on the new command.  */
    354 
    355 struct cmd_list_element *
    356 add_abbrev_prefix_cmd (const char *name, enum command_class class,
    357 		       void (*fun) (char *, int), char *doc,
    358 		       struct cmd_list_element **prefixlist, char *prefixname,
    359 		       int allow_unknown, struct cmd_list_element **list)
    360 {
    361   struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
    362 
    363   c->prefixlist = prefixlist;
    364   c->prefixname = prefixname;
    365   c->allow_unknown = allow_unknown;
    366   c->abbrev_flag = 1;
    367   return c;
    368 }
    369 
    370 /* This is an empty "cfunc".  */
    371 void
    372 not_just_help_class_command (char *args, int from_tty)
    373 {
    374 }
    375 
    376 /* This is an empty "sfunc".  */
    377 static void empty_sfunc (char *, int, struct cmd_list_element *);
    378 
    379 static void
    380 empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
    381 {
    382 }
    383 
    384 /* Add element named NAME to command list LIST (the list for set/show
    385    or some sublist thereof).
    386    TYPE is set_cmd or show_cmd.
    387    CLASS is as in add_cmd.
    388    VAR_TYPE is the kind of thing we are setting.
    389    VAR is address of the variable being controlled by this command.
    390    DOC is the documentation string.  */
    391 
    392 static struct cmd_list_element *
    393 add_set_or_show_cmd (const char *name,
    394 		     enum cmd_types type,
    395 		     enum command_class class,
    396 		     var_types var_type,
    397 		     void *var,
    398 		     char *doc,
    399 		     struct cmd_list_element **list)
    400 {
    401   struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);
    402 
    403   gdb_assert (type == set_cmd || type == show_cmd);
    404   c->type = type;
    405   c->var_type = var_type;
    406   c->var = var;
    407   /* This needs to be something besides NULL so that this isn't
    408      treated as a help class.  */
    409   set_cmd_sfunc (c, empty_sfunc);
    410   return c;
    411 }
    412 
    413 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
    414    CLASS is as in add_cmd.  VAR_TYPE is the kind of thing we are
    415    setting.  VAR is address of the variable being controlled by this
    416    command.  SET_FUNC and SHOW_FUNC are the callback functions (if
    417    non-NULL).  SET_DOC, SHOW_DOC and HELP_DOC are the documentation
    418    strings.  PRINT the format string to print the value.  SET_RESULT
    419    and SHOW_RESULT, if not NULL, are set to the resulting command
    420    structures.  */
    421 
    422 static void
    423 add_setshow_cmd_full (const char *name,
    424 		      enum command_class class,
    425 		      var_types var_type, void *var,
    426 		      const char *set_doc, const char *show_doc,
    427 		      const char *help_doc,
    428 		      cmd_sfunc_ftype *set_func,
    429 		      show_value_ftype *show_func,
    430 		      struct cmd_list_element **set_list,
    431 		      struct cmd_list_element **show_list,
    432 		      struct cmd_list_element **set_result,
    433 		      struct cmd_list_element **show_result)
    434 {
    435   struct cmd_list_element *set;
    436   struct cmd_list_element *show;
    437   char *full_set_doc;
    438   char *full_show_doc;
    439 
    440   if (help_doc != NULL)
    441     {
    442       full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
    443       full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
    444     }
    445   else
    446     {
    447       full_set_doc = xstrdup (set_doc);
    448       full_show_doc = xstrdup (show_doc);
    449     }
    450   set = add_set_or_show_cmd (name, set_cmd, class, var_type, var,
    451 			     full_set_doc, set_list);
    452   set->flags |= DOC_ALLOCATED;
    453 
    454   if (set_func != NULL)
    455     set_cmd_sfunc (set, set_func);
    456 
    457   set_cmd_prefix (set, set_list);
    458 
    459   show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
    460 			      full_show_doc, show_list);
    461   show->flags |= DOC_ALLOCATED;
    462   show->show_value_func = show_func;
    463 
    464   if (set_result != NULL)
    465     *set_result = set;
    466   if (show_result != NULL)
    467     *show_result = show;
    468 }
    469 
    470 /* Add element named NAME to command list LIST (the list for set or
    471    some sublist thereof).  CLASS is as in add_cmd.  ENUMLIST is a list
    472    of strings which may follow NAME.  VAR is address of the variable
    473    which will contain the matching string (from ENUMLIST).  */
    474 
    475 void
    476 add_setshow_enum_cmd (const char *name,
    477 		      enum command_class class,
    478 		      const char *const *enumlist,
    479 		      const char **var,
    480 		      const char *set_doc,
    481 		      const char *show_doc,
    482 		      const char *help_doc,
    483 		      cmd_sfunc_ftype *set_func,
    484 		      show_value_ftype *show_func,
    485 		      struct cmd_list_element **set_list,
    486 		      struct cmd_list_element **show_list)
    487 {
    488   struct cmd_list_element *c;
    489 
    490   add_setshow_cmd_full (name, class, var_enum, var,
    491 			set_doc, show_doc, help_doc,
    492 			set_func, show_func,
    493 			set_list, show_list,
    494 			&c, NULL);
    495   c->enums = enumlist;
    496 }
    497 
    498 const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
    499 
    500 /* Add an auto-boolean command named NAME to both the set and show
    501    command list lists.  CLASS is as in add_cmd.  VAR is address of the
    502    variable which will contain the value.  DOC is the documentation
    503    string.  FUNC is the corresponding callback.  */
    504 void
    505 add_setshow_auto_boolean_cmd (const char *name,
    506 			      enum command_class class,
    507 			      enum auto_boolean *var,
    508 			      const char *set_doc, const char *show_doc,
    509 			      const char *help_doc,
    510 			      cmd_sfunc_ftype *set_func,
    511 			      show_value_ftype *show_func,
    512 			      struct cmd_list_element **set_list,
    513 			      struct cmd_list_element **show_list)
    514 {
    515   struct cmd_list_element *c;
    516 
    517   add_setshow_cmd_full (name, class, var_auto_boolean, var,
    518 			set_doc, show_doc, help_doc,
    519 			set_func, show_func,
    520 			set_list, show_list,
    521 			&c, NULL);
    522   c->enums = auto_boolean_enums;
    523 }
    524 
    525 /* Add element named NAME to both the set and show command LISTs (the
    526    list for set/show or some sublist thereof).  CLASS is as in
    527    add_cmd.  VAR is address of the variable which will contain the
    528    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
    529 void
    530 add_setshow_boolean_cmd (const char *name, enum command_class class, int *var,
    531 			 const char *set_doc, const char *show_doc,
    532 			 const char *help_doc,
    533 			 cmd_sfunc_ftype *set_func,
    534 			 show_value_ftype *show_func,
    535 			 struct cmd_list_element **set_list,
    536 			 struct cmd_list_element **show_list)
    537 {
    538   static const char *boolean_enums[] = { "on", "off", NULL };
    539   struct cmd_list_element *c;
    540 
    541   add_setshow_cmd_full (name, class, var_boolean, var,
    542 			set_doc, show_doc, help_doc,
    543 			set_func, show_func,
    544 			set_list, show_list,
    545 			&c, NULL);
    546   c->enums = boolean_enums;
    547 }
    548 
    549 /* Add element named NAME to both the set and show command LISTs (the
    550    list for set/show or some sublist thereof).  */
    551 void
    552 add_setshow_filename_cmd (const char *name, enum command_class class,
    553 			  char **var,
    554 			  const char *set_doc, const char *show_doc,
    555 			  const char *help_doc,
    556 			  cmd_sfunc_ftype *set_func,
    557 			  show_value_ftype *show_func,
    558 			  struct cmd_list_element **set_list,
    559 			  struct cmd_list_element **show_list)
    560 {
    561   struct cmd_list_element *set_result;
    562 
    563   add_setshow_cmd_full (name, class, var_filename, var,
    564 			set_doc, show_doc, help_doc,
    565 			set_func, show_func,
    566 			set_list, show_list,
    567 			&set_result, NULL);
    568   set_cmd_completer (set_result, filename_completer);
    569 }
    570 
    571 /* Add element named NAME to both the set and show command LISTs (the
    572    list for set/show or some sublist thereof).  */
    573 void
    574 add_setshow_string_cmd (const char *name, enum command_class class,
    575 			char **var,
    576 			const char *set_doc, const char *show_doc,
    577 			const char *help_doc,
    578 			cmd_sfunc_ftype *set_func,
    579 			show_value_ftype *show_func,
    580 			struct cmd_list_element **set_list,
    581 			struct cmd_list_element **show_list)
    582 {
    583   add_setshow_cmd_full (name, class, var_string, var,
    584 			set_doc, show_doc, help_doc,
    585 			set_func, show_func,
    586 			set_list, show_list,
    587 			NULL, NULL);
    588 }
    589 
    590 /* Add element named NAME to both the set and show command LISTs (the
    591    list for set/show or some sublist thereof).  */
    592 struct cmd_list_element *
    593 add_setshow_string_noescape_cmd (const char *name, enum command_class class,
    594 				 char **var,
    595 				 const char *set_doc, const char *show_doc,
    596 				 const char *help_doc,
    597 				 cmd_sfunc_ftype *set_func,
    598 				 show_value_ftype *show_func,
    599 				 struct cmd_list_element **set_list,
    600 				 struct cmd_list_element **show_list)
    601 {
    602   struct cmd_list_element *set_cmd;
    603 
    604   add_setshow_cmd_full (name, class, var_string_noescape, var,
    605 			set_doc, show_doc, help_doc,
    606 			set_func, show_func,
    607 			set_list, show_list,
    608 			&set_cmd, NULL);
    609   return set_cmd;
    610 }
    611 
    612 /* Add element named NAME to both the set and show command LISTs (the
    613    list for set/show or some sublist thereof).  */
    614 void
    615 add_setshow_optional_filename_cmd (const char *name, enum command_class class,
    616 				   char **var,
    617 				   const char *set_doc, const char *show_doc,
    618 				   const char *help_doc,
    619 				   cmd_sfunc_ftype *set_func,
    620 				   show_value_ftype *show_func,
    621 				   struct cmd_list_element **set_list,
    622 				   struct cmd_list_element **show_list)
    623 {
    624   struct cmd_list_element *set_result;
    625 
    626   add_setshow_cmd_full (name, class, var_optional_filename, var,
    627 			set_doc, show_doc, help_doc,
    628 			set_func, show_func,
    629 			set_list, show_list,
    630 			&set_result, NULL);
    631 
    632   set_cmd_completer (set_result, filename_completer);
    633 
    634 }
    635 
    636 /* Completes on literal "unlimited".  Used by integer commands that
    637    support a special "unlimited" value.  */
    638 
    639 static VEC (char_ptr) *
    640 integer_unlimited_completer (struct cmd_list_element *ignore,
    641 			     const char *text, const char *word)
    642 {
    643   static const char * const keywords[] =
    644     {
    645       "unlimited",
    646       NULL,
    647     };
    648 
    649   return complete_on_enum (keywords, text, word);
    650 }
    651 
    652 /* Add element named NAME to both the set and show command LISTs (the
    653    list for set/show or some sublist thereof).  CLASS is as in
    654    add_cmd.  VAR is address of the variable which will contain the
    655    value.  SET_DOC and SHOW_DOC are the documentation strings.  This
    656    function is only used in Python API.  Please don't use it elsewhere.  */
    657 void
    658 add_setshow_integer_cmd (const char *name, enum command_class class,
    659 			 int *var,
    660 			 const char *set_doc, const char *show_doc,
    661 			 const char *help_doc,
    662 			 cmd_sfunc_ftype *set_func,
    663 			 show_value_ftype *show_func,
    664 			 struct cmd_list_element **set_list,
    665 			 struct cmd_list_element **show_list)
    666 {
    667   struct cmd_list_element *set;
    668 
    669   add_setshow_cmd_full (name, class, var_integer, var,
    670 			set_doc, show_doc, help_doc,
    671 			set_func, show_func,
    672 			set_list, show_list,
    673 			&set, NULL);
    674 
    675   set_cmd_completer (set, integer_unlimited_completer);
    676 }
    677 
    678 /* Add element named NAME to both the set and show command LISTs (the
    679    list for set/show or some sublist thereof).  CLASS is as in
    680    add_cmd.  VAR is address of the variable which will contain the
    681    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
    682 void
    683 add_setshow_uinteger_cmd (const char *name, enum command_class class,
    684 			  unsigned int *var,
    685 			  const char *set_doc, const char *show_doc,
    686 			  const char *help_doc,
    687 			  cmd_sfunc_ftype *set_func,
    688 			  show_value_ftype *show_func,
    689 			  struct cmd_list_element **set_list,
    690 			  struct cmd_list_element **show_list)
    691 {
    692   struct cmd_list_element *set;
    693 
    694   add_setshow_cmd_full (name, class, var_uinteger, var,
    695 			set_doc, show_doc, help_doc,
    696 			set_func, show_func,
    697 			set_list, show_list,
    698 			&set, NULL);
    699 
    700   set_cmd_completer (set, integer_unlimited_completer);
    701 }
    702 
    703 /* Add element named NAME to both the set and show command LISTs (the
    704    list for set/show or some sublist thereof).  CLASS is as in
    705    add_cmd.  VAR is address of the variable which will contain the
    706    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
    707 void
    708 add_setshow_zinteger_cmd (const char *name, enum command_class class,
    709 			  int *var,
    710 			  const char *set_doc, const char *show_doc,
    711 			  const char *help_doc,
    712 			  cmd_sfunc_ftype *set_func,
    713 			  show_value_ftype *show_func,
    714 			  struct cmd_list_element **set_list,
    715 			  struct cmd_list_element **show_list)
    716 {
    717   add_setshow_cmd_full (name, class, var_zinteger, var,
    718 			set_doc, show_doc, help_doc,
    719 			set_func, show_func,
    720 			set_list, show_list,
    721 			NULL, NULL);
    722 }
    723 
    724 void
    725 add_setshow_zuinteger_unlimited_cmd (const char *name,
    726 				     enum command_class class,
    727 				     int *var,
    728 				     const char *set_doc,
    729 				     const char *show_doc,
    730 				     const char *help_doc,
    731 				     cmd_sfunc_ftype *set_func,
    732 				     show_value_ftype *show_func,
    733 				     struct cmd_list_element **set_list,
    734 				     struct cmd_list_element **show_list)
    735 {
    736   struct cmd_list_element *set;
    737 
    738   add_setshow_cmd_full (name, class, var_zuinteger_unlimited, var,
    739 			set_doc, show_doc, help_doc,
    740 			set_func, show_func,
    741 			set_list, show_list,
    742 			&set, NULL);
    743 
    744   set_cmd_completer (set, integer_unlimited_completer);
    745 }
    746 
    747 /* Add element named NAME to both the set and show command LISTs (the
    748    list for set/show or some sublist thereof).  CLASS is as in
    749    add_cmd.  VAR is address of the variable which will contain the
    750    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
    751 void
    752 add_setshow_zuinteger_cmd (const char *name, enum command_class class,
    753 			   unsigned int *var,
    754 			   const char *set_doc, const char *show_doc,
    755 			   const char *help_doc,
    756 			   cmd_sfunc_ftype *set_func,
    757 			   show_value_ftype *show_func,
    758 			   struct cmd_list_element **set_list,
    759 			   struct cmd_list_element **show_list)
    760 {
    761   add_setshow_cmd_full (name, class, var_zuinteger, var,
    762 			set_doc, show_doc, help_doc,
    763 			set_func, show_func,
    764 			set_list, show_list,
    765 			NULL, NULL);
    766 }
    767 
    768 /* Remove the command named NAME from the command list.  Return the
    769    list commands which were aliased to the deleted command.  If the
    770    command had no aliases, return NULL.  The various *HOOKs are set to
    771    the pre- and post-hook commands for the deleted command.  If the
    772    command does not have a hook, the corresponding out parameter is
    773    set to NULL.  */
    774 
    775 static struct cmd_list_element *
    776 delete_cmd (const char *name, struct cmd_list_element **list,
    777 	    struct cmd_list_element **prehook,
    778 	    struct cmd_list_element **prehookee,
    779 	    struct cmd_list_element **posthook,
    780 	    struct cmd_list_element **posthookee)
    781 {
    782   struct cmd_list_element *iter;
    783   struct cmd_list_element **previous_chain_ptr;
    784   struct cmd_list_element *aliases = NULL;
    785 
    786   *prehook = NULL;
    787   *prehookee = NULL;
    788   *posthook = NULL;
    789   *posthookee = NULL;
    790   previous_chain_ptr = list;
    791 
    792   for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
    793     {
    794       if (strcmp (iter->name, name) == 0)
    795 	{
    796 	  if (iter->destroyer)
    797 	    iter->destroyer (iter, iter->context);
    798 	  if (iter->hookee_pre)
    799 	    iter->hookee_pre->hook_pre = 0;
    800 	  *prehook = iter->hook_pre;
    801 	  *prehookee = iter->hookee_pre;
    802 	  if (iter->hookee_post)
    803 	    iter->hookee_post->hook_post = 0;
    804 	  if (iter->doc && (iter->flags & DOC_ALLOCATED) != 0)
    805 	    xfree (iter->doc);
    806 	  *posthook = iter->hook_post;
    807 	  *posthookee = iter->hookee_post;
    808 
    809 	  /* Update the link.  */
    810 	  *previous_chain_ptr = iter->next;
    811 
    812 	  aliases = iter->aliases;
    813 
    814 	  /* If this command was an alias, remove it from the list of
    815 	     aliases.  */
    816 	  if (iter->cmd_pointer)
    817 	    {
    818 	      struct cmd_list_element **prevp = &iter->cmd_pointer->aliases;
    819 	      struct cmd_list_element *a = *prevp;
    820 
    821 	      while (a != iter)
    822 		{
    823 		  prevp = &a->alias_chain;
    824 		  a = *prevp;
    825 		}
    826 	      *prevp = iter->alias_chain;
    827 	    }
    828 
    829 	  xfree (iter);
    830 
    831 	  /* We won't see another command with the same name.  */
    832 	  break;
    833 	}
    834       else
    835 	previous_chain_ptr = &iter->next;
    836     }
    837 
    838   return aliases;
    839 }
    840 
    841 /* Shorthands to the commands above.  */
    843 
    844 /* Add an element to the list of info subcommands.  */
    845 
    846 struct cmd_list_element *
    847 add_info (const char *name, void (*fun) (char *, int), char *doc)
    848 {
    849   return add_cmd (name, no_class, fun, doc, &infolist);
    850 }
    851 
    852 /* Add an alias to the list of info subcommands.  */
    853 
    854 struct cmd_list_element *
    855 add_info_alias (const char *name, char *oldname, int abbrev_flag)
    856 {
    857   return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
    858 }
    859 
    860 /* Add an element to the list of commands.  */
    861 
    862 struct cmd_list_element *
    863 add_com (const char *name, enum command_class class, void (*fun) (char *, int),
    864 	 char *doc)
    865 {
    866   return add_cmd (name, class, fun, doc, &cmdlist);
    867 }
    868 
    869 /* Add an alias or abbreviation command to the list of commands.  */
    870 
    871 struct cmd_list_element *
    872 add_com_alias (const char *name, const char *oldname, enum command_class class,
    873 	       int abbrev_flag)
    874 {
    875   return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
    876 }
    877 
    878 /* Recursively walk the commandlist structures, and print out the
    880    documentation of commands that match our regex in either their
    881    name, or their documentation.
    882 */
    883 void
    884 apropos_cmd (struct ui_file *stream,
    885 	     struct cmd_list_element *commandlist,
    886 	     struct re_pattern_buffer *regex, char *prefix)
    887 {
    888   struct cmd_list_element *c;
    889   int returnvalue;
    890 
    891   /* Walk through the commands.  */
    892   for (c=commandlist;c;c=c->next)
    893     {
    894       returnvalue = -1; /* Needed to avoid double printing.  */
    895       if (c->name != NULL)
    896 	{
    897 	  /* Try to match against the name.  */
    898 	  returnvalue = re_search (regex, c->name, strlen(c->name),
    899 				   0, strlen (c->name), NULL);
    900 	  if (returnvalue >= 0)
    901 	    {
    902 	      print_help_for_command (c, prefix,
    903 				      0 /* don't recurse */, stream);
    904 	    }
    905 	}
    906       if (c->doc != NULL && returnvalue < 0)
    907 	{
    908 	  /* Try to match against documentation.  */
    909 	  if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
    910 	    {
    911 	      print_help_for_command (c, prefix,
    912 				      0 /* don't recurse */, stream);
    913 	    }
    914 	}
    915       /* Check if this command has subcommands and is not an
    916 	 abbreviation.  We skip listing subcommands of abbreviations
    917 	 in order to avoid duplicates in the output.  */
    918       if (c->prefixlist != NULL && !c->abbrev_flag)
    919 	{
    920 	  /* Recursively call ourselves on the subcommand list,
    921 	     passing the right prefix in.  */
    922 	  apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
    923 	}
    924     }
    925 }
    926 
    927 /* This command really has to deal with two things:
    928    1) I want documentation on *this string* (usually called by
    929       "help commandname").
    930 
    931    2) I want documentation on *this list* (usually called by giving a
    932       command that requires subcommands.  Also called by saying just
    933       "help".)
    934 
    935    I am going to split this into two seperate comamnds, help_cmd and
    936    help_list.  */
    937 
    938 void
    939 help_cmd (char *arg, struct ui_file *stream)
    940 {
    941   struct cmd_list_element *c;
    942   const char *command = arg;
    943 
    944   if (!command)
    945     {
    946       help_list (cmdlist, "", all_classes, stream);
    947       return;
    948     }
    949 
    950   if (strcmp (command, "all") == 0)
    951     {
    952       help_all (stream);
    953       return;
    954     }
    955 
    956   c = lookup_cmd (&command, cmdlist, "", 0, 0);
    957 
    958   if (c == 0)
    959     return;
    960 
    961   /* There are three cases here.
    962      If c->prefixlist is nonzero, we have a prefix command.
    963      Print its documentation, then list its subcommands.
    964 
    965      If c->func is non NULL, we really have a command.  Print its
    966      documentation and return.
    967 
    968      If c->func is NULL, we have a class name.  Print its
    969      documentation (as if it were a command) and then set class to the
    970      number of this class so that the commands in the class will be
    971      listed.  */
    972 
    973   fputs_filtered (c->doc, stream);
    974   fputs_filtered ("\n", stream);
    975 
    976   if (c->prefixlist == 0 && c->func != NULL)
    977     return;
    978   fprintf_filtered (stream, "\n");
    979 
    980   /* If this is a prefix command, print it's subcommands.  */
    981   if (c->prefixlist)
    982     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
    983 
    984   /* If this is a class name, print all of the commands in the class.  */
    985   if (c->func == NULL)
    986     help_list (cmdlist, "", c->class, stream);
    987 
    988   if (c->hook_pre || c->hook_post)
    989     fprintf_filtered (stream,
    990                       "\nThis command has a hook (or hooks) defined:\n");
    991 
    992   if (c->hook_pre)
    993     fprintf_filtered (stream,
    994                       "\tThis command is run after  : %s (pre hook)\n",
    995                     c->hook_pre->name);
    996   if (c->hook_post)
    997     fprintf_filtered (stream,
    998                       "\tThis command is run before : %s (post hook)\n",
    999                     c->hook_post->name);
   1000 }
   1001 
   1002 /*
   1003  * Get a specific kind of help on a command list.
   1004  *
   1005  * LIST is the list.
   1006  * CMDTYPE is the prefix to use in the title string.
   1007  * CLASS is the class with which to list the nodes of this list (see
   1008  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
   1009  * everything, ALL_CLASSES for just classes, and non-negative for only things
   1010  * in a specific class.
   1011  * and STREAM is the output stream on which to print things.
   1012  * If you call this routine with a class >= 0, it recurses.
   1013  */
   1014 void
   1015 help_list (struct cmd_list_element *list, char *cmdtype,
   1016 	   enum command_class class, struct ui_file *stream)
   1017 {
   1018   int len;
   1019   char *cmdtype1, *cmdtype2;
   1020 
   1021   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
   1022    */
   1023   len = strlen (cmdtype);
   1024   cmdtype1 = (char *) alloca (len + 1);
   1025   cmdtype1[0] = 0;
   1026   cmdtype2 = (char *) alloca (len + 4);
   1027   cmdtype2[0] = 0;
   1028   if (len)
   1029     {
   1030       cmdtype1[0] = ' ';
   1031       strncpy (cmdtype1 + 1, cmdtype, len - 1);
   1032       cmdtype1[len] = 0;
   1033       strncpy (cmdtype2, cmdtype, len - 1);
   1034       strcpy (cmdtype2 + len - 1, " sub");
   1035     }
   1036 
   1037   if (class == all_classes)
   1038     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
   1039   else
   1040     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
   1041 
   1042   help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
   1043 
   1044   if (class == all_classes)
   1045     {
   1046       fprintf_filtered (stream, "\n\
   1047 Type \"help%s\" followed by a class name for a list of commands in ",
   1048 			cmdtype1);
   1049       wrap_here ("");
   1050       fprintf_filtered (stream, "that class.");
   1051 
   1052       fprintf_filtered (stream, "\n\
   1053 Type \"help all\" for the list of all commands.");
   1054     }
   1055 
   1056   fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
   1057 		    cmdtype1, cmdtype2);
   1058   wrap_here ("");
   1059   fputs_filtered ("for ", stream);
   1060   wrap_here ("");
   1061   fputs_filtered ("full ", stream);
   1062   wrap_here ("");
   1063   fputs_filtered ("documentation.\n", stream);
   1064   fputs_filtered ("Type \"apropos word\" to search "
   1065 		  "for commands related to \"word\".\n", stream);
   1066   fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
   1067 		  stream);
   1068 }
   1069 
   1070 static void
   1071 help_all (struct ui_file *stream)
   1072 {
   1073   struct cmd_list_element *c;
   1074   int seen_unclassified = 0;
   1075 
   1076   for (c = cmdlist; c; c = c->next)
   1077     {
   1078       if (c->abbrev_flag)
   1079         continue;
   1080       /* If this is a class name, print all of the commands in the
   1081 	 class.  */
   1082 
   1083       if (c->func == NULL)
   1084 	{
   1085 	  fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
   1086 	  help_cmd_list (cmdlist, c->class, "", 1, stream);
   1087 	}
   1088     }
   1089 
   1090   /* While it's expected that all commands are in some class,
   1091      as a safety measure, we'll print commands outside of any
   1092      class at the end.  */
   1093 
   1094   for (c = cmdlist; c; c = c->next)
   1095     {
   1096       if (c->abbrev_flag)
   1097         continue;
   1098 
   1099       if (c->class == no_class)
   1100 	{
   1101 	  if (!seen_unclassified)
   1102 	    {
   1103 	      fprintf_filtered (stream, "\nUnclassified commands\n\n");
   1104 	      seen_unclassified = 1;
   1105 	    }
   1106 	  print_help_for_command (c, "", 1, stream);
   1107 	}
   1108     }
   1109 
   1110 }
   1111 
   1112 /* Print only the first line of STR on STREAM.  */
   1113 void
   1114 print_doc_line (struct ui_file *stream, char *str)
   1115 {
   1116   static char *line_buffer = 0;
   1117   static int line_size;
   1118   char *p;
   1119 
   1120   if (!line_buffer)
   1121     {
   1122       line_size = 80;
   1123       line_buffer = (char *) xmalloc (line_size);
   1124     }
   1125 
   1126   /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
   1127      like '.gdbinit'.  */
   1128   p = str;
   1129   while (*p && *p != '\n'
   1130 	 && ((*p != '.' && *p != ',') || (p[1] && !isspace (p[1]))))
   1131     p++;
   1132   if (p - str > line_size - 1)
   1133     {
   1134       line_size = p - str + 1;
   1135       xfree (line_buffer);
   1136       line_buffer = (char *) xmalloc (line_size);
   1137     }
   1138   strncpy (line_buffer, str, p - str);
   1139   line_buffer[p - str] = '\0';
   1140   if (islower (line_buffer[0]))
   1141     line_buffer[0] = toupper (line_buffer[0]);
   1142   fputs_filtered (line_buffer, stream);
   1143 }
   1144 
   1145 /* Print one-line help for command C.
   1146    If RECURSE is non-zero, also print one-line descriptions
   1147    of all prefixed subcommands.  */
   1148 static void
   1149 print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
   1150 			struct ui_file *stream)
   1151 {
   1152   fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
   1153   print_doc_line (stream, c->doc);
   1154   fputs_filtered ("\n", stream);
   1155 
   1156   if (recurse
   1157       && c->prefixlist != 0
   1158       && c->abbrev_flag == 0)
   1159     /* Subcommands of a prefix command typically have 'all_commands'
   1160        as class.  If we pass CLASS to recursive invocation,
   1161        most often we won't see anything.  */
   1162     help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream);
   1163 }
   1164 
   1165 /*
   1166  * Implement a help command on command list LIST.
   1167  * RECURSE should be non-zero if this should be done recursively on
   1168  * all sublists of LIST.
   1169  * PREFIX is the prefix to print before each command name.
   1170  * STREAM is the stream upon which the output should be written.
   1171  * CLASS should be:
   1172  *      A non-negative class number to list only commands in that
   1173  * class.
   1174  *      ALL_COMMANDS to list all commands in list.
   1175  *      ALL_CLASSES  to list all classes in list.
   1176  *
   1177  *   Note that RECURSE will be active on *all* sublists, not just the
   1178  * ones selected by the criteria above (ie. the selection mechanism
   1179  * is at the low level, not the high-level).
   1180  */
   1181 void
   1182 help_cmd_list (struct cmd_list_element *list, enum command_class class,
   1183 	       char *prefix, int recurse, struct ui_file *stream)
   1184 {
   1185   struct cmd_list_element *c;
   1186 
   1187   for (c = list; c; c = c->next)
   1188     {
   1189       if (c->abbrev_flag == 0
   1190 	  && (class == all_commands
   1191 	      || (class == all_classes && c->func == NULL)
   1192 	      || (class == c->class && c->func != NULL)))
   1193 	{
   1194 	  print_help_for_command (c, prefix, recurse, stream);
   1195 	}
   1196       else if (c->abbrev_flag == 0 && recurse
   1197 	       && class == class_user && c->prefixlist != NULL)
   1198 	/* User-defined commands may be subcommands.  */
   1199 	help_cmd_list (*c->prefixlist, class, c->prefixname,
   1200 		       recurse, stream);
   1201     }
   1202 }
   1203 
   1204 
   1206 /* Search the input clist for 'command'.  Return the command if
   1207    found (or NULL if not), and return the number of commands
   1208    found in nfound.  */
   1209 
   1210 static struct cmd_list_element *
   1211 find_cmd (const char *command, int len, struct cmd_list_element *clist,
   1212 	  int ignore_help_classes, int *nfound)
   1213 {
   1214   struct cmd_list_element *found, *c;
   1215 
   1216   found = (struct cmd_list_element *) NULL;
   1217   *nfound = 0;
   1218   for (c = clist; c; c = c->next)
   1219     if (!strncmp (command, c->name, len)
   1220 	&& (!ignore_help_classes || c->func))
   1221       {
   1222 	found = c;
   1223 	(*nfound)++;
   1224 	if (c->name[len] == '\0')
   1225 	  {
   1226 	    *nfound = 1;
   1227 	    break;
   1228 	  }
   1229       }
   1230   return found;
   1231 }
   1232 
   1233 static int
   1234 find_command_name_length (const char *text)
   1235 {
   1236   const char *p = text;
   1237 
   1238   /* Treating underscores as part of command words is important
   1239      so that "set args_foo()" doesn't get interpreted as
   1240      "set args _foo()".  */
   1241   /* Some characters are only used for TUI specific commands.
   1242      However, they are always allowed for the sake of consistency.
   1243 
   1244      The XDB compatibility characters are only allowed when using the
   1245      right mode because they clash with other GDB commands -
   1246      specifically '/' is used as a suffix for print, examine and
   1247      display.
   1248 
   1249      Note that this is larger than the character set allowed when
   1250      creating user-defined commands.  */
   1251 
   1252   /* Recognize '!' as a single character command so that, e.g., "!ls"
   1253      works as expected.  */
   1254   if (*p == '!')
   1255     return 1;
   1256 
   1257   while (isalnum (*p) || *p == '-' || *p == '_'
   1258 	 /* Characters used by TUI specific commands.  */
   1259 	 || *p == '+' || *p == '<' || *p == '>' || *p == '$'
   1260 	 /* Characters used for XDB compatibility.  */
   1261 	 || (xdb_commands && (*p == '/' || *p == '?')))
   1262     p++;
   1263 
   1264   return p - text;
   1265 }
   1266 
   1267 /* Return TRUE if NAME is a valid user-defined command name.
   1268    This is a stricter subset of all gdb commands,
   1269    see find_command_name_length.  */
   1270 
   1271 int
   1272 valid_user_defined_cmd_name_p (const char *name)
   1273 {
   1274   const char *p;
   1275 
   1276   if (*name == '\0')
   1277     return FALSE;
   1278 
   1279   /* Alas "42" is a legitimate user-defined command.
   1280      In the interests of not breaking anything we preserve that.  */
   1281 
   1282   for (p = name; *p != '\0'; ++p)
   1283     {
   1284       if (isalnum (*p)
   1285 	  || *p == '-'
   1286 	  || *p == '_')
   1287 	; /* Ok.  */
   1288       else
   1289 	return FALSE;
   1290     }
   1291 
   1292   return TRUE;
   1293 }
   1294 
   1295 /* This routine takes a line of TEXT and a CLIST in which to start the
   1296    lookup.  When it returns it will have incremented the text pointer past
   1297    the section of text it matched, set *RESULT_LIST to point to the list in
   1298    which the last word was matched, and will return a pointer to the cmd
   1299    list element which the text matches.  It will return NULL if no match at
   1300    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
   1301    matches are possible; in this case *RESULT_LIST will be set to point to
   1302    the list in which there are ambiguous choices (and *TEXT will be set to
   1303    the ambiguous text string).
   1304 
   1305    If the located command was an abbreviation, this routine returns the base
   1306    command of the abbreviation.
   1307 
   1308    It does no error reporting whatsoever; control will always return
   1309    to the superior routine.
   1310 
   1311    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
   1312    at the prefix_command (ie. the best match) *or* (special case) will be NULL
   1313    if no prefix command was ever found.  For example, in the case of "info a",
   1314    "info" matches without ambiguity, but "a" could be "args" or "address", so
   1315    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
   1316    RESULT_LIST should not be interpeted as a pointer to the beginning of a
   1317    list; it simply points to a specific command.  In the case of an ambiguous
   1318    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
   1319    "info t" can be "info types" or "info target"; upon return *TEXT has been
   1320    advanced past "info ").
   1321 
   1322    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
   1323    affect the operation).
   1324 
   1325    This routine does *not* modify the text pointed to by TEXT.
   1326 
   1327    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
   1328    are actually help classes rather than commands (i.e. the function field of
   1329    the struct cmd_list_element is NULL).  */
   1330 
   1331 struct cmd_list_element *
   1332 lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
   1333 	      struct cmd_list_element **result_list, int ignore_help_classes)
   1334 {
   1335   char *command;
   1336   int len, tmp, nfound;
   1337   struct cmd_list_element *found, *c;
   1338   const char *line = *text;
   1339 
   1340   while (**text == ' ' || **text == '\t')
   1341     (*text)++;
   1342 
   1343   /* Identify the name of the command.  */
   1344   len = find_command_name_length (*text);
   1345 
   1346   /* If nothing but whitespace, return 0.  */
   1347   if (len == 0)
   1348     return 0;
   1349 
   1350   /* *text and p now bracket the first command word to lookup (and
   1351      it's length is len).  We copy this into a local temporary.  */
   1352 
   1353 
   1354   command = (char *) alloca (len + 1);
   1355   memcpy (command, *text, len);
   1356   command[len] = '\0';
   1357 
   1358   /* Look it up.  */
   1359   found = 0;
   1360   nfound = 0;
   1361   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
   1362 
   1363   /* We didn't find the command in the entered case, so lower case it
   1364      and search again.  */
   1365   if (!found || nfound == 0)
   1366     {
   1367       for (tmp = 0; tmp < len; tmp++)
   1368 	{
   1369 	  char x = command[tmp];
   1370 
   1371 	  command[tmp] = isupper (x) ? tolower (x) : x;
   1372 	}
   1373       found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
   1374     }
   1375 
   1376   /* If nothing matches, we have a simple failure.  */
   1377   if (nfound == 0)
   1378     return 0;
   1379 
   1380   if (nfound > 1)
   1381     {
   1382       if (result_list != NULL)
   1383 	/* Will be modified in calling routine
   1384 	   if we know what the prefix command is.  */
   1385 	*result_list = 0;
   1386       return CMD_LIST_AMBIGUOUS;	/* Ambiguous.  */
   1387     }
   1388 
   1389   /* We've matched something on this list.  Move text pointer forward.  */
   1390 
   1391   *text += len;
   1392 
   1393   if (found->cmd_pointer)
   1394     {
   1395       /* We drop the alias (abbreviation) in favor of the command it
   1396        is pointing to.  If the alias is deprecated, though, we need to
   1397        warn the user about it before we drop it.  Note that while we
   1398        are warning about the alias, we may also warn about the command
   1399        itself and we will adjust the appropriate DEPRECATED_WARN_USER
   1400        flags.  */
   1401 
   1402       if (found->flags & DEPRECATED_WARN_USER)
   1403 	deprecated_cmd_warning (line);
   1404       found = found->cmd_pointer;
   1405     }
   1406   /* If we found a prefix command, keep looking.  */
   1407 
   1408   if (found->prefixlist)
   1409     {
   1410       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
   1411 			ignore_help_classes);
   1412       if (!c)
   1413 	{
   1414 	  /* Didn't find anything; this is as far as we got.  */
   1415 	  if (result_list != NULL)
   1416 	    *result_list = clist;
   1417 	  return found;
   1418 	}
   1419       else if (c == CMD_LIST_AMBIGUOUS)
   1420 	{
   1421 	  /* We've gotten this far properly, but the next step is
   1422 	     ambiguous.  We need to set the result list to the best
   1423 	     we've found (if an inferior hasn't already set it).  */
   1424 	  if (result_list != NULL)
   1425 	    if (!*result_list)
   1426 	      /* This used to say *result_list = *found->prefixlist.
   1427 	         If that was correct, need to modify the documentation
   1428 	         at the top of this function to clarify what is
   1429 	         supposed to be going on.  */
   1430 	      *result_list = found;
   1431 	  return c;
   1432 	}
   1433       else
   1434 	{
   1435 	  /* We matched!  */
   1436 	  return c;
   1437 	}
   1438     }
   1439   else
   1440     {
   1441       if (result_list != NULL)
   1442 	*result_list = clist;
   1443       return found;
   1444     }
   1445 }
   1446 
   1447 /* All this hair to move the space to the front of cmdtype */
   1448 
   1449 static void
   1450 undef_cmd_error (const char *cmdtype, const char *q)
   1451 {
   1452   error (_("Undefined %scommand: \"%s\".  Try \"help%s%.*s\"."),
   1453 	 cmdtype,
   1454 	 q,
   1455 	 *cmdtype ? " " : "",
   1456 	 (int) strlen (cmdtype) - 1,
   1457 	 cmdtype);
   1458 }
   1459 
   1460 /* Look up the contents of *LINE as a command in the command list LIST.
   1461    LIST is a chain of struct cmd_list_element's.
   1462    If it is found, return the struct cmd_list_element for that command
   1463    and update *LINE to point after the command name, at the first argument.
   1464    If not found, call error if ALLOW_UNKNOWN is zero
   1465    otherwise (or if error returns) return zero.
   1466    Call error if specified command is ambiguous,
   1467    unless ALLOW_UNKNOWN is negative.
   1468    CMDTYPE precedes the word "command" in the error message.
   1469 
   1470    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
   1471    elements which are actually help classes rather than commands (i.e.
   1472    the function field of the struct cmd_list_element is 0).  */
   1473 
   1474 struct cmd_list_element *
   1475 lookup_cmd (const char **line, struct cmd_list_element *list, char *cmdtype,
   1476 	    int allow_unknown, int ignore_help_classes)
   1477 {
   1478   struct cmd_list_element *last_list = 0;
   1479   struct cmd_list_element *c;
   1480 
   1481   /* Note: Do not remove trailing whitespace here because this
   1482      would be wrong for complete_command.  Jim Kingdon  */
   1483 
   1484   if (!*line)
   1485     error (_("Lack of needed %scommand"), cmdtype);
   1486 
   1487   c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
   1488 
   1489   if (!c)
   1490     {
   1491       if (!allow_unknown)
   1492 	{
   1493 	  char *q;
   1494 	  int len = find_command_name_length (*line);
   1495 
   1496 	  q = (char *) alloca (len + 1);
   1497 	  strncpy (q, *line, len);
   1498 	  q[len] = '\0';
   1499 	  undef_cmd_error (cmdtype, q);
   1500 	}
   1501       else
   1502 	return 0;
   1503     }
   1504   else if (c == CMD_LIST_AMBIGUOUS)
   1505     {
   1506       /* Ambigous.  Local values should be off prefixlist or called
   1507          values.  */
   1508       int local_allow_unknown = (last_list ? last_list->allow_unknown :
   1509 				 allow_unknown);
   1510       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
   1511       struct cmd_list_element *local_list =
   1512 	(last_list ? *(last_list->prefixlist) : list);
   1513 
   1514       if (local_allow_unknown < 0)
   1515 	{
   1516 	  if (last_list)
   1517 	    return last_list;	/* Found something.  */
   1518 	  else
   1519 	    return 0;		/* Found nothing.  */
   1520 	}
   1521       else
   1522 	{
   1523 	  /* Report as error.  */
   1524 	  int amb_len;
   1525 	  char ambbuf[100];
   1526 
   1527 	  for (amb_len = 0;
   1528 	       ((*line)[amb_len] && (*line)[amb_len] != ' '
   1529 		&& (*line)[amb_len] != '\t');
   1530 	       amb_len++)
   1531 	    ;
   1532 
   1533 	  ambbuf[0] = 0;
   1534 	  for (c = local_list; c; c = c->next)
   1535 	    if (!strncmp (*line, c->name, amb_len))
   1536 	      {
   1537 		if (strlen (ambbuf) + strlen (c->name) + 6
   1538 		    < (int) sizeof ambbuf)
   1539 		  {
   1540 		    if (strlen (ambbuf))
   1541 		      strcat (ambbuf, ", ");
   1542 		    strcat (ambbuf, c->name);
   1543 		  }
   1544 		else
   1545 		  {
   1546 		    strcat (ambbuf, "..");
   1547 		    break;
   1548 		  }
   1549 	      }
   1550 	  error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype,
   1551 		 *line, ambbuf);
   1552 	  return 0;		/* lint */
   1553 	}
   1554     }
   1555   else
   1556     {
   1557       if (c->type == set_cmd && **line != '\0' && !isspace (**line))
   1558         error (_("Argument must be preceded by space."));
   1559 
   1560       /* We've got something.  It may still not be what the caller
   1561          wants (if this command *needs* a subcommand).  */
   1562       while (**line == ' ' || **line == '\t')
   1563 	(*line)++;
   1564 
   1565       if (c->prefixlist && **line && !c->allow_unknown)
   1566 	undef_cmd_error (c->prefixname, *line);
   1567 
   1568       /* Seems to be what he wants.  Return it.  */
   1569       return c;
   1570     }
   1571   return 0;
   1572 }
   1573 
   1574 /* We are here presumably because an alias or command in TEXT is
   1575    deprecated and a warning message should be generated.  This
   1576    function decodes TEXT and potentially generates a warning message
   1577    as outlined below.
   1578 
   1579    Example for 'set endian big' which has a fictitious alias 'seb'.
   1580 
   1581    If alias wasn't used in TEXT, and the command is deprecated:
   1582    "warning: 'set endian big' is deprecated."
   1583 
   1584    If alias was used, and only the alias is deprecated:
   1585    "warning: 'seb' an alias for the command 'set endian big' is deprecated."
   1586 
   1587    If alias was used and command is deprecated (regardless of whether
   1588    the alias itself is deprecated:
   1589 
   1590    "warning: 'set endian big' (seb) is deprecated."
   1591 
   1592    After the message has been sent, clear the appropriate flags in the
   1593    command and/or the alias so the user is no longer bothered.
   1594 
   1595 */
   1596 void
   1597 deprecated_cmd_warning (const char *text)
   1598 {
   1599   struct cmd_list_element *alias = NULL;
   1600   struct cmd_list_element *prefix_cmd = NULL;
   1601   struct cmd_list_element *cmd = NULL;
   1602 
   1603   if (!lookup_cmd_composition (text, &alias, &prefix_cmd, &cmd))
   1604     /* Return if text doesn't evaluate to a command.  */
   1605     return;
   1606 
   1607   if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
   1608       || (cmd->flags & DEPRECATED_WARN_USER) ) )
   1609     /* Return if nothing is deprecated.  */
   1610     return;
   1611 
   1612   printf_filtered ("Warning:");
   1613 
   1614   if (alias && !(cmd->flags & CMD_DEPRECATED))
   1615     printf_filtered (" '%s', an alias for the", alias->name);
   1616 
   1617   printf_filtered (" command '");
   1618 
   1619   if (prefix_cmd)
   1620     printf_filtered ("%s", prefix_cmd->prefixname);
   1621 
   1622   printf_filtered ("%s", cmd->name);
   1623 
   1624   if (alias && (cmd->flags & CMD_DEPRECATED))
   1625     printf_filtered ("' (%s) is deprecated.\n", alias->name);
   1626   else
   1627     printf_filtered ("' is deprecated.\n");
   1628 
   1629 
   1630   /* If it is only the alias that is deprecated, we want to indicate
   1631      the new alias, otherwise we'll indicate the new command.  */
   1632 
   1633   if (alias && !(cmd->flags & CMD_DEPRECATED))
   1634     {
   1635       if (alias->replacement)
   1636 	printf_filtered ("Use '%s'.\n\n", alias->replacement);
   1637       else
   1638 	printf_filtered ("No alternative known.\n\n");
   1639      }
   1640   else
   1641     {
   1642       if (cmd->replacement)
   1643 	printf_filtered ("Use '%s'.\n\n", cmd->replacement);
   1644       else
   1645 	printf_filtered ("No alternative known.\n\n");
   1646     }
   1647 
   1648   /* We've warned you, now we'll keep quiet.  */
   1649   if (alias)
   1650     alias->flags &= ~DEPRECATED_WARN_USER;
   1651 
   1652   cmd->flags &= ~DEPRECATED_WARN_USER;
   1653 }
   1654 
   1655 
   1656 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
   1657    Return 1 on success, 0 on failure.
   1658 
   1659    If LINE refers to an alias, *alias will point to that alias.
   1660 
   1661    If LINE is a postfix command (i.e. one that is preceded by a prefix
   1662    command) set *prefix_cmd.
   1663 
   1664    Set *cmd to point to the command LINE indicates.
   1665 
   1666    If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
   1667    exist, they are NULL when we return.
   1668 
   1669 */
   1670 int
   1671 lookup_cmd_composition (const char *text,
   1672                       struct cmd_list_element **alias,
   1673                       struct cmd_list_element **prefix_cmd,
   1674                       struct cmd_list_element **cmd)
   1675 {
   1676   char *command;
   1677   int len, tmp, nfound;
   1678   struct cmd_list_element *cur_list;
   1679   struct cmd_list_element *prev_cmd;
   1680 
   1681   *alias = NULL;
   1682   *prefix_cmd = NULL;
   1683   *cmd = NULL;
   1684 
   1685   cur_list = cmdlist;
   1686 
   1687   while (1)
   1688     {
   1689       /* Go through as many command lists as we need to,
   1690 	 to find the command TEXT refers to.  */
   1691 
   1692       prev_cmd = *cmd;
   1693 
   1694       while (*text == ' ' || *text == '\t')
   1695 	(text)++;
   1696 
   1697       /* Identify the name of the command.  */
   1698       len = find_command_name_length (text);
   1699 
   1700       /* If nothing but whitespace, return.  */
   1701       if (len == 0)
   1702 	return 0;
   1703 
   1704       /* Text is the start of the first command word to lookup (and
   1705 	 it's length is len).  We copy this into a local temporary.  */
   1706 
   1707       command = (char *) alloca (len + 1);
   1708       memcpy (command, text, len);
   1709       command[len] = '\0';
   1710 
   1711       /* Look it up.  */
   1712       *cmd = 0;
   1713       nfound = 0;
   1714       *cmd = find_cmd (command, len, cur_list, 1, &nfound);
   1715 
   1716       /* We didn't find the command in the entered case, so lower case
   1717 	 it and search again.
   1718       */
   1719       if (!*cmd || nfound == 0)
   1720 	{
   1721 	  for (tmp = 0; tmp < len; tmp++)
   1722 	    {
   1723 	      char x = command[tmp];
   1724 
   1725 	      command[tmp] = isupper (x) ? tolower (x) : x;
   1726 	    }
   1727 	  *cmd = find_cmd (command, len, cur_list, 1, &nfound);
   1728 	}
   1729 
   1730       if (*cmd == CMD_LIST_AMBIGUOUS)
   1731 	{
   1732 	  return 0;              /* ambiguous */
   1733 	}
   1734 
   1735       if (*cmd == NULL)
   1736 	return 0;                /* nothing found */
   1737       else
   1738 	{
   1739 	  if ((*cmd)->cmd_pointer)
   1740 	    {
   1741 	      /* cmd was actually an alias, we note that an alias was
   1742 		 used (by assigning *alais) and we set *cmd.  */
   1743 	      *alias = *cmd;
   1744 	      *cmd = (*cmd)->cmd_pointer;
   1745 	    }
   1746 	  *prefix_cmd = prev_cmd;
   1747 	}
   1748       if ((*cmd)->prefixlist)
   1749 	cur_list = *(*cmd)->prefixlist;
   1750       else
   1751 	return 1;
   1752 
   1753       text += len;
   1754     }
   1755 }
   1756 
   1757 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
   1758 
   1759 /* Return a vector of char pointers which point to the different
   1760    possible completions in LIST of TEXT.
   1761 
   1762    WORD points in the same buffer as TEXT, and completions should be
   1763    returned relative to this position.  For example, suppose TEXT is
   1764    "foo" and we want to complete to "foobar".  If WORD is "oo", return
   1765    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
   1766 
   1767 VEC (char_ptr) *
   1768 complete_on_cmdlist (struct cmd_list_element *list,
   1769 		     const char *text, const char *word,
   1770 		     int ignore_help_classes)
   1771 {
   1772   struct cmd_list_element *ptr;
   1773   VEC (char_ptr) *matchlist = NULL;
   1774   int textlen = strlen (text);
   1775   int pass;
   1776   int saw_deprecated_match = 0;
   1777 
   1778   /* We do one or two passes.  In the first pass, we skip deprecated
   1779      commands.  If we see no matching commands in the first pass, and
   1780      if we did happen to see a matching deprecated command, we do
   1781      another loop to collect those.  */
   1782   for (pass = 0; matchlist == 0 && pass < 2; ++pass)
   1783     {
   1784       for (ptr = list; ptr; ptr = ptr->next)
   1785 	if (!strncmp (ptr->name, text, textlen)
   1786 	    && !ptr->abbrev_flag
   1787 	    && (!ignore_help_classes || ptr->func
   1788 		|| ptr->prefixlist))
   1789 	  {
   1790 	    char *match;
   1791 
   1792 	    if (pass == 0)
   1793 	      {
   1794 		if ((ptr->flags & CMD_DEPRECATED) != 0)
   1795 		  {
   1796 		    saw_deprecated_match = 1;
   1797 		    continue;
   1798 		  }
   1799 	      }
   1800 
   1801 	    match = (char *) xmalloc (strlen (word) + strlen (ptr->name) + 1);
   1802 	    if (word == text)
   1803 	      strcpy (match, ptr->name);
   1804 	    else if (word > text)
   1805 	      {
   1806 		/* Return some portion of ptr->name.  */
   1807 		strcpy (match, ptr->name + (word - text));
   1808 	      }
   1809 	    else
   1810 	      {
   1811 		/* Return some of text plus ptr->name.  */
   1812 		strncpy (match, word, text - word);
   1813 		match[text - word] = '\0';
   1814 		strcat (match, ptr->name);
   1815 	      }
   1816 	    VEC_safe_push (char_ptr, matchlist, match);
   1817 	  }
   1818       /* If we saw no matching deprecated commands in the first pass,
   1819 	 just bail out.  */
   1820       if (!saw_deprecated_match)
   1821 	break;
   1822     }
   1823 
   1824   return matchlist;
   1825 }
   1826 
   1827 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
   1828 
   1829 /* Return a vector of char pointers which point to the different
   1830    possible completions in CMD of TEXT.
   1831 
   1832    WORD points in the same buffer as TEXT, and completions should be
   1833    returned relative to this position.  For example, suppose TEXT is "foo"
   1834    and we want to complete to "foobar".  If WORD is "oo", return
   1835    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
   1836 
   1837 VEC (char_ptr) *
   1838 complete_on_enum (const char *const *enumlist,
   1839 		  const char *text, const char *word)
   1840 {
   1841   VEC (char_ptr) *matchlist = NULL;
   1842   int textlen = strlen (text);
   1843   int i;
   1844   const char *name;
   1845 
   1846   for (i = 0; (name = enumlist[i]) != NULL; i++)
   1847     if (strncmp (name, text, textlen) == 0)
   1848       {
   1849 	char *match;
   1850 
   1851 	match = (char *) xmalloc (strlen (word) + strlen (name) + 1);
   1852 	if (word == text)
   1853 	  strcpy (match, name);
   1854 	else if (word > text)
   1855 	  {
   1856 	    /* Return some portion of name.  */
   1857 	    strcpy (match, name + (word - text));
   1858 	  }
   1859 	else
   1860 	  {
   1861 	    /* Return some of text plus name.  */
   1862 	    strncpy (match, word, text - word);
   1863 	    match[text - word] = '\0';
   1864 	    strcat (match, name);
   1865 	  }
   1866 	VEC_safe_push (char_ptr, matchlist, match);
   1867       }
   1868 
   1869   return matchlist;
   1870 }
   1871 
   1872 
   1873 /* Check function pointer.  */
   1874 int
   1875 cmd_func_p (struct cmd_list_element *cmd)
   1876 {
   1877   return (cmd->func != NULL);
   1878 }
   1879 
   1880 
   1881 /* Call the command function.  */
   1882 void
   1883 cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
   1884 {
   1885   if (cmd_func_p (cmd))
   1886     (*cmd->func) (cmd, args, from_tty);
   1887   else
   1888     error (_("Invalid command"));
   1889 }
   1890