cli-decode.c revision 1.10 1 /* Handle lists of commands, their decoding and documentation, for GDB.
2
3 Copyright (C) 1986-2023 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 if (*text == '\0')
993 tracker.add_completion (make_unique_xstrdup ("NUMBER"));
994 complete_on_enum (tracker, keywords, text, word);
995 }
996
997 /* Add element named NAME to both the set and show command LISTs (the
998 list for set/show or some sublist thereof). CLASS is as in
999 add_cmd. VAR is address of the variable which will contain the
1000 value. SET_DOC and SHOW_DOC are the documentation strings. This
1001 function is only used in Python API. Please don't use it elsewhere. */
1002
1003 set_show_commands
1004 add_setshow_integer_cmd (const char *name, enum command_class theclass,
1005 int *var,
1006 const char *set_doc, const char *show_doc,
1007 const char *help_doc,
1008 cmd_func_ftype *set_func,
1009 show_value_ftype *show_func,
1010 struct cmd_list_element **set_list,
1011 struct cmd_list_element **show_list)
1012 {
1013 set_show_commands commands
1014 = add_setshow_cmd_full<int> (name, theclass, var_integer, var,
1015 set_doc, show_doc, help_doc,
1016 nullptr, nullptr, set_func,
1017 show_func, set_list, show_list);
1018
1019 set_cmd_completer (commands.set, integer_unlimited_completer);
1020
1021 return commands;
1022 }
1023
1024 /* Same as above but using a getter and a setter function instead of a pointer
1025 to a global storage buffer. */
1026
1027 set_show_commands
1028 add_setshow_integer_cmd (const char *name, command_class theclass,
1029 const char *set_doc, const char *show_doc,
1030 const char *help_doc,
1031 setting_func_types<int>::set set_func,
1032 setting_func_types<int>::get get_func,
1033 show_value_ftype *show_func,
1034 cmd_list_element **set_list,
1035 cmd_list_element **show_list)
1036 {
1037 auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
1038 set_doc, show_doc, help_doc, set_func,
1039 get_func, nullptr, show_func, set_list,
1040 show_list);
1041
1042 set_cmd_completer (cmds.set, integer_unlimited_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). CLASS 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. */
1051
1052 set_show_commands
1053 add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
1054 unsigned int *var,
1055 const char *set_doc, const char *show_doc,
1056 const char *help_doc,
1057 cmd_func_ftype *set_func,
1058 show_value_ftype *show_func,
1059 struct cmd_list_element **set_list,
1060 struct cmd_list_element **show_list)
1061 {
1062 set_show_commands commands
1063 = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var,
1064 set_doc, show_doc, help_doc,
1065 nullptr, nullptr, set_func,
1066 show_func, set_list, show_list);
1067
1068 set_cmd_completer (commands.set, integer_unlimited_completer);
1069
1070 return commands;
1071 }
1072
1073 /* Same as above but using a getter and a setter function instead of a pointer
1074 to a global storage buffer. */
1075
1076 set_show_commands
1077 add_setshow_uinteger_cmd (const char *name, command_class theclass,
1078 const char *set_doc, const char *show_doc,
1079 const char *help_doc,
1080 setting_func_types<unsigned int>::set set_func,
1081 setting_func_types<unsigned int>::get get_func,
1082 show_value_ftype *show_func,
1083 cmd_list_element **set_list,
1084 cmd_list_element **show_list)
1085 {
1086 auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
1087 nullptr, set_doc, show_doc,
1088 help_doc, set_func, get_func,
1089 nullptr, show_func, set_list,
1090 show_list);
1091
1092 set_cmd_completer (cmds.set, integer_unlimited_completer);
1093
1094 return cmds;
1095 }
1096
1097 /* Add element named NAME to both the set and show command LISTs (the
1098 list for set/show or some sublist thereof). CLASS is as in
1099 add_cmd. VAR is address of the variable which will contain the
1100 value. SET_DOC and SHOW_DOC are the documentation strings. */
1101
1102 set_show_commands
1103 add_setshow_zinteger_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 return add_setshow_cmd_full<int> (name, theclass, var_zinteger, var,
1113 set_doc, show_doc, help_doc,
1114 nullptr, nullptr, set_func,
1115 show_func, set_list, show_list);
1116 }
1117
1118 /* Same as above but using a getter and a setter function instead of a pointer
1119 to a global storage buffer. */
1120
1121 set_show_commands
1122 add_setshow_zinteger_cmd (const char *name, command_class theclass,
1123 const char *set_doc, const char *show_doc,
1124 const char *help_doc,
1125 setting_func_types<int>::set set_func,
1126 setting_func_types<int>::get get_func,
1127 show_value_ftype *show_func,
1128 cmd_list_element **set_list,
1129 cmd_list_element **show_list)
1130 {
1131 return add_setshow_cmd_full<int> (name, theclass, var_zinteger, nullptr,
1132 set_doc, show_doc, help_doc, set_func,
1133 get_func, nullptr, show_func, set_list,
1134 show_list);
1135 }
1136
1137 set_show_commands
1138 add_setshow_zuinteger_unlimited_cmd (const char *name,
1139 enum command_class theclass,
1140 int *var,
1141 const char *set_doc,
1142 const char *show_doc,
1143 const char *help_doc,
1144 cmd_func_ftype *set_func,
1145 show_value_ftype *show_func,
1146 struct cmd_list_element **set_list,
1147 struct cmd_list_element **show_list)
1148 {
1149 set_show_commands commands
1150 = add_setshow_cmd_full<int> (name, theclass, var_zuinteger_unlimited, var,
1151 set_doc, show_doc, help_doc, nullptr,
1152 nullptr, set_func, show_func, set_list,
1153 show_list);
1154
1155 set_cmd_completer (commands.set, integer_unlimited_completer);
1156
1157 return commands;
1158 }
1159
1160 /* Same as above but using a getter and a setter function instead of a pointer
1161 to a global storage buffer. */
1162
1163 set_show_commands
1164 add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass,
1165 const char *set_doc, const char *show_doc,
1166 const char *help_doc,
1167 setting_func_types<int>::set set_func,
1168 setting_func_types<int>::get get_func,
1169 show_value_ftype *show_func,
1170 cmd_list_element **set_list,
1171 cmd_list_element **show_list)
1172 {
1173 auto cmds
1174 = add_setshow_cmd_full<int> (name, theclass, var_zuinteger_unlimited,
1175 nullptr, set_doc, show_doc, help_doc, set_func,
1176 get_func, nullptr, show_func, set_list,
1177 show_list);
1178
1179 set_cmd_completer (cmds.set, integer_unlimited_completer);
1180
1181 return cmds;
1182 }
1183
1184 /* Add element named NAME to both the set and show command LISTs (the
1185 list for set/show or some sublist thereof). CLASS is as in
1186 add_cmd. VAR is address of the variable which will contain the
1187 value. SET_DOC and SHOW_DOC are the documentation strings. */
1188
1189 set_show_commands
1190 add_setshow_zuinteger_cmd (const char *name, enum command_class theclass,
1191 unsigned int *var,
1192 const char *set_doc, const char *show_doc,
1193 const char *help_doc,
1194 cmd_func_ftype *set_func,
1195 show_value_ftype *show_func,
1196 struct cmd_list_element **set_list,
1197 struct cmd_list_element **show_list)
1198 {
1199 return add_setshow_cmd_full<unsigned int> (name, theclass, var_zuinteger,
1200 var, set_doc, show_doc, help_doc,
1201 nullptr, nullptr, set_func,
1202 show_func, set_list, show_list);
1203 }
1204
1205 /* Same as above but using a getter and a setter function instead of a pointer
1206 to a global storage buffer. */
1207
1208 set_show_commands
1209 add_setshow_zuinteger_cmd (const char *name, command_class theclass,
1210 const char *set_doc, const char *show_doc,
1211 const char *help_doc,
1212 setting_func_types<unsigned int>::set set_func,
1213 setting_func_types<unsigned int>::get get_func,
1214 show_value_ftype *show_func,
1215 cmd_list_element **set_list,
1216 cmd_list_element **show_list)
1217 {
1218 return add_setshow_cmd_full<unsigned int> (name, theclass, var_zuinteger,
1219 nullptr, set_doc, show_doc,
1220 help_doc, set_func, get_func,
1221 nullptr, show_func, set_list,
1222 show_list);
1223 }
1224
1225 /* Remove the command named NAME from the command list. Return the list
1226 commands which were aliased to the deleted command. The various *HOOKs are
1227 set to the pre- and post-hook commands for the deleted command. If the
1228 command does not have a hook, the corresponding out parameter is set to
1229 NULL. */
1230
1231 static cmd_list_element::aliases_list_type
1232 delete_cmd (const char *name, struct cmd_list_element **list,
1233 struct cmd_list_element **prehook,
1234 struct cmd_list_element **prehookee,
1235 struct cmd_list_element **posthook,
1236 struct cmd_list_element **posthookee)
1237 {
1238 struct cmd_list_element *iter;
1239 struct cmd_list_element **previous_chain_ptr;
1240 cmd_list_element::aliases_list_type aliases;
1241
1242 *prehook = NULL;
1243 *prehookee = NULL;
1244 *posthook = NULL;
1245 *posthookee = NULL;
1246 previous_chain_ptr = list;
1247
1248 for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
1249 {
1250 if (strcmp (iter->name, name) == 0)
1251 {
1252 if (iter->destroyer)
1253 iter->destroyer (iter, iter->context ());
1254
1255 if (iter->hookee_pre)
1256 iter->hookee_pre->hook_pre = 0;
1257 *prehook = iter->hook_pre;
1258 *prehookee = iter->hookee_pre;
1259 if (iter->hookee_post)
1260 iter->hookee_post->hook_post = 0;
1261 *posthook = iter->hook_post;
1262 *posthookee = iter->hookee_post;
1263
1264 /* Update the link. */
1265 *previous_chain_ptr = iter->next;
1266
1267 aliases = std::move (iter->aliases);
1268
1269 /* If this command was an alias, remove it from the list of
1270 aliases. */
1271 if (iter->is_alias ())
1272 {
1273 auto it = iter->alias_target->aliases.iterator_to (*iter);
1274 iter->alias_target->aliases.erase (it);
1275 }
1276
1277 delete iter;
1278
1279 /* We won't see another command with the same name. */
1280 break;
1281 }
1282 else
1283 previous_chain_ptr = &iter->next;
1284 }
1285
1286 return aliases;
1287 }
1288
1289 /* Shorthands to the commands above. */
1291
1292 /* Add an element to the list of info subcommands. */
1293
1294 struct cmd_list_element *
1295 add_info (const char *name, cmd_simple_func_ftype *fun, const char *doc)
1296 {
1297 return add_cmd (name, class_info, fun, doc, &infolist);
1298 }
1299
1300 /* Add an alias to the list of info subcommands. */
1301
1302 cmd_list_element *
1303 add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
1304 {
1305 return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist);
1306 }
1307
1308 /* Add an element to the list of commands. */
1309
1310 struct cmd_list_element *
1311 add_com (const char *name, enum command_class theclass,
1312 cmd_simple_func_ftype *fun,
1313 const char *doc)
1314 {
1315 return add_cmd (name, theclass, fun, doc, &cmdlist);
1316 }
1317
1318 /* Add an alias or abbreviation command to the list of commands.
1319 For aliases predefined by GDB (such as bt), THECLASS must be
1320 different of class_alias, as class_alias is used to identify
1321 user defined aliases. */
1322
1323 cmd_list_element *
1324 add_com_alias (const char *name, cmd_list_element *target,
1325 command_class theclass, int abbrev_flag)
1326 {
1327 return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist);
1328 }
1329
1330 /* Add an element with a suppress notification to the list of commands. */
1331
1332 struct cmd_list_element *
1333 add_com_suppress_notification (const char *name, enum command_class theclass,
1334 cmd_simple_func_ftype *fun, const char *doc,
1335 bool *suppress_notification)
1336 {
1337 return add_cmd_suppress_notification (name, theclass, fun, doc,
1338 &cmdlist, suppress_notification);
1339 }
1340
1341 /* Print the prefix of C followed by name of C in title style. */
1342
1343 static void
1344 fput_command_name_styled (const cmd_list_element &c, struct ui_file *stream)
1345 {
1346 std::string prefixname
1347 = c.prefix == nullptr ? "" : c.prefix->prefixname ();
1348
1349 fprintf_styled (stream, title_style.style (), "%s%s",
1350 prefixname.c_str (), c.name);
1351 }
1352
1353 /* True if ALIAS has a user-defined documentation. */
1354
1355 static bool
1356 user_documented_alias (const cmd_list_element &alias)
1357 {
1358 gdb_assert (alias.is_alias ());
1359 /* Alias is user documented if it has an allocated documentation
1360 that differs from the aliased command. */
1361 return (alias.doc_allocated
1362 && strcmp (alias.doc, alias.alias_target->doc) != 0);
1363 }
1364
1365 /* Print the definition of alias C using title style for alias
1366 and aliased command. */
1367
1368 static void
1369 fput_alias_definition_styled (const cmd_list_element &c,
1370 struct ui_file *stream)
1371 {
1372 gdb_assert (c.is_alias ());
1373 gdb_puts (" alias ", stream);
1374 fput_command_name_styled (c, stream);
1375 gdb_printf (stream, " = ");
1376 fput_command_name_styled (*c.alias_target, stream);
1377 gdb_printf (stream, " %s\n", c.default_args.c_str ());
1378 }
1379
1380 /* Print the definition of CMD aliases not deprecated and having default args
1381 and not specifically documented by the user. */
1382
1383 static void
1384 fput_aliases_definition_styled (const cmd_list_element &cmd,
1385 struct ui_file *stream)
1386 {
1387 for (const cmd_list_element &alias : cmd.aliases)
1388 if (!alias.cmd_deprecated
1389 && !user_documented_alias (alias)
1390 && !alias.default_args.empty ())
1391 fput_alias_definition_styled (alias, stream);
1392 }
1393
1394 /* If C has one or more aliases, style print the name of C and the name of its
1395 aliases not documented specifically by the user, separated by commas.
1396 If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1397 If one or more names are printed, POSTFIX is printed after the last name.
1398 */
1399
1400 static void
1401 fput_command_names_styled (const cmd_list_element &c,
1402 bool always_fput_c_name, const char *postfix,
1403 struct ui_file *stream)
1404 {
1405 /* First, check if we are going to print something. That is, either if
1406 ALWAYS_FPUT_C_NAME is true or if there exists at least one non-deprecated
1407 alias not documented specifically by the user. */
1408
1409 auto print_alias = [] (const cmd_list_element &alias)
1410 {
1411 return !alias.cmd_deprecated && !user_documented_alias (alias);
1412 };
1413
1414 bool print_something = always_fput_c_name;
1415 if (!print_something)
1416 for (const cmd_list_element &alias : c.aliases)
1417 {
1418 if (!print_alias (alias))
1419 continue;
1420
1421 print_something = true;
1422 break;
1423 }
1424
1425 if (print_something)
1426 fput_command_name_styled (c, stream);
1427
1428 for (const cmd_list_element &alias : c.aliases)
1429 {
1430 if (!print_alias (alias))
1431 continue;
1432
1433 gdb_puts (", ", stream);
1434 stream->wrap_here (3);
1435 fput_command_name_styled (alias, stream);
1436 }
1437
1438 if (print_something)
1439 gdb_puts (postfix, stream);
1440 }
1441
1442 /* If VERBOSE, print the full help for command C and highlight the
1443 documentation parts matching HIGHLIGHT,
1444 otherwise print only one-line help for command C. */
1445
1446 static void
1447 print_doc_of_command (const cmd_list_element &c, const char *prefix,
1448 bool verbose, compiled_regex &highlight,
1449 struct ui_file *stream)
1450 {
1451 /* When printing the full documentation, add a line to separate
1452 this documentation from the previous command help, in the likely
1453 case that apropos finds several commands. */
1454 if (verbose)
1455 gdb_puts ("\n", stream);
1456
1457 fput_command_names_styled (c, true,
1458 verbose ? "" : " -- ", stream);
1459 if (verbose)
1460 {
1461 gdb_puts ("\n", stream);
1462 fput_aliases_definition_styled (c, stream);
1463 fputs_highlighted (c.doc, highlight, stream);
1464 gdb_puts ("\n", stream);
1465 }
1466 else
1467 {
1468 print_doc_line (stream, c.doc, false);
1469 gdb_puts ("\n", stream);
1470 fput_aliases_definition_styled (c, stream);
1471 }
1472 }
1473
1474 /* Recursively walk the commandlist structures, and print out the
1475 documentation of commands that match our regex in either their
1476 name, or their documentation.
1477 If VERBOSE, prints the complete documentation and highlight the
1478 documentation parts matching REGEX, otherwise prints only
1479 the first line.
1480 */
1481 void
1482 apropos_cmd (struct ui_file *stream,
1483 struct cmd_list_element *commandlist,
1484 bool verbose, compiled_regex ®ex, const char *prefix)
1485 {
1486 struct cmd_list_element *c;
1487 int returnvalue;
1488
1489 /* Walk through the commands. */
1490 for (c=commandlist;c;c=c->next)
1491 {
1492 if (c->is_alias () && !user_documented_alias (*c))
1493 {
1494 /* Command aliases/abbreviations not specifically documented by the
1495 user are skipped to ensure we print the doc of a command only once,
1496 when encountering the aliased command. */
1497 continue;
1498 }
1499
1500 returnvalue = -1; /* Needed to avoid double printing. */
1501 if (c->name != NULL)
1502 {
1503 size_t name_len = strlen (c->name);
1504
1505 /* Try to match against the name. */
1506 returnvalue = regex.search (c->name, name_len, 0, name_len, NULL);
1507 if (returnvalue >= 0)
1508 print_doc_of_command (*c, prefix, verbose, regex, stream);
1509
1510 /* Try to match against the name of the aliases. */
1511 for (const cmd_list_element &alias : c->aliases)
1512 {
1513 name_len = strlen (alias.name);
1514 returnvalue = regex.search (alias.name, name_len, 0, name_len, NULL);
1515 if (returnvalue >= 0)
1516 {
1517 print_doc_of_command (*c, prefix, verbose, regex, stream);
1518 break;
1519 }
1520 }
1521 }
1522 if (c->doc != NULL && returnvalue < 0)
1523 {
1524 size_t doc_len = strlen (c->doc);
1525
1526 /* Try to match against documentation. */
1527 if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0)
1528 print_doc_of_command (*c, prefix, verbose, regex, stream);
1529 }
1530 /* Check if this command has subcommands. */
1531 if (c->is_prefix ())
1532 {
1533 /* Recursively call ourselves on the subcommand list,
1534 passing the right prefix in. */
1535 apropos_cmd (stream, *c->subcommands, verbose, regex,
1536 c->prefixname ().c_str ());
1537 }
1538 }
1539 }
1540
1541 /* This command really has to deal with two things:
1542 1) I want documentation on *this string* (usually called by
1543 "help commandname").
1544
1545 2) I want documentation on *this list* (usually called by giving a
1546 command that requires subcommands. Also called by saying just
1547 "help".)
1548
1549 I am going to split this into two separate commands, help_cmd and
1550 help_list. */
1551
1552 void
1553 help_cmd (const char *command, struct ui_file *stream)
1554 {
1555 struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd;
1556
1557 if (!command)
1558 {
1559 help_list (cmdlist, "", all_classes, stream);
1560 return;
1561 }
1562
1563 if (strcmp (command, "all") == 0)
1564 {
1565 help_all (stream);
1566 return;
1567 }
1568
1569 const char *orig_command = command;
1570 c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0);
1571
1572 if (c == 0)
1573 return;
1574
1575 lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd);
1576
1577 /* There are three cases here.
1578 If c->subcommands is nonzero, we have a prefix command.
1579 Print its documentation, then list its subcommands.
1580
1581 If c->func is non NULL, we really have a command. Print its
1582 documentation and return.
1583
1584 If c->func is NULL, we have a class name. Print its
1585 documentation (as if it were a command) and then set class to the
1586 number of this class so that the commands in the class will be
1587 listed. */
1588
1589 if (alias == nullptr || !user_documented_alias (*alias))
1590 {
1591 /* Case of a normal command, or an alias not explictly
1592 documented by the user. */
1593 /* If the user asked 'help somecommand' and there is no alias,
1594 the false indicates to not output the (single) command name. */
1595 fput_command_names_styled (*c, false, "\n", stream);
1596 fput_aliases_definition_styled (*c, stream);
1597 gdb_puts (c->doc, stream);
1598 }
1599 else
1600 {
1601 /* Case of an alias explictly documented by the user.
1602 Only output the alias definition and its explicit documentation. */
1603 fput_alias_definition_styled (*alias, stream);
1604 fput_command_names_styled (*alias, false, "\n", stream);
1605 gdb_puts (alias->doc, stream);
1606 }
1607 gdb_puts ("\n", stream);
1608
1609 if (!c->is_prefix () && !c->is_command_class_help ())
1610 return;
1611
1612 gdb_printf (stream, "\n");
1613
1614 /* If this is a prefix command, print it's subcommands. */
1615 if (c->is_prefix ())
1616 help_list (*c->subcommands, c->prefixname ().c_str (),
1617 all_commands, stream);
1618
1619 /* If this is a class name, print all of the commands in the class. */
1620 if (c->is_command_class_help ())
1621 help_list (cmdlist, "", c->theclass, stream);
1622
1623 if (c->hook_pre || c->hook_post)
1624 gdb_printf (stream,
1625 "\nThis command has a hook (or hooks) defined:\n");
1626
1627 if (c->hook_pre)
1628 gdb_printf (stream,
1629 "\tThis command is run after : %s (pre hook)\n",
1630 c->hook_pre->name);
1631 if (c->hook_post)
1632 gdb_printf (stream,
1633 "\tThis command is run before : %s (post hook)\n",
1634 c->hook_post->name);
1635 }
1636
1637 /*
1638 * Get a specific kind of help on a command list.
1639 *
1640 * LIST is the list.
1641 * CMDTYPE is the prefix to use in the title string.
1642 * CLASS is the class with which to list the nodes of this list (see
1643 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1644 * everything, ALL_CLASSES for just classes, and non-negative for only things
1645 * in a specific class.
1646 * and STREAM is the output stream on which to print things.
1647 * If you call this routine with a class >= 0, it recurses.
1648 */
1649 void
1650 help_list (struct cmd_list_element *list, const char *cmdtype,
1651 enum command_class theclass, struct ui_file *stream)
1652 {
1653 int len;
1654 char *cmdtype1, *cmdtype2;
1655
1656 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1657 */
1658 len = strlen (cmdtype);
1659 cmdtype1 = (char *) alloca (len + 1);
1660 cmdtype1[0] = 0;
1661 cmdtype2 = (char *) alloca (len + 4);
1662 cmdtype2[0] = 0;
1663 if (len)
1664 {
1665 cmdtype1[0] = ' ';
1666 memcpy (cmdtype1 + 1, cmdtype, len - 1);
1667 cmdtype1[len] = 0;
1668 memcpy (cmdtype2, cmdtype, len - 1);
1669 strcpy (cmdtype2 + len - 1, " sub");
1670 }
1671
1672 if (theclass == all_classes)
1673 gdb_printf (stream, "List of classes of %scommands:\n\n", cmdtype2);
1674 else
1675 gdb_printf (stream, "List of %scommands:\n\n", cmdtype2);
1676
1677 help_cmd_list (list, theclass, theclass >= 0, stream);
1678
1679 if (theclass == all_classes)
1680 {
1681 gdb_printf (stream, "\n\
1682 Type \"help%s\" followed by a class name for a list of commands in ",
1683 cmdtype1);
1684 stream->wrap_here (0);
1685 gdb_printf (stream, "that class.");
1686
1687 gdb_printf (stream, "\n\
1688 Type \"help all\" for the list of all commands.");
1689 }
1690
1691 gdb_printf (stream, "\nType \"help%s\" followed by %scommand name ",
1692 cmdtype1, cmdtype2);
1693 stream->wrap_here (0);
1694 gdb_puts ("for ", stream);
1695 stream->wrap_here (0);
1696 gdb_puts ("full ", stream);
1697 stream->wrap_here (0);
1698 gdb_puts ("documentation.\n", stream);
1699 gdb_puts ("Type \"apropos word\" to search "
1700 "for commands related to \"word\".\n", stream);
1701 gdb_puts ("Type \"apropos -v word\" for full documentation", stream);
1702 stream->wrap_here (0);
1703 gdb_puts (" of commands related to \"word\".\n", stream);
1704 gdb_puts ("Command name abbreviations are allowed if unambiguous.\n",
1705 stream);
1706 }
1707
1708 static void
1709 help_all (struct ui_file *stream)
1710 {
1711 struct cmd_list_element *c;
1712 int seen_unclassified = 0;
1713
1714 for (c = cmdlist; c; c = c->next)
1715 {
1716 if (c->abbrev_flag)
1717 continue;
1718 /* If this is a class name, print all of the commands in the
1719 class. */
1720
1721 if (c->is_command_class_help ())
1722 {
1723 gdb_printf (stream, "\nCommand class: %s\n\n", c->name);
1724 help_cmd_list (cmdlist, c->theclass, true, stream);
1725 }
1726 }
1727
1728 /* While it's expected that all commands are in some class,
1729 as a safety measure, we'll print commands outside of any
1730 class at the end. */
1731
1732 for (c = cmdlist; c; c = c->next)
1733 {
1734 if (c->abbrev_flag)
1735 continue;
1736
1737 if (c->theclass == no_class)
1738 {
1739 if (!seen_unclassified)
1740 {
1741 gdb_printf (stream, "\nUnclassified commands\n\n");
1742 seen_unclassified = 1;
1743 }
1744 print_help_for_command (*c, true, stream);
1745 }
1746 }
1747
1748 }
1749
1750 /* See cli-decode.h. */
1751
1752 void
1753 print_doc_line (struct ui_file *stream, const char *str,
1754 bool for_value_prefix)
1755 {
1756 static char *line_buffer = 0;
1757 static int line_size;
1758 const char *p;
1759
1760 if (!line_buffer)
1761 {
1762 line_size = 80;
1763 line_buffer = (char *) xmalloc (line_size);
1764 }
1765
1766 /* Searches for the first end of line or the end of STR. */
1767 p = str;
1768 while (*p && *p != '\n')
1769 p++;
1770 if (p - str > line_size - 1)
1771 {
1772 line_size = p - str + 1;
1773 xfree (line_buffer);
1774 line_buffer = (char *) xmalloc (line_size);
1775 }
1776 strncpy (line_buffer, str, p - str);
1777 if (for_value_prefix)
1778 {
1779 if (islower (line_buffer[0]))
1780 line_buffer[0] = toupper (line_buffer[0]);
1781 gdb_assert (p > str);
1782 if (line_buffer[p - str - 1] == '.')
1783 line_buffer[p - str - 1] = '\0';
1784 else
1785 line_buffer[p - str] = '\0';
1786 }
1787 else
1788 line_buffer[p - str] = '\0';
1789 gdb_puts (line_buffer, stream);
1790 }
1791
1792 /* Print one-line help for command C.
1793 If RECURSE is non-zero, also print one-line descriptions
1794 of all prefixed subcommands. */
1795 static void
1796 print_help_for_command (const cmd_list_element &c,
1797 bool recurse, struct ui_file *stream)
1798 {
1799 fput_command_names_styled (c, true, " -- ", stream);
1800 print_doc_line (stream, c.doc, false);
1801 gdb_puts ("\n", stream);
1802 if (!c.default_args.empty ())
1803 fput_alias_definition_styled (c, stream);
1804 fput_aliases_definition_styled (c, stream);
1805
1806 if (recurse
1807 && c.is_prefix ()
1808 && c.abbrev_flag == 0)
1809 /* Subcommands of a prefix command typically have 'all_commands'
1810 as class. If we pass CLASS to recursive invocation,
1811 most often we won't see anything. */
1812 help_cmd_list (*c.subcommands, all_commands, true, stream);
1813 }
1814
1815 /*
1816 * Implement a help command on command list LIST.
1817 * RECURSE should be non-zero if this should be done recursively on
1818 * all sublists of LIST.
1819 * STREAM is the stream upon which the output should be written.
1820 * THECLASS should be:
1821 * A non-negative class number to list only commands in that
1822 * ALL_COMMANDS to list all commands in list.
1823 * ALL_CLASSES to list all classes in list.
1824 *
1825 * Note that aliases are only shown when THECLASS is class_alias.
1826 * In the other cases, the aliases will be shown together with their
1827 * aliased command.
1828 *
1829 * Note that RECURSE will be active on *all* sublists, not just the
1830 * ones selected by the criteria above (ie. the selection mechanism
1831 * is at the low level, not the high-level).
1832 */
1833
1834 static void
1835 help_cmd_list (struct cmd_list_element *list, enum command_class theclass,
1836 bool recurse, struct ui_file *stream)
1837 {
1838 struct cmd_list_element *c;
1839
1840 for (c = list; c; c = c->next)
1841 {
1842 if (c->abbrev_flag == 1 || c->cmd_deprecated)
1843 {
1844 /* Do not show abbreviations or deprecated commands. */
1845 continue;
1846 }
1847
1848 if (c->is_alias () && theclass != class_alias)
1849 {
1850 /* Do not show an alias, unless specifically showing the
1851 list of aliases: for all other classes, an alias is
1852 shown (if needed) together with its aliased command. */
1853 continue;
1854 }
1855
1856 if (theclass == all_commands
1857 || (theclass == all_classes && c->is_command_class_help ())
1858 || (theclass == c->theclass && !c->is_command_class_help ()))
1859 {
1860 /* show C when
1861 - showing all commands
1862 - showing all classes and C is a help class
1863 - showing commands of THECLASS and C is not the help class */
1864
1865 /* If we show the class_alias and C is an alias, do not recurse,
1866 as this would show the (possibly very long) not very useful
1867 list of sub-commands of the aliased command. */
1868 print_help_for_command
1869 (*c,
1870 recurse && (theclass != class_alias || !c->is_alias ()),
1871 stream);
1872 continue;
1873 }
1874
1875 if (recurse
1876 && (theclass == class_user || theclass == class_alias)
1877 && c->is_prefix ())
1878 {
1879 /* User-defined commands or aliases may be subcommands. */
1880 help_cmd_list (*c->subcommands, theclass, recurse, stream);
1881 continue;
1882 }
1883
1884 /* Do not show C or recurse on C, e.g. because C does not belong to
1885 THECLASS or because C is a help class. */
1886 }
1887 }
1888
1889
1891 /* Search the input clist for 'command'. Return the command if
1892 found (or NULL if not), and return the number of commands
1893 found in nfound. */
1894
1895 static struct cmd_list_element *
1896 find_cmd (const char *command, int len, struct cmd_list_element *clist,
1897 int ignore_help_classes, int *nfound)
1898 {
1899 struct cmd_list_element *found, *c;
1900
1901 found = NULL;
1902 *nfound = 0;
1903 for (c = clist; c; c = c->next)
1904 if (!strncmp (command, c->name, len)
1905 && (!ignore_help_classes || !c->is_command_class_help ()))
1906 {
1907 found = c;
1908 (*nfound)++;
1909 if (c->name[len] == '\0')
1910 {
1911 *nfound = 1;
1912 break;
1913 }
1914 }
1915 return found;
1916 }
1917
1918 /* Return the length of command name in TEXT. */
1919
1920 int
1921 find_command_name_length (const char *text)
1922 {
1923 const char *p = text;
1924
1925 /* Treating underscores as part of command words is important
1926 so that "set args_foo()" doesn't get interpreted as
1927 "set args _foo()". */
1928 /* Some characters are only used for TUI specific commands.
1929 However, they are always allowed for the sake of consistency.
1930
1931 Note that this is larger than the character set allowed when
1932 creating user-defined commands. */
1933
1934 /* Recognize the single character commands so that, e.g., "!ls"
1935 works as expected. */
1936 if (*p == '!' || *p == '|')
1937 return 1;
1938
1939 while (valid_cmd_char_p (*p)
1940 /* Characters used by TUI specific commands. */
1941 || *p == '+' || *p == '<' || *p == '>' || *p == '$')
1942 p++;
1943
1944 return p - text;
1945 }
1946
1947 /* See command.h. */
1948
1949 bool
1950 valid_cmd_char_p (int c)
1951 {
1952 /* Alas "42" is a legitimate user-defined command.
1953 In the interests of not breaking anything we preserve that. */
1954
1955 return isalnum (c) || c == '-' || c == '_' || c == '.';
1956 }
1957
1958 /* See command.h. */
1959
1960 bool
1961 valid_user_defined_cmd_name_p (const char *name)
1962 {
1963 const char *p;
1964
1965 if (*name == '\0')
1966 return false;
1967
1968 for (p = name; *p != '\0'; ++p)
1969 {
1970 if (valid_cmd_char_p (*p))
1971 ; /* Ok. */
1972 else
1973 return false;
1974 }
1975
1976 return true;
1977 }
1978
1979 /* See command.h. */
1980
1981 struct cmd_list_element *
1982 lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
1983 struct cmd_list_element **result_list, std::string *default_args,
1984 int ignore_help_classes, bool lookup_for_completion_p)
1985 {
1986 char *command;
1987 int len, nfound;
1988 struct cmd_list_element *found, *c;
1989 bool found_alias = false;
1990 const char *line = *text;
1991
1992 while (**text == ' ' || **text == '\t')
1993 (*text)++;
1994
1995 /* Identify the name of the command. */
1996 len = find_command_name_length (*text);
1997
1998 /* If nothing but whitespace, return 0. */
1999 if (len == 0)
2000 return 0;
2001
2002 /* *text and p now bracket the first command word to lookup (and
2003 it's length is len). We copy this into a local temporary. */
2004
2005
2006 command = (char *) alloca (len + 1);
2007 memcpy (command, *text, len);
2008 command[len] = '\0';
2009
2010 /* Look it up. */
2011 found = 0;
2012 nfound = 0;
2013 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
2014
2015 /* If nothing matches, we have a simple failure. */
2016 if (nfound == 0)
2017 return 0;
2018
2019 if (nfound > 1)
2020 {
2021 if (result_list != nullptr)
2022 /* Will be modified in calling routine
2023 if we know what the prefix command is. */
2024 *result_list = 0;
2025 if (default_args != nullptr)
2026 *default_args = std::string ();
2027 return CMD_LIST_AMBIGUOUS; /* Ambiguous. */
2028 }
2029
2030 /* We've matched something on this list. Move text pointer forward. */
2031
2032 *text += len;
2033
2034 if (found->is_alias ())
2035 {
2036 /* We drop the alias (abbreviation) in favor of the command it
2037 is pointing to. If the alias is deprecated, though, we need to
2038 warn the user about it before we drop it. Note that while we
2039 are warning about the alias, we may also warn about the command
2040 itself and we will adjust the appropriate DEPRECATED_WARN_USER
2041 flags. */
2042
2043 if (found->deprecated_warn_user && !lookup_for_completion_p)
2044 deprecated_cmd_warning (line, clist);
2045
2046
2047 /* Return the default_args of the alias, not the default_args
2048 of the command it is pointing to. */
2049 if (default_args != nullptr)
2050 *default_args = found->default_args;
2051 found = found->alias_target;
2052 found_alias = true;
2053 }
2054 /* If we found a prefix command, keep looking. */
2055
2056 if (found->is_prefix ())
2057 {
2058 c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args,
2059 ignore_help_classes, lookup_for_completion_p);
2060 if (!c)
2061 {
2062 /* Didn't find anything; this is as far as we got. */
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 else if (c == CMD_LIST_AMBIGUOUS)
2070 {
2071 /* We've gotten this far properly, but the next step is
2072 ambiguous. We need to set the result list to the best
2073 we've found (if an inferior hasn't already set it). */
2074 if (result_list != nullptr)
2075 if (!*result_list)
2076 /* This used to say *result_list = *found->subcommands.
2077 If that was correct, need to modify the documentation
2078 at the top of this function to clarify what is
2079 supposed to be going on. */
2080 *result_list = found;
2081 /* For ambiguous commands, do not return any default_args args. */
2082 if (default_args != nullptr)
2083 *default_args = std::string ();
2084 return c;
2085 }
2086 else
2087 {
2088 /* We matched! */
2089 return c;
2090 }
2091 }
2092 else
2093 {
2094 if (result_list != nullptr)
2095 *result_list = clist;
2096 if (!found_alias && default_args != nullptr)
2097 *default_args = found->default_args;
2098 return found;
2099 }
2100 }
2101
2102 /* All this hair to move the space to the front of cmdtype */
2103
2104 static void
2105 undef_cmd_error (const char *cmdtype, const char *q)
2106 {
2107 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
2108 cmdtype,
2109 q,
2110 *cmdtype ? " " : "",
2111 (int) strlen (cmdtype) - 1,
2112 cmdtype);
2113 }
2114
2115 /* Look up the contents of *LINE as a command in the command list LIST.
2116 LIST is a chain of struct cmd_list_element's.
2117 If it is found, return the struct cmd_list_element for that command,
2118 update *LINE to point after the command name, at the first argument
2119 and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
2120 args to prepend to the user provided args when running the command.
2121 Note that if the found cmd_list_element is found via an alias,
2122 the default args of the alias are returned.
2123
2124 If not found, call error if ALLOW_UNKNOWN is zero
2125 otherwise (or if error returns) return zero.
2126 Call error if specified command is ambiguous,
2127 unless ALLOW_UNKNOWN is negative.
2128 CMDTYPE precedes the word "command" in the error message.
2129
2130 If IGNORE_HELP_CLASSES is nonzero, ignore any command list
2131 elements which are actually help classes rather than commands (i.e.
2132 the function field of the struct cmd_list_element is 0). */
2133
2134 struct cmd_list_element *
2135 lookup_cmd (const char **line, struct cmd_list_element *list,
2136 const char *cmdtype,
2137 std::string *default_args,
2138 int allow_unknown, int ignore_help_classes)
2139 {
2140 struct cmd_list_element *last_list = 0;
2141 struct cmd_list_element *c;
2142
2143 /* Note: Do not remove trailing whitespace here because this
2144 would be wrong for complete_command. Jim Kingdon */
2145
2146 if (!*line)
2147 error (_("Lack of needed %scommand"), cmdtype);
2148
2149 c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes);
2150
2151 if (!c)
2152 {
2153 if (!allow_unknown)
2154 {
2155 char *q;
2156 int len = find_command_name_length (*line);
2157
2158 q = (char *) alloca (len + 1);
2159 strncpy (q, *line, len);
2160 q[len] = '\0';
2161 undef_cmd_error (cmdtype, q);
2162 }
2163 else
2164 return 0;
2165 }
2166 else if (c == CMD_LIST_AMBIGUOUS)
2167 {
2168 /* Ambigous. Local values should be off subcommands or called
2169 values. */
2170 int local_allow_unknown = (last_list ? last_list->allow_unknown :
2171 allow_unknown);
2172 std::string local_cmdtype
2173 = last_list ? last_list->prefixname () : cmdtype;
2174 struct cmd_list_element *local_list =
2175 (last_list ? *(last_list->subcommands) : list);
2176
2177 if (local_allow_unknown < 0)
2178 {
2179 if (last_list)
2180 return last_list; /* Found something. */
2181 else
2182 return 0; /* Found nothing. */
2183 }
2184 else
2185 {
2186 /* Report as error. */
2187 int amb_len;
2188 char ambbuf[100];
2189
2190 for (amb_len = 0;
2191 ((*line)[amb_len] && (*line)[amb_len] != ' '
2192 && (*line)[amb_len] != '\t');
2193 amb_len++)
2194 ;
2195
2196 ambbuf[0] = 0;
2197 for (c = local_list; c; c = c->next)
2198 if (!strncmp (*line, c->name, amb_len))
2199 {
2200 if (strlen (ambbuf) + strlen (c->name) + 6
2201 < (int) sizeof ambbuf)
2202 {
2203 if (strlen (ambbuf))
2204 strcat (ambbuf, ", ");
2205 strcat (ambbuf, c->name);
2206 }
2207 else
2208 {
2209 strcat (ambbuf, "..");
2210 break;
2211 }
2212 }
2213 error (_("Ambiguous %scommand \"%s\": %s."),
2214 local_cmdtype.c_str (), *line, ambbuf);
2215 }
2216 }
2217 else
2218 {
2219 if (c->type == set_cmd && **line != '\0' && !isspace (**line))
2220 error (_("Argument must be preceded by space."));
2221
2222 /* We've got something. It may still not be what the caller
2223 wants (if this command *needs* a subcommand). */
2224 while (**line == ' ' || **line == '\t')
2225 (*line)++;
2226
2227 if (c->is_prefix () && **line && !c->allow_unknown)
2228 undef_cmd_error (c->prefixname ().c_str (), *line);
2229
2230 /* Seems to be what he wants. Return it. */
2231 return c;
2232 }
2233 return 0;
2234 }
2235
2236 /* See command.h. */
2237
2238 struct cmd_list_element *
2239 lookup_cmd_exact (const char *name,
2240 struct cmd_list_element *list,
2241 bool ignore_help_classes)
2242 {
2243 const char *tem = name;
2244 struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
2245 ignore_help_classes);
2246 if (cmd != nullptr && strcmp (name, cmd->name) != 0)
2247 cmd = nullptr;
2248 return cmd;
2249 }
2250
2251 /* We are here presumably because an alias or command in TEXT is
2252 deprecated and a warning message should be generated. This
2253 function decodes TEXT and potentially generates a warning message
2254 as outlined below.
2255
2256 Example for 'set endian big' which has a fictitious alias 'seb'.
2257
2258 If alias wasn't used in TEXT, and the command is deprecated:
2259 "warning: 'set endian big' is deprecated."
2260
2261 If alias was used, and only the alias is deprecated:
2262 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
2263
2264 If alias was used and command is deprecated (regardless of whether
2265 the alias itself is deprecated:
2266
2267 "warning: 'set endian big' (seb) is deprecated."
2268
2269 After the message has been sent, clear the appropriate flags in the
2270 command and/or the alias so the user is no longer bothered.
2271
2272 */
2273 void
2274 deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
2275 {
2276 struct cmd_list_element *alias = nullptr;
2277 struct cmd_list_element *cmd = nullptr;
2278
2279 /* Return if text doesn't evaluate to a command. We place this lookup
2280 within its own scope so that the PREFIX_CMD local is not visible
2281 later in this function. The value returned in PREFIX_CMD is based on
2282 the prefix found in TEXT, and is our case this prefix can be missing
2283 in some situations (when LIST is not the global CMDLIST).
2284
2285 It is better for our purposes to use the prefix commands directly from
2286 the ALIAS and CMD results. */
2287 {
2288 struct cmd_list_element *prefix_cmd = nullptr;
2289 if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list))
2290 return;
2291 }
2292
2293 /* Return if nothing is deprecated. */
2294 if (!((alias != nullptr ? alias->deprecated_warn_user : 0)
2295 || cmd->deprecated_warn_user))
2296 return;
2297
2298 /* Join command prefix (if any) and the command name. */
2299 std::string tmp_cmd_str;
2300 if (cmd->prefix != nullptr)
2301 tmp_cmd_str += cmd->prefix->prefixname ();
2302 tmp_cmd_str += std::string (cmd->name);
2303
2304 /* Display the appropriate first line, this warns that the thing the user
2305 entered is deprecated. */
2306 if (alias != nullptr)
2307 {
2308 /* Join the alias prefix (if any) and the alias name. */
2309 std::string tmp_alias_str;
2310 if (alias->prefix != nullptr)
2311 tmp_alias_str += alias->prefix->prefixname ();
2312 tmp_alias_str += std::string (alias->name);
2313
2314 if (cmd->cmd_deprecated)
2315 gdb_printf (_("Warning: command '%ps' (%ps) is deprecated.\n"),
2316 styled_string (title_style.style (),
2317 tmp_cmd_str.c_str ()),
2318 styled_string (title_style.style (),
2319 tmp_alias_str.c_str ()));
2320 else
2321 gdb_printf (_("Warning: '%ps', an alias for the command '%ps', "
2322 "is deprecated.\n"),
2323 styled_string (title_style.style (),
2324 tmp_alias_str.c_str ()),
2325 styled_string (title_style.style (),
2326 tmp_cmd_str.c_str ()));
2327 }
2328 else
2329 gdb_printf (_("Warning: command '%ps' is deprecated.\n"),
2330 styled_string (title_style.style (),
2331 tmp_cmd_str.c_str ()));
2332
2333 /* Now display a second line indicating what the user should use instead.
2334 If it is only the alias that is deprecated, we want to indicate the
2335 new alias, otherwise we'll indicate the new command. */
2336 const char *replacement;
2337 if (alias != nullptr && !cmd->cmd_deprecated)
2338 replacement = alias->replacement;
2339 else
2340 replacement = cmd->replacement;
2341 if (replacement != nullptr)
2342 gdb_printf (_("Use '%ps'.\n\n"),
2343 styled_string (title_style.style (),
2344 replacement));
2345 else
2346 gdb_printf (_("No alternative known.\n\n"));
2347
2348 /* We've warned you, now we'll keep quiet. */
2349 if (alias != nullptr)
2350 alias->deprecated_warn_user = 0;
2351 cmd->deprecated_warn_user = 0;
2352 }
2353
2354 /* Look up the contents of TEXT as a command in the command list CUR_LIST.
2355 Return 1 on success, 0 on failure.
2356
2357 If TEXT refers to an alias, *ALIAS will point to that alias.
2358
2359 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2360 command) set *PREFIX_CMD.
2361
2362 Set *CMD to point to the command TEXT indicates.
2363
2364 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2365 exist, they are NULL when we return.
2366
2367 */
2368
2369 static int
2370 lookup_cmd_composition_1 (const char *text,
2371 struct cmd_list_element **alias,
2372 struct cmd_list_element **prefix_cmd,
2373 struct cmd_list_element **cmd,
2374 struct cmd_list_element *cur_list)
2375 {
2376 *alias = nullptr;
2377 *prefix_cmd = cur_list->prefix;
2378 *cmd = nullptr;
2379
2380 text = skip_spaces (text);
2381
2382 /* Go through as many command lists as we need to, to find the command
2383 TEXT refers to. */
2384 while (1)
2385 {
2386 /* Identify the name of the command. */
2387 int len = find_command_name_length (text);
2388
2389 /* If nothing but whitespace, return. */
2390 if (len == 0)
2391 return 0;
2392
2393 /* TEXT is the start of the first command word to lookup (and
2394 it's length is LEN). We copy this into a local temporary. */
2395 std::string command (text, len);
2396
2397 /* Look it up. */
2398 int nfound = 0;
2399 *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
2400
2401 /* We only handle the case where a single command was found. */
2402 if (*cmd == CMD_LIST_AMBIGUOUS || *cmd == nullptr)
2403 return 0;
2404 else
2405 {
2406 if ((*cmd)->is_alias ())
2407 {
2408 /* If the command was actually an alias, we note that an
2409 alias was used (by assigning *ALIAS) and we set *CMD. */
2410 *alias = *cmd;
2411 *cmd = (*cmd)->alias_target;
2412 }
2413 }
2414
2415 text += len;
2416 text = skip_spaces (text);
2417
2418 if ((*cmd)->is_prefix () && *text != '\0')
2419 {
2420 cur_list = *(*cmd)->subcommands;
2421 *prefix_cmd = *cmd;
2422 }
2423 else
2424 return 1;
2425 }
2426 }
2427
2428 /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2429 Return 1 on success, 0 on failure.
2430
2431 If TEXT refers to an alias, *ALIAS will point to that alias.
2432
2433 If TEXT is a subcommand (i.e. one that is preceded by a prefix
2434 command) set *PREFIX_CMD.
2435
2436 Set *CMD to point to the command TEXT indicates.
2437
2438 If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2439 exist, they are NULL when we return.
2440
2441 */
2442
2443 int
2444 lookup_cmd_composition (const char *text,
2445 struct cmd_list_element **alias,
2446 struct cmd_list_element **prefix_cmd,
2447 struct cmd_list_element **cmd)
2448 {
2449 return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist);
2450 }
2451
2452 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2453
2454 /* Return a vector of char pointers which point to the different
2455 possible completions in LIST of TEXT.
2456
2457 WORD points in the same buffer as TEXT, and completions should be
2458 returned relative to this position. For example, suppose TEXT is
2459 "foo" and we want to complete to "foobar". If WORD is "oo", return
2460 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2461
2462 void
2463 complete_on_cmdlist (struct cmd_list_element *list,
2464 completion_tracker &tracker,
2465 const char *text, const char *word,
2466 int ignore_help_classes)
2467 {
2468 struct cmd_list_element *ptr;
2469 int textlen = strlen (text);
2470 int pass;
2471 int saw_deprecated_match = 0;
2472
2473 /* We do one or two passes. In the first pass, we skip deprecated
2474 commands. If we see no matching commands in the first pass, and
2475 if we did happen to see a matching deprecated command, we do
2476 another loop to collect those. */
2477 for (pass = 0; pass < 2; ++pass)
2478 {
2479 bool got_matches = false;
2480
2481 for (ptr = list; ptr; ptr = ptr->next)
2482 if (!strncmp (ptr->name, text, textlen)
2483 && !ptr->abbrev_flag
2484 && (!ignore_help_classes || !ptr->is_command_class_help ()
2485 || ptr->is_prefix ()))
2486 {
2487 if (pass == 0)
2488 {
2489 if (ptr->cmd_deprecated)
2490 {
2491 saw_deprecated_match = 1;
2492 continue;
2493 }
2494 }
2495
2496 tracker.add_completion
2497 (make_completion_match_str (ptr->name, text, word));
2498 got_matches = true;
2499 }
2500
2501 if (got_matches)
2502 break;
2503
2504 /* If we saw no matching deprecated commands in the first pass,
2505 just bail out. */
2506 if (!saw_deprecated_match)
2507 break;
2508 }
2509 }
2510
2511 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2512
2513 /* Add the different possible completions in ENUMLIST of TEXT.
2514
2515 WORD points in the same buffer as TEXT, and completions should be
2516 returned relative to this position. For example, suppose TEXT is "foo"
2517 and we want to complete to "foobar". If WORD is "oo", return
2518 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2519
2520 void
2521 complete_on_enum (completion_tracker &tracker,
2522 const char *const *enumlist,
2523 const char *text, const char *word)
2524 {
2525 int textlen = strlen (text);
2526 int i;
2527 const char *name;
2528
2529 for (i = 0; (name = enumlist[i]) != NULL; i++)
2530 if (strncmp (name, text, textlen) == 0)
2531 tracker.add_completion (make_completion_match_str (name, text, word));
2532 }
2533
2534 /* Call the command function. */
2535 void
2536 cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty)
2537 {
2538 if (!cmd->is_command_class_help ())
2539 {
2540 gdb::optional<scoped_restore_tmpl<bool>> restore_suppress;
2541
2542 if (cmd->suppress_notification != NULL)
2543 restore_suppress.emplace (cmd->suppress_notification, true);
2544
2545 cmd->func (args, from_tty, cmd);
2546 }
2547 else
2548 error (_("Invalid command"));
2549 }
2550
2551 int
2552 cli_user_command_p (struct cmd_list_element *cmd)
2553 {
2554 return cmd->theclass == class_user && cmd->func == do_simple_func;
2555 }
2556