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
,
57 void (*cfunc
) (char *args
, int from_tty
))
63 cmd
->function
.cfunc
= cfunc
; /* Ok. */
67 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
69 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
73 set_cmd_sfunc (struct cmd_list_element
*cmd
,
74 void (*sfunc
) (char *args
, int from_tty
,
75 struct cmd_list_element
* c
))
81 cmd
->function
.sfunc
= sfunc
; /* Ok. */
85 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
86 void (*cfunc
) (char *args
, int from_tty
))
88 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
92 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
94 cmd
->context
= context
;
98 get_cmd_context (struct cmd_list_element
*cmd
)
104 cmd_type (struct cmd_list_element
*cmd
)
110 set_cmd_completer (struct cmd_list_element
*cmd
,
111 char **(*completer
) (char *text
, char *word
))
113 cmd
->completer
= completer
; /* Ok. */
117 /* Add element named NAME.
118 CLASS is the top level category into which commands are broken down
120 FUN should be the function to execute the command;
121 it will get a character string as argument, with leading
122 and trailing blanks already eliminated.
124 DOC is a documentation string for the command.
125 Its first line should be a complete sentence.
126 It should start with ? for a command that is an abbreviation
127 or with * for a command that most users don't need to know about.
129 Add this command to command list *LIST.
131 Returns a pointer to the added command (not necessarily the head
134 struct cmd_list_element
*
135 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
136 char *doc
, struct cmd_list_element
**list
)
138 register struct cmd_list_element
*c
139 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
140 struct cmd_list_element
*p
;
142 delete_cmd (name
, list
);
144 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
152 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
162 set_cmd_cfunc (c
, fun
);
163 set_cmd_context (c
, NULL
);
166 c
->replacement
= NULL
;
167 c
->pre_show_hook
= NULL
;
171 c
->prefixlist
= NULL
;
172 c
->prefixname
= NULL
;
173 c
->allow_unknown
= 0;
175 set_cmd_completer (c
, make_symbol_completion_list
);
176 c
->type
= not_set_cmd
;
178 c
->var_type
= var_boolean
;
180 c
->user_commands
= NULL
;
181 c
->hookee_pre
= NULL
;
182 c
->hookee_post
= NULL
;
183 c
->cmd_pointer
= NULL
;
188 /* Same as above, except that the abbrev_flag is set. */
189 /* Note: Doesn't seem to be used anywhere currently. */
191 struct cmd_list_element
*
192 add_abbrev_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
193 char *doc
, struct cmd_list_element
**list
)
195 register struct cmd_list_element
*c
196 = add_cmd (name
, class, fun
, doc
, list
);
202 /* Deprecates a command CMD.
203 REPLACEMENT is the name of the command which should be used in place
204 of this command, or NULL if no such command exists.
206 This function does not check to see if command REPLACEMENT exists
207 since gdb may not have gotten around to adding REPLACEMENT when this
210 Returns a pointer to the deprecated command. */
212 struct cmd_list_element
*
213 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
215 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
217 if (replacement
!= NULL
)
218 cmd
->replacement
= replacement
;
220 cmd
->replacement
= NULL
;
225 struct cmd_list_element
*
226 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
227 int abbrev_flag
, struct cmd_list_element
**list
)
229 /* Must do this since lookup_cmd tries to side-effect its first arg */
231 register struct cmd_list_element
*old
;
232 register struct cmd_list_element
*c
;
233 copied_name
= (char *) alloca (strlen (oldname
) + 1);
234 strcpy (copied_name
, oldname
);
235 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
239 delete_cmd (name
, list
);
243 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
244 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
246 c
->function
= old
->function
;
247 c
->prefixlist
= old
->prefixlist
;
248 c
->prefixname
= old
->prefixname
;
249 c
->allow_unknown
= old
->allow_unknown
;
250 c
->abbrev_flag
= abbrev_flag
;
251 c
->cmd_pointer
= old
;
255 /* Like add_cmd but adds an element for a command prefix:
256 a name that should be followed by a subcommand to be looked up
257 in another command list. PREFIXLIST should be the address
258 of the variable containing that list. */
260 struct cmd_list_element
*
261 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
262 char *doc
, struct cmd_list_element
**prefixlist
,
263 char *prefixname
, int allow_unknown
,
264 struct cmd_list_element
**list
)
266 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
267 c
->prefixlist
= prefixlist
;
268 c
->prefixname
= prefixname
;
269 c
->allow_unknown
= allow_unknown
;
273 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
275 struct cmd_list_element
*
276 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
277 void (*fun
) (char *, int), char *doc
,
278 struct cmd_list_element
**prefixlist
, char *prefixname
,
279 int allow_unknown
, struct cmd_list_element
**list
)
281 register struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
282 c
->prefixlist
= prefixlist
;
283 c
->prefixname
= prefixname
;
284 c
->allow_unknown
= allow_unknown
;
289 /* This is an empty "cfunc". */
291 not_just_help_class_command (char *args
, int from_tty
)
295 /* This is an empty "sfunc". */
296 static void empty_sfunc (char *, int, struct cmd_list_element
*);
299 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
303 /* Add element named NAME to command list LIST (the list for set/show
304 or some sublist thereof).
305 TYPE is set_cmd or show_cmd.
306 CLASS is as in add_cmd.
307 VAR_TYPE is the kind of thing we are setting.
308 VAR is address of the variable being controlled by this command.
309 DOC is the documentation string. */
311 static struct cmd_list_element
*
312 add_set_or_show_cmd (char *name
,
314 enum command_class
class,
318 struct cmd_list_element
**list
)
320 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
321 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
323 c
->var_type
= var_type
;
325 /* This needs to be something besides NULL so that this isn't
326 treated as a help class. */
327 set_cmd_sfunc (c
, empty_sfunc
);
332 struct cmd_list_element
*
333 add_set_cmd (char *name
,
334 enum command_class
class,
338 struct cmd_list_element
**list
)
340 return add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
, doc
, list
);
343 /* Add element named NAME to command list LIST (the list for set
344 or some sublist thereof).
345 CLASS is as in add_cmd.
346 ENUMLIST is a list of strings which may follow NAME.
347 VAR is address of the variable which will contain the matching string
349 DOC is the documentation string. */
351 struct cmd_list_element
*
352 add_set_enum_cmd (char *name
,
353 enum command_class
class,
354 const char *enumlist
[],
357 struct cmd_list_element
**list
)
359 struct cmd_list_element
*c
360 = add_set_cmd (name
, class, var_enum
, var
, doc
, list
);
366 /* Add element named NAME to command list LIST (the list for set
367 or some sublist thereof).
368 CLASS is as in add_cmd.
369 VAR is address of the variable which will contain the value.
370 DOC is the documentation string. */
371 struct cmd_list_element
*
372 add_set_auto_boolean_cmd (char *name
,
373 enum command_class
class,
374 enum auto_boolean
*var
,
376 struct cmd_list_element
**list
)
378 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
379 struct cmd_list_element
*c
;
380 c
= add_set_cmd (name
, class, var_auto_boolean
, var
, doc
, list
);
381 c
->enums
= auto_boolean_enums
;
385 /* Add element named NAME to command list LIST (the list for set
386 or some sublist thereof).
387 CLASS is as in add_cmd.
388 VAR is address of the variable which will contain the value.
389 DOC is the documentation string. */
390 struct cmd_list_element
*
391 add_set_boolean_cmd (char *name
,
392 enum command_class
class,
395 struct cmd_list_element
**list
)
397 static const char *boolean_enums
[] = { "on", "off", NULL
};
398 struct cmd_list_element
*c
;
399 c
= add_set_cmd (name
, class, var_boolean
, var
, doc
, list
);
400 c
->enums
= boolean_enums
;
404 /* Where SETCMD has already been added, add the corresponding show
405 command to LIST and return a pointer to the added command (not
406 necessarily the head of LIST). */
407 /* NOTE: cagney/2002-03-17: The original version of add_show_from_set
408 used memcpy() to clone `set' into `show'. This ment that in
409 addition to all the needed fields (var, name, et.al.) some
410 unnecessary fields were copied (namely the callback function). The
411 function explictly copies relevant fields. For a `set' and `show'
412 command to share the same callback, the caller must set both
414 struct cmd_list_element
*
415 add_show_from_set (struct cmd_list_element
*setcmd
,
416 struct cmd_list_element
**list
)
419 const static char setstring
[] = "Set ";
421 /* Create a doc string by replacing "Set " at the start of the
422 `set'' command's doco with "Show ". */
423 gdb_assert (strncmp (setcmd
->doc
, setstring
, sizeof (setstring
) - 1) == 0);
424 doc
= concat ("Show ", setcmd
->doc
+ sizeof (setstring
) - 1, NULL
);
426 /* Insert the basic command. */
427 return add_set_or_show_cmd (setcmd
->name
, show_cmd
, setcmd
->class,
428 setcmd
->var_type
, setcmd
->var
, doc
, list
);
431 /* Remove the command named NAME from the command list. */
434 delete_cmd (char *name
, struct cmd_list_element
**list
)
436 register struct cmd_list_element
*c
;
437 struct cmd_list_element
*p
;
439 while (*list
&& STREQ ((*list
)->name
, name
))
441 if ((*list
)->hookee_pre
)
442 (*list
)->hookee_pre
->hook_pre
= 0; /* Hook slips out of its mouth */
443 if ((*list
)->hookee_post
)
444 (*list
)->hookee_post
->hook_post
= 0; /* Hook slips out of its bottom */
451 for (c
= *list
; c
->next
;)
453 if (STREQ (c
->next
->name
, name
))
455 if (c
->next
->hookee_pre
)
456 c
->next
->hookee_pre
->hook_pre
= 0; /* hooked cmd gets away. */
457 if (c
->next
->hookee_post
)
458 c
->next
->hookee_post
->hook_post
= 0; /* remove post hook */
459 /* :( no fishing metaphore */
469 /* Shorthands to the commands above. */
471 /* Add an element to the list of info subcommands. */
473 struct cmd_list_element
*
474 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
476 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
479 /* Add an alias to the list of info subcommands. */
481 struct cmd_list_element
*
482 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
484 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
487 /* Add an element to the list of commands. */
489 struct cmd_list_element
*
490 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
493 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
496 /* Add an alias or abbreviation command to the list of commands. */
498 struct cmd_list_element
*
499 add_com_alias (char *name
, char *oldname
, enum command_class
class,
502 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
505 /* Recursively walk the commandlist structures, and print out the
506 documentation of commands that match our regex in either their
507 name, or their documentation.
510 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
511 struct re_pattern_buffer
*regex
, char *prefix
)
513 register struct cmd_list_element
*c
;
514 int returnvalue
=1; /*Needed to avoid double printing*/
515 /* Walk through the commands */
516 for (c
=commandlist
;c
;c
=c
->next
)
520 /* Try to match against the name*/
521 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
522 if (returnvalue
>= 0)
524 /* Stolen from help_cmd_list. We don't directly use
525 * help_cmd_list because it doesn't let us print out
528 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
529 print_doc_line (stream
, c
->doc
);
530 fputs_filtered ("\n", stream
);
531 returnvalue
=0; /*Set this so we don't print it again.*/
534 if (c
->doc
!= NULL
&& returnvalue
!= 0)
536 /* Try to match against documentation */
537 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
539 /* Stolen from help_cmd_list. We don't directly use
540 * help_cmd_list because it doesn't let us print out
543 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
544 print_doc_line (stream
, c
->doc
);
545 fputs_filtered ("\n", stream
);
548 /* Check if this command has subcommands */
549 if (c
->prefixlist
!= NULL
)
551 /* Recursively call ourselves on the subcommand list,
552 passing the right prefix in.
554 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
559 /* This command really has to deal with two things:
560 * 1) I want documentation on *this string* (usually called by
561 * "help commandname").
562 * 2) I want documentation on *this list* (usually called by
563 * giving a command that requires subcommands. Also called by saying
566 * I am going to split this into two seperate comamnds, help_cmd and
571 help_cmd (char *command
, struct ui_file
*stream
)
573 struct cmd_list_element
*c
;
574 extern struct cmd_list_element
*cmdlist
;
578 help_list (cmdlist
, "", all_classes
, stream
);
582 if (strcmp (command
, "all") == 0)
588 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
593 /* There are three cases here.
594 If c->prefixlist is nonzero, we have a prefix command.
595 Print its documentation, then list its subcommands.
597 If c->func is non NULL, we really have a command. Print its
598 documentation and return.
600 If c->func is NULL, we have a class name. Print its
601 documentation (as if it were a command) and then set class to the
602 number of this class so that the commands in the class will be
605 fputs_filtered (c
->doc
, stream
);
606 fputs_filtered ("\n", stream
);
608 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
610 fprintf_filtered (stream
, "\n");
612 /* If this is a prefix command, print it's subcommands */
614 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
616 /* If this is a class name, print all of the commands in the class */
618 help_list (cmdlist
, "", c
->class, stream
);
620 if (c
->hook_pre
|| c
->hook_post
)
621 fprintf_filtered (stream
,
622 "\nThis command has a hook (or hooks) defined:\n");
625 fprintf_filtered (stream
,
626 "\tThis command is run after : %s (pre hook)\n",
629 fprintf_filtered (stream
,
630 "\tThis command is run before : %s (post hook)\n",
635 * Get a specific kind of help on a command list.
638 * CMDTYPE is the prefix to use in the title string.
639 * CLASS is the class with which to list the nodes of this list (see
640 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
641 * everything, ALL_CLASSES for just classes, and non-negative for only things
642 * in a specific class.
643 * and STREAM is the output stream on which to print things.
644 * If you call this routine with a class >= 0, it recurses.
647 help_list (struct cmd_list_element
*list
, char *cmdtype
,
648 enum command_class
class, struct ui_file
*stream
)
651 char *cmdtype1
, *cmdtype2
;
653 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
654 len
= strlen (cmdtype
);
655 cmdtype1
= (char *) alloca (len
+ 1);
657 cmdtype2
= (char *) alloca (len
+ 4);
662 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
664 strncpy (cmdtype2
, cmdtype
, len
- 1);
665 strcpy (cmdtype2
+ len
- 1, " sub");
668 if (class == all_classes
)
669 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
671 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
673 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
675 if (class == all_classes
)
677 fprintf_filtered (stream
, "\n\
678 Type \"help%s\" followed by a class name for a list of commands in ",
681 fprintf_filtered (stream
, "that class.");
684 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
687 fputs_filtered ("for ", stream
);
689 fputs_filtered ("full ", stream
);
691 fputs_filtered ("documentation.\n", stream
);
692 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
697 help_all (struct ui_file
*stream
)
699 struct cmd_list_element
*c
;
700 extern struct cmd_list_element
*cmdlist
;
702 for (c
= cmdlist
; c
; c
= c
->next
)
706 /* If this is a prefix command, print it's subcommands */
708 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 0, stream
);
710 /* If this is a class name, print all of the commands in the class */
711 else if (c
->func
== NULL
)
712 help_cmd_list (cmdlist
, c
->class, "", 0, stream
);
716 /* Print only the first line of STR on STREAM. */
718 print_doc_line (struct ui_file
*stream
, char *str
)
720 static char *line_buffer
= 0;
721 static int line_size
;
727 line_buffer
= (char *) xmalloc (line_size
);
731 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
733 if (p
- str
> line_size
- 1)
735 line_size
= p
- str
+ 1;
737 line_buffer
= (char *) xmalloc (line_size
);
739 strncpy (line_buffer
, str
, p
- str
);
740 line_buffer
[p
- str
] = '\0';
741 if (islower (line_buffer
[0]))
742 line_buffer
[0] = toupper (line_buffer
[0]);
743 ui_out_text (uiout
, line_buffer
);
747 * Implement a help command on command list LIST.
748 * RECURSE should be non-zero if this should be done recursively on
749 * all sublists of LIST.
750 * PREFIX is the prefix to print before each command name.
751 * STREAM is the stream upon which the output should be written.
753 * A non-negative class number to list only commands in that
755 * ALL_COMMANDS to list all commands in list.
756 * ALL_CLASSES to list all classes in list.
758 * Note that RECURSE will be active on *all* sublists, not just the
759 * ones selected by the criteria above (ie. the selection mechanism
760 * is at the low level, not the high-level).
763 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
764 char *prefix
, int recurse
, struct ui_file
*stream
)
766 register struct cmd_list_element
*c
;
768 for (c
= list
; c
; c
= c
->next
)
770 if (c
->abbrev_flag
== 0 &&
771 (class == all_commands
772 || (class == all_classes
&& c
->func
== NULL
)
773 || (class == c
->class && c
->func
!= NULL
)))
775 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
776 print_doc_line (stream
, c
->doc
);
777 fputs_filtered ("\n", stream
);
780 && c
->prefixlist
!= 0
781 && c
->abbrev_flag
== 0)
782 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, 1, stream
);
787 /* Search the input clist for 'command'. Return the command if
788 found (or NULL if not), and return the number of commands
791 static struct cmd_list_element
*
792 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
793 int ignore_help_classes
, int *nfound
)
795 struct cmd_list_element
*found
, *c
;
797 found
= (struct cmd_list_element
*) NULL
;
799 for (c
= clist
; c
; c
= c
->next
)
800 if (!strncmp (command
, c
->name
, len
)
801 && (!ignore_help_classes
|| c
->func
))
805 if (c
->name
[len
] == '\0')
814 /* This routine takes a line of TEXT and a CLIST in which to start the
815 lookup. When it returns it will have incremented the text pointer past
816 the section of text it matched, set *RESULT_LIST to point to the list in
817 which the last word was matched, and will return a pointer to the cmd
818 list element which the text matches. It will return NULL if no match at
819 all was possible. It will return -1 (cast appropriately, ick) if ambigous
820 matches are possible; in this case *RESULT_LIST will be set to point to
821 the list in which there are ambiguous choices (and *TEXT will be set to
822 the ambiguous text string).
824 If the located command was an abbreviation, this routine returns the base
825 command of the abbreviation.
827 It does no error reporting whatsoever; control will always return
828 to the superior routine.
830 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
831 at the prefix_command (ie. the best match) *or* (special case) will be NULL
832 if no prefix command was ever found. For example, in the case of "info a",
833 "info" matches without ambiguity, but "a" could be "args" or "address", so
834 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
835 RESULT_LIST should not be interpeted as a pointer to the beginning of a
836 list; it simply points to a specific command. In the case of an ambiguous
837 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
838 "info t" can be "info types" or "info target"; upon return *TEXT has been
839 advanced past "info ").
841 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
842 affect the operation).
844 This routine does *not* modify the text pointed to by TEXT.
846 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
847 are actually help classes rather than commands (i.e. the function field of
848 the struct cmd_list_element is NULL). */
850 struct cmd_list_element
*
851 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
852 struct cmd_list_element
**result_list
, int ignore_help_classes
)
855 int len
, tmp
, nfound
;
856 struct cmd_list_element
*found
, *c
;
859 while (**text
== ' ' || **text
== '\t')
862 /* Treating underscores as part of command words is important
863 so that "set args_foo()" doesn't get interpreted as
864 "set args _foo()". */
866 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
868 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
869 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
873 /* If nothing but whitespace, return 0. */
879 /* *text and p now bracket the first command word to lookup (and
880 it's length is len). We copy this into a local temporary */
883 command
= (char *) alloca (len
+ 1);
884 for (tmp
= 0; tmp
< len
; tmp
++)
886 char x
= (*text
)[tmp
];
894 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
897 ** We didn't find the command in the entered case, so lower case it
900 if (!found
|| nfound
== 0)
902 for (tmp
= 0; tmp
< len
; tmp
++)
904 char x
= command
[tmp
];
905 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
907 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
910 /* If nothing matches, we have a simple failure. */
916 if (result_list
!= NULL
)
917 /* Will be modified in calling routine
918 if we know what the prefix command is. */
920 return (struct cmd_list_element
*) -1; /* Ambiguous. */
923 /* We've matched something on this list. Move text pointer forward. */
927 if (found
->cmd_pointer
)
929 /* We drop the alias (abbreviation) in favor of the command it is
930 pointing to. If the alias is deprecated, though, we need to
931 warn the user about it before we drop it. Note that while we
932 are warning about the alias, we may also warn about the command
933 itself and we will adjust the appropriate DEPRECATED_WARN_USER
936 if (found
->flags
& DEPRECATED_WARN_USER
)
937 deprecated_cmd_warning (&line
);
938 found
= found
->cmd_pointer
;
940 /* If we found a prefix command, keep looking. */
942 if (found
->prefixlist
)
944 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
945 ignore_help_classes
);
948 /* Didn't find anything; this is as far as we got. */
949 if (result_list
!= NULL
)
950 *result_list
= clist
;
953 else if (c
== (struct cmd_list_element
*) -1)
955 /* We've gotten this far properly, but the next step
956 is ambiguous. We need to set the result list to the best
957 we've found (if an inferior hasn't already set it). */
958 if (result_list
!= NULL
)
960 /* This used to say *result_list = *found->prefixlist
961 If that was correct, need to modify the documentation
962 at the top of this function to clarify what is supposed
964 *result_list
= found
;
975 if (result_list
!= NULL
)
976 *result_list
= clist
;
981 /* All this hair to move the space to the front of cmdtype */
984 undef_cmd_error (char *cmdtype
, char *q
)
986 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
990 (int) strlen (cmdtype
) - 1,
994 /* Look up the contents of *LINE as a command in the command list LIST.
995 LIST is a chain of struct cmd_list_element's.
996 If it is found, return the struct cmd_list_element for that command
997 and update *LINE to point after the command name, at the first argument.
998 If not found, call error if ALLOW_UNKNOWN is zero
999 otherwise (or if error returns) return zero.
1000 Call error if specified command is ambiguous,
1001 unless ALLOW_UNKNOWN is negative.
1002 CMDTYPE precedes the word "command" in the error message.
1004 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1005 elements which are actually help classes rather than commands (i.e.
1006 the function field of the struct cmd_list_element is 0). */
1008 struct cmd_list_element
*
1009 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1010 int allow_unknown
, int ignore_help_classes
)
1012 struct cmd_list_element
*last_list
= 0;
1013 struct cmd_list_element
*c
=
1014 lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1016 /* Note: Do not remove trailing whitespace here because this
1017 would be wrong for complete_command. Jim Kingdon */
1024 error ("Lack of needed %scommand", cmdtype
);
1027 char *p
= *line
, *q
;
1029 while (isalnum (*p
) || *p
== '-')
1032 q
= (char *) alloca (p
- *line
+ 1);
1033 strncpy (q
, *line
, p
- *line
);
1034 q
[p
- *line
] = '\0';
1035 undef_cmd_error (cmdtype
, q
);
1041 else if (c
== (struct cmd_list_element
*) -1)
1043 /* Ambigous. Local values should be off prefixlist or called
1045 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1047 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1048 struct cmd_list_element
*local_list
=
1049 (last_list
? *(last_list
->prefixlist
) : list
);
1051 if (local_allow_unknown
< 0)
1054 return last_list
; /* Found something. */
1056 return 0; /* Found nothing. */
1060 /* Report as error. */
1065 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1066 && (*line
)[amb_len
] != '\t');
1071 for (c
= local_list
; c
; c
= c
->next
)
1072 if (!strncmp (*line
, c
->name
, amb_len
))
1074 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1076 if (strlen (ambbuf
))
1077 strcat (ambbuf
, ", ");
1078 strcat (ambbuf
, c
->name
);
1082 strcat (ambbuf
, "..");
1086 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype
,
1088 return 0; /* lint */
1093 /* We've got something. It may still not be what the caller
1094 wants (if this command *needs* a subcommand). */
1095 while (**line
== ' ' || **line
== '\t')
1098 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1099 undef_cmd_error (c
->prefixname
, *line
);
1101 /* Seems to be what he wants. Return it. */
1107 /* We are here presumably because an alias or command in *TEXT is
1108 deprecated and a warning message should be generated. This function
1109 decodes *TEXT and potentially generates a warning message as outlined
1112 Example for 'set endian big' which has a fictitious alias 'seb'.
1114 If alias wasn't used in *TEXT, and the command is deprecated:
1115 "warning: 'set endian big' is deprecated."
1117 If alias was used, and only the alias is deprecated:
1118 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1120 If alias was used and command is deprecated (regardless of whether the
1121 alias itself is deprecated:
1123 "warning: 'set endian big' (seb) is deprecated."
1125 After the message has been sent, clear the appropriate flags in the
1126 command and/or the alias so the user is no longer bothered.
1130 deprecated_cmd_warning (char **text
)
1132 struct cmd_list_element
*alias
= NULL
;
1133 struct cmd_list_element
*prefix_cmd
= NULL
;
1134 struct cmd_list_element
*cmd
= NULL
;
1135 struct cmd_list_element
*c
;
1138 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1139 /* return if text doesn't evaluate to a command */
1142 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1143 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1144 /* return if nothing is deprecated */
1147 printf_filtered ("Warning:");
1149 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1150 printf_filtered (" '%s', an alias for the", alias
->name
);
1152 printf_filtered (" command '");
1155 printf_filtered ("%s", prefix_cmd
->prefixname
);
1157 printf_filtered ("%s", cmd
->name
);
1159 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1160 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1162 printf_filtered ("' is deprecated.\n");
1165 /* if it is only the alias that is deprecated, we want to indicate the
1166 new alias, otherwise we'll indicate the new command */
1168 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1170 if (alias
->replacement
)
1171 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1173 printf_filtered ("No alternative known.\n\n");
1177 if (cmd
->replacement
)
1178 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1180 printf_filtered ("No alternative known.\n\n");
1183 /* We've warned you, now we'll keep quiet */
1185 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1187 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1192 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1193 Return 1 on success, 0 on failure.
1195 If LINE refers to an alias, *alias will point to that alias.
1197 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1198 command) set *prefix_cmd.
1200 Set *cmd to point to the command LINE indicates.
1202 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1203 exist, they are NULL when we return.
1207 lookup_cmd_composition (char *text
,
1208 struct cmd_list_element
**alias
,
1209 struct cmd_list_element
**prefix_cmd
,
1210 struct cmd_list_element
**cmd
)
1213 int len
, tmp
, nfound
;
1214 struct cmd_list_element
*cur_list
;
1215 struct cmd_list_element
*prev_cmd
;
1224 /* Go through as many command lists as we need to
1225 to find the command TEXT refers to. */
1229 while (*text
== ' ' || *text
== '\t')
1232 /* Treating underscores as part of command words is important
1233 so that "set args_foo()" doesn't get interpreted as
1234 "set args _foo()". */
1236 *p
&& (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1238 (*p
== '+' || *p
== '<' || *p
== '>' || *p
== '$')) ||
1239 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')));
1243 /* If nothing but whitespace, return. */
1249 /* text and p now bracket the first command word to lookup (and
1250 it's length is len). We copy this into a local temporary */
1252 command
= (char *) alloca (len
+ 1);
1253 for (tmp
= 0; tmp
< len
; tmp
++)
1258 command
[len
] = '\0';
1263 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1265 /* We didn't find the command in the entered case, so lower case it
1268 if (!*cmd
|| nfound
== 0)
1270 for (tmp
= 0; tmp
< len
; tmp
++)
1272 char x
= command
[tmp
];
1273 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1275 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1278 if (*cmd
== (struct cmd_list_element
*) -1)
1280 return 0; /* ambiguous */
1284 return 0; /* nothing found */
1287 if ((*cmd
)->cmd_pointer
)
1289 /* cmd was actually an alias, we note that an alias was used
1290 (by assigning *alais) and we set *cmd.
1293 *cmd
= (*cmd
)->cmd_pointer
;
1295 *prefix_cmd
= prev_cmd
;
1297 if ((*cmd
)->prefixlist
)
1298 cur_list
= *(*cmd
)->prefixlist
;
1306 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1308 /* Return a vector of char pointers which point to the different
1309 possible completions in LIST of TEXT.
1311 WORD points in the same buffer as TEXT, and completions should be
1312 returned relative to this position. For example, suppose TEXT is "foo"
1313 and we want to complete to "foobar". If WORD is "oo", return
1314 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1317 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1319 struct cmd_list_element
*ptr
;
1321 int sizeof_matchlist
;
1323 int textlen
= strlen (text
);
1325 sizeof_matchlist
= 10;
1326 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1329 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1330 if (!strncmp (ptr
->name
, text
, textlen
)
1331 && !ptr
->abbrev_flag
1333 || ptr
->prefixlist
))
1335 if (matches
== sizeof_matchlist
)
1337 sizeof_matchlist
*= 2;
1338 matchlist
= (char **) xrealloc ((char *) matchlist
,
1340 * sizeof (char *)));
1343 matchlist
[matches
] = (char *)
1344 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1346 strcpy (matchlist
[matches
], ptr
->name
);
1347 else if (word
> text
)
1349 /* Return some portion of ptr->name. */
1350 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1354 /* Return some of text plus ptr->name. */
1355 strncpy (matchlist
[matches
], word
, text
- word
);
1356 matchlist
[matches
][text
- word
] = '\0';
1357 strcat (matchlist
[matches
], ptr
->name
);
1369 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1370 * sizeof (char *)));
1371 matchlist
[matches
] = (char *) 0;
1377 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1379 /* Return a vector of char pointers which point to the different
1380 possible completions in CMD of TEXT.
1382 WORD points in the same buffer as TEXT, and completions should be
1383 returned relative to this position. For example, suppose TEXT is "foo"
1384 and we want to complete to "foobar". If WORD is "oo", return
1385 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1388 complete_on_enum (const char *enumlist
[],
1393 int sizeof_matchlist
;
1395 int textlen
= strlen (text
);
1399 sizeof_matchlist
= 10;
1400 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1403 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1404 if (strncmp (name
, text
, textlen
) == 0)
1406 if (matches
== sizeof_matchlist
)
1408 sizeof_matchlist
*= 2;
1409 matchlist
= (char **) xrealloc ((char *) matchlist
,
1411 * sizeof (char *)));
1414 matchlist
[matches
] = (char *)
1415 xmalloc (strlen (word
) + strlen (name
) + 1);
1417 strcpy (matchlist
[matches
], name
);
1418 else if (word
> text
)
1420 /* Return some portion of name. */
1421 strcpy (matchlist
[matches
], name
+ (word
- text
));
1425 /* Return some of text plus name. */
1426 strncpy (matchlist
[matches
], word
, text
- word
);
1427 matchlist
[matches
][text
- word
] = '\0';
1428 strcat (matchlist
[matches
], name
);
1440 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1441 * sizeof (char *)));
1442 matchlist
[matches
] = (char *) 0;