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. */
28 /* Prototypes for local functions */
31 undef_cmd_error PARAMS ((char *, char *));
34 show_user PARAMS ((char *, int));
37 show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
40 make_command PARAMS ((char *, int));
43 shell_escape PARAMS ((char *, int));
46 parse_binary_operation PARAMS ((char *));
49 print_doc_line PARAMS ((GDB_FILE *, char *));
51 /* Add element named NAME.
52 CLASS is the top level category into which commands are broken down
54 FUN should be the function to execute the command;
55 it will get a character string as argument, with leading
56 and trailing blanks already eliminated.
58 DOC is a documentation string for the command.
59 Its first line should be a complete sentence.
60 It should start with ? for a command that is an abbreviation
61 or with * for a command that most users don't need to know about.
63 Add this command to command list *LIST. */
65 struct cmd_list_element *
66 add_cmd (name, class, fun, doc, list)
68 enum command_class class;
69 void (*fun) PARAMS ((char *, int));
71 struct cmd_list_element **list;
73 register struct cmd_list_element *c
74 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
76 delete_cmd (name, list);
80 c->function.cfunc = fun;
83 c->prefixname = (char *)NULL;
89 c->type = not_set_cmd;
90 c->completer = make_symbol_completion_list;
92 c->var_type = var_boolean;
98 /* Same as above, except that the abbrev_flag is set. */
100 #if 0 /* Currently unused */
102 struct cmd_list_element *
103 add_abbrev_cmd (name, class, fun, doc, list)
105 enum command_class class;
106 void (*fun) PARAMS ((char *, int));
108 struct cmd_list_element **list;
110 register struct cmd_list_element *c
111 = add_cmd (name, class, fun, doc, list);
119 struct cmd_list_element *
120 add_alias_cmd (name, oldname, class, abbrev_flag, list)
123 enum command_class class;
125 struct cmd_list_element **list;
127 /* Must do this since lookup_cmd tries to side-effect its first arg */
129 register struct cmd_list_element *old;
130 register struct cmd_list_element *c;
131 copied_name = (char *) alloca (strlen (oldname) + 1);
132 strcpy (copied_name, oldname);
133 old = lookup_cmd (&copied_name, *list, "", 1, 1);
137 delete_cmd (name, list);
141 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
142 c->prefixlist = old->prefixlist;
143 c->prefixname = old->prefixname;
144 c->allow_unknown = old->allow_unknown;
145 c->abbrev_flag = abbrev_flag;
146 c->cmd_pointer = old;
150 /* Like add_cmd but adds an element for a command prefix:
151 a name that should be followed by a subcommand to be looked up
152 in another command list. PREFIXLIST should be the address
153 of the variable containing that list. */
155 struct cmd_list_element *
156 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
159 enum command_class class;
160 void (*fun) PARAMS ((char *, int));
162 struct cmd_list_element **prefixlist;
165 struct cmd_list_element **list;
167 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
168 c->prefixlist = prefixlist;
169 c->prefixname = prefixname;
170 c->allow_unknown = allow_unknown;
174 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
176 struct cmd_list_element *
177 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
180 enum command_class class;
181 void (*fun) PARAMS ((char *, int));
183 struct cmd_list_element **prefixlist;
186 struct cmd_list_element **list;
188 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
189 c->prefixlist = prefixlist;
190 c->prefixname = prefixname;
191 c->allow_unknown = allow_unknown;
196 /* This is an empty "cfunc". */
198 not_just_help_class_command (args, from_tty)
204 /* This is an empty "sfunc". */
205 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
208 empty_sfunc (args, from_tty, c)
211 struct cmd_list_element *c;
215 /* Add element named NAME to command list LIST (the list for set
216 or some sublist thereof).
217 CLASS is as in add_cmd.
218 VAR_TYPE is the kind of thing we are setting.
219 VAR is address of the variable being controlled by this command.
220 DOC is the documentation string. */
222 struct cmd_list_element *
223 add_set_cmd (name, class, var_type, var, doc, list)
225 enum command_class class;
229 struct cmd_list_element **list;
231 struct cmd_list_element *c
232 = add_cmd (name, class, NO_FUNCTION, doc, list);
235 c->var_type = var_type;
237 /* This needs to be something besides NO_FUNCTION so that this isn't
238 treated as a help class. */
239 c->function.sfunc = empty_sfunc;
243 /* Add element named NAME to command list LIST (the list for set
244 or some sublist thereof).
245 CLASS is as in add_cmd.
246 ENUMLIST is a list of strings which may follow NAME.
247 VAR is address of the variable which will contain the matching string
249 DOC is the documentation string. */
251 struct cmd_list_element *
252 add_set_enum_cmd (name, class, enumlist, var, doc, list)
254 enum command_class class;
258 struct cmd_list_element **list;
260 struct cmd_list_element *c
261 = add_set_cmd (name, class, var_enum, var, doc, list);
268 /* Where SETCMD has already been added, add the corresponding show
269 command to LIST and return a pointer to it. */
270 struct cmd_list_element *
271 add_show_from_set (setcmd, list)
272 struct cmd_list_element *setcmd;
273 struct cmd_list_element **list;
275 struct cmd_list_element *showcmd =
276 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
278 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
279 delete_cmd (showcmd->name, list);
280 showcmd->type = show_cmd;
282 /* Replace "set " at start of docstring with "show ". */
283 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
284 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
285 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
287 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
289 showcmd->next = *list;
294 /* Remove the command named NAME from the command list. */
297 delete_cmd (name, list)
299 struct cmd_list_element **list;
301 register struct cmd_list_element *c;
302 struct cmd_list_element *p;
304 while (*list && STREQ ((*list)->name, name))
307 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
314 for (c = *list; c->next;)
316 if (STREQ (c->next->name, name))
319 c->next->hookee->hook = 0; /* hooked cmd gets away. */
329 /* This command really has to deal with two things:
330 * 1) I want documentation on *this string* (usually called by
331 * "help commandname").
332 * 2) I want documentation on *this list* (usually called by
333 * giving a command that requires subcommands. Also called by saying
336 * I am going to split this into two seperate comamnds, help_cmd and
341 help_cmd (command, stream)
345 struct cmd_list_element *c;
346 extern struct cmd_list_element *cmdlist;
350 help_list (cmdlist, "", all_classes, stream);
354 c = lookup_cmd (&command, cmdlist, "", 0, 0);
359 /* There are three cases here.
360 If c->prefixlist is nonzero, we have a prefix command.
361 Print its documentation, then list its subcommands.
363 If c->function is nonzero, we really have a command.
364 Print its documentation and return.
366 If c->function is zero, we have a class name.
367 Print its documentation (as if it were a command)
368 and then set class to the number of this class
369 so that the commands in the class will be listed. */
371 fputs_filtered (c->doc, stream);
372 fputs_filtered ("\n", stream);
374 if (c->prefixlist == 0 && c->function.cfunc != NULL)
376 fprintf_filtered (stream, "\n");
378 /* If this is a prefix command, print it's subcommands */
380 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
382 /* If this is a class name, print all of the commands in the class */
383 if (c->function.cfunc == NULL)
384 help_list (cmdlist, "", c->class, stream);
387 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
392 * Get a specific kind of help on a command list.
395 * CMDTYPE is the prefix to use in the title string.
396 * CLASS is the class with which to list the nodes of this list (see
397 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
398 * everything, ALL_CLASSES for just classes, and non-negative for only things
399 * in a specific class.
400 * and STREAM is the output stream on which to print things.
401 * If you call this routine with a class >= 0, it recurses.
404 help_list (list, cmdtype, class, stream)
405 struct cmd_list_element *list;
407 enum command_class class;
411 char *cmdtype1, *cmdtype2;
413 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
414 len = strlen (cmdtype);
415 cmdtype1 = (char *) alloca (len + 1);
417 cmdtype2 = (char *) alloca (len + 4);
422 strncpy (cmdtype1 + 1, cmdtype, len - 1);
424 strncpy (cmdtype2, cmdtype, len - 1);
425 strcpy (cmdtype2 + len - 1, " sub");
428 if (class == all_classes)
429 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
431 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
433 help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
435 if (class == all_classes)
436 fprintf_filtered (stream, "\n\
437 Type \"help%s\" followed by a class name for a list of commands in that class.",
440 fprintf_filtered (stream, "\n\
441 Type \"help%s\" followed by %scommand name for full documentation.\n\
442 Command name abbreviations are allowed if unambiguous.\n",
446 /* Print only the first line of STR on STREAM. */
448 print_doc_line (stream, str)
452 static char *line_buffer = 0;
453 static int line_size;
459 line_buffer = (char *) xmalloc (line_size);
463 while (*p && *p != '\n' && *p != '.' && *p != ',')
465 if (p - str > line_size - 1)
467 line_size = p - str + 1;
468 free ((PTR)line_buffer);
469 line_buffer = (char *) xmalloc (line_size);
471 strncpy (line_buffer, str, p - str);
472 line_buffer[p - str] = '\0';
473 if (islower (line_buffer[0]))
474 line_buffer[0] = toupper (line_buffer[0]);
475 fputs_filtered (line_buffer, stream);
479 * Implement a help command on command list LIST.
480 * RECURSE should be non-zero if this should be done recursively on
481 * all sublists of LIST.
482 * PREFIX is the prefix to print before each command name.
483 * STREAM is the stream upon which the output should be written.
485 * A non-negative class number to list only commands in that
487 * ALL_COMMANDS to list all commands in list.
488 * ALL_CLASSES to list all classes in list.
490 * Note that RECURSE will be active on *all* sublists, not just the
491 * ones selected by the criteria above (ie. the selection mechanism
492 * is at the low level, not the high-level).
495 help_cmd_list (list, class, prefix, recurse, stream)
496 struct cmd_list_element *list;
497 enum command_class class;
502 register struct cmd_list_element *c;
504 for (c = list; c; c = c->next)
506 if (c->abbrev_flag == 0 &&
507 (class == all_commands
508 || (class == all_classes && c->function.cfunc == NULL)
509 || (class == c->class && c->function.cfunc != NULL)))
511 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
512 print_doc_line (stream, c->doc);
513 fputs_filtered ("\n", stream);
516 && c->prefixlist != 0
517 && c->abbrev_flag == 0)
518 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
522 /* This routine takes a line of TEXT and a CLIST in which to start the
523 lookup. When it returns it will have incremented the text pointer past
524 the section of text it matched, set *RESULT_LIST to point to the list in
525 which the last word was matched, and will return a pointer to the cmd
526 list element which the text matches. It will return NULL if no match at
527 all was possible. It will return -1 (cast appropriately, ick) if ambigous
528 matches are possible; in this case *RESULT_LIST will be set to point to
529 the list in which there are ambiguous choices (and *TEXT will be set to
530 the ambiguous text string).
532 If the located command was an abbreviation, this routine returns the base
533 command of the abbreviation.
535 It does no error reporting whatsoever; control will always return
536 to the superior routine.
538 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
539 at the prefix_command (ie. the best match) *or* (special case) will be NULL
540 if no prefix command was ever found. For example, in the case of "info a",
541 "info" matches without ambiguity, but "a" could be "args" or "address", so
542 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
543 RESULT_LIST should not be interpeted as a pointer to the beginning of a
544 list; it simply points to a specific command. In the case of an ambiguous
545 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
546 "info t" can be "info types" or "info target"; upon return *TEXT has been
547 advanced past "info ").
549 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
550 affect the operation).
552 This routine does *not* modify the text pointed to by TEXT.
554 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
555 are actually help classes rather than commands (i.e. the function field of
556 the struct cmd_list_element is NULL). */
558 struct cmd_list_element *
559 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
561 struct cmd_list_element *clist, **result_list;
562 int ignore_help_classes;
565 int len, tmp, nfound;
566 struct cmd_list_element *found, *c;
568 while (**text == ' ' || **text == '\t')
571 /* Treating underscores as part of command words is important
572 so that "set args_foo()" doesn't get interpreted as
573 "set args _foo()". */
575 *p && (isalnum(*p) || *p == '-' || *p == '_');
579 /* If nothing but whitespace, return 0. */
585 /* *text and p now bracket the first command word to lookup (and
586 it's length is len). We copy this into a local temporary,
587 converting to lower case as we go. */
589 command = (char *) alloca (len + 1);
590 for (tmp = 0; tmp < len; tmp++)
592 char x = (*text)[tmp];
593 command[tmp] = isupper(x) ? tolower(x) : x;
600 for (c = clist; c; c = c->next)
601 if (!strncmp (command, c->name, len)
602 && (!ignore_help_classes || c->function.cfunc))
606 if (c->name[len] == '\0')
613 /* If nothing matches, we have a simple failure. */
619 if (result_list != NULL)
620 /* Will be modified in calling routine
621 if we know what the prefix command is. */
623 return (struct cmd_list_element *) -1; /* Ambiguous. */
626 /* We've matched something on this list. Move text pointer forward. */
630 /* If this was an abbreviation, use the base command instead. */
632 if (found->cmd_pointer)
633 found = found->cmd_pointer;
635 /* If we found a prefix command, keep looking. */
637 if (found->prefixlist)
639 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
640 ignore_help_classes);
643 /* Didn't find anything; this is as far as we got. */
644 if (result_list != NULL)
645 *result_list = clist;
648 else if (c == (struct cmd_list_element *) -1)
650 /* We've gotten this far properley, but the next step
651 is ambiguous. We need to set the result list to the best
652 we've found (if an inferior hasn't already set it). */
653 if (result_list != NULL)
655 /* This used to say *result_list = *found->prefixlist
656 If that was correct, need to modify the documentation
657 at the top of this function to clarify what is supposed
659 *result_list = found;
670 if (result_list != NULL)
671 *result_list = clist;
676 /* All this hair to move the space to the front of cmdtype */
679 undef_cmd_error (cmdtype, q)
682 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
690 /* Look up the contents of *LINE as a command in the command list LIST.
691 LIST is a chain of struct cmd_list_element's.
692 If it is found, return the struct cmd_list_element for that command
693 and update *LINE to point after the command name, at the first argument.
694 If not found, call error if ALLOW_UNKNOWN is zero
695 otherwise (or if error returns) return zero.
696 Call error if specified command is ambiguous,
697 unless ALLOW_UNKNOWN is negative.
698 CMDTYPE precedes the word "command" in the error message.
700 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
701 elements which are actually help classes rather than commands (i.e.
702 the function field of the struct cmd_list_element is 0). */
704 struct cmd_list_element *
705 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
707 struct cmd_list_element *list;
710 int ignore_help_classes;
712 struct cmd_list_element *last_list = 0;
713 struct cmd_list_element *c =
714 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
716 /* This is wrong for complete_command. */
717 char *ptr = (*line) + strlen (*line) - 1;
719 /* Clear off trailing whitespace. */
720 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
730 error ("Lack of needed %scommand", cmdtype);
735 while (isalnum(*p) || *p == '-')
738 q = (char *) alloca (p - *line + 1);
739 strncpy (q, *line, p - *line);
741 undef_cmd_error (cmdtype, q);
747 else if (c == (struct cmd_list_element *) -1)
749 /* Ambigous. Local values should be off prefixlist or called
751 int local_allow_unknown = (last_list ? last_list->allow_unknown :
753 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
754 struct cmd_list_element *local_list =
755 (last_list ? *(last_list->prefixlist) : list);
757 if (local_allow_unknown < 0)
760 return last_list; /* Found something. */
762 return 0; /* Found nothing. */
766 /* Report as error. */
771 ((*line)[amb_len] && (*line)[amb_len] != ' '
772 && (*line)[amb_len] != '\t');
777 for (c = local_list; c; c = c->next)
778 if (!strncmp (*line, c->name, amb_len))
780 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
783 strcat (ambbuf, ", ");
784 strcat (ambbuf, c->name);
788 strcat (ambbuf, "..");
792 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
799 /* We've got something. It may still not be what the caller
800 wants (if this command *needs* a subcommand). */
801 while (**line == ' ' || **line == '\t')
804 if (c->prefixlist && **line && !c->allow_unknown)
805 undef_cmd_error (c->prefixname, *line);
807 /* Seems to be what he wants. Return it. */
814 /* Look up the contents of *LINE as a command in the command list LIST.
815 LIST is a chain of struct cmd_list_element's.
816 If it is found, return the struct cmd_list_element for that command
817 and update *LINE to point after the command name, at the first argument.
818 If not found, call error if ALLOW_UNKNOWN is zero
819 otherwise (or if error returns) return zero.
820 Call error if specified command is ambiguous,
821 unless ALLOW_UNKNOWN is negative.
822 CMDTYPE precedes the word "command" in the error message. */
824 struct cmd_list_element *
825 lookup_cmd (line, list, cmdtype, allow_unknown)
827 struct cmd_list_element *list;
832 register struct cmd_list_element *c, *found;
838 /* Skip leading whitespace. */
840 while (**line == ' ' || **line == '\t')
843 /* Clear out trailing whitespace. */
845 p = *line + strlen (*line);
846 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
850 /* Find end of command name. */
853 while (*p == '-' || isalnum(*p))
856 /* Look up the command name.
857 If exact match, keep that.
858 Otherwise, take command abbreviated, if unique. Note that (in my
859 opinion) a null string does *not* indicate ambiguity; simply the
860 end of the argument. */
865 error ("Lack of needed %scommand", cmdtype);
869 /* Copy over to a local buffer, converting to lowercase on the way.
870 This is in case the command being parsed is a subcommand which
871 doesn't match anything, and that's ok. We want the original
872 untouched for the routine of the original command. */
874 processed_cmd = (char *) alloca (p - *line + 1);
875 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
877 char x = (*line)[cmd_len];
879 processed_cmd[cmd_len] = tolower(x);
881 processed_cmd[cmd_len] = x;
883 processed_cmd[cmd_len] = '\0';
885 /* Check all possibilities in the current command list. */
888 for (c = list; c; c = c->next)
890 if (!strncmp (processed_cmd, c->name, cmd_len))
894 if (c->name[cmd_len] == 0)
902 /* Report error for undefined command name. */
906 if (nfound > 1 && allow_unknown >= 0)
909 for (c = list; c; c = c->next)
910 if (!strncmp (processed_cmd, c->name, cmd_len))
912 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
915 strcat (ambbuf, ", ");
916 strcat (ambbuf, c->name);
920 strcat (ambbuf, "..");
924 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
925 processed_cmd, ambbuf);
927 else if (!allow_unknown)
928 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
932 /* Skip whitespace before the argument. */
934 while (*p == ' ' || *p == '\t') p++;
937 if (found->prefixlist && *p)
939 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
940 found->allow_unknown);
949 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
951 /* Return a vector of char pointers which point to the different
952 possible completions in LIST of TEXT.
954 WORD points in the same buffer as TEXT, and completions should be
955 returned relative to this position. For example, suppose TEXT is "foo"
956 and we want to complete to "foobar". If WORD is "oo", return
957 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
960 complete_on_cmdlist (list, text, word)
961 struct cmd_list_element *list;
965 struct cmd_list_element *ptr;
967 int sizeof_matchlist;
969 int textlen = strlen (text);
971 sizeof_matchlist = 10;
972 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
975 for (ptr = list; ptr; ptr = ptr->next)
976 if (!strncmp (ptr->name, text, textlen)
978 && (ptr->function.cfunc
981 if (matches == sizeof_matchlist)
983 sizeof_matchlist *= 2;
984 matchlist = (char **) xrealloc ((char *)matchlist,
989 matchlist[matches] = (char *)
990 xmalloc (strlen (word) + strlen (ptr->name) + 1);
992 strcpy (matchlist[matches], ptr->name);
993 else if (word > text)
995 /* Return some portion of ptr->name. */
996 strcpy (matchlist[matches], ptr->name + (word - text));
1000 /* Return some of text plus ptr->name. */
1001 strncpy (matchlist[matches], word, text - word);
1002 matchlist[matches][text - word] = '\0';
1003 strcat (matchlist[matches], ptr->name);
1010 free ((PTR)matchlist);
1015 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1016 * sizeof (char *)));
1017 matchlist[matches] = (char *) 0;
1023 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1025 /* Return a vector of char pointers which point to the different
1026 possible completions in CMD of TEXT.
1028 WORD points in the same buffer as TEXT, and completions should be
1029 returned relative to this position. For example, suppose TEXT is "foo"
1030 and we want to complete to "foobar". If WORD is "oo", return
1031 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1034 complete_on_enum (enumlist, text, word)
1040 int sizeof_matchlist;
1042 int textlen = strlen (text);
1046 sizeof_matchlist = 10;
1047 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1050 for (i = 0; name = enumlist[i]; i++)
1051 if (strncmp (name, text, textlen) == 0)
1053 if (matches == sizeof_matchlist)
1055 sizeof_matchlist *= 2;
1056 matchlist = (char **) xrealloc ((char *)matchlist,
1058 * sizeof (char *)));
1061 matchlist[matches] = (char *)
1062 xmalloc (strlen (word) + strlen (name) + 1);
1064 strcpy (matchlist[matches], name);
1065 else if (word > text)
1067 /* Return some portion of name. */
1068 strcpy (matchlist[matches], name + (word - text));
1072 /* Return some of text plus name. */
1073 strncpy (matchlist[matches], word, text - word);
1074 matchlist[matches][text - word] = '\0';
1075 strcat (matchlist[matches], name);
1082 free ((PTR)matchlist);
1087 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1088 * sizeof (char *)));
1089 matchlist[matches] = (char *) 0;
1096 parse_binary_operation (arg)
1104 length = strlen (arg);
1106 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1109 if (!strncmp (arg, "on", length)
1110 || !strncmp (arg, "1", length)
1111 || !strncmp (arg, "yes", length))
1114 if (!strncmp (arg, "off", length)
1115 || !strncmp (arg, "0", length)
1116 || !strncmp (arg, "no", length))
1120 error ("\"on\" or \"off\" expected.");
1125 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1126 of the argument, and FROM_TTY is nonzero if this command is being entered
1127 directly by the user (i.e. these are just like any other
1128 command). C is the command list element for the command. */
1130 do_setshow_command (arg, from_tty, c)
1133 struct cmd_list_element *c;
1135 if (c->type == set_cmd)
1137 switch (c->var_type)
1148 new = (char *) xmalloc (strlen (arg) + 2);
1150 while ((ch = *p++) != '\000')
1154 /* \ at end of argument is used after spaces
1155 so they won't be lost. */
1156 /* This is obsolete now that we no longer strip
1157 trailing whitespace and actually, the backslash
1158 didn't get here in my test, readline or
1159 something did something funky with a backslash
1160 right before a newline. */
1163 ch = parse_escape (&p);
1165 break; /* C loses */
1173 if (*(p - 1) != '\\')
1177 new = (char *) xrealloc (new, q - new);
1178 if (*(char **)c->var != NULL)
1179 free (*(char **)c->var);
1180 *(char **) c->var = new;
1183 case var_string_noescape:
1186 if (*(char **)c->var != NULL)
1187 free (*(char **)c->var);
1188 *(char **) c->var = savestring (arg, strlen (arg));
1192 error_no_arg ("filename to set it to.");
1193 if (*(char **)c->var != NULL)
1194 free (*(char **)c->var);
1195 *(char **)c->var = tilde_expand (arg);
1198 *(int *) c->var = parse_binary_operation (arg);
1202 error_no_arg ("integer to set it to.");
1203 *(unsigned int *) c->var = parse_and_eval_address (arg);
1204 if (*(unsigned int *) c->var == 0)
1205 *(unsigned int *) c->var = UINT_MAX;
1211 error_no_arg ("integer to set it to.");
1212 val = parse_and_eval_address (arg);
1214 *(int *) c->var = INT_MAX;
1215 else if (val >= INT_MAX)
1216 error ("integer %u out of range", val);
1218 *(int *) c->var = val;
1223 error_no_arg ("integer to set it to.");
1224 *(int *) c->var = parse_and_eval_address (arg);
1234 p = strchr (arg, ' ');
1242 for (i = 0; c->enums[i]; i++)
1243 if (strncmp (arg, c->enums[i], len) == 0)
1245 match = c->enums[i];
1250 error ("Undefined item: \"%s\".", arg);
1253 error ("Ambiguous item \"%s\".", arg);
1255 *(char **)c->var = match;
1259 error ("gdb internal error: bad var_type in do_setshow_command");
1262 else if (c->type == show_cmd)
1264 /* Print doc minus "show" at start. */
1265 print_doc_line (gdb_stdout, c->doc + 5);
1267 fputs_filtered (" is ", gdb_stdout);
1269 switch (c->var_type)
1274 fputs_filtered ("\"", gdb_stdout);
1275 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1276 gdb_printchar (*p, gdb_stdout, '"');
1277 fputs_filtered ("\"", gdb_stdout);
1280 case var_string_noescape:
1283 fputs_filtered ("\"", gdb_stdout);
1284 fputs_filtered (*(char **) c->var, gdb_stdout);
1285 fputs_filtered ("\"", gdb_stdout);
1288 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1291 if (*(unsigned int *) c->var == UINT_MAX) {
1292 fputs_filtered ("unlimited", gdb_stdout);
1295 /* else fall through */
1297 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1300 if (*(int *) c->var == INT_MAX)
1302 fputs_filtered ("unlimited", gdb_stdout);
1305 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1309 error ("gdb internal error: bad var_type in do_setshow_command");
1311 fputs_filtered (".\n", gdb_stdout);
1314 error ("gdb internal error: bad cmd_type in do_setshow_command");
1315 (*c->function.sfunc) (NULL, from_tty, c);
1318 /* Show all the settings in a list of show commands. */
1321 cmd_show_list (list, from_tty, prefix)
1322 struct cmd_list_element *list;
1326 for (; list != NULL; list = list->next) {
1327 /* If we find a prefix, run its list, prefixing our output by its
1328 prefix (with "show " skipped). */
1329 if (list->prefixlist && !list->abbrev_flag)
1330 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1331 if (list->type == show_cmd)
1333 fputs_filtered (prefix, gdb_stdout);
1334 fputs_filtered (list->name, gdb_stdout);
1335 fputs_filtered (": ", gdb_stdout);
1336 do_setshow_command ((char *)NULL, from_tty, list);
1343 shell_escape (arg, from_tty)
1348 /* FIXME: what about errors (I don't know how GO32 system() handles
1351 #else /* Can fork. */
1352 int rc, status, pid;
1353 char *p, *user_shell;
1355 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1356 user_shell = "/bin/sh";
1358 /* Get the name of the shell for arg0 */
1359 if ((p = strrchr (user_shell, '/')) == NULL)
1362 p++; /* Get past '/' */
1364 if ((pid = fork()) == 0)
1367 execl (user_shell, p, 0);
1369 execl (user_shell, p, "-c", arg, 0);
1371 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1372 safe_strerror (errno));
1373 gdb_flush (gdb_stderr);
1378 while ((rc = wait (&status)) != pid && rc != -1)
1381 error ("Fork failed");
1382 #endif /* Can fork. */
1386 make_command (arg, from_tty)
1396 p = xmalloc (sizeof("make ") + strlen(arg));
1397 strcpy (p, "make ");
1398 strcpy (p + sizeof("make ")-1, arg);
1401 shell_escape (p, from_tty);
1405 show_user_1 (c, stream)
1406 struct cmd_list_element *c;
1409 register struct command_line *cmdlines;
1411 cmdlines = c->user_commands;
1414 fputs_filtered ("User command ", stream);
1415 fputs_filtered (c->name, stream);
1416 fputs_filtered (":\n", stream);
1420 print_command_line (cmdlines, 4);
1421 cmdlines = cmdlines->next;
1423 fputs_filtered ("\n", stream);
1428 show_user (args, from_tty)
1432 struct cmd_list_element *c;
1433 extern struct cmd_list_element *cmdlist;
1437 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1438 if (c->class != class_user)
1439 error ("Not a user command.");
1440 show_user_1 (c, gdb_stdout);
1444 for (c = cmdlist; c; c = c->next)
1446 if (c->class == class_user)
1447 show_user_1 (c, gdb_stdout);
1453 _initialize_command ()
1455 add_com ("shell", class_support, shell_escape,
1456 "Execute the rest of the line as a shell command. \n\
1457 With no arguments, run an inferior shell.");
1458 add_com ("make", class_support, make_command,
1459 "Run the ``make'' program using the rest of the line as arguments.");
1460 add_cmd ("user", no_class, show_user,
1461 "Show definitions of user defined commands.\n\
1462 Argument is the name of the user defined command.\n\
1463 With no argument, show definitions of all user defined commands.", &showlist);