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