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