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);
290 /* Where SETCMD has already been added, add the corresponding show
291 command to LIST and return a pointer to the added command (not
292 necessarily the head of LIST). */
293 struct cmd_list_element *
294 add_show_from_set (setcmd, list)
295 struct cmd_list_element *setcmd;
296 struct cmd_list_element **list;
298 struct cmd_list_element *showcmd =
299 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
300 struct cmd_list_element *p;
302 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
303 delete_cmd (showcmd->name, list);
304 showcmd->type = show_cmd;
306 /* Replace "set " at start of docstring with "show ". */
307 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
308 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
309 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
311 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
313 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
315 showcmd->next = *list;
321 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
325 showcmd->next = p->next;
332 /* Remove the command named NAME from the command list. */
335 delete_cmd (name, list)
337 struct cmd_list_element **list;
339 register struct cmd_list_element *c;
340 struct cmd_list_element *p;
342 while (*list && STREQ ((*list)->name, name))
345 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
352 for (c = *list; c->next;)
354 if (STREQ (c->next->name, name))
357 c->next->hookee->hook = 0; /* hooked cmd gets away. */
367 /* This command really has to deal with two things:
368 * 1) I want documentation on *this string* (usually called by
369 * "help commandname").
370 * 2) I want documentation on *this list* (usually called by
371 * giving a command that requires subcommands. Also called by saying
374 * I am going to split this into two seperate comamnds, help_cmd and
379 help_cmd (command, stream)
383 struct cmd_list_element *c;
384 extern struct cmd_list_element *cmdlist;
388 help_list (cmdlist, "", all_classes, stream);
392 c = lookup_cmd (&command, cmdlist, "", 0, 0);
397 /* There are three cases here.
398 If c->prefixlist is nonzero, we have a prefix command.
399 Print its documentation, then list its subcommands.
401 If c->function is nonzero, we really have a command.
402 Print its documentation and return.
404 If c->function is zero, we have a class name.
405 Print its documentation (as if it were a command)
406 and then set class to the number of this class
407 so that the commands in the class will be listed. */
409 fputs_filtered (c->doc, stream);
410 fputs_filtered ("\n", stream);
412 if (c->prefixlist == 0 && c->function.cfunc != NULL)
414 fprintf_filtered (stream, "\n");
416 /* If this is a prefix command, print it's subcommands */
418 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
420 /* If this is a class name, print all of the commands in the class */
421 if (c->function.cfunc == NULL)
422 help_list (cmdlist, "", c->class, stream);
425 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
430 * Get a specific kind of help on a command list.
433 * CMDTYPE is the prefix to use in the title string.
434 * CLASS is the class with which to list the nodes of this list (see
435 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
436 * everything, ALL_CLASSES for just classes, and non-negative for only things
437 * in a specific class.
438 * and STREAM is the output stream on which to print things.
439 * If you call this routine with a class >= 0, it recurses.
442 help_list (list, cmdtype, class, stream)
443 struct cmd_list_element *list;
445 enum command_class class;
449 char *cmdtype1, *cmdtype2;
451 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
452 len = strlen (cmdtype);
453 cmdtype1 = (char *) alloca (len + 1);
455 cmdtype2 = (char *) alloca (len + 4);
460 strncpy (cmdtype1 + 1, cmdtype, len - 1);
462 strncpy (cmdtype2, cmdtype, len - 1);
463 strcpy (cmdtype2 + len - 1, " sub");
466 if (class == all_classes)
467 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
469 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
471 help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
473 if (class == all_classes)
474 fprintf_filtered (stream, "\n\
475 Type \"help%s\" followed by a class name for a list of commands in that class.",
478 fprintf_filtered (stream, "\n\
479 Type \"help%s\" followed by %scommand name for full documentation.\n\
480 Command name abbreviations are allowed if unambiguous.\n",
484 /* Print only the first line of STR on STREAM. */
486 print_doc_line (stream, str)
490 static char *line_buffer = 0;
491 static int line_size;
497 line_buffer = (char *) xmalloc (line_size);
501 while (*p && *p != '\n' && *p != '.' && *p != ',')
503 if (p - str > line_size - 1)
505 line_size = p - str + 1;
506 free ((PTR)line_buffer);
507 line_buffer = (char *) xmalloc (line_size);
509 strncpy (line_buffer, str, p - str);
510 line_buffer[p - str] = '\0';
511 if (islower (line_buffer[0]))
512 line_buffer[0] = toupper (line_buffer[0]);
513 fputs_filtered (line_buffer, stream);
517 * Implement a help command on command list LIST.
518 * RECURSE should be non-zero if this should be done recursively on
519 * all sublists of LIST.
520 * PREFIX is the prefix to print before each command name.
521 * STREAM is the stream upon which the output should be written.
523 * A non-negative class number to list only commands in that
525 * ALL_COMMANDS to list all commands in list.
526 * ALL_CLASSES to list all classes in list.
528 * Note that RECURSE will be active on *all* sublists, not just the
529 * ones selected by the criteria above (ie. the selection mechanism
530 * is at the low level, not the high-level).
533 help_cmd_list (list, class, prefix, recurse, stream)
534 struct cmd_list_element *list;
535 enum command_class class;
540 register struct cmd_list_element *c;
542 for (c = list; c; c = c->next)
544 if (c->abbrev_flag == 0 &&
545 (class == all_commands
546 || (class == all_classes && c->function.cfunc == NULL)
547 || (class == c->class && c->function.cfunc != NULL)))
549 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
550 print_doc_line (stream, c->doc);
551 fputs_filtered ("\n", stream);
554 && c->prefixlist != 0
555 && c->abbrev_flag == 0)
556 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
560 /* This routine takes a line of TEXT and a CLIST in which to start the
561 lookup. When it returns it will have incremented the text pointer past
562 the section of text it matched, set *RESULT_LIST to point to the list in
563 which the last word was matched, and will return a pointer to the cmd
564 list element which the text matches. It will return NULL if no match at
565 all was possible. It will return -1 (cast appropriately, ick) if ambigous
566 matches are possible; in this case *RESULT_LIST will be set to point to
567 the list in which there are ambiguous choices (and *TEXT will be set to
568 the ambiguous text string).
570 If the located command was an abbreviation, this routine returns the base
571 command of the abbreviation.
573 It does no error reporting whatsoever; control will always return
574 to the superior routine.
576 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
577 at the prefix_command (ie. the best match) *or* (special case) will be NULL
578 if no prefix command was ever found. For example, in the case of "info a",
579 "info" matches without ambiguity, but "a" could be "args" or "address", so
580 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
581 RESULT_LIST should not be interpeted as a pointer to the beginning of a
582 list; it simply points to a specific command. In the case of an ambiguous
583 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
584 "info t" can be "info types" or "info target"; upon return *TEXT has been
585 advanced past "info ").
587 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
588 affect the operation).
590 This routine does *not* modify the text pointed to by TEXT.
592 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
593 are actually help classes rather than commands (i.e. the function field of
594 the struct cmd_list_element is NULL). */
596 struct cmd_list_element *
597 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
599 struct cmd_list_element *clist, **result_list;
600 int ignore_help_classes;
603 int len, tmp, nfound;
604 struct cmd_list_element *found, *c;
606 while (**text == ' ' || **text == '\t')
609 /* Treating underscores as part of command words is important
610 so that "set args_foo()" doesn't get interpreted as
611 "set args _foo()". */
613 *p && (isalnum(*p) || *p == '-' || *p == '_');
617 /* If nothing but whitespace, return 0. */
623 /* *text and p now bracket the first command word to lookup (and
624 it's length is len). We copy this into a local temporary,
625 converting to lower case as we go. */
627 command = (char *) alloca (len + 1);
628 for (tmp = 0; tmp < len; tmp++)
630 char x = (*text)[tmp];
631 command[tmp] = isupper(x) ? tolower(x) : x;
638 for (c = clist; c; c = c->next)
639 if (!strncmp (command, c->name, len)
640 && (!ignore_help_classes || c->function.cfunc))
644 if (c->name[len] == '\0')
651 /* If nothing matches, we have a simple failure. */
657 if (result_list != NULL)
658 /* Will be modified in calling routine
659 if we know what the prefix command is. */
661 return (struct cmd_list_element *) -1; /* Ambiguous. */
664 /* We've matched something on this list. Move text pointer forward. */
668 /* If this was an abbreviation, use the base command instead. */
670 if (found->cmd_pointer)
671 found = found->cmd_pointer;
673 /* If we found a prefix command, keep looking. */
675 if (found->prefixlist)
677 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
678 ignore_help_classes);
681 /* Didn't find anything; this is as far as we got. */
682 if (result_list != NULL)
683 *result_list = clist;
686 else if (c == (struct cmd_list_element *) -1)
688 /* We've gotten this far properly, but the next step
689 is ambiguous. We need to set the result list to the best
690 we've found (if an inferior hasn't already set it). */
691 if (result_list != NULL)
693 /* This used to say *result_list = *found->prefixlist
694 If that was correct, need to modify the documentation
695 at the top of this function to clarify what is supposed
697 *result_list = found;
708 if (result_list != NULL)
709 *result_list = clist;
714 /* All this hair to move the space to the front of cmdtype */
717 undef_cmd_error (cmdtype, q)
720 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
728 /* Look up the contents of *LINE as a command in the command list LIST.
729 LIST is a chain of struct cmd_list_element's.
730 If it is found, return the struct cmd_list_element for that command
731 and update *LINE to point after the command name, at the first argument.
732 If not found, call error if ALLOW_UNKNOWN is zero
733 otherwise (or if error returns) return zero.
734 Call error if specified command is ambiguous,
735 unless ALLOW_UNKNOWN is negative.
736 CMDTYPE precedes the word "command" in the error message.
738 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
739 elements which are actually help classes rather than commands (i.e.
740 the function field of the struct cmd_list_element is 0). */
742 struct cmd_list_element *
743 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
745 struct cmd_list_element *list;
748 int ignore_help_classes;
750 struct cmd_list_element *last_list = 0;
751 struct cmd_list_element *c =
752 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
754 /* This is wrong for complete_command. */
755 char *ptr = (*line) + strlen (*line) - 1;
757 /* Clear off trailing whitespace. */
758 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
768 error ("Lack of needed %scommand", cmdtype);
773 while (isalnum(*p) || *p == '-')
776 q = (char *) alloca (p - *line + 1);
777 strncpy (q, *line, p - *line);
779 undef_cmd_error (cmdtype, q);
785 else if (c == (struct cmd_list_element *) -1)
787 /* Ambigous. Local values should be off prefixlist or called
789 int local_allow_unknown = (last_list ? last_list->allow_unknown :
791 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
792 struct cmd_list_element *local_list =
793 (last_list ? *(last_list->prefixlist) : list);
795 if (local_allow_unknown < 0)
798 return last_list; /* Found something. */
800 return 0; /* Found nothing. */
804 /* Report as error. */
809 ((*line)[amb_len] && (*line)[amb_len] != ' '
810 && (*line)[amb_len] != '\t');
815 for (c = local_list; c; c = c->next)
816 if (!strncmp (*line, c->name, amb_len))
818 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
821 strcat (ambbuf, ", ");
822 strcat (ambbuf, c->name);
826 strcat (ambbuf, "..");
830 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
837 /* We've got something. It may still not be what the caller
838 wants (if this command *needs* a subcommand). */
839 while (**line == ' ' || **line == '\t')
842 if (c->prefixlist && **line && !c->allow_unknown)
843 undef_cmd_error (c->prefixname, *line);
845 /* Seems to be what he wants. Return it. */
852 /* Look up the contents of *LINE as a command in the command list LIST.
853 LIST is a chain of struct cmd_list_element's.
854 If it is found, return the struct cmd_list_element for that command
855 and update *LINE to point after the command name, at the first argument.
856 If not found, call error if ALLOW_UNKNOWN is zero
857 otherwise (or if error returns) return zero.
858 Call error if specified command is ambiguous,
859 unless ALLOW_UNKNOWN is negative.
860 CMDTYPE precedes the word "command" in the error message. */
862 struct cmd_list_element *
863 lookup_cmd (line, list, cmdtype, allow_unknown)
865 struct cmd_list_element *list;
870 register struct cmd_list_element *c, *found;
876 /* Skip leading whitespace. */
878 while (**line == ' ' || **line == '\t')
881 /* Clear out trailing whitespace. */
883 p = *line + strlen (*line);
884 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
888 /* Find end of command name. */
891 while (*p == '-' || isalnum(*p))
894 /* Look up the command name.
895 If exact match, keep that.
896 Otherwise, take command abbreviated, if unique. Note that (in my
897 opinion) a null string does *not* indicate ambiguity; simply the
898 end of the argument. */
903 error ("Lack of needed %scommand", cmdtype);
907 /* Copy over to a local buffer, converting to lowercase on the way.
908 This is in case the command being parsed is a subcommand which
909 doesn't match anything, and that's ok. We want the original
910 untouched for the routine of the original command. */
912 processed_cmd = (char *) alloca (p - *line + 1);
913 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
915 char x = (*line)[cmd_len];
917 processed_cmd[cmd_len] = tolower(x);
919 processed_cmd[cmd_len] = x;
921 processed_cmd[cmd_len] = '\0';
923 /* Check all possibilities in the current command list. */
926 for (c = list; c; c = c->next)
928 if (!strncmp (processed_cmd, c->name, cmd_len))
932 if (c->name[cmd_len] == 0)
940 /* Report error for undefined command name. */
944 if (nfound > 1 && allow_unknown >= 0)
947 for (c = list; c; c = c->next)
948 if (!strncmp (processed_cmd, c->name, cmd_len))
950 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
953 strcat (ambbuf, ", ");
954 strcat (ambbuf, c->name);
958 strcat (ambbuf, "..");
962 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
963 processed_cmd, ambbuf);
965 else if (!allow_unknown)
966 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
970 /* Skip whitespace before the argument. */
972 while (*p == ' ' || *p == '\t') p++;
975 if (found->prefixlist && *p)
977 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
978 found->allow_unknown);
987 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
989 /* Return a vector of char pointers which point to the different
990 possible completions in LIST of TEXT.
992 WORD points in the same buffer as TEXT, and completions should be
993 returned relative to this position. For example, suppose TEXT is "foo"
994 and we want to complete to "foobar". If WORD is "oo", return
995 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
998 complete_on_cmdlist (list, text, word)
999 struct cmd_list_element *list;
1003 struct cmd_list_element *ptr;
1005 int sizeof_matchlist;
1007 int textlen = strlen (text);
1009 sizeof_matchlist = 10;
1010 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1013 for (ptr = list; ptr; ptr = ptr->next)
1014 if (!strncmp (ptr->name, text, textlen)
1015 && !ptr->abbrev_flag
1016 && (ptr->function.cfunc
1017 || ptr->prefixlist))
1019 if (matches == sizeof_matchlist)
1021 sizeof_matchlist *= 2;
1022 matchlist = (char **) xrealloc ((char *)matchlist,
1024 * sizeof (char *)));
1027 matchlist[matches] = (char *)
1028 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1030 strcpy (matchlist[matches], ptr->name);
1031 else if (word > text)
1033 /* Return some portion of ptr->name. */
1034 strcpy (matchlist[matches], ptr->name + (word - text));
1038 /* Return some of text plus ptr->name. */
1039 strncpy (matchlist[matches], word, text - word);
1040 matchlist[matches][text - word] = '\0';
1041 strcat (matchlist[matches], ptr->name);
1048 free ((PTR)matchlist);
1053 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1054 * sizeof (char *)));
1055 matchlist[matches] = (char *) 0;
1061 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1063 /* Return a vector of char pointers which point to the different
1064 possible completions in CMD of TEXT.
1066 WORD points in the same buffer as TEXT, and completions should be
1067 returned relative to this position. For example, suppose TEXT is "foo"
1068 and we want to complete to "foobar". If WORD is "oo", return
1069 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1072 complete_on_enum (enumlist, text, word)
1078 int sizeof_matchlist;
1080 int textlen = strlen (text);
1084 sizeof_matchlist = 10;
1085 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1088 for (i = 0; (name = enumlist[i]) != NULL; i++)
1089 if (strncmp (name, text, textlen) == 0)
1091 if (matches == sizeof_matchlist)
1093 sizeof_matchlist *= 2;
1094 matchlist = (char **) xrealloc ((char *)matchlist,
1096 * sizeof (char *)));
1099 matchlist[matches] = (char *)
1100 xmalloc (strlen (word) + strlen (name) + 1);
1102 strcpy (matchlist[matches], name);
1103 else if (word > text)
1105 /* Return some portion of name. */
1106 strcpy (matchlist[matches], name + (word - text));
1110 /* Return some of text plus name. */
1111 strncpy (matchlist[matches], word, text - word);
1112 matchlist[matches][text - word] = '\0';
1113 strcat (matchlist[matches], name);
1120 free ((PTR)matchlist);
1125 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1126 * sizeof (char *)));
1127 matchlist[matches] = (char *) 0;
1134 parse_binary_operation (arg)
1142 length = strlen (arg);
1144 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1147 if (!strncmp (arg, "on", length)
1148 || !strncmp (arg, "1", length)
1149 || !strncmp (arg, "yes", length))
1152 if (!strncmp (arg, "off", length)
1153 || !strncmp (arg, "0", length)
1154 || !strncmp (arg, "no", length))
1158 error ("\"on\" or \"off\" expected.");
1163 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1164 of the argument, and FROM_TTY is nonzero if this command is being entered
1165 directly by the user (i.e. these are just like any other
1166 command). C is the command list element for the command. */
1168 do_setshow_command (arg, from_tty, c)
1171 struct cmd_list_element *c;
1173 if (c->type == set_cmd)
1175 switch (c->var_type)
1186 new = (char *) xmalloc (strlen (arg) + 2);
1188 while ((ch = *p++) != '\000')
1192 /* \ at end of argument is used after spaces
1193 so they won't be lost. */
1194 /* This is obsolete now that we no longer strip
1195 trailing whitespace and actually, the backslash
1196 didn't get here in my test, readline or
1197 something did something funky with a backslash
1198 right before a newline. */
1201 ch = parse_escape (&p);
1203 break; /* C loses */
1211 if (*(p - 1) != '\\')
1215 new = (char *) xrealloc (new, q - new);
1216 if (*(char **)c->var != NULL)
1217 free (*(char **)c->var);
1218 *(char **) c->var = new;
1221 case var_string_noescape:
1224 if (*(char **)c->var != NULL)
1225 free (*(char **)c->var);
1226 *(char **) c->var = savestring (arg, strlen (arg));
1230 error_no_arg ("filename to set it to.");
1231 if (*(char **)c->var != NULL)
1232 free (*(char **)c->var);
1233 *(char **)c->var = tilde_expand (arg);
1236 *(int *) c->var = parse_binary_operation (arg);
1240 error_no_arg ("integer to set it to.");
1241 *(unsigned int *) c->var = parse_and_eval_address (arg);
1242 if (*(unsigned int *) c->var == 0)
1243 *(unsigned int *) c->var = UINT_MAX;
1249 error_no_arg ("integer to set it to.");
1250 val = parse_and_eval_address (arg);
1252 *(int *) c->var = INT_MAX;
1253 else if (val >= INT_MAX)
1254 error ("integer %u out of range", val);
1256 *(int *) c->var = val;
1261 error_no_arg ("integer to set it to.");
1262 *(int *) c->var = parse_and_eval_address (arg);
1272 p = strchr (arg, ' ');
1280 for (i = 0; c->enums[i]; i++)
1281 if (strncmp (arg, c->enums[i], len) == 0)
1283 match = c->enums[i];
1288 error ("Undefined item: \"%s\".", arg);
1291 error ("Ambiguous item \"%s\".", arg);
1293 *(char **)c->var = match;
1297 error ("gdb internal error: bad var_type in do_setshow_command");
1300 else if (c->type == show_cmd)
1302 /* Print doc minus "show" at start. */
1303 print_doc_line (gdb_stdout, c->doc + 5);
1305 fputs_filtered (" is ", gdb_stdout);
1307 switch (c->var_type)
1313 fputs_filtered ("\"", gdb_stdout);
1314 if (*(unsigned char **)c->var)
1315 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1316 gdb_printchar (*p, gdb_stdout, '"');
1317 fputs_filtered ("\"", gdb_stdout);
1320 case var_string_noescape:
1323 fputs_filtered ("\"", gdb_stdout);
1324 if (*(char **)c->var)
1325 fputs_filtered (*(char **) c->var, gdb_stdout);
1326 fputs_filtered ("\"", gdb_stdout);
1329 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1332 if (*(unsigned int *) c->var == UINT_MAX) {
1333 fputs_filtered ("unlimited", gdb_stdout);
1336 /* else fall through */
1338 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1341 if (*(int *) c->var == INT_MAX)
1343 fputs_filtered ("unlimited", gdb_stdout);
1346 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1350 error ("gdb internal error: bad var_type in do_setshow_command");
1352 fputs_filtered (".\n", gdb_stdout);
1355 error ("gdb internal error: bad cmd_type in do_setshow_command");
1356 (*c->function.sfunc) (NULL, from_tty, c);
1359 /* Show all the settings in a list of show commands. */
1362 cmd_show_list (list, from_tty, prefix)
1363 struct cmd_list_element *list;
1367 for (; list != NULL; list = list->next) {
1368 /* If we find a prefix, run its list, prefixing our output by its
1369 prefix (with "show " skipped). */
1370 if (list->prefixlist && !list->abbrev_flag)
1371 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1372 if (list->type == show_cmd)
1374 fputs_filtered (prefix, gdb_stdout);
1375 fputs_filtered (list->name, gdb_stdout);
1376 fputs_filtered (": ", gdb_stdout);
1377 do_setshow_command ((char *)NULL, from_tty, list);
1384 shell_escape (arg, from_tty)
1389 /* FIXME: what about errors (I don't know how GO32 system() handles
1392 #else /* Can fork. */
1393 int rc, status, pid;
1394 char *p, *user_shell;
1396 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1397 user_shell = "/bin/sh";
1399 /* Get the name of the shell for arg0 */
1400 if ((p = strrchr (user_shell, '/')) == NULL)
1403 p++; /* Get past '/' */
1405 if ((pid = fork()) == 0)
1408 execl (user_shell, p, 0);
1410 execl (user_shell, p, "-c", arg, 0);
1412 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1413 safe_strerror (errno));
1414 gdb_flush (gdb_stderr);
1419 while ((rc = wait (&status)) != pid && rc != -1)
1422 error ("Fork failed");
1423 #endif /* Can fork. */
1427 make_command (arg, from_tty)
1437 p = xmalloc (sizeof("make ") + strlen(arg));
1438 strcpy (p, "make ");
1439 strcpy (p + sizeof("make ")-1, arg);
1442 shell_escape (p, from_tty);
1446 show_user_1 (c, stream)
1447 struct cmd_list_element *c;
1450 register struct command_line *cmdlines;
1452 cmdlines = c->user_commands;
1455 fputs_filtered ("User command ", stream);
1456 fputs_filtered (c->name, stream);
1457 fputs_filtered (":\n", stream);
1461 print_command_line (cmdlines, 4);
1462 cmdlines = cmdlines->next;
1464 fputs_filtered ("\n", stream);
1469 show_user (args, from_tty)
1473 struct cmd_list_element *c;
1474 extern struct cmd_list_element *cmdlist;
1478 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1479 if (c->class != class_user)
1480 error ("Not a user command.");
1481 show_user_1 (c, gdb_stdout);
1485 for (c = cmdlist; c; c = c->next)
1487 if (c->class == class_user)
1488 show_user_1 (c, gdb_stdout);
1494 _initialize_command ()
1496 add_com ("shell", class_support, shell_escape,
1497 "Execute the rest of the line as a shell command. \n\
1498 With no arguments, run an inferior shell.");
1499 add_com ("make", class_support, make_command,
1500 "Run the ``make'' program using the rest of the line as arguments.");
1501 add_cmd ("user", no_class, show_user,
1502 "Show definitions of user defined commands.\n\
1503 Argument is the name of the user defined command.\n\
1504 With no argument, show definitions of all user defined commands.", &showlist);