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