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