1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
30 #include "gnu-regex.h"
31 /* FIXME: this should be auto-configured! */
36 /* Prototypes for local functions */
38 static void undef_cmd_error PARAMS ((char *, char *));
40 static void show_user PARAMS ((char *, int));
42 static void show_user_1 (struct cmd_list_element *, struct ui_file *);
44 static void make_command PARAMS ((char *, int));
46 static void shell_escape PARAMS ((char *, int));
48 static int parse_binary_operation PARAMS ((char *));
50 static void print_doc_line (struct ui_file *, char *);
52 static struct cmd_list_element *find_cmd PARAMS ((char *command,
54 struct cmd_list_element * clist,
55 int ignore_help_classes,
57 static void apropos_cmd_helper (struct ui_file *, struct cmd_list_element *,
58 struct re_pattern_buffer *, char *);
60 void apropos_command (char *, int);
62 void _initialize_command PARAMS ((void));
64 /* Add element named NAME.
65 CLASS is the top level category into which commands are broken down
67 FUN should be the function to execute the command;
68 it will get a character string as argument, with leading
69 and trailing blanks already eliminated.
71 DOC is a documentation string for the command.
72 Its first line should be a complete sentence.
73 It should start with ? for a command that is an abbreviation
74 or with * for a command that most users don't need to know about.
76 Add this command to command list *LIST.
78 Returns a pointer to the added command (not necessarily the head
81 struct cmd_list_element *
82 add_cmd (name, class, fun, doc, list)
84 enum command_class class;
85 void (*fun) PARAMS ((char *, int));
87 struct cmd_list_element **list;
89 register struct cmd_list_element *c
90 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
91 struct cmd_list_element *p;
93 delete_cmd (name, list);
95 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
103 while (p->next && STRCMP (p->next->name, name) <= 0)
113 c->function.cfunc = fun;
116 c->replacement = NULL;
118 c->prefixlist = NULL;
119 c->prefixname = NULL;
120 c->allow_unknown = 0;
122 c->completer = make_symbol_completion_list;
123 c->type = not_set_cmd;
125 c->var_type = var_boolean;
127 c->user_commands = NULL;
129 c->cmd_pointer = NULL;
135 /* Deprecates a command CMD.
136 REPLACEMENT is the name of the command which should be used in place
137 of this command, or NULL if no such command exists.
139 This function does not check to see if command REPLACEMENT exists
140 since gdb may not have gotten around to adding REPLACEMENT when this
143 Returns a pointer to the deprecated command. */
145 struct cmd_list_element *
146 deprecate_cmd (cmd, replacement)
147 struct cmd_list_element *cmd;
150 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
152 if (replacement != NULL)
153 cmd->replacement = replacement;
155 cmd->replacement = NULL;
161 /* Same as above, except that the abbrev_flag is set. */
163 #if 0 /* Currently unused */
165 struct cmd_list_element *
166 add_abbrev_cmd (name, class, fun, doc, list)
168 enum command_class class;
169 void (*fun) PARAMS ((char *, int));
171 struct cmd_list_element **list;
173 register struct cmd_list_element *c
174 = add_cmd (name, class, fun, doc, list);
182 struct cmd_list_element *
183 add_alias_cmd (name, oldname, class, abbrev_flag, list)
186 enum command_class class;
188 struct cmd_list_element **list;
190 /* Must do this since lookup_cmd tries to side-effect its first arg */
192 register struct cmd_list_element *old;
193 register struct cmd_list_element *c;
194 copied_name = (char *) alloca (strlen (oldname) + 1);
195 strcpy (copied_name, oldname);
196 old = lookup_cmd (&copied_name, *list, "", 1, 1);
200 delete_cmd (name, list);
204 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
205 c->prefixlist = old->prefixlist;
206 c->prefixname = old->prefixname;
207 c->allow_unknown = old->allow_unknown;
208 c->abbrev_flag = abbrev_flag;
209 c->cmd_pointer = old;
213 /* Like add_cmd but adds an element for a command prefix:
214 a name that should be followed by a subcommand to be looked up
215 in another command list. PREFIXLIST should be the address
216 of the variable containing that list. */
218 struct cmd_list_element *
219 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
222 enum command_class class;
223 void (*fun) PARAMS ((char *, int));
225 struct cmd_list_element **prefixlist;
228 struct cmd_list_element **list;
230 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
231 c->prefixlist = prefixlist;
232 c->prefixname = prefixname;
233 c->allow_unknown = allow_unknown;
237 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
239 struct cmd_list_element *
240 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
243 enum command_class class;
244 void (*fun) PARAMS ((char *, int));
246 struct cmd_list_element **prefixlist;
249 struct cmd_list_element **list;
251 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
252 c->prefixlist = prefixlist;
253 c->prefixname = prefixname;
254 c->allow_unknown = allow_unknown;
259 /* This is an empty "cfunc". */
261 not_just_help_class_command (args, from_tty)
267 /* This is an empty "sfunc". */
268 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
271 empty_sfunc (args, from_tty, c)
274 struct cmd_list_element *c;
278 /* Add element named NAME to command list LIST (the list for set
279 or some sublist thereof).
280 CLASS is as in add_cmd.
281 VAR_TYPE is the kind of thing we are setting.
282 VAR is address of the variable being controlled by this command.
283 DOC is the documentation string. */
285 struct cmd_list_element *
286 add_set_cmd (name, class, var_type, var, doc, list)
288 enum command_class class;
292 struct cmd_list_element **list;
294 struct cmd_list_element *c
295 = add_cmd (name, class, NO_FUNCTION, doc, list);
298 c->var_type = var_type;
300 /* This needs to be something besides NO_FUNCTION so that this isn't
301 treated as a help class. */
302 c->function.sfunc = empty_sfunc;
306 /* Add element named NAME to command list LIST (the list for set
307 or some sublist thereof).
308 CLASS is as in add_cmd.
309 ENUMLIST is a list of strings which may follow NAME.
310 VAR is address of the variable which will contain the matching string
312 DOC is the documentation string. */
314 struct cmd_list_element *
315 add_set_enum_cmd (name, class, enumlist, var, doc, list)
317 enum command_class class;
321 struct cmd_list_element **list;
323 struct cmd_list_element *c
324 = add_set_cmd (name, class, var_enum, var, doc, list);
330 /* Where SETCMD has already been added, add the corresponding show
331 command to LIST and return a pointer to the added command (not
332 necessarily the head of LIST). */
333 struct cmd_list_element *
334 add_show_from_set (setcmd, list)
335 struct cmd_list_element *setcmd;
336 struct cmd_list_element **list;
338 struct cmd_list_element *showcmd =
339 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
340 struct cmd_list_element *p;
342 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
343 delete_cmd (showcmd->name, list);
344 showcmd->type = show_cmd;
346 /* Replace "set " at start of docstring with "show ". */
347 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
348 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
349 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
351 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
353 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
355 showcmd->next = *list;
361 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
365 showcmd->next = p->next;
372 /* Remove the command named NAME from the command list. */
375 delete_cmd (name, list)
377 struct cmd_list_element **list;
379 register struct cmd_list_element *c;
380 struct cmd_list_element *p;
382 while (*list && STREQ ((*list)->name, name))
385 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
392 for (c = *list; c->next;)
394 if (STREQ (c->next->name, name))
397 c->next->hookee->hook = 0; /* hooked cmd gets away. */
399 free ((PTR) c->next);
406 /* Recursively walk the commandlist structures, and print out the
407 documentation of commands that match our regex in either their
408 name, or their documentation.
411 apropos_cmd_helper (struct ui_file *stream, struct cmd_list_element *commandlist,
412 struct re_pattern_buffer *regex, char *prefix)
414 register struct cmd_list_element *c;
415 int returnvalue=1; /*Needed to avoid double printing*/
416 /* Walk through the commands */
417 for (c=commandlist;c;c=c->next)
421 /* Try to match against the name*/
422 returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
423 if (returnvalue >= 0)
425 /* Stolen from help_cmd_list. We don't directly use
426 * help_cmd_list because it doesn't let us print out
429 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
430 print_doc_line (stream, c->doc);
431 fputs_filtered ("\n", stream);
432 returnvalue=0; /*Set this so we don't print it again.*/
435 if (c->doc != NULL && returnvalue != 0)
437 /* Try to match against documentation */
438 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
440 /* Stolen from help_cmd_list. We don't directly use
441 * help_cmd_list because it doesn't let us print out
444 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
445 print_doc_line (stream, c->doc);
446 fputs_filtered ("\n", stream);
449 /* Check if this command has subcommands */
450 if (c->prefixlist != NULL)
452 /* Recursively call ourselves on the subcommand list,
453 passing the right prefix in.
455 apropos_cmd_helper(stream,*c->prefixlist,regex,c->prefixname);
459 /* Search through names of commands and documentations for a certain
463 apropos_command (char *searchstr, int from_tty)
465 extern struct cmd_list_element *cmdlist; /*This is the main command list*/
467 char *pattern_fastmap;
468 char errorbuffer[512];
469 pattern_fastmap=calloc(256,sizeof(char));
470 if (searchstr == NULL)
471 error("REGEXP string is empty");
473 if (regcomp(&pattern,searchstr,REG_ICASE) == 0)
475 pattern.fastmap=pattern_fastmap;
476 re_compile_fastmap(&pattern);
477 apropos_cmd_helper(gdb_stdout,cmdlist,&pattern,"");
481 regerror(regcomp(&pattern,searchstr,REG_ICASE),NULL,errorbuffer,512);
482 error("Error in regular expression:%s",errorbuffer);
484 free(pattern_fastmap);
488 /* This command really has to deal with two things:
489 * 1) I want documentation on *this string* (usually called by
490 * "help commandname").
491 * 2) I want documentation on *this list* (usually called by
492 * giving a command that requires subcommands. Also called by saying
495 * I am going to split this into two seperate comamnds, help_cmd and
500 help_cmd (command, stream)
502 struct ui_file *stream;
504 struct cmd_list_element *c;
505 extern struct cmd_list_element *cmdlist;
509 help_list (cmdlist, "", all_classes, stream);
513 c = lookup_cmd (&command, cmdlist, "", 0, 0);
518 /* There are three cases here.
519 If c->prefixlist is nonzero, we have a prefix command.
520 Print its documentation, then list its subcommands.
522 If c->function is nonzero, we really have a command.
523 Print its documentation and return.
525 If c->function is zero, we have a class name.
526 Print its documentation (as if it were a command)
527 and then set class to the number of this class
528 so that the commands in the class will be listed. */
530 fputs_filtered (c->doc, stream);
531 fputs_filtered ("\n", stream);
533 if (c->prefixlist == 0 && c->function.cfunc != NULL)
535 fprintf_filtered (stream, "\n");
537 /* If this is a prefix command, print it's subcommands */
539 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
541 /* If this is a class name, print all of the commands in the class */
542 if (c->function.cfunc == NULL)
543 help_list (cmdlist, "", c->class, stream);
546 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
551 * Get a specific kind of help on a command list.
554 * CMDTYPE is the prefix to use in the title string.
555 * CLASS is the class with which to list the nodes of this list (see
556 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
557 * everything, ALL_CLASSES for just classes, and non-negative for only things
558 * in a specific class.
559 * and STREAM is the output stream on which to print things.
560 * If you call this routine with a class >= 0, it recurses.
563 help_list (list, cmdtype, class, stream)
564 struct cmd_list_element *list;
566 enum command_class class;
567 struct ui_file *stream;
570 char *cmdtype1, *cmdtype2;
572 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
573 len = strlen (cmdtype);
574 cmdtype1 = (char *) alloca (len + 1);
576 cmdtype2 = (char *) alloca (len + 4);
581 strncpy (cmdtype1 + 1, cmdtype, len - 1);
583 strncpy (cmdtype2, cmdtype, len - 1);
584 strcpy (cmdtype2 + len - 1, " sub");
587 if (class == all_classes)
588 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
590 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
592 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
594 if (class == all_classes)
595 fprintf_filtered (stream, "\n\
596 Type \"help%s\" followed by a class name for a list of commands in that class.",
599 fprintf_filtered (stream, "\n\
600 Type \"help%s\" followed by %scommand name for full documentation.\n\
601 Command name abbreviations are allowed if unambiguous.\n",
605 /* Print only the first line of STR on STREAM. */
607 print_doc_line (stream, str)
608 struct ui_file *stream;
611 static char *line_buffer = 0;
612 static int line_size;
618 line_buffer = (char *) xmalloc (line_size);
622 while (*p && *p != '\n' && *p != '.' && *p != ',')
624 if (p - str > line_size - 1)
626 line_size = p - str + 1;
627 free ((PTR) line_buffer);
628 line_buffer = (char *) xmalloc (line_size);
630 strncpy (line_buffer, str, p - str);
631 line_buffer[p - str] = '\0';
632 if (islower (line_buffer[0]))
633 line_buffer[0] = toupper (line_buffer[0]);
635 ui_out_text (uiout, line_buffer);
637 fputs_filtered (line_buffer, stream);
642 * Implement a help command on command list LIST.
643 * RECURSE should be non-zero if this should be done recursively on
644 * all sublists of LIST.
645 * PREFIX is the prefix to print before each command name.
646 * STREAM is the stream upon which the output should be written.
648 * A non-negative class number to list only commands in that
650 * ALL_COMMANDS to list all commands in list.
651 * ALL_CLASSES to list all classes in list.
653 * Note that RECURSE will be active on *all* sublists, not just the
654 * ones selected by the criteria above (ie. the selection mechanism
655 * is at the low level, not the high-level).
658 help_cmd_list (list, class, prefix, recurse, stream)
659 struct cmd_list_element *list;
660 enum command_class class;
663 struct ui_file *stream;
665 register struct cmd_list_element *c;
667 for (c = list; c; c = c->next)
669 if (c->abbrev_flag == 0 &&
670 (class == all_commands
671 || (class == all_classes && c->function.cfunc == NULL)
672 || (class == c->class && c->function.cfunc != NULL)))
674 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
675 print_doc_line (stream, c->doc);
676 fputs_filtered ("\n", stream);
679 && c->prefixlist != 0
680 && c->abbrev_flag == 0)
681 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
686 /* Search the input clist for 'command'. Return the command if
687 found (or NULL if not), and return the number of commands
690 static struct cmd_list_element *
691 find_cmd (command, len, clist, ignore_help_classes, nfound)
694 struct cmd_list_element *clist;
695 int ignore_help_classes;
698 struct cmd_list_element *found, *c;
700 found = (struct cmd_list_element *) NULL;
702 for (c = clist; c; c = c->next)
703 if (!strncmp (command, c->name, len)
704 && (!ignore_help_classes || c->function.cfunc))
708 if (c->name[len] == '\0')
717 /* This routine takes a line of TEXT and a CLIST in which to start the
718 lookup. When it returns it will have incremented the text pointer past
719 the section of text it matched, set *RESULT_LIST to point to the list in
720 which the last word was matched, and will return a pointer to the cmd
721 list element which the text matches. It will return NULL if no match at
722 all was possible. It will return -1 (cast appropriately, ick) if ambigous
723 matches are possible; in this case *RESULT_LIST will be set to point to
724 the list in which there are ambiguous choices (and *TEXT will be set to
725 the ambiguous text string).
727 If the located command was an abbreviation, this routine returns the base
728 command of the abbreviation.
730 It does no error reporting whatsoever; control will always return
731 to the superior routine.
733 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
734 at the prefix_command (ie. the best match) *or* (special case) will be NULL
735 if no prefix command was ever found. For example, in the case of "info a",
736 "info" matches without ambiguity, but "a" could be "args" or "address", so
737 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
738 RESULT_LIST should not be interpeted as a pointer to the beginning of a
739 list; it simply points to a specific command. In the case of an ambiguous
740 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
741 "info t" can be "info types" or "info target"; upon return *TEXT has been
742 advanced past "info ").
744 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
745 affect the operation).
747 This routine does *not* modify the text pointed to by TEXT.
749 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
750 are actually help classes rather than commands (i.e. the function field of
751 the struct cmd_list_element is NULL). */
753 struct cmd_list_element *
754 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
756 struct cmd_list_element *clist, **result_list;
757 int ignore_help_classes;
760 int len, tmp, nfound;
761 struct cmd_list_element *found, *c;
764 while (**text == ' ' || **text == '\t')
767 /* Treating underscores as part of command words is important
768 so that "set args_foo()" doesn't get interpreted as
769 "set args _foo()". */
771 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
773 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
774 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
778 /* If nothing but whitespace, return 0. */
784 /* *text and p now bracket the first command word to lookup (and
785 it's length is len). We copy this into a local temporary */
788 command = (char *) alloca (len + 1);
789 for (tmp = 0; tmp < len; tmp++)
791 char x = (*text)[tmp];
799 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
802 ** We didn't find the command in the entered case, so lower case it
805 if (!found || nfound == 0)
807 for (tmp = 0; tmp < len; tmp++)
809 char x = command[tmp];
810 command[tmp] = isupper (x) ? tolower (x) : x;
812 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
815 /* If nothing matches, we have a simple failure. */
821 if (result_list != NULL)
822 /* Will be modified in calling routine
823 if we know what the prefix command is. */
825 return (struct cmd_list_element *) -1; /* Ambiguous. */
828 /* We've matched something on this list. Move text pointer forward. */
832 if (found->cmd_pointer)
834 /* We drop the alias (abbreviation) in favor of the command it is
835 pointing to. If the alias is deprecated, though, we need to
836 warn the user about it before we drop it. Note that while we
837 are warning about the alias, we may also warn about the command
838 itself and we will adjust the appropriate DEPRECATED_WARN_USER
841 if (found->flags & DEPRECATED_WARN_USER)
842 deprecated_cmd_warning (&line);
843 found = found->cmd_pointer;
845 /* If we found a prefix command, keep looking. */
847 if (found->prefixlist)
849 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
850 ignore_help_classes);
853 /* Didn't find anything; this is as far as we got. */
854 if (result_list != NULL)
855 *result_list = clist;
858 else if (c == (struct cmd_list_element *) -1)
860 /* We've gotten this far properly, but the next step
861 is ambiguous. We need to set the result list to the best
862 we've found (if an inferior hasn't already set it). */
863 if (result_list != NULL)
865 /* This used to say *result_list = *found->prefixlist
866 If that was correct, need to modify the documentation
867 at the top of this function to clarify what is supposed
869 *result_list = found;
880 if (result_list != NULL)
881 *result_list = clist;
886 /* All this hair to move the space to the front of cmdtype */
889 undef_cmd_error (cmdtype, q)
892 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
896 strlen (cmdtype) - 1,
900 /* Look up the contents of *LINE as a command in the command list LIST.
901 LIST is a chain of struct cmd_list_element's.
902 If it is found, return the struct cmd_list_element for that command
903 and update *LINE to point after the command name, at the first argument.
904 If not found, call error if ALLOW_UNKNOWN is zero
905 otherwise (or if error returns) return zero.
906 Call error if specified command is ambiguous,
907 unless ALLOW_UNKNOWN is negative.
908 CMDTYPE precedes the word "command" in the error message.
910 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
911 elements which are actually help classes rather than commands (i.e.
912 the function field of the struct cmd_list_element is 0). */
914 struct cmd_list_element *
915 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
917 struct cmd_list_element *list;
920 int ignore_help_classes;
922 struct cmd_list_element *last_list = 0;
923 struct cmd_list_element *c =
924 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
926 /* This is wrong for complete_command. */
927 char *ptr = (*line) + strlen (*line) - 1;
929 /* Clear off trailing whitespace. */
930 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
940 error ("Lack of needed %scommand", cmdtype);
945 while (isalnum (*p) || *p == '-')
948 q = (char *) alloca (p - *line + 1);
949 strncpy (q, *line, p - *line);
951 undef_cmd_error (cmdtype, q);
957 else if (c == (struct cmd_list_element *) -1)
959 /* Ambigous. Local values should be off prefixlist or called
961 int local_allow_unknown = (last_list ? last_list->allow_unknown :
963 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
964 struct cmd_list_element *local_list =
965 (last_list ? *(last_list->prefixlist) : list);
967 if (local_allow_unknown < 0)
970 return last_list; /* Found something. */
972 return 0; /* Found nothing. */
976 /* Report as error. */
981 ((*line)[amb_len] && (*line)[amb_len] != ' '
982 && (*line)[amb_len] != '\t');
987 for (c = local_list; c; c = c->next)
988 if (!strncmp (*line, c->name, amb_len))
990 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
993 strcat (ambbuf, ", ");
994 strcat (ambbuf, c->name);
998 strcat (ambbuf, "..");
1002 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
1004 return 0; /* lint */
1009 /* We've got something. It may still not be what the caller
1010 wants (if this command *needs* a subcommand). */
1011 while (**line == ' ' || **line == '\t')
1014 if (c->prefixlist && **line && !c->allow_unknown)
1015 undef_cmd_error (c->prefixname, *line);
1017 /* Seems to be what he wants. Return it. */
1023 /* We are here presumably because an alias or command in *TEXT is
1024 deprecated and a warning message should be generated. This function
1025 decodes *TEXT and potentially generates a warning message as outlined
1028 Example for 'set endian big' which has a fictitious alias 'seb'.
1030 If alias wasn't used in *TEXT, and the command is deprecated:
1031 "warning: 'set endian big' is deprecated."
1033 If alias was used, and only the alias is deprecated:
1034 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1036 If alias was used and command is deprecated (regardless of whether the
1037 alias itself is deprecated:
1039 "warning: 'set endian big' (seb) is deprecated."
1041 After the message has been sent, clear the appropriate flags in the
1042 command and/or the alias so the user is no longer bothered.
1046 deprecated_cmd_warning (char **text)
1048 struct cmd_list_element *alias = NULL;
1049 struct cmd_list_element *prefix_cmd = NULL;
1050 struct cmd_list_element *cmd = NULL;
1051 struct cmd_list_element *c;
1054 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1055 /* return if text doesn't evaluate to a command */
1058 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1059 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1060 /* return if nothing is deprecated */
1063 printf_filtered ("Warning:");
1065 if (alias && !(cmd->flags & CMD_DEPRECATED))
1066 printf_filtered (" '%s', an alias for the", alias->name);
1068 printf_filtered (" command '");
1071 printf_filtered ("%s", prefix_cmd->prefixname);
1073 printf_filtered ("%s", cmd->name);
1075 if (alias && (cmd->flags & CMD_DEPRECATED))
1076 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1078 printf_filtered ("' is deprecated.\n");
1081 /* if it is only the alias that is deprecated, we want to indicate the
1082 new alias, otherwise we'll indicate the new command */
1084 if (alias && !(cmd->flags & CMD_DEPRECATED))
1086 if (alias->replacement)
1087 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1089 printf_filtered ("No alternative known.\n\n");
1093 if (cmd->replacement)
1094 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1096 printf_filtered ("No alternative known.\n\n");
1099 /* We've warned you, now we'll keep quiet */
1101 alias->flags &= ~DEPRECATED_WARN_USER;
1103 cmd->flags &= ~DEPRECATED_WARN_USER;
1108 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1109 Return 1 on success, 0 on failure.
1111 If LINE refers to an alias, *alias will point to that alias.
1113 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1114 command) set *prefix_cmd.
1116 Set *cmd to point to the command LINE indicates.
1118 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1119 exist, they are NULL when we return.
1123 lookup_cmd_composition (char *text,
1124 struct cmd_list_element **alias,
1125 struct cmd_list_element **prefix_cmd,
1126 struct cmd_list_element **cmd)
1129 int len, tmp, nfound;
1130 struct cmd_list_element *cur_list;
1131 struct cmd_list_element *prev_cmd;
1140 /* Go through as many command lists as we need to
1141 to find the command TEXT refers to. */
1145 while (*text == ' ' || *text == '\t')
1148 /* Treating underscores as part of command words is important
1149 so that "set args_foo()" doesn't get interpreted as
1150 "set args _foo()". */
1152 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1154 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1155 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1159 /* If nothing but whitespace, return. */
1165 /* text and p now bracket the first command word to lookup (and
1166 it's length is len). We copy this into a local temporary */
1168 command = (char *) alloca (len + 1);
1169 for (tmp = 0; tmp < len; tmp++)
1174 command[len] = '\0';
1179 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1181 /* We didn't find the command in the entered case, so lower case it
1184 if (!*cmd || nfound == 0)
1186 for (tmp = 0; tmp < len; tmp++)
1188 char x = command[tmp];
1189 command[tmp] = isupper (x) ? tolower (x) : x;
1191 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1194 if (*cmd == (struct cmd_list_element *) -1)
1196 return 0; /* ambiguous */
1200 return 0; /* nothing found */
1203 if ((*cmd)->cmd_pointer)
1205 /* cmd was actually an alias, we note that an alias was used
1206 (by assigning *alais) and we set *cmd.
1209 *cmd = (*cmd)->cmd_pointer;
1211 *prefix_cmd = prev_cmd;
1213 if ((*cmd)->prefixlist)
1214 cur_list = *(*cmd)->prefixlist;
1226 /* Look up the contents of *LINE as a command in the command list LIST.
1227 LIST is a chain of struct cmd_list_element's.
1228 If it is found, return the struct cmd_list_element for that command
1229 and update *LINE to point after the command name, at the first argument.
1230 If not found, call error if ALLOW_UNKNOWN is zero
1231 otherwise (or if error returns) return zero.
1232 Call error if specified command is ambiguous,
1233 unless ALLOW_UNKNOWN is negative.
1234 CMDTYPE precedes the word "command" in the error message. */
1236 struct cmd_list_element *
1237 lookup_cmd (line, list, cmdtype, allow_unknown)
1239 struct cmd_list_element *list;
1244 register struct cmd_list_element *c, *found;
1247 char *processed_cmd;
1250 /* Skip leading whitespace. */
1252 while (**line == ' ' || **line == '\t')
1255 /* Clear out trailing whitespace. */
1257 p = *line + strlen (*line);
1258 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
1262 /* Find end of command name. */
1265 while (*p == '-' || isalnum (*p))
1268 /* Look up the command name.
1269 If exact match, keep that.
1270 Otherwise, take command abbreviated, if unique. Note that (in my
1271 opinion) a null string does *not* indicate ambiguity; simply the
1272 end of the argument. */
1277 error ("Lack of needed %scommand", cmdtype);
1281 /* Copy over to a local buffer, converting to lowercase on the way.
1282 This is in case the command being parsed is a subcommand which
1283 doesn't match anything, and that's ok. We want the original
1284 untouched for the routine of the original command. */
1286 processed_cmd = (char *) alloca (p - *line + 1);
1287 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
1289 char x = (*line)[cmd_len];
1291 processed_cmd[cmd_len] = tolower (x);
1293 processed_cmd[cmd_len] = x;
1295 processed_cmd[cmd_len] = '\0';
1297 /* Check all possibilities in the current command list. */
1300 for (c = list; c; c = c->next)
1302 if (!strncmp (processed_cmd, c->name, cmd_len))
1306 if (c->name[cmd_len] == 0)
1314 /* Report error for undefined command name. */
1318 if (nfound > 1 && allow_unknown >= 0)
1321 for (c = list; c; c = c->next)
1322 if (!strncmp (processed_cmd, c->name, cmd_len))
1324 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1326 if (strlen (ambbuf))
1327 strcat (ambbuf, ", ");
1328 strcat (ambbuf, c->name);
1332 strcat (ambbuf, "..");
1336 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1337 processed_cmd, ambbuf);
1339 else if (!allow_unknown)
1340 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1344 /* Skip whitespace before the argument. */
1346 while (*p == ' ' || *p == '\t')
1350 if (found->prefixlist && *p)
1352 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1353 found->allow_unknown);
1362 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1364 /* Return a vector of char pointers which point to the different
1365 possible completions in LIST of TEXT.
1367 WORD points in the same buffer as TEXT, and completions should be
1368 returned relative to this position. For example, suppose TEXT is "foo"
1369 and we want to complete to "foobar". If WORD is "oo", return
1370 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1373 complete_on_cmdlist (list, text, word)
1374 struct cmd_list_element *list;
1378 struct cmd_list_element *ptr;
1380 int sizeof_matchlist;
1382 int textlen = strlen (text);
1384 sizeof_matchlist = 10;
1385 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1388 for (ptr = list; ptr; ptr = ptr->next)
1389 if (!strncmp (ptr->name, text, textlen)
1390 && !ptr->abbrev_flag
1391 && (ptr->function.cfunc
1392 || ptr->prefixlist))
1394 if (matches == sizeof_matchlist)
1396 sizeof_matchlist *= 2;
1397 matchlist = (char **) xrealloc ((char *) matchlist,
1399 * sizeof (char *)));
1402 matchlist[matches] = (char *)
1403 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1405 strcpy (matchlist[matches], ptr->name);
1406 else if (word > text)
1408 /* Return some portion of ptr->name. */
1409 strcpy (matchlist[matches], ptr->name + (word - text));
1413 /* Return some of text plus ptr->name. */
1414 strncpy (matchlist[matches], word, text - word);
1415 matchlist[matches][text - word] = '\0';
1416 strcat (matchlist[matches], ptr->name);
1423 free ((PTR) matchlist);
1428 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1429 * sizeof (char *)));
1430 matchlist[matches] = (char *) 0;
1436 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1438 /* Return a vector of char pointers which point to the different
1439 possible completions in CMD of TEXT.
1441 WORD points in the same buffer as TEXT, and completions should be
1442 returned relative to this position. For example, suppose TEXT is "foo"
1443 and we want to complete to "foobar". If WORD is "oo", return
1444 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1447 complete_on_enum (enumlist, text, word)
1453 int sizeof_matchlist;
1455 int textlen = strlen (text);
1459 sizeof_matchlist = 10;
1460 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1463 for (i = 0; (name = enumlist[i]) != NULL; i++)
1464 if (strncmp (name, text, textlen) == 0)
1466 if (matches == sizeof_matchlist)
1468 sizeof_matchlist *= 2;
1469 matchlist = (char **) xrealloc ((char *) matchlist,
1471 * sizeof (char *)));
1474 matchlist[matches] = (char *)
1475 xmalloc (strlen (word) + strlen (name) + 1);
1477 strcpy (matchlist[matches], name);
1478 else if (word > text)
1480 /* Return some portion of name. */
1481 strcpy (matchlist[matches], name + (word - text));
1485 /* Return some of text plus name. */
1486 strncpy (matchlist[matches], word, text - word);
1487 matchlist[matches][text - word] = '\0';
1488 strcat (matchlist[matches], name);
1495 free ((PTR) matchlist);
1500 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1501 * sizeof (char *)));
1502 matchlist[matches] = (char *) 0;
1509 parse_binary_operation (arg)
1517 length = strlen (arg);
1519 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1522 if (!strncmp (arg, "on", length)
1523 || !strncmp (arg, "1", length)
1524 || !strncmp (arg, "yes", length))
1526 else if (!strncmp (arg, "off", length)
1527 || !strncmp (arg, "0", length)
1528 || !strncmp (arg, "no", length))
1532 error ("\"on\" or \"off\" expected.");
1537 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1538 of the argument, and FROM_TTY is nonzero if this command is being entered
1539 directly by the user (i.e. these are just like any other
1540 command). C is the command list element for the command. */
1542 do_setshow_command (arg, from_tty, c)
1545 struct cmd_list_element *c;
1547 if (c->type == set_cmd)
1549 switch (c->var_type)
1560 new = (char *) xmalloc (strlen (arg) + 2);
1563 while ((ch = *p++) != '\000')
1567 /* \ at end of argument is used after spaces
1568 so they won't be lost. */
1569 /* This is obsolete now that we no longer strip
1570 trailing whitespace and actually, the backslash
1571 didn't get here in my test, readline or
1572 something did something funky with a backslash
1573 right before a newline. */
1576 ch = parse_escape (&p);
1578 break; /* C loses */
1586 if (*(p - 1) != '\\')
1590 new = (char *) xrealloc (new, q - new);
1591 if (*(char **) c->var != NULL)
1592 free (*(char **) c->var);
1593 *(char **) c->var = new;
1596 case var_string_noescape:
1599 if (*(char **) c->var != NULL)
1600 free (*(char **) c->var);
1601 *(char **) c->var = savestring (arg, strlen (arg));
1605 error_no_arg ("filename to set it to.");
1606 if (*(char **) c->var != NULL)
1607 free (*(char **) c->var);
1608 *(char **) c->var = tilde_expand (arg);
1611 *(int *) c->var = parse_binary_operation (arg);
1615 error_no_arg ("integer to set it to.");
1616 *(unsigned int *) c->var = parse_and_eval_address (arg);
1617 if (*(unsigned int *) c->var == 0)
1618 *(unsigned int *) c->var = UINT_MAX;
1624 error_no_arg ("integer to set it to.");
1625 val = parse_and_eval_address (arg);
1627 *(int *) c->var = INT_MAX;
1628 else if (val >= INT_MAX)
1629 error ("integer %u out of range", val);
1631 *(int *) c->var = val;
1636 error_no_arg ("integer to set it to.");
1637 *(int *) c->var = parse_and_eval_address (arg);
1647 /* if no argument was supplied, print an informative error message */
1651 strcpy (msg, "Requires an argument. Valid arguments are ");
1652 for (i = 0; c->enums[i]; i++)
1656 strcat (msg, c->enums[i]);
1662 p = strchr (arg, ' ');
1670 for (i = 0; c->enums[i]; i++)
1671 if (strncmp (arg, c->enums[i], len) == 0)
1673 match = c->enums[i];
1678 error ("Undefined item: \"%s\".", arg);
1681 error ("Ambiguous item \"%s\".", arg);
1683 *(char **) c->var = match;
1687 error ("gdb internal error: bad var_type in do_setshow_command");
1690 else if (c->type == show_cmd)
1693 struct cleanup *old_chain;
1694 struct ui_stream *stb;
1697 stb = ui_out_stream_new (uiout);
1698 old_chain = make_cleanup ((make_cleanup_func) ui_out_stream_delete, stb);
1701 /* Print doc minus "show" at start. */
1702 print_doc_line (gdb_stdout, c->doc + 5);
1705 ui_out_text (uiout, " is ");
1706 ui_out_wrap_hint (uiout, " ");
1708 switch (c->var_type)
1714 if (*(unsigned char **) c->var)
1715 fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
1719 case var_string_noescape:
1722 if (*(char **) c->var)
1723 fputs_filtered (*(char **) c->var, stb->stream);
1727 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
1730 if (*(unsigned int *) c->var == UINT_MAX)
1732 fputs_filtered ("unlimited", stb->stream);
1735 /* else fall through */
1737 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
1740 if (*(int *) c->var == INT_MAX)
1742 fputs_filtered ("unlimited", stb->stream);
1745 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
1749 error ("gdb internal error: bad var_type in do_setshow_command");
1752 ui_out_text (uiout, "\"");
1753 ui_out_field_stream (uiout, "value", stb);
1755 ui_out_text (uiout, "\"");
1756 ui_out_text (uiout, ".\n");
1757 do_cleanups (old_chain);
1759 fputs_filtered (" is ", gdb_stdout);
1761 switch (c->var_type)
1765 fputs_filtered ("\"", gdb_stdout);
1766 if (*(unsigned char **) c->var)
1767 fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
1768 fputs_filtered ("\"", gdb_stdout);
1771 case var_string_noescape:
1774 fputs_filtered ("\"", gdb_stdout);
1775 if (*(char **) c->var)
1776 fputs_filtered (*(char **) c->var, gdb_stdout);
1777 fputs_filtered ("\"", gdb_stdout);
1780 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1783 if (*(unsigned int *) c->var == UINT_MAX)
1785 fputs_filtered ("unlimited", gdb_stdout);
1788 /* else fall through */
1790 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1793 if (*(int *) c->var == INT_MAX)
1795 fputs_filtered ("unlimited", gdb_stdout);
1798 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1802 error ("gdb internal error: bad var_type in do_setshow_command");
1804 fputs_filtered (".\n", gdb_stdout);
1808 error ("gdb internal error: bad cmd_type in do_setshow_command");
1809 (*c->function.sfunc) (NULL, from_tty, c);
1810 if (c->type == set_cmd && set_hook)
1814 /* Show all the settings in a list of show commands. */
1817 cmd_show_list (list, from_tty, prefix)
1818 struct cmd_list_element *list;
1823 ui_out_list_begin (uiout, "showlist");
1825 for (; list != NULL; list = list->next)
1827 /* If we find a prefix, run its list, prefixing our output by its
1828 prefix (with "show " skipped). */
1830 if (list->prefixlist && !list->abbrev_flag)
1832 ui_out_list_begin (uiout, "optionlist");
1833 ui_out_field_string (uiout, "prefix", list->prefixname + 5);
1834 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1835 ui_out_list_end (uiout);
1837 if (list->type == show_cmd)
1839 ui_out_list_begin (uiout, "option");
1840 ui_out_text (uiout, prefix);
1841 ui_out_field_string (uiout, "name", list->name);
1842 ui_out_text (uiout, ": ");
1843 do_setshow_command ((char *) NULL, from_tty, list);
1844 ui_out_list_end (uiout);
1847 if (list->prefixlist && !list->abbrev_flag)
1848 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1849 if (list->type == show_cmd)
1851 fputs_filtered (prefix, gdb_stdout);
1852 fputs_filtered (list->name, gdb_stdout);
1853 fputs_filtered (": ", gdb_stdout);
1854 do_setshow_command ((char *) NULL, from_tty, list);
1859 ui_out_list_end (uiout);
1865 shell_escape (arg, from_tty)
1870 /* If ARG is NULL, they want an inferior shell, but `system' just
1871 reports if the shell is available when passed a NULL arg. */
1872 int rc = system (arg ? arg : "");
1875 arg = "inferior shell";
1879 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1880 safe_strerror (errno));
1881 gdb_flush (gdb_stderr);
1885 fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1886 gdb_flush (gdb_stderr);
1889 /* Make sure to return to the directory GDB thinks it is, in case the
1890 shell command we just ran changed it. */
1891 chdir (current_directory);
1893 #else /* Can fork. */
1894 int rc, status, pid;
1895 char *p, *user_shell;
1897 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1898 user_shell = "/bin/sh";
1900 /* Get the name of the shell for arg0 */
1901 if ((p = strrchr (user_shell, '/')) == NULL)
1904 p++; /* Get past '/' */
1906 if ((pid = fork ()) == 0)
1909 execl (user_shell, p, 0);
1911 execl (user_shell, p, "-c", arg, 0);
1913 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1914 safe_strerror (errno));
1915 gdb_flush (gdb_stderr);
1920 while ((rc = wait (&status)) != pid && rc != -1)
1923 error ("Fork failed");
1924 #endif /* Can fork. */
1928 make_command (arg, from_tty)
1938 p = xmalloc (sizeof ("make ") + strlen (arg));
1939 strcpy (p, "make ");
1940 strcpy (p + sizeof ("make ") - 1, arg);
1943 shell_escape (p, from_tty);
1947 show_user_1 (c, stream)
1948 struct cmd_list_element *c;
1949 struct ui_file *stream;
1951 register struct command_line *cmdlines;
1953 cmdlines = c->user_commands;
1956 fputs_filtered ("User command ", stream);
1957 fputs_filtered (c->name, stream);
1958 fputs_filtered (":\n", stream);
1961 print_command_lines (uiout, cmdlines, 1);
1962 fputs_filtered ("\n", stream);
1966 print_command_line (cmdlines, 4, stream);
1967 cmdlines = cmdlines->next;
1969 fputs_filtered ("\n", stream);
1975 show_user (args, from_tty)
1979 struct cmd_list_element *c;
1980 extern struct cmd_list_element *cmdlist;
1984 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1985 if (c->class != class_user)
1986 error ("Not a user command.");
1987 show_user_1 (c, gdb_stdout);
1991 for (c = cmdlist; c; c = c->next)
1993 if (c->class == class_user)
1994 show_user_1 (c, gdb_stdout);
2000 _initialize_command ()
2002 add_com ("shell", class_support, shell_escape,
2003 "Execute the rest of the line as a shell command. \n\
2004 With no arguments, run an inferior shell.");
2006 /* NOTE: cagney/2000-03-20: Being able to enter ``(gdb) !ls'' would
2007 be a really useful feature. Unfortunatly, the below wont do
2008 this. Instead it adds support for the form ``(gdb) ! ls''
2009 (i.e. the space is required). If the ``!'' command below is
2010 added the complains about no ``!'' command would be replaced by
2011 complains about how the ``!'' command is broken :-) */
2013 add_com_alias ("!", "shell", class_support, 0);
2015 add_com ("make", class_support, make_command,
2016 "Run the ``make'' program using the rest of the line as arguments.");
2017 add_cmd ("user", no_class, show_user,
2018 "Show definitions of user defined commands.\n\
2019 Argument is the name of the user defined command.\n\
2020 With no argument, show definitions of all user defined commands.", &showlist);
2021 add_com ("apropos", class_support, apropos_command, "Search for commands matching a REGEXP");