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. */
23 #include "gdb_string.h"
31 # ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
38 /* Prototypes for local functions */
41 undef_cmd_error PARAMS ((char *, char *));
44 show_user PARAMS ((char *, int));
47 show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
50 make_command PARAMS ((char *, int));
53 shell_escape PARAMS ((char *, int));
56 parse_binary_operation PARAMS ((char *));
59 print_doc_line PARAMS ((GDB_FILE *, char *));
61 /* Add element named NAME.
62 CLASS is the top level category into which commands are broken down
64 FUN should be the function to execute the command;
65 it will get a character string as argument, with leading
66 and trailing blanks already eliminated.
68 DOC is a documentation string for the command.
69 Its first line should be a complete sentence.
70 It should start with ? for a command that is an abbreviation
71 or with * for a command that most users don't need to know about.
73 Add this command to command list *LIST.
75 Returns a pointer to the added command (not necessarily the head
78 struct cmd_list_element *
79 add_cmd (name, class, fun, doc, list)
81 enum command_class class;
82 void (*fun) PARAMS ((char *, int));
84 struct cmd_list_element **list;
86 register struct cmd_list_element *c
87 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
88 struct cmd_list_element *p;
90 delete_cmd (name, list);
92 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
100 while (p->next && STRCMP (p->next->name, name) <= 0)
110 c->function.cfunc = fun;
113 c->prefixlist = NULL;
114 c->prefixname = NULL;
115 c->allow_unknown = 0;
117 c->completer = make_symbol_completion_list;
118 c->type = not_set_cmd;
120 c->var_type = var_boolean;
122 c->user_commands = NULL;
124 c->cmd_pointer = NULL;
129 /* Same as above, except that the abbrev_flag is set. */
131 #if 0 /* Currently unused */
133 struct cmd_list_element *
134 add_abbrev_cmd (name, class, fun, doc, list)
136 enum command_class class;
137 void (*fun) PARAMS ((char *, int));
139 struct cmd_list_element **list;
141 register struct cmd_list_element *c
142 = add_cmd (name, class, fun, doc, list);
150 struct cmd_list_element *
151 add_alias_cmd (name, oldname, class, abbrev_flag, list)
154 enum command_class class;
156 struct cmd_list_element **list;
158 /* Must do this since lookup_cmd tries to side-effect its first arg */
160 register struct cmd_list_element *old;
161 register struct cmd_list_element *c;
162 copied_name = (char *) alloca (strlen (oldname) + 1);
163 strcpy (copied_name, oldname);
164 old = lookup_cmd (&copied_name, *list, "", 1, 1);
168 delete_cmd (name, list);
172 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
173 c->prefixlist = old->prefixlist;
174 c->prefixname = old->prefixname;
175 c->allow_unknown = old->allow_unknown;
176 c->abbrev_flag = abbrev_flag;
177 c->cmd_pointer = old;
181 /* Like add_cmd but adds an element for a command prefix:
182 a name that should be followed by a subcommand to be looked up
183 in another command list. PREFIXLIST should be the address
184 of the variable containing that list. */
186 struct cmd_list_element *
187 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
190 enum command_class class;
191 void (*fun) PARAMS ((char *, int));
193 struct cmd_list_element **prefixlist;
196 struct cmd_list_element **list;
198 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
199 c->prefixlist = prefixlist;
200 c->prefixname = prefixname;
201 c->allow_unknown = allow_unknown;
205 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
207 struct cmd_list_element *
208 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
211 enum command_class class;
212 void (*fun) PARAMS ((char *, int));
214 struct cmd_list_element **prefixlist;
217 struct cmd_list_element **list;
219 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
220 c->prefixlist = prefixlist;
221 c->prefixname = prefixname;
222 c->allow_unknown = allow_unknown;
227 /* This is an empty "cfunc". */
229 not_just_help_class_command (args, from_tty)
235 /* This is an empty "sfunc". */
236 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
239 empty_sfunc (args, from_tty, c)
242 struct cmd_list_element *c;
246 /* Add element named NAME to command list LIST (the list for set
247 or some sublist thereof).
248 CLASS is as in add_cmd.
249 VAR_TYPE is the kind of thing we are setting.
250 VAR is address of the variable being controlled by this command.
251 DOC is the documentation string. */
253 struct cmd_list_element *
254 add_set_cmd (name, class, var_type, var, doc, list)
256 enum command_class class;
260 struct cmd_list_element **list;
262 struct cmd_list_element *c
263 = add_cmd (name, class, NO_FUNCTION, doc, list);
266 c->var_type = var_type;
268 /* This needs to be something besides NO_FUNCTION so that this isn't
269 treated as a help class. */
270 c->function.sfunc = empty_sfunc;
274 /* Add element named NAME to command list LIST (the list for set
275 or some sublist thereof).
276 CLASS is as in add_cmd.
277 ENUMLIST is a list of strings which may follow NAME.
278 VAR is address of the variable which will contain the matching string
280 DOC is the documentation string. */
282 struct cmd_list_element *
283 add_set_enum_cmd (name, class, enumlist, var, doc, list)
285 enum command_class class;
289 struct cmd_list_element **list;
291 struct cmd_list_element *c
292 = add_set_cmd (name, class, var_enum, var, doc, list);
298 /* Where SETCMD has already been added, add the corresponding show
299 command to LIST and return a pointer to the added command (not
300 necessarily the head of LIST). */
301 struct cmd_list_element *
302 add_show_from_set (setcmd, list)
303 struct cmd_list_element *setcmd;
304 struct cmd_list_element **list;
306 struct cmd_list_element *showcmd =
307 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
308 struct cmd_list_element *p;
310 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
311 delete_cmd (showcmd->name, list);
312 showcmd->type = show_cmd;
314 /* Replace "set " at start of docstring with "show ". */
315 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
316 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
317 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
319 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
321 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
323 showcmd->next = *list;
329 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
333 showcmd->next = p->next;
340 /* Remove the command named NAME from the command list. */
343 delete_cmd (name, list)
345 struct cmd_list_element **list;
347 register struct cmd_list_element *c;
348 struct cmd_list_element *p;
350 while (*list && STREQ ((*list)->name, name))
353 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
360 for (c = *list; c->next;)
362 if (STREQ (c->next->name, name))
365 c->next->hookee->hook = 0; /* hooked cmd gets away. */
375 /* This command really has to deal with two things:
376 * 1) I want documentation on *this string* (usually called by
377 * "help commandname").
378 * 2) I want documentation on *this list* (usually called by
379 * giving a command that requires subcommands. Also called by saying
382 * I am going to split this into two seperate comamnds, help_cmd and
387 help_cmd (command, stream)
391 struct cmd_list_element *c;
392 extern struct cmd_list_element *cmdlist;
396 help_list (cmdlist, "", all_classes, stream);
400 c = lookup_cmd (&command, cmdlist, "", 0, 0);
405 /* There are three cases here.
406 If c->prefixlist is nonzero, we have a prefix command.
407 Print its documentation, then list its subcommands.
409 If c->function is nonzero, we really have a command.
410 Print its documentation and return.
412 If c->function is zero, we have a class name.
413 Print its documentation (as if it were a command)
414 and then set class to the number of this class
415 so that the commands in the class will be listed. */
417 fputs_filtered (c->doc, stream);
418 fputs_filtered ("\n", stream);
420 if (c->prefixlist == 0 && c->function.cfunc != NULL)
422 fprintf_filtered (stream, "\n");
424 /* If this is a prefix command, print it's subcommands */
426 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
428 /* If this is a class name, print all of the commands in the class */
429 if (c->function.cfunc == NULL)
430 help_list (cmdlist, "", c->class, stream);
433 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
438 * Get a specific kind of help on a command list.
441 * CMDTYPE is the prefix to use in the title string.
442 * CLASS is the class with which to list the nodes of this list (see
443 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
444 * everything, ALL_CLASSES for just classes, and non-negative for only things
445 * in a specific class.
446 * and STREAM is the output stream on which to print things.
447 * If you call this routine with a class >= 0, it recurses.
450 help_list (list, cmdtype, class, stream)
451 struct cmd_list_element *list;
453 enum command_class class;
457 char *cmdtype1, *cmdtype2;
459 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
460 len = strlen (cmdtype);
461 cmdtype1 = (char *) alloca (len + 1);
463 cmdtype2 = (char *) alloca (len + 4);
468 strncpy (cmdtype1 + 1, cmdtype, len - 1);
470 strncpy (cmdtype2, cmdtype, len - 1);
471 strcpy (cmdtype2 + len - 1, " sub");
474 if (class == all_classes)
475 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
477 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
479 help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
481 if (class == all_classes)
482 fprintf_filtered (stream, "\n\
483 Type \"help%s\" followed by a class name for a list of commands in that class.",
486 fprintf_filtered (stream, "\n\
487 Type \"help%s\" followed by %scommand name for full documentation.\n\
488 Command name abbreviations are allowed if unambiguous.\n",
492 /* Print only the first line of STR on STREAM. */
494 print_doc_line (stream, str)
498 static char *line_buffer = 0;
499 static int line_size;
505 line_buffer = (char *) xmalloc (line_size);
509 while (*p && *p != '\n' && *p != '.' && *p != ',')
511 if (p - str > line_size - 1)
513 line_size = p - str + 1;
514 free ((PTR)line_buffer);
515 line_buffer = (char *) xmalloc (line_size);
517 strncpy (line_buffer, str, p - str);
518 line_buffer[p - str] = '\0';
519 if (islower (line_buffer[0]))
520 line_buffer[0] = toupper (line_buffer[0]);
521 fputs_filtered (line_buffer, stream);
525 * Implement a help command on command list LIST.
526 * RECURSE should be non-zero if this should be done recursively on
527 * all sublists of LIST.
528 * PREFIX is the prefix to print before each command name.
529 * STREAM is the stream upon which the output should be written.
531 * A non-negative class number to list only commands in that
533 * ALL_COMMANDS to list all commands in list.
534 * ALL_CLASSES to list all classes in list.
536 * Note that RECURSE will be active on *all* sublists, not just the
537 * ones selected by the criteria above (ie. the selection mechanism
538 * is at the low level, not the high-level).
541 help_cmd_list (list, class, prefix, recurse, stream)
542 struct cmd_list_element *list;
543 enum command_class class;
548 register struct cmd_list_element *c;
550 for (c = list; c; c = c->next)
552 if (c->abbrev_flag == 0 &&
553 (class == all_commands
554 || (class == all_classes && c->function.cfunc == NULL)
555 || (class == c->class && c->function.cfunc != NULL)))
557 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
558 print_doc_line (stream, c->doc);
559 fputs_filtered ("\n", stream);
562 && c->prefixlist != 0
563 && c->abbrev_flag == 0)
564 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
568 /* This routine takes a line of TEXT and a CLIST in which to start the
569 lookup. When it returns it will have incremented the text pointer past
570 the section of text it matched, set *RESULT_LIST to point to the list in
571 which the last word was matched, and will return a pointer to the cmd
572 list element which the text matches. It will return NULL if no match at
573 all was possible. It will return -1 (cast appropriately, ick) if ambigous
574 matches are possible; in this case *RESULT_LIST will be set to point to
575 the list in which there are ambiguous choices (and *TEXT will be set to
576 the ambiguous text string).
578 If the located command was an abbreviation, this routine returns the base
579 command of the abbreviation.
581 It does no error reporting whatsoever; control will always return
582 to the superior routine.
584 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
585 at the prefix_command (ie. the best match) *or* (special case) will be NULL
586 if no prefix command was ever found. For example, in the case of "info a",
587 "info" matches without ambiguity, but "a" could be "args" or "address", so
588 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
589 RESULT_LIST should not be interpeted as a pointer to the beginning of a
590 list; it simply points to a specific command. In the case of an ambiguous
591 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
592 "info t" can be "info types" or "info target"; upon return *TEXT has been
593 advanced past "info ").
595 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
596 affect the operation).
598 This routine does *not* modify the text pointed to by TEXT.
600 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
601 are actually help classes rather than commands (i.e. the function field of
602 the struct cmd_list_element is NULL). */
604 struct cmd_list_element *
605 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
607 struct cmd_list_element *clist, **result_list;
608 int ignore_help_classes;
611 int len, tmp, nfound;
612 struct cmd_list_element *found, *c;
614 while (**text == ' ' || **text == '\t')
617 /* Treating underscores as part of command words is important
618 so that "set args_foo()" doesn't get interpreted as
619 "set args _foo()". */
621 *p && (isalnum(*p) || *p == '-' || *p == '_');
625 /* If nothing but whitespace, return 0. */
631 /* *text and p now bracket the first command word to lookup (and
632 it's length is len). We copy this into a local temporary,
633 converting to lower case as we go. */
635 command = (char *) alloca (len + 1);
636 for (tmp = 0; tmp < len; tmp++)
638 char x = (*text)[tmp];
639 command[tmp] = isupper(x) ? tolower(x) : x;
646 for (c = clist; c; c = c->next)
647 if (!strncmp (command, c->name, len)
648 && (!ignore_help_classes || c->function.cfunc))
652 if (c->name[len] == '\0')
659 /* If nothing matches, we have a simple failure. */
665 if (result_list != NULL)
666 /* Will be modified in calling routine
667 if we know what the prefix command is. */
669 return (struct cmd_list_element *) -1; /* Ambiguous. */
672 /* We've matched something on this list. Move text pointer forward. */
676 /* If this was an abbreviation, use the base command instead. */
678 if (found->cmd_pointer)
679 found = found->cmd_pointer;
681 /* If we found a prefix command, keep looking. */
683 if (found->prefixlist)
685 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
686 ignore_help_classes);
689 /* Didn't find anything; this is as far as we got. */
690 if (result_list != NULL)
691 *result_list = clist;
694 else if (c == (struct cmd_list_element *) -1)
696 /* We've gotten this far properly, but the next step
697 is ambiguous. We need to set the result list to the best
698 we've found (if an inferior hasn't already set it). */
699 if (result_list != NULL)
701 /* This used to say *result_list = *found->prefixlist
702 If that was correct, need to modify the documentation
703 at the top of this function to clarify what is supposed
705 *result_list = found;
716 if (result_list != NULL)
717 *result_list = clist;
722 /* All this hair to move the space to the front of cmdtype */
725 undef_cmd_error (cmdtype, q)
728 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
736 /* Look up the contents of *LINE as a command in the command list LIST.
737 LIST is a chain of struct cmd_list_element's.
738 If it is found, return the struct cmd_list_element for that command
739 and update *LINE to point after the command name, at the first argument.
740 If not found, call error if ALLOW_UNKNOWN is zero
741 otherwise (or if error returns) return zero.
742 Call error if specified command is ambiguous,
743 unless ALLOW_UNKNOWN is negative.
744 CMDTYPE precedes the word "command" in the error message.
746 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
747 elements which are actually help classes rather than commands (i.e.
748 the function field of the struct cmd_list_element is 0). */
750 struct cmd_list_element *
751 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
753 struct cmd_list_element *list;
756 int ignore_help_classes;
758 struct cmd_list_element *last_list = 0;
759 struct cmd_list_element *c =
760 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
762 /* This is wrong for complete_command. */
763 char *ptr = (*line) + strlen (*line) - 1;
765 /* Clear off trailing whitespace. */
766 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
776 error ("Lack of needed %scommand", cmdtype);
781 while (isalnum(*p) || *p == '-')
784 q = (char *) alloca (p - *line + 1);
785 strncpy (q, *line, p - *line);
787 undef_cmd_error (cmdtype, q);
793 else if (c == (struct cmd_list_element *) -1)
795 /* Ambigous. Local values should be off prefixlist or called
797 int local_allow_unknown = (last_list ? last_list->allow_unknown :
799 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
800 struct cmd_list_element *local_list =
801 (last_list ? *(last_list->prefixlist) : list);
803 if (local_allow_unknown < 0)
806 return last_list; /* Found something. */
808 return 0; /* Found nothing. */
812 /* Report as error. */
817 ((*line)[amb_len] && (*line)[amb_len] != ' '
818 && (*line)[amb_len] != '\t');
823 for (c = local_list; c; c = c->next)
824 if (!strncmp (*line, c->name, amb_len))
826 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
829 strcat (ambbuf, ", ");
830 strcat (ambbuf, c->name);
834 strcat (ambbuf, "..");
838 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
845 /* We've got something. It may still not be what the caller
846 wants (if this command *needs* a subcommand). */
847 while (**line == ' ' || **line == '\t')
850 if (c->prefixlist && **line && !c->allow_unknown)
851 undef_cmd_error (c->prefixname, *line);
853 /* Seems to be what he wants. Return it. */
860 /* Look up the contents of *LINE as a command in the command list LIST.
861 LIST is a chain of struct cmd_list_element's.
862 If it is found, return the struct cmd_list_element for that command
863 and update *LINE to point after the command name, at the first argument.
864 If not found, call error if ALLOW_UNKNOWN is zero
865 otherwise (or if error returns) return zero.
866 Call error if specified command is ambiguous,
867 unless ALLOW_UNKNOWN is negative.
868 CMDTYPE precedes the word "command" in the error message. */
870 struct cmd_list_element *
871 lookup_cmd (line, list, cmdtype, allow_unknown)
873 struct cmd_list_element *list;
878 register struct cmd_list_element *c, *found;
884 /* Skip leading whitespace. */
886 while (**line == ' ' || **line == '\t')
889 /* Clear out trailing whitespace. */
891 p = *line + strlen (*line);
892 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
896 /* Find end of command name. */
899 while (*p == '-' || isalnum(*p))
902 /* Look up the command name.
903 If exact match, keep that.
904 Otherwise, take command abbreviated, if unique. Note that (in my
905 opinion) a null string does *not* indicate ambiguity; simply the
906 end of the argument. */
911 error ("Lack of needed %scommand", cmdtype);
915 /* Copy over to a local buffer, converting to lowercase on the way.
916 This is in case the command being parsed is a subcommand which
917 doesn't match anything, and that's ok. We want the original
918 untouched for the routine of the original command. */
920 processed_cmd = (char *) alloca (p - *line + 1);
921 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
923 char x = (*line)[cmd_len];
925 processed_cmd[cmd_len] = tolower(x);
927 processed_cmd[cmd_len] = x;
929 processed_cmd[cmd_len] = '\0';
931 /* Check all possibilities in the current command list. */
934 for (c = list; c; c = c->next)
936 if (!strncmp (processed_cmd, c->name, cmd_len))
940 if (c->name[cmd_len] == 0)
948 /* Report error for undefined command name. */
952 if (nfound > 1 && allow_unknown >= 0)
955 for (c = list; c; c = c->next)
956 if (!strncmp (processed_cmd, c->name, cmd_len))
958 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
961 strcat (ambbuf, ", ");
962 strcat (ambbuf, c->name);
966 strcat (ambbuf, "..");
970 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
971 processed_cmd, ambbuf);
973 else if (!allow_unknown)
974 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
978 /* Skip whitespace before the argument. */
980 while (*p == ' ' || *p == '\t') p++;
983 if (found->prefixlist && *p)
985 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
986 found->allow_unknown);
995 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
997 /* Return a vector of char pointers which point to the different
998 possible completions in LIST of TEXT.
1000 WORD points in the same buffer as TEXT, and completions should be
1001 returned relative to this position. For example, suppose TEXT is "foo"
1002 and we want to complete to "foobar". If WORD is "oo", return
1003 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1006 complete_on_cmdlist (list, text, word)
1007 struct cmd_list_element *list;
1011 struct cmd_list_element *ptr;
1013 int sizeof_matchlist;
1015 int textlen = strlen (text);
1017 sizeof_matchlist = 10;
1018 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1021 for (ptr = list; ptr; ptr = ptr->next)
1022 if (!strncmp (ptr->name, text, textlen)
1023 && !ptr->abbrev_flag
1024 && (ptr->function.cfunc
1025 || ptr->prefixlist))
1027 if (matches == sizeof_matchlist)
1029 sizeof_matchlist *= 2;
1030 matchlist = (char **) xrealloc ((char *)matchlist,
1032 * sizeof (char *)));
1035 matchlist[matches] = (char *)
1036 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1038 strcpy (matchlist[matches], ptr->name);
1039 else if (word > text)
1041 /* Return some portion of ptr->name. */
1042 strcpy (matchlist[matches], ptr->name + (word - text));
1046 /* Return some of text plus ptr->name. */
1047 strncpy (matchlist[matches], word, text - word);
1048 matchlist[matches][text - word] = '\0';
1049 strcat (matchlist[matches], ptr->name);
1056 free ((PTR)matchlist);
1061 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1062 * sizeof (char *)));
1063 matchlist[matches] = (char *) 0;
1069 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1071 /* Return a vector of char pointers which point to the different
1072 possible completions in CMD of TEXT.
1074 WORD points in the same buffer as TEXT, and completions should be
1075 returned relative to this position. For example, suppose TEXT is "foo"
1076 and we want to complete to "foobar". If WORD is "oo", return
1077 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1080 complete_on_enum (enumlist, text, word)
1086 int sizeof_matchlist;
1088 int textlen = strlen (text);
1092 sizeof_matchlist = 10;
1093 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1096 for (i = 0; (name = enumlist[i]) != NULL; i++)
1097 if (strncmp (name, text, textlen) == 0)
1099 if (matches == sizeof_matchlist)
1101 sizeof_matchlist *= 2;
1102 matchlist = (char **) xrealloc ((char *)matchlist,
1104 * sizeof (char *)));
1107 matchlist[matches] = (char *)
1108 xmalloc (strlen (word) + strlen (name) + 1);
1110 strcpy (matchlist[matches], name);
1111 else if (word > text)
1113 /* Return some portion of name. */
1114 strcpy (matchlist[matches], name + (word - text));
1118 /* Return some of text plus name. */
1119 strncpy (matchlist[matches], word, text - word);
1120 matchlist[matches][text - word] = '\0';
1121 strcat (matchlist[matches], name);
1128 free ((PTR)matchlist);
1133 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1134 * sizeof (char *)));
1135 matchlist[matches] = (char *) 0;
1142 parse_binary_operation (arg)
1150 length = strlen (arg);
1152 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1155 if (!strncmp (arg, "on", length)
1156 || !strncmp (arg, "1", length)
1157 || !strncmp (arg, "yes", length))
1160 if (!strncmp (arg, "off", length)
1161 || !strncmp (arg, "0", length)
1162 || !strncmp (arg, "no", length))
1166 error ("\"on\" or \"off\" expected.");
1171 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1172 of the argument, and FROM_TTY is nonzero if this command is being entered
1173 directly by the user (i.e. these are just like any other
1174 command). C is the command list element for the command. */
1176 do_setshow_command (arg, from_tty, c)
1179 struct cmd_list_element *c;
1181 if (c->type == set_cmd)
1183 switch (c->var_type)
1194 new = (char *) xmalloc (strlen (arg) + 2);
1196 while ((ch = *p++) != '\000')
1200 /* \ at end of argument is used after spaces
1201 so they won't be lost. */
1202 /* This is obsolete now that we no longer strip
1203 trailing whitespace and actually, the backslash
1204 didn't get here in my test, readline or
1205 something did something funky with a backslash
1206 right before a newline. */
1209 ch = parse_escape (&p);
1211 break; /* C loses */
1219 if (*(p - 1) != '\\')
1223 new = (char *) xrealloc (new, q - new);
1224 if (*(char **)c->var != NULL)
1225 free (*(char **)c->var);
1226 *(char **) c->var = new;
1229 case var_string_noescape:
1232 if (*(char **)c->var != NULL)
1233 free (*(char **)c->var);
1234 *(char **) c->var = savestring (arg, strlen (arg));
1238 error_no_arg ("filename to set it to.");
1239 if (*(char **)c->var != NULL)
1240 free (*(char **)c->var);
1241 *(char **)c->var = tilde_expand (arg);
1244 *(int *) c->var = parse_binary_operation (arg);
1248 error_no_arg ("integer to set it to.");
1249 *(unsigned int *) c->var = parse_and_eval_address (arg);
1250 if (*(unsigned int *) c->var == 0)
1251 *(unsigned int *) c->var = UINT_MAX;
1257 error_no_arg ("integer to set it to.");
1258 val = parse_and_eval_address (arg);
1260 *(int *) c->var = INT_MAX;
1261 else if (val >= INT_MAX)
1262 error ("integer %u out of range", val);
1264 *(int *) c->var = val;
1269 error_no_arg ("integer to set it to.");
1270 *(int *) c->var = parse_and_eval_address (arg);
1280 /* if no argument was supplied, print an informative error message */
1284 strcpy (msg, "Requires an argument. Valid arguments are ");
1285 for (i = 0; c->enums[i]; i++)
1289 strcat (msg, c->enums[i]);
1295 p = strchr (arg, ' ');
1303 for (i = 0; c->enums[i]; i++)
1304 if (strncmp (arg, c->enums[i], len) == 0)
1306 match = c->enums[i];
1311 error ("Undefined item: \"%s\".", arg);
1314 error ("Ambiguous item \"%s\".", arg);
1316 *(char **)c->var = match;
1320 error ("gdb internal error: bad var_type in do_setshow_command");
1323 else if (c->type == show_cmd)
1325 /* Print doc minus "show" at start. */
1326 print_doc_line (gdb_stdout, c->doc + 5);
1328 fputs_filtered (" is ", gdb_stdout);
1330 switch (c->var_type)
1336 fputs_filtered ("\"", gdb_stdout);
1337 if (*(unsigned char **)c->var)
1338 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1339 gdb_printchar (*p, gdb_stdout, '"');
1340 fputs_filtered ("\"", gdb_stdout);
1343 case var_string_noescape:
1346 fputs_filtered ("\"", gdb_stdout);
1347 if (*(char **)c->var)
1348 fputs_filtered (*(char **) c->var, gdb_stdout);
1349 fputs_filtered ("\"", gdb_stdout);
1352 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1355 if (*(unsigned int *) c->var == UINT_MAX) {
1356 fputs_filtered ("unlimited", gdb_stdout);
1359 /* else fall through */
1361 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1364 if (*(int *) c->var == INT_MAX)
1366 fputs_filtered ("unlimited", gdb_stdout);
1369 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1373 error ("gdb internal error: bad var_type in do_setshow_command");
1375 fputs_filtered (".\n", gdb_stdout);
1378 error ("gdb internal error: bad cmd_type in do_setshow_command");
1379 (*c->function.sfunc) (NULL, from_tty, c);
1382 /* Show all the settings in a list of show commands. */
1385 cmd_show_list (list, from_tty, prefix)
1386 struct cmd_list_element *list;
1390 for (; list != NULL; list = list->next) {
1391 /* If we find a prefix, run its list, prefixing our output by its
1392 prefix (with "show " skipped). */
1393 if (list->prefixlist && !list->abbrev_flag)
1394 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1395 if (list->type == show_cmd)
1397 fputs_filtered (prefix, gdb_stdout);
1398 fputs_filtered (list->name, gdb_stdout);
1399 fputs_filtered (": ", gdb_stdout);
1400 do_setshow_command ((char *)NULL, from_tty, list);
1407 shell_escape (arg, from_tty)
1412 /* FIXME: what about errors (I don't know how GO32 system() handles
1415 #else /* Can fork. */
1416 int rc, status, pid;
1417 char *p, *user_shell;
1419 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1420 user_shell = "/bin/sh";
1422 /* Get the name of the shell for arg0 */
1423 if ((p = strrchr (user_shell, '/')) == NULL)
1426 p++; /* Get past '/' */
1428 if ((pid = fork()) == 0)
1431 execl (user_shell, p, 0);
1433 execl (user_shell, p, "-c", arg, 0);
1435 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1436 safe_strerror (errno));
1437 gdb_flush (gdb_stderr);
1442 while ((rc = wait (&status)) != pid && rc != -1)
1445 error ("Fork failed");
1446 #endif /* Can fork. */
1450 make_command (arg, from_tty)
1460 p = xmalloc (sizeof("make ") + strlen(arg));
1461 strcpy (p, "make ");
1462 strcpy (p + sizeof("make ")-1, arg);
1465 shell_escape (p, from_tty);
1469 show_user_1 (c, stream)
1470 struct cmd_list_element *c;
1473 register struct command_line *cmdlines;
1475 cmdlines = c->user_commands;
1478 fputs_filtered ("User command ", stream);
1479 fputs_filtered (c->name, stream);
1480 fputs_filtered (":\n", stream);
1484 print_command_line (cmdlines, 4);
1485 cmdlines = cmdlines->next;
1487 fputs_filtered ("\n", stream);
1492 show_user (args, from_tty)
1496 struct cmd_list_element *c;
1497 extern struct cmd_list_element *cmdlist;
1501 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1502 if (c->class != class_user)
1503 error ("Not a user command.");
1504 show_user_1 (c, gdb_stdout);
1508 for (c = cmdlist; c; c = c->next)
1510 if (c->class == class_user)
1511 show_user_1 (c, gdb_stdout);
1517 _initialize_command ()
1519 add_com ("shell", class_support, shell_escape,
1520 "Execute the rest of the line as a shell command. \n\
1521 With no arguments, run an inferior shell.");
1522 add_com ("make", class_support, make_command,
1523 "Run the ``make'' program using the rest of the line as arguments.");
1524 add_cmd ("user", no_class, show_user,
1525 "Show definitions of user defined commands.\n\
1526 Argument is the name of the user defined command.\n\
1527 With no argument, show definitions of all user defined commands.", &showlist);