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