Home | History | Annotate | Line # | Download | only in cli
cli-decode.c revision 1.11
      1 /* Handle lists of commands, their decoding and documentation, for GDB.
      2 
      3    Copyright (C) 1986-2024 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 "symtab.h"
     19 #include <ctype.h>
     20 #include "gdbsupport/gdb_regex.h"
     21 #include "completer.h"
     22 #include "ui-out.h"
     23 #include "cli/cli-cmds.h"
     24 #include "cli/cli-decode.h"
     25 #include "cli/cli-style.h"
     26 #include <optional>
     27 
     28 /* Prototypes for local functions.  */
     29 
     30 static void undef_cmd_error (const char *, const char *);
     31 
     32 static cmd_list_element::aliases_list_type delete_cmd
     33   (const char *name, cmd_list_element **list, cmd_list_element **prehook,
     34    cmd_list_element **prehookee, cmd_list_element **posthook,
     35    cmd_list_element **posthookee);
     36 
     37 static struct cmd_list_element *find_cmd (const char *command,
     38 					  int len,
     39 					  struct cmd_list_element *clist,
     40 					  int ignore_help_classes,
     41 					  int *nfound);
     42 
     43 static void help_cmd_list (struct cmd_list_element *list,
     44 			   enum command_class theclass,
     45 			   bool recurse,
     46 			   struct ui_file *stream);
     47 
     48 static void help_all (struct ui_file *stream);
     49 
     50 static int lookup_cmd_composition_1 (const char *text,
     51 				     struct cmd_list_element **alias,
     52 				     struct cmd_list_element **prefix_cmd,
     53 				     struct cmd_list_element **cmd,
     54 				     struct cmd_list_element *cur_list);
     55 
     56 /* Look up a command whose 'subcommands' field is SUBCOMMANDS.  Return the
     57    command if found, otherwise return NULL.  */
     58 
     59 static struct cmd_list_element *
     60 lookup_cmd_with_subcommands (cmd_list_element **subcommands,
     61 			     cmd_list_element *list)
     62 {
     63   struct cmd_list_element *p = NULL;
     64 
     65   for (p = list; p != NULL; p = p->next)
     66     {
     67       struct cmd_list_element *q;
     68 
     69       if (!p->is_prefix ())
     70 	continue;
     71 
     72       else if (p->subcommands == subcommands)
     73 	{
     74 	  /* If we found an alias, we must return the aliased
     75 	     command.  */
     76 	  return p->is_alias () ? p->alias_target : p;
     77 	}
     78 
     79       q = lookup_cmd_with_subcommands (subcommands, *(p->subcommands));
     80       if (q != NULL)
     81 	return q;
     82     }
     83 
     84   return NULL;
     85 }
     86 
     87 static void
     88 print_help_for_command (const cmd_list_element &c,
     89 			bool recurse, struct ui_file *stream);
     90 
     91 static void
     92 do_simple_func (const char *args, int from_tty, cmd_list_element *c)
     93 {
     94   c->function.simple_func (args, from_tty);
     95 }
     96 
     97 static void
     98 set_cmd_simple_func (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
     99 {
    100   if (simple_func == NULL)
    101     cmd->func = NULL;
    102   else
    103     cmd->func = do_simple_func;
    104 
    105   cmd->function.simple_func = simple_func;
    106 }
    107 
    108 int
    109 cmd_simple_func_eq (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
    110 {
    111   return (cmd->func == do_simple_func
    112 	  && cmd->function.simple_func == simple_func);
    113 }
    114 
    115 void
    116 set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
    117 {
    118   cmd->completer = completer; /* Ok.  */
    119 }
    120 
    121 /* See definition in commands.h.  */
    122 
    123 void
    124 set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
    125 				   completer_handle_brkchars_ftype *func)
    126 {
    127   cmd->completer_handle_brkchars = func;
    128 }
    129 
    130 std::string
    131 cmd_list_element::prefixname () const
    132 {
    133   if (!this->is_prefix ())
    134     /* Not a prefix command.  */
    135     return "";
    136 
    137   std::string prefixname;
    138   if (this->prefix != nullptr)
    139     prefixname = this->prefix->prefixname ();
    140 
    141   prefixname += this->name;
    142   prefixname += " ";
    143 
    144   return prefixname;
    145 }
    146 
    147 /* See cli/cli-decode.h.  */
    148 
    149 std::vector<std::string>
    150 cmd_list_element::command_components () const
    151 {
    152   std::vector<std::string> result;
    153 
    154   if (this->prefix != nullptr)
    155     result = this->prefix->command_components ();
    156 
    157   result.emplace_back (this->name);
    158   return result;
    159 }
    160 
    161 /* Add element named NAME.
    162    Space for NAME and DOC must be allocated by the caller.
    163    THECLASS is the top level category into which commands are broken down
    164    for "help" purposes.
    165    FUN should be the function to execute the command;
    166    it will get a character string as argument, with leading
    167    and trailing blanks already eliminated.
    168 
    169    DOC is a documentation string for the command.
    170    Its first line should be a complete sentence.
    171    It should start with ? for a command that is an abbreviation
    172    or with * for a command that most users don't need to know about.
    173 
    174    Add this command to command list *LIST.
    175 
    176    Returns a pointer to the added command (not necessarily the head
    177    of *LIST).  */
    178 
    179 static struct cmd_list_element *
    180 do_add_cmd (const char *name, enum command_class theclass,
    181 	    const char *doc, struct cmd_list_element **list)
    182 {
    183   struct cmd_list_element *c = new struct cmd_list_element (name, theclass,
    184 							    doc);
    185 
    186   /* Turn each alias of the old command into an alias of the new
    187      command.  */
    188   c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
    189 			   &c->hook_post, &c->hookee_post);
    190 
    191   for (cmd_list_element &alias : c->aliases)
    192     alias.alias_target = c;
    193 
    194   if (c->hook_pre)
    195     c->hook_pre->hookee_pre = c;
    196 
    197   if (c->hookee_pre)
    198     c->hookee_pre->hook_pre = c;
    199 
    200   if (c->hook_post)
    201     c->hook_post->hookee_post = c;
    202 
    203   if (c->hookee_post)
    204     c->hookee_post->hook_post = c;
    205 
    206   if (*list == NULL || strcmp ((*list)->name, name) >= 0)
    207     {
    208       c->next = *list;
    209       *list = c;
    210     }
    211   else
    212     {
    213       cmd_list_element *p = *list;
    214       while (p->next && strcmp (p->next->name, name) <= 0)
    215 	{
    216 	  p = p->next;
    217 	}
    218       c->next = p->next;
    219       p->next = c;
    220     }
    221 
    222   /* Search the prefix cmd of C, and assigns it to C->prefix.
    223      See also add_prefix_cmd and update_prefix_field_of_prefixed_commands.  */
    224   cmd_list_element *prefixcmd = lookup_cmd_with_subcommands (list, cmdlist);
    225   c->prefix = prefixcmd;
    226 
    227 
    228   return c;
    229 }
    230 
    231 struct cmd_list_element *
    232 add_cmd (const char *name, enum command_class theclass,
    233 	 const char *doc, struct cmd_list_element **list)
    234 {
    235   cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
    236   result->func = NULL;
    237   result->function.simple_func = NULL;
    238   return result;
    239 }
    240 
    241 struct cmd_list_element *
    242 add_cmd (const char *name, enum command_class theclass,
    243 	 cmd_simple_func_ftype *fun,
    244 	 const char *doc, struct cmd_list_element **list)
    245 {
    246   cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
    247   set_cmd_simple_func (result, fun);
    248   return result;
    249 }
    250 
    251 /* Add an element with a suppress notification to the LIST of commands.  */
    252 
    253 struct cmd_list_element *
    254 add_cmd_suppress_notification (const char *name, enum command_class theclass,
    255 			       cmd_simple_func_ftype *fun, const char *doc,
    256 			       struct cmd_list_element **list,
    257 			       bool *suppress_notification)
    258 {
    259   struct cmd_list_element *element;
    260 
    261   element = add_cmd (name, theclass, fun, doc, list);
    262   element->suppress_notification = suppress_notification;
    263 
    264   return element;
    265 }
    266 
    267 
    268 /* Deprecates a command CMD.
    269    REPLACEMENT is the name of the command which should be used in
    270    place of this command, or NULL if no such command exists.
    271 
    272    This function does not check to see if command REPLACEMENT exists
    273    since gdb may not have gotten around to adding REPLACEMENT when
    274    this function is called.
    275 
    276    Returns a pointer to the deprecated command.  */
    277 
    278 struct cmd_list_element *
    279 deprecate_cmd (struct cmd_list_element *cmd, const char *replacement)
    280 {
    281   cmd->cmd_deprecated = 1;
    282   cmd->deprecated_warn_user = 1;
    283 
    284   if (replacement != NULL)
    285     cmd->replacement = replacement;
    286   else
    287     cmd->replacement = NULL;
    288 
    289   return cmd;
    290 }
    291 
    292 struct cmd_list_element *
    293 add_alias_cmd (const char *name, cmd_list_element *target,
    294 	       enum command_class theclass, int abbrev_flag,
    295 	       struct cmd_list_element **list)
    296 {
    297   gdb_assert (target != nullptr);
    298 
    299   struct cmd_list_element *c = add_cmd (name, theclass, target->doc, list);
    300 
    301   /* If TARGET->DOC can be freed, we should make another copy.  */
    302   if (target->doc_allocated)
    303     {
    304       c->doc = xstrdup (target->doc);
    305       c->doc_allocated = 1;
    306     }
    307   /* NOTE: Both FUNC and all the FUNCTIONs need to be copied.  */
    308   c->func = target->func;
    309   c->function = target->function;
    310   c->subcommands = target->subcommands;
    311   c->allow_unknown = target->allow_unknown;
    312   c->abbrev_flag = abbrev_flag;
    313   c->alias_target = target;
    314   target->aliases.push_front (*c);
    315 
    316   return c;
    317 }
    318 
    319 /* Update the prefix field of all sub-commands of the prefix command C.
    320    We must do this when a prefix command is defined as the GDB init sequence
    321    does not guarantee that a prefix command is created before its sub-commands.
    322    For example, break-catch-sig.c initialization runs before breakpoint.c
    323    initialization, but it is breakpoint.c that creates the "catch" command used
    324    by the "catch signal" command created by break-catch-sig.c.  */
    325 
    326 static void
    327 update_prefix_field_of_prefixed_commands (struct cmd_list_element *c)
    328 {
    329   for (cmd_list_element *p = *c->subcommands; p != NULL; p = p->next)
    330     {
    331       p->prefix = c;
    332 
    333       /* We must recursively update the prefix field to cover
    334 	 e.g.  'info auto-load libthread-db' where the creation
    335 	 order was:
    336 	   libthread-db
    337 	   auto-load
    338 	   info
    339 	 In such a case, when 'auto-load' was created by do_add_cmd,
    340 	 the 'libthread-db' prefix field could not be updated, as the
    341 	 'auto-load' command was not yet reachable by
    342 	    lookup_cmd_for_subcommands (list, cmdlist)
    343 	    that searches from the top level 'cmdlist'.  */
    344       if (p->is_prefix ())
    345 	update_prefix_field_of_prefixed_commands (p);
    346     }
    347 }
    348 
    349 
    350 /* Like add_cmd but adds an element for a command prefix: a name that
    351    should be followed by a subcommand to be looked up in another
    352    command list.  SUBCOMMANDS should be the address of the variable
    353    containing that list.  */
    354 
    355 struct cmd_list_element *
    356 add_prefix_cmd (const char *name, enum command_class theclass,
    357 		cmd_simple_func_ftype *fun,
    358 		const char *doc, struct cmd_list_element **subcommands,
    359 		int allow_unknown, struct cmd_list_element **list)
    360 {
    361   struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
    362 
    363   c->subcommands = subcommands;
    364   c->allow_unknown = allow_unknown;
    365 
    366   /* Now that prefix command C is defined, we need to set the prefix field
    367      of all prefixed commands that were defined before C itself was defined.  */
    368   update_prefix_field_of_prefixed_commands (c);
    369 
    370   return c;
    371 }
    372 
    373 /* A helper function for add_basic_prefix_cmd.  This is a command
    374    function that just forwards to help_list.  */
    375 
    376 static void
    377 do_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
    378 {
    379   /* Look past all aliases.  */
    380   while (c->is_alias ())
    381     c = c->alias_target;
    382 
    383   help_list (*c->subcommands, c->prefixname ().c_str (),
    384 	     all_commands, gdb_stdout);
    385 }
    386 
    387 /* See command.h.  */
    388 
    389 struct cmd_list_element *
    390 add_basic_prefix_cmd (const char *name, enum command_class theclass,
    391 		      const char *doc, struct cmd_list_element **subcommands,
    392 		      int allow_unknown, struct cmd_list_element **list)
    393 {
    394   struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
    395 						 doc, subcommands,
    396 						 allow_unknown, list);
    397   cmd->func = do_prefix_cmd;
    398   return cmd;
    399 }
    400 
    401 /* A helper function for add_show_prefix_cmd.  This is a command
    402    function that just forwards to cmd_show_list.  */
    403 
    404 static void
    405 do_show_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
    406 {
    407   cmd_show_list (*c->subcommands, from_tty);
    408 }
    409 
    410 /* See command.h.  */
    411 
    412 struct cmd_list_element *
    413 add_show_prefix_cmd (const char *name, enum command_class theclass,
    414 		     const char *doc, struct cmd_list_element **subcommands,
    415 		     int allow_unknown, struct cmd_list_element **list)
    416 {
    417   struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
    418 						 doc, subcommands,
    419 						 allow_unknown, list);
    420   cmd->func = do_show_prefix_cmd;
    421   return cmd;
    422 }
    423 
    424 /* See command.h.  */
    425 
    426 set_show_commands
    427 add_setshow_prefix_cmd (const char *name, command_class theclass,
    428 			const char *set_doc, const char *show_doc,
    429 			cmd_list_element **set_subcommands_list,
    430 			cmd_list_element **show_subcommands_list,
    431 			cmd_list_element **set_list,
    432 			cmd_list_element **show_list)
    433 {
    434   set_show_commands cmds;
    435 
    436   cmds.set = add_basic_prefix_cmd (name, theclass, set_doc,
    437 				   set_subcommands_list, 0,
    438 				   set_list);
    439   cmds.show = add_show_prefix_cmd (name, theclass, show_doc,
    440 				   show_subcommands_list, 0,
    441 				   show_list);
    442 
    443   return cmds;
    444 }
    445 
    446 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
    447    new command list element.  */
    448 
    449 struct cmd_list_element *
    450 add_prefix_cmd_suppress_notification
    451 	       (const char *name, enum command_class theclass,
    452 		cmd_simple_func_ftype *fun,
    453 		const char *doc, struct cmd_list_element **subcommands,
    454 		int allow_unknown, struct cmd_list_element **list,
    455 		bool *suppress_notification)
    456 {
    457   struct cmd_list_element *element
    458     = add_prefix_cmd (name, theclass, fun, doc, subcommands,
    459 		      allow_unknown, list);
    460   element->suppress_notification = suppress_notification;
    461   return element;
    462 }
    463 
    464 /* Like add_prefix_cmd but sets the abbrev_flag on the new command.  */
    465 
    466 struct cmd_list_element *
    467 add_abbrev_prefix_cmd (const char *name, enum command_class theclass,
    468 		       cmd_simple_func_ftype *fun, const char *doc,
    469 		       struct cmd_list_element **subcommands,
    470 		       int allow_unknown, struct cmd_list_element **list)
    471 {
    472   struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
    473 
    474   c->subcommands = subcommands;
    475   c->allow_unknown = allow_unknown;
    476   c->abbrev_flag = 1;
    477   return c;
    478 }
    479 
    480 /* This is an empty "simple func".  */
    481 void
    482 not_just_help_class_command (const char *args, int from_tty)
    483 {
    484 }
    485 
    486 /* This is an empty cmd func.  */
    487 
    488 static void
    489 empty_func (const char *args, int from_tty, cmd_list_element *c)
    490 {
    491 }
    492 
    493 /* Add element named NAME to command list LIST (the list for set/show
    494    or some sublist thereof).
    495    TYPE is set_cmd or show_cmd.
    496    THECLASS is as in add_cmd.
    497    VAR_TYPE is the kind of thing we are setting.
    498    EXTRA_LITERALS if non-NULL define extra literals to be accepted in lieu of
    499    a number for integer variables.
    500    ARGS is a pre-validated type-erased reference to the variable being
    501    controlled by this command.
    502    DOC is the documentation string.  */
    503 
    504 static struct cmd_list_element *
    505 add_set_or_show_cmd (const char *name,
    506 		     enum cmd_types type,
    507 		     enum command_class theclass,
    508 		     var_types var_type,
    509 		     const literal_def *extra_literals,
    510 		     const setting::erased_args &arg,
    511 		     const char *doc,
    512 		     struct cmd_list_element **list)
    513 {
    514   struct cmd_list_element *c = add_cmd (name, theclass, doc, list);
    515 
    516   gdb_assert (type == set_cmd || type == show_cmd);
    517   c->type = type;
    518   c->var.emplace (var_type, extra_literals, arg);
    519 
    520   /* This needs to be something besides NULL so that this isn't
    521      treated as a help class.  */
    522   c->func = empty_func;
    523   return c;
    524 }
    525 
    526 /* Add element named NAME to both command lists SET_LIST and SHOW_LIST.
    527    THECLASS is as in add_cmd.  VAR_TYPE is the kind of thing we are
    528    setting.  EXTRA_LITERALS if non-NULL define extra literals to be
    529    accepted in lieu of a number for integer variables.  ARGS is a
    530    pre-validated type-erased reference to the variable being controlled
    531    by this command.  SET_FUNC and SHOW_FUNC are the callback functions
    532    (if non-NULL).  SET_DOC, SHOW_DOC and HELP_DOC are the documentation
    533    strings.
    534 
    535    Return the newly created set and show commands.  */
    536 
    537 static set_show_commands
    538 add_setshow_cmd_full_erased (const char *name,
    539 			     enum command_class theclass,
    540 			     var_types var_type,
    541 			     const literal_def *extra_literals,
    542 			     const setting::erased_args &args,
    543 			     const char *set_doc, const char *show_doc,
    544 			     const char *help_doc,
    545 			     cmd_func_ftype *set_func,
    546 			     show_value_ftype *show_func,
    547 			     struct cmd_list_element **set_list,
    548 			     struct cmd_list_element **show_list)
    549 {
    550   struct cmd_list_element *set;
    551   struct cmd_list_element *show;
    552   gdb::unique_xmalloc_ptr<char> full_set_doc;
    553   gdb::unique_xmalloc_ptr<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 = make_unique_xstrdup (set_doc);
    563       full_show_doc = make_unique_xstrdup (show_doc);
    564     }
    565   set = add_set_or_show_cmd (name, set_cmd, theclass, var_type,
    566 			     extra_literals, args,
    567 			     full_set_doc.release (), set_list);
    568   set->doc_allocated = 1;
    569 
    570   if (set_func != NULL)
    571     set->func = set_func;
    572 
    573   show = add_set_or_show_cmd (name, show_cmd, theclass, var_type,
    574 			      extra_literals, args,
    575 			      full_show_doc.release (), show_list);
    576   show->doc_allocated = 1;
    577   show->show_value_func = show_func;
    578   /* Disable the default symbol completer.  Doesn't make much sense
    579      for the "show" command to complete on anything.  */
    580   set_cmd_completer (show, nullptr);
    581 
    582   return {set, show};
    583 }
    584 
    585 /* Completes on integer commands that support extra literals.  */
    586 
    587 static void
    588 integer_literals_completer (struct cmd_list_element *c,
    589 			    completion_tracker &tracker,
    590 			    const char *text, const char *word)
    591 {
    592   const literal_def *extra_literals = c->var->extra_literals ();
    593 
    594   if (*text == '\0')
    595     {
    596       tracker.add_completion (make_unique_xstrdup ("NUMBER"));
    597       for (const literal_def *l = extra_literals;
    598 	   l->literal != nullptr;
    599 	   l++)
    600 	tracker.add_completion (make_unique_xstrdup (l->literal));
    601     }
    602   else
    603     for (const literal_def *l = extra_literals;
    604 	 l->literal != nullptr;
    605 	 l++)
    606       if (startswith (l->literal, text))
    607 	tracker.add_completion (make_unique_xstrdup (l->literal));
    608 }
    609 
    610 /* Add element named NAME to both command lists SET_LIST and SHOW_LIST.
    611    THECLASS is as in add_cmd.  VAR_TYPE is the kind of thing we are
    612    setting.  VAR is address of the variable being controlled by this
    613    command.  EXTRA_LITERALS if non-NULL define extra literals to be
    614    accepted in lieu of a number for integer variables.  If nullptr is
    615    given as VAR, then both SET_SETTING_FUNC and GET_SETTING_FUNC must
    616    be provided.  SET_SETTING_FUNC and GET_SETTING_FUNC are callbacks
    617    used to access and modify the underlying property, whatever its
    618    storage is.  SET_FUNC and SHOW_FUNC are the callback functions
    619    (if non-NULL).  SET_DOC, SHOW_DOC and HELP_DOC are the
    620    documentation strings.
    621 
    622    Return the newly created set and show commands.  */
    623 
    624 template<typename T>
    625 static set_show_commands
    626 add_setshow_cmd_full (const char *name,
    627 		      enum command_class theclass,
    628 		      var_types var_type, T *var,
    629 		      const literal_def *extra_literals,
    630 		      const char *set_doc, const char *show_doc,
    631 		      const char *help_doc,
    632 		      typename setting_func_types<T>::set set_setting_func,
    633 		      typename setting_func_types<T>::get get_setting_func,
    634 		      cmd_func_ftype *set_func,
    635 		      show_value_ftype *show_func,
    636 		      struct cmd_list_element **set_list,
    637 		      struct cmd_list_element **show_list)
    638 {
    639   auto erased_args
    640     = setting::erase_args (var_type, var,
    641 			   set_setting_func, get_setting_func);
    642   auto cmds = add_setshow_cmd_full_erased (name,
    643 					   theclass,
    644 					   var_type, extra_literals,
    645 					   erased_args,
    646 					   set_doc, show_doc,
    647 					   help_doc,
    648 					   set_func,
    649 					   show_func,
    650 					   set_list,
    651 					   show_list);
    652 
    653   if (extra_literals != nullptr)
    654     set_cmd_completer (cmds.set, integer_literals_completer);
    655 
    656   return cmds;
    657 }
    658 
    659 /* Same as above but omitting EXTRA_LITERALS.  */
    660 
    661 template<typename T>
    662 static set_show_commands
    663 add_setshow_cmd_full (const char *name,
    664 		      enum command_class theclass,
    665 		      var_types var_type, T *var,
    666 		      const char *set_doc, const char *show_doc,
    667 		      const char *help_doc,
    668 		      typename setting_func_types<T>::set set_setting_func,
    669 		      typename setting_func_types<T>::get get_setting_func,
    670 		      cmd_func_ftype *set_func,
    671 		      show_value_ftype *show_func,
    672 		      struct cmd_list_element **set_list,
    673 		      struct cmd_list_element **show_list)
    674 {
    675   return add_setshow_cmd_full (name, theclass, var_type, var, nullptr,
    676 			       set_doc, show_doc, help_doc,
    677 			       set_setting_func, get_setting_func,
    678 			       set_func, show_func, set_list, show_list);
    679 }
    680 
    681 /* Add element named NAME to command list LIST (the list for set or
    682    some sublist thereof).  THECLASS is as in add_cmd.  ENUMLIST is a list
    683    of strings which may follow NAME.  VAR is address of the variable
    684    which will contain the matching string (from ENUMLIST).  */
    685 
    686 set_show_commands
    687 add_setshow_enum_cmd (const char *name,
    688 		      enum command_class theclass,
    689 		      const char *const *enumlist,
    690 		      const char **var,
    691 		      const char *set_doc,
    692 		      const char *show_doc,
    693 		      const char *help_doc,
    694 		      cmd_func_ftype *set_func,
    695 		      show_value_ftype *show_func,
    696 		      struct cmd_list_element **set_list,
    697 		      struct cmd_list_element **show_list)
    698 {
    699   /* We require *VAR to be initialized before this call, and
    700      furthermore it must be == to one of the values in ENUMLIST.  */
    701   gdb_assert (var != nullptr && *var != nullptr);
    702   for (int i = 0; ; ++i)
    703     {
    704       gdb_assert (enumlist[i] != nullptr);
    705       if (*var == enumlist[i])
    706 	break;
    707     }
    708 
    709   set_show_commands commands
    710     =  add_setshow_cmd_full<const char *> (name, theclass, var_enum, var,
    711 					   set_doc, show_doc, help_doc,
    712 					   nullptr, nullptr, set_func,
    713 					   show_func, set_list, show_list);
    714   commands.set->enums = enumlist;
    715   return commands;
    716 }
    717 
    718 /* Same as above but using a getter and a setter function instead of a pointer
    719    to a global storage buffer.  */
    720 
    721 set_show_commands
    722 add_setshow_enum_cmd (const char *name, command_class theclass,
    723 		      const char *const *enumlist, const char *set_doc,
    724 		      const char *show_doc, const char *help_doc,
    725 		      setting_func_types<const char *>::set set_func,
    726 		      setting_func_types<const char *>::get get_func,
    727 		      show_value_ftype *show_func,
    728 		      cmd_list_element **set_list,
    729 		      cmd_list_element **show_list)
    730 {
    731   auto cmds = add_setshow_cmd_full<const char *> (name, theclass, var_enum,
    732 						  nullptr, set_doc, show_doc,
    733 						  help_doc, set_func, get_func,
    734 						  nullptr, show_func, set_list,
    735 						  show_list);
    736 
    737   cmds.set->enums = enumlist;
    738 
    739   return cmds;
    740 }
    741 
    742 /* See cli-decode.h.  */
    743 const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
    744 
    745 /* Add an auto-boolean command named NAME to both the set and show
    746    command list lists.  THECLASS is as in add_cmd.  VAR is address of the
    747    variable which will contain the value.  DOC is the documentation
    748    string.  FUNC is the corresponding callback.  */
    749 
    750 set_show_commands
    751 add_setshow_auto_boolean_cmd (const char *name,
    752 			      enum command_class theclass,
    753 			      enum auto_boolean *var,
    754 			      const char *set_doc, const char *show_doc,
    755 			      const char *help_doc,
    756 			      cmd_func_ftype *set_func,
    757 			      show_value_ftype *show_func,
    758 			      struct cmd_list_element **set_list,
    759 			      struct cmd_list_element **show_list)
    760 {
    761   set_show_commands commands
    762     = add_setshow_cmd_full<enum auto_boolean> (name, theclass, var_auto_boolean,
    763 					       var, set_doc, show_doc, help_doc,
    764 					       nullptr, nullptr, set_func,
    765 					       show_func, set_list, show_list);
    766 
    767   commands.set->enums = auto_boolean_enums;
    768 
    769   return commands;
    770 }
    771 
    772 /* Same as above but using a getter and a setter function instead of a pointer
    773    to a global storage buffer.  */
    774 
    775 set_show_commands
    776 add_setshow_auto_boolean_cmd (const char *name, command_class theclass,
    777 			      const char *set_doc, const char *show_doc,
    778 			      const char *help_doc,
    779 			      setting_func_types<enum auto_boolean>::set set_func,
    780 			      setting_func_types<enum auto_boolean>::get get_func,
    781 			      show_value_ftype *show_func,
    782 			      cmd_list_element **set_list,
    783 			      cmd_list_element **show_list)
    784 {
    785   auto cmds = add_setshow_cmd_full<enum auto_boolean> (name, theclass,
    786 						       var_auto_boolean,
    787 						       nullptr, set_doc,
    788 						       show_doc, help_doc,
    789 						       set_func, get_func,
    790 						       nullptr, show_func,
    791 						       set_list, show_list);
    792 
    793   cmds.set->enums = auto_boolean_enums;
    794 
    795   return cmds;
    796 }
    797 
    798 /* See cli-decode.h.  */
    799 const char * const boolean_enums[] = { "on", "off", NULL };
    800 
    801 /* Add element named NAME to both the set and show command LISTs (the
    802    list for set/show or some sublist thereof).  THECLASS 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    Returns the new command element.  */
    806 
    807 set_show_commands
    808 add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *var,
    809 			 const char *set_doc, const char *show_doc,
    810 			 const char *help_doc,
    811 			 cmd_func_ftype *set_func,
    812 			 show_value_ftype *show_func,
    813 			 struct cmd_list_element **set_list,
    814 			 struct cmd_list_element **show_list)
    815 {
    816   set_show_commands commands
    817     = add_setshow_cmd_full<bool> (name, theclass, var_boolean, var,
    818 				  set_doc, show_doc, help_doc,
    819 				  nullptr, nullptr, set_func, show_func,
    820 				  set_list, show_list);
    821 
    822   commands.set->enums = boolean_enums;
    823 
    824   return commands;
    825 }
    826 
    827 /* Same as above but using a getter and a setter function instead of a pointer
    828    to a global storage buffer.  */
    829 
    830 set_show_commands
    831 add_setshow_boolean_cmd (const char *name, command_class theclass,
    832 			 const char *set_doc, const char *show_doc,
    833 			 const char *help_doc,
    834 			 setting_func_types<bool>::set set_func,
    835 			 setting_func_types<bool>::get get_func,
    836 			 show_value_ftype *show_func,
    837 			 cmd_list_element **set_list,
    838 			 cmd_list_element **show_list)
    839 {
    840   auto cmds = add_setshow_cmd_full<bool> (name, theclass, var_boolean, nullptr,
    841 					  set_doc, show_doc, help_doc,
    842 					  set_func, get_func, nullptr,
    843 					  show_func, set_list, show_list);
    844 
    845   cmds.set->enums = boolean_enums;
    846 
    847   return cmds;
    848 }
    849 
    850 /* Add element named NAME to both the set and show command LISTs (the
    851    list for set/show or some sublist thereof).  */
    852 
    853 set_show_commands
    854 add_setshow_filename_cmd (const char *name, enum command_class theclass,
    855 			  std::string *var,
    856 			  const char *set_doc, const char *show_doc,
    857 			  const char *help_doc,
    858 			  cmd_func_ftype *set_func,
    859 			  show_value_ftype *show_func,
    860 			  struct cmd_list_element **set_list,
    861 			  struct cmd_list_element **show_list)
    862 {
    863   set_show_commands commands
    864     = add_setshow_cmd_full<std::string> (name, theclass, var_filename, var,
    865 					 set_doc, show_doc, help_doc,
    866 					 nullptr, nullptr, set_func,
    867 					 show_func, set_list, show_list);
    868 
    869   set_cmd_completer (commands.set, filename_completer);
    870 
    871   return commands;
    872 }
    873 
    874 /* Same as above but using a getter and a setter function instead of a pointer
    875    to a global storage buffer.  */
    876 
    877 set_show_commands
    878 add_setshow_filename_cmd (const char *name, command_class theclass,
    879 			  const char *set_doc, const char *show_doc,
    880 			  const char *help_doc,
    881 			  setting_func_types<std::string>::set set_func,
    882 			  setting_func_types<std::string>::get get_func,
    883 			  show_value_ftype *show_func,
    884 			  cmd_list_element **set_list,
    885 			  cmd_list_element **show_list)
    886 {
    887   auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_filename,
    888 						 nullptr, set_doc, show_doc,
    889 						 help_doc, set_func, get_func,
    890 						 nullptr, show_func, set_list,
    891 						 show_list);
    892 
    893   set_cmd_completer (cmds.set, filename_completer);
    894 
    895   return cmds;
    896 }
    897 
    898 /* Add element named NAME to both the set and show command LISTs (the
    899    list for set/show or some sublist thereof).  */
    900 
    901 set_show_commands
    902 add_setshow_string_cmd (const char *name, enum command_class theclass,
    903 			std::string *var,
    904 			const char *set_doc, const char *show_doc,
    905 			const char *help_doc,
    906 			cmd_func_ftype *set_func,
    907 			show_value_ftype *show_func,
    908 			struct cmd_list_element **set_list,
    909 			struct cmd_list_element **show_list)
    910 {
    911   set_show_commands commands
    912     = add_setshow_cmd_full<std::string> (name, theclass, var_string, var,
    913 					set_doc, show_doc, help_doc,
    914 					nullptr, nullptr, set_func,
    915 					show_func, set_list, show_list);
    916 
    917   /* Disable the default symbol completer.  */
    918   set_cmd_completer (commands.set, nullptr);
    919 
    920   return commands;
    921 }
    922 
    923 /* Same as above but using a getter and a setter function instead of a pointer
    924    to a global storage buffer.  */
    925 
    926 set_show_commands
    927 add_setshow_string_cmd (const char *name, command_class theclass,
    928 			const char *set_doc, const char *show_doc,
    929 			const char *help_doc,
    930 			setting_func_types<std::string>::set set_func,
    931 			setting_func_types<std::string>::get get_func,
    932 			show_value_ftype *show_func,
    933 			cmd_list_element **set_list,
    934 			cmd_list_element **show_list)
    935 {
    936   auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_string,
    937 						 nullptr, set_doc, show_doc,
    938 						 help_doc, set_func, get_func,
    939 						 nullptr, show_func, set_list,
    940 						 show_list);
    941 
    942   /* Disable the default symbol completer.  */
    943   set_cmd_completer (cmds.set, nullptr);
    944 
    945   return cmds;
    946 }
    947 
    948 /* Add element named NAME to both the set and show command LISTs (the
    949    list for set/show or some sublist thereof).  */
    950 
    951 set_show_commands
    952 add_setshow_string_noescape_cmd (const char *name, enum command_class theclass,
    953 				 std::string *var,
    954 				 const char *set_doc, const char *show_doc,
    955 				 const char *help_doc,
    956 				 cmd_func_ftype *set_func,
    957 				 show_value_ftype *show_func,
    958 				 struct cmd_list_element **set_list,
    959 				 struct cmd_list_element **show_list)
    960 {
    961   set_show_commands commands
    962     = add_setshow_cmd_full<std::string> (name, theclass, var_string_noescape,
    963 					 var, set_doc, show_doc, help_doc,
    964 					 nullptr, nullptr, set_func, show_func,
    965 					 set_list, show_list);
    966 
    967   /* Disable the default symbol completer.  */
    968   set_cmd_completer (commands.set, nullptr);
    969 
    970   return commands;
    971 }
    972 
    973 /* Same as above but using a getter and a setter function instead of a pointer
    974    to a global storage buffer.  */
    975 
    976 set_show_commands
    977 add_setshow_string_noescape_cmd (const char *name, command_class theclass,
    978 				 const char *set_doc, const char *show_doc,
    979 				 const char *help_doc,
    980 				 setting_func_types<std::string>::set set_func,
    981 				 setting_func_types<std::string>::get get_func,
    982 				 show_value_ftype *show_func,
    983 				 cmd_list_element **set_list,
    984 				 cmd_list_element **show_list)
    985 {
    986   auto cmds = add_setshow_cmd_full<std::string> (name, theclass,
    987 						 var_string_noescape, nullptr,
    988 						 set_doc, show_doc, help_doc,
    989 						 set_func, get_func,
    990 						 nullptr, show_func, set_list,
    991 						 show_list);
    992 
    993   /* Disable the default symbol completer.  */
    994   set_cmd_completer (cmds.set, nullptr);
    995 
    996   return cmds;
    997 }
    998 
    999 /* Add element named NAME to both the set and show command LISTs (the
   1000    list for set/show or some sublist thereof).  */
   1001 
   1002 set_show_commands
   1003 add_setshow_optional_filename_cmd (const char *name, enum command_class theclass,
   1004 				   std::string *var,
   1005 				   const char *set_doc, const char *show_doc,
   1006 				   const char *help_doc,
   1007 				   cmd_func_ftype *set_func,
   1008 				   show_value_ftype *show_func,
   1009 				   struct cmd_list_element **set_list,
   1010 				   struct cmd_list_element **show_list)
   1011 {
   1012   set_show_commands commands
   1013     = add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
   1014 					 var, set_doc, show_doc, help_doc,
   1015 					 nullptr, nullptr, set_func, show_func,
   1016 					 set_list, show_list);
   1017 
   1018   set_cmd_completer (commands.set, filename_completer);
   1019 
   1020   return commands;
   1021 }
   1022 
   1023 /* Same as above but using a getter and a setter function instead of a pointer
   1024    to a global storage buffer.  */
   1025 
   1026 set_show_commands
   1027 add_setshow_optional_filename_cmd (const char *name, command_class theclass,
   1028 				   const char *set_doc, const char *show_doc,
   1029 				   const char *help_doc,
   1030 				   setting_func_types<std::string>::set set_func,
   1031 				   setting_func_types<std::string>::get get_func,
   1032 				   show_value_ftype *show_func,
   1033 				   cmd_list_element **set_list,
   1034 				   cmd_list_element **show_list)
   1035 {
   1036   auto cmds =
   1037     add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
   1038 				       nullptr, set_doc, show_doc, help_doc,
   1039 				       set_func, get_func, nullptr, show_func,
   1040 				       set_list,show_list);
   1041 
   1042   set_cmd_completer (cmds.set, filename_completer);
   1043 
   1044   return cmds;
   1045 }
   1046 
   1047 /* Add element named NAME to both the set and show command LISTs (the
   1048    list for set/show or some sublist thereof).  THECLASS is as in
   1049    add_cmd.  VAR is address of the variable which will contain the
   1050    value.  SET_DOC and SHOW_DOC are the documentation strings.  This
   1051    function is only used in Python API.  Please don't use it elsewhere.  */
   1052 
   1053 set_show_commands
   1054 add_setshow_integer_cmd (const char *name, enum command_class theclass,
   1055 			 int *var, const literal_def *extra_literals,
   1056 			 const char *set_doc, const char *show_doc,
   1057 			 const char *help_doc,
   1058 			 cmd_func_ftype *set_func,
   1059 			 show_value_ftype *show_func,
   1060 			 struct cmd_list_element **set_list,
   1061 			 struct cmd_list_element **show_list)
   1062 {
   1063   set_show_commands commands
   1064     = add_setshow_cmd_full<int> (name, theclass, var_integer, var,
   1065 				 extra_literals, set_doc, show_doc,
   1066 				 help_doc, nullptr, nullptr, set_func,
   1067 				 show_func, set_list, show_list);
   1068   return commands;
   1069 }
   1070 
   1071 /* Same as above but using a getter and a setter function instead of a pointer
   1072    to a global storage buffer.  */
   1073 
   1074 set_show_commands
   1075 add_setshow_integer_cmd (const char *name, command_class theclass,
   1076 			 const literal_def *extra_literals,
   1077 			 const char *set_doc, const char *show_doc,
   1078 			 const char *help_doc,
   1079 			 setting_func_types<int>::set set_func,
   1080 			 setting_func_types<int>::get get_func,
   1081 			 show_value_ftype *show_func,
   1082 			 cmd_list_element **set_list,
   1083 			 cmd_list_element **show_list)
   1084 {
   1085   auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
   1086 					 extra_literals, set_doc, show_doc,
   1087 					 help_doc, set_func, get_func, nullptr,
   1088 					 show_func, set_list, show_list);
   1089   return cmds;
   1090 }
   1091 
   1092 /* Accept `unlimited' or 0, translated internally to INT_MAX.  */
   1093 const literal_def integer_unlimited_literals[] =
   1094   {
   1095     { "unlimited", INT_MAX, 0 },
   1096     { nullptr }
   1097   };
   1098 
   1099 /* Same as above but using `integer_unlimited_literals', with a pointer
   1100    to a global storage buffer.  */
   1101 
   1102 set_show_commands
   1103 add_setshow_integer_cmd (const char *name, enum command_class theclass,
   1104 			 int *var,
   1105 			 const char *set_doc, const char *show_doc,
   1106 			 const char *help_doc,
   1107 			 cmd_func_ftype *set_func,
   1108 			 show_value_ftype *show_func,
   1109 			 struct cmd_list_element **set_list,
   1110 			 struct cmd_list_element **show_list)
   1111 {
   1112   set_show_commands commands
   1113     = add_setshow_cmd_full<int> (name, theclass, var_integer, var,
   1114 				 integer_unlimited_literals,
   1115 				 set_doc, show_doc, help_doc,
   1116 				 nullptr, nullptr, set_func,
   1117 				 show_func, set_list, show_list);
   1118   return commands;
   1119 }
   1120 
   1121 /* Same as above but using a getter and a setter function instead of a pointer
   1122    to a global storage buffer.  */
   1123 
   1124 set_show_commands
   1125 add_setshow_integer_cmd (const char *name, command_class theclass,
   1126 			 const char *set_doc, const char *show_doc,
   1127 			 const char *help_doc,
   1128 			 setting_func_types<int>::set set_func,
   1129 			 setting_func_types<int>::get get_func,
   1130 			 show_value_ftype *show_func,
   1131 			 cmd_list_element **set_list,
   1132 			 cmd_list_element **show_list)
   1133 {
   1134   auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
   1135 					 integer_unlimited_literals,
   1136 					 set_doc, show_doc, help_doc, set_func,
   1137 					 get_func, nullptr, show_func, set_list,
   1138 					 show_list);
   1139   return cmds;
   1140 }
   1141 
   1142 /* Add element named NAME to both the set and show command LISTs (the
   1143    list for set/show or some sublist thereof).  CLASS is as in
   1144    add_cmd.  VAR is address of the variable which will contain the
   1145    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
   1146 
   1147 set_show_commands
   1148 add_setshow_pinteger_cmd (const char *name, enum command_class theclass,
   1149 			  int *var, const literal_def *extra_literals,
   1150 			  const char *set_doc, const char *show_doc,
   1151 			  const char *help_doc,
   1152 			  cmd_func_ftype *set_func,
   1153 			  show_value_ftype *show_func,
   1154 			  struct cmd_list_element **set_list,
   1155 			  struct cmd_list_element **show_list)
   1156 {
   1157   set_show_commands commands
   1158     = add_setshow_cmd_full<int> (name, theclass, var_pinteger, var,
   1159 				 extra_literals, set_doc, show_doc,
   1160 				 help_doc, nullptr, nullptr, set_func,
   1161 				 show_func, set_list, show_list);
   1162   return commands;
   1163 }
   1164 
   1165 /* Same as above but using a getter and a setter function instead of a pointer
   1166    to a global storage buffer.  */
   1167 
   1168 set_show_commands
   1169 add_setshow_pinteger_cmd (const char *name, command_class theclass,
   1170 			  const literal_def *extra_literals,
   1171 			  const char *set_doc, const char *show_doc,
   1172 			  const char *help_doc,
   1173 			  setting_func_types<int>::set set_func,
   1174 			  setting_func_types<int>::get get_func,
   1175 			  show_value_ftype *show_func,
   1176 			  cmd_list_element **set_list,
   1177 			  cmd_list_element **show_list)
   1178 {
   1179   auto cmds = add_setshow_cmd_full<int> (name, theclass, var_pinteger, nullptr,
   1180 					 extra_literals, set_doc, show_doc,
   1181 					 help_doc, set_func, get_func, nullptr,
   1182 					 show_func, set_list, show_list);
   1183   return cmds;
   1184 }
   1185 
   1186 /* Add element named NAME to both the set and show command LISTs (the
   1187    list for set/show or some sublist thereof).  THECLASS is as in
   1188    add_cmd.  VAR is address of the variable which will contain the
   1189    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
   1190 
   1191 set_show_commands
   1192 add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
   1193 			  unsigned int *var, const literal_def *extra_literals,
   1194 			  const char *set_doc, const char *show_doc,
   1195 			  const char *help_doc,
   1196 			  cmd_func_ftype *set_func,
   1197 			  show_value_ftype *show_func,
   1198 			  struct cmd_list_element **set_list,
   1199 			  struct cmd_list_element **show_list)
   1200 {
   1201   set_show_commands commands
   1202     = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var,
   1203 					  extra_literals, set_doc, show_doc,
   1204 					  help_doc, nullptr, nullptr, set_func,
   1205 					  show_func, set_list, show_list);
   1206   return commands;
   1207 }
   1208 
   1209 /* Same as above but using a getter and a setter function instead of a pointer
   1210    to a global storage buffer.  */
   1211 
   1212 set_show_commands
   1213 add_setshow_uinteger_cmd (const char *name, command_class theclass,
   1214 			  const literal_def *extra_literals,
   1215 			  const char *set_doc, const char *show_doc,
   1216 			  const char *help_doc,
   1217 			  setting_func_types<unsigned int>::set set_func,
   1218 			  setting_func_types<unsigned int>::get get_func,
   1219 			  show_value_ftype *show_func,
   1220 			  cmd_list_element **set_list,
   1221 			  cmd_list_element **show_list)
   1222 {
   1223   auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
   1224 						  nullptr, extra_literals,
   1225 						  set_doc, show_doc, help_doc,
   1226 						  set_func, get_func, nullptr,
   1227 						  show_func, set_list,
   1228 						  show_list);
   1229   return cmds;
   1230 }
   1231 
   1232 /* Accept `unlimited' or 0, translated internally to UINT_MAX.  */
   1233 const literal_def uinteger_unlimited_literals[] =
   1234   {
   1235     { "unlimited", UINT_MAX, 0 },
   1236     { nullptr }
   1237   };
   1238 
   1239 /* Same as above but using `uinteger_unlimited_literals', with a pointer
   1240    to a global storage buffer.  */
   1241 
   1242 set_show_commands
   1243 add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
   1244 			  unsigned int *var,
   1245 			  const char *set_doc, const char *show_doc,
   1246 			  const char *help_doc,
   1247 			  cmd_func_ftype *set_func,
   1248 			  show_value_ftype *show_func,
   1249 			  struct cmd_list_element **set_list,
   1250 			  struct cmd_list_element **show_list)
   1251 {
   1252   set_show_commands commands
   1253     = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var,
   1254 					  uinteger_unlimited_literals,
   1255 					  set_doc, show_doc, help_doc, nullptr,
   1256 					  nullptr, set_func, show_func,
   1257 					  set_list, show_list);
   1258   return commands;
   1259 }
   1260 
   1261 /* Same as above but using a getter and a setter function instead of a pointer
   1262    to a global storage buffer.  */
   1263 
   1264 set_show_commands
   1265 add_setshow_uinteger_cmd (const char *name, command_class theclass,
   1266 			  const char *set_doc, const char *show_doc,
   1267 			  const char *help_doc,
   1268 			  setting_func_types<unsigned int>::set set_func,
   1269 			  setting_func_types<unsigned int>::get get_func,
   1270 			  show_value_ftype *show_func,
   1271 			  cmd_list_element **set_list,
   1272 			  cmd_list_element **show_list)
   1273 {
   1274   auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
   1275 						  nullptr,
   1276 						  uinteger_unlimited_literals,
   1277 						  set_doc, show_doc, help_doc,
   1278 						  set_func, get_func, nullptr,
   1279 						  show_func, set_list,
   1280 						  show_list);
   1281   return cmds;
   1282 }
   1283 
   1284 /* Add element named NAME to both the set and show command LISTs (the
   1285    list for set/show or some sublist thereof).  THECLASS is as in
   1286    add_cmd.  VAR is address of the variable which will contain the
   1287    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
   1288 
   1289 set_show_commands
   1290 add_setshow_zinteger_cmd (const char *name, enum command_class theclass,
   1291 			  int *var,
   1292 			  const char *set_doc, const char *show_doc,
   1293 			  const char *help_doc,
   1294 			  cmd_func_ftype *set_func,
   1295 			  show_value_ftype *show_func,
   1296 			  struct cmd_list_element **set_list,
   1297 			  struct cmd_list_element **show_list)
   1298 {
   1299   return add_setshow_cmd_full<int> (name, theclass, var_integer, var,
   1300 				    set_doc, show_doc, help_doc,
   1301 				    nullptr, nullptr, set_func,
   1302 				    show_func, set_list, show_list);
   1303 }
   1304 
   1305 /* Same as above but using a getter and a setter function instead of a pointer
   1306    to a global storage buffer.  */
   1307 
   1308 set_show_commands
   1309 add_setshow_zinteger_cmd (const char *name, command_class theclass,
   1310 			  const char *set_doc, const char *show_doc,
   1311 			  const char *help_doc,
   1312 			  setting_func_types<int>::set set_func,
   1313 			  setting_func_types<int>::get get_func,
   1314 			  show_value_ftype *show_func,
   1315 			  cmd_list_element **set_list,
   1316 			  cmd_list_element **show_list)
   1317 {
   1318   return add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
   1319 				    set_doc, show_doc, help_doc, set_func,
   1320 				    get_func, nullptr, show_func, set_list,
   1321 				    show_list);
   1322 }
   1323 
   1324 /* Accept `unlimited' or -1, using -1 internally.  */
   1325 const literal_def pinteger_unlimited_literals[] =
   1326   {
   1327     { "unlimited", -1, -1 },
   1328     { nullptr }
   1329   };
   1330 
   1331 /* Same as above but using `pinteger_unlimited_literals', with a pointer
   1332    to a global storage buffer.  */
   1333 
   1334 set_show_commands
   1335 add_setshow_zuinteger_unlimited_cmd (const char *name,
   1336 				     enum command_class theclass,
   1337 				     int *var,
   1338 				     const char *set_doc,
   1339 				     const char *show_doc,
   1340 				     const char *help_doc,
   1341 				     cmd_func_ftype *set_func,
   1342 				     show_value_ftype *show_func,
   1343 				     struct cmd_list_element **set_list,
   1344 				     struct cmd_list_element **show_list)
   1345 {
   1346   set_show_commands commands
   1347     = add_setshow_cmd_full<int> (name, theclass, var_pinteger, var,
   1348 				 pinteger_unlimited_literals,
   1349 				 set_doc, show_doc, help_doc, nullptr,
   1350 				 nullptr, set_func, show_func, set_list,
   1351 				 show_list);
   1352   return commands;
   1353 }
   1354 
   1355 /* Same as above but using a getter and a setter function instead of a pointer
   1356    to a global storage buffer.  */
   1357 
   1358 set_show_commands
   1359 add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass,
   1360 				     const char *set_doc, const char *show_doc,
   1361 				     const char *help_doc,
   1362 				     setting_func_types<int>::set set_func,
   1363 				     setting_func_types<int>::get get_func,
   1364 				     show_value_ftype *show_func,
   1365 				     cmd_list_element **set_list,
   1366 				     cmd_list_element **show_list)
   1367 {
   1368   auto cmds
   1369     = add_setshow_cmd_full<int> (name, theclass, var_pinteger, nullptr,
   1370 				 pinteger_unlimited_literals,
   1371 				 set_doc, show_doc, help_doc, set_func,
   1372 				 get_func, nullptr, show_func, set_list,
   1373 				 show_list);
   1374   return cmds;
   1375 }
   1376 
   1377 /* Add element named NAME to both the set and show command LISTs (the
   1378    list for set/show or some sublist thereof).  THECLASS is as in
   1379    add_cmd.  VAR is address of the variable which will contain the
   1380    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
   1381 
   1382 set_show_commands
   1383 add_setshow_zuinteger_cmd (const char *name, enum command_class theclass,
   1384 			   unsigned int *var,
   1385 			   const char *set_doc, const char *show_doc,
   1386 			   const char *help_doc,
   1387 			   cmd_func_ftype *set_func,
   1388 			   show_value_ftype *show_func,
   1389 			   struct cmd_list_element **set_list,
   1390 			   struct cmd_list_element **show_list)
   1391 {
   1392   return add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
   1393 					     var, set_doc, show_doc, help_doc,
   1394 					     nullptr, nullptr, set_func,
   1395 					     show_func, set_list, show_list);
   1396 }
   1397 
   1398 /* Same as above but using a getter and a setter function instead of a pointer
   1399    to a global storage buffer.  */
   1400 
   1401 set_show_commands
   1402 add_setshow_zuinteger_cmd (const char *name, command_class theclass,
   1403 			   const char *set_doc, const char *show_doc,
   1404 			   const char *help_doc,
   1405 			   setting_func_types<unsigned int>::set set_func,
   1406 			   setting_func_types<unsigned int>::get get_func,
   1407 			   show_value_ftype *show_func,
   1408 			   cmd_list_element **set_list,
   1409 			   cmd_list_element **show_list)
   1410 {
   1411   return add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
   1412 					     nullptr, set_doc, show_doc,
   1413 					     help_doc, set_func, get_func,
   1414 					     nullptr, show_func, set_list,
   1415 					     show_list);
   1416 }
   1417 
   1418 /* Remove the command named NAME from the command list.  Return the list
   1419    commands which were aliased to the deleted command.  The various *HOOKs are
   1420    set to the pre- and post-hook commands for the deleted command.  If the
   1421    command does not have a hook, the corresponding out parameter is set to
   1422    NULL.  */
   1423 
   1424 static cmd_list_element::aliases_list_type
   1425 delete_cmd (const char *name, struct cmd_list_element **list,
   1426 	    struct cmd_list_element **prehook,
   1427 	    struct cmd_list_element **prehookee,
   1428 	    struct cmd_list_element **posthook,
   1429 	    struct cmd_list_element **posthookee)
   1430 {
   1431   struct cmd_list_element *iter;
   1432   struct cmd_list_element **previous_chain_ptr;
   1433   cmd_list_element::aliases_list_type aliases;
   1434 
   1435   *prehook = NULL;
   1436   *prehookee = NULL;
   1437   *posthook = NULL;
   1438   *posthookee = NULL;
   1439   previous_chain_ptr = list;
   1440 
   1441   for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
   1442     {
   1443       if (strcmp (iter->name, name) == 0)
   1444 	{
   1445 	  if (iter->destroyer)
   1446 	    iter->destroyer (iter, iter->context ());
   1447 
   1448 	  if (iter->hookee_pre)
   1449 	    iter->hookee_pre->hook_pre = 0;
   1450 	  *prehook = iter->hook_pre;
   1451 	  *prehookee = iter->hookee_pre;
   1452 	  if (iter->hookee_post)
   1453 	    iter->hookee_post->hook_post = 0;
   1454 	  *posthook = iter->hook_post;
   1455 	  *posthookee = iter->hookee_post;
   1456 
   1457 	  /* Update the link.  */
   1458 	  *previous_chain_ptr = iter->next;
   1459 
   1460 	  aliases = std::move (iter->aliases);
   1461 
   1462 	  /* If this command was an alias, remove it from the list of
   1463 	     aliases.  */
   1464 	  if (iter->is_alias ())
   1465 	    {
   1466 	      auto it = iter->alias_target->aliases.iterator_to (*iter);
   1467 	      iter->alias_target->aliases.erase (it);
   1468 	    }
   1469 
   1470 	  delete iter;
   1471 
   1472 	  /* We won't see another command with the same name.  */
   1473 	  break;
   1474 	}
   1475       else
   1476 	previous_chain_ptr = &iter->next;
   1477     }
   1478 
   1479   return aliases;
   1480 }
   1481 
   1482 /* Shorthands to the commands above.  */
   1484 
   1485 /* Add an element to the list of info subcommands.  */
   1486 
   1487 struct cmd_list_element *
   1488 add_info (const char *name, cmd_simple_func_ftype *fun, const char *doc)
   1489 {
   1490   return add_cmd (name, class_info, fun, doc, &infolist);
   1491 }
   1492 
   1493 /* Add an alias to the list of info subcommands.  */
   1494 
   1495 cmd_list_element *
   1496 add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
   1497 {
   1498   return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist);
   1499 }
   1500 
   1501 /* Add an element to the list of commands.  */
   1502 
   1503 struct cmd_list_element *
   1504 add_com (const char *name, enum command_class theclass,
   1505 	 cmd_simple_func_ftype *fun,
   1506 	 const char *doc)
   1507 {
   1508   return add_cmd (name, theclass, fun, doc, &cmdlist);
   1509 }
   1510 
   1511 /* Add an alias or abbreviation command to the list of commands.
   1512    For aliases predefined by GDB (such as bt), THECLASS must be
   1513    different of class_alias, as class_alias is used to identify
   1514    user defined aliases.  */
   1515 
   1516 cmd_list_element *
   1517 add_com_alias (const char *name, cmd_list_element *target,
   1518 	       command_class theclass, int abbrev_flag)
   1519 {
   1520   return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist);
   1521 }
   1522 
   1523 /* Add an element with a suppress notification to the list of commands.  */
   1524 
   1525 struct cmd_list_element *
   1526 add_com_suppress_notification (const char *name, enum command_class theclass,
   1527 			       cmd_simple_func_ftype *fun, const char *doc,
   1528 			       bool *suppress_notification)
   1529 {
   1530   return add_cmd_suppress_notification (name, theclass, fun, doc,
   1531 					&cmdlist, suppress_notification);
   1532 }
   1533 
   1534 /* Print the prefix of C followed by name of C in title style.  */
   1535 
   1536 static void
   1537 fput_command_name_styled (const cmd_list_element &c, struct ui_file *stream)
   1538 {
   1539   std::string prefixname
   1540     = c.prefix == nullptr ? "" : c.prefix->prefixname ();
   1541 
   1542   fprintf_styled (stream, title_style.style (), "%s%s",
   1543 		  prefixname.c_str (), c.name);
   1544 }
   1545 
   1546 /* True if ALIAS has a user-defined documentation.  */
   1547 
   1548 static bool
   1549 user_documented_alias (const cmd_list_element &alias)
   1550 {
   1551   gdb_assert (alias.is_alias ());
   1552   /* Alias is user documented if it has an allocated documentation
   1553      that differs from the aliased command.  */
   1554   return (alias.doc_allocated
   1555 	  && strcmp (alias.doc, alias.alias_target->doc) != 0);
   1556 }
   1557 
   1558 /* Print the definition of alias C using title style for alias
   1559    and aliased command.  */
   1560 
   1561 static void
   1562 fput_alias_definition_styled (const cmd_list_element &c,
   1563 			      struct ui_file *stream)
   1564 {
   1565   gdb_assert (c.is_alias ());
   1566   gdb_puts ("  alias ", stream);
   1567   fput_command_name_styled (c, stream);
   1568   gdb_printf (stream, " = ");
   1569   fput_command_name_styled (*c.alias_target, stream);
   1570   gdb_printf (stream, " %s\n", c.default_args.c_str ());
   1571 }
   1572 
   1573 /* Print the definition of CMD aliases not deprecated and having default args
   1574    and not specifically documented by the user.  */
   1575 
   1576 static void
   1577 fput_aliases_definition_styled (const cmd_list_element &cmd,
   1578 				struct ui_file *stream)
   1579 {
   1580   for (const cmd_list_element &alias : cmd.aliases)
   1581     if (!alias.cmd_deprecated
   1582 	&& !user_documented_alias (alias)
   1583 	&& !alias.default_args.empty ())
   1584       fput_alias_definition_styled (alias, stream);
   1585 }
   1586 
   1587 /* If C has one or more aliases, style print the name of C and the name of its
   1588    aliases not documented specifically by the user, separated by commas.
   1589    If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
   1590    If one or more names are printed, POSTFIX is printed after the last name.
   1591 */
   1592 
   1593 static void
   1594 fput_command_names_styled (const cmd_list_element &c,
   1595 			   bool always_fput_c_name, const char *postfix,
   1596 			   struct ui_file *stream)
   1597 {
   1598   /* First, check if we are going to print something.  That is, either if
   1599      ALWAYS_FPUT_C_NAME is true or if there exists at least one non-deprecated
   1600      alias not documented specifically by the user.  */
   1601 
   1602   auto print_alias = [] (const cmd_list_element &alias)
   1603     {
   1604       return !alias.cmd_deprecated && !user_documented_alias (alias);
   1605     };
   1606 
   1607   bool print_something = always_fput_c_name;
   1608   if (!print_something)
   1609     for (const cmd_list_element &alias : c.aliases)
   1610       {
   1611 	if (!print_alias (alias))
   1612 	  continue;
   1613 
   1614 	print_something = true;
   1615 	break;
   1616       }
   1617 
   1618   if (print_something)
   1619     fput_command_name_styled (c, stream);
   1620 
   1621   for (const cmd_list_element &alias : c.aliases)
   1622     {
   1623       if (!print_alias (alias))
   1624 	continue;
   1625 
   1626       gdb_puts (", ", stream);
   1627       stream->wrap_here (3);
   1628       fput_command_name_styled (alias, stream);
   1629     }
   1630 
   1631   if (print_something)
   1632     gdb_puts (postfix, stream);
   1633 }
   1634 
   1635 /* If VERBOSE, print the full help for command C and highlight the
   1636    documentation parts matching HIGHLIGHT,
   1637    otherwise print only one-line help for command C.  */
   1638 
   1639 static void
   1640 print_doc_of_command (const cmd_list_element &c, bool verbose,
   1641 		      compiled_regex &highlight, struct ui_file *stream)
   1642 {
   1643   /* When printing the full documentation, add a line to separate
   1644      this documentation from the previous command help, in the likely
   1645      case that apropos finds several commands.  */
   1646   if (verbose)
   1647     gdb_puts ("\n", stream);
   1648 
   1649   fput_command_names_styled (c, true,
   1650 			     verbose ? "" : " -- ", stream);
   1651   if (verbose)
   1652     {
   1653       gdb_puts ("\n", stream);
   1654       fput_aliases_definition_styled (c, stream);
   1655       fputs_highlighted (c.doc, highlight, stream);
   1656       gdb_puts ("\n", stream);
   1657     }
   1658   else
   1659     {
   1660       print_doc_line (stream, c.doc, false);
   1661       gdb_puts ("\n", stream);
   1662       fput_aliases_definition_styled (c, stream);
   1663     }
   1664 }
   1665 
   1666 /* Recursively walk the commandlist structures, and print out the
   1667    documentation of commands that match our regex in either their
   1668    name, or their documentation.
   1669    If VERBOSE, prints the complete documentation and highlight the
   1670    documentation parts matching REGEX, otherwise prints only
   1671    the first line.
   1672 */
   1673 void
   1674 apropos_cmd (struct ui_file *stream,
   1675 	     struct cmd_list_element *commandlist,
   1676 	     bool verbose, compiled_regex &regex)
   1677 {
   1678   struct cmd_list_element *c;
   1679   int returnvalue;
   1680 
   1681   /* Walk through the commands.  */
   1682   for (c=commandlist;c;c=c->next)
   1683     {
   1684       if (c->is_alias () && !user_documented_alias (*c))
   1685 	{
   1686 	  /* Command aliases/abbreviations not specifically documented by the
   1687 	     user are skipped to ensure we print the doc of a command only once,
   1688 	     when encountering the aliased command.  */
   1689 	  continue;
   1690 	}
   1691 
   1692       returnvalue = -1; /* Needed to avoid double printing.  */
   1693       if (c->name != NULL)
   1694 	{
   1695 	  size_t name_len = strlen (c->name);
   1696 
   1697 	  /* Try to match against the name.  */
   1698 	  returnvalue = regex.search (c->name, name_len, 0, name_len, NULL);
   1699 	  if (returnvalue >= 0)
   1700 	    print_doc_of_command (*c, verbose, regex, stream);
   1701 
   1702 	  /* Try to match against the name of the aliases.  */
   1703 	  for (const cmd_list_element &alias : c->aliases)
   1704 	    {
   1705 	      name_len = strlen (alias.name);
   1706 	      returnvalue = regex.search (alias.name, name_len, 0, name_len, NULL);
   1707 	      if (returnvalue >= 0)
   1708 		{
   1709 		  print_doc_of_command (*c, verbose, regex, stream);
   1710 		  break;
   1711 		}
   1712 	    }
   1713 	}
   1714       if (c->doc != NULL && returnvalue < 0)
   1715 	{
   1716 	  size_t doc_len = strlen (c->doc);
   1717 
   1718 	  /* Try to match against documentation.  */
   1719 	  if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0)
   1720 	    print_doc_of_command (*c, verbose, regex, stream);
   1721 	}
   1722       /* Check if this command has subcommands.  */
   1723       if (c->is_prefix ())
   1724 	{
   1725 	  /* Recursively call ourselves on the subcommand list,
   1726 	     passing the right prefix in.  */
   1727 	  apropos_cmd (stream, *c->subcommands, verbose, regex);
   1728 	}
   1729     }
   1730 }
   1731 
   1732 /* This command really has to deal with two things:
   1733    1) I want documentation on *this string* (usually called by
   1734       "help commandname").
   1735 
   1736    2) I want documentation on *this list* (usually called by giving a
   1737       command that requires subcommands.  Also called by saying just
   1738       "help".)
   1739 
   1740    I am going to split this into two separate commands, help_cmd and
   1741    help_list.  */
   1742 
   1743 void
   1744 help_cmd (const char *command, struct ui_file *stream)
   1745 {
   1746   struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd;
   1747 
   1748   if (!command)
   1749     {
   1750       help_list (cmdlist, "", all_classes, stream);
   1751       return;
   1752     }
   1753 
   1754   if (strcmp (command, "all") == 0)
   1755     {
   1756       help_all (stream);
   1757       return;
   1758     }
   1759 
   1760   const char *orig_command = command;
   1761   c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0);
   1762 
   1763   if (c == 0)
   1764     return;
   1765 
   1766   lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd);
   1767 
   1768   /* There are three cases here.
   1769      If c->subcommands is nonzero, we have a prefix command.
   1770      Print its documentation, then list its subcommands.
   1771 
   1772      If c->func is non NULL, we really have a command.  Print its
   1773      documentation and return.
   1774 
   1775      If c->func is NULL, we have a class name.  Print its
   1776      documentation (as if it were a command) and then set class to the
   1777      number of this class so that the commands in the class will be
   1778      listed.  */
   1779 
   1780   if (alias == nullptr || !user_documented_alias (*alias))
   1781     {
   1782       /* Case of a normal command, or an alias not explicitly
   1783 	 documented by the user.  */
   1784       /* If the user asked 'help somecommand' and there is no alias,
   1785 	 the false indicates to not output the (single) command name.  */
   1786       fput_command_names_styled (*c, false, "\n", stream);
   1787       fput_aliases_definition_styled (*c, stream);
   1788       gdb_puts (c->doc, stream);
   1789     }
   1790   else
   1791     {
   1792       /* Case of an alias explicitly documented by the user.
   1793 	 Only output the alias definition and its explicit documentation.  */
   1794       fput_alias_definition_styled (*alias, stream);
   1795       fput_command_names_styled (*alias, false, "\n", stream);
   1796       gdb_puts (alias->doc, stream);
   1797     }
   1798   gdb_puts ("\n", stream);
   1799 
   1800   if (!c->is_prefix () && !c->is_command_class_help ())
   1801     return;
   1802 
   1803   gdb_printf (stream, "\n");
   1804 
   1805   /* If this is a prefix command, print it's subcommands.  */
   1806   if (c->is_prefix ())
   1807     help_list (*c->subcommands, c->prefixname ().c_str (),
   1808 	       all_commands, stream);
   1809 
   1810   /* If this is a class name, print all of the commands in the class.  */
   1811   if (c->is_command_class_help ())
   1812     help_list (cmdlist, "", c->theclass, stream);
   1813 
   1814   if (c->hook_pre || c->hook_post)
   1815     gdb_printf (stream,
   1816 		"\nThis command has a hook (or hooks) defined:\n");
   1817 
   1818   if (c->hook_pre)
   1819     gdb_printf (stream,
   1820 		"\tThis command is run after  : %s (pre hook)\n",
   1821 		c->hook_pre->name);
   1822   if (c->hook_post)
   1823     gdb_printf (stream,
   1824 		"\tThis command is run before : %s (post hook)\n",
   1825 		c->hook_post->name);
   1826 }
   1827 
   1828 /*
   1829  * Get a specific kind of help on a command list.
   1830  *
   1831  * LIST is the list.
   1832  * CMDTYPE is the prefix to use in the title string.
   1833  * THECLASS is the class with which to list the nodes of this list (see
   1834  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
   1835  * everything, ALL_CLASSES for just classes, and non-negative for only things
   1836  * in a specific class.
   1837  * and STREAM is the output stream on which to print things.
   1838  * If you call this routine with a class >= 0, it recurses.
   1839  */
   1840 void
   1841 help_list (struct cmd_list_element *list, const char *cmdtype,
   1842 	   enum command_class theclass, struct ui_file *stream)
   1843 {
   1844   int len;
   1845   char *cmdtype1, *cmdtype2;
   1846 
   1847   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
   1848    */
   1849   len = strlen (cmdtype);
   1850   cmdtype1 = (char *) alloca (len + 1);
   1851   cmdtype1[0] = 0;
   1852   cmdtype2 = (char *) alloca (len + 4);
   1853   cmdtype2[0] = 0;
   1854   if (len)
   1855     {
   1856       cmdtype1[0] = ' ';
   1857       memcpy (cmdtype1 + 1, cmdtype, len - 1);
   1858       cmdtype1[len] = 0;
   1859       memcpy (cmdtype2, cmdtype, len - 1);
   1860       strcpy (cmdtype2 + len - 1, " sub");
   1861     }
   1862 
   1863   if (theclass == all_classes)
   1864     gdb_printf (stream, "List of classes of %scommands:\n\n", cmdtype2);
   1865   else
   1866     gdb_printf (stream, "List of %scommands:\n\n", cmdtype2);
   1867 
   1868   help_cmd_list (list, theclass, theclass >= 0, stream);
   1869 
   1870   if (theclass == all_classes)
   1871     {
   1872       gdb_printf (stream, "\n\
   1873 Type \"help%s\" followed by a class name for a list of commands in ",
   1874 		  cmdtype1);
   1875       stream->wrap_here (0);
   1876       gdb_printf (stream, "that class.");
   1877 
   1878       gdb_printf (stream, "\n\
   1879 Type \"help all\" for the list of all commands.");
   1880     }
   1881 
   1882   gdb_printf (stream, "\nType \"help%s\" followed by %scommand name ",
   1883 	      cmdtype1, cmdtype2);
   1884   stream->wrap_here (0);
   1885   gdb_puts ("for ", stream);
   1886   stream->wrap_here (0);
   1887   gdb_puts ("full ", stream);
   1888   stream->wrap_here (0);
   1889   gdb_puts ("documentation.\n", stream);
   1890   gdb_puts ("Type \"apropos word\" to search "
   1891 	    "for commands related to \"word\".\n", stream);
   1892   gdb_puts ("Type \"apropos -v word\" for full documentation", stream);
   1893   stream->wrap_here (0);
   1894   gdb_puts (" of commands related to \"word\".\n", stream);
   1895   gdb_puts ("Command name abbreviations are allowed if unambiguous.\n",
   1896 	    stream);
   1897 }
   1898 
   1899 static void
   1900 help_all (struct ui_file *stream)
   1901 {
   1902   struct cmd_list_element *c;
   1903   int seen_unclassified = 0;
   1904 
   1905   for (c = cmdlist; c; c = c->next)
   1906     {
   1907       if (c->abbrev_flag)
   1908 	continue;
   1909       /* If this is a class name, print all of the commands in the
   1910 	 class.  */
   1911 
   1912       if (c->is_command_class_help ())
   1913 	{
   1914 	  gdb_printf (stream, "\nCommand class: %s\n\n", c->name);
   1915 	  help_cmd_list (cmdlist, c->theclass, true, stream);
   1916 	}
   1917     }
   1918 
   1919   /* While it's expected that all commands are in some class,
   1920      as a safety measure, we'll print commands outside of any
   1921      class at the end.  */
   1922 
   1923   for (c = cmdlist; c; c = c->next)
   1924     {
   1925       if (c->abbrev_flag)
   1926 	continue;
   1927 
   1928       if (c->theclass == no_class)
   1929 	{
   1930 	  if (!seen_unclassified)
   1931 	    {
   1932 	      gdb_printf (stream, "\nUnclassified commands\n\n");
   1933 	      seen_unclassified = 1;
   1934 	    }
   1935 	  print_help_for_command (*c, true, stream);
   1936 	}
   1937     }
   1938 
   1939 }
   1940 
   1941 /* See cli-decode.h.  */
   1942 
   1943 void
   1944 print_doc_line (struct ui_file *stream, const char *str,
   1945 		bool for_value_prefix)
   1946 {
   1947   static char *line_buffer = 0;
   1948   static int line_size;
   1949   const char *p;
   1950 
   1951   if (!line_buffer)
   1952     {
   1953       line_size = 80;
   1954       line_buffer = (char *) xmalloc (line_size);
   1955     }
   1956 
   1957   /* Searches for the first end of line or the end of STR.  */
   1958   p = str;
   1959   while (*p && *p != '\n')
   1960     p++;
   1961   if (p - str > line_size - 1)
   1962     {
   1963       line_size = p - str + 1;
   1964       xfree (line_buffer);
   1965       line_buffer = (char *) xmalloc (line_size);
   1966     }
   1967   strncpy (line_buffer, str, p - str);
   1968   if (for_value_prefix)
   1969     {
   1970       if (islower (line_buffer[0]))
   1971 	line_buffer[0] = toupper (line_buffer[0]);
   1972       gdb_assert (p > str);
   1973       if (line_buffer[p - str - 1] == '.')
   1974 	line_buffer[p - str - 1] = '\0';
   1975       else
   1976 	line_buffer[p - str] = '\0';
   1977     }
   1978   else
   1979     line_buffer[p - str] = '\0';
   1980   gdb_puts (line_buffer, stream);
   1981 }
   1982 
   1983 /* Print one-line help for command C.
   1984    If RECURSE is non-zero, also print one-line descriptions
   1985    of all prefixed subcommands.  */
   1986 static void
   1987 print_help_for_command (const cmd_list_element &c,
   1988 			bool recurse, struct ui_file *stream)
   1989 {
   1990   fput_command_names_styled (c, true, " -- ", stream);
   1991   print_doc_line (stream, c.doc, false);
   1992   gdb_puts ("\n", stream);
   1993   if (!c.default_args.empty ())
   1994     fput_alias_definition_styled (c, stream);
   1995   fput_aliases_definition_styled (c, stream);
   1996 
   1997   if (recurse
   1998       && c.is_prefix ()
   1999       && c.abbrev_flag == 0)
   2000     /* Subcommands of a prefix command typically have 'all_commands'
   2001        as class.  If we pass CLASS to recursive invocation,
   2002        most often we won't see anything.  */
   2003     help_cmd_list (*c.subcommands, all_commands, true, stream);
   2004 }
   2005 
   2006 /*
   2007  * Implement a help command on command list LIST.
   2008  * RECURSE should be non-zero if this should be done recursively on
   2009  * all sublists of LIST.
   2010  * STREAM is the stream upon which the output should be written.
   2011  * THECLASS should be:
   2012  *      A non-negative class number to list only commands in that
   2013  *      ALL_COMMANDS to list all commands in list.
   2014  *      ALL_CLASSES  to list all classes in list.
   2015  *
   2016  *   Note that aliases are only shown when THECLASS is class_alias.
   2017  *   In the other cases, the aliases will be shown together with their
   2018  *   aliased command.
   2019  *
   2020  *   Note that RECURSE will be active on *all* sublists, not just the
   2021  * ones selected by the criteria above (ie. the selection mechanism
   2022  * is at the low level, not the high-level).
   2023  */
   2024 
   2025 static void
   2026 help_cmd_list (struct cmd_list_element *list, enum command_class theclass,
   2027 	       bool recurse, struct ui_file *stream)
   2028 {
   2029   struct cmd_list_element *c;
   2030 
   2031   for (c = list; c; c = c->next)
   2032     {
   2033       if (c->abbrev_flag == 1 || c->cmd_deprecated)
   2034 	{
   2035 	  /* Do not show abbreviations or deprecated commands.  */
   2036 	  continue;
   2037 	}
   2038 
   2039       if (c->is_alias () && theclass != class_alias)
   2040 	{
   2041 	  /* Do not show an alias, unless specifically showing the
   2042 	     list of aliases:  for all other classes, an alias is
   2043 	     shown (if needed) together with its aliased command.  */
   2044 	  continue;
   2045 	}
   2046 
   2047       if (theclass == all_commands
   2048 	  || (theclass == all_classes && c->is_command_class_help ())
   2049 	  || (theclass == c->theclass && !c->is_command_class_help ()))
   2050 	{
   2051 	  /* show C when
   2052 	     - showing all commands
   2053 	     - showing all classes and C is a help class
   2054 	     - showing commands of THECLASS and C is not the help class  */
   2055 
   2056 	  /* If we show the class_alias and C is an alias, do not recurse,
   2057 	     as this would show the (possibly very long) not very useful
   2058 	     list of sub-commands of the aliased command.  */
   2059 	  print_help_for_command
   2060 	    (*c,
   2061 	     recurse && (theclass != class_alias || !c->is_alias ()),
   2062 	     stream);
   2063 	  continue;
   2064 	}
   2065 
   2066       if (recurse
   2067 	  && (theclass == class_user || theclass == class_alias)
   2068 	  && c->is_prefix ())
   2069 	{
   2070 	  /* User-defined commands or aliases may be subcommands.  */
   2071 	  help_cmd_list (*c->subcommands, theclass, recurse, stream);
   2072 	  continue;
   2073 	}
   2074 
   2075       /* Do not show C or recurse on C, e.g. because C does not belong to
   2076 	 THECLASS or because C is a help class.  */
   2077     }
   2078 }
   2079 
   2080 
   2082 /* Search the input clist for 'command'.  Return the command if
   2083    found (or NULL if not), and return the number of commands
   2084    found in nfound.  */
   2085 
   2086 static struct cmd_list_element *
   2087 find_cmd (const char *command, int len, struct cmd_list_element *clist,
   2088 	  int ignore_help_classes, int *nfound)
   2089 {
   2090   struct cmd_list_element *found, *c;
   2091 
   2092   found = NULL;
   2093   *nfound = 0;
   2094   for (c = clist; c; c = c->next)
   2095     if (!strncmp (command, c->name, len)
   2096 	&& (!ignore_help_classes || !c->is_command_class_help ()))
   2097       {
   2098 	found = c;
   2099 	(*nfound)++;
   2100 	if (c->name[len] == '\0')
   2101 	  {
   2102 	    *nfound = 1;
   2103 	    break;
   2104 	  }
   2105       }
   2106   return found;
   2107 }
   2108 
   2109 /* Return the length of command name in TEXT.  */
   2110 
   2111 int
   2112 find_command_name_length (const char *text)
   2113 {
   2114   const char *p = text;
   2115 
   2116   /* Treating underscores as part of command words is important
   2117      so that "set args_foo()" doesn't get interpreted as
   2118      "set args _foo()".  */
   2119   /* Some characters are only used for TUI specific commands.
   2120      However, they are always allowed for the sake of consistency.
   2121 
   2122      Note that this is larger than the character set allowed when
   2123      creating user-defined commands.  */
   2124 
   2125   /* Recognize the single character commands so that, e.g., "!ls"
   2126      works as expected.  */
   2127   if (*p == '!' || *p == '|')
   2128     return 1;
   2129 
   2130   while (valid_cmd_char_p (*p)
   2131 	 /* Characters used by TUI specific commands.  */
   2132 	 || *p == '+' || *p == '<' || *p == '>' || *p == '$')
   2133     p++;
   2134 
   2135   return p - text;
   2136 }
   2137 
   2138 /* See command.h.  */
   2139 
   2140 bool
   2141 valid_cmd_char_p (int c)
   2142 {
   2143   /* Alas "42" is a legitimate user-defined command.
   2144      In the interests of not breaking anything we preserve that.  */
   2145 
   2146   return isalnum (c) || c == '-' || c == '_' || c == '.';
   2147 }
   2148 
   2149 /* See command.h.  */
   2150 
   2151 bool
   2152 valid_user_defined_cmd_name_p (const char *name)
   2153 {
   2154   const char *p;
   2155 
   2156   if (*name == '\0')
   2157     return false;
   2158 
   2159   for (p = name; *p != '\0'; ++p)
   2160     {
   2161       if (valid_cmd_char_p (*p))
   2162 	; /* Ok.  */
   2163       else
   2164 	return false;
   2165     }
   2166 
   2167   return true;
   2168 }
   2169 
   2170 /* See command.h.  */
   2171 
   2172 struct cmd_list_element *
   2173 lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
   2174 	      struct cmd_list_element **result_list, std::string *default_args,
   2175 	      int ignore_help_classes, bool lookup_for_completion_p)
   2176 {
   2177   char *command;
   2178   int len, nfound;
   2179   struct cmd_list_element *found, *c;
   2180   bool found_alias = false;
   2181   const char *line = *text;
   2182 
   2183   while (**text == ' ' || **text == '\t')
   2184     (*text)++;
   2185 
   2186   /* Identify the name of the command.  */
   2187   len = find_command_name_length (*text);
   2188 
   2189   /* If nothing but whitespace, return 0.  */
   2190   if (len == 0)
   2191     return 0;
   2192 
   2193   /* *text and p now bracket the first command word to lookup (and
   2194      it's length is len).  We copy this into a local temporary.  */
   2195 
   2196 
   2197   command = (char *) alloca (len + 1);
   2198   memcpy (command, *text, len);
   2199   command[len] = '\0';
   2200 
   2201   /* Look it up.  */
   2202   found = 0;
   2203   nfound = 0;
   2204   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
   2205 
   2206   /* If nothing matches, we have a simple failure.  */
   2207   if (nfound == 0)
   2208     return 0;
   2209 
   2210   if (nfound > 1)
   2211     {
   2212       if (result_list != nullptr)
   2213 	/* Will be modified in calling routine
   2214 	   if we know what the prefix command is.  */
   2215 	*result_list = 0;
   2216       if (default_args != nullptr)
   2217 	*default_args = std::string ();
   2218       return CMD_LIST_AMBIGUOUS;	/* Ambiguous.  */
   2219     }
   2220 
   2221   /* We've matched something on this list.  Move text pointer forward.  */
   2222 
   2223   *text += len;
   2224 
   2225   if (found->is_alias ())
   2226     {
   2227       /* We drop the alias (abbreviation) in favor of the command it
   2228        is pointing to.  If the alias is deprecated, though, we need to
   2229        warn the user about it before we drop it.  Note that while we
   2230        are warning about the alias, we may also warn about the command
   2231        itself and we will adjust the appropriate DEPRECATED_WARN_USER
   2232        flags.  */
   2233 
   2234       if (found->deprecated_warn_user && !lookup_for_completion_p)
   2235 	deprecated_cmd_warning (line, clist);
   2236 
   2237 
   2238       /* Return the default_args of the alias, not the default_args
   2239 	 of the command it is pointing to.  */
   2240       if (default_args != nullptr)
   2241 	*default_args = found->default_args;
   2242       found = found->alias_target;
   2243       found_alias = true;
   2244     }
   2245   /* If we found a prefix command, keep looking.  */
   2246 
   2247   if (found->is_prefix ())
   2248     {
   2249       c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args,
   2250 			ignore_help_classes, lookup_for_completion_p);
   2251       if (!c)
   2252 	{
   2253 	  /* Didn't find anything; this is as far as we got.  */
   2254 	  if (result_list != nullptr)
   2255 	    *result_list = clist;
   2256 	  if (!found_alias && default_args != nullptr)
   2257 	    *default_args = found->default_args;
   2258 	  return found;
   2259 	}
   2260       else if (c == CMD_LIST_AMBIGUOUS)
   2261 	{
   2262 	  /* We've gotten this far properly, but the next step is
   2263 	     ambiguous.  We need to set the result list to the best
   2264 	     we've found (if an inferior hasn't already set it).  */
   2265 	  if (result_list != nullptr)
   2266 	    if (!*result_list)
   2267 	      /* This used to say *result_list = *found->subcommands.
   2268 		 If that was correct, need to modify the documentation
   2269 		 at the top of this function to clarify what is
   2270 		 supposed to be going on.  */
   2271 	      *result_list = found;
   2272 	  /* For ambiguous commands, do not return any default_args args.  */
   2273 	  if (default_args != nullptr)
   2274 	    *default_args = std::string ();
   2275 	  return c;
   2276 	}
   2277       else
   2278 	{
   2279 	  /* We matched!  */
   2280 	  return c;
   2281 	}
   2282     }
   2283   else
   2284     {
   2285       if (result_list != nullptr)
   2286 	*result_list = clist;
   2287       if (!found_alias && default_args != nullptr)
   2288 	*default_args = found->default_args;
   2289       return found;
   2290     }
   2291 }
   2292 
   2293 /* All this hair to move the space to the front of cmdtype */
   2294 
   2295 static void
   2296 undef_cmd_error (const char *cmdtype, const char *q)
   2297 {
   2298   error (_("Undefined %scommand: \"%s\".  Try \"help%s%.*s\"."),
   2299 	 cmdtype,
   2300 	 q,
   2301 	 *cmdtype ? " " : "",
   2302 	 (int) strlen (cmdtype) - 1,
   2303 	 cmdtype);
   2304 }
   2305 
   2306 /* Look up the contents of *LINE as a command in the command list LIST.
   2307    LIST is a chain of struct cmd_list_element's.
   2308    If it is found, return the struct cmd_list_element for that command,
   2309    update *LINE to point after the command name, at the first argument
   2310    and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
   2311    args to prepend to the user provided args when running the command.
   2312    Note that if the found cmd_list_element is found via an alias,
   2313    the default args of the alias are returned.
   2314 
   2315    If not found, call error if ALLOW_UNKNOWN is zero
   2316    otherwise (or if error returns) return zero.
   2317    Call error if specified command is ambiguous,
   2318    unless ALLOW_UNKNOWN is negative.
   2319    CMDTYPE precedes the word "command" in the error message.
   2320 
   2321    If IGNORE_HELP_CLASSES is nonzero, ignore any command list
   2322    elements which are actually help classes rather than commands (i.e.
   2323    the function field of the struct cmd_list_element is 0).  */
   2324 
   2325 struct cmd_list_element *
   2326 lookup_cmd (const char **line, struct cmd_list_element *list,
   2327 	    const char *cmdtype,
   2328 	    std::string *default_args,
   2329 	    int allow_unknown, int ignore_help_classes)
   2330 {
   2331   struct cmd_list_element *last_list = 0;
   2332   struct cmd_list_element *c;
   2333 
   2334   /* Note: Do not remove trailing whitespace here because this
   2335      would be wrong for complete_command.  Jim Kingdon  */
   2336 
   2337   if (!*line)
   2338     error (_("Lack of needed %scommand"), cmdtype);
   2339 
   2340   c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes);
   2341 
   2342   if (!c)
   2343     {
   2344       if (!allow_unknown)
   2345 	{
   2346 	  char *q;
   2347 	  int len = find_command_name_length (*line);
   2348 
   2349 	  q = (char *) alloca (len + 1);
   2350 	  strncpy (q, *line, len);
   2351 	  q[len] = '\0';
   2352 	  undef_cmd_error (cmdtype, q);
   2353 	}
   2354       else
   2355 	return 0;
   2356     }
   2357   else if (c == CMD_LIST_AMBIGUOUS)
   2358     {
   2359       /* Ambigous.  Local values should be off subcommands or called
   2360 	 values.  */
   2361       int local_allow_unknown = (last_list ? last_list->allow_unknown :
   2362 				 allow_unknown);
   2363       std::string local_cmdtype
   2364 	= last_list ? last_list->prefixname () : cmdtype;
   2365       struct cmd_list_element *local_list =
   2366 	(last_list ? *(last_list->subcommands) : list);
   2367 
   2368       if (local_allow_unknown < 0)
   2369 	{
   2370 	  if (last_list)
   2371 	    return last_list;	/* Found something.  */
   2372 	  else
   2373 	    return 0;		/* Found nothing.  */
   2374 	}
   2375       else
   2376 	{
   2377 	  /* Report as error.  */
   2378 	  int amb_len;
   2379 	  char ambbuf[100];
   2380 
   2381 	  for (amb_len = 0;
   2382 	       ((*line)[amb_len] && (*line)[amb_len] != ' '
   2383 		&& (*line)[amb_len] != '\t');
   2384 	       amb_len++)
   2385 	    ;
   2386 
   2387 	  ambbuf[0] = 0;
   2388 	  for (c = local_list; c; c = c->next)
   2389 	    if (!strncmp (*line, c->name, amb_len))
   2390 	      {
   2391 		if (strlen (ambbuf) + strlen (c->name) + 6
   2392 		    < (int) sizeof ambbuf)
   2393 		  {
   2394 		    if (strlen (ambbuf))
   2395 		      strcat (ambbuf, ", ");
   2396 		    strcat (ambbuf, c->name);
   2397 		  }
   2398 		else
   2399 		  {
   2400 		    strcat (ambbuf, "..");
   2401 		    break;
   2402 		  }
   2403 	      }
   2404 	  error (_("Ambiguous %scommand \"%s\": %s."),
   2405 		 local_cmdtype.c_str (), *line, ambbuf);
   2406 	}
   2407     }
   2408   else
   2409     {
   2410       if (c->type == set_cmd && **line != '\0' && !isspace (**line))
   2411 	error (_("Argument must be preceded by space."));
   2412 
   2413       /* We've got something.  It may still not be what the caller
   2414 	 wants (if this command *needs* a subcommand).  */
   2415       while (**line == ' ' || **line == '\t')
   2416 	(*line)++;
   2417 
   2418       if (c->is_prefix () && **line && !c->allow_unknown)
   2419 	undef_cmd_error (c->prefixname ().c_str (), *line);
   2420 
   2421       /* Seems to be what he wants.  Return it.  */
   2422       return c;
   2423     }
   2424   return 0;
   2425 }
   2426 
   2427 /* See command.h.  */
   2428 
   2429 struct cmd_list_element *
   2430 lookup_cmd_exact (const char *name,
   2431 		  struct cmd_list_element *list,
   2432 		  bool ignore_help_classes)
   2433 {
   2434   const char *tem = name;
   2435   struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
   2436 					     ignore_help_classes);
   2437   if (cmd != nullptr && strcmp (name, cmd->name) != 0)
   2438     cmd = nullptr;
   2439   return cmd;
   2440 }
   2441 
   2442 /* We are here presumably because an alias or command in TEXT is
   2443    deprecated and a warning message should be generated.  This
   2444    function decodes TEXT and potentially generates a warning message
   2445    as outlined below.
   2446 
   2447    Example for 'set endian big' which has a fictitious alias 'seb'.
   2448 
   2449    If alias wasn't used in TEXT, and the command is deprecated:
   2450    "warning: 'set endian big' is deprecated."
   2451 
   2452    If alias was used, and only the alias is deprecated:
   2453    "warning: 'seb' an alias for the command 'set endian big' is deprecated."
   2454 
   2455    If alias was used and command is deprecated (regardless of whether
   2456    the alias itself is deprecated:
   2457 
   2458    "warning: 'set endian big' (seb) is deprecated."
   2459 
   2460    After the message has been sent, clear the appropriate flags in the
   2461    command and/or the alias so the user is no longer bothered.
   2462 
   2463 */
   2464 void
   2465 deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
   2466 {
   2467   struct cmd_list_element *alias = nullptr;
   2468   struct cmd_list_element *cmd = nullptr;
   2469 
   2470   /* Return if text doesn't evaluate to a command.  We place this lookup
   2471      within its own scope so that the PREFIX_CMD local is not visible
   2472      later in this function.  The value returned in PREFIX_CMD is based on
   2473      the prefix found in TEXT, and is our case this prefix can be missing
   2474      in some situations (when LIST is not the global CMDLIST).
   2475 
   2476      It is better for our purposes to use the prefix commands directly from
   2477      the ALIAS and CMD results.  */
   2478   {
   2479     struct cmd_list_element *prefix_cmd = nullptr;
   2480     if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list))
   2481       return;
   2482   }
   2483 
   2484   /* Return if nothing is deprecated.  */
   2485   if (!((alias != nullptr ? alias->deprecated_warn_user : 0)
   2486 	|| cmd->deprecated_warn_user))
   2487     return;
   2488 
   2489   /* Join command prefix (if any) and the command name.  */
   2490   std::string tmp_cmd_str;
   2491   if (cmd->prefix != nullptr)
   2492     tmp_cmd_str += cmd->prefix->prefixname ();
   2493   tmp_cmd_str += std::string (cmd->name);
   2494 
   2495   /* Display the appropriate first line, this warns that the thing the user
   2496      entered is deprecated.  */
   2497   if (alias != nullptr)
   2498     {
   2499       /* Join the alias prefix (if any) and the alias name.  */
   2500       std::string tmp_alias_str;
   2501       if (alias->prefix != nullptr)
   2502 	tmp_alias_str += alias->prefix->prefixname ();
   2503       tmp_alias_str += std::string (alias->name);
   2504 
   2505       if (cmd->cmd_deprecated)
   2506 	gdb_printf (_("Warning: command '%ps' (%ps) is deprecated.\n"),
   2507 		    styled_string (title_style.style (),
   2508 				   tmp_cmd_str.c_str ()),
   2509 		    styled_string (title_style.style (),
   2510 				   tmp_alias_str.c_str ()));
   2511       else
   2512 	gdb_printf (_("Warning: '%ps', an alias for the command '%ps', "
   2513 		      "is deprecated.\n"),
   2514 		    styled_string (title_style.style (),
   2515 				   tmp_alias_str.c_str ()),
   2516 		    styled_string (title_style.style (),
   2517 				   tmp_cmd_str.c_str ()));
   2518     }
   2519   else
   2520     gdb_printf (_("Warning: command '%ps' is deprecated.\n"),
   2521 		styled_string (title_style.style (),
   2522 			       tmp_cmd_str.c_str ()));
   2523 
   2524   /* Now display a second line indicating what the user should use instead.
   2525      If it is only the alias that is deprecated, we want to indicate the
   2526      new alias, otherwise we'll indicate the new command.  */
   2527   const char *replacement;
   2528   if (alias != nullptr && !cmd->cmd_deprecated)
   2529     replacement = alias->replacement;
   2530   else
   2531     replacement = cmd->replacement;
   2532   if (replacement != nullptr)
   2533     gdb_printf (_("Use '%ps'.\n\n"),
   2534 		styled_string (title_style.style (),
   2535 			       replacement));
   2536   else
   2537     gdb_printf (_("No alternative known.\n\n"));
   2538 
   2539   /* We've warned you, now we'll keep quiet.  */
   2540   if (alias != nullptr)
   2541     alias->deprecated_warn_user = 0;
   2542   cmd->deprecated_warn_user = 0;
   2543 }
   2544 
   2545 /* Look up the contents of TEXT as a command in the command list CUR_LIST.
   2546    Return 1 on success, 0 on failure.
   2547 
   2548    If TEXT refers to an alias, *ALIAS will point to that alias.
   2549 
   2550    If TEXT is a subcommand (i.e. one that is preceded by a prefix
   2551    command) set *PREFIX_CMD.
   2552 
   2553    Set *CMD to point to the command TEXT indicates, or to
   2554    CMD_LIST_AMBIGUOUS if there are multiple possible matches.
   2555 
   2556    If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
   2557    exist, they are NULL when we return.
   2558 
   2559 */
   2560 
   2561 static int
   2562 lookup_cmd_composition_1 (const char *text,
   2563 			  struct cmd_list_element **alias,
   2564 			  struct cmd_list_element **prefix_cmd,
   2565 			  struct cmd_list_element **cmd,
   2566 			  struct cmd_list_element *cur_list)
   2567 {
   2568   *alias = nullptr;
   2569   *prefix_cmd = cur_list->prefix;
   2570   *cmd = nullptr;
   2571 
   2572   text = skip_spaces (text);
   2573 
   2574   /* Go through as many command lists as we need to, to find the command
   2575      TEXT refers to.  */
   2576   while (1)
   2577     {
   2578       /* Identify the name of the command.  */
   2579       int len = find_command_name_length (text);
   2580 
   2581       /* If nothing but whitespace, return.  */
   2582       if (len == 0)
   2583 	return 0;
   2584 
   2585       /* TEXT is the start of the first command word to lookup (and
   2586 	 it's length is LEN).  We copy this into a local temporary.  */
   2587       std::string command (text, len);
   2588 
   2589       /* Look it up.  */
   2590       int nfound = 0;
   2591       *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
   2592 
   2593       /* We only handle the case where a single command was found.  */
   2594       if (nfound > 1)
   2595 	{
   2596 	  *cmd = CMD_LIST_AMBIGUOUS;
   2597 	  return 0;
   2598 	}
   2599       else if (*cmd == nullptr)
   2600 	return 0;
   2601       else
   2602 	{
   2603 	  if ((*cmd)->is_alias ())
   2604 	    {
   2605 	      /* If the command was actually an alias, we note that an
   2606 		 alias was used (by assigning *ALIAS) and we set *CMD.  */
   2607 	      *alias = *cmd;
   2608 	      *cmd = (*cmd)->alias_target;
   2609 	    }
   2610 	}
   2611 
   2612       text += len;
   2613       text = skip_spaces (text);
   2614 
   2615       if ((*cmd)->is_prefix () && *text != '\0')
   2616 	{
   2617 	  cur_list = *(*cmd)->subcommands;
   2618 	  *prefix_cmd = *cmd;
   2619 	}
   2620       else
   2621 	return 1;
   2622     }
   2623 }
   2624 
   2625 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
   2626    Return 1 on success, 0 on failure.
   2627 
   2628    If TEXT refers to an alias, *ALIAS will point to that alias.
   2629 
   2630    If TEXT is a subcommand (i.e. one that is preceded by a prefix
   2631    command) set *PREFIX_CMD.
   2632 
   2633    Set *CMD to point to the command TEXT indicates, or to
   2634    CMD_LIST_AMBIGUOUS if there are multiple possible matches.
   2635 
   2636    If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
   2637    exist, they are NULL when we return.
   2638 
   2639 */
   2640 
   2641 int
   2642 lookup_cmd_composition (const char *text,
   2643 			struct cmd_list_element **alias,
   2644 			struct cmd_list_element **prefix_cmd,
   2645 			struct cmd_list_element **cmd)
   2646 {
   2647   return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist);
   2648 }
   2649 
   2650 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
   2651 
   2652 /* Return a vector of char pointers which point to the different
   2653    possible completions in LIST of TEXT.
   2654 
   2655    WORD points in the same buffer as TEXT, and completions should be
   2656    returned relative to this position.  For example, suppose TEXT is
   2657    "foo" and we want to complete to "foobar".  If WORD is "oo", return
   2658    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
   2659 
   2660 void
   2661 complete_on_cmdlist (struct cmd_list_element *list,
   2662 		     completion_tracker &tracker,
   2663 		     const char *text, const char *word,
   2664 		     int ignore_help_classes)
   2665 {
   2666   struct cmd_list_element *ptr;
   2667   int textlen = strlen (text);
   2668   int pass;
   2669   int saw_deprecated_match = 0;
   2670 
   2671   /* We do one or two passes.  In the first pass, we skip deprecated
   2672      commands.  If we see no matching commands in the first pass, and
   2673      if we did happen to see a matching deprecated command, we do
   2674      another loop to collect those.  */
   2675   for (pass = 0; pass < 2; ++pass)
   2676     {
   2677       bool got_matches = false;
   2678 
   2679       for (ptr = list; ptr; ptr = ptr->next)
   2680 	if (!strncmp (ptr->name, text, textlen)
   2681 	    && !ptr->abbrev_flag
   2682 	    && (!ignore_help_classes || !ptr->is_command_class_help ()
   2683 		|| ptr->is_prefix ()))
   2684 	  {
   2685 	    if (pass == 0)
   2686 	      {
   2687 		if (ptr->cmd_deprecated)
   2688 		  {
   2689 		    saw_deprecated_match = 1;
   2690 		    continue;
   2691 		  }
   2692 	      }
   2693 
   2694 	    tracker.add_completion
   2695 	      (make_completion_match_str (ptr->name, text, word));
   2696 	    got_matches = true;
   2697 	  }
   2698 
   2699       if (got_matches)
   2700 	break;
   2701 
   2702       /* If we saw no matching deprecated commands in the first pass,
   2703 	 just bail out.  */
   2704       if (!saw_deprecated_match)
   2705 	break;
   2706     }
   2707 }
   2708 
   2709 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
   2710 
   2711 /* Add the different possible completions in ENUMLIST of TEXT.
   2712 
   2713    WORD points in the same buffer as TEXT, and completions should be
   2714    returned relative to this position.  For example, suppose TEXT is "foo"
   2715    and we want to complete to "foobar".  If WORD is "oo", return
   2716    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
   2717 
   2718 void
   2719 complete_on_enum (completion_tracker &tracker,
   2720 		  const char *const *enumlist,
   2721 		  const char *text, const char *word)
   2722 {
   2723   int textlen = strlen (text);
   2724   int i;
   2725   const char *name;
   2726 
   2727   for (i = 0; (name = enumlist[i]) != NULL; i++)
   2728     if (strncmp (name, text, textlen) == 0)
   2729       tracker.add_completion (make_completion_match_str (name, text, word));
   2730 }
   2731 
   2732 /* Call the command function.  */
   2733 void
   2734 cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty)
   2735 {
   2736   if (!cmd->is_command_class_help ())
   2737     {
   2738       std::optional<scoped_restore_tmpl<bool>> restore_suppress;
   2739 
   2740       if (cmd->suppress_notification != NULL)
   2741 	restore_suppress.emplace (cmd->suppress_notification, true);
   2742 
   2743       cmd->func (args, from_tty, cmd);
   2744     }
   2745   else
   2746     error (_("Invalid command"));
   2747 }
   2748 
   2749 int
   2750 cli_user_command_p (struct cmd_list_element *cmd)
   2751 {
   2752   return cmd->theclass == class_user && cmd->func == do_simple_func;
   2753 }
   2754