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