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