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