gdb/cli/cli-decode.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Handle lists of commands, their decoding and documentation, for GDB.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 3 of the License, or
  6.    (at your option) any later version.

  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.

  11.    You should have received a copy of the GNU General Public License
  12.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  13. #include "defs.h"
  14. #include "symtab.h"
  15. #include <ctype.h>
  16. #include "gdb_regex.h"
  17. #include "completer.h"
  18. #include "ui-out.h"
  19. #include "cli/cli-cmds.h"
  20. #include "cli/cli-decode.h"

  21. /* Prototypes for local functions.  */

  22. static void undef_cmd_error (const char *, const char *);

  23. static struct cmd_list_element *delete_cmd (const char *name,
  24.                                             struct cmd_list_element **list,
  25.                                             struct cmd_list_element **prehook,
  26.                                             struct cmd_list_element **prehookee,
  27.                                             struct cmd_list_element **posthook,
  28.                                             struct cmd_list_element **posthookee);

  29. static struct cmd_list_element *find_cmd (const char *command,
  30.                                           int len,
  31.                                           struct cmd_list_element *clist,
  32.                                           int ignore_help_classes,
  33.                                           int *nfound);

  34. static void help_all (struct ui_file *stream);

  35. /* Look up a command whose 'prefixlist' is KEY.  Return the command if found,
  36.    otherwise return NULL.  */

  37. static struct cmd_list_element *
  38. lookup_cmd_for_prefixlist (struct cmd_list_element **key,
  39.                            struct cmd_list_element *list)
  40. {
  41.   struct cmd_list_element *p = NULL;

  42.   for (p = list; p != NULL; p = p->next)
  43.     {
  44.       struct cmd_list_element *q;

  45.       if (p->prefixlist == NULL)
  46.         continue;
  47.       else if (p->prefixlist == key)
  48.         return p;

  49.       q = lookup_cmd_for_prefixlist (key, *(p->prefixlist));
  50.       if (q != NULL)
  51.         return q;
  52.     }

  53.   return NULL;
  54. }

  55. static void
  56. set_cmd_prefix (struct cmd_list_element *c, struct cmd_list_element **list)
  57. {
  58.   struct cmd_list_element *p;

  59.   /* Check to see if *LIST contains any element other than C.  */
  60.   for (p = *list; p != NULL; p = p->next)
  61.     if (p != c)
  62.       break;

  63.   if (p == NULL)
  64.     {
  65.       /* *SET_LIST only contains SET.  */
  66.       p = lookup_cmd_for_prefixlist (list, setlist);

  67.       c->prefix = p ? (p->cmd_pointer ? p->cmd_pointer : p) : p;
  68.     }
  69.   else
  70.     c->prefix = p->prefix;
  71. }

  72. static void
  73. print_help_for_command (struct cmd_list_element *c, const char *prefix,
  74.                         int recurse, struct ui_file *stream);


  75. /* Set the callback function for the specified command.  For each both
  76.    the commands callback and func() are set.  The latter set to a
  77.    bounce function (unless cfunc / sfunc is NULL that is).  */

  78. static void
  79. do_cfunc (struct cmd_list_element *c, char *args, int from_tty)
  80. {
  81.   c->function.cfunc (args, from_tty); /* Ok.  */
  82. }

  83. void
  84. set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
  85. {
  86.   if (cfunc == NULL)
  87.     cmd->func = NULL;
  88.   else
  89.     cmd->func = do_cfunc;
  90.   cmd->function.cfunc = cfunc; /* Ok.  */
  91. }

  92. static void
  93. do_sfunc (struct cmd_list_element *c, char *args, int from_tty)
  94. {
  95.   c->function.sfunc (args, from_tty, c); /* Ok.  */
  96. }

  97. void
  98. set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc)
  99. {
  100.   if (sfunc == NULL)
  101.     cmd->func = NULL;
  102.   else
  103.     cmd->func = do_sfunc;
  104.   cmd->function.sfunc = sfunc; /* Ok.  */
  105. }

  106. int
  107. cmd_cfunc_eq (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc)
  108. {
  109.   return cmd->func == do_cfunc && cmd->function.cfunc == cfunc;
  110. }

  111. void
  112. set_cmd_context (struct cmd_list_element *cmd, void *context)
  113. {
  114.   cmd->context = context;
  115. }

  116. void *
  117. get_cmd_context (struct cmd_list_element *cmd)
  118. {
  119.   return cmd->context;
  120. }

  121. enum cmd_types
  122. cmd_type (struct cmd_list_element *cmd)
  123. {
  124.   return cmd->type;
  125. }

  126. void
  127. set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
  128. {
  129.   cmd->completer = completer; /* Ok.  */
  130. }

  131. /* See definition in commands.h.  */

  132. void
  133. set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
  134.                                completer_ftype_void *completer_handle_brkchars)
  135. {
  136.   cmd->completer_handle_brkchars = completer_handle_brkchars;
  137. }

  138. /* Add element named NAME.
  139.    Space for NAME and DOC must be allocated by the caller.
  140.    CLASS is the top level category into which commands are broken down
  141.    for "help" purposes.
  142.    FUN should be the function to execute the command;
  143.    it will get a character string as argument, with leading
  144.    and trailing blanks already eliminated.

  145.    DOC is a documentation string for the command.
  146.    Its first line should be a complete sentence.
  147.    It should start with ? for a command that is an abbreviation
  148.    or with * for a command that most users don't need to know about.

  149.    Add this command to command list *LIST.

  150.    Returns a pointer to the added command (not necessarily the head
  151.    of *LIST).  */

  152. struct cmd_list_element *
  153. add_cmd (const char *name, enum command_class class, cmd_cfunc_ftype *fun,
  154.          const char *doc, struct cmd_list_element **list)
  155. {
  156.   struct cmd_list_element *c
  157.     = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
  158.   struct cmd_list_element *p, *iter;

  159.   /* Turn each alias of the old command into an alias of the new
  160.      command.  */
  161.   c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
  162.                            &c->hook_post, &c->hookee_post);
  163.   for (iter = c->aliases; iter; iter = iter->alias_chain)
  164.     iter->cmd_pointer = c;
  165.   if (c->hook_pre)
  166.     c->hook_pre->hookee_pre = c;
  167.   if (c->hookee_pre)
  168.     c->hookee_pre->hook_pre = c;
  169.   if (c->hook_post)
  170.     c->hook_post->hookee_post = c;
  171.   if (c->hookee_post)
  172.     c->hookee_post->hook_post = c;

  173.   if (*list == NULL || strcmp ((*list)->name, name) >= 0)
  174.     {
  175.       c->next = *list;
  176.       *list = c;
  177.     }
  178.   else
  179.     {
  180.       p = *list;
  181.       while (p->next && strcmp (p->next->name, name) <= 0)
  182.         {
  183.           p = p->next;
  184.         }
  185.       c->next = p->next;
  186.       p->next = c;
  187.     }

  188.   c->name = name;
  189.   c->class = class;
  190.   set_cmd_cfunc (c, fun);
  191.   set_cmd_context (c, NULL);
  192.   c->doc = doc;
  193.   c->cmd_deprecated = 0;
  194.   c->deprecated_warn_user = 0;
  195.   c->malloced_replacement = 0;
  196.   c->doc_allocated = 0;
  197.   c->replacement = NULL;
  198.   c->pre_show_hook = NULL;
  199.   c->hook_in = 0;
  200.   c->prefixlist = NULL;
  201.   c->prefixname = NULL;
  202.   c->allow_unknown = 0;
  203.   c->prefix = NULL;
  204.   c->abbrev_flag = 0;
  205.   set_cmd_completer (c, make_symbol_completion_list_fn);
  206.   c->completer_handle_brkchars = NULL;
  207.   c->destroyer = NULL;
  208.   c->type = not_set_cmd;
  209.   c->var = NULL;
  210.   c->var_type = var_boolean;
  211.   c->enums = NULL;
  212.   c->user_commands = NULL;
  213.   c->cmd_pointer = NULL;
  214.   c->alias_chain = NULL;

  215.   return c;
  216. }

  217. /* Deprecates a command CMD.
  218.    REPLACEMENT is the name of the command which should be used in
  219.    place of this command, or NULL if no such command exists.

  220.    This function does not check to see if command REPLACEMENT exists
  221.    since gdb may not have gotten around to adding REPLACEMENT when
  222.    this function is called.

  223.    Returns a pointer to the deprecated command.  */

  224. struct cmd_list_element *
  225. deprecate_cmd (struct cmd_list_element *cmd, const char *replacement)
  226. {
  227.   cmd->cmd_deprecated = 1;
  228.   cmd->deprecated_warn_user = 1;

  229.   if (replacement != NULL)
  230.     cmd->replacement = replacement;
  231.   else
  232.     cmd->replacement = NULL;

  233.   return cmd;
  234. }

  235. struct cmd_list_element *
  236. add_alias_cmd (const char *name, const char *oldname, enum command_class class,
  237.                int abbrev_flag, struct cmd_list_element **list)
  238. {
  239.   const char *tmp;
  240.   struct cmd_list_element *old;
  241.   struct cmd_list_element *c;

  242.   tmp = oldname;
  243.   old = lookup_cmd (&tmp, *list, "", 1, 1);

  244.   if (old == 0)
  245.     {
  246.       struct cmd_list_element *prehook, *prehookee, *posthook, *posthookee;
  247.       struct cmd_list_element *aliases = delete_cmd (name, list,
  248.                                                      &prehook, &prehookee,
  249.                                                      &posthook, &posthookee);

  250.       /* If this happens, it means a programmer error somewhere.  */
  251.       gdb_assert (!aliases && !prehook && !prehookee
  252.                   && !posthook && ! posthookee);
  253.       return 0;
  254.     }

  255.   c = add_cmd (name, class, NULL, old->doc, list);

  256.   /* If OLD->DOC can be freed, we should make another copy.  */
  257.   if (old->doc_allocated)
  258.     {
  259.       c->doc = xstrdup (old->doc);
  260.       c->doc_allocated = 1;
  261.     }
  262.   /* NOTE: Both FUNC and all the FUNCTIONs need to be copied.  */
  263.   c->func = old->func;
  264.   c->function = old->function;
  265.   c->prefixlist = old->prefixlist;
  266.   c->prefixname = old->prefixname;
  267.   c->allow_unknown = old->allow_unknown;
  268.   c->abbrev_flag = abbrev_flag;
  269.   c->cmd_pointer = old;
  270.   c->alias_chain = old->aliases;
  271.   old->aliases = c;

  272.   set_cmd_prefix (c, list);
  273.   return c;
  274. }

  275. /* Like add_cmd but adds an element for a command prefix: a name that
  276.    should be followed by a subcommand to be looked up in another
  277.    command list.  PREFIXLIST should be the address of the variable
  278.    containing that list.  */

  279. struct cmd_list_element *
  280. add_prefix_cmd (const char *name, enum command_class class,
  281.                 cmd_cfunc_ftype *fun,
  282.                 const char *doc, struct cmd_list_element **prefixlist,
  283.                 const char *prefixname, int allow_unknown,
  284.                 struct cmd_list_element **list)
  285. {
  286.   struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
  287.   struct cmd_list_element *p;

  288.   c->prefixlist = prefixlist;
  289.   c->prefixname = prefixname;
  290.   c->allow_unknown = allow_unknown;

  291.   if (list == &cmdlist)
  292.     c->prefix = NULL;
  293.   else
  294.     set_cmd_prefix (c, list);

  295.   /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST.  */
  296.   for (p = *prefixlist; p != NULL; p = p->next)
  297.     p->prefix = c;

  298.   return c;
  299. }

  300. /* Like add_prefix_cmd but sets the abbrev_flag on the new command.  */

  301. struct cmd_list_element *
  302. add_abbrev_prefix_cmd (const char *name, enum command_class class,
  303.                        cmd_cfunc_ftype *fun, const char *doc,
  304.                        struct cmd_list_element **prefixlist,
  305.                        const char *prefixname,
  306.                        int allow_unknown, struct cmd_list_element **list)
  307. {
  308.   struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);

  309.   c->prefixlist = prefixlist;
  310.   c->prefixname = prefixname;
  311.   c->allow_unknown = allow_unknown;
  312.   c->abbrev_flag = 1;
  313.   return c;
  314. }

  315. /* This is an empty "cfunc".  */
  316. void
  317. not_just_help_class_command (char *args, int from_tty)
  318. {
  319. }

  320. /* This is an empty "sfunc".  */
  321. static void empty_sfunc (char *, int, struct cmd_list_element *);

  322. static void
  323. empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
  324. {
  325. }

  326. /* Add element named NAME to command list LIST (the list for set/show
  327.    or some sublist thereof).
  328.    TYPE is set_cmd or show_cmd.
  329.    CLASS is as in add_cmd.
  330.    VAR_TYPE is the kind of thing we are setting.
  331.    VAR is address of the variable being controlled by this command.
  332.    DOC is the documentation string.  */

  333. static struct cmd_list_element *
  334. add_set_or_show_cmd (const char *name,
  335.                      enum cmd_types type,
  336.                      enum command_class class,
  337.                      var_types var_type,
  338.                      void *var,
  339.                      const char *doc,
  340.                      struct cmd_list_element **list)
  341. {
  342.   struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list);

  343.   gdb_assert (type == set_cmd || type == show_cmd);
  344.   c->type = type;
  345.   c->var_type = var_type;
  346.   c->var = var;
  347.   /* This needs to be something besides NULL so that this isn't
  348.      treated as a help class.  */
  349.   set_cmd_sfunc (c, empty_sfunc);
  350.   return c;
  351. }

  352. /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
  353.    CLASS is as in add_cmd.  VAR_TYPE is the kind of thing we are
  354.    setting.  VAR is address of the variable being controlled by this
  355.    command.  SET_FUNC and SHOW_FUNC are the callback functions (if
  356.    non-NULL).  SET_DOC, SHOW_DOC and HELP_DOC are the documentation
  357.    strings.  PRINT the format string to print the value.  SET_RESULT
  358.    and SHOW_RESULT, if not NULL, are set to the resulting command
  359.    structures.  */

  360. static void
  361. add_setshow_cmd_full (const char *name,
  362.                       enum command_class class,
  363.                       var_types var_type, void *var,
  364.                       const char *set_doc, const char *show_doc,
  365.                       const char *help_doc,
  366.                       cmd_sfunc_ftype *set_func,
  367.                       show_value_ftype *show_func,
  368.                       struct cmd_list_element **set_list,
  369.                       struct cmd_list_element **show_list,
  370.                       struct cmd_list_element **set_result,
  371.                       struct cmd_list_element **show_result)
  372. {
  373.   struct cmd_list_element *set;
  374.   struct cmd_list_element *show;
  375.   char *full_set_doc;
  376.   char *full_show_doc;

  377.   if (help_doc != NULL)
  378.     {
  379.       full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
  380.       full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
  381.     }
  382.   else
  383.     {
  384.       full_set_doc = xstrdup (set_doc);
  385.       full_show_doc = xstrdup (show_doc);
  386.     }
  387.   set = add_set_or_show_cmd (name, set_cmd, class, var_type, var,
  388.                              full_set_doc, set_list);
  389.   set->doc_allocated = 1;

  390.   if (set_func != NULL)
  391.     set_cmd_sfunc (set, set_func);

  392.   set_cmd_prefix (set, set_list);

  393.   show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
  394.                               full_show_doc, show_list);
  395.   show->doc_allocated = 1;
  396.   show->show_value_func = show_func;

  397.   if (set_result != NULL)
  398.     *set_result = set;
  399.   if (show_result != NULL)
  400.     *show_result = show;
  401. }

  402. /* Add element named NAME to command list LIST (the list for set or
  403.    some sublist thereof).  CLASS is as in add_cmd.  ENUMLIST is a list
  404.    of strings which may follow NAMEVAR is address of the variable
  405.    which will contain the matching string (from ENUMLIST).  */

  406. void
  407. add_setshow_enum_cmd (const char *name,
  408.                       enum command_class class,
  409.                       const char *const *enumlist,
  410.                       const char **var,
  411.                       const char *set_doc,
  412.                       const char *show_doc,
  413.                       const char *help_doc,
  414.                       cmd_sfunc_ftype *set_func,
  415.                       show_value_ftype *show_func,
  416.                       struct cmd_list_element **set_list,
  417.                       struct cmd_list_element **show_list)
  418. {
  419.   struct cmd_list_element *c;

  420.   add_setshow_cmd_full (name, class, var_enum, var,
  421.                         set_doc, show_doc, help_doc,
  422.                         set_func, show_func,
  423.                         set_list, show_list,
  424.                         &c, NULL);
  425.   c->enums = enumlist;
  426. }

  427. const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };

  428. /* Add an auto-boolean command named NAME to both the set and show
  429.    command list lists.  CLASS is as in add_cmdVAR is address of the
  430.    variable which will contain the value.  DOC is the documentation
  431.    stringFUNC is the corresponding callback.  */
  432. void
  433. add_setshow_auto_boolean_cmd (const char *name,
  434.                               enum command_class class,
  435.                               enum auto_boolean *var,
  436.                               const char *set_doc, const char *show_doc,
  437.                               const char *help_doc,
  438.                               cmd_sfunc_ftype *set_func,
  439.                               show_value_ftype *show_func,
  440.                               struct cmd_list_element **set_list,
  441.                               struct cmd_list_element **show_list)
  442. {
  443.   struct cmd_list_element *c;

  444.   add_setshow_cmd_full (name, class, var_auto_boolean, var,
  445.                         set_doc, show_doc, help_doc,
  446.                         set_func, show_func,
  447.                         set_list, show_list,
  448.                         &c, NULL);
  449.   c->enums = auto_boolean_enums;
  450. }

  451. /* Add element named NAME to both the set and show command LISTs (the
  452.    list for set/show or some sublist thereof).  CLASS is as in
  453.    add_cmdVAR is address of the variable which will contain the
  454.    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
  455. void
  456. add_setshow_boolean_cmd (const char *name, enum command_class class, int *var,
  457.                          const char *set_doc, const char *show_doc,
  458.                          const char *help_doc,
  459.                          cmd_sfunc_ftype *set_func,
  460.                          show_value_ftype *show_func,
  461.                          struct cmd_list_element **set_list,
  462.                          struct cmd_list_element **show_list)
  463. {
  464.   static const char *boolean_enums[] = { "on", "off", NULL };
  465.   struct cmd_list_element *c;

  466.   add_setshow_cmd_full (name, class, var_boolean, var,
  467.                         set_doc, show_doc, help_doc,
  468.                         set_func, show_func,
  469.                         set_list, show_list,
  470.                         &c, NULL);
  471.   c->enums = boolean_enums;
  472. }

  473. /* Add element named NAME to both the set and show command LISTs (the
  474.    list for set/show or some sublist thereof).  */
  475. void
  476. add_setshow_filename_cmd (const char *name, enum command_class class,
  477.                           char **var,
  478.                           const char *set_doc, const char *show_doc,
  479.                           const char *help_doc,
  480.                           cmd_sfunc_ftype *set_func,
  481.                           show_value_ftype *show_func,
  482.                           struct cmd_list_element **set_list,
  483.                           struct cmd_list_element **show_list)
  484. {
  485.   struct cmd_list_element *set_result;

  486.   add_setshow_cmd_full (name, class, var_filename, var,
  487.                         set_doc, show_doc, help_doc,
  488.                         set_func, show_func,
  489.                         set_list, show_list,
  490.                         &set_result, NULL);
  491.   set_cmd_completer (set_result, filename_completer);
  492. }

  493. /* Add element named NAME to both the set and show command LISTs (the
  494.    list for set/show or some sublist thereof).  */
  495. void
  496. add_setshow_string_cmd (const char *name, enum command_class class,
  497.                         char **var,
  498.                         const char *set_doc, const char *show_doc,
  499.                         const char *help_doc,
  500.                         cmd_sfunc_ftype *set_func,
  501.                         show_value_ftype *show_func,
  502.                         struct cmd_list_element **set_list,
  503.                         struct cmd_list_element **show_list)
  504. {
  505.   add_setshow_cmd_full (name, class, var_string, var,
  506.                         set_doc, show_doc, help_doc,
  507.                         set_func, show_func,
  508.                         set_list, show_list,
  509.                         NULL, NULL);
  510. }

  511. /* Add element named NAME to both the set and show command LISTs (the
  512.    list for set/show or some sublist thereof).  */
  513. struct cmd_list_element *
  514. add_setshow_string_noescape_cmd (const char *name, enum command_class class,
  515.                                  char **var,
  516.                                  const char *set_doc, const char *show_doc,
  517.                                  const char *help_doc,
  518.                                  cmd_sfunc_ftype *set_func,
  519.                                  show_value_ftype *show_func,
  520.                                  struct cmd_list_element **set_list,
  521.                                  struct cmd_list_element **show_list)
  522. {
  523.   struct cmd_list_element *set_cmd;

  524.   add_setshow_cmd_full (name, class, var_string_noescape, var,
  525.                         set_doc, show_doc, help_doc,
  526.                         set_func, show_func,
  527.                         set_list, show_list,
  528.                         &set_cmd, NULL);
  529.   return set_cmd;
  530. }

  531. /* Add element named NAME to both the set and show command LISTs (the
  532.    list for set/show or some sublist thereof).  */
  533. void
  534. add_setshow_optional_filename_cmd (const char *name, enum command_class class,
  535.                                    char **var,
  536.                                    const char *set_doc, const char *show_doc,
  537.                                    const char *help_doc,
  538.                                    cmd_sfunc_ftype *set_func,
  539.                                    show_value_ftype *show_func,
  540.                                    struct cmd_list_element **set_list,
  541.                                    struct cmd_list_element **show_list)
  542. {
  543.   struct cmd_list_element *set_result;

  544.   add_setshow_cmd_full (name, class, var_optional_filename, var,
  545.                         set_doc, show_doc, help_doc,
  546.                         set_func, show_func,
  547.                         set_list, show_list,
  548.                         &set_result, NULL);

  549.   set_cmd_completer (set_result, filename_completer);

  550. }

  551. /* Completes on literal "unlimited".  Used by integer commands that
  552.    support a special "unlimited" value.  */

  553. static VEC (char_ptr) *
  554. integer_unlimited_completer (struct cmd_list_element *ignore,
  555.                              const char *text, const char *word)
  556. {
  557.   static const char * const keywords[] =
  558.     {
  559.       "unlimited",
  560.       NULL,
  561.     };

  562.   return complete_on_enum (keywords, text, word);
  563. }

  564. /* Add element named NAME to both the set and show command LISTs (the
  565.    list for set/show or some sublist thereof).  CLASS is as in
  566.    add_cmdVAR is address of the variable which will contain the
  567.    value.  SET_DOC and SHOW_DOC are the documentation strings.  This
  568.    function is only used in Python API.  Please don't use it elsewhere.  */
  569. void
  570. add_setshow_integer_cmd (const char *name, enum command_class class,
  571.                          int *var,
  572.                          const char *set_doc, const char *show_doc,
  573.                          const char *help_doc,
  574.                          cmd_sfunc_ftype *set_func,
  575.                          show_value_ftype *show_func,
  576.                          struct cmd_list_element **set_list,
  577.                          struct cmd_list_element **show_list)
  578. {
  579.   struct cmd_list_element *set;

  580.   add_setshow_cmd_full (name, class, var_integer, var,
  581.                         set_doc, show_doc, help_doc,
  582.                         set_func, show_func,
  583.                         set_list, show_list,
  584.                         &set, NULL);

  585.   set_cmd_completer (set, integer_unlimited_completer);
  586. }

  587. /* Add element named NAME to both the set and show command LISTs (the
  588.    list for set/show or some sublist thereof).  CLASS is as in
  589.    add_cmdVAR is address of the variable which will contain the
  590.    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
  591. void
  592. add_setshow_uinteger_cmd (const char *name, enum command_class class,
  593.                           unsigned int *var,
  594.                           const char *set_doc, const char *show_doc,
  595.                           const char *help_doc,
  596.                           cmd_sfunc_ftype *set_func,
  597.                           show_value_ftype *show_func,
  598.                           struct cmd_list_element **set_list,
  599.                           struct cmd_list_element **show_list)
  600. {
  601.   struct cmd_list_element *set;

  602.   add_setshow_cmd_full (name, class, var_uinteger, var,
  603.                         set_doc, show_doc, help_doc,
  604.                         set_func, show_func,
  605.                         set_list, show_list,
  606.                         &set, NULL);

  607.   set_cmd_completer (set, integer_unlimited_completer);
  608. }

  609. /* Add element named NAME to both the set and show command LISTs (the
  610.    list for set/show or some sublist thereof).  CLASS is as in
  611.    add_cmdVAR is address of the variable which will contain the
  612.    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
  613. void
  614. add_setshow_zinteger_cmd (const char *name, enum command_class class,
  615.                           int *var,
  616.                           const char *set_doc, const char *show_doc,
  617.                           const char *help_doc,
  618.                           cmd_sfunc_ftype *set_func,
  619.                           show_value_ftype *show_func,
  620.                           struct cmd_list_element **set_list,
  621.                           struct cmd_list_element **show_list)
  622. {
  623.   add_setshow_cmd_full (name, class, var_zinteger, var,
  624.                         set_doc, show_doc, help_doc,
  625.                         set_func, show_func,
  626.                         set_list, show_list,
  627.                         NULL, NULL);
  628. }

  629. void
  630. add_setshow_zuinteger_unlimited_cmd (const char *name,
  631.                                      enum command_class class,
  632.                                      int *var,
  633.                                      const char *set_doc,
  634.                                      const char *show_doc,
  635.                                      const char *help_doc,
  636.                                      cmd_sfunc_ftype *set_func,
  637.                                      show_value_ftype *show_func,
  638.                                      struct cmd_list_element **set_list,
  639.                                      struct cmd_list_element **show_list)
  640. {
  641.   struct cmd_list_element *set;

  642.   add_setshow_cmd_full (name, class, var_zuinteger_unlimited, var,
  643.                         set_doc, show_doc, help_doc,
  644.                         set_func, show_func,
  645.                         set_list, show_list,
  646.                         &set, NULL);

  647.   set_cmd_completer (set, integer_unlimited_completer);
  648. }

  649. /* Add element named NAME to both the set and show command LISTs (the
  650.    list for set/show or some sublist thereof).  CLASS is as in
  651.    add_cmdVAR is address of the variable which will contain the
  652.    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
  653. void
  654. add_setshow_zuinteger_cmd (const char *name, enum command_class class,
  655.                            unsigned int *var,
  656.                            const char *set_doc, const char *show_doc,
  657.                            const char *help_doc,
  658.                            cmd_sfunc_ftype *set_func,
  659.                            show_value_ftype *show_func,
  660.                            struct cmd_list_element **set_list,
  661.                            struct cmd_list_element **show_list)
  662. {
  663.   add_setshow_cmd_full (name, class, var_zuinteger, var,
  664.                         set_doc, show_doc, help_doc,
  665.                         set_func, show_func,
  666.                         set_list, show_list,
  667.                         NULL, NULL);
  668. }

  669. /* Remove the command named NAME from the command list.  Return the
  670.    list commands which were aliased to the deleted command.  If the
  671.    command had no aliases, return NULL.  The various *HOOKs are set to
  672.    the pre- and post-hook commands for the deleted command.  If the
  673.    command does not have a hook, the corresponding out parameter is
  674.    set to NULL.  */

  675. static struct cmd_list_element *
  676. delete_cmd (const char *name, struct cmd_list_element **list,
  677.             struct cmd_list_element **prehook,
  678.             struct cmd_list_element **prehookee,
  679.             struct cmd_list_element **posthook,
  680.             struct cmd_list_element **posthookee)
  681. {
  682.   struct cmd_list_element *iter;
  683.   struct cmd_list_element **previous_chain_ptr;
  684.   struct cmd_list_element *aliases = NULL;

  685.   *prehook = NULL;
  686.   *prehookee = NULL;
  687.   *posthook = NULL;
  688.   *posthookee = NULL;
  689.   previous_chain_ptr = list;

  690.   for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
  691.     {
  692.       if (strcmp (iter->name, name) == 0)
  693.         {
  694.           if (iter->destroyer)
  695.             iter->destroyer (iter, iter->context);
  696.           if (iter->hookee_pre)
  697.             iter->hookee_pre->hook_pre = 0;
  698.           *prehook = iter->hook_pre;
  699.           *prehookee = iter->hookee_pre;
  700.           if (iter->hookee_post)
  701.             iter->hookee_post->hook_post = 0;
  702.           if (iter->doc && iter->doc_allocated)
  703.             xfree ((char *) iter->doc);
  704.           *posthook = iter->hook_post;
  705.           *posthookee = iter->hookee_post;

  706.           /* Update the link.  */
  707.           *previous_chain_ptr = iter->next;

  708.           aliases = iter->aliases;

  709.           /* If this command was an alias, remove it from the list of
  710.              aliases.  */
  711.           if (iter->cmd_pointer)
  712.             {
  713.               struct cmd_list_element **prevp = &iter->cmd_pointer->aliases;
  714.               struct cmd_list_element *a = *prevp;

  715.               while (a != iter)
  716.                 {
  717.                   prevp = &a->alias_chain;
  718.                   a = *prevp;
  719.                 }
  720.               *prevp = iter->alias_chain;
  721.             }

  722.           xfree (iter);

  723.           /* We won't see another command with the same name.  */
  724.           break;
  725.         }
  726.       else
  727.         previous_chain_ptr = &iter->next;
  728.     }

  729.   return aliases;
  730. }

  731. /* Shorthands to the commands above.  */

  732. /* Add an element to the list of info subcommands.  */

  733. struct cmd_list_element *
  734. add_info (const char *name, cmd_cfunc_ftype *fun, const char *doc)
  735. {
  736.   return add_cmd (name, no_class, fun, doc, &infolist);
  737. }

  738. /* Add an alias to the list of info subcommands.  */

  739. struct cmd_list_element *
  740. add_info_alias (const char *name, const char *oldname, int abbrev_flag)
  741. {
  742.   return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
  743. }

  744. /* Add an element to the list of commands.  */

  745. struct cmd_list_element *
  746. add_com (const char *name, enum command_class class, cmd_cfunc_ftype *fun,
  747.          const char *doc)
  748. {
  749.   return add_cmd (name, class, fun, doc, &cmdlist);
  750. }

  751. /* Add an alias or abbreviation command to the list of commands.  */

  752. struct cmd_list_element *
  753. add_com_alias (const char *name, const char *oldname, enum command_class class,
  754.                int abbrev_flag)
  755. {
  756.   return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
  757. }

  758. /* Recursively walk the commandlist structures, and print out the
  759.    documentation of commands that match our regex in either their
  760.    name, or their documentation.
  761. */
  762. void
  763. apropos_cmd (struct ui_file *stream,
  764.              struct cmd_list_element *commandlist,
  765.              struct re_pattern_buffer *regex, const char *prefix)
  766. {
  767.   struct cmd_list_element *c;
  768.   int returnvalue;

  769.   /* Walk through the commands.  */
  770.   for (c=commandlist;c;c=c->next)
  771.     {
  772.       returnvalue = -1; /* Needed to avoid double printing.  */
  773.       if (c->name != NULL)
  774.         {
  775.           /* Try to match against the name.  */
  776.           returnvalue = re_search (regex, c->name, strlen(c->name),
  777.                                    0, strlen (c->name), NULL);
  778.           if (returnvalue >= 0)
  779.             {
  780.               print_help_for_command (c, prefix,
  781.                                       0 /* don't recurse */, stream);
  782.             }
  783.         }
  784.       if (c->doc != NULL && returnvalue < 0)
  785.         {
  786.           /* Try to match against documentation.  */
  787.           if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
  788.             {
  789.               print_help_for_command (c, prefix,
  790.                                       0 /* don't recurse */, stream);
  791.             }
  792.         }
  793.       /* Check if this command has subcommands and is not an
  794.          abbreviation.  We skip listing subcommands of abbreviations
  795.          in order to avoid duplicates in the output.  */
  796.       if (c->prefixlist != NULL && !c->abbrev_flag)
  797.         {
  798.           /* Recursively call ourselves on the subcommand list,
  799.              passing the right prefix in.  */
  800.           apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
  801.         }
  802.     }
  803. }

  804. /* This command really has to deal with two things:
  805.    1) I want documentation on *this string* (usually called by
  806.       "help commandname").

  807.    2) I want documentation on *this list* (usually called by giving a
  808.       command that requires subcommands.  Also called by saying just
  809.       "help".)

  810.    I am going to split this into two seperate comamnds, help_cmd and
  811.    help_list.  */

  812. void
  813. help_cmd (const char *command, struct ui_file *stream)
  814. {
  815.   struct cmd_list_element *c;

  816.   if (!command)
  817.     {
  818.       help_list (cmdlist, "", all_classes, stream);
  819.       return;
  820.     }

  821.   if (strcmp (command, "all") == 0)
  822.     {
  823.       help_all (stream);
  824.       return;
  825.     }

  826.   c = lookup_cmd (&command, cmdlist, "", 0, 0);

  827.   if (c == 0)
  828.     return;

  829.   /* There are three cases here.
  830.      If c->prefixlist is nonzero, we have a prefix command.
  831.      Print its documentation, then list its subcommands.

  832.      If c->func is non NULL, we really have a command.  Print its
  833.      documentation and return.

  834.      If c->func is NULL, we have a class name.  Print its
  835.      documentation (as if it were a command) and then set class to the
  836.      number of this class so that the commands in the class will be
  837.      listed.  */

  838.   fputs_filtered (c->doc, stream);
  839.   fputs_filtered ("\n", stream);

  840.   if (c->prefixlist == 0 && c->func != NULL)
  841.     return;
  842.   fprintf_filtered (stream, "\n");

  843.   /* If this is a prefix command, print it's subcommands.  */
  844.   if (c->prefixlist)
  845.     help_list (*c->prefixlist, c->prefixname, all_commands, stream);

  846.   /* If this is a class name, print all of the commands in the class.  */
  847.   if (c->func == NULL)
  848.     help_list (cmdlist, "", c->class, stream);

  849.   if (c->hook_pre || c->hook_post)
  850.     fprintf_filtered (stream,
  851.                       "\nThis command has a hook (or hooks) defined:\n");

  852.   if (c->hook_pre)
  853.     fprintf_filtered (stream,
  854.                       "\tThis command is run after  : %s (pre hook)\n",
  855.                     c->hook_pre->name);
  856.   if (c->hook_post)
  857.     fprintf_filtered (stream,
  858.                       "\tThis command is run before : %s (post hook)\n",
  859.                     c->hook_post->name);
  860. }

  861. /*
  862. * Get a specific kind of help on a command list.
  863. *
  864. * LIST is the list.
  865. * CMDTYPE is the prefix to use in the title string.
  866. * CLASS is the class with which to list the nodes of this list (see
  867. * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
  868. * everything, ALL_CLASSES for just classes, and non-negative for only things
  869. * in a specific class.
  870. * and STREAM is the output stream on which to print things.
  871. * If you call this routine with a class >= 0, it recurses.
  872. */
  873. void
  874. help_list (struct cmd_list_element *list, const char *cmdtype,
  875.            enum command_class class, struct ui_file *stream)
  876. {
  877.   int len;
  878.   char *cmdtype1, *cmdtype2;

  879.   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
  880.    */
  881.   len = strlen (cmdtype);
  882.   cmdtype1 = (char *) alloca (len + 1);
  883.   cmdtype1[0] = 0;
  884.   cmdtype2 = (char *) alloca (len + 4);
  885.   cmdtype2[0] = 0;
  886.   if (len)
  887.     {
  888.       cmdtype1[0] = ' ';
  889.       strncpy (cmdtype1 + 1, cmdtype, len - 1);
  890.       cmdtype1[len] = 0;
  891.       strncpy (cmdtype2, cmdtype, len - 1);
  892.       strcpy (cmdtype2 + len - 1, " sub");
  893.     }

  894.   if (class == all_classes)
  895.     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
  896.   else
  897.     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);

  898.   help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);

  899.   if (class == all_classes)
  900.     {
  901.       fprintf_filtered (stream, "\n\
  902. Type \"help%s\" followed by a class name for a list of commands in ",
  903.                         cmdtype1);
  904.       wrap_here ("");
  905.       fprintf_filtered (stream, "that class.");

  906.       fprintf_filtered (stream, "\n\
  907. Type \"help all\" for the list of all commands.");
  908.     }

  909.   fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
  910.                     cmdtype1, cmdtype2);
  911.   wrap_here ("");
  912.   fputs_filtered ("for ", stream);
  913.   wrap_here ("");
  914.   fputs_filtered ("full ", stream);
  915.   wrap_here ("");
  916.   fputs_filtered ("documentation.\n", stream);
  917.   fputs_filtered ("Type \"apropos word\" to search "
  918.                   "for commands related to \"word\".\n", stream);
  919.   fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
  920.                   stream);
  921. }

  922. static void
  923. help_all (struct ui_file *stream)
  924. {
  925.   struct cmd_list_element *c;
  926.   int seen_unclassified = 0;

  927.   for (c = cmdlist; c; c = c->next)
  928.     {
  929.       if (c->abbrev_flag)
  930.         continue;
  931.       /* If this is a class name, print all of the commands in the
  932.          class.  */

  933.       if (c->func == NULL)
  934.         {
  935.           fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
  936.           help_cmd_list (cmdlist, c->class, "", 1, stream);
  937.         }
  938.     }

  939.   /* While it's expected that all commands are in some class,
  940.      as a safety measure, we'll print commands outside of any
  941.      class at the end.  */

  942.   for (c = cmdlist; c; c = c->next)
  943.     {
  944.       if (c->abbrev_flag)
  945.         continue;

  946.       if (c->class == no_class)
  947.         {
  948.           if (!seen_unclassified)
  949.             {
  950.               fprintf_filtered (stream, "\nUnclassified commands\n\n");
  951.               seen_unclassified = 1;
  952.             }
  953.           print_help_for_command (c, "", 1, stream);
  954.         }
  955.     }

  956. }

  957. /* Print only the first line of STR on STREAM.  */
  958. void
  959. print_doc_line (struct ui_file *stream, const char *str)
  960. {
  961.   static char *line_buffer = 0;
  962.   static int line_size;
  963.   const char *p;

  964.   if (!line_buffer)
  965.     {
  966.       line_size = 80;
  967.       line_buffer = (char *) xmalloc (line_size);
  968.     }

  969.   /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
  970.      like '.gdbinit'.  */
  971.   p = str;
  972.   while (*p && *p != '\n'
  973.          && ((*p != '.' && *p != ',') || (p[1] && !isspace (p[1]))))
  974.     p++;
  975.   if (p - str > line_size - 1)
  976.     {
  977.       line_size = p - str + 1;
  978.       xfree (line_buffer);
  979.       line_buffer = (char *) xmalloc (line_size);
  980.     }
  981.   strncpy (line_buffer, str, p - str);
  982.   line_buffer[p - str] = '\0';
  983.   if (islower (line_buffer[0]))
  984.     line_buffer[0] = toupper (line_buffer[0]);
  985.   fputs_filtered (line_buffer, stream);
  986. }

  987. /* Print one-line help for command C.
  988.    If RECURSE is non-zero, also print one-line descriptions
  989.    of all prefixed subcommands.  */
  990. static void
  991. print_help_for_command (struct cmd_list_element *c, const char *prefix,
  992.                         int recurse, struct ui_file *stream)
  993. {
  994.   fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
  995.   print_doc_line (stream, c->doc);
  996.   fputs_filtered ("\n", stream);

  997.   if (recurse
  998.       && c->prefixlist != 0
  999.       && c->abbrev_flag == 0)
  1000.     /* Subcommands of a prefix command typically have 'all_commands'
  1001.        as class.  If we pass CLASS to recursive invocation,
  1002.        most often we won't see anything.  */
  1003.     help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream);
  1004. }

  1005. /*
  1006. * Implement a help command on command list LIST.
  1007. * RECURSE should be non-zero if this should be done recursively on
  1008. * all sublists of LIST.
  1009. * PREFIX is the prefix to print before each command name.
  1010. * STREAM is the stream upon which the output should be written.
  1011. * CLASS should be:
  1012. *      A non-negative class number to list only commands in that
  1013. * class.
  1014. *      ALL_COMMANDS to list all commands in list.
  1015. *      ALL_CLASSES  to list all classes in list.
  1016. *
  1017. *   Note that RECURSE will be active on *all* sublists, not just the
  1018. * ones selected by the criteria above (ie. the selection mechanism
  1019. * is at the low level, not the high-level).
  1020. */
  1021. void
  1022. help_cmd_list (struct cmd_list_element *list, enum command_class class,
  1023.                const char *prefix, int recurse, struct ui_file *stream)
  1024. {
  1025.   struct cmd_list_element *c;

  1026.   for (c = list; c; c = c->next)
  1027.     {
  1028.       if (c->abbrev_flag == 0
  1029.           && (class == all_commands
  1030.               || (class == all_classes && c->func == NULL)
  1031.               || (class == c->class && c->func != NULL)))
  1032.         {
  1033.           print_help_for_command (c, prefix, recurse, stream);
  1034.         }
  1035.       else if (c->abbrev_flag == 0 && recurse
  1036.                && class == class_user && c->prefixlist != NULL)
  1037.         /* User-defined commands may be subcommands.  */
  1038.         help_cmd_list (*c->prefixlist, class, c->prefixname,
  1039.                        recurse, stream);
  1040.     }
  1041. }


  1042. /* Search the input clist for 'command'.  Return the command if
  1043.    found (or NULL if not), and return the number of commands
  1044.    found in nfound.  */

  1045. static struct cmd_list_element *
  1046. find_cmd (const char *command, int len, struct cmd_list_element *clist,
  1047.           int ignore_help_classes, int *nfound)
  1048. {
  1049.   struct cmd_list_element *found, *c;

  1050.   found = (struct cmd_list_element *) NULL;
  1051.   *nfound = 0;
  1052.   for (c = clist; c; c = c->next)
  1053.     if (!strncmp (command, c->name, len)
  1054.         && (!ignore_help_classes || c->func))
  1055.       {
  1056.         found = c;
  1057.         (*nfound)++;
  1058.         if (c->name[len] == '\0')
  1059.           {
  1060.             *nfound = 1;
  1061.             break;
  1062.           }
  1063.       }
  1064.   return found;
  1065. }

  1066. static int
  1067. find_command_name_length (const char *text)
  1068. {
  1069.   const char *p = text;

  1070.   /* Treating underscores as part of command words is important
  1071.      so that "set args_foo()" doesn't get interpreted as
  1072.      "set args _foo()".  */
  1073.   /* Some characters are only used for TUI specific commands.
  1074.      However, they are always allowed for the sake of consistency.

  1075.      The XDB compatibility characters are only allowed when using the
  1076.      right mode because they clash with other GDB commands -
  1077.      specifically '/' is used as a suffix for print, examine and
  1078.      display.

  1079.      Note that this is larger than the character set allowed when
  1080.      creating user-defined commands.  */

  1081.   /* Recognize '!' as a single character command so that, e.g., "!ls"
  1082.      works as expected.  */
  1083.   if (*p == '!')
  1084.     return 1;

  1085.   while (isalnum (*p) || *p == '-' || *p == '_'
  1086.          /* Characters used by TUI specific commands.  */
  1087.          || *p == '+' || *p == '<' || *p == '>' || *p == '$'
  1088.          /* Characters used for XDB compatibility.  */
  1089.          || (xdb_commands && (*p == '/' || *p == '?')))
  1090.     p++;

  1091.   return p - text;
  1092. }

  1093. /* Return TRUE if NAME is a valid user-defined command name.
  1094.    This is a stricter subset of all gdb commands,
  1095.    see find_command_name_length.  */

  1096. int
  1097. valid_user_defined_cmd_name_p (const char *name)
  1098. {
  1099.   const char *p;

  1100.   if (*name == '\0')
  1101.     return FALSE;

  1102.   /* Alas "42" is a legitimate user-defined command.
  1103.      In the interests of not breaking anything we preserve that.  */

  1104.   for (p = name; *p != '\0'; ++p)
  1105.     {
  1106.       if (isalnum (*p)
  1107.           || *p == '-'
  1108.           || *p == '_')
  1109.         ; /* Ok.  */
  1110.       else
  1111.         return FALSE;
  1112.     }

  1113.   return TRUE;
  1114. }

  1115. /* This routine takes a line of TEXT and a CLIST in which to start the
  1116.    lookup.  When it returns it will have incremented the text pointer past
  1117.    the section of text it matched, set *RESULT_LIST to point to the list in
  1118.    which the last word was matched, and will return a pointer to the cmd
  1119.    list element which the text matches.  It will return NULL if no match at
  1120.    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
  1121.    matches are possible; in this case *RESULT_LIST will be set to point to
  1122.    the list in which there are ambiguous choices (and *TEXT will be set to
  1123.    the ambiguous text string).

  1124.    If the located command was an abbreviation, this routine returns the base
  1125.    command of the abbreviation.

  1126.    It does no error reporting whatsoever; control will always return
  1127.    to the superior routine.

  1128.    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
  1129.    at the prefix_command (ie. the best match) *or* (special case) will be NULL
  1130.    if no prefix command was ever found.  For example, in the case of "info a",
  1131.    "info" matches without ambiguity, but "a" could be "args" or "address", so
  1132.    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
  1133.    RESULT_LIST should not be interpeted as a pointer to the beginning of a
  1134.    list; it simply points to a specific command.  In the case of an ambiguous
  1135.    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
  1136.    "info t" can be "info types" or "info target"; upon return *TEXT has been
  1137.    advanced past "info ").

  1138.    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
  1139.    affect the operation).

  1140.    This routine does *not* modify the text pointed to by TEXT.

  1141.    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
  1142.    are actually help classes rather than commands (i.e. the function field of
  1143.    the struct cmd_list_element is NULL).  */

  1144. struct cmd_list_element *
  1145. lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
  1146.               struct cmd_list_element **result_list, int ignore_help_classes)
  1147. {
  1148.   char *command;
  1149.   int len, tmp, nfound;
  1150.   struct cmd_list_element *found, *c;
  1151.   const char *line = *text;

  1152.   while (**text == ' ' || **text == '\t')
  1153.     (*text)++;

  1154.   /* Identify the name of the command.  */
  1155.   len = find_command_name_length (*text);

  1156.   /* If nothing but whitespace, return 0.  */
  1157.   if (len == 0)
  1158.     return 0;

  1159.   /* *text and p now bracket the first command word to lookup (and
  1160.      it's length is len).  We copy this into a local temporary.  */


  1161.   command = (char *) alloca (len + 1);
  1162.   memcpy (command, *text, len);
  1163.   command[len] = '\0';

  1164.   /* Look it up.  */
  1165.   found = 0;
  1166.   nfound = 0;
  1167.   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);

  1168.   /* We didn't find the command in the entered case, so lower case it
  1169.      and search again.  */
  1170.   if (!found || nfound == 0)
  1171.     {
  1172.       for (tmp = 0; tmp < len; tmp++)
  1173.         {
  1174.           char x = command[tmp];

  1175.           command[tmp] = isupper (x) ? tolower (x) : x;
  1176.         }
  1177.       found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
  1178.     }

  1179.   /* If nothing matches, we have a simple failure.  */
  1180.   if (nfound == 0)
  1181.     return 0;

  1182.   if (nfound > 1)
  1183.     {
  1184.       if (result_list != NULL)
  1185.         /* Will be modified in calling routine
  1186.            if we know what the prefix command is.  */
  1187.         *result_list = 0;
  1188.       return CMD_LIST_AMBIGUOUS;        /* Ambiguous.  */
  1189.     }

  1190.   /* We've matched something on this list.  Move text pointer forward.  */

  1191.   *text += len;

  1192.   if (found->cmd_pointer)
  1193.     {
  1194.       /* We drop the alias (abbreviation) in favor of the command it
  1195.        is pointing to.  If the alias is deprecated, though, we need to
  1196.        warn the user about it before we drop it.  Note that while we
  1197.        are warning about the alias, we may also warn about the command
  1198.        itself and we will adjust the appropriate DEPRECATED_WARN_USER
  1199.        flags.  */

  1200.       if (found->deprecated_warn_user)
  1201.         deprecated_cmd_warning (line);
  1202.       found = found->cmd_pointer;
  1203.     }
  1204.   /* If we found a prefix command, keep looking.  */

  1205.   if (found->prefixlist)
  1206.     {
  1207.       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
  1208.                         ignore_help_classes);
  1209.       if (!c)
  1210.         {
  1211.           /* Didn't find anything; this is as far as we got.  */
  1212.           if (result_list != NULL)
  1213.             *result_list = clist;
  1214.           return found;
  1215.         }
  1216.       else if (c == CMD_LIST_AMBIGUOUS)
  1217.         {
  1218.           /* We've gotten this far properly, but the next step is
  1219.              ambiguous.  We need to set the result list to the best
  1220.              we've found (if an inferior hasn't already set it).  */
  1221.           if (result_list != NULL)
  1222.             if (!*result_list)
  1223.               /* This used to say *result_list = *found->prefixlist.
  1224.                  If that was correct, need to modify the documentation
  1225.                  at the top of this function to clarify what is
  1226.                  supposed to be going on.  */
  1227.               *result_list = found;
  1228.           return c;
  1229.         }
  1230.       else
  1231.         {
  1232.           /* We matched!  */
  1233.           return c;
  1234.         }
  1235.     }
  1236.   else
  1237.     {
  1238.       if (result_list != NULL)
  1239.         *result_list = clist;
  1240.       return found;
  1241.     }
  1242. }

  1243. /* All this hair to move the space to the front of cmdtype */

  1244. static void
  1245. undef_cmd_error (const char *cmdtype, const char *q)
  1246. {
  1247.   error (_("Undefined %scommand: \"%s\".  Try \"help%s%.*s\"."),
  1248.          cmdtype,
  1249.          q,
  1250.          *cmdtype ? " " : "",
  1251.          (int) strlen (cmdtype) - 1,
  1252.          cmdtype);
  1253. }

  1254. /* Look up the contents of *LINE as a command in the command list LIST.
  1255.    LIST is a chain of struct cmd_list_element's.
  1256.    If it is found, return the struct cmd_list_element for that command
  1257.    and update *LINE to point after the command name, at the first argument.
  1258.    If not found, call error if ALLOW_UNKNOWN is zero
  1259.    otherwise (or if error returns) return zero.
  1260.    Call error if specified command is ambiguous,
  1261.    unless ALLOW_UNKNOWN is negative.
  1262.    CMDTYPE precedes the word "command" in the error message.

  1263.    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
  1264.    elements which are actually help classes rather than commands (i.e.
  1265.    the function field of the struct cmd_list_element is 0).  */

  1266. struct cmd_list_element *
  1267. lookup_cmd (const char **line, struct cmd_list_element *list, char *cmdtype,
  1268.             int allow_unknown, int ignore_help_classes)
  1269. {
  1270.   struct cmd_list_element *last_list = 0;
  1271.   struct cmd_list_element *c;

  1272.   /* Note: Do not remove trailing whitespace here because this
  1273.      would be wrong for complete_command.  Jim Kingdon  */

  1274.   if (!*line)
  1275.     error (_("Lack of needed %scommand"), cmdtype);

  1276.   c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes);

  1277.   if (!c)
  1278.     {
  1279.       if (!allow_unknown)
  1280.         {
  1281.           char *q;
  1282.           int len = find_command_name_length (*line);

  1283.           q = (char *) alloca (len + 1);
  1284.           strncpy (q, *line, len);
  1285.           q[len] = '\0';
  1286.           undef_cmd_error (cmdtype, q);
  1287.         }
  1288.       else
  1289.         return 0;
  1290.     }
  1291.   else if (c == CMD_LIST_AMBIGUOUS)
  1292.     {
  1293.       /* Ambigous.  Local values should be off prefixlist or called
  1294.          values.  */
  1295.       int local_allow_unknown = (last_list ? last_list->allow_unknown :
  1296.                                  allow_unknown);
  1297.       const char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
  1298.       struct cmd_list_element *local_list =
  1299.         (last_list ? *(last_list->prefixlist) : list);

  1300.       if (local_allow_unknown < 0)
  1301.         {
  1302.           if (last_list)
  1303.             return last_list;        /* Found something.  */
  1304.           else
  1305.             return 0;                /* Found nothing.  */
  1306.         }
  1307.       else
  1308.         {
  1309.           /* Report as error.  */
  1310.           int amb_len;
  1311.           char ambbuf[100];

  1312.           for (amb_len = 0;
  1313.                ((*line)[amb_len] && (*line)[amb_len] != ' '
  1314.                 && (*line)[amb_len] != '\t');
  1315.                amb_len++)
  1316.             ;

  1317.           ambbuf[0] = 0;
  1318.           for (c = local_list; c; c = c->next)
  1319.             if (!strncmp (*line, c->name, amb_len))
  1320.               {
  1321.                 if (strlen (ambbuf) + strlen (c->name) + 6
  1322.                     < (int) sizeof ambbuf)
  1323.                   {
  1324.                     if (strlen (ambbuf))
  1325.                       strcat (ambbuf, ", ");
  1326.                     strcat (ambbuf, c->name);
  1327.                   }
  1328.                 else
  1329.                   {
  1330.                     strcat (ambbuf, "..");
  1331.                     break;
  1332.                   }
  1333.               }
  1334.           error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype,
  1335.                  *line, ambbuf);
  1336.           return 0;                /* lint */
  1337.         }
  1338.     }
  1339.   else
  1340.     {
  1341.       if (c->type == set_cmd && **line != '\0' && !isspace (**line))
  1342.         error (_("Argument must be preceded by space."));

  1343.       /* We've got something.  It may still not be what the caller
  1344.          wants (if this command *needs* a subcommand).  */
  1345.       while (**line == ' ' || **line == '\t')
  1346.         (*line)++;

  1347.       if (c->prefixlist && **line && !c->allow_unknown)
  1348.         undef_cmd_error (c->prefixname, *line);

  1349.       /* Seems to be what he wants.  Return it.  */
  1350.       return c;
  1351.     }
  1352.   return 0;
  1353. }

  1354. /* We are here presumably because an alias or command in TEXT is
  1355.    deprecated and a warning message should be generated.  This
  1356.    function decodes TEXT and potentially generates a warning message
  1357.    as outlined below.

  1358.    Example for 'set endian big' which has a fictitious alias 'seb'.

  1359.    If alias wasn't used in TEXT, and the command is deprecated:
  1360.    "warning: 'set endian big' is deprecated."

  1361.    If alias was used, and only the alias is deprecated:
  1362.    "warning: 'seb' an alias for the command 'set endian big' is deprecated."

  1363.    If alias was used and command is deprecated (regardless of whether
  1364.    the alias itself is deprecated:

  1365.    "warning: 'set endian big' (seb) is deprecated."

  1366.    After the message has been sent, clear the appropriate flags in the
  1367.    command and/or the alias so the user is no longer bothered.

  1368. */
  1369. void
  1370. deprecated_cmd_warning (const char *text)
  1371. {
  1372.   struct cmd_list_element *alias = NULL;
  1373.   struct cmd_list_element *prefix_cmd = NULL;
  1374.   struct cmd_list_element *cmd = NULL;

  1375.   if (!lookup_cmd_composition (text, &alias, &prefix_cmd, &cmd))
  1376.     /* Return if text doesn't evaluate to a command.  */
  1377.     return;

  1378.   if (!((alias ? alias->deprecated_warn_user : 0)
  1379.       || cmd->deprecated_warn_user) )
  1380.     /* Return if nothing is deprecated.  */
  1381.     return;

  1382.   printf_filtered ("Warning:");

  1383.   if (alias && !cmd->cmd_deprecated)
  1384.     printf_filtered (" '%s', an alias for the", alias->name);

  1385.   printf_filtered (" command '");

  1386.   if (prefix_cmd)
  1387.     printf_filtered ("%s", prefix_cmd->prefixname);

  1388.   printf_filtered ("%s", cmd->name);

  1389.   if (alias && cmd->cmd_deprecated)
  1390.     printf_filtered ("' (%s) is deprecated.\n", alias->name);
  1391.   else
  1392.     printf_filtered ("' is deprecated.\n");


  1393.   /* If it is only the alias that is deprecated, we want to indicate
  1394.      the new alias, otherwise we'll indicate the new command.  */

  1395.   if (alias && !cmd->cmd_deprecated)
  1396.     {
  1397.       if (alias->replacement)
  1398.         printf_filtered ("Use '%s'.\n\n", alias->replacement);
  1399.       else
  1400.         printf_filtered ("No alternative known.\n\n");
  1401.      }
  1402.   else
  1403.     {
  1404.       if (cmd->replacement)
  1405.         printf_filtered ("Use '%s'.\n\n", cmd->replacement);
  1406.       else
  1407.         printf_filtered ("No alternative known.\n\n");
  1408.     }

  1409.   /* We've warned you, now we'll keep quiet.  */
  1410.   if (alias)
  1411.     alias->deprecated_warn_user = 0;

  1412.   cmd->deprecated_warn_user = 0;
  1413. }


  1414. /* Look up the contents of LINE as a command in the command list 'cmdlist'.
  1415.    Return 1 on success, 0 on failure.

  1416.    If LINE refers to an alias, *alias will point to that alias.

  1417.    If LINE is a postfix command (i.e. one that is preceded by a prefix
  1418.    command) set *prefix_cmd.

  1419.    Set *cmd to point to the command LINE indicates.

  1420.    If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
  1421.    exist, they are NULL when we return.

  1422. */
  1423. int
  1424. lookup_cmd_composition (const char *text,
  1425.                       struct cmd_list_element **alias,
  1426.                       struct cmd_list_element **prefix_cmd,
  1427.                       struct cmd_list_element **cmd)
  1428. {
  1429.   char *command;
  1430.   int len, tmp, nfound;
  1431.   struct cmd_list_element *cur_list;
  1432.   struct cmd_list_element *prev_cmd;

  1433.   *alias = NULL;
  1434.   *prefix_cmd = NULL;
  1435.   *cmd = NULL;

  1436.   cur_list = cmdlist;

  1437.   while (1)
  1438.     {
  1439.       /* Go through as many command lists as we need to,
  1440.          to find the command TEXT refers to.  */

  1441.       prev_cmd = *cmd;

  1442.       while (*text == ' ' || *text == '\t')
  1443.         (text)++;

  1444.       /* Identify the name of the command.  */
  1445.       len = find_command_name_length (text);

  1446.       /* If nothing but whitespace, return.  */
  1447.       if (len == 0)
  1448.         return 0;

  1449.       /* Text is the start of the first command word to lookup (and
  1450.          it's length is len).  We copy this into a local temporary.  */

  1451.       command = (char *) alloca (len + 1);
  1452.       memcpy (command, text, len);
  1453.       command[len] = '\0';

  1454.       /* Look it up.  */
  1455.       *cmd = 0;
  1456.       nfound = 0;
  1457.       *cmd = find_cmd (command, len, cur_list, 1, &nfound);

  1458.       /* We didn't find the command in the entered case, so lower case
  1459.          it and search again.
  1460.       */
  1461.       if (!*cmd || nfound == 0)
  1462.         {
  1463.           for (tmp = 0; tmp < len; tmp++)
  1464.             {
  1465.               char x = command[tmp];

  1466.               command[tmp] = isupper (x) ? tolower (x) : x;
  1467.             }
  1468.           *cmd = find_cmd (command, len, cur_list, 1, &nfound);
  1469.         }

  1470.       if (*cmd == CMD_LIST_AMBIGUOUS)
  1471.         {
  1472.           return 0;              /* ambiguous */
  1473.         }

  1474.       if (*cmd == NULL)
  1475.         return 0;                /* nothing found */
  1476.       else
  1477.         {
  1478.           if ((*cmd)->cmd_pointer)
  1479.             {
  1480.               /* cmd was actually an alias, we note that an alias was
  1481.                  used (by assigning *alais) and we set *cmd.  */
  1482.               *alias = *cmd;
  1483.               *cmd = (*cmd)->cmd_pointer;
  1484.             }
  1485.           *prefix_cmd = prev_cmd;
  1486.         }
  1487.       if ((*cmd)->prefixlist)
  1488.         cur_list = *(*cmd)->prefixlist;
  1489.       else
  1490.         return 1;

  1491.       text += len;
  1492.     }
  1493. }

  1494. /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */

  1495. /* Return a vector of char pointers which point to the different
  1496.    possible completions in LIST of TEXT.

  1497.    WORD points in the same buffer as TEXT, and completions should be
  1498.    returned relative to this position.  For example, suppose TEXT is
  1499.    "foo" and we want to complete to "foobar".  If WORD is "oo", return
  1500.    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */

  1501. VEC (char_ptr) *
  1502. complete_on_cmdlist (struct cmd_list_element *list,
  1503.                      const char *text, const char *word,
  1504.                      int ignore_help_classes)
  1505. {
  1506.   struct cmd_list_element *ptr;
  1507.   VEC (char_ptr) *matchlist = NULL;
  1508.   int textlen = strlen (text);
  1509.   int pass;
  1510.   int saw_deprecated_match = 0;

  1511.   /* We do one or two passes.  In the first pass, we skip deprecated
  1512.      commands.  If we see no matching commands in the first pass, and
  1513.      if we did happen to see a matching deprecated command, we do
  1514.      another loop to collect those.  */
  1515.   for (pass = 0; matchlist == 0 && pass < 2; ++pass)
  1516.     {
  1517.       for (ptr = list; ptr; ptr = ptr->next)
  1518.         if (!strncmp (ptr->name, text, textlen)
  1519.             && !ptr->abbrev_flag
  1520.             && (!ignore_help_classes || ptr->func
  1521.                 || ptr->prefixlist))
  1522.           {
  1523.             char *match;

  1524.             if (pass == 0)
  1525.               {
  1526.                 if (ptr->cmd_deprecated)
  1527.                   {
  1528.                     saw_deprecated_match = 1;
  1529.                     continue;
  1530.                   }
  1531.               }

  1532.             match = (char *) xmalloc (strlen (word) + strlen (ptr->name) + 1);
  1533.             if (word == text)
  1534.               strcpy (match, ptr->name);
  1535.             else if (word > text)
  1536.               {
  1537.                 /* Return some portion of ptr->name.  */
  1538.                 strcpy (match, ptr->name + (word - text));
  1539.               }
  1540.             else
  1541.               {
  1542.                 /* Return some of text plus ptr->name.  */
  1543.                 strncpy (match, word, text - word);
  1544.                 match[text - word] = '\0';
  1545.                 strcat (match, ptr->name);
  1546.               }
  1547.             VEC_safe_push (char_ptr, matchlist, match);
  1548.           }
  1549.       /* If we saw no matching deprecated commands in the first pass,
  1550.          just bail out.  */
  1551.       if (!saw_deprecated_match)
  1552.         break;
  1553.     }

  1554.   return matchlist;
  1555. }

  1556. /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */

  1557. /* Return a vector of char pointers which point to the different
  1558.    possible completions in CMD of TEXT.

  1559.    WORD points in the same buffer as TEXT, and completions should be
  1560.    returned relative to this position.  For example, suppose TEXT is "foo"
  1561.    and we want to complete to "foobar".  If WORD is "oo", return
  1562.    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */

  1563. VEC (char_ptr) *
  1564. complete_on_enum (const char *const *enumlist,
  1565.                   const char *text, const char *word)
  1566. {
  1567.   VEC (char_ptr) *matchlist = NULL;
  1568.   int textlen = strlen (text);
  1569.   int i;
  1570.   const char *name;

  1571.   for (i = 0; (name = enumlist[i]) != NULL; i++)
  1572.     if (strncmp (name, text, textlen) == 0)
  1573.       {
  1574.         char *match;

  1575.         match = (char *) xmalloc (strlen (word) + strlen (name) + 1);
  1576.         if (word == text)
  1577.           strcpy (match, name);
  1578.         else if (word > text)
  1579.           {
  1580.             /* Return some portion of name.  */
  1581.             strcpy (match, name + (word - text));
  1582.           }
  1583.         else
  1584.           {
  1585.             /* Return some of text plus name.  */
  1586.             strncpy (match, word, text - word);
  1587.             match[text - word] = '\0';
  1588.             strcat (match, name);
  1589.           }
  1590.         VEC_safe_push (char_ptr, matchlist, match);
  1591.       }

  1592.   return matchlist;
  1593. }


  1594. /* Check function pointer.  */
  1595. int
  1596. cmd_func_p (struct cmd_list_element *cmd)
  1597. {
  1598.   return (cmd->func != NULL);
  1599. }


  1600. /* Call the command function.  */
  1601. void
  1602. cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
  1603. {
  1604.   if (cmd_func_p (cmd))
  1605.     (*cmd->func) (cmd, args, from_tty);
  1606.   else
  1607.     error (_("Invalid command"));
  1608. }

  1609. int
  1610. cli_user_command_p (struct cmd_list_element *cmd)
  1611. {
  1612.   return (cmd->class == class_user
  1613.           && (cmd->func == do_cfunc || cmd->func == do_sfunc));
  1614. }