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