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