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