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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
29 /* Prototypes for local functions */
32 undef_cmd_error PARAMS ((char *, char *));
35 show_user PARAMS ((char *, int));
38 show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
41 make_command PARAMS ((char *, int));
44 shell_escape PARAMS ((char *, int));
47 parse_binary_operation PARAMS ((char *));
50 print_doc_line PARAMS ((GDB_FILE *, char *));
52 /* Add element named NAME.
53 CLASS is the top level category into which commands are broken down
55 FUN should be the function to execute the command;
56 it will get a character string as argument, with leading
57 and trailing blanks already eliminated.
59 DOC is a documentation string for the command.
60 Its first line should be a complete sentence.
61 It should start with ? for a command that is an abbreviation
62 or with * for a command that most users don't need to know about.
64 Add this command to command list *LIST.
66 Returns a pointer to the added command (not necessarily the head
69 struct cmd_list_element *
70 add_cmd (name, class, fun, doc, list)
72 enum command_class class;
73 void (*fun) PARAMS ((char *, int));
75 struct cmd_list_element **list;
77 register struct cmd_list_element *c
78 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
79 struct cmd_list_element *p;
81 delete_cmd (name, list);
83 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
91 while (p->next && STRCMP (p->next->name, name) <= 0)
101 c->function.cfunc = fun;
104 c->prefixlist = NULL;
105 c->prefixname = NULL;
106 c->allow_unknown = 0;
108 c->completer = make_symbol_completion_list;
109 c->type = not_set_cmd;
111 c->var_type = var_boolean;
113 c->user_commands = NULL;
115 c->cmd_pointer = NULL;
120 /* Same as above, except that the abbrev_flag is set. */
122 #if 0 /* Currently unused */
124 struct cmd_list_element *
125 add_abbrev_cmd (name, class, fun, doc, list)
127 enum command_class class;
128 void (*fun) PARAMS ((char *, int));
130 struct cmd_list_element **list;
132 register struct cmd_list_element *c
133 = add_cmd (name, class, fun, doc, list);
141 struct cmd_list_element *
142 add_alias_cmd (name, oldname, class, abbrev_flag, list)
145 enum command_class class;
147 struct cmd_list_element **list;
149 /* Must do this since lookup_cmd tries to side-effect its first arg */
151 register struct cmd_list_element *old;
152 register struct cmd_list_element *c;
153 copied_name = (char *) alloca (strlen (oldname) + 1);
154 strcpy (copied_name, oldname);
155 old = lookup_cmd (&copied_name, *list, "", 1, 1);
159 delete_cmd (name, list);
163 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
164 c->prefixlist = old->prefixlist;
165 c->prefixname = old->prefixname;
166 c->allow_unknown = old->allow_unknown;
167 c->abbrev_flag = abbrev_flag;
168 c->cmd_pointer = old;
172 /* Like add_cmd but adds an element for a command prefix:
173 a name that should be followed by a subcommand to be looked up
174 in another command list. PREFIXLIST should be the address
175 of the variable containing that list. */
177 struct cmd_list_element *
178 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
181 enum command_class class;
182 void (*fun) PARAMS ((char *, int));
184 struct cmd_list_element **prefixlist;
187 struct cmd_list_element **list;
189 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
190 c->prefixlist = prefixlist;
191 c->prefixname = prefixname;
192 c->allow_unknown = allow_unknown;
196 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
198 struct cmd_list_element *
199 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
202 enum command_class class;
203 void (*fun) PARAMS ((char *, int));
205 struct cmd_list_element **prefixlist;
208 struct cmd_list_element **list;
210 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
211 c->prefixlist = prefixlist;
212 c->prefixname = prefixname;
213 c->allow_unknown = allow_unknown;
218 /* This is an empty "cfunc". */
220 not_just_help_class_command (args, from_tty)
226 /* This is an empty "sfunc". */
227 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
230 empty_sfunc (args, from_tty, c)
233 struct cmd_list_element *c;
237 /* Add element named NAME to command list LIST (the list for set
238 or some sublist thereof).
239 CLASS is as in add_cmd.
240 VAR_TYPE is the kind of thing we are setting.
241 VAR is address of the variable being controlled by this command.
242 DOC is the documentation string. */
244 struct cmd_list_element *
245 add_set_cmd (name, class, var_type, var, doc, list)
247 enum command_class class;
251 struct cmd_list_element **list;
253 struct cmd_list_element *c
254 = add_cmd (name, class, NO_FUNCTION, doc, list);
257 c->var_type = var_type;
259 /* This needs to be something besides NO_FUNCTION so that this isn't
260 treated as a help class. */
261 c->function.sfunc = empty_sfunc;
265 /* Add element named NAME to command list LIST (the list for set
266 or some sublist thereof).
267 CLASS is as in add_cmd.
268 ENUMLIST is a list of strings which may follow NAME.
269 VAR is address of the variable which will contain the matching string
271 DOC is the documentation string. */
273 struct cmd_list_element *
274 add_set_enum_cmd (name, class, enumlist, var, doc, list)
276 enum command_class class;
280 struct cmd_list_element **list;
282 struct cmd_list_element *c
283 = add_set_cmd (name, class, var_enum, var, doc, list);
289 /* Where SETCMD has already been added, add the corresponding show
290 command to LIST and return a pointer to the added command (not
291 necessarily the head of LIST). */
292 struct cmd_list_element *
293 add_show_from_set (setcmd, list)
294 struct cmd_list_element *setcmd;
295 struct cmd_list_element **list;
297 struct cmd_list_element *showcmd =
298 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
299 struct cmd_list_element *p;
301 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
302 delete_cmd (showcmd->name, list);
303 showcmd->type = show_cmd;
305 /* Replace "set " at start of docstring with "show ". */
306 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
307 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
308 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
310 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
312 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
314 showcmd->next = *list;
320 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
324 showcmd->next = p->next;
331 /* Remove the command named NAME from the command list. */
334 delete_cmd (name, list)
336 struct cmd_list_element **list;
338 register struct cmd_list_element *c;
339 struct cmd_list_element *p;
341 while (*list && STREQ ((*list)->name, name))
344 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
351 for (c = *list; c->next;)
353 if (STREQ (c->next->name, name))
356 c->next->hookee->hook = 0; /* hooked cmd gets away. */
366 /* This command really has to deal with two things:
367 * 1) I want documentation on *this string* (usually called by
368 * "help commandname").
369 * 2) I want documentation on *this list* (usually called by
370 * giving a command that requires subcommands. Also called by saying
373 * I am going to split this into two seperate comamnds, help_cmd and
378 help_cmd (command, stream)
382 struct cmd_list_element *c;
383 extern struct cmd_list_element *cmdlist;
387 help_list (cmdlist, "", all_classes, stream);
391 c = lookup_cmd (&command, cmdlist, "", 0, 0);
396 /* There are three cases here.
397 If c->prefixlist is nonzero, we have a prefix command.
398 Print its documentation, then list its subcommands.
400 If c->function is nonzero, we really have a command.
401 Print its documentation and return.
403 If c->function is zero, we have a class name.
404 Print its documentation (as if it were a command)
405 and then set class to the number of this class
406 so that the commands in the class will be listed. */
408 fputs_filtered (c->doc, stream);
409 fputs_filtered ("\n", stream);
411 if (c->prefixlist == 0 && c->function.cfunc != NULL)
413 fprintf_filtered (stream, "\n");
415 /* If this is a prefix command, print it's subcommands */
417 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
419 /* If this is a class name, print all of the commands in the class */
420 if (c->function.cfunc == NULL)
421 help_list (cmdlist, "", c->class, stream);
424 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
429 * Get a specific kind of help on a command list.
432 * CMDTYPE is the prefix to use in the title string.
433 * CLASS is the class with which to list the nodes of this list (see
434 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
435 * everything, ALL_CLASSES for just classes, and non-negative for only things
436 * in a specific class.
437 * and STREAM is the output stream on which to print things.
438 * If you call this routine with a class >= 0, it recurses.
441 help_list (list, cmdtype, class, stream)
442 struct cmd_list_element *list;
444 enum command_class class;
448 char *cmdtype1, *cmdtype2;
450 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
451 len = strlen (cmdtype);
452 cmdtype1 = (char *) alloca (len + 1);
454 cmdtype2 = (char *) alloca (len + 4);
459 strncpy (cmdtype1 + 1, cmdtype, len - 1);
461 strncpy (cmdtype2, cmdtype, len - 1);
462 strcpy (cmdtype2 + len - 1, " sub");
465 if (class == all_classes)
466 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
468 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
470 help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
472 if (class == all_classes)
473 fprintf_filtered (stream, "\n\
474 Type \"help%s\" followed by a class name for a list of commands in that class.",
477 fprintf_filtered (stream, "\n\
478 Type \"help%s\" followed by %scommand name for full documentation.\n\
479 Command name abbreviations are allowed if unambiguous.\n",
483 /* Print only the first line of STR on STREAM. */
485 print_doc_line (stream, str)
489 static char *line_buffer = 0;
490 static int line_size;
496 line_buffer = (char *) xmalloc (line_size);
500 while (*p && *p != '\n' && *p != '.' && *p != ',')
502 if (p - str > line_size - 1)
504 line_size = p - str + 1;
505 free ((PTR)line_buffer);
506 line_buffer = (char *) xmalloc (line_size);
508 strncpy (line_buffer, str, p - str);
509 line_buffer[p - str] = '\0';
510 if (islower (line_buffer[0]))
511 line_buffer[0] = toupper (line_buffer[0]);
512 fputs_filtered (line_buffer, stream);
516 * Implement a help command on command list LIST.
517 * RECURSE should be non-zero if this should be done recursively on
518 * all sublists of LIST.
519 * PREFIX is the prefix to print before each command name.
520 * STREAM is the stream upon which the output should be written.
522 * A non-negative class number to list only commands in that
524 * ALL_COMMANDS to list all commands in list.
525 * ALL_CLASSES to list all classes in list.
527 * Note that RECURSE will be active on *all* sublists, not just the
528 * ones selected by the criteria above (ie. the selection mechanism
529 * is at the low level, not the high-level).
532 help_cmd_list (list, class, prefix, recurse, stream)
533 struct cmd_list_element *list;
534 enum command_class class;
539 register struct cmd_list_element *c;
541 for (c = list; c; c = c->next)
543 if (c->abbrev_flag == 0 &&
544 (class == all_commands
545 || (class == all_classes && c->function.cfunc == NULL)
546 || (class == c->class && c->function.cfunc != NULL)))
548 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
549 print_doc_line (stream, c->doc);
550 fputs_filtered ("\n", stream);
553 && c->prefixlist != 0
554 && c->abbrev_flag == 0)
555 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
559 /* This routine takes a line of TEXT and a CLIST in which to start the
560 lookup. When it returns it will have incremented the text pointer past
561 the section of text it matched, set *RESULT_LIST to point to the list in
562 which the last word was matched, and will return a pointer to the cmd
563 list element which the text matches. It will return NULL if no match at
564 all was possible. It will return -1 (cast appropriately, ick) if ambigous
565 matches are possible; in this case *RESULT_LIST will be set to point to
566 the list in which there are ambiguous choices (and *TEXT will be set to
567 the ambiguous text string).
569 If the located command was an abbreviation, this routine returns the base
570 command of the abbreviation.
572 It does no error reporting whatsoever; control will always return
573 to the superior routine.
575 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
576 at the prefix_command (ie. the best match) *or* (special case) will be NULL
577 if no prefix command was ever found. For example, in the case of "info a",
578 "info" matches without ambiguity, but "a" could be "args" or "address", so
579 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
580 RESULT_LIST should not be interpeted as a pointer to the beginning of a
581 list; it simply points to a specific command. In the case of an ambiguous
582 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
583 "info t" can be "info types" or "info target"; upon return *TEXT has been
584 advanced past "info ").
586 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
587 affect the operation).
589 This routine does *not* modify the text pointed to by TEXT.
591 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
592 are actually help classes rather than commands (i.e. the function field of
593 the struct cmd_list_element is NULL). */
595 struct cmd_list_element *
596 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
598 struct cmd_list_element *clist, **result_list;
599 int ignore_help_classes;
602 int len, tmp, nfound;
603 struct cmd_list_element *found, *c;
605 while (**text == ' ' || **text == '\t')
608 /* Treating underscores as part of command words is important
609 so that "set args_foo()" doesn't get interpreted as
610 "set args _foo()". */
612 *p && (isalnum(*p) || *p == '-' || *p == '_');
616 /* If nothing but whitespace, return 0. */
622 /* *text and p now bracket the first command word to lookup (and
623 it's length is len). We copy this into a local temporary,
624 converting to lower case as we go. */
626 command = (char *) alloca (len + 1);
627 for (tmp = 0; tmp < len; tmp++)
629 char x = (*text)[tmp];
630 command[tmp] = isupper(x) ? tolower(x) : x;
637 for (c = clist; c; c = c->next)
638 if (!strncmp (command, c->name, len)
639 && (!ignore_help_classes || c->function.cfunc))
643 if (c->name[len] == '\0')
650 /* If nothing matches, we have a simple failure. */
656 if (result_list != NULL)
657 /* Will be modified in calling routine
658 if we know what the prefix command is. */
660 return (struct cmd_list_element *) -1; /* Ambiguous. */
663 /* We've matched something on this list. Move text pointer forward. */
667 /* If this was an abbreviation, use the base command instead. */
669 if (found->cmd_pointer)
670 found = found->cmd_pointer;
672 /* If we found a prefix command, keep looking. */
674 if (found->prefixlist)
676 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
677 ignore_help_classes);
680 /* Didn't find anything; this is as far as we got. */
681 if (result_list != NULL)
682 *result_list = clist;
685 else if (c == (struct cmd_list_element *) -1)
687 /* We've gotten this far properly, but the next step
688 is ambiguous. We need to set the result list to the best
689 we've found (if an inferior hasn't already set it). */
690 if (result_list != NULL)
692 /* This used to say *result_list = *found->prefixlist
693 If that was correct, need to modify the documentation
694 at the top of this function to clarify what is supposed
696 *result_list = found;
707 if (result_list != NULL)
708 *result_list = clist;
713 /* All this hair to move the space to the front of cmdtype */
716 undef_cmd_error (cmdtype, q)
719 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
727 /* Look up the contents of *LINE as a command in the command list LIST.
728 LIST is a chain of struct cmd_list_element's.
729 If it is found, return the struct cmd_list_element for that command
730 and update *LINE to point after the command name, at the first argument.
731 If not found, call error if ALLOW_UNKNOWN is zero
732 otherwise (or if error returns) return zero.
733 Call error if specified command is ambiguous,
734 unless ALLOW_UNKNOWN is negative.
735 CMDTYPE precedes the word "command" in the error message.
737 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
738 elements which are actually help classes rather than commands (i.e.
739 the function field of the struct cmd_list_element is 0). */
741 struct cmd_list_element *
742 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
744 struct cmd_list_element *list;
747 int ignore_help_classes;
749 struct cmd_list_element *last_list = 0;
750 struct cmd_list_element *c =
751 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
753 /* This is wrong for complete_command. */
754 char *ptr = (*line) + strlen (*line) - 1;
756 /* Clear off trailing whitespace. */
757 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
767 error ("Lack of needed %scommand", cmdtype);
772 while (isalnum(*p) || *p == '-')
775 q = (char *) alloca (p - *line + 1);
776 strncpy (q, *line, p - *line);
778 undef_cmd_error (cmdtype, q);
784 else if (c == (struct cmd_list_element *) -1)
786 /* Ambigous. Local values should be off prefixlist or called
788 int local_allow_unknown = (last_list ? last_list->allow_unknown :
790 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
791 struct cmd_list_element *local_list =
792 (last_list ? *(last_list->prefixlist) : list);
794 if (local_allow_unknown < 0)
797 return last_list; /* Found something. */
799 return 0; /* Found nothing. */
803 /* Report as error. */
808 ((*line)[amb_len] && (*line)[amb_len] != ' '
809 && (*line)[amb_len] != '\t');
814 for (c = local_list; c; c = c->next)
815 if (!strncmp (*line, c->name, amb_len))
817 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
820 strcat (ambbuf, ", ");
821 strcat (ambbuf, c->name);
825 strcat (ambbuf, "..");
829 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
836 /* We've got something. It may still not be what the caller
837 wants (if this command *needs* a subcommand). */
838 while (**line == ' ' || **line == '\t')
841 if (c->prefixlist && **line && !c->allow_unknown)
842 undef_cmd_error (c->prefixname, *line);
844 /* Seems to be what he wants. Return it. */
851 /* Look up the contents of *LINE as a command in the command list LIST.
852 LIST is a chain of struct cmd_list_element's.
853 If it is found, return the struct cmd_list_element for that command
854 and update *LINE to point after the command name, at the first argument.
855 If not found, call error if ALLOW_UNKNOWN is zero
856 otherwise (or if error returns) return zero.
857 Call error if specified command is ambiguous,
858 unless ALLOW_UNKNOWN is negative.
859 CMDTYPE precedes the word "command" in the error message. */
861 struct cmd_list_element *
862 lookup_cmd (line, list, cmdtype, allow_unknown)
864 struct cmd_list_element *list;
869 register struct cmd_list_element *c, *found;
875 /* Skip leading whitespace. */
877 while (**line == ' ' || **line == '\t')
880 /* Clear out trailing whitespace. */
882 p = *line + strlen (*line);
883 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
887 /* Find end of command name. */
890 while (*p == '-' || isalnum(*p))
893 /* Look up the command name.
894 If exact match, keep that.
895 Otherwise, take command abbreviated, if unique. Note that (in my
896 opinion) a null string does *not* indicate ambiguity; simply the
897 end of the argument. */
902 error ("Lack of needed %scommand", cmdtype);
906 /* Copy over to a local buffer, converting to lowercase on the way.
907 This is in case the command being parsed is a subcommand which
908 doesn't match anything, and that's ok. We want the original
909 untouched for the routine of the original command. */
911 processed_cmd = (char *) alloca (p - *line + 1);
912 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
914 char x = (*line)[cmd_len];
916 processed_cmd[cmd_len] = tolower(x);
918 processed_cmd[cmd_len] = x;
920 processed_cmd[cmd_len] = '\0';
922 /* Check all possibilities in the current command list. */
925 for (c = list; c; c = c->next)
927 if (!strncmp (processed_cmd, c->name, cmd_len))
931 if (c->name[cmd_len] == 0)
939 /* Report error for undefined command name. */
943 if (nfound > 1 && allow_unknown >= 0)
946 for (c = list; c; c = c->next)
947 if (!strncmp (processed_cmd, c->name, cmd_len))
949 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
952 strcat (ambbuf, ", ");
953 strcat (ambbuf, c->name);
957 strcat (ambbuf, "..");
961 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
962 processed_cmd, ambbuf);
964 else if (!allow_unknown)
965 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
969 /* Skip whitespace before the argument. */
971 while (*p == ' ' || *p == '\t') p++;
974 if (found->prefixlist && *p)
976 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
977 found->allow_unknown);
986 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
988 /* Return a vector of char pointers which point to the different
989 possible completions in LIST of TEXT.
991 WORD points in the same buffer as TEXT, and completions should be
992 returned relative to this position. For example, suppose TEXT is "foo"
993 and we want to complete to "foobar". If WORD is "oo", return
994 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
997 complete_on_cmdlist (list, text, word)
998 struct cmd_list_element *list;
1002 struct cmd_list_element *ptr;
1004 int sizeof_matchlist;
1006 int textlen = strlen (text);
1008 sizeof_matchlist = 10;
1009 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1012 for (ptr = list; ptr; ptr = ptr->next)
1013 if (!strncmp (ptr->name, text, textlen)
1014 && !ptr->abbrev_flag
1015 && (ptr->function.cfunc
1016 || ptr->prefixlist))
1018 if (matches == sizeof_matchlist)
1020 sizeof_matchlist *= 2;
1021 matchlist = (char **) xrealloc ((char *)matchlist,
1023 * sizeof (char *)));
1026 matchlist[matches] = (char *)
1027 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1029 strcpy (matchlist[matches], ptr->name);
1030 else if (word > text)
1032 /* Return some portion of ptr->name. */
1033 strcpy (matchlist[matches], ptr->name + (word - text));
1037 /* Return some of text plus ptr->name. */
1038 strncpy (matchlist[matches], word, text - word);
1039 matchlist[matches][text - word] = '\0';
1040 strcat (matchlist[matches], ptr->name);
1047 free ((PTR)matchlist);
1052 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1053 * sizeof (char *)));
1054 matchlist[matches] = (char *) 0;
1060 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1062 /* Return a vector of char pointers which point to the different
1063 possible completions in CMD of TEXT.
1065 WORD points in the same buffer as TEXT, and completions should be
1066 returned relative to this position. For example, suppose TEXT is "foo"
1067 and we want to complete to "foobar". If WORD is "oo", return
1068 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1071 complete_on_enum (enumlist, text, word)
1077 int sizeof_matchlist;
1079 int textlen = strlen (text);
1083 sizeof_matchlist = 10;
1084 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1087 for (i = 0; (name = enumlist[i]) != NULL; i++)
1088 if (strncmp (name, text, textlen) == 0)
1090 if (matches == sizeof_matchlist)
1092 sizeof_matchlist *= 2;
1093 matchlist = (char **) xrealloc ((char *)matchlist,
1095 * sizeof (char *)));
1098 matchlist[matches] = (char *)
1099 xmalloc (strlen (word) + strlen (name) + 1);
1101 strcpy (matchlist[matches], name);
1102 else if (word > text)
1104 /* Return some portion of name. */
1105 strcpy (matchlist[matches], name + (word - text));
1109 /* Return some of text plus name. */
1110 strncpy (matchlist[matches], word, text - word);
1111 matchlist[matches][text - word] = '\0';
1112 strcat (matchlist[matches], name);
1119 free ((PTR)matchlist);
1124 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1125 * sizeof (char *)));
1126 matchlist[matches] = (char *) 0;
1133 parse_binary_operation (arg)
1141 length = strlen (arg);
1143 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1146 if (!strncmp (arg, "on", length)
1147 || !strncmp (arg, "1", length)
1148 || !strncmp (arg, "yes", length))
1151 if (!strncmp (arg, "off", length)
1152 || !strncmp (arg, "0", length)
1153 || !strncmp (arg, "no", length))
1157 error ("\"on\" or \"off\" expected.");
1162 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1163 of the argument, and FROM_TTY is nonzero if this command is being entered
1164 directly by the user (i.e. these are just like any other
1165 command). C is the command list element for the command. */
1167 do_setshow_command (arg, from_tty, c)
1170 struct cmd_list_element *c;
1172 if (c->type == set_cmd)
1174 switch (c->var_type)
1185 new = (char *) xmalloc (strlen (arg) + 2);
1187 while ((ch = *p++) != '\000')
1191 /* \ at end of argument is used after spaces
1192 so they won't be lost. */
1193 /* This is obsolete now that we no longer strip
1194 trailing whitespace and actually, the backslash
1195 didn't get here in my test, readline or
1196 something did something funky with a backslash
1197 right before a newline. */
1200 ch = parse_escape (&p);
1202 break; /* C loses */
1210 if (*(p - 1) != '\\')
1214 new = (char *) xrealloc (new, q - new);
1215 if (*(char **)c->var != NULL)
1216 free (*(char **)c->var);
1217 *(char **) c->var = new;
1220 case var_string_noescape:
1223 if (*(char **)c->var != NULL)
1224 free (*(char **)c->var);
1225 *(char **) c->var = savestring (arg, strlen (arg));
1229 error_no_arg ("filename to set it to.");
1230 if (*(char **)c->var != NULL)
1231 free (*(char **)c->var);
1232 *(char **)c->var = tilde_expand (arg);
1235 *(int *) c->var = parse_binary_operation (arg);
1239 error_no_arg ("integer to set it to.");
1240 *(unsigned int *) c->var = parse_and_eval_address (arg);
1241 if (*(unsigned int *) c->var == 0)
1242 *(unsigned int *) c->var = UINT_MAX;
1248 error_no_arg ("integer to set it to.");
1249 val = parse_and_eval_address (arg);
1251 *(int *) c->var = INT_MAX;
1252 else if (val >= INT_MAX)
1253 error ("integer %u out of range", val);
1255 *(int *) c->var = val;
1260 error_no_arg ("integer to set it to.");
1261 *(int *) c->var = parse_and_eval_address (arg);
1271 /* if no argument was supplied, print an informative error message */
1275 strcpy (msg, "Requires an argument. Valid arguments are ");
1276 for (i = 0; c->enums[i]; i++)
1280 strcat (msg, c->enums[i]);
1286 p = strchr (arg, ' ');
1294 for (i = 0; c->enums[i]; i++)
1295 if (strncmp (arg, c->enums[i], len) == 0)
1297 match = c->enums[i];
1302 error ("Undefined item: \"%s\".", arg);
1305 error ("Ambiguous item \"%s\".", arg);
1307 *(char **)c->var = match;
1311 error ("gdb internal error: bad var_type in do_setshow_command");
1314 else if (c->type == show_cmd)
1316 /* Print doc minus "show" at start. */
1317 print_doc_line (gdb_stdout, c->doc + 5);
1319 fputs_filtered (" is ", gdb_stdout);
1321 switch (c->var_type)
1327 fputs_filtered ("\"", gdb_stdout);
1328 if (*(unsigned char **)c->var)
1329 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1330 gdb_printchar (*p, gdb_stdout, '"');
1331 fputs_filtered ("\"", gdb_stdout);
1334 case var_string_noescape:
1337 fputs_filtered ("\"", gdb_stdout);
1338 if (*(char **)c->var)
1339 fputs_filtered (*(char **) c->var, gdb_stdout);
1340 fputs_filtered ("\"", gdb_stdout);
1343 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1346 if (*(unsigned int *) c->var == UINT_MAX) {
1347 fputs_filtered ("unlimited", gdb_stdout);
1350 /* else fall through */
1352 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1355 if (*(int *) c->var == INT_MAX)
1357 fputs_filtered ("unlimited", gdb_stdout);
1360 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1364 error ("gdb internal error: bad var_type in do_setshow_command");
1366 fputs_filtered (".\n", gdb_stdout);
1369 error ("gdb internal error: bad cmd_type in do_setshow_command");
1370 (*c->function.sfunc) (NULL, from_tty, c);
1373 /* Show all the settings in a list of show commands. */
1376 cmd_show_list (list, from_tty, prefix)
1377 struct cmd_list_element *list;
1381 for (; list != NULL; list = list->next) {
1382 /* If we find a prefix, run its list, prefixing our output by its
1383 prefix (with "show " skipped). */
1384 if (list->prefixlist && !list->abbrev_flag)
1385 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1386 if (list->type == show_cmd)
1388 fputs_filtered (prefix, gdb_stdout);
1389 fputs_filtered (list->name, gdb_stdout);
1390 fputs_filtered (": ", gdb_stdout);
1391 do_setshow_command ((char *)NULL, from_tty, list);
1398 shell_escape (arg, from_tty)
1403 /* FIXME: what about errors (I don't know how GO32 system() handles
1406 #else /* Can fork. */
1407 int rc, status, pid;
1408 char *p, *user_shell;
1410 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1411 user_shell = "/bin/sh";
1413 /* Get the name of the shell for arg0 */
1414 if ((p = strrchr (user_shell, '/')) == NULL)
1417 p++; /* Get past '/' */
1419 if ((pid = fork()) == 0)
1422 execl (user_shell, p, 0);
1424 execl (user_shell, p, "-c", arg, 0);
1426 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1427 safe_strerror (errno));
1428 gdb_flush (gdb_stderr);
1433 while ((rc = wait (&status)) != pid && rc != -1)
1436 error ("Fork failed");
1437 #endif /* Can fork. */
1441 make_command (arg, from_tty)
1451 p = xmalloc (sizeof("make ") + strlen(arg));
1452 strcpy (p, "make ");
1453 strcpy (p + sizeof("make ")-1, arg);
1456 shell_escape (p, from_tty);
1460 show_user_1 (c, stream)
1461 struct cmd_list_element *c;
1464 register struct command_line *cmdlines;
1466 cmdlines = c->user_commands;
1469 fputs_filtered ("User command ", stream);
1470 fputs_filtered (c->name, stream);
1471 fputs_filtered (":\n", stream);
1475 print_command_line (cmdlines, 4);
1476 cmdlines = cmdlines->next;
1478 fputs_filtered ("\n", stream);
1483 show_user (args, from_tty)
1487 struct cmd_list_element *c;
1488 extern struct cmd_list_element *cmdlist;
1492 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1493 if (c->class != class_user)
1494 error ("Not a user command.");
1495 show_user_1 (c, gdb_stdout);
1499 for (c = cmdlist; c; c = c->next)
1501 if (c->class == class_user)
1502 show_user_1 (c, gdb_stdout);
1508 _initialize_command ()
1510 add_com ("shell", class_support, shell_escape,
1511 "Execute the rest of the line as a shell command. \n\
1512 With no arguments, run an inferior shell.");
1513 add_com ("make", class_support, make_command,
1514 "Run the ``make'' program using the rest of the line as arguments.");
1515 add_cmd ("user", no_class, show_user,
1516 "Show definitions of user defined commands.\n\
1517 Argument is the name of the user defined command.\n\
1518 With no argument, show definitions of all user defined commands.", &showlist);