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