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