1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998, 2000 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,
17 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
31 /* FIXME: this should be auto-configured! */
36 /* Prototypes for local functions */
38 static void undef_cmd_error PARAMS ((char *, char *));
40 static void show_user PARAMS ((char *, int));
42 static void show_user_1 (struct cmd_list_element *, struct ui_file *);
44 static void make_command PARAMS ((char *, int));
46 static void shell_escape PARAMS ((char *, int));
48 static int parse_binary_operation PARAMS ((char *));
50 static void print_doc_line (struct ui_file *, char *);
52 static struct cmd_list_element *find_cmd PARAMS ((char *command,
54 struct cmd_list_element * clist,
55 int ignore_help_classes,
58 void _initialize_command PARAMS ((void));
60 /* Add element named NAME.
61 CLASS is the top level category into which commands are broken down
63 FUN should be the function to execute the command;
64 it will get a character string as argument, with leading
65 and trailing blanks already eliminated.
67 DOC is a documentation string for the command.
68 Its first line should be a complete sentence.
69 It should start with ? for a command that is an abbreviation
70 or with * for a command that most users don't need to know about.
72 Add this command to command list *LIST.
74 Returns a pointer to the added command (not necessarily the head
77 struct cmd_list_element *
78 add_cmd (name, class, fun, doc, list)
80 enum command_class class;
81 void (*fun) PARAMS ((char *, int));
83 struct cmd_list_element **list;
85 register struct cmd_list_element *c
86 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
87 struct cmd_list_element *p;
89 delete_cmd (name, list);
91 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
99 while (p->next && STRCMP (p->next->name, name) <= 0)
109 c->function.cfunc = fun;
112 c->prefixlist = NULL;
113 c->prefixname = NULL;
114 c->allow_unknown = 0;
116 c->completer = make_symbol_completion_list;
117 c->type = not_set_cmd;
119 c->var_type = var_boolean;
121 c->user_commands = NULL;
123 c->cmd_pointer = NULL;
128 /* Same as above, except that the abbrev_flag is set. */
130 #if 0 /* Currently unused */
132 struct cmd_list_element *
133 add_abbrev_cmd (name, class, fun, doc, list)
135 enum command_class class;
136 void (*fun) PARAMS ((char *, int));
138 struct cmd_list_element **list;
140 register struct cmd_list_element *c
141 = add_cmd (name, class, fun, doc, list);
149 struct cmd_list_element *
150 add_alias_cmd (name, oldname, class, abbrev_flag, list)
153 enum command_class class;
155 struct cmd_list_element **list;
157 /* Must do this since lookup_cmd tries to side-effect its first arg */
159 register struct cmd_list_element *old;
160 register struct cmd_list_element *c;
161 copied_name = (char *) alloca (strlen (oldname) + 1);
162 strcpy (copied_name, oldname);
163 old = lookup_cmd (&copied_name, *list, "", 1, 1);
167 delete_cmd (name, list);
171 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
172 c->prefixlist = old->prefixlist;
173 c->prefixname = old->prefixname;
174 c->allow_unknown = old->allow_unknown;
175 c->abbrev_flag = abbrev_flag;
176 c->cmd_pointer = old;
180 /* Like add_cmd but adds an element for a command prefix:
181 a name that should be followed by a subcommand to be looked up
182 in another command list. PREFIXLIST should be the address
183 of the variable containing that list. */
185 struct cmd_list_element *
186 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
189 enum command_class class;
190 void (*fun) PARAMS ((char *, int));
192 struct cmd_list_element **prefixlist;
195 struct cmd_list_element **list;
197 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
198 c->prefixlist = prefixlist;
199 c->prefixname = prefixname;
200 c->allow_unknown = allow_unknown;
204 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
206 struct cmd_list_element *
207 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
210 enum command_class class;
211 void (*fun) PARAMS ((char *, int));
213 struct cmd_list_element **prefixlist;
216 struct cmd_list_element **list;
218 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
219 c->prefixlist = prefixlist;
220 c->prefixname = prefixname;
221 c->allow_unknown = allow_unknown;
226 /* This is an empty "cfunc". */
228 not_just_help_class_command (args, from_tty)
234 /* This is an empty "sfunc". */
235 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
238 empty_sfunc (args, from_tty, c)
241 struct cmd_list_element *c;
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 VAR_TYPE is the kind of thing we are setting.
249 VAR is address of the variable being controlled by this command.
250 DOC is the documentation string. */
252 struct cmd_list_element *
253 add_set_cmd (name, class, var_type, var, doc, list)
255 enum command_class class;
259 struct cmd_list_element **list;
261 struct cmd_list_element *c
262 = add_cmd (name, class, NO_FUNCTION, doc, list);
265 c->var_type = var_type;
267 /* This needs to be something besides NO_FUNCTION so that this isn't
268 treated as a help class. */
269 c->function.sfunc = empty_sfunc;
273 /* Add element named NAME to command list LIST (the list for set
274 or some sublist thereof).
275 CLASS is as in add_cmd.
276 ENUMLIST is a list of strings which may follow NAME.
277 VAR is address of the variable which will contain the matching string
279 DOC is the documentation string. */
281 struct cmd_list_element *
282 add_set_enum_cmd (name, class, enumlist, var, doc, list)
284 enum command_class class;
288 struct cmd_list_element **list;
290 struct cmd_list_element *c
291 = add_set_cmd (name, class, var_enum, var, doc, list);
297 /* Where SETCMD has already been added, add the corresponding show
298 command to LIST and return a pointer to the added command (not
299 necessarily the head of LIST). */
300 struct cmd_list_element *
301 add_show_from_set (setcmd, list)
302 struct cmd_list_element *setcmd;
303 struct cmd_list_element **list;
305 struct cmd_list_element *showcmd =
306 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
307 struct cmd_list_element *p;
309 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
310 delete_cmd (showcmd->name, list);
311 showcmd->type = show_cmd;
313 /* Replace "set " at start of docstring with "show ". */
314 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
315 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
316 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
318 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
320 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
322 showcmd->next = *list;
328 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
332 showcmd->next = p->next;
339 /* Remove the command named NAME from the command list. */
342 delete_cmd (name, list)
344 struct cmd_list_element **list;
346 register struct cmd_list_element *c;
347 struct cmd_list_element *p;
349 while (*list && STREQ ((*list)->name, name))
352 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
359 for (c = *list; c->next;)
361 if (STREQ (c->next->name, name))
364 c->next->hookee->hook = 0; /* hooked cmd gets away. */
366 free ((PTR) c->next);
374 /* This command really has to deal with two things:
375 * 1) I want documentation on *this string* (usually called by
376 * "help commandname").
377 * 2) I want documentation on *this list* (usually called by
378 * giving a command that requires subcommands. Also called by saying
381 * I am going to split this into two seperate comamnds, help_cmd and
386 help_cmd (command, stream)
388 struct ui_file *stream;
390 struct cmd_list_element *c;
391 extern struct cmd_list_element *cmdlist;
395 help_list (cmdlist, "", all_classes, stream);
399 c = lookup_cmd (&command, cmdlist, "", 0, 0);
404 /* There are three cases here.
405 If c->prefixlist is nonzero, we have a prefix command.
406 Print its documentation, then list its subcommands.
408 If c->function is nonzero, we really have a command.
409 Print its documentation and return.
411 If c->function is zero, we have a class name.
412 Print its documentation (as if it were a command)
413 and then set class to the number of this class
414 so that the commands in the class will be listed. */
416 fputs_filtered (c->doc, stream);
417 fputs_filtered ("\n", stream);
419 if (c->prefixlist == 0 && c->function.cfunc != NULL)
421 fprintf_filtered (stream, "\n");
423 /* If this is a prefix command, print it's subcommands */
425 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
427 /* If this is a class name, print all of the commands in the class */
428 if (c->function.cfunc == NULL)
429 help_list (cmdlist, "", c->class, stream);
432 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
437 * Get a specific kind of help on a command list.
440 * CMDTYPE is the prefix to use in the title string.
441 * CLASS is the class with which to list the nodes of this list (see
442 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
443 * everything, ALL_CLASSES for just classes, and non-negative for only things
444 * in a specific class.
445 * and STREAM is the output stream on which to print things.
446 * If you call this routine with a class >= 0, it recurses.
449 help_list (list, cmdtype, class, stream)
450 struct cmd_list_element *list;
452 enum command_class class;
453 struct ui_file *stream;
456 char *cmdtype1, *cmdtype2;
458 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
459 len = strlen (cmdtype);
460 cmdtype1 = (char *) alloca (len + 1);
462 cmdtype2 = (char *) alloca (len + 4);
467 strncpy (cmdtype1 + 1, cmdtype, len - 1);
469 strncpy (cmdtype2, cmdtype, len - 1);
470 strcpy (cmdtype2 + len - 1, " sub");
473 if (class == all_classes)
474 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
476 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
478 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
480 if (class == all_classes)
481 fprintf_filtered (stream, "\n\
482 Type \"help%s\" followed by a class name for a list of commands in that class.",
485 fprintf_filtered (stream, "\n\
486 Type \"help%s\" followed by %scommand name for full documentation.\n\
487 Command name abbreviations are allowed if unambiguous.\n",
491 /* Print only the first line of STR on STREAM. */
493 print_doc_line (stream, str)
494 struct ui_file *stream;
497 static char *line_buffer = 0;
498 static int line_size;
504 line_buffer = (char *) xmalloc (line_size);
508 while (*p && *p != '\n' && *p != '.' && *p != ',')
510 if (p - str > line_size - 1)
512 line_size = p - str + 1;
513 free ((PTR) line_buffer);
514 line_buffer = (char *) xmalloc (line_size);
516 strncpy (line_buffer, str, p - str);
517 line_buffer[p - str] = '\0';
518 if (islower (line_buffer[0]))
519 line_buffer[0] = toupper (line_buffer[0]);
521 ui_out_text (uiout, line_buffer);
523 fputs_filtered (line_buffer, stream);
528 * Implement a help command on command list LIST.
529 * RECURSE should be non-zero if this should be done recursively on
530 * all sublists of LIST.
531 * PREFIX is the prefix to print before each command name.
532 * STREAM is the stream upon which the output should be written.
534 * A non-negative class number to list only commands in that
536 * ALL_COMMANDS to list all commands in list.
537 * ALL_CLASSES to list all classes in list.
539 * Note that RECURSE will be active on *all* sublists, not just the
540 * ones selected by the criteria above (ie. the selection mechanism
541 * is at the low level, not the high-level).
544 help_cmd_list (list, class, prefix, recurse, stream)
545 struct cmd_list_element *list;
546 enum command_class class;
549 struct ui_file *stream;
551 register struct cmd_list_element *c;
553 for (c = list; c; c = c->next)
555 if (c->abbrev_flag == 0 &&
556 (class == all_commands
557 || (class == all_classes && c->function.cfunc == NULL)
558 || (class == c->class && c->function.cfunc != NULL)))
560 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
561 print_doc_line (stream, c->doc);
562 fputs_filtered ("\n", stream);
565 && c->prefixlist != 0
566 && c->abbrev_flag == 0)
567 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
572 /* Search the input clist for 'command'. Return the command if
573 found (or NULL if not), and return the number of commands
576 static struct cmd_list_element *
577 find_cmd (command, len, clist, ignore_help_classes, nfound)
580 struct cmd_list_element *clist;
581 int ignore_help_classes;
584 struct cmd_list_element *found, *c;
586 found = (struct cmd_list_element *) NULL;
588 for (c = clist; c; c = c->next)
589 if (!strncmp (command, c->name, len)
590 && (!ignore_help_classes || c->function.cfunc))
594 if (c->name[len] == '\0')
603 /* This routine takes a line of TEXT and a CLIST in which to start the
604 lookup. When it returns it will have incremented the text pointer past
605 the section of text it matched, set *RESULT_LIST to point to the list in
606 which the last word was matched, and will return a pointer to the cmd
607 list element which the text matches. It will return NULL if no match at
608 all was possible. It will return -1 (cast appropriately, ick) if ambigous
609 matches are possible; in this case *RESULT_LIST will be set to point to
610 the list in which there are ambiguous choices (and *TEXT will be set to
611 the ambiguous text string).
613 If the located command was an abbreviation, this routine returns the base
614 command of the abbreviation.
616 It does no error reporting whatsoever; control will always return
617 to the superior routine.
619 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
620 at the prefix_command (ie. the best match) *or* (special case) will be NULL
621 if no prefix command was ever found. For example, in the case of "info a",
622 "info" matches without ambiguity, but "a" could be "args" or "address", so
623 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
624 RESULT_LIST should not be interpeted as a pointer to the beginning of a
625 list; it simply points to a specific command. In the case of an ambiguous
626 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
627 "info t" can be "info types" or "info target"; upon return *TEXT has been
628 advanced past "info ").
630 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
631 affect the operation).
633 This routine does *not* modify the text pointed to by TEXT.
635 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
636 are actually help classes rather than commands (i.e. the function field of
637 the struct cmd_list_element is NULL). */
639 struct cmd_list_element *
640 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
642 struct cmd_list_element *clist, **result_list;
643 int ignore_help_classes;
646 int len, tmp, nfound;
647 struct cmd_list_element *found, *c;
649 while (**text == ' ' || **text == '\t')
652 /* Treating underscores as part of command words is important
653 so that "set args_foo()" doesn't get interpreted as
654 "set args _foo()". */
656 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
658 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
659 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
663 /* If nothing but whitespace, return 0. */
669 /* *text and p now bracket the first command word to lookup (and
670 it's length is len). We copy this into a local temporary */
673 command = (char *) alloca (len + 1);
674 for (tmp = 0; tmp < len; tmp++)
676 char x = (*text)[tmp];
684 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
687 ** We didn't find the command in the entered case, so lower case it
690 if (!found || nfound == 0)
692 for (tmp = 0; tmp < len; tmp++)
694 char x = command[tmp];
695 command[tmp] = isupper (x) ? tolower (x) : x;
697 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
700 /* If nothing matches, we have a simple failure. */
706 if (result_list != NULL)
707 /* Will be modified in calling routine
708 if we know what the prefix command is. */
710 return (struct cmd_list_element *) -1; /* Ambiguous. */
713 /* We've matched something on this list. Move text pointer forward. */
717 /* If this was an abbreviation, use the base command instead. */
719 if (found->cmd_pointer)
720 found = found->cmd_pointer;
722 /* If we found a prefix command, keep looking. */
724 if (found->prefixlist)
726 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
727 ignore_help_classes);
730 /* Didn't find anything; this is as far as we got. */
731 if (result_list != NULL)
732 *result_list = clist;
735 else if (c == (struct cmd_list_element *) -1)
737 /* We've gotten this far properly, but the next step
738 is ambiguous. We need to set the result list to the best
739 we've found (if an inferior hasn't already set it). */
740 if (result_list != NULL)
742 /* This used to say *result_list = *found->prefixlist
743 If that was correct, need to modify the documentation
744 at the top of this function to clarify what is supposed
746 *result_list = found;
757 if (result_list != NULL)
758 *result_list = clist;
763 /* All this hair to move the space to the front of cmdtype */
766 undef_cmd_error (cmdtype, q)
769 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
773 strlen (cmdtype) - 1,
777 /* Look up the contents of *LINE as a command in the command list LIST.
778 LIST is a chain of struct cmd_list_element's.
779 If it is found, return the struct cmd_list_element for that command
780 and update *LINE to point after the command name, at the first argument.
781 If not found, call error if ALLOW_UNKNOWN is zero
782 otherwise (or if error returns) return zero.
783 Call error if specified command is ambiguous,
784 unless ALLOW_UNKNOWN is negative.
785 CMDTYPE precedes the word "command" in the error message.
787 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
788 elements which are actually help classes rather than commands (i.e.
789 the function field of the struct cmd_list_element is 0). */
791 struct cmd_list_element *
792 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
794 struct cmd_list_element *list;
797 int ignore_help_classes;
799 struct cmd_list_element *last_list = 0;
800 struct cmd_list_element *c =
801 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
803 /* This is wrong for complete_command. */
804 char *ptr = (*line) + strlen (*line) - 1;
806 /* Clear off trailing whitespace. */
807 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
817 error ("Lack of needed %scommand", cmdtype);
822 while (isalnum (*p) || *p == '-')
825 q = (char *) alloca (p - *line + 1);
826 strncpy (q, *line, p - *line);
828 undef_cmd_error (cmdtype, q);
834 else if (c == (struct cmd_list_element *) -1)
836 /* Ambigous. Local values should be off prefixlist or called
838 int local_allow_unknown = (last_list ? last_list->allow_unknown :
840 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
841 struct cmd_list_element *local_list =
842 (last_list ? *(last_list->prefixlist) : list);
844 if (local_allow_unknown < 0)
847 return last_list; /* Found something. */
849 return 0; /* Found nothing. */
853 /* Report as error. */
858 ((*line)[amb_len] && (*line)[amb_len] != ' '
859 && (*line)[amb_len] != '\t');
864 for (c = local_list; c; c = c->next)
865 if (!strncmp (*line, c->name, amb_len))
867 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
870 strcat (ambbuf, ", ");
871 strcat (ambbuf, c->name);
875 strcat (ambbuf, "..");
879 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
886 /* We've got something. It may still not be what the caller
887 wants (if this command *needs* a subcommand). */
888 while (**line == ' ' || **line == '\t')
891 if (c->prefixlist && **line && !c->allow_unknown)
892 undef_cmd_error (c->prefixname, *line);
894 /* Seems to be what he wants. Return it. */
901 /* Look up the contents of *LINE as a command in the command list LIST.
902 LIST is a chain of struct cmd_list_element's.
903 If it is found, return the struct cmd_list_element for that command
904 and update *LINE to point after the command name, at the first argument.
905 If not found, call error if ALLOW_UNKNOWN is zero
906 otherwise (or if error returns) return zero.
907 Call error if specified command is ambiguous,
908 unless ALLOW_UNKNOWN is negative.
909 CMDTYPE precedes the word "command" in the error message. */
911 struct cmd_list_element *
912 lookup_cmd (line, list, cmdtype, allow_unknown)
914 struct cmd_list_element *list;
919 register struct cmd_list_element *c, *found;
925 /* Skip leading whitespace. */
927 while (**line == ' ' || **line == '\t')
930 /* Clear out trailing whitespace. */
932 p = *line + strlen (*line);
933 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
937 /* Find end of command name. */
940 while (*p == '-' || isalnum (*p))
943 /* Look up the command name.
944 If exact match, keep that.
945 Otherwise, take command abbreviated, if unique. Note that (in my
946 opinion) a null string does *not* indicate ambiguity; simply the
947 end of the argument. */
952 error ("Lack of needed %scommand", cmdtype);
956 /* Copy over to a local buffer, converting to lowercase on the way.
957 This is in case the command being parsed is a subcommand which
958 doesn't match anything, and that's ok. We want the original
959 untouched for the routine of the original command. */
961 processed_cmd = (char *) alloca (p - *line + 1);
962 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
964 char x = (*line)[cmd_len];
966 processed_cmd[cmd_len] = tolower (x);
968 processed_cmd[cmd_len] = x;
970 processed_cmd[cmd_len] = '\0';
972 /* Check all possibilities in the current command list. */
975 for (c = list; c; c = c->next)
977 if (!strncmp (processed_cmd, c->name, cmd_len))
981 if (c->name[cmd_len] == 0)
989 /* Report error for undefined command name. */
993 if (nfound > 1 && allow_unknown >= 0)
996 for (c = list; c; c = c->next)
997 if (!strncmp (processed_cmd, c->name, cmd_len))
999 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1001 if (strlen (ambbuf))
1002 strcat (ambbuf, ", ");
1003 strcat (ambbuf, c->name);
1007 strcat (ambbuf, "..");
1011 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1012 processed_cmd, ambbuf);
1014 else if (!allow_unknown)
1015 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1019 /* Skip whitespace before the argument. */
1021 while (*p == ' ' || *p == '\t')
1025 if (found->prefixlist && *p)
1027 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1028 found->allow_unknown);
1037 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1039 /* Return a vector of char pointers which point to the different
1040 possible completions in LIST of TEXT.
1042 WORD points in the same buffer as TEXT, and completions should be
1043 returned relative to this position. For example, suppose TEXT is "foo"
1044 and we want to complete to "foobar". If WORD is "oo", return
1045 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1048 complete_on_cmdlist (list, text, word)
1049 struct cmd_list_element *list;
1053 struct cmd_list_element *ptr;
1055 int sizeof_matchlist;
1057 int textlen = strlen (text);
1059 sizeof_matchlist = 10;
1060 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1063 for (ptr = list; ptr; ptr = ptr->next)
1064 if (!strncmp (ptr->name, text, textlen)
1065 && !ptr->abbrev_flag
1066 && (ptr->function.cfunc
1067 || ptr->prefixlist))
1069 if (matches == sizeof_matchlist)
1071 sizeof_matchlist *= 2;
1072 matchlist = (char **) xrealloc ((char *) matchlist,
1074 * sizeof (char *)));
1077 matchlist[matches] = (char *)
1078 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1080 strcpy (matchlist[matches], ptr->name);
1081 else if (word > text)
1083 /* Return some portion of ptr->name. */
1084 strcpy (matchlist[matches], ptr->name + (word - text));
1088 /* Return some of text plus ptr->name. */
1089 strncpy (matchlist[matches], word, text - word);
1090 matchlist[matches][text - word] = '\0';
1091 strcat (matchlist[matches], ptr->name);
1098 free ((PTR) matchlist);
1103 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1104 * sizeof (char *)));
1105 matchlist[matches] = (char *) 0;
1111 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1113 /* Return a vector of char pointers which point to the different
1114 possible completions in CMD of TEXT.
1116 WORD points in the same buffer as TEXT, and completions should be
1117 returned relative to this position. For example, suppose TEXT is "foo"
1118 and we want to complete to "foobar". If WORD is "oo", return
1119 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1122 complete_on_enum (enumlist, text, word)
1128 int sizeof_matchlist;
1130 int textlen = strlen (text);
1134 sizeof_matchlist = 10;
1135 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1138 for (i = 0; (name = enumlist[i]) != NULL; i++)
1139 if (strncmp (name, text, textlen) == 0)
1141 if (matches == sizeof_matchlist)
1143 sizeof_matchlist *= 2;
1144 matchlist = (char **) xrealloc ((char *) matchlist,
1146 * sizeof (char *)));
1149 matchlist[matches] = (char *)
1150 xmalloc (strlen (word) + strlen (name) + 1);
1152 strcpy (matchlist[matches], name);
1153 else if (word > text)
1155 /* Return some portion of name. */
1156 strcpy (matchlist[matches], name + (word - text));
1160 /* Return some of text plus name. */
1161 strncpy (matchlist[matches], word, text - word);
1162 matchlist[matches][text - word] = '\0';
1163 strcat (matchlist[matches], name);
1170 free ((PTR) matchlist);
1175 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1176 * sizeof (char *)));
1177 matchlist[matches] = (char *) 0;
1184 parse_binary_operation (arg)
1192 length = strlen (arg);
1194 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1197 if (!strncmp (arg, "on", length)
1198 || !strncmp (arg, "1", length)
1199 || !strncmp (arg, "yes", length))
1201 else if (!strncmp (arg, "off", length)
1202 || !strncmp (arg, "0", length)
1203 || !strncmp (arg, "no", length))
1207 error ("\"on\" or \"off\" expected.");
1212 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1213 of the argument, and FROM_TTY is nonzero if this command is being entered
1214 directly by the user (i.e. these are just like any other
1215 command). C is the command list element for the command. */
1217 do_setshow_command (arg, from_tty, c)
1220 struct cmd_list_element *c;
1222 if (c->type == set_cmd)
1224 switch (c->var_type)
1235 new = (char *) xmalloc (strlen (arg) + 2);
1238 while ((ch = *p++) != '\000')
1242 /* \ at end of argument is used after spaces
1243 so they won't be lost. */
1244 /* This is obsolete now that we no longer strip
1245 trailing whitespace and actually, the backslash
1246 didn't get here in my test, readline or
1247 something did something funky with a backslash
1248 right before a newline. */
1251 ch = parse_escape (&p);
1253 break; /* C loses */
1261 if (*(p - 1) != '\\')
1265 new = (char *) xrealloc (new, q - new);
1266 if (*(char **) c->var != NULL)
1267 free (*(char **) c->var);
1268 *(char **) c->var = new;
1271 case var_string_noescape:
1274 if (*(char **) c->var != NULL)
1275 free (*(char **) c->var);
1276 *(char **) c->var = savestring (arg, strlen (arg));
1280 error_no_arg ("filename to set it to.");
1281 if (*(char **) c->var != NULL)
1282 free (*(char **) c->var);
1283 *(char **) c->var = tilde_expand (arg);
1286 *(int *) c->var = parse_binary_operation (arg);
1290 error_no_arg ("integer to set it to.");
1291 *(unsigned int *) c->var = parse_and_eval_address (arg);
1292 if (*(unsigned int *) c->var == 0)
1293 *(unsigned int *) c->var = UINT_MAX;
1299 error_no_arg ("integer to set it to.");
1300 val = parse_and_eval_address (arg);
1302 *(int *) c->var = INT_MAX;
1303 else if (val >= INT_MAX)
1304 error ("integer %u out of range", val);
1306 *(int *) c->var = val;
1311 error_no_arg ("integer to set it to.");
1312 *(int *) c->var = parse_and_eval_address (arg);
1322 /* if no argument was supplied, print an informative error message */
1326 strcpy (msg, "Requires an argument. Valid arguments are ");
1327 for (i = 0; c->enums[i]; i++)
1331 strcat (msg, c->enums[i]);
1337 p = strchr (arg, ' ');
1345 for (i = 0; c->enums[i]; i++)
1346 if (strncmp (arg, c->enums[i], len) == 0)
1348 match = c->enums[i];
1353 error ("Undefined item: \"%s\".", arg);
1356 error ("Ambiguous item \"%s\".", arg);
1358 *(char **) c->var = match;
1362 error ("gdb internal error: bad var_type in do_setshow_command");
1365 else if (c->type == show_cmd)
1368 struct cleanup *old_chain;
1369 struct ui_stream *stb;
1372 stb = ui_out_stream_new (uiout);
1373 old_chain = make_cleanup ((make_cleanup_func) ui_out_stream_delete, stb);
1376 /* Print doc minus "show" at start. */
1377 print_doc_line (gdb_stdout, c->doc + 5);
1380 ui_out_text (uiout, " is ");
1381 ui_out_wrap_hint (uiout, " ");
1383 switch (c->var_type)
1389 if (*(unsigned char **) c->var)
1390 fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
1394 case var_string_noescape:
1397 if (*(char **) c->var)
1398 fputs_filtered (*(char **) c->var, stb->stream);
1402 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
1405 if (*(unsigned int *) c->var == UINT_MAX)
1407 fputs_filtered ("unlimited", stb->stream);
1410 /* else fall through */
1412 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
1415 if (*(int *) c->var == INT_MAX)
1417 fputs_filtered ("unlimited", stb->stream);
1420 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
1424 error ("gdb internal error: bad var_type in do_setshow_command");
1427 ui_out_text (uiout, "\"");
1428 ui_out_field_stream (uiout, "value", stb);
1430 ui_out_text (uiout, "\"");
1431 ui_out_text (uiout, ".\n");
1432 do_cleanups (old_chain);
1434 fputs_filtered (" is ", gdb_stdout);
1436 switch (c->var_type)
1440 fputs_filtered ("\"", gdb_stdout);
1441 if (*(unsigned char **) c->var)
1442 fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
1443 fputs_filtered ("\"", gdb_stdout);
1446 case var_string_noescape:
1449 fputs_filtered ("\"", gdb_stdout);
1450 if (*(char **) c->var)
1451 fputs_filtered (*(char **) c->var, gdb_stdout);
1452 fputs_filtered ("\"", gdb_stdout);
1455 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1458 if (*(unsigned int *) c->var == UINT_MAX)
1460 fputs_filtered ("unlimited", gdb_stdout);
1463 /* else fall through */
1465 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1468 if (*(int *) c->var == INT_MAX)
1470 fputs_filtered ("unlimited", gdb_stdout);
1473 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1477 error ("gdb internal error: bad var_type in do_setshow_command");
1479 fputs_filtered (".\n", gdb_stdout);
1483 error ("gdb internal error: bad cmd_type in do_setshow_command");
1484 (*c->function.sfunc) (NULL, from_tty, c);
1485 if (c->type == set_cmd && set_hook)
1489 /* Show all the settings in a list of show commands. */
1492 cmd_show_list (list, from_tty, prefix)
1493 struct cmd_list_element *list;
1498 ui_out_list_begin (uiout, "showlist");
1500 for (; list != NULL; list = list->next)
1502 /* If we find a prefix, run its list, prefixing our output by its
1503 prefix (with "show " skipped). */
1505 if (list->prefixlist && !list->abbrev_flag)
1507 ui_out_list_begin (uiout, "optionlist");
1508 ui_out_field_string (uiout, "prefix", list->prefixname + 5);
1509 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1510 ui_out_list_end (uiout);
1512 if (list->type == show_cmd)
1514 ui_out_list_begin (uiout, "option");
1515 ui_out_text (uiout, prefix);
1516 ui_out_field_string (uiout, "name", list->name);
1517 ui_out_text (uiout, ": ");
1518 do_setshow_command ((char *) NULL, from_tty, list);
1519 ui_out_list_end (uiout);
1522 if (list->prefixlist && !list->abbrev_flag)
1523 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1524 if (list->type == show_cmd)
1526 fputs_filtered (prefix, gdb_stdout);
1527 fputs_filtered (list->name, gdb_stdout);
1528 fputs_filtered (": ", gdb_stdout);
1529 do_setshow_command ((char *) NULL, from_tty, list);
1534 ui_out_list_end (uiout);
1540 shell_escape (arg, from_tty)
1545 /* If ARG is NULL, they want an inferior shell, but `system' just
1546 reports if the shell is available when passed a NULL arg. */
1547 int rc = system (arg ? arg : "");
1550 arg = "inferior shell";
1554 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1555 safe_strerror (errno));
1556 gdb_flush (gdb_stderr);
1560 fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1561 gdb_flush (gdb_stderr);
1564 /* Make sure to return to the directory GDB thinks it is, in case the
1565 shell command we just ran changed it. */
1566 chdir (current_directory);
1568 #else /* Can fork. */
1569 int rc, status, pid;
1570 char *p, *user_shell;
1572 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1573 user_shell = "/bin/sh";
1575 /* Get the name of the shell for arg0 */
1576 if ((p = strrchr (user_shell, '/')) == NULL)
1579 p++; /* Get past '/' */
1581 if ((pid = fork ()) == 0)
1584 execl (user_shell, p, 0);
1586 execl (user_shell, p, "-c", arg, 0);
1588 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1589 safe_strerror (errno));
1590 gdb_flush (gdb_stderr);
1595 while ((rc = wait (&status)) != pid && rc != -1)
1598 error ("Fork failed");
1599 #endif /* Can fork. */
1603 make_command (arg, from_tty)
1613 p = xmalloc (sizeof ("make ") + strlen (arg));
1614 strcpy (p, "make ");
1615 strcpy (p + sizeof ("make ") - 1, arg);
1618 shell_escape (p, from_tty);
1622 show_user_1 (c, stream)
1623 struct cmd_list_element *c;
1624 struct ui_file *stream;
1626 register struct command_line *cmdlines;
1628 cmdlines = c->user_commands;
1631 fputs_filtered ("User command ", stream);
1632 fputs_filtered (c->name, stream);
1633 fputs_filtered (":\n", stream);
1636 print_command_lines (uiout, cmdlines, 1);
1637 fputs_filtered ("\n", stream);
1641 print_command_line (cmdlines, 4, stream);
1642 cmdlines = cmdlines->next;
1644 fputs_filtered ("\n", stream);
1650 show_user (args, from_tty)
1654 struct cmd_list_element *c;
1655 extern struct cmd_list_element *cmdlist;
1659 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1660 if (c->class != class_user)
1661 error ("Not a user command.");
1662 show_user_1 (c, gdb_stdout);
1666 for (c = cmdlist; c; c = c->next)
1668 if (c->class == class_user)
1669 show_user_1 (c, gdb_stdout);
1675 _initialize_command ()
1677 add_com ("shell", class_support, shell_escape,
1678 "Execute the rest of the line as a shell command. \n\
1679 With no arguments, run an inferior shell.");
1682 add_com_alias ("!", "shell", class_support, 0);
1684 add_com ("make", class_support, make_command,
1685 "Run the ``make'' program using the rest of the line as arguments.");
1686 add_cmd ("user", no_class, show_user,
1687 "Show definitions of user defined commands.\n\
1688 Argument is the name of the user defined command.\n\
1689 With no argument, show definitions of all user defined commands.", &showlist);