1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002 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"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-decode.h"
31 #include "gdb_assert.h"
33 /* Prototypes for local functions */
35 static void undef_cmd_error (char *, char *);
37 static struct cmd_list_element
*find_cmd (char *command
,
39 struct cmd_list_element
*clist
,
40 int ignore_help_classes
,
43 static void help_all (struct ui_file
*stream
);
45 /* Set the callback function for the specified command. For each both
46 the commands callback and func() are set. The latter set to a
47 bounce function (unless cfunc / sfunc is NULL that is). */
50 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
52 c
->function
.cfunc (args
, from_tty
); /* Ok. */
56 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
62 cmd
->function
.cfunc
= cfunc
; /* Ok. */
66 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
68 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
72 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
78 cmd
->function
.sfunc
= sfunc
; /* Ok. */
82 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
83 void (*cfunc
) (char *args
, int from_tty
))
85 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
89 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
91 cmd
->context
= context
;
95 get_cmd_context (struct cmd_list_element
*cmd
)
101 cmd_type (struct cmd_list_element
*cmd
)
107 set_cmd_completer (struct cmd_list_element
*cmd
,
108 char **(*completer
) (char *text
, char *word
))
110 cmd
->completer
= completer
; /* Ok. */
114 /* Add element named NAME.
115 CLASS is the top level category into which commands are broken down
117 FUN should be the function to execute the command;
118 it will get a character string as argument, with leading
119 and trailing blanks already eliminated.
121 DOC is a documentation string for the command.
122 Its first line should be a complete sentence.
123 It should start with ? for a command that is an abbreviation
124 or with * for a command that most users don't need to know about.
126 Add this command to command list *LIST.
128 Returns a pointer to the added command (not necessarily the head
131 struct cmd_list_element
*
132 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
133 char *doc
, struct cmd_list_element
**list
)
135 register struct cmd_list_element
*c
136 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
137 struct cmd_list_element
*p
;
139 delete_cmd (name
, list
);
141 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
149 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
159 set_cmd_cfunc (c
, fun
);
160 set_cmd_context (c
, NULL
);
163 c
->replacement
= NULL
;
164 c
->pre_show_hook
= NULL
;
168 c
->prefixlist
= NULL
;
169 c
->prefixname
= NULL
;
170 c
->allow_unknown
= 0;
172 set_cmd_completer (c
, make_symbol_completion_list
);
173 c
->type
= not_set_cmd
;
175 c
->var_type
= var_boolean
;
177 c
->user_commands
= NULL
;
178 c
->hookee_pre
= NULL
;
179 c
->hookee_post
= NULL
;
180 c
->cmd_pointer
= NULL
;
185 /* Same as above, except that the abbrev_flag is set. */
186 /* Note: Doesn't seem to be used anywhere currently. */
188 struct cmd_list_element
*
189 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
190 char *doc
, struct cmd_list_element
**list
)
192 register struct cmd_list_element
*c
193 = add_cmd (name
, class, fun
, doc
, list
);
199 /* Deprecates a command CMD.
200 REPLACEMENT is the name of the command which should be used in place
201 of this command, or NULL if no such command exists.
203 This function does not check to see if command REPLACEMENT exists
204 since gdb may not have gotten around to adding REPLACEMENT when this
207 Returns a pointer to the deprecated command. */
209 struct cmd_list_element
*
210 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
212 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
214 if (replacement
!= NULL
)
215 cmd
->replacement
= replacement
;
217 cmd
->replacement
= NULL
;
222 struct cmd_list_element
*
223 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
224 int abbrev_flag
, struct cmd_list_element
**list
)
226 /* Must do this since lookup_cmd tries to side-effect its first arg */
228 register struct cmd_list_element
*old
;
229 register struct cmd_list_element
*c
;
230 copied_name
= (char *) alloca (strlen (oldname
) + 1);
231 strcpy (copied_name
, oldname
);
232 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
236 delete_cmd (name
, list
);
240 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
241 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
243 c
->function
= old
->function
;
244 c
->prefixlist
= old
->prefixlist
;
245 c
->prefixname
= old
->prefixname
;
246 c
->allow_unknown
= old
->allow_unknown
;
247 c
->abbrev_flag
= abbrev_flag
;
248 c
->cmd_pointer
= old
;
252 /* Like add_cmd but adds an element for a command prefix:
253 a name that should be followed by a subcommand to be looked up
254 in another command list. PREFIXLIST should be the address
255 of the variable containing that list. */
257 struct cmd_list_element
*
258 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
259 char *doc
, struct cmd_list_element
**prefixlist
,
260 char *prefixname
, int allow_unknown
,
261 struct cmd_list_element
**list
)
263 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
264 c
->prefixlist
= prefixlist
;
265 c
->prefixname
= prefixname
;
266 c
->allow_unknown
= allow_unknown
;
270 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
272 struct cmd_list_element
*
273 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
274 void (*fun
) (char *, int), char *doc
,
275 struct cmd_list_element
**prefixlist
, char *prefixname
,
276 int allow_unknown
, struct cmd_list_element
**list
)
278 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
279 c
->prefixlist
= prefixlist
;
280 c
->prefixname
= prefixname
;
281 c
->allow_unknown
= allow_unknown
;
286 /* This is an empty "cfunc". */
288 not_just_help_class_command (char *args
, int from_tty
)
292 /* This is an empty "sfunc". */
293 static void empty_sfunc (char *, int, struct cmd_list_element
*);
296 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
300 /* Add element named NAME to command list LIST (the list for set/show
301 or some sublist thereof).
302 TYPE is set_cmd or show_cmd.
303 CLASS is as in add_cmd.
304 VAR_TYPE is the kind of thing we are setting.
305 VAR is address of the variable being controlled by this command.
306 DOC is the documentation string. */
308 static struct cmd_list_element
*
309 add_set_or_show_cmd (char *name
,
311 enum command_class
class,
315 struct cmd_list_element
**list
)
317 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
318 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
320 c
->var_type
= var_type
;
322 /* This needs to be something besides NULL so that this isn't
323 treated as a help class. */
324 set_cmd_sfunc (c
, empty_sfunc
);
328 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
329 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
330 setting. VAR is address of the variable being controlled by this
331 command. SET_FUNC and SHOW_FUNC are the callback functions (if
332 non-NULL). SET_DOC and SHOW_DOC are the documentation strings. */
334 static struct cmd_list_element
*
335 add_setshow_cmd (char *name
,
336 enum command_class
class,
337 var_types var_type
, void *var
,
338 char *set_doc
, char *show_doc
,
339 cmd_sfunc_ftype
*set_func
, cmd_sfunc_ftype
*show_func
,
340 struct cmd_list_element
**set_list
,
341 struct cmd_list_element
**show_list
)
343 struct cmd_list_element
*set
;
344 struct cmd_list_element
*show
;
345 set
= add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
,
347 if (set_func
!= NULL
)
348 set_cmd_sfunc (set
, set_func
);
349 show
= add_set_or_show_cmd (name
, show_cmd
, class, var_type
, var
,
350 show_doc
, show_list
);
351 if (show_func
!= NULL
)
352 set_cmd_sfunc (show
, show_func
);
353 /* The caller often wants to modify set to include info like an
358 struct cmd_list_element
*
359 add_set_cmd (char *name
,
360 enum command_class
class,
364 struct cmd_list_element
**list
)
366 return add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
, doc
, list
);
369 /* Add element named NAME to command list LIST (the list for set
370 or some sublist thereof).
371 CLASS is as in add_cmd.
372 ENUMLIST is a list of strings which may follow NAME.
373 VAR is address of the variable which will contain the matching string
375 DOC is the documentation string. */
377 struct cmd_list_element
*
378 add_set_enum_cmd (char *name
,
379 enum command_class
class,
380 const char *enumlist
[],
383 struct cmd_list_element
**list
)
385 struct cmd_list_element
*c
386 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
392 /* Add an auto-boolean command named NAME to both the set and show
393 command list lists. CLASS is as in add_cmd. VAR is address of the
394 variable which will contain the value. DOC is the documentation
395 string. FUNC is the corresponding callback. */
397 add_setshow_auto_boolean_cmd (char *name
,
398 enum command_class
class,
399 enum auto_boolean
*var
,
400 char *set_doc
, char *show_doc
,
401 cmd_sfunc_ftype
*set_func
,
402 cmd_sfunc_ftype
*show_func
,
403 struct cmd_list_element
**set_list
,
404 struct cmd_list_element
**show_list
)
406 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
407 struct cmd_list_element
*c
;
408 c
= add_setshow_cmd (name
, class, var_auto_boolean
, var
,
409 set_doc
, show_doc
, set_func
, show_func
,
410 set_list
, show_list
);
411 c
->enums
= auto_boolean_enums
;
414 /* Add element named NAME to both the set and show command LISTs (the
415 list for set/show or some sublist thereof). CLASS is as in
416 add_cmd. VAR is address of the variable which will contain the
417 value. SET_DOC and SHOW_DOR are the documentation strings. */
419 add_setshow_boolean_cmd (char *name
,
420 enum command_class
class,
421 int *var
, char *set_doc
, char *show_doc
,
422 cmd_sfunc_ftype
*set_func
,
423 cmd_sfunc_ftype
*show_func
,
424 struct cmd_list_element
**set_list
,
425 struct cmd_list_element
**show_list
)
427 static const char *boolean_enums
[] = { "on", "off", NULL
};
428 struct cmd_list_element
*c
;
429 c
= add_setshow_cmd (name
, class, var_boolean
, var
,
432 set_list
, show_list
);
433 c
->enums
= boolean_enums
;
436 /* Where SETCMD has already been added, add the corresponding show
437 command to LIST and return a pointer to the added command (not
438 necessarily the head of LIST). */
439 /* NOTE: cagney/2002-03-17: The original version of add_show_from_set
440 used memcpy() to clone `set' into `show'. This ment that in
441 addition to all the needed fields (var, name, et.al.) some
442 unnecessary fields were copied (namely the callback function). The
443 function explictly copies relevant fields. For a `set' and `show'
444 command to share the same callback, the caller must set both
446 struct cmd_list_element
*
447 add_show_from_set (struct cmd_list_element
*setcmd
,
448 struct cmd_list_element
**list
)
451 const static char setstring
[] = "Set ";
453 /* Create a doc string by replacing "Set " at the start of the
454 `set'' command's doco with "Show ". */
455 gdb_assert (strncmp (setcmd
->doc
, setstring
, sizeof (setstring
) - 1) == 0);
456 doc
= concat ("Show ", setcmd
->doc
+ sizeof (setstring
) - 1, NULL
);
458 /* Insert the basic command. */
459 return add_set_or_show_cmd (setcmd
->name
, show_cmd
, setcmd
->class,
460 setcmd
->var_type
, setcmd
->var
, doc
, list
);
463 /* Remove the command named NAME from the command list. */
466 delete_cmd (char *name
, struct cmd_list_element
**list
)
468 register struct cmd_list_element
*c
;
469 struct cmd_list_element
*p
;
471 while (*list
&& STREQ ((*list
)->name
, name
))
473 if ((*list
)->hookee_pre
)
474 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
475 if ((*list
)->hookee_post
)
476 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
483 for (c
= *list
; c
->next
;)
485 if (STREQ (c
->next
->name
, name
))
487 if (c
->next
->hookee_pre
)
488 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
489 if (c
->next
->hookee_post
)
490 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
491 /* :( no fishing metaphore */
501 /* Shorthands to the commands above. */
503 /* Add an element to the list of info subcommands. */
505 struct cmd_list_element
*
506 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
508 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
511 /* Add an alias to the list of info subcommands. */
513 struct cmd_list_element
*
514 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
516 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
519 /* Add an element to the list of commands. */
521 struct cmd_list_element
*
522 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
525 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
528 /* Add an alias or abbreviation command to the list of commands. */
530 struct cmd_list_element
*
531 add_com_alias (char *name
, char *oldname
, enum command_class
class,
534 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
537 /* Recursively walk the commandlist structures, and print out the
538 documentation of commands that match our regex in either their
539 name, or their documentation.
542 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
543 struct re_pattern_buffer
*regex
, char *prefix
)
545 register struct cmd_list_element
*c
;
546 int returnvalue
=1; /*Needed to avoid double printing*/
547 /* Walk through the commands */
548 for (c
=commandlist
;c
;c
=c
->next
)
552 /* Try to match against the name*/
553 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
554 if (returnvalue
>= 0)
556 /* Stolen from help_cmd_list. We don't directly use
557 * help_cmd_list because it doesn't let us print out
560 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
561 print_doc_line (stream
, c
->doc
);
562 fputs_filtered ("\n", stream
);
563 returnvalue
=0; /*Set this so we don't print it again.*/
566 if (c
->doc
!= NULL
&& returnvalue
!= 0)
568 /* Try to match against documentation */
569 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
571 /* Stolen from help_cmd_list. We don't directly use
572 * help_cmd_list because it doesn't let us print out
575 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
576 print_doc_line (stream
, c
->doc
);
577 fputs_filtered ("\n", stream
);
580 /* Check if this command has subcommands */
581 if (c
->prefixlist
!= NULL
)
583 /* Recursively call ourselves on the subcommand list,
584 passing the right prefix in.
586 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
591 /* This command really has to deal with two things:
592 * 1) I want documentation on *this string* (usually called by
593 * "help commandname").
594 * 2) I want documentation on *this list* (usually called by
595 * giving a command that requires subcommands. Also called by saying
598 * I am going to split this into two seperate comamnds, help_cmd and
603 help_cmd (char *command
, struct ui_file
*stream
)
605 struct cmd_list_element
*c
;
606 extern struct cmd_list_element
*cmdlist
;
610 help_list (cmdlist
, "", all_classes
, stream
);
614 if (strcmp (command
, "all") == 0)
620 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
625 /* There are three cases here.
626 If c->prefixlist is nonzero, we have a prefix command.
627 Print its documentation, then list its subcommands.
629 If c->func is non NULL, we really have a command. Print its
630 documentation and return.
632 If c->func is NULL, we have a class name. Print its
633 documentation (as if it were a command) and then set class to the
634 number of this class so that the commands in the class will be
637 fputs_filtered (c
->doc
, stream
);
638 fputs_filtered ("\n", stream
);
640 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
642 fprintf_filtered (stream
, "\n");
644 /* If this is a prefix command, print it's subcommands */
646 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
648 /* If this is a class name, print all of the commands in the class */
650 help_list (cmdlist
, "", c
->class, stream
);
652 if (c
->hook_pre
|| c
->hook_post
)
653 fprintf_filtered (stream
,
654 "\nThis command has a hook (or hooks) defined:\n");
657 fprintf_filtered (stream
,
658 "\tThis command is run after : %s (pre hook)\n",
661 fprintf_filtered (stream
,
662 "\tThis command is run before : %s (post hook)\n",
667 * Get a specific kind of help on a command list.
670 * CMDTYPE is the prefix to use in the title string.
671 * CLASS is the class with which to list the nodes of this list (see
672 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
673 * everything, ALL_CLASSES for just classes, and non-negative for only things
674 * in a specific class.
675 * and STREAM is the output stream on which to print things.
676 * If you call this routine with a class >= 0, it recurses.
679 help_list (struct cmd_list_element
*list
, char *cmdtype
,
680 enum command_class
class, struct ui_file
*stream
)
683 char *cmdtype1
, *cmdtype2
;
685 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
686 len
= strlen (cmdtype
);
687 cmdtype1
= (char *) alloca (len
+ 1);
689 cmdtype2
= (char *) alloca (len
+ 4);
694 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
696 strncpy (cmdtype2
, cmdtype
, len
- 1);
697 strcpy (cmdtype2
+ len
- 1, " sub");
700 if (class == all_classes
)
701 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
703 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
705 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
707 if (class == all_classes
)
709 fprintf_filtered (stream
, "\n\
710 Type \"help%s\" followed by a class name for a list of commands in ",
713 fprintf_filtered (stream
, "that class.");
716 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
719 fputs_filtered ("for ", stream
);
721 fputs_filtered ("full ", stream
);
723 fputs_filtered ("documentation.\n", stream
);
724 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
729 help_all (struct ui_file
*stream
)
731 struct cmd_list_element
*c
;
732 extern struct cmd_list_element
*cmdlist
;
734 for (c
= cmdlist
; c
; c
= c
->next
)
738 /* If this is a prefix command, print it's subcommands */
740 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
742 /* If this is a class name, print all of the commands in the class */
743 else if (c
->func
== NULL
)
744 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
748 /* Print only the first line of STR on STREAM. */
750 print_doc_line (struct ui_file
*stream
, char *str
)
752 static char *line_buffer
= 0;
753 static int line_size
;
759 line_buffer
= (char *) xmalloc (line_size
);
763 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
765 if (p
- str
> line_size
- 1)
767 line_size
= p
- str
+ 1;
769 line_buffer
= (char *) xmalloc (line_size
);
771 strncpy (line_buffer
, str
, p
- str
);
772 line_buffer
[p
- str
] = '\0';
773 if (islower (line_buffer
[0]))
774 line_buffer
[0] = toupper (line_buffer
[0]);
775 ui_out_text (uiout
, line_buffer
);
779 * Implement a help command on command list LIST.
780 * RECURSE should be non-zero if this should be done recursively on
781 * all sublists of LIST.
782 * PREFIX is the prefix to print before each command name.
783 * STREAM is the stream upon which the output should be written.
785 * A non-negative class number to list only commands in that
787 * ALL_COMMANDS to list all commands in list.
788 * ALL_CLASSES to list all classes in list.
790 * Note that RECURSE will be active on *all* sublists, not just the
791 * ones selected by the criteria above (ie. the selection mechanism
792 * is at the low level, not the high-level).
795 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
796 char *prefix
, int recurse
, struct ui_file
*stream
)
798 register struct cmd_list_element
*c
;
800 for (c
= list
; c
; c
= c
->next
)
802 if (c
->abbrev_flag
== 0 &&
803 (class == all_commands
804 || (class == all_classes
&& c
->func
== NULL
)
805 || (class == c
->class && c
->func
!= NULL
)))
807 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
808 print_doc_line (stream
, c
->doc
);
809 fputs_filtered ("\n", stream
);
812 && c
->prefixlist
!= 0
813 && c
->abbrev_flag
== 0)
814 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
819 /* Search the input clist for 'command'. Return the command if
820 found (or NULL if not), and return the number of commands
823 static struct cmd_list_element
*
824 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
825 int ignore_help_classes
, int *nfound
)
827 struct cmd_list_element
*found
, *c
;
829 found
= (struct cmd_list_element
*) NULL
;
831 for (c
= clist
; c
; c
= c
->next
)
832 if (!strncmp (command
, c
->name
, len
)
833 && (!ignore_help_classes
|| c
->func
))
837 if (c
->name
[len
] == '\0')
846 /* This routine takes a line of TEXT and a CLIST in which to start the
847 lookup. When it returns it will have incremented the text pointer past
848 the section of text it matched, set *RESULT_LIST to point to the list in
849 which the last word was matched, and will return a pointer to the cmd
850 list element which the text matches. It will return NULL if no match at
851 all was possible. It will return -1 (cast appropriately, ick) if ambigous
852 matches are possible; in this case *RESULT_LIST will be set to point to
853 the list in which there are ambiguous choices (and *TEXT will be set to
854 the ambiguous text string).
856 If the located command was an abbreviation, this routine returns the base
857 command of the abbreviation.
859 It does no error reporting whatsoever; control will always return
860 to the superior routine.
862 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
863 at the prefix_command (ie. the best match) *or* (special case) will be NULL
864 if no prefix command was ever found. For example, in the case of "info a",
865 "info" matches without ambiguity, but "a" could be "args" or "address", so
866 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
867 RESULT_LIST should not be interpeted as a pointer to the beginning of a
868 list; it simply points to a specific command. In the case of an ambiguous
869 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
870 "info t" can be "info types" or "info target"; upon return *TEXT has been
871 advanced past "info ").
873 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
874 affect the operation).
876 This routine does *not* modify the text pointed to by TEXT.
878 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
879 are actually help classes rather than commands (i.e. the function field of
880 the struct cmd_list_element is NULL). */
882 struct cmd_list_element
*
883 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
884 struct cmd_list_element
**result_list
, int ignore_help_classes
)
887 int len
, tmp
, nfound
;
888 struct cmd_list_element
*found
, *c
;
891 while (**text
== ' ' || **text
== '\t')
894 /* Treating underscores as part of command words is important
895 so that "set args_foo()" doesn't get interpreted as
896 "set args _foo()". */
898 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
900 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
901 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
905 /* If nothing but whitespace, return 0. */
911 /* *text and p now bracket the first command word to lookup (and
912 it's length is len). We copy this into a local temporary */
915 command
= (char *) alloca (len
+ 1);
916 for (tmp
= 0; tmp
< len
; tmp
++)
918 char x
= (*text
)[tmp
];
926 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
929 ** We didn't find the command in the entered case, so lower case it
932 if (!found
|| nfound
== 0)
934 for (tmp
= 0; tmp
< len
; tmp
++)
936 char x
= command
[tmp
];
937 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
939 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
942 /* If nothing matches, we have a simple failure. */
948 if (result_list
!= NULL
)
949 /* Will be modified in calling routine
950 if we know what the prefix command is. */
952 return (struct cmd_list_element
*) -1; /* Ambiguous. */
955 /* We've matched something on this list. Move text pointer forward. */
959 if (found
->cmd_pointer
)
961 /* We drop the alias (abbreviation) in favor of the command it is
962 pointing to. If the alias is deprecated, though, we need to
963 warn the user about it before we drop it. Note that while we
964 are warning about the alias, we may also warn about the command
965 itself and we will adjust the appropriate DEPRECATED_WARN_USER
968 if (found
->flags
& DEPRECATED_WARN_USER
)
969 deprecated_cmd_warning (&line
);
970 found
= found
->cmd_pointer
;
972 /* If we found a prefix command, keep looking. */
974 if (found
->prefixlist
)
976 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
977 ignore_help_classes
);
980 /* Didn't find anything; this is as far as we got. */
981 if (result_list
!= NULL
)
982 *result_list
= clist
;
985 else if (c
== (struct cmd_list_element
*) -1)
987 /* We've gotten this far properly, but the next step
988 is ambiguous. We need to set the result list to the best
989 we've found (if an inferior hasn't already set it). */
990 if (result_list
!= NULL
)
992 /* This used to say *result_list = *found->prefixlist
993 If that was correct, need to modify the documentation
994 at the top of this function to clarify what is supposed
996 *result_list
= found
;
1007 if (result_list
!= NULL
)
1008 *result_list
= clist
;
1013 /* All this hair to move the space to the front of cmdtype */
1016 undef_cmd_error (char *cmdtype
, char *q
)
1018 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
1021 *cmdtype
? " " : "",
1022 (int) strlen (cmdtype
) - 1,
1026 /* Look up the contents of *LINE as a command in the command list LIST.
1027 LIST is a chain of struct cmd_list_element's.
1028 If it is found, return the struct cmd_list_element for that command
1029 and update *LINE to point after the command name, at the first argument.
1030 If not found, call error if ALLOW_UNKNOWN is zero
1031 otherwise (or if error returns) return zero.
1032 Call error if specified command is ambiguous,
1033 unless ALLOW_UNKNOWN is negative.
1034 CMDTYPE precedes the word "command" in the error message.
1036 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1037 elements which are actually help classes rather than commands (i.e.
1038 the function field of the struct cmd_list_element is 0). */
1040 struct cmd_list_element
*
1041 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1042 int allow_unknown
, int ignore_help_classes
)
1044 struct cmd_list_element
*last_list
= 0;
1045 struct cmd_list_element
*c
=
1046 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1048 /* Note: Do not remove trailing whitespace here because this
1049 would be wrong for complete_command. Jim Kingdon */
1056 error ("Lack of needed %scommand", cmdtype
);
1059 char *p
= *line
, *q
;
1061 while (isalnum (*p
) || *p
== '-')
1064 q
= (char *) alloca (p
- *line
+ 1);
1065 strncpy (q
, *line
, p
- *line
);
1066 q
[p
- *line
] = '\0';
1067 undef_cmd_error (cmdtype
, q
);
1073 else if (c
== (struct cmd_list_element
*) -1)
1075 /* Ambigous. Local values should be off prefixlist or called
1077 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1079 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1080 struct cmd_list_element
*local_list
=
1081 (last_list
? *(last_list
->prefixlist
) : list
);
1083 if (local_allow_unknown
< 0)
1086 return last_list
; /* Found something. */
1088 return 0; /* Found nothing. */
1092 /* Report as error. */
1097 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1098 && (*line
)[amb_len
] != '\t');
1103 for (c
= local_list
; c
; c
= c
->next
)
1104 if (!strncmp (*line
, c
->name
, amb_len
))
1106 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1108 if (strlen (ambbuf
))
1109 strcat (ambbuf
, ", ");
1110 strcat (ambbuf
, c
->name
);
1114 strcat (ambbuf
, "..");
1118 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1120 return 0; /* lint */
1125 /* We've got something. It may still not be what the caller
1126 wants (if this command *needs* a subcommand). */
1127 while (**line
== ' ' || **line
== '\t')
1130 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1131 undef_cmd_error (c
->prefixname
, *line
);
1133 /* Seems to be what he wants. Return it. */
1139 /* We are here presumably because an alias or command in *TEXT is
1140 deprecated and a warning message should be generated. This function
1141 decodes *TEXT and potentially generates a warning message as outlined
1144 Example for 'set endian big' which has a fictitious alias 'seb'.
1146 If alias wasn't used in *TEXT, and the command is deprecated:
1147 "warning: 'set endian big' is deprecated."
1149 If alias was used, and only the alias is deprecated:
1150 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1152 If alias was used and command is deprecated (regardless of whether the
1153 alias itself is deprecated:
1155 "warning: 'set endian big' (seb) is deprecated."
1157 After the message has been sent, clear the appropriate flags in the
1158 command and/or the alias so the user is no longer bothered.
1162 deprecated_cmd_warning (char **text
)
1164 struct cmd_list_element
*alias
= NULL
;
1165 struct cmd_list_element
*prefix_cmd
= NULL
;
1166 struct cmd_list_element
*cmd
= NULL
;
1167 struct cmd_list_element
*c
;
1170 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1171 /* return if text doesn't evaluate to a command */
1174 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1175 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1176 /* return if nothing is deprecated */
1179 printf_filtered ("Warning:");
1181 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1182 printf_filtered (" '%s', an alias for the", alias
->name
);
1184 printf_filtered (" command '");
1187 printf_filtered ("%s", prefix_cmd
->prefixname
);
1189 printf_filtered ("%s", cmd
->name
);
1191 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1192 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1194 printf_filtered ("' is deprecated.\n");
1197 /* if it is only the alias that is deprecated, we want to indicate the
1198 new alias, otherwise we'll indicate the new command */
1200 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1202 if (alias
->replacement
)
1203 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1205 printf_filtered ("No alternative known.\n\n");
1209 if (cmd
->replacement
)
1210 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1212 printf_filtered ("No alternative known.\n\n");
1215 /* We've warned you, now we'll keep quiet */
1217 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1219 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1224 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1225 Return 1 on success, 0 on failure.
1227 If LINE refers to an alias, *alias will point to that alias.
1229 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1230 command) set *prefix_cmd.
1232 Set *cmd to point to the command LINE indicates.
1234 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1235 exist, they are NULL when we return.
1239 lookup_cmd_composition (char *text
,
1240 struct cmd_list_element
**alias
,
1241 struct cmd_list_element
**prefix_cmd
,
1242 struct cmd_list_element
**cmd
)
1245 int len
, tmp
, nfound
;
1246 struct cmd_list_element
*cur_list
;
1247 struct cmd_list_element
*prev_cmd
;
1256 /* Go through as many command lists as we need to
1257 to find the command TEXT refers to. */
1261 while (*text
== ' ' || *text
== '\t')
1264 /* Treating underscores as part of command words is important
1265 so that "set args_foo()" doesn't get interpreted as
1266 "set args _foo()". */
1268 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1270 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1271 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1275 /* If nothing but whitespace, return. */
1281 /* text and p now bracket the first command word to lookup (and
1282 it's length is len). We copy this into a local temporary */
1284 command
= (char *) alloca (len
+ 1);
1285 for (tmp
= 0; tmp
< len
; tmp
++)
1290 command
[len
] = '\0';
1295 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1297 /* We didn't find the command in the entered case, so lower case it
1300 if (!*cmd
|| nfound
== 0)
1302 for (tmp
= 0; tmp
< len
; tmp
++)
1304 char x
= command
[tmp
];
1305 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1307 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1310 if (*cmd
== (struct cmd_list_element
*) -1)
1312 return 0; /* ambiguous */
1316 return 0; /* nothing found */
1319 if ((*cmd
)->cmd_pointer
)
1321 /* cmd was actually an alias, we note that an alias was used
1322 (by assigning *alais) and we set *cmd.
1325 *cmd
= (*cmd
)->cmd_pointer
;
1327 *prefix_cmd
= prev_cmd
;
1329 if ((*cmd
)->prefixlist
)
1330 cur_list
= *(*cmd
)->prefixlist
;
1338 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1340 /* Return a vector of char pointers which point to the different
1341 possible completions in LIST of TEXT.
1343 WORD points in the same buffer as TEXT, and completions should be
1344 returned relative to this position. For example, suppose TEXT is "foo"
1345 and we want to complete to "foobar". If WORD is "oo", return
1346 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1349 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1351 struct cmd_list_element
*ptr
;
1353 int sizeof_matchlist
;
1355 int textlen
= strlen (text
);
1357 sizeof_matchlist
= 10;
1358 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1361 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1362 if (!strncmp (ptr
->name
, text
, textlen
)
1363 && !ptr
->abbrev_flag
1365 || ptr
->prefixlist
))
1367 if (matches
== sizeof_matchlist
)
1369 sizeof_matchlist
*= 2;
1370 matchlist
= (char **) xrealloc ((char *) matchlist
,
1372 * sizeof (char *)));
1375 matchlist
[matches
] = (char *)
1376 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1378 strcpy (matchlist
[matches
], ptr
->name
);
1379 else if (word
> text
)
1381 /* Return some portion of ptr->name. */
1382 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1386 /* Return some of text plus ptr->name. */
1387 strncpy (matchlist
[matches
], word
, text
- word
);
1388 matchlist
[matches
][text
- word
] = '\0';
1389 strcat (matchlist
[matches
], ptr
->name
);
1401 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1402 * sizeof (char *)));
1403 matchlist
[matches
] = (char *) 0;
1409 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1411 /* Return a vector of char pointers which point to the different
1412 possible completions in CMD of TEXT.
1414 WORD points in the same buffer as TEXT, and completions should be
1415 returned relative to this position. For example, suppose TEXT is "foo"
1416 and we want to complete to "foobar". If WORD is "oo", return
1417 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1420 complete_on_enum (const char *enumlist
[],
1425 int sizeof_matchlist
;
1427 int textlen
= strlen (text
);
1431 sizeof_matchlist
= 10;
1432 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1435 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1436 if (strncmp (name
, text
, textlen
) == 0)
1438 if (matches
== sizeof_matchlist
)
1440 sizeof_matchlist
*= 2;
1441 matchlist
= (char **) xrealloc ((char *) matchlist
,
1443 * sizeof (char *)));
1446 matchlist
[matches
] = (char *)
1447 xmalloc (strlen (word
) + strlen (name
) + 1);
1449 strcpy (matchlist
[matches
], name
);
1450 else if (word
> text
)
1452 /* Return some portion of name. */
1453 strcpy (matchlist
[matches
], name
+ (word
- text
));
1457 /* Return some of text plus name. */
1458 strncpy (matchlist
[matches
], word
, text
- word
);
1459 matchlist
[matches
][text
- word
] = '\0';
1460 strcat (matchlist
[matches
], name
);
1472 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1473 * sizeof (char *)));
1474 matchlist
[matches
] = (char *) 0;