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