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