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