]> Git Repo - binutils.git/blob - gdb/cli/cli-decode.c
Move gdb_regex to gdbsupport
[binutils.git] / gdb / cli / cli-decode.c
1 /* Handle lists of commands, their decoding and documentation, for GDB.
2
3    Copyright (C) 1986-2022 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include "defs.h"
19 #include "symtab.h"
20 #include <ctype.h>
21 #include "gdbsupport/gdb_regex.h"
22 #include "completer.h"
23 #include "ui-out.h"
24 #include "cli/cli-cmds.h"
25 #include "cli/cli-decode.h"
26 #include "cli/cli-style.h"
27 #include "gdbsupport/gdb_optional.h"
28
29 /* Prototypes for local functions.  */
30
31 static void undef_cmd_error (const char *, const char *);
32
33 static cmd_list_element::aliases_list_type delete_cmd
34   (const char *name, cmd_list_element **list, cmd_list_element **prehook,
35    cmd_list_element **prehookee, cmd_list_element **posthook,
36    cmd_list_element **posthookee);
37
38 static struct cmd_list_element *find_cmd (const char *command,
39                                           int len,
40                                           struct cmd_list_element *clist,
41                                           int ignore_help_classes,
42                                           int *nfound);
43
44 static void help_cmd_list (struct cmd_list_element *list,
45                            enum command_class theclass,
46                            bool recurse,
47                            struct ui_file *stream);
48
49 static void help_all (struct ui_file *stream);
50
51 static int lookup_cmd_composition_1 (const char *text,
52                                      struct cmd_list_element **alias,
53                                      struct cmd_list_element **prefix_cmd,
54                                      struct cmd_list_element **cmd,
55                                      struct cmd_list_element *cur_list);
56
57 /* Look up a command whose 'subcommands' field is SUBCOMMANDS.  Return the
58    command if found, otherwise return NULL.  */
59
60 static struct cmd_list_element *
61 lookup_cmd_with_subcommands (cmd_list_element **subcommands,
62                              cmd_list_element *list)
63 {
64   struct cmd_list_element *p = NULL;
65
66   for (p = list; p != NULL; p = p->next)
67     {
68       struct cmd_list_element *q;
69
70       if (!p->is_prefix ())
71         continue;
72
73       else if (p->subcommands == subcommands)
74         {
75           /* If we found an alias, we must return the aliased
76              command.  */
77           return p->is_alias () ? p->alias_target : p;
78         }
79
80       q = lookup_cmd_with_subcommands (subcommands, *(p->subcommands));
81       if (q != NULL)
82         return q;
83     }
84
85   return NULL;
86 }
87
88 static void
89 print_help_for_command (const cmd_list_element &c,
90                         bool recurse, struct ui_file *stream);
91
92 static void
93 do_simple_func (const char *args, int from_tty, cmd_list_element *c)
94 {
95   c->function.simple_func (args, from_tty);
96 }
97
98 static void
99 set_cmd_simple_func (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
100 {
101   if (simple_func == NULL)
102     cmd->func = NULL;
103   else
104     cmd->func = do_simple_func;
105
106   cmd->function.simple_func = simple_func;
107 }
108
109 int
110 cmd_simple_func_eq (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
111 {
112   return (cmd->func == do_simple_func
113           && cmd->function.simple_func == simple_func);
114 }
115
116 void
117 set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
118 {
119   cmd->completer = completer; /* Ok.  */
120 }
121
122 /* See definition in commands.h.  */
123
124 void
125 set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
126                                    completer_handle_brkchars_ftype *func)
127 {
128   cmd->completer_handle_brkchars = func;
129 }
130
131 std::string
132 cmd_list_element::prefixname () const
133 {
134   if (!this->is_prefix ())
135     /* Not a prefix command.  */
136     return "";
137
138   std::string prefixname;
139   if (this->prefix != nullptr)
140     prefixname = this->prefix->prefixname ();
141
142   prefixname += this->name;
143   prefixname += " ";
144
145   return prefixname;
146 }
147
148 /* Add element named NAME.
149    Space for NAME and DOC must be allocated by the caller.
150    CLASS is the top level category into which commands are broken down
151    for "help" purposes.
152    FUN should be the function to execute the command;
153    it will get a character string as argument, with leading
154    and trailing blanks already eliminated.
155
156    DOC is a documentation string for the command.
157    Its first line should be a complete sentence.
158    It should start with ? for a command that is an abbreviation
159    or with * for a command that most users don't need to know about.
160
161    Add this command to command list *LIST.
162
163    Returns a pointer to the added command (not necessarily the head 
164    of *LIST).  */
165
166 static struct cmd_list_element *
167 do_add_cmd (const char *name, enum command_class theclass,
168             const char *doc, struct cmd_list_element **list)
169 {
170   struct cmd_list_element *c = new struct cmd_list_element (name, theclass,
171                                                             doc);
172
173   /* Turn each alias of the old command into an alias of the new
174      command.  */
175   c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
176                            &c->hook_post, &c->hookee_post);
177
178   for (cmd_list_element &alias : c->aliases)
179     alias.alias_target = c;
180
181   if (c->hook_pre)
182     c->hook_pre->hookee_pre = c;
183
184   if (c->hookee_pre)
185     c->hookee_pre->hook_pre = c;
186
187   if (c->hook_post)
188     c->hook_post->hookee_post = c;
189
190   if (c->hookee_post)
191     c->hookee_post->hook_post = c;
192
193   if (*list == NULL || strcmp ((*list)->name, name) >= 0)
194     {
195       c->next = *list;
196       *list = c;
197     }
198   else
199     {
200       cmd_list_element *p = *list;
201       while (p->next && strcmp (p->next->name, name) <= 0)
202         {
203           p = p->next;
204         }
205       c->next = p->next;
206       p->next = c;
207     }
208
209   /* Search the prefix cmd of C, and assigns it to C->prefix.
210      See also add_prefix_cmd and update_prefix_field_of_prefixed_commands.  */
211   cmd_list_element *prefixcmd = lookup_cmd_with_subcommands (list, cmdlist);
212   c->prefix = prefixcmd;
213
214
215   return c;
216 }
217
218 struct cmd_list_element *
219 add_cmd (const char *name, enum command_class theclass,
220          const char *doc, struct cmd_list_element **list)
221 {
222   cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
223   result->func = NULL;
224   result->function.simple_func = NULL;
225   return result;
226 }
227
228 struct cmd_list_element *
229 add_cmd (const char *name, enum command_class theclass,
230          cmd_simple_func_ftype *fun,
231          const char *doc, struct cmd_list_element **list)
232 {
233   cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
234   set_cmd_simple_func (result, fun);
235   return result;
236 }
237
238 /* Add an element with a suppress notification to the LIST of commands.  */
239
240 struct cmd_list_element *
241 add_cmd_suppress_notification (const char *name, enum command_class theclass,
242                                cmd_simple_func_ftype *fun, const char *doc,
243                                struct cmd_list_element **list,
244                                int *suppress_notification)
245 {
246   struct cmd_list_element *element;
247
248   element = add_cmd (name, theclass, fun, doc, list);
249   element->suppress_notification = suppress_notification;
250
251   return element;
252 }
253
254
255 /* Deprecates a command CMD.
256    REPLACEMENT is the name of the command which should be used in
257    place of this command, or NULL if no such command exists.
258
259    This function does not check to see if command REPLACEMENT exists
260    since gdb may not have gotten around to adding REPLACEMENT when
261    this function is called.
262
263    Returns a pointer to the deprecated command.  */
264
265 struct cmd_list_element *
266 deprecate_cmd (struct cmd_list_element *cmd, const char *replacement)
267 {
268   cmd->cmd_deprecated = 1;
269   cmd->deprecated_warn_user = 1;
270
271   if (replacement != NULL)
272     cmd->replacement = replacement;
273   else
274     cmd->replacement = NULL;
275
276   return cmd;
277 }
278
279 struct cmd_list_element *
280 add_alias_cmd (const char *name, cmd_list_element *target,
281                enum command_class theclass, int abbrev_flag,
282                struct cmd_list_element **list)
283 {
284   gdb_assert (target != nullptr);
285
286   struct cmd_list_element *c = add_cmd (name, theclass, target->doc, list);
287
288   /* If TARGET->DOC can be freed, we should make another copy.  */
289   if (target->doc_allocated)
290     {
291       c->doc = xstrdup (target->doc);
292       c->doc_allocated = 1;
293     }
294   /* NOTE: Both FUNC and all the FUNCTIONs need to be copied.  */
295   c->func = target->func;
296   c->function = target->function;
297   c->subcommands = target->subcommands;
298   c->allow_unknown = target->allow_unknown;
299   c->abbrev_flag = abbrev_flag;
300   c->alias_target = target;
301   target->aliases.push_front (*c);
302
303   return c;
304 }
305
306 /* Update the prefix field of all sub-commands of the prefix command C.
307    We must do this when a prefix command is defined as the GDB init sequence
308    does not guarantee that a prefix command is created before its sub-commands.
309    For example, break-catch-sig.c initialization runs before breakpoint.c
310    initialization, but it is breakpoint.c that creates the "catch" command used
311    by the "catch signal" command created by break-catch-sig.c.  */
312
313 static void
314 update_prefix_field_of_prefixed_commands (struct cmd_list_element *c)
315 {
316   for (cmd_list_element *p = *c->subcommands; p != NULL; p = p->next)
317     {
318       p->prefix = c;
319
320       /* We must recursively update the prefix field to cover
321          e.g.  'info auto-load libthread-db' where the creation
322          order was:
323            libthread-db
324            auto-load
325            info
326          In such a case, when 'auto-load' was created by do_add_cmd,
327          the 'libthread-db' prefix field could not be updated, as the
328          'auto-load' command was not yet reachable by
329             lookup_cmd_for_subcommands (list, cmdlist)
330             that searches from the top level 'cmdlist'.  */
331       if (p->is_prefix ())
332         update_prefix_field_of_prefixed_commands (p);
333     }
334 }
335
336
337 /* Like add_cmd but adds an element for a command prefix: a name that
338    should be followed by a subcommand to be looked up in another
339    command list.  SUBCOMMANDS should be the address of the variable
340    containing that list.  */
341
342 struct cmd_list_element *
343 add_prefix_cmd (const char *name, enum command_class theclass,
344                 cmd_simple_func_ftype *fun,
345                 const char *doc, struct cmd_list_element **subcommands,
346                 int allow_unknown, struct cmd_list_element **list)
347 {
348   struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
349
350   c->subcommands = subcommands;
351   c->allow_unknown = allow_unknown;
352
353   /* Now that prefix command C is defined, we need to set the prefix field
354      of all prefixed commands that were defined before C itself was defined.  */
355   update_prefix_field_of_prefixed_commands (c);
356
357   return c;
358 }
359
360 /* A helper function for add_basic_prefix_cmd.  This is a command
361    function that just forwards to help_list.  */
362
363 static void
364 do_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
365 {
366   /* Look past all aliases.  */
367   while (c->is_alias ())
368     c = c->alias_target;
369
370   help_list (*c->subcommands, c->prefixname ().c_str (),
371              all_commands, gdb_stdout);
372 }
373
374 /* See command.h.  */
375
376 struct cmd_list_element *
377 add_basic_prefix_cmd (const char *name, enum command_class theclass,
378                       const char *doc, struct cmd_list_element **subcommands,
379                       int allow_unknown, struct cmd_list_element **list)
380 {
381   struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
382                                                  doc, subcommands,
383                                                  allow_unknown, list);
384   cmd->func = do_prefix_cmd;
385   return cmd;
386 }
387
388 /* A helper function for add_show_prefix_cmd.  This is a command
389    function that just forwards to cmd_show_list.  */
390
391 static void
392 do_show_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
393 {
394   cmd_show_list (*c->subcommands, from_tty);
395 }
396
397 /* See command.h.  */
398
399 struct cmd_list_element *
400 add_show_prefix_cmd (const char *name, enum command_class theclass,
401                      const char *doc, struct cmd_list_element **subcommands,
402                      int allow_unknown, struct cmd_list_element **list)
403 {
404   struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
405                                                  doc, subcommands,
406                                                  allow_unknown, list);
407   cmd->func = do_show_prefix_cmd;
408   return cmd;
409 }
410
411 /* See command.h.  */
412
413 set_show_commands
414 add_setshow_prefix_cmd (const char *name, command_class theclass,
415                         const char *set_doc, const char *show_doc,
416                         cmd_list_element **set_subcommands_list,
417                         cmd_list_element **show_subcommands_list,
418                         cmd_list_element **set_list,
419                         cmd_list_element **show_list)
420 {
421   set_show_commands cmds;
422
423   cmds.set = add_basic_prefix_cmd (name, theclass, set_doc,
424                                    set_subcommands_list, 0,
425                                    set_list);
426   cmds.show = add_show_prefix_cmd (name, theclass, show_doc,
427                                    show_subcommands_list, 0,
428                                    show_list);
429
430   return cmds;
431 }
432
433 /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
434    new command list element.  */
435
436 struct cmd_list_element *
437 add_prefix_cmd_suppress_notification
438                (const char *name, enum command_class theclass,
439                 cmd_simple_func_ftype *fun,
440                 const char *doc, struct cmd_list_element **subcommands,
441                 int allow_unknown, struct cmd_list_element **list,
442                 int *suppress_notification)
443 {
444   struct cmd_list_element *element
445     = add_prefix_cmd (name, theclass, fun, doc, subcommands,
446                       allow_unknown, list);
447   element->suppress_notification = suppress_notification;
448   return element;
449 }
450
451 /* Like add_prefix_cmd but sets the abbrev_flag on the new command.  */
452
453 struct cmd_list_element *
454 add_abbrev_prefix_cmd (const char *name, enum command_class theclass,
455                        cmd_simple_func_ftype *fun, const char *doc,
456                        struct cmd_list_element **subcommands,
457                        int allow_unknown, struct cmd_list_element **list)
458 {
459   struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
460
461   c->subcommands = subcommands;
462   c->allow_unknown = allow_unknown;
463   c->abbrev_flag = 1;
464   return c;
465 }
466
467 /* This is an empty "simple func".  */
468 void
469 not_just_help_class_command (const char *args, int from_tty)
470 {
471 }
472
473 /* This is an empty cmd func.  */
474
475 static void
476 empty_func (const char *args, int from_tty, cmd_list_element *c)
477 {
478 }
479
480 /* Add element named NAME to command list LIST (the list for set/show
481    or some sublist thereof).
482    TYPE is set_cmd or show_cmd.
483    CLASS is as in add_cmd.
484    VAR_TYPE is the kind of thing we are setting.
485    VAR is address of the variable being controlled by this command.
486    SET_SETTING_FUNC is a pointer to an optional function callback used to set
487    the setting value.
488    GET_SETTING_FUNC is a pointer to an optional function callback used to get
489    the setting value.
490    DOC is the documentation string.  */
491
492 static struct cmd_list_element *
493 add_set_or_show_cmd (const char *name,
494                      enum cmd_types type,
495                      enum command_class theclass,
496                      var_types var_type,
497                      const setting::erased_args &arg,
498                      const char *doc,
499                      struct cmd_list_element **list)
500 {
501   struct cmd_list_element *c = add_cmd (name, theclass, doc, list);
502
503   gdb_assert (type == set_cmd || type == show_cmd);
504   c->type = type;
505   c->var.emplace (var_type, arg);
506
507   /* This needs to be something besides NULL so that this isn't
508      treated as a help class.  */
509   c->func = empty_func;
510   return c;
511 }
512
513 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
514    CLASS is as in add_cmd.  VAR_TYPE is the kind of thing we are
515    setting.  VAR is address of the variable being controlled by this
516    command.  If nullptr is given as VAR, then both SET_SETTING_FUNC and
517    GET_SETTING_FUNC must be provided. SET_SETTING_FUNC and GET_SETTING_FUNC are
518    callbacks used to access and modify the underlying property, whatever its
519    storage is.  SET_FUNC and SHOW_FUNC are the callback functions (if non-NULL).
520    SET_DOC, SHOW_DOC and HELP_DOC are the documentation strings.
521
522    Return the newly created set and show commands.  */
523
524 static set_show_commands
525 add_setshow_cmd_full_erased (const char *name,
526                              enum command_class theclass,
527                              var_types var_type,
528                              const setting::erased_args &args,
529                              const char *set_doc, const char *show_doc,
530                              const char *help_doc,
531                              cmd_func_ftype *set_func,
532                              show_value_ftype *show_func,
533                              struct cmd_list_element **set_list,
534                              struct cmd_list_element **show_list)
535 {
536   struct cmd_list_element *set;
537   struct cmd_list_element *show;
538   gdb::unique_xmalloc_ptr<char> full_set_doc;
539   gdb::unique_xmalloc_ptr<char> full_show_doc;
540
541   if (help_doc != NULL)
542     {
543       full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
544       full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
545     }
546   else
547     {
548       full_set_doc = make_unique_xstrdup (set_doc);
549       full_show_doc = make_unique_xstrdup (show_doc);
550     }
551   set = add_set_or_show_cmd (name, set_cmd, theclass, var_type, args,
552                              full_set_doc.release (), set_list);
553   set->doc_allocated = 1;
554
555   if (set_func != NULL)
556     set->func = set_func;
557
558   show = add_set_or_show_cmd (name, show_cmd, theclass, var_type, args,
559                               full_show_doc.release (), show_list);
560   show->doc_allocated = 1;
561   show->show_value_func = show_func;
562   /* Disable the default symbol completer.  Doesn't make much sense
563      for the "show" command to complete on anything.  */
564   set_cmd_completer (show, nullptr);
565
566   return {set, show};
567 }
568
569 template<typename T>
570 static set_show_commands
571 add_setshow_cmd_full (const char *name,
572                       enum command_class theclass,
573                       var_types var_type, T *var,
574                       const char *set_doc, const char *show_doc,
575                       const char *help_doc,
576                       typename setting_func_types<T>::set set_setting_func,
577                       typename setting_func_types<T>::get get_setting_func,
578                       cmd_func_ftype *set_func,
579                       show_value_ftype *show_func,
580                       struct cmd_list_element **set_list,
581                       struct cmd_list_element **show_list)
582 {
583   auto erased_args
584     = setting::erase_args (var_type, var,
585                            set_setting_func, get_setting_func);
586
587   return add_setshow_cmd_full_erased (name,
588                                       theclass,
589                                       var_type, erased_args,
590                                       set_doc, show_doc,
591                                       help_doc,
592                                       set_func,
593                                       show_func,
594                                       set_list,
595                                       show_list);
596 }
597
598 /* Add element named NAME to command list LIST (the list for set or
599    some sublist thereof).  CLASS is as in add_cmd.  ENUMLIST is a list
600    of strings which may follow NAME.  VAR is address of the variable
601    which will contain the matching string (from ENUMLIST).  */
602
603 set_show_commands
604 add_setshow_enum_cmd (const char *name,
605                       enum command_class theclass,
606                       const char *const *enumlist,
607                       const char **var,
608                       const char *set_doc,
609                       const char *show_doc,
610                       const char *help_doc,
611                       cmd_func_ftype *set_func,
612                       show_value_ftype *show_func,
613                       struct cmd_list_element **set_list,
614                       struct cmd_list_element **show_list)
615 {
616   set_show_commands commands
617     =  add_setshow_cmd_full<const char *> (name, theclass, var_enum, var,
618                                            set_doc, show_doc, help_doc,
619                                            nullptr, nullptr, set_func,
620                                            show_func, set_list, show_list);
621   commands.set->enums = enumlist;
622   return commands;
623 }
624
625 /* Same as above but using a getter and a setter function instead of a pointer
626    to a global storage buffer.  */
627
628 set_show_commands
629 add_setshow_enum_cmd (const char *name, command_class theclass,
630                       const char *const *enumlist, const char *set_doc,
631                       const char *show_doc, const char *help_doc,
632                       setting_func_types<const char *>::set set_func,
633                       setting_func_types<const char *>::get get_func,
634                       show_value_ftype *show_func,
635                       cmd_list_element **set_list,
636                       cmd_list_element **show_list)
637 {
638   auto cmds = add_setshow_cmd_full<const char *> (name, theclass, var_enum,
639                                                   nullptr, set_doc, show_doc,
640                                                   help_doc, set_func, get_func,
641                                                   nullptr, show_func, set_list,
642                                                   show_list);
643
644   cmds.set->enums = enumlist;
645
646   return cmds;
647 }
648
649 /* See cli-decode.h.  */
650 const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
651
652 /* Add an auto-boolean command named NAME to both the set and show
653    command list lists.  CLASS is as in add_cmd.  VAR is address of the
654    variable which will contain the value.  DOC is the documentation
655    string.  FUNC is the corresponding callback.  */
656
657 set_show_commands
658 add_setshow_auto_boolean_cmd (const char *name,
659                               enum command_class theclass,
660                               enum auto_boolean *var,
661                               const char *set_doc, const char *show_doc,
662                               const char *help_doc,
663                               cmd_func_ftype *set_func,
664                               show_value_ftype *show_func,
665                               struct cmd_list_element **set_list,
666                               struct cmd_list_element **show_list)
667 {
668   set_show_commands commands
669     = add_setshow_cmd_full<enum auto_boolean> (name, theclass, var_auto_boolean,
670                                                var, set_doc, show_doc, help_doc,
671                                                nullptr, nullptr, set_func,
672                                                show_func, set_list, show_list);
673
674   commands.set->enums = auto_boolean_enums;
675
676   return commands;
677 }
678
679 /* Same as above but using a getter and a setter function instead of a pointer
680    to a global storage buffer.  */
681
682 set_show_commands
683 add_setshow_auto_boolean_cmd (const char *name, command_class theclass,
684                               const char *set_doc, const char *show_doc,
685                               const char *help_doc,
686                               setting_func_types<enum auto_boolean>::set set_func,
687                               setting_func_types<enum auto_boolean>::get get_func,
688                               show_value_ftype *show_func,
689                               cmd_list_element **set_list,
690                               cmd_list_element **show_list)
691 {
692   auto cmds = add_setshow_cmd_full<enum auto_boolean> (name, theclass,
693                                                        var_auto_boolean,
694                                                        nullptr, set_doc,
695                                                        show_doc, help_doc,
696                                                        set_func, get_func,
697                                                        nullptr, show_func,
698                                                        set_list, show_list);
699
700   cmds.set->enums = auto_boolean_enums;
701
702   return cmds;
703 }
704
705 /* See cli-decode.h.  */
706 const char * const boolean_enums[] = { "on", "off", NULL };
707
708 /* Add element named NAME to both the set and show command LISTs (the
709    list for set/show or some sublist thereof).  CLASS is as in
710    add_cmd.  VAR is address of the variable which will contain the
711    value.  SET_DOC and SHOW_DOC are the documentation strings.
712    Returns the new command element.  */
713
714 set_show_commands
715 add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *var,
716                          const char *set_doc, const char *show_doc,
717                          const char *help_doc,
718                          cmd_func_ftype *set_func,
719                          show_value_ftype *show_func,
720                          struct cmd_list_element **set_list,
721                          struct cmd_list_element **show_list)
722 {
723   set_show_commands commands
724     = add_setshow_cmd_full<bool> (name, theclass, var_boolean, var,
725                                   set_doc, show_doc, help_doc,
726                                   nullptr, nullptr, set_func, show_func,
727                                   set_list, show_list);
728
729   commands.set->enums = boolean_enums;
730
731   return commands;
732 }
733
734 /* Same as above but using a getter and a setter function instead of a pointer
735    to a global storage buffer.  */
736
737 set_show_commands
738 add_setshow_boolean_cmd (const char *name, command_class theclass,
739                          const char *set_doc, const char *show_doc,
740                          const char *help_doc,
741                          setting_func_types<bool>::set set_func,
742                          setting_func_types<bool>::get get_func,
743                          show_value_ftype *show_func,
744                          cmd_list_element **set_list,
745                          cmd_list_element **show_list)
746 {
747   auto cmds = add_setshow_cmd_full<bool> (name, theclass, var_boolean, nullptr,
748                                           set_doc, show_doc, help_doc,
749                                           set_func, get_func, nullptr,
750                                           show_func, set_list, show_list);
751
752   cmds.set->enums = boolean_enums;
753
754   return cmds;
755 }
756
757 /* Add element named NAME to both the set and show command LISTs (the
758    list for set/show or some sublist thereof).  */
759
760 set_show_commands
761 add_setshow_filename_cmd (const char *name, enum command_class theclass,
762                           std::string *var,
763                           const char *set_doc, const char *show_doc,
764                           const char *help_doc,
765                           cmd_func_ftype *set_func,
766                           show_value_ftype *show_func,
767                           struct cmd_list_element **set_list,
768                           struct cmd_list_element **show_list)
769 {
770   set_show_commands commands
771     = add_setshow_cmd_full<std::string> (name, theclass, var_filename, var,
772                                          set_doc, show_doc, help_doc,
773                                          nullptr, nullptr, set_func,
774                                          show_func, set_list, show_list);
775
776   set_cmd_completer (commands.set, filename_completer);
777
778   return commands;
779 }
780
781 /* Same as above but using a getter and a setter function instead of a pointer
782    to a global storage buffer.  */
783
784 set_show_commands
785 add_setshow_filename_cmd (const char *name, command_class theclass,
786                           const char *set_doc, const char *show_doc,
787                           const char *help_doc,
788                           setting_func_types<std::string>::set set_func,
789                           setting_func_types<std::string>::get get_func,
790                           show_value_ftype *show_func,
791                           cmd_list_element **set_list,
792                           cmd_list_element **show_list)
793 {
794   auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_filename,
795                                                  nullptr, set_doc, show_doc,
796                                                  help_doc, set_func, get_func,
797                                                  nullptr, show_func, set_list,
798                                                  show_list);
799
800   set_cmd_completer (cmds.set, filename_completer);
801
802   return cmds;
803 }
804
805 /* Add element named NAME to both the set and show command LISTs (the
806    list for set/show or some sublist thereof).  */
807
808 set_show_commands
809 add_setshow_string_cmd (const char *name, enum command_class theclass,
810                         std::string *var,
811                         const char *set_doc, const char *show_doc,
812                         const char *help_doc,
813                         cmd_func_ftype *set_func,
814                         show_value_ftype *show_func,
815                         struct cmd_list_element **set_list,
816                         struct cmd_list_element **show_list)
817 {
818   set_show_commands commands
819     = add_setshow_cmd_full<std::string> (name, theclass, var_string, var,
820                                         set_doc, show_doc, help_doc,
821                                         nullptr, nullptr, set_func,
822                                         show_func, set_list, show_list);
823
824   /* Disable the default symbol completer.  */
825   set_cmd_completer (commands.set, nullptr);
826
827   return commands;
828 }
829
830 /* Same as above but using a getter and a setter function instead of a pointer
831    to a global storage buffer.  */
832
833 set_show_commands
834 add_setshow_string_cmd (const char *name, command_class theclass,
835                         const char *set_doc, const char *show_doc,
836                         const char *help_doc,
837                         setting_func_types<std::string>::set set_func,
838                         setting_func_types<std::string>::get get_func,
839                         show_value_ftype *show_func,
840                         cmd_list_element **set_list,
841                         cmd_list_element **show_list)
842 {
843   auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_string,
844                                                  nullptr, set_doc, show_doc,
845                                                  help_doc, set_func, get_func,
846                                                  nullptr, show_func, set_list,
847                                                  show_list);
848
849   /* Disable the default symbol completer.  */
850   set_cmd_completer (cmds.set, nullptr);
851
852   return cmds;
853 }
854
855 /* Add element named NAME to both the set and show command LISTs (the
856    list for set/show or some sublist thereof).  */
857
858 set_show_commands
859 add_setshow_string_noescape_cmd (const char *name, enum command_class theclass,
860                                  std::string *var,
861                                  const char *set_doc, const char *show_doc,
862                                  const char *help_doc,
863                                  cmd_func_ftype *set_func,
864                                  show_value_ftype *show_func,
865                                  struct cmd_list_element **set_list,
866                                  struct cmd_list_element **show_list)
867 {
868   set_show_commands commands
869     = add_setshow_cmd_full<std::string> (name, theclass, var_string_noescape,
870                                          var, set_doc, show_doc, help_doc,
871                                          nullptr, nullptr, set_func, show_func,
872                                          set_list, show_list);
873
874   /* Disable the default symbol completer.  */
875   set_cmd_completer (commands.set, nullptr);
876
877   return commands;
878 }
879
880 /* Same as above but using a getter and a setter function instead of a pointer
881    to a global storage buffer.  */
882
883 set_show_commands
884 add_setshow_string_noescape_cmd (const char *name, command_class theclass,
885                                  const char *set_doc, const char *show_doc,
886                                  const char *help_doc,
887                                  setting_func_types<std::string>::set set_func,
888                                  setting_func_types<std::string>::get get_func,
889                                  show_value_ftype *show_func,
890                                  cmd_list_element **set_list,
891                                  cmd_list_element **show_list)
892 {
893   auto cmds = add_setshow_cmd_full<std::string> (name, theclass,
894                                                  var_string_noescape, nullptr,
895                                                  set_doc, show_doc, help_doc,
896                                                  set_func, get_func,
897                                                  nullptr, show_func, set_list,
898                                                  show_list);
899
900   /* Disable the default symbol completer.  */
901   set_cmd_completer (cmds.set, nullptr);
902
903   return cmds;
904 }
905
906 /* Add element named NAME to both the set and show command LISTs (the
907    list for set/show or some sublist thereof).  */
908
909 set_show_commands
910 add_setshow_optional_filename_cmd (const char *name, enum command_class theclass,
911                                    std::string *var,
912                                    const char *set_doc, const char *show_doc,
913                                    const char *help_doc,
914                                    cmd_func_ftype *set_func,
915                                    show_value_ftype *show_func,
916                                    struct cmd_list_element **set_list,
917                                    struct cmd_list_element **show_list)
918 {
919   set_show_commands commands
920     = add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
921                                          var, set_doc, show_doc, help_doc,
922                                          nullptr, nullptr, set_func, show_func,
923                                          set_list, show_list);
924
925   set_cmd_completer (commands.set, filename_completer);
926
927   return commands;
928 }
929
930 /* Same as above but using a getter and a setter function instead of a pointer
931    to a global storage buffer.  */
932
933 set_show_commands
934 add_setshow_optional_filename_cmd (const char *name, command_class theclass,
935                                    const char *set_doc, const char *show_doc,
936                                    const char *help_doc,
937                                    setting_func_types<std::string>::set set_func,
938                                    setting_func_types<std::string>::get get_func,
939                                    show_value_ftype *show_func,
940                                    cmd_list_element **set_list,
941                                    cmd_list_element **show_list)
942 {
943   auto cmds =
944     add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
945                                        nullptr, set_doc, show_doc, help_doc,
946                                        set_func, get_func, nullptr, show_func,
947                                        set_list,show_list);
948
949   set_cmd_completer (cmds.set, filename_completer);
950
951   return cmds;
952 }
953
954 /* Completes on literal "unlimited".  Used by integer commands that
955    support a special "unlimited" value.  */
956
957 static void
958 integer_unlimited_completer (struct cmd_list_element *ignore,
959                              completion_tracker &tracker,
960                              const char *text, const char *word)
961 {
962   static const char * const keywords[] =
963     {
964       "unlimited",
965       NULL,
966     };
967
968   complete_on_enum (tracker, keywords, text, word);
969 }
970
971 /* Add element named NAME to both the set and show command LISTs (the
972    list for set/show or some sublist thereof).  CLASS is as in
973    add_cmd.  VAR is address of the variable which will contain the
974    value.  SET_DOC and SHOW_DOC are the documentation strings.  This
975    function is only used in Python API.  Please don't use it elsewhere.  */
976
977 set_show_commands
978 add_setshow_integer_cmd (const char *name, enum command_class theclass,
979                          int *var,
980                          const char *set_doc, const char *show_doc,
981                          const char *help_doc,
982                          cmd_func_ftype *set_func,
983                          show_value_ftype *show_func,
984                          struct cmd_list_element **set_list,
985                          struct cmd_list_element **show_list)
986 {
987   set_show_commands commands
988     = add_setshow_cmd_full<int> (name, theclass, var_integer, var,
989                                  set_doc, show_doc, help_doc,
990                                  nullptr, nullptr, set_func,
991                                  show_func, set_list, show_list);
992
993   set_cmd_completer (commands.set, integer_unlimited_completer);
994
995   return commands;
996 }
997
998 /* Same as above but using a getter and a setter function instead of a pointer
999    to a global storage buffer.  */
1000
1001 set_show_commands
1002 add_setshow_integer_cmd (const char *name, command_class theclass,
1003                          const char *set_doc, const char *show_doc,
1004                          const char *help_doc,
1005                          setting_func_types<int>::set set_func,
1006                          setting_func_types<int>::get get_func,
1007                          show_value_ftype *show_func,
1008                          cmd_list_element **set_list,
1009                          cmd_list_element **show_list)
1010 {
1011   auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
1012                                          set_doc, show_doc, help_doc, set_func,
1013                                          get_func, nullptr, show_func, set_list,
1014                                          show_list);
1015
1016   set_cmd_completer (cmds.set, integer_unlimited_completer);
1017
1018   return cmds;
1019 }
1020
1021 /* Add element named NAME to both the set and show command LISTs (the
1022    list for set/show or some sublist thereof).  CLASS is as in
1023    add_cmd.  VAR is address of the variable which will contain the
1024    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
1025
1026 set_show_commands
1027 add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
1028                           unsigned int *var,
1029                           const char *set_doc, const char *show_doc,
1030                           const char *help_doc,
1031                           cmd_func_ftype *set_func,
1032                           show_value_ftype *show_func,
1033                           struct cmd_list_element **set_list,
1034                           struct cmd_list_element **show_list)
1035 {
1036   set_show_commands commands
1037     = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var,
1038                                           set_doc, show_doc, help_doc,
1039                                           nullptr, nullptr, set_func,
1040                                           show_func, set_list, show_list);
1041
1042   set_cmd_completer (commands.set, integer_unlimited_completer);
1043
1044   return commands;
1045 }
1046
1047 /* Same as above but using a getter and a setter function instead of a pointer
1048    to a global storage buffer.  */
1049
1050 set_show_commands
1051 add_setshow_uinteger_cmd (const char *name, command_class theclass,
1052                           const char *set_doc, const char *show_doc,
1053                           const char *help_doc,
1054                           setting_func_types<unsigned int>::set set_func,
1055                           setting_func_types<unsigned int>::get get_func,
1056                           show_value_ftype *show_func,
1057                           cmd_list_element **set_list,
1058                           cmd_list_element **show_list)
1059 {
1060   auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
1061                                                   nullptr, set_doc, show_doc,
1062                                                   help_doc, set_func, get_func,
1063                                                   nullptr, show_func, set_list,
1064                                                   show_list);
1065
1066   set_cmd_completer (cmds.set, integer_unlimited_completer);
1067
1068   return cmds;
1069 }
1070
1071 /* Add element named NAME to both the set and show command LISTs (the
1072    list for set/show or some sublist thereof).  CLASS is as in
1073    add_cmd.  VAR is address of the variable which will contain the
1074    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
1075
1076 set_show_commands
1077 add_setshow_zinteger_cmd (const char *name, enum command_class theclass,
1078                           int *var,
1079                           const char *set_doc, const char *show_doc,
1080                           const char *help_doc,
1081                           cmd_func_ftype *set_func,
1082                           show_value_ftype *show_func,
1083                           struct cmd_list_element **set_list,
1084                           struct cmd_list_element **show_list)
1085 {
1086   return add_setshow_cmd_full<int> (name, theclass, var_zinteger, var,
1087                                     set_doc, show_doc, help_doc,
1088                                     nullptr, nullptr, set_func,
1089                                     show_func, set_list, show_list);
1090 }
1091
1092 /* Same as above but using a getter and a setter function instead of a pointer
1093    to a global storage buffer.  */
1094
1095 set_show_commands
1096 add_setshow_zinteger_cmd (const char *name, command_class theclass,
1097                           const char *set_doc, const char *show_doc,
1098                           const char *help_doc,
1099                           setting_func_types<int>::set set_func,
1100                           setting_func_types<int>::get get_func,
1101                           show_value_ftype *show_func,
1102                           cmd_list_element **set_list,
1103                           cmd_list_element **show_list)
1104 {
1105   return add_setshow_cmd_full<int> (name, theclass, var_zinteger, nullptr,
1106                                     set_doc, show_doc, help_doc, set_func,
1107                                     get_func, nullptr, show_func, set_list,
1108                                     show_list);
1109 }
1110
1111 set_show_commands
1112 add_setshow_zuinteger_unlimited_cmd (const char *name,
1113                                      enum command_class theclass,
1114                                      int *var,
1115                                      const char *set_doc,
1116                                      const char *show_doc,
1117                                      const char *help_doc,
1118                                      cmd_func_ftype *set_func,
1119                                      show_value_ftype *show_func,
1120                                      struct cmd_list_element **set_list,
1121                                      struct cmd_list_element **show_list)
1122 {
1123   set_show_commands commands
1124     = add_setshow_cmd_full<int> (name, theclass, var_zuinteger_unlimited, var,
1125                                  set_doc, show_doc, help_doc, nullptr,
1126                                  nullptr, set_func, show_func, set_list,
1127                                  show_list);
1128
1129   set_cmd_completer (commands.set, integer_unlimited_completer);
1130
1131   return commands;
1132 }
1133
1134 /* Same as above but using a getter and a setter function instead of a pointer
1135    to a global storage buffer.  */
1136
1137 set_show_commands
1138 add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass,
1139                                      const char *set_doc, const char *show_doc,
1140                                      const char *help_doc,
1141                                      setting_func_types<int>::set set_func,
1142                                      setting_func_types<int>::get get_func,
1143                                      show_value_ftype *show_func,
1144                                      cmd_list_element **set_list,
1145                                      cmd_list_element **show_list)
1146 {
1147   auto cmds
1148     = add_setshow_cmd_full<int> (name, theclass, var_zuinteger_unlimited,
1149                                  nullptr, set_doc, show_doc, help_doc, set_func,
1150                                  get_func, nullptr, show_func, set_list,
1151                                  show_list);
1152
1153   set_cmd_completer (cmds.set, integer_unlimited_completer);
1154
1155   return cmds;
1156 }
1157
1158 /* Add element named NAME to both the set and show command LISTs (the
1159    list for set/show or some sublist thereof).  CLASS is as in
1160    add_cmd.  VAR is address of the variable which will contain the
1161    value.  SET_DOC and SHOW_DOC are the documentation strings.  */
1162
1163 set_show_commands
1164 add_setshow_zuinteger_cmd (const char *name, enum command_class theclass,
1165                            unsigned int *var,
1166                            const char *set_doc, const char *show_doc,
1167                            const char *help_doc,
1168                            cmd_func_ftype *set_func,
1169                            show_value_ftype *show_func,
1170                            struct cmd_list_element **set_list,
1171                            struct cmd_list_element **show_list)
1172 {
1173   return add_setshow_cmd_full<unsigned int> (name, theclass, var_zuinteger,
1174                                              var, set_doc, show_doc, help_doc,
1175                                              nullptr, nullptr, set_func,
1176                                              show_func, set_list, show_list);
1177 }
1178
1179 /* Same as above but using a getter and a setter function instead of a pointer
1180    to a global storage buffer.  */
1181
1182 set_show_commands
1183 add_setshow_zuinteger_cmd (const char *name, command_class theclass,
1184                            const char *set_doc, const char *show_doc,
1185                            const char *help_doc,
1186                            setting_func_types<unsigned int>::set set_func,
1187                            setting_func_types<unsigned int>::get get_func,
1188                            show_value_ftype *show_func,
1189                            cmd_list_element **set_list,
1190                            cmd_list_element **show_list)
1191 {
1192   return add_setshow_cmd_full<unsigned int> (name, theclass, var_zuinteger,
1193                                              nullptr, set_doc, show_doc,
1194                                              help_doc, set_func, get_func,
1195                                              nullptr, show_func, set_list,
1196                                              show_list);
1197 }
1198
1199 /* Remove the command named NAME from the command list.  Return the list
1200    commands which were aliased to the deleted command.  The various *HOOKs are
1201    set to the pre- and post-hook commands for the deleted command.  If the
1202    command does not have a hook, the corresponding out parameter is set to
1203    NULL.  */
1204
1205 static cmd_list_element::aliases_list_type
1206 delete_cmd (const char *name, struct cmd_list_element **list,
1207             struct cmd_list_element **prehook,
1208             struct cmd_list_element **prehookee,
1209             struct cmd_list_element **posthook,
1210             struct cmd_list_element **posthookee)
1211 {
1212   struct cmd_list_element *iter;
1213   struct cmd_list_element **previous_chain_ptr;
1214   cmd_list_element::aliases_list_type aliases;
1215
1216   *prehook = NULL;
1217   *prehookee = NULL;
1218   *posthook = NULL;
1219   *posthookee = NULL;
1220   previous_chain_ptr = list;
1221
1222   for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
1223     {
1224       if (strcmp (iter->name, name) == 0)
1225         {
1226           if (iter->destroyer)
1227             iter->destroyer (iter, iter->context ());
1228
1229           if (iter->hookee_pre)
1230             iter->hookee_pre->hook_pre = 0;
1231           *prehook = iter->hook_pre;
1232           *prehookee = iter->hookee_pre;
1233           if (iter->hookee_post)
1234             iter->hookee_post->hook_post = 0;
1235           *posthook = iter->hook_post;
1236           *posthookee = iter->hookee_post;
1237
1238           /* Update the link.  */
1239           *previous_chain_ptr = iter->next;
1240
1241           aliases = std::move (iter->aliases);
1242
1243           /* If this command was an alias, remove it from the list of
1244              aliases.  */
1245           if (iter->is_alias ())
1246             {
1247               auto it = iter->alias_target->aliases.iterator_to (*iter);
1248               iter->alias_target->aliases.erase (it);
1249             }
1250
1251           delete iter;
1252
1253           /* We won't see another command with the same name.  */
1254           break;
1255         }
1256       else
1257         previous_chain_ptr = &iter->next;
1258     }
1259
1260   return aliases;
1261 }
1262 \f
1263 /* Shorthands to the commands above.  */
1264
1265 /* Add an element to the list of info subcommands.  */
1266
1267 struct cmd_list_element *
1268 add_info (const char *name, cmd_simple_func_ftype *fun, const char *doc)
1269 {
1270   return add_cmd (name, class_info, fun, doc, &infolist);
1271 }
1272
1273 /* Add an alias to the list of info subcommands.  */
1274
1275 cmd_list_element *
1276 add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
1277 {
1278   return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist);
1279 }
1280
1281 /* Add an element to the list of commands.  */
1282
1283 struct cmd_list_element *
1284 add_com (const char *name, enum command_class theclass,
1285          cmd_simple_func_ftype *fun,
1286          const char *doc)
1287 {
1288   return add_cmd (name, theclass, fun, doc, &cmdlist);
1289 }
1290
1291 /* Add an alias or abbreviation command to the list of commands.
1292    For aliases predefined by GDB (such as bt), THECLASS must be
1293    different of class_alias, as class_alias is used to identify
1294    user defined aliases.  */
1295
1296 cmd_list_element *
1297 add_com_alias (const char *name, cmd_list_element *target,
1298                command_class theclass, int abbrev_flag)
1299 {
1300   return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist);
1301 }
1302
1303 /* Add an element with a suppress notification to the list of commands.  */
1304
1305 struct cmd_list_element *
1306 add_com_suppress_notification (const char *name, enum command_class theclass,
1307                                cmd_simple_func_ftype *fun, const char *doc,
1308                                int *suppress_notification)
1309 {
1310   return add_cmd_suppress_notification (name, theclass, fun, doc,
1311                                         &cmdlist, suppress_notification);
1312 }
1313
1314 /* Print the prefix of C followed by name of C in title style.  */
1315
1316 static void
1317 fput_command_name_styled (const cmd_list_element &c, struct ui_file *stream)
1318 {
1319   std::string prefixname
1320     = c.prefix == nullptr ? "" : c.prefix->prefixname ();
1321
1322   fprintf_styled (stream, title_style.style (), "%s%s",
1323                   prefixname.c_str (), c.name);
1324 }
1325
1326 /* Print the definition of alias C using title style for alias
1327    and aliased command.  */
1328
1329 static void
1330 fput_alias_definition_styled (const cmd_list_element &c,
1331                               struct ui_file *stream)
1332 {
1333   gdb_assert (c.is_alias ());
1334   fputs_filtered ("  alias ", stream);
1335   fput_command_name_styled (c, stream);
1336   fprintf_filtered (stream, " = ");
1337   fput_command_name_styled (*c.alias_target, stream);
1338   fprintf_filtered (stream, " %s\n", c.default_args.c_str ());
1339 }
1340
1341 /* Print the definition of the aliases of CMD that have default args.  */
1342
1343 static void
1344 fput_aliases_definition_styled (const cmd_list_element &cmd,
1345                                 struct ui_file *stream)
1346 {
1347   for (const cmd_list_element &alias : cmd.aliases)
1348     if (!alias.cmd_deprecated && !alias.default_args.empty ())
1349       fput_alias_definition_styled (alias, stream);
1350 }
1351
1352
1353 /* If C has one or more aliases, style print the name of C and
1354    the name of its aliases, separated by commas.
1355    If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1356    If one or more names are printed, POSTFIX is printed after the last name.
1357 */
1358
1359 static void
1360 fput_command_names_styled (const cmd_list_element &c,
1361                            bool always_fput_c_name, const char *postfix,
1362                            struct ui_file *stream)
1363 {
1364   /* First, check if we are going to print something.  That is, either if
1365      ALWAYS_FPUT_C_NAME is true or if there exists at least one non-deprecated
1366      alias.  */
1367
1368   auto print_alias = [] (const cmd_list_element &alias)
1369     {
1370       return !alias.cmd_deprecated;
1371     };
1372
1373   bool print_something = always_fput_c_name;
1374   if (!print_something)
1375     for (const cmd_list_element &alias : c.aliases)
1376       {
1377         if (!print_alias (alias))
1378           continue;
1379
1380         print_something = true;
1381         break;
1382       }
1383
1384   if (print_something)
1385     fput_command_name_styled (c, stream);
1386
1387   for (const cmd_list_element &alias : c.aliases)
1388     {
1389       if (!print_alias (alias))
1390         continue;
1391
1392       fputs_filtered (", ", stream);
1393       wrap_here ("   ");
1394       fput_command_name_styled (alias, stream);
1395     }
1396
1397   if (print_something)
1398     fputs_filtered (postfix, stream);
1399 }
1400
1401 /* If VERBOSE, print the full help for command C and highlight the
1402    documentation parts matching HIGHLIGHT,
1403    otherwise print only one-line help for command C.  */
1404
1405 static void
1406 print_doc_of_command (const cmd_list_element &c, const char *prefix,
1407                       bool verbose, compiled_regex &highlight,
1408                       struct ui_file *stream)
1409 {
1410   /* When printing the full documentation, add a line to separate
1411      this documentation from the previous command help, in the likely
1412      case that apropos finds several commands.  */
1413   if (verbose)
1414     fputs_filtered ("\n", stream);
1415
1416   fput_command_names_styled (c, true,
1417                              verbose ? "" : " -- ", stream);
1418   if (verbose)
1419     {
1420       fputs_filtered ("\n", stream);
1421       fput_aliases_definition_styled (c, stream);
1422       fputs_highlighted (c.doc, highlight, stream);
1423       fputs_filtered ("\n", stream);
1424     }
1425   else
1426     {
1427       print_doc_line (stream, c.doc, false);
1428       fputs_filtered ("\n", stream);
1429       fput_aliases_definition_styled (c, stream);
1430     }
1431 }
1432
1433 /* Recursively walk the commandlist structures, and print out the
1434    documentation of commands that match our regex in either their
1435    name, or their documentation.
1436    If VERBOSE, prints the complete documentation and highlight the
1437    documentation parts matching REGEX, otherwise prints only
1438    the first line.
1439 */
1440 void
1441 apropos_cmd (struct ui_file *stream,
1442              struct cmd_list_element *commandlist,
1443              bool verbose, compiled_regex &regex, const char *prefix)
1444 {
1445   struct cmd_list_element *c;
1446   int returnvalue;
1447
1448   /* Walk through the commands.  */
1449   for (c=commandlist;c;c=c->next)
1450     {
1451       if (c->is_alias ())
1452         {
1453           /* Command aliases/abbreviations are skipped to ensure we print the
1454              doc of a command only once, when encountering the aliased
1455              command.  */
1456           continue;
1457         }
1458
1459       returnvalue = -1; /* Needed to avoid double printing.  */
1460       if (c->name != NULL)
1461         {
1462           size_t name_len = strlen (c->name);
1463
1464           /* Try to match against the name.  */
1465           returnvalue = regex.search (c->name, name_len, 0, name_len, NULL);
1466           if (returnvalue >= 0)
1467             print_doc_of_command (*c, prefix, verbose, regex, stream);
1468
1469           /* Try to match against the name of the aliases.  */
1470           for (const cmd_list_element &alias : c->aliases)
1471             {
1472               name_len = strlen (alias.name);
1473               returnvalue = regex.search (alias.name, name_len, 0, name_len, NULL);
1474               if (returnvalue >= 0)
1475                 {
1476                   print_doc_of_command (*c, prefix, verbose, regex, stream);
1477                   break;
1478                 }
1479             }
1480         }
1481       if (c->doc != NULL && returnvalue < 0)
1482         {
1483           size_t doc_len = strlen (c->doc);
1484
1485           /* Try to match against documentation.  */
1486           if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0)
1487             print_doc_of_command (*c, prefix, verbose, regex, stream);
1488         }
1489       /* Check if this command has subcommands.  */
1490       if (c->is_prefix ())
1491         {
1492           /* Recursively call ourselves on the subcommand list,
1493              passing the right prefix in.  */
1494           apropos_cmd (stream, *c->subcommands, verbose, regex,
1495                        c->prefixname ().c_str ());
1496         }
1497     }
1498 }
1499
1500 /* This command really has to deal with two things:
1501    1) I want documentation on *this string* (usually called by
1502       "help commandname").
1503
1504    2) I want documentation on *this list* (usually called by giving a
1505       command that requires subcommands.  Also called by saying just
1506       "help".)
1507
1508    I am going to split this into two separate commands, help_cmd and
1509    help_list.  */
1510
1511 void
1512 help_cmd (const char *command, struct ui_file *stream)
1513 {
1514   struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd;
1515
1516   if (!command)
1517     {
1518       help_list (cmdlist, "", all_classes, stream);
1519       return;
1520     }
1521
1522   if (strcmp (command, "all") == 0)
1523     {
1524       help_all (stream);
1525       return;
1526     }
1527
1528   const char *orig_command = command;
1529   c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0);
1530
1531   if (c == 0)
1532     return;
1533
1534   lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd);
1535
1536   /* There are three cases here.
1537      If c->subcommands is nonzero, we have a prefix command.
1538      Print its documentation, then list its subcommands.
1539
1540      If c->func is non NULL, we really have a command.  Print its
1541      documentation and return.
1542
1543      If c->func is NULL, we have a class name.  Print its
1544      documentation (as if it were a command) and then set class to the
1545      number of this class so that the commands in the class will be
1546      listed.  */
1547
1548   /* If the user asked 'help somecommand' and there is no alias,
1549      the false indicates to not output the (single) command name.  */
1550   fput_command_names_styled (*c, false, "\n", stream);
1551   fput_aliases_definition_styled (*c, stream);
1552   fputs_filtered (c->doc, stream);
1553   fputs_filtered ("\n", stream);
1554
1555   if (!c->is_prefix () && !c->is_command_class_help ())
1556     return;
1557
1558   fprintf_filtered (stream, "\n");
1559
1560   /* If this is a prefix command, print it's subcommands.  */
1561   if (c->is_prefix ())
1562     help_list (*c->subcommands, c->prefixname ().c_str (),
1563                all_commands, stream);
1564
1565   /* If this is a class name, print all of the commands in the class.  */
1566   if (c->is_command_class_help ())
1567     help_list (cmdlist, "", c->theclass, stream);
1568
1569   if (c->hook_pre || c->hook_post)
1570     fprintf_filtered (stream,
1571                       "\nThis command has a hook (or hooks) defined:\n");
1572
1573   if (c->hook_pre)
1574     fprintf_filtered (stream,
1575                       "\tThis command is run after  : %s (pre hook)\n",
1576                     c->hook_pre->name);
1577   if (c->hook_post)
1578     fprintf_filtered (stream,
1579                       "\tThis command is run before : %s (post hook)\n",
1580                     c->hook_post->name);
1581 }
1582
1583 /*
1584  * Get a specific kind of help on a command list.
1585  *
1586  * LIST is the list.
1587  * CMDTYPE is the prefix to use in the title string.
1588  * CLASS is the class with which to list the nodes of this list (see
1589  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
1590  * everything, ALL_CLASSES for just classes, and non-negative for only things
1591  * in a specific class.
1592  * and STREAM is the output stream on which to print things.
1593  * If you call this routine with a class >= 0, it recurses.
1594  */
1595 void
1596 help_list (struct cmd_list_element *list, const char *cmdtype,
1597            enum command_class theclass, struct ui_file *stream)
1598 {
1599   int len;
1600   char *cmdtype1, *cmdtype2;
1601
1602   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1603    */
1604   len = strlen (cmdtype);
1605   cmdtype1 = (char *) alloca (len + 1);
1606   cmdtype1[0] = 0;
1607   cmdtype2 = (char *) alloca (len + 4);
1608   cmdtype2[0] = 0;
1609   if (len)
1610     {
1611       cmdtype1[0] = ' ';
1612       memcpy (cmdtype1 + 1, cmdtype, len - 1);
1613       cmdtype1[len] = 0;
1614       memcpy (cmdtype2, cmdtype, len - 1);
1615       strcpy (cmdtype2 + len - 1, " sub");
1616     }
1617
1618   if (theclass == all_classes)
1619     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
1620   else
1621     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
1622
1623   help_cmd_list (list, theclass, theclass >= 0, stream);
1624
1625   if (theclass == all_classes)
1626     {
1627       fprintf_filtered (stream, "\n\
1628 Type \"help%s\" followed by a class name for a list of commands in ",
1629                         cmdtype1);
1630       wrap_here ("");
1631       fprintf_filtered (stream, "that class.");
1632
1633       fprintf_filtered (stream, "\n\
1634 Type \"help all\" for the list of all commands.");
1635     }
1636
1637   fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
1638                     cmdtype1, cmdtype2);
1639   wrap_here ("");
1640   fputs_filtered ("for ", stream);
1641   wrap_here ("");
1642   fputs_filtered ("full ", stream);
1643   wrap_here ("");
1644   fputs_filtered ("documentation.\n", stream);
1645   fputs_filtered ("Type \"apropos word\" to search "
1646                   "for commands related to \"word\".\n", stream);
1647   fputs_filtered ("Type \"apropos -v word\" for full documentation", stream);
1648   wrap_here ("");
1649   fputs_filtered (" of commands related to \"word\".\n", stream);
1650   fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1651                   stream);
1652 }
1653
1654 static void
1655 help_all (struct ui_file *stream)
1656 {
1657   struct cmd_list_element *c;
1658   int seen_unclassified = 0;
1659
1660   for (c = cmdlist; c; c = c->next)
1661     {
1662       if (c->abbrev_flag)
1663         continue;
1664       /* If this is a class name, print all of the commands in the
1665          class.  */
1666
1667       if (c->is_command_class_help ())
1668         {
1669           fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
1670           help_cmd_list (cmdlist, c->theclass, true, stream);
1671         }
1672     }
1673
1674   /* While it's expected that all commands are in some class,
1675      as a safety measure, we'll print commands outside of any
1676      class at the end.  */
1677
1678   for (c = cmdlist; c; c = c->next)
1679     {
1680       if (c->abbrev_flag)
1681         continue;
1682
1683       if (c->theclass == no_class)
1684         {
1685           if (!seen_unclassified)
1686             {
1687               fprintf_filtered (stream, "\nUnclassified commands\n\n");
1688               seen_unclassified = 1;
1689             }
1690           print_help_for_command (*c, true, stream);
1691         }
1692     }
1693
1694 }
1695
1696 /* See cli-decode.h.  */
1697
1698 void
1699 print_doc_line (struct ui_file *stream, const char *str,
1700                 bool for_value_prefix)
1701 {
1702   static char *line_buffer = 0;
1703   static int line_size;
1704   const char *p;
1705
1706   if (!line_buffer)
1707     {
1708       line_size = 80;
1709       line_buffer = (char *) xmalloc (line_size);
1710     }
1711
1712   /* Searches for the first end of line or the end of STR.  */
1713   p = str;
1714   while (*p && *p != '\n')
1715     p++;
1716   if (p - str > line_size - 1)
1717     {
1718       line_size = p - str + 1;
1719       xfree (line_buffer);
1720       line_buffer = (char *) xmalloc (line_size);
1721     }
1722   strncpy (line_buffer, str, p - str);
1723   if (for_value_prefix)
1724     {
1725       if (islower (line_buffer[0]))
1726         line_buffer[0] = toupper (line_buffer[0]);
1727       gdb_assert (p > str);
1728       if (line_buffer[p - str - 1] == '.')
1729         line_buffer[p - str - 1] = '\0';
1730       else
1731         line_buffer[p - str] = '\0';
1732     }
1733   else
1734     line_buffer[p - str] = '\0';
1735   fputs_filtered (line_buffer, stream);
1736 }
1737
1738 /* Print one-line help for command C.
1739    If RECURSE is non-zero, also print one-line descriptions
1740    of all prefixed subcommands.  */
1741 static void
1742 print_help_for_command (const cmd_list_element &c,
1743                         bool recurse, struct ui_file *stream)
1744 {
1745   fput_command_names_styled (c, true, " -- ", stream);
1746   print_doc_line (stream, c.doc, false);
1747   fputs_filtered ("\n", stream);
1748   if (!c.default_args.empty ())
1749     fput_alias_definition_styled (c, stream);
1750   fput_aliases_definition_styled (c, stream);
1751
1752   if (recurse
1753       && c.is_prefix ()
1754       && c.abbrev_flag == 0)
1755     /* Subcommands of a prefix command typically have 'all_commands'
1756        as class.  If we pass CLASS to recursive invocation,
1757        most often we won't see anything.  */
1758     help_cmd_list (*c.subcommands, all_commands, true, stream);
1759 }
1760
1761 /*
1762  * Implement a help command on command list LIST.
1763  * RECURSE should be non-zero if this should be done recursively on
1764  * all sublists of LIST.
1765  * STREAM is the stream upon which the output should be written.
1766  * THECLASS should be:
1767  *      A non-negative class number to list only commands in that
1768  *      ALL_COMMANDS to list all commands in list.
1769  *      ALL_CLASSES  to list all classes in list.
1770  *
1771  *   Note that aliases are only shown when THECLASS is class_alias.
1772  *   In the other cases, the aliases will be shown together with their
1773  *   aliased command.
1774  *
1775  *   Note that RECURSE will be active on *all* sublists, not just the
1776  * ones selected by the criteria above (ie. the selection mechanism
1777  * is at the low level, not the high-level).
1778  */
1779
1780 static void
1781 help_cmd_list (struct cmd_list_element *list, enum command_class theclass,
1782                bool recurse, struct ui_file *stream)
1783 {
1784   struct cmd_list_element *c;
1785
1786   for (c = list; c; c = c->next)
1787     {
1788       if (c->abbrev_flag == 1 || c->cmd_deprecated)
1789         {
1790           /* Do not show abbreviations or deprecated commands.  */
1791           continue;
1792         }
1793
1794       if (c->is_alias () && theclass != class_alias)
1795         {
1796           /* Do not show an alias, unless specifically showing the
1797              list of aliases:  for all other classes, an alias is
1798              shown (if needed) together with its aliased command.  */
1799           continue;
1800         }
1801
1802       if (theclass == all_commands
1803           || (theclass == all_classes && c->is_command_class_help ())
1804           || (theclass == c->theclass && !c->is_command_class_help ()))
1805         {
1806           /* show C when
1807              - showing all commands
1808              - showing all classes and C is a help class
1809              - showing commands of THECLASS and C is not the help class  */
1810
1811           /* If we show the class_alias and C is an alias, do not recurse,
1812              as this would show the (possibly very long) not very useful
1813              list of sub-commands of the aliased command.  */
1814           print_help_for_command
1815             (*c,
1816              recurse && (theclass != class_alias || !c->is_alias ()),
1817              stream);
1818           continue;
1819         }
1820
1821       if (recurse
1822           && (theclass == class_user || theclass == class_alias)
1823           && c->is_prefix ())
1824         {
1825           /* User-defined commands or aliases may be subcommands.  */
1826           help_cmd_list (*c->subcommands, theclass, recurse, stream);
1827           continue;
1828         }
1829
1830       /* Do not show C or recurse on C, e.g. because C does not belong to
1831          THECLASS or because C is a help class.  */
1832     }
1833 }
1834 \f
1835
1836 /* Search the input clist for 'command'.  Return the command if
1837    found (or NULL if not), and return the number of commands
1838    found in nfound.  */
1839
1840 static struct cmd_list_element *
1841 find_cmd (const char *command, int len, struct cmd_list_element *clist,
1842           int ignore_help_classes, int *nfound)
1843 {
1844   struct cmd_list_element *found, *c;
1845
1846   found = NULL;
1847   *nfound = 0;
1848   for (c = clist; c; c = c->next)
1849     if (!strncmp (command, c->name, len)
1850         && (!ignore_help_classes || !c->is_command_class_help ()))
1851       {
1852         found = c;
1853         (*nfound)++;
1854         if (c->name[len] == '\0')
1855           {
1856             *nfound = 1;
1857             break;
1858           }
1859       }
1860   return found;
1861 }
1862
1863 /* Return the length of command name in TEXT.  */
1864
1865 int
1866 find_command_name_length (const char *text)
1867 {
1868   const char *p = text;
1869
1870   /* Treating underscores as part of command words is important
1871      so that "set args_foo()" doesn't get interpreted as
1872      "set args _foo()".  */
1873   /* Some characters are only used for TUI specific commands.
1874      However, they are always allowed for the sake of consistency.
1875
1876      Note that this is larger than the character set allowed when
1877      creating user-defined commands.  */
1878
1879   /* Recognize the single character commands so that, e.g., "!ls"
1880      works as expected.  */
1881   if (*p == '!' || *p == '|')
1882     return 1;
1883
1884   while (valid_cmd_char_p (*p)
1885          /* Characters used by TUI specific commands.  */
1886          || *p == '+' || *p == '<' || *p == '>' || *p == '$')
1887     p++;
1888
1889   return p - text;
1890 }
1891
1892 /* See command.h.  */
1893
1894 bool
1895 valid_cmd_char_p (int c)
1896 {
1897   /* Alas "42" is a legitimate user-defined command.
1898      In the interests of not breaking anything we preserve that.  */
1899
1900   return isalnum (c) || c == '-' || c == '_' || c == '.';
1901 }
1902
1903 /* See command.h.  */
1904
1905 bool
1906 valid_user_defined_cmd_name_p (const char *name)
1907 {
1908   const char *p;
1909
1910   if (*name == '\0')
1911     return false;
1912
1913   for (p = name; *p != '\0'; ++p)
1914     {
1915       if (valid_cmd_char_p (*p))
1916         ; /* Ok.  */
1917       else
1918         return false;
1919     }
1920
1921   return true;
1922 }
1923
1924 /* See command.h.  */
1925
1926 struct cmd_list_element *
1927 lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
1928               struct cmd_list_element **result_list, std::string *default_args,
1929               int ignore_help_classes, bool lookup_for_completion_p)
1930 {
1931   char *command;
1932   int len, nfound;
1933   struct cmd_list_element *found, *c;
1934   bool found_alias = false;
1935   const char *line = *text;
1936
1937   while (**text == ' ' || **text == '\t')
1938     (*text)++;
1939
1940   /* Identify the name of the command.  */
1941   len = find_command_name_length (*text);
1942
1943   /* If nothing but whitespace, return 0.  */
1944   if (len == 0)
1945     return 0;
1946
1947   /* *text and p now bracket the first command word to lookup (and
1948      it's length is len).  We copy this into a local temporary.  */
1949
1950
1951   command = (char *) alloca (len + 1);
1952   memcpy (command, *text, len);
1953   command[len] = '\0';
1954
1955   /* Look it up.  */
1956   found = 0;
1957   nfound = 0;
1958   found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1959
1960   /* If nothing matches, we have a simple failure.  */
1961   if (nfound == 0)
1962     return 0;
1963
1964   if (nfound > 1)
1965     {
1966       if (result_list != nullptr)
1967         /* Will be modified in calling routine
1968            if we know what the prefix command is.  */
1969         *result_list = 0;
1970       if (default_args != nullptr)
1971         *default_args = std::string ();
1972       return CMD_LIST_AMBIGUOUS;        /* Ambiguous.  */
1973     }
1974
1975   /* We've matched something on this list.  Move text pointer forward.  */
1976
1977   *text += len;
1978
1979   if (found->is_alias ())
1980     {
1981       /* We drop the alias (abbreviation) in favor of the command it
1982        is pointing to.  If the alias is deprecated, though, we need to
1983        warn the user about it before we drop it.  Note that while we
1984        are warning about the alias, we may also warn about the command
1985        itself and we will adjust the appropriate DEPRECATED_WARN_USER
1986        flags.  */
1987
1988       if (found->deprecated_warn_user && !lookup_for_completion_p)
1989         deprecated_cmd_warning (line, clist);
1990
1991
1992       /* Return the default_args of the alias, not the default_args
1993          of the command it is pointing to.  */
1994       if (default_args != nullptr)
1995         *default_args = found->default_args;
1996       found = found->alias_target;
1997       found_alias = true;
1998     }
1999   /* If we found a prefix command, keep looking.  */
2000
2001   if (found->is_prefix ())
2002     {
2003       c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args,
2004                         ignore_help_classes, lookup_for_completion_p);
2005       if (!c)
2006         {
2007           /* Didn't find anything; this is as far as we got.  */
2008           if (result_list != nullptr)
2009             *result_list = clist;
2010           if (!found_alias && default_args != nullptr)
2011             *default_args = found->default_args;
2012           return found;
2013         }
2014       else if (c == CMD_LIST_AMBIGUOUS)
2015         {
2016           /* We've gotten this far properly, but the next step is
2017              ambiguous.  We need to set the result list to the best
2018              we've found (if an inferior hasn't already set it).  */
2019           if (result_list != nullptr)
2020             if (!*result_list)
2021               /* This used to say *result_list = *found->subcommands.
2022                  If that was correct, need to modify the documentation
2023                  at the top of this function to clarify what is
2024                  supposed to be going on.  */
2025               *result_list = found;
2026           /* For ambiguous commands, do not return any default_args args.  */
2027           if (default_args != nullptr)
2028             *default_args = std::string ();
2029           return c;
2030         }
2031       else
2032         {
2033           /* We matched!  */
2034           return c;
2035         }
2036     }
2037   else
2038     {
2039       if (result_list != nullptr)
2040         *result_list = clist;
2041       if (!found_alias && default_args != nullptr)
2042         *default_args = found->default_args;
2043       return found;
2044     }
2045 }
2046
2047 /* All this hair to move the space to the front of cmdtype */
2048
2049 static void
2050 undef_cmd_error (const char *cmdtype, const char *q)
2051 {
2052   error (_("Undefined %scommand: \"%s\".  Try \"help%s%.*s\"."),
2053          cmdtype,
2054          q,
2055          *cmdtype ? " " : "",
2056          (int) strlen (cmdtype) - 1,
2057          cmdtype);
2058 }
2059
2060 /* Look up the contents of *LINE as a command in the command list LIST.
2061    LIST is a chain of struct cmd_list_element's.
2062    If it is found, return the struct cmd_list_element for that command,
2063    update *LINE to point after the command name, at the first argument
2064    and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
2065    args to prepend to the user provided args when running the command.
2066    Note that if the found cmd_list_element is found via an alias,
2067    the default args of the alias are returned.
2068
2069    If not found, call error if ALLOW_UNKNOWN is zero
2070    otherwise (or if error returns) return zero.
2071    Call error if specified command is ambiguous,
2072    unless ALLOW_UNKNOWN is negative.
2073    CMDTYPE precedes the word "command" in the error message.
2074
2075    If IGNORE_HELP_CLASSES is nonzero, ignore any command list
2076    elements which are actually help classes rather than commands (i.e.
2077    the function field of the struct cmd_list_element is 0).  */
2078
2079 struct cmd_list_element *
2080 lookup_cmd (const char **line, struct cmd_list_element *list,
2081             const char *cmdtype,
2082             std::string *default_args,
2083             int allow_unknown, int ignore_help_classes)
2084 {
2085   struct cmd_list_element *last_list = 0;
2086   struct cmd_list_element *c;
2087
2088   /* Note: Do not remove trailing whitespace here because this
2089      would be wrong for complete_command.  Jim Kingdon  */
2090
2091   if (!*line)
2092     error (_("Lack of needed %scommand"), cmdtype);
2093
2094   c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes);
2095
2096   if (!c)
2097     {
2098       if (!allow_unknown)
2099         {
2100           char *q;
2101           int len = find_command_name_length (*line);
2102
2103           q = (char *) alloca (len + 1);
2104           strncpy (q, *line, len);
2105           q[len] = '\0';
2106           undef_cmd_error (cmdtype, q);
2107         }
2108       else
2109         return 0;
2110     }
2111   else if (c == CMD_LIST_AMBIGUOUS)
2112     {
2113       /* Ambigous.  Local values should be off subcommands or called
2114          values.  */
2115       int local_allow_unknown = (last_list ? last_list->allow_unknown :
2116                                  allow_unknown);
2117       std::string local_cmdtype
2118         = last_list ? last_list->prefixname () : cmdtype;
2119       struct cmd_list_element *local_list =
2120         (last_list ? *(last_list->subcommands) : list);
2121
2122       if (local_allow_unknown < 0)
2123         {
2124           if (last_list)
2125             return last_list;   /* Found something.  */
2126           else
2127             return 0;           /* Found nothing.  */
2128         }
2129       else
2130         {
2131           /* Report as error.  */
2132           int amb_len;
2133           char ambbuf[100];
2134
2135           for (amb_len = 0;
2136                ((*line)[amb_len] && (*line)[amb_len] != ' '
2137                 && (*line)[amb_len] != '\t');
2138                amb_len++)
2139             ;
2140
2141           ambbuf[0] = 0;
2142           for (c = local_list; c; c = c->next)
2143             if (!strncmp (*line, c->name, amb_len))
2144               {
2145                 if (strlen (ambbuf) + strlen (c->name) + 6
2146                     < (int) sizeof ambbuf)
2147                   {
2148                     if (strlen (ambbuf))
2149                       strcat (ambbuf, ", ");
2150                     strcat (ambbuf, c->name);
2151                   }
2152                 else
2153                   {
2154                     strcat (ambbuf, "..");
2155                     break;
2156                   }
2157               }
2158           error (_("Ambiguous %scommand \"%s\": %s."),
2159                  local_cmdtype.c_str (), *line, ambbuf);
2160         }
2161     }
2162   else
2163     {
2164       if (c->type == set_cmd && **line != '\0' && !isspace (**line))
2165         error (_("Argument must be preceded by space."));
2166
2167       /* We've got something.  It may still not be what the caller
2168          wants (if this command *needs* a subcommand).  */
2169       while (**line == ' ' || **line == '\t')
2170         (*line)++;
2171
2172       if (c->is_prefix () && **line && !c->allow_unknown)
2173         undef_cmd_error (c->prefixname ().c_str (), *line);
2174
2175       /* Seems to be what he wants.  Return it.  */
2176       return c;
2177     }
2178   return 0;
2179 }
2180
2181 /* See command.h.  */
2182
2183 struct cmd_list_element *
2184 lookup_cmd_exact (const char *name,
2185                   struct cmd_list_element *list,
2186                   bool ignore_help_classes)
2187 {
2188   const char *tem = name;
2189   struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
2190                                              ignore_help_classes);
2191   if (cmd != nullptr && strcmp (name, cmd->name) != 0)
2192     cmd = nullptr;
2193   return cmd;
2194 }
2195
2196 /* We are here presumably because an alias or command in TEXT is
2197    deprecated and a warning message should be generated.  This
2198    function decodes TEXT and potentially generates a warning message
2199    as outlined below.
2200    
2201    Example for 'set endian big' which has a fictitious alias 'seb'.
2202    
2203    If alias wasn't used in TEXT, and the command is deprecated:
2204    "warning: 'set endian big' is deprecated." 
2205    
2206    If alias was used, and only the alias is deprecated:
2207    "warning: 'seb' an alias for the command 'set endian big' is deprecated."
2208    
2209    If alias was used and command is deprecated (regardless of whether
2210    the alias itself is deprecated:
2211    
2212    "warning: 'set endian big' (seb) is deprecated."
2213
2214    After the message has been sent, clear the appropriate flags in the
2215    command and/or the alias so the user is no longer bothered.
2216    
2217 */
2218 void
2219 deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
2220 {
2221   struct cmd_list_element *alias = nullptr;
2222   struct cmd_list_element *cmd = nullptr;
2223
2224   /* Return if text doesn't evaluate to a command.  We place this lookup
2225      within its own scope so that the PREFIX_CMD local is not visible
2226      later in this function.  The value returned in PREFIX_CMD is based on
2227      the prefix found in TEXT, and is our case this prefix can be missing
2228      in some situations (when LIST is not the global CMDLIST).
2229
2230      It is better for our purposes to use the prefix commands directly from
2231      the ALIAS and CMD results.  */
2232   {
2233     struct cmd_list_element *prefix_cmd = nullptr;
2234     if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list))
2235       return;
2236   }
2237
2238   /* Return if nothing is deprecated.  */
2239   if (!((alias != nullptr ? alias->deprecated_warn_user : 0)
2240         || cmd->deprecated_warn_user))
2241     return;
2242
2243   /* Join command prefix (if any) and the command name.  */
2244   std::string tmp_cmd_str;
2245   if (cmd->prefix != nullptr)
2246     tmp_cmd_str += cmd->prefix->prefixname ();
2247   tmp_cmd_str += std::string (cmd->name);
2248
2249   /* Display the appropriate first line, this warns that the thing the user
2250      entered is deprecated.  */
2251   if (alias != nullptr)
2252     {
2253       /* Join the alias prefix (if any) and the alias name.  */
2254       std::string tmp_alias_str;
2255       if (alias->prefix != nullptr)
2256         tmp_alias_str += alias->prefix->prefixname ();
2257       tmp_alias_str += std::string (alias->name);
2258
2259       if (cmd->cmd_deprecated)
2260         printf_filtered (_("Warning: command '%ps' (%ps) is deprecated.\n"),
2261                          styled_string (title_style.style (),
2262                                         tmp_cmd_str.c_str ()),
2263                          styled_string (title_style.style (),
2264                                         tmp_alias_str.c_str ()));
2265       else
2266         printf_filtered (_("Warning: '%ps', an alias for the command '%ps', "
2267                            "is deprecated.\n"),
2268                          styled_string (title_style.style (),
2269                                         tmp_alias_str.c_str ()),
2270                          styled_string (title_style.style (),
2271                                         tmp_cmd_str.c_str ()));
2272     }
2273   else
2274     printf_filtered (_("Warning: command '%ps' is deprecated.\n"),
2275                      styled_string (title_style.style (),
2276                                     tmp_cmd_str.c_str ()));
2277
2278   /* Now display a second line indicating what the user should use instead.
2279      If it is only the alias that is deprecated, we want to indicate the
2280      new alias, otherwise we'll indicate the new command.  */
2281   const char *replacement;
2282   if (alias != nullptr && !cmd->cmd_deprecated)
2283     replacement = alias->replacement;
2284   else
2285     replacement = cmd->replacement;
2286   if (replacement != nullptr)
2287     printf_filtered (_("Use '%ps'.\n\n"),
2288                      styled_string (title_style.style (),
2289                                     replacement));
2290   else
2291     printf_filtered (_("No alternative known.\n\n"));
2292
2293   /* We've warned you, now we'll keep quiet.  */
2294   if (alias != nullptr)
2295     alias->deprecated_warn_user = 0;
2296   cmd->deprecated_warn_user = 0;
2297 }
2298
2299 /* Look up the contents of TEXT as a command in the command list CUR_LIST.
2300    Return 1 on success, 0 on failure.
2301
2302    If TEXT refers to an alias, *ALIAS will point to that alias.
2303
2304    If TEXT is a subcommand (i.e. one that is preceded by a prefix
2305    command) set *PREFIX_CMD.
2306
2307    Set *CMD to point to the command TEXT indicates.
2308
2309    If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2310    exist, they are NULL when we return.
2311
2312 */
2313
2314 static int
2315 lookup_cmd_composition_1 (const char *text,
2316                           struct cmd_list_element **alias,
2317                           struct cmd_list_element **prefix_cmd,
2318                           struct cmd_list_element **cmd,
2319                           struct cmd_list_element *cur_list)
2320 {
2321   *alias = nullptr;
2322   *prefix_cmd = cur_list->prefix;
2323   *cmd = nullptr;
2324
2325   text = skip_spaces (text);
2326
2327   /* Go through as many command lists as we need to, to find the command
2328      TEXT refers to.  */
2329   while (1)
2330     {
2331       /* Identify the name of the command.  */
2332       int len = find_command_name_length (text);
2333
2334       /* If nothing but whitespace, return.  */
2335       if (len == 0)
2336         return 0;
2337
2338       /* TEXT is the start of the first command word to lookup (and
2339          it's length is LEN).  We copy this into a local temporary.  */
2340       std::string command (text, len);
2341
2342       /* Look it up.  */
2343       int nfound = 0;
2344       *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
2345
2346       /* We only handle the case where a single command was found.  */
2347       if (*cmd == CMD_LIST_AMBIGUOUS || *cmd == nullptr)
2348         return 0;
2349       else
2350         {
2351           if ((*cmd)->is_alias ())
2352             {
2353               /* If the command was actually an alias, we note that an
2354                  alias was used (by assigning *ALIAS) and we set *CMD.  */
2355               *alias = *cmd;
2356               *cmd = (*cmd)->alias_target;
2357             }
2358         }
2359
2360       text += len;
2361       text = skip_spaces (text);
2362
2363       if ((*cmd)->is_prefix () && *text != '\0')
2364         {
2365           cur_list = *(*cmd)->subcommands;
2366           *prefix_cmd = *cmd;
2367         }
2368       else
2369         return 1;
2370     }
2371 }
2372
2373 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2374    Return 1 on success, 0 on failure.
2375
2376    If TEXT refers to an alias, *ALIAS will point to that alias.
2377
2378    If TEXT is a subcommand (i.e. one that is preceded by a prefix
2379    command) set *PREFIX_CMD.
2380
2381    Set *CMD to point to the command TEXT indicates.
2382
2383    If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2384    exist, they are NULL when we return.
2385
2386 */
2387
2388 int
2389 lookup_cmd_composition (const char *text,
2390                         struct cmd_list_element **alias,
2391                         struct cmd_list_element **prefix_cmd,
2392                         struct cmd_list_element **cmd)
2393 {
2394   return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist);
2395 }
2396
2397 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
2398
2399 /* Return a vector of char pointers which point to the different
2400    possible completions in LIST of TEXT.
2401
2402    WORD points in the same buffer as TEXT, and completions should be
2403    returned relative to this position.  For example, suppose TEXT is
2404    "foo" and we want to complete to "foobar".  If WORD is "oo", return
2405    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
2406
2407 void
2408 complete_on_cmdlist (struct cmd_list_element *list,
2409                      completion_tracker &tracker,
2410                      const char *text, const char *word,
2411                      int ignore_help_classes)
2412 {
2413   struct cmd_list_element *ptr;
2414   int textlen = strlen (text);
2415   int pass;
2416   int saw_deprecated_match = 0;
2417
2418   /* We do one or two passes.  In the first pass, we skip deprecated
2419      commands.  If we see no matching commands in the first pass, and
2420      if we did happen to see a matching deprecated command, we do
2421      another loop to collect those.  */
2422   for (pass = 0; pass < 2; ++pass)
2423     {
2424       bool got_matches = false;
2425
2426       for (ptr = list; ptr; ptr = ptr->next)
2427         if (!strncmp (ptr->name, text, textlen)
2428             && !ptr->abbrev_flag
2429             && (!ignore_help_classes || !ptr->is_command_class_help ()
2430                 || ptr->is_prefix ()))
2431           {
2432             if (pass == 0)
2433               {
2434                 if (ptr->cmd_deprecated)
2435                   {
2436                     saw_deprecated_match = 1;
2437                     continue;
2438                   }
2439               }
2440
2441             tracker.add_completion
2442               (make_completion_match_str (ptr->name, text, word));
2443             got_matches = true;
2444           }
2445
2446       if (got_matches)
2447         break;
2448
2449       /* If we saw no matching deprecated commands in the first pass,
2450          just bail out.  */
2451       if (!saw_deprecated_match)
2452         break;
2453     }
2454 }
2455
2456 /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
2457
2458 /* Add the different possible completions in ENUMLIST of TEXT.
2459
2460    WORD points in the same buffer as TEXT, and completions should be
2461    returned relative to this position.  For example, suppose TEXT is "foo"
2462    and we want to complete to "foobar".  If WORD is "oo", return
2463    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
2464
2465 void
2466 complete_on_enum (completion_tracker &tracker,
2467                   const char *const *enumlist,
2468                   const char *text, const char *word)
2469 {
2470   int textlen = strlen (text);
2471   int i;
2472   const char *name;
2473
2474   for (i = 0; (name = enumlist[i]) != NULL; i++)
2475     if (strncmp (name, text, textlen) == 0)
2476       tracker.add_completion (make_completion_match_str (name, text, word));
2477 }
2478
2479 /* Call the command function.  */
2480 void
2481 cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty)
2482 {
2483   if (!cmd->is_command_class_help ())
2484     {
2485       gdb::optional<scoped_restore_tmpl<int>> restore_suppress;
2486
2487       if (cmd->suppress_notification != NULL)
2488         restore_suppress.emplace (cmd->suppress_notification, 1);
2489
2490       cmd->func (args, from_tty, cmd);
2491     }
2492   else
2493     error (_("Invalid command"));
2494 }
2495
2496 int
2497 cli_user_command_p (struct cmd_list_element *cmd)
2498 {
2499   return cmd->theclass == class_user && cmd->func == do_simple_func;
2500 }
This page took 0.17916 seconds and 4 git commands to generate.