1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* Prototypes for local functions */
28 undef_cmd_error PARAMS ((char *, char *));
31 show_user PARAMS ((char *, int));
34 show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
37 make_command PARAMS ((char *, int));
40 shell_escape PARAMS ((char *, int));
43 parse_binary_operation PARAMS ((char *));
46 print_doc_line PARAMS ((GDB_FILE *, char *));
48 /* Add element named NAME.
49 CLASS is the top level category into which commands are broken down
51 FUN should be the function to execute the command;
52 it will get a character string as argument, with leading
53 and trailing blanks already eliminated.
55 DOC is a documentation string for the command.
56 Its first line should be a complete sentence.
57 It should start with ? for a command that is an abbreviation
58 or with * for a command that most users don't need to know about.
60 Add this command to command list *LIST. */
62 struct cmd_list_element *
63 add_cmd (name, class, fun, doc, list)
65 enum command_class class;
66 void (*fun) PARAMS ((char *, int));
68 struct cmd_list_element **list;
70 register struct cmd_list_element *c
71 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
73 delete_cmd (name, list);
77 c->function.cfunc = fun;
80 c->prefixname = (char *)NULL;
86 c->type = not_set_cmd;
87 c->completer = make_symbol_completion_list;
89 c->var_type = var_boolean;
95 /* Same as above, except that the abbrev_flag is set. */
97 #if 0 /* Currently unused */
99 struct cmd_list_element *
100 add_abbrev_cmd (name, class, fun, doc, list)
102 enum command_class class;
103 void (*fun) PARAMS ((char *, int));
105 struct cmd_list_element **list;
107 register struct cmd_list_element *c
108 = add_cmd (name, class, fun, doc, list);
116 struct cmd_list_element *
117 add_alias_cmd (name, oldname, class, abbrev_flag, list)
120 enum command_class class;
122 struct cmd_list_element **list;
124 /* Must do this since lookup_cmd tries to side-effect its first arg */
126 register struct cmd_list_element *old;
127 register struct cmd_list_element *c;
128 copied_name = (char *) alloca (strlen (oldname) + 1);
129 strcpy (copied_name, oldname);
130 old = lookup_cmd (&copied_name, *list, "", 1, 1);
134 delete_cmd (name, list);
138 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
139 c->prefixlist = old->prefixlist;
140 c->prefixname = old->prefixname;
141 c->allow_unknown = old->allow_unknown;
142 c->abbrev_flag = abbrev_flag;
143 c->cmd_pointer = old;
147 /* Like add_cmd but adds an element for a command prefix:
148 a name that should be followed by a subcommand to be looked up
149 in another command list. PREFIXLIST should be the address
150 of the variable containing that list. */
152 struct cmd_list_element *
153 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
156 enum command_class class;
157 void (*fun) PARAMS ((char *, int));
159 struct cmd_list_element **prefixlist;
162 struct cmd_list_element **list;
164 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
165 c->prefixlist = prefixlist;
166 c->prefixname = prefixname;
167 c->allow_unknown = allow_unknown;
171 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
173 struct cmd_list_element *
174 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
177 enum command_class class;
178 void (*fun) PARAMS ((char *, int));
180 struct cmd_list_element **prefixlist;
183 struct cmd_list_element **list;
185 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
186 c->prefixlist = prefixlist;
187 c->prefixname = prefixname;
188 c->allow_unknown = allow_unknown;
193 /* This is an empty "cfunc". */
195 not_just_help_class_command (args, from_tty)
201 /* This is an empty "sfunc". */
202 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
205 empty_sfunc (args, from_tty, c)
208 struct cmd_list_element *c;
212 /* Add element named NAME to command list LIST (the list for set
213 or some sublist thereof).
214 CLASS is as in add_cmd.
215 VAR_TYPE is the kind of thing we are setting.
216 VAR is address of the variable being controlled by this command.
217 DOC is the documentation string. */
219 struct cmd_list_element *
220 add_set_cmd (name, class, var_type, var, doc, list)
222 enum command_class class;
226 struct cmd_list_element **list;
228 struct cmd_list_element *c
229 = add_cmd (name, class, NO_FUNCTION, doc, list);
232 c->var_type = var_type;
234 /* This needs to be something besides NO_FUNCTION so that this isn't
235 treated as a help class. */
236 c->function.sfunc = empty_sfunc;
240 /* Where SETCMD has already been added, add the corresponding show
241 command to LIST and return a pointer to it. */
242 struct cmd_list_element *
243 add_show_from_set (setcmd, list)
244 struct cmd_list_element *setcmd;
245 struct cmd_list_element **list;
247 struct cmd_list_element *showcmd =
248 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
250 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
251 delete_cmd (showcmd->name, list);
252 showcmd->type = show_cmd;
254 /* Replace "set " at start of docstring with "show ". */
255 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
256 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
257 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
259 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
261 showcmd->next = *list;
266 /* Remove the command named NAME from the command list. */
269 delete_cmd (name, list)
271 struct cmd_list_element **list;
273 register struct cmd_list_element *c;
274 struct cmd_list_element *p;
276 while (*list && STREQ ((*list)->name, name))
279 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
286 for (c = *list; c->next;)
288 if (STREQ (c->next->name, name))
291 c->next->hookee->hook = 0; /* hooked cmd gets away. */
301 /* This command really has to deal with two things:
302 * 1) I want documentation on *this string* (usually called by
303 * "help commandname").
304 * 2) I want documentation on *this list* (usually called by
305 * giving a command that requires subcommands. Also called by saying
308 * I am going to split this into two seperate comamnds, help_cmd and
313 help_cmd (command, stream)
317 struct cmd_list_element *c;
318 extern struct cmd_list_element *cmdlist;
322 help_list (cmdlist, "", all_classes, stream);
326 c = lookup_cmd (&command, cmdlist, "", 0, 0);
331 /* There are three cases here.
332 If c->prefixlist is nonzero, we have a prefix command.
333 Print its documentation, then list its subcommands.
335 If c->function is nonzero, we really have a command.
336 Print its documentation and return.
338 If c->function is zero, we have a class name.
339 Print its documentation (as if it were a command)
340 and then set class to the number of this class
341 so that the commands in the class will be listed. */
343 fputs_filtered (c->doc, stream);
344 fputs_filtered ("\n", stream);
346 if (c->prefixlist == 0 && c->function.cfunc != NULL)
348 fprintf_filtered (stream, "\n");
350 /* If this is a prefix command, print it's subcommands */
352 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
354 /* If this is a class name, print all of the commands in the class */
355 if (c->function.cfunc == NULL)
356 help_list (cmdlist, "", c->class, stream);
359 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
364 * Get a specific kind of help on a command list.
367 * CMDTYPE is the prefix to use in the title string.
368 * CLASS is the class with which to list the nodes of this list (see
369 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
370 * everything, ALL_CLASSES for just classes, and non-negative for only things
371 * in a specific class.
372 * and STREAM is the output stream on which to print things.
373 * If you call this routine with a class >= 0, it recurses.
376 help_list (list, cmdtype, class, stream)
377 struct cmd_list_element *list;
379 enum command_class class;
383 char *cmdtype1, *cmdtype2;
385 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
386 len = strlen (cmdtype);
387 cmdtype1 = (char *) alloca (len + 1);
389 cmdtype2 = (char *) alloca (len + 4);
394 strncpy (cmdtype1 + 1, cmdtype, len - 1);
396 strncpy (cmdtype2, cmdtype, len - 1);
397 strcpy (cmdtype2 + len - 1, " sub");
400 if (class == all_classes)
401 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
403 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
405 help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
407 if (class == all_classes)
408 fprintf_filtered (stream, "\n\
409 Type \"help%s\" followed by a class name for a list of commands in that class.",
412 fprintf_filtered (stream, "\n\
413 Type \"help%s\" followed by %scommand name for full documentation.\n\
414 Command name abbreviations are allowed if unambiguous.\n",
418 /* Print only the first line of STR on STREAM. */
420 print_doc_line (stream, str)
424 static char *line_buffer = 0;
425 static int line_size;
431 line_buffer = (char *) xmalloc (line_size);
435 while (*p && *p != '\n' && *p != '.' && *p != ',')
437 if (p - str > line_size - 1)
439 line_size = p - str + 1;
440 free ((PTR)line_buffer);
441 line_buffer = (char *) xmalloc (line_size);
443 strncpy (line_buffer, str, p - str);
444 line_buffer[p - str] = '\0';
445 if (islower (line_buffer[0]))
446 line_buffer[0] = toupper (line_buffer[0]);
447 fputs_filtered (line_buffer, stream);
451 * Implement a help command on command list LIST.
452 * RECURSE should be non-zero if this should be done recursively on
453 * all sublists of LIST.
454 * PREFIX is the prefix to print before each command name.
455 * STREAM is the stream upon which the output should be written.
457 * A non-negative class number to list only commands in that
459 * ALL_COMMANDS to list all commands in list.
460 * ALL_CLASSES to list all classes in list.
462 * Note that RECURSE will be active on *all* sublists, not just the
463 * ones selected by the criteria above (ie. the selection mechanism
464 * is at the low level, not the high-level).
467 help_cmd_list (list, class, prefix, recurse, stream)
468 struct cmd_list_element *list;
469 enum command_class class;
474 register struct cmd_list_element *c;
476 for (c = list; c; c = c->next)
478 if (c->abbrev_flag == 0 &&
479 (class == all_commands
480 || (class == all_classes && c->function.cfunc == NULL)
481 || (class == c->class && c->function.cfunc != NULL)))
483 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
484 print_doc_line (stream, c->doc);
485 fputs_filtered ("\n", stream);
488 && c->prefixlist != 0
489 && c->abbrev_flag == 0)
490 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
494 /* This routine takes a line of TEXT and a CLIST in which to start the
495 lookup. When it returns it will have incremented the text pointer past
496 the section of text it matched, set *RESULT_LIST to point to the list in
497 which the last word was matched, and will return a pointer to the cmd
498 list element which the text matches. It will return NULL if no match at
499 all was possible. It will return -1 (cast appropriately, ick) if ambigous
500 matches are possible; in this case *RESULT_LIST will be set to point to
501 the list in which there are ambiguous choices (and *TEXT will be set to
502 the ambiguous text string).
504 If the located command was an abbreviation, this routine returns the base
505 command of the abbreviation.
507 It does no error reporting whatsoever; control will always return
508 to the superior routine.
510 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
511 at the prefix_command (ie. the best match) *or* (special case) will be NULL
512 if no prefix command was ever found. For example, in the case of "info a",
513 "info" matches without ambiguity, but "a" could be "args" or "address", so
514 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
515 RESULT_LIST should not be interpeted as a pointer to the beginning of a
516 list; it simply points to a specific command. In the case of an ambiguous
517 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
518 "info t" can be "info types" or "info target"; upon return *TEXT has been
519 advanced past "info ").
521 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
522 affect the operation).
524 This routine does *not* modify the text pointed to by TEXT.
526 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
527 are actually help classes rather than commands (i.e. the function field of
528 the struct cmd_list_element is NULL). */
530 struct cmd_list_element *
531 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
533 struct cmd_list_element *clist, **result_list;
534 int ignore_help_classes;
537 int len, tmp, nfound;
538 struct cmd_list_element *found, *c;
540 while (**text == ' ' || **text == '\t')
543 /* Treating underscores as part of command words is important
544 so that "set args_foo()" doesn't get interpreted as
545 "set args _foo()". */
547 *p && (isalnum(*p) || *p == '-' || *p == '_');
551 /* If nothing but whitespace, return 0. */
557 /* *text and p now bracket the first command word to lookup (and
558 it's length is len). We copy this into a local temporary,
559 converting to lower case as we go. */
561 command = (char *) alloca (len + 1);
562 for (tmp = 0; tmp < len; tmp++)
564 char x = (*text)[tmp];
565 command[tmp] = isupper(x) ? tolower(x) : x;
572 for (c = clist; c; c = c->next)
573 if (!strncmp (command, c->name, len)
574 && (!ignore_help_classes || c->function.cfunc))
578 if (c->name[len] == '\0')
585 /* If nothing matches, we have a simple failure. */
591 if (result_list != NULL)
592 /* Will be modified in calling routine
593 if we know what the prefix command is. */
595 return (struct cmd_list_element *) -1; /* Ambiguous. */
598 /* We've matched something on this list. Move text pointer forward. */
602 /* If this was an abbreviation, use the base command instead. */
604 if (found->cmd_pointer)
605 found = found->cmd_pointer;
607 /* If we found a prefix command, keep looking. */
609 if (found->prefixlist)
611 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
612 ignore_help_classes);
615 /* Didn't find anything; this is as far as we got. */
616 if (result_list != NULL)
617 *result_list = clist;
620 else if (c == (struct cmd_list_element *) -1)
622 /* We've gotten this far properley, but the next step
623 is ambiguous. We need to set the result list to the best
624 we've found (if an inferior hasn't already set it). */
625 if (result_list != NULL)
627 /* This used to say *result_list = *found->prefixlist
628 If that was correct, need to modify the documentation
629 at the top of this function to clarify what is supposed
631 *result_list = found;
642 if (result_list != NULL)
643 *result_list = clist;
648 /* All this hair to move the space to the front of cmdtype */
651 undef_cmd_error (cmdtype, q)
654 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
662 /* Look up the contents of *LINE as a command in the command list LIST.
663 LIST is a chain of struct cmd_list_element's.
664 If it is found, return the struct cmd_list_element for that command
665 and update *LINE to point after the command name, at the first argument.
666 If not found, call error if ALLOW_UNKNOWN is zero
667 otherwise (or if error returns) return zero.
668 Call error if specified command is ambiguous,
669 unless ALLOW_UNKNOWN is negative.
670 CMDTYPE precedes the word "command" in the error message.
672 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
673 elements which are actually help classes rather than commands (i.e.
674 the function field of the struct cmd_list_element is 0). */
676 struct cmd_list_element *
677 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
679 struct cmd_list_element *list;
682 int ignore_help_classes;
684 struct cmd_list_element *last_list = 0;
685 struct cmd_list_element *c =
686 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
688 /* This is wrong for complete_command. */
689 char *ptr = (*line) + strlen (*line) - 1;
691 /* Clear off trailing whitespace. */
692 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
702 error ("Lack of needed %scommand", cmdtype);
707 while (isalnum(*p) || *p == '-')
710 q = (char *) alloca (p - *line + 1);
711 strncpy (q, *line, p - *line);
713 undef_cmd_error (cmdtype, q);
719 else if (c == (struct cmd_list_element *) -1)
721 /* Ambigous. Local values should be off prefixlist or called
723 int local_allow_unknown = (last_list ? last_list->allow_unknown :
725 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
726 struct cmd_list_element *local_list =
727 (last_list ? *(last_list->prefixlist) : list);
729 if (local_allow_unknown < 0)
732 return last_list; /* Found something. */
734 return 0; /* Found nothing. */
738 /* Report as error. */
743 ((*line)[amb_len] && (*line)[amb_len] != ' '
744 && (*line)[amb_len] != '\t');
749 for (c = local_list; c; c = c->next)
750 if (!strncmp (*line, c->name, amb_len))
752 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
755 strcat (ambbuf, ", ");
756 strcat (ambbuf, c->name);
760 strcat (ambbuf, "..");
764 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
771 /* We've got something. It may still not be what the caller
772 wants (if this command *needs* a subcommand). */
773 while (**line == ' ' || **line == '\t')
776 if (c->prefixlist && **line && !c->allow_unknown)
777 undef_cmd_error (c->prefixname, *line);
779 /* Seems to be what he wants. Return it. */
786 /* Look up the contents of *LINE as a command in the command list LIST.
787 LIST is a chain of struct cmd_list_element's.
788 If it is found, return the struct cmd_list_element for that command
789 and update *LINE to point after the command name, at the first argument.
790 If not found, call error if ALLOW_UNKNOWN is zero
791 otherwise (or if error returns) return zero.
792 Call error if specified command is ambiguous,
793 unless ALLOW_UNKNOWN is negative.
794 CMDTYPE precedes the word "command" in the error message. */
796 struct cmd_list_element *
797 lookup_cmd (line, list, cmdtype, allow_unknown)
799 struct cmd_list_element *list;
804 register struct cmd_list_element *c, *found;
810 /* Skip leading whitespace. */
812 while (**line == ' ' || **line == '\t')
815 /* Clear out trailing whitespace. */
817 p = *line + strlen (*line);
818 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
822 /* Find end of command name. */
825 while (*p == '-' || isalnum(*p))
828 /* Look up the command name.
829 If exact match, keep that.
830 Otherwise, take command abbreviated, if unique. Note that (in my
831 opinion) a null string does *not* indicate ambiguity; simply the
832 end of the argument. */
837 error ("Lack of needed %scommand", cmdtype);
841 /* Copy over to a local buffer, converting to lowercase on the way.
842 This is in case the command being parsed is a subcommand which
843 doesn't match anything, and that's ok. We want the original
844 untouched for the routine of the original command. */
846 processed_cmd = (char *) alloca (p - *line + 1);
847 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
849 char x = (*line)[cmd_len];
851 processed_cmd[cmd_len] = tolower(x);
853 processed_cmd[cmd_len] = x;
855 processed_cmd[cmd_len] = '\0';
857 /* Check all possibilities in the current command list. */
860 for (c = list; c; c = c->next)
862 if (!strncmp (processed_cmd, c->name, cmd_len))
866 if (c->name[cmd_len] == 0)
874 /* Report error for undefined command name. */
878 if (nfound > 1 && allow_unknown >= 0)
881 for (c = list; c; c = c->next)
882 if (!strncmp (processed_cmd, c->name, cmd_len))
884 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
887 strcat (ambbuf, ", ");
888 strcat (ambbuf, c->name);
892 strcat (ambbuf, "..");
896 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
897 processed_cmd, ambbuf);
899 else if (!allow_unknown)
900 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
904 /* Skip whitespace before the argument. */
906 while (*p == ' ' || *p == '\t') p++;
909 if (found->prefixlist && *p)
911 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
912 found->allow_unknown);
921 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
923 /* Return a vector of char pointers which point to the different
924 possible completions in LIST of TEXT.
926 WORD points in the same buffer as TEXT, and completions should be
927 returned relative to this position. For example, suppose TEXT is "foo"
928 and we want to complete to "foobar". If WORD is "oo", return
929 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
932 complete_on_cmdlist (list, text, word)
933 struct cmd_list_element *list;
937 struct cmd_list_element *ptr;
939 int sizeof_matchlist;
941 int textlen = strlen (text);
943 sizeof_matchlist = 10;
944 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
947 for (ptr = list; ptr; ptr = ptr->next)
948 if (!strncmp (ptr->name, text, textlen)
950 && (ptr->function.cfunc
953 if (matches == sizeof_matchlist)
955 sizeof_matchlist *= 2;
956 matchlist = (char **) xrealloc ((char *)matchlist,
961 matchlist[matches] = (char *)
962 xmalloc (strlen (word) + strlen (ptr->name) + 1);
964 strcpy (matchlist[matches], ptr->name);
965 else if (word > text)
967 /* Return some portion of ptr->name. */
968 strcpy (matchlist[matches], ptr->name + (word - text));
972 /* Return some of text plus ptr->name. */
973 strncpy (matchlist[matches], word, text - word);
974 matchlist[matches][text - word] = '\0';
975 strcat (matchlist[matches], ptr->name);
982 free ((PTR)matchlist);
987 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
989 matchlist[matches] = (char *) 0;
996 parse_binary_operation (arg)
1004 length = strlen (arg);
1006 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1009 if (!strncmp (arg, "on", length)
1010 || !strncmp (arg, "1", length)
1011 || !strncmp (arg, "yes", length))
1014 if (!strncmp (arg, "off", length)
1015 || !strncmp (arg, "0", length)
1016 || !strncmp (arg, "no", length))
1020 error ("\"on\" or \"off\" expected.");
1025 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1026 of the argument, and FROM_TTY is nonzero if this command is being entered
1027 directly by the user (i.e. these are just like any other
1028 command). C is the command list element for the command. */
1030 do_setshow_command (arg, from_tty, c)
1033 struct cmd_list_element *c;
1035 if (c->type == set_cmd)
1037 switch (c->var_type)
1048 new = (char *) xmalloc (strlen (arg) + 2);
1050 while ((ch = *p++) != '\000')
1054 /* \ at end of argument is used after spaces
1055 so they won't be lost. */
1056 /* This is obsolete now that we no longer strip
1057 trailing whitespace and actually, the backslash
1058 didn't get here in my test, readline or
1059 something did something funky with a backslash
1060 right before a newline. */
1063 ch = parse_escape (&p);
1065 break; /* C loses */
1073 if (*(p - 1) != '\\')
1077 new = (char *) xrealloc (new, q - new);
1078 if (*(char **)c->var != NULL)
1079 free (*(char **)c->var);
1080 *(char **) c->var = new;
1083 case var_string_noescape:
1086 if (*(char **)c->var != NULL)
1087 free (*(char **)c->var);
1088 *(char **) c->var = savestring (arg, strlen (arg));
1092 error_no_arg ("filename to set it to.");
1093 if (*(char **)c->var != NULL)
1094 free (*(char **)c->var);
1095 *(char **)c->var = tilde_expand (arg);
1098 *(int *) c->var = parse_binary_operation (arg);
1102 error_no_arg ("integer to set it to.");
1103 *(unsigned int *) c->var = parse_and_eval_address (arg);
1104 if (*(unsigned int *) c->var == 0)
1105 *(unsigned int *) c->var = UINT_MAX;
1111 error_no_arg ("integer to set it to.");
1112 val = parse_and_eval_address (arg);
1114 *(int *) c->var = INT_MAX;
1115 else if (val >= INT_MAX)
1116 error ("integer %u out of range", val);
1118 *(int *) c->var = val;
1123 error_no_arg ("integer to set it to.");
1124 *(int *) c->var = parse_and_eval_address (arg);
1127 error ("gdb internal error: bad var_type in do_setshow_command");
1130 else if (c->type == show_cmd)
1132 /* Print doc minus "show" at start. */
1133 print_doc_line (gdb_stdout, c->doc + 5);
1135 fputs_filtered (" is ", gdb_stdout);
1137 switch (c->var_type)
1142 fputs_filtered ("\"", gdb_stdout);
1143 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1144 gdb_printchar (*p, gdb_stdout, '"');
1145 fputs_filtered ("\"", gdb_stdout);
1148 case var_string_noescape:
1150 fputs_filtered ("\"", gdb_stdout);
1151 fputs_filtered (*(char **) c->var, gdb_stdout);
1152 fputs_filtered ("\"", gdb_stdout);
1155 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1158 if (*(unsigned int *) c->var == UINT_MAX) {
1159 fputs_filtered ("unlimited", gdb_stdout);
1162 /* else fall through */
1164 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1167 if (*(int *) c->var == INT_MAX)
1169 fputs_filtered ("unlimited", gdb_stdout);
1172 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1176 error ("gdb internal error: bad var_type in do_setshow_command");
1178 fputs_filtered (".\n", gdb_stdout);
1181 error ("gdb internal error: bad cmd_type in do_setshow_command");
1182 (*c->function.sfunc) (NULL, from_tty, c);
1185 /* Show all the settings in a list of show commands. */
1188 cmd_show_list (list, from_tty, prefix)
1189 struct cmd_list_element *list;
1193 for (; list != NULL; list = list->next) {
1194 /* If we find a prefix, run its list, prefixing our output by its
1195 prefix (with "show " skipped). */
1196 if (list->prefixlist && !list->abbrev_flag)
1197 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1198 if (list->type == show_cmd)
1200 fputs_filtered (prefix, gdb_stdout);
1201 fputs_filtered (list->name, gdb_stdout);
1202 fputs_filtered (": ", gdb_stdout);
1203 do_setshow_command ((char *)NULL, from_tty, list);
1210 shell_escape (arg, from_tty)
1215 /* FIXME: what about errors (I don't know how GO32 system() handles
1218 #else /* Can fork. */
1219 int rc, status, pid;
1220 char *p, *user_shell;
1222 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1223 user_shell = "/bin/sh";
1225 /* Get the name of the shell for arg0 */
1226 if ((p = strrchr (user_shell, '/')) == NULL)
1229 p++; /* Get past '/' */
1231 if ((pid = fork()) == 0)
1234 execl (user_shell, p, 0);
1236 execl (user_shell, p, "-c", arg, 0);
1238 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1239 safe_strerror (errno));
1240 gdb_flush (gdb_stderr);
1245 while ((rc = wait (&status)) != pid && rc != -1)
1248 error ("Fork failed");
1249 #endif /* Can fork. */
1253 make_command (arg, from_tty)
1263 p = xmalloc (sizeof("make ") + strlen(arg));
1264 strcpy (p, "make ");
1265 strcpy (p + sizeof("make ")-1, arg);
1268 shell_escape (p, from_tty);
1272 show_user_1 (c, stream)
1273 struct cmd_list_element *c;
1276 register struct command_line *cmdlines;
1278 cmdlines = c->user_commands;
1281 fputs_filtered ("User command ", stream);
1282 fputs_filtered (c->name, stream);
1283 fputs_filtered (":\n", stream);
1286 fputs_filtered (cmdlines->line, stream);
1287 fputs_filtered ("\n", stream);
1288 cmdlines = cmdlines->next;
1290 fputs_filtered ("\n", stream);
1295 show_user (args, from_tty)
1299 struct cmd_list_element *c;
1300 extern struct cmd_list_element *cmdlist;
1304 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1305 if (c->class != class_user)
1306 error ("Not a user command.");
1307 show_user_1 (c, gdb_stdout);
1311 for (c = cmdlist; c; c = c->next)
1313 if (c->class == class_user)
1314 show_user_1 (c, gdb_stdout);
1320 _initialize_command ()
1322 add_com ("shell", class_support, shell_escape,
1323 "Execute the rest of the line as a shell command. \n\
1324 With no arguments, run an inferior shell.");
1325 add_com ("make", class_support, make_command,
1326 "Run the ``make'' program using the rest of the line as arguments.");
1327 add_cmd ("user", no_class, show_user,
1328 "Show definitions of user defined commands.\n\
1329 Argument is the name of the user defined command.\n\
1330 With no argument, show definitions of all user defined commands.", &showlist);