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