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 struct cmd_list_element *
67 add_cmd (name, class, fun, doc, list)
69 enum command_class class;
70 void (*fun) PARAMS ((char *, int));
72 struct cmd_list_element **list;
74 register struct cmd_list_element *c
75 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
77 delete_cmd (name, list);
81 c->function.cfunc = fun;
88 c->completer = make_symbol_completion_list;
89 c->type = not_set_cmd;
91 c->var_type = var_boolean;
93 c->user_commands = NULL;
95 c->cmd_pointer = NULL;
100 /* Same as above, except that the abbrev_flag is set. */
102 #if 0 /* Currently unused */
104 struct cmd_list_element *
105 add_abbrev_cmd (name, class, fun, doc, list)
107 enum command_class class;
108 void (*fun) PARAMS ((char *, int));
110 struct cmd_list_element **list;
112 register struct cmd_list_element *c
113 = add_cmd (name, class, fun, doc, list);
121 struct cmd_list_element *
122 add_alias_cmd (name, oldname, class, abbrev_flag, list)
125 enum command_class class;
127 struct cmd_list_element **list;
129 /* Must do this since lookup_cmd tries to side-effect its first arg */
131 register struct cmd_list_element *old;
132 register struct cmd_list_element *c;
133 copied_name = (char *) alloca (strlen (oldname) + 1);
134 strcpy (copied_name, oldname);
135 old = lookup_cmd (&copied_name, *list, "", 1, 1);
139 delete_cmd (name, list);
143 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
144 c->prefixlist = old->prefixlist;
145 c->prefixname = old->prefixname;
146 c->allow_unknown = old->allow_unknown;
147 c->abbrev_flag = abbrev_flag;
148 c->cmd_pointer = old;
152 /* Like add_cmd but adds an element for a command prefix:
153 a name that should be followed by a subcommand to be looked up
154 in another command list. PREFIXLIST should be the address
155 of the variable containing that list. */
157 struct cmd_list_element *
158 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
161 enum command_class class;
162 void (*fun) PARAMS ((char *, int));
164 struct cmd_list_element **prefixlist;
167 struct cmd_list_element **list;
169 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
170 c->prefixlist = prefixlist;
171 c->prefixname = prefixname;
172 c->allow_unknown = allow_unknown;
176 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
178 struct cmd_list_element *
179 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
182 enum command_class class;
183 void (*fun) PARAMS ((char *, int));
185 struct cmd_list_element **prefixlist;
188 struct cmd_list_element **list;
190 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
191 c->prefixlist = prefixlist;
192 c->prefixname = prefixname;
193 c->allow_unknown = allow_unknown;
198 /* This is an empty "cfunc". */
200 not_just_help_class_command (args, from_tty)
206 /* This is an empty "sfunc". */
207 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
210 empty_sfunc (args, from_tty, c)
213 struct cmd_list_element *c;
217 /* Add element named NAME to command list LIST (the list for set
218 or some sublist thereof).
219 CLASS is as in add_cmd.
220 VAR_TYPE is the kind of thing we are setting.
221 VAR is address of the variable being controlled by this command.
222 DOC is the documentation string. */
224 struct cmd_list_element *
225 add_set_cmd (name, class, var_type, var, doc, list)
227 enum command_class class;
231 struct cmd_list_element **list;
233 struct cmd_list_element *c
234 = add_cmd (name, class, NO_FUNCTION, doc, list);
237 c->var_type = var_type;
239 /* This needs to be something besides NO_FUNCTION so that this isn't
240 treated as a help class. */
241 c->function.sfunc = empty_sfunc;
245 /* Add element named NAME to command list LIST (the list for set
246 or some sublist thereof).
247 CLASS is as in add_cmd.
248 ENUMLIST is a list of strings which may follow NAME.
249 VAR is address of the variable which will contain the matching string
251 DOC is the documentation string. */
253 struct cmd_list_element *
254 add_set_enum_cmd (name, class, enumlist, var, doc, list)
256 enum command_class class;
260 struct cmd_list_element **list;
262 struct cmd_list_element *c
263 = add_set_cmd (name, class, var_enum, var, doc, list);
270 /* Where SETCMD has already been added, add the corresponding show
271 command to LIST and return a pointer to it. */
272 struct cmd_list_element *
273 add_show_from_set (setcmd, list)
274 struct cmd_list_element *setcmd;
275 struct cmd_list_element **list;
277 struct cmd_list_element *showcmd =
278 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
280 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
281 delete_cmd (showcmd->name, list);
282 showcmd->type = show_cmd;
284 /* Replace "set " at start of docstring with "show ". */
285 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
286 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
287 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
289 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
291 showcmd->next = *list;
296 /* Remove the command named NAME from the command list. */
299 delete_cmd (name, list)
301 struct cmd_list_element **list;
303 register struct cmd_list_element *c;
304 struct cmd_list_element *p;
306 while (*list && STREQ ((*list)->name, name))
309 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
316 for (c = *list; c->next;)
318 if (STREQ (c->next->name, name))
321 c->next->hookee->hook = 0; /* hooked cmd gets away. */
331 /* This command really has to deal with two things:
332 * 1) I want documentation on *this string* (usually called by
333 * "help commandname").
334 * 2) I want documentation on *this list* (usually called by
335 * giving a command that requires subcommands. Also called by saying
338 * I am going to split this into two seperate comamnds, help_cmd and
343 help_cmd (command, stream)
347 struct cmd_list_element *c;
348 extern struct cmd_list_element *cmdlist;
352 help_list (cmdlist, "", all_classes, stream);
356 c = lookup_cmd (&command, cmdlist, "", 0, 0);
361 /* There are three cases here.
362 If c->prefixlist is nonzero, we have a prefix command.
363 Print its documentation, then list its subcommands.
365 If c->function is nonzero, we really have a command.
366 Print its documentation and return.
368 If c->function is zero, we have a class name.
369 Print its documentation (as if it were a command)
370 and then set class to the number of this class
371 so that the commands in the class will be listed. */
373 fputs_filtered (c->doc, stream);
374 fputs_filtered ("\n", stream);
376 if (c->prefixlist == 0 && c->function.cfunc != NULL)
378 fprintf_filtered (stream, "\n");
380 /* If this is a prefix command, print it's subcommands */
382 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
384 /* If this is a class name, print all of the commands in the class */
385 if (c->function.cfunc == NULL)
386 help_list (cmdlist, "", c->class, stream);
389 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
394 * Get a specific kind of help on a command list.
397 * CMDTYPE is the prefix to use in the title string.
398 * CLASS is the class with which to list the nodes of this list (see
399 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
400 * everything, ALL_CLASSES for just classes, and non-negative for only things
401 * in a specific class.
402 * and STREAM is the output stream on which to print things.
403 * If you call this routine with a class >= 0, it recurses.
406 help_list (list, cmdtype, class, stream)
407 struct cmd_list_element *list;
409 enum command_class class;
413 char *cmdtype1, *cmdtype2;
415 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
416 len = strlen (cmdtype);
417 cmdtype1 = (char *) alloca (len + 1);
419 cmdtype2 = (char *) alloca (len + 4);
424 strncpy (cmdtype1 + 1, cmdtype, len - 1);
426 strncpy (cmdtype2, cmdtype, len - 1);
427 strcpy (cmdtype2 + len - 1, " sub");
430 if (class == all_classes)
431 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
433 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
435 help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
437 if (class == all_classes)
438 fprintf_filtered (stream, "\n\
439 Type \"help%s\" followed by a class name for a list of commands in that class.",
442 fprintf_filtered (stream, "\n\
443 Type \"help%s\" followed by %scommand name for full documentation.\n\
444 Command name abbreviations are allowed if unambiguous.\n",
448 /* Print only the first line of STR on STREAM. */
450 print_doc_line (stream, str)
454 static char *line_buffer = 0;
455 static int line_size;
461 line_buffer = (char *) xmalloc (line_size);
465 while (*p && *p != '\n' && *p != '.' && *p != ',')
467 if (p - str > line_size - 1)
469 line_size = p - str + 1;
470 free ((PTR)line_buffer);
471 line_buffer = (char *) xmalloc (line_size);
473 strncpy (line_buffer, str, p - str);
474 line_buffer[p - str] = '\0';
475 if (islower (line_buffer[0]))
476 line_buffer[0] = toupper (line_buffer[0]);
477 fputs_filtered (line_buffer, stream);
481 * Implement a help command on command list LIST.
482 * RECURSE should be non-zero if this should be done recursively on
483 * all sublists of LIST.
484 * PREFIX is the prefix to print before each command name.
485 * STREAM is the stream upon which the output should be written.
487 * A non-negative class number to list only commands in that
489 * ALL_COMMANDS to list all commands in list.
490 * ALL_CLASSES to list all classes in list.
492 * Note that RECURSE will be active on *all* sublists, not just the
493 * ones selected by the criteria above (ie. the selection mechanism
494 * is at the low level, not the high-level).
497 help_cmd_list (list, class, prefix, recurse, stream)
498 struct cmd_list_element *list;
499 enum command_class class;
504 register struct cmd_list_element *c;
506 for (c = list; c; c = c->next)
508 if (c->abbrev_flag == 0 &&
509 (class == all_commands
510 || (class == all_classes && c->function.cfunc == NULL)
511 || (class == c->class && c->function.cfunc != NULL)))
513 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
514 print_doc_line (stream, c->doc);
515 fputs_filtered ("\n", stream);
518 && c->prefixlist != 0
519 && c->abbrev_flag == 0)
520 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
524 /* This routine takes a line of TEXT and a CLIST in which to start the
525 lookup. When it returns it will have incremented the text pointer past
526 the section of text it matched, set *RESULT_LIST to point to the list in
527 which the last word was matched, and will return a pointer to the cmd
528 list element which the text matches. It will return NULL if no match at
529 all was possible. It will return -1 (cast appropriately, ick) if ambigous
530 matches are possible; in this case *RESULT_LIST will be set to point to
531 the list in which there are ambiguous choices (and *TEXT will be set to
532 the ambiguous text string).
534 If the located command was an abbreviation, this routine returns the base
535 command of the abbreviation.
537 It does no error reporting whatsoever; control will always return
538 to the superior routine.
540 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
541 at the prefix_command (ie. the best match) *or* (special case) will be NULL
542 if no prefix command was ever found. For example, in the case of "info a",
543 "info" matches without ambiguity, but "a" could be "args" or "address", so
544 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
545 RESULT_LIST should not be interpeted as a pointer to the beginning of a
546 list; it simply points to a specific command. In the case of an ambiguous
547 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
548 "info t" can be "info types" or "info target"; upon return *TEXT has been
549 advanced past "info ").
551 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
552 affect the operation).
554 This routine does *not* modify the text pointed to by TEXT.
556 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
557 are actually help classes rather than commands (i.e. the function field of
558 the struct cmd_list_element is NULL). */
560 struct cmd_list_element *
561 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
563 struct cmd_list_element *clist, **result_list;
564 int ignore_help_classes;
567 int len, tmp, nfound;
568 struct cmd_list_element *found, *c;
570 while (**text == ' ' || **text == '\t')
573 /* Treating underscores as part of command words is important
574 so that "set args_foo()" doesn't get interpreted as
575 "set args _foo()". */
577 *p && (isalnum(*p) || *p == '-' || *p == '_');
581 /* If nothing but whitespace, return 0. */
587 /* *text and p now bracket the first command word to lookup (and
588 it's length is len). We copy this into a local temporary,
589 converting to lower case as we go. */
591 command = (char *) alloca (len + 1);
592 for (tmp = 0; tmp < len; tmp++)
594 char x = (*text)[tmp];
595 command[tmp] = isupper(x) ? tolower(x) : x;
602 for (c = clist; c; c = c->next)
603 if (!strncmp (command, c->name, len)
604 && (!ignore_help_classes || c->function.cfunc))
608 if (c->name[len] == '\0')
615 /* If nothing matches, we have a simple failure. */
621 if (result_list != NULL)
622 /* Will be modified in calling routine
623 if we know what the prefix command is. */
625 return (struct cmd_list_element *) -1; /* Ambiguous. */
628 /* We've matched something on this list. Move text pointer forward. */
632 /* If this was an abbreviation, use the base command instead. */
634 if (found->cmd_pointer)
635 found = found->cmd_pointer;
637 /* If we found a prefix command, keep looking. */
639 if (found->prefixlist)
641 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
642 ignore_help_classes);
645 /* Didn't find anything; this is as far as we got. */
646 if (result_list != NULL)
647 *result_list = clist;
650 else if (c == (struct cmd_list_element *) -1)
652 /* We've gotten this far properley, but the next step
653 is ambiguous. We need to set the result list to the best
654 we've found (if an inferior hasn't already set it). */
655 if (result_list != NULL)
657 /* This used to say *result_list = *found->prefixlist
658 If that was correct, need to modify the documentation
659 at the top of this function to clarify what is supposed
661 *result_list = found;
672 if (result_list != NULL)
673 *result_list = clist;
678 /* All this hair to move the space to the front of cmdtype */
681 undef_cmd_error (cmdtype, q)
684 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
692 /* Look up the contents of *LINE as a command in the command list LIST.
693 LIST is a chain of struct cmd_list_element's.
694 If it is found, return the struct cmd_list_element for that command
695 and update *LINE to point after the command name, at the first argument.
696 If not found, call error if ALLOW_UNKNOWN is zero
697 otherwise (or if error returns) return zero.
698 Call error if specified command is ambiguous,
699 unless ALLOW_UNKNOWN is negative.
700 CMDTYPE precedes the word "command" in the error message.
702 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
703 elements which are actually help classes rather than commands (i.e.
704 the function field of the struct cmd_list_element is 0). */
706 struct cmd_list_element *
707 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
709 struct cmd_list_element *list;
712 int ignore_help_classes;
714 struct cmd_list_element *last_list = 0;
715 struct cmd_list_element *c =
716 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
718 /* This is wrong for complete_command. */
719 char *ptr = (*line) + strlen (*line) - 1;
721 /* Clear off trailing whitespace. */
722 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
732 error ("Lack of needed %scommand", cmdtype);
737 while (isalnum(*p) || *p == '-')
740 q = (char *) alloca (p - *line + 1);
741 strncpy (q, *line, p - *line);
743 undef_cmd_error (cmdtype, q);
749 else if (c == (struct cmd_list_element *) -1)
751 /* Ambigous. Local values should be off prefixlist or called
753 int local_allow_unknown = (last_list ? last_list->allow_unknown :
755 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
756 struct cmd_list_element *local_list =
757 (last_list ? *(last_list->prefixlist) : list);
759 if (local_allow_unknown < 0)
762 return last_list; /* Found something. */
764 return 0; /* Found nothing. */
768 /* Report as error. */
773 ((*line)[amb_len] && (*line)[amb_len] != ' '
774 && (*line)[amb_len] != '\t');
779 for (c = local_list; c; c = c->next)
780 if (!strncmp (*line, c->name, amb_len))
782 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
785 strcat (ambbuf, ", ");
786 strcat (ambbuf, c->name);
790 strcat (ambbuf, "..");
794 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
801 /* We've got something. It may still not be what the caller
802 wants (if this command *needs* a subcommand). */
803 while (**line == ' ' || **line == '\t')
806 if (c->prefixlist && **line && !c->allow_unknown)
807 undef_cmd_error (c->prefixname, *line);
809 /* Seems to be what he wants. Return it. */
816 /* Look up the contents of *LINE as a command in the command list LIST.
817 LIST is a chain of struct cmd_list_element's.
818 If it is found, return the struct cmd_list_element for that command
819 and update *LINE to point after the command name, at the first argument.
820 If not found, call error if ALLOW_UNKNOWN is zero
821 otherwise (or if error returns) return zero.
822 Call error if specified command is ambiguous,
823 unless ALLOW_UNKNOWN is negative.
824 CMDTYPE precedes the word "command" in the error message. */
826 struct cmd_list_element *
827 lookup_cmd (line, list, cmdtype, allow_unknown)
829 struct cmd_list_element *list;
834 register struct cmd_list_element *c, *found;
840 /* Skip leading whitespace. */
842 while (**line == ' ' || **line == '\t')
845 /* Clear out trailing whitespace. */
847 p = *line + strlen (*line);
848 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
852 /* Find end of command name. */
855 while (*p == '-' || isalnum(*p))
858 /* Look up the command name.
859 If exact match, keep that.
860 Otherwise, take command abbreviated, if unique. Note that (in my
861 opinion) a null string does *not* indicate ambiguity; simply the
862 end of the argument. */
867 error ("Lack of needed %scommand", cmdtype);
871 /* Copy over to a local buffer, converting to lowercase on the way.
872 This is in case the command being parsed is a subcommand which
873 doesn't match anything, and that's ok. We want the original
874 untouched for the routine of the original command. */
876 processed_cmd = (char *) alloca (p - *line + 1);
877 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
879 char x = (*line)[cmd_len];
881 processed_cmd[cmd_len] = tolower(x);
883 processed_cmd[cmd_len] = x;
885 processed_cmd[cmd_len] = '\0';
887 /* Check all possibilities in the current command list. */
890 for (c = list; c; c = c->next)
892 if (!strncmp (processed_cmd, c->name, cmd_len))
896 if (c->name[cmd_len] == 0)
904 /* Report error for undefined command name. */
908 if (nfound > 1 && allow_unknown >= 0)
911 for (c = list; c; c = c->next)
912 if (!strncmp (processed_cmd, c->name, cmd_len))
914 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
917 strcat (ambbuf, ", ");
918 strcat (ambbuf, c->name);
922 strcat (ambbuf, "..");
926 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
927 processed_cmd, ambbuf);
929 else if (!allow_unknown)
930 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
934 /* Skip whitespace before the argument. */
936 while (*p == ' ' || *p == '\t') p++;
939 if (found->prefixlist && *p)
941 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
942 found->allow_unknown);
951 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
953 /* Return a vector of char pointers which point to the different
954 possible completions in LIST of TEXT.
956 WORD points in the same buffer as TEXT, and completions should be
957 returned relative to this position. For example, suppose TEXT is "foo"
958 and we want to complete to "foobar". If WORD is "oo", return
959 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
962 complete_on_cmdlist (list, text, word)
963 struct cmd_list_element *list;
967 struct cmd_list_element *ptr;
969 int sizeof_matchlist;
971 int textlen = strlen (text);
973 sizeof_matchlist = 10;
974 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
977 for (ptr = list; ptr; ptr = ptr->next)
978 if (!strncmp (ptr->name, text, textlen)
980 && (ptr->function.cfunc
983 if (matches == sizeof_matchlist)
985 sizeof_matchlist *= 2;
986 matchlist = (char **) xrealloc ((char *)matchlist,
991 matchlist[matches] = (char *)
992 xmalloc (strlen (word) + strlen (ptr->name) + 1);
994 strcpy (matchlist[matches], ptr->name);
995 else if (word > text)
997 /* Return some portion of ptr->name. */
998 strcpy (matchlist[matches], ptr->name + (word - text));
1002 /* Return some of text plus ptr->name. */
1003 strncpy (matchlist[matches], word, text - word);
1004 matchlist[matches][text - word] = '\0';
1005 strcat (matchlist[matches], ptr->name);
1012 free ((PTR)matchlist);
1017 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1018 * sizeof (char *)));
1019 matchlist[matches] = (char *) 0;
1025 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1027 /* Return a vector of char pointers which point to the different
1028 possible completions in CMD of TEXT.
1030 WORD points in the same buffer as TEXT, and completions should be
1031 returned relative to this position. For example, suppose TEXT is "foo"
1032 and we want to complete to "foobar". If WORD is "oo", return
1033 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1036 complete_on_enum (enumlist, text, word)
1042 int sizeof_matchlist;
1044 int textlen = strlen (text);
1048 sizeof_matchlist = 10;
1049 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1052 for (i = 0; (name = enumlist[i]) != NULL; i++)
1053 if (strncmp (name, text, textlen) == 0)
1055 if (matches == sizeof_matchlist)
1057 sizeof_matchlist *= 2;
1058 matchlist = (char **) xrealloc ((char *)matchlist,
1060 * sizeof (char *)));
1063 matchlist[matches] = (char *)
1064 xmalloc (strlen (word) + strlen (name) + 1);
1066 strcpy (matchlist[matches], name);
1067 else if (word > text)
1069 /* Return some portion of name. */
1070 strcpy (matchlist[matches], name + (word - text));
1074 /* Return some of text plus name. */
1075 strncpy (matchlist[matches], word, text - word);
1076 matchlist[matches][text - word] = '\0';
1077 strcat (matchlist[matches], name);
1084 free ((PTR)matchlist);
1089 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1090 * sizeof (char *)));
1091 matchlist[matches] = (char *) 0;
1098 parse_binary_operation (arg)
1106 length = strlen (arg);
1108 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1111 if (!strncmp (arg, "on", length)
1112 || !strncmp (arg, "1", length)
1113 || !strncmp (arg, "yes", length))
1116 if (!strncmp (arg, "off", length)
1117 || !strncmp (arg, "0", length)
1118 || !strncmp (arg, "no", length))
1122 error ("\"on\" or \"off\" expected.");
1127 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1128 of the argument, and FROM_TTY is nonzero if this command is being entered
1129 directly by the user (i.e. these are just like any other
1130 command). C is the command list element for the command. */
1132 do_setshow_command (arg, from_tty, c)
1135 struct cmd_list_element *c;
1137 if (c->type == set_cmd)
1139 switch (c->var_type)
1150 new = (char *) xmalloc (strlen (arg) + 2);
1152 while ((ch = *p++) != '\000')
1156 /* \ at end of argument is used after spaces
1157 so they won't be lost. */
1158 /* This is obsolete now that we no longer strip
1159 trailing whitespace and actually, the backslash
1160 didn't get here in my test, readline or
1161 something did something funky with a backslash
1162 right before a newline. */
1165 ch = parse_escape (&p);
1167 break; /* C loses */
1175 if (*(p - 1) != '\\')
1179 new = (char *) xrealloc (new, q - new);
1180 if (*(char **)c->var != NULL)
1181 free (*(char **)c->var);
1182 *(char **) c->var = new;
1185 case var_string_noescape:
1188 if (*(char **)c->var != NULL)
1189 free (*(char **)c->var);
1190 *(char **) c->var = savestring (arg, strlen (arg));
1194 error_no_arg ("filename to set it to.");
1195 if (*(char **)c->var != NULL)
1196 free (*(char **)c->var);
1197 *(char **)c->var = tilde_expand (arg);
1200 *(int *) c->var = parse_binary_operation (arg);
1204 error_no_arg ("integer to set it to.");
1205 *(unsigned int *) c->var = parse_and_eval_address (arg);
1206 if (*(unsigned int *) c->var == 0)
1207 *(unsigned int *) c->var = UINT_MAX;
1213 error_no_arg ("integer to set it to.");
1214 val = parse_and_eval_address (arg);
1216 *(int *) c->var = INT_MAX;
1217 else if (val >= INT_MAX)
1218 error ("integer %u out of range", val);
1220 *(int *) c->var = val;
1225 error_no_arg ("integer to set it to.");
1226 *(int *) c->var = parse_and_eval_address (arg);
1236 p = strchr (arg, ' ');
1244 for (i = 0; c->enums[i]; i++)
1245 if (strncmp (arg, c->enums[i], len) == 0)
1247 match = c->enums[i];
1252 error ("Undefined item: \"%s\".", arg);
1255 error ("Ambiguous item \"%s\".", arg);
1257 *(char **)c->var = match;
1261 error ("gdb internal error: bad var_type in do_setshow_command");
1264 else if (c->type == show_cmd)
1266 /* Print doc minus "show" at start. */
1267 print_doc_line (gdb_stdout, c->doc + 5);
1269 fputs_filtered (" is ", gdb_stdout);
1271 switch (c->var_type)
1277 fputs_filtered ("\"", gdb_stdout);
1278 if (*(unsigned char **)c->var)
1279 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1280 gdb_printchar (*p, gdb_stdout, '"');
1281 fputs_filtered ("\"", gdb_stdout);
1284 case var_string_noescape:
1287 fputs_filtered ("\"", gdb_stdout);
1288 if (*(char **)c->var)
1289 fputs_filtered (*(char **) c->var, gdb_stdout);
1290 fputs_filtered ("\"", gdb_stdout);
1293 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1296 if (*(unsigned int *) c->var == UINT_MAX) {
1297 fputs_filtered ("unlimited", gdb_stdout);
1300 /* else fall through */
1302 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1305 if (*(int *) c->var == INT_MAX)
1307 fputs_filtered ("unlimited", gdb_stdout);
1310 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1314 error ("gdb internal error: bad var_type in do_setshow_command");
1316 fputs_filtered (".\n", gdb_stdout);
1319 error ("gdb internal error: bad cmd_type in do_setshow_command");
1320 (*c->function.sfunc) (NULL, from_tty, c);
1323 /* Show all the settings in a list of show commands. */
1326 cmd_show_list (list, from_tty, prefix)
1327 struct cmd_list_element *list;
1331 for (; list != NULL; list = list->next) {
1332 /* If we find a prefix, run its list, prefixing our output by its
1333 prefix (with "show " skipped). */
1334 if (list->prefixlist && !list->abbrev_flag)
1335 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1336 if (list->type == show_cmd)
1338 fputs_filtered (prefix, gdb_stdout);
1339 fputs_filtered (list->name, gdb_stdout);
1340 fputs_filtered (": ", gdb_stdout);
1341 do_setshow_command ((char *)NULL, from_tty, list);
1348 shell_escape (arg, from_tty)
1353 /* FIXME: what about errors (I don't know how GO32 system() handles
1356 #else /* Can fork. */
1357 int rc, status, pid;
1358 char *p, *user_shell;
1360 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1361 user_shell = "/bin/sh";
1363 /* Get the name of the shell for arg0 */
1364 if ((p = strrchr (user_shell, '/')) == NULL)
1367 p++; /* Get past '/' */
1369 if ((pid = fork()) == 0)
1372 execl (user_shell, p, 0);
1374 execl (user_shell, p, "-c", arg, 0);
1376 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1377 safe_strerror (errno));
1378 gdb_flush (gdb_stderr);
1383 while ((rc = wait (&status)) != pid && rc != -1)
1386 error ("Fork failed");
1387 #endif /* Can fork. */
1391 make_command (arg, from_tty)
1401 p = xmalloc (sizeof("make ") + strlen(arg));
1402 strcpy (p, "make ");
1403 strcpy (p + sizeof("make ")-1, arg);
1406 shell_escape (p, from_tty);
1410 show_user_1 (c, stream)
1411 struct cmd_list_element *c;
1414 register struct command_line *cmdlines;
1416 cmdlines = c->user_commands;
1419 fputs_filtered ("User command ", stream);
1420 fputs_filtered (c->name, stream);
1421 fputs_filtered (":\n", stream);
1425 print_command_line (cmdlines, 4);
1426 cmdlines = cmdlines->next;
1428 fputs_filtered ("\n", stream);
1433 show_user (args, from_tty)
1437 struct cmd_list_element *c;
1438 extern struct cmd_list_element *cmdlist;
1442 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1443 if (c->class != class_user)
1444 error ("Not a user command.");
1445 show_user_1 (c, gdb_stdout);
1449 for (c = cmdlist; c; c = c->next)
1451 if (c->class == class_user)
1452 show_user_1 (c, gdb_stdout);
1458 _initialize_command ()
1460 add_com ("shell", class_support, shell_escape,
1461 "Execute the rest of the line as a shell command. \n\
1462 With no arguments, run an inferior shell.");
1463 add_com ("make", class_support, make_command,
1464 "Run the ``make'' program using the rest of the line as arguments.");
1465 add_cmd ("user", no_class, show_user,
1466 "Show definitions of user defined commands.\n\
1467 Argument is the name of the user defined command.\n\
1468 With no argument, show definitions of all user defined commands.", &showlist);