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