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