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