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
);
329 struct cmd_list_element
*
330 add_set_cmd (char *name
,
331 enum command_class
class,
335 struct cmd_list_element
**list
)
337 return add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
, doc
, list
);
340 /* Add element named NAME to command list LIST (the list for set
341 or some sublist thereof).
342 CLASS is as in add_cmd.
343 ENUMLIST is a list of strings which may follow NAME.
344 VAR is address of the variable which will contain the matching string
346 DOC is the documentation string. */
348 struct cmd_list_element
*
349 add_set_enum_cmd (char *name
,
350 enum command_class
class,
351 const char *enumlist
[],
354 struct cmd_list_element
**list
)
356 struct cmd_list_element
*c
357 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
363 /* Add element named NAME to command list LIST (the list for set
364 or some sublist thereof).
365 CLASS is as in add_cmd.
366 VAR is address of the variable which will contain the value.
367 DOC is the documentation string. */
368 struct cmd_list_element
*
369 add_set_auto_boolean_cmd (char *name
,
370 enum command_class
class,
371 enum auto_boolean
*var
,
373 struct cmd_list_element
**list
)
375 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
376 struct cmd_list_element
*c
;
377 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
378 c
->enums
= auto_boolean_enums
;
382 /* Add element named NAME to command list LIST (the list for set
383 or some sublist thereof).
384 CLASS is as in add_cmd.
385 VAR is address of the variable which will contain the value.
386 DOC is the documentation string. */
387 struct cmd_list_element
*
388 add_set_boolean_cmd (char *name
,
389 enum command_class
class,
392 struct cmd_list_element
**list
)
394 static const char *boolean_enums
[] = { "on", "off", NULL
};
395 struct cmd_list_element
*c
;
396 c
= add_set_cmd (name
, class, var_boolean
, var
, doc
, list
);
397 c
->enums
= boolean_enums
;
401 /* Where SETCMD has already been added, add the corresponding show
402 command to LIST and return a pointer to the added command (not
403 necessarily the head of LIST). */
404 /* NOTE: cagney/2002-03-17: The original version of add_show_from_set
405 used memcpy() to clone `set' into `show'. This ment that in
406 addition to all the needed fields (var, name, et.al.) some
407 unnecessary fields were copied (namely the callback function). The
408 function explictly copies relevant fields. For a `set' and `show'
409 command to share the same callback, the caller must set both
411 struct cmd_list_element
*
412 add_show_from_set (struct cmd_list_element
*setcmd
,
413 struct cmd_list_element
**list
)
416 const static char setstring
[] = "Set ";
418 /* Create a doc string by replacing "Set " at the start of the
419 `set'' command's doco with "Show ". */
420 gdb_assert (strncmp (setcmd
->doc
, setstring
, sizeof (setstring
) - 1) == 0);
421 doc
= concat ("Show ", setcmd
->doc
+ sizeof (setstring
) - 1, NULL
);
423 /* Insert the basic command. */
424 return add_set_or_show_cmd (setcmd
->name
, show_cmd
, setcmd
->class,
425 setcmd
->var_type
, setcmd
->var
, doc
, list
);
428 /* Remove the command named NAME from the command list. */
431 delete_cmd (char *name
, struct cmd_list_element
**list
)
433 register struct cmd_list_element
*c
;
434 struct cmd_list_element
*p
;
436 while (*list
&& STREQ ((*list
)->name
, name
))
438 if ((*list
)->hookee_pre
)
439 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
440 if ((*list
)->hookee_post
)
441 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
448 for (c
= *list
; c
->next
;)
450 if (STREQ (c
->next
->name
, name
))
452 if (c
->next
->hookee_pre
)
453 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
454 if (c
->next
->hookee_post
)
455 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
456 /* :( no fishing metaphore */
466 /* Shorthands to the commands above. */
468 /* Add an element to the list of info subcommands. */
470 struct cmd_list_element
*
471 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
473 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
476 /* Add an alias to the list of info subcommands. */
478 struct cmd_list_element
*
479 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
481 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
484 /* Add an element to the list of commands. */
486 struct cmd_list_element
*
487 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
490 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
493 /* Add an alias or abbreviation command to the list of commands. */
495 struct cmd_list_element
*
496 add_com_alias (char *name
, char *oldname
, enum command_class
class,
499 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
502 /* Recursively walk the commandlist structures, and print out the
503 documentation of commands that match our regex in either their
504 name, or their documentation.
507 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
508 struct re_pattern_buffer
*regex
, char *prefix
)
510 register struct cmd_list_element
*c
;
511 int returnvalue
=1; /*Needed to avoid double printing*/
512 /* Walk through the commands */
513 for (c
=commandlist
;c
;c
=c
->next
)
517 /* Try to match against the name*/
518 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
519 if (returnvalue
>= 0)
521 /* Stolen from help_cmd_list. We don't directly use
522 * help_cmd_list because it doesn't let us print out
525 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
526 print_doc_line (stream
, c
->doc
);
527 fputs_filtered ("\n", stream
);
528 returnvalue
=0; /*Set this so we don't print it again.*/
531 if (c
->doc
!= NULL
&& returnvalue
!= 0)
533 /* Try to match against documentation */
534 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
536 /* Stolen from help_cmd_list. We don't directly use
537 * help_cmd_list because it doesn't let us print out
540 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
541 print_doc_line (stream
, c
->doc
);
542 fputs_filtered ("\n", stream
);
545 /* Check if this command has subcommands */
546 if (c
->prefixlist
!= NULL
)
548 /* Recursively call ourselves on the subcommand list,
549 passing the right prefix in.
551 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
556 /* This command really has to deal with two things:
557 * 1) I want documentation on *this string* (usually called by
558 * "help commandname").
559 * 2) I want documentation on *this list* (usually called by
560 * giving a command that requires subcommands. Also called by saying
563 * I am going to split this into two seperate comamnds, help_cmd and
568 help_cmd (char *command
, struct ui_file
*stream
)
570 struct cmd_list_element
*c
;
571 extern struct cmd_list_element
*cmdlist
;
575 help_list (cmdlist
, "", all_classes
, stream
);
579 if (strcmp (command
, "all") == 0)
585 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
590 /* There are three cases here.
591 If c->prefixlist is nonzero, we have a prefix command.
592 Print its documentation, then list its subcommands.
594 If c->func is non NULL, we really have a command. Print its
595 documentation and return.
597 If c->func is NULL, we have a class name. Print its
598 documentation (as if it were a command) and then set class to the
599 number of this class so that the commands in the class will be
602 fputs_filtered (c
->doc
, stream
);
603 fputs_filtered ("\n", stream
);
605 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
607 fprintf_filtered (stream
, "\n");
609 /* If this is a prefix command, print it's subcommands */
611 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
613 /* If this is a class name, print all of the commands in the class */
615 help_list (cmdlist
, "", c
->class, stream
);
617 if (c
->hook_pre
|| c
->hook_post
)
618 fprintf_filtered (stream
,
619 "\nThis command has a hook (or hooks) defined:\n");
622 fprintf_filtered (stream
,
623 "\tThis command is run after : %s (pre hook)\n",
626 fprintf_filtered (stream
,
627 "\tThis command is run before : %s (post hook)\n",
632 * Get a specific kind of help on a command list.
635 * CMDTYPE is the prefix to use in the title string.
636 * CLASS is the class with which to list the nodes of this list (see
637 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
638 * everything, ALL_CLASSES for just classes, and non-negative for only things
639 * in a specific class.
640 * and STREAM is the output stream on which to print things.
641 * If you call this routine with a class >= 0, it recurses.
644 help_list (struct cmd_list_element
*list
, char *cmdtype
,
645 enum command_class
class, struct ui_file
*stream
)
648 char *cmdtype1
, *cmdtype2
;
650 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
651 len
= strlen (cmdtype
);
652 cmdtype1
= (char *) alloca (len
+ 1);
654 cmdtype2
= (char *) alloca (len
+ 4);
659 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
661 strncpy (cmdtype2
, cmdtype
, len
- 1);
662 strcpy (cmdtype2
+ len
- 1, " sub");
665 if (class == all_classes
)
666 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
668 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
670 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
672 if (class == all_classes
)
674 fprintf_filtered (stream
, "\n\
675 Type \"help%s\" followed by a class name for a list of commands in ",
678 fprintf_filtered (stream
, "that class.");
681 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
684 fputs_filtered ("for ", stream
);
686 fputs_filtered ("full ", stream
);
688 fputs_filtered ("documentation.\n", stream
);
689 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
694 help_all (struct ui_file
*stream
)
696 struct cmd_list_element
*c
;
697 extern struct cmd_list_element
*cmdlist
;
699 for (c
= cmdlist
; c
; c
= c
->next
)
703 /* If this is a prefix command, print it's subcommands */
705 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
707 /* If this is a class name, print all of the commands in the class */
708 else if (c
->func
== NULL
)
709 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
713 /* Print only the first line of STR on STREAM. */
715 print_doc_line (struct ui_file
*stream
, char *str
)
717 static char *line_buffer
= 0;
718 static int line_size
;
724 line_buffer
= (char *) xmalloc (line_size
);
728 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
730 if (p
- str
> line_size
- 1)
732 line_size
= p
- str
+ 1;
734 line_buffer
= (char *) xmalloc (line_size
);
736 strncpy (line_buffer
, str
, p
- str
);
737 line_buffer
[p
- str
] = '\0';
738 if (islower (line_buffer
[0]))
739 line_buffer
[0] = toupper (line_buffer
[0]);
740 ui_out_text (uiout
, line_buffer
);
744 * Implement a help command on command list LIST.
745 * RECURSE should be non-zero if this should be done recursively on
746 * all sublists of LIST.
747 * PREFIX is the prefix to print before each command name.
748 * STREAM is the stream upon which the output should be written.
750 * A non-negative class number to list only commands in that
752 * ALL_COMMANDS to list all commands in list.
753 * ALL_CLASSES to list all classes in list.
755 * Note that RECURSE will be active on *all* sublists, not just the
756 * ones selected by the criteria above (ie. the selection mechanism
757 * is at the low level, not the high-level).
760 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
761 char *prefix
, int recurse
, struct ui_file
*stream
)
763 register struct cmd_list_element
*c
;
765 for (c
= list
; c
; c
= c
->next
)
767 if (c
->abbrev_flag
== 0 &&
768 (class == all_commands
769 || (class == all_classes
&& c
->func
== NULL
)
770 || (class == c
->class && c
->func
!= NULL
)))
772 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
773 print_doc_line (stream
, c
->doc
);
774 fputs_filtered ("\n", stream
);
777 && c
->prefixlist
!= 0
778 && c
->abbrev_flag
== 0)
779 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
784 /* Search the input clist for 'command'. Return the command if
785 found (or NULL if not), and return the number of commands
788 static struct cmd_list_element
*
789 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
790 int ignore_help_classes
, int *nfound
)
792 struct cmd_list_element
*found
, *c
;
794 found
= (struct cmd_list_element
*) NULL
;
796 for (c
= clist
; c
; c
= c
->next
)
797 if (!strncmp (command
, c
->name
, len
)
798 && (!ignore_help_classes
|| c
->func
))
802 if (c
->name
[len
] == '\0')
811 /* This routine takes a line of TEXT and a CLIST in which to start the
812 lookup. When it returns it will have incremented the text pointer past
813 the section of text it matched, set *RESULT_LIST to point to the list in
814 which the last word was matched, and will return a pointer to the cmd
815 list element which the text matches. It will return NULL if no match at
816 all was possible. It will return -1 (cast appropriately, ick) if ambigous
817 matches are possible; in this case *RESULT_LIST will be set to point to
818 the list in which there are ambiguous choices (and *TEXT will be set to
819 the ambiguous text string).
821 If the located command was an abbreviation, this routine returns the base
822 command of the abbreviation.
824 It does no error reporting whatsoever; control will always return
825 to the superior routine.
827 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
828 at the prefix_command (ie. the best match) *or* (special case) will be NULL
829 if no prefix command was ever found. For example, in the case of "info a",
830 "info" matches without ambiguity, but "a" could be "args" or "address", so
831 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
832 RESULT_LIST should not be interpeted as a pointer to the beginning of a
833 list; it simply points to a specific command. In the case of an ambiguous
834 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
835 "info t" can be "info types" or "info target"; upon return *TEXT has been
836 advanced past "info ").
838 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
839 affect the operation).
841 This routine does *not* modify the text pointed to by TEXT.
843 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
844 are actually help classes rather than commands (i.e. the function field of
845 the struct cmd_list_element is NULL). */
847 struct cmd_list_element
*
848 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
849 struct cmd_list_element
**result_list
, int ignore_help_classes
)
852 int len
, tmp
, nfound
;
853 struct cmd_list_element
*found
, *c
;
856 while (**text
== ' ' || **text
== '\t')
859 /* Treating underscores as part of command words is important
860 so that "set args_foo()" doesn't get interpreted as
861 "set args _foo()". */
863 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
865 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
866 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
870 /* If nothing but whitespace, return 0. */
876 /* *text and p now bracket the first command word to lookup (and
877 it's length is len). We copy this into a local temporary */
880 command
= (char *) alloca (len
+ 1);
881 for (tmp
= 0; tmp
< len
; tmp
++)
883 char x
= (*text
)[tmp
];
891 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
894 ** We didn't find the command in the entered case, so lower case it
897 if (!found
|| nfound
== 0)
899 for (tmp
= 0; tmp
< len
; tmp
++)
901 char x
= command
[tmp
];
902 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
904 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
907 /* If nothing matches, we have a simple failure. */
913 if (result_list
!= NULL
)
914 /* Will be modified in calling routine
915 if we know what the prefix command is. */
917 return (struct cmd_list_element
*) -1; /* Ambiguous. */
920 /* We've matched something on this list. Move text pointer forward. */
924 if (found
->cmd_pointer
)
926 /* We drop the alias (abbreviation) in favor of the command it is
927 pointing to. If the alias is deprecated, though, we need to
928 warn the user about it before we drop it. Note that while we
929 are warning about the alias, we may also warn about the command
930 itself and we will adjust the appropriate DEPRECATED_WARN_USER
933 if (found
->flags
& DEPRECATED_WARN_USER
)
934 deprecated_cmd_warning (&line
);
935 found
= found
->cmd_pointer
;
937 /* If we found a prefix command, keep looking. */
939 if (found
->prefixlist
)
941 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
942 ignore_help_classes
);
945 /* Didn't find anything; this is as far as we got. */
946 if (result_list
!= NULL
)
947 *result_list
= clist
;
950 else if (c
== (struct cmd_list_element
*) -1)
952 /* We've gotten this far properly, but the next step
953 is ambiguous. We need to set the result list to the best
954 we've found (if an inferior hasn't already set it). */
955 if (result_list
!= NULL
)
957 /* This used to say *result_list = *found->prefixlist
958 If that was correct, need to modify the documentation
959 at the top of this function to clarify what is supposed
961 *result_list
= found
;
972 if (result_list
!= NULL
)
973 *result_list
= clist
;
978 /* All this hair to move the space to the front of cmdtype */
981 undef_cmd_error (char *cmdtype
, char *q
)
983 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
987 (int) strlen (cmdtype
) - 1,
991 /* Look up the contents of *LINE as a command in the command list LIST.
992 LIST is a chain of struct cmd_list_element's.
993 If it is found, return the struct cmd_list_element for that command
994 and update *LINE to point after the command name, at the first argument.
995 If not found, call error if ALLOW_UNKNOWN is zero
996 otherwise (or if error returns) return zero.
997 Call error if specified command is ambiguous,
998 unless ALLOW_UNKNOWN is negative.
999 CMDTYPE precedes the word "command" in the error message.
1001 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1002 elements which are actually help classes rather than commands (i.e.
1003 the function field of the struct cmd_list_element is 0). */
1005 struct cmd_list_element
*
1006 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1007 int allow_unknown
, int ignore_help_classes
)
1009 struct cmd_list_element
*last_list
= 0;
1010 struct cmd_list_element
*c
=
1011 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1013 /* Note: Do not remove trailing whitespace here because this
1014 would be wrong for complete_command. Jim Kingdon */
1021 error ("Lack of needed %scommand", cmdtype
);
1024 char *p
= *line
, *q
;
1026 while (isalnum (*p
) || *p
== '-')
1029 q
= (char *) alloca (p
- *line
+ 1);
1030 strncpy (q
, *line
, p
- *line
);
1031 q
[p
- *line
] = '\0';
1032 undef_cmd_error (cmdtype
, q
);
1038 else if (c
== (struct cmd_list_element
*) -1)
1040 /* Ambigous. Local values should be off prefixlist or called
1042 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1044 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1045 struct cmd_list_element
*local_list
=
1046 (last_list
? *(last_list
->prefixlist
) : list
);
1048 if (local_allow_unknown
< 0)
1051 return last_list
; /* Found something. */
1053 return 0; /* Found nothing. */
1057 /* Report as error. */
1062 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1063 && (*line
)[amb_len
] != '\t');
1068 for (c
= local_list
; c
; c
= c
->next
)
1069 if (!strncmp (*line
, c
->name
, amb_len
))
1071 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1073 if (strlen (ambbuf
))
1074 strcat (ambbuf
, ", ");
1075 strcat (ambbuf
, c
->name
);
1079 strcat (ambbuf
, "..");
1083 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1085 return 0; /* lint */
1090 /* We've got something. It may still not be what the caller
1091 wants (if this command *needs* a subcommand). */
1092 while (**line
== ' ' || **line
== '\t')
1095 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1096 undef_cmd_error (c
->prefixname
, *line
);
1098 /* Seems to be what he wants. Return it. */
1104 /* We are here presumably because an alias or command in *TEXT is
1105 deprecated and a warning message should be generated. This function
1106 decodes *TEXT and potentially generates a warning message as outlined
1109 Example for 'set endian big' which has a fictitious alias 'seb'.
1111 If alias wasn't used in *TEXT, and the command is deprecated:
1112 "warning: 'set endian big' is deprecated."
1114 If alias was used, and only the alias is deprecated:
1115 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1117 If alias was used and command is deprecated (regardless of whether the
1118 alias itself is deprecated:
1120 "warning: 'set endian big' (seb) is deprecated."
1122 After the message has been sent, clear the appropriate flags in the
1123 command and/or the alias so the user is no longer bothered.
1127 deprecated_cmd_warning (char **text
)
1129 struct cmd_list_element
*alias
= NULL
;
1130 struct cmd_list_element
*prefix_cmd
= NULL
;
1131 struct cmd_list_element
*cmd
= NULL
;
1132 struct cmd_list_element
*c
;
1135 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1136 /* return if text doesn't evaluate to a command */
1139 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1140 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1141 /* return if nothing is deprecated */
1144 printf_filtered ("Warning:");
1146 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1147 printf_filtered (" '%s', an alias for the", alias
->name
);
1149 printf_filtered (" command '");
1152 printf_filtered ("%s", prefix_cmd
->prefixname
);
1154 printf_filtered ("%s", cmd
->name
);
1156 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1157 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1159 printf_filtered ("' is deprecated.\n");
1162 /* if it is only the alias that is deprecated, we want to indicate the
1163 new alias, otherwise we'll indicate the new command */
1165 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1167 if (alias
->replacement
)
1168 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1170 printf_filtered ("No alternative known.\n\n");
1174 if (cmd
->replacement
)
1175 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1177 printf_filtered ("No alternative known.\n\n");
1180 /* We've warned you, now we'll keep quiet */
1182 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1184 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1189 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1190 Return 1 on success, 0 on failure.
1192 If LINE refers to an alias, *alias will point to that alias.
1194 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1195 command) set *prefix_cmd.
1197 Set *cmd to point to the command LINE indicates.
1199 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1200 exist, they are NULL when we return.
1204 lookup_cmd_composition (char *text
,
1205 struct cmd_list_element
**alias
,
1206 struct cmd_list_element
**prefix_cmd
,
1207 struct cmd_list_element
**cmd
)
1210 int len
, tmp
, nfound
;
1211 struct cmd_list_element
*cur_list
;
1212 struct cmd_list_element
*prev_cmd
;
1221 /* Go through as many command lists as we need to
1222 to find the command TEXT refers to. */
1226 while (*text
== ' ' || *text
== '\t')
1229 /* Treating underscores as part of command words is important
1230 so that "set args_foo()" doesn't get interpreted as
1231 "set args _foo()". */
1233 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1235 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1236 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1240 /* If nothing but whitespace, return. */
1246 /* text and p now bracket the first command word to lookup (and
1247 it's length is len). We copy this into a local temporary */
1249 command
= (char *) alloca (len
+ 1);
1250 for (tmp
= 0; tmp
< len
; tmp
++)
1255 command
[len
] = '\0';
1260 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1262 /* We didn't find the command in the entered case, so lower case it
1265 if (!*cmd
|| nfound
== 0)
1267 for (tmp
= 0; tmp
< len
; tmp
++)
1269 char x
= command
[tmp
];
1270 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1272 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1275 if (*cmd
== (struct cmd_list_element
*) -1)
1277 return 0; /* ambiguous */
1281 return 0; /* nothing found */
1284 if ((*cmd
)->cmd_pointer
)
1286 /* cmd was actually an alias, we note that an alias was used
1287 (by assigning *alais) and we set *cmd.
1290 *cmd
= (*cmd
)->cmd_pointer
;
1292 *prefix_cmd
= prev_cmd
;
1294 if ((*cmd
)->prefixlist
)
1295 cur_list
= *(*cmd
)->prefixlist
;
1303 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1305 /* Return a vector of char pointers which point to the different
1306 possible completions in LIST of TEXT.
1308 WORD points in the same buffer as TEXT, and completions should be
1309 returned relative to this position. For example, suppose TEXT is "foo"
1310 and we want to complete to "foobar". If WORD is "oo", return
1311 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1314 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1316 struct cmd_list_element
*ptr
;
1318 int sizeof_matchlist
;
1320 int textlen
= strlen (text
);
1322 sizeof_matchlist
= 10;
1323 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1326 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1327 if (!strncmp (ptr
->name
, text
, textlen
)
1328 && !ptr
->abbrev_flag
1330 || ptr
->prefixlist
))
1332 if (matches
== sizeof_matchlist
)
1334 sizeof_matchlist
*= 2;
1335 matchlist
= (char **) xrealloc ((char *) matchlist
,
1337 * sizeof (char *)));
1340 matchlist
[matches
] = (char *)
1341 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1343 strcpy (matchlist
[matches
], ptr
->name
);
1344 else if (word
> text
)
1346 /* Return some portion of ptr->name. */
1347 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1351 /* Return some of text plus ptr->name. */
1352 strncpy (matchlist
[matches
], word
, text
- word
);
1353 matchlist
[matches
][text
- word
] = '\0';
1354 strcat (matchlist
[matches
], ptr
->name
);
1366 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1367 * sizeof (char *)));
1368 matchlist
[matches
] = (char *) 0;
1374 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1376 /* Return a vector of char pointers which point to the different
1377 possible completions in CMD of TEXT.
1379 WORD points in the same buffer as TEXT, and completions should be
1380 returned relative to this position. For example, suppose TEXT is "foo"
1381 and we want to complete to "foobar". If WORD is "oo", return
1382 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1385 complete_on_enum (const char *enumlist
[],
1390 int sizeof_matchlist
;
1392 int textlen
= strlen (text
);
1396 sizeof_matchlist
= 10;
1397 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1400 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1401 if (strncmp (name
, text
, textlen
) == 0)
1403 if (matches
== sizeof_matchlist
)
1405 sizeof_matchlist
*= 2;
1406 matchlist
= (char **) xrealloc ((char *) matchlist
,
1408 * sizeof (char *)));
1411 matchlist
[matches
] = (char *)
1412 xmalloc (strlen (word
) + strlen (name
) + 1);
1414 strcpy (matchlist
[matches
], name
);
1415 else if (word
> text
)
1417 /* Return some portion of name. */
1418 strcpy (matchlist
[matches
], name
+ (word
- text
));
1422 /* Return some of text plus name. */
1423 strncpy (matchlist
[matches
], word
, text
- word
);
1424 matchlist
[matches
][text
- word
] = '\0';
1425 strcat (matchlist
[matches
], name
);
1437 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1438 * sizeof (char *)));
1439 matchlist
[matches
] = (char *) 0;