1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002, 2004 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "gdb_regex.h"
25 #include "gdb_string.h"
26 #include "completer.h"
29 #include "cli/cli-cmds.h"
30 #include "cli/cli-decode.h"
33 #include "tui/tui.h" /* For tui_active et.al. */
36 #include "gdb_assert.h"
38 /* Prototypes for local functions */
40 static void undef_cmd_error (char *, char *);
42 static struct cmd_list_element
*find_cmd (char *command
,
44 struct cmd_list_element
*clist
,
45 int ignore_help_classes
,
48 static void help_all (struct ui_file
*stream
);
50 /* Set the callback function for the specified command. For each both
51 the commands callback and func() are set. The latter set to a
52 bounce function (unless cfunc / sfunc is NULL that is). */
55 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
57 c
->function
.cfunc (args
, from_tty
); /* Ok. */
61 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
67 cmd
->function
.cfunc
= cfunc
; /* Ok. */
71 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
73 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
77 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
83 cmd
->function
.sfunc
= sfunc
; /* Ok. */
87 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
88 void (*cfunc
) (char *args
, int from_tty
))
90 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
94 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
96 cmd
->context
= context
;
100 get_cmd_context (struct cmd_list_element
*cmd
)
106 cmd_type (struct cmd_list_element
*cmd
)
112 set_cmd_completer (struct cmd_list_element
*cmd
,
113 char **(*completer
) (char *text
, char *word
))
115 cmd
->completer
= completer
; /* Ok. */
119 /* Add element named NAME.
120 CLASS is the top level category into which commands are broken down
122 FUN should be the function to execute the command;
123 it will get a character string as argument, with leading
124 and trailing blanks already eliminated.
126 DOC is a documentation string for the command.
127 Its first line should be a complete sentence.
128 It should start with ? for a command that is an abbreviation
129 or with * for a command that most users don't need to know about.
131 Add this command to command list *LIST.
133 Returns a pointer to the added command (not necessarily the head
136 struct cmd_list_element
*
137 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
138 char *doc
, struct cmd_list_element
**list
)
140 struct cmd_list_element
*c
141 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
142 struct cmd_list_element
*p
;
144 delete_cmd (name
, list
);
146 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
154 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
164 set_cmd_cfunc (c
, fun
);
165 set_cmd_context (c
, NULL
);
168 c
->replacement
= NULL
;
169 c
->pre_show_hook
= NULL
;
173 c
->prefixlist
= NULL
;
174 c
->prefixname
= NULL
;
175 c
->allow_unknown
= 0;
177 set_cmd_completer (c
, make_symbol_completion_list
);
178 c
->type
= not_set_cmd
;
180 c
->var_type
= var_boolean
;
182 c
->user_commands
= NULL
;
183 c
->hookee_pre
= NULL
;
184 c
->hookee_post
= NULL
;
185 c
->cmd_pointer
= NULL
;
190 /* Deprecates a command CMD.
191 REPLACEMENT is the name of the command which should be used in place
192 of this command, or NULL if no such command exists.
194 This function does not check to see if command REPLACEMENT exists
195 since gdb may not have gotten around to adding REPLACEMENT when this
198 Returns a pointer to the deprecated command. */
200 struct cmd_list_element
*
201 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
203 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
205 if (replacement
!= NULL
)
206 cmd
->replacement
= replacement
;
208 cmd
->replacement
= NULL
;
213 struct cmd_list_element
*
214 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
215 int abbrev_flag
, struct cmd_list_element
**list
)
217 /* Must do this since lookup_cmd tries to side-effect its first arg */
219 struct cmd_list_element
*old
;
220 struct cmd_list_element
*c
;
221 copied_name
= (char *) alloca (strlen (oldname
) + 1);
222 strcpy (copied_name
, oldname
);
223 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
227 delete_cmd (name
, list
);
231 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
232 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
234 c
->function
= old
->function
;
235 c
->prefixlist
= old
->prefixlist
;
236 c
->prefixname
= old
->prefixname
;
237 c
->allow_unknown
= old
->allow_unknown
;
238 c
->abbrev_flag
= abbrev_flag
;
239 c
->cmd_pointer
= old
;
243 /* Like add_cmd but adds an element for a command prefix:
244 a name that should be followed by a subcommand to be looked up
245 in another command list. PREFIXLIST should be the address
246 of the variable containing that list. */
248 struct cmd_list_element
*
249 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
250 char *doc
, struct cmd_list_element
**prefixlist
,
251 char *prefixname
, int allow_unknown
,
252 struct cmd_list_element
**list
)
254 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
255 c
->prefixlist
= prefixlist
;
256 c
->prefixname
= prefixname
;
257 c
->allow_unknown
= allow_unknown
;
261 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
263 struct cmd_list_element
*
264 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
265 void (*fun
) (char *, int), char *doc
,
266 struct cmd_list_element
**prefixlist
, char *prefixname
,
267 int allow_unknown
, struct cmd_list_element
**list
)
269 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
270 c
->prefixlist
= prefixlist
;
271 c
->prefixname
= prefixname
;
272 c
->allow_unknown
= allow_unknown
;
277 /* This is an empty "cfunc". */
279 not_just_help_class_command (char *args
, int from_tty
)
283 /* This is an empty "sfunc". */
284 static void empty_sfunc (char *, int, struct cmd_list_element
*);
287 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
291 /* Add element named NAME to command list LIST (the list for set/show
292 or some sublist thereof).
293 TYPE is set_cmd or show_cmd.
294 CLASS is as in add_cmd.
295 VAR_TYPE is the kind of thing we are setting.
296 VAR is address of the variable being controlled by this command.
297 DOC is the documentation string. */
299 static struct cmd_list_element
*
300 add_set_or_show_cmd (char *name
,
302 enum command_class
class,
306 struct cmd_list_element
**list
)
308 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
309 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
311 c
->var_type
= var_type
;
313 /* This needs to be something besides NULL so that this isn't
314 treated as a help class. */
315 set_cmd_sfunc (c
, empty_sfunc
);
319 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
320 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
321 setting. VAR is address of the variable being controlled by this
322 command. SET_FUNC and SHOW_FUNC are the callback functions (if
323 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
324 strings. PRINT the format string to print the value. SET_RESULT
325 and SHOW_RESULT, if not NULL, are set to the resulting command
329 add_setshow_cmd_full (char *name
,
330 enum command_class
class,
331 var_types var_type
, void *var
,
332 const char *set_doc
, const char *show_doc
,
333 const char *help_doc
,
334 cmd_sfunc_ftype
*set_func
,
335 show_value_ftype
*show_func
,
336 struct cmd_list_element
**set_list
,
337 struct cmd_list_element
**show_list
,
338 struct cmd_list_element
**set_result
,
339 struct cmd_list_element
**show_result
)
341 struct cmd_list_element
*set
;
342 struct cmd_list_element
*show
;
346 if (help_doc
!= NULL
)
348 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
349 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
353 full_set_doc
= xstrdup (set_doc
);
354 full_show_doc
= xstrdup (show_doc
);
356 set
= add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
,
357 full_set_doc
, set_list
);
358 if (set_func
!= NULL
)
359 set_cmd_sfunc (set
, set_func
);
360 show
= add_set_or_show_cmd (name
, show_cmd
, class, var_type
, var
,
361 full_show_doc
, show_list
);
362 show
->show_value_func
= show_func
;
364 if (set_result
!= NULL
)
366 if (show_result
!= NULL
)
370 struct cmd_list_element
*
371 add_set_cmd (char *name
,
372 enum command_class
class,
376 struct cmd_list_element
**list
)
378 return add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
, doc
, list
);
381 /* Add element named NAME to command list LIST (the list for set
382 or some sublist thereof).
383 CLASS is as in add_cmd.
384 ENUMLIST is a list of strings which may follow NAME.
385 VAR is address of the variable which will contain the matching string
387 DOC is the documentation string. */
389 struct cmd_list_element
*
390 add_set_enum_cmd (char *name
,
391 enum command_class
class,
392 const char *enumlist
[],
395 struct cmd_list_element
**list
)
397 struct cmd_list_element
*c
398 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
404 /* Add element named NAME to command list LIST (the list for set or
405 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
406 of strings which may follow NAME. VAR is address of the variable
407 which will contain the matching string (from ENUMLIST). */
410 add_setshow_enum_cmd (char *name
,
411 enum command_class
class,
412 const char *enumlist
[],
415 const char *show_doc
,
416 const char *help_doc
,
417 cmd_sfunc_ftype
*set_func
,
418 show_value_ftype
*show_func
,
419 struct cmd_list_element
**set_list
,
420 struct cmd_list_element
**show_list
)
422 struct cmd_list_element
*c
;
423 add_setshow_cmd_full (name
, class, var_enum
, var
,
424 set_doc
, show_doc
, help_doc
,
431 /* Add an auto-boolean command named NAME to both the set and show
432 command list lists. CLASS is as in add_cmd. VAR is address of the
433 variable which will contain the value. DOC is the documentation
434 string. FUNC is the corresponding callback. */
436 add_setshow_auto_boolean_cmd (char *name
,
437 enum command_class
class,
438 enum auto_boolean
*var
,
439 const char *set_doc
, const char *show_doc
,
440 const char *help_doc
,
441 cmd_sfunc_ftype
*set_func
,
442 show_value_ftype
*show_func
,
443 struct cmd_list_element
**set_list
,
444 struct cmd_list_element
**show_list
)
446 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
447 struct cmd_list_element
*c
;
448 add_setshow_cmd_full (name
, class, var_auto_boolean
, var
,
449 set_doc
, show_doc
, help_doc
,
453 c
->enums
= auto_boolean_enums
;
456 /* Add element named NAME to both the set and show command LISTs (the
457 list for set/show or some sublist thereof). CLASS is as in
458 add_cmd. VAR is address of the variable which will contain the
459 value. SET_DOC and SHOW_DOC are the documentation strings. */
461 add_setshow_boolean_cmd (char *name
, enum command_class
class, int *var
,
462 const char *set_doc
, const char *show_doc
,
463 const char *help_doc
,
464 cmd_sfunc_ftype
*set_func
,
465 show_value_ftype
*show_func
,
466 struct cmd_list_element
**set_list
,
467 struct cmd_list_element
**show_list
)
469 static const char *boolean_enums
[] = { "on", "off", NULL
};
470 struct cmd_list_element
*c
;
471 add_setshow_cmd_full (name
, class, var_boolean
, var
,
472 set_doc
, show_doc
, help_doc
,
476 c
->enums
= boolean_enums
;
479 /* Add element named NAME to both the set and show command LISTs (the
480 list for set/show or some sublist thereof). */
482 add_setshow_filename_cmd (char *name
, enum command_class
class,
484 const char *set_doc
, const char *show_doc
,
485 const char *help_doc
,
486 cmd_sfunc_ftype
*set_func
,
487 show_value_ftype
*show_func
,
488 struct cmd_list_element
**set_list
,
489 struct cmd_list_element
**show_list
)
491 struct cmd_list_element
*set_result
;
492 add_setshow_cmd_full (name
, class, var_filename
, var
,
493 set_doc
, show_doc
, help_doc
,
497 set_cmd_completer (set_result
, filename_completer
);
500 /* Add element named NAME to both the set and show command LISTs (the
501 list for set/show or some sublist thereof). */
503 add_setshow_string_cmd (char *name
, enum command_class
class,
505 const char *set_doc
, const char *show_doc
,
506 const char *help_doc
,
507 cmd_sfunc_ftype
*set_func
,
508 show_value_ftype
*show_func
,
509 struct cmd_list_element
**set_list
,
510 struct cmd_list_element
**show_list
)
512 add_setshow_cmd_full (name
, class, var_string
, var
,
513 set_doc
, show_doc
, help_doc
,
519 /* Add element named NAME to both the set and show command LISTs (the
520 list for set/show or some sublist thereof). */
522 add_setshow_string_noescape_cmd (char *name
, enum command_class
class,
524 const char *set_doc
, const char *show_doc
,
525 const char *help_doc
,
526 cmd_sfunc_ftype
*set_func
,
527 show_value_ftype
*show_func
,
528 struct cmd_list_element
**set_list
,
529 struct cmd_list_element
**show_list
)
531 add_setshow_cmd_full (name
, class, var_string_noescape
, var
,
532 set_doc
, show_doc
, help_doc
,
538 /* Add element named NAME to both the set and show command LISTs (the
539 list for set/show or some sublist thereof). CLASS is as in
540 add_cmd. VAR is address of the variable which will contain the
541 value. SET_DOC and SHOW_DOC are the documentation strings. */
543 add_setshow_integer_cmd (char *name
, enum command_class
class,
545 const char *set_doc
, const char *show_doc
,
546 const char *help_doc
,
547 cmd_sfunc_ftype
*set_func
,
548 show_value_ftype
*show_func
,
549 struct cmd_list_element
**set_list
,
550 struct cmd_list_element
**show_list
)
552 add_setshow_cmd_full (name
, class, var_integer
, var
,
553 set_doc
, show_doc
, help_doc
,
559 /* Add element named NAME to both the set and show command LISTs (the
560 list for set/show or some sublist thereof). CLASS is as in
561 add_cmd. VAR is address of the variable which will contain the
562 value. SET_DOC and SHOW_DOC are the documentation strings. */
564 add_setshow_uinteger_cmd (char *name
, enum command_class
class,
566 const char *set_doc
, const char *show_doc
,
567 const char *help_doc
,
568 cmd_sfunc_ftype
*set_func
,
569 show_value_ftype
*show_func
,
570 struct cmd_list_element
**set_list
,
571 struct cmd_list_element
**show_list
)
573 add_setshow_cmd_full (name
, class, var_uinteger
, var
,
574 set_doc
, show_doc
, help_doc
,
580 /* Add element named NAME to both the set and show command LISTs (the
581 list for set/show or some sublist thereof). CLASS is as in
582 add_cmd. VAR is address of the variable which will contain the
583 value. SET_DOC and SHOW_DOC are the documentation strings. */
585 add_setshow_zinteger_cmd (char *name
, enum command_class
class,
587 const char *set_doc
, const char *show_doc
,
588 const char *help_doc
,
589 cmd_sfunc_ftype
*set_func
,
590 show_value_ftype
*show_func
,
591 struct cmd_list_element
**set_list
,
592 struct cmd_list_element
**show_list
)
594 add_setshow_cmd_full (name
, class, var_zinteger
, var
,
595 set_doc
, show_doc
, help_doc
,
601 /* Where SETCMD has already been added, add the corresponding show
602 command to LIST and return a pointer to the added command (not
603 necessarily the head of LIST). */
604 /* NOTE: cagney/2002-03-17: The original version of
605 deprecated_add_show_from_set used memcpy() to clone `set' into
606 `show'. This meant that in addition to all the needed fields (var,
607 name, et.al.) some unnecessary fields were copied (namely the
608 callback function). The function explictly copies relevant fields.
609 For a `set' and `show' command to share the same callback, the
610 caller must set both explicitly. */
611 struct cmd_list_element
*
612 deprecated_add_show_from_set (struct cmd_list_element
*setcmd
,
613 struct cmd_list_element
**list
)
616 const static char setstring
[] = "Set ";
618 /* Create a doc string by replacing "Set " at the start of the
619 `set'' command's doco with "Show ". */
620 gdb_assert (strncmp (setcmd
->doc
, setstring
, sizeof (setstring
) - 1) == 0);
621 doc
= concat ("Show ", setcmd
->doc
+ sizeof (setstring
) - 1, NULL
);
623 /* Insert the basic command. */
624 return add_set_or_show_cmd (setcmd
->name
, show_cmd
, setcmd
->class,
625 setcmd
->var_type
, setcmd
->var
, doc
, list
);
628 /* Remove the command named NAME from the command list. */
631 delete_cmd (char *name
, struct cmd_list_element
**list
)
633 struct cmd_list_element
*c
;
634 struct cmd_list_element
*p
;
636 while (*list
&& strcmp ((*list
)->name
, name
) == 0)
638 if ((*list
)->hookee_pre
)
639 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
640 if ((*list
)->hookee_post
)
641 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
648 for (c
= *list
; c
->next
;)
650 if (strcmp (c
->next
->name
, name
) == 0)
652 if (c
->next
->hookee_pre
)
653 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
654 if (c
->next
->hookee_post
)
655 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
656 /* :( no fishing metaphore */
666 /* Shorthands to the commands above. */
668 /* Add an element to the list of info subcommands. */
670 struct cmd_list_element
*
671 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
673 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
676 /* Add an alias to the list of info subcommands. */
678 struct cmd_list_element
*
679 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
681 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
684 /* Add an element to the list of commands. */
686 struct cmd_list_element
*
687 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
690 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
693 /* Add an alias or abbreviation command to the list of commands. */
695 struct cmd_list_element
*
696 add_com_alias (char *name
, char *oldname
, enum command_class
class,
699 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
702 /* Recursively walk the commandlist structures, and print out the
703 documentation of commands that match our regex in either their
704 name, or their documentation.
707 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
708 struct re_pattern_buffer
*regex
, char *prefix
)
710 struct cmd_list_element
*c
;
711 int returnvalue
=1; /*Needed to avoid double printing*/
712 /* Walk through the commands */
713 for (c
=commandlist
;c
;c
=c
->next
)
717 /* Try to match against the name*/
718 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
719 if (returnvalue
>= 0)
721 /* Stolen from help_cmd_list. We don't directly use
722 * help_cmd_list because it doesn't let us print out
725 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
726 print_doc_line (stream
, c
->doc
);
727 fputs_filtered ("\n", stream
);
728 returnvalue
=0; /*Set this so we don't print it again.*/
731 if (c
->doc
!= NULL
&& returnvalue
!= 0)
733 /* Try to match against documentation */
734 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
736 /* Stolen from help_cmd_list. We don't directly use
737 * help_cmd_list because it doesn't let us print out
740 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
741 print_doc_line (stream
, c
->doc
);
742 fputs_filtered ("\n", stream
);
745 /* Check if this command has subcommands */
746 if (c
->prefixlist
!= NULL
)
748 /* Recursively call ourselves on the subcommand list,
749 passing the right prefix in.
751 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
756 /* This command really has to deal with two things:
757 * 1) I want documentation on *this string* (usually called by
758 * "help commandname").
759 * 2) I want documentation on *this list* (usually called by
760 * giving a command that requires subcommands. Also called by saying
763 * I am going to split this into two seperate comamnds, help_cmd and
768 help_cmd (char *command
, struct ui_file
*stream
)
770 struct cmd_list_element
*c
;
771 extern struct cmd_list_element
*cmdlist
;
775 help_list (cmdlist
, "", all_classes
, stream
);
779 if (strcmp (command
, "all") == 0)
785 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
790 /* There are three cases here.
791 If c->prefixlist is nonzero, we have a prefix command.
792 Print its documentation, then list its subcommands.
794 If c->func is non NULL, we really have a command. Print its
795 documentation and return.
797 If c->func is NULL, we have a class name. Print its
798 documentation (as if it were a command) and then set class to the
799 number of this class so that the commands in the class will be
802 fputs_filtered (c
->doc
, stream
);
803 fputs_filtered ("\n", stream
);
805 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
807 fprintf_filtered (stream
, "\n");
809 /* If this is a prefix command, print it's subcommands */
811 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
813 /* If this is a class name, print all of the commands in the class */
815 help_list (cmdlist
, "", c
->class, stream
);
817 if (c
->hook_pre
|| c
->hook_post
)
818 fprintf_filtered (stream
,
819 "\nThis command has a hook (or hooks) defined:\n");
822 fprintf_filtered (stream
,
823 "\tThis command is run after : %s (pre hook)\n",
826 fprintf_filtered (stream
,
827 "\tThis command is run before : %s (post hook)\n",
832 * Get a specific kind of help on a command list.
835 * CMDTYPE is the prefix to use in the title string.
836 * CLASS is the class with which to list the nodes of this list (see
837 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
838 * everything, ALL_CLASSES for just classes, and non-negative for only things
839 * in a specific class.
840 * and STREAM is the output stream on which to print things.
841 * If you call this routine with a class >= 0, it recurses.
844 help_list (struct cmd_list_element
*list
, char *cmdtype
,
845 enum command_class
class, struct ui_file
*stream
)
848 char *cmdtype1
, *cmdtype2
;
850 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
851 len
= strlen (cmdtype
);
852 cmdtype1
= (char *) alloca (len
+ 1);
854 cmdtype2
= (char *) alloca (len
+ 4);
859 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
861 strncpy (cmdtype2
, cmdtype
, len
- 1);
862 strcpy (cmdtype2
+ len
- 1, " sub");
865 if (class == all_classes
)
866 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
868 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
870 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
872 if (class == all_classes
)
874 fprintf_filtered (stream
, "\n\
875 Type \"help%s\" followed by a class name for a list of commands in ",
878 fprintf_filtered (stream
, "that class.");
881 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
884 fputs_filtered ("for ", stream
);
886 fputs_filtered ("full ", stream
);
888 fputs_filtered ("documentation.\n", stream
);
889 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
894 help_all (struct ui_file
*stream
)
896 struct cmd_list_element
*c
;
897 extern struct cmd_list_element
*cmdlist
;
899 for (c
= cmdlist
; c
; c
= c
->next
)
903 /* If this is a prefix command, print it's subcommands */
905 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
907 /* If this is a class name, print all of the commands in the class */
908 else if (c
->func
== NULL
)
909 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
913 /* Print only the first line of STR on STREAM. */
915 print_doc_line (struct ui_file
*stream
, char *str
)
917 static char *line_buffer
= 0;
918 static int line_size
;
924 line_buffer
= (char *) xmalloc (line_size
);
928 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
930 if (p
- str
> line_size
- 1)
932 line_size
= p
- str
+ 1;
934 line_buffer
= (char *) xmalloc (line_size
);
936 strncpy (line_buffer
, str
, p
- str
);
937 line_buffer
[p
- str
] = '\0';
938 if (islower (line_buffer
[0]))
939 line_buffer
[0] = toupper (line_buffer
[0]);
940 ui_out_text (uiout
, line_buffer
);
944 * Implement a help command on command list LIST.
945 * RECURSE should be non-zero if this should be done recursively on
946 * all sublists of LIST.
947 * PREFIX is the prefix to print before each command name.
948 * STREAM is the stream upon which the output should be written.
950 * A non-negative class number to list only commands in that
952 * ALL_COMMANDS to list all commands in list.
953 * ALL_CLASSES to list all classes in list.
955 * Note that RECURSE will be active on *all* sublists, not just the
956 * ones selected by the criteria above (ie. the selection mechanism
957 * is at the low level, not the high-level).
960 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
961 char *prefix
, int recurse
, struct ui_file
*stream
)
963 struct cmd_list_element
*c
;
965 for (c
= list
; c
; c
= c
->next
)
967 if (c
->abbrev_flag
== 0 &&
968 (class == all_commands
969 || (class == all_classes
&& c
->func
== NULL
)
970 || (class == c
->class && c
->func
!= NULL
)))
972 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
973 print_doc_line (stream
, c
->doc
);
974 fputs_filtered ("\n", stream
);
977 && c
->prefixlist
!= 0
978 && c
->abbrev_flag
== 0)
979 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
984 /* Search the input clist for 'command'. Return the command if
985 found (or NULL if not), and return the number of commands
988 static struct cmd_list_element
*
989 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
990 int ignore_help_classes
, int *nfound
)
992 struct cmd_list_element
*found
, *c
;
994 found
= (struct cmd_list_element
*) NULL
;
996 for (c
= clist
; c
; c
= c
->next
)
997 if (!strncmp (command
, c
->name
, len
)
998 && (!ignore_help_classes
|| c
->func
))
1002 if (c
->name
[len
] == '\0')
1011 /* This routine takes a line of TEXT and a CLIST in which to start the
1012 lookup. When it returns it will have incremented the text pointer past
1013 the section of text it matched, set *RESULT_LIST to point to the list in
1014 which the last word was matched, and will return a pointer to the cmd
1015 list element which the text matches. It will return NULL if no match at
1016 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1017 matches are possible; in this case *RESULT_LIST will be set to point to
1018 the list in which there are ambiguous choices (and *TEXT will be set to
1019 the ambiguous text string).
1021 If the located command was an abbreviation, this routine returns the base
1022 command of the abbreviation.
1024 It does no error reporting whatsoever; control will always return
1025 to the superior routine.
1027 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1028 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1029 if no prefix command was ever found. For example, in the case of "info a",
1030 "info" matches without ambiguity, but "a" could be "args" or "address", so
1031 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1032 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1033 list; it simply points to a specific command. In the case of an ambiguous
1034 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1035 "info t" can be "info types" or "info target"; upon return *TEXT has been
1036 advanced past "info ").
1038 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1039 affect the operation).
1041 This routine does *not* modify the text pointed to by TEXT.
1043 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1044 are actually help classes rather than commands (i.e. the function field of
1045 the struct cmd_list_element is NULL). */
1047 struct cmd_list_element
*
1048 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
1049 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1052 int len
, tmp
, nfound
;
1053 struct cmd_list_element
*found
, *c
;
1056 while (**text
== ' ' || **text
== '\t')
1059 /* Treating underscores as part of command words is important
1060 so that "set args_foo()" doesn't get interpreted as
1061 "set args _foo()". */
1062 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1065 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1068 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1070 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1074 /* If nothing but whitespace, return 0. */
1080 /* *text and p now bracket the first command word to lookup (and
1081 it's length is len). We copy this into a local temporary */
1084 command
= (char *) alloca (len
+ 1);
1085 for (tmp
= 0; tmp
< len
; tmp
++)
1087 char x
= (*text
)[tmp
];
1090 command
[len
] = '\0';
1095 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1098 ** We didn't find the command in the entered case, so lower case it
1099 ** and search again.
1101 if (!found
|| nfound
== 0)
1103 for (tmp
= 0; tmp
< len
; tmp
++)
1105 char x
= command
[tmp
];
1106 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1108 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1111 /* If nothing matches, we have a simple failure. */
1117 if (result_list
!= NULL
)
1118 /* Will be modified in calling routine
1119 if we know what the prefix command is. */
1121 return (struct cmd_list_element
*) -1; /* Ambiguous. */
1124 /* We've matched something on this list. Move text pointer forward. */
1128 if (found
->cmd_pointer
)
1130 /* We drop the alias (abbreviation) in favor of the command it is
1131 pointing to. If the alias is deprecated, though, we need to
1132 warn the user about it before we drop it. Note that while we
1133 are warning about the alias, we may also warn about the command
1134 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1137 if (found
->flags
& DEPRECATED_WARN_USER
)
1138 deprecated_cmd_warning (&line
);
1139 found
= found
->cmd_pointer
;
1141 /* If we found a prefix command, keep looking. */
1143 if (found
->prefixlist
)
1145 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1146 ignore_help_classes
);
1149 /* Didn't find anything; this is as far as we got. */
1150 if (result_list
!= NULL
)
1151 *result_list
= clist
;
1154 else if (c
== (struct cmd_list_element
*) -1)
1156 /* We've gotten this far properly, but the next step
1157 is ambiguous. We need to set the result list to the best
1158 we've found (if an inferior hasn't already set it). */
1159 if (result_list
!= NULL
)
1161 /* This used to say *result_list = *found->prefixlist
1162 If that was correct, need to modify the documentation
1163 at the top of this function to clarify what is supposed
1165 *result_list
= found
;
1176 if (result_list
!= NULL
)
1177 *result_list
= clist
;
1182 /* All this hair to move the space to the front of cmdtype */
1185 undef_cmd_error (char *cmdtype
, char *q
)
1187 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1190 *cmdtype
? " " : "",
1191 (int) strlen (cmdtype
) - 1,
1195 /* Look up the contents of *LINE as a command in the command list LIST.
1196 LIST is a chain of struct cmd_list_element's.
1197 If it is found, return the struct cmd_list_element for that command
1198 and update *LINE to point after the command name, at the first argument.
1199 If not found, call error if ALLOW_UNKNOWN is zero
1200 otherwise (or if error returns) return zero.
1201 Call error if specified command is ambiguous,
1202 unless ALLOW_UNKNOWN is negative.
1203 CMDTYPE precedes the word "command" in the error message.
1205 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1206 elements which are actually help classes rather than commands (i.e.
1207 the function field of the struct cmd_list_element is 0). */
1209 struct cmd_list_element
*
1210 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1211 int allow_unknown
, int ignore_help_classes
)
1213 struct cmd_list_element
*last_list
= 0;
1214 struct cmd_list_element
*c
=
1215 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1217 /* Note: Do not remove trailing whitespace here because this
1218 would be wrong for complete_command. Jim Kingdon */
1225 error (_("Lack of needed %scommand"), cmdtype
);
1228 char *p
= *line
, *q
;
1230 while (isalnum (*p
) || *p
== '-')
1233 q
= (char *) alloca (p
- *line
+ 1);
1234 strncpy (q
, *line
, p
- *line
);
1235 q
[p
- *line
] = '\0';
1236 undef_cmd_error (cmdtype
, q
);
1242 else if (c
== (struct cmd_list_element
*) -1)
1244 /* Ambigous. Local values should be off prefixlist or called
1246 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1248 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1249 struct cmd_list_element
*local_list
=
1250 (last_list
? *(last_list
->prefixlist
) : list
);
1252 if (local_allow_unknown
< 0)
1255 return last_list
; /* Found something. */
1257 return 0; /* Found nothing. */
1261 /* Report as error. */
1266 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1267 && (*line
)[amb_len
] != '\t');
1272 for (c
= local_list
; c
; c
= c
->next
)
1273 if (!strncmp (*line
, c
->name
, amb_len
))
1275 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1277 if (strlen (ambbuf
))
1278 strcat (ambbuf
, ", ");
1279 strcat (ambbuf
, c
->name
);
1283 strcat (ambbuf
, "..");
1287 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1289 return 0; /* lint */
1294 /* We've got something. It may still not be what the caller
1295 wants (if this command *needs* a subcommand). */
1296 while (**line
== ' ' || **line
== '\t')
1299 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1300 undef_cmd_error (c
->prefixname
, *line
);
1302 /* Seems to be what he wants. Return it. */
1308 /* We are here presumably because an alias or command in *TEXT is
1309 deprecated and a warning message should be generated. This function
1310 decodes *TEXT and potentially generates a warning message as outlined
1313 Example for 'set endian big' which has a fictitious alias 'seb'.
1315 If alias wasn't used in *TEXT, and the command is deprecated:
1316 "warning: 'set endian big' is deprecated."
1318 If alias was used, and only the alias is deprecated:
1319 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1321 If alias was used and command is deprecated (regardless of whether the
1322 alias itself is deprecated:
1324 "warning: 'set endian big' (seb) is deprecated."
1326 After the message has been sent, clear the appropriate flags in the
1327 command and/or the alias so the user is no longer bothered.
1331 deprecated_cmd_warning (char **text
)
1333 struct cmd_list_element
*alias
= NULL
;
1334 struct cmd_list_element
*prefix_cmd
= NULL
;
1335 struct cmd_list_element
*cmd
= NULL
;
1336 struct cmd_list_element
*c
;
1339 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1340 /* return if text doesn't evaluate to a command */
1343 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1344 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1345 /* return if nothing is deprecated */
1348 printf_filtered ("Warning:");
1350 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1351 printf_filtered (" '%s', an alias for the", alias
->name
);
1353 printf_filtered (" command '");
1356 printf_filtered ("%s", prefix_cmd
->prefixname
);
1358 printf_filtered ("%s", cmd
->name
);
1360 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1361 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1363 printf_filtered ("' is deprecated.\n");
1366 /* if it is only the alias that is deprecated, we want to indicate the
1367 new alias, otherwise we'll indicate the new command */
1369 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1371 if (alias
->replacement
)
1372 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1374 printf_filtered ("No alternative known.\n\n");
1378 if (cmd
->replacement
)
1379 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1381 printf_filtered ("No alternative known.\n\n");
1384 /* We've warned you, now we'll keep quiet */
1386 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1388 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1393 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1394 Return 1 on success, 0 on failure.
1396 If LINE refers to an alias, *alias will point to that alias.
1398 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1399 command) set *prefix_cmd.
1401 Set *cmd to point to the command LINE indicates.
1403 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1404 exist, they are NULL when we return.
1408 lookup_cmd_composition (char *text
,
1409 struct cmd_list_element
**alias
,
1410 struct cmd_list_element
**prefix_cmd
,
1411 struct cmd_list_element
**cmd
)
1414 int len
, tmp
, nfound
;
1415 struct cmd_list_element
*cur_list
;
1416 struct cmd_list_element
*prev_cmd
;
1425 /* Go through as many command lists as we need to
1426 to find the command TEXT refers to. */
1430 while (*text
== ' ' || *text
== '\t')
1433 /* Treating underscores as part of command words is important
1434 so that "set args_foo()" doesn't get interpreted as
1435 "set args _foo()". */
1436 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1439 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1442 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1444 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1448 /* If nothing but whitespace, return. */
1454 /* text and p now bracket the first command word to lookup (and
1455 it's length is len). We copy this into a local temporary */
1457 command
= (char *) alloca (len
+ 1);
1458 for (tmp
= 0; tmp
< len
; tmp
++)
1463 command
[len
] = '\0';
1468 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1470 /* We didn't find the command in the entered case, so lower case it
1473 if (!*cmd
|| nfound
== 0)
1475 for (tmp
= 0; tmp
< len
; tmp
++)
1477 char x
= command
[tmp
];
1478 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1480 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1483 if (*cmd
== (struct cmd_list_element
*) -1)
1485 return 0; /* ambiguous */
1489 return 0; /* nothing found */
1492 if ((*cmd
)->cmd_pointer
)
1494 /* cmd was actually an alias, we note that an alias was used
1495 (by assigning *alais) and we set *cmd.
1498 *cmd
= (*cmd
)->cmd_pointer
;
1500 *prefix_cmd
= prev_cmd
;
1502 if ((*cmd
)->prefixlist
)
1503 cur_list
= *(*cmd
)->prefixlist
;
1511 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1513 /* Return a vector of char pointers which point to the different
1514 possible completions in LIST of TEXT.
1516 WORD points in the same buffer as TEXT, and completions should be
1517 returned relative to this position. For example, suppose TEXT is "foo"
1518 and we want to complete to "foobar". If WORD is "oo", return
1519 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1522 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1524 struct cmd_list_element
*ptr
;
1526 int sizeof_matchlist
;
1528 int textlen
= strlen (text
);
1530 sizeof_matchlist
= 10;
1531 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1534 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1535 if (!strncmp (ptr
->name
, text
, textlen
)
1536 && !ptr
->abbrev_flag
1538 || ptr
->prefixlist
))
1540 if (matches
== sizeof_matchlist
)
1542 sizeof_matchlist
*= 2;
1543 matchlist
= (char **) xrealloc ((char *) matchlist
,
1545 * sizeof (char *)));
1548 matchlist
[matches
] = (char *)
1549 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1551 strcpy (matchlist
[matches
], ptr
->name
);
1552 else if (word
> text
)
1554 /* Return some portion of ptr->name. */
1555 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1559 /* Return some of text plus ptr->name. */
1560 strncpy (matchlist
[matches
], word
, text
- word
);
1561 matchlist
[matches
][text
- word
] = '\0';
1562 strcat (matchlist
[matches
], ptr
->name
);
1574 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1575 * sizeof (char *)));
1576 matchlist
[matches
] = (char *) 0;
1582 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1584 /* Return a vector of char pointers which point to the different
1585 possible completions in CMD of TEXT.
1587 WORD points in the same buffer as TEXT, and completions should be
1588 returned relative to this position. For example, suppose TEXT is "foo"
1589 and we want to complete to "foobar". If WORD is "oo", return
1590 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1593 complete_on_enum (const char *enumlist
[],
1598 int sizeof_matchlist
;
1600 int textlen
= strlen (text
);
1604 sizeof_matchlist
= 10;
1605 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1608 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1609 if (strncmp (name
, text
, textlen
) == 0)
1611 if (matches
== sizeof_matchlist
)
1613 sizeof_matchlist
*= 2;
1614 matchlist
= (char **) xrealloc ((char *) matchlist
,
1616 * sizeof (char *)));
1619 matchlist
[matches
] = (char *)
1620 xmalloc (strlen (word
) + strlen (name
) + 1);
1622 strcpy (matchlist
[matches
], name
);
1623 else if (word
> text
)
1625 /* Return some portion of name. */
1626 strcpy (matchlist
[matches
], name
+ (word
- text
));
1630 /* Return some of text plus name. */
1631 strncpy (matchlist
[matches
], word
, text
- word
);
1632 matchlist
[matches
][text
- word
] = '\0';
1633 strcat (matchlist
[matches
], name
);
1645 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1646 * sizeof (char *)));
1647 matchlist
[matches
] = (char *) 0;
1654 /* check function pointer */
1656 cmd_func_p (struct cmd_list_element
*cmd
)
1658 return (cmd
->func
!= NULL
);
1662 /* call the command function */
1664 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1666 if (cmd_func_p (cmd
))
1667 (*cmd
->func
) (cmd
, args
, from_tty
);
1669 error (_("Invalid command"));