1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998 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"
34 /* FIXME: this should be auto-configured! */
39 /* Prototypes for local functions */
41 static void undef_cmd_error PARAMS ((char *, char *));
43 static void show_user PARAMS ((char *, int));
45 static void show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
47 static void make_command PARAMS ((char *, int));
49 static void shell_escape PARAMS ((char *, int));
51 static int parse_binary_operation PARAMS ((char *));
53 static void print_doc_line PARAMS ((GDB_FILE *, char *));
55 static struct cmd_list_element *find_cmd PARAMS ((char *command,
57 struct cmd_list_element * clist,
58 int ignore_help_classes,
61 void _initialize_command PARAMS ((void));
63 /* Add element named NAME.
64 CLASS is the top level category into which commands are broken down
66 FUN should be the function to execute the command;
67 it will get a character string as argument, with leading
68 and trailing blanks already eliminated.
70 DOC is a documentation string for the command.
71 Its first line should be a complete sentence.
72 It should start with ? for a command that is an abbreviation
73 or with * for a command that most users don't need to know about.
75 Add this command to command list *LIST.
77 Returns a pointer to the added command (not necessarily the head
80 struct cmd_list_element *
81 add_cmd (name, class, fun, doc, list)
83 enum command_class class;
84 void (*fun) PARAMS ((char *, int));
86 struct cmd_list_element **list;
88 register struct cmd_list_element *c
89 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
90 struct cmd_list_element *p;
92 delete_cmd (name, list);
94 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
102 while (p->next && STRCMP (p->next->name, name) <= 0)
112 c->function.cfunc = fun;
115 c->prefixlist = NULL;
116 c->prefixname = NULL;
117 c->allow_unknown = 0;
119 c->completer = make_symbol_completion_list;
120 c->type = not_set_cmd;
122 c->var_type = var_boolean;
124 c->user_commands = NULL;
126 c->cmd_pointer = NULL;
131 /* Same as above, except that the abbrev_flag is set. */
133 #if 0 /* Currently unused */
135 struct cmd_list_element *
136 add_abbrev_cmd (name, class, fun, doc, list)
138 enum command_class class;
139 void (*fun) PARAMS ((char *, int));
141 struct cmd_list_element **list;
143 register struct cmd_list_element *c
144 = add_cmd (name, class, fun, doc, list);
152 struct cmd_list_element *
153 add_alias_cmd (name, oldname, class, abbrev_flag, list)
156 enum command_class class;
158 struct cmd_list_element **list;
160 /* Must do this since lookup_cmd tries to side-effect its first arg */
162 register struct cmd_list_element *old;
163 register struct cmd_list_element *c;
164 copied_name = (char *) alloca (strlen (oldname) + 1);
165 strcpy (copied_name, oldname);
166 old = lookup_cmd (&copied_name, *list, "", 1, 1);
170 delete_cmd (name, list);
174 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
175 c->prefixlist = old->prefixlist;
176 c->prefixname = old->prefixname;
177 c->allow_unknown = old->allow_unknown;
178 c->abbrev_flag = abbrev_flag;
179 c->cmd_pointer = old;
183 /* Like add_cmd but adds an element for a command prefix:
184 a name that should be followed by a subcommand to be looked up
185 in another command list. PREFIXLIST should be the address
186 of the variable containing that list. */
188 struct cmd_list_element *
189 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
192 enum command_class class;
193 void (*fun) PARAMS ((char *, int));
195 struct cmd_list_element **prefixlist;
198 struct cmd_list_element **list;
200 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
201 c->prefixlist = prefixlist;
202 c->prefixname = prefixname;
203 c->allow_unknown = allow_unknown;
207 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
209 struct cmd_list_element *
210 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
213 enum command_class class;
214 void (*fun) PARAMS ((char *, int));
216 struct cmd_list_element **prefixlist;
219 struct cmd_list_element **list;
221 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
222 c->prefixlist = prefixlist;
223 c->prefixname = prefixname;
224 c->allow_unknown = allow_unknown;
229 /* This is an empty "cfunc". */
231 not_just_help_class_command (args, from_tty)
237 /* This is an empty "sfunc". */
238 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
241 empty_sfunc (args, from_tty, c)
244 struct cmd_list_element *c;
248 /* Add element named NAME to command list LIST (the list for set
249 or some sublist thereof).
250 CLASS is as in add_cmd.
251 VAR_TYPE is the kind of thing we are setting.
252 VAR is address of the variable being controlled by this command.
253 DOC is the documentation string. */
255 struct cmd_list_element *
256 add_set_cmd (name, class, var_type, var, doc, list)
258 enum command_class class;
262 struct cmd_list_element **list;
264 struct cmd_list_element *c
265 = add_cmd (name, class, NO_FUNCTION, doc, list);
268 c->var_type = var_type;
270 /* This needs to be something besides NO_FUNCTION so that this isn't
271 treated as a help class. */
272 c->function.sfunc = empty_sfunc;
276 /* Add element named NAME to command list LIST (the list for set
277 or some sublist thereof).
278 CLASS is as in add_cmd.
279 ENUMLIST is a list of strings which may follow NAME.
280 VAR is address of the variable which will contain the matching string
282 DOC is the documentation string. */
284 struct cmd_list_element *
285 add_set_enum_cmd (name, class, enumlist, var, doc, list)
287 enum command_class class;
291 struct cmd_list_element **list;
293 struct cmd_list_element *c
294 = add_set_cmd (name, class, var_enum, var, doc, list);
300 /* Where SETCMD has already been added, add the corresponding show
301 command to LIST and return a pointer to the added command (not
302 necessarily the head of LIST). */
303 struct cmd_list_element *
304 add_show_from_set (setcmd, list)
305 struct cmd_list_element *setcmd;
306 struct cmd_list_element **list;
308 struct cmd_list_element *showcmd =
309 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
310 struct cmd_list_element *p;
312 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
313 delete_cmd (showcmd->name, list);
314 showcmd->type = show_cmd;
316 /* Replace "set " at start of docstring with "show ". */
317 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
318 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
319 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
321 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
323 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
325 showcmd->next = *list;
331 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
335 showcmd->next = p->next;
342 /* Remove the command named NAME from the command list. */
345 delete_cmd (name, list)
347 struct cmd_list_element **list;
349 register struct cmd_list_element *c;
350 struct cmd_list_element *p;
352 while (*list && STREQ ((*list)->name, name))
355 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
362 for (c = *list; c->next;)
364 if (STREQ (c->next->name, name))
367 c->next->hookee->hook = 0; /* hooked cmd gets away. */
369 free ((PTR) c->next);
377 /* This command really has to deal with two things:
378 * 1) I want documentation on *this string* (usually called by
379 * "help commandname").
380 * 2) I want documentation on *this list* (usually called by
381 * giving a command that requires subcommands. Also called by saying
384 * I am going to split this into two seperate comamnds, help_cmd and
389 help_cmd (command, stream)
393 struct cmd_list_element *c;
394 extern struct cmd_list_element *cmdlist;
398 help_list (cmdlist, "", all_classes, stream);
402 c = lookup_cmd (&command, cmdlist, "", 0, 0);
407 /* There are three cases here.
408 If c->prefixlist is nonzero, we have a prefix command.
409 Print its documentation, then list its subcommands.
411 If c->function is nonzero, we really have a command.
412 Print its documentation and return.
414 If c->function is zero, we have a class name.
415 Print its documentation (as if it were a command)
416 and then set class to the number of this class
417 so that the commands in the class will be listed. */
419 fputs_filtered (c->doc, stream);
420 fputs_filtered ("\n", stream);
422 if (c->prefixlist == 0 && c->function.cfunc != NULL)
424 fprintf_filtered (stream, "\n");
426 /* If this is a prefix command, print it's subcommands */
428 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
430 /* If this is a class name, print all of the commands in the class */
431 if (c->function.cfunc == NULL)
432 help_list (cmdlist, "", c->class, stream);
435 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
440 * Get a specific kind of help on a command list.
443 * CMDTYPE is the prefix to use in the title string.
444 * CLASS is the class with which to list the nodes of this list (see
445 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
446 * everything, ALL_CLASSES for just classes, and non-negative for only things
447 * in a specific class.
448 * and STREAM is the output stream on which to print things.
449 * If you call this routine with a class >= 0, it recurses.
452 help_list (list, cmdtype, class, stream)
453 struct cmd_list_element *list;
455 enum command_class class;
459 char *cmdtype1, *cmdtype2;
461 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
462 len = strlen (cmdtype);
463 cmdtype1 = (char *) alloca (len + 1);
465 cmdtype2 = (char *) alloca (len + 4);
470 strncpy (cmdtype1 + 1, cmdtype, len - 1);
472 strncpy (cmdtype2, cmdtype, len - 1);
473 strcpy (cmdtype2 + len - 1, " sub");
476 if (class == all_classes)
477 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
479 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
481 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
483 if (class == all_classes)
484 fprintf_filtered (stream, "\n\
485 Type \"help%s\" followed by a class name for a list of commands in that class.",
488 fprintf_filtered (stream, "\n\
489 Type \"help%s\" followed by %scommand name for full documentation.\n\
490 Command name abbreviations are allowed if unambiguous.\n",
494 /* Print only the first line of STR on STREAM. */
496 print_doc_line (stream, str)
500 static char *line_buffer = 0;
501 static int line_size;
507 line_buffer = (char *) xmalloc (line_size);
511 while (*p && *p != '\n' && *p != '.' && *p != ',')
513 if (p - str > line_size - 1)
515 line_size = p - str + 1;
516 free ((PTR) line_buffer);
517 line_buffer = (char *) xmalloc (line_size);
519 strncpy (line_buffer, str, p - str);
520 line_buffer[p - str] = '\0';
521 if (islower (line_buffer[0]))
522 line_buffer[0] = toupper (line_buffer[0]);
523 fputs_filtered (line_buffer, stream);
527 * Implement a help command on command list LIST.
528 * RECURSE should be non-zero if this should be done recursively on
529 * all sublists of LIST.
530 * PREFIX is the prefix to print before each command name.
531 * STREAM is the stream upon which the output should be written.
533 * A non-negative class number to list only commands in that
535 * ALL_COMMANDS to list all commands in list.
536 * ALL_CLASSES to list all classes in list.
538 * Note that RECURSE will be active on *all* sublists, not just the
539 * ones selected by the criteria above (ie. the selection mechanism
540 * is at the low level, not the high-level).
543 help_cmd_list (list, class, prefix, recurse, stream)
544 struct cmd_list_element *list;
545 enum command_class class;
550 register struct cmd_list_element *c;
552 for (c = list; c; c = c->next)
554 if (c->abbrev_flag == 0 &&
555 (class == all_commands
556 || (class == all_classes && c->function.cfunc == NULL)
557 || (class == c->class && c->function.cfunc != NULL)))
559 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
560 print_doc_line (stream, c->doc);
561 fputs_filtered ("\n", stream);
564 && c->prefixlist != 0
565 && c->abbrev_flag == 0)
566 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
571 /* Search the input clist for 'command'. Return the command if
572 found (or NULL if not), and return the number of commands
575 static struct cmd_list_element *
576 find_cmd (command, len, clist, ignore_help_classes, nfound)
579 struct cmd_list_element *clist;
580 int ignore_help_classes;
583 struct cmd_list_element *found, *c;
585 found = (struct cmd_list_element *) NULL;
587 for (c = clist; c; c = c->next)
588 if (!strncmp (command, c->name, len)
589 && (!ignore_help_classes || c->function.cfunc))
593 if (c->name[len] == '\0')
602 /* This routine takes a line of TEXT and a CLIST in which to start the
603 lookup. When it returns it will have incremented the text pointer past
604 the section of text it matched, set *RESULT_LIST to point to the list in
605 which the last word was matched, and will return a pointer to the cmd
606 list element which the text matches. It will return NULL if no match at
607 all was possible. It will return -1 (cast appropriately, ick) if ambigous
608 matches are possible; in this case *RESULT_LIST will be set to point to
609 the list in which there are ambiguous choices (and *TEXT will be set to
610 the ambiguous text string).
612 If the located command was an abbreviation, this routine returns the base
613 command of the abbreviation.
615 It does no error reporting whatsoever; control will always return
616 to the superior routine.
618 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
619 at the prefix_command (ie. the best match) *or* (special case) will be NULL
620 if no prefix command was ever found. For example, in the case of "info a",
621 "info" matches without ambiguity, but "a" could be "args" or "address", so
622 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
623 RESULT_LIST should not be interpeted as a pointer to the beginning of a
624 list; it simply points to a specific command. In the case of an ambiguous
625 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
626 "info t" can be "info types" or "info target"; upon return *TEXT has been
627 advanced past "info ").
629 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
630 affect the operation).
632 This routine does *not* modify the text pointed to by TEXT.
634 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
635 are actually help classes rather than commands (i.e. the function field of
636 the struct cmd_list_element is NULL). */
638 struct cmd_list_element *
639 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
641 struct cmd_list_element *clist, **result_list;
642 int ignore_help_classes;
645 int len, tmp, nfound;
646 struct cmd_list_element *found, *c;
648 while (**text == ' ' || **text == '\t')
651 /* Treating underscores as part of command words is important
652 so that "set args_foo()" doesn't get interpreted as
653 "set args _foo()". */
655 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
657 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
658 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
662 /* If nothing but whitespace, return 0. */
668 /* *text and p now bracket the first command word to lookup (and
669 it's length is len). We copy this into a local temporary */
672 command = (char *) alloca (len + 1);
673 for (tmp = 0; tmp < len; tmp++)
675 char x = (*text)[tmp];
683 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
686 ** We didn't find the command in the entered case, so lower case it
689 if (!found || nfound == 0)
691 for (tmp = 0; tmp < len; tmp++)
693 char x = command[tmp];
694 command[tmp] = isupper (x) ? tolower (x) : x;
696 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
699 /* If nothing matches, we have a simple failure. */
705 if (result_list != NULL)
706 /* Will be modified in calling routine
707 if we know what the prefix command is. */
709 return (struct cmd_list_element *) -1; /* Ambiguous. */
712 /* We've matched something on this list. Move text pointer forward. */
716 /* If this was an abbreviation, use the base command instead. */
718 if (found->cmd_pointer)
719 found = found->cmd_pointer;
721 /* If we found a prefix command, keep looking. */
723 if (found->prefixlist)
725 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
726 ignore_help_classes);
729 /* Didn't find anything; this is as far as we got. */
730 if (result_list != NULL)
731 *result_list = clist;
734 else if (c == (struct cmd_list_element *) -1)
736 /* We've gotten this far properly, but the next step
737 is ambiguous. We need to set the result list to the best
738 we've found (if an inferior hasn't already set it). */
739 if (result_list != NULL)
741 /* This used to say *result_list = *found->prefixlist
742 If that was correct, need to modify the documentation
743 at the top of this function to clarify what is supposed
745 *result_list = found;
756 if (result_list != NULL)
757 *result_list = clist;
762 /* All this hair to move the space to the front of cmdtype */
765 undef_cmd_error (cmdtype, q)
768 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
772 strlen (cmdtype) - 1,
776 /* Look up the contents of *LINE as a command in the command list LIST.
777 LIST is a chain of struct cmd_list_element's.
778 If it is found, return the struct cmd_list_element for that command
779 and update *LINE to point after the command name, at the first argument.
780 If not found, call error if ALLOW_UNKNOWN is zero
781 otherwise (or if error returns) return zero.
782 Call error if specified command is ambiguous,
783 unless ALLOW_UNKNOWN is negative.
784 CMDTYPE precedes the word "command" in the error message.
786 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
787 elements which are actually help classes rather than commands (i.e.
788 the function field of the struct cmd_list_element is 0). */
790 struct cmd_list_element *
791 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
793 struct cmd_list_element *list;
796 int ignore_help_classes;
798 struct cmd_list_element *last_list = 0;
799 struct cmd_list_element *c =
800 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
802 /* This is wrong for complete_command. */
803 char *ptr = (*line) + strlen (*line) - 1;
805 /* Clear off trailing whitespace. */
806 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
816 error ("Lack of needed %scommand", cmdtype);
821 while (isalnum (*p) || *p == '-')
824 q = (char *) alloca (p - *line + 1);
825 strncpy (q, *line, p - *line);
827 undef_cmd_error (cmdtype, q);
833 else if (c == (struct cmd_list_element *) -1)
835 /* Ambigous. Local values should be off prefixlist or called
837 int local_allow_unknown = (last_list ? last_list->allow_unknown :
839 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
840 struct cmd_list_element *local_list =
841 (last_list ? *(last_list->prefixlist) : list);
843 if (local_allow_unknown < 0)
846 return last_list; /* Found something. */
848 return 0; /* Found nothing. */
852 /* Report as error. */
857 ((*line)[amb_len] && (*line)[amb_len] != ' '
858 && (*line)[amb_len] != '\t');
863 for (c = local_list; c; c = c->next)
864 if (!strncmp (*line, c->name, amb_len))
866 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
869 strcat (ambbuf, ", ");
870 strcat (ambbuf, c->name);
874 strcat (ambbuf, "..");
878 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
885 /* We've got something. It may still not be what the caller
886 wants (if this command *needs* a subcommand). */
887 while (**line == ' ' || **line == '\t')
890 if (c->prefixlist && **line && !c->allow_unknown)
891 undef_cmd_error (c->prefixname, *line);
893 /* Seems to be what he wants. Return it. */
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 struct cmd_list_element *
911 lookup_cmd (line, list, cmdtype, allow_unknown)
913 struct cmd_list_element *list;
918 register struct cmd_list_element *c, *found;
924 /* Skip leading whitespace. */
926 while (**line == ' ' || **line == '\t')
929 /* Clear out trailing whitespace. */
931 p = *line + strlen (*line);
932 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
936 /* Find end of command name. */
939 while (*p == '-' || isalnum (*p))
942 /* Look up the command name.
943 If exact match, keep that.
944 Otherwise, take command abbreviated, if unique. Note that (in my
945 opinion) a null string does *not* indicate ambiguity; simply the
946 end of the argument. */
951 error ("Lack of needed %scommand", cmdtype);
955 /* Copy over to a local buffer, converting to lowercase on the way.
956 This is in case the command being parsed is a subcommand which
957 doesn't match anything, and that's ok. We want the original
958 untouched for the routine of the original command. */
960 processed_cmd = (char *) alloca (p - *line + 1);
961 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
963 char x = (*line)[cmd_len];
965 processed_cmd[cmd_len] = tolower (x);
967 processed_cmd[cmd_len] = x;
969 processed_cmd[cmd_len] = '\0';
971 /* Check all possibilities in the current command list. */
974 for (c = list; c; c = c->next)
976 if (!strncmp (processed_cmd, c->name, cmd_len))
980 if (c->name[cmd_len] == 0)
988 /* Report error for undefined command name. */
992 if (nfound > 1 && allow_unknown >= 0)
995 for (c = list; c; c = c->next)
996 if (!strncmp (processed_cmd, c->name, cmd_len))
998 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1000 if (strlen (ambbuf))
1001 strcat (ambbuf, ", ");
1002 strcat (ambbuf, c->name);
1006 strcat (ambbuf, "..");
1010 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1011 processed_cmd, ambbuf);
1013 else if (!allow_unknown)
1014 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1018 /* Skip whitespace before the argument. */
1020 while (*p == ' ' || *p == '\t')
1024 if (found->prefixlist && *p)
1026 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1027 found->allow_unknown);
1036 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1038 /* Return a vector of char pointers which point to the different
1039 possible completions in LIST of TEXT.
1041 WORD points in the same buffer as TEXT, and completions should be
1042 returned relative to this position. For example, suppose TEXT is "foo"
1043 and we want to complete to "foobar". If WORD is "oo", return
1044 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1047 complete_on_cmdlist (list, text, word)
1048 struct cmd_list_element *list;
1052 struct cmd_list_element *ptr;
1054 int sizeof_matchlist;
1056 int textlen = strlen (text);
1058 sizeof_matchlist = 10;
1059 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1062 for (ptr = list; ptr; ptr = ptr->next)
1063 if (!strncmp (ptr->name, text, textlen)
1064 && !ptr->abbrev_flag
1065 && (ptr->function.cfunc
1066 || ptr->prefixlist))
1068 if (matches == sizeof_matchlist)
1070 sizeof_matchlist *= 2;
1071 matchlist = (char **) xrealloc ((char *) matchlist,
1073 * sizeof (char *)));
1076 matchlist[matches] = (char *)
1077 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1079 strcpy (matchlist[matches], ptr->name);
1080 else if (word > text)
1082 /* Return some portion of ptr->name. */
1083 strcpy (matchlist[matches], ptr->name + (word - text));
1087 /* Return some of text plus ptr->name. */
1088 strncpy (matchlist[matches], word, text - word);
1089 matchlist[matches][text - word] = '\0';
1090 strcat (matchlist[matches], ptr->name);
1097 free ((PTR) matchlist);
1102 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1103 * sizeof (char *)));
1104 matchlist[matches] = (char *) 0;
1110 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1112 /* Return a vector of char pointers which point to the different
1113 possible completions in CMD of TEXT.
1115 WORD points in the same buffer as TEXT, and completions should be
1116 returned relative to this position. For example, suppose TEXT is "foo"
1117 and we want to complete to "foobar". If WORD is "oo", return
1118 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1121 complete_on_enum (enumlist, text, word)
1127 int sizeof_matchlist;
1129 int textlen = strlen (text);
1133 sizeof_matchlist = 10;
1134 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1137 for (i = 0; (name = enumlist[i]) != NULL; i++)
1138 if (strncmp (name, text, textlen) == 0)
1140 if (matches == sizeof_matchlist)
1142 sizeof_matchlist *= 2;
1143 matchlist = (char **) xrealloc ((char *) matchlist,
1145 * sizeof (char *)));
1148 matchlist[matches] = (char *)
1149 xmalloc (strlen (word) + strlen (name) + 1);
1151 strcpy (matchlist[matches], name);
1152 else if (word > text)
1154 /* Return some portion of name. */
1155 strcpy (matchlist[matches], name + (word - text));
1159 /* Return some of text plus name. */
1160 strncpy (matchlist[matches], word, text - word);
1161 matchlist[matches][text - word] = '\0';
1162 strcat (matchlist[matches], name);
1169 free ((PTR) matchlist);
1174 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1175 * sizeof (char *)));
1176 matchlist[matches] = (char *) 0;
1183 parse_binary_operation (arg)
1191 length = strlen (arg);
1193 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1196 if (!strncmp (arg, "on", length)
1197 || !strncmp (arg, "1", length)
1198 || !strncmp (arg, "yes", length))
1200 else if (!strncmp (arg, "off", length)
1201 || !strncmp (arg, "0", length)
1202 || !strncmp (arg, "no", length))
1206 error ("\"on\" or \"off\" expected.");
1211 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1212 of the argument, and FROM_TTY is nonzero if this command is being entered
1213 directly by the user (i.e. these are just like any other
1214 command). C is the command list element for the command. */
1216 do_setshow_command (arg, from_tty, c)
1219 struct cmd_list_element *c;
1221 if (c->type == set_cmd)
1223 switch (c->var_type)
1234 new = (char *) xmalloc (strlen (arg) + 2);
1237 while ((ch = *p++) != '\000')
1241 /* \ at end of argument is used after spaces
1242 so they won't be lost. */
1243 /* This is obsolete now that we no longer strip
1244 trailing whitespace and actually, the backslash
1245 didn't get here in my test, readline or
1246 something did something funky with a backslash
1247 right before a newline. */
1250 ch = parse_escape (&p);
1252 break; /* C loses */
1260 if (*(p - 1) != '\\')
1264 new = (char *) xrealloc (new, q - new);
1265 if (*(char **) c->var != NULL)
1266 free (*(char **) c->var);
1267 *(char **) c->var = new;
1270 case var_string_noescape:
1273 if (*(char **) c->var != NULL)
1274 free (*(char **) c->var);
1275 *(char **) c->var = savestring (arg, strlen (arg));
1279 error_no_arg ("filename to set it to.");
1280 if (*(char **) c->var != NULL)
1281 free (*(char **) c->var);
1282 *(char **) c->var = tilde_expand (arg);
1285 *(int *) c->var = parse_binary_operation (arg);
1289 error_no_arg ("integer to set it to.");
1290 *(unsigned int *) c->var = parse_and_eval_address (arg);
1291 if (*(unsigned int *) c->var == 0)
1292 *(unsigned int *) c->var = UINT_MAX;
1298 error_no_arg ("integer to set it to.");
1299 val = parse_and_eval_address (arg);
1301 *(int *) c->var = INT_MAX;
1302 else if (val >= INT_MAX)
1303 error ("integer %u out of range", val);
1305 *(int *) c->var = val;
1310 error_no_arg ("integer to set it to.");
1311 *(int *) c->var = parse_and_eval_address (arg);
1321 /* if no argument was supplied, print an informative error message */
1325 strcpy (msg, "Requires an argument. Valid arguments are ");
1326 for (i = 0; c->enums[i]; i++)
1330 strcat (msg, c->enums[i]);
1336 p = strchr (arg, ' ');
1344 for (i = 0; c->enums[i]; i++)
1345 if (strncmp (arg, c->enums[i], len) == 0)
1347 match = c->enums[i];
1352 error ("Undefined item: \"%s\".", arg);
1355 error ("Ambiguous item \"%s\".", arg);
1357 *(char **) c->var = match;
1361 error ("gdb internal error: bad var_type in do_setshow_command");
1364 else if (c->type == show_cmd)
1366 /* Print doc minus "show" at start. */
1367 print_doc_line (gdb_stdout, c->doc + 5);
1369 fputs_filtered (" is ", gdb_stdout);
1371 switch (c->var_type)
1375 fputs_filtered ("\"", gdb_stdout);
1376 if (*(unsigned char **) c->var)
1377 fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
1378 fputs_filtered ("\"", gdb_stdout);
1381 case var_string_noescape:
1384 fputs_filtered ("\"", gdb_stdout);
1385 if (*(char **) c->var)
1386 fputs_filtered (*(char **) c->var, gdb_stdout);
1387 fputs_filtered ("\"", gdb_stdout);
1390 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1393 if (*(unsigned int *) c->var == UINT_MAX)
1395 fputs_filtered ("unlimited", gdb_stdout);
1398 /* else fall through */
1400 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1403 if (*(int *) c->var == INT_MAX)
1405 fputs_filtered ("unlimited", gdb_stdout);
1408 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1412 error ("gdb internal error: bad var_type in do_setshow_command");
1414 fputs_filtered (".\n", gdb_stdout);
1417 error ("gdb internal error: bad cmd_type in do_setshow_command");
1418 (*c->function.sfunc) (NULL, from_tty, c);
1419 if (c->type == set_cmd && set_hook)
1423 /* Show all the settings in a list of show commands. */
1426 cmd_show_list (list, from_tty, prefix)
1427 struct cmd_list_element *list;
1431 for (; list != NULL; list = list->next)
1433 /* If we find a prefix, run its list, prefixing our output by its
1434 prefix (with "show " skipped). */
1435 if (list->prefixlist && !list->abbrev_flag)
1436 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1437 if (list->type == show_cmd)
1439 fputs_filtered (prefix, gdb_stdout);
1440 fputs_filtered (list->name, gdb_stdout);
1441 fputs_filtered (": ", gdb_stdout);
1442 do_setshow_command ((char *) NULL, from_tty, list);
1449 shell_escape (arg, from_tty)
1454 /* If ARG is NULL, they want an inferior shell, but `system' just
1455 reports if the shell is available when passed a NULL arg. */
1456 int rc = system (arg ? arg : "");
1459 arg = "inferior shell";
1463 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1464 safe_strerror (errno));
1465 gdb_flush (gdb_stderr);
1469 fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1470 gdb_flush (gdb_stderr);
1473 /* Make sure to return to the directory GDB thinks it is, in case the
1474 shell command we just ran changed it. */
1475 chdir (current_directory);
1477 #else /* Can fork. */
1478 int rc, status, pid;
1479 char *p, *user_shell;
1481 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1482 user_shell = "/bin/sh";
1484 /* Get the name of the shell for arg0 */
1485 if ((p = strrchr (user_shell, '/')) == NULL)
1488 p++; /* Get past '/' */
1490 if ((pid = fork ()) == 0)
1493 execl (user_shell, p, 0);
1495 execl (user_shell, p, "-c", arg, 0);
1497 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1498 safe_strerror (errno));
1499 gdb_flush (gdb_stderr);
1504 while ((rc = wait (&status)) != pid && rc != -1)
1507 error ("Fork failed");
1508 #endif /* Can fork. */
1512 make_command (arg, from_tty)
1522 p = xmalloc (sizeof ("make ") + strlen (arg));
1523 strcpy (p, "make ");
1524 strcpy (p + sizeof ("make ") - 1, arg);
1527 shell_escape (p, from_tty);
1531 show_user_1 (c, stream)
1532 struct cmd_list_element *c;
1535 register struct command_line *cmdlines;
1537 cmdlines = c->user_commands;
1540 fputs_filtered ("User command ", stream);
1541 fputs_filtered (c->name, stream);
1542 fputs_filtered (":\n", stream);
1546 print_command_line (cmdlines, 4, stream);
1547 cmdlines = cmdlines->next;
1549 fputs_filtered ("\n", stream);
1554 show_user (args, from_tty)
1558 struct cmd_list_element *c;
1559 extern struct cmd_list_element *cmdlist;
1563 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1564 if (c->class != class_user)
1565 error ("Not a user command.");
1566 show_user_1 (c, gdb_stdout);
1570 for (c = cmdlist; c; c = c->next)
1572 if (c->class == class_user)
1573 show_user_1 (c, gdb_stdout);
1579 _initialize_command ()
1581 add_com ("shell", class_support, shell_escape,
1582 "Execute the rest of the line as a shell command. \n\
1583 With no arguments, run an inferior shell.");
1586 add_com_alias ("!", "shell", class_support, 0);
1588 add_com ("make", class_support, make_command,
1589 "Run the ``make'' program using the rest of the line as arguments.");
1590 add_cmd ("user", no_class, show_user,
1591 "Show definitions of user defined commands.\n\
1592 Argument is the name of the user defined command.\n\
1593 With no argument, show definitions of all user defined commands.", &showlist);